@@ -1294,6 +1294,9 @@ describe("openai transport stream", () => {
12941294 cost : { input : 0 , output : 0 , cacheRead : 0 , cacheWrite : 0 } ,
12951295 contextWindow : 256_000 ,
12961296 maxTokens : 16_384 ,
1297+ compat : {
1298+ supportsReasoningEffort : true ,
1299+ } ,
12971300 } satisfies Model < "openai-completions" > ;
12981301 const stream = createOpenAICompletionsTransportStreamFn ( ) (
12991302 model ,
@@ -1302,7 +1305,7 @@ describe("openai transport stream", () => {
13021305 messages : [ { role : "user" , content : "Reply live-ok" , timestamp : Date . now ( ) } ] ,
13031306 tools : [ ] ,
13041307 } as never ,
1305- { apiKey : "test-key" } as never ,
1308+ { apiKey : "test-key" , reasoningEffort : "high" } as never ,
13061309 ) ;
13071310
13081311 let doneReason : string | undefined ;
@@ -1335,6 +1338,140 @@ describe("openai transport stream", () => {
13351338 }
13361339 } ) ;
13371340
1341+ it ( "emits Qwen thinking streams when enabled without reasoning_effort support" , async ( ) => {
1342+ let capturedPayload : Record < string , unknown > | undefined ;
1343+ const server = createServer ( ( req , res ) => {
1344+ let body = "" ;
1345+ req . setEncoding ( "utf8" ) ;
1346+ req . on ( "data" , ( chunk ) => {
1347+ body += chunk ;
1348+ } ) ;
1349+ req . on ( "end" , ( ) => {
1350+ capturedPayload = JSON . parse ( body ) as Record < string , unknown > ;
1351+ res . writeHead ( 200 , {
1352+ "content-type" : "application/json; charset=utf-8" ,
1353+ } ) ;
1354+ res . end (
1355+ JSON . stringify ( {
1356+ id : "chatcmpl-qwen-thinking" ,
1357+ object : "chat.completion" ,
1358+ model : "qwen3.5-32b" ,
1359+ choices : [
1360+ {
1361+ index : 0 ,
1362+ message : {
1363+ role : "assistant" ,
1364+ reasoning_content : "Need a Qwen answer." ,
1365+ content : "qwen-ok" ,
1366+ } ,
1367+ finish_reason : "stop" ,
1368+ } ,
1369+ ] ,
1370+ } ) ,
1371+ ) ;
1372+ } ) ;
1373+ } ) ;
1374+
1375+ await new Promise < void > ( ( resolve ) => {
1376+ server . listen ( 0 , "127.0.0.1" , resolve ) ;
1377+ } ) ;
1378+ try {
1379+ const address = server . address ( ) ;
1380+ if ( ! address || typeof address === "string" ) {
1381+ throw new Error ( "Missing loopback server address" ) ;
1382+ }
1383+ const model = {
1384+ id : "qwen3.5-32b" ,
1385+ name : "Qwen 3.5 32B" ,
1386+ api : "openai-completions" ,
1387+ provider : "qwen" ,
1388+ baseUrl : `http://127.0.0.1:${ address . port } /v1` ,
1389+ reasoning : true ,
1390+ input : [ "text" ] ,
1391+ cost : { input : 0 , output : 0 , cacheRead : 0 , cacheWrite : 0 } ,
1392+ contextWindow : 131072 ,
1393+ maxTokens : 8192 ,
1394+ compat : {
1395+ thinkingFormat : "qwen" ,
1396+ supportsReasoningEffort : false ,
1397+ } ,
1398+ } satisfies Model < "openai-completions" > ;
1399+ const stream = createOpenAICompletionsTransportStreamFn ( ) (
1400+ model ,
1401+ {
1402+ systemPrompt : "system" ,
1403+ messages : [ { role : "user" , content : "Reply qwen-ok" , timestamp : Date . now ( ) } ] ,
1404+ tools : [ ] ,
1405+ } as never ,
1406+ { apiKey : "test-key" , reasoning : "medium" } as never ,
1407+ ) ;
1408+
1409+ let thinking = "" ;
1410+ let text = "" ;
1411+ for await ( const event of stream as AsyncIterable < { type : string ; delta ?: string } > ) {
1412+ if ( event . type === "thinking_delta" ) {
1413+ thinking += event . delta ?? "" ;
1414+ }
1415+ if ( event . type === "text_delta" ) {
1416+ text += event . delta ?? "" ;
1417+ }
1418+ }
1419+
1420+ expect ( capturedPayload ?. enable_thinking ) . toBe ( true ) ;
1421+ expect ( capturedPayload ) . not . toHaveProperty ( "reasoning_effort" ) ;
1422+ expect ( thinking ) . toBe ( "Need a Qwen answer." ) ;
1423+ expect ( text ) . toBe ( "qwen-ok" ) ;
1424+ } finally {
1425+ await new Promise < void > ( ( resolve , reject ) => {
1426+ server . close ( ( error ) => ( error ? reject ( error ) : resolve ( ) ) ) ;
1427+ } ) ;
1428+ }
1429+ } ) ;
1430+
1431+ it ( "does not emit thinking streams when reasoning is disabled" , ( ) => {
1432+ const model = {
1433+ id : "grok-4.20-beta-latest-reasoning" ,
1434+ name : "Grok 4.20 Beta Latest (Reasoning)" ,
1435+ api : "openai-completions" ,
1436+ provider : "xai" ,
1437+ baseUrl : "https://api.x.ai/v1" ,
1438+ reasoning : true ,
1439+ input : [ "text" ] ,
1440+ cost : { input : 0 , output : 0 , cacheRead : 0 , cacheWrite : 0 } ,
1441+ contextWindow : 2_000_000 ,
1442+ maxTokens : 30_000 ,
1443+ } satisfies Model < "openai-completions" > ;
1444+
1445+ expect (
1446+ testing . shouldEmitOpenAICompletionsReasoningForModel ( model , {
1447+ apiKey : "test-key" ,
1448+ reasoning : "off" ,
1449+ } as never ) ,
1450+ ) . toBe ( false ) ;
1451+ } ) ;
1452+
1453+ it ( "emits Z.ai thinking streams when enabled without reasoning_effort support" , ( ) => {
1454+ const model = {
1455+ id : "glm-4.7" ,
1456+ name : "GLM 4.7" ,
1457+ api : "openai-completions" ,
1458+ provider : "zai" ,
1459+ baseUrl : "" ,
1460+ reasoning : true ,
1461+ input : [ "text" ] ,
1462+ cost : { input : 0 , output : 0 , cacheRead : 0 , cacheWrite : 0 } ,
1463+ contextWindow : 128_000 ,
1464+ maxTokens : 8192 ,
1465+ } satisfies Model < "openai-completions" > ;
1466+
1467+ expect (
1468+ testing . shouldEmitOpenAICompletionsReasoningForModel ( model , {
1469+ apiKey : "test-key" ,
1470+ reasoning : "medium" ,
1471+ } as never ) ,
1472+ ) . toBe ( true ) ;
1473+ } ) ;
1474+
13381475 it ( "preserves OpenAI-compatible error metadata on failed chat requests" , async ( ) => {
13391476 const server = createServer ( ( req , res ) => {
13401477 req . resume ( ) ;
@@ -7396,6 +7533,73 @@ describe("openai transport stream", () => {
73967533 expect ( events . filter ( ( event ) => event . type === "thinking_delta" ) ) . toHaveLength ( 1 ) ;
73977534 } ) ;
73987535
7536+ it ( "drops mirrored reasoning when disabled without recovering hidden reasoning tags" , async ( ) => {
7537+ const model = {
7538+ id : "MiniMax-M2.7" ,
7539+ name : "MiniMax M2.7" ,
7540+ api : "openai-completions" ,
7541+ provider : "minimax" ,
7542+ baseUrl : "https://api.minimax.test/v1" ,
7543+ reasoning : true ,
7544+ input : [ "text" ] ,
7545+ cost : { input : 0 , output : 0 , cacheRead : 0 , cacheWrite : 0 } ,
7546+ contextWindow : 200000 ,
7547+ maxTokens : 8192 ,
7548+ } satisfies Model < "openai-completions" > ;
7549+ const output = createAssistantOutput ( model ) ;
7550+ const events : CapturedStreamEvent [ ] = [ ] ;
7551+
7552+ await testing . processOpenAICompletionsStream (
7553+ streamChunks ( [
7554+ {
7555+ id : "chatcmpl-reasoning-disabled" ,
7556+ object : "chat.completion.chunk" as const ,
7557+ created : 1 ,
7558+ model : model . id ,
7559+ choices : [
7560+ {
7561+ index : 0 ,
7562+ delta : {
7563+ content : "<think>private reasoning" ,
7564+ } ,
7565+ logprobs : null ,
7566+ finish_reason : null ,
7567+ } ,
7568+ ] ,
7569+ } ,
7570+ {
7571+ id : "chatcmpl-reasoning-disabled" ,
7572+ object : "chat.completion.chunk" as const ,
7573+ created : 1 ,
7574+ model : model . id ,
7575+ choices : [
7576+ {
7577+ index : 0 ,
7578+ delta : {
7579+ reasoning_content : "private reasoning" ,
7580+ } ,
7581+ logprobs : null ,
7582+ finish_reason : "stop" as const ,
7583+ } ,
7584+ ] ,
7585+ } ,
7586+ ] ) ,
7587+ output ,
7588+ model ,
7589+ { push : ( event ) => events . push ( event as CapturedStreamEvent ) } ,
7590+ { emitReasoning : false } ,
7591+ ) ;
7592+
7593+ const visibleText = output . content
7594+ . filter ( ( block ) : block is { type : "text" ; text : string } => block . type === "text" )
7595+ . map ( ( block ) => block . text )
7596+ . join ( "" ) ;
7597+
7598+ expect ( visibleText ) . toBe ( "" ) ;
7599+ expect ( output . content . some ( ( block ) => block . type === "thinking" ) ) . toBe ( false ) ;
7600+ expect ( events . some ( ( event ) => event . type === "thinking_delta" ) ) . toBe ( false ) ;
7601+ } ) ;
7602+
73997603 it ( "keeps literal reasoning tag examples visible without mirrored reasoning" , async ( ) => {
74007604 const model = createDeepSeekCompletionsModel ( ) ;
74017605 const output = createAssistantOutput ( model ) ;
0 commit comments