@@ -10,58 +10,121 @@ export enum AgentStatus {
1010 TERMINATED = 'terminated' ,
1111}
1212
13- export interface Agent {
13+ export interface AgentInfo {
14+ // Basic identification and status
1415 agentId : string ;
1516 status : AgentStatus ;
1617 startTime : Date ;
1718 endTime ?: Date ;
1819 goal : string ;
20+
21+ // Result information
1922 result ?: string ;
2023 error ?: string ;
21- }
2224
23- // Internal agent state tracking (similar to existing agentStates)
24- export interface AgentState {
25- agentId : string ;
26- goal : string ;
25+ // Internal state information
2726 prompt : string ;
2827 output : string ;
2928 capturedLogs : string [ ] ; // Captured log messages from agent and immediate tools
3029 completed : boolean ;
31- error ?: string ;
32- result ?: ToolAgentResult ;
30+ result_detailed ?: ToolAgentResult ;
3331 context : ToolContext ;
3432 workingDirectory : string ;
3533 tools : unknown [ ] ;
3634 aborted : boolean ;
3735 parentMessages : string [ ] ; // Messages from parent agent
3836}
3937
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 } ;
58+
4059export class AgentTracker {
41- private agents : Map < string , Agent > = new Map ( ) ;
42- private agentStates : Map < string , AgentState > = new Map ( ) ;
60+ private agentInfos : Map < string , AgentInfo > = new Map ( ) ;
4361
4462 constructor ( public ownerAgentId : string | undefined ) { }
4563
4664 // Register a new agent
4765 public registerAgent ( goal : string ) : string {
4866 const agentId = uuidv4 ( ) ;
4967
50- // Create agent tracking entry
51- const agent : Agent = {
68+ // Create basic agent info entry
69+ const agentInfo : Partial < AgentInfo > = {
5270 agentId : agentId ,
5371 status : AgentStatus . RUNNING ,
5472 startTime : new Date ( ) ,
5573 goal,
74+ // Initialize arrays and default values
75+ capturedLogs : [ ] ,
76+ completed : false ,
77+ aborted : false ,
78+ parentMessages : [ ] ,
79+ output : '' ,
5680 } ;
5781
58- this . agents . set ( agentId , agent ) ;
82+ this . agentInfos . set ( agentId , agentInfo as AgentInfo ) ;
5983 return agentId ;
6084 }
6185
62- // Register agent state
86+ // Register agent state - for backward compatibility
6387 public registerAgentState ( agentId : string , state : AgentState ) : void {
64- this . agentStates . set ( agentId , state ) ;
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 ,
94+ status : AgentStatus . RUNNING ,
95+ 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 ,
108+ } ;
109+ this . agentInfos . set ( agentId , newAgentInfo ) ;
110+ return ;
111+ }
112+
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 ,
127+ } ) ;
65128 }
66129
67130 // Update agent status
@@ -70,48 +133,95 @@ export class AgentTracker {
70133 status : AgentStatus ,
71134 metadata ?: { result ?: string ; error ?: string } ,
72135 ) : boolean {
73- const agent = this . agents . get ( agentId ) ;
74- if ( ! agent ) {
136+ const agentInfo = this . agentInfos . get ( agentId ) ;
137+ if ( ! agentInfo ) {
75138 return false ;
76139 }
77140
78- agent . status = status ;
141+ agentInfo . status = status ;
79142
80143 if (
81144 status === AgentStatus . COMPLETED ||
82145 status === AgentStatus . ERROR ||
83146 status === AgentStatus . TERMINATED
84147 ) {
85- agent . endTime = new Date ( ) ;
148+ agentInfo . endTime = new Date ( ) ;
86149 }
87150
88151 if ( metadata ) {
89- if ( metadata . result !== undefined ) agent . result = metadata . result ;
90- if ( metadata . error !== undefined ) agent . error = metadata . error ;
152+ if ( metadata . result !== undefined ) agentInfo . result = metadata . result ;
153+ if ( metadata . error !== undefined ) agentInfo . error = metadata . error ;
91154 }
92155
93156 return true ;
94157 }
95158
96- // Get a specific agent state
159+ // Get a specific agent info
160+ public getAgentInfo ( agentId : string ) : AgentInfo | undefined {
161+ return this . agentInfos . get ( agentId ) ;
162+ }
163+
164+ // Get a specific agent state - for backward compatibility
97165 public getAgentState ( agentId : string ) : AgentState | undefined {
98- return this . agentStates . get ( agentId ) ;
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 ;
99187 }
100188
101- // Get a specific agent tracking info
189+ // Get a specific agent tracking info - for backward compatibility
102190 public getAgent ( agentId : string ) : Agent | undefined {
103- return this . agents . get ( agentId ) ;
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 ;
104206 }
105207
106208 // Get all agents with optional filtering
107209 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+ } ) ) ;
219+
108220 if ( ! status ) {
109- return Array . from ( this . agents . values ( ) ) ;
221+ return agents ;
110222 }
111223
112- return Array . from ( this . agents . values ( ) ) . filter (
113- ( agent ) => agent . status === status ,
114- ) ;
224+ return agents . filter ( ( agent ) => agent . status === status ) ;
115225 }
116226
117227 /**
@@ -122,11 +232,13 @@ export class AgentTracker {
122232 description : string ;
123233 status : AgentStatus ;
124234 } > {
125- return this . getAgents ( AgentStatus . RUNNING ) . map ( ( agent ) => ( {
126- agentId : agent . agentId ,
127- description : agent . goal ,
128- status : agent . status ,
129- } ) ) ;
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+ } ) ) ;
130242 }
131243
132244 // Cleanup and terminate agents
@@ -141,16 +253,16 @@ export class AgentTracker {
141253 // Terminate a specific agent
142254 public async terminateAgent ( agentId : string ) : Promise < void > {
143255 try {
144- const agentState = this . agentStates . get ( agentId ) ;
145- if ( agentState && ! agentState . aborted ) {
256+ const agentInfo = this . agentInfos . get ( agentId ) ;
257+ if ( agentInfo && ! agentInfo . aborted ) {
146258 // Set the agent as aborted and completed
147- agentState . aborted = true ;
148- agentState . completed = true ;
259+ agentInfo . aborted = true ;
260+ agentInfo . completed = true ;
149261
150262 // Clean up resources owned by this sub-agent
151- await agentState . context . agentTracker . cleanup ( ) ;
152- await agentState . context . shellTracker . cleanup ( ) ;
153- await agentState . context . browserTracker . cleanup ( ) ;
263+ await agentInfo . context . agentTracker . cleanup ( ) ;
264+ await agentInfo . context . shellTracker . cleanup ( ) ;
265+ await agentInfo . context . browserTracker . cleanup ( ) ;
154266 }
155267 this . updateAgentStatus ( agentId , AgentStatus . TERMINATED ) ;
156268 } catch ( error ) {
0 commit comments