/* chatparse.h -- reading a model's reply back as it streams: reasoning, content, tool calls. * * ONE PASS, ONE PARSER. A reply is fed as it is generated, in pieces of any size, and every byte * is examined a bounded number of times: the whole reply costs O(its length), whether it arrives * in one piece or one byte at a time. Nothing is re-parsed, nothing is matched with a regular * expression, and there is no second pass that repairs what the first one missed -- the malformed * shapes a model actually writes are states of this machine, not fallbacks around it. * * It is driven by a ChatFormat (chatfmt.h) and built from three pieces: * * * DELIMITER RECOGNITION is a deterministic Aho-Corasick automaton over the delimiters that can * end the current region -- "" and the call opener inside reasoning, the call opener * in content, the value suffix inside a value. In the automaton's root state the scan skips * straight to the next byte that can begin a delimiter (memchr for the common one-byte case), * so a long run of prose or file content costs a memchr and a copy. * * THE TAG STRUCTURE is a transducer over those regions: call opener, name, arguments, close. * * A JSON-TYPED ARGUMENT is recognised by an incremental JSON scanner with an explicit stack. * * WHAT IT PRODUCES is exactly what the format's grammar says the reply means, including the * places where the grammar is lenient on purpose: an argument the schema does not declare is * still an argument, a non-string argument that is not valid JSON is taken as text, a reasoning * block the model never closed ends where a call begins, prose before, between and after calls is * content. Where the reply leaves the grammar altogether -- a call to a function that is not a * tool, text inside a call, a missing close -- it recovers locally and says so (strict()). * * That includes a call written WITHOUT its wrapper. In a format whose calls are wrapped (Qwen's * `\n`) the grammar, and constrained decoding with it, begins only at the wrapper, so a * call the model writes as a bare ``, or with the wrapper run straight into it, is * unconstrained -- and it is the one most likely to be lost. In content, name_prefix alone and * call_open without its trailing whitespace also open a call, with every tag's surrounding * whitespace optional inside it. Because such an opener is also ordinary text, it becomes a call * only once the name tag is closed and an argument or the call's close follows; until then it is * held (a bounded number of bytes), and if it is not a call it is content. * * THE STREAMING CONTRACT: what has been emitted is final. A delta is never taken back, so every * emitted prefix of content, reasoning or arguments is a prefix of the finished value, and the * concatenated deltas ARE the finished values. That forces some bytes to be held until their * meaning is known, and the holds are bounded: * * * a possible delimiter prefix -- at most the longest delimiter, less one byte; * * an unfinished UTF-8 sequence -- at most three bytes, because a delta goes into a JSON string; * * whitespace whose fate depends on what follows -- the trailing whitespace of content, which * is trimmed if nothing follows it, and the whitespace that opens a reasoning block, which is * dropped if the block holds nothing else; * * a tag's own name while it is being read, since a name is only a name once it is closed; * * a JSON-typed argument value until its closing delimiter: `30` and `30 seconds` differ from * their first byte (a number, or the string "30 seconds"), so the value is emitted whole once * its type is known. A value that stops being JSON part-way is streamed as text from there, * except a container (`[`, `{`), which is held to its end: only there is it known whether * it is JSON once its Python quoting is normalised, or the JSON string of its text. * * Streaming emits in the reply's order; a call's arguments are streamed as they are parsed. */ #pragma once #include "rad_internal.h" #include #include #include #include #include #include namespace rad { struct ChatFormat; /* One increment of a parsed reply. Consecutive increments of the same kind are merged. */ struct ReplyEvent { enum Kind : uint8_t { Reasoning, /* text: a reasoning delta */ Content, /* text: a content delta */ CallBegin, /* text: the function name; `call` is the new call's index */ CallArgs, /* text: an arguments delta for call `call` */ CallEnd, /* call `call` closed */ }; Kind kind = Content; uint32_t call = 0; std::string text; }; struct ReplyCall { std::string name; std::string arguments; /* JSON text, as the OpenAI API carries it */ bool closed = false; /* Where the call's text begins in the reply, and the content up to it: `content_at` bytes * of the message's content and the untrimmed whitespace held after them. What the content * is when the call is read as text (ReplyStream::content_with_open_call). */ size_t at = 0; size_t content_at = 0; std::string content_held; }; /* A reply, taken apart. After finish() it is the whole reply; before, what has been emitted. */ struct ReplyMessage { std::string content; std::string reasoning; std::vector calls; }; /* What held bytes are waiting on, by kind. See the header comment for the bound on each. */ struct ReplyHeld { size_t delimiter = 0; /* a possible delimiter prefix, and an unfinished UTF-8 sequence */ size_t space = 0; /* whitespace that may yet be trimmed */ size_t structure = 0; /* a tag name being read, or a JSON value awaiting its type */ }; /* The per-request inputs. */ struct ReplyOptions { /* The assistant prefix the template already rendered. It decides whether the reply starts * inside a reasoning block (ChatFormat::reasoning_in_prompt). */ std::string generation_prompt; /* reasoning_format "none" keeps a reasoning block inline in the content. */ bool extract_reasoning = true; /* The request's tools, OpenAI-shaped; null or empty parses no calls, and call syntax in the * reply is then content. Read once, when the parser is created. */ const nlohmann::ordered_json* tools = nullptr; bool parallel_tool_calls = true; /* A response_format was requested: the reply is one JSON document, optionally fenced as * ```json ... ```, and the document is the content. */ bool response_format = false; /* The model's turn terminators. The decoder renders one only when the format needs it, and * one left on the end of content is not part of the answer. */ std::vector terminators; }; class ReplyStream; /* The reasoning block as one request's reply meets it: its markers with the optional whitespace * around them trimmed, and whether the reply starts inside it -- the generation prompt opened it, * or the format has no opener. Empty `close` is a format with no reasoning block. */ struct ReasoningMarkers { std::string open, close; bool starts_open = false; }; /* A format compiled once: its automata and its delimiters. Shared by every request. */ class ReplyFormat { public: ~ReplyFormat(); /* RAD_OK, or RAD_E_UNSUPPORTED with *why when chat_format_check refuses the format. */ static int compile(const ChatFormat& f, std::shared_ptr* out, std::string* why); const ChatFormat& format() const; struct Impl; const Impl& impl() const { return *impl_; } private: ReplyFormat(); std::unique_ptr impl_; }; /* One request's parser: the format bound to the tools the request sent and the prompt it * rendered. Immutable once made; each choice opens its own stream from it. */ class ReplyParser { public: ~ReplyParser(); /* RAD_OK, or RAD_E_INVAL with *why when a tool cannot be read unambiguously in this format -- * a function or parameter name containing the delimiter that ends it -- and * RAD_E_UNSUPPORTED when the request sends tools and the format has no tool calls. */ static int create(std::shared_ptr fmt, const ReplyOptions& opt, std::shared_ptr* out, std::string* why); std::unique_ptr open() const; const ChatFormat& format() const; /* The most bytes ReplyHeld::delimiter can reach for this request: a delimiter or terminator * prefix in each place one can be pending at once, a value's undecided head, and an * unfinished UTF-8 sequence on each output. A constant of the format, not of the reply. */ size_t hold_bound() const; /* Read from the generation prompt exactly as a stream's first bytes are, so a reply this * says starts in the block is one the stream reads as reasoning from its first byte. */ ReasoningMarkers reasoning() const; struct Impl; const Impl& impl() const { return *impl_; } private: ReplyParser(); std::unique_ptr impl_; }; class ReplyStream { public: ~ReplyStream(); /* Append bytes. The increments they complete are appended to *out (which may be null). */ void feed(std::string_view bytes, std::vector* out); /* The reply is complete. Whatever was held is resolved and emitted. Idempotent. */ void finish(std::vector* out); const ReplyMessage& message() const; /* After finish(): the content were the last call text, when the reply ended inside that call * -- the content before it, then the call as `reply` (every byte fed) spells it, trimmed at * the end as content is. A call that never closed cannot be reported as a call, and its text * is what the model wrote. The message's content when the last call closed, or there is none. */ std::string content_with_open_call(std::string_view reply) const; /* False once the reply left the format's grammar and was read by a recovery rule. */ bool strict() const; ReplyHeld held() const; struct Impl; private: friend class ReplyParser; ReplyStream(); std::unique_ptr impl_; }; /* Parse a complete reply in one call. */ ReplyMessage reply_parse(const ReplyParser& p, std::string_view text, bool* strict = nullptr); } /* namespace rad */