@@ -35,6 +35,7 @@ function installZAiFetchInterceptor(
3535 passthrough ?: boolean ,
3636) : ( ) => void {
3737 const original = globalThis . fetch
38+ const capturedRequests : Array < { maxTokens ?: number } > = [ ]
3839
3940 globalThis . fetch = async function ( input : RequestInfo | URL , init ?: RequestInit ) : Promise < Response > {
4041 const url = typeof input === "string" ? input : input instanceof URL ? input . href : ( input as Request ) . url
@@ -47,6 +48,8 @@ function installZAiFetchInterceptor(
4748 } )
4849 : { }
4950
51+ capturedRequests . push ( { maxTokens : body . max_tokens } )
52+
5053 if ( capture ) {
5154 capture . maxTokens = body . max_tokens
5255 }
@@ -73,6 +76,9 @@ function installZAiFetchInterceptor(
7376
7477 return ( ) => {
7578 globalThis . fetch = original
79+ if ( capture && capturedRequests . length > 0 ) {
80+ capture . maxTokens = capturedRequests [ capturedRequests . length - 1 ] . maxTokens
81+ }
7682 }
7783}
7884
@@ -219,6 +225,8 @@ suite("Z.ai GLM provider", function () {
219225 } )
220226
221227 await waitUntilCompleted ( { api, taskId } )
228+ // Allow any pending async requests to finish before snapshotting max_tokens
229+ await new Promise ( ( resolve ) => setTimeout ( resolve , 100 ) )
222230 const capturedMaxTokens = requestCapture . maxTokens
223231
224232 const completionMessage = messages . find (
@@ -229,8 +237,6 @@ suite("Z.ai GLM provider", function () {
229237
230238 // Verify max_tokens uses the restored default clamp (20% of context window)
231239 // unless the user explicitly overrides it via modelMaxTokens.
232- // Snapshot immediately after waitUntilCompleted to avoid straggling async calls
233- // from this task overwriting requestCapture before the assertion runs.
234240 assert . strictEqual (
235241 capturedMaxTokens ,
236242 40_000 ,
@@ -263,6 +269,8 @@ suite("Z.ai GLM provider", function () {
263269 } )
264270
265271 await waitUntilCompleted ( { api, taskId } )
272+ // Allow any pending async requests to finish before snapshotting max_tokens
273+ await new Promise ( ( resolve ) => setTimeout ( resolve , 100 ) )
266274 const capturedMaxTokens = requestCapture . maxTokens
267275
268276 const completionMessage = messages . find (
@@ -273,8 +281,6 @@ suite("Z.ai GLM provider", function () {
273281
274282 // Verify max_tokens uses the restored default clamp (20% of context window)
275283 // unless the user explicitly overrides it via modelMaxTokens.
276- // Snapshot immediately after waitUntilCompleted to avoid straggling async calls
277- // from the prior test overwriting requestCapture before this assertion runs.
278284 assert . strictEqual (
279285 capturedMaxTokens ,
280286 40_000 ,
0 commit comments