@@ -23,107 +23,138 @@ export interface AgentInfo {
2323 error ?: string ;
2424
2525 // Internal state information
26- prompt : string ;
26+ prompt ? : string ;
2727 output : string ;
2828 capturedLogs : string [ ] ; // Captured log messages from agent and immediate tools
2929 completed : boolean ;
3030 result_detailed ?: ToolAgentResult ;
31- context : ToolContext ;
32- workingDirectory : string ;
33- tools : unknown [ ] ;
31+ context ? : ToolContext ;
32+ workingDirectory ? : string ;
33+ tools ? : unknown [ ] ;
3434 aborted : boolean ;
3535 parentMessages : string [ ] ; // Messages from parent agent
3636}
3737
38- // For backward compatibility
39- export type Agent = Pick <
40- AgentInfo ,
41- 'agentId' | 'status' | 'startTime' | 'endTime' | 'goal' | 'result' | 'error'
42- > ;
43- export type AgentState = Pick <
44- AgentInfo ,
45- | 'agentId'
46- | 'goal'
47- | 'prompt'
48- | 'output'
49- | 'capturedLogs'
50- | 'completed'
51- | 'error'
52- | 'context'
53- | 'workingDirectory'
54- | 'tools'
55- | 'aborted'
56- | 'parentMessages'
57- > & { result ?: ToolAgentResult } ;
38+ // For backward compatibility - these are deprecated and will be removed in a future version
39+ /** @deprecated Use AgentInfo instead */
40+ export type Agent = AgentInfo ;
41+ /** @deprecated Use AgentInfo instead */
42+ export type AgentState = AgentInfo ;
5843
5944export class AgentTracker {
6045 private agentInfos : Map < string , AgentInfo > = new Map ( ) ;
6146
6247 constructor ( public ownerAgentId : string | undefined ) { }
6348
64- // Register a new agent
65- public registerAgent ( goal : string ) : string {
66- const agentId = uuidv4 ( ) ;
67-
68- // Create basic agent info entry
69- const agentInfo : Partial < AgentInfo > = {
70- agentId : agentId ,
71- status : AgentStatus . RUNNING ,
72- startTime : new Date ( ) ,
73- goal,
74- // Initialize arrays and default values
75- capturedLogs : [ ] ,
76- completed : false ,
77- aborted : false ,
78- parentMessages : [ ] ,
79- output : '' ,
80- } ;
81-
82- this . agentInfos . set ( agentId , agentInfo as AgentInfo ) ;
83- return agentId ;
84- }
85-
86- // Register agent state - for backward compatibility
87- public registerAgentState ( agentId : string , state : AgentState ) : void {
88- const agentInfo = this . agentInfos . get ( agentId ) ;
89-
90- if ( ! agentInfo ) {
91- // If agent doesn't exist yet (shouldn't happen in normal flow), create it
92- const newAgentInfo : AgentInfo = {
93- agentId : state . agentId ,
49+ /**
50+ * Register a new agent with basic information or update an existing agent with full state
51+ * @param goalOrState Either a goal string or a complete AgentInfo object
52+ * @param state Optional additional state information to set
53+ * @returns The agent ID
54+ */
55+ public registerAgent (
56+ goalOrState : string | Partial < AgentInfo > ,
57+ state ?: Partial < AgentInfo > ,
58+ ) : string {
59+ let agentId : string ;
60+
61+ // Case 1: Simple registration with just a goal string
62+ if ( typeof goalOrState === 'string' ) {
63+ agentId = uuidv4 ( ) ;
64+
65+ // Create basic agent info entry
66+ const agentInfo : AgentInfo = {
67+ agentId,
9468 status : AgentStatus . RUNNING ,
9569 startTime : new Date ( ) ,
96- goal : state . goal ,
97- prompt : state . prompt ,
98- output : state . output ,
99- capturedLogs : state . capturedLogs ,
100- completed : state . completed ,
101- error : state . error ,
102- result_detailed : state . result ,
103- context : state . context ,
104- workingDirectory : state . workingDirectory ,
105- tools : state . tools ,
106- aborted : state . aborted ,
107- parentMessages : state . parentMessages ,
70+ goal : goalOrState ,
71+ // Initialize arrays and default values
72+ capturedLogs : [ ] ,
73+ completed : false ,
74+ aborted : false ,
75+ parentMessages : [ ] ,
76+ output : '' ,
10877 } ;
109- this . agentInfos . set ( agentId , newAgentInfo ) ;
110- return ;
78+
79+ this . agentInfos . set ( agentId , agentInfo ) ;
11180 }
81+ // Case 2: Registration with a partial or complete AgentInfo object
82+ else {
83+ if ( goalOrState . agentId ) {
84+ // Use existing ID if provided
85+ agentId = goalOrState . agentId ;
86+
87+ // Check if agent already exists
88+ const existingAgent = this . agentInfos . get ( agentId ) ;
89+
90+ if ( existingAgent ) {
91+ // Update existing agent
92+ Object . assign ( existingAgent , goalOrState ) ;
93+ } else {
94+ // Create new agent with provided ID
95+ const newAgent : AgentInfo = {
96+ // Set defaults for required fields
97+ agentId,
98+ status : AgentStatus . RUNNING ,
99+ startTime : new Date ( ) ,
100+ goal : goalOrState . goal || 'Unknown goal' ,
101+ capturedLogs : [ ] ,
102+ completed : false ,
103+ aborted : false ,
104+ parentMessages : [ ] ,
105+ output : '' ,
106+ // Merge in provided values
107+ ...goalOrState ,
108+ } ;
109+
110+ this . agentInfos . set ( agentId , newAgent ) ;
111+ }
112+ } else {
113+ // Generate new ID if not provided
114+ agentId = uuidv4 ( ) ;
115+
116+ // Create new agent
117+ const newAgent : AgentInfo = {
118+ // Set defaults for required fields
119+ agentId,
120+ status : AgentStatus . RUNNING ,
121+ startTime : new Date ( ) ,
122+ goal : goalOrState . goal || 'Unknown goal' ,
123+ capturedLogs : [ ] ,
124+ completed : false ,
125+ aborted : false ,
126+ parentMessages : [ ] ,
127+ output : '' ,
128+ // Merge in provided values
129+ ...goalOrState ,
130+ } ;
131+
132+ this . agentInfos . set ( agentId , newAgent ) ;
133+ }
134+ }
135+
136+ // Apply additional state if provided
137+ if ( state ) {
138+ const agent = this . agentInfos . get ( agentId ) ;
139+ if ( agent ) {
140+ Object . assign ( agent , state ) ;
141+ }
142+ }
143+
144+ return agentId ;
145+ }
146+
147+ /**
148+ * @deprecated Use registerAgent instead
149+ */
150+ public registerAgentState ( agentId : string , state : AgentState ) : void {
151+ // Make a copy of state without the agentId to avoid duplication
152+ const { agentId : _ , ...stateWithoutId } = state ;
112153
113- // Update existing agent info with state data
114- Object . assign ( agentInfo , {
115- goal : state . goal ,
116- prompt : state . prompt ,
117- output : state . output ,
118- capturedLogs : state . capturedLogs ,
119- completed : state . completed ,
120- error : state . error ,
121- result_detailed : state . result ,
122- context : state . context ,
123- workingDirectory : state . workingDirectory ,
124- tools : state . tools ,
125- aborted : state . aborted ,
126- parentMessages : state . parentMessages ,
154+ // Register with the correct agentId
155+ this . registerAgent ( {
156+ ...stateWithoutId ,
157+ agentId,
127158 } ) ;
128159 }
129160
@@ -156,66 +187,36 @@ export class AgentTracker {
156187 return true ;
157188 }
158189
159- // Get a specific agent info
160- public getAgentInfo ( agentId : string ) : AgentInfo | undefined {
190+ /**
191+ * Get an agent by ID
192+ * @param agentId The agent ID
193+ * @returns The agent info or undefined if not found
194+ */
195+ public getAgent ( agentId : string ) : AgentInfo | undefined {
161196 return this . agentInfos . get ( agentId ) ;
162197 }
163198
164- // Get a specific agent state - for backward compatibility
165- public getAgentState ( agentId : string ) : AgentState | undefined {
166- const agentInfo = this . agentInfos . get ( agentId ) ;
167- if ( ! agentInfo ) return undefined ;
168-
169- // Convert AgentInfo to AgentState
170- const state : AgentState = {
171- agentId : agentInfo . agentId ,
172- goal : agentInfo . goal ,
173- prompt : agentInfo . prompt ,
174- output : agentInfo . output ,
175- capturedLogs : agentInfo . capturedLogs ,
176- completed : agentInfo . completed ,
177- error : agentInfo . error ,
178- result : agentInfo . result_detailed ,
179- context : agentInfo . context ,
180- workingDirectory : agentInfo . workingDirectory ,
181- tools : agentInfo . tools ,
182- aborted : agentInfo . aborted ,
183- parentMessages : agentInfo . parentMessages ,
184- } ;
185-
186- return state ;
199+ /**
200+ * @deprecated Use getAgent instead
201+ */
202+ public getAgentInfo ( agentId : string ) : AgentInfo | undefined {
203+ return this . getAgent ( agentId ) ;
187204 }
188205
189- // Get a specific agent tracking info - for backward compatibility
190- public getAgent ( agentId : string ) : Agent | undefined {
191- const agentInfo = this . agentInfos . get ( agentId ) ;
192- if ( ! agentInfo ) return undefined ;
193-
194- // Convert AgentInfo to Agent
195- const agent : Agent = {
196- agentId : agentInfo . agentId ,
197- status : agentInfo . status ,
198- startTime : agentInfo . startTime ,
199- endTime : agentInfo . endTime ,
200- goal : agentInfo . goal ,
201- result : agentInfo . result ,
202- error : agentInfo . error ,
203- } ;
204-
205- return agent ;
206+ /**
207+ * @deprecated Use getAgent instead
208+ */
209+ public getAgentState ( agentId : string ) : AgentState | undefined {
210+ return this . getAgent ( agentId ) ;
206211 }
207212
208- // Get all agents with optional filtering
209- public getAgents ( status ?: AgentStatus ) : Agent [ ] {
210- const agents = Array . from ( this . agentInfos . values ( ) ) . map ( ( info ) => ( {
211- agentId : info . agentId ,
212- status : info . status ,
213- startTime : info . startTime ,
214- endTime : info . endTime ,
215- goal : info . goal ,
216- result : info . result ,
217- error : info . error ,
218- } ) ) ;
213+ /**
214+ * Get all agents, optionally filtered by status
215+ * @param status Optional status to filter by
216+ * @returns Array of agents
217+ */
218+ public getAgents ( status ?: AgentStatus ) : AgentInfo [ ] {
219+ const agents = Array . from ( this . agentInfos . values ( ) ) ;
219220
220221 if ( ! status ) {
221222 return agents ;
@@ -226,19 +227,18 @@ export class AgentTracker {
226227
227228 /**
228229 * Get list of active agents with their descriptions
230+ * @deprecated Use getAgents(AgentStatus.RUNNING) instead
229231 */
230232 public getActiveAgents ( ) : Array < {
231233 agentId : string ;
232234 description : string ;
233235 status : AgentStatus ;
234236 } > {
235- return Array . from ( this . agentInfos . values ( ) )
236- . filter ( ( info ) => info . status === AgentStatus . RUNNING )
237- . map ( ( info ) => ( {
238- agentId : info . agentId ,
239- description : info . goal ,
240- status : info . status ,
241- } ) ) ;
237+ return this . getAgents ( AgentStatus . RUNNING ) . map ( ( info ) => ( {
238+ agentId : info . agentId ,
239+ description : info . goal ,
240+ status : info . status ,
241+ } ) ) ;
242242 }
243243
244244 // Cleanup and terminate agents
@@ -260,9 +260,11 @@ export class AgentTracker {
260260 agentInfo . completed = true ;
261261
262262 // Clean up resources owned by this sub-agent
263- await agentInfo . context . agentTracker . cleanup ( ) ;
264- await agentInfo . context . shellTracker . cleanup ( ) ;
265- await agentInfo . context . browserTracker . cleanup ( ) ;
263+ if ( agentInfo . context ) {
264+ await agentInfo . context . agentTracker . cleanup ( ) ;
265+ await agentInfo . context . shellTracker . cleanup ( ) ;
266+ await agentInfo . context . browserTracker . cleanup ( ) ;
267+ }
266268 }
267269 this . updateAgentStatus ( agentId , AgentStatus . TERMINATED ) ;
268270 } catch ( error ) {
0 commit comments