server: fix missing output_index in Responses API SSE events for compatibility (#30863)
* server: include output_index in Responses API SSE events * server: reuse Anthropic streaming code for fix output_index * server: remove unnecessary generated_tool_call_ids and oai_resp_tool_calls_started
This commit is contained in:
1 parent
36140dbee2
commit
1ba568c5ad
1 file changed
+21
@@ -591,8 +591,10 @@ json server_task_result_cmpl_final::to_json_oaicompat_resp() {
|
||||
json server_task_result_cmpl_final::to_json_oaicompat_resp_stream() {
|
||||
std::vector<json> server_sent_events;
|
||||
std::vector<json> output;
|
||||
size_t output_index = 0;
|
||||
|
||||
if (oaicompat_msg.reasoning_content != "") {
|
||||
const size_t item_output_index = output_index++;
|
||||
const json output_item = json {
|
||||
{"id", oai_resp_reasoning_id},
|
||||
{"summary", json::array()},
|
||||
@@ -608,6 +610,7 @@ json server_task_result_cmpl_final::to_json_oaicompat_resp_stream() {
|
||||
{"event", "response.output_item.done"},
|
||||
{"data", json {
|
||||
{"type", "response.output_item.done"},
|
||||
{"output_index", item_output_index},
|
||||
{"item", output_item}
|
||||
}}
|
||||
});
|
||||
@@ -615,11 +618,14 @@ json server_task_result_cmpl_final::to_json_oaicompat_resp_stream() {
|
||||
}
|
||||
|
||||
if (oaicompat_msg.content != "") {
|
||||
const size_t item_output_index = output_index++;
|
||||
server_sent_events.push_back(json {
|
||||
{"event", "response.output_text.done"},
|
||||
{"data", json {
|
||||
{"type", "response.output_text.done"},
|
||||
{"item_id", oai_resp_message_id},
|
||||
{"output_index", item_output_index},
|
||||
{"content_index", 0},
|
||||
{"text", oaicompat_msg.content}
|
||||
}}
|
||||
});
|
||||
@@ -636,6 +642,8 @@ json server_task_result_cmpl_final::to_json_oaicompat_resp_stream() {
|
||||
{"data", json {
|
||||
{"type", "response.content_part.done"},
|
||||
{"item_id", oai_resp_message_id},
|
||||
{"output_index", item_output_index},
|
||||
{"content_index", 0},
|
||||
{"part", content_part}
|
||||
}}
|
||||
});
|
||||
@@ -651,6 +659,7 @@ json server_task_result_cmpl_final::to_json_oaicompat_resp_stream() {
|
||||
{"event", "response.output_item.done"},
|
||||
{"data", json {
|
||||
{"type", "response.output_item.done"},
|
||||
{"output_index", item_output_index},
|
||||
{"item", output_item}
|
||||
}}
|
||||
});
|
||||
@@ -658,6 +667,7 @@ json server_task_result_cmpl_final::to_json_oaicompat_resp_stream() {
|
||||
}
|
||||
|
||||
for (const common_chat_tool_call & tool_call : oaicompat_msg.tool_calls) {
|
||||
const size_t item_output_index = output_index++;
|
||||
const json output_item = {
|
||||
{"id", "fc_" + tool_call.id},
|
||||
{"type", "function_call"},
|
||||
@@ -670,6 +680,7 @@ json server_task_result_cmpl_final::to_json_oaicompat_resp_stream() {
|
||||
{"event", "response.output_item.done"},
|
||||
{"data", json {
|
||||
{"type", "response.output_item.done"},
|
||||
{"output_index", item_output_index},
|
||||
{"item", output_item}
|
||||
}}
|
||||
});
|
||||
@@ -1202,6 +1213,7 @@ json server_task_result_cmpl_partial::to_json_oaicompat_resp() {
|
||||
{"event", "response.output_item.added"},
|
||||
{"data", json {
|
||||
{"type", "response.output_item.added"},
|
||||
{"output_index", 0},
|
||||
{"item", json {
|
||||
{"id", oai_resp_reasoning_id},
|
||||
{"summary", json::array()},
|
||||
@@ -1218,6 +1230,8 @@ json server_task_result_cmpl_partial::to_json_oaicompat_resp() {
|
||||
{"event", "response.reasoning_text.delta"},
|
||||
{"data", json {
|
||||
{"type", "response.reasoning_text.delta"},
|
||||
{"output_index", 0},
|
||||
{"content_index", 0},
|
||||
{"delta", diff.reasoning_content_delta},
|
||||
{"item_id", oai_resp_reasoning_id},
|
||||
}},
|
||||
@@ -1230,6 +1244,7 @@ json server_task_result_cmpl_partial::to_json_oaicompat_resp() {
|
||||
{"event", "response.output_item.added"},
|
||||
{"data", json {
|
||||
{"type", "response.output_item.added"},
|
||||
{"output_index", anthropic_has_reasoning ? 1 : 0},
|
||||
{"item", json {
|
||||
{"content", json::array()},
|
||||
{"id", oai_resp_message_id},
|
||||
@@ -1244,6 +1259,8 @@ json server_task_result_cmpl_partial::to_json_oaicompat_resp() {
|
||||
{"data", json {
|
||||
{"type", "response.content_part.added"},
|
||||
{"item_id", oai_resp_message_id},
|
||||
{"output_index", anthropic_has_reasoning ? 1 : 0},
|
||||
{"content_index", 0},
|
||||
{"part", json {
|
||||
{"type", "output_text"},
|
||||
{"text", ""},
|
||||
@@ -1257,6 +1274,8 @@ json server_task_result_cmpl_partial::to_json_oaicompat_resp() {
|
||||
{"data", json {
|
||||
{"type", "response.output_text.delta"},
|
||||
{"item_id", oai_resp_message_id},
|
||||
{"output_index", anthropic_has_reasoning ? 1 : 0},
|
||||
{"content_index", 0},
|
||||
{"delta", diff.content_delta},
|
||||
}},
|
||||
});
|
||||
@@ -1267,6 +1286,7 @@ json server_task_result_cmpl_partial::to_json_oaicompat_resp() {
|
||||
{"event", "response.output_item.added"},
|
||||
{"data", json {
|
||||
{"type", "response.output_item.added"},
|
||||
{"output_index", (anthropic_has_reasoning ? 1 : 0) + (text_block_started ? 1 : 0) + diff.tool_call_index},
|
||||
{"item", json {
|
||||
{"id", "fc_" + diff.tool_call_delta.id},
|
||||
{"arguments", ""},
|
||||
@@ -1287,6 +1307,7 @@ json server_task_result_cmpl_partial::to_json_oaicompat_resp() {
|
||||
{"type", "response.function_call_arguments.delta"},
|
||||
{"delta", diff.tool_call_delta.arguments},
|
||||
{"item_id", "fc_" + oai_resp_fc_id},
|
||||
{"output_index", (anthropic_has_reasoning ? 1 : 0) + (text_block_started ? 1 : 0) + diff.tool_call_index},
|
||||
}},
|
||||
});
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user