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

Convert CoreMessage to useChat Message for Persisting User Chats #3161

Open
sullyo opened this issue Sep 30, 2024 · 4 comments
Open

Convert CoreMessage to useChat Message for Persisting User Chats #3161

sullyo opened this issue Sep 30, 2024 · 4 comments
Labels
ai/ui enhancement New feature or request

Comments

@sullyo
Copy link

sullyo commented Sep 30, 2024

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

@jeremyphilemon jeremyphilemon added enhancement New feature or request ai/ui labels Oct 1, 2024
@yamz8
Copy link

yamz8 commented Oct 4, 2024

any updates here?

@ingara
Copy link

ingara commented Oct 8, 2024

@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.

@danielzohar
Copy link

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;
  });
}

@jeremyphilemon
Copy link
Contributor

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

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

No branches or pull requests

5 participants