| 1 | import { ChatCompletionMessageParam } from "openai/resources.mjs"; |
| 2 | |
| 3 | import { formatToolCall } from "../../tools/index.js"; |
| 4 | import { DisplayMessage } from "../types.js"; |
| 5 | |
| 6 | interface CreateStreamCallbacksOptions { |
| 7 | setMessages: React.Dispatch<React.SetStateAction<DisplayMessage[]>>; |
| 8 | setActivePermissionRequest: React.Dispatch<React.SetStateAction<any>>; |
| 9 | } |
| 10 | |
| 11 | /** |
| 12 | * Create stream callbacks for chat response |
| 13 | */ |
| 14 | export function createStreamCallbacks( |
| 15 | options: CreateStreamCallbacksOptions, |
| 16 | currentStreamingMessageRef: { current: any }, |
| 17 | ): any { |
| 18 | const { setMessages, setActivePermissionRequest } = options; |
| 19 | |
| 20 | return { |
| 21 | onContent: (content: string) => { |
| 22 | if (!currentStreamingMessageRef.current) { |
| 23 | currentStreamingMessageRef.current = { |
| 24 | role: "assistant", |
| 25 | content: "", |
| 26 | isStreaming: true, |
| 27 | }; |
| 28 | } |
| 29 | currentStreamingMessageRef.current.content += content; |
| 30 | }, |
| 31 | onContentComplete: (content: string) => { |
| 32 | if (currentStreamingMessageRef.current) { |
| 33 | setMessages((prev) => [ |
| 34 | ...prev, |
| 35 | { |
| 36 | role: "assistant", |
| 37 | content: content, |
| 38 | isStreaming: false, |
| 39 | }, |
| 40 | ]); |
| 41 | currentStreamingMessageRef.current = null; |
| 42 | } |
| 43 | }, |
| 44 | onToolStart: (toolName: string, toolArgs?: any) => { |
| 45 | if ( |
| 46 | currentStreamingMessageRef.current && |
| 47 | currentStreamingMessageRef.current.content |
| 48 | ) { |
| 49 | const messageContent = currentStreamingMessageRef.current.content; |
| 50 | setMessages((prev) => [ |
| 51 | ...prev, |
| 52 | { |
| 53 | role: "assistant", |
| 54 | content: messageContent, |
| 55 | isStreaming: false, |
| 56 | }, |
| 57 | ]); |
| 58 | currentStreamingMessageRef.current = null; |
| 59 | } |
| 60 | |
| 61 | setMessages((prev) => [ |
| 62 | ...prev, |
| 63 | { |
| 64 | role: "system", |
| 65 | content: formatToolCall(toolName, toolArgs), |
| 66 | messageType: "tool-start", |
| 67 | toolName, |
| 68 | }, |
| 69 | ]); |
| 70 | }, |
| 71 | onToolResult: (result: string, toolName: string) => { |
| 72 | setMessages((prev) => { |
| 73 | const newMessages = [...prev]; |
| 74 | for (let i = newMessages.length - 1; i >= 0; i--) { |
| 75 | if ( |
| 76 | newMessages[i].messageType === "tool-start" && |
| 77 | newMessages[i].toolName === toolName |
| 78 | ) { |
| 79 | newMessages[i] = { |
| 80 | ...newMessages[i], |
| 81 | messageType: "tool-result", |
| 82 | toolResult: result, |
| 83 | }; |
| 84 | break; |
| 85 | } |
| 86 | } |
| 87 | return newMessages; |
| 88 | }); |
| 89 | }, |
| 90 | onToolError: (error: string, toolName?: string) => { |
| 91 | setMessages((prev) => [ |
| 92 | ...prev, |
| 93 | { |
| 94 | role: "system", |
| 95 | content: error, |
| 96 | messageType: "tool-error", |
| 97 | toolName, |
| 98 | }, |
| 99 | ]); |
| 100 | }, |
| 101 | onToolPermissionRequest: ( |
| 102 | toolName: string, |
| 103 | toolArgs: any, |
| 104 | requestId: string, |
| 105 | toolCallPreview?: any[], |
| 106 | ) => { |
| 107 | setActivePermissionRequest({ |
| 108 | toolName, |
| 109 | toolArgs, |
| 110 | requestId, |
| 111 | toolCallPreview, |
| 112 | }); |
| 113 | }, |
| 114 | }; |
| 115 | } |
| 116 | |
| 117 | interface ExecuteStreamingOptions { |
| 118 | newHistory: ChatCompletionMessageParam[]; |
| 119 | model: any; |
| 120 | llmApi: any; |
| 121 | controller: AbortController; |
| 122 | streamCallbacks: any; |
| 123 | currentCompactionIndex: number | null; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Execute streaming chat response |
| 128 | */ |
| 129 | export async function executeStreaming({ |
| 130 | newHistory, |
| 131 | model, |
| 132 | llmApi, |
| 133 | controller, |
| 134 | streamCallbacks, |
| 135 | currentCompactionIndex, |
| 136 | }: ExecuteStreamingOptions): Promise<void> { |
| 137 | const { getHistoryForLLM } = await import("../../compaction.js"); |
| 138 | const { streamChatResponse } = await import("../../streamChatResponse.js"); |
| 139 | |
| 140 | if (model && llmApi) { |
| 141 | if ( |
| 142 | currentCompactionIndex !== null && |
| 143 | currentCompactionIndex !== undefined |
| 144 | ) { |
| 145 | const historyForLLM = getHistoryForLLM( |
| 146 | newHistory, |
| 147 | currentCompactionIndex, |
| 148 | ); |
| 149 | const originalLength = historyForLLM.length; |
| 150 | |
| 151 | await streamChatResponse( |
| 152 | historyForLLM, |
| 153 | model, |
| 154 | llmApi, |
| 155 | controller, |
| 156 | streamCallbacks, |
| 157 | ); |
| 158 | |
| 159 | const newMessages = historyForLLM.slice(originalLength); |
| 160 | newHistory.push(...newMessages); |
| 161 | } else { |
| 162 | await streamChatResponse( |
| 163 | newHistory, |
| 164 | model, |
| 165 | llmApi, |
| 166 | controller, |
| 167 | streamCallbacks, |
| 168 | ); |
| 169 | } |
| 170 | } |
| 171 | } |