@@ -295,5 +295,143 @@ describe("ZAiHandler", () => {
295295 undefined ,
296296 )
297297 } )
298+
299+ describe ( "Reasoning functionality" , ( ) => {
300+ it ( "should include thinking parameter when enableReasoningEffort is true and model supports reasoning in createMessage" , async ( ) => {
301+ const handlerWithReasoning = new ZAiHandler ( {
302+ apiModelId : "glm-4.6" , // GLM-4.6 has supportsReasoning: true
303+ zaiApiKey : "test-zai-api-key" ,
304+ zaiApiLine : "international_coding" ,
305+ enableReasoningEffort : true ,
306+ } )
307+
308+ mockCreate . mockImplementationOnce ( ( ) => {
309+ return {
310+ [ Symbol . asyncIterator ] : ( ) => ( {
311+ async next ( ) {
312+ return { done : true }
313+ } ,
314+ } ) ,
315+ }
316+ } )
317+
318+ const systemPrompt = "Test system prompt"
319+ const messages : Anthropic . Messages . MessageParam [ ] = [ { role : "user" , content : "Test message" } ]
320+
321+ const messageGenerator = handlerWithReasoning . createMessage ( systemPrompt , messages )
322+ await messageGenerator . next ( )
323+
324+ expect ( mockCreate ) . toHaveBeenCalledWith (
325+ expect . objectContaining ( {
326+ thinking : { type : "enabled" } ,
327+ } ) ,
328+ undefined ,
329+ )
330+ } )
331+
332+ it ( "should not include thinking parameter when enableReasoningEffort is false in createMessage" , async ( ) => {
333+ const handlerWithoutReasoning = new ZAiHandler ( {
334+ apiModelId : "glm-4.6" , // GLM-4.6 has supportsReasoning: true
335+ zaiApiKey : "test-zai-api-key" ,
336+ zaiApiLine : "international_coding" ,
337+ enableReasoningEffort : false ,
338+ } )
339+
340+ mockCreate . mockImplementationOnce ( ( ) => {
341+ return {
342+ [ Symbol . asyncIterator ] : ( ) => ( {
343+ async next ( ) {
344+ return { done : true }
345+ } ,
346+ } ) ,
347+ }
348+ } )
349+
350+ const systemPrompt = "Test system prompt"
351+ const messages : Anthropic . Messages . MessageParam [ ] = [ { role : "user" , content : "Test message" } ]
352+
353+ const messageGenerator = handlerWithoutReasoning . createMessage ( systemPrompt , messages )
354+ await messageGenerator . next ( )
355+
356+ expect ( mockCreate ) . toHaveBeenCalledWith (
357+ expect . not . objectContaining ( {
358+ thinking : expect . anything ( ) ,
359+ } ) ,
360+ undefined ,
361+ )
362+ } )
363+
364+ it ( "should not include thinking parameter when model does not support reasoning in createMessage" , async ( ) => {
365+ const handlerWithNonReasoningModel = new ZAiHandler ( {
366+ apiModelId : "glm-4-32b-0414-128k" , // This model doesn't have supportsReasoning: true
367+ zaiApiKey : "test-zai-api-key" ,
368+ zaiApiLine : "international_coding" ,
369+ enableReasoningEffort : true ,
370+ } )
371+
372+ mockCreate . mockImplementationOnce ( ( ) => {
373+ return {
374+ [ Symbol . asyncIterator ] : ( ) => ( {
375+ async next ( ) {
376+ return { done : true }
377+ } ,
378+ } ) ,
379+ }
380+ } )
381+
382+ const systemPrompt = "Test system prompt"
383+ const messages : Anthropic . Messages . MessageParam [ ] = [ { role : "user" , content : "Test message" } ]
384+
385+ const messageGenerator = handlerWithNonReasoningModel . createMessage ( systemPrompt , messages )
386+ await messageGenerator . next ( )
387+
388+ expect ( mockCreate ) . toHaveBeenCalledWith (
389+ expect . not . objectContaining ( {
390+ thinking : expect . anything ( ) ,
391+ } ) ,
392+ undefined ,
393+ )
394+ } )
395+
396+ it ( "should include thinking parameter when enableReasoningEffort is true and model supports reasoning in completePrompt" , async ( ) => {
397+ const handlerWithReasoning = new ZAiHandler ( {
398+ apiModelId : "glm-4.5" , // GLM-4.5 has supportsReasoning: true
399+ zaiApiKey : "test-zai-api-key" ,
400+ zaiApiLine : "international_coding" ,
401+ enableReasoningEffort : true ,
402+ } )
403+
404+ const expectedResponse = "This is a test response"
405+ mockCreate . mockResolvedValueOnce ( { choices : [ { message : { content : expectedResponse } } ] } )
406+
407+ await handlerWithReasoning . completePrompt ( "test prompt" )
408+
409+ expect ( mockCreate ) . toHaveBeenCalledWith (
410+ expect . objectContaining ( {
411+ thinking : { type : "enabled" } ,
412+ } ) ,
413+ )
414+ } )
415+
416+ it ( "should not include thinking parameter when enableReasoningEffort is false in completePrompt" , async ( ) => {
417+ const handlerWithoutReasoning = new ZAiHandler ( {
418+ apiModelId : "glm-4.5" , // GLM-4.5 has supportsReasoning: true
419+ zaiApiKey : "test-zai-api-key" ,
420+ zaiApiLine : "international_coding" ,
421+ enableReasoningEffort : false ,
422+ } )
423+
424+ const expectedResponse = "This is a test response"
425+ mockCreate . mockResolvedValueOnce ( { choices : [ { message : { content : expectedResponse } } ] } )
426+
427+ await handlerWithoutReasoning . completePrompt ( "test prompt" )
428+
429+ expect ( mockCreate ) . toHaveBeenCalledWith (
430+ expect . not . objectContaining ( {
431+ thinking : expect . anything ( ) ,
432+ } ) ,
433+ )
434+ } )
435+ } )
298436 } )
299437} )
0 commit comments