Add Web Dashboard with multi-device control and callback hooks

Features:
- Web Dashboard: FastAPI-based dashboard with Vue.js frontend
  - Multi-device support (ADB, HDC, iOS)
  - Real-time WebSocket updates for task progress
  - Device management with status tracking
  - Task queue with execution controls (start/stop/re-execute)
  - Detailed task information display (thinking, actions, completion messages)
  - Screenshot viewing per device
  - LAN deployment support with configurable CORS

- Callback Hooks: Interrupt and modify task execution
  - step_callback: Called after each step with StepResult
  - before_action_callback: Called before executing action
  - Support for task interruption and dynamic task switching
  - Example scripts demonstrating callback usage

- Configuration: Environment-based configuration
  - .env file support for all settings
  - .env.example template with documentation
  - Model API configuration (base URL, model name, API key)
  - Dashboard configuration (host, port, CORS, device type)
  - Phone agent configuration (delays, max steps, language)

Technical improvements:
- Fixed forward reference issue with StepResult
- Added package exports for callback types and configs
- Enhanced dependencies with FastAPI, WebSocket support
- Thread-safe task execution with device locking
- Async WebSocket broadcasting from sync thread pool

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
let5sne.win10
2026-01-09 02:20:06 +08:00
parent 9fe189a8f8
commit 3552df23d6
31 changed files with 4221 additions and 8 deletions

View File

