mirror of
https://github.com/zyycn/codex-proxy-rs.git
synced 2026-11-29 00:01:06 +00:00
Resolve declared model groups to concrete variants before account and model-policy checks. Preserve explicit model IDs and native request bodies, and expose only accessible groups in the catalog. Co-authored-by: Codex <noreply@openai.com>
165 lines
6.5 KiB
Rust
165 lines
6.5 KiB
Rust
//! 验证不同协议的共享请求事实
|
|
|
|
use gateway_protocol::request::{request_facts, request_observation};
|
|
|
|
#[test]
|
|
fn token_count_observation_uses_the_nested_generation_request() {
|
|
for (body, expected) in [
|
|
(
|
|
serde_json::json!({"generationConfig":{"thinkingConfig":{"thinkingLevel":"MEDIUM"}}}),
|
|
Some("medium"),
|
|
),
|
|
(
|
|
serde_json::json!({"generation_config":{"thinking_config":{"thinking_level":"HIGH"}}}),
|
|
Some("high"),
|
|
),
|
|
(
|
|
serde_json::json!({"generationConfig":{"thinkingConfig":{"thinkingLevel":"HIGH"}},"generateContentRequest":{"generationConfig":{"thinkingConfig":{"thinkingLevel":"MEDIUM"}}}}),
|
|
Some("medium"),
|
|
),
|
|
(
|
|
serde_json::json!({"generationConfig":{"thinkingConfig":{"thinkingLevel":"HIGH"}},"generateContentRequest":{"contents":[]}}),
|
|
None,
|
|
),
|
|
] {
|
|
let observation = gateway_protocol::request::token_count_observation(
|
|
"genai",
|
|
body.as_object().unwrap(),
|
|
&Default::default(),
|
|
);
|
|
assert_eq!(observation.reasoning_effort.as_deref(), expected);
|
|
}
|
|
}
|
|
use serde_json::{Map, json};
|
|
|
|
#[test]
|
|
fn messages_facts_include_images_inside_tool_results() {
|
|
let body = json!({"max_tokens":0,"messages":[{"role":"user","content":[{"type":"tool_result","content":[{"type":"image","source":{"type":"url","url":"https://example.com/image.png"}}]}]}],"tools":[{"name":"search"}],"thinking":{"type":"adaptive"},"output_config":{"format":{"type":"json_schema","schema":{"type":"object"}}}});
|
|
let facts = request_facts("anthropic", body.as_object().expect("请求正文"));
|
|
assert_eq!(facts.max_output_tokens, Some(0));
|
|
assert!(facts.vision && facts.tools && facts.reasoning && facts.json_schema);
|
|
assert!(!facts.native_continuation);
|
|
}
|
|
|
|
#[test]
|
|
fn responses_facts_keep_native_continuation_separate_from_history() {
|
|
let body = json!({"max_output_tokens":123,"input":[],"previous_response_id":"resp_1"});
|
|
let facts = request_facts("openai", body.as_object().expect("请求正文"));
|
|
assert_eq!(facts.max_output_tokens, Some(123));
|
|
assert!(facts.native_continuation);
|
|
assert!(!facts.tools && !facts.vision && !facts.reasoning && !facts.json_schema);
|
|
}
|
|
|
|
#[test]
|
|
fn image_generation_detection_distinguishes_tools_from_image_inputs_and_passive_namespaces() {
|
|
for body in [
|
|
json!({"tools":[{"type":"image_generation"}]}),
|
|
json!({"tool_choice":{"type":"image_generation"}}),
|
|
json!({"tool_choice":{"type":"allowed_tools","tools":[{"type":"image_generation"}]}}),
|
|
json!({"input":[{"type":"additional_tools","tools":[{"type":"image_generation"}]}]}),
|
|
] {
|
|
assert!(
|
|
request_facts("openai", body.as_object().expect("请求正文")).image_generation,
|
|
"{body}"
|
|
);
|
|
}
|
|
for body in [
|
|
json!({"input":"Please generate an image"}),
|
|
json!({"input":[{"role":"user","content":[{"type":"input_image","image_url":"data:image/png;base64,AA=="}]}]}),
|
|
json!({"tools":[{"type":"namespace","name":"image_gen","tools":[{"type":"function","name":"imagegen"}]}]}),
|
|
json!({"tools":[{"type":"function","name":"image_generation"}]}),
|
|
] {
|
|
assert!(
|
|
!request_facts("openai", body.as_object().expect("请求正文")).image_generation,
|
|
"{body}"
|
|
);
|
|
}
|
|
let body = json!({"tools":[{"type":"image_generation"}]});
|
|
for protocol in ["anthropic", "private"] {
|
|
assert!(!request_facts(protocol, body.as_object().expect("请求正文")).image_generation);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn messages_request_observation_preserves_explicit_effort_without_inventing_defaults() {
|
|
for (options, expected) in [
|
|
(json!({"output_config":{"effort":"xhigh"}}), Some("xhigh")),
|
|
(
|
|
json!({"thinking":{"type":"adaptive"},"output_config":{"effort":"low"}}),
|
|
Some("low"),
|
|
),
|
|
(
|
|
json!({"thinking":{"type":"disabled"},"output_config":{"effort":"high"}}),
|
|
Some("high"),
|
|
),
|
|
(
|
|
json!({"output_config":{"effort":" Future-Level "}}),
|
|
Some("Future-Level"),
|
|
),
|
|
(json!({"output_config":{"effort":" "}}), None),
|
|
(json!({"output_config":{"effort":null}}), None),
|
|
(json!({"output_config":{"effort":42}}), None),
|
|
(json!({"thinking":{"type":"adaptive"}}), None),
|
|
(
|
|
json!({"thinking":{"type":"enabled","budget_tokens":32768}}),
|
|
None,
|
|
),
|
|
(json!({}), None),
|
|
] {
|
|
let mut body = json!({"model":"gpt-5.4","max_tokens":128,"messages":[{"role":"user","content":"hello"}]});
|
|
body.as_object_mut()
|
|
.unwrap()
|
|
.extend(options.as_object().unwrap().clone());
|
|
let observation = request_observation(
|
|
"anthropic",
|
|
body.as_object().unwrap(),
|
|
&Map::new(),
|
|
Some("inapplicable-previous-effort"),
|
|
);
|
|
assert_eq!(
|
|
observation.reasoning_effort.as_deref(),
|
|
expected,
|
|
"{options}"
|
|
);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn genai_native_fields_preserve_tool_arguments_and_separate_vision_from_image_generation() {
|
|
let body = json!({
|
|
"tools": [{"functionDeclarations": [{"name": "lookup"}]}],
|
|
"contents": [{"role": "user", "parts": [{
|
|
"functionResponse": {
|
|
"parts": [{"inlineData": {"mimeType": "image/png", "data": "AA=="}}],
|
|
"response": {"responseFormat": [{"image": {}}]}
|
|
}
|
|
}]}],
|
|
"generationConfig": {
|
|
"maxOutputTokens": 128,
|
|
"responseFormat": [{"text":{"schema":{"type":"object"}}}],
|
|
"thinkingConfig": {"thinkingLevel":"HIGH"}
|
|
}
|
|
});
|
|
let body = body.as_object().expect("请求正文");
|
|
let facts = request_facts("genai", body);
|
|
assert_eq!(facts.max_output_tokens, Some(128));
|
|
assert!(facts.tools && facts.vision && facts.reasoning && facts.json_schema);
|
|
assert!(!facts.image_generation && !facts.native_continuation);
|
|
assert_eq!(
|
|
request_observation("genai", body, &Map::new(), None)
|
|
.reasoning_effort
|
|
.as_deref(),
|
|
Some("high")
|
|
);
|
|
|
|
for config in [
|
|
json!({"response_modalities": ["TEXT", "IMAGE"]}),
|
|
json!({"responseModalities": [2]}),
|
|
json!({"response_format":[{"image":{}}]}),
|
|
json!({"image_config":{}}),
|
|
] {
|
|
let body = json!({"generation_config": config});
|
|
assert!(request_facts("genai", body.as_object().expect("请求正文")).image_generation);
|
|
}
|
|
}
|