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
25 changes: 25 additions & 0 deletions server/src/server/tool_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
// 5. call:<ns>?<verb>{relaxed-JSON args} (gemma plain-text emissions)
// 6. Bare JSON objects with name+arguments fields
// 7. Whole-response JSON args for exactly one declared tool
// 8. <TOOL_NAME>...<parameter=K>V</parameter>...</function> (bare tool tag)
//
// Pattern 5 runs *before* pattern 6 so that args like
// call:outer{"name": "inner", "arguments": {}}
Expand Down Expand Up @@ -184,6 +185,11 @@ static const std::regex & re_function_signature() {
return r;
}

static const std::regex & re_bare_tool_name_xml() {
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
static std::regex r(R"(<([A-Za-z_][\w.\-]*?)>([\s\S]*?)(?:</function>|</\1>))");
return r;
}

static const std::regex & re_tool_code() {
static std::regex r(R"(<tool_code>([\s\S]*?)</tool_code>)");
return r;
Expand Down Expand Up @@ -756,6 +762,25 @@ ToolParseResult parse_tool_calls(const std::string & text, const json & tools) {
}
}

// Pattern 3b: <TOOL_NAME>...params...</function>. Some agents/models
// emit the selected tool name as the XML tag itself, then close with the
// Qwen </function> tag. Only accept requested tools and real parameter
// tags so arbitrary XML-ish prose remains visible text.
if (tools.is_array() && !tools.empty()) {
auto begin = std::sregex_iterator(text.begin(), text.end(), re_bare_tool_name_xml());
auto end = std::sregex_iterator();
for (auto it = begin; it != end; ++it) {
size_t pos = it->position();
if (overlaps(removals, pos)) continue;
std::string fn_name = (*it)[1].str();
std::string params = (*it)[2].str();
if (!tool_allowed(tools, fn_name)) continue;
if (params.find("<parameter=") == std::string::npos) continue;
add_call(fn_name, parse_xml_params(params, fn_name, tools),
pos, pos + it->length());
}
}

// Pattern 4: <tool_code>{JSON}</tool_code>
{
auto begin = std::sregex_iterator(text.begin(), text.end(), re_tool_code());
Expand Down
35 changes: 35 additions & 0 deletions server/test/test_server_unit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,40 @@ static void test_parse_bare_function_xml() {
}
}

static void test_parse_bare_tool_name_xml_with_function_close() {
std::string text =
"\n\n\nLet me find the correct line range for the tests array.\n\n\n"
"<bash>\n"
"<parameter=command>\n"
"grep -n \"f5.test\" /workspace/project/tests/bootstrap.cjs\n"
"</parameter>\n"
"</function>\n";
json tools = json::array({
{{"type", "function"},
{"function", {
{"name", "bash"},
{"parameters", {
{"type", "object"},
{"properties", {
{"command", {{"type", "string"}}}
}}
}}
}}}
});
auto result = parse_tool_calls(text, tools);
TEST_ASSERT(result.tool_calls.size() == 1);
if (!result.tool_calls.empty()) {
TEST_ASSERT(result.tool_calls[0].name == "bash");
auto args = json::parse(result.tool_calls[0].arguments);
TEST_ASSERT(args["command"] ==
"grep -n \"f5.test\" /workspace/project/tests/bootstrap.cjs");
}
TEST_ASSERT(result.cleaned_text.find("<bash>") == std::string::npos);
TEST_ASSERT(result.cleaned_text.find("</function>") == std::string::npos);
TEST_ASSERT(result.cleaned_text.find("Let me find the correct line range") !=
std::string::npos);
}

static void test_parse_json_tool_call() {
std::string text =
"{\"name\": \"search\", \"arguments\": {\"query\": \"hello world\"}}";
Expand Down Expand Up @@ -4423,6 +4457,7 @@ int main() {
std::fprintf(stderr, "\n── Tool parser ──\n");
RUN_TEST(test_parse_tool_call_xml);
RUN_TEST(test_parse_bare_function_xml);
RUN_TEST(test_parse_bare_tool_name_xml_with_function_close);
RUN_TEST(test_parse_json_tool_call);
RUN_TEST(test_parse_single_tool_bare_json_args);
RUN_TEST(test_parse_single_tool_bare_json_args_allows_empty_optional_object);
Expand Down