| 1 | import type { ChatHistoryItem, ToolCallState, ToolStatus } from "core/index.js"; |
| 2 | |
| 3 | interface CreateStreamCallbacksOptions { |
| 4 | setChatHistory: React.Dispatch<React.SetStateAction<ChatHistoryItem[]>>; |
| 5 | setActivePermissionRequest: React.Dispatch<React.SetStateAction<any>>; |
| 6 | } |
| 7 | |
| 8 | /** |
| 9 | * Create stream callbacks for chat response |
| 10 | */ |
| 11 | export function createStreamCallbacks( |
| 12 | options: CreateStreamCallbacksOptions, |
| 13 | ): any { |
| 14 | const { setChatHistory, setActivePermissionRequest } = options; |
| 15 | |
| 16 | return { |
| 17 | onContent: (_: string) => {}, |
| 18 | |
| 19 | onContentComplete: (content: string) => { |
| 20 | setChatHistory((prev) => [ |
| 21 | ...prev, |
| 22 | { |
| 23 | contextItems: [], |
| 24 | message: { |
| 25 | role: "assistant", |
| 26 | content: content, |
| 27 | isStreaming: false, |
| 28 | }, |
| 29 | }, |
| 30 | ]); |
| 31 | }, |
| 32 | |
| 33 | onToolStart: (toolName: string, toolArgs?: any) => { |
| 34 | setChatHistory((prev) => { |
| 35 | const newHistory = [...prev]; |
| 36 | |
| 37 | // Always create a new assistant message for each tool call |
| 38 | const toolCallId = `tool_${Date.now()}_${Math.random().toString(36).substring(2, 15)}`; |
| 39 | const toolCallState: ToolCallState = { |
| 40 | toolCallId: toolCallId, |
| 41 | toolCall: { |
| 42 | id: toolCallId, |
| 43 | type: "function", |
| 44 | function: { |
| 45 | name: toolName, |
| 46 | arguments: JSON.stringify(toolArgs || {}), |
| 47 | }, |
| 48 | }, |
| 49 | status: "calling", |
| 50 | parsedArgs: toolArgs, |
| 51 | }; |
| 52 | |
| 53 | newHistory.push({ |
| 54 | message: { |
| 55 | role: "assistant", |
| 56 | content: "", |
| 57 | toolCalls: [ |
| 58 | { |
| 59 | id: toolCallId, |
| 60 | type: "function", |
| 61 | function: { |
| 62 | name: toolName, |
| 63 | arguments: JSON.stringify(toolArgs || {}), |
| 64 | }, |
| 65 | }, |
| 66 | ], |
| 67 | }, |
| 68 | contextItems: [], |
| 69 | toolCallStates: [toolCallState], |
| 70 | }); |
| 71 | |
| 72 | return newHistory; |
| 73 | }); |
| 74 | }, |
| 75 | |
| 76 | onToolResult: (result: string, toolName: string, status: ToolStatus) => { |
| 77 | setChatHistory((prev) => { |
| 78 | const newHistory = [...prev]; |
| 79 | |
| 80 | // Find the tool call state and update it |
| 81 | for (let i = newHistory.length - 1; i >= 0; i--) { |
| 82 | const item = newHistory[i]; |
| 83 | if (item.toolCallStates) { |
| 84 | const toolState = item.toolCallStates.find( |
| 85 | (ts) => |
| 86 | ts.toolCall.function.name === toolName && |
| 87 | ts.status === "calling", |
| 88 | ); |
| 89 | |
| 90 | if (toolState) { |
| 91 | toolState.status = status; |
| 92 | toolState.output = [ |
| 93 | { |
| 94 | content: result, |
| 95 | name: `Tool Result: ${toolName}`, |
| 96 | description: "Tool execution result", |
| 97 | }, |
| 98 | ]; |
| 99 | break; |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | return newHistory; |
| 105 | }); |
| 106 | }, |
| 107 | |
| 108 | onToolError: (error: string, toolName?: string) => { |
| 109 | setChatHistory((prev) => { |
| 110 | const newHistory = [...prev]; |
| 111 | |
| 112 | // Find the tool call state and mark it as errored |
| 113 | if (toolName) { |
| 114 | for (let i = newHistory.length - 1; i >= 0; i--) { |
| 115 | const item = newHistory[i]; |
| 116 | if (item.toolCallStates) { |
| 117 | const toolState = item.toolCallStates.find( |
| 118 | (ts) => |
| 119 | ts.toolCall.function.name === toolName && |
| 120 | ts.status === "calling", |
| 121 | ); |
| 122 | |
| 123 | if (toolState) { |
| 124 | toolState.status = "errored"; |
| 125 | toolState.output = [ |
| 126 | { |
| 127 | content: error, |
| 128 | name: `Tool Error: ${toolName}`, |
| 129 | description: "Tool execution error", |
| 130 | }, |
| 131 | ]; |
| 132 | break; |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | } else { |
| 137 | // General error message |
| 138 | newHistory.push({ |
| 139 | message: { |
| 140 | role: "system", |
| 141 | content: error, |
| 142 | }, |
| 143 | contextItems: [], |
| 144 | }); |
| 145 | } |
| 146 | |
| 147 | return newHistory; |
| 148 | }); |
| 149 | }, |
| 150 | |
| 151 | onToolPermissionRequest: ( |
| 152 | toolName: string, |
| 153 | toolArgs: any, |
| 154 | requestId: string, |
| 155 | toolCallPreview?: any[], |
| 156 | ) => { |
| 157 | setActivePermissionRequest({ |
| 158 | toolName, |
| 159 | toolArgs, |
| 160 | requestId, |
| 161 | toolCallPreview, |
| 162 | }); |
| 163 | }, |
| 164 | |
| 165 | onSystemMessage: (message: string) => { |
| 166 | setChatHistory((prev) => [ |
| 167 | ...prev, |
| 168 | { |
| 169 | message: { |
| 170 | role: "system", |
| 171 | content: message, |
| 172 | }, |
| 173 | contextItems: [], |
| 174 | }, |
| 175 | ]); |
| 176 | }, |
| 177 | }; |
| 178 | } |
| 179 | |
| 180 | interface ExecuteStreamingOptions { |
| 181 | chatHistory: ChatHistoryItem[]; |
| 182 | model: any; |
| 183 | llmApi: any; |
| 184 | controller: AbortController; |
| 185 | streamCallbacks: any; |
| 186 | currentCompactionIndex: number | null; |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Execute streaming chat response |
| 191 | */ |
| 192 | export async function executeStreaming({ |
| 193 | chatHistory, |
| 194 | model, |
| 195 | llmApi, |
| 196 | controller, |
| 197 | streamCallbacks, |
| 198 | currentCompactionIndex, |
| 199 | }: ExecuteStreamingOptions): Promise<void> { |
| 200 | const { getHistoryForLLM } = await import("../../compaction.js"); |
| 201 | const { streamChatResponse } = await import("../../streamChatResponse.js"); |
| 202 | |
| 203 | if (model && llmApi) { |
| 204 | if ( |
| 205 | currentCompactionIndex !== null && |
| 206 | currentCompactionIndex !== undefined |
| 207 | ) { |
| 208 | const historyForLLM = getHistoryForLLM( |
| 209 | chatHistory, |
| 210 | currentCompactionIndex, |
| 211 | ); |
| 212 | const _originalLength = historyForLLM.length; |
| 213 | |
| 214 | await streamChatResponse( |
| 215 | historyForLLM, |
| 216 | model, |
| 217 | llmApi, |
| 218 | controller, |
| 219 | streamCallbacks, |
| 220 | ); |
| 221 | |
| 222 | // streamChatResponse modifies the array in place |
| 223 | // The new messages are already in historyForLLM |
| 224 | } else { |
| 225 | await streamChatResponse( |
| 226 | chatHistory, |
| 227 | model, |
| 228 | llmApi, |
| 229 | controller, |
| 230 | streamCallbacks, |
| 231 | ); |
| 232 | } |
| 233 | } |
| 234 | } |