async function decomposeTask(
task: string
): Promise<TaskGraph> {
const prompt = `
Decompose this task into subtasks:
${task}
Return a task graph in JSON:
{
"tasks": [
{
"id": "task1",
"description": "...",
"dependencies": [],
"estimatedTime": 30
}
]
}
`;
const response = await query([{ role: 'user', content: prompt }]);
return JSON.parse(response);
}async function orchestrateTasks(
graph: TaskGraph
): Promise<ExecutionResult> {
const completed = new Set<string>();
const results = new Map<string, any>();
while (completed.size < graph.tasks.length) {
// 找出可执行的任务
const ready = graph.tasks.filter(task =>
!completed.has(task.id) &&
task.dependencies.every(dep => completed.has(dep))
);
// 并行执行
const batchResults = await Promise.all(
ready.map(task => executeTask(task, results))
);
// 更新状态
ready.forEach((task, i) => {
completed.add(task.id);
results.set(task.id, batchResults[i]);
});
}
return { results };
}