mirror of
https://github.com/farion1231/cc-switch.git
synced 2026-07-24 21:30:17 +08:00
67e074c0a7
* style(frontend): reformat provider forms, constants and hooks
Apply prettier formatting across 5 frontend files. No logic changes.
Changed files:
- AddProviderDialog.tsx: reformat generic type annotation and callback
- ClaudeFormFields.tsx: consolidate multi-line useState and Collapsible props
- CodexConfigSections.tsx: expand single-line React imports to multi-line,
collapse removeCodexTopLevelField() call
- constants.ts: merge TemplateType into single line
- useSkills.ts: expand single-line TanStack Query imports to multi-line,
reformat uninstallSkill mutationFn chain
* deps(proxy): add hyper ecosystem crates and manual decompression libs
reqwest internally normalizes all header names to lowercase and does not
preserve insertion order, causing proxied requests to differ from the
original client requests. To achieve transparent header forwarding with
original casing and order, introduce lower-level hyper HTTP client libs.
New dependencies:
- hyper-util 0.1: TokioExecutor + legacy Client with
preserve_header_case support for HTTP/1.1
- hyper-rustls 0.27: rustls-based TLS connector for hyper
- http 1 / http-body 1 / http-body-util 0.1: HTTP type crates for
hyper 1.x request/response construction
- flate2 1: manual gzip/deflate decompression (replaces reqwest auto)
- brotli 7: manual brotli decompression
Changed dependencies:
- serde_json: enable preserve_order feature to keep JSON field order
- reqwest: drop gzip feature to prevent reqwest from overriding the
client's original accept-encoding header
* refactor(proxy): use hyper client for header-case preserving forwarding
Previously the proxy used reqwest for all upstream requests. reqwest
normalizes header names to lowercase and reorders them internally,
making proxied requests distinguishable from direct CLI requests.
Some upstream providers are sensitive to these differences.
This commit replaces reqwest with a hyper-based HTTP client on the
default (non-proxy) path, achieving wire-level header fidelity:
Server layer (server.rs):
- Replace axum::serve with a manual hyper HTTP/1.1 accept loop
- Enable preserve_header_case(true) so incoming header casing is
captured in a HeaderCaseMap extension on each request
- Bridge hyper requests to axum Router via tower::Service
New hyper client module (hyper_client.rs):
- Lazy-initialized hyper-util Client with preserve_header_case
- ProxyResponse enum wrapping both hyper::Response and reqwest::Response
behind a unified interface (status, headers, bytes, bytes_stream)
- send_request() builds requests with ordered HeaderMap + case map
Request handlers (handlers.rs):
- Switch from (HeaderMap, Json<Value>) extractors to raw
axum::extract::Request to preserve Extensions (containing the
HeaderCaseMap from the accept loop)
- Pass extensions through the forwarding chain
Forwarder (forwarder.rs):
- Remove HEADER_BLACKLIST array; replace with ordered header iteration
that preserves original header sequence and casing
- Build ordered_headers by iterating client headers, skipping only
auth/host/content-length, and inserting auth headers at the original
authorization position to maintain order
- Handle anthropic-beta (ensure claude-code-20250219 tag) and
anthropic-version (passthrough or default) inline during iteration
- Remove should_force_identity_encoding() — accept-encoding is now
transparently forwarded to upstream
- Use hyper client by default; fall back to reqwest only when an
HTTP/SOCKS5 proxy tunnel is configured
Provider adapters (adapter.rs, claude.rs, codex.rs, gemini.rs):
- Replace add_auth_headers(RequestBuilder) -> RequestBuilder with
get_auth_headers(AuthInfo) -> Vec<(HeaderName, HeaderValue)>
- Adapters now return header pairs instead of mutating a reqwest builder
- Claude adapter: merge Anthropic/ClaudeAuth/Bearer into single branch;
move Copilot fingerprint headers into get_auth_headers
Response processing (response_processor.rs):
- Add manual decompression (gzip/deflate/brotli via flate2 + brotli)
for non-streaming responses, since reqwest auto-decompression is now
disabled to allow accept-encoding passthrough
- Add compressed-SSE warning log for streaming responses
- Accept ProxyResponse instead of reqwest::Response
HTTP client (http_client.rs):
- Disable reqwest auto-decompression (.no_gzip/.no_brotli/.no_deflate)
on both global and per-provider clients
Streaming adapters (streaming.rs, streaming_responses.rs):
- Generalize stream error type from reqwest::Error to generic E: Error
Misc:
- log_codes.rs: add SRV-005 (ACCEPT_ERR) and SRV-006 (CONN_ERR)
- stream_check.rs: reformat copilot header lines
- transform.rs: fix trailing whitespace alignment
* fix(lint): resolve 35 clippy warnings across Rust codebase
Fix all clippy warnings reported by `cargo clippy --lib`:
- codex_config.rs: fix doc_overindented_list_items (3 spaces -> 2)
- commands/copilot.rs: inline format args in 2 log::error! calls
- commands/provider.rs: inline format args in 3 map_err closures
- proxy/hyper_client.rs: inline format arg in log::debug! call
- proxy/providers/copilot_auth.rs: inline format args in 16 locations
(log macros, format! in headers, error constructors)
- proxy/thinking_optimizer.rs: inline format args in 2 log::info! calls
- services/skill.rs: inline format args in log::debug! call
- services/webdav_sync.rs: inline format args in 6 format! calls
(version compat messages, download limit messages)
- services/webdav_sync/archive.rs: inline format args in 2 format! calls
- session_manager/providers/opencode.rs: inline format args in
source_path format!
All fixes use the clippy::uninlined_format_args suggestion pattern:
format!("msg: {}", var) -> format!("msg: {var}")
* deps(proxy): add raw HTTP write and native TLS cert dependencies
Add crates required for the raw TCP/TLS write path that bypasses
hyper's header encoder to preserve original header name casing:
- httparse: parse raw TCP peek bytes to capture header casings
- tokio-rustls + rustls: direct TLS connections for raw write path
- webpki-roots: Mozilla CA bundle baseline
- rustls-native-certs: load system keychain CAs (trusts proxy MITM
certificates from Clash, mitmproxy, etc.)
* fix(proxy): address code review feedback on response handling
Fixes from PR #1714 code review:
- Extract `read_decoded_body()` and `strip_entity_headers_for_rebuilt_body()`
in response_processor to properly clean content-encoding/content-length
headers after decompression
- Reuse `read_decoded_body()` in handlers.rs for Claude transform path,
ensuring compressed responses are decoded before format conversion
- Make `build_proxy_url_from_config()` public so forwarder can pass proxy
URL to the hyper raw write path
- Add `has_system_proxy_env()` utility with test coverage
- Add 50ms backoff after accept() failures in server.rs to prevent
tight-loop CPU spin on transient socket errors
* feat(proxy): implement raw TCP/TLS write with HTTP CONNECT tunnel
Rewrite hyper_client with a two-tier strategy for header case preservation:
Primary path (raw write):
- Peek raw TCP bytes in server.rs to capture OriginalHeaderCases before
hyper lowercases them
- Build raw HTTP/1.1 request bytes with exact original header name casing
- Write directly to TLS stream, then use WriteFilter to let hyper parse
the response while discarding its duplicate request writes
- Support HTTP CONNECT tunneling through upstream proxies, so header case
is preserved even when a proxy (Clash, V2Ray) is configured
Fallback path (hyper-util Client):
- Used when OriginalHeaderCases is empty or raw write fails
- Configured with title_case_headers(true) for best-effort casing
TLS improvements:
- Load native system certificates alongside webpki roots so proxy MITM
CAs (installed in system keychain) are trusted through CONNECT tunnels
Key types added:
- OriginalHeaderCases: maps lowercase name → original wire-casing bytes
- WriteFilter<S>: AsyncRead+AsyncWrite wrapper that discards writes
- connect_via_proxy(): HTTP CONNECT tunnel establishment
- ExtensionDebugMarker: diagnostic marker for extension chain debugging
* refactor(proxy): route requests through hyper with proxy-aware forwarding
Rework forwarder request dispatch to always prefer the hyper raw write
path (header case preservation) over reqwest:
Request routing:
- HTTP/HTTPS proxy: hyper raw write through CONNECT tunnel (case preserved)
- SOCKS5 proxy: reqwest fallback (CONNECT not supported for SOCKS5)
- No proxy: hyper raw write direct connection
Header handling improvements:
- Replace host header in-place at original position instead of
skip-and-append, preserving client's header ordering
- Preserve client's original accept-encoding for transparent passthrough;
only force identity encoding when transform path needs decompression
- Add should_force_identity_encoding() to centralize the decision
- Remove hardcoded 'br, gzip, deflate' override that masked client values
Proxy URL resolution (priority order):
1. Provider-specific proxy config (if enabled)
2. Global proxy URL configured in CC Switch
3. Direct connection (no proxy)
* chore(proxy): remove dead code, redundant tests and debug scaffolding
- Inline should_force_identity_encoding() (was just `needs_transform`)
and delete its 5 test cases
- Remove ExtensionDebugMarker diagnostic type
- Remove unused has_system_proxy_env() and its test
- Remove strip_entity_headers test
- Simplify hyper path: remove redundant is_socks_proxy ternary
- Update hyper_client module doc to reflect CONNECT tunnel support
* fix(proxy): block direct-connect fallback and complete CONNECT tunnel support
* feat(hooks): improve proxy requirement warnings with specific reasons
- Remove redundant OpenAI format hint toast messages
- Add detailed reason detection for proxy requirements (OpenAI Chat, OpenAI Responses, full URL mode)
- Update i18n files with new reason-specific keys
* style(*): format code with prettier
- Remove extra whitespace in http_client.rs
- Fix formatting issues in useProviderActions.ts
* fix(proxy): post-merge fixes for forward return type and clippy warnings
- Restore forward() return type to (ProxyResponse, Option<String>)
to pass claude_api_format through to callers
- Inline format args in log::warn! macro (clippy::uninlined_format_args)
- Suppress too_many_arguments for check_claude_stream
* refactor(proxy): preserve original header wire order and add non-streaming body timeout
- Rewrite build_raw_request to emit headers in original
client-sent sequence instead of hash-map order
- Remove unused OriginalHeaderCases::get_all method
- Add body_timeout to read_decoded_body to prevent
requests hanging when upstream stalls after headers
471 lines
14 KiB
Rust
471 lines
14 KiB
Rust
// unused imports removed
|
||
use std::path::PathBuf;
|
||
|
||
use crate::config::{
|
||
atomic_write, delete_file, get_home_dir, sanitize_provider_name, write_json_file,
|
||
write_text_file,
|
||
};
|
||
use crate::error::AppError;
|
||
use serde_json::Value;
|
||
use std::fs;
|
||
use std::path::Path;
|
||
use toml_edit::DocumentMut;
|
||
|
||
/// 获取 Codex 配置目录路径
|
||
pub fn get_codex_config_dir() -> PathBuf {
|
||
if let Some(custom) = crate::settings::get_codex_override_dir() {
|
||
return custom;
|
||
}
|
||
|
||
get_home_dir().join(".codex")
|
||
}
|
||
|
||
/// 获取 Codex auth.json 路径
|
||
pub fn get_codex_auth_path() -> PathBuf {
|
||
get_codex_config_dir().join("auth.json")
|
||
}
|
||
|
||
/// 获取 Codex config.toml 路径
|
||
pub fn get_codex_config_path() -> PathBuf {
|
||
get_codex_config_dir().join("config.toml")
|
||
}
|
||
|
||
/// 获取 Codex 供应商配置文件路径
|
||
#[allow(dead_code)]
|
||
pub fn get_codex_provider_paths(
|
||
provider_id: &str,
|
||
provider_name: Option<&str>,
|
||
) -> (PathBuf, PathBuf) {
|
||
let base_name = provider_name
|
||
.map(sanitize_provider_name)
|
||
.unwrap_or_else(|| sanitize_provider_name(provider_id));
|
||
|
||
let auth_path = get_codex_config_dir().join(format!("auth-{base_name}.json"));
|
||
let config_path = get_codex_config_dir().join(format!("config-{base_name}.toml"));
|
||
|
||
(auth_path, config_path)
|
||
}
|
||
|
||
/// 删除 Codex 供应商配置文件
|
||
#[allow(dead_code)]
|
||
pub fn delete_codex_provider_config(
|
||
provider_id: &str,
|
||
provider_name: &str,
|
||
) -> Result<(), AppError> {
|
||
let (auth_path, config_path) = get_codex_provider_paths(provider_id, Some(provider_name));
|
||
|
||
delete_file(&auth_path).ok();
|
||
delete_file(&config_path).ok();
|
||
|
||
Ok(())
|
||
}
|
||
|
||
/// 原子写 Codex 的 `auth.json` 与 `config.toml`,在第二步失败时回滚第一步
|
||
pub fn write_codex_live_atomic(
|
||
auth: &Value,
|
||
config_text_opt: Option<&str>,
|
||
) -> Result<(), AppError> {
|
||
let auth_path = get_codex_auth_path();
|
||
let config_path = get_codex_config_path();
|
||
|
||
if let Some(parent) = auth_path.parent() {
|
||
std::fs::create_dir_all(parent).map_err(|e| AppError::io(parent, e))?;
|
||
}
|
||
|
||
// 读取旧内容用于回滚
|
||
let old_auth = if auth_path.exists() {
|
||
Some(fs::read(&auth_path).map_err(|e| AppError::io(&auth_path, e))?)
|
||
} else {
|
||
None
|
||
};
|
||
let _old_config = if config_path.exists() {
|
||
Some(fs::read(&config_path).map_err(|e| AppError::io(&config_path, e))?)
|
||
} else {
|
||
None
|
||
};
|
||
|
||
// 准备写入内容
|
||
let cfg_text = match config_text_opt {
|
||
Some(s) => s.to_string(),
|
||
None => String::new(),
|
||
};
|
||
if !cfg_text.trim().is_empty() {
|
||
toml::from_str::<toml::Table>(&cfg_text).map_err(|e| AppError::toml(&config_path, e))?;
|
||
}
|
||
|
||
// 第一步:写 auth.json
|
||
write_json_file(&auth_path, auth)?;
|
||
|
||
// 第二步:写 config.toml(失败则回滚 auth.json)
|
||
if let Err(e) = write_text_file(&config_path, &cfg_text) {
|
||
// 回滚 auth.json
|
||
if let Some(bytes) = old_auth {
|
||
let _ = atomic_write(&auth_path, &bytes);
|
||
} else {
|
||
let _ = delete_file(&auth_path);
|
||
}
|
||
return Err(e);
|
||
}
|
||
|
||
Ok(())
|
||
}
|
||
|
||
/// 读取 `~/.codex/config.toml`,若不存在返回空字符串
|
||
pub fn read_codex_config_text() -> Result<String, AppError> {
|
||
let path = get_codex_config_path();
|
||
if path.exists() {
|
||
std::fs::read_to_string(&path).map_err(|e| AppError::io(&path, e))
|
||
} else {
|
||
Ok(String::new())
|
||
}
|
||
}
|
||
|
||
/// 对非空的 TOML 文本进行语法校验
|
||
pub fn validate_config_toml(text: &str) -> Result<(), AppError> {
|
||
if text.trim().is_empty() {
|
||
return Ok(());
|
||
}
|
||
toml::from_str::<toml::Table>(text)
|
||
.map(|_| ())
|
||
.map_err(|e| AppError::toml(Path::new("config.toml"), e))
|
||
}
|
||
|
||
/// 读取并校验 `~/.codex/config.toml`,返回文本(可能为空)
|
||
pub fn read_and_validate_codex_config_text() -> Result<String, AppError> {
|
||
let s = read_codex_config_text()?;
|
||
validate_config_toml(&s)?;
|
||
Ok(s)
|
||
}
|
||
|
||
/// Update a field in Codex config.toml using toml_edit (syntax-preserving).
|
||
///
|
||
/// Supported fields:
|
||
/// - `"base_url"`: writes to `[model_providers.<current>].base_url` if `model_provider` exists,
|
||
/// otherwise falls back to top-level `base_url`.
|
||
/// - `"model"`: writes to top-level `model` field.
|
||
///
|
||
/// Empty value removes the field.
|
||
pub fn update_codex_toml_field(toml_str: &str, field: &str, value: &str) -> Result<String, String> {
|
||
let mut doc = toml_str
|
||
.parse::<DocumentMut>()
|
||
.map_err(|e| format!("TOML parse error: {e}"))?;
|
||
|
||
let trimmed = value.trim();
|
||
|
||
match field {
|
||
"base_url" => {
|
||
let model_provider = doc
|
||
.get("model_provider")
|
||
.and_then(|item| item.as_str())
|
||
.map(str::to_string);
|
||
|
||
if let Some(provider_key) = model_provider {
|
||
// Ensure [model_providers] table exists
|
||
if doc.get("model_providers").is_none() {
|
||
doc["model_providers"] = toml_edit::table();
|
||
}
|
||
|
||
if let Some(model_providers) = doc["model_providers"].as_table_mut() {
|
||
// Ensure [model_providers.<provider_key>] table exists
|
||
if !model_providers.contains_key(&provider_key) {
|
||
model_providers[&provider_key] = toml_edit::table();
|
||
}
|
||
|
||
if let Some(provider_table) = model_providers[&provider_key].as_table_mut() {
|
||
if trimmed.is_empty() {
|
||
provider_table.remove("base_url");
|
||
} else {
|
||
provider_table["base_url"] = toml_edit::value(trimmed);
|
||
}
|
||
return Ok(doc.to_string());
|
||
}
|
||
}
|
||
}
|
||
|
||
// Fallback: no model_provider or structure mismatch → top-level base_url
|
||
if trimmed.is_empty() {
|
||
doc.as_table_mut().remove("base_url");
|
||
} else {
|
||
doc["base_url"] = toml_edit::value(trimmed);
|
||
}
|
||
}
|
||
"model" => {
|
||
if trimmed.is_empty() {
|
||
doc.as_table_mut().remove("model");
|
||
} else {
|
||
doc["model"] = toml_edit::value(trimmed);
|
||
}
|
||
}
|
||
_ => return Err(format!("unsupported field: {field}")),
|
||
}
|
||
|
||
Ok(doc.to_string())
|
||
}
|
||
|
||
/// Remove `base_url` from the active model_provider section only if it matches `predicate`.
|
||
/// Also removes top-level `base_url` if it matches.
|
||
/// Used by proxy cleanup to strip local proxy URLs without touching user-configured URLs.
|
||
pub fn remove_codex_toml_base_url_if(toml_str: &str, predicate: impl Fn(&str) -> bool) -> String {
|
||
let mut doc = match toml_str.parse::<DocumentMut>() {
|
||
Ok(doc) => doc,
|
||
Err(_) => return toml_str.to_string(),
|
||
};
|
||
|
||
let model_provider = doc
|
||
.get("model_provider")
|
||
.and_then(|item| item.as_str())
|
||
.map(str::to_string);
|
||
|
||
if let Some(provider_key) = model_provider {
|
||
if let Some(model_providers) = doc
|
||
.get_mut("model_providers")
|
||
.and_then(|v| v.as_table_mut())
|
||
{
|
||
if let Some(provider_table) = model_providers
|
||
.get_mut(provider_key.as_str())
|
||
.and_then(|v| v.as_table_mut())
|
||
{
|
||
let should_remove = provider_table
|
||
.get("base_url")
|
||
.and_then(|item| item.as_str())
|
||
.map(&predicate)
|
||
.unwrap_or(false);
|
||
if should_remove {
|
||
provider_table.remove("base_url");
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
// Fallback: also clean up top-level base_url if it matches
|
||
let should_remove_root = doc
|
||
.get("base_url")
|
||
.and_then(|item| item.as_str())
|
||
.map(&predicate)
|
||
.unwrap_or(false);
|
||
if should_remove_root {
|
||
doc.as_table_mut().remove("base_url");
|
||
}
|
||
|
||
doc.to_string()
|
||
}
|
||
|
||
#[cfg(test)]
|
||
mod tests {
|
||
use super::*;
|
||
|
||
#[test]
|
||
fn base_url_writes_into_correct_model_provider_section() {
|
||
let input = r#"model_provider = "any"
|
||
model = "gpt-5.1-codex"
|
||
|
||
[model_providers.any]
|
||
name = "any"
|
||
wire_api = "responses"
|
||
"#;
|
||
|
||
let result = update_codex_toml_field(input, "base_url", "https://example.com/v1").unwrap();
|
||
let parsed: toml::Value = toml::from_str(&result).unwrap();
|
||
|
||
let base_url = parsed
|
||
.get("model_providers")
|
||
.and_then(|v| v.get("any"))
|
||
.and_then(|v| v.get("base_url"))
|
||
.and_then(|v| v.as_str())
|
||
.expect("base_url should be in model_providers.any");
|
||
assert_eq!(base_url, "https://example.com/v1");
|
||
|
||
// Should NOT have top-level base_url
|
||
assert!(parsed.get("base_url").is_none());
|
||
|
||
// wire_api preserved
|
||
let wire_api = parsed
|
||
.get("model_providers")
|
||
.and_then(|v| v.get("any"))
|
||
.and_then(|v| v.get("wire_api"))
|
||
.and_then(|v| v.as_str());
|
||
assert_eq!(wire_api, Some("responses"));
|
||
}
|
||
|
||
#[test]
|
||
fn base_url_creates_section_when_missing() {
|
||
let input = r#"model_provider = "custom"
|
||
model = "gpt-4"
|
||
"#;
|
||
|
||
let result = update_codex_toml_field(input, "base_url", "https://custom.api/v1").unwrap();
|
||
let parsed: toml::Value = toml::from_str(&result).unwrap();
|
||
|
||
let base_url = parsed
|
||
.get("model_providers")
|
||
.and_then(|v| v.get("custom"))
|
||
.and_then(|v| v.get("base_url"))
|
||
.and_then(|v| v.as_str())
|
||
.expect("should create section and set base_url");
|
||
assert_eq!(base_url, "https://custom.api/v1");
|
||
}
|
||
|
||
#[test]
|
||
fn base_url_falls_back_to_top_level_without_model_provider() {
|
||
let input = r#"model = "gpt-4"
|
||
"#;
|
||
|
||
let result = update_codex_toml_field(input, "base_url", "https://fallback.api/v1").unwrap();
|
||
let parsed: toml::Value = toml::from_str(&result).unwrap();
|
||
|
||
let base_url = parsed
|
||
.get("base_url")
|
||
.and_then(|v| v.as_str())
|
||
.expect("should set top-level base_url");
|
||
assert_eq!(base_url, "https://fallback.api/v1");
|
||
}
|
||
|
||
#[test]
|
||
fn clearing_base_url_removes_only_from_correct_section() {
|
||
let input = r#"model_provider = "any"
|
||
|
||
[model_providers.any]
|
||
name = "any"
|
||
base_url = "https://old.api/v1"
|
||
wire_api = "responses"
|
||
|
||
[mcp_servers.context7]
|
||
command = "npx"
|
||
"#;
|
||
|
||
let result = update_codex_toml_field(input, "base_url", "").unwrap();
|
||
let parsed: toml::Value = toml::from_str(&result).unwrap();
|
||
|
||
// base_url removed from model_providers.any
|
||
let any_section = parsed
|
||
.get("model_providers")
|
||
.and_then(|v| v.get("any"))
|
||
.expect("model_providers.any should exist");
|
||
assert!(any_section.get("base_url").is_none());
|
||
|
||
// wire_api preserved
|
||
assert_eq!(
|
||
any_section.get("wire_api").and_then(|v| v.as_str()),
|
||
Some("responses")
|
||
);
|
||
|
||
// mcp_servers untouched
|
||
assert!(parsed.get("mcp_servers").is_some());
|
||
}
|
||
|
||
#[test]
|
||
fn model_field_operates_on_top_level() {
|
||
let input = r#"model_provider = "any"
|
||
model = "gpt-4"
|
||
|
||
[model_providers.any]
|
||
name = "any"
|
||
"#;
|
||
|
||
let result = update_codex_toml_field(input, "model", "gpt-5").unwrap();
|
||
let parsed: toml::Value = toml::from_str(&result).unwrap();
|
||
assert_eq!(parsed.get("model").and_then(|v| v.as_str()), Some("gpt-5"));
|
||
|
||
// Clear model
|
||
let result2 = update_codex_toml_field(&result, "model", "").unwrap();
|
||
let parsed2: toml::Value = toml::from_str(&result2).unwrap();
|
||
assert!(parsed2.get("model").is_none());
|
||
}
|
||
|
||
#[test]
|
||
fn preserves_comments_and_whitespace() {
|
||
let input = r#"# My Codex config
|
||
model_provider = "any"
|
||
model = "gpt-4"
|
||
|
||
# Provider section
|
||
[model_providers.any]
|
||
name = "any"
|
||
base_url = "https://old.api/v1"
|
||
"#;
|
||
|
||
let result = update_codex_toml_field(input, "base_url", "https://new.api/v1").unwrap();
|
||
|
||
// Comments should be preserved
|
||
assert!(result.contains("# My Codex config"));
|
||
assert!(result.contains("# Provider section"));
|
||
}
|
||
|
||
#[test]
|
||
fn does_not_misplace_when_profiles_section_follows() {
|
||
let input = r#"model_provider = "any"
|
||
|
||
[model_providers.any]
|
||
name = "any"
|
||
base_url = "https://old.api/v1"
|
||
|
||
[profiles.default]
|
||
model = "gpt-4"
|
||
"#;
|
||
|
||
let result = update_codex_toml_field(input, "base_url", "https://new.api/v1").unwrap();
|
||
let parsed: toml::Value = toml::from_str(&result).unwrap();
|
||
|
||
// base_url in correct section
|
||
let base_url = parsed
|
||
.get("model_providers")
|
||
.and_then(|v| v.get("any"))
|
||
.and_then(|v| v.get("base_url"))
|
||
.and_then(|v| v.as_str());
|
||
assert_eq!(base_url, Some("https://new.api/v1"));
|
||
|
||
// profiles section untouched
|
||
let profile_model = parsed
|
||
.get("profiles")
|
||
.and_then(|v| v.get("default"))
|
||
.and_then(|v| v.get("model"))
|
||
.and_then(|v| v.as_str());
|
||
assert_eq!(profile_model, Some("gpt-4"));
|
||
}
|
||
|
||
#[test]
|
||
fn remove_base_url_if_predicate() {
|
||
let input = r#"model_provider = "any"
|
||
|
||
[model_providers.any]
|
||
name = "any"
|
||
base_url = "http://127.0.0.1:5000/v1"
|
||
wire_api = "responses"
|
||
"#;
|
||
|
||
let result =
|
||
remove_codex_toml_base_url_if(input, |url| url.starts_with("http://127.0.0.1"));
|
||
let parsed: toml::Value = toml::from_str(&result).unwrap();
|
||
|
||
let any_section = parsed
|
||
.get("model_providers")
|
||
.and_then(|v| v.get("any"))
|
||
.unwrap();
|
||
assert!(any_section.get("base_url").is_none());
|
||
assert_eq!(
|
||
any_section.get("wire_api").and_then(|v| v.as_str()),
|
||
Some("responses")
|
||
);
|
||
}
|
||
|
||
#[test]
|
||
fn remove_base_url_if_keeps_non_matching() {
|
||
let input = r#"model_provider = "any"
|
||
|
||
[model_providers.any]
|
||
base_url = "https://production.api/v1"
|
||
"#;
|
||
|
||
let result =
|
||
remove_codex_toml_base_url_if(input, |url| url.starts_with("http://127.0.0.1"));
|
||
let parsed: toml::Value = toml::from_str(&result).unwrap();
|
||
|
||
let base_url = parsed
|
||
.get("model_providers")
|
||
.and_then(|v| v.get("any"))
|
||
.and_then(|v| v.get("base_url"))
|
||
.and_then(|v| v.as_str());
|
||
assert_eq!(base_url, Some("https://production.api/v1"));
|
||
}
|
||
}
|