@@ -0,0 +1,539 @@
/* AutoGLM Dashboard Styles */
:root {
--primary-color: #6366f1;
--primary-hover: #4f46e5;
--success-color: #10b981;
--warning-color: #f59e0b;
--danger-color: #ef4444;
--bg-color: #0f172a;
--card-bg: #1e293b;
--border-color: #334155;
--text-primary: #f1f5f9;
--text-secondary: #94a3b8;
--shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.3);
--shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.4);
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
background-color: var(--bg-color);
color: var(--text-primary);
line-height: 1.6;
min-height: 100vh;
}
/* Header */
.header {
background-color: var(--card-bg);
border-bottom: 1px solid var(--border-color);
padding: 1rem 2rem;
display: flex;
justify-content: space-between;
align-items: center;
position: sticky;
top: 0;
z-index: 100;
box-shadow: var(--shadow);
}
.header-content {
display: flex;
align-items: center;
gap: 2rem;
}
.header h1 {
font-size: 1.5rem;
font-weight: 600;
color: var(--text-primary);
}
.stats {
display: flex;
gap: 1.5rem;
}
.stat {
display: flex;
align-items: center;
gap: 0.5rem;
font-size: 0.875rem;
color: var(--text-secondary);
}
.stat svg {
opacity: 0.7;
}
.ws-status {
color: var(--danger-color);
}
.ws-status.connected {
color: var(--success-color);
}
.header-actions {
display: flex;
gap: 0.75rem;
}
/* Main Content */
.main-content {
padding: 2rem;
max-width: 1600px;
margin: 0 auto;
}
.main-content h2 {
font-size: 1.25rem;
font-weight: 600;
margin-bottom: 1rem;
color: var(--text-primary);
}
/* Device Grid */
.device-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(350px, 1fr));
gap: 1.5rem;
margin-bottom: 3rem;
}
.device-card {
background-color: var(--card-bg);
border: 1px solid var(--border-color);
border-radius: 12px;
padding: 1.25rem;
transition: box-shadow 0.2s, border-color 0.2s;
}
.device-card:hover {
box-shadow: var(--shadow-lg);
}
.device-card.busy {
border-color: var(--warning-color);
}
.device-card.offline {
opacity: 0.6;
}
.device-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 1rem;
}
.device-header h3 {
font-size: 1rem;
font-weight: 600;
color: var(--text-primary);
word-break: break-all;
}
.status-badge {
padding: 0.25rem 0.75rem;
border-radius: 9999px;
font-size: 0.75rem;
font-weight: 600;
text-transform: uppercase;
}
.status-badge.online {
background-color: rgba(16, 185, 129, 0.2);
color: var(--success-color);
}
.status-badge.offline {
background-color: rgba(239, 68, 68, 0.2);
color: var(--danger-color);
}
.status-badge.busy {
background-color: rgba(245, 158, 11, 0.2);
color: var(--warning-color);
}
.device-info {
margin-bottom: 1rem;
}
.device-info p {
font-size: 0.875rem;
color: var(--text-secondary);
margin-bottom: 0.25rem;
display: flex;
align-items: center;
gap: 0.5rem;
}
.device-info p svg {
flex-shrink: 0;
}
.screenshot {
margin-bottom: 1rem;
border-radius: 8px;
overflow: hidden;
background-color: #000;
aspect-ratio: 9/16;
}
.screenshot img {
width: 100%;
height: 100%;
object-fit: contain;
}
.device-actions {
display: flex;
gap: 0.5rem;
}
.device-actions input {
flex: 1;
padding: 0.625rem 0.875rem;
background-color: var(--bg-color);
border: 1px solid var(--border-color);
border-radius: 8px;
color: var(--text-primary);
font-size: 0.875rem;
}
.device-actions input:focus {
outline: none;
border-color: var(--primary-color);
}
.device-actions input:disabled {
opacity: 0.5;
cursor: not-allowed;
}
/* Task List */
.task-list {
display: flex;
flex-direction: column;
gap: 0.75rem;
}
.task-item {
background-color: var(--card-bg);
border: 1px solid var(--border-color);
border-radius: 8px;
padding: 1rem;
display: flex;
justify-content: space-between;
align-items: start;
gap: 1rem;
transition: border-color 0.2s;
}
.task-item.active {
border-color: var(--primary-color);
box-shadow: 0 0 0 1px var(--primary-color);
}
.task-info {
flex: 1;
}
.task-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 0.5rem;
}
.task-id {
font-size: 0.75rem;
color: var(--text-secondary);
font-family: monospace;
}
.task-status {
padding: 0.125rem 0.5rem;
border-radius: 4px;
font-size: 0.7rem;
font-weight: 600;
text-transform: uppercase;
}
.task-status.running {
background-color: rgba(99, 102, 241, 0.2);
color: var(--primary-color);
}
.task-status.completed {
background-color: rgba(16, 185, 129, 0.2);
color: var(--success-color);
}
.task-status.failed {
background-color: rgba(239, 68, 68, 0.2);
color: var(--danger-color);
}
.task-status.stopped {
background-color: rgba(148, 163, 184, 0.2);
color: var(--text-secondary);
}
.task-description {
font-size: 0.95rem;
color: var(--text-primary);
margin-bottom: 0.5rem;
}
.task-meta {
display: flex;
gap: 1rem;
font-size: 0.8rem;
color: var(--text-secondary);
}
.task-progress {
height: 4px;
background-color: var(--bg-color);
border-radius: 2px;
overflow: hidden;
margin-top: 0.5rem;
}
.progress-bar {
height: 100%;
background-color: var(--primary-color);
transition: width 0.3s ease;
}
.task-thinking {
display: flex;
align-items: start;
gap: 0.5rem;
margin-top: 0.5rem;
padding: 0.5rem;
background-color: var(--bg-color);
border-radius: 6px;
font-size: 0.85rem;
color: var(--text-secondary);
}
.task-thinking svg {
flex-shrink: 0;
margin-top: 2px;
}
.task-action {
display: flex;
align-items: center;
gap: 0.5rem;
margin-top: 0.5rem;
padding: 0.5rem;
background-color: rgba(99, 102, 241, 0.1);
border-radius: 6px;
font-size: 0.85rem;
color: var(--primary-color);
}
.task-action svg {
flex-shrink: 0;
}
.task-action strong {
color: var(--text-primary);
}
.task-message {
display: flex;
align-items: start;
gap: 0.5rem;
margin-top: 0.5rem;
padding: 0.5rem;
border-radius: 6px;
font-size: 0.85rem;
line-height: 1.4;
}
.task-message svg {
flex-shrink: 0;
margin-top: 2px;
}
.task-message.completed {
background-color: rgba(16, 185, 129, 0.1);
color: var(--success-color);
}
.task-message.failed {
background-color: rgba(239, 68, 68, 0.1);
color: var(--danger-color);
}
.task-message.stopped {
background-color: rgba(148, 163, 184, 0.1);
color: var(--text-secondary);
}
.task-actions {
flex-shrink: 0;
}
/* Buttons */
.btn {
padding: 0.625rem 1rem;
border: none;
border-radius: 8px;
font-size: 0.875rem;
font-weight: 500;
cursor: pointer;
transition: all 0.2s;
display: inline-flex;
align-items: center;
gap: 0.5rem;
}
.btn:disabled {
opacity: 0.5;
cursor: not-allowed;
}
.btn-primary {
background-color: var(--primary-color);
color: white;
}
.btn-primary:hover:not(:disabled) {
background-color: var(--primary-hover);
}
.btn-secondary {
background-color: var(--card-bg);
color: var(--text-primary);
border: 1px solid var(--border-color);
}
.btn-secondary:hover:not(:disabled) {
background-color: var(--border-color);
}
.btn-danger {
background-color: var(--danger-color);
color: white;
}
.btn-danger:hover:not(:disabled) {
background-color: #dc2626;
}
.btn-sm {
padding: 0.5rem 0.75rem;
font-size: 0.8rem;
}
.spinning {
animation: spin 1s linear infinite;
}
@keyframes spin {
from { transform: rotate(0deg); }
to { transform: rotate(360deg); }
}
/* Empty State */
.empty-state {
text-align: center;
padding: 4rem 2rem;
color: var(--text-secondary);
}
.empty-state svg {
margin-bottom: 1rem;
opacity: 0.3;
}
.empty-state p {
margin-bottom: 1.5rem;
}
.empty-state .hint {
font-size: 0.875rem;
opacity: 0.7;
}
/* Toast */
.toast-container {
position: fixed;
bottom: 2rem;
right: 2rem;
z-index: 1000;
display: flex;
flex-direction: column;
gap: 0.5rem;
}
.toast {
padding: 0.75rem 1rem;
border-radius: 8px;
font-size: 0.875rem;
box-shadow: var(--shadow-lg);
animation: slideIn 0.3s ease;
}
.toast.success {
background-color: var(--success-color);
color: white;
}
.toast.error {
background-color: var(--danger-color);
color: white;
}
.toast.info {
background-color: var(--primary-color);
color: white;
}
@keyframes slideIn {
from {
transform: translateX(100%);
opacity: 0;
}
to {
transform: translateX(0);
opacity: 1;
}
}
/* Responsive */
@media (max-width: 768px) {
.header {
flex-direction: column;
gap: 1rem;
padding: 1rem;
}
.header-content {
flex-direction: column;
gap: 0.75rem;
align-items: start;
}
.device-grid {
grid-template-columns: 1fr;
}
.main-content {
padding: 1rem;
}
}

