@@ -15,29 +15,9 @@ export class ExecutionAgent extends Agent<WorkerEnv, ExecutionState> {
1515 } ;
1616
1717 /**
18- * Execute a task using an agentic loop with tools
18+ * Execute task in background and ping interaction worker when done
1919 */
20- @callable ( )
21- async executeTask (
22- input : TaskInput ,
23- ) : Promise < { success : boolean ; result ?: unknown ; error ?: string } > {
24- // Check if already executing to prevent concurrent runs
25- if ( this . state . isExecuting ) {
26- logger
27- . withTags ( {
28- agentId : input . agentId ,
29- conversationId : input . conversationId ,
30- } )
31- . warn ( "ExecutionAgent: Attempted concurrent execution" , {
32- taskDescription : input . taskDescription . substring ( 0 , 100 ) ,
33- } ) ;
34-
35- return {
36- success : false ,
37- error : "Agent is already executing a task" ,
38- } ;
39- }
40-
20+ private async executeTaskInBackground ( input : TaskInput ) : Promise < void > {
4121 this . setState ( { isExecuting : true } ) ;
4222
4323 logger
@@ -204,7 +184,40 @@ ${input.taskDescription}`,
204184 } ) ;
205185
206186 this . setState ( { isExecuting : false } ) ;
207- return { success : true , result : finalResult } ;
187+
188+ // Ping interaction worker with completion via RPC
189+ try {
190+ logger
191+ . withTags ( {
192+ agentId : input . agentId ,
193+ conversationId : input . conversationId ,
194+ } )
195+ . info ( "ExecutionAgent: Pinging interaction worker with completion" ) ;
196+
197+ await this . env . INTERACTION_WORKER . handleAgentCompletion ( {
198+ agentId : input . agentId ,
199+ conversationId : input . conversationId ,
200+ success : true ,
201+ result : result . text ,
202+ } ) ;
203+
204+ logger
205+ . withTags ( {
206+ agentId : input . agentId ,
207+ conversationId : input . conversationId ,
208+ } )
209+ . info ( "ExecutionAgent: Successfully pinged interaction worker" ) ;
210+ } catch ( rpcError ) {
211+ logger
212+ . withTags ( {
213+ agentId : input . agentId ,
214+ conversationId : input . conversationId ,
215+ } )
216+ . error ( "ExecutionAgent: Failed to ping interaction worker" , {
217+ error :
218+ rpcError instanceof Error ? rpcError . message : String ( rpcError ) ,
219+ } ) ;
220+ }
208221 } catch ( error ) {
209222 const errorMessage =
210223 error instanceof Error ? error . message : String ( error ) ;
@@ -251,7 +264,89 @@ ${input.taskDescription}`,
251264 }
252265
253266 this . setState ( { isExecuting : false } ) ;
254- return { success : false , error : errorMessage } ;
267+
268+ // Ping interaction worker with error via RPC
269+ try {
270+ logger
271+ . withTags ( {
272+ agentId : input . agentId ,
273+ conversationId : input . conversationId ,
274+ } )
275+ . info ( "ExecutionAgent: Pinging interaction worker with error" ) ;
276+
277+ await this . env . INTERACTION_WORKER . handleAgentCompletion ( {
278+ agentId : input . agentId ,
279+ conversationId : input . conversationId ,
280+ success : false ,
281+ error : errorMessage ,
282+ } ) ;
283+
284+ logger
285+ . withTags ( {
286+ agentId : input . agentId ,
287+ conversationId : input . conversationId ,
288+ } )
289+ . info (
290+ "ExecutionAgent: Successfully pinged interaction worker with error" ,
291+ ) ;
292+ } catch ( rpcError ) {
293+ logger
294+ . withTags ( {
295+ agentId : input . agentId ,
296+ conversationId : input . conversationId ,
297+ } )
298+ . error (
299+ "ExecutionAgent: Failed to ping interaction worker with error" ,
300+ {
301+ error :
302+ rpcError instanceof Error ? rpcError . message : String ( rpcError ) ,
303+ } ,
304+ ) ;
305+ }
255306 }
256307 }
308+
309+ /**
310+ * Execute a task using an agentic loop with tools
311+ * Returns immediately and runs task in background
312+ */
313+ @callable ( )
314+ async executeTask (
315+ input : TaskInput ,
316+ ) : Promise < { success : boolean ; message : string } > {
317+ // Check if already executing to prevent concurrent runs
318+ if ( this . state . isExecuting ) {
319+ logger
320+ . withTags ( {
321+ agentId : input . agentId ,
322+ conversationId : input . conversationId ,
323+ } )
324+ . warn ( "ExecutionAgent: Attempted concurrent execution" , {
325+ taskDescription : input . taskDescription . substring ( 0 , 100 ) ,
326+ } ) ;
327+
328+ return {
329+ success : false ,
330+ message : "Agent is already executing a task" ,
331+ } ;
332+ }
333+
334+ logger
335+ . withTags ( {
336+ agentId : input . agentId ,
337+ conversationId : input . conversationId ,
338+ } )
339+ . info ( "ExecutionAgent: Starting task execution in background" , {
340+ taskDescription : input . taskDescription . substring ( 0 , 200 ) ,
341+ taskLength : input . taskDescription . length ,
342+ } ) ;
343+
344+ // Start execution in background
345+ this . ctx . waitUntil ( this . executeTaskInBackground ( input ) ) ;
346+
347+ return {
348+ success : true ,
349+ message : "Task execution started in background" ,
350+ } ;
351+ }
257352}
0 commit comments