M
Mastra2mo ago
Aamish

Agent calling a workflow (with Vercel AI SDK chat)

Hey guys, had an issue with agents calling workflows. It seems that if I write into a workflow's writer, and I invoked it from an agent, the writer doesn't pass messages to the agent's stream. I'm not sure if this is by design. The issue I'm facing is that I want to send a custom data message from a workflow I call through an agent and wait for user input (I have a useChat hook from the Vercel AI SDK for chat input). I seem to neither be able to send a custom message through the writer to the chat, nor can I seem to suspend the Mastra app for input (when I suspend the workflow, control goes over to the agent and it just sends back text instead of waiting for a response). Am I missing something here? P.S: there seems to be a lot of documentation about workflows calling agents, but none of agents calling workflows.
18 Replies
Sam Bhagwat
Sam Bhagwat2mo ago
cc @Romain to track this down
Mastra Triager
Mastra Triager2mo ago
GitHub
Agent calling a workflow (with Vercel AI SDK chat) · Issue #9720 ...
This issue was created from Discord post 1435376463464628365: Hey guys, had an issue with agents calling workflows. It seems that if I write into a workflow's writer, and I invoked it from an a...
_roamin_
_roamin_2mo ago
Hey @Aamish ! Could you share how you're currently sending your custom data?
Aamish
AamishOP2mo ago
Simple writer?.custom calls. Have tried writer?.write, same result. If I turn this into a tool it works fine. @Romain let me know if I can help further
Bastien
Bastien5w ago
Hi! Just realised that I face exactly the same issue : https://discord.com/channels/1309558646228779139/1436306450816307210/1436306450816307210 From what I gathered, custom events from worfklows are wrapped inside tool-ouput "USER" parts that are not handled by the AgentStreamToAISDKTransformer
Aamish
AamishOP5w ago
Seems about right, this is the issue I’m facing. Add to that the suspend idea, is there a way to suspend the entire Mastra instance? Workflow called by an agent technically doesn’t allow suspension without the agent taking back control.
Ariel
Ariel5w ago
@Aamish
nor can I seem to suspend the Mastra app for input (when I suspend the workflow, control goes over to the agent and it just sends back text instead of waiting for a response). Am I missing something here?
Have you found a solution to this? Workflow is suspended but the agent just continued
Aamish
AamishOP5w ago
@Ariel working on it as I write, I think I might move away from the agent for now and just stream a workflow, which itself has a more fundamental issue (suspend/resume does not seem to be supported for useChat currently).
Ariel
Ariel5w ago
The issue you described here with suspending a workflow within an agent network does not work even in the Mastra Playground, so I don't think it's a useChat exclusive issue. If you find a reliable way to stream text from a workflow, please let me know. I cannot use the built in Mastra endpoints (those that expose params.write) because the writer does not stream text chunks at all. So I am running my own Hono server that calls the workflow, passes writer in runtimeContext, and then the agent writes to that writer. There's another bug with running agents within workflow steps, where if you have memory, and even if you congfigure savePerStep to persist messages after each step, then if you use tool call suspension, nothing gets saved. The individual building blocks of Mastra are nice, the demo apps are cool, but I'd like to be able to build something complex.
No description
Aamish
AamishOP4w ago
Yeah workflow suspension seems difficult currently within an agent, an option might be to embed the agent itself within the workflow (agent in a loop as a step)
dero
dero4w ago
Hi @Aamish for your original issue, we have a PR up that would fix https://github.com/mastra-ai/mastra/pull/9919
GitHub
Improve ai-sdk transformers, handle custom data from agent sub work...
Improve ai-sdk transformers, handle custom data from agent sub workflow, sug agent tools changeset Description Related Issue(s) Fixes #9874 Type of Change Bug fix (non-breaking change that fix...
dero
dero4w ago
fyi, for this we also introduced changes recently to support passing a resumeData to the workflowRoute for suspended steps https://github.com/mastra-ai/mastra/pull/9392. All these would be in next release. Do let us know if these patterns help you achieve your use case, or any more improvement/support we can provide. Gonna bring @roamin into the mix too
Ariel
Ariel4w ago
@dero it would be great to be able to provide a custom writer when executing from a custom server, so that the writer accessed in the params.writer would be that one, then we won't need to use runtimeContext for that
dero
dero4w ago
Hi @Ariel, kindly create this as a feature request on github. That would help us track down and assign priority.
Aamish
AamishOP4w ago
Any updates on being able to suspend workflows called agents @dero ?
Vulcano
Vulcano4w ago
I have run into this issue too and I resolved it for the moment by making a tool that can start/resume workflows that reads the workflow output stream and forwards it to its own writer. My writer.custom and writer.write data from th workflow steps reaches my frontend with this approach.
dero
dero4w ago
Hi @Aamish can you add more details? do you mean workflows registered on agents, where the agent can use it as a tool or an agent in a workflow step. If it's the later you can already do that today. This seems like a potential pattern to with too where the agent has tools that routes over the workflow runs. A more detailed requirment of what you are trying to achieve would help @Aamish , maybe we can explore adding such a pattern to the ui dojo
Aamish
AamishOP3w ago
I mean the first one. Basically I have an agent, and I provide it a workflow in its workflows array (basically like a tool). When it calls the workflow, the workflow is directly controlled by the agent itself, and human-in-the-loop seems impossible since whenever the workflow tries to suspend, the entire Mastra graph does not suspend, only the workflow does, which gives control back to the agent, which then considers the workflow done or exited and returns a response to the user as if the workflow had exited fully, while the intention with suspend/resume seems like it should be to give the user a place to control the workflow, the result of which then, once we do an actual exit, should be fed into the agent to retrieve a response. @dero any updates?

Did you find this page helpful?