@@ -538,6 +538,74 @@ describe("VertexHandler", () => {
538538 expect ( usageChunks [ 0 ] ) . toHaveProperty ( "cacheWriteTokens" , 5 )
539539 expect ( usageChunks [ 0 ] ) . toHaveProperty ( "cacheReadTokens" , 3 )
540540 } )
541+
542+ describe ( "abort signal" , ( ) => {
543+ const systemPrompt = "You are a helpful assistant"
544+ const mockMessages : Anthropic . Messages . MessageParam [ ] = [
545+ {
546+ role : "user" ,
547+ content : "Hello" ,
548+ } ,
549+ ]
550+
551+ it ( "should pass abort signal through to client in createMessage" , async ( ) => {
552+ handler = new AnthropicVertexHandler ( {
553+ apiModelId : "claude-3-5-sonnet-v2@20241022" ,
554+ vertexProjectId : "test-project" ,
555+ vertexRegion : "us-central1" ,
556+ } )
557+
558+ const controller = new AbortController ( )
559+
560+ const mockCreate = vitest . fn ( ) . mockImplementation ( async ( ) => ( {
561+ async * [ Symbol . asyncIterator ] ( ) {
562+ yield { type : "message_start" , message : { usage : { input_tokens : 10 , output_tokens : 5 } } }
563+ } ,
564+ } ) )
565+ ; ( handler [ "client" ] . messages as any ) . create = mockCreate
566+
567+ const stream = handler . createMessage ( systemPrompt , mockMessages , {
568+ taskId : "test-task" ,
569+ abortSignal : controller . signal as any ,
570+ } )
571+ for await ( const _ of stream ) {
572+ // consume stream
573+ }
574+
575+ expect ( mockCreate ) . toHaveBeenCalledWith (
576+ expect . any ( Object ) ,
577+ expect . objectContaining ( { signal : controller . signal } ) ,
578+ )
579+ } )
580+
581+ it ( "should pass the exact same signal reference (reference identity)" , async ( ) => {
582+ handler = new AnthropicVertexHandler ( {
583+ apiModelId : "claude-3-5-sonnet-v2@20241022" ,
584+ vertexProjectId : "test-project" ,
585+ vertexRegion : "us-central1" ,
586+ } )
587+
588+ const controller = new AbortController ( )
589+
590+ const mockCreate = vitest . fn ( ) . mockImplementation ( async ( ) => ( {
591+ async * [ Symbol . asyncIterator ] ( ) {
592+ yield { type : "message_start" , message : { usage : { input_tokens : 10 , output_tokens : 5 } } }
593+ } ,
594+ } ) )
595+ ; ( handler [ "client" ] . messages as any ) . create = mockCreate
596+
597+ const stream = handler . createMessage ( systemPrompt , mockMessages , {
598+ taskId : "test-task" ,
599+ abortSignal : controller . signal as any ,
600+ } )
601+ for await ( const _ of stream ) {
602+ // consume stream
603+ }
604+
605+ const callOptions = mockCreate . mock . calls [ 0 ] [ 1 ]
606+ expect ( callOptions ?. signal ) . toBe ( controller . signal )
607+ } )
608+ } )
541609 } )
542610
543611 describe ( "thinking functionality" , ( ) => {
0 commit comments