- Created backend core systems: - Remote Execution System (远程执行系统) - Task Orchestration Engine (任务编排引擎) - Persistent Memory System (持续记忆系统) - Proactive Service System (主动服务系统) - Created Tauri desktop app: - Three-column layout based on AutoClaw design - React + TypeScript + Tailwind CSS - Zustand state management - Lucide React icons - Components: - Sidebar (Agent list, IM channels, scheduled tasks) - ChatArea (Chat interface with message bubbles) - RightPanel (Task progress, statistics, next actions) Next: Test Tauri dev server and integrate with OpenClaw backend
126 lines
3.2 KiB
TypeScript
126 lines
3.2 KiB
TypeScript
// 远程执行系统 - 实现类
|
||
import type {
|
||
RemoteExecutionSystem,
|
||
Device,
|
||
Task,
|
||
TaskStatus,
|
||
StatusHandler,
|
||
Result,
|
||
DeviceStatus
|
||
} from './types';
|
||
|
||
export class RemoteExecutionEngine implements RemoteExecutionSystem {
|
||
private devices: Map<string, Device> = new Map();
|
||
private tasks: Map<string, Task> = new Map();
|
||
private subscriptions: Map<string, StatusHandler[]> = new Map();
|
||
private taskQueue: Task[] = [];
|
||
|
||
async registerDevice(device: Device): Promise<void> {
|
||
this.devices.set(device.id, device);
|
||
console.log([RemoteExecution] Device registered: ());
|
||
}
|
||
|
||
async heartbeat(deviceId: string): Promise<DeviceStatus> {
|
||
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<string> {
|
||
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<void> {
|
||
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<TaskStatus> {
|
||
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<void> {
|
||
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<void> {
|
||
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__;
|
||
}
|
||
}
|