流活动
🌐 Streaming Events
从代理或工作流进行流式传输可以实时查看 LLM 的输出或工作流运行的状态。这些反馈可以直接传递给用户,或在应用中使用以更有效地处理工作流状态,从而创建更流畅、更响应的体验。
🌐 Streaming from agents or workflows provides real-time visibility into either the LLM’s output or the status of a workflow run. This feedback can be passed directly to the user, or used within applications to handle workflow status more effectively, creating a smoother and more responsive experience.
从代理或工作流发出的事件表示生成和执行的不同阶段,例如运行开始时、文本生成时或工具被调用时。
🌐 Events emitted from agents or workflows represent different stages of generation and execution, such as when a run starts, when text is produced, or when a tool is invoked.
事件类型Direct link to 事件类型
🌐 Event types
以下是 .stream() 发出的所有事件的完整列表。根据你是从 代理 还是 工作流 进行流式传输,仅会发生这些事件的一个子集:
🌐 Below is a complete list of events emitted from .stream().
Depending on whether you’re streaming from an agent or a workflow, only a subset of these events will occur:
- start:标记代理或工作流运行的开始。
- 步骤开始:表示工作流步骤已开始执行。
- text-delta:由大型语言模型生成的增量文本块。
- 工具调用:当代理决定使用某个工具时,包括工具名称和参数。
- 工具结果:工具执行返回的结果。
- 步骤完成:确认某个特定步骤已完全结束,可能包括该步骤完成原因等元数据。
- 完成:当代理或工作流完成时,包括使用统计信息。
网络事件类型Direct link to 网络事件类型
🌐 Network event types
在使用 agent.network() 进行多智能体协作时,会发出额外的事件类型来跟踪协作流程:
🌐 When using agent.network() for multi-agent collaboration, additional event types are emitted to track the orchestration flow:
- routing-agent-start:路由代理开始分析任务,以决定将其分配给哪个原始单元(代理/工作流/工具)。
- routing-agent-text-delta:路由代理在处理所选原语的响应时的增量文本。
- 路由代理结束:路由代理完成其选择,包括所选原语及选择原因。
- 代理执行开始:一个被委派的代理开始执行。
- 代理执行结束:委派的代理完成执行。
- agent-execution-event-*:来自被委派代理执行的事件(例如,
agent-execution-event-text-delta)。 - 工作流执行开始:一个委托的工作流开始执行。
- workflow-execution-end:一个委托工作流执行完成。
- workflow-execution-event-*:来自委托工作流执行的事件。
- 工具执行开始:一个被委托的工具开始执行。
- 工具执行结束:委派的工具完成执行。
- network-execution-event-step-finish:网络迭代步骤完成。
- network-execution-event-finish:整个网络执行完成。
检查代理流Direct link to 检查代理流
🌐 Inspecting agent streams
使用 for await 循环遍历 stream 以检查所有发出的事件块。
🌐 Iterate over the stream with a for await loop to inspect all emitted event chunks.
const testAgent = mastra.getAgent("testAgent");
const stream = await testAgent.stream([
{ role: "user", content: "Help me organize my day" },
]);
for await (const chunk of stream) {
console.log(chunk);
}
访问 Agent.stream() 获取更多信息。
🌐 Visit Agent.stream() for more information.
示例代理输出Direct link to 示例代理输出
🌐 Example agent output
下面是可能触发的事件示例。每个事件都会包含一个 type,并且可以包含其他字段,例如 from 和 payload。
🌐 Below is an example of events that may be emitted. Each event always includes a type and can include additional fields like from and payload.
{
type: 'start',
from: 'AGENT',
// ..
}
{
type: 'step-start',
from: 'AGENT',
payload: {
messageId: 'msg-cdUrkirvXw8A6oE4t5lzDuxi',
// ...
}
}
{
type: 'tool-call',
from: 'AGENT',
payload: {
toolCallId: 'call_jbhi3s1qvR6Aqt9axCfTBMsA',
toolName: 'testTool'
// ..
}
}
检查工作流流Direct link to 检查工作流流
🌐 Inspecting workflow streams
使用 for await 循环遍历 stream 以检查所有发出的事件块。
🌐 Iterate over the stream with a for await loop to inspect all emitted event chunks.
const testWorkflow = mastra.getWorkflow("testWorkflow");
const run = await testWorkflow.createRun();
const stream = await run.stream({
inputData: {
value: "initial data",
},
});
for await (const chunk of stream) {
console.log(chunk);
}
示例工作流输出Direct link to 示例工作流输出
🌐 Example workflow output
下面是可能触发的事件示例。每个事件都会包含一个 type,并且可以包含其他字段,例如 from 和 payload。
🌐 Below is an example of events that may be emitted. Each event always includes a type and can include additional fields like from and payload.
{
type: 'workflow-start',
runId: '221333ed-d9ee-4737-922b-4ab4d9de73e6',
from: 'WORKFLOW',
// ...
}
{
type: 'workflow-step-start',
runId: '221333ed-d9ee-4737-922b-4ab4d9de73e6',
from: 'WORKFLOW',
payload: {
stepName: 'step-1',
args: { value: 'initial data' },
stepCallId: '9e8c5217-490b-4fe7-8c31-6e2353a3fc98',
startedAt: 1755269732792,
status: 'running'
}
}
检查代理网络Direct link to 检查代理网络
🌐 Inspecting agent networks
在使用 agent.network() 进行多代理协作时,遍历流以跟踪任务如何在代理、工作流和工具之间分配和执行。
🌐 When using multi-agent collaboration with agent.network(), iterate over the stream to track how tasks are delegated and executed across agents, workflows, and tools.
const networkAgent = mastra.getAgent("networkAgent");
const networkStream = await networkAgent.network(
"Research dolphins then write a report",
);
for await (const chunk of networkStream) {
console.log(chunk);
}
访问 Agent.network() 以获取更多信息。
🌐 Visit Agent.network() for more information.
示例网络输出Direct link to 示例网络输出
🌐 Example network output
网络流会发出跟踪编排流程的事件。每次迭代从路由开始,然后执行所选的原语。
🌐 Network streams emit events that track the orchestration flow. Each iteration begins with routing, followed by execution of the selected primitive.
// Routing agent decides what to do
{
type: 'routing-agent-start',
from: 'NETWORK',
runId: '7a3b9c2d-1e4f-5a6b-8c9d-0e1f2a3b4c5d',
payload: {
agentId: 'routing-agent',
// ...
}
}
// Routing agent makes a selection
{
type: 'routing-agent-end',
from: 'NETWORK',
runId: '7a3b9c2d-1e4f-5a6b-8c9d-0e1f2a3b4c5d',
payload: {
// ...
}
}
// Delegated agent begins execution
{
type: 'agent-execution-start',
from: 'NETWORK',
runId: '8b4c0d3e-2f5a-6b7c-9d0e-1f2a3b4c5d6e',
payload: {
// ...
}
}
// Events from the delegated agent's execution
{
type: 'agent-execution-event-text-delta',
from: 'NETWORK',
runId: '8b4c0d3e-2f5a-6b7c-9d0e-1f2a3b4c5d6e',
payload: {
type: 'text-delta',
payload: {
// ...
}
}
}
过滤网络事件Direct link to 过滤网络事件
🌐 Filtering network events
你可以按类型筛选事件,以跟踪网络执行的特定方面:
🌐 You can filter events by type to track specific aspects of the network execution:
const networkStream = await networkAgent.network(
"Analyze data and create visualization",
);
for await (const chunk of networkStream) {
// Track routing decisions
if (chunk.type === "routing-agent-end") {
console.log(
"Selected:",
chunk.payload.resourceType,
chunk.payload.resourceId,
);
console.log("Reason:", chunk.payload.selectionReason);
}
// Track agent delegations
if (chunk.type === "agent-execution-start") {
console.log("Delegating to agent:", chunk.payload.agentId);
}
// Track workflow delegations
if (chunk.type === "workflow-execution-start") {
console.log("Executing workflow:", chunk.payload.name);
}
}