import { createDeepAgent } from "deepagents";const agent = createDeepAgent({ systemPrompt: "You are a project coordinator. Always delegate research tasks " + "to your researcher subagent using the task tool. Keep your final response to one sentence.", subagents: [ { name: "researcher", description: "Researches topics thoroughly", systemPrompt: "You are a thorough researcher. Research the given topic " + "and provide a concise summary in 2-3 sentences.", }, ],});for await (const [namespace, chunk] of await agent.stream( { messages: [ { role: "user", content: "Write a short summary about AI safety" }, ], }, { streamMode: "updates", subgraphs: true },)) { // 主代理更新(空命名空间) if (namespace.length === 0) { for (const [nodeName, data] of Object.entries(chunk)) { if (nodeName === "tools") { // 返回给主代理的子代理结果 for (const msg of (data as any).messages ?? []) { if (msg.type === "tool") { console.log(`\nSubagent complete: ${msg.name}`); console.log(` Result: ${String(msg.content).slice(0, 200)}...`); } } } else { console.log(`[main agent] step: ${nodeName}`); } } } // 子代理更新(非空命名空间) else { for (const [nodeName] of Object.entries(chunk)) { console.log(` [${namespace[0]}] step: ${nodeName}`); } }}
输出
Main agent step: model_request [tools:call_abc123] step: model_request [tools:call_abc123] step: tools [tools:call_abc123] step: model_requestSubagent complete: taskResult: ## AI Safety Report...Main agent step: model_request [tools:call_def456] step: model_request [tools:call_def456] step: model_requestSubagent complete: taskResult: # Comprehensive Report on AI Safety...Main agent step: model_request
import { createDeepAgent } from "deepagents";import { tool, type ToolRuntime } from "langchain";import { z } from "zod";/** * 一个通过 config.writer 发出自定义进度事件的工具。 * writer 将数据发送到 "custom" 流模式。 */const analyzeData = tool( async ({ topic }: { topic: string }, config: ToolRuntime) => { const writer = config.writer; writer?.({ status: "starting", topic, progress: 0 }); await new Promise((r) => setTimeout(r, 500)); writer?.({ status: "analyzing", progress: 50 }); await new Promise((r) => setTimeout(r, 500)); writer?.({ status: "complete", progress: 100 }); return `Analysis of "${topic}": Customer sentiment is 85% positive, driven by product quality and support response times.`; }, { name: "analyze_data", description: "Run a data analysis on a given topic. " + "This tool performs the actual analysis and emits progress updates. " + "You MUST call this tool for any analysis request.", schema: z.object({ topic: z.string().describe("The topic or subject to analyze"), }), },);const agent = createDeepAgent({ systemPrompt: "You are a coordinator. For any analysis request, you MUST delegate " + "to the analyst subagent using the task tool. Never try to answer directly. " + "After receiving the result, summarize it in one sentence.", subagents: [ { name: "analyst", description: "Performs data analysis with real-time progress tracking", systemPrompt: "You are a data analyst. You MUST call the analyze_data tool " + "for every analysis request. Do not use any other tools. " + "After the analysis completes, report the result.", tools: [analyzeData], }, ],});for await (const [namespace, chunk] of await agent.stream( { messages: [ { role: "user", content: "Analyze customer satisfaction trends", }, ], }, { streamMode: "custom", subgraphs: true },)) { const isSubagent = namespace.some((s: string) => s.startsWith("tools:")); if (isSubagent) { const subagentNs = namespace.find((s: string) => s.startsWith("tools:"))!; console.log(`[${subagentNs}]`, chunk); } else { console.log("[main]", chunk); }}