diff --git a/apps/web/src/hooks/useChat.ts b/apps/web/src/hooks/useChat.ts index 881e9a4..c9fe796 100644 --- a/apps/web/src/hooks/useChat.ts +++ b/apps/web/src/hooks/useChat.ts @@ -343,63 +343,64 @@ export function useChat(options: UseChatOptions = {}): UseChatReturn { error: err instanceof Error ? err : new Error(String(err)), }); - setMessages((prev) => { - const withoutPlaceholder = prev.filter((m) => m.id !== assistantMessageId); - messagesRef.current = withoutPlaceholder; - return withoutPlaceholder; - }); - setIsStreaming(false); - - try { - const response = await sendChatMessage(request); - - const assistantMessage: Message = { - id: `assistant-${Date.now().toString()}`, - role: "assistant", - content: response.message.content, - createdAt: new Date().toISOString(), - model: response.model, - promptTokens: response.promptEvalCount ?? 0, - completionTokens: response.evalCount ?? 0, - totalTokens: (response.promptEvalCount ?? 0) + (response.evalCount ?? 0), - }; - setMessages((prev) => { - const updated = [...prev, assistantMessage]; - messagesRef.current = updated; - return updated; + const withoutPlaceholder = prev.filter((m) => m.id !== assistantMessageId); + messagesRef.current = withoutPlaceholder; + return withoutPlaceholder; }); + setIsStreaming(false); - streamingSucceeded = true; - } catch (fallbackErr: unknown) { - const errorMsg = - fallbackErr instanceof Error ? fallbackErr.message : "Failed to send message"; - setError("Unable to send message. Please try again."); - onError?.(fallbackErr instanceof Error ? fallbackErr : new Error(errorMsg)); - console.error("Failed to send chat message", { - error: fallbackErr, - errorType: "LLM_ERROR", - conversationId: conversationIdRef.current, - messageLength: content.length, - messagePreview: content.substring(0, 50), - model, - messageCount: messagesRef.current.length, - timestamp: new Date().toISOString(), - }); + try { + const response = await sendChatMessage(request); - const errorMessage: Message = { - id: `error-${String(Date.now())}`, - role: "assistant", - content: "Something went wrong. Please try again.", - createdAt: new Date().toISOString(), - }; - setMessages((prev) => { - const updated = [...prev, errorMessage]; - messagesRef.current = updated; - return updated; - }); - setIsLoading(false); - return; + const assistantMessage: Message = { + id: `assistant-${Date.now().toString()}`, + role: "assistant", + content: response.message.content, + createdAt: new Date().toISOString(), + model: response.model, + promptTokens: response.promptEvalCount ?? 0, + completionTokens: response.evalCount ?? 0, + totalTokens: (response.promptEvalCount ?? 0) + (response.evalCount ?? 0), + }; + + setMessages((prev) => { + const updated = [...prev, assistantMessage]; + messagesRef.current = updated; + return updated; + }); + + streamingSucceeded = true; + } catch (fallbackErr: unknown) { + const errorMsg = + fallbackErr instanceof Error ? fallbackErr.message : "Failed to send message"; + setError("Unable to send message. Please try again."); + onError?.(fallbackErr instanceof Error ? fallbackErr : new Error(errorMsg)); + console.error("Failed to send chat message", { + error: fallbackErr, + errorType: "LLM_ERROR", + conversationId: conversationIdRef.current, + messageLength: content.length, + messagePreview: content.substring(0, 50), + model, + messageCount: messagesRef.current.length, + timestamp: new Date().toISOString(), + }); + + const errorMessage: Message = { + id: `error-${String(Date.now())}`, + role: "assistant", + content: "Something went wrong. Please try again.", + createdAt: new Date().toISOString(), + }; + setMessages((prev) => { + const updated = [...prev, errorMessage]; + messagesRef.current = updated; + return updated; + }); + setIsLoading(false); + return; + } } }