()
| 6 | import { WebSocketClient, SubscriptionManager, isProgressEvent, isControlEvent, isMonitorEvent, isEventType } from "@aster/client-js"; |
| 7 | |
| 8 | async function main() { |
| 9 | // 1. 创建 WebSocket 客户端 |
| 10 | const ws = new WebSocketClient({ |
| 11 | maxReconnectAttempts: 5, |
| 12 | reconnectDelay: 1000, |
| 13 | heartbeatInterval: 30000, |
| 14 | heartbeatTimeout: 10000, |
| 15 | }); |
| 16 | |
| 17 | // 2. 连接到 aster 服务器 |
| 18 | try { |
| 19 | await ws.connect("ws://localhost:8080/ws"); |
| 20 | console.log("✅ Connected to aster"); |
| 21 | } catch (error) { |
| 22 | console.error("❌ Connection failed:", error); |
| 23 | return; |
| 24 | } |
| 25 | |
| 26 | // 3. 创建订阅管理器 |
| 27 | const subscriptionManager = new SubscriptionManager(ws); |
| 28 | |
| 29 | // 4. 订阅所有三个通道 |
| 30 | const subscription = subscriptionManager.subscribe(["progress", "control", "monitor"], { |
| 31 | agentId: "agent-123", |
| 32 | eventTypes: ["thinking", "text_chunk", "tool_start", "token_usage"], |
| 33 | }); |
| 34 | |
| 35 | // 5. 处理事件 |
| 36 | try { |
| 37 | for await (const envelope of subscription) { |
| 38 | const event = envelope.event; |
| 39 | |
| 40 | // 按通道分类处理 |
| 41 | if (isProgressEvent(event)) { |
| 42 | handleProgressEvent(event); |
| 43 | } else if (isControlEvent(event)) { |
| 44 | handleControlEvent(event); |
| 45 | } else if (isMonitorEvent(event)) { |
| 46 | handleMonitorEvent(event); |
| 47 | } |
| 48 | } |
| 49 | } catch (error) { |
| 50 | console.error("❌ Event subscription error:", error); |
| 51 | } |
| 52 | |
| 53 | // 6. 清理 |
| 54 | subscription.unsubscribe(); |
| 55 | ws.disconnect(); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * 处理 Progress Channel 事件 |
no test coverage detected