(
newHistory: ChatHistoryItem[],
currentCompactionIndex: number | null,
)
| 292 | }, [initialPrompt, additionalPrompts, isChatHistoryInitialized]); |
| 293 | |
| 294 | const executeStreamingResponse = async ( |
| 295 | newHistory: ChatHistoryItem[], |
| 296 | currentCompactionIndex: number | null, |
| 297 | ) => { |
| 298 | // Clean up previous abort controller if it exists |
| 299 | if (abortController && !abortController.signal.aborted) { |
| 300 | abortController.abort(); |
| 301 | } |
| 302 | |
| 303 | // Start streaming response |
| 304 | const controller = new AbortController(); |
| 305 | setAbortController(controller); |
| 306 | setIsWaitingForResponse(true); |
| 307 | setResponseStartTime(Date.now()); |
| 308 | setInputMode(false); |
| 309 | logger.debug("Starting chat response stream", { |
| 310 | historyLength: newHistory.length, |
| 311 | }); |
| 312 | |
| 313 | try { |
| 314 | const streamCallbacks = createStreamCallbacks({ |
| 315 | setChatHistory: setChatHistory, |
| 316 | setActivePermissionRequest, |
| 317 | llmApi, |
| 318 | model, |
| 319 | }); |
| 320 | |
| 321 | // Execute streaming chat response |
| 322 | await executeStreaming({ |
| 323 | chatHistory: newHistory, |
| 324 | model, |
| 325 | llmApi, |
| 326 | controller, |
| 327 | streamCallbacks, |
| 328 | currentCompactionIndex, |
| 329 | }); |
| 330 | |
| 331 | // Save the updated session with the latest chat history that includes the assistant's reply |
| 332 | // The streamCallbacks update setChatHistory during streaming, so we need to get the current state |
| 333 | setChatHistory((currentHistory) => { |
| 334 | const updatedSession: Session = { |
| 335 | ...currentSession, |
| 336 | history: currentHistory, |
| 337 | }; |
| 338 | updateSessionHistory(currentHistory); |
| 339 | setCurrentSession(updatedSession); |
| 340 | logger.debug("Session saved"); |
| 341 | return currentHistory; |
| 342 | }); |
| 343 | } catch (error: any) { |
| 344 | const errorMessage = `Error: ${formatError(error)}`; |
| 345 | setChatHistory((prev) => [ |
| 346 | ...prev, |
| 347 | { |
| 348 | message: { |
| 349 | role: "system", |
| 350 | content: errorMessage, |
| 351 | }, |
no test coverage detected