server : preserve anthropic thinking blocks in conversion (#1441)

This commit is contained in:
hksdpc255 2026-03-16 23:59:19 +11:00 committed by GitHub
parent 29e6d6b4c1
commit fe92e30d1e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1250,6 +1250,7 @@ json anthropic_params_from_json(
json tool_calls = json::array();
json converted_content = json::array();
json tool_results = json::array();
std::string reasoning_content;
bool has_tool_calls = false;
for (auto& block : content) {
@ -1258,6 +1259,9 @@ json anthropic_params_from_json(
if (type == "text") {
converted_content.push_back(block);
}
else if (type == "thinking") {
reasoning_content += json_value(block, "thinking", std::string());
}
else if (type == "image") {
json source = json_value(block, "source", json::object());
std::string source_type = json_value(source, "type", std::string());
@ -1318,38 +1322,25 @@ json anthropic_params_from_json(
}
}
if (!tool_results.empty()) {
if (!converted_content.empty() || has_tool_calls) {
json new_msg = { {"role", role} };
if (!converted_content.empty()) {
new_msg["content"] = converted_content;
}
else if (has_tool_calls) {
new_msg["content"] = "";
}
if (!tool_calls.empty()) {
new_msg["tool_calls"] = tool_calls;
}
oai_messages.push_back(new_msg);
if (!converted_content.empty() || has_tool_calls || !reasoning_content.empty()) {
json new_msg = {{"role", role}};
if (!converted_content.empty()) {
new_msg["content"] = converted_content;
}
for (const auto& tool_msg : tool_results) {
oai_messages.push_back(tool_msg);
else if (has_tool_calls || !reasoning_content.empty()) {
new_msg["content"] = "";
}
if (!tool_calls.empty()) {
new_msg["tool_calls"] = tool_calls;
}
if (!reasoning_content.empty()) {
new_msg["reasoning_content"] = reasoning_content;
}
oai_messages.push_back(new_msg);
}
else {
if (!converted_content.empty() || has_tool_calls) {
json new_msg = { {"role", role} };
if (!converted_content.empty()) {
new_msg["content"] = converted_content;
}
else if (has_tool_calls) {
new_msg["content"] = "";
}
if (!tool_calls.empty()) {
new_msg["tool_calls"] = tool_calls;
}
oai_messages.push_back(new_msg);
}
for (const auto& tool_msg : tool_results) {
oai_messages.push_back(tool_msg);
}
}