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> {
|
async function refreshToken(refreshToken: string): Promise<AuthConfig> {
|
||||||
try {
|
try {
|
||||||
const response = await axios.post(`${env.apiBase}auth/refresh`, {
|
const response = await axios.post(`${env.apiBase}/auth/refresh`, {
|
||||||
refreshToken,
|
refreshToken,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -52,6 +52,7 @@ type TODO = any;
|
||||||
|
|
||||||
export interface StreamCallbacks {
|
export interface StreamCallbacks {
|
||||||
onContent?: (content: string) => void;
|
onContent?: (content: string) => void;
|
||||||
|
onContentComplete?: (content: string) => void;
|
||||||
onToolStart?: (toolName: string, toolArgs?: any) => void;
|
onToolStart?: (toolName: string, toolArgs?: any) => void;
|
||||||
onToolResult?: (result: string, toolName: string) => void;
|
onToolResult?: (result: string, toolName: string) => void;
|
||||||
onToolError?: (error: 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
|
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
|
// Add the assistant's response to chat history if there's content or tool calls
|
||||||
if (currentToolCalls.length > 0) {
|
if (currentToolCalls.length > 0) {
|
||||||
const toolCalls: ChatCompletionMessageToolCall[] = currentToolCalls.map(
|
const toolCalls: ChatCompletionMessageToolCall[] = currentToolCalls.map(
|
||||||
|
|
@ -207,6 +213,10 @@ export async function streamChatResponse(
|
||||||
} else if (aiResponse.trim()) {
|
} else if (aiResponse.trim()) {
|
||||||
// Only add assistant response if there's actual content
|
// Only add assistant response if there's actual content
|
||||||
chatHistory.push({ role: "assistant", content: aiResponse });
|
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
|
// If we have tool calls, execute them
|
||||||
|
|
|
||||||
|
|
@ -135,6 +135,5 @@ export function constructSystemMessage(rulesSystemMessage: string): string {
|
||||||
systemMessage += "\n</context>";
|
systemMessage += "\n</context>";
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log("SYS: ", systemMessage);
|
|
||||||
return systemMessage;
|
return systemMessage;
|
||||||
}
|
}
|
||||||
|
|
@ -89,7 +89,8 @@ const TUIChat: React.FC<TUIChatProps> = ({
|
||||||
role: "user",
|
role: "user",
|
||||||
content: message,
|
content: message,
|
||||||
};
|
};
|
||||||
setChatHistory((prev) => [...prev, newUserMessage]);
|
const newHistory = [...chatHistory, newUserMessage];
|
||||||
|
setChatHistory(newHistory);
|
||||||
setMessages((prev) => [...prev, { role: "user", content: message }]);
|
setMessages((prev) => [...prev, { role: "user", content: message }]);
|
||||||
|
|
||||||
// Start streaming response
|
// Start streaming response
|
||||||
|
|
@ -98,29 +99,45 @@ const TUIChat: React.FC<TUIChatProps> = ({
|
||||||
setIsWaitingForResponse(true);
|
setIsWaitingForResponse(true);
|
||||||
setInputMode(false);
|
setInputMode(false);
|
||||||
|
|
||||||
// Add placeholder for streaming message
|
|
||||||
setMessages((prev) => [
|
|
||||||
...prev,
|
|
||||||
{ role: "assistant", content: "", isStreaming: true },
|
|
||||||
]);
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const newHistory = [...chatHistory, newUserMessage];
|
let currentStreamingMessage: DisplayMessage | null = null;
|
||||||
let assistantResponse = "";
|
|
||||||
|
|
||||||
// Create callbacks to handle streaming updates
|
// Create callbacks to handle streaming updates
|
||||||
const streamCallbacks: StreamCallbacks = {
|
const streamCallbacks: StreamCallbacks = {
|
||||||
onContent: (content: string) => {
|
onContent: (content: string) => {
|
||||||
assistantResponse += content;
|
|
||||||
setMessages((prev) => {
|
setMessages((prev) => {
|
||||||
const newMessages = [...prev];
|
const newMessages = [...prev];
|
||||||
const lastMessage = newMessages[newMessages.length - 1];
|
|
||||||
if (lastMessage.isStreaming) {
|
// Find or create current streaming message
|
||||||
lastMessage.content += content;
|
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;
|
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) => {
|
onToolStart: (toolName: string, toolArgs?: any) => {
|
||||||
const formatToolCall = (name: string, args: any) => {
|
const formatToolCall = (name: string, args: any) => {
|
||||||
const displayName = getToolDisplayName(name);
|
const displayName = getToolDisplayName(name);
|
||||||
|
|
@ -182,22 +199,17 @@ const TUIChat: React.FC<TUIChatProps> = ({
|
||||||
controller
|
controller
|
||||||
);
|
);
|
||||||
|
|
||||||
// Finalize the assistant message
|
// Finalize any remaining streaming message
|
||||||
const finalAssistantMessage: ChatCompletionMessageParam = {
|
if (currentStreamingMessage) {
|
||||||
role: "assistant",
|
setMessages((prev) => {
|
||||||
content: assistantResponse,
|
const newMessages = [...prev];
|
||||||
};
|
const streamingIndex = newMessages.indexOf(currentStreamingMessage!);
|
||||||
setChatHistory((prev) => [...prev, finalAssistantMessage]);
|
if (streamingIndex >= 0) {
|
||||||
|
newMessages[streamingIndex].isStreaming = false;
|
||||||
// Update messages to remove streaming flag
|
}
|
||||||
setMessages((prev) => {
|
return newMessages;
|
||||||
const newMessages = [...prev];
|
});
|
||||||
const lastMessage = newMessages[newMessages.length - 1];
|
}
|
||||||
if (lastMessage.isStreaming) {
|
|
||||||
lastMessage.isStreaming = false;
|
|
||||||
}
|
|
||||||
return newMessages;
|
|
||||||
});
|
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
const errorMessage = `Error: ${error.message}`;
|
const errorMessage = `Error: ${error.message}`;
|
||||||
setMessages((prev) => [
|
setMessages((prev) => [
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue