@@ -257,6 +257,113 @@ describe("createConfiguredOllamaCompatStreamWrapper", () => {
257257 expect ( legacyFunctionCall . arguments ) . toBe ( '{"action":"config.get","path":"gateway.host"}' ) ;
258258 } ) ;
259259
260+ it ( "replays the same tool call as object args natively but string args for OpenAI-compat" , async ( ) => {
261+ // #96441 invariant: identical replayed tool calls must diverge by API. Native
262+ // /api/chat expects object arguments; OpenAI-compatible Chat Completions requires
263+ // JSON-string arguments. This guards against re-unifying the two normalization paths.
264+ const stringArgs = '{"action":"config.get","path":"gateway.port"}' ;
265+
266+ const nativeMessages = convertToOllamaMessages ( [
267+ {
268+ role : "assistant" ,
269+ content : [ { type : "toolCall" , id : "call_gateway" , name : "gateway" , arguments : stringArgs } ] ,
270+ } ,
271+ ] ) ;
272+ expect ( nativeMessages [ 0 ] . tool_calls ) . toEqual ( [
273+ {
274+ id : "call_gateway" ,
275+ function : { name : "gateway" , arguments : { action : "config.get" , path : "gateway.port" } } ,
276+ } ,
277+ ] ) ;
278+
279+ let patchedPayload : Record < string , unknown > | undefined ;
280+ const baseStreamFn = vi . fn ( ( _model , _context , options ) => {
281+ options ?. onPayload ?.( {
282+ messages : [
283+ {
284+ role : "assistant" ,
285+ tool_calls : [
286+ {
287+ id : "call_gateway" ,
288+ type : "function" ,
289+ function : { name : "gateway" , arguments : stringArgs } ,
290+ } ,
291+ ] ,
292+ } ,
293+ ] ,
294+ } ) ;
295+ return ( async function * ( ) { } ) ( ) ;
296+ } ) ;
297+ const model = {
298+ api : "openai-completions" ,
299+ provider : "ollama" ,
300+ id : "glm-5.2:cloud" ,
301+ contextWindow : 131072 ,
302+ params : { num_ctx : 32768 } ,
303+ } ;
304+
305+ const wrapped = createConfiguredOllamaCompatStreamWrapper ( {
306+ provider : "ollama" ,
307+ modelId : "glm-5.2:cloud" ,
308+ model,
309+ streamFn : baseStreamFn ,
310+ } as never ) ;
311+
312+ await wrapped ?.(
313+ model as never ,
314+ { messages : [ ] } as never ,
315+ {
316+ onPayload : ( payload : unknown ) => {
317+ patchedPayload = payload as Record < string , unknown > ;
318+ } ,
319+ } as never ,
320+ ) ;
321+
322+ const payload = requireRecord ( patchedPayload , "patched payload" ) ;
323+ const compatToolCalls = ( payload . messages as Array < Record < string , unknown > > ) [ 0 ]
324+ . tool_calls as Array < {
325+ function : { arguments : unknown } ;
326+ } > ;
327+ expect ( compatToolCalls [ 0 ] . function . arguments ) . toBe ( stringArgs ) ;
328+ } ) ;
329+
330+ it ( "injects num_ctx without clobbering existing OpenAI-compat options" , async ( ) => {
331+ let patchedPayload : Record < string , unknown > | undefined ;
332+ const baseStreamFn = vi . fn ( ( _model , _context , options ) => {
333+ options ?. onPayload ?.( { options : { temperature : 0.5 } } ) ;
334+ return ( async function * ( ) { } ) ( ) ;
335+ } ) ;
336+ const model = {
337+ api : "openai-completions" ,
338+ provider : "ollama" ,
339+ id : "glm-5.2:cloud" ,
340+ contextWindow : 131072 ,
341+ params : { num_ctx : 32768 } ,
342+ } ;
343+
344+ const wrapped = createConfiguredOllamaCompatStreamWrapper ( {
345+ provider : "ollama" ,
346+ modelId : "glm-5.2:cloud" ,
347+ model,
348+ streamFn : baseStreamFn ,
349+ } as never ) ;
350+
351+ await wrapped ?.(
352+ model as never ,
353+ { messages : [ ] } as never ,
354+ {
355+ onPayload : ( payload : unknown ) => {
356+ patchedPayload = payload as Record < string , unknown > ;
357+ } ,
358+ } as never ,
359+ ) ;
360+
361+ const payload = requireRecord ( patchedPayload , "patched payload" ) ;
362+ const options = requireRecord ( payload . options , "patched options" ) ;
363+ expect ( options . temperature ) . toBe ( 0.5 ) ;
364+ expect ( options . num_ctx ) . toBe ( 32768 ) ;
365+ } ) ;
366+
260367 it ( "forwards think=false on native Ollama chat requests when thinking is off" , async ( ) => {
261368 await withMockNdjsonFetch (
262369 [
0 commit comments