Repository: ggerganov/llama.cpp
File: tools/server/server-common.cpp (C++)

Code before:
                //     item.at("status") == "incomplete") &&
                // item["status"] not sent by codex-cli
                exists_and_is_string(item, "type") &&
                item.at("type") == "message"
            ) {
                // #responses_create-input-input_item_list-item-output_message
                std::vector<json> chatcmpl_content;

                for (const auto & output_text : item.at("content")) {
                    const std::string type = json_value(output_text, "type", std::string());
                    if (type != "output_text") {
                        throw std::invalid_argument("'type' must be 'output_text'");
                    }
                    if (!exists_and_is_string(output_text, "text")) {
                        throw std::invalid_argument("'Output text' requires 'text'");
                    }
                    // Ignore annotations and logprobs for now
                    chatcmpl_content.push_back({
                        {"text", output_text.at("text")},
                        {"type", "text"},
                    });
                }

                if (should_merge()) {
                    auto & prev_msg = chatcmpl_messages.back();
                    prev_msg["content"] = chatcmpl_content;
                } else {
                    item.erase("status");
                    item.erase("type");
                    item["content"] = chatcmpl_content;
                    chatcmpl_messages.push_back(item);
                }
            } else if (exists_and_is_string(item, "arguments") &&
                exists_and_is_string(item, "call_id") &&
                exists_and_is_string(item, "name") &&
                exists_and_is_string(item, "type") &&
                item.at("type") == "function_call"
            ) {
                // #responses_create-input-input_item_list-item-function_tool_call
                json tool_call = {
                    {"function", json {
                        {"arguments", item.at("arguments")},
                        {"name",      item.at("name")},
                    }},
                    {"id",   item.at("call_id")},
                    {"type", "function"},
                };

                if (should_merge()) {
                    auto & prev_msg = chatcmpl_messages.back();
                    if (!exists_and_is_array(prev_msg, "tool_calls")) {

Review comment (suggestion):
`=` or `+=`? Any idea if we can have successive blocks of pure text? (or text/reasoning without tool calls, for that matter)

Code after:
                item.at("type") == "message"
            ) {
                // #responses_create-input-input_item_list-item-output_message
                auto chatcmpl_content = json::array();

                for (const auto & output_text : item.at("content")) {
                    const std::string type = json_value(output_text, "type", std::string());
                    if (type != "output_text") {
                        throw std::invalid_argument("'type' must be 'output_text'");
                    }
                    if (!exists_and_is_string(output_text, "text")) {
                        throw std::invalid_argument("'Output text' requires 'text'");
                    }
                    // Ignore annotations and logprobs for now
                    chatcmpl_content.push_back({
                        {"text", output_text.at("text")},
                        {"type", "text"},
                    });
                }

                if (merge_prev) {
                    auto & prev_msg = chatcmpl_messages.back();
                    if (!exists_and_is_array(prev_msg, "content")) {
                        prev_msg["content"] = json::array();
                    }
                    auto & prev_content = prev_msg["content"];
                    prev_content.insert(prev_content.end(), chatcmpl_content.begin(), chatcmpl_content.end());
                } else {
                    item.erase("status");
                    item.erase("type");
                    item["content"] = chatcmpl_content;
                    chatcmpl_messages.push_back(item);
                }
            } else if (exists_and_is_string(item, "arguments") &&
                exists_and_is_string(item, "call_id") &&
                exists_and_is_string(item, "name") &&
                exists_and_is_string(item, "type") &&
                item.at("type") == "function_call"
            ) {
                // #responses_create-input-input_item_list-item-function_tool_call
                json tool_call = {
                    {"function", json {
                        {"arguments", item.at("arguments")},
                        {"name",      item.at("name")},
                    }},
                    {"id",   item.at("call_id")},
                    {"type", "function"},
                };

                if (merge_prev) {
                    auto & prev_msg = chatcmpl_messages.back();