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:
37
dashboard/models/__init__.py
Normal file
37
dashboard/models/__init__.py
Normal file
@@ -0,0 +1,37 @@
|
||||
"""
|
||||
Data models for the dashboard API.
|
||||
|
||||
Includes Pydantic schemas for devices, tasks, and WebSocket messages.
|
||||
"""
|
||||
|
||||
from dashboard.models.device import (
|
||||
DeviceType,
|
||||
DeviceStatus,
|
||||
DeviceSchema,
|
||||
DeviceInfo,
|
||||
)
|
||||
from dashboard.models.task import (
|
||||
TaskStatus,
|
||||
TaskRequest,
|
||||
TaskSchema,
|
||||
TaskCreateRequest,
|
||||
)
|
||||
from dashboard.models.ws_messages import (
|
||||
WSMessageType,
|
||||
WSMessage,
|
||||
StepUpdate,
|
||||
)
|
||||
|
||||
__all__ = [
|
||||
"DeviceType",
|
||||
"DeviceStatus",
|
||||
"DeviceSchema",
|
||||
"DeviceInfo",
|
||||
"TaskStatus",
|
||||
"TaskRequest",
|
||||
"TaskSchema",
|
||||
"TaskCreateRequest",
|
||||
"WSMessageType",
|
||||
"WSMessage",
|
||||
"StepUpdate",
|
||||
]
|
||||
67
dashboard/models/device.py
Normal file
67
dashboard/models/device.py
Normal file
@@ -0,0 +1,67 @@
|
||||
"""
|
||||
Device data models for the dashboard.
|
||||
"""
|
||||
|
||||
from datetime import datetime
|
||||
from enum import Enum
|
||||
from typing import Optional
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
|
||||
class DeviceType(str, Enum):
|
||||
"""Device connection type."""
|
||||
|
||||
ADB = "adb"
|
||||
HDC = "hdc"
|
||||
IOS = "ios"
|
||||
|
||||
|
||||
class DeviceStatus(str, Enum):
|
||||
"""Device status."""
|
||||
|
||||
ONLINE = "online"
|
||||
OFFLINE = "offline"
|
||||
BUSY = "busy"
|
||||
ERROR = "error"
|
||||
|
||||
|
||||
class DeviceSchema(BaseModel):
|
||||
"""Device schema for API responses."""
|
||||
|
||||
device_id: str = Field(..., description="Unique device identifier")
|
||||
status: DeviceStatus = Field(default=DeviceStatus.OFFLINE, description="Device status")
|
||||
device_type: DeviceType = Field(..., description="Device connection type")
|
||||
model: Optional[str] = Field(None, description="Device model name")
|
||||
android_version: Optional[str] = Field(None, description="Android/iOS version")
|
||||
current_app: Optional[str] = Field(None, description="Currently active app")
|
||||
last_seen: datetime = Field(default_factory=datetime.now, description="Last connection time")
|
||||
is_connected: bool = Field(True, description="Whether device is connected")
|
||||
screenshot: Optional[str] = Field(None, description="Base64 encoded screenshot")
|
||||
|
||||
class Config:
|
||||
json_schema_extra = {
|
||||
"example": {
|
||||
"device_id": "emulator-5554",
|
||||
"status": "online",
|
||||
"device_type": "adb",
|
||||
"model": "sdk_gphone64_x86_64",
|
||||
"android_version": "14",
|
||||
"current_app": "com.android.launcher3",
|
||||
"is_connected": True,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class DeviceInfo(BaseModel):
|
||||
"""Extended device information."""
|
||||
|
||||
device_id: str
|
||||
status: DeviceStatus
|
||||
device_type: DeviceType
|
||||
model: Optional[str] = None
|
||||
android_version: Optional[str] = None
|
||||
current_app: Optional[str] = None
|
||||
last_seen: datetime
|
||||
screenshot: Optional[str] = None
|
||||
is_connected: bool = True
|
||||
81
dashboard/models/task.py
Normal file
81
dashboard/models/task.py
Normal file
@@ -0,0 +1,81 @@
|
||||
"""
|
||||
Task data models for the dashboard.
|
||||
"""
|
||||
|
||||
from datetime import datetime
|
||||
from enum import Enum
|
||||
from typing import Any, Dict, Optional
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from phone_agent.model import ModelConfig
|
||||
|
||||
|
||||
class TaskStatus(str, Enum):
|
||||
"""Task execution status."""
|
||||
|
||||
PENDING = "pending"
|
||||
RUNNING = "running"
|
||||
COMPLETED = "completed"
|
||||
FAILED = "failed"
|
||||
STOPPED = "stopped"
|
||||
|
||||
|
||||
class TaskCreateRequest(BaseModel):
|
||||
"""Request to create a new task."""
|
||||
|
||||
device_id: str = Field(..., description="Target device ID")
|
||||
task: str = Field(..., description="Task description")
|
||||
max_steps: int = Field(100, description="Maximum execution steps")
|
||||
lang: str = Field("cn", description="Language (cn or en)")
|
||||
|
||||
# Model config - use dict to avoid validation issues with ModelConfig
|
||||
base_url: str = Field(
|
||||
default="http://localhost:8000/v1", description="Model API base URL"
|
||||
)
|
||||
model_name: str = Field(default="autoglm-phone-9b", description="Model name")
|
||||
api_key: str = Field(default="EMPTY", description="API key")
|
||||
max_tokens: int = Field(default=3000, description="Max tokens per response")
|
||||
temperature: float = Field(default=0.0, description="Sampling temperature")
|
||||
top_p: float = Field(default=0.85, description="Top-p sampling parameter")
|
||||
frequency_penalty: float = Field(default=0.2, description="Frequency penalty")
|
||||
|
||||
|
||||
class TaskSchema(BaseModel):
|
||||
"""Task schema for API responses."""
|
||||
|
||||
task_id: str = Field(..., description="Unique task identifier")
|
||||
device_id: str = Field(..., description="Target device ID")
|
||||
task: str = Field(..., description="Task description")
|
||||
status: TaskStatus = Field(..., description="Task status")
|
||||
current_step: int = Field(0, description="Current step number")
|
||||
max_steps: int = Field(100, description="Maximum steps")
|
||||
current_action: Optional[Dict[str, Any]] = Field(None, description="Current action")
|
||||
thinking: Optional[str] = Field(None, description="Current thinking/reasoning")
|
||||
started_at: datetime = Field(..., description="Task start time")
|
||||
updated_at: datetime = Field(..., description="Last update time")
|
||||
finished_at: Optional[datetime] = Field(None, description="Task completion time")
|
||||
error: Optional[str] = Field(None, description="Error message if failed")
|
||||
completion_message: Optional[str] = Field(
|
||||
None, description="Task completion message with details"
|
||||
)
|
||||
|
||||
class Config:
|
||||
json_schema_extra = {
|
||||
"example": {
|
||||
"task_id": "task_123456",
|
||||
"device_id": "emulator-5554",
|
||||
"task": "Open WeChat",
|
||||
"status": "running",
|
||||
"current_step": 3,
|
||||
"max_steps": 100,
|
||||
"current_action": {"action": "Tap", "element": "WeChat icon"},
|
||||
"thinking": "Looking for WeChat icon on home screen",
|
||||
"started_at": "2024-01-09T10:00:00",
|
||||
"updated_at": "2024-01-09T10:00:15",
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
# For backward compatibility
|
||||
TaskRequest = TaskCreateRequest
|
||||
70
dashboard/models/ws_messages.py
Normal file
70
dashboard/models/ws_messages.py
Normal file
@@ -0,0 +1,70 @@
|
||||
"""
|
||||
WebSocket message models for real-time updates.
|
||||
"""
|
||||
|
||||
from datetime import datetime
|
||||
from enum import Enum
|
||||
from typing import Any, Dict, Optional
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
|
||||
class WSMessageType(str, Enum):
|
||||
"""WebSocket message types."""
|
||||
|
||||
DEVICE_UPDATE = "device_update"
|
||||
TASK_STARTED = "task_started"
|
||||
TASK_STEP = "task_step"
|
||||
TASK_COMPLETED = "task_completed"
|
||||
TASK_FAILED = "task_failed"
|
||||
TASK_STOPPED = "task_stopped"
|
||||
SCREENSHOT = "screenshot"
|
||||
ERROR = "error"
|
||||
PING = "ping"
|
||||
PONG = "pong"
|
||||
|
||||
|
||||
class WSMessage(BaseModel):
|
||||
"""Base WebSocket message."""
|
||||
|
||||
type: WSMessageType = Field(..., description="Message type")
|
||||
data: Dict[str, Any] = Field(default_factory=dict, description="Message data")
|
||||
timestamp: datetime = Field(default_factory=datetime.now, description="Message timestamp")
|
||||
|
||||
class Config:
|
||||
json_schema_extra = {
|
||||
"example": {
|
||||
"type": "task_step",
|
||||
"data": {
|
||||
"task_id": "task_123",
|
||||
"device_id": "emulator-5554",
|
||||
"step": 5,
|
||||
"action": {"action": "Tap"},
|
||||
"thinking": "Tapping on button",
|
||||
},
|
||||
"timestamp": "2024-01-09T10:00:00",
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class StepUpdate(BaseModel):
|
||||
"""Step update message data."""
|
||||
|
||||
task_id: str = Field(..., description="Task ID")
|
||||
device_id: str = Field(..., description="Device ID")
|
||||
step: int = Field(..., description="Step number")
|
||||
action: Optional[Dict[str, Any]] = Field(None, description="Action taken")
|
||||
thinking: Optional[str] = Field(None, description="AI reasoning")
|
||||
finished: bool = Field(False, description="Whether task is finished")
|
||||
success: bool = Field(True, description="Whether step succeeded")
|
||||
message: Optional[str] = Field(None, description="Status message")
|
||||
|
||||
|
||||
class ScreenshotUpdate(BaseModel):
|
||||
"""Screenshot update message data."""
|
||||
|
||||
device_id: str = Field(..., description="Device ID")
|
||||
screenshot: str = Field(..., description="Base64 encoded screenshot")
|
||||
width: int = Field(..., description="Screenshot width")
|
||||
height: int = Field(..., description="Screenshot height")
|
||||
timestamp: datetime = Field(default_factory=datetime.now)
|
||||
Reference in New Issue
Block a user