-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconnection.ts
More file actions
216 lines (191 loc) · 6.49 KB
/
connection.ts
File metadata and controls
216 lines (191 loc) · 6.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import { randomBytes } from 'node:crypto';
import { EventEmitter } from 'node:events';
import { hostname, userInfo, platform } from 'node:os';
import { execSync } from 'node:child_process';
import type { ModelPersistence } from './persistence.js';
import { getLogger } from './logger.js';
function gitUserName(): string | null {
try {
return execSync('git config user.name', { encoding: 'utf8', timeout: 2000 }).trim() || null;
} catch {
return null;
}
}
function buildAgentName(sessionId: string): string {
const gitName = gitUserName();
const raw = gitName
? gitName.split(/\s+/)[0]
: userInfo().username;
const firstName = raw.charAt(0).toUpperCase() + raw.slice(1).toLowerCase();
const shortId = sessionId.slice(0, 4);
return `${firstName}·${shortId}`;
}
function buildAgentStatus(): Record<string, string> {
const status: Record<string, string> = {};
const git = gitUserName();
if (git) status.user = git;
status.username = userInfo().username;
status.hostname = hostname().replace(/\.local$/, '');
status.platform = platform();
status.cwd = process.cwd();
return status;
}
export interface ModelChange {
action: 'added' | 'removed' | 'updated';
entityType: 'scene' | 'message' | 'moment' | 'narrative';
id: string;
name: string;
details?: string;
}
export interface ConnectionManagerOptions {
serverUrl: string;
apiKey: string;
workspaceId: string;
persistence: ModelPersistence;
}
export interface AgentEndpoint {
label: string;
url: string;
}
const BASE_RECONNECT_MS = 3000;
const MAX_RECONNECT_MS = 30000;
export class ConnectionManager extends EventEmitter {
private ws: import('ws').WebSocket | null = null;
private connected = false;
private disposed = false;
private reconnectTimer: ReturnType<typeof setTimeout> | null = null;
private reconnectAttempts = 0;
private endpoints: AgentEndpoint[] = [];
readonly sessionId = randomBytes(12).toString('hex');
readonly name: string;
readonly status: Record<string, string>;
constructor(private options: ConnectionManagerOptions) {
super();
this.name = buildAgentName(this.sessionId);
this.status = buildAgentStatus();
this.on('error', () => {});
}
async connect(timeoutMs = 10000): Promise<void> {
this.disposed = false;
await this.doConnect(timeoutMs, true);
}
private async doConnect(timeoutMs: number, rejectOnError: boolean): Promise<void> {
const log = getLogger();
const WebSocket = (await import('ws')).default;
const wsUrl = this.options.serverUrl.replace(/^http/, 'ws');
const url = `${wsUrl}/api/agent/model/${this.options.workspaceId}/stream?key=${this.options.apiKey}`;
log.info('connection', `connecting to ${wsUrl}`, { workspaceId: this.options.workspaceId, sessionId: this.sessionId });
this.ws = new WebSocket(url);
return new Promise<void>((resolve, reject) => {
const timeout = setTimeout(() => {
log.error('connection', `timeout after ${timeoutMs}ms`);
this.ws?.close();
this.ws = null;
if (rejectOnError) {
this.disposed = true;
reject(new Error(`Connection timeout after ${timeoutMs}ms`));
} else {
this.scheduleReconnect();
}
}, timeoutMs);
this.ws!.on('open', () => {
log.info('connection', 'websocket open, sending hello');
this.connected = true;
this.ws!.send(JSON.stringify({ type: 'hello', sessionId: this.sessionId, name: this.name, status: this.status }));
if (this.endpoints.length > 0) {
this.sendEndpoints();
}
});
this.ws!.on('message', (data: Buffer) => {
try {
const msg = JSON.parse(data.toString());
log.info('connection', `received message type=${msg.type}`);
this.processMessage(msg);
if (msg.type === 'full') {
clearTimeout(timeout);
this.reconnectAttempts = 0;
resolve();
}
} catch (err) {
log.error('connection', 'failed to process message', err instanceof Error ? err : new Error(String(err)));
this.emit('error', err);
}
});
this.ws!.on('close', () => {
log.warn('connection', 'websocket closed');
this.connected = false;
this.emit('disconnected');
this.scheduleReconnect();
});
this.ws!.on('error', (err: Error) => {
log.error('connection', 'websocket error', err);
clearTimeout(timeout);
if (rejectOnError) {
reject(err);
} else {
this.emit('error', err);
}
});
});
}
private scheduleReconnect(): void {
if (this.disposed) return;
const log = getLogger();
const delay = Math.min(BASE_RECONNECT_MS * Math.pow(2, this.reconnectAttempts), MAX_RECONNECT_MS);
this.reconnectAttempts++;
log.info('connection', `scheduling reconnect attempt ${this.reconnectAttempts} in ${delay}ms`);
this.reconnectTimer = setTimeout(() => {
this.reconnectTimer = null;
if (!this.disposed) {
log.info('connection', 'attempting reconnect');
this.doConnect(10000, false).catch((err) => {
log.error('connection', 'reconnect failed', err instanceof Error ? err : new Error(String(err)));
});
}
}, delay);
}
disconnect(): void {
const log = getLogger();
log.info('connection', 'disconnecting');
this.disposed = true;
if (this.reconnectTimer) {
clearTimeout(this.reconnectTimer);
this.reconnectTimer = null;
}
if (this.ws) {
this.ws.close();
this.ws = null;
}
this.connected = false;
this.options.persistence.destroy();
}
isConnected(): boolean {
return this.connected;
}
getEndpoints(): AgentEndpoint[] {
return this.endpoints;
}
updateEndpoints(endpoints: AgentEndpoint[]): void {
this.endpoints = endpoints;
this.sendEndpoints();
}
private sendEndpoints(): void {
if (this.connected && this.ws) {
this.ws.send(JSON.stringify({ type: 'update', endpoints: this.endpoints }));
}
}
processMessage(msg: { type: string; model?: unknown; changes?: ModelChange[] }): void {
if (msg.type === 'full' && msg.model) {
this.options.persistence.update(msg.model);
this.emit('model-updated', msg.model);
if (!this.connected) {
this.connected = true;
this.emit('connected');
}
} else if (msg.type === 'changes' && msg.changes) {
for (const change of msg.changes) {
this.options.persistence.appendChange(change);
}
}
}
}