11import { describe , it , expect , vi , beforeEach , afterEach } from "vitest" ;
22import { z } from "zod" ;
33import { BasicAgent , defineTool , type ToolDefinition } from "../index" ;
4- import { EventType , type RunAgentInput } from "@ag-ui/client" ;
4+ import {
5+ EventType ,
6+ type BaseEvent ,
7+ type ReasoningStartEvent ,
8+ type RunAgentInput ,
9+ } from "@ag-ui/client" ;
510import { streamText } from "ai" ;
611import {
712 mockStreamTextResponse ,
@@ -1075,7 +1080,7 @@ describe("BasicAgent", () => {
10751080 reasoningDelta ( "Deep thought" ) ,
10761081 reasoningEnd ( ) ,
10771082 finish ( ) ,
1078- ] ) as any ,
1083+ ] ) ,
10791084 ) ;
10801085
10811086 const input : RunAgentInput = {
@@ -1116,7 +1121,7 @@ describe("BasicAgent", () => {
11161121 reasoningDelta ( "" ) ,
11171122 reasoningEnd ( ) ,
11181123 finish ( ) ,
1119- ] ) as any ,
1124+ ] ) ,
11201125 ) ;
11211126
11221127 const input : RunAgentInput = {
@@ -1132,12 +1137,12 @@ describe("BasicAgent", () => {
11321137
11331138 // No REASONING_MESSAGE_CONTENT events — empty delta skipped
11341139 const contentEvents = events . filter (
1135- ( e : any ) => e . type === EventType . REASONING_MESSAGE_CONTENT ,
1140+ ( e ) => e . type === EventType . REASONING_MESSAGE_CONTENT ,
11361141 ) ;
11371142 expect ( contentEvents ) . toHaveLength ( 0 ) ;
11381143
11391144 // Stream still completes with RUN_FINISHED
1140- const eventTypes = events . map ( ( e : any ) => e . type ) ;
1145+ const eventTypes = events . map ( ( e ) => e . type ) ;
11411146 expect ( eventTypes [ eventTypes . length - 1 ] ) . toBe ( EventType . RUN_FINISHED ) ;
11421147 } ) ;
11431148
@@ -1156,7 +1161,7 @@ describe("BasicAgent", () => {
11561161 toolCall ( "call1" , "testTool" , { arg : "val" } ) ,
11571162 toolResult ( "call1" , "testTool" , { result : "success" } ) ,
11581163 finish ( ) ,
1159- ] ) as any ,
1164+ ] ) ,
11601165 ) ;
11611166
11621167 const input : RunAgentInput = {
@@ -1169,7 +1174,7 @@ describe("BasicAgent", () => {
11691174 } ;
11701175
11711176 const events = await collectEvents ( agent [ "run" ] ( input ) ) ;
1172- const eventTypes = events . map ( ( e : any ) => e . type ) ;
1177+ const eventTypes = events . map ( ( e ) => e . type ) ;
11731178
11741179 // REASONING_MESSAGE_END must appear before REASONING_END, which must appear before TOOL_CALL_START
11751180 const reasoningMsgEndIdx = eventTypes . indexOf (
@@ -1206,7 +1211,7 @@ describe("BasicAgent", () => {
12061211 textStart ( ) ,
12071212 textDelta ( "Answer" ) ,
12081213 finish ( ) ,
1209- ] ) as any ,
1214+ ] ) ,
12101215 ) ;
12111216
12121217 const input : RunAgentInput = {
@@ -1219,7 +1224,7 @@ describe("BasicAgent", () => {
12191224 } ;
12201225
12211226 const events = await collectEvents ( agent [ "run" ] ( input ) ) ;
1222- const eventTypes = events . map ( ( e : any ) => e . type ) ;
1227+ const eventTypes = events . map ( ( e ) => e . type ) ;
12231228
12241229 // REASONING_MESSAGE_END must appear before REASONING_END, which must appear before TEXT_MESSAGE_CHUNK
12251230 const reasoningMsgEndIdx = eventTypes . indexOf (
@@ -1254,7 +1259,7 @@ describe("BasicAgent", () => {
12541259 reasoningDelta ( "Deep thought" ) ,
12551260 // NO reasoningEnd() — simulates @ai-sdk/anthropic behaviour
12561261 finish ( ) ,
1257- ] ) as any ,
1262+ ] ) ,
12581263 ) ;
12591264
12601265 const input : RunAgentInput = {
@@ -1267,7 +1272,7 @@ describe("BasicAgent", () => {
12671272 } ;
12681273
12691274 const events = await collectEvents ( agent [ "run" ] ( input ) ) ;
1270- const eventTypes = events . map ( ( e : any ) => e . type ) ;
1275+ const eventTypes = events . map ( ( e ) => e . type ) ;
12711276
12721277 // REASONING_MESSAGE_END must appear before REASONING_END (auto-closed by finish case)
12731278 const reasoningMsgEndIdx = eventTypes . indexOf (
@@ -1300,7 +1305,7 @@ describe("BasicAgent", () => {
13001305 reasoningDelta ( "Thinking..." ) ,
13011306 // NO reasoningEnd() — stream aborts before SDK can close reasoning
13021307 abort ( ) ,
1303- ] ) as any ,
1308+ ] ) ,
13041309 ) ;
13051310
13061311 const input : RunAgentInput = {
@@ -1313,7 +1318,7 @@ describe("BasicAgent", () => {
13131318 } ;
13141319
13151320 const events = await collectEvents ( agent [ "run" ] ( input ) ) ;
1316- const eventTypes = events . map ( ( e : any ) => e . type ) ;
1321+ const eventTypes = events . map ( ( e ) => e . type ) ;
13171322
13181323 // REASONING_MESSAGE_END must appear before REASONING_END, both before RUN_FINISHED
13191324 const reasoningMsgEndIdx = eventTypes . indexOf (
@@ -1348,7 +1353,7 @@ describe("BasicAgent", () => {
13481353 reasoningDelta ( "Thinking..." ) ,
13491354 // NO reasoningEnd() — stream errors before SDK can close reasoning
13501355 error ( "stream failed" ) ,
1351- ] ) as any ,
1356+ ] ) ,
13521357 ) ;
13531358
13541359 const input : RunAgentInput = {
@@ -1361,16 +1366,16 @@ describe("BasicAgent", () => {
13611366 } ;
13621367
13631368 // subscriber.error() causes collectEvents to reject, so collect manually
1364- const events : any [ ] = [ ] ;
1369+ const events : BaseEvent [ ] = [ ] ;
13651370 await new Promise < void > ( ( resolve ) => {
13661371 agent [ "run" ] ( input ) . subscribe ( {
1367- next : ( e : any ) => events . push ( e ) ,
1372+ next : ( e ) => events . push ( e ) ,
13681373 error : ( ) => resolve ( ) , // error is expected
13691374 complete : ( ) => resolve ( ) ,
13701375 } ) ;
13711376 } ) ;
13721377
1373- const eventTypes = events . map ( ( e : any ) => e . type ) ;
1378+ const eventTypes = events . map ( ( e ) => e . type ) ;
13741379
13751380 // REASONING_MESSAGE_END must appear before REASONING_END, both before RUN_ERROR
13761381 const reasoningMsgEndIdx = eventTypes . indexOf (
@@ -1405,7 +1410,7 @@ describe("BasicAgent", () => {
14051410 reasoningDelta ( "Second thought" ) ,
14061411 reasoningEnd ( ) ,
14071412 finish ( ) ,
1408- ] ) as any ,
1413+ ] ) ,
14091414 ) ;
14101415
14111416 const input : RunAgentInput = {
@@ -1418,7 +1423,7 @@ describe("BasicAgent", () => {
14181423 } ;
14191424
14201425 const events = await collectEvents ( agent [ "run" ] ( input ) ) ;
1421- const eventTypes = events . map ( ( e : any ) => e . type ) ;
1426+ const eventTypes = events . map ( ( e ) => e . type ) ;
14221427
14231428 // Both reasoning blocks must be properly closed — two complete lifecycles
14241429 expect (
@@ -1437,12 +1442,10 @@ describe("BasicAgent", () => {
14371442
14381443 // The two blocks must use distinct messageIds
14391444 const startEvents = events . filter (
1440- ( e : any ) => e . type === EventType . REASONING_START ,
1445+ ( e ) : e is ReasoningStartEvent => e . type === EventType . REASONING_START ,
14411446 ) ;
14421447 expect ( startEvents ) . toHaveLength ( 2 ) ;
1443- expect ( ( startEvents [ 0 ] as any ) . messageId ) . not . toBe (
1444- ( startEvents [ 1 ] as any ) . messageId ,
1445- ) ;
1448+ expect ( startEvents [ 0 ] . messageId ) . not . toBe ( startEvents [ 1 ] . messageId ) ;
14461449
14471450 expect ( eventTypes [ eventTypes . length - 1 ] ) . toBe ( EventType . RUN_FINISHED ) ;
14481451 } ) ;
@@ -1460,7 +1463,9 @@ describe("BasicAgent", () => {
14601463 throw new Error ( "unexpected network failure" ) ;
14611464 } ) ( ) ,
14621465 } ;
1463- vi . mocked ( streamText ) . mockReturnValue ( throwingStream as any ) ;
1466+ vi . mocked ( streamText ) . mockReturnValue (
1467+ throwingStream as unknown as ReturnType < typeof streamText > ,
1468+ ) ;
14641469
14651470 const input : RunAgentInput = {
14661471 threadId : "thread1" ,
@@ -1472,16 +1477,16 @@ describe("BasicAgent", () => {
14721477 } ;
14731478
14741479 // subscriber.error() causes collectEvents to reject, so collect manually
1475- const events : any [ ] = [ ] ;
1480+ const events : BaseEvent [ ] = [ ] ;
14761481 await new Promise < void > ( ( resolve ) => {
14771482 agent [ "run" ] ( input ) . subscribe ( {
1478- next : ( e : any ) => events . push ( e ) ,
1483+ next : ( e ) => events . push ( e ) ,
14791484 error : ( ) => resolve ( ) , // error is expected
14801485 complete : ( ) => resolve ( ) ,
14811486 } ) ;
14821487 } ) ;
14831488
1484- const eventTypes = events . map ( ( e : any ) => e . type ) ;
1489+ const eventTypes = events . map ( ( e ) => e . type ) ;
14851490
14861491 // Reasoning must be closed before RUN_ERROR despite the exception path
14871492 const reasoningMsgEndIdx = eventTypes . indexOf (
@@ -1512,7 +1517,7 @@ describe("BasicAgent", () => {
15121517 reasoningStart ( ) ,
15131518 reasoningDelta ( "Thinking..." ) ,
15141519 // deliberate: no finish(), no abort(), no error()
1515- ] ) as any ,
1520+ ] ) ,
15161521 ) ;
15171522
15181523 const input : RunAgentInput = {
@@ -1525,7 +1530,7 @@ describe("BasicAgent", () => {
15251530 } ;
15261531
15271532 const events = await collectEvents ( agent [ "run" ] ( input ) ) ;
1528- const eventTypes = events . map ( ( e : any ) => e . type ) ;
1533+ const eventTypes = events . map ( ( e ) => e . type ) ;
15291534
15301535 // Reasoning must be closed before RUN_FINISHED via fallback
15311536 const reasoningMsgEndIdx = eventTypes . indexOf (
@@ -1575,7 +1580,7 @@ describe("BasicAgent", () => {
15751580 } ;
15761581
15771582 const events = await collectEvents ( agent [ "run" ] ( input ) ) ;
1578- const eventTypes = events . map ( ( e : any ) => e . type ) ;
1583+ const eventTypes = events . map ( ( e ) => e . type ) ;
15791584
15801585 // Reasoning events precede tool call events
15811586 const reasoningEndIdx = eventTypes . indexOf ( EventType . REASONING_END ) ;
0 commit comments