1#include "chat-peg-parser.h"
  2
  3#include <nlohmann/json.hpp>
  4
  5using json = nlohmann::json;
  6
  7static std::string_view trim_trailing_space(std::string_view sv, int max = -1) {
  8    int count = 0;
  9    while (!sv.empty() && std::isspace(static_cast<unsigned char>(sv.back()))) {
 10        if (max != -1 && count <= max) {
 11            break;
 12        }
 13        sv.remove_suffix(1);
 14        count++;
 15    }
 16    return sv;
 17}
 18
 19void common_chat_peg_mapper::from_ast(const common_peg_ast_arena & arena, const common_peg_parse_result & result) {
 20    arena.visit(result, [this](const common_peg_ast_node & node) {
 21        map(node);
 22    });
 23}
 24
 25void common_chat_peg_mapper::map(const common_peg_ast_node & node) {
 26    bool is_reasoning = node.tag == common_chat_peg_builder::REASONING;
 27    bool is_content = node.tag == common_chat_peg_builder::CONTENT;
 28
 29    if (is_reasoning) {
 30        result.reasoning_content = std::string(trim_trailing_space(node.text));
 31    }
 32
 33    if (is_content) {
 34        result.content = std::string(trim_trailing_space(node.text));
 35    }
 36}
 37
 38void common_chat_peg_native_mapper::map(const common_peg_ast_node & node) {
 39    common_chat_peg_mapper::map(node);
 40
 41    bool is_tool_open = node.tag == common_chat_peg_native_builder::TOOL_OPEN;
 42    bool is_tool_name = node.tag == common_chat_peg_native_builder::TOOL_NAME;
 43    bool is_tool_id = node.tag == common_chat_peg_native_builder::TOOL_ID;
 44    bool is_tool_args = node.tag == common_chat_peg_native_builder::TOOL_ARGS;
 45
 46    if (is_tool_open) {
 47        result.tool_calls.emplace_back();
 48        current_tool = &result.tool_calls.back();
 49    }
 50
 51    if (is_tool_id && current_tool) {
 52        current_tool->id = std::string(trim_trailing_space(node.text));
 53    }
 54
 55    if (is_tool_name && current_tool) {
 56        current_tool->name = std::string(trim_trailing_space(node.text));
 57    }
 58
 59    if (is_tool_args && current_tool) {
 60        current_tool->arguments = std::string(trim_trailing_space(node.text));
 61    }
 62}
 63
 64void common_chat_peg_constructed_mapper::map(const common_peg_ast_node & node) {
 65    common_chat_peg_mapper::map(node);
 66
 67    bool is_tool_open = node.tag == common_chat_peg_constructed_builder::TOOL_OPEN;
 68    bool is_tool_name = node.tag == common_chat_peg_constructed_builder::TOOL_NAME;
 69    bool is_tool_close = node.tag == common_chat_peg_constructed_builder::TOOL_CLOSE;
 70    bool is_arg_open = node.tag == common_chat_peg_constructed_builder::TOOL_ARG_OPEN;
 71    bool is_arg_close = node.tag == common_chat_peg_constructed_builder::TOOL_ARG_CLOSE;
 72    bool is_arg_name = node.tag == common_chat_peg_constructed_builder::TOOL_ARG_NAME;
 73    bool is_arg_string = node.tag == common_chat_peg_constructed_builder::TOOL_ARG_STRING_VALUE;
 74    bool is_arg_json = node.tag == common_chat_peg_constructed_builder::TOOL_ARG_JSON_VALUE;
 75
 76    if (is_tool_open) {
 77        result.tool_calls.emplace_back();
 78        current_tool = &result.tool_calls.back();
 79        arg_count = 0;
 80    }
 81
 82    if (is_tool_name) {
 83        current_tool->name = std::string(node.text);
 84        current_tool->arguments = "{";
 85    }
 86
 87    if (is_arg_open) {
 88        needs_closing_quote = false;
 89    }
 90
 91    if (is_arg_name && current_tool) {
 92        if (arg_count > 0) {
 93            current_tool->arguments += ",";
 94        }
 95        current_tool->arguments += json(trim_trailing_space(node.text)).dump() + ":";
 96        ++arg_count;
 97    }
 98
 99    if (is_arg_string && current_tool) {
100        // Serialize to JSON, but exclude the end quote
101        std::string dumped = json(trim_trailing_space(node.text)).dump();
102        current_tool->arguments += dumped.substr(0, dumped.size() - 1);
103        needs_closing_quote = true;
104    }
105
106    if (is_arg_close && current_tool) {
107        if (needs_closing_quote) {
108            current_tool->arguments += "\"";
109            needs_closing_quote = false;
110        }
111    }
112
113    if (is_arg_json && current_tool) {
114        current_tool->arguments += std::string(trim_trailing_space(node.text));
115    }
116
117    if (is_tool_close && current_tool) {
118        if (needs_closing_quote) {
119            current_tool->arguments += "\"";
120            needs_closing_quote = false;
121        }
122        current_tool->arguments += "}";
123    }
124}