Skip to content

Commit 2f95c1e

Browse files
committed
fix(ollama): preserve compat tool argument strings
1 parent ce6d97d commit 2f95c1e

2 files changed

Lines changed: 81 additions & 41 deletions

File tree

‎extensions/ollama/src/stream-runtime.test.ts‎

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,26 @@ describe("buildOllamaChatRequest", () => {
115115
});
116116
expect(request.model).toBe("library/qwen3:32b");
117117
});
118+
119+
it("keeps native Ollama replay tool arguments as objects", () => {
120+
const messages = convertToOllamaMessages([
121+
{
122+
role: "assistant",
123+
content: [
124+
{
125+
type: "toolCall",
126+
name: "gateway",
127+
arguments: '{"action":"config.get","path":"gateway.port"}',
128+
},
129+
],
130+
},
131+
]);
132+
133+
expect(messages[0]?.tool_calls?.[0]?.function.arguments).toEqual({
134+
action: "config.get",
135+
path: "gateway.port",
136+
});
137+
});
118138
});
119139

120140
describe("createConfiguredOllamaCompatStreamWrapper", () => {
@@ -156,6 +176,67 @@ describe("createConfiguredOllamaCompatStreamWrapper", () => {
156176
expect(payload.options).toEqual({ num_ctx: 65536 });
157177
});
158178

179+
it("preserves OpenAI-compatible replay tool arguments as strings", async () => {
180+
let patchedPayload: Record<string, unknown> | undefined;
181+
const baseStreamFn = vi.fn((_model, _context, options) => {
182+
options?.onPayload?.({
183+
messages: [
184+
{
185+
role: "assistant",
186+
function_call: {
187+
name: "legacy_gateway",
188+
arguments: '{"action":"config.get"}',
189+
},
190+
tool_calls: [
191+
{
192+
id: "call_gateway",
193+
type: "function",
194+
function: {
195+
name: "gateway",
196+
arguments: '{"action":"config.get","path":"gateway.port"}',
197+
},
198+
},
199+
],
200+
},
201+
],
202+
});
203+
return (async function* () {})();
204+
});
205+
const model = {
206+
api: "openai-completions",
207+
provider: "ollama",
208+
id: "glm-5.2:cloud",
209+
contextWindow: 262144,
210+
};
211+
212+
const wrapped = createConfiguredOllamaCompatStreamWrapper({
213+
provider: "ollama",
214+
modelId: "glm-5.2:cloud",
215+
model,
216+
streamFn: baseStreamFn,
217+
} as never);
218+
219+
await wrapped?.(
220+
model as never,
221+
{ messages: [] } as never,
222+
{
223+
onPayload: (payload: unknown) => {
224+
patchedPayload = payload as Record<string, unknown>;
225+
},
226+
} as never,
227+
);
228+
229+
const payload = requireRecord(patchedPayload, "patched payload");
230+
const messages = payload.messages as Array<Record<string, unknown>>;
231+
const assistantMessage = requireRecord(messages[0], "assistant message");
232+
const functionCall = requireRecord(assistantMessage.function_call, "function call");
233+
const toolCalls = assistantMessage.tool_calls as Array<Record<string, unknown>>;
234+
const toolCallFunction = requireRecord(toolCalls[0]?.function, "tool call function");
235+
expect(functionCall.arguments).toBe('{"action":"config.get"}');
236+
expect(toolCallFunction.arguments).toBe('{"action":"config.get","path":"gateway.port"}');
237+
expect(payload.options).toEqual({ num_ctx: 262144 });
238+
});
239+
159240
it("falls back to contextWindow when configured num_ctx is invalid", async () => {
160241
let patchedPayload: Record<string, unknown> | undefined;
161242
const baseStreamFn = vi.fn((_model, _context, options) => {

‎extensions/ollama/src/stream.ts‎

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,6 @@ export function wrapOllamaCompatNumCtx(baseFn: StreamFn | undefined, numCtx: num
247247
payloadRecord.options = {};
248248
}
249249
(payloadRecord.options as Record<string, unknown>).num_ctx = numCtx;
250-
normalizeOllamaCompatMessageToolArgs(payloadRecord);
251250
});
252251
}
253252

@@ -736,46 +735,6 @@ function normalizeOllamaToolCallArguments(value: unknown): Record<string, unknow
736735
return ensureArgsObject(value);
737736
}
738737

739-
function normalizeOllamaCompatMessageToolArgs(payloadRecord: Record<string, unknown>): void {
740-
const messages = payloadRecord.messages;
741-
if (!Array.isArray(messages)) {
742-
return;
743-
}
744-
745-
for (const message of messages) {
746-
if (!message || typeof message !== "object" || Array.isArray(message)) {
747-
continue;
748-
}
749-
const messageRecord = message as Record<string, unknown>;
750-
751-
const functionCall = messageRecord.function_call;
752-
if (functionCall && typeof functionCall === "object" && !Array.isArray(functionCall)) {
753-
const functionCallRecord = functionCall as Record<string, unknown>;
754-
if (Object.hasOwn(functionCallRecord, "arguments")) {
755-
functionCallRecord.arguments = ensureArgsObject(functionCallRecord.arguments);
756-
}
757-
}
758-
759-
const toolCalls = messageRecord.tool_calls;
760-
if (!Array.isArray(toolCalls)) {
761-
continue;
762-
}
763-
for (const toolCall of toolCalls) {
764-
if (!toolCall || typeof toolCall !== "object" || Array.isArray(toolCall)) {
765-
continue;
766-
}
767-
const functionSpec = (toolCall as Record<string, unknown>).function;
768-
if (!functionSpec || typeof functionSpec !== "object" || Array.isArray(functionSpec)) {
769-
continue;
770-
}
771-
const functionRecord = functionSpec as Record<string, unknown>;
772-
if (Object.hasOwn(functionRecord, "arguments")) {
773-
functionRecord.arguments = ensureArgsObject(functionRecord.arguments);
774-
}
775-
}
776-
}
777-
}
778-
779738
function inferOllamaSchemaType(schema: Record<string, unknown>): string | undefined {
780739
if (schema.properties && isRecord(schema.properties)) {
781740
return "object";

0 commit comments

Comments
 (0)