Agent.network()
这是一个实验性 API,未来版本可能会有所更改。network() 方法支持多代理协作和工作流编排。在生产环境中使用时请谨慎。
.network() 方法支持多智能体协作和路径规划。此方法接收消息和可选的执行选项。
🌐 The .network() method enables multi-agent collaboration and routing. This method accepts messages and optional execution options.
使用示例Direct link to 使用示例
🌐 Usage example
import { Agent } from "@mastra/core/agent";
import { agent1, agent2 } from "./agents";
import { workflow1 } from "./workflows";
import { tool1, tool2 } from "./tools";
const agent = new Agent({
id: "network-agent",
name: "Network Agent",
instructions:
"You are a network agent that can help users with a variety of tasks.",
model: "openai/gpt-5.1",
agents: {
agent1,
agent2,
},
workflows: {
workflow1,
},
tools: {
tool1,
tool2,
},
});
await agent.network(`
Find me the weather in Tokyo.
Based on the weather, plan an activity for me.
`);
参数Direct link to 参数
🌐 Parameters
messages:
options?:
选项Direct link to 选项
🌐 Options
maxSteps?:
memory?:
thread:
resource:
options?:
tracingContext?:
currentSpan?:
tracingOptions?:
metadata?:
requestContextKeys?:
traceId?:
parentSpanId?:
tags?:
telemetry?:
isEnabled?:
recordInputs?:
recordOutputs?:
functionId?:
modelSettings?:
temperature?:
maxOutputTokens?:
maxRetries?:
topP?:
topK?:
presencePenalty?:
frequencyPenalty?:
stopSequences?:
structuredOutput?:
schema:
model?:
instructions?:
runId?:
requestContext?:
traceId?:
返回Direct link to 返回
🌐 Returns
stream:
status:
result:
usage:
object:
objectStream:
结构化输出Direct link to 结构化输出
🌐 Structured Output
当你需要来自网络的已键入、验证的结果时,请使用 structuredOutput 选项。网络将在任务完成后生成与你的模式匹配的响应。
🌐 When you need typed, validated results from your network, use the structuredOutput option. The network will generate a response matching your schema after task completion.
import { z } from "zod";
const resultSchema = z.object({
summary: z.string().describe("A brief summary of the findings"),
recommendations: z.array(z.string()).describe("List of recommendations"),
confidence: z.number().min(0).max(1).describe("Confidence score"),
});
const stream = await agent.network("Research AI trends and summarize", {
structuredOutput: {
schema: resultSchema,
},
});
// Consume the stream
for await (const chunk of stream) {
// Handle streaming events
}
// Get the typed result
const result = await stream.object;
// result is typed as { summary: string; recommendations: string[]; confidence: number }
console.log(result?.summary);
console.log(result?.recommendations);
流式部分对象Direct link to 流式部分对象
🌐 Streaming Partial Objects
你也可以在对象生成的过程中部分地进行流式传输:
🌐 You can also stream partial objects as they're being generated:
const stream = await agent.network("Analyze data", {
structuredOutput: { schema: resultSchema },
});
// Stream partial objects
for await (const partial of stream.objectStream) {
console.log("Partial result:", partial);
}
// Get final result
const final = await stream.object;
块类型Direct link to 块类型
🌐 Chunk Types
在使用结构化输出时,会生成额外的块类型:
🌐 When using structured output, additional chunk types are emitted:
network-object:在流式传输过程中发出部分对象network-object-result:已发出最终结构化对象