Skip to main content

Documentation Index

Fetch the complete documentation index at: https://nvd-54.mintlify.app/llms.txt

Use this file to discover all available pages before exploring further.

深度智能体基于 LangGraph 的事件流模型构建,并添加了一流的 subagents 投影。当你需要面向应用的协调器智能体、委托子智能体、嵌套消息、工具调用和最终状态的流时,请使用它。
查看流式输出示例集,获取可运行的示例和详细参考文档链接。
有关底层 Pregel 流模式的信息,请参阅深度智能体流式输出文档。

流式输出子智能体

深度智能体在 LangGraph 流式输出之上添加了子智能体投影。当你需要为每个委托的 task 调用获取一个流句柄时,请使用 run.subagents。投影首先发现子智能体任务,然后在你访问子智能体句柄上的属性时打开消息、工具调用和值流。
const run = await agent.streamEvents(
  { messages: [{ role: "user", content: "Write me a haiku about the sea" }] },
  { version: "v3" }
);

for await (const subagent of run.subagents) {
  console.log(subagent.name);
  console.log(await subagent.taskInput);

  for await (const message of subagent.messages) {
    console.log(await message.text);
  }
}

子智能体流字段

每个子智能体流都暴露与父级运行相同类型的投影,例如消息、工具调用、嵌套子智能体和最终输出。有关通用的父级运行流模型,请参阅 LangChain 事件流 TypeScript 使用驼峰命名法的投影名称,例如 toolCallstaskInput
字段描述
name子智能体名称。
messages子智能体发出的消息。
subagents嵌套子智能体调用。
output最终子智能体状态,或委托任务的完成信号。
| taskInput | 传递给 task 工具的提示的 Promise。 | | callId | 委托任务的工具调用 ID。 | | namespace | 子智能体运行的命名空间路径。 | | toolCalls | 限定在子智能体范围内的工具调用。 |

跟踪子智能体生命周期

当你只需要显示哪些子智能体已启动和完成时,请使用 run.subagents。除非你访问单个子智能体上的那些投影,否则不需要订阅消息或值流。
const run = await agent.streamEvents(input, { version: "v3" });

let running = 0;
let completed = 0;
let failed = 0;
const watchers: Promise<void>[] = [];

for await (const subagent of run.subagents) {
  running += 1;
  console.log(`${subagent.name}: started (${subagent.callId})`);

  watchers.push(
    subagent.output.then(
      () => {
        running -= 1;
        completed += 1;
        console.log(`${subagent.name}: completed`);
      },
      () => {
        running -= 1;
        failed += 1;
        console.log(`${subagent.name}: failed`);
      }
    )
  );
}

await Promise.all(watchers);
console.log({ running, completed, failed });

流式输出消息和工具

深度智能体可以从协调器智能体和委托的子智能体发出消息和工具调用。使用 run.messagesrun.tool_calls 获取协调器流,然后在每个子智能体上访问相同的投影。
const run = await agent.streamEvents(input, { version: "v3" });

for await (const message of run.messages) {
  console.log("[coordinator]", await message.text);
}

for await (const subagent of run.subagents) {
  for await (const message of subagent.messages) {
    console.log(`[${subagent.name}]`, await message.text);
  }

  for await (const call of subagent.toolCalls) {
    console.log(`[${subagent.name} tool]`, call.name, call.input);
    console.log(await call.status);
  }
}

并发消费

协调器和子智能体的输出经常交错出现。当你需要实时 UI 更新时,请并发消费投影。 在 JavaScript 中使用并发消费者:
const run = await agent.streamEvents(input, { version: "v3" });

await Promise.all([
  (async () => {
    for await (const message of run.messages) {
      console.log("[coordinator]", await message.text);
    }
  })(),
  (async () => {
    for await (const subagent of run.subagents) {
      void (async () => {
        for await (const message of subagent.messages) {
          console.log(`[${subagent.name}]`, await message.text);
        }
      })();
    }
  })(),
]);

相关资源