Make Codex and Claude Code subscriptions go further: a local model router that gives routine agent work to economical models and keeps your strongest model for the hard parts.
Rust ai-agentsai-gatewayclaude-codecodexcoding-agentcost-optimizationdeepseekllm-gatewayllm-routerlocal-firstmodel-routingrusttask-routingtauri
fix(gateway): preserve Pi Responses options on Chat routes (#65)
澄潭15c0a990d49b
diff --git a/crates/gateway/src/adapters/request.rs b/crates/gateway/src/adapters/request.rsindex d53e7705..94876eb6 100644--- a/crates/gateway/src/adapters/request.rs+++ b/crates/gateway/src/adapters/request.rs@@ -646,6 +646,14 @@ fn serialize_chat());}let mut body = Map::new();+ if let Some(options) = &request.responses_options {+ if let Some(store) = options.store {+ body.insert("store".into(), Value::Bool(store));+ }+ if let Some(key) = &options.prompt_cache_key {+ body.insert("prompt_cache_key".into(), Value::String(key.clone()));+ }+ }body.insert("model".into(),Value::String(profile.capability.native_model.clone()),@@ -812,15 +820,52 @@ fn portable_completed_response_item(request: &ModelRequestIRV1, index: usize) ->})}+fn validate_responses_options(+ options: Option<&ResponsesRequestOptionsV1>,+ target: IngressProtocol,+) -> Result<(), ProtocolAdapterError> {+ let Some(options) = options else {+ return Ok(());+ };+ if target == IngressProtocol::Responses {+ return Ok(());+ }+ // These controls share an explicit Chat wire representation. All other+ // Responses options remain native; do not treat an empty value as absent.+ for (field, unsupported) in [+ (+ "store",+ options+ .store+ .is_some_and(|store| store || target != IngressProtocol::ChatCompletions),+ ),+ (+ "prompt_cache_key",+ options.prompt_cache_key.is_some() && target != IngressProtocol::ChatCompletions,+ ),+ ("include", options.include.is_some()),+ ("client_metadata", options.client_metadata.is_some()),+ ("reasoning.summary", options.reasoning_summary.is_some()),+ ("reasoning.context", options.reasoning_context.is_some()),+ ] {+ if unsupported {+ return Err(ProtocolAdapterError::ClientUnrepresentable(format!(+ "Responses option {field} has no equivalent on this upstream protocol"+ )));+ }+ }+ Ok(())+}+fn validate_message_shapes(request: &ModelRequestIRV1,target: IngressProtocol,) -> Result<(), ProtocolAdapterError> {+ validate_responses_options(request.responses_options.as_ref(), target)?;let mut projected = std::collections::BTreeMap::new();if (!request.responses_annotations.is_empty()|| !request.responses_search_history.is_empty()|| request.web_search.is_some()- || request.responses_options.is_some()|| request.responses_item_ids.keys().any(|index| {!request.responses_reasoning_history.contains_key(index)&& !portable_completed_response_item(request, *index)diff --git a/crates/gateway/src/adapters/responses_options.rs b/crates/gateway/src/adapters/responses_options.rsindex f06738b0..024c4c22 100644--- a/crates/gateway/src/adapters/responses_options.rs+++ b/crates/gateway/src/adapters/responses_options.rs@@ -1,5 +1,5 @@-//! Controls observed on the installed Codex Responses client. Preserve their exact values-//! only on a Responses upstream; they convey no HiRoute identity or provider-state ownership.+//! Responses client controls. Store=false and prompt_cache_key also have explicit Chat+//! representations; native controls convey no HiRoute identity or provider-state ownership.use super::*;pub(super) fn annotations(diff --git a/crates/gateway/src/adapters/tests.rs b/crates/gateway/src/adapters/tests.rsindex e43bb632..ee9d855c 100644--- a/crates/gateway/src/adapters/tests.rs+++ b/crates/gateway/src/adapters/tests.rs@@ -24,6 +24,9 @@ mod payload_tolerance;#[path = "tests/structured_output.rs"]mod structured_output;+#[path = "tests/responses_options.rs"]+mod responses_options;+fn exact_state_profile(ingress: IngressProtocol,upstream: IngressProtocol,diff --git a/crates/gateway/src/adapters/tests/responses_options.rs b/crates/gateway/src/adapters/tests/responses_options.rsnew file mode 100644index 00000000..d3c3daff--- /dev/null+++ b/crates/gateway/src/adapters/tests/responses_options.rs@@ -0,0 +1,245 @@+use super::*;+use serde_json::Value;++fn profile(target: IngressProtocol) -> CandidateProtocolProfile {+ exact_state_profile(IngressProtocol::Responses, target)+}++fn document(options: Value) -> Value {+ let mut body = json!({"model":"alias","input":"hello","stream":true,+ "max_output_tokens":128,"tools":[{"type":"function","name":"lookup",+ "description":"Look up a value","parameters":{"type":"object",+ "properties":{"key":{"type":"string"}},"required":["key"]},"strict":false}]});+ body.as_object_mut()+ .unwrap()+ .extend(options.as_object().unwrap().clone());+ body+}++#[test]+fn pi_options_four_cases_preserve_actual_chat_wire() {+ for options in [+ json!({}),+ json!({"store":false}),+ json!({"prompt_cache_key":"pi-session"}),+ json!({"store":false,"prompt_cache_key":"pi-session"}),+ ] {+ let original = document(options);+ let request = decode_ingress_request(IngressProtocol::Responses, &original).unwrap();+ let projected =+ project_candidate_request(&request, &profile(IngressProtocol::ChatCompletions))+ .expect("Pi's stateless options have Chat equivalents");+ let wire: Value = serde_json::from_slice(&projected.bytes).unwrap();+ assert_eq!(projected.path, "/v1/chat/completions");+ for field in ["store", "prompt_cache_key"] {+ assert_eq!(wire.get(field), original.get(field), "{field}");+ }+ assert_eq!(wire["model"], "physical");+ assert_eq!(wire["max_completion_tokens"], 128);+ assert_eq!(wire["stream"], true);+ assert_eq!(wire["tools"][0]["function"]["name"], "lookup");+ assert_eq!(+ wire["tools"][0]["function"]["parameters"],+ original["tools"][0]["parameters"]+ );+ assert_eq!(wire["tools"][0]["function"]["strict"], false);+ }+}++#[test]+fn pi_options_preserve_native_responses_values() {+ let original = document(json!({"store":false,"prompt_cache_key":"pi-session",+ "include":["reasoning.encrypted_content"],"client_metadata":{"source":"pi"},+ "reasoning":{"summary":"auto","context":"all_turns"}}));+ let mut request = decode_ingress_request(IngressProtocol::Responses, &original).unwrap();+ // Exercise both the native body and canonical serializer.+ for canonical in [false, true] {+ if canonical {+ request.native_body = None;+ }+ let projected =+ project_candidate_request(&request, &profile(IngressProtocol::Responses)).unwrap();+ let wire: Value = serde_json::from_slice(&projected.bytes).unwrap();+ for field in [+ "store",+ "prompt_cache_key",+ "include",+ "client_metadata",+ "reasoning",+ ] {+ assert_eq!(+ wire[field], original[field],+ "{field}, canonical={canonical}"+ );+ }+ }+}++#[test]+fn pi_options_native_fields_and_messages_remain_field_specific_rejections() {+ for (field, options) in [+ ("include", json!({"include":[]})),+ (+ "include",+ json!({"include":["reasoning.encrypted_content"]}),+ ),+ ("client_metadata", json!({"client_metadata":{}})),+ ("reasoning.summary", json!({"reasoning":{"summary":"auto"}})),+ (+ "reasoning.context",+ json!({"reasoning":{"context":"all_turns"}}),+ ),+ ] {+ let mut original = document(options);+ original["store"] = json!(false);+ original["prompt_cache_key"] = json!("pi-session");+ let request = decode_ingress_request(IngressProtocol::Responses, &original).unwrap();+ let error = project_candidate_request(&request, &profile(IngressProtocol::ChatCompletions))+ .unwrap_err();+ assert_eq!(error.code(), "CLIENT_PROTOCOL_UNREPRESENTABLE");+ assert!(error.to_string().contains(field), "{field}: {error}");+ }+ for (field, options) in [+ ("store", json!({"store":false})),+ ("prompt_cache_key", json!({"prompt_cache_key":"pi-session"})),+ ] {+ let request =+ decode_ingress_request(IngressProtocol::Responses, &document(options)).unwrap();+ let error =+ project_candidate_request(&request, &profile(IngressProtocol::Messages)).unwrap_err();+ assert_eq!(error.code(), "CLIENT_PROTOCOL_UNREPRESENTABLE");+ assert!(error.to_string().contains(field), "{error}");+ }+}++#[test]+fn pi_options_do_not_make_messages_thinking_display_a_responses_control() {+ // thinking.display belongs to Messages. Mapping known Responses options+ // must not admit a mixed-protocol extension through the native-only guard.+ let original = document(json!({"store":false,"prompt_cache_key":"pi-session",+ "thinking":{"display":"omitted"}}));+ let request = decode_ingress_request(IngressProtocol::Responses, &original).unwrap();+ assert!(request.native_only);+ let native = project_candidate_request(&request, &profile(IngressProtocol::Responses)).unwrap();+ for field in ["store", "prompt_cache_key", "thinking"] {+ assert_eq!(native.body[field], original[field]);+ }+ let error = project_candidate_request(&request, &profile(IngressProtocol::ChatCompletions))+ .unwrap_err();+ assert_eq!(error.code(), "CLIENT_PROTOCOL_UNREPRESENTABLE");+}++#[test]+fn pi_options_do_not_admit_invalid_types_or_server_state() {+ for options in [+ json!({"store":true}),+ json!({"store":"false"}),+ json!({"store":null}),+ json!({"prompt_cache_key":42}),+ json!({"prompt_cache_key":null}),+ json!({"previous_response_id":"resp-previous"}),+ json!({"conversation":"conv-1"}),+ ] {+ assert!(decode_ingress_request(IngressProtocol::Responses, &document(options)).is_err());+ }+}++#[test]+fn pi_options_continue_multiple_tools_and_failed_results_with_plan_effort() {+ use crate::server::core_runtime::profiles::{+ NativeReasoningFieldAssignment, NativeReasoningRender, NativeReasoningValue,+ ReasoningControlKind,+ };+ let mut original = document(json!({"store":false,"prompt_cache_key":"pi-session",+ "reasoning":{"effort":"low"}}));+ original["input"] = json!([+ {"role":"user","content":"look up two values"},+ {"type":"function_call","call_id":"call-1","name":"lookup","arguments":"{\"key\":\"one\"}"},+ {"type":"function_call","call_id":"call-2","name":"lookup","arguments":"{\"key\":\"two\"}"},+ {"type":"function_call_output","call_id":"call-1","output":"value one"},+ {"type":"function_call_output","call_id":"call-2","output":"tool failed: missing key"},+ {"role":"user","content":"Continue and handle the missing key"}]);+ let request = decode_ingress_request(IngressProtocol::Responses, &original).unwrap();+ let mut profile = profile(IngressProtocol::ChatCompletions);+ let reasoning = &mut profile.capability.reasoning_profiles[0];+ reasoning.control_kind = ReasoningControlKind::Discrete;+ reasoning.render = NativeReasoningRender::ExactFields {+ protocol: IngressProtocol::ChatCompletions,+ fields: vec![NativeReasoningFieldAssignment {+ path: vec!["reasoning_effort".into()],+ value: NativeReasoningValue::String("high".into()),+ }],+ };+ let projected = project_candidate_request(&request, &profile).unwrap();+ let wire: Value = serde_json::from_slice(&projected.bytes).unwrap();+ assert_eq!(wire["store"], false);+ assert_eq!(wire["prompt_cache_key"], "pi-session");+ assert_eq!(wire["reasoning_effort"], "high");+ assert_eq!(+ wire["messages"][1]["tool_calls"].as_array().unwrap().len(),+ 2+ );+ for (index, id, args) in [+ (0, "call-1", "{\"key\":\"one\"}"),+ (1, "call-2", "{\"key\":\"two\"}"),+ ] {+ assert_eq!(wire["messages"][1]["tool_calls"][index]["id"], id);+ assert_eq!(+ wire["messages"][1]["tool_calls"][index]["function"]["arguments"],+ args+ );+ }+ assert_eq!(wire["messages"][2]["tool_call_id"], "call-1");+ assert_eq!(wire["messages"][3]["content"], "tool failed: missing key");+ assert_eq!(+ wire["messages"][4]["content"],+ "Continue and handle the missing key"+ );+}++#[test]+fn pi_options_survive_externalized_history_and_attempt_replay() {+ let root = std::env::temp_dir().join(format!(+ "hiroute-pi-options-{}-{}",+ std::process::id(),+ std::time::SystemTime::now()+ .duration_since(std::time::UNIX_EPOCH)+ .unwrap()+ .as_nanos()+ ));+ let manager = ReplayManager::open(ReplayConfig {+ root: root.clone(),+ memory_threshold_bytes: 128,+ record_bytes: 31,+ orphan_ttl: std::time::Duration::from_secs(60),+ })+ .unwrap();+ let tree = BudgetTree::new(4 * 1024 * 1024, 4 * 1024 * 1024).unwrap();+ let budget = tree.stream(4 * 1024 * 1024).unwrap();+ let store = manager.begin_request(budget.clone()).unwrap();+ let mut original = document(json!({"store":false,"prompt_cache_key":"pi-session"}));+ original["input"] = json!("history ".repeat(4096));+ let mut request = decode_ingress_request(IngressProtocol::Responses, &original).unwrap();+ externalize_model_request(&mut request, &store, 128).unwrap();+ assert!(!model_content_refs(&request).is_empty());+ store.prevalidate(&model_content_refs(&request)).unwrap();+ for _ in 0..2 {+ let template = project_candidate_request_template(+ &request,+ &profile(IngressProtocol::ChatCompletions),+ )+ .unwrap();+ let mut reader = sequential_attempt_body(template, store.clone(), &budget, 127).unwrap();+ let mut bytes = Vec::new();+ while let Some(chunk) = reader.next_chunk().unwrap() {+ bytes.extend_from_slice(chunk.bytes());+ }+ let wire: Value = serde_json::from_slice(&bytes).unwrap();+ assert_eq!(wire["store"], false);+ assert_eq!(wire["prompt_cache_key"], "pi-session");+ assert_eq!(wire["messages"][0]["content"], original["input"]);+ }+ drop(store);+ drop(manager);+ fs_err_remove_dir(&root);+}diff --git a/tools/e2e-harness/tests/p0_gateway_runtime.rs b/tools/e2e-harness/tests/p0_gateway_runtime.rsindex 6fffbf60..e1c804ce 100644--- a/tools/e2e-harness/tests/p0_gateway_runtime.rs+++ b/tools/e2e-harness/tests/p0_gateway_runtime.rs@@ -44,6 +44,9 @@ mod accepted_history;#[path = "p0_gateway_runtime/nonstream_terminal.rs"]mod nonstream_terminal;+#[path = "p0_gateway_runtime/responses_options.rs"]+mod responses_options;+#[test]fn fixed_requests_retry_only_same_source_keys_and_never_enter_an_allowed_plan() {for (status, error) in [(401, AUTH_ERROR), (403, AUTH_ERROR), (429, QUOTA_ERROR)] {diff --git a/tools/e2e-harness/tests/p0_gateway_runtime/responses_options.rs b/tools/e2e-harness/tests/p0_gateway_runtime/responses_options.rsnew file mode 100644index 00000000..89e0eb42--- /dev/null+++ b/tools/e2e-harness/tests/p0_gateway_runtime/responses_options.rs@@ -0,0 +1,65 @@+use super::*;+use serde_json::{Value, json};++const CHAT_OK: &[u8] = br#"{"id":"chat-options","model":"runtime-native-model-1","choices":[{"index":0,"message":{"role":"assistant","content":"ok"},"finish_reason":"stop"}],"usage":{"prompt_tokens":1,"completion_tokens":1,"total_tokens":2}}"#;++#[test]+fn pi_options_reach_real_gateway_chat_upstream_with_exact_auth_and_tools() {+ for options in [+ json!({}),+ json!({"store":false}),+ json!({"prompt_cache_key":"pi-session"}),+ json!({"store":false,"prompt_cache_key":"pi-session"}),+ ] {+ let provider = NativeProvider::start(vec![ProviderReply::Complete {+ status: 200,+ error_kind: None,+ body: CHAT_OK,+ }]);+ let candidates = [PublicationCandidate {+ provider_index: 0,+ upstream_protocol: "chat_completions",+ statically_enabled: true,+ }];+ let fixture =+ RuntimeFixture::launch_with_publication_candidates(&[&provider], 1, Some(&candidates));+ let mut body = json!({"model":MODEL,"input":"hello","stream":false,+ "max_output_tokens":128,"tools":[{"type":"function","name":"lookup",+ "parameters":{"type":"object","properties":{"key":{"type":"string"}}}}]});+ body.as_object_mut()+ .unwrap()+ .extend(options.as_object().unwrap().clone());+ let response = fixture.request_body(&serde_json::to_vec(&body).unwrap());+ assert_eq!(+ response.status,+ 200,+ "{}",+ String::from_utf8_lossy(&response.body)+ );+ assert_eq!(provider.calls(), 1);+ let requests = provider.requests();+ let wire = &requests[0];+ assert!(wire.starts_with(b"POST /v1/chat/completions "));+ assert_eq!(+ wire_header(wire, "authorization"),+ Some("Bearer provider-secret-1".into())+ );+ assert!(wire_header(wire, "x-hiroute-token").is_none());+ let offset = wire+ .windows(4)+ .position(|part| part == b"\r\n\r\n")+ .unwrap()+ + 4;+ let actual: Value = serde_json::from_slice(&wire[offset..]).unwrap();+ for field in ["store", "prompt_cache_key"] {+ assert_eq!(actual.get(field), body.get(field));+ }+ assert_eq!(actual["model"], "runtime-native-model-1");+ assert_eq!(actual["max_completion_tokens"], 128);+ assert_eq!(actual["tools"][0]["function"]["name"], "lookup");+ assert_eq!(+ actual["tools"][0]["function"]["parameters"],+ body["tools"][0]["parameters"]+ );+ }+}