Skip to content

Commit 1b88a4e

Browse files
authored
fix: update AI message handling in useChat hook (#109)
1 parent 93ad8f8 commit 1b88a4e

File tree

1 file changed

+12
-3
lines changed
  • apps/dashboard/app/(main)/websites/[id]/assistant/hooks

1 file changed

+12
-3
lines changed

apps/dashboard/app/(main)/websites/[id]/assistant/hooks/use-chat.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import type { StreamingUpdate } from '@databuddy/shared';
22
import { useAtom } from 'jotai';
33
import { useCallback, useEffect, useRef, useState } from 'react';
4-
import { trpc } from '@/lib/trpc';
54
import {
65
inputValueAtom,
76
isLoadingAtom,
@@ -121,6 +120,15 @@ export function useChat() {
121120
};
122121
}, []);
123122

123+
function updateAiMessage(message: Message) {
124+
setMessages((prev) => {
125+
//TODO: find a way to update the message with the correct id
126+
const newMessages = [...prev];
127+
newMessages[newMessages.length - 1] = message;
128+
return newMessages;
129+
});
130+
}
131+
124132
const sendMessage = useCallback(
125133
async (content?: string) => {
126134
const messageContent = content || inputValue.trim();
@@ -155,6 +163,8 @@ export function useChat() {
155163
thinkingSteps: [],
156164
};
157165

166+
setMessages((prev) => [...prev, assistantMessage]);
167+
158168
try {
159169
// Stream the AI response using the new single endpoint
160170
const response = await fetch(
@@ -304,6 +314,7 @@ export function useChat() {
304314
break;
305315
}
306316
}
317+
updateAiMessage(assistantMessage);
307318
} catch (_parseError) {
308319
console.warn('Failed to parse SSE data:', line);
309320
}
@@ -323,8 +334,6 @@ export function useChat() {
323334
} finally {
324335
setIsLoading(false);
325336
}
326-
327-
setMessages((prev) => [...prev, assistantMessage]);
328337
},
329338
[
330339
inputValue,

0 commit comments

Comments
 (0)