|
| 1 | +# Agent Tools |
| 2 | + |
| 3 | +The agent tools provide ways to create and interact with sub-agents. There are two approaches available: |
| 4 | + |
| 5 | +1. The original `subAgent` tool (synchronous, blocking) |
| 6 | +2. The new `agentStart` and `agentMessage` tools (asynchronous, non-blocking) |
| 7 | + |
| 8 | +## subAgent Tool |
| 9 | + |
| 10 | +The `subAgent` tool creates a sub-agent that runs synchronously until completion. The parent agent waits for the sub-agent to complete before continuing. |
| 11 | + |
| 12 | +```typescript |
| 13 | +subAgent({ |
| 14 | + description: "A brief description of the sub-agent's purpose", |
| 15 | + goal: "The main objective that the sub-agent needs to achieve", |
| 16 | + projectContext: "Context about the problem or environment", |
| 17 | + workingDirectory: "/path/to/working/directory", // optional |
| 18 | + relevantFilesDirectories: "src/**/*.ts", // optional |
| 19 | +}); |
| 20 | +``` |
| 21 | + |
| 22 | +## agentStart and agentMessage Tools |
| 23 | + |
| 24 | +The `agentStart` and `agentMessage` tools provide an asynchronous approach to working with sub-agents. This allows the parent agent to: |
| 25 | + |
| 26 | +- Start multiple sub-agents in parallel |
| 27 | +- Monitor sub-agent progress |
| 28 | +- Provide guidance to sub-agents |
| 29 | +- Terminate sub-agents if needed |
| 30 | + |
| 31 | +### agentStart |
| 32 | + |
| 33 | +The `agentStart` tool creates a sub-agent and immediately returns an instance ID. The sub-agent runs asynchronously in the background. |
| 34 | + |
| 35 | +```typescript |
| 36 | +const { instanceId } = agentStart({ |
| 37 | + description: "A brief description of the sub-agent's purpose", |
| 38 | + goal: "The main objective that the sub-agent needs to achieve", |
| 39 | + projectContext: "Context about the problem or environment", |
| 40 | + workingDirectory: "/path/to/working/directory", // optional |
| 41 | + relevantFilesDirectories: "src/**/*.ts", // optional |
| 42 | + enableUserPrompt: false, // optional, default: false |
| 43 | +}); |
| 44 | +``` |
| 45 | + |
| 46 | +### agentMessage |
| 47 | + |
| 48 | +The `agentMessage` tool allows interaction with a running sub-agent. It can be used to check the agent's progress, provide guidance, or terminate the agent. |
| 49 | + |
| 50 | +```typescript |
| 51 | +// Check agent progress |
| 52 | +const { output, completed } = agentMessage({ |
| 53 | + instanceId: "agent-instance-id", |
| 54 | + description: "Checking agent progress", |
| 55 | +}); |
| 56 | + |
| 57 | +// Provide guidance (note: guidance implementation is limited in the current version) |
| 58 | +agentMessage({ |
| 59 | + instanceId: "agent-instance-id", |
| 60 | + guidance: "Focus on the task at hand and avoid unnecessary exploration", |
| 61 | + description: "Providing guidance to the agent", |
| 62 | +}); |
| 63 | + |
| 64 | +// Terminate the agent |
| 65 | +agentMessage({ |
| 66 | + instanceId: "agent-instance-id", |
| 67 | + terminate: true, |
| 68 | + description: "Terminating the agent", |
| 69 | +}); |
| 70 | +``` |
| 71 | + |
| 72 | +## Example: Using agentStart and agentMessage to run multiple sub-agents in parallel |
| 73 | + |
| 74 | +```typescript |
| 75 | +// Start multiple sub-agents |
| 76 | +const agent1 = agentStart({ |
| 77 | + description: "Agent 1", |
| 78 | + goal: "Implement feature A", |
| 79 | + projectContext: "Project X", |
| 80 | +}); |
| 81 | + |
| 82 | +const agent2 = agentStart({ |
| 83 | + description: "Agent 2", |
| 84 | + goal: "Implement feature B", |
| 85 | + projectContext: "Project X", |
| 86 | +}); |
| 87 | + |
| 88 | +// Check progress of both agents |
| 89 | +let agent1Completed = false; |
| 90 | +let agent2Completed = false; |
| 91 | + |
| 92 | +while (!agent1Completed || !agent2Completed) { |
| 93 | + if (!agent1Completed) { |
| 94 | + const result1 = agentMessage({ |
| 95 | + instanceId: agent1.instanceId, |
| 96 | + description: "Checking Agent 1 progress", |
| 97 | + }); |
| 98 | + agent1Completed = result1.completed; |
| 99 | + |
| 100 | + if (agent1Completed) { |
| 101 | + console.log("Agent 1 completed with result:", result1.output); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + if (!agent2Completed) { |
| 106 | + const result2 = agentMessage({ |
| 107 | + instanceId: agent2.instanceId, |
| 108 | + description: "Checking Agent 2 progress", |
| 109 | + }); |
| 110 | + agent2Completed = result2.completed; |
| 111 | + |
| 112 | + if (agent2Completed) { |
| 113 | + console.log("Agent 2 completed with result:", result2.output); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + // Wait before checking again |
| 118 | + if (!agent1Completed || !agent2Completed) { |
| 119 | + sleep({ seconds: 5 }); |
| 120 | + } |
| 121 | +} |
| 122 | +``` |
| 123 | + |
| 124 | +## Choosing Between Approaches |
| 125 | + |
| 126 | +- Use `subAgent` for simpler tasks where blocking execution is acceptable |
| 127 | +- Use `agentStart` and `agentMessage` for: |
| 128 | + - Parallel execution of multiple sub-agents |
| 129 | + - Tasks where you need to monitor progress |
| 130 | + - Situations where you may need to provide guidance or terminate early |
0 commit comments