diff --git a/abi/rad_builder.h b/abi/rad_builder.h index 1dd92868..6f06e25a 100644 --- a/abi/rad_builder.h +++ b/abi/rad_builder.h @@ -1526,6 +1526,12 @@ int rad_arch_probe(const RadModelMeta* meta, RadArchProbe* out); * arg_prefix ARG arg_name_suffix arg_value_prefix VALUE arg_value_suffix ... * call_end [call_close] * + * An argument's tag may also say its value's type, with arg_json_value_prefix written in place of + * arg_value_prefix before a JSON value. DeepSeek-V4.1's DSML, inside a section + * "<|DSML| calls>" / "", is "<|DSML| invoke name=\"" / "\">\n" / "", + * arguments "<|DSML| parameter name=\"" / "\"" / " string=\"true\">" (or + * " string=\"false\">") / "". + * * Qwen3.8, for example, is "\n" / "\n\n\n", calls "\n" / "", * "\n" / "\n", arguments "\n" / "" / "\n". * Whitespace at the ends of a tag is optional when matching. Every string is NUL-terminated @@ -1626,6 +1632,12 @@ typedef struct RadChatFormat { * calls) and is read as reasoning there too, and a reasoning_close with no block open is not * part of the answer. Zero: only a block the reply starts with is reasoning. */ uint32_t reasoning_anywhere; + + /* Tagged arguments whose tag says the value's type: written in place of arg_value_prefix, + * this opens a JSON value, and arg_value_prefix then opens a string. The tag the reply writes + * decides how its value is read, whatever the schema says; the grammar writes the one the + * schema calls for. NULL: the schema alone types a value. */ + const char* arg_json_value_prefix; } RadChatFormat; /* Return the format, or NULL for "derive it from the chat template". Called once at startup. */ diff --git a/core/text/chatfmt.cpp b/core/text/chatfmt.cpp index ad7c2301..bcb0ee0a 100644 --- a/core/text/chatfmt.cpp +++ b/core/text/chatfmt.cpp @@ -163,8 +163,8 @@ int chat_format_check(const ChatFormat& f, std::string* why) { for (const std::string* s : { &f.section_open, &f.section_close, &f.call_separator, &f.call_open, &f.call_close, &f.name_suffix, &f.call_end, &f.arg_prefix, &f.arg_name_suffix, &f.arg_value_prefix, - &f.arg_value_suffix, &f.object_string_open, - &f.object_string_close }) + &f.arg_json_value_prefix, &f.arg_value_suffix, + &f.object_string_open, &f.object_string_close }) if (!s->empty()) return no("tool-call delimiters are set but name_prefix is empty"); if (f.object_args() || f.object_bare_keys) return no("an argument object is described but name_prefix is empty"); @@ -235,9 +235,24 @@ int chat_format_check(const ChatFormat& f, std::string* why) { f.object_bare_keys) return no("the object_* fields describe an argument object, and args_form is " "RAD_CHAT_ARGS_TAGGED"); + if (f.typed_tags()) { + /* The two tags are told apart where the name ends: one may not begin the other. */ + if (f.arg_value_prefix.empty()) + return no("arg_json_value_prefix is set and arg_value_prefix, the string tag, is " + "empty"); + const std::string s = f.arg_name_suffix + f.arg_value_prefix; + const std::string j = f.arg_name_suffix + f.arg_json_value_prefix; + if (confusable(s, j)) + return no("arg_value_prefix " + show(f.arg_value_prefix) + + " and arg_json_value_prefix " + show(f.arg_json_value_prefix) + + " cannot be told apart"); + if (f.other_value != ChatValue::Json) + return no("arg_json_value_prefix opens a JSON value, and other_value is not " + "RAD_CHAT_VALUE_JSON"); + } } else { for (const std::string* s : { &f.arg_prefix, &f.arg_name_suffix, &f.arg_value_prefix, - &f.arg_value_suffix }) + &f.arg_json_value_prefix, &f.arg_value_suffix }) if (!s->empty()) return no("the arg_* fields describe tagged arguments, and args_form is " "RAD_CHAT_ARGS_OBJECT"); @@ -337,6 +352,7 @@ int chat_format_from_abi(const RadChatFormat* in, ChatFormat* out, std::string* f.object_string_close = s(c.object_string_close); f.object_bare_keys = c.object_bare_keys != 0; f.reasoning_anywhere = c.reasoning_anywhere != 0; + f.arg_json_value_prefix = s(c.arg_json_value_prefix); *out = std::move(f); return RAD_OK; } @@ -464,6 +480,7 @@ std::string chat_format_describe(const ChatFormat& f) { } else if (f.has_tools()) { s += "; calls " + show(f.call_open) + show(f.name_prefix) + "NAME" + show(f.name_suffix) + " " + show(f.arg_prefix) + "ARG" + show(f.arg_name_suffix) + show(f.arg_value_prefix) + + (f.typed_tags() ? "|" + show(f.arg_json_value_prefix) : std::string()) + "VALUE" + show(f.arg_value_suffix) + " " + show(f.call_end) + show(f.call_close); } if (f.has_tools()) { @@ -484,8 +501,9 @@ std::vector chat_format_markers(const ChatFormat& f) { for (const std::string* s : { &f.reasoning_open, &f.reasoning_close, &f.section_open, &f.section_close, &f.call_open, &f.call_close, &f.name_prefix, &f.name_suffix, &f.call_end, &f.arg_prefix, &f.arg_name_suffix, - &f.arg_value_prefix, &f.arg_value_suffix, - &f.object_string_open, &f.object_string_close }) { + &f.arg_value_prefix, &f.arg_json_value_prefix, + &f.arg_value_suffix, &f.object_string_open, + &f.object_string_close }) { const std::string t = trim_ws(*s); if (!t.empty() && std::find(out.begin(), out.end(), t) == out.end()) out.push_back(t); } @@ -878,6 +896,9 @@ int chat_format_grammar(const ChatFormat& f, const ChatGrammarRequest& req, Chat * refused there by name. */ if (!required.count(pname) && info.unsatisfiable(pschema)) continue; G value; + /* A format whose tag types the value writes the JSON tag before a JSON + * value; the string tag otherwise. */ + const std::string* vprefix = &f.arg_value_prefix; if (f.other_value != ChatValue::Json || info.resolves_to_string(pschema)) { value = wrap(rule_ref("until-suffix")); } else { @@ -885,10 +906,11 @@ int chat_format_grammar(const ChatFormat& f, const ChatGrammarRequest& req, Chat const std::string sch = b.add_schema( "tool-" + name + "-arg-" + pname + "-schema", pschema); value = alt({ seq({ wrap(leaf(sch)), space() }), wrap(rule_ref("until-suffix")) }); + if (f.typed_tags()) vprefix = &f.arg_json_value_prefix; } G arm = wrap(seq({ wrap(seq({ lit(f.arg_prefix), wrap(lit(pname)), lit(f.arg_name_suffix) })), - lit(f.arg_value_prefix), value, + lit(*vprefix), value, wrap(lit(f.arg_value_suffix)) })); const std::string rule = rule_name("tool-" + name + "-arg-" + pname); b.add_rule(rule, render(arm)); @@ -899,10 +921,13 @@ int chat_format_grammar(const ChatFormat& f, const ChatGrammarRequest& req, Chat /* The generic arm for an undeclared name; not emitted for a tool that declares * no parameters, since it takes no arguments. */ if (!any.empty()) { + G tag = f.typed_tags() ? alt({ lit(f.arg_value_prefix), + lit(f.arg_json_value_prefix) }) + : lit(f.arg_value_prefix); G arm = wrap(seq({ wrap(seq({ lit(f.arg_prefix), wrap(leaf(until_pattern({ name_end }))), lit(f.arg_name_suffix) })), - lit(f.arg_value_prefix), wrap(rule_ref("until-suffix")), + tag, wrap(rule_ref("until-suffix")), wrap(lit(f.arg_value_suffix)) })); const std::string rule = rule_name("tool-" + name + "-arg-undeclared"); b.add_rule(rule, render(arm)); diff --git a/core/text/chatfmt.h b/core/text/chatfmt.h index ba76ce75..ef5867ce 100644 --- a/core/text/chatfmt.h +++ b/core/text/chatfmt.h @@ -129,6 +129,11 @@ struct ChatFormat { * they are then read as one delimiter, written back to back. */ std::string arg_name_suffix; /* ">\n" (MiniCPM: empty, the value prefix ends it) */ std::string arg_value_prefix; /* "" (MiniCPM: "\">") */ + /* A second value prefix, for a format whose argument tag says the value's type (DSML: + * ` string="false">` before JSON, arg_value_prefix ` string="true">` before a string). The + * prefix the reply writes types its value, whatever the schema says; the grammar writes the + * one the schema calls for. Empty: the schema alone types a value. */ + std::string arg_json_value_prefix; std::string arg_value_suffix; /* "\n" */ ChatValue string_value = ChatValue::Cdata; /* a string-typed argument: Raw or Cdata */ @@ -143,6 +148,7 @@ struct ChatFormat { std::vector triggers; bool has_reasoning() const { return !reasoning_close.empty(); } + bool typed_tags() const { return !arg_json_value_prefix.empty(); } bool has_tools() const { return !name_prefix.empty(); } bool object_args() const { return args_form == ChatArgs::Object; } }; diff --git a/core/text/chatparse.cpp b/core/text/chatparse.cpp index 41af41a2..e2f025a5 100644 --- a/core/text/chatparse.cpp +++ b/core/text/chatparse.cpp @@ -729,12 +729,14 @@ struct ReplyFormat::Impl { size_t section_open_hit = (size_t)-1, section_close_hit = (size_t)-1; size_t think_open_hit = (size_t)-1, think_close_hit = (size_t)-1; size_t body_section_close_hit = (size_t)-1; - /* What ends an argument name: arg_name_suffix and arg_value_prefix, back to back. */ - std::string name_end; - /* The same two, whitespace-trimmed. A loose call is one the grammar never opened, so it was + /* What ends an argument name: arg_name_suffix and arg_value_prefix, back to back. With + * typed tags (arg_json_value_prefix) also `name_end_json`, which opens a JSON value; the + * arg_name automata report it as pattern 1. */ + std::string name_end, name_end_json; + /* The same, whitespace-trimmed. A loose call is one the grammar never opened, so it was * written unconstrained; its argument tags, like its name and close, are read with their * surrounding whitespace optional. */ - std::string name_end_core, value_core; + std::string name_end_core, name_end_json_core, value_core; /* call_close's core, when distinct from what else may follow inside a call, so a wrapped * call missing its own close is ended by its wrapper's. */ std::string cc_core; @@ -806,6 +808,7 @@ int ReplyFormat::compile(const ChatFormat& f, std::shared_ptr m.text_markers.push_back(m.section_close_core); } m.name_end = f.arg_name_suffix + f.arg_value_prefix; + if (f.typed_tags()) m.name_end_json = f.arg_name_suffix + f.arg_json_value_prefix; m.object = f.has_tools() && f.object_args(); m.name_open = f.has_tools() && !m.object && f.name_suffix.empty(); if (m.object && f.name_suffix.empty()) { @@ -866,12 +869,18 @@ int ReplyFormat::compile(const ChatFormat& f, std::shared_ptr m.obj_key.build(key); } else if (f.has_tools()) { m.text.build(m.text_markers); - m.arg_name.build({ m.name_end }); - m.value.build({ f.arg_value_suffix }); std::string trail; split_ws(m.name_end, &m.name_end_core, &trail); + split_ws(m.name_end_json, &m.name_end_json_core, &trail); split_ws(f.arg_value_suffix, &m.value_core, &trail); - m.arg_name_loose.build({ m.name_end_core }); + if (f.typed_tags()) { + m.arg_name.build({ m.name_end, m.name_end_json }); + m.arg_name_loose.build({ m.name_end_core, m.name_end_json_core }); + } else { + m.arg_name.build({ m.name_end }); + m.arg_name_loose.build({ m.name_end_core }); + } + m.value.build({ f.arg_value_suffix }); m.value_loose.build({ m.value_core }); split_ws(f.call_close, &m.cc_core, &trail); auto apart = [](const std::string& a, const std::string& b) { @@ -890,7 +899,8 @@ int ReplyFormat::compile(const ChatFormat& f, std::shared_ptr &f.section_close, &f.call_open, &f.call_close, &f.name_prefix, &f.name_suffix, &f.call_end, &f.arg_prefix, &m.name_end, - &f.arg_value_suffix, &f.object_string_open, + &m.name_end_json, &f.arg_value_suffix, + &f.object_string_open, &f.object_string_close }; for (const std::string* s : delims) m.longest = std::max(m.longest, s->size()); for (const std::string& s : f.reasoning_breaks) m.longest = std::max(m.longest, s.size()); @@ -991,12 +1001,15 @@ static int bind_tools(const ReplyFormat::Impl& F, const ojson& tools, ReplyParse /* A name its own delimiter could end early would be read as a different name. * Object keys are delimited by the object's punctuation instead, and a key * containing it is written as a string. */ - const std::string probe = pname + F.name_end; - if (!F.object && probe.find(F.name_end) != pname.size()) { - *why = "parameter '" + pname + "' of tool '" + t.name + "' contains '" + - F.name_end + "', which this model's tool-call format uses to end an " - "argument name"; - return RAD_E_INVAL; + for (const std::string* ne : { &F.name_end, &F.name_end_json }) { + if (ne->empty()) continue; + const std::string probe = pname + *ne; + if (!F.object && probe.find(*ne) != pname.size()) { + *why = "parameter '" + pname + "' of tool '" + t.name + "' contains '" + + *ne + "', which this model's tool-call format uses to end an " + "argument name"; + return RAD_E_INVAL; + } } t.params.emplace(pname, p); } @@ -1189,6 +1202,8 @@ struct ReplyStream::Impl { int depth = 0; /* unclosed braces in the arguments so far */ bool in_str = false, esc = false; const Param* param = nullptr; + /* The argument's tag was the JSON one (typed tags): its value is read as JSON. */ + bool json_tag = false; /* a value */ ValueOut val; ValueOut::Mode vmode = ValueOut::String; @@ -1957,7 +1972,9 @@ struct ReplyStream::Impl { args_out(key); ++argc; name.clear(); - if (param && param->typed) { + /* With typed tags the reply's own tag says JSON or string, for any name; else the + * declared schema does. */ + if (F.f.typed_tags() ? json_tag : (param && param->typed)) { js.reset(); vheld.clear(); st = St::ValJson; @@ -1968,7 +1985,10 @@ struct ReplyStream::Impl { /* What ends an argument name and a value in the call being read: the tags as the format * writes them, or their cores in a loose call. */ - const std::string& name_end() const { return loose ? F.name_end_core : F.name_end; } + const std::string& name_end() const { + if (json_tag) return loose ? F.name_end_json_core : F.name_end_json; + return loose ? F.name_end_core : F.name_end; + } const std::string& value_suffix() const { return loose ? F.value_core : F.f.arg_value_suffix; } /* A CDATA section is punctuation around any value the format lets carry one, typed or not. */ @@ -2221,11 +2241,15 @@ struct ReplyStream::Impl { const char c = *p; att.push_back(c); lhold.push_back(c); - const std::string& ne = F.name_end_core; if (lhold.size() > kMaxLooseName) { fail_attempt(); return p + 1; } - if (lhold.size() >= ne.size() && - lhold.compare(lhold.size() - ne.size(), ne.size(), ne) == 0) { - const std::string_view nm(lhold.data(), lhold.size() - ne.size()); + const std::string* ne = &F.name_end_core; + auto ends = [&](const std::string& s) { + return !s.empty() && lhold.size() >= s.size() && + lhold.compare(lhold.size() - s.size(), s.size(), s) == 0; + }; + if (!ends(*ne) && ends(F.name_end_json_core)) ne = &F.name_end_json_core; + if (ends(*ne)) { + const std::string_view nm(lhold.data(), lhold.size() - ne->size()); if (nm.empty() || nm.find('<') != std::string_view::npos) { fail_attempt(); return p + 1; @@ -2278,6 +2302,7 @@ struct ReplyStream::Impl { name.append(q, n); if (loose && std::memchr(q, '<', n)) name_lt = true; }); + json_tag = hit == 1; if (name_lt) { end_loose_call_at_arg(hit >= 0); return p; } if (hit >= 0) { name += name_end(); begin_value(); } return p; diff --git a/spec.md b/spec.md index debdb878..696187e8 100644 --- a/spec.md +++ b/spec.md @@ -1401,7 +1401,9 @@ derived from it: the reply parser, the lazy tool-call grammar and its trigger wo tokens the decoder must render as text. There are two families of call syntax. In the tagged family each argument has its own tags (Qwen's -``, MiniCPM's ``) and is typed by the tool's schema. In the +``, MiniCPM's ``) and is typed by the tool's schema, or by its +own tag where the format's tag says the type (DeepSeek-V4.1's DSML writes `string="true"` before +text and `string="false"` before JSON; the grammar writes the tag the schema calls for). In the object family a call is its name and one object holding the arguments: JSON, or a dialect with strings between declared delimiters and bare keys, as Gemma 4 writes `call:NAME{path:<|"|>/a<|"|>,limit:3}`; values carry their own types. diff --git a/tests/abi_layout_rows.h b/tests/abi_layout_rows.h index 7fad6e4d..3d03886d 100644 --- a/tests/abi_layout_rows.h +++ b/tests/abi_layout_rows.h @@ -248,6 +248,7 @@ ABI_FIELD(RadChatFormat, object_string_open) ABI_FIELD(RadChatFormat, object_string_close) ABI_FIELD(RadChatFormat, object_bare_keys) ABI_FIELD(RadChatFormat, reasoning_anywhere) +ABI_FIELD(RadChatFormat, arg_json_value_prefix) ABI_STRUCT(RadConstraint) ABI_FIELD(RadConstraint, key) ABI_FIELD(RadConstraint, op) diff --git a/tests/chatparse_test.cpp b/tests/chatparse_test.cpp index 5f70e68a..cbe6a4c5 100644 --- a/tests/chatparse_test.cpp +++ b/tests/chatparse_test.cpp @@ -1829,6 +1829,196 @@ TEST(section_wrapped_calls_cannot_bypass_the_wrapper_in_reasoning) { CHECK(actual.calls[0].closed); } +/* ================================================================== typed argument tags + * + * A format whose argument tag says the value's type (arg_json_value_prefix): the tag the reply + * writes decides JSON or string for any name, declared or not, and the grammar writes the tag the + * schema calls for. Shown on an ASCII dialect, so the grammar can be run over the reply, and on + * DeepSeek-V4.1's declaration with the bytes its own encoder writes. */ + +static ChatFormat typed_tags_format() { + ChatFormat f; + f.name = "typed"; + f.reasoning_open = ""; + f.reasoning_close = ""; + f.reasoning_breaks = { "" }; + f.section_open = ""; + f.section_close = ""; + f.name_prefix = "\n"; + f.call_end = ""; + f.arg_prefix = ""; + f.arg_json_value_prefix = " type=\"json\">"; + f.arg_value_suffix = ""; + f.string_value = ChatValue::Raw; + f.other_value = ChatValue::Json; + f.value_newlines = false; + f.triggers = { "" }; + return f; +} + +static const json& typed_tags_tools() { + static const json t = json::parse(R"([{"type":"function","function":{"name":"run", + "parameters":{"type":"object","properties":{"cmd":{"type":"string"},"timeout":{"type":"integer"}, + "env":{"type":"object"}},"required":["cmd"]}}}])"); + return t; +} + +TEST(typed_argument_tags_say_how_each_value_is_read) { + const ChatFormat f = typed_tags_format(); + std::string why; + CHECK_OK(chat_format_check(f, &why)); + ReplyOptions ro; + ro.tools = &typed_tags_tools(); + const auto p = parser_of(f, ro); + REQUIRE(p != nullptr); + + const std::string reply = + "Running it.\n\n\n" + "\n" + "ls -la \"/tmp\"\n" + "30\n" + "{\"A\": [1, true]}\n" + "\n" + "\n" + "pwd\n" + "\n"; + bool strict = false; + const ReplyMessage m = reply_parse(*p, reply, &strict); + CHECK(strict); + CHECK_EQ(m.content, std::string("Running it.")); + REQUIRE(m.calls.size() == 2); + CHECK_EQ(m.calls[0].name, std::string("run")); + CHECK_EQ(m.calls[0].arguments, + std::string("{\"cmd\":\"ls -la \\\"/tmp\\\"\",\"timeout\":30,\"env\":{\"A\": [1, true]}}")); + CHECK_EQ(m.calls[1].arguments, std::string("{\"cmd\":\"pwd\"}")); + CHECK(m.calls[0].closed && m.calls[1].closed); + std::string err = stream_all(*p, reply); + if (!err.empty()) ::radtest::fail(__FILE__, __LINE__, err); + for (size_t n = 0; n <= reply.size(); ++n) { + err = stream_all(*p, reply.substr(0, n), 0); + if (!err.empty()) { + ::radtest::fail(__FILE__, __LINE__, "truncated at " + std::to_string(n) + ": " + err); + break; + } + } + + /* The tag, not the schema: a number written as text is a string, a string written as JSON + * is that JSON, an undeclared name written as JSON is JSON, and JSON that does not parse is + * its text. */ + const std::string other = + "\n\n" + "\"echo\"\n" + "30\n" + "[1, 2]\n" + "soon\n" + "\n"; + const ReplyMessage o = reply_parse(*p, other, &strict); + REQUIRE(o.calls.size() == 1); + CHECK_EQ(o.calls[0].arguments, + std::string("{\"cmd\":\"echo\",\"timeout\":\"30\",\"extra\":[1, 2],\"env\":\"soon\"}")); + CHECK(server::json_complete(o.calls[0].arguments)); + err = stream_all(*p, other); + if (!err.empty()) ::radtest::fail(__FILE__, __LINE__, err); + + /* Without the JSON tag the same dialect is typed by the schema, as before. */ + ChatFormat untyped = f; + untyped.arg_json_value_prefix.clear(); + const auto u = parser_of(untyped, ro); + REQUIRE(u != nullptr); + const ReplyMessage um = reply_parse(*u, + "\n\nls\n" + "30\n\n"); + REQUIRE(um.calls.size() == 1); + CHECK_EQ(um.calls[0].arguments, std::string("{\"cmd\":\"ls\",\"timeout\":30}")); +} + +TEST(typed_argument_tags_constrain_each_argument_to_its_schemas_tag) { + const ChatFormat f = typed_tags_format(); + std::string why; + ChatGrammarRequest gq; + gq.tools = &typed_tags_tools(); + ChatGrammar g; + CHECK_OK(chat_format_grammar(f, gq, &g, &why)); + CHECK(g.lazy); + CHECK(g.trigger_words == std::vector{ "" }); + const std::string call = "\n\n" + "ls\n" + "30\n" + "\n"; + CHECK_EQ(object_admits(g.text, call), 1); + /* A declared JSON argument is not written with the text tag, nor a string with the JSON tag: + * the grammar's arm for each name writes the tag its schema calls for. */ + const size_t cmd_rule = g.text.find("tool-run-arg-cmd ::="); + const size_t timeout_rule = g.text.find("tool-run-arg-timeout ::="); + REQUIRE(cmd_rule != std::string::npos && timeout_rule != std::string::npos); + const std::string cmd_line = g.text.substr(cmd_rule, g.text.find('\n', cmd_rule) - cmd_rule); + const std::string timeout_line = + g.text.substr(timeout_rule, g.text.find('\n', timeout_rule) - timeout_rule); + CHECK(cmd_line.find("type=\\\"text\\\">") != std::string::npos); + CHECK(cmd_line.find("type=\\\"json\\\">") == std::string::npos); + CHECK(timeout_line.find("type=\\\"json\\\">") != std::string::npos); + CHECK(timeout_line.find("type=\\\"text\\\">") == std::string::npos); + /* An undeclared name takes either tag. */ + CHECK_EQ(object_admits(g.text, "\n\n" + "ls\n" + "1\n" + "2\n" + "\n"), 1); +} + +TEST(typed_argument_tags_are_refused_where_they_cannot_be_read) { + auto refused = [](void (*edit)(ChatFormat&), const char* word) { + ChatFormat f = typed_tags_format(); + edit(f); + std::string why; + CHECK(chat_format_check(f, &why) == RAD_E_UNSUPPORTED); + if (why.find(word) == std::string::npos) + ::radtest::fail(__FILE__, __LINE__, std::string("expected '") + word + "' in: " + why); + }; + refused([](ChatFormat& f) { f.arg_value_prefix.clear(); }, "arg_value_prefix"); + refused([](ChatFormat& f) { f.arg_json_value_prefix = f.arg_value_prefix; }, "told apart"); + refused([](ChatFormat& f) { f.arg_json_value_prefix = " type=\"text\">x"; }, "told apart"); + refused([](ChatFormat& f) { f.other_value = ChatValue::Raw; }, "other_value"); + refused([](ChatFormat& f) { + f.name_prefix.clear(); f.name_suffix.clear(); f.call_end.clear(); f.arg_prefix.clear(); + f.arg_name_suffix.clear(); f.arg_value_prefix.clear(); f.arg_value_suffix.clear(); + f.section_open.clear(); f.section_close.clear(); f.reasoning_breaks.clear(); + f.triggers.clear(); + }, "name_prefix is empty"); + + /* A plugin declares it at the end of RadChatFormat; an older plugin's struct ends before the + * field, and it reads as unset. */ + static const char* const kTrig[] = { "" }; + RadChatFormat c{}; + c.struct_size = (uint32_t)sizeof(RadChatFormat); + c.name = "plugin:typed"; + c.section_open = ""; + c.section_close = ""; + c.name_prefix = "\n"; + c.call_end = ""; + c.arg_prefix = ""; + c.arg_json_value_prefix = " type=\"json\">"; + c.arg_value_suffix = ""; + c.string_value = RAD_CHAT_VALUE_RAW; + c.other_value = RAD_CHAT_VALUE_JSON; + c.triggers = kTrig; + c.n_triggers = 1; + ChatFormat g; + std::string why; + CHECK_OK(chat_format_from_abi(&c, &g, &why)); + CHECK_EQ(g.arg_json_value_prefix, std::string(" type=\"json\">")); + CHECK_OK(chat_format_check(g, &why)); + c.struct_size = (uint32_t)offsetof(RadChatFormat, arg_json_value_prefix); + CHECK_OK(chat_format_from_abi(&c, &g, &why)); + CHECK(g.arg_json_value_prefix.empty()); +} + TEST(json_arguments_behind_a_name_tag_read_as_one_object) { std::shared_ptr p = object_format(false); REQUIRE(p != nullptr);