README
章节内容
核心执行流程
用户输入 → Query Loop → AI 响应 → 工具执行 → 结果反馈 → 继续对话用户输入 → Query Loop → AI 响应 → 工具执行 → 结果反馈 → 继续对话async function* query(messages, tools, options) {
// 流式接收 AI 响应
for await (const event of anthropic.messages.stream(...)) {
yield { type: 'content', content: event.delta.text };
}
// 流式执行工具
for await (const result of executeTools(toolCalls)) {
yield { type: 'tool_result', result };
}
}// 最多 5 个工具并发执行
const results = await Promise.all(
toolCalls.slice(0, 5).map(call => executeTool(call))
);if (tokenCount > 180000) {
await autoCompact(messages, { aggressive: true });
} else if (tokenCount > 150000) {
await contextCollapse(messages);
} else if (tokenCount > 120000) {
await microcompact(messages);
}// 工具输入验证
const input = FileEditInputSchema.parse(rawInput);
// 类型推导
type Input = z.infer<typeof FileEditInputSchema>;
// 运行时检查
if (!input.path.startsWith('/')) {
throw new Error('Path must be absolute');
}工具层错误 → 捕获并格式化
↓
服务层错误 → 记录并重试
↓
Query 层错误 → 通知用户
↓
UI 层错误 → 显示错误信息