@@ -96,20 +96,8 @@ export function useMessages(threadId: number | null, onError?: (error: string) =
9696 };
9797 setMessages((prev) => [...prev, userMsg]);
9898
99- // Create assistant message placeholder
99+ // Create assistant message placeholder (but don't add it yet - wait for content)
100100 const assistantMsgId = Date.now() + 1;
101- const botMsg: Message = {
102- id: assistantMsgId,
103- threadId: targetThreadId,
104- role: "assistant",
105- content: "",
106- createdAt: new Date(),
107- model: null,
108- maxPromptLength: null,
109- generationTimeMs: null,
110- toolCallCounts: null,
111- };
112- setMessages((prev) => [...prev, botMsg]);
113101 setStreamingMessageId(assistantMsgId);
114102
115103 // Create AbortController for this request
@@ -141,6 +129,7 @@ export function useMessages(threadId: number | null, onError?: (error: string) =
141129 let buffer = "";
142130 let accumulatedContent = "";
143131 let shouldStop = false;
132+ let messageAdded = false;
144133
145134 while (true) {
146135 const { done, value } = await reader.read();
@@ -157,28 +146,62 @@ export function useMessages(threadId: number | null, onError?: (error: string) =
157146 const data = JSON.parse(line.slice(6));
158147 if (data.type === "content") {
159148 accumulatedContent += data.content || "";
160- setMessages((prev) =>
161- prev.map((msg) =>
162- msg.id === assistantMsgId ? { ...msg, content: accumulatedContent } : msg
163- )
164- );
149+ // Only add the message when we have content
150+ if (!messageAdded && accumulatedContent.trim()) {
151+ const botMsg: Message = {
152+ id: assistantMsgId,
153+ threadId: targetThreadId,
154+ role: "assistant",
155+ content: accumulatedContent,
156+ createdAt: new Date(),
157+ model: null,
158+ maxPromptLength: null,
159+ generationTimeMs: null,
160+ toolCallCounts: null,
161+ };
162+ setMessages((prev) => [...prev, botMsg]);
163+ messageAdded = true;
164+ } else if (messageAdded) {
165+ setMessages((prev) =>
166+ prev.map((msg) =>
167+ msg.id === assistantMsgId ? { ...msg, content: accumulatedContent } : msg
168+ )
169+ );
170+ }
165171 } else if (data.type === "done") {
166172 // Final update with complete answer and metadata
167- setMessages((prev) =>
168- prev.map((msg) =>
169- msg.id === assistantMsgId
170- ? {
171- ...msg,
172- content: data.answer || accumulatedContent,
173- model: data.model || msg.model,
174- maxPromptLength:
175- data.maxPromptLength !== undefined
176- ? data.maxPromptLength
177- : msg.maxPromptLength,
178- }
179- : msg
180- )
181- );
173+ // If message wasn't added yet (no content received), add it now
174+ if (!messageAdded) {
175+ const botMsg: Message = {
176+ id: assistantMsgId,
177+ threadId: targetThreadId,
178+ role: "assistant",
179+ content: data.answer || accumulatedContent,
180+ createdAt: new Date(),
181+ model: data.model || null,
182+ maxPromptLength: data.maxPromptLength !== undefined ? data.maxPromptLength : null,
183+ generationTimeMs: null,
184+ toolCallCounts: data.toolCallCounts !== undefined ? data.toolCallCounts : null,
185+ };
186+ setMessages((prev) => [...prev, botMsg]);
187+ } else {
188+ setMessages((prev) =>
189+ prev.map((msg) =>
190+ msg.id === assistantMsgId
191+ ? {
192+ ...msg,
193+ content: data.answer || accumulatedContent,
194+ model: data.model || msg.model,
195+ maxPromptLength:
196+ data.maxPromptLength !== undefined
197+ ? data.maxPromptLength
198+ : msg.maxPromptLength,
199+ toolCallCounts: data.toolCallCounts !== undefined ? data.toolCallCounts : msg.toolCallCounts,
200+ }
201+ : msg
202+ )
203+ );
204+ }
182205 setStreamingMessageId(null);
183206 shouldStop = true;
184207 break;
@@ -186,6 +209,21 @@ export function useMessages(threadId: number | null, onError?: (error: string) =
186209 // If it's a stop error, keep the partial content
187210 // Note: metadata will be sent via "done" message after the message is saved
188211 if (data.error === "Generation stopped") {
212+ // If we have content but message wasn't added, add it now
213+ if (!messageAdded && accumulatedContent.trim()) {
214+ const botMsg: Message = {
215+ id: assistantMsgId,
216+ threadId: targetThreadId,
217+ role: "assistant",
218+ content: accumulatedContent,
219+ createdAt: new Date(),
220+ model: null,
221+ maxPromptLength: null,
222+ generationTimeMs: null,
223+ toolCallCounts: null,
224+ };
225+ setMessages((prev) => [...prev, botMsg]);
226+ }
189227 setStreamingMessageId(null);
190228 shouldStop = true;
191229 break;
@@ -209,13 +247,13 @@ export function useMessages(threadId: number | null, onError?: (error: string) =
209247 } catch (error) {
210248 // Don't log abort errors as they're intentional
211249 if (error instanceof Error && error.name !== "AbortError") {
212- // Only remove the assistant message on non-abort errors
250+ // Only remove the assistant message on non-abort errors (if it was added)
213251 setMessages((prev) => prev.filter((msg) => msg.id !== assistantMsgId));
214252 if (onError && error instanceof Error) {
215253 onError(error.message);
216254 }
217255 }
218- // For abort errors, keep the partial content
256+ // For abort errors, keep the partial content (message will be added if it has content)
219257 setStreamingMessageId(null);
220258 } finally {
221259 setIsLoading(false);