| 1 | import type { Session, ToolCallState, ToolStatus } from "core/index.js"; |
| 2 | |
| 3 | import { streamChatResponse } from "../stream/streamChatResponse.js"; |
| 4 | import { StreamCallbacks } from "../stream/streamChatResponse.types.js"; |
| 5 | |
| 6 | // Modified version of streamChatResponse that supports interruption |
| 7 | export async function streamChatResponseWithInterruption( |
| 8 | state: ServerState, |
| 9 | llmApi: any, |
| 10 | abortController: AbortController, |
| 11 | shouldInterrupt: () => boolean, |
| 12 | ): Promise<string> { |
| 13 | // Import the original streamChatResponse logic but add interruption checks |
| 14 | // Create a wrapper that checks for interruption |
| 15 | const originalSignal = abortController.signal; |
| 16 | const checkInterruption = () => { |
| 17 | if (shouldInterrupt() && !originalSignal.aborted) { |
| 18 | abortController.abort(); |
| 19 | } |
| 20 | }; |
| 21 | |
| 22 | // Set up periodic interruption checks |
| 23 | const interruptionChecker = setInterval(checkInterruption, 100); |
| 24 | |
| 25 | // Create callbacks to capture tool events |
| 26 | const callbacks: StreamCallbacks = { |
| 27 | onContent: (_: string) => { |
| 28 | // onContent is empty - doesn't update history during streaming |
| 29 | // This is just for real-time display purposes |
| 30 | }, |
| 31 | onContentComplete: (_: string) => { |
| 32 | // Note: streamChatResponse already adds messages to history via handleToolCalls |
| 33 | // so we don't need to add them here - this callback is just for notification |
| 34 | // that content streaming is complete |
| 35 | }, |
| 36 | onToolStart: (__: string, _?: any) => { |
| 37 | // Note: handleToolCalls already adds the tool call message to history |
| 38 | // This callback is just for notification/UI updates |
| 39 | // The tool call state is already created and added by handleToolCalls |
| 40 | }, |
| 41 | onToolResult: (result: string, toolName: string, status: ToolStatus) => { |
| 42 | // Update only the tool call state status |
| 43 | // The actual result is already added as a separate message by handleToolCalls |
| 44 | for (let i = state.session.history.length - 1; i >= 0; i--) { |
| 45 | const item = state.session.history[i]; |
| 46 | if (item.toolCallStates) { |
| 47 | const toolState = item.toolCallStates.find( |
| 48 | (ts: ToolCallState) => |
| 49 | ts.toolCall.function.name === toolName && |
| 50 | (ts.status === "calling" || ts.status === "generated"), |
| 51 | ); |
| 52 | if (toolState) { |
| 53 | // Only update the status, not the output |
| 54 | toolState.status = status; |
| 55 | break; |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | }, |
| 60 | onToolError: (error: string, toolName?: string) => { |
| 61 | // Only update the tool call state to errored status when tool name is provided |
| 62 | // The error message is already added as a separate tool result message |
| 63 | // by handleToolCalls/preprocessStreamedToolCalls/executeStreamedToolCalls |
| 64 | if (toolName) { |
| 65 | // Find and update the corresponding tool call state |
| 66 | for (let i = state.session.history.length - 1; i >= 0; i--) { |
| 67 | const item = state.session.history[i]; |
| 68 | if (item.toolCallStates) { |
| 69 | const toolState = item.toolCallStates.find( |
| 70 | (ts: ToolCallState) => |
| 71 | ts.toolCall.function.name === toolName && |
| 72 | (ts.status === "calling" || ts.status === "generated"), |
| 73 | ); |
| 74 | if (toolState) { |
| 75 | // Only update the status, not the output |
| 76 | toolState.status = "errored"; |
| 77 | break; |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | }, |
| 83 | onToolPermissionRequest: ( |
| 84 | toolName: string, |
| 85 | toolArgs: any, |
| 86 | requestId: string, |
| 87 | toolCallPreview?: any[], |
| 88 | ) => { |
| 89 | // Set pending permission state |
| 90 | state.pendingPermission = { |
| 91 | toolName, |
| 92 | toolArgs, |
| 93 | requestId, |
| 94 | timestamp: Date.now(), |
| 95 | toolCallPreview, |
| 96 | }; |
| 97 | |
| 98 | // Add a system message indicating permission is needed |
| 99 | state.session.history.push({ |
| 100 | message: { |
| 101 | role: "system", |
| 102 | content: `WARNING: Tool ${toolName} requires permission`, |
| 103 | }, |
| 104 | contextItems: [], |
| 105 | }); |
| 106 | |
| 107 | // Don't wait here - the streamChatResponse will handle waiting |
| 108 | }, |
| 109 | onSystemMessage: (message: string) => { |
| 110 | state.session.history.push({ |
| 111 | message: { |
| 112 | role: "system", |
| 113 | content: message, |
| 114 | }, |
| 115 | contextItems: [], |
| 116 | }); |
| 117 | }, |
| 118 | }; |
| 119 | |
| 120 | try { |
| 121 | const response = await streamChatResponse( |
| 122 | state.session.history, |
| 123 | state.model, |
| 124 | llmApi, |
| 125 | abortController, |
| 126 | callbacks, |
| 127 | ); |
| 128 | return response || ""; |
| 129 | } finally { |
| 130 | clearInterval(interruptionChecker); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | export interface PendingPermission { |
| 135 | toolName: string; |
| 136 | toolArgs: any; |
| 137 | requestId: string; |
| 138 | timestamp: number; |
| 139 | toolCallPreview?: any[]; |
| 140 | } |
| 141 | |
| 142 | export interface ServerState { |
| 143 | session: Session; |
| 144 | config: any; |
| 145 | model: any; |
| 146 | isProcessing: boolean; |
| 147 | lastActivity: number; |
| 148 | messageQueue: string[]; |
| 149 | currentAbortController: AbortController | null; |
| 150 | shouldInterrupt: boolean; |
| 151 | serverRunning: boolean; |
| 152 | pendingPermission: PendingPermission | null; |
| 153 | systemMessage?: string; |
| 154 | } |