Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions packages/sdk/server/utils/tools/parsers/hermes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,30 @@ import {
type ParserResult,
} from "@/server/utils/tools/shared";

// Qwen3.5/3.6 can fuse its two tool templates into one frame, embedding the
// XML `<function=NAME>` token as a bare string key inside the JSON envelope:
// {"function=NAME","arguments":{...}} (invalid JSON, not parseable as-is)
// Rewrite only that exact shape to a canonical `{"name":NAME,"arguments":...}`
// frame. Kept deliberately narrow so well-formed JSON frames are never touched.
function repairFunctionEqualsJson(candidate: string): string | undefined {
const match =
/^\{\s*"function=([^"]+)"\s*,\s*"arguments"\s*:\s*([\s\S]+)\}\s*$/.exec(
candidate,
);
if (!match) return undefined;
return `{"name":${JSON.stringify(match[1])},"arguments":${match[2]}}`;
}

function parseHermesJson(candidate: string): unknown {
try {
return JSON.parse(candidate);
} catch (err) {
const repaired = repairFunctionEqualsJson(candidate);
if (repaired === undefined) throw err;
return JSON.parse(repaired);
}
}

// Hermes-style: JSON payload wrapped in `<tool_call>...</tool_call>` tags
export function parseHermesFormat(text: string, tools: Tool[]): ParserResult {
const toolCalls: ToolCall[] = [];
Expand All @@ -32,7 +56,7 @@ export function parseHermesFormat(text: string, tools: Tool[]): ParserResult {

let callItem: unknown;
try {
callItem = JSON.parse(trimmedJson);
callItem = parseHermesJson(trimmedJson);
} catch (error) {
errors.push({
code: "PARSE_ERROR",
Expand Down Expand Up @@ -96,7 +120,7 @@ function recoverIncompleteHermesFrame(

let parsed: unknown;
try {
parsed = JSON.parse(candidate);
parsed = parseHermesJson(candidate);
} catch {
return { matched: false, toolCalls: [], errors: [] };
}
Expand Down
27 changes: 27 additions & 0 deletions packages/sdk/test/unit/tool-parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -972,6 +972,33 @@ test("parseToolCalls(dialect=qwen35): JSON inside tool_call falls through to her
t.alike(toolCalls[0]?.arguments, { city: "Seoul" });
});

test("parseToolCalls(dialect=qwen35): recovers function-equals JSON hybrid", (t) => {
const webfetchTool: Tool = {
type: "function",
name: "webfetch",
description: "Fetch a URL",
parameters: {
type: "object",
properties: {
url: { type: "string" },
format: { type: "string" },
},
required: ["url"],
},
};
const text = `<tool_call>
{"function=webfetch","arguments":{"url":"https://docs.opencode.ai","format":"markdown"}}
</tool_call>`;
const { toolCalls, errors } = parseToolCalls(text, [webfetchTool], "qwen35");
t.is(errors.length, 0);
t.is(toolCalls.length, 1);
t.is(toolCalls[0]?.name, "webfetch");
t.alike(toolCalls[0]?.arguments, {
url: "https://docs.opencode.ai",
format: "markdown",
});
});

// --- gemma4 structural and error-surface tests ---

test("parseGemma4NativeFormat: bare numeric arg is parsed as number", (t) => {
Expand Down
Loading