Skip to content
Discussion options

You must be logged in to vote

You can use experimental_prepareRequestBody in useChat hook.

https://ai-sdk.dev/docs/reference/ai-sdk-ui/use-chat#experimental_prepare-request-body

// chat.tsx

const {
  messages,
  setMessages,
  handleSubmit,
  input,
  setInput,
  append,
  status,
  stop,
} = useChat({
  id,
  initialMessages,
  sendExtraMessageFields: true,
  experimental_prepareRequestBody: (body) => ({
    id,
    message: body.messages.at(-1),
  }),
})

In the backend, get the latest message and append it to the messages fetched from the database.

//route.ts

import {
  appendClientMessage,
  streamText,
} from 'ai';
import { google } from '@ai-sdk/google';
import { getMessagesByChatId } from '@/lib/db';

export a…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by a4amado
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
Labels
None yet
2 participants