Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions cactus-engine/src/chat_tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,11 @@ inline std::string py_literal_to_json(const std::string& s, size_t& p) {
++p; bool first = true;
while (true) {
skip_ws(s, p);
if (p >= s.size() || s[p] == close) { if (p < s.size()) ++p; break; }
// A mismatched closing bracket must not leave the parser stuck.
if (p >= s.size() || s[p] == close || s[p] == ')' || s[p] == ']' || s[p] == '}') {
if (p < s.size() && s[p] == close) ++p;
break;
}
if (s[p] == ',') { ++p; continue; }
if (!first) out += ", ";
first = false;
Expand All @@ -210,7 +214,8 @@ inline std::string py_literal_to_json(const std::string& s, size_t& p) {
if (s.compare(p, 4, "None") == 0) { p += 4; return "null"; }
size_t start = p;
while (p < s.size() && s[p] != ',' && s[p] != ')' && s[p] != ']' && s[p] != '}') ++p;
return trim(s.substr(start, p - start));
std::string v = trim(s.substr(start, p - start));
return v.empty() ? "null" : v;
}

inline void extract_lfm2_tool_calls(std::string& text, std::vector<std::string>& calls) {
Expand Down Expand Up @@ -239,13 +244,15 @@ inline void extract_lfm2_tool_calls(std::string& text, std::vector<std::string>&
while (true) {
skip_ws(inner, p);
if (p >= inner.size() || inner[p] == ')') { if (p < inner.size()) ++p; break; }
if (inner[p] == ',') { ++p; continue; }
// Skip unexpected closing brackets between arguments.
if (inner[p] == ',' || inner[p] == '}' || inner[p] == ']') { ++p; continue; }
size_t ks = p;
while (p < inner.size() && (std::isalnum(static_cast<unsigned char>(inner[p])) || inner[p] == '_')) ++p;
std::string key = inner.substr(ks, p - ks);
skip_ws(inner, p);
if (p < inner.size() && inner[p] == '=') ++p;
std::string val = py_literal_to_json(inner, p);
if (p == ks) { ++p; continue; } // Always advance on malformed input.
if (!first) args += ", ";
first = false;
args += "\"" + key + "\": " + val;
Expand Down
45 changes: 45 additions & 0 deletions cactus-engine/tests/test_chat_tools_823.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include "../src/chat_tools.h"
#include <iostream>
#include <string>
#include <vector>

int main() {
struct Case {
const char* input;
const char* expected;
};

const std::vector<Case> cases = {
{R"([get_weather(city="Paris", options={"units": "metric"})])",
R"({"name": "get_weather", "arguments": {"city": "Paris", "options": {"units": "metric"}}})"},

{R"([get_weather(city="Paris", options={"units": "metric")])",
R"({"name": "get_weather", "arguments": {"city": "Paris", "options": {"units": "metric"}}})"},

{R"([set_alarm(times=["07:00", "07:30"})])",
R"({"name": "set_alarm", "arguments": {"times": ["07:00", "07:30"]}})"},

{R"([get_weather(city="Paris"})])",
R"({"name": "get_weather", "arguments": {"city": "Paris"}})"}
};

for (size_t i = 0; i < cases.size(); ++i) {
std::string text =
std::string("<|tool_call_start|>") +
cases[i].input + "<|tool_call_end|>";

std::vector<std::string> calls;
chat_tools::extract_lfm2_tool_calls(text, calls);

if (calls.size() != 1 ||
calls[0] != cases[i].expected ||
!text.empty()) {
std::cerr << "FAIL case " << i + 1 << "\n";
return 1;
}

std::cout << "PASS case " << i + 1 << "\n";
}

return 0;
}