Parse HYV3 native sectioned tool calls and tagged arguments

Use the existing RadChatFormat fields for the outer tool-call section, name separator and tagged values. Cover grammar, template replay, streaming at every truncation, section boundaries and OpenAI response lifting; leave the supports_reasoning adapter unchanged.
This commit is contained in:
Constantin authored and StillDeadcode committed 2026-10-25 12:09:29 +01:00
1 parent 7621e4ac2b
commit 7e04bcac2b
11 files changed
+840 -34

No files matched your search

+251 -1
View File
@@ -1052,6 +1052,33 @@ TEST(no_template_is_a_format_with_no_reasoning_and_no_calls) {
static const char* const kBreaks[] = { "<tool_call>\n" };
static const char* const kTriggers[] = { "<tool_call>\n" };
static RadChatFormat hyv3_abi() {
static const char* const triggers[] = { "<tool_calls>" };
RadChatFormat c;
std::memset(&c, 0, sizeof c);
c.struct_size = sizeof c;
c.name = "hyv3-native";
c.reasoning_open = "<think>";
c.reasoning_close = "</think>";
c.reasoning_breaks = triggers;
c.n_reasoning_breaks = 1;
c.reasoning_in_prompt = RAD_CHAT_THINK_FROM_PROMPT;
c.section_open = "<tool_calls>";
c.section_close = "</tool_calls>";
c.name_prefix = "<tool_call>";
c.name_suffix = "<tool_sep>";
c.call_end = "</tool_call>";
c.arg_prefix = "<arg_key>";
c.arg_name_suffix = "</arg_key>\n";
c.arg_value_prefix = "<arg_value>";
c.arg_value_suffix = "</arg_value>";
c.string_value = RAD_CHAT_VALUE_RAW;
c.other_value = RAD_CHAT_VALUE_JSON;
c.triggers = triggers;
c.n_triggers = 1;
return c;
}
static RadChatFormat qwen_abi() {
RadChatFormat c;
std::memset(&c, 0, sizeof c);
@@ -1268,7 +1295,7 @@ TEST(a_format_the_parser_cannot_read_is_refused_by_name) {
CHECK(refused([](RadChatFormat& c) { c.arg_prefix = "</function>\n"; }, "told apart"));
CHECK(refused([](RadChatFormat& c) { c.reasoning_close = ""; }, "reasoning_close"));
static const char* const other[] = { "<call>" };
CHECK(refused([](RadChatFormat& c) { c.reasoning_breaks = other; }, "call opener"));
CHECK(refused([](RadChatFormat& c) { c.reasoning_breaks = other; }, "opener"));
CHECK(refused([](RadChatFormat& c) { c.string_value = RAD_CHAT_VALUE_JSON; }, "string_value"));
}
@@ -1446,6 +1473,68 @@ TEST(the_chat_bridge_derives_the_whole_request_from_one_format) {
CHECK(why.find("a>b") != std::string::npos);
}
/* The declared format preserves HYV3's outer section and all of its argument markers. */
TEST(hyv3_plugin_format_runs_through_the_chat_template_bridge) {
std::shared_ptr<Vocab> v = vocab_with(fixture("hyv3"));
REQUIRE(v != nullptr);
RadChatFormat c = hyv3_abi();
server::ChatTemplateBridge br;
CHECK_OK(br.init(*v, &c, "hyv3_bf16"));
CHECK(br.supports_tools());
CHECK_EQ(br.tool_refusal(), std::string());
const json tools = json::parse(R"([{"type":"function","function":{"name":"lookup_weather",
"parameters":{"type":"object","properties":{"city":{"type":"string"}},
"required":["city"]}}}])");
const server::ChatRender r = bridge_render(br, tools);
REQUIRE(!r.prompt.empty());
CHECK(!r.grammar.empty());
CHECK(r.grammar_lazy);
REQUIRE(r.grammar_triggers.size() == 1);
CHECK_EQ(r.grammar_triggers[0].value, std::string("<tool_calls>"));
for (const char* marker : { "<think>", "</think>", "<tool_calls>", "</tool_calls>",
"<tool_call>", "<tool_sep>",
"<arg_key>", "</arg_key>", "<arg_value>", "</arg_value>" })
CHECK(std::find(r.preserved_tokens.begin(), r.preserved_tokens.end(), marker) !=
r.preserved_tokens.end());
REQUIRE(r.parser != nullptr);
std::unique_ptr<server::IReplyReader> rd = r.parser->open();
rd->feed("Cloudy.\n<tool_calls><tool_call>lookup_weather<tool_sep>\n"
"<arg_key>city</arg_key>\n<arg_value>Toronto</arg_value>\n"
"</tool_call></tool_calls>", nullptr);
rd->finish(nullptr);
const server::ParsedMessage pm = rd->message();
CHECK_EQ(pm.content, std::string("Cloudy."));
REQUIRE(pm.tool_calls.size() == 1);
CHECK_EQ(pm.tool_calls[0].name, std::string("lookup_weather"));
CHECK_EQ(pm.tool_calls[0].arguments, std::string("{\"city\":\"Toronto\"}"));
}
TEST(hyv3_plugin_format_splits_requested_reasoning_before_native_tools) {
std::shared_ptr<Vocab> v = vocab_with(fixture("hyv3"));
REQUIRE(v != nullptr);
RadChatFormat c = hyv3_abi();
server::ChatTemplateBridge br;
CHECK_OK(br.init(*v, &c, "hyv3_bf16"));
const json msgs = json::array({ { {"role", "user"}, {"content", "List the files."} } });
server::ChatRenderOptions opt;
opt.template_kwargs["reasoning_effort"] = json("high").dump();
server::ChatRender out;
std::string why;
CHECK_OK(br.apply(msgs, agent_tools(), opt, &out, &why));
REQUIRE(out.parser != nullptr);
CHECK(out.generation_prompt.ends_with("<think>"));
auto rd = out.parser->open();
rd->feed("Plan.</think>Checking.<tool_calls><tool_call>bash<tool_sep>"
"<arg_key>command</arg_key>\n<arg_value>ls</arg_value>"
"</tool_call></tool_calls>", nullptr);
rd->finish(nullptr);
const server::ParsedMessage m = rd->message();
CHECK_EQ(m.reasoning, std::string("Plan."));
CHECK_EQ(m.content, std::string("Checking."));
REQUIRE(m.tool_calls.size() == 1);
CHECK_EQ(m.tool_calls[0].name, std::string("bash"));
}
/* GLM-5.3-Flash's template reads list items as `m.content.0.type`, jinja2's dotted index, and
* writes calls as `<tool_call>NAME<arg_key>K</arg_key><arg_value>V</arg_value></tool_call>`. The
* capability probe renders with tools and tool calls, so a runtime that throws on the dotted index
@@ -1579,6 +1668,167 @@ static int object_admits(const std::string& gbnf, const std::string& text) {
return gr->complete() ? 1 : 0;
}
TEST(hyv3_sectioned_tagged_calls_parse_stream_and_constrain) {
RadChatFormat abi = hyv3_abi();
ChatFormat f;
std::string why;
CHECK_OK(chat_format_from_abi(&abi, &f, &why));
CHECK_OK(chat_format_check(f, &why));
const json tools = json::parse(R"([{"type":"function","function":{"name":"lookup_weather",
"parameters":{"type":"object","properties":{"city":{"type":"string"}},"required":["city"]}}},
{"type":"function","function":{"name":"add","parameters":{"type":"object",
"properties":{"a":{"type":"integer"},"b":{"type":"integer"}},"required":["a","b"]}}}])");
ReplyOptions ro;
ro.tools = &tools;
const std::shared_ptr<const ReplyParser> p = parser_of(f, ro);
REQUIRE(p != nullptr);
const std::string reply =
"Checking both results.<tool_calls>\n"
"<tool_call>lookup_weather<tool_sep>\n"
"<arg_key>city</arg_key>\n<arg_value>Québec & Montréal</arg_value>\n"
"</tool_call>\n"
"<tool_call>add<tool_sep>\n"
"<arg_key>a</arg_key>\n<arg_value>20</arg_value>\n"
"<arg_key>b</arg_key>\n<arg_value>22</arg_value>\n"
"</tool_call>\n</tool_calls>";
bool strict = false;
const ReplyMessage m = reply_parse(*p, reply, &strict);
CHECK(strict);
CHECK_EQ(m.content, std::string("Checking both results."));
REQUIRE(m.calls.size() == 2);
CHECK_EQ(m.calls[0].name, std::string("lookup_weather"));
CHECK_EQ(m.calls[0].arguments, std::string("{\"city\":\"Québec & Montréal\"}"));
CHECK(m.calls[0].closed);
CHECK_EQ(m.calls[1].name, std::string("add"));
CHECK_EQ(m.calls[1].arguments, std::string("{\"a\":20,\"b\":22}"));
CHECK(m.calls[1].closed);
CHECK(m.content.find("<tool_calls>") == std::string::npos);
CHECK(m.content.find("</tool_calls>") == std::string::npos);
const std::string stream_error = stream_all(*p, reply);
if (!stream_error.empty()) ::radtest::fail(__FILE__, __LINE__, stream_error);
ChatGrammarRequest gq;
gq.tools = &tools;
ChatGrammar g;
CHECK_OK(chat_format_grammar(f, gq, &g, &why));
CHECK(g.lazy);
CHECK(g.trigger_words == std::vector<std::string>{ "<tool_calls>" });
const size_t section = reply.find("<tool_calls>");
REQUIRE(section != std::string::npos);
CHECK_EQ(object_admits(g.text, reply.substr(section)), 1);
gq.parallel_tool_calls = false;
CHECK_OK(chat_format_grammar(f, gq, &g, &why));
CHECK_EQ(object_admits(g.text, reply.substr(section)), 0);
/* A quoted call tag outside the section remains user-visible content, not an invocation. */
const std::string outside = "Example: <tool_call>add<tool_sep>";
const ReplyMessage quoted = reply_parse(*p, outside, &strict);
CHECK_EQ(quoted.calls.size(), (size_t)0);
CHECK_EQ(quoted.content, outside);
/* A section close cannot turn a call missing its own close tag into an executable call. */
const std::string truncated =
"<tool_calls>\n<tool_call>add<tool_sep>\n"
"<arg_key>a</arg_key>\n<arg_value>20</arg_value>\n</tool_calls>";
const ReplyMessage incomplete = reply_parse(*p, truncated, &strict);
CHECK(!strict);
REQUIRE(incomplete.calls.size() == 1);
CHECK(!incomplete.calls[0].closed);
const std::string truncated_stream_error = stream_all(*p, truncated);
if (!truncated_stream_error.empty())
::radtest::fail(__FILE__, __LINE__, truncated_stream_error);
}
TEST(section_wrapped_calls_do_not_cross_section_boundaries) {
ChatFormat f;
std::string why;
RadChatFormat abi = hyv3_abi();
CHECK_OK(chat_format_from_abi(&abi, &f, &why));
ReplyOptions ro;
ro.tools = &agent_tools();
const auto p = parser_of(f, ro);
REQUIRE(p != nullptr);
const std::string call = "<tool_call>bash<tool_sep>"
"<arg_key>command</arg_key>\n<arg_value>ls</arg_value></tool_call>";
ChatGrammarRequest gq;
gq.tools = &agent_tools();
ChatGrammar g;
CHECK_OK(chat_format_grammar(f, gq, &g, &why));
CHECK_EQ(object_admits(g.text, "<tool_calls>" + call + call + "</tool_calls>"), 1);
/* The inter-call grammar must not swallow an earlier section close as prose. */
CHECK_EQ(object_admits(g.text, "<tool_calls>" + call + "</tool_calls>" +
call + "</tool_calls>"), 0);
/* A wrapped dialect whose call close duplicates call_end has no distinct close scanner.
* Its section close must still abandon a malformed call, not close it as executable. */
f.call_open = "<call>";
f.call_close = f.call_end;
const auto wrapped = parser_of(f, ro);
REQUIRE(wrapped != nullptr);
bool strict = true;
const std::string malformed = "<tool_calls><call><tool_call>bash<tool_sep>junk</tool_calls>";
const ReplyMessage m = reply_parse(*wrapped, malformed, &strict);
CHECK(!strict);
REQUIRE(m.calls.size() == 1);
CHECK(!m.calls[0].closed);
const std::string err = stream_all(*wrapped, malformed);
if (!err.empty()) ::radtest::fail(__FILE__, __LINE__, err);
}
TEST(section_wrapped_calls_stream_consistently_at_every_truncation) {
ChatFormat f;
std::string why;
RadChatFormat abi = hyv3_abi();
CHECK_OK(chat_format_from_abi(&abi, &f, &why));
ReplyOptions ro;
ro.tools = &agent_tools();
const auto p = parser_of(f, ro);
REQUIRE(p != nullptr);
const std::string call = "<tool_call>bash<tool_sep>"
"<arg_key>command</arg_key>\n<arg_value>ls</arg_value></tool_call>";
const std::string reply = "<tool_calls>" + call + call + "</tool_calls>";
for (size_t n = 0; n <= reply.size(); ++n) {
const std::string err = stream_all(*p, reply.substr(0, n), 0);
if (!err.empty()) {
::radtest::fail(__FILE__, __LINE__, "truncated at " + std::to_string(n) + ": " + err);
break;
}
}
const ReplyMessage incomplete = reply_parse(*p, "<tool_calls>" + call);
REQUIRE(incomplete.calls.size() == 1);
CHECK(!incomplete.calls[0].closed);
}
TEST(section_wrapped_calls_cannot_bypass_the_wrapper_in_reasoning) {
ChatFormat f;
std::string why;
RadChatFormat abi = hyv3_abi();
CHECK_OK(chat_format_from_abi(&abi, &f, &why));
f.reasoning_open = "<think>";
f.reasoning_close = "</think>";
f.reasoning_breaks = { f.section_open, f.name_prefix };
ReplyOptions ro;
ro.tools = &agent_tools();
const auto p = parser_of(f, ro);
REQUIRE(p != nullptr);
const std::string call = "<tool_call>bash<tool_sep>"
"<arg_key>command</arg_key>\n<arg_value>ls</arg_value></tool_call>";
const std::string text = "<think>Example: " + call + "</think>Answer.";
const ReplyMessage quoted = reply_parse(*p, text);
CHECK(quoted.calls.empty());
CHECK_EQ(quoted.reasoning, "Example: " + call);
CHECK_EQ(quoted.content, std::string("Answer."));
const std::string err = stream_all(*p, text);
if (!err.empty()) ::radtest::fail(__FILE__, __LINE__, err);
const ReplyMessage actual = reply_parse(*p, "<think>Plan.<tool_calls>" + call +
"</tool_calls>");
REQUIRE(actual.calls.size() == 1);
CHECK(actual.calls[0].closed);
}
TEST(json_arguments_behind_a_name_tag_read_as_one_object) {
std::shared_ptr<const ReplyParser> p = object_format(false);
REQUIRE(p != nullptr);