219
dashboard/static/index.html Normal file
View File

@@ -0,0 +1,219 @@
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>AutoGLM Dashboard</title>
<!-- Vue.js 3 -->
<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
<!-- Axios for API requests -->
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<!-- CSS -->
<link rel="stylesheet" href="/static/css/dashboard.css">
</head>
<body>
<div id="app">
<!-- Header -->
<header class="header">
<div class="header-content">
<h1>AutoGLM Dashboard</h1>
<div class="stats">
<span class="stat" title="Connected Devices">
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<rect x="5" y="2" width="14" height="20" rx="2" ry="2"></rect>
<line x1="12" y1="18" x2="12" y2="18"></line>
</svg>
{{ connectedDeviceCount }} / {{ devices.length }}
</span>
<span class="stat" title="Active Tasks">
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<polyline points="22 12 18 12 15 21 9 3 6 12 2 12"></polyline>
</svg>
{{ activeTasks.length }}
</span>
<span class="stat ws-status" :class="{ connected: wsConnected }" title="WebSocket">
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<circle cx="12" cy="12" r="10"></circle>
<line x1="2" y1="12" x2="22" y2="12"></line>
<path d="M12 2a15.3 15.3 0 0 1 4 10 15.3 15.3 0 0 1-4 10 15.3 15.3 0 0 1-4-10 15.3 15.3 0 0 1 4-10z"></path>
</svg>
</span>
</div>
</div>
<div class="header-actions">
<button @click="refreshDevices" class="btn btn-secondary" :disabled="refreshing">
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" :class="{ spinning: refreshing }">
<polyline points="23 4 23 10 17 10"></polyline>
<polyline points="1 20 1 14 7 14"></polyline>
<path d="M3.51 9a9 9 0 0 1 14.85-3.36L23 10M1 14l4.64 4.36A9 9 0 0 0 20.49 15"></path>
</svg>
Refresh
</button>
</div>
</header>
<!-- Main Content -->
<main class="main-content">
<!-- Device Grid -->
<section class="devices-section">
<h2>Devices</h2>
<div class="device-grid" v-if="devices.length > 0">
<div
class="device-card"
v-for="device in devices"
:key="device.device_id"
:class="{ busy: device.status === 'busy', offline: !device.is_connected }"
>
<div class="device-header">
<h3>{{ device.device_id }}</h3>
<span class="status-badge" :class="device.status">
{{ device.status }}
</span>
</div>
<div class="device-info">
<p v-if="device.model">{{ device.model }}</p>
<p v-if="device.android_version" class="version">
<svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<path d="M12 2L2 7l10 5 10-5-10-5z"></path>
<path d="M2 17l10 5 10-5"></path>
<path d="M2 12l10 5 10-5"></path>
</svg>
{{ device.android_version }}
</p>
<p v-if="device.current_app" class="app">
<svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<rect x="3" y="3" width="18" height="18" rx="2"></rect>
</svg>
{{ formatAppName(device.current_app) }}
</p>
</div>
<div class="screenshot" v-if="device.screenshot">
<img :src="'data:image/png;base64,' + device.screenshot" alt="Screen">
</div>
<div class="device-actions">
<input
v-model="device.taskInput"
type="text"
placeholder="Enter task..."
@keyup.enter="executeTask(device)"
:disabled="device.status === 'busy' || !device.is_connected"
>
<button
@click="executeTask(device)"
class="btn btn-primary"
:disabled="device.status === 'busy' || !device.is_connected || !device.taskInput"
>
{{ device.status === 'busy' ? 'Running...' : 'Execute' }}
</button>
</div>
</div>
</div>
<div class="empty-state" v-else>
<svg width="64" height="64" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1">
<rect x="5" y="2" width="14" height="20" rx="2" ry="2"></rect>
<line x1="12" y1="18" x2="12" y2="18"></line>
</svg>
<p>No devices found</p>
<button @click="refreshDevices" class="btn btn-secondary">Scan for Devices</button>
</div>
</section>
<!-- Task Queue -->
<section class="tasks-section">
<h2>Task Queue</h2>
<div class="task-list" v-if="tasks.length > 0">
<div
class="task-item"
v-for="task in tasks"
:key="task.task_id"
:class="{ active: task.status === 'running' }"
>
<div class="task-info">
<div class="task-header">
<span class="task-id">{{ task.task_id }}</span>
<span class="task-status" :class="task.status">{{ task.status }}</span>
</div>
<p class="task-description">{{ task.task }}</p>
<div class="task-meta">
<span>Device: {{ task.device_id }}</span>
<span>Step: {{ task.current_step }}/{{ task.max_steps }}</span>
</div>
<div class="task-progress" v-if="task.status === 'running'">
<div class="progress-bar" :style="{ width: (task.current_step / task.max_steps * 100) + '%' }"></div>
</div>
<!-- Current Action -->
<div class="task-action" v-if="task.current_action && task.status === 'running'">
<svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<polyline points="22 12 18 12 15 21 9 3 6 12 2 12"></polyline>
</svg>
<strong>{{ task.current_action.action }}</strong>
<span v-if="task.current_action.element"> on {{ task.current_action.element }}</span>
<span v-if="task.current_action.text">: "{{ task.current_action.text }}"</span>
</div>
<!-- Thinking -->
<div class="task-thinking" v-if="task.thinking">
<svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<circle cx="12" cy="12" r="10"></circle>
<line x1="12" y1="16" x2="12" y2="12"></line>
<line x1="12" y1="8" x2="12.01" y2="8"></line>
</svg>
{{ task.thinking }}
</div>
<!-- Completion Message -->
<div class="task-message" v-if="task.completion_message && (task.status === 'completed' || task.status === 'failed' || task.status === 'stopped')">
<svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<path v-if="task.status === 'completed'" d="M22 11.08V12a10 10 0 1 1-5.93-9.14"></path>
<polyline v-if="task.status === 'completed'" points="22 4 12 14.01 9 11.01"></polyline>
<circle v-if="task.status === 'failed'" cx="12" cy="12" r="10"></circle>
<line v-if="task.status === 'failed'" x1="15" y1="9" x2="9" y2="15"></line>
<line v-if="task.status === 'failed'" x1="9" y1="9" x2="15" y2="15"></line>
</svg>
{{ task.completion_message }}
</div>
</div>
<div class="task-actions" v-if="task.status === 'running'">
<button @click="stopTask(task)" class="btn btn-danger btn-sm">
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<rect x="6" y="6" width="12" height="12"></rect>
</svg>
Stop
</button>
</div>
<div class="task-actions" v-else>
<button @click="reExecuteTask(task)" class="btn btn-secondary btn-sm">
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<polyline points="23 4 23 10 17 10"></polyline>
<polyline points="1 20 1 14 7 14"></polyline>
<path d="M3.51 9a9 9 0 0 1 14.85-3.36L23 10M1 14l4.64 4.36A9 9 0 0 0 20.49 15"></path>
</svg>
Re-execute
</button>
</div>
</div>
</div>
<div class="empty-state" v-else>
<svg width="64" height="64" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1">
<polyline points="22 12 18 12 15 21 9 3 6 12 2 12"></polyline>
</svg>
<p>No tasks yet</p>
<p class="hint">Enter a task above to get started</p>
</div>
</section>
</main>
<!-- Toast notifications -->
<div class="toast-container">
<div
class="toast"
v-for="toast in toasts"
:key="toast.id"
:class="toast.type"
>
{{ toast.message }}
</div>
</div>
</div>
<script src="/static/js/dashboard.js"></script>
</body>
</html>

