forked from op7418/CodePilot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconversation-engine.ts
More file actions
482 lines (440 loc) · 17.1 KB
/
conversation-engine.ts
File metadata and controls
482 lines (440 loc) · 17.1 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
471
472
473
474
475
476
477
478
479
480
481
482
/**
* Conversation Engine — processes inbound IM messages through Claude.
*
* Takes a ChannelBinding + inbound message, calls streamClaude(),
* consumes the SSE stream server-side, saves messages to DB,
* and returns the response text for delivery.
*/
import fs from 'fs';
import path from 'path';
import os from 'os';
import type { ChannelBinding } from './types';
import type { SSEEvent, TokenUsage, MessageContentBlock, FileAttachment, MCPServerConfig } from '@/types';
import { streamClaude } from '../claude-client';
import {
addMessage,
getMessages,
acquireSessionLock,
renewSessionLock,
releaseSessionLock,
setSessionRuntimeStatus,
updateSdkSessionId,
updateSessionModel,
syncSdkTasks,
getSession,
getSetting,
} from '../db';
import { resolveProvider as resolveProviderUnified } from '../provider-resolver';
import crypto from 'crypto';
/** Read MCP server configs from ~/.claude.json and ~/.claude/settings.json */
function loadMcpServers(): Record<string, MCPServerConfig> | undefined {
try {
const readJson = (p: string): Record<string, unknown> => {
if (!fs.existsSync(p)) return {};
try { return JSON.parse(fs.readFileSync(p, 'utf-8')); } catch { return {}; }
};
const userConfig = readJson(path.join(os.homedir(), '.claude.json'));
const settings = readJson(path.join(os.homedir(), '.claude', 'settings.json'));
// Also read project-level .mcp.json
const projectMcp = readJson(path.join(process.cwd(), '.mcp.json'));
const merged = {
...((userConfig.mcpServers || {}) as Record<string, MCPServerConfig>),
...((settings.mcpServers || {}) as Record<string, MCPServerConfig>),
...((projectMcp.mcpServers || {}) as Record<string, MCPServerConfig>),
};
// Resolve ${...} placeholders in env values against DB settings
for (const server of Object.values(merged)) {
if (server.env) {
for (const [key, value] of Object.entries(server.env)) {
if (typeof value === 'string' && value.startsWith('${') && value.endsWith('}')) {
const settingKey = value.slice(2, -1);
const resolved = getSetting(settingKey);
server.env[key] = resolved || '';
}
}
}
}
return Object.keys(merged).length > 0 ? merged : undefined;
} catch {
return undefined;
}
}
export interface PermissionRequestInfo {
permissionRequestId: string;
toolName: string;
toolInput: Record<string, unknown>;
suggestions?: unknown[];
}
/**
* Callback invoked immediately when a permission_request SSE event arrives.
* This breaks the deadlock: the stream blocks until the permission is resolved,
* so we must forward the request to the IM *during* stream consumption,
* not after it returns.
*/
export type OnPermissionRequest = (perm: PermissionRequestInfo) => Promise<void>;
/**
* Callback invoked on each `text` SSE event with the full accumulated text so far.
* Must return synchronously — the bridge-manager handles throttling and fire-and-forget.
*/
export type OnPartialText = (fullText: string) => void;
/**
* Callback invoked on tool_use / tool_result SSE events.
* Used by card streaming to show tool progress indicators.
*/
export type OnToolEvent = (event: { type: 'tool_use'; id: string; name: string } | { type: 'tool_result'; tool_use_id: string; is_error: boolean }) => void;
export interface ConversationResult {
responseText: string;
tokenUsage: TokenUsage | null;
hasError: boolean;
errorMessage: string;
/** Permission request events that were forwarded during streaming */
permissionRequests: PermissionRequestInfo[];
/** SDK session ID captured from status/result events, for session resume */
sdkSessionId: string | null;
}
/**
* Process an inbound message: send to Claude, consume the response stream,
* save to DB, and return the result.
*/
export async function processMessage(
binding: ChannelBinding,
text: string,
onPermissionRequest?: OnPermissionRequest,
abortSignal?: AbortSignal,
files?: FileAttachment[],
onPartialText?: OnPartialText,
onToolEvent?: OnToolEvent,
): Promise<ConversationResult> {
const sessionId = binding.codepilotSessionId;
// Acquire session lock
const lockId = crypto.randomBytes(8).toString('hex');
const lockAcquired = acquireSessionLock(sessionId, lockId, `bridge-${binding.channelType}`, 600);
if (!lockAcquired) {
return {
responseText: '',
tokenUsage: null,
hasError: true,
errorMessage: 'Session is busy processing another request',
permissionRequests: [],
sdkSessionId: null,
};
}
setSessionRuntimeStatus(sessionId, 'running');
// Lock renewal interval
const renewalInterval = setInterval(() => {
try { renewSessionLock(sessionId, lockId, 600); } catch { /* best effort */ }
}, 60_000);
try {
// Resolve session early — needed for workingDirectory and provider resolution
const session = getSession(sessionId);
// Save user message — persist file attachments to disk using the same
// <!--files:JSON--> format as the desktop chat route, so the UI can render them.
// Also attach filePath to the file objects so streamClaude() can reuse
// on-disk copies (matching the desktop route behavior, preventing duplicate writes).
let savedContent = text;
if (files && files.length > 0) {
const workDir = binding.workingDirectory || session?.working_directory || '';
if (workDir) {
try {
const uploadDir = path.join(workDir, '.codepilot-uploads');
if (!fs.existsSync(uploadDir)) {
fs.mkdirSync(uploadDir, { recursive: true });
}
const fileMeta = files.map((f) => {
const safeName = path.basename(f.name).replace(/[^a-zA-Z0-9._-]/g, '_');
const filePath = path.join(uploadDir, `${Date.now()}-${safeName}`);
const buffer = Buffer.from(f.data, 'base64');
fs.writeFileSync(filePath, buffer);
// Attach filePath to the original file object so streamClaude()
// can reference the on-disk copy via getUploadedFilePaths()
f.filePath = filePath;
return { id: f.id, name: f.name, type: f.type, size: buffer.length, filePath };
});
savedContent = `<!--files:${JSON.stringify(fileMeta)}-->${text}`;
} catch (err) {
console.warn('[conversation-engine] Failed to persist file attachments:', err instanceof Error ? err.message : err);
savedContent = `[${files.length} image(s) attached] ${text}`;
}
} else {
savedContent = `[${files.length} image(s) attached] ${text}`;
}
}
addMessage(sessionId, 'user', savedContent);
// Resolve provider via unified resolver (same logic as desktop chat route)
const resolved = resolveProviderUnified({
sessionProviderId: session?.provider_id || undefined,
model: binding.model || undefined,
sessionModel: session?.model || undefined,
});
const resolvedProvider = resolved.provider;
// Use upstream model from unified resolver (same chain as chat route)
const effectiveModel = resolved.upstreamModel || resolved.model || binding.model || session?.model || getSetting('default_model') || undefined;
// Permission mode from binding mode
let permissionMode: string;
switch (binding.mode) {
case 'plan': permissionMode = 'plan'; break;
case 'ask': permissionMode = 'default'; break;
default: permissionMode = 'acceptEdits'; break;
}
// Bypass permissions entirely when session has full_access profile
const bypassPermissions = session?.permission_profile === 'full_access';
// Load conversation history for context
const { messages: recentMsgs } = getMessages(sessionId, { limit: 50 });
const historyMsgs = recentMsgs.slice(0, -1).map(m => ({
role: m.role as 'user' | 'assistant',
content: m.content,
}));
const abortController = new AbortController();
if (abortSignal) {
if (abortSignal.aborted) {
abortController.abort();
} else {
abortSignal.addEventListener('abort', () => abortController.abort(), { once: true });
}
}
// Load MCP servers from Claude config files so the SDK has access to
// user-level MCP tools, matching the desktop chat route behavior.
const mcpServers = loadMcpServers();
const stream = streamClaude({
prompt: text,
sessionId,
sdkSessionId: binding.sdkSessionId || undefined,
model: effectiveModel,
systemPrompt: session?.system_prompt || undefined,
workingDirectory: binding.workingDirectory || session?.working_directory || undefined,
abortController,
permissionMode,
provider: resolvedProvider,
sessionProviderId: session?.provider_id || undefined,
mcpServers,
conversationHistory: historyMsgs,
files,
bypassPermissions,
onRuntimeStatusChange: (status: string) => {
try { setSessionRuntimeStatus(sessionId, status); } catch { /* best effort */ }
},
});
// Consume the stream server-side (replicate collectStreamResponse pattern).
// Permission requests are forwarded immediately via the callback during streaming
// because the stream blocks until permission is resolved — we can't wait until after.
return await consumeStream(stream, sessionId, onPermissionRequest, onPartialText, onToolEvent);
} finally {
clearInterval(renewalInterval);
releaseSessionLock(sessionId, lockId);
setSessionRuntimeStatus(sessionId, 'idle');
}
}
/**
* Consume an SSE stream and extract response data.
* Mirrors the collectStreamResponse() logic from chat/route.ts.
*/
async function consumeStream(
stream: ReadableStream<string>,
sessionId: string,
onPermissionRequest?: OnPermissionRequest,
onPartialText?: OnPartialText,
onToolEvent?: OnToolEvent,
): Promise<ConversationResult> {
const reader = stream.getReader();
const contentBlocks: MessageContentBlock[] = [];
let currentText = '';
/** Monotonically accumulated text for streaming preview — never resets on tool_use. */
let previewText = '';
let tokenUsage: TokenUsage | null = null;
let hasError = false;
let errorMessage = '';
const seenToolResultIds = new Set<string>();
const permissionRequests: PermissionRequestInfo[] = [];
let capturedSdkSessionId: string | null = null;
try {
while (true) {
const { done, value } = await reader.read();
if (done) break;
const lines = value.split('\n');
for (const line of lines) {
if (!line.startsWith('data: ')) continue;
let event: SSEEvent;
try {
event = JSON.parse(line.slice(6));
} catch {
continue;
}
switch (event.type) {
case 'text':
currentText += event.data;
if (onPartialText) {
previewText += event.data;
try { onPartialText(previewText); } catch { /* non-critical */ }
}
break;
case 'tool_use': {
if (currentText.trim()) {
contentBlocks.push({ type: 'text', text: currentText });
currentText = '';
}
try {
const toolData = JSON.parse(event.data);
contentBlocks.push({
type: 'tool_use',
id: toolData.id,
name: toolData.name,
input: toolData.input,
});
if (onToolEvent) {
try { onToolEvent({ type: 'tool_use', id: toolData.id, name: toolData.name }); } catch { /* non-critical */ }
}
} catch { /* skip */ }
break;
}
case 'tool_result': {
try {
const resultData = JSON.parse(event.data);
const newBlock = {
type: 'tool_result' as const,
tool_use_id: resultData.tool_use_id,
content: resultData.content,
is_error: resultData.is_error || false,
};
if (seenToolResultIds.has(resultData.tool_use_id)) {
const idx = contentBlocks.findIndex(
(b) => b.type === 'tool_result' && 'tool_use_id' in b && b.tool_use_id === resultData.tool_use_id
);
if (idx >= 0) contentBlocks[idx] = newBlock;
} else {
seenToolResultIds.add(resultData.tool_use_id);
contentBlocks.push(newBlock);
}
if (onToolEvent) {
try { onToolEvent({ type: 'tool_result', tool_use_id: resultData.tool_use_id, is_error: resultData.is_error || false }); } catch { /* non-critical */ }
}
} catch { /* skip */ }
break;
}
case 'permission_request': {
try {
const permData = JSON.parse(event.data);
const perm: PermissionRequestInfo = {
permissionRequestId: permData.permissionRequestId,
toolName: permData.toolName,
toolInput: permData.toolInput,
suggestions: permData.suggestions,
};
permissionRequests.push(perm);
// Forward immediately — the stream blocks until the permission is
// resolved, so we must send the IM prompt *now*, not after the stream ends.
if (onPermissionRequest) {
onPermissionRequest(perm).catch((err) => {
console.error('[conversation-engine] Failed to forward permission request:', err);
});
}
} catch { /* skip */ }
break;
}
case 'status': {
try {
const statusData = JSON.parse(event.data);
if (statusData.session_id) {
capturedSdkSessionId = statusData.session_id;
updateSdkSessionId(sessionId, statusData.session_id);
}
if (statusData.model) {
updateSessionModel(sessionId, statusData.model);
}
} catch { /* skip */ }
break;
}
case 'task_update': {
try {
const taskData = JSON.parse(event.data);
if (taskData.session_id && taskData.todos) {
syncSdkTasks(taskData.session_id, taskData.todos);
}
} catch { /* skip */ }
break;
}
case 'error':
hasError = true;
errorMessage = event.data || 'Unknown error';
break;
case 'result': {
try {
const resultData = JSON.parse(event.data);
if (resultData.usage) tokenUsage = resultData.usage;
if (resultData.is_error) hasError = true;
if (resultData.session_id) {
capturedSdkSessionId = resultData.session_id;
updateSdkSessionId(sessionId, resultData.session_id);
}
} catch { /* skip */ }
break;
}
// tool_output, tool_timeout, mode_changed, done — ignored for bridge
}
}
}
// Flush remaining text
if (currentText.trim()) {
contentBlocks.push({ type: 'text', text: currentText });
}
// Save assistant message
if (contentBlocks.length > 0) {
const hasToolBlocks = contentBlocks.some(
(b) => b.type === 'tool_use' || b.type === 'tool_result'
);
const content = hasToolBlocks
? JSON.stringify(contentBlocks)
: contentBlocks
.filter((b): b is Extract<MessageContentBlock, { type: 'text' }> => b.type === 'text')
.map((b) => b.text)
.join('\n\n')
.trim();
if (content) {
addMessage(sessionId, 'assistant', content, tokenUsage ? JSON.stringify(tokenUsage) : null);
}
}
// Extract text-only response for IM delivery
const responseText = contentBlocks
.filter((b): b is Extract<MessageContentBlock, { type: 'text' }> => b.type === 'text')
.map((b) => b.text)
.join('')
.trim();
return {
responseText,
tokenUsage,
hasError,
errorMessage,
permissionRequests,
sdkSessionId: capturedSdkSessionId,
};
} catch (e) {
// Best-effort save on stream error
if (currentText.trim()) {
contentBlocks.push({ type: 'text', text: currentText });
}
if (contentBlocks.length > 0) {
const hasToolBlocks = contentBlocks.some(
(b) => b.type === 'tool_use' || b.type === 'tool_result'
);
const content = hasToolBlocks
? JSON.stringify(contentBlocks)
: contentBlocks
.filter((b): b is Extract<MessageContentBlock, { type: 'text' }> => b.type === 'text')
.map((b) => b.text)
.join('\n\n')
.trim();
if (content) {
addMessage(sessionId, 'assistant', content);
}
}
const isAbort = e instanceof DOMException && e.name === 'AbortError'
|| e instanceof Error && e.name === 'AbortError';
return {
responseText: '',
tokenUsage,
hasError: true,
errorMessage: isAbort ? 'Task stopped by user' : (e instanceof Error ? e.message : 'Stream consumption error'),
permissionRequests,
sdkSessionId: capturedSdkSessionId,
};
}
}