From b822b938f110af2de2c040f45cd3fca770d9a4a7 Mon Sep 17 00:00:00 2001 From: Nate Date: Sun, 13 Jul 2025 23:04:07 -0700 Subject: [PATCH] fix: improve streaming message handling and API endpoint --- src/auth/workos.ts | 2 +- src/streamChatResponse.ts | 10 ++++++ src/systemMessage.ts | 3 +- src/ui/TUIChat.tsx | 70 +++++++++++++++++++++++---------------- 4 files changed, 53 insertions(+), 32 deletions(-) diff --git a/src/auth/workos.ts b/src/auth/workos.ts index 91b32733e..e99183f14 100644 --- a/src/auth/workos.ts +++ b/src/auth/workos.ts @@ -132,7 +132,7 @@ function getAuthUrlForTokenPage(useOnboarding: boolean = false): string { */ async function refreshToken(refreshToken: string): Promise { try { - const response = await axios.post(`${env.apiBase}auth/refresh`, { + const response = await axios.post(`${env.apiBase}/auth/refresh`, { refreshToken, }); diff --git a/src/streamChatResponse.ts b/src/streamChatResponse.ts index 3ac1e5649..9526e0701 100644 --- a/src/streamChatResponse.ts +++ b/src/streamChatResponse.ts @@ -52,6 +52,7 @@ type TODO = any; export interface StreamCallbacks { onContent?: (content: string) => void; + onContentComplete?: (content: string) => void; onToolStart?: (toolName: string, toolArgs?: any) => void; onToolResult?: (result: string, toolName: string) => void; onToolError?: (error: string, toolName?: string) => void; @@ -187,6 +188,11 @@ export async function streamChatResponse( console.info(); // Add a newline after the response } + // Notify that content is complete if we have content and are about to process tool calls + if (aiResponse.trim() && currentToolCalls.length > 0 && callbacks?.onContentComplete) { + callbacks.onContentComplete(aiResponse); + } + // Add the assistant's response to chat history if there's content or tool calls if (currentToolCalls.length > 0) { const toolCalls: ChatCompletionMessageToolCall[] = currentToolCalls.map( @@ -207,6 +213,10 @@ export async function streamChatResponse( } else if (aiResponse.trim()) { // Only add assistant response if there's actual content chatHistory.push({ role: "assistant", content: aiResponse }); + // Also notify content complete for standalone messages + if (callbacks?.onContentComplete) { + callbacks.onContentComplete(aiResponse); + } } // If we have tool calls, execute them diff --git a/src/systemMessage.ts b/src/systemMessage.ts index 9e6f013f5..ae13dda8a 100644 --- a/src/systemMessage.ts +++ b/src/systemMessage.ts @@ -135,6 +135,5 @@ export function constructSystemMessage(rulesSystemMessage: string): string { systemMessage += "\n"; } - console.log("SYS: ", systemMessage); return systemMessage; -} +} \ No newline at end of file diff --git a/src/ui/TUIChat.tsx b/src/ui/TUIChat.tsx index f1ec8d6f0..25db23d85 100644 --- a/src/ui/TUIChat.tsx +++ b/src/ui/TUIChat.tsx @@ -89,7 +89,8 @@ const TUIChat: React.FC = ({ role: "user", content: message, }; - setChatHistory((prev) => [...prev, newUserMessage]); + const newHistory = [...chatHistory, newUserMessage]; + setChatHistory(newHistory); setMessages((prev) => [...prev, { role: "user", content: message }]); // Start streaming response @@ -98,29 +99,45 @@ const TUIChat: React.FC = ({ setIsWaitingForResponse(true); setInputMode(false); - // Add placeholder for streaming message - setMessages((prev) => [ - ...prev, - { role: "assistant", content: "", isStreaming: true }, - ]); - try { - const newHistory = [...chatHistory, newUserMessage]; - let assistantResponse = ""; + let currentStreamingMessage: DisplayMessage | null = null; // Create callbacks to handle streaming updates const streamCallbacks: StreamCallbacks = { onContent: (content: string) => { - assistantResponse += content; setMessages((prev) => { const newMessages = [...prev]; - const lastMessage = newMessages[newMessages.length - 1]; - if (lastMessage.isStreaming) { - lastMessage.content += content; + + // Find or create current streaming message + if (!currentStreamingMessage) { + currentStreamingMessage = { role: "assistant", content: "", isStreaming: true }; + newMessages.push(currentStreamingMessage); } + + // Update the streaming message + const streamingIndex = newMessages.indexOf(currentStreamingMessage); + if (streamingIndex >= 0) { + newMessages[streamingIndex].content += content; + } + return newMessages; }); }, + onContentComplete: (content: string) => { + // Finalize the current streaming message + if (currentStreamingMessage) { + setMessages((prev) => { + const newMessages = [...prev]; + const streamingIndex = newMessages.indexOf(currentStreamingMessage!); + if (streamingIndex >= 0) { + newMessages[streamingIndex].isStreaming = false; + newMessages[streamingIndex].content = content; + } + return newMessages; + }); + currentStreamingMessage = null; + } + }, onToolStart: (toolName: string, toolArgs?: any) => { const formatToolCall = (name: string, args: any) => { const displayName = getToolDisplayName(name); @@ -182,22 +199,17 @@ const TUIChat: React.FC = ({ controller ); - // Finalize the assistant message - const finalAssistantMessage: ChatCompletionMessageParam = { - role: "assistant", - content: assistantResponse, - }; - setChatHistory((prev) => [...prev, finalAssistantMessage]); - - // Update messages to remove streaming flag - setMessages((prev) => { - const newMessages = [...prev]; - const lastMessage = newMessages[newMessages.length - 1]; - if (lastMessage.isStreaming) { - lastMessage.isStreaming = false; - } - return newMessages; - }); + // Finalize any remaining streaming message + if (currentStreamingMessage) { + setMessages((prev) => { + const newMessages = [...prev]; + const streamingIndex = newMessages.indexOf(currentStreamingMessage!); + if (streamingIndex >= 0) { + newMessages[streamingIndex].isStreaming = false; + } + return newMessages; + }); + } } catch (error: any) { const errorMessage = `Error: ${error.message}`; setMessages((prev) => [