M
MastraAI3w ago
Atiq

Stream order issue in nested agent via tool call

execute: async ({ context, runtimeContext }) => {
const messageId = runtimeContext.get("messageId") as NewChatRuntimeContext["messageId"];
const stream = await SandboxAgent.stream(
[
{
id: messageId,
role: "assistant",
content: [
context.task ? `Task: ${context.task}` : undefined,
context.goal ? `Goal: ${context.goal}` : undefined,
]
.filter(Boolean)
.join("\n") || "Execute the assigned task.",
},
],
{
format: "aisdk",
runtimeContext: runtimeContext,
stopWhen: stepCountIs(context.maxSteps),
memory: {
thread: threadId,
resource: userId,
},
},
);
// Stream sub-agent UI events to the runtime UI writer in real-time
writer.merge(
stream.toUIMessageStream({
sendReasoning: true,
sendStart: false,
sendFinish: false,
generateMessageId: () => messageId,

}),
);

// Stream the sub-agent output through this tool’s stream
// await stream.fullStream.pipeTo(mastraWriter!);

// Drive the stream consumption so events flow
stream.consumeStream();

return {
message: `Transferred to sandbox agent`,
};
},
execute: async ({ context, runtimeContext }) => {
const messageId = runtimeContext.get("messageId") as NewChatRuntimeContext["messageId"];
const stream = await SandboxAgent.stream(
[
{
id: messageId,
role: "assistant",
content: [
context.task ? `Task: ${context.task}` : undefined,
context.goal ? `Goal: ${context.goal}` : undefined,
]
.filter(Boolean)
.join("\n") || "Execute the assigned task.",
},
],
{
format: "aisdk",
runtimeContext: runtimeContext,
stopWhen: stepCountIs(context.maxSteps),
memory: {
thread: threadId,
resource: userId,
},
},
);
// Stream sub-agent UI events to the runtime UI writer in real-time
writer.merge(
stream.toUIMessageStream({
sendReasoning: true,
sendStart: false,
sendFinish: false,
generateMessageId: () => messageId,

}),
);

// Stream the sub-agent output through this tool’s stream
// await stream.fullStream.pipeTo(mastraWriter!);

// Drive the stream consumption so events flow
stream.consumeStream();

return {
message: `Transferred to sandbox agent`,
};
},
Above example works when agent stream chat realtime, but once chat completes and we reopen it then, we see two seperate assistant message for parent + this subagent calls. It should show a single assistant message instead of two after persitance.
3 Replies
Mastra Triager
GitHub
[DISCORD:1424123959573614642] Stream order issue in nested agent vi...
This issue was created from Discord post: https://discord.com/channels/1309558646228779139/1424123959573614642 execute: async ({ context, runtimeContext }) => { const messageId = runtimeContext....
fracheton
fracheton22h ago
Hey there and thanks for rising the issue 🙂 . How do you define your writer in your code?
Atiq
AtiqOP15h ago
@fracheton Thanks for reply added a full code example here: https://github.com/mastra-ai/mastra/issues/8501#issuecomment-3423661493 discord doesn't like large code example I guess Is there a better of doing this my end goal is a main agent -> 5 subagents, where main agent can call all subagents using their trigger tool calls and I need to stream entire subagents stream to client side but processed summary to main agent. I know this might not be the best way mastra recommend this pattern, but I tried mastra networks with latest release and it doesn't stream tool calls and some text delta on first stream.

Did you find this page helpful?