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.