vercel / ai

Build AI-powered applications with React, Svelte, Vue, and Solid
https://sdk.vercel.ai/docs
Other
9.55k stars 1.4k forks source link

AI State History Not Updating #2627

Open yasserelsaid opened 1 month ago

yasserelsaid commented 1 month ago

Description

I am working on creating a playground to compare multiple chatbot instances using Vercel's AI SDK. Below is a simplified representation of our AI state structure:

// AI State Structure
type PlaygroundCompareAIState = {
  chatbots: Map<string, PlaygroundCompareChatbotAIState>
}

type PlaygroundCompareChatbotAIState = {
  history: MessageWithRole[]
}

As you can see, it is a map of instances, and each instance has its own history of messages array.

Problem:

When updating the history in our chat action (server side) using the following code:

const aiState = getMutableAIState()
const newAIState = aiState.get()
const chatbotAIState = newAIState.chatbots.get(instanceID)

// Update the history
chatbotAIState.history = [...newHistory]
newAIState.chatbots.set(instanceID, chatbotAIState)

aiState.done({
  ...aiState.get(),
  ...newAIState
})

The history is not updating at all and remains an empty array. Even when we log the new AI state before calling .done(), the history is still empty.

Expected Behavior:

The history array should be updated with the new messages, reflecting the latest conversation state for each chatbot instance.

Actual Behavior:

The history array remains empty, and no changes are reflected.

Thank you for your assistance!

Code example

No response

Additional context

No response

lgrammel commented 1 month ago

@yasserelsaid does this also happen when you use a record/object instead of a map?