SpyBara
Go Premium

agent-sdk/todo-tracking.md 2026-05-04 22:58 UTC to 2026-05-05 23:00 UTC

This page contains 189 additions and 0 deletions.

2026
Tue 5 23:00 Mon 18 23:59

待办事项列表

使用 Claude Agent SDK 跟踪和显示待办事项,实现有组织的任务管理

待办事项跟踪提供了一种结构化的方式来管理任务并向用户显示进度。Claude Agent SDK 包含内置的待办事项功能,可帮助组织复杂的工作流程并让用户了解任务进度。

待办事项生命周期

待办事项遵循可预测的生命周期:

  1. 创建为 pending 状态,当任务被识别时
  2. 激活为 in_progress 状态,当工作开始时
  3. 完成当任务成功完成时
  4. 移除当组中的所有任务都完成时

何时使用待办事项

SDK 会自动为以下情况创建待办事项:

  • 复杂的多步骤任务需要 3 个或更多不同的操作
  • 用户提供的任务列表当提到多个项目时
  • 非平凡的操作受益于进度跟踪
  • 明确的请求当用户要求组织待办事项时

示例

监控待办事项变化

import { query } from "@anthropic-ai/claude-agent-sdk";

for await (const message of query({
prompt: "Optimize my React app performance and track progress with todos",
options: { maxTurns: 15 }
})) {
// Todo updates are reflected in the message stream
if (message.type === "assistant") {
for (const block of message.message.content) {
if (block.type === "tool_use" && block.name === "TodoWrite") {
const todos = block.input.todos;

console.log("Todo Status Update:");
todos.forEach((todo, index) => {
const status =
todo.status === "completed" ? "✅" : todo.status === "in_progress" ? "🔧" : "❌";
console.log(`${index + 1}. ${status} ${todo.content}`);
});
}
}
}
}

实时进度显示

import { query } from "@anthropic-ai/claude-agent-sdk";

class TodoTracker {
private todos: any[] = [];

displayProgress() {
if (this.todos.length === 0) return;

const completed = this.todos.filter((t) => t.status === "completed").length;
const inProgress = this.todos.filter((t) => t.status === "in_progress").length;
const total = this.todos.length;

console.log(`\nProgress: ${completed}/${total} completed`);
console.log(`Currently working on: ${inProgress} task(s)\n`);

this.todos.forEach((todo, index) => {
const icon =
todo.status === "completed" ? "✅" : todo.status === "in_progress" ? "🔧" : "❌";
const text = todo.status === "in_progress" ? todo.activeForm : todo.content;
console.log(`${index + 1}. ${icon} ${text}`);
});
}

async trackQuery(prompt: string) {
for await (const message of query({
prompt,
options: { maxTurns: 20 }
})) {
if (message.type === "assistant") {
for (const block of message.message.content) {
if (block.type === "tool_use" && block.name === "TodoWrite") {
this.todos = block.input.todos;
this.displayProgress();
}
}
}
}
}
}

// Usage
const tracker = new TodoTracker();
await tracker.trackQuery("Build a complete authentication system with todos");

相关文档