Files
metona-ai-desktop/electron/harness/tools/built-in/delegate-task.ts
T
thzxx 6f08759f63 v0.1.2: 全项目代码审查修复 + 子代理编排器 + 上下文压缩 + 并行工具执行
后端修复: Ollama adapter 移除死代码/修复超时硬编码/iteration硬编码/pullModel无超时/流异常断开补发DONE; SSE解析器支持非字符串arguments; Sandbox fail-closed安全加固; IPC移除未使用变量

新增功能: TaskOrchestrator子任务编排器; DelegateTaskTool委派工具; Agent Loop并行工具执行; 上下文自动压缩; 记忆检索注入System Prompt

前端修复: useAgentStream text_delta thought累积bug; 工具调用状态正确流转; 修复重复stateChange事件; TraceStep.thought正确填充
2026-07-06 22:48:33 +08:00

78 lines
2.7 KiB
TypeScript

/**
* 子任务委派工具
*
* 允许主 Agent 将子任务委派给独立的 SubAgent 执行。
* SubAgent 运行在独立的 AgentLoopEngine 实例中,可限制可用工具。
*
* @see electron/harness/orchestration/orchestrator.ts — TaskOrchestrator
*/
import type { IMetonaTool, ToolExecutionContext } from '../../types/metona-tool';
import type { MetonaToolDef } from '../../../harness/types';
import { MetonaToolCategory, MetonaRiskLevel } from '../../../harness/types';
import type { TaskOrchestrator } from '../../orchestration/orchestrator';
export class DelegateTaskTool implements IMetonaTool {
readonly definition: MetonaToolDef = {
name: 'delegate_task',
description: 'Delegate a sub-task to an independent SubAgent for parallel or isolated execution. The SubAgent runs its own ReAct loop and returns the final result. Use this for complex sub-tasks that benefit from focused reasoning.',
parameters: {
type: 'object',
properties: {
description: {
type: 'string',
description: 'Clear and detailed description of the sub-task to delegate. This will be the SubAgent\'s user message.',
},
tools: {
type: 'array',
items: { type: 'string' },
description: 'Optional whitelist of tool names the SubAgent can use (e.g. ["read_file", "web_search"]). If omitted, SubAgent inherits all tools.',
},
maxIterations: {
type: 'number',
description: 'Maximum iterations for the SubAgent (default: 10)',
},
},
required: ['description'],
},
category: MetonaToolCategory.CUSTOM,
riskLevel: MetonaRiskLevel.MEDIUM,
requiresPermission: false,
timeoutMs: 300_000, // 5 分钟,子任务可能较复杂
};
constructor(private orchestrator: TaskOrchestrator) {}
async execute(args: Record<string, unknown>, context: ToolExecutionContext): Promise<unknown> {
const description = args.description as string;
const tools = args.tools as string[] | undefined;
const maxIterations = (args.maxIterations as number) ?? 10;
if (!description) {
return { success: false, error: 'Missing required parameter: description' };
}
try {
const result = await this.orchestrator.delegate({
description,
parentSessionId: context.sessionId,
maxIterations,
tools,
});
return {
success: result.success,
result: result.result,
durationMs: result.durationMs,
taskId: result.taskId,
iterations: result.iterations,
};
} catch (error) {
return {
success: false,
error: `SubAgent execution failed: ${(error as Error).message}`,
};
}
}
}