Skip to content

Commit

Permalink
fix: use stable initialMessages for useChat (#493)
Browse files Browse the repository at this point in the history
  • Loading branch information
codeincontext authored Jan 16, 2025
1 parent 3f32c75 commit 3233c2e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ const ChatRightHandSideLesson = ({
demo,
}: Readonly<ChatRightHandSideLessonProps>) => {
const { messages } = useLessonChat();
console.log({ messages, showLessonMobile });
console.log(messages.length);

const chatEndRef = useRef<HTMLDivElement>(null);

Expand Down
9 changes: 6 additions & 3 deletions apps/nextjs/src/components/ContextProviders/ChatProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,11 @@ export function ChatProvider({ id, children }: Readonly<ChatProviderProps>) {
const { invokeActionMessages } = useActionMessages();

/******************* Streaming of all chat starts from messages here *******************/
const initialMessages = useMemo(() => {
return chat?.messages?.filter((m) =>
isValidMessageRole(m.role),
) as Message[];
}, [chat?.messages]);

const {
messages,
Expand All @@ -186,9 +191,7 @@ export function ChatProvider({ id, children }: Readonly<ChatProviderProps>) {
setMessages,
} = useChat({
sendExtraMessageFields: true,
initialMessages: (chat?.messages ?? []).filter((m) =>
isValidMessageRole(m.role),
) as Message[],
initialMessages,
generateId: () => generateMessageId({ role: "user" }),
id,
body: {
Expand Down

0 comments on commit 3233c2e

Please sign in to comment.