This repository was archived by the owner on Feb 14, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 283
Expand file tree
/
Copy pathapiSession.ts
More file actions
470 lines (426 loc) · 18.6 KB
/
apiSession.ts
File metadata and controls
470 lines (426 loc) · 18.6 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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
import { logger } from '@/ui/logger'
import { EventEmitter } from 'node:events'
import { io, Socket } from 'socket.io-client'
import { AgentState, ClientToServerEvents, MessageContent, Metadata, ServerToClientEvents, Session, Update, UserMessage, UserMessageSchema, Usage } from './types'
import { decodeBase64, decrypt, encodeBase64, encrypt } from './encryption';
import { backoff } from '@/utils/time';
import { configuration } from '@/configuration';
import { RawJSONLines } from '@/claude/types';
import { randomUUID } from 'node:crypto';
import { AsyncLock } from '@/utils/lock';
import { RpcHandlerManager } from './rpc/RpcHandlerManager';
import { registerCommonHandlers } from '../modules/common/registerCommonHandlers';
/**
* ACP (Agent Communication Protocol) message data types.
* This is the unified format for all agent messages - CLI adapts each provider's format to ACP.
*/
export type ACPMessageData =
// Core message types
| { type: 'message'; message: string }
| { type: 'reasoning'; message: string }
| { type: 'thinking'; text: string }
// Tool interactions
| { type: 'tool-call'; callId: string; name: string; input: unknown; id: string }
| { type: 'tool-result'; callId: string; output: unknown; id: string; isError?: boolean }
// File operations
| { type: 'file-edit'; description: string; filePath: string; diff?: string; oldContent?: string; newContent?: string; id: string }
// Terminal/command output
| { type: 'terminal-output'; data: string; callId: string }
// Task lifecycle events
| { type: 'task_started'; id: string }
| { type: 'task_complete'; id: string }
| { type: 'turn_aborted'; id: string }
// Permissions
| { type: 'permission-request'; permissionId: string; toolName: string; description: string; options?: unknown }
// Usage/metrics
| { type: 'token_count'; [key: string]: unknown };
export type ACPProvider = 'gemini' | 'codex' | 'claude' | 'opencode';
export class ApiSessionClient extends EventEmitter {
private readonly token: string;
readonly sessionId: string;
private metadata: Metadata | null;
private metadataVersion: number;
private agentState: AgentState | null;
private agentStateVersion: number;
private socket: Socket<ServerToClientEvents, ClientToServerEvents>;
private pendingMessages: UserMessage[] = [];
private pendingMessageCallback: ((message: UserMessage) => void) | null = null;
readonly rpcHandlerManager: RpcHandlerManager;
private agentStateLock = new AsyncLock();
private metadataLock = new AsyncLock();
private encryptionKey: Uint8Array;
private encryptionVariant: 'legacy' | 'dataKey';
constructor(token: string, session: Session) {
super()
this.token = token;
this.sessionId = session.id;
this.metadata = session.metadata;
this.metadataVersion = session.metadataVersion;
this.agentState = session.agentState;
this.agentStateVersion = session.agentStateVersion;
this.encryptionKey = session.encryptionKey;
this.encryptionVariant = session.encryptionVariant;
// Initialize RPC handler manager
this.rpcHandlerManager = new RpcHandlerManager({
scopePrefix: this.sessionId,
encryptionKey: this.encryptionKey,
encryptionVariant: this.encryptionVariant,
logger: (msg, data) => logger.debug(msg, data)
});
// Some legacy/invalid sessions may fail to decrypt metadata (null). Avoid crashing the CLI;
// the session can still function for remote messaging and will receive metadata updates later.
if (this.metadata?.path) {
registerCommonHandlers(this.rpcHandlerManager, this.metadata.path);
} else {
logger.debug('[API] Session metadata unavailable; skipping common handler registration', {
sessionId: this.sessionId,
});
}
//
// Create socket
//
this.socket = io(configuration.serverUrl, {
auth: {
token: this.token,
clientType: 'session-scoped' as const,
sessionId: this.sessionId
},
path: '/v1/updates',
reconnection: true,
reconnectionAttempts: Infinity,
reconnectionDelay: 1000,
reconnectionDelayMax: 5000,
transports: ['websocket'],
withCredentials: true,
autoConnect: false
});
//
// Handlers
//
this.socket.on('connect', () => {
logger.debug('Socket connected successfully');
this.rpcHandlerManager.onSocketConnect(this.socket);
})
// Set up global RPC request handler
this.socket.on('rpc-request', async (data: { method: string, params: string }, callback: (response: string) => void) => {
callback(await this.rpcHandlerManager.handleRequest(data));
})
this.socket.on('disconnect', (reason) => {
logger.debug('[API] Socket disconnected:', reason);
this.rpcHandlerManager.onSocketDisconnect();
})
this.socket.on('connect_error', (error) => {
logger.debug('[API] Socket connection error:', error);
this.rpcHandlerManager.onSocketDisconnect();
})
// Server events
this.socket.on('update', (data: Update) => {
try {
logger.debugLargeJson('[SOCKET] [UPDATE] Received update:', data);
if (!data.body) {
logger.debug('[SOCKET] [UPDATE] [ERROR] No body in update!');
return;
}
if (data.body.t === 'new-message' && data.body.message.content.t === 'encrypted') {
const body = decrypt(this.encryptionKey, this.encryptionVariant, decodeBase64(data.body.message.content.c));
logger.debugLargeJson('[SOCKET] [UPDATE] Received update:', body)
// Try to parse as user message first
const userResult = UserMessageSchema.safeParse(body);
if (userResult.success) {
// Server already filtered to only our session
if (this.pendingMessageCallback) {
this.pendingMessageCallback(userResult.data);
} else {
this.pendingMessages.push(userResult.data);
}
} else {
// If not a user message, it might be a permission response or other message type
this.emit('message', body);
}
} else if (data.body.t === 'update-session') {
if (data.body.metadata && data.body.metadata.version > this.metadataVersion) {
this.metadata = decrypt(this.encryptionKey, this.encryptionVariant,decodeBase64(data.body.metadata.value));
this.metadataVersion = data.body.metadata.version;
}
if (data.body.agentState && data.body.agentState.version > this.agentStateVersion) {
this.agentState = data.body.agentState.value ? decrypt(this.encryptionKey, this.encryptionVariant, decodeBase64(data.body.agentState.value)) : null;
this.agentStateVersion = data.body.agentState.version;
}
} else if (data.body.t === 'update-machine') {
// Session clients shouldn't receive machine updates - log warning
logger.debug(`[SOCKET] WARNING: Session client received unexpected machine update - ignoring`);
} else {
// If not a user message, it might be a permission response or other message type
this.emit('message', data.body);
}
} catch (error) {
logger.debug('[SOCKET] [UPDATE] [ERROR] Error handling update', { error });
}
});
// DEATH
this.socket.on('error', (error) => {
logger.debug('[API] Socket error:', error);
});
//
// Connect (after short delay to give a time to add handlers)
//
this.socket.connect();
}
onUserMessage(callback: (data: UserMessage) => void) {
this.pendingMessageCallback = callback;
while (this.pendingMessages.length > 0) {
callback(this.pendingMessages.shift()!);
}
}
/**
* Send message to session
* @param body - Message body (can be MessageContent or raw content for agent messages)
*/
sendClaudeSessionMessage(body: RawJSONLines) {
let content: MessageContent;
// Check if body is already a MessageContent (has role property)
if (body.type === 'user' && typeof body.message.content === 'string' && body.isSidechain !== true && body.isMeta !== true) {
content = {
role: 'user',
content: {
type: 'text',
text: body.message.content
},
meta: {
sentFrom: 'cli'
}
}
} else {
// Wrap Claude messages in the expected format
content = {
role: 'agent',
content: {
type: 'output',
data: body // This wraps the entire Claude message
},
meta: {
sentFrom: 'cli'
}
};
}
logger.debugLargeJson('[SOCKET] Sending message through socket:', content)
// Check if socket is connected before sending
if (!this.socket.connected) {
logger.debug('[API] Socket not connected, cannot send Claude session message. Message will be lost:', { type: body.type });
return;
}
const encrypted = encodeBase64(encrypt(this.encryptionKey, this.encryptionVariant, content));
this.socket.emit('message', {
sid: this.sessionId,
message: encrypted
});
// Track usage from assistant messages
if (body.type === 'assistant' && body.message?.usage) {
try {
this.sendUsageData(body.message.usage);
} catch (error) {
logger.debug('[SOCKET] Failed to send usage data:', error);
}
}
// Update metadata with summary if this is a summary message
if (body.type === 'summary' && 'summary' in body && 'leafUuid' in body) {
this.updateMetadata((metadata) => ({
...metadata,
summary: {
text: body.summary,
updatedAt: Date.now()
}
}));
}
}
sendCodexMessage(body: any) {
let content = {
role: 'agent',
content: {
type: 'codex',
data: body // This wraps the entire Claude message
},
meta: {
sentFrom: 'cli'
}
};
const encrypted = encodeBase64(encrypt(this.encryptionKey, this.encryptionVariant, content));
// Check if socket is connected before sending
if (!this.socket.connected) {
logger.debug('[API] Socket not connected, cannot send message. Message will be lost:', { type: body.type });
// TODO: Consider implementing message queue or HTTP fallback for reliability
}
this.socket.emit('message', {
sid: this.sessionId,
message: encrypted
});
}
/**
* Send a generic agent message to the session using ACP (Agent Communication Protocol) format.
* Works for any agent type (Gemini, Codex, Claude, etc.) - CLI normalizes to unified ACP format.
*
* @param provider - The agent provider sending the message (e.g., 'gemini', 'codex', 'claude')
* @param body - The message payload (type: 'message' | 'reasoning' | 'tool-call' | 'tool-result')
*/
sendAgentMessage(provider: 'gemini' | 'codex' | 'claude' | 'opencode', body: ACPMessageData) {
let content = {
role: 'agent',
content: {
type: 'acp',
provider,
data: body
},
meta: {
sentFrom: 'cli'
}
};
logger.debug(`[SOCKET] Sending ACP message from ${provider}:`, { type: body.type, hasMessage: 'message' in body });
const encrypted = encodeBase64(encrypt(this.encryptionKey, this.encryptionVariant, content));
this.socket.emit('message', {
sid: this.sessionId,
message: encrypted
});
}
sendSessionEvent(event: {
type: 'switch', mode: 'local' | 'remote'
} | {
type: 'message', message: string
} | {
type: 'permission-mode-changed', mode: 'default' | 'acceptEdits' | 'bypassPermissions' | 'plan'
} | {
type: 'ready'
}, id?: string) {
let content = {
role: 'agent',
content: {
id: id ?? randomUUID(),
type: 'event',
data: event
}
};
const encrypted = encodeBase64(encrypt(this.encryptionKey, this.encryptionVariant, content));
this.socket.emit('message', {
sid: this.sessionId,
message: encrypted
});
}
/**
* Send a ping message to keep the connection alive
*/
keepAlive(thinking: boolean, mode: 'local' | 'remote') {
if (process.env.DEBUG) { // too verbose for production
logger.debug(`[API] Sending keep alive message: ${thinking}`);
}
this.socket.volatile.emit('session-alive', {
sid: this.sessionId,
time: Date.now(),
thinking,
mode
});
}
/**
* Send session death message
*/
sendSessionDeath() {
this.socket.emit('session-end', { sid: this.sessionId, time: Date.now() });
}
/**
* Send usage data to the server
*/
sendUsageData(usage: Usage) {
// Calculate total tokens
const totalTokens = usage.input_tokens + usage.output_tokens + (usage.cache_creation_input_tokens || 0) + (usage.cache_read_input_tokens || 0);
// Transform Claude usage format to backend expected format
const usageReport = {
key: 'claude-session',
sessionId: this.sessionId,
tokens: {
total: totalTokens,
input: usage.input_tokens,
output: usage.output_tokens,
cache_creation: usage.cache_creation_input_tokens || 0,
cache_read: usage.cache_read_input_tokens || 0
},
cost: {
// TODO: Calculate actual costs based on pricing
// For now, using placeholder values
total: 0,
input: 0,
output: 0
}
}
logger.debugLargeJson('[SOCKET] Sending usage data:', usageReport)
this.socket.emit('usage-report', usageReport);
}
/**
* Update session metadata
* @param handler - Handler function that returns the updated metadata
*/
updateMetadata(handler: (metadata: Metadata) => Metadata) {
this.metadataLock.inLock(async () => {
await backoff(async () => {
let updated = handler(this.metadata!); // Weird state if metadata is null - should never happen but here we are
const answer = await this.socket.emitWithAck('update-metadata', { sid: this.sessionId, expectedVersion: this.metadataVersion, metadata: encodeBase64(encrypt(this.encryptionKey, this.encryptionVariant, updated)) });
if (answer.result === 'success') {
this.metadata = decrypt(this.encryptionKey, this.encryptionVariant, decodeBase64(answer.metadata));
this.metadataVersion = answer.version;
} else if (answer.result === 'version-mismatch') {
if (answer.version > this.metadataVersion) {
this.metadataVersion = answer.version;
this.metadata = decrypt(this.encryptionKey, this.encryptionVariant, decodeBase64(answer.metadata));
}
throw new Error('Metadata version mismatch');
} else if (answer.result === 'error') {
// Hard error - ignore
}
});
});
}
/**
* Update session agent state
* @param handler - Handler function that returns the updated agent state
*/
updateAgentState(handler: (metadata: AgentState) => AgentState) {
logger.debugLargeJson('Updating agent state', this.agentState);
this.agentStateLock.inLock(async () => {
await backoff(async () => {
let updated = handler(this.agentState || {});
const answer = await this.socket.emitWithAck('update-state', { sid: this.sessionId, expectedVersion: this.agentStateVersion, agentState: updated ? encodeBase64(encrypt(this.encryptionKey, this.encryptionVariant, updated)) : null });
if (answer.result === 'success') {
this.agentState = answer.agentState ? decrypt(this.encryptionKey, this.encryptionVariant, decodeBase64(answer.agentState)) : null;
this.agentStateVersion = answer.version;
logger.debug('Agent state updated', this.agentState);
} else if (answer.result === 'version-mismatch') {
if (answer.version > this.agentStateVersion) {
this.agentStateVersion = answer.version;
this.agentState = answer.agentState ? decrypt(this.encryptionKey, this.encryptionVariant, decodeBase64(answer.agentState)) : null;
}
throw new Error('Agent state version mismatch');
} else if (answer.result === 'error') {
// console.error('Agent state update error', answer);
// Hard error - ignore
}
});
});
}
/**
* Wait for socket buffer to flush
*/
async flush(): Promise<void> {
if (!this.socket.connected) {
return;
}
return new Promise((resolve) => {
this.socket.emit('ping', () => {
resolve();
});
setTimeout(() => {
resolve();
}, 10000);
});
}
async close() {
logger.debug('[API] socket.close() called');
this.socket.close();
}
}