11// Verifies session thinking levels reach OpenAI and Codex Responses transports.
2+ import { createServer } from "node:http" ;
3+ import { createLlmRuntime } from "@openclaw/ai" ;
24import { Agent , type StreamFn } from "openclaw/plugin-sdk/agent-core" ;
35import {
46 createAssistantMessageEventStream ,
@@ -9,6 +11,7 @@ import {
911 streamSimple ,
1012} from "openclaw/plugin-sdk/llm" ;
1113import { describe , expect , it } from "vitest" ;
14+ import { resolveEmbeddedAgentStreamFn } from "./embedded-agent-runner/stream-resolution.js" ;
1215
1316type ResponsesModel = Model < "openai-responses" > | Model < "openai-chatgpt-responses" > ;
1417
@@ -36,6 +39,73 @@ const codexTestToken = [
3639] . join ( "." ) ;
3740
3841describe ( "OpenAI thinking contract" , ( ) => {
42+ it . each (
43+ ( [ "qwen" , "qwen-chat-template" ] as const ) . flatMap ( ( thinkingFormat ) =>
44+ ( [ "managed" , "direct" ] as const ) . flatMap ( ( transport ) =>
45+ ( [ undefined , null , "low" , "none" ] as const ) . flatMap ( ( offFallback ) =>
46+ ( [ undefined , "off" , "high" ] as const ) . map ( ( thinkingLevel ) => ( {
47+ thinkingFormat,
48+ transport,
49+ offFallback,
50+ thinkingLevel,
51+ } ) ) ,
52+ ) ,
53+ ) ,
54+ ) ,
55+ ) (
56+ "honors Agent $thinkingLevel with off=$offFallback over $transport $thinkingFormat HTTP" ,
57+ async ( { thinkingFormat, transport, offFallback, thinkingLevel } ) => {
58+ const payload = await captureHttpProviderPayload ( {
59+ api : "openai-completions" ,
60+ thinkingFormat,
61+ transport,
62+ thinkingLevelMap : { off : offFallback } ,
63+ thinkingLevel,
64+ mode : "agent" ,
65+ } ) ;
66+ const thinking = thinkingFormat === "qwen" ? payload : payload . chat_template_kwargs ;
67+ expect ( thinking ) . toMatchObject ( {
68+ enable_thinking :
69+ thinkingLevel === "high" ||
70+ offFallback === "low" ||
71+ ( offFallback === null && transport === "managed" ) ,
72+ } ) ;
73+ } ,
74+ ) ;
75+
76+ it ( "preserves explicit Agent off when a managed Responses request asks for a summary" , async ( ) => {
77+ for ( const thinkingLevel of [ "off" , "high" ] as const ) {
78+ const payload = await captureHttpProviderPayload ( {
79+ api : "openai-responses" ,
80+ thinkingLevel,
81+ reasoningSummary : "auto" ,
82+ mode : "agent" ,
83+ } ) ;
84+ expect ( payload . reasoning ) . toEqual (
85+ thinkingLevel === "off" ? undefined : { effort : "high" , summary : "auto" } ,
86+ ) ;
87+ }
88+ } ) ;
89+
90+ it ( "retains standalone managed defaults when no reasoning option is supplied" , async ( ) => {
91+ const completions = await captureHttpProviderPayload ( {
92+ api : "openai-completions" ,
93+ thinkingFormat : "qwen-chat-template" ,
94+ mode : "standalone" ,
95+ } ) ;
96+ expect ( completions . chat_template_kwargs ) . toMatchObject ( { enable_thinking : true } ) ;
97+ for ( const reasoningSummary of [ undefined , "auto" ] as const ) {
98+ const responses = await captureHttpProviderPayload ( {
99+ api : "openai-responses" ,
100+ reasoningSummary,
101+ mode : "standalone" ,
102+ } ) ;
103+ expect ( responses . reasoning ) . toEqual (
104+ reasoningSummary ? { effort : "high" , summary : "auto" } : undefined ,
105+ ) ;
106+ }
107+ } ) ;
108+
39109 it . each ( [
40110 { model : openaiModel , expectedReasoning : "high" } ,
41111 { model : codexModel , expectedReasoning : "high" } ,
@@ -58,7 +128,7 @@ describe("OpenAI thinking contract", () => {
58128 ) ;
59129
60130 it . each ( [ openaiModel , codexModel ] ) (
61- "does not forward reasoning when session thinkingLevel is off for $provider/$id" ,
131+ "preserves explicit off when session thinkingLevel is off for $provider/$id" ,
62132 async ( model ) => {
63133 const capturedOptions : SimpleStreamOptions [ ] = [ ] ;
64134 const agent = new Agent ( {
@@ -71,7 +141,7 @@ describe("OpenAI thinking contract", () => {
71141
72142 await agent . prompt ( "hello" ) ;
73143
74- expect ( capturedOptions . map ( ( { reasoning } ) => reasoning ) ) . toStrictEqual ( [ undefined ] ) ;
144+ expect ( capturedOptions . map ( ( { reasoning } ) => reasoning ) ) . toStrictEqual ( [ "off" ] ) ;
75145 } ,
76146 ) ;
77147
@@ -95,27 +165,132 @@ describe("OpenAI thinking contract", () => {
95165 expect ( payload . reasoning ) . toEqual ( { effort : "high" , summary : "auto" } ) ;
96166 } ) ;
97167
98- it ( "leaves Codex Responses reasoning absent when agent runtime disables thinking" , async ( ) => {
99- const payload = await captureProviderPayload ( {
100- model : codexModel ,
101- streamFn : streamSimple ,
102- options : { transport : "sse" } ,
103- } ) ;
168+ it . each ( [ undefined , "off" ] as const ) (
169+ "leaves direct Codex Responses reasoning absent for %s" ,
170+ async ( reasoning ) => {
171+ const payload = await captureProviderPayload ( {
172+ model : codexModel ,
173+ streamFn : streamSimple ,
174+ options : { transport : "sse" , reasoning } ,
175+ } ) ;
104176
105- expect ( payload ) . not . toHaveProperty ( "reasoning" ) ;
106- } ) ;
177+ expect ( payload ) . not . toHaveProperty ( "reasoning" ) ;
178+ } ,
179+ ) ;
107180
108- it ( "keeps OpenAI Responses reasoning explicitly disabled when agent runtime disables thinking" , async ( ) => {
109- const payload = await captureProviderPayload ( {
110- model : openaiModel ,
111- streamFn : streamSimple ,
112- options : { } ,
113- } ) ;
181+ it . each ( [ undefined , "off" ] as const ) (
182+ "keeps direct OpenAI Responses reasoning disabled for %s" ,
183+ async ( reasoning ) => {
184+ const payload = await captureProviderPayload ( {
185+ model : openaiModel ,
186+ streamFn : streamSimple ,
187+ options : { reasoning } ,
188+ } ) ;
114189
115- expect ( payload . reasoning ) . toEqual ( { effort : "none" } ) ;
116- } ) ;
190+ expect ( payload . reasoning ) . toEqual ( { effort : "none" } ) ;
191+ } ,
192+ ) ;
117193} ) ;
118194
195+ async function captureHttpProviderPayload ( params : {
196+ api : "openai-completions" | "openai-responses" ;
197+ thinkingFormat ?: "qwen" | "qwen-chat-template" ;
198+ transport ?: "managed" | "direct" ;
199+ thinkingLevelMap ?: Model [ "thinkingLevelMap" ] ;
200+ thinkingLevel ?: "off" | "high" ;
201+ reasoningSummary ?: "auto" ;
202+ mode : "agent" | "standalone" ;
203+ } ) : Promise < Record < string , unknown > > {
204+ let payload : Record < string , unknown > | undefined ;
205+ const server = createServer ( ( request , response ) => {
206+ let body = "" ;
207+ request . setEncoding ( "utf8" ) ;
208+ request . on ( "data" , ( chunk : string ) => {
209+ body += chunk ;
210+ } ) ;
211+ request . on ( "end" , ( ) => {
212+ payload = JSON . parse ( body ) as Record < string , unknown > ;
213+ const event =
214+ params . api === "openai-completions"
215+ ? {
216+ id : "chatcmpl_thinking" ,
217+ choices : [
218+ { index : 0 , delta : { role : "assistant" , content : "ok" } , finish_reason : "stop" } ,
219+ ] ,
220+ }
221+ : {
222+ type : "response.completed" ,
223+ response : { id : "resp_thinking" , status : "completed" , output : [ ] } ,
224+ } ;
225+ response . writeHead ( 200 , { "content-type" : "text/event-stream" } ) ;
226+ response . end ( `data: ${ JSON . stringify ( event ) } \n\ndata: [DONE]\n\n` ) ;
227+ } ) ;
228+ } ) ;
229+ await new Promise < void > ( ( resolve ) => {
230+ server . listen ( 0 , "127.0.0.1" , resolve ) ;
231+ } ) ;
232+ try {
233+ const address = server . address ( ) ;
234+ if ( ! address || typeof address === "string" ) {
235+ throw new Error ( "Missing loopback server address" ) ;
236+ }
237+ const model : Model = {
238+ id : params . api === "openai-completions" ? "qwen3.6-27b" : "gpt-5.5" ,
239+ name : "Thinking contract model" ,
240+ api : params . api ,
241+ provider : "local-thinking" ,
242+ baseUrl : `http://127.0.0.1:${ address . port } /v1` ,
243+ reasoning : true ,
244+ thinkingLevelMap : params . thinkingLevelMap ,
245+ input : [ "text" ] ,
246+ cost : { input : 0 , output : 0 , cacheRead : 0 , cacheWrite : 0 } ,
247+ contextWindow : 128_000 ,
248+ maxTokens : 4_096 ,
249+ ...( params . thinkingFormat
250+ ? { compat : { thinkingFormat : params . thinkingFormat , supportsReasoningEffort : false } }
251+ : { } ) ,
252+ } ;
253+ const providerStream =
254+ params . transport === "direct"
255+ ? streamSimple
256+ : resolveEmbeddedAgentStreamFn ( {
257+ llmRuntime : createLlmRuntime ( ) ,
258+ currentStreamFn : undefined ,
259+ model,
260+ sessionId : "thinking-contract" ,
261+ resolvedApiKey : "synthetic-test-key" ,
262+ } ) ;
263+ const streamFn : StreamFn = ( requestModel , context , options ) =>
264+ providerStream ( requestModel , context , {
265+ ...options ,
266+ apiKey : "synthetic-test-key" ,
267+ ...( params . reasoningSummary ? { reasoningSummary : params . reasoningSummary } : { } ) ,
268+ } ) ;
269+ if ( params . mode === "agent" ) {
270+ const agent = new Agent ( {
271+ initialState : { model, thinkingLevel : params . thinkingLevel } ,
272+ streamFn,
273+ } ) ;
274+ await agent . prompt ( "hello" ) ;
275+ expect ( agent . state . errorMessage ) . toBeUndefined ( ) ;
276+ } else {
277+ const stream = await streamFn ( model , {
278+ messages : [ { role : "user" , content : "hello" , timestamp : 0 } ] ,
279+ } ) ;
280+ expect ( ( await stream . result ( ) ) . stopReason ) . toBe ( "stop" ) ;
281+ }
282+ if ( ! payload ) {
283+ throw new Error ( "Provider did not receive a request" ) ;
284+ }
285+ return payload ;
286+ } finally {
287+ server . closeAllConnections ( ) ;
288+ await new Promise < void > ( ( resolve , reject ) => {
289+ server . close ( ( error ) => ( error ? reject ( error ) : resolve ( ) ) ) ;
290+ } ) ;
291+ }
292+ }
293+
119294function createCapturingStreamFn (
120295 model : ResponsesModel ,
121296 capturedOptions : SimpleStreamOptions [ ] ,
0 commit comments