feat(gateway): persist OpenAI Responses continuation history

This commit is contained in:
ZheFox
2026-07-30 19:26:52 +08:00
parent 1ab4f079c9
commit 118f441029
25 changed files with 1881 additions and 84 deletions
@@ -584,6 +584,14 @@ pub(crate) async fn resolve_local_standard_candidate_payload_parts(
return Ok(None);
}
};
crate::ai_serving::hydrate_openai_response_history(
state.runtime_state(),
body_json,
spec_metadata.api_format,
provider_api_format,
input.auth_context.api_key_id.as_str(),
)
.await?;
let redaction = resolve_provider_chat_pii_redaction(
state,
parts,
@@ -3,7 +3,7 @@ use serde_json::Value;
use crate::ai_serving::transport::apply_standard_provider_request_body_rules_with_request_headers;
use crate::ai_serving::{
apply_openai_responses_compact_special_body_edits,
build_cross_format_openai_responses_request_body_with_model_directives as surface_build_cross_format_openai_responses_request_body,
build_cross_format_openai_responses_request_body_with_model_directives_and_history_scope as surface_build_cross_format_openai_responses_request_body,
build_local_openai_responses_request_body_with_model_directives as surface_build_local_openai_responses_request_body,
GatewayProviderTransportSnapshot,
};
@@ -105,7 +105,7 @@ pub(crate) fn build_cross_format_openai_responses_request_body(
force_body_stream_field: bool,
provider_type: &str,
body_rules: Option<&Value>,
_user_api_key_id: Option<&str>,
user_api_key_id: Option<&str>,
request_headers: &http::HeaderMap,
enable_model_directives: bool,
) -> Option<Value> {
@@ -119,6 +119,7 @@ pub(crate) fn build_cross_format_openai_responses_request_body(
provider_type,
body_rules,
request_headers,
user_api_key_id,
None,
enable_model_directives,
)
@@ -134,6 +135,7 @@ pub(crate) fn build_cross_format_openai_responses_request_body_with_codex_model_
provider_type: &str,
body_rules: Option<&Value>,
request_headers: &http::HeaderMap,
history_scope: Option<&str>,
model_capabilities: Option<&crate::ai_serving::CodexResponsesModelCapabilities>,
enable_model_directives: bool,
) -> Option<Value> {
@@ -144,6 +146,7 @@ pub(crate) fn build_cross_format_openai_responses_request_body_with_codex_model_
provider_api_format,
upstream_is_stream,
enable_model_directives,
history_scope,
)?;
let mut provider_request_body =
apply_standard_provider_request_body_rules_with_request_headers(
@@ -82,6 +82,96 @@ fn is_grok_text_provider_api_format(provider_api_format: &str) -> bool {
)
}
fn response_function_tool_names(body: &Value) -> Vec<String> {
body.get("tools")
.and_then(Value::as_array)
.into_iter()
.flatten()
.filter(|tool| tool.get("type").and_then(Value::as_str) == Some("function"))
.filter_map(|tool| tool.get("name").and_then(Value::as_str))
.map(ToOwned::to_owned)
.collect()
}
fn chat_function_tool_names(body: &Value) -> Vec<String> {
body.get("tools")
.and_then(Value::as_array)
.into_iter()
.flatten()
.filter_map(|tool| {
tool.get("function")
.and_then(|function| function.get("name"))
.and_then(Value::as_str)
})
.map(ToOwned::to_owned)
.collect()
}
fn response_input_call_ids(body: &Value) -> Vec<String> {
body.get("input")
.and_then(Value::as_array)
.into_iter()
.flatten()
.filter(|item| {
matches!(
item.get("type").and_then(Value::as_str),
Some("function_call" | "function_call_output")
)
})
.filter_map(|item| item.get("call_id").and_then(Value::as_str))
.map(ToOwned::to_owned)
.collect()
}
fn chat_message_call_ids(body: &Value) -> Vec<String> {
let mut call_ids = Vec::new();
for message in body
.get("messages")
.and_then(Value::as_array)
.into_iter()
.flatten()
{
if let Some(tool_calls) = message.get("tool_calls").and_then(Value::as_array) {
call_ids.extend(
tool_calls
.iter()
.filter_map(|tool_call| tool_call.get("id").and_then(Value::as_str))
.map(ToOwned::to_owned),
);
}
if let Some(tool_call_id) = message.get("tool_call_id").and_then(Value::as_str) {
call_ids.push(tool_call_id.to_string());
}
}
call_ids
}
fn log_responses_to_chat_tool_conversion(trace_id: &str, inbound: &Value, outbound: &Value) {
let inbound_tool_names = response_function_tool_names(inbound);
let outbound_tool_names = chat_function_tool_names(outbound);
let inbound_call_ids = response_input_call_ids(inbound);
let outbound_call_ids = chat_message_call_ids(outbound);
let previous_response_id = inbound
.get("previous_response_id")
.and_then(Value::as_str)
.unwrap_or_default();
debug!(
event_name = "openai_responses_to_chat_tool_conversion",
log_type = "debug",
trace_id = %trace_id,
inbound_tool_count = inbound_tool_names.len(),
outbound_tool_count = outbound_tool_names.len(),
inbound_tool_names = ?inbound_tool_names,
outbound_tool_names = ?outbound_tool_names,
previous_response_id = %previous_response_id,
inbound_call_ids = ?inbound_call_ids,
outbound_call_ids = ?outbound_call_ids,
history_recovered = !previous_response_id.is_empty()
&& outbound_call_ids.iter().any(|call_id| inbound_call_ids.contains(call_id)),
"converted OpenAI Responses tools and continuation context to OpenAI Chat"
);
}
pub(crate) struct LocalOpenAiResponsesCandidatePayloadParts {
pub(super) auth_header: String,
pub(super) auth_value: String,
@@ -306,6 +396,14 @@ pub(crate) async fn resolve_local_openai_responses_candidate_payload_parts(
return Ok(None);
}
};
crate::ai_serving::hydrate_openai_response_history(
state.runtime_state(),
body_json,
spec_metadata.api_format,
provider_api_format,
input.auth_context.api_key_id.as_str(),
)
.await?;
let redaction = resolve_provider_chat_pii_redaction(
state,
parts,
@@ -367,6 +465,7 @@ pub(crate) async fn resolve_local_openai_responses_candidate_payload_parts(
transport.endpoint.body_rules.as_ref()
},
effective_headers,
Some(input.auth_context.api_key_id.as_str()),
codex_model_capabilities.as_ref(),
false,
)
@@ -471,6 +570,11 @@ pub(crate) async fn resolve_local_openai_responses_candidate_payload_parts(
.await;
return Ok(None);
}
if needs_bidirectional_conversion
&& crate::ai_serving::api_format_alias_matches(provider_api_format, "openai:chat")
{
log_responses_to_chat_tool_conversion(trace_id, body_json, &base_provider_request_body);
}
let provider_request_body = base_provider_request_body;
if let Some(kiro_auth) = kiro_auth.as_ref() {