8000 feat: implement background tool tracking (issue #112) by bhouston · Pull Request #216 · drivecore/mycoder · GitHub
[go: up one dir, main page]

Skip to content

feat: implement background tool tracking (issue #112) #216

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 12, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
feat: add agent tracking to background tools
  • Loading branch information
bhouston committed Mar 12, 2025
commit 4a3bcc72f27af5fdbeeb407a748d5ecf3b7faed5
6 changes: 6 additions & 0 deletions packages/agent/src/tools/interaction/agentMessage.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { z } from 'zod';
import { zodToJsonSchema } from 'zod-to-json-schema';

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

import { agentStates } from './agentStart.js';
Expand Down Expand Up @@ -75,6 +76,11 @@ export const agentMessageTool: Tool<Parameters, ReturnType> = {
if (terminate) {
agentState.aborted = true;
agentState.completed = true;

// Update background tool registry with terminated status
backgroundToolRegistry.updateToolStatus(instanceId, BackgroundToolStatus.TERMINATED, {
terminatedByUser: true
});

return {
output: agentState.output || 'Sub-agent terminated before completion',
Expand Down
23 changes: 20 additions & 3 deletions packages/agent/src/tools/interaction/agentStart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { v4 as uuidv4 } from 'uuid';
import { z } from 'zod';
import { zodToJsonSchema } from 'zod-to-json-schema';

import { backgroundToolRegistry, BackgroundToolStatus, BackgroundToolType } from '../../core/backgroundTools.js';
import {
getDefaultSystemPrompt,
getModel,
Expand Down Expand Up @@ -90,6 +91,8 @@ export const agentStartTool: Tool<Parameters, ReturnType> = {
returns: returnSchema,
returnsJsonSchema: zodToJsonSchema(returnSchema),
execute: async (params, context) => {
const { logger, agentId } = context;

// Validate parameters
const {
description,
Expand All @@ -99,6 +102,13 @@ export const agentStartTool: Tool<Parameters, ReturnType> = {
relevantFilesDir 8000 ectories,
enableUserPrompt = false,
} = parameterSchema.parse(params);

// Create an instance ID
const instanceId = uuidv4();

// Register this agent with the background tool registry
backgroundToolRegistry.registerAgent(agentId || 'unknown', goal);
logger.verbose(`Registered agent with ID: ${instanceId}`);

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

const tools = getTools({ enableUserPrompt });

// Create an instance ID
const instanceId = uuidv4();

// Store the agent state
const agentState: AgentState = {
goal,
Expand Down Expand Up @@ -147,13 +154,23 @@ export const agentStartTool: Tool<Parameters, ReturnType> = {
state.completed = true;
state.result = result;
state.output = result.result;

// Update background tool registry with completed status
backgroundToolRegistry.updateToolStatus(instanceId, BackgroundToolStatus.COMPLETED, {
result: result.result.substring(0, 100) + (result.result.length > 100 ? '...' : '')
});
}
} catch (error) {
// Update agent state with the error
const state = agentStates.get(instanceId);
if (state && !state.aborted) {
state.completed = true;
state.error = error instanceof Error ? error.message : String(error);

// Update background tool registry with error status
backgroundToolRegistry.updateToolStatus(instanceId, BackgroundToolStatus.ERROR, {
error: error instanceof Error ? error.message : String(error)
});
}
}
return true;
Expand Down
32 changes: 27 additions & 5 deletions packages/agent/src/tools/interaction/subAgent.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { z } from 'zod';
import { zodToJsonSchema } from 'zod-to-json-schema';

import { backgroundToolRegistry, BackgroundToolStatus, BackgroundToolType } from '../../core/backgroundTools.js';
import {
getDefaultSystemPrompt,
getModel,
Expand Down Expand Up @@ -68,6 +69,8 @@ export const subAgentTool: Tool<Parameters, ReturnType> = {
returns: returnSchema,
returnsJsonSchema: zodToJsonSchema(returnSchema),
execute: async (params, context) => {
const { logger, agentId } = context;

// Validate parameters
const {
description,
Expand All @@ -76,6 +79,10 @@ export const subAgentTool: Tool<Parameters, ReturnType> = {
workingDirectory,
relevantFilesDirectories,
} = parameterSchema.parse(params);

// Register this sub-agent with the background tool registry
const subAgentId = backgroundToolRegistry.registerAgent(agentId || 'unknown', goal);
logger.verbose(`Registered sub-agent with ID: ${subAgentId}`);

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

const result = await toolAgent(prompt, tools, config, {
...context,
workingDirectory: workingDirectory ?? context.workingDirectory,
});
return { response: result.result };
try {
const result = await toolAgent(prompt, tools, config, {
...context,
workingDirectory: workingDirectory ?? context.workingDirectory,
});

// Update background tool registry with completed status
backgroundToolRegistry.updateToolStatus(subAgentId, BackgroundToolStatus.COMPLETED, {
result: result.result.substring(0, 100) + (result.result.length > 100 ? '...' : '')
});

return { response: result.result };
} catch (error) {
// Update background tool registry with error status
backgroundToolRegistry.updateToolStatus(subAgentId, BackgroundToolStatus.ERROR, {
error: error instanceof Error ? error.message : String(error)
});

throw error;
}
},
logParameters: (input, { logger }) => {
logger.info(`Delegating task "${input.description}"`);
Expand Down
Loading
0