@cydm/magic-shell-agent-node 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. package/dist/adapters/pty-adapter.d.ts +18 -0
  2. package/dist/adapters/pty-adapter.js +99 -0
  3. package/dist/adapters/registry.d.ts +28 -0
  4. package/dist/adapters/registry.js +64 -0
  5. package/dist/adapters/rpc-adapter.d.ts +19 -0
  6. package/dist/adapters/rpc-adapter.js +182 -0
  7. package/dist/adapters/stdio-adapter.d.ts +17 -0
  8. package/dist/adapters/stdio-adapter.js +107 -0
  9. package/dist/adapters/types.d.ts +17 -0
  10. package/dist/adapters/types.js +2 -0
  11. package/dist/claude-exec.d.ts +11 -0
  12. package/dist/claude-exec.js +54 -0
  13. package/dist/claude-worker.d.ts +12 -0
  14. package/dist/claude-worker.js +163 -0
  15. package/dist/codex-exec.d.ts +12 -0
  16. package/dist/codex-exec.js +84 -0
  17. package/dist/codex-worker.d.ts +12 -0
  18. package/dist/codex-worker.js +179 -0
  19. package/dist/directory-browser.d.ts +3 -0
  20. package/dist/directory-browser.js +48 -0
  21. package/dist/index.d.ts +2 -0
  22. package/dist/index.js +2 -0
  23. package/dist/local-direct-server.d.ts +38 -0
  24. package/dist/local-direct-server.js +266 -0
  25. package/dist/node-conversation.d.ts +21 -0
  26. package/dist/node-conversation.js +28 -0
  27. package/dist/node-intent.d.ts +2 -0
  28. package/dist/node-intent.js +40 -0
  29. package/dist/node-reply.d.ts +30 -0
  30. package/dist/node-reply.js +77 -0
  31. package/dist/node.d.ts +132 -0
  32. package/dist/node.js +1954 -0
  33. package/dist/pie-session-control.d.ts +21 -0
  34. package/dist/pie-session-control.js +28 -0
  35. package/dist/plugin-loader.d.ts +19 -0
  36. package/dist/plugin-loader.js +144 -0
  37. package/dist/plugins/pie.json +7 -0
  38. package/dist/primary-agent-bridge.d.ts +69 -0
  39. package/dist/primary-agent-bridge.js +282 -0
  40. package/dist/session-manager.d.ts +66 -0
  41. package/dist/session-manager.js +197 -0
  42. package/dist/terminal-metadata.d.ts +7 -0
  43. package/dist/terminal-metadata.js +52 -0
  44. package/dist/types.d.ts +1 -0
  45. package/dist/types.js +1 -0
  46. package/dist/worker-control.d.ts +15 -0
  47. package/dist/worker-control.js +89 -0
  48. package/dist/worker-narration.d.ts +25 -0
  49. package/dist/worker-narration.js +90 -0
  50. package/dist/worker-output.d.ts +6 -0
  51. package/dist/worker-output.js +72 -0
  52. package/dist/worker-registry.d.ts +45 -0
  53. package/dist/worker-registry.js +501 -0
  54. package/dist/worker-runtime.d.ts +18 -0
  55. package/dist/worker-runtime.js +69 -0
  56. package/dist/ws-client.d.ts +68 -0
  57. package/dist/ws-client.js +193 -0
  58. package/package.json +38 -0
