mirror of
https://github.com/fawney19/Aether.git
synced 2026-10-08 18:37:46 +08:00
Merge origin/main into main
This commit is contained in:
@@ -2198,10 +2198,6 @@ struct OpenAIResponsesClientToolResultState {
|
||||
item_started: bool,
|
||||
}
|
||||
|
||||
fn is_responses_web_search_tool(name: &str) -> bool {
|
||||
matches!(name, "web_search" | "web_search_preview")
|
||||
}
|
||||
|
||||
fn web_search_query_from_arguments(arguments: &str) -> String {
|
||||
serde_json::from_str::<Value>(arguments)
|
||||
.ok()
|
||||
@@ -3526,12 +3522,14 @@ impl OpenAIResponsesClientEmitter {
|
||||
.map(|(_, child_name)| child_name.to_string())
|
||||
.unwrap_or_else(|| name.clone());
|
||||
let emitted_namespace = namespaced_tool.map(|(namespace, _)| namespace.to_string());
|
||||
let is_namespaced_tool = namespaced_tool.is_some();
|
||||
let web_search = self
|
||||
.namespace_tool_aliases
|
||||
.emits_hosted_web_search_call(&name);
|
||||
let state = self.tool_calls.entry(index).or_default();
|
||||
state.call_id = call_id.clone();
|
||||
state.name = emitted_name;
|
||||
state.namespace = emitted_namespace;
|
||||
state.web_search = !is_namespaced_tool && is_responses_web_search_tool(&name);
|
||||
state.web_search = web_search;
|
||||
let emitted_call_id = state.call_id.clone();
|
||||
let emitted_name = state.name.clone();
|
||||
let emitted_namespace = state.namespace.clone();
|
||||
@@ -6379,6 +6377,53 @@ mod tests {
|
||||
assert!(!sse.contains("response.function_call_arguments.delta"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn openai_responses_client_emitter_keeps_client_declared_web_search_function_as_function_call()
|
||||
{
|
||||
let mut emitter = OpenAIResponsesClientEmitter::with_report_context(&json!({
|
||||
"original_request_body": {
|
||||
"tools": [{
|
||||
"type": "function",
|
||||
"name": "web_search",
|
||||
"parameters": {"type": "object", "properties": {"query": {"type": "string"}}}
|
||||
}]
|
||||
}
|
||||
}));
|
||||
let mut bytes = Vec::new();
|
||||
for event in [
|
||||
CanonicalStreamEvent::ToolCallStart {
|
||||
index: 0,
|
||||
call_id: "call_ws_1".to_string(),
|
||||
name: "web_search".to_string(),
|
||||
},
|
||||
CanonicalStreamEvent::ToolCallArgumentsDelta {
|
||||
index: 0,
|
||||
arguments: r#"{"query":"today tech"}"#.to_string(),
|
||||
},
|
||||
CanonicalStreamEvent::Finish {
|
||||
finish_reason: Some("tool_calls".to_string()),
|
||||
usage: None,
|
||||
},
|
||||
] {
|
||||
bytes.extend(
|
||||
emitter
|
||||
.emit(CanonicalStreamFrame {
|
||||
id: "resp_123".to_string(),
|
||||
model: "gemini-3.8-flash".to_string(),
|
||||
event,
|
||||
})
|
||||
.expect("event should encode"),
|
||||
);
|
||||
}
|
||||
|
||||
let sse = String::from_utf8(bytes).expect("sse should be utf8");
|
||||
assert!(!sse.contains("web_search_call"));
|
||||
assert!(sse.contains(r#""type":"function_call""#));
|
||||
assert!(sse.contains(r#""call_id":"call_ws_1""#));
|
||||
assert!(sse.contains(r#""name":"web_search""#));
|
||||
assert!(sse.contains("response.function_call_arguments.delta"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn openai_responses_provider_state_accepts_legacy_outtext_delta_alias() {
|
||||
let mut state = OpenAIResponsesProviderState::default();
|
||||
|
||||
@@ -54,6 +54,7 @@ pub(crate) struct NamespaceToolAliases {
|
||||
by_chat_name: BTreeMap<String, (String, String)>,
|
||||
namespace_tool_indices: BTreeSet<usize>,
|
||||
invalid_namespace_tool_indices: BTreeSet<usize>,
|
||||
client_function_tool_names: BTreeSet<String>,
|
||||
}
|
||||
|
||||
impl NamespaceToolAliases {
|
||||
@@ -171,10 +172,24 @@ impl NamespaceToolAliases {
|
||||
else {
|
||||
return Self::default();
|
||||
};
|
||||
let Some(canonical) = openai_responses_tools_to_canonical(Some(tools)) else {
|
||||
return Self::default();
|
||||
};
|
||||
Self::from_canonical_tools(&canonical)
|
||||
let client_function_tool_names = client_function_tool_names(tools);
|
||||
let mut result = openai_responses_tools_to_canonical(Some(tools))
|
||||
.map(|canonical| Self::from_canonical_tools(&canonical))
|
||||
.unwrap_or_default();
|
||||
result.client_function_tool_names = client_function_tool_names;
|
||||
result
|
||||
}
|
||||
|
||||
/// Whether a tool call named `name` should surface to a Responses client as
|
||||
/// a hosted `web_search_call`. A client that declared its own function or
|
||||
/// custom tool called `web_search` must get a `function_call` back, or it
|
||||
/// cannot answer the call and will echo an unconvertible hosted item.
|
||||
/// When the client declares both a hosted `web_search` tool and a function
|
||||
/// of the same name, the function wins: only the client can answer it.
|
||||
pub(crate) fn emits_hosted_web_search_call(&self, name: &str) -> bool {
|
||||
matches!(name, "web_search" | "web_search_preview")
|
||||
&& self.responses_name(name).is_none()
|
||||
&& !self.client_function_tool_names.contains(name)
|
||||
}
|
||||
|
||||
pub(crate) fn chat_name(&self, namespace: &str, child_name: &str) -> Option<&str> {
|
||||
@@ -227,6 +242,32 @@ impl NamespaceToolAliases {
|
||||
}
|
||||
}
|
||||
|
||||
fn client_function_tool_names(tools: &Value) -> BTreeSet<String> {
|
||||
tools
|
||||
.as_array()
|
||||
.into_iter()
|
||||
.flatten()
|
||||
.filter_map(Value::as_object)
|
||||
.filter(|tool| {
|
||||
tool.get("type")
|
||||
.and_then(Value::as_str)
|
||||
.map(str::trim)
|
||||
.is_none_or(|tool_type| {
|
||||
tool_type.eq_ignore_ascii_case("function")
|
||||
|| tool_type.eq_ignore_ascii_case("custom")
|
||||
})
|
||||
})
|
||||
.filter_map(|tool| {
|
||||
non_empty_string(tool.get("name")).or_else(|| {
|
||||
["function", "custom"]
|
||||
.iter()
|
||||
.find_map(|key| non_empty_string(tool.get(*key)?.get("name")))
|
||||
})
|
||||
})
|
||||
.map(ToOwned::to_owned)
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub(crate) fn canonical_tool_is_responses_namespace(tool: &CanonicalToolDefinition) -> bool {
|
||||
raw_responses_tool(tool).is_some_and(|raw| {
|
||||
raw.get("type")
|
||||
@@ -414,6 +455,111 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
fn hosted_web_search(tools: Value, name: &str) -> bool {
|
||||
NamespaceToolAliases::from_report_context(&json!({
|
||||
"original_request_body": {"tools": tools}
|
||||
}))
|
||||
.emits_hosted_web_search_call(name)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hosted_web_search_call_is_reserved_for_undeclared_search_names() {
|
||||
let schema = json!({"type": "object", "properties": {"query": {"type": "string"}}});
|
||||
let cases = [
|
||||
("no tools", json!([]), "web_search", true),
|
||||
(
|
||||
"hosted tool",
|
||||
json!([{"type": "web_search"}]),
|
||||
"web_search",
|
||||
true,
|
||||
),
|
||||
(
|
||||
"hosted preview tool",
|
||||
json!([{"type": "web_search_preview"}]),
|
||||
"web_search_preview",
|
||||
true,
|
||||
),
|
||||
(
|
||||
"function tool",
|
||||
json!([{"type": "function", "name": "web_search", "parameters": schema}]),
|
||||
"web_search",
|
||||
false,
|
||||
),
|
||||
(
|
||||
"function tool named preview",
|
||||
json!([{"type": "function", "name": "web_search_preview", "parameters": schema}]),
|
||||
"web_search_preview",
|
||||
false,
|
||||
),
|
||||
(
|
||||
"custom tool",
|
||||
json!([{"type": "custom", "name": "web_search"}]),
|
||||
"web_search",
|
||||
false,
|
||||
),
|
||||
(
|
||||
"tool without type",
|
||||
json!([{"name": "web_search", "parameters": schema}]),
|
||||
"web_search",
|
||||
false,
|
||||
),
|
||||
(
|
||||
"chat-shaped function tool",
|
||||
json!([{"type": "function", "function": {"name": "web_search", "parameters": schema}}]),
|
||||
"web_search",
|
||||
false,
|
||||
),
|
||||
(
|
||||
"chat-shaped custom tool",
|
||||
json!([{"type": "custom", "custom": {"name": "web_search"}}]),
|
||||
"web_search",
|
||||
false,
|
||||
),
|
||||
(
|
||||
"hosted and function tool together",
|
||||
json!([
|
||||
{"type": "web_search"},
|
||||
{"type": "function", "name": "web_search", "parameters": schema}
|
||||
]),
|
||||
"web_search",
|
||||
false,
|
||||
),
|
||||
(
|
||||
"unrelated function tool",
|
||||
json!([{"type": "function", "name": "lookup", "parameters": schema}]),
|
||||
"web_search",
|
||||
true,
|
||||
),
|
||||
(
|
||||
"non-search name",
|
||||
json!([{"type": "web_search"}]),
|
||||
"lookup",
|
||||
false,
|
||||
),
|
||||
];
|
||||
|
||||
for (label, tools, name, expected) in cases {
|
||||
assert_eq!(hosted_web_search(tools, name), expected, "{label}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn namespaced_web_search_child_is_not_a_hosted_web_search_call() {
|
||||
assert!(!hosted_web_search(
|
||||
json!([{
|
||||
"type": "namespace",
|
||||
"name": "mcp__search",
|
||||
"description": "Search tools",
|
||||
"tools": [{
|
||||
"type": "function",
|
||||
"name": "web_search",
|
||||
"parameters": {"type": "object", "properties": {}}
|
||||
}]
|
||||
}]),
|
||||
"web_search"
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn namespace_aliases_are_unique_bounded_and_prefix_safe() {
|
||||
let long_namespace = format!("namespace__{}", "n".repeat(120));
|
||||
|
||||
@@ -275,7 +275,7 @@ pub fn to_raw(canonical: &CanonicalResponse, report_context: &Value, compact: bo
|
||||
}));
|
||||
}
|
||||
let namespaced_tool = namespace_tool_aliases.responses_name(name);
|
||||
if namespaced_tool.is_none() && is_responses_web_search_tool(name) {
|
||||
if namespace_tool_aliases.emits_hosted_web_search_call(name) {
|
||||
output.push(json!({
|
||||
"type": "web_search_call",
|
||||
"id": id,
|
||||
@@ -601,10 +601,6 @@ fn openai_responses_output_format_from_mime_type(mime_type: &str) -> String {
|
||||
.to_string()
|
||||
}
|
||||
|
||||
fn is_responses_web_search_tool(name: &str) -> bool {
|
||||
matches!(name, "web_search" | "web_search_preview")
|
||||
}
|
||||
|
||||
fn web_search_query_from_value(input: &Value) -> String {
|
||||
input
|
||||
.get("query")
|
||||
@@ -647,6 +643,70 @@ mod tests {
|
||||
assert!(body["completed_at"].as_i64().is_some());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn responses_response_builder_keeps_client_declared_web_search_function_as_function_call() {
|
||||
let report_context = json!({
|
||||
"original_request_body": {
|
||||
"tools": [{
|
||||
"type": "function",
|
||||
"name": "web_search",
|
||||
"parameters": {"type": "object", "properties": {"query": {"type": "string"}}}
|
||||
}]
|
||||
}
|
||||
});
|
||||
let response = CanonicalResponse {
|
||||
id: "resp_test".to_string(),
|
||||
model: "gemini-3.8-flash".to_string(),
|
||||
content: vec![CanonicalContentBlock::ToolUse {
|
||||
id: "call_ws_1".to_string(),
|
||||
name: "web_search".to_string(),
|
||||
input: json!({"query": "today tech"}),
|
||||
extensions: BTreeMap::new(),
|
||||
}],
|
||||
outputs: Vec::new(),
|
||||
stop_reason: Some(CanonicalStopReason::ToolUse),
|
||||
usage: None,
|
||||
extensions: BTreeMap::new(),
|
||||
};
|
||||
|
||||
let body = to_raw(&response, &report_context, false);
|
||||
|
||||
let item = &body["output"][0];
|
||||
assert_eq!(item["type"], "function_call");
|
||||
assert_eq!(item["name"], "web_search");
|
||||
assert_eq!(item["call_id"], "call_ws_1");
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(item["arguments"].as_str().unwrap()).unwrap(),
|
||||
json!({"query": "today tech"})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn responses_response_builder_emits_web_search_call_for_hosted_web_search_tool() {
|
||||
let report_context = json!({
|
||||
"original_request_body": {"tools": [{"type": "web_search"}]}
|
||||
});
|
||||
let response = CanonicalResponse {
|
||||
id: "resp_test".to_string(),
|
||||
model: "gpt-5-5-low".to_string(),
|
||||
content: vec![CanonicalContentBlock::ToolUse {
|
||||
id: "call_ws_1".to_string(),
|
||||
name: "web_search".to_string(),
|
||||
input: json!({"query": "today tech"}),
|
||||
extensions: BTreeMap::new(),
|
||||
}],
|
||||
outputs: Vec::new(),
|
||||
stop_reason: Some(CanonicalStopReason::ToolUse),
|
||||
usage: None,
|
||||
extensions: BTreeMap::new(),
|
||||
};
|
||||
|
||||
let body = to_raw(&response, &report_context, false);
|
||||
|
||||
assert_eq!(body["output"][0]["type"], "web_search_call");
|
||||
assert_eq!(body["output"][0]["action"]["query"], "today tech");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn responses_response_builder_restores_namespaced_chat_tool_identity() {
|
||||
let report_context = json!({
|
||||
|
||||
@@ -973,6 +973,66 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn streams_gemini_web_search_function_call_to_responses_function_call_for_function_tool() {
|
||||
let mut context = report_context("gemini:generate_content", "openai:responses");
|
||||
context["original_request_body"] = json!({
|
||||
"model": "gemini-3.8-flash",
|
||||
"tools": [{
|
||||
"type": "function",
|
||||
"name": "web_search",
|
||||
"parameters": {"type": "object", "properties": {"query": {"type": "string"}}}
|
||||
}]
|
||||
});
|
||||
let mut matrix = StreamingStandardFormatMatrix::default();
|
||||
let mut output = matrix
|
||||
.transform_line(
|
||||
&context,
|
||||
data_line(json!({
|
||||
"response": {
|
||||
"responseId": "resp_ws_function",
|
||||
"modelVersion": "gemini-3.8-flash",
|
||||
"candidates": [{
|
||||
"index": 0,
|
||||
"content": {
|
||||
"role": "model",
|
||||
"parts": [{
|
||||
"thoughtSignature": "signature",
|
||||
"functionCall": {
|
||||
"name": "web_search",
|
||||
"args": {"query": "conpty newline"},
|
||||
"id": "call_109312"
|
||||
}
|
||||
}]
|
||||
},
|
||||
"finishReason": "STOP"
|
||||
}]
|
||||
}
|
||||
})),
|
||||
)
|
||||
.expect("Gemini function call should transform");
|
||||
output.extend(matrix.finish(&context).expect("stream should finish"));
|
||||
|
||||
let events = json_data_events(&output);
|
||||
let completed = events
|
||||
.iter()
|
||||
.find(|event| event["type"] == "response.completed")
|
||||
.expect("response should complete");
|
||||
let items = completed["response"]["output"]
|
||||
.as_array()
|
||||
.expect("completed response should carry output");
|
||||
assert!(
|
||||
items.iter().all(|item| item["type"] != "web_search_call"),
|
||||
"{items:?}"
|
||||
);
|
||||
let call = items
|
||||
.iter()
|
||||
.find(|item| item["type"] == "function_call")
|
||||
.expect("function tool call should stay a function_call");
|
||||
assert_eq!(call["name"], "web_search");
|
||||
assert_eq!(call["call_id"], "call_109312");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn terminal_observer_marks_malformed_gemini_function_call_as_failure() {
|
||||
let context = report_context("gemini:generate_content", "openai:responses");
|
||||
|
||||
@@ -53,6 +53,75 @@ pub fn extract_provider_reasoning_effort_from_body(value: Option<&Value>) -> Opt
|
||||
.and_then(Value::as_str)
|
||||
})
|
||||
.and_then(normalize_provider_reasoning_effort)
|
||||
.or_else(|| {
|
||||
// Gemini also nests its payload one level down, so both the flat
|
||||
// `generateContent` body and the `v1internal` envelope that carries it are read.
|
||||
extract_gemini_reasoning_effort_from_body(object).or_else(|| {
|
||||
object
|
||||
.get("request")
|
||||
.and_then(Value::as_object)
|
||||
.and_then(extract_gemini_reasoning_effort_from_body)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/// Gemini `generateContent` states its reasoning depth inside
|
||||
/// `generationConfig.thinkingConfig`, either as a symbolic `thinkingLevel` or as a token
|
||||
/// `thinkingBudget`. Both camelCase and snake_case spellings are read so that a captured client
|
||||
/// body and a converted provider body resolve to the same label.
|
||||
///
|
||||
/// `includeThoughts` alone is a visibility flag, not a depth, so it never produces a label.
|
||||
fn extract_gemini_reasoning_effort_from_body(
|
||||
object: &serde_json::Map<String, Value>,
|
||||
) -> Option<String> {
|
||||
let generation_config = object
|
||||
.get("generationConfig")
|
||||
.or_else(|| object.get("generation_config"))
|
||||
.and_then(Value::as_object)?;
|
||||
let thinking_config = generation_config
|
||||
.get("thinkingConfig")
|
||||
.or_else(|| generation_config.get("thinking_config"))
|
||||
.and_then(Value::as_object)?;
|
||||
|
||||
if let Some(level) = thinking_config
|
||||
.get("thinkingLevel")
|
||||
.or_else(|| thinking_config.get("thinking_level"))
|
||||
.and_then(Value::as_str)
|
||||
.and_then(normalize_gemini_thinking_level)
|
||||
{
|
||||
return Some(level);
|
||||
}
|
||||
|
||||
thinking_config
|
||||
.get("thinkingBudget")
|
||||
.or_else(|| thinking_config.get("thinking_budget"))
|
||||
.and_then(Value::as_u64)
|
||||
.map(|budget| {
|
||||
// `0` disables reasoning outright. The shared budget ladder collapses it into `low`,
|
||||
// which would report an explicitly disabled request as a shallow one.
|
||||
if budget == 0 {
|
||||
"none".to_string()
|
||||
} else {
|
||||
aether_ai_formats::formats::openai::shared::map_thinking_budget_to_openai_reasoning_effort(budget)
|
||||
.to_string()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Gemini also emits the protobuf enum spelling (`THINKING_LEVEL_HIGH`); the level itself is what
|
||||
/// the badge vocabulary understands, so the enum prefix is stripped before normalizing.
|
||||
///
|
||||
/// `THINKING_LEVEL_UNSPECIFIED` is the enum's "no explicit level" member, not a depth. It is
|
||||
/// rejected rather than surfaced, otherwise the badge would read `unspecified`.
|
||||
fn normalize_gemini_thinking_level(value: &str) -> Option<String> {
|
||||
let normalized = value.trim().to_ascii_lowercase();
|
||||
let normalized = normalized
|
||||
.strip_prefix("thinking_level_")
|
||||
.unwrap_or(normalized.as_str());
|
||||
if normalized == "unspecified" {
|
||||
return None;
|
||||
}
|
||||
normalize_provider_reasoning_effort(normalized)
|
||||
}
|
||||
|
||||
fn normalize_provider_reasoning_effort(value: &str) -> Option<String> {
|
||||
@@ -3588,6 +3657,157 @@ mod tests {
|
||||
assert_eq!(usage.provider_service_tier(), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gemini_thinking_level_supplies_reasoning_effort_for_both_body_spellings() {
|
||||
let mut usage = sample_usage();
|
||||
usage.provider_request_body = Some(json!({
|
||||
"generationConfig": {
|
||||
"thinkingConfig": { "includeThoughts": true, "thinkingLevel": "HIGH" }
|
||||
}
|
||||
}));
|
||||
|
||||
assert_eq!(usage.provider_reasoning_effort().as_deref(), Some("high"));
|
||||
|
||||
// The converted provider body keeps snake_case keys, and the client body may carry the
|
||||
// protobuf enum spelling. Both must land on the same badge vocabulary.
|
||||
usage.provider_request_body = Some(json!({
|
||||
"generation_config": {
|
||||
"thinking_config": { "thinking_level": "thinking_level_medium" }
|
||||
}
|
||||
}));
|
||||
|
||||
assert_eq!(usage.provider_reasoning_effort().as_deref(), Some("medium"));
|
||||
|
||||
usage.provider_request_body = Some(json!({
|
||||
"generationConfig": {
|
||||
"thinkingConfig": { "thinkingLevel": " low " }
|
||||
}
|
||||
}));
|
||||
|
||||
assert_eq!(usage.provider_reasoning_effort().as_deref(), Some("low"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gemini_thinking_budget_supplies_reasoning_effort_without_collapsing_zero() {
|
||||
let mut usage = sample_usage();
|
||||
usage.provider_request_body = Some(json!({
|
||||
"generationConfig": { "thinkingConfig": { "thinkingBudget": 8192 } }
|
||||
}));
|
||||
|
||||
assert_eq!(usage.provider_reasoning_effort().as_deref(), Some("xhigh"));
|
||||
|
||||
// `0` disables reasoning. The shared budget ladder maps 0..=1664 to `low`, which would
|
||||
// report an explicitly disabled request as shallow, so the Gemini path reports `none`.
|
||||
usage.provider_request_body = Some(json!({
|
||||
"generationConfig": { "thinkingConfig": { "thinkingBudget": 0 } }
|
||||
}));
|
||||
|
||||
assert_eq!(usage.provider_reasoning_effort().as_deref(), Some("none"));
|
||||
|
||||
usage.provider_request_body = Some(json!({
|
||||
"generation_config": { "thinking_config": { "thinking_budget": 1280 } }
|
||||
}));
|
||||
|
||||
assert_eq!(usage.provider_reasoning_effort().as_deref(), Some("low"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gemini_thinking_config_without_level_or_budget_yields_no_reasoning_effort() {
|
||||
let mut usage = sample_usage();
|
||||
usage.provider_request_body = Some(json!({
|
||||
"generationConfig": { "thinkingConfig": { "includeThoughts": true } }
|
||||
}));
|
||||
|
||||
assert_eq!(usage.provider_reasoning_effort(), None);
|
||||
|
||||
// A level-less, budget-less config must not fall back to metadata either: the captured
|
||||
// body is authoritative and it says nothing about depth.
|
||||
usage.request_metadata = Some(json!({ "provider_reasoning_effort": "max" }));
|
||||
assert_eq!(usage.provider_reasoning_effort(), None);
|
||||
|
||||
// `THINKING_LEVEL_UNSPECIFIED` is the enum's "no explicit level" member, not a depth.
|
||||
usage.request_metadata = None;
|
||||
usage.provider_request_body = Some(json!({
|
||||
"generationConfig": {
|
||||
"thinkingConfig": { "thinkingLevel": "THINKING_LEVEL_UNSPECIFIED" }
|
||||
}
|
||||
}));
|
||||
|
||||
assert_eq!(usage.provider_reasoning_effort(), None);
|
||||
}
|
||||
|
||||
/// The v1internal envelope nests the real `generateContent` payload under `request`. This is
|
||||
/// the shape the Antigravity/Gemini CLI transports actually send upstream, so the extraction
|
||||
/// has to descend into it or every converted `openai:chat -> gemini` request loses its badge.
|
||||
#[test]
|
||||
fn gemini_thinking_config_is_read_from_the_v1internal_envelope() {
|
||||
let mut usage = sample_usage();
|
||||
usage.provider_request_body = Some(json!({
|
||||
"model": "gemini-3.8-flash-tiered",
|
||||
"project": "aicode-consumers",
|
||||
"requestId": "req-1",
|
||||
"requestType": "agent",
|
||||
"userAgent": "vscode/1.X.X (Antigravity/4.3.0)",
|
||||
"request": {
|
||||
"contents": [{ "role": "user", "parts": [{ "text": "hi" }] }],
|
||||
"generationConfig": {
|
||||
"maxOutputTokens": 65536,
|
||||
"thinkingConfig": { "includeThoughts": true, "thinkingLevel": "high" }
|
||||
}
|
||||
}
|
||||
}));
|
||||
|
||||
assert_eq!(usage.provider_reasoning_effort().as_deref(), Some("high"));
|
||||
|
||||
usage.provider_request_body = Some(json!({
|
||||
"model": "gemini-3.8-flash-tiered",
|
||||
"request": {
|
||||
"generation_config": {
|
||||
"thinking_config": { "include_thoughts": true, "thinking_budget": 32768 }
|
||||
}
|
||||
}
|
||||
}));
|
||||
|
||||
assert_eq!(usage.provider_reasoning_effort().as_deref(), Some("xhigh"));
|
||||
|
||||
usage.provider_request_body = Some(json!({
|
||||
"model": "gemini-3.8-flash-tiered",
|
||||
"request": {
|
||||
"generationConfig": { "thinkingConfig": { "thinkingBudget": 0 } }
|
||||
}
|
||||
}));
|
||||
|
||||
assert_eq!(usage.provider_reasoning_effort().as_deref(), Some("none"));
|
||||
}
|
||||
|
||||
/// A converted request that carries only `maxOutputTokens` must stay badge-less rather than
|
||||
/// picking up a depth from somewhere else in the envelope.
|
||||
#[test]
|
||||
fn v1internal_envelope_without_thinking_config_yields_no_reasoning_effort() {
|
||||
let mut usage = sample_usage();
|
||||
usage.provider_request_body = Some(json!({
|
||||
"model": "gemini-3.8-flash-tiered",
|
||||
"project": "aicode-consumers",
|
||||
"request": {
|
||||
"contents": [{ "role": "user", "parts": [{ "text": "hi" }] }],
|
||||
"generationConfig": { "maxOutputTokens": 65536 }
|
||||
}
|
||||
}));
|
||||
|
||||
assert_eq!(usage.provider_reasoning_effort(), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gemini_thinking_config_does_not_shadow_explicit_effort_fields() {
|
||||
let mut usage = sample_usage();
|
||||
usage.provider_request_body = Some(json!({
|
||||
"reasoning_effort": "max",
|
||||
"generationConfig": { "thinkingConfig": { "thinkingLevel": "low" } }
|
||||
}));
|
||||
|
||||
assert_eq!(usage.provider_reasoning_effort().as_deref(), Some("max"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn requested_and_provider_reasoning_efforts_remain_independent() {
|
||||
let mut usage = sample_usage();
|
||||
|
||||
@@ -43,7 +43,7 @@ pub use quota::{
|
||||
provider_pool_key_model_quota_hard_blocked, provider_pool_key_quota_hard_blocked,
|
||||
provider_pool_key_scheduling_label, provider_pool_member_quota_snapshot,
|
||||
provider_pool_quota_metadata_provider_type, provider_pool_quota_metadata_updated_at,
|
||||
provider_pool_quota_snapshot_updated_at,
|
||||
provider_pool_quota_snapshot_updated_at, provider_pool_reset_deadline_elapsed,
|
||||
};
|
||||
pub use quota_refresh::ProviderPoolQuotaRequestSpec;
|
||||
pub use service::ProviderPoolService;
|
||||
|
||||
@@ -857,7 +857,14 @@ fn provider_pool_reset_deadline_unix_secs(
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn provider_pool_reset_deadline_elapsed(
|
||||
/// Whether the quota window's reset deadline has already passed.
|
||||
///
|
||||
/// The deadline is taken from `reset_at`/`next_reset_at` when present, otherwise
|
||||
/// derived from `reset_seconds`/`reset_after_seconds` anchored at the window (or
|
||||
/// fallback) observation time. Scheduling ignores exhausted windows once this is
|
||||
/// true; read paths can reuse the same predicate so the displayed quota matches
|
||||
/// the scheduling decision after a reset.
|
||||
pub fn provider_pool_reset_deadline_elapsed(
|
||||
item: &Map<String, Value>,
|
||||
fallback_observed_at: Option<u64>,
|
||||
now_unix_secs: u64,
|
||||
|
||||
@@ -200,7 +200,8 @@ pub use windsurf::{
|
||||
pub use xai::{
|
||||
extract_xai_user_id_from_auth_config, extract_xai_user_id_from_value,
|
||||
insert_cli_identity_headers, insert_cli_identity_headers_if_needed, is_xai_provider_transport,
|
||||
resolved_xai_request_base_url, resolved_xai_upstream_base_url,
|
||||
should_attach_cli_identity_headers, xai_auth_uses_api, xai_uses_official_api, XAI_API_BASE_URL,
|
||||
XAI_CHAT_PROXY_BASE_URL, XAI_PROVIDER_TYPE,
|
||||
resolved_xai_request_base_url, resolved_xai_upstream_base_url, set_xai_client_version,
|
||||
should_attach_cli_identity_headers, xai_auth_uses_api, xai_client_version,
|
||||
xai_uses_official_api, XAI_API_BASE_URL, XAI_CHAT_PROXY_BASE_URL, XAI_DEFAULT_CLIENT_VERSION,
|
||||
XAI_PROVIDER_TYPE,
|
||||
};
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
pub mod video;
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
use std::sync::{OnceLock, RwLock};
|
||||
|
||||
use aether_ai_formats::normalize_api_format_alias;
|
||||
use serde_json::Value;
|
||||
@@ -10,7 +11,10 @@ use crate::snapshot::GatewayProviderTransportSnapshot;
|
||||
pub const XAI_PROVIDER_TYPE: &str = "xai";
|
||||
pub const XAI_CHAT_PROXY_BASE_URL: &str = "https://cli-chat-proxy.grok.com/v1";
|
||||
pub const XAI_API_BASE_URL: &str = "https://api.x.ai/v1";
|
||||
pub const XAI_CLIENT_VERSION: &str = "0.2.120";
|
||||
/// 内置的 Grok CLI 版本;网关后台任务会用官方发布版本覆盖它。
|
||||
///
|
||||
/// cli-chat-proxy 会对过旧的版本直接返回 426,因此这里只作为发布检查不可用时的兜底。
|
||||
pub const XAI_DEFAULT_CLIENT_VERSION: &str = "1.0.46";
|
||||
pub const XAI_TOKEN_AUTH_HEADER: &str = "x-xai-token-auth";
|
||||
pub const XAI_TOKEN_AUTH_VALUE: &str = "xai-grok-cli";
|
||||
pub const XAI_CLIENT_VERSION_HEADER: &str = "x-grok-client-version";
|
||||
@@ -19,8 +23,37 @@ pub const XAI_CLIENT_IDENTIFIER_VALUE: &str = "grok-shell";
|
||||
pub const XAI_AUTHENTICATE_RESPONSE_HEADER: &str = "x-authenticateresponse";
|
||||
pub const XAI_AUTHENTICATE_RESPONSE_VALUE: &str = "authenticate-response";
|
||||
|
||||
static ACTIVE_CLIENT_VERSION: OnceLock<RwLock<String>> = OnceLock::new();
|
||||
|
||||
fn active_client_version() -> &'static RwLock<String> {
|
||||
ACTIVE_CLIENT_VERSION.get_or_init(|| RwLock::new(XAI_DEFAULT_CLIENT_VERSION.to_owned()))
|
||||
}
|
||||
|
||||
/// 返回当前发布的 Grok CLI 版本快照。
|
||||
pub fn xai_client_version() -> String {
|
||||
active_client_version()
|
||||
.read()
|
||||
.unwrap_or_else(std::sync::PoisonError::into_inner)
|
||||
.clone()
|
||||
}
|
||||
|
||||
/// 原子替换当前 Grok CLI 版本,返回替换前的版本;版本校验由发布检查器负责,这里只拒绝明显非法值。
|
||||
pub fn set_xai_client_version(version: &str) -> Result<String, &'static str> {
|
||||
let version = version.trim();
|
||||
if version.is_empty()
|
||||
|| version.len() > 64
|
||||
|| !version.bytes().all(|byte| (33..=126).contains(&byte))
|
||||
{
|
||||
return Err("invalid Grok CLI version");
|
||||
}
|
||||
let mut current = active_client_version()
|
||||
.write()
|
||||
.unwrap_or_else(std::sync::PoisonError::into_inner);
|
||||
Ok(std::mem::replace(&mut *current, version.to_owned()))
|
||||
}
|
||||
|
||||
pub fn xai_cli_user_agent() -> String {
|
||||
format!("xai-grok-workspace/{XAI_CLIENT_VERSION}")
|
||||
format!("xai-grok-workspace/{}", xai_client_version())
|
||||
}
|
||||
|
||||
pub fn is_xai_provider_transport(transport: &GatewayProviderTransportSnapshot) -> bool {
|
||||
@@ -91,10 +124,11 @@ pub fn should_attach_cli_identity_headers(
|
||||
}
|
||||
|
||||
pub fn insert_cli_identity_headers(headers: &mut BTreeMap<String, String>) {
|
||||
let client_version = xai_client_version();
|
||||
let user_agent = xai_cli_user_agent();
|
||||
for (name, value) in [
|
||||
(XAI_TOKEN_AUTH_HEADER, XAI_TOKEN_AUTH_VALUE),
|
||||
(XAI_CLIENT_VERSION_HEADER, XAI_CLIENT_VERSION),
|
||||
(XAI_CLIENT_VERSION_HEADER, client_version.as_str()),
|
||||
("user-agent", user_agent.as_str()),
|
||||
(XAI_CLIENT_IDENTIFIER_HEADER, XAI_CLIENT_IDENTIFIER_VALUE),
|
||||
(
|
||||
@@ -441,4 +475,31 @@ mod tests {
|
||||
Some(r#"{"api_key":"xai-key","using_api":true}"#)
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cli_identity_headers_follow_published_client_version() {
|
||||
use super::{
|
||||
insert_cli_identity_headers, set_xai_client_version, xai_client_version,
|
||||
XAI_CLIENT_VERSION_HEADER,
|
||||
};
|
||||
|
||||
let previous = xai_client_version();
|
||||
assert!(set_xai_client_version("").is_err());
|
||||
assert!(set_xai_client_version("1.0 .1").is_err());
|
||||
assert_eq!(xai_client_version(), previous);
|
||||
|
||||
set_xai_client_version(" 9.8.7 ").expect("valid version");
|
||||
let mut headers = BTreeMap::new();
|
||||
insert_cli_identity_headers(&mut headers);
|
||||
set_xai_client_version(&previous).expect("restore version");
|
||||
|
||||
assert_eq!(
|
||||
headers.get(XAI_CLIENT_VERSION_HEADER).map(String::as_str),
|
||||
Some("9.8.7")
|
||||
);
|
||||
assert_eq!(
|
||||
headers.get("user-agent").map(String::as_str),
|
||||
Some("xai-grok-workspace/9.8.7")
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -744,6 +744,39 @@ mod tests {
|
||||
assert!(cleared.get("requested_reasoning_effort").is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gemini_thinking_config_is_derived_into_client_and_provider_reasoning_metadata() {
|
||||
let client_body = json!({
|
||||
"generationConfig": {
|
||||
"thinkingConfig": { "includeThoughts": true, "thinkingLevel": "HIGH" }
|
||||
}
|
||||
});
|
||||
let provider_body = json!({
|
||||
"generation_config": {
|
||||
"thinking_config": { "thinking_budget": 8192 }
|
||||
}
|
||||
});
|
||||
|
||||
let metadata = attach_client_request_body_metadata(
|
||||
Some(json!({ "trace_id": "trace-1" })),
|
||||
Some(&client_body),
|
||||
)
|
||||
.expect("metadata should remain");
|
||||
assert_eq!(metadata["requested_reasoning_effort"], "high");
|
||||
|
||||
let metadata = attach_provider_request_body_metadata(
|
||||
Some(metadata),
|
||||
Some("gemini:generate_content"),
|
||||
Some("gemini-3.8-flash"),
|
||||
Some("gemini-3.8-flash"),
|
||||
Some(&provider_body),
|
||||
)
|
||||
.expect("metadata should remain");
|
||||
|
||||
assert_eq!(metadata["requested_reasoning_effort"], "high");
|
||||
assert_eq!(metadata["provider_reasoning_effort"], "xhigh");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn provider_request_body_metadata_uses_final_provider_body_as_source_of_truth() {
|
||||
let metadata = Some(json!({
|
||||
|
||||
Reference in New Issue
Block a user