@agentmeshhq/agent 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.
- package/README.md +111 -0
- package/dist/cli/attach.d.ts +1 -0
- package/dist/cli/attach.js +18 -0
- package/dist/cli/attach.js.map +1 -0
- package/dist/cli/index.d.ts +2 -0
- package/dist/cli/index.js +98 -0
- package/dist/cli/index.js.map +1 -0
- package/dist/cli/init.d.ts +1 -0
- package/dist/cli/init.js +55 -0
- package/dist/cli/init.js.map +1 -0
- package/dist/cli/list.d.ts +1 -0
- package/dist/cli/list.js +45 -0
- package/dist/cli/list.js.map +1 -0
- package/dist/cli/nudge.d.ts +1 -0
- package/dist/cli/nudge.js +72 -0
- package/dist/cli/nudge.js.map +1 -0
- package/dist/cli/start.d.ts +8 -0
- package/dist/cli/start.js +37 -0
- package/dist/cli/start.js.map +1 -0
- package/dist/cli/stop.d.ts +1 -0
- package/dist/cli/stop.js +33 -0
- package/dist/cli/stop.js.map +1 -0
- package/dist/config/loader.d.ts +10 -0
- package/dist/config/loader.js +65 -0
- package/dist/config/loader.js.map +1 -0
- package/dist/config/schema.d.ts +32 -0
- package/dist/config/schema.js +11 -0
- package/dist/config/schema.js.map +1 -0
- package/dist/core/daemon.d.ts +20 -0
- package/dist/core/daemon.js +164 -0
- package/dist/core/daemon.js.map +1 -0
- package/dist/core/heartbeat.d.ts +14 -0
- package/dist/core/heartbeat.js +42 -0
- package/dist/core/heartbeat.js.map +1 -0
- package/dist/core/injector.d.ts +8 -0
- package/dist/core/injector.js +84 -0
- package/dist/core/injector.js.map +1 -0
- package/dist/core/registry.d.ts +27 -0
- package/dist/core/registry.js +52 -0
- package/dist/core/registry.js.map +1 -0
- package/dist/core/tmux.d.ts +11 -0
- package/dist/core/tmux.js +112 -0
- package/dist/core/tmux.js.map +1 -0
- package/dist/core/websocket.d.ts +25 -0
- package/dist/core/websocket.js +65 -0
- package/dist/core/websocket.js.map +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +10 -0
- package/dist/index.js.map +1 -0
- package/package.json +35 -0
- package/src/cli/attach.ts +22 -0
- package/src/cli/index.ts +101 -0
- package/src/cli/init.ts +87 -0
- package/src/cli/list.ts +62 -0
- package/src/cli/nudge.ts +84 -0
- package/src/cli/start.ts +50 -0
- package/src/cli/stop.ts +39 -0
- package/src/config/loader.ts +81 -0
- package/src/config/schema.ts +44 -0
- package/src/core/daemon.ts +213 -0
- package/src/core/heartbeat.ts +54 -0
- package/src/core/injector.ts +128 -0
- package/src/core/registry.ts +105 -0
- package/src/core/tmux.ts +139 -0
- package/src/core/websocket.ts +94 -0
- package/src/index.ts +9 -0
- package/tsconfig.json +8 -0
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import WebSocket from "ws";
|
|
2
|
+
|
|
3
|
+
export interface WebSocketConfig {
|
|
4
|
+
url: string;
|
|
5
|
+
token: string;
|
|
6
|
+
onMessage: (data: WebSocketEvent) => void;
|
|
7
|
+
onConnect?: () => void;
|
|
8
|
+
onDisconnect?: () => void;
|
|
9
|
+
onError?: (error: Error) => void;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface WebSocketEvent {
|
|
13
|
+
type: string;
|
|
14
|
+
[key: string]: unknown;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export class AgentWebSocket {
|
|
18
|
+
private ws: WebSocket | null = null;
|
|
19
|
+
private config: WebSocketConfig;
|
|
20
|
+
private reconnectAttempts = 0;
|
|
21
|
+
private maxReconnectAttempts = 10;
|
|
22
|
+
private reconnectDelay = 1000;
|
|
23
|
+
private shouldReconnect = true;
|
|
24
|
+
|
|
25
|
+
constructor(config: WebSocketConfig) {
|
|
26
|
+
this.config = config;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
connect(): void {
|
|
30
|
+
if (this.ws?.readyState === WebSocket.OPEN) {
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const wsUrl = `${this.config.url}?token=${this.config.token}`;
|
|
35
|
+
|
|
36
|
+
this.ws = new WebSocket(wsUrl);
|
|
37
|
+
|
|
38
|
+
this.ws.on("open", () => {
|
|
39
|
+
this.reconnectAttempts = 0;
|
|
40
|
+
this.config.onConnect?.();
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
this.ws.on("message", (data) => {
|
|
44
|
+
try {
|
|
45
|
+
const event = JSON.parse(data.toString()) as WebSocketEvent;
|
|
46
|
+
this.config.onMessage(event);
|
|
47
|
+
} catch (error) {
|
|
48
|
+
console.error("Failed to parse WebSocket message:", error);
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
this.ws.on("close", () => {
|
|
53
|
+
this.config.onDisconnect?.();
|
|
54
|
+
this.scheduleReconnect();
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
this.ws.on("error", (error) => {
|
|
58
|
+
this.config.onError?.(error);
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
private scheduleReconnect(): void {
|
|
63
|
+
if (!this.shouldReconnect) {
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if (this.reconnectAttempts >= this.maxReconnectAttempts) {
|
|
68
|
+
console.error("Max reconnect attempts reached");
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
this.reconnectAttempts++;
|
|
73
|
+
const delay = this.reconnectDelay * Math.pow(2, this.reconnectAttempts - 1);
|
|
74
|
+
|
|
75
|
+
setTimeout(() => {
|
|
76
|
+
console.log(
|
|
77
|
+
`Reconnecting... (attempt ${this.reconnectAttempts}/${this.maxReconnectAttempts})`
|
|
78
|
+
);
|
|
79
|
+
this.connect();
|
|
80
|
+
}, delay);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
disconnect(): void {
|
|
84
|
+
this.shouldReconnect = false;
|
|
85
|
+
if (this.ws) {
|
|
86
|
+
this.ws.close();
|
|
87
|
+
this.ws = null;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
isConnected(): boolean {
|
|
92
|
+
return this.ws?.readyState === WebSocket.OPEN;
|
|
93
|
+
}
|
|
94
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// Re-export core modules for programmatic usage
|
|
2
|
+
export { AgentDaemon } from "./core/daemon.js";
|
|
3
|
+
export { AgentWebSocket } from "./core/websocket.js";
|
|
4
|
+
export { Heartbeat } from "./core/heartbeat.js";
|
|
5
|
+
export * from "./core/tmux.js";
|
|
6
|
+
export * from "./core/registry.js";
|
|
7
|
+
export * from "./core/injector.js";
|
|
8
|
+
export * from "./config/schema.js";
|
|
9
|
+
export * from "./config/loader.js";
|