子智能体
子智能体是你的智能体派生的独立智能体实例。父级使用 task() 或 subagent() hook 委托工作,子智能体在自己的对话上下文中运行并返回结果。父子关系允许你将工作分解为独立的、可并行化的单元,每个单元都有自己的状态和对话历史,同时共享父级的工作空间和沙盒。
创建子智能体
使用 task() hook 创建子智能体。在你的智能体函数内部调用它:
import { task } from '@flue/runtime';
export function Orchestrator() {
const research = task('Research competitor pricing.');
const design = task('Draft UI mockups for the new feature.');
// research 和 design 是子智能体的 promise
}
每个 task() 调用创建一个新的子智能体对话,传递指定的指令。子智能体继承父级的环境——相同的文件系统、相同的工具——但有自己的对话历史和状态。
子智能体继承的内容
子智能体从父级继承:
- 工作空间。 相同的文件系统、相同的工具集。子智能体可以读取和写入与父级相同的文件。
- 沙盒。 子智能体使用与父级相同的沙盒环境。
- 技能。 子智能体可以访问与父级相同的技能。
子智能体不继承:
- 对话历史。 子智能体从空白状态开始。
- 持久状态。 子智能体有自己的
usePersistentState()实例。 - 模型选择。 子智能体可以指定自己的模型。
task 工具始终存在,但其必需的 agent 参数仅对声明的委托进行解析——没有 useSubagent() 调用的智能体没有可传递的有效值,无法进行委托。模型还可以传递可选的 cwd 将子级指向不同的工作目录,并可以通过附件 ID 转发对话中的图片和文档。
子智能体作为异步操作运行。使用返回的 promise 来等待结果:
export function Orchestrator() {
const research = task('Research competitor pricing.');
const design = task('Draft UI mockups for the new feature.');
// 等待两个子智能体完成
const researchResult = await research;
const designResult = await design;
return 'Combine research and design into a proposal.';
}
默认情况下,task() 在后台运行子智能体。要同步等待结果,在创建子智能体之前调用 await:
export function Orchestrator() {
// 这些按顺序运行
const research = await task('Research competitor pricing.');
const design = await task('Draft UI mockups for the new feature.');
return 'Combine research and design into a proposal.';
}
子智能体选项
task() 和 subagent() 接受选项来自定义子智能体:
const research = task('Research competitor pricing.', {
model: 'anthropic/claude-sonnet-4-6', // 使用不同的模型
cwd: '/workspace/research', // 限定到不同的工作目录
timeout: 30_000, // 30 秒超时
metadata: { taskId: 'research-001' }, // 任意元数据
});
选项:
- model —— 覆盖子智能体的模型选择。
- cwd —— 设置子智能体的工作目录。它仍然在父级的沙盒内,但
AGENTS.md发现和工具默认在该目录中操作。 - timeout —— 子智能体的最大执行时间(毫秒)。
- metadata —— 与子智能体关联的任意键值数据。
子智能体 hook
使用 subagent() hook 获得对子智能体生命周期的更多控制:
import { subagent } from '@flue/runtime';
export function Orchestrator() {
const agent = subagent('Research competitor pricing.');
// 访问子智能体的对话
const conversation = agent.conversation;
// 在子智能体运行时发送额外消息
await agent.send('Also check recent funding rounds.');
// 等待完成
const result = await agent.wait();
return result;
}
subagent() 返回一个具有以下属性的对象:
- conversation —— 子智能体的对话实例。
- send(message) —— 向子智能体发送额外消息。
- wait() —— 等待子智能体完成并返回结果。
- abort() —— 中止子智能体。
- id —— 子智能体的唯一 ID。
并行执行
子智能体默认并行运行。利用这一点来同时运行独立的任务:
export function Orchestrator() {
// 同时启动所有子智能体
const research = task('Research competitor pricing.');
const design = task('Draft UI mockups.');
const testing = task('Write unit tests for the auth module.');
// 等待所有子智能体完成
const [researchResult, designResult, testingResult] = await Promise.all([
research,
design,
testing,
]);
return 'Combine all results into a final report.';
}
当任务是独立的时,并行执行显著减少总执行时间。
错误处理
子智能体可能失败,因此处理错误:
export function Orchestrator() {
try {
const result = await task('Fetch data from external API.');
return result;
} catch (error) {
// 处理子智能体失败
return 'The data fetch failed. Using cached data instead.';
}
}
默认情况下,子智能体失败会传播到父级。使用 try/catch 来优雅地处理失败,或使用 timeout 选项来限制执行时间。
子智能体模式
研究与综合。 派生多个研究子智能体,每个关注不同的方面,然后综合结果。
并行处理。 同时处理多个数据源或文件。
专家子智能体。 为特定任务创建专门的子智能体(例如,代码审查、测试编写、文档生成)。
流水线。 链式运行子智能体,每个处理前一个的输出。