@@ -0,0 +1,193 @@
1
+ import WebSocket from "ws";
2
+ /**
3
+ * WebSocket 客户端
4
+ * 负责与 Relay Server 保持连接,自动重连
5
+ */
6
+ export class WebSocketClient {
7
+ options;
8
+ ws;
9
+ messageHandlers = [];
10
+ statusHandlers = [];
11
+ reconnectAttempts = 0;
12
+ reconnectTimer;
13
+ isConnected = false;
14
+ isConnecting = false;
15
+ manualDisconnect = false;
16
+ constructor(options) {
17
+ this.options = {
18
+ autoReconnect: true,
19
+ reconnectDelay: 1000,
20
+ maxReconnectDelay: 30000,
21
+ ...options,
22
+ };
23
+ }
24
+ /**
25
+ * 连接到 Relay Server
26
+ */
27
+ async connect() {
28
+ if (this.isConnected || this.isConnecting) {
29
+ return;
30
+ }
31
+ if (this.reconnectTimer) {
32
+ clearTimeout(this.reconnectTimer);
33
+ this.reconnectTimer = undefined;
34
+ }
35
+ this.manualDisconnect = false;
36
+ this.isConnecting = true;
37
+ console.log(`[WebSocketClient] Connecting to ${this.options.url}...`);
38
+ return new Promise((resolve, reject) => {
39
+ try {
40
+ const socket = new WebSocket(this.options.url);
41
+ this.ws = socket;
42
+ socket.on("open", () => {
43
+ this.isConnected = true;
44
+ this.isConnecting = false;
45
+ this.reconnectAttempts = 0;
46
+ console.log("[WebSocketClient] Connected");
47
+ // 发送认证
48
+ this.send({
49
+ type: "auth",
50
+ clientType: "node",
51
+ nodeId: this.options.nodeId,
52
+ password: this.options.password,
53
+ });
54
+ this.emitStatus("connected");
55
+ resolve();
56
+ });
57
+ socket.on("message", (data) => {
58
+ try {
59
+ const message = JSON.parse(data.toString());
60
+ this.emitMessage(message);
61
+ }
62
+ catch (err) {
63
+ console.error("[WebSocketClient] Failed to parse message:", err);
64
+ }
65
+ });
66
+ socket.on("close", (code, reason) => {
67
+ console.log(`[WebSocketClient] Closed: code=${code}, reason=${reason.toString()}`);
68
+ if (this.ws === socket) {
69
+ this.ws = undefined;
70
+ }
71
+ this.handleDisconnect();
72
+ });
73
+ socket.on("error", (err) => {
74
+ console.error("[WebSocketClient] Error:", err.message);
75
+ this.isConnecting = false;
76
+ this.emitStatus("error", err);
77
+ if (!this.isConnected) {
78
+ reject(err);
79
+ }
80
+ });
81
+ }
82
+ catch (err) {
83
+ this.isConnecting = false;
84
+ reject(err);
85
+ }
86
+ });
87
+ }
88
+ /**
89
+ * 断开连接
90
+ */
91
+ disconnect() {
92
+ this.manualDisconnect = true;
93
+ if (this.reconnectTimer) {
94
+ clearTimeout(this.reconnectTimer);
95
+ this.reconnectTimer = undefined;
96
+ }
97
+ if (this.ws) {
98
+ this.ws.close();
99
+ this.ws = undefined;
100
+ }
101
+ this.handleDisconnect();
102
+ }
103
+ /**
104
+ * 发送消息
105
+ */
106
+ send(message) {
107
+ if (!this.isConnected || !this.ws) {
108
+ console.warn("[WebSocketClient] Cannot send, not connected");
109
+ return;
110
+ }
111
+ this.ws.send(JSON.stringify(message));
112
+ }
113
+ /**
114
+ * 注册消息处理器
115
+ */
116
+ onMessage(handler) {
117
+ this.messageHandlers.push(handler);
118
+ }
119
+ /**
120
+ * 注册状态处理器
121
+ */
122
+ onStatus(handler) {
123
+ this.statusHandlers.push(handler);
124
+ }
125
+ /**
126
+ * 获取连接状态
127
+ */
128
+ getStatus() {
129
+ if (this.isConnected)
130
+ return "connected";
131
+ if (this.isConnecting)
132
+ return "connecting";
133
+ return "disconnected";
134
+ }
135
+ /**
136
+ * 处理断开连接
137
+ */
138
+ handleDisconnect() {
139
+ const wasConnected = this.isConnected;
140
+ this.isConnected = false;
141
+ this.isConnecting = false;
142
+ if (wasConnected) {
143
+ this.emitStatus("disconnected");
144
+ }
145
+ // 自动重连
146
+ if (!this.manualDisconnect && this.options.autoReconnect && !this.reconnectTimer) {
147
+ this.scheduleReconnect();
148
+ }
149
+ }
150
+ /**
151
+ * 安排重连
152
+ */
153
+ scheduleReconnect() {
154
+ const delay = Math.min(this.options.reconnectDelay * Math.pow(2, this.reconnectAttempts), this.options.maxReconnectDelay);
155
+ this.reconnectAttempts++;
156
+ console.log(`[WebSocketClient] Reconnecting in ${delay}ms (attempt ${this.reconnectAttempts})...`);
157
+ this.reconnectTimer = setTimeout(() => {
158
+ this.reconnectTimer = undefined;
159
+ this.connect().catch(() => {
160
+ // 重连失败,继续尝试
161
+ if (this.options.autoReconnect) {
162
+ this.scheduleReconnect();
163
+ }
164
+ });
165
+ }, delay);
166
+ }
167
+ /**
168
+ * 触发消息事件
169
+ */
170
+ emitMessage(message) {
171
+ for (const handler of this.messageHandlers) {
172
+ try {
173
+ handler(message);
174
+ }
175
+ catch (err) {
176
+ console.error("[WebSocketClient] Message handler error:", err);
177
+ }
178
+ }
179
+ }
180
+ /**
181
+ * 触发状态事件
182
+ */
183
+ emitStatus(status, error) {
184
+ for (const handler of this.statusHandlers) {
185
+ try {
186
+ handler(status, error);
187
+ }
188
+ catch (err) {
189
+ console.error("[WebSocketClient] Status handler error:", err);
190
+ }
191
+ }
192
+ }
193
+ }
package/package.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "name": "@cydm/magic-shell-agent-node",
3
+ "version": "0.1.0",
4
+ "description": "Magic Shell Agent Node - Local agent connector",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "bin": {
9
+ "agent-node": "./dist/node.js"
10
+ },
11
+ "files": [
12
+ "dist"
13
+ ],
14
+ "publishConfig": {
15
+ "access": "public"
16
+ },
17
+ "scripts": {
18
+ "build": "tsc && node ./scripts/copy-default-plugins.mjs",
19
+ "dev": "tsc --watch",
20
+ "start": "node dist/node.js"
21
+ },
22
+ "dependencies": {
23
+ "@cydm/magic-shell-protocol": "0.1.0",
24
+ "node-pty": "^1.1.0",
25
+ "ws": "^8.18.0"
26
+ },
27
+ "devDependencies": {
28
+ "@types/node": "^20.10.0",
29
+ "@types/ws": "^8.5.13",
30
+ "typescript": "^5.3.0"
31
+ },
32
+ "exports": {
33
+ ".": {
34
+ "import": "./dist/index.js",
35
+ "types": "./dist/index.d.ts"
36
+ }
37
+ }
38
+ }