| 1 | import type { Session, ToolStatus } from "core/index.js"; |
| 2 | |
| 3 | import { services } from "../services/index.js"; |
| 4 | import { streamChatResponse } from "../stream/streamChatResponse.js"; |
| 5 | import { StreamCallbacks } from "../stream/streamChatResponse.types.js"; |
| 6 | import { logger } from "../util/logger.js"; |
| 7 | |
| 8 | // Modified version of streamChatResponse that supports interruption |
| 9 | export async function streamChatResponseWithInterruption( |
| 10 | state: ServerState, |
| 11 | llmApi: any, |
| 12 | abortController: AbortController, |
| 13 | shouldInterrupt: () => boolean, |
| 14 | ): Promise<string> { |
| 15 | // Import the original streamChatResponse logic but add interruption checks |
| 16 | // Create a wrapper that checks for interruption |
| 17 | const originalSignal = abortController.signal; |
| 18 | const checkInterruption = () => { |
| 19 | if (shouldInterrupt() && !originalSignal.aborted) { |
| 20 | abortController.abort(); |
| 21 | } |
| 22 | }; |
| 23 | |
| 24 | // Set up periodic interruption checks |
| 25 | const interruptionChecker = setInterval(checkInterruption, 100); |
| 26 | |
| 27 | // Create callbacks to capture tool events |
| 28 | const callbacks: StreamCallbacks = { |
| 29 | onContent: (_: string) => { |
| 30 | // onContent is empty - doesn't update history during streaming |
| 31 | // This is just for real-time display purposes |
| 32 | }, |
| 33 | onContentComplete: (_: string) => { |
| 34 | // Note: streamChatResponse already adds messages to history via handleToolCalls |
| 35 | // so we don't need to add them here - this callback is just for notification |
| 36 | // that content streaming is complete |
| 37 | }, |
| 38 | onToolStart: (__: string, _?: any) => { |
| 39 | // Note: handleToolCalls already adds the tool call message to history |
| 40 | // This callback is just for notification/UI updates |
| 41 | // The tool call state is already created and added by handleToolCalls |
| 42 | }, |
| 43 | onToolResult: (_result: string, _toolName: string, _status: ToolStatus) => { |
| 44 | // No-op when using ChatHistoryService; it updates tool states/results |
| 45 | }, |
| 46 | onToolError: (_error: string, _toolName?: string) => { |
| 47 | // No-op; errors are added to history via handleToolCalls flow |
| 48 | }, |
| 49 | onToolPermissionRequest: ( |
| 50 | toolName: string, |
| 51 | toolArgs: any, |
| 52 | requestId: string, |
| 53 | toolCallPreview?: any[], |
| 54 | ) => { |
| 55 | // Set pending permission state |
| 56 | state.pendingPermission = { |
| 57 | toolName, |
| 58 | toolArgs, |
| 59 | requestId, |
| 60 | timestamp: Date.now(), |
| 61 | toolCallPreview, |
| 62 | }; |
| 63 | |
| 64 | // Add a system message indicating permission is needed via service |
| 65 | try { |
| 66 | services.chatHistory.addSystemMessage( |
| 67 | `WARNING: Tool ${toolName} requires permission`, |
| 68 | ); |
| 69 | } catch (err) { |
| 70 | // Do not mutate session history; ChatHistoryService is the source of truth |
| 71 | logger.error( |
| 72 | "Failed to add system message via ChatHistoryService", |
| 73 | err, |
| 74 | { context: "onToolPermissionRequest", toolName, requestId }, |
| 75 | ); |
| 76 | } |
| 77 | |
| 78 | // Don't wait here - the streamChatResponse will handle waiting |
| 79 | }, |
| 80 | onSystemMessage: (message: string) => { |
| 81 | try { |
| 82 | services.chatHistory.addSystemMessage(message); |
| 83 | } catch (err) { |
| 84 | // Do not mutate session history; ChatHistoryService is the source of truth |
| 85 | logger.error( |
| 86 | "Failed to add system message via ChatHistoryService", |
| 87 | err, |
| 88 | { context: "onSystemMessage" }, |
| 89 | ); |
| 90 | } |
| 91 | }, |
| 92 | }; |
| 93 | |
| 94 | try { |
| 95 | const response = await streamChatResponse( |
| 96 | state.session.history, |
| 97 | state.model, |
| 98 | llmApi, |
| 99 | abortController, |
| 100 | callbacks, |
| 101 | ); |
| 102 | return response || ""; |
| 103 | } finally { |
| 104 | clearInterval(interruptionChecker); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | export interface PendingPermission { |
| 109 | toolName: string; |
| 110 | toolArgs: any; |
| 111 | requestId: string; |
| 112 | timestamp: number; |
| 113 | toolCallPreview?: any[]; |
| 114 | } |
| 115 | |
| 116 | export interface ServerState { |
| 117 | session: Session; |
| 118 | config: any; |
| 119 | model: any; |
| 120 | isProcessing: boolean; |
| 121 | lastActivity: number; |
| 122 | currentAbortController: AbortController | null; |
| 123 | serverRunning: boolean; |
| 124 | pendingPermission: PendingPermission | null; |
| 125 | systemMessage?: string; |
| 126 | } |