diff --git a/src/common/AutoModel/modeling_lfm2.cpp b/src/common/AutoModel/modeling_lfm2.cpp index 6d6d94bf..f82837cb 100644 --- a/src/common/AutoModel/modeling_lfm2.cpp +++ b/src/common/AutoModel/modeling_lfm2.cpp @@ -7,6 +7,7 @@ #include "AutoModel/modeling_lfm2.hpp" #include "utils/utils.hpp" +#include "utils/tool_calling_utils.hpp" #include "metrices.hpp" /************ LFM2 family **************/ @@ -226,12 +227,7 @@ StreamResult LFM2::parse_stream_content(const std::string content) { value_end++; } value = args_part.substr(pos, value_end - pos); - size_t v_start = value.find_first_not_of(" \t\n\r"); - size_t v_end = value.find_last_not_of(" \t\n\r"); - if (v_start != std::string::npos) { - value = value.substr(v_start, v_end - v_start + 1); - } - args_json[key] = value; + args_json[key] = normalize_tool_argument_value(value); pos = value_end; } } @@ -516,12 +512,7 @@ StreamResult LFM2_5_TK::parse_stream_content(const std::string content) { value_end++; } value = args_part.substr(pos, value_end - pos); - size_t v_start = value.find_first_not_of(" \t\n\r"); - size_t v_end = value.find_last_not_of(" \t\n\r"); - if (v_start != std::string::npos) { - value = value.substr(v_start, v_end - v_start + 1); - } - args_json[key] = value; + args_json[key] = normalize_tool_argument_value(value); pos = value_end; } } diff --git a/src/include/utils/tool_calling_utils.hpp b/src/include/utils/tool_calling_utils.hpp new file mode 100644 index 00000000..68b73061 --- /dev/null +++ b/src/include/utils/tool_calling_utils.hpp @@ -0,0 +1,47 @@ +#pragma once + +#include +#include +#include +#include + +inline std::string trim_tool_argument_token(const std::string& text) { + size_t start = 0; + while (start < text.size() && std::isspace(static_cast(text[start]))) { + start++; + } + + size_t end = text.size(); + while (end > start && std::isspace(static_cast(text[end - 1]))) { + end--; + } + + return text.substr(start, end - start); +} + +inline nlohmann::ordered_json normalize_tool_argument_value(const std::string& value_text) { + std::string trimmed = trim_tool_argument_token(value_text); + if (trimmed.empty()) { + return ""; + } + + if (trimmed.size() >= 2 && trimmed.front() == '\'' && trimmed.back() == '\'') { + return trimmed.substr(1, trimmed.size() - 2); + } + + try { + return nlohmann::ordered_json::parse(trimmed); + } + catch (...) { + return trimmed; + } +} + +inline nlohmann::ordered_json normalize_tool_arguments(const std::string& arguments_text) { + try { + return nlohmann::ordered_json::parse(arguments_text); + } + catch (...) { + return arguments_text; + } +} \ No newline at end of file