@@ -18,6 +18,55 @@ function makeDirectAi(
1818}
1919
2020describe ( 'runtime ai' , ( ) => {
21+ it . each ( [ 'direct' , 'http' , 'service' ] as const ) (
22+ 'cancels provider response streams on the %s transport without serializing the signal' ,
23+ async ( transport ) => {
24+ vi . resetModules ( ) ;
25+ const { envContext } = await import ( '../../src/runtime/env/raw.ts' ) ;
26+ const { ai } = await import ( '../../src/runtime/ai/index.ts' ) ;
27+ const controller = new AbortController ( ) ;
28+ const request = vi . fn (
29+ async ( _url : string , init : RequestInit ) =>
30+ new Response (
31+ new ReadableStream ( {
32+ start ( stream ) {
33+ init . signal ?. addEventListener ( 'abort' , ( ) => stream . error ( init . signal ! . reason ) ) ;
34+ } ,
35+ } ) ,
36+ ) ,
37+ ) ;
38+ vi . stubGlobal ( 'fetch' , request ) ;
39+ const env =
40+ transport === 'direct'
41+ ? { AI : makeDirectAi ( ) . AI , __VOID_AI_GATEWAY : 'gateway' }
42+ : transport === 'service'
43+ ? {
44+ __VOID_PROXY : { fetch : request } ,
45+ __PROJECT_ID : 'project' ,
46+ __VOID_PROXY_TOKEN : 'token' ,
47+ }
48+ : {
49+ __VOID_TOKEN : 'token' ,
50+ __VOID_PROJECT_ID : 'project' ,
51+ __VOID_PROXY_URL : 'https://proxy.test' ,
52+ } ;
53+ await envContext . run ( { ...env , OPENAI_API_KEY : 'synthetic-provider-key' } , async ( ) => {
54+ const response = await ai . provider ( 'openai' ) . fetch ( '/chat/completions' , {
55+ body : { messages : [ ] } ,
56+ signal : controller . signal ,
57+ } ) ;
58+ const init = request . mock . calls [ 0 ] ! [ 1 ] ;
59+ expect ( init . signal ) . toBe ( controller . signal ) ;
60+ const payload = JSON . parse ( init . body as string ) ;
61+ expect ( payload ) . not . toHaveProperty ( 'signal' ) ;
62+ expect ( payload . options ) . toBeUndefined ( ) ;
63+ const reading = response . text ( ) ;
64+ controller . abort ( new Error ( 'provider request cancelled' ) ) ;
65+ await expect ( reading ) . rejects . toThrow ( 'provider request cancelled' ) ;
66+ } ) ;
67+ } ,
68+ ) ;
69+
2170 it . each ( [ 'http' , 'service' ] as const ) (
2271 'keeps cancellation on the %s transport instead of serializing it as a binding option' ,
2372 async ( transport ) => {
0 commit comments