Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add converter from type CoreMessage to type Message #4875

Open
yudistiraashadi opened this issue Feb 14, 2025 · 5 comments
Open

Add converter from type CoreMessage to type Message #4875

yudistiraashadi opened this issue Feb 14, 2025 · 5 comments
Labels
enhancement New feature or request

Comments

@yudistiraashadi
Copy link
Contributor

Feature Description

Reason

For persistence data which we save messages data to the Database, I think saving the data as Array<CoreMessage> makes more sense instead of saving it as Array<Message> as suggested on AI SDK docs. Array<CoreMessage> is used on the server which communicates with the DB, and it is also easier to add files as context like this example.

Solution

Add a converter from CoreMessage to Message to make it easier to translate the types. in AI Chatbot template they have a function called convertToUIMessages() link which helps translate their DB which looks like a raw CoreMessage to Message that is consumed by the UI. I think we can build something similar to that.

Use Cases

On server

streamText({
  ....
  onFinish: async (result) => {
    saveMessagesToDb(result.response.messages)
  }
})
....

On client

const savedMessages = getMessagesFromDB()

const chat = useChat({
  ...
  initialMessage: convertToUIMessages(savedMessages)
})

Additional context

No response

@yudistiraashadi yudistiraashadi added the enhancement New feature or request label Feb 14, 2025
@lgrammel
Copy link
Collaborator

I strongly recommend not saving core messages. UI messages contain a lot of information that is required for correct persistence of UI state.

@yudistiraashadi
Copy link
Contributor Author

I strongly recommend not saving core messages. UI messages contain a lot of information that is required for correct persistence of UI state.

I agree, but I argue it is better to save the messages state in DB as CoreMessage especially for file handling since UI Message only supports Image and Text based on here

@lgrammel
Copy link
Collaborator

Would saving UI messages work for you if they included attachments?

@yudistiraashadi
Copy link
Contributor Author

yep, but I'm personally not happy making my DB structure based on currently experimental attachments structure of UIMessage. CoreMessage structure also afaik looks similar to the google and anthropic approach for file handling.

I'm currently using something similar to convertToUIMessages() based on Vercel's AI Chatbot repo, and I think that is a very good util function to be added to the AI SDK.

@tresorama
Copy link

tresorama commented Feb 17, 2025

I support @yudistiraashadi on making convertCoreMessageToUiMessage part of the sdk.
It would be very helpful.
Along with a zod schema to validate UIMessage, to have parity between core and ui message utilities.

// if you begin with core message and want a ui message...
const coreMessage = ...
const uiMessage = convertCoreMessageToUiMessage(coreMessage) // fn suggested (not present in sdk as today)
const parsedUiMessage = uiMessageSchema.parse(uiMessage) // zod schema suggested  (not present in sdk as today)

// opposite...
const uiMessage = ...
const coreMessage = convertToCoreMessage(uiMessage) // fn present in sdk as today
const parsedCoreMessage = coreMessageSchema.parse(coreMessage) // zod schema present in sdk as today

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants