fix: improve streaming message handling and API endpoint
This commit is contained in:
parent
eb59dcc76a
commit
b822b938f1
4 changed files with 53 additions and 32 deletions
|
|
@ -132,7 +132,7 @@ function getAuthUrlForTokenPage(useOnboarding: boolean = false): string {
|
|||
*/
|
||||
async function refreshToken(refreshToken: string): Promise<AuthConfig> {
|
||||
try {
|
||||
const response = await axios.post(`${env.apiBase}auth/refresh`, {
|
||||
const response = await axios.post(`${env.apiBase}/auth/refresh`, {
|
||||
refreshToken,
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -135,6 +135,5 @@ export function constructSystemMessage(rulesSystemMessage: string): string {
|
|||
systemMessage += "\n</context>";
|
||||
}
|
||||
|
||||
console.log("SYS: ", systemMessage);
|
||||
return systemMessage;
|
||||
}
|
||||
}
|
||||
|
|
@ -89,7 +89,8 @@ const TUIChat: React.FC<TUIChatProps> = ({
|
|||
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<TUIChatProps> = ({
|
|||
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<TUIChatProps> = ({
|
|||
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) => [
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue