1
1
import { toArray } from "rxjs/operators" ;
2
- import { v4 as uuidv4 } from "uuid" ;
3
- import { LegacyRuntimeProtocolEvent } from "../../legacy/types" ;
4
2
import { EventType , BaseEvent , RunAgentInput } from "@ag-ui/core" ;
5
3
import { AbstractAgent } from "../../agent/agent" ;
6
4
import { Observable , lastValueFrom } from "rxjs" ;
@@ -86,6 +84,63 @@ class ChunkTestAgent extends AbstractAgent {
86
84
}
87
85
}
88
86
87
+ // Agent that emits tool call events with results
88
+ class ToolCallTestAgent extends AbstractAgent {
89
+ protected run ( input : RunAgentInput ) : Observable < BaseEvent > {
90
+ const toolCallId = "test-tool-call-id" ;
91
+ const toolCallName = "get_weather" ;
92
+ return new Observable < BaseEvent > ( ( observer ) => {
93
+ observer . next ( {
94
+ type : EventType . RUN_STARTED ,
95
+ threadId : input . threadId ,
96
+ runId : input . runId ,
97
+ timestamp : Date . now ( ) ,
98
+ } as BaseEvent ) ;
99
+
100
+ // Start tool call
101
+ observer . next ( {
102
+ type : EventType . TOOL_CALL_START ,
103
+ toolCallId,
104
+ toolCallName,
105
+ timestamp : Date . now ( ) ,
106
+ } as BaseEvent ) ;
107
+
108
+ // Tool call arguments
109
+ observer . next ( {
110
+ type : EventType . TOOL_CALL_ARGS ,
111
+ toolCallId,
112
+ delta : '{"location": "San Francisco"}' ,
113
+ timestamp : Date . now ( ) ,
114
+ } as BaseEvent ) ;
115
+
116
+ // End tool call
117
+ observer . next ( {
118
+ type : EventType . TOOL_CALL_END ,
119
+ toolCallId,
120
+ timestamp : Date . now ( ) ,
121
+ } as BaseEvent ) ;
122
+
123
+ // Tool call result
124
+ observer . next ( {
125
+ messageId : "test-message-id" ,
126
+ type : EventType . TOOL_CALL_RESULT ,
127
+ toolCallId,
128
+ content : "The weather in San Francisco is 72°F and sunny." ,
129
+ timestamp : Date . now ( ) ,
130
+ } as BaseEvent ) ;
131
+
132
+ observer . next ( {
133
+ type : EventType . RUN_FINISHED ,
134
+ threadId : input . threadId ,
135
+ runId : input . runId ,
136
+ timestamp : Date . now ( ) ,
137
+ } as BaseEvent ) ;
138
+
139
+ observer . complete ( ) ;
140
+ } ) ;
141
+ }
142
+ }
143
+
89
144
describe ( "AbstractAgent.legacy_to_be_removed_runAgentBridged" , ( ) => {
90
145
beforeEach ( ( ) => {
91
146
jest . clearAllMocks ( ) ;
@@ -96,7 +151,6 @@ describe("AbstractAgent.legacy_to_be_removed_runAgentBridged", () => {
96
151
const agent = new TestAgent ( {
97
152
threadId : "test-thread-id" ,
98
153
agentId : "test-agent-id" ,
99
- debug : true ,
100
154
} ) ;
101
155
102
156
// Get the observable that emits legacy events
@@ -241,7 +295,6 @@ describe("AbstractAgent.legacy_to_be_removed_runAgentBridged", () => {
241
295
const agent = new ChunkTestAgent ( {
242
296
threadId : "test-thread-id" ,
243
297
agentId : "test-agent-id" ,
244
- debug : true ,
245
298
} ) ;
246
299
247
300
// Get the observable that emits legacy events
@@ -280,4 +333,57 @@ describe("AbstractAgent.legacy_to_be_removed_runAgentBridged", () => {
280
333
active : false ,
281
334
} ) ;
282
335
} ) ;
336
+
337
+ it ( "should transform tool call events with results into legacy events with correct tool name" , async ( ) => {
338
+ // Setup agent with mock IDs
339
+ const agent = new ToolCallTestAgent ( {
340
+ threadId : "test-thread-id" ,
341
+ agentId : "test-agent-id" ,
342
+ } ) ;
343
+
344
+ // Get the observable that emits legacy events
345
+ const legacy$ = agent . legacy_to_be_removed_runAgentBridged ( ) ;
346
+
347
+ // Collect all emitted events
348
+ const legacyEvents = await lastValueFrom ( legacy$ . pipe ( toArray ( ) ) ) ;
349
+
350
+ // Verify events are in correct legacy format
351
+ expect ( legacyEvents ) . toHaveLength ( 5 ) ; // ActionExecutionStart, ActionExecutionArgs, ActionExecutionEnd, ActionExecutionResult, AgentStateMessage
352
+
353
+ // ActionExecutionStart
354
+ expect ( legacyEvents [ 0 ] ) . toMatchObject ( {
355
+ type : "ActionExecutionStart" ,
356
+ actionExecutionId : "test-tool-call-id" ,
357
+ actionName : "get_weather" ,
358
+ } ) ;
359
+
360
+ // ActionExecutionArgs
361
+ expect ( legacyEvents [ 1 ] ) . toMatchObject ( {
362
+ type : "ActionExecutionArgs" ,
363
+ actionExecutionId : "test-tool-call-id" ,
364
+ args : '{"location": "San Francisco"}' ,
365
+ } ) ;
366
+
367
+ // ActionExecutionEnd
368
+ expect ( legacyEvents [ 2 ] ) . toMatchObject ( {
369
+ type : "ActionExecutionEnd" ,
370
+ actionExecutionId : "test-tool-call-id" ,
371
+ } ) ;
372
+
373
+ // ActionExecutionResult - this should include the tool name
374
+ expect ( legacyEvents [ 3 ] ) . toMatchObject ( {
375
+ type : "ActionExecutionResult" ,
376
+ actionExecutionId : "test-tool-call-id" ,
377
+ actionName : "get_weather" , // This verifies the tool name is correctly included
378
+ result : "The weather in San Francisco is 72°F and sunny." ,
379
+ } ) ;
380
+
381
+ // Final AgentStateMessage
382
+ expect ( legacyEvents [ 4 ] ) . toMatchObject ( {
383
+ type : "AgentStateMessage" ,
384
+ threadId : "test-thread-id" ,
385
+ agentName : "test-agent-id" ,
386
+ active : false ,
387
+ } ) ;
388
+ } ) ;
283
389
} ) ;
0 commit comments