@@ -2,6 +2,7 @@ import { v4 as uuidv4 } from 'uuid';
22import { z } from 'zod' ;
33import { zodToJsonSchema } from 'zod-to-json-schema' ;
44
5+ import { backgroundToolRegistry , BackgroundToolStatus , BackgroundToolType } from '../../core/backgroundTools.js' ;
56import {
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 ;
0 commit comments