View File

@@ -0,0 +1,404 @@
/**
* AutoGLM Dashboard - Vue.js Application
*/
const { createApp } = Vue;
createApp({
data() {
return {
devices: [],
tasks: [],
ws: null,
wsConnected: false,
refreshing: false,
toasts: [],
toastIdCounter: 0,
reconnectAttempts: 0,
maxReconnectAttempts: 5,
reconnectDelay: 2000,
};
},
computed: {
activeTasks() {
return this.tasks.filter(t => t.status === 'running');
},
connectedDeviceCount() {
return this.devices.filter(d => d.is_connected).length;
},
},
methods: {
/* API Methods */
async loadDevices() {
try {
const response = await axios.get('/api/devices');
this.devices = response.data.map(d => ({
...d,
taskInput: '',
screenshot: null,
}));
} catch (error) {
this.showToast('Failed to load devices', 'error');
console.error('Error loading devices:', error);
}
},
async loadTasks() {
try {
const response = await axios.get('/api/tasks');
this.tasks = response.data;
} catch (error) {
console.error('Error loading tasks:', error);
}
},
async refreshDevices() {
this.refreshing = true;
try {
await axios.get('/api/devices/refresh');
await this.loadDevices();
this.showToast('Devices refreshed', 'success');
} catch (error) {
this.showToast('Failed to refresh devices', 'error');
} finally {
this.refreshing = false;
}
},
async executeTask(device) {
if (!device.taskInput || !device.taskInput.trim()) {
return;
}
const task = device.taskInput.trim();
device.taskInput = '';
try {
const response = await axios.post('/api/tasks/execute', {
device_id: device.device_id,
task: task,
max_steps: 100,
lang: 'cn',
// Backend will use config from environment
});
this.showToast(`Task started: ${task}`, 'success');
await this.loadTasks();
} catch (error) {
this.showToast('Failed to start task', 'error');
console.error('Error executing task:', error);
}
},
async stopTask(task) {
try {
await axios.post(`/api/tasks/${task.task_id}/stop`);
this.showToast('Stopping task...', 'info');
} catch (error) {
this.showToast('Failed to stop task', 'error');
}
},
async reExecuteTask(task) {
const device = this.devices.find(d => d.device_id === task.device_id);
if (!device) {
this.showToast('Device not found', 'error');
return;
}
if (!device.is_connected) {
this.showToast('Device is not connected', 'error');
return;
}
if (device.status === 'busy') {
this.showToast('Device is busy', 'error');
return;
}
try {
const response = await axios.post('/api/tasks/execute', {
device_id: task.device_id,
task: task.task,
max_steps: task.max_steps || 100,
lang: 'cn',
});
this.showToast(`Task re-executed: ${task.task}`, 'success');
await this.loadTasks();
} catch (error) {
this.showToast('Failed to re-execute task', 'error');
console.error('Error re-executing task:', error);
}
},
async captureScreenshot(deviceId) {
try {
const response = await axios.get(`/api/devices/${deviceId}/screenshot`);
const device = this.devices.find(d => d.device_id === deviceId);
if (device) {
device.screenshot = response.data.screenshot;
}
} catch (error) {
console.error('Error capturing screenshot:', error);
}
},
/* WebSocket Methods */
connectWebSocket() {
const protocol = location.protocol === 'https:' ? 'wss:' : 'ws:';
const wsUrl = `${protocol}//${location.host}/ws`;
this.ws = new WebSocket(wsUrl);
this.ws.onopen = () => {
this.wsConnected = true;
this.reconnectAttempts = 0;
console.log('WebSocket connected');
// Subscribe to all devices updates
this.sendWebSocketMessage({
type: 'subscribe',
device_id: '*', // Subscribe to all devices
});
};
this.ws.onmessage = (event) => {
try {
const msg = JSON.parse(event.data);
this.handleWSMessage(msg);
} catch (error) {
console.error('Error parsing WebSocket message:', error);
}
};
this.ws.onclose = () => {
this.wsConnected = false;
console.log('WebSocket disconnected');
// Attempt to reconnect
if (this.reconnectAttempts < this.maxReconnectAttempts) {
this.reconnectAttempts++;
console.log(`Reconnecting... (${this.reconnectAttempts}/${this.maxReconnectAttempts})`);
setTimeout(() => this.connectWebSocket(), this.reconnectDelay);
} else {
this.showToast('WebSocket connection lost', 'error');
}
};
this.ws.onerror = (error) => {
console.error('WebSocket error:', error);
};
},
sendWebSocketMessage(data) {
if (this.ws && this.ws.readyState === WebSocket.OPEN) {
this.ws.send(JSON.stringify(data));
}
},
handleWSMessage(msg) {
switch (msg.type) {
case 'device_update':
this.updateDevice(msg.data);
break;
case 'task_started':
this.handleTaskStarted(msg.data);
break;
case 'task_step':
this.handleTaskStep(msg.data);
break;
case 'task_completed':
this.handleTaskCompleted(msg.data);
break;
case 'task_failed':
this.handleTaskFailed(msg.data);
break;
case 'task_stopped':
this.handleTaskStopped(msg.data);
break;
case 'screenshot':
this.handleScreenshot(msg.data);
break;
case 'error':
this.showToast(msg.data.error || 'An error occurred', 'error');
break;
case 'pong':
// Ping response, ignore
break;
default:
console.log('Unknown WebSocket message type:', msg.type);
}
},
/* Message Handlers */
updateDevice(data) {
const device = this.devices.find(d => d.device_id === data.device_id);
if (device) {
Object.assign(device, {
status: data.status,
is_connected: data.is_connected,
model: data.model,
current_app: data.current_app,
});
}
},
handleTaskStarted(data) {
this.tasks.unshift({
task_id: data.task_id,
device_id: data.device_id,
task: data.task,
status: 'running',
current_step: 0,
max_steps: data.max_steps || 100,
current_action: null,
thinking: null,
started_at: new Date(),
});
// Update device status
const device = this.devices.find(d => d.device_id === data.device_id);
if (device) {
device.status = 'busy';
}
},
handleTaskStep(data) {
const task = this.tasks.find(t => t.task_id === data.task_id);
if (task) {
task.current_step = data.step;
task.current_action = data.action;
task.thinking = data.thinking;
// Store the completion message
if (data.message && data.finished) {
task.completion_message = data.message;
}
if (data.finished) {
task.status = data.success ? 'completed' : 'failed';
this.releaseDevice(data.device_id);
}
}
},
handleTaskCompleted(data) {
const task = this.tasks.find(t => t.task_id === data.task_id);
if (task) {
task.status = 'completed';
task.finished_at = new Date();
// Store completion message
if (data.message) {
task.completion_message = data.message;
}
this.releaseDevice(data.device_id);
this.showToast(`Task completed: ${task.task}`, 'success');
}
},
handleTaskFailed(data) {
const task = this.tasks.find(t => t.task_id === data.task_id);
if (task) {
task.status = 'failed';
task.error = data.error;
task.finished_at = new Date();
this.releaseDevice(data.device_id);
this.showToast(`Task failed: ${data.error}`, 'error');
}
},
handleTaskStopped(data) {
const task = this.tasks.find(t => t.task_id === data.task_id);
if (task) {
task.status = 'stopped';
task.finished_at = new Date();
this.releaseDevice(data.device_id);
this.showToast('Task stopped', 'info');
}
},
handleScreenshot(data) {
const device = this.devices.find(d => d.device_id === data.device_id);
if (device) {
device.screenshot = data.screenshot;
}
},
releaseDevice(deviceId) {
const device = this.devices.find(d => d.device_id === deviceId);
if (device && device.status === 'busy') {
device.status = 'online';
}
},
/* Utility Methods */
formatAppName(packageName) {
if (!packageName) return '';
// Format package name for display
const parts = packageName.split('.');
return parts[parts.length - 1] || packageName;
},
showToast(message, type = 'info') {
const id = this.toastIdCounter++;
this.toasts.push({ id, message, type });
// Auto-remove after 3 seconds
setTimeout(() => {
this.toasts = this.toasts.filter(t => t.id !== id);
}, 3000);
},
/* Heartbeat */
startHeartbeat() {
setInterval(() => {
if (this.ws && this.ws.readyState === WebSocket.OPEN) {
this.sendWebSocketMessage({ type: 'ping' });
}
}, 30000); // Ping every 30 seconds
},
},
mounted() {
// Load initial data
this.loadDevices();
this.loadTasks();
// Connect WebSocket
this.connectWebSocket();
// Start heartbeat
this.startHeartbeat();
// Refresh devices periodically
setInterval(() => {
if (!this.refreshing) {
this.loadDevices();
}
}, 10000); // Every 10 seconds
// Refresh tasks periodically
setInterval(() => {
this.loadTasks();
}, 5000); // Every 5 seconds
},
beforeUnmount() {
// Close WebSocket on unmount
if (this.ws) {
this.ws.close();
}
},
}).mount('#app');