Restoring chat history based on workflow with multiple agents
I'm using a workflow where I have multiple steps that call different agents.
In the UI this workflow is displayed like a chat.
The agents sometimes just emit a text stream and sometimes call tools that I use to display UI components.
The workflow uses suspend/resume and I want to be able to let the user reload the page and restore everything in the Chat UI.
My current setup is using AI-SDK and
workflowRoute to stream the workflow.
When I stream from an agent directly I use a hook similar to useAgentMessages (like in the playground) that retrieves the messages for that specific agent.
Currently I can only retrieve the messages per single agent, which also means I have to know all the agents involved in the different workflow steps.
The different agents in the workflow share the same threadId and resourceId so I was wondering if it was feasible to retrieve all messages just by using these IDs without the agentId.
I guess the reason for requiring an agentId or a networkId is that each agent can have a different memory configuration. In my case they share the same memory configuration and run sequentially, although I wasn't sure if there could be potential conflicts when both share the same memory or even run in parallel.
Right now my workaround would be that I load the messages of the specific agents individually and merge and sort them to restore the original message order.
But I was curious what other solutions there might be. It would be really cool if the agent memory was aware of them running inside a workflow so that the Client SDK could support something like listWorkflowMessages(workflowId).6 Replies
📝 Created GitHub issue: https://github.com/mastra-ai/mastra/issues/10867
🔍 If you're experiencing an error, please provide a minimal reproducible example whenever possible to help us resolve it quickly.
🙏 Thank you for helping us improve Mastra!
They all use the same resourceId/threadId. do they share the same memory instance?
You can get fetch messages for that thread from Storage, if all agents are configured with the same ids, they’ll write to the same thread
@Abhi Aiyer Thanks, I just had a look on the Mastra instance in the backend and found this:
This is not part of the REST API though, right? I was wondering if this was not needed yet in the API or the Client SDK or if it was a conscious decision to not include it.
Would be happy to contribute something if this makes sense.
You can fetch messages with the Client SDK
I do see the confusion though cause agentId is required there?
i can make some changes here in v1
Ah I see, it makes sense that the thread includes all messages after all. Yeah the required
agentId makes it look like you only get those messages.
If there was a getMemoryThreadById that requires resourceId and threadId and a getMemoryThreadsByAgentId that requires resourceId and agentId the distinction might be clearer.Agent Id isnt actually relevant for threads, so i think we should remove this constraint.
I can make some adjustments in the v1 version
Actually coming back to this you're right @Sebastian
• Threads are NOT tied to
agentId - they're identified by resourceId
• agentId IS required because we are getting the data from the memory instance attached to that agent.
So we should do both