// 远程执行系统 - 实现类 import type { RemoteExecutionSystem, Device, Task, TaskStatus, StatusHandler, Result, DeviceStatus } from './types'; export class RemoteExecutionEngine implements RemoteExecutionSystem { private devices: Map = new Map(); private tasks: Map = new Map(); private subscriptions: Map = new Map(); private taskQueue: Task[] = []; async registerDevice(device: Device): Promise { this.devices.set(device.id, device); console.log([RemoteExecution] Device registered: ()); } async heartbeat(deviceId: string): Promise { const device = this.devices.get(deviceId); if (!device) { throw new Error(Device not found: ); } device.lastHeartbeat = new Date(); return device.status; } async submitTask(task: Task): Promise { task.id = task.id || this.generateId(); task.status = 'pending'; task.createdAt = new Date(); this.tasks.set(task.id, task); this.taskQueue.push(task); console.log([RemoteExecution] Task submitted: ); // 立即执行(后续会改为队列处理) this.executeTask(task).catch(console.error); return task.id; } async cancelTask(taskId: string): Promise { const task = this.tasks.get(taskId); if (!task) { throw new Error(Task not found: ); } task.status = 'cancelled'; this.notifySubscribers(taskId, 'cancelled'); } async getStatus(taskId: string): Promise { const task = this.tasks.get(taskId); if (!task) { throw new Error(Task not found: ); } return task.status; } subscribe(taskId: string, handler: StatusHandler): void { if (!this.subscriptions.has(taskId)) { this.subscriptions.set(taskId, []); } this.subscriptions.get(taskId)!.push(handler); } async pushResult(taskId: string, result: Result): Promise { const task = this.tasks.get(taskId); if (!task) { throw new Error(Task not found: ); } task.result = result; task.status = result.success ? 'completed' : 'failed'; task.completedAt = new Date(); this.notifySubscribers(taskId, task.status); } private async executeTask(task: Task): Promise { try { task.status = 'running'; task.startedAt = new Date(); this.notifySubscribers(task.id, 'running'); // TODO: 实际执行逻辑(调用 OpenClaw SDK) console.log([RemoteExecution] Executing task: ); // 模拟执行 await new Promise(resolve => setTimeout(resolve, 1000)); await this.pushResult(task.id, { taskId: task.id, success: true, data: { message: 'Task completed successfully' } }); } catch (error: any) { await this.pushResult(task.id, { taskId: task.id, success: false, error: error.message }); } } private notifySubscribers(taskId: string, status: TaskStatus, progress?: number): void { const handlers = this.subscriptions.get(taskId); if (handlers) { handlers.forEach(handler => handler(status, progress)); } } private generateId(): string { return ask__; } }