-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Convert CoreMessage to useChat Message for Persisting User Chats #3161
Comments
any updates here? |
@sullyo have you already implemented this logic on your end? If so, would you be open to sharing it? We’re at a point where we’ll need to develop something similar soon. |
Here's a simple implementation: import { CoreMessage, generateId, Message } from "ai";
function createMessage(role: "user" | "assistant", content: string): Message {
const res: Message = {
id: generateId(),
role,
content,
createdAt: new Date(),
};
return res;
}
/**
* Converts CoreMessages to Messages
* Should be provided by the `ai` package in the future
* @see https:/vercel/ai/issues/3161
*/
export function convertToMessages(coreMessages: CoreMessage[]): Message[] {
return coreMessages.flatMap((m) => {
if (m.role !== "user" && m.role !== "assistant") return [];
if (m.content === null) return [];
if (typeof m.content === "string") {
return [createMessage(m.role, m.content)];
}
const res = [];
for (const c of m.content) {
if (c.type === "text") {
res.push(createMessage(m.role, c.text));
}
}
return res;
});
} |
Hey @sullyo, thanks for the feature request – this sounds like a great utility function to include! We just implemented this in the chatbot template here and definitely can see it useful for restoring conversations from history @ingara would love for you to try it out and share feedback with us before we add it to the sdk |
Feature Description
It would be great to have a utility function or method to convert a CoreMessage (server-side format) to the format used by useChat messages for easier loading and persisting of user chats.
Currently, we can convert messages to CoreMessage on the server-side, but there's no direct way to convert them back to the format used by useChat. This creates complexity when a user has tool calls, closes the chat, and returns later, as all messages need to be loaded again. A straightforward utility would streamline this process, especially for reloading conversations.
Right now custom logic is required to load and display past messages in useChat when a user resumes a chat session.
Use Case
Save chat messages for the user.
Load all user messages, including tool calls, on returning to a chat session.
Additional context
No response
The text was updated successfully, but these errors were encountered: