feat(ai): support custom headers for api services

This commit is contained in:
二丫讲梵 authored and GitHub committed 2026-08-31 23:47:14 +08:00
1 parent ba11e4be8d
commit 42c0dc7f89
27 files changed
+299 -10

No files matched your search

+104 -3
View File
@@ -1,6 +1,6 @@
use crate::token_usage::TokenUsage;
use futures::StreamExt;
use reqwest::header::{HeaderMap, HeaderValue, AUTHORIZATION, CONTENT_TYPE};
use reqwest::header::{HeaderMap, HeaderName, HeaderValue, AUTHORIZATION, CONTENT_TYPE};
use serde::{Deserialize, Serialize};
use serde_json::json;
use std::collections::{HashMap, HashSet};
@@ -371,6 +371,10 @@ pub struct AiConfig {
pub models: Vec<AiModelListItem>,
#[serde(default)]
pub api_style: AiApiStyle,
/// Additional headers attached to every API-backed AI request. Values can
/// contain gateway credentials, so callers must never log this map.
#[serde(default)]
pub custom_headers: HashMap<String, String>,
#[serde(default)]
pub proxy_enabled: bool,
#[serde(default)]
@@ -1591,6 +1595,7 @@ pub fn maybe_bearer_headers(config: &AiConfig) -> Result<HeaderMap, String> {
if !api_key.is_empty() {
headers.insert(AUTHORIZATION, HeaderValue::from_str(&format!("Bearer {api_key}")).map_err(|e| e.to_string())?);
}
append_custom_headers(&mut headers, config)?;
Ok(headers)
}
@@ -1612,6 +1617,36 @@ pub fn claude_headers(config: &AiConfig) -> Result<HeaderMap, String> {
}
}
headers.insert("anthropic-version", HeaderValue::from_static("2023-06-01"));
append_custom_headers(&mut headers, config)?;
Ok(headers)
}
/// Adds user-supplied gateway headers after DBX's provider headers. This
/// intentionally lets an explicit `Authorization` value replace the provider
/// default, while protecting HTTP framing and JSON content semantics.
fn append_custom_headers(headers: &mut HeaderMap, config: &AiConfig) -> Result<(), String> {
const RESERVED: &[&str] =
&["host", "content-length", "content-type", "connection", "transfer-encoding", "proxy-authorization"];
let mut seen = HashSet::new();
for (raw_name, raw_value) in &config.custom_headers {
let name = HeaderName::from_bytes(raw_name.trim().as_bytes())
.map_err(|_| format!("Invalid AI custom header name: {raw_name}"))?;
if RESERVED.iter().any(|reserved| name.as_str().eq_ignore_ascii_case(reserved)) {
return Err(format!("Invalid AI custom header name: {raw_name} (reserved by DBX)"));
}
if !seen.insert(name.clone()) {
return Err(format!("Duplicate AI custom header name: {raw_name}"));
}
let value = HeaderValue::from_str(raw_value)
.map_err(|error| format!("Invalid AI custom header value for {raw_name}: {error}"))?;
headers.insert(name, value);
}
Ok(())
}
fn custom_headers(config: &AiConfig) -> Result<HeaderMap, String> {
let mut headers = HeaderMap::new();
append_custom_headers(&mut headers, config)?;
Ok(headers)
}
@@ -1812,7 +1847,10 @@ async fn list_gemini_models(client: &reqwest::Client, config: &AiConfig) -> Resu
let mut models = Vec::new();
loop {
let mut request = client.get(&endpoint).query(&[("key", config.api_key.as_str()), ("pageSize", "1000")]);
let mut request = client
.get(&endpoint)
.query(&[("key", config.api_key.as_str()), ("pageSize", "1000")])
.headers(custom_headers(config)?);
if let Some(token) = page_token.as_deref() {
request = request.query(&[("pageToken", token)]);
}
@@ -2209,6 +2247,7 @@ pub async fn call_gemini(client: &reqwest::Client, request: AiCompletionRequest)
.post(resolve_endpoint(&request.config))
.query(&[("key", normalized_api_key(&request.config))])
.header(CONTENT_TYPE, "application/json")
.headers(custom_headers(&request.config)?)
.json(&body)
.send()
.await
@@ -2688,6 +2727,7 @@ pub async fn test_connection_core(config: &AiConfig) -> Result<AiTestConnectionR
let res = client
.post(&gemini_ep)
.header(CONTENT_TYPE, "application/json")
.headers(custom_headers(&config_inner)?)
.query(&[("key", api_key.as_str()), ("alt", "sse")])
.json(&json!({
"contents": [{ "parts": [{ "text": TEST_PROMPT }], "role": "user" }],
@@ -3433,6 +3473,7 @@ async fn stream_gemini(
let endpoint = resolve_gemini_stream_endpoint(&request.config);
let api_key = normalized_api_key(&request.config).to_string();
let headers = custom_headers(&request.config)?;
let config = request.config.clone();
let emitted = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
@@ -3440,6 +3481,7 @@ async fn stream_gemini(
let body = body.clone();
let endpoint = endpoint.clone();
let api_key = api_key.clone();
let headers = headers.clone();
let session_id = session_id.to_string();
let emitted = emitted.clone();
async move {
@@ -3447,6 +3489,7 @@ async fn stream_gemini(
.post(&endpoint)
.query(&[("key", api_key.as_str()), ("alt", "sse")])
.header(CONTENT_TYPE, "application/json")
.headers(headers)
.json(&body)
.send()
.await
@@ -4278,6 +4321,7 @@ async fn stream_gemini_with_tools(
let endpoint = resolve_gemini_stream_endpoint(&request.config);
let api_key = normalized_api_key(&request.config).to_string();
let headers = custom_headers(&request.config)?;
let config = request.config.clone();
let emitted = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
@@ -4285,6 +4329,7 @@ async fn stream_gemini_with_tools(
let body = body.clone();
let endpoint = endpoint.clone();
let api_key = api_key.clone();
let headers = headers.clone();
let session_id = session_id.to_string();
let emitted = emitted.clone();
async move {
@@ -4292,6 +4337,7 @@ async fn stream_gemini_with_tools(
.post(&endpoint)
.query(&[("key", api_key.as_str()), ("alt", "sse")])
.header(CONTENT_TYPE, "application/json")
.headers(headers)
.json(&body)
.send()
.await
@@ -4490,7 +4536,8 @@ mod tests {
AiAuthMethod, AiCapabilitySource, AiChatSelectionState, AiCompletionRequest, AiConfig, AiEffortCapability,
AiEffortOption, AiEffortSelection, AiInlineImage, AiMessage, AiModelInfo, AiProvider, AiReasoningLevel,
MiniMaxStreamDelta, MiniMaxStreamState, MiniMaxTextAccumulator, StreamToolEvent, StreamingToolCallAccumulator,
ToolCallRef, AUTHORIZATION, CLAUDE_DEFAULT_SYSTEM, MINIMAX_REASONING_DETAILS_PAYLOAD_KEY, TEST_PROMPT,
ToolCallRef, AUTHORIZATION, CLAUDE_DEFAULT_SYSTEM, CONTENT_TYPE, MINIMAX_REASONING_DETAILS_PAYLOAD_KEY,
TEST_PROMPT,
};
#[test]
@@ -4758,6 +4805,7 @@ mod tests {
model: "claude-sonnet-4-6[1m]".to_string(),
models: Vec::new(),
api_style: AiApiStyle::Completions,
custom_headers: Default::default(),
proxy_enabled: false,
proxy_url: String::new(),
enable_thinking: true,
@@ -5420,6 +5468,7 @@ mod tests {
model: "gpt-4o".to_string(),
models: Vec::new(),
api_style: AiApiStyle::Completions,
custom_headers: Default::default(),
proxy_enabled: true,
proxy_url: "not a proxy url".to_string(),
enable_thinking: true,
@@ -5460,6 +5509,7 @@ mod tests {
model: "gpt-4o".to_string(),
models: Vec::new(),
api_style: AiApiStyle::Completions,
custom_headers: Default::default(),
proxy_enabled: true,
proxy_url: "127.0.0.1:7890".to_string(),
enable_thinking: true,
@@ -5498,6 +5548,7 @@ mod tests {
model: "gpt-4o".to_string(),
models: Vec::new(),
api_style: AiApiStyle::Completions,
custom_headers: Default::default(),
proxy_enabled: true,
proxy_url: "not a proxy url".to_string(),
enable_thinking: true,
@@ -5536,6 +5587,7 @@ mod tests {
model: "gemini-1.5-pro".to_string(),
models: Vec::new(),
api_style: AiApiStyle::Completions,
custom_headers: Default::default(),
proxy_enabled: false,
proxy_url: String::new(),
enable_thinking: true,
@@ -5578,6 +5630,7 @@ mod tests {
model: "llama3.1".to_string(),
models: Vec::new(),
api_style: AiApiStyle::Completions,
custom_headers: Default::default(),
proxy_enabled: false,
proxy_url: String::new(),
enable_thinking: true,
@@ -5617,6 +5670,7 @@ mod tests {
model: "local-model".to_string(),
models: Vec::new(),
api_style: AiApiStyle::Completions,
custom_headers: Default::default(),
proxy_enabled: false,
proxy_url: String::new(),
enable_thinking: true,
@@ -5675,6 +5729,7 @@ mod tests {
model: String::new(),
models: Vec::new(),
api_style: AiApiStyle::Completions,
custom_headers: Default::default(),
proxy_enabled: false,
proxy_url: String::new(),
enable_thinking: true,
@@ -5709,6 +5764,7 @@ mod tests {
model: String::new(),
models: Vec::new(),
api_style: AiApiStyle::Completions,
custom_headers: Default::default(),
proxy_enabled: false,
proxy_url: String::new(),
enable_thinking: true,
@@ -5746,6 +5802,7 @@ mod tests {
model: "claude-sonnet-4-20250514".to_string(),
models: Vec::new(),
api_style: AiApiStyle::AnthropicMessages,
custom_headers: Default::default(),
proxy_enabled: false,
proxy_url: String::new(),
enable_thinking: true,
@@ -5835,6 +5892,7 @@ mod tests {
model: "MiniMax-M3".to_string(),
models: Vec::new(),
api_style: AiApiStyle::Completions,
custom_headers: Default::default(),
proxy_enabled: false,
proxy_url: String::new(),
enable_thinking: true,
@@ -5894,6 +5952,7 @@ mod tests {
model: "test-model".to_string(),
models: Vec::new(),
api_style: AiApiStyle::Completions,
custom_headers: Default::default(),
proxy_enabled: false,
proxy_url: String::new(),
enable_thinking: true,
@@ -5967,6 +6026,7 @@ mod tests {
model: "gpt-5.6-luna".to_string(),
models: Vec::new(),
api_style: AiApiStyle::Responses,
custom_headers: Default::default(),
proxy_enabled: false,
proxy_url: String::new(),
enable_thinking: true,
@@ -5997,6 +6057,7 @@ mod tests {
let completions = AiConfig {
endpoint: "https://api.openai.com/v1/responses".to_string(),
api_style: AiApiStyle::Completions,
custom_headers: Default::default(),
..config
};
assert_eq!(resolve_endpoint(&completions), "https://api.openai.com/v1/chat/completions");
@@ -6012,6 +6073,7 @@ mod tests {
model: "claude-sonnet-4-20250514".to_string(),
models: Vec::new(),
api_style: AiApiStyle::Completions,
custom_headers: Default::default(),
proxy_enabled: false,
proxy_url: String::new(),
enable_thinking: true,
@@ -6058,6 +6120,34 @@ mod tests {
assert_eq!(unauthenticated_headers.get("anthropic-version").unwrap(), "2023-06-01");
}
#[test]
fn custom_ai_headers_are_merged_and_protect_http_framing() {
let mut config = test_config(AiProvider::OpenaiCompatible);
config.api_key = "provider-key".to_string();
config.custom_headers = HashMap::from([
("X-GoModel-User-Path".to_string(), "/dbx-assistant".to_string()),
("Authorization".to_string(), "Bearer gateway-token".to_string()),
]);
let headers = maybe_bearer_headers(&config).unwrap();
assert_eq!(headers.get("x-gomodel-user-path").unwrap(), "/dbx-assistant");
assert_eq!(headers.get(AUTHORIZATION).unwrap(), "Bearer gateway-token");
assert_eq!(headers.get(CONTENT_TYPE).unwrap(), "application/json");
config.custom_headers = HashMap::from([("Content-Length".to_string(), "0".to_string())]);
assert!(maybe_bearer_headers(&config).unwrap_err().contains("reserved by DBX"));
config.custom_headers =
HashMap::from([("X-Tenant".to_string(), "one".to_string()), ("x-tenant".to_string(), "two".to_string())]);
assert!(maybe_bearer_headers(&config).unwrap_err().contains("Duplicate AI custom header name"));
config.custom_headers = HashMap::from([("bad header".to_string(), "value".to_string())]);
assert!(maybe_bearer_headers(&config).unwrap_err().contains("Invalid AI custom header name"));
config.custom_headers = HashMap::from([("X-Test".to_string(), "bad\r\nvalue".to_string())]);
assert!(maybe_bearer_headers(&config).unwrap_err().contains("Invalid AI custom header value"));
}
#[test]
fn claude_system_prompt_substitutes_default_when_empty() {
// Empty or whitespace-only prompts must fall back to a non-empty value,
@@ -6137,6 +6227,7 @@ mod tests {
model: String::new(),
models: Vec::new(),
api_style: AiApiStyle::Completions,
custom_headers: Default::default(),
proxy_enabled: false,
proxy_url: String::new(),
enable_thinking: false,
@@ -6179,6 +6270,7 @@ mod tests {
model: "qwen3:0.6b".to_string(),
models: Vec::new(),
api_style: AiApiStyle::Completions,
custom_headers: Default::default(),
proxy_enabled: false,
proxy_url: String::new(),
enable_thinking: false,
@@ -6270,6 +6362,7 @@ mod tests {
model: String::new(),
models: Vec::new(),
api_style: AiApiStyle::Completions,
custom_headers: Default::default(),
proxy_enabled: false,
proxy_url: String::new(),
enable_thinking: false,
@@ -6544,6 +6637,7 @@ mod tests {
model: "gpt-5.5".to_string(),
models: Vec::new(),
api_style: AiApiStyle::Completions,
custom_headers: Default::default(),
proxy_enabled: false,
proxy_url: String::new(),
enable_thinking: true,
@@ -6765,6 +6859,7 @@ mod tests {
model: "kimi-k2.5".to_string(),
models: Vec::new(),
api_style: AiApiStyle::Completions,
custom_headers: Default::default(),
proxy_enabled: false,
proxy_url: String::new(),
enable_thinking: false,
@@ -6812,6 +6907,7 @@ mod tests {
model: "gpt-5".to_string(),
models: vec![],
api_style: AiApiStyle::Completions,
custom_headers: Default::default(),
proxy_enabled: false,
proxy_url: String::new(),
enable_thinking: false,
@@ -6865,6 +6961,7 @@ mod tests {
model: "qwen3".to_string(),
models: vec![],
api_style: AiApiStyle::Completions,
custom_headers: Default::default(),
proxy_enabled: false,
proxy_url: String::new(),
enable_thinking: false,
@@ -6912,6 +7009,7 @@ mod tests {
model: "MiniMax-M3".to_string(),
models: vec![],
api_style: AiApiStyle::Completions,
custom_headers: Default::default(),
proxy_enabled: false,
proxy_url: String::new(),
enable_thinking: false,
@@ -7303,6 +7401,7 @@ mod tests {
model: "qwen3".to_string(),
models: vec![],
api_style: AiApiStyle::Completions,
custom_headers: Default::default(),
proxy_enabled: false,
proxy_url: String::new(),
enable_thinking: false,
@@ -7377,6 +7476,7 @@ mod tests {
model: "local-model".to_string(),
models: Vec::new(),
api_style: AiApiStyle::Completions,
custom_headers: Default::default(),
proxy_enabled: false,
proxy_url: String::new(),
enable_thinking: true,
@@ -7446,6 +7546,7 @@ mod tests {
model: "deepseek-r1:14b".to_string(),
models: vec![],
api_style: AiApiStyle::Completions,
custom_headers: Default::default(),
proxy_enabled: false,
proxy_url: String::new(),
enable_thinking: false,
@@ -568,6 +568,7 @@ mod tests {
model: model.to_string(),
models: Vec::new(),
api_style: AiApiStyle::Completions,
custom_headers: Default::default(),
proxy_enabled: false,
proxy_url: String::new(),
enable_thinking: true,
+1
View File
@@ -923,6 +923,7 @@ mod tests {
model: model.to_string(),
models: Vec::new(),
api_style: AiApiStyle::Completions,
custom_headers: Default::default(),
proxy_enabled: false,
proxy_url: String::new(),
enable_thinking: true,
+1
View File
@@ -1114,6 +1114,7 @@ mod tests {
model: model.to_string(),
models: Vec::new(),
api_style: AiApiStyle::Completions,
custom_headers: Default::default(),
proxy_enabled: false,
proxy_url: String::new(),
enable_thinking: true,
+1
View File
@@ -405,6 +405,7 @@ mod tests {
model: model.to_string(),
models: Vec::new(),
api_style: AiApiStyle::Completions,
custom_headers: Default::default(),
proxy_enabled: false,
proxy_url: String::new(),
enable_thinking: true,
+1
View File
@@ -423,6 +423,7 @@ mod tests {
model: model.to_string(),
models: Vec::new(),
api_style: AiApiStyle::Completions,
custom_headers: Default::default(),
proxy_enabled: false,
proxy_url: String::new(),
enable_thinking: true,
+1
View File
@@ -785,6 +785,7 @@ mod tests {
model: "default".to_string(),
models: Vec::new(),
api_style: AiApiStyle::Completions,
custom_headers: Default::default(),
proxy_enabled: false,
proxy_url: String::new(),
enable_thinking: true,
+1
View File
@@ -451,6 +451,7 @@ mod tests {
model: model.to_string(),
models: Vec::new(),
api_style: AiApiStyle::Completions,
custom_headers: Default::default(),
proxy_enabled: false,
proxy_url: String::new(),
enable_thinking: true,
+1
View File
@@ -859,6 +859,7 @@ mod tests {
model: "openai-codex/gpt-5.4".to_string(),
models: Vec::new(),
api_style: AiApiStyle::Completions,
custom_headers: Default::default(),
proxy_enabled: false,
proxy_url: String::new(),
enable_thinking: true,
+1
View File
@@ -527,6 +527,7 @@ mod tests {
model: model.to_string(),
models: Vec::new(),
api_style: AiApiStyle::Completions,
custom_headers: Default::default(),
proxy_enabled: false,
proxy_url: String::new(),
enable_thinking: true,
+1
View File
@@ -1634,6 +1634,7 @@ mod tests {
model: "gpt-4o-mini".to_string(),
models: vec![],
api_style: AiApiStyle::Completions,
custom_headers: Default::default(),
proxy_enabled: false,
proxy_url: String::new(),
enable_thinking: true,
+1
View File
@@ -7239,6 +7239,7 @@ mod tests {
model: "gpt-4o".to_string(),
models: Vec::new(),
api_style: AiApiStyle::Completions,
custom_headers: Default::default(),
proxy_enabled: false,
proxy_url: String::new(),
enable_thinking: true,