Skip to content

Commit b1deca6

Browse files
fix: Found one narrow regression risk in the new Ollama tool-call name
1 parent 97e2f5b commit b1deca6

2 files changed

Lines changed: 139 additions & 8 deletions

File tree

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

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,48 @@ describe("convertToOllamaMessages", () => {
577577
]);
578578
});
579579

580+
it("preserves exact allowlisted tool-prefix names before Ollama replay", () => {
581+
const messages = [
582+
{
583+
role: "assistant",
584+
content: [
585+
{ type: "toolCall", id: "call_1", name: "tool_a", arguments: { value: 1 } },
586+
{ type: "tool_use", id: "call_2", name: "tools_invoke_test", input: { value: 2 } },
587+
{ type: "toolCall", id: "call_3", name: "function-run", arguments: { value: 3 } },
588+
],
589+
},
590+
];
591+
const result = convertToOllamaMessages(messages, undefined, {
592+
availableToolNames: new Set(["tool_a", "tools_invoke_test", "function-run"]),
593+
});
594+
expect(result[0].tool_calls).toEqual([
595+
{ function: { name: "tool_a", arguments: { value: 1 } } },
596+
{ function: { name: "tools_invoke_test", arguments: { value: 2 } } },
597+
{ function: { name: "function-run", arguments: { value: 3 } } },
598+
]);
599+
});
600+
601+
it("strips underscore and dash provider prefixes only when the suffix is allowlisted", () => {
602+
const messages = [
603+
{
604+
role: "assistant",
605+
content: [
606+
{ type: "toolCall", id: "call_1", name: "tools_exec", arguments: { command: "pwd" } },
607+
{ type: "tool_use", id: "call_2", name: "function-read", input: { path: "." } },
608+
{ type: "toolCall", id: "call_3", name: "tool_missing", arguments: {} },
609+
],
610+
},
611+
];
612+
const result = convertToOllamaMessages(messages, undefined, {
613+
availableToolNames: new Set(["exec", "read"]),
614+
});
615+
expect(result[0].tool_calls).toEqual([
616+
{ function: { name: "exec", arguments: { command: "pwd" } } },
617+
{ function: { name: "read", arguments: { path: "." } } },
618+
{ function: { name: "tool_missing", arguments: {} } },
619+
]);
620+
});
621+
580622
it("keeps non-prefixed Ollama replay tool names intact", () => {
581623
const messages = [
582624
{
@@ -585,6 +627,7 @@ describe("convertToOllamaMessages", () => {
585627
{ type: "toolCall", id: "call_1", name: "functionshell", arguments: {} },
586628
{ type: "toolCall", id: "call_2", name: "tooling", arguments: {} },
587629
{ type: "toolCall", id: "call_3", name: "tools", arguments: {} },
630+
{ type: "toolCall", id: "call_4", name: "tool_a", arguments: {} },
588631
],
589632
},
590633
];
@@ -593,6 +636,7 @@ describe("convertToOllamaMessages", () => {
593636
{ function: { name: "functionshell", arguments: {} } },
594637
{ function: { name: "tooling", arguments: {} } },
595638
{ function: { name: "tools", arguments: {} } },
639+
{ function: { name: "tool_a", arguments: {} } },
596640
]);
597641
});
598642

@@ -825,6 +869,39 @@ describe("buildAssistantMessage", () => {
825869
]);
826870
});
827871

872+
it("preserves exact allowlisted tool-prefix names in Ollama responses", () => {
873+
const response = {
874+
model: "qwen3:32b",
875+
created_at: "2026-01-01T00:00:00Z",
876+
message: {
877+
role: "assistant" as const,
878+
content: "",
879+
tool_calls: [
880+
{ function: { name: "tool_a", arguments: { value: 1 } } },
881+
{ function: { name: "tools_invoke_test", arguments: { value: 2 } } },
882+
{ function: { name: "function-run", arguments: { value: 3 } } },
883+
],
884+
},
885+
done: true,
886+
};
887+
const result = buildAssistantMessage(response, modelInfo, undefined, {
888+
availableToolNames: new Set(["tool_a", "tools_invoke_test", "function-run"]),
889+
});
890+
expect(result.content).toEqual([
891+
expect.objectContaining({ type: "toolCall", name: "tool_a", arguments: { value: 1 } }),
892+
expect.objectContaining({
893+
type: "toolCall",
894+
name: "tools_invoke_test",
895+
arguments: { value: 2 },
896+
}),
897+
expect.objectContaining({
898+
type: "toolCall",
899+
name: "function-run",
900+
arguments: { value: 3 },
901+
}),
902+
]);
903+
});
904+
828905
it("keeps non-prefixed Ollama response tool names intact", () => {
829906
const response = {
830907
model: "qwen3:32b",
@@ -836,6 +913,7 @@ describe("buildAssistantMessage", () => {
836913
{ function: { name: "functionshell", arguments: {} } },
837914
{ function: { name: "tooling", arguments: {} } },
838915
{ function: { name: "tools", arguments: {} } },
916+
{ function: { name: "tool_a", arguments: {} } },
839917
],
840918
},
841919
done: true,
@@ -845,6 +923,7 @@ describe("buildAssistantMessage", () => {
845923
expect.objectContaining({ type: "toolCall", name: "functionshell", arguments: {} }),
846924
expect.objectContaining({ type: "toolCall", name: "tooling", arguments: {} }),
847925
expect.objectContaining({ type: "toolCall", name: "tools", arguments: {} }),
926+
expect.objectContaining({ type: "toolCall", name: "tool_a", arguments: {} }),
848927
]);
849928
});
850929

‎extensions/ollama/src/stream.ts‎

Lines changed: 60 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,14 @@ function normalizeOllamaToolSchema(schema: unknown, isRoot = false): Record<stri
758758
return normalized;
759759
}
760760

761-
function extractToolCalls(content: unknown): OllamaToolCall[] {
761+
type OllamaToolCallNameOptions = {
762+
availableToolNames?: ReadonlySet<string>;
763+
};
764+
765+
function extractToolCalls(
766+
content: unknown,
767+
options: OllamaToolCallNameOptions = {},
768+
): OllamaToolCall[] {
762769
if (!Array.isArray(content)) {
763770
return [];
764771
}
@@ -768,14 +775,14 @@ function extractToolCalls(content: unknown): OllamaToolCall[] {
768775
if (part.type === "toolCall") {
769776
result.push({
770777
function: {
771-
name: normalizeOllamaToolCallName(part.name),
778+
name: normalizeOllamaToolCallName(part.name, options),
772779
arguments: ensureArgsObject(part.arguments),
773780
},
774781
});
775782
} else if (part.type === "tool_use") {
776783
result.push({
777784
function: {
778-
name: normalizeOllamaToolCallName(part.name),
785+
name: normalizeOllamaToolCallName(part.name, options),
779786
arguments: ensureArgsObject(part.input),
780787
},
781788
});
@@ -784,17 +791,51 @@ function extractToolCalls(content: unknown): OllamaToolCall[] {
784791
return result;
785792
}
786793

787-
function normalizeOllamaToolCallName(rawName: string): string {
794+
function buildOllamaToolNameSet(tools: Tool[] | undefined): ReadonlySet<string> | undefined {
795+
if (!tools || !Array.isArray(tools)) {
796+
return undefined;
797+
}
798+
const names = new Set<string>();
799+
for (const tool of tools) {
800+
if (typeof tool.name === "string" && tool.name.trim()) {
801+
names.add(tool.name.trim());
802+
}
803+
}
804+
return names.size > 0 ? names : undefined;
805+
}
806+
807+
function normalizeOllamaToolCallName(
808+
rawName: string,
809+
options: OllamaToolCallNameOptions = {},
810+
): string {
788811
const trimmed = rawName.trim();
789812
if (!trimmed) {
790813
return trimmed;
791814
}
792-
return trimmed.replace(/^(?:functions?|tools?)[./_-]+/iu, "").trim();
815+
const availableToolNames = options.availableToolNames;
816+
if (availableToolNames?.has(trimmed)) {
817+
return trimmed;
818+
}
819+
820+
const strippedAnySeparator = trimmed.replace(/^(?:functions?|tools?)[./_-]+/iu, "").trim();
821+
if (
822+
availableToolNames &&
823+
strippedAnySeparator !== trimmed &&
824+
availableToolNames.has(strippedAnySeparator)
825+
) {
826+
return strippedAnySeparator;
827+
}
828+
if (availableToolNames) {
829+
return trimmed;
830+
}
831+
832+
return trimmed.replace(/^(?:functions?|tools?)[./]+/iu, "").trim();
793833
}
794834

795835
export function convertToOllamaMessages(
796836
messages: Array<{ role: string; content: unknown }>,
797837
system?: string,
838+
options: OllamaToolCallNameOptions = {},
798839
): OllamaChatMessage[] {
799840
const result: OllamaChatMessage[] = [];
800841

@@ -816,7 +857,7 @@ export function convertToOllamaMessages(
816857

817858
if (msg.role === "assistant") {
818859
const text = extractTextContent(msg.content);
819-
const toolCalls = extractToolCalls(msg.content);
860+
const toolCalls = extractToolCalls(msg.content, options);
820861
result.push({
821862
role: "assistant",
822863
content: text,
@@ -867,6 +908,7 @@ export function buildAssistantMessage(
867908
response: OllamaChatResponse,
868909
modelInfo: StreamModelDescriptor,
869910
usageFallback?: OllamaUsageFallback,
911+
options: OllamaToolCallNameOptions = {},
870912
): AssistantMessage {
871913
const content: (TextContent | ThinkingContent | ToolCall)[] = [];
872914
const thinking = response.message.thinking ?? response.message.reasoning ?? "";
@@ -884,7 +926,7 @@ export function buildAssistantMessage(
884926
content.push({
885927
type: "toolCall",
886928
id: `ollama_call_${randomUUID()}`,
887-
name: normalizeOllamaToolCallName(toolCall.function.name),
929+
name: normalizeOllamaToolCallName(toolCall.function.name, options),
888930
arguments: normalizeOllamaToolCallArguments(toolCall.function.arguments),
889931
});
890932
}
@@ -976,9 +1018,14 @@ export function createOllamaStreamFn(
9761018

9771019
const run = async () => {
9781020
try {
1021+
const availableToolNames = buildOllamaToolNameSet(context.tools);
1022+
const toolCallNameOptions: OllamaToolCallNameOptions = availableToolNames
1023+
? { availableToolNames }
1024+
: {};
9791025
const ollamaMessages = convertToOllamaMessages(
9801026
context.messages ?? [],
9811027
context.systemPrompt,
1028+
toolCallNameOptions,
9821029
);
9831030
const ollamaTools = extractOllamaTools(context.tools);
9841031

@@ -1214,7 +1261,12 @@ export function createOllamaStreamFn(
12141261
input: estimateOllamaPromptTokens({ messages: ollamaMessages, tools: ollamaTools }),
12151262
output: estimateOllamaCompletionTokens(finalResponse),
12161263
};
1217-
const assistantMessage = buildAssistantMessage(finalResponse, modelInfo, usageFallback);
1264+
const assistantMessage = buildAssistantMessage(
1265+
finalResponse,
1266+
modelInfo,
1267+
usageFallback,
1268+
toolCallNameOptions,
1269+
);
12181270
closeThinkingBlock();
12191271
closeTextBlock();
12201272

0 commit comments

Comments
 (0)