xonecas.io
53129
Pa

PAT / commit 99a77ba

fix: optimistic ui too optimistic

xonecas2025-12-18 99a77ba8c679ca3fbe0d9e4ef4ad0629723eb7b1 parent 612510c

3 files changed

src/hooks/useMessages.ts+73−35
@@ -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);
src/lib/ollama/chat.ts+3−3
@@ -105,7 +105,6 @@ export function streamAssistantResponse({
105105 signal.addEventListener("abort", abortHandler);
106106
107107 let generationTimeMs: number | undefined;
108- let toolCalls: ToolCall[] | undefined;
109108
110109 try {
111110 const result = await fetchOllamaResponse(
@@ -116,7 +115,6 @@ export function streamAssistantResponse({
116115 threadMaxPromptLength
117116 );
118117 generationTimeMs = result.generationTimeMs;
119- toolCalls = result.toolCalls;
120118 } catch (error) {
121119 // If aborted, don't throw - just close
122120 if (signal.aborted || (error instanceof Error && error.message === "Request aborted")) {
@@ -150,6 +148,7 @@ export function streamAssistantResponse({
150148 answer: accumulatedContent,
151149 model: threadModel,
152150 maxPromptLength: threadMaxPromptLength,
151+ toolCallCounts: extractToolCounts(allToolCalls),
153152 });
154153 }
155154 safeClose();
@@ -165,7 +164,7 @@ export function streamAssistantResponse({
165164 maxPromptLength: threadMaxPromptLength === "none" ? null : threadMaxPromptLength, // null means "none" (no limit), otherwise 1024 or 4096
166165 createdAt: new Date(),
167166 generationTimeMs,
168- toolCallCounts: toolCalls ? extractToolCounts(toolCalls) : null,
167+ toolCallCounts: extractToolCounts(allToolCalls),
169168 });
170169
171170 // Update thread's updatedAt timestamp
@@ -177,6 +176,7 @@ export function streamAssistantResponse({
177176 answer: accumulatedContent,
178177 model: threadModel,
179178 maxPromptLength: threadMaxPromptLength,
179+ toolCallCounts: extractToolCounts(allToolCalls),
180180 });
181181 safeClose();
182182 } catch {
tsconfig.tsbuildinfo+1−1
@@ -1 +1 @@
1-{"fileNames":["./node_modules/typescript/lib/lib.es5.d.ts","./node_modules/typescript/lib/lib.es2015.d.ts","./node_modules/typescript/lib/lib.es2016.d.ts","./node_modules/typescript/lib/lib.es2017.d.ts","./node_modules/typescript/lib/lib.es2018.d.ts","./node_modules/typescript/lib/lib.es2019.d.ts","./node_modules/typescript/lib/lib.es2020.d.ts","./node_modules/typescript/lib/lib.es2021.d.ts","./node_modules/typescript/lib/lib.es2022.d.ts","./node_modules/typescript/lib/lib.es2023.d.ts","./node_m …
1+{"fileNames":["./node_modules/typescript/lib/lib.es5.d.ts","./node_modules/typescript/lib/lib.es2015.d.ts","./node_modules/typescript/lib/lib.es2016.d.ts","./node_modules/typescript/lib/lib.es2017.d.ts","./node_modules/typescript/lib/lib.es2018.d.ts","./node_modules/typescript/lib/lib.es2019.d.ts","./node_modules/typescript/lib/lib.es2020.d.ts","./node_modules/typescript/lib/lib.es2021.d.ts","./node_modules/typescript/lib/lib.es2022.d.ts","./node_modules/typescript/lib/lib.es2023.d.ts","./node_m …