Skip to content

Commit 4a3bcc7

Browse files
committed
feat: add agent tracking to background tools
1 parent b5bb489 commit 4a3bcc7

File tree

3 files changed

+53
-8
lines changed

3 files changed

+53
-8
lines changed

packages/agent/src/tools/interaction/agentMessage.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { z } from 'zod';
22
import { zodToJsonSchema } from 'zod-to-json-schema';
33

4+
import { backgroundToolRegistry, BackgroundToolStatus } from '../../core/backgroundTools.js';
45
import { Tool } from '../../core/types.js';
56

67
import { agentStates } from './agentStart.js';
@@ -75,6 +76,11 @@ export const agentMessageTool: Tool<Parameters, ReturnType> = {
7576
if (terminate) {
7677
agentState.aborted = true;
7778
agentState.completed = true;
79+
80+
// Update background tool registry with terminated status
81+
backgroundToolRegistry.updateToolStatus(instanceId, BackgroundToolStatus.TERMINATED, {
82+
terminatedByUser: true
83+
});
7884

7985
return {
8086
output: agentState.output || 'Sub-agent terminated before completion',

packages/agent/src/tools/interaction/agentStart.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { v4 as uuidv4 } from 'uuid';
22
import { z } from 'zod';
33
import { zodToJsonSchema } from 'zod-to-json-schema';
44

5+
import { backgroundToolRegistry, BackgroundToolStatus, BackgroundToolType } from '../../core/backgroundTools.js';
56
import {
67
getDefaultSystemPrompt,
78
getModel,
@@ -90,6 +91,8 @@ export const agentStartTool: Tool<Parameters, ReturnType> = {
9091
returns: returnSchema,
9192
returnsJsonSchema: zodToJsonSchema(returnSchema),
9293
execute: async (params, context) => {
94+
const { logger, agentId } = context;
95+
9396
// Validate parameters
9497
const {
9598
description,
@@ -99,6 +102,13 @@ export const agentStartTool: Tool<Parameters, ReturnType> = {
99102
relevantFilesDirectories,
100103
enableUserPrompt = false,
101104
} = parameterSchema.parse(params);
105+
106+
// Create an instance ID
107+
const instanceId = uuidv4();
108+
109+
// Register this agent with the background tool registry
110+
backgroundToolRegistry.registerAgent(agentId || 'unknown', goal);
111+
logger.verbose(`Registered agent with ID: ${instanceId}`);
102112

103113
// Construct a well-structured prompt
104114
const prompt = [
@@ -115,9 +125,6 @@ export const agentStartTool: Tool<Parameters, ReturnType> = {
115125

116126
const tools = getTools({ enableUserPrompt });
117127

118-
// Create an instance ID
119-
const instanceId = uuidv4();
120-
121128
// Store the agent state
122129
const agentState: AgentState = {
123130
goal,
@@ -147,13 +154,23 @@ export const agentStartTool: Tool<Parameters, ReturnType> = {
147154
state.completed = true;
148155
state.result = result;
149156
state.output = result.result;
157+
158+
// Update background tool registry with completed status
159+
backgroundToolRegistry.updateToolStatus(instanceId, BackgroundToolStatus.COMPLETED, {
160+
result: result.result.substring(0, 100) + (result.result.length > 100 ? '...' : '')
161+
});
150162
}
151163
} catch (error) {
152164
// Update agent state with the error
153165
const state = agentStates.get(instanceId);
154166
if (state && !state.aborted) {
155167
state.completed = true;
156168
state.error = error instanceof Error ? error.message : String(error);
169+
170+
// Update background tool registry with error status
171+
backgroundToolRegistry.updateToolStatus(instanceId, BackgroundToolStatus.ERROR, {
172+
error: error instanceof Error ? error.message : String(error)
173+
});
157174
}
158175
}
159176
return true;

packages/agent/src/tools/interaction/subAgent.ts

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { z } from 'zod';
22
import { zodToJsonSchema } from 'zod-to-json-schema';
33

4+
import { backgroundToolRegistry, BackgroundToolStatus, BackgroundToolType } from '../../core/backgroundTools.js';
45
import {
56
getDefaultSystemPrompt,
67
getModel,
@@ -68,6 +69,8 @@ export const subAgentTool: Tool<Parameters, ReturnType> = {
6869
returns: returnSchema,
6970
returnsJsonSchema: zodToJsonSchema(returnSchema),
7071
execute: async (params, context) => {
72+
const { logger, agentId } = context;
73+
7174
// Validate parameters
7275
const {
7376
description,
@@ -76,6 +79,10 @@ export const subAgentTool: Tool<Parameters, ReturnType> = {
7679
workingDirectory,
7780
relevantFilesDirectories,
7881
} = parameterSchema.parse(params);
82+
83+
// Register this sub-agent with the background tool registry
84+
const subAgentId = backgroundToolRegistry.registerAgent(agentId || 'unknown', goal);
85+
logger.verbose(`Registered sub-agent with ID: ${subAgentId}`);
7986

8087
// Construct a well-structured prompt
8188
const prompt = [
@@ -97,11 +104,26 @@ export const subAgentTool: Tool<Parameters, ReturnType> = {
97104
...subAgentConfig,
98105
};
99106

100-
const result = await toolAgent(prompt, tools, config, {
101-
...context,
102-
workingDirectory: workingDirectory ?? context.workingDirectory,
103-
});
104-
return { response: result.result };
107+
try {
108+
const result = await toolAgent(prompt, tools, config, {
109+
...context,
110+
workingDirectory: workingDirectory ?? context.workingDirectory,
111+
});
112+
113+
// Update background tool registry with completed status
114+
backgroundToolRegistry.updateToolStatus(subAgentId, BackgroundToolStatus.COMPLETED, {
115+
result: result.result.substring(0, 100) + (result.result.length > 100 ? '...' : '')
116+
});
117+
118+
return { response: result.result };
119+
} catch (error) {
120+
// Update background tool registry with error status
121+
backgroundToolRegistry.updateToolStatus(subAgentId, BackgroundToolStatus.ERROR, {
122+
error: error instanceof Error ? error.message : String(error)
123+
});
124+
125+
throw error;
126+
}
105127
},
106128
logParameters: (input, { logger }) => {
107129
logger.info(`Delegating task "${input.description}"`);

0 commit comments

Comments
 (0)