feat: stream LLM responses (#2677)

Co-authored-by: Michael Neale <michael.neale@gmail.com>
This commit is contained in:
Jack Amadeo
2025-07-14 14:57:03 -04:00
committed by GitHub
parent 99e4deed3e
commit fde3a578a5
34 changed files with 943 additions and 564 deletions
+1 -1
View File
@@ -786,7 +786,7 @@ function ChatContent({
<SearchView>
{filteredMessages.map((message, index) => (
<div
key={message.id || index}
key={(message.id && `${message.id}-${message.content.length}`) || index}
className="mt-4 px-4"
data-testid="message-container"
>
+1 -1
View File
@@ -130,7 +130,7 @@ export default function GooseMessage({
]);
return (
<div className="goose-message flex w-[90%] justify-start opacity-0 animate-[appear_150ms_ease-in_forwards]">
<div className="goose-message flex w-[90%] justify-start">
<div className="flex flex-col w-full">
{/* Chain-of-Thought (hidden by default) */}
{cotText && (
+21 -3
View File
@@ -1,4 +1,4 @@
import { useState, useCallback, useEffect, useRef, useId } from 'react';
import { useState, useCallback, useEffect, useRef, useId, useReducer } from 'react';
import useSWR from 'swr';
import { getSecretKey } from '../config';
import { Message, createUserMessage, hasCompletedToolCalls } from '../types/message';
@@ -235,6 +235,9 @@ export function useMessageStream({
};
}, [headers, body]);
// TODO: not this?
const [, forceUpdate] = useReducer((x) => x + 1, 0);
// Process the SSE stream from the server
const processMessageStream = useCallback(
async (response: Response, currentMessages: Message[]) => {
@@ -284,8 +287,23 @@ export function useMessageStream({
: parsedEvent.message.sendToLLM,
};
console.log('New message:', JSON.stringify(newMessage, null, 2));
// Update messages with the new message
currentMessages = [...currentMessages, newMessage];
if (
newMessage.id &&
currentMessages.length > 0 &&
currentMessages[currentMessages.length - 1].id === newMessage.id
) {
// If the last message has the same ID, update it instead of adding a new one
const lastMessage = currentMessages[currentMessages.length - 1];
lastMessage.content = [...lastMessage.content, ...newMessage.content];
forceUpdate();
} else {
currentMessages = [...currentMessages, newMessage];
}
mutate(currentMessages, false);
break;
}
@@ -373,7 +391,7 @@ export function useMessageStream({
return currentMessages;
},
[mutate, onFinish, onError]
[mutate, onFinish, onError, forceUpdate]
);
// Send a request to the server
+1 -1
View File
@@ -201,7 +201,7 @@ export function getTextContent(message: Message): string {
}
return '';
})
.join('\n');
.join('');
}
export function getToolRequests(message: Message): ToolRequestMessageContent[] {