Files

1 line
6.4 KiB
JSON

{"repo": "chacelyee2101/llm-router", "files": {"src-tauri/src/providers/url.rs": {"1e2bb58d81b6fc8ddc4677722fa8a281fa4f9a92": {"t": 1785746249.5686474, "n": 2958, "h": "1b79b3d859b70399", "c": "//! Base-URL normalization shared by all OpenAI-/Anthropic-compatible adapters.\n//!\n//! Users commonly enter a provider's base URL in one of several forms:\n//!\n//! https://api.deepseek.com\n//! https://api.deepseek.com/\n//! https://api.deepseek.com/v1\n//! https://api.deepseek.com/v1/\n//!\n//! Our adapters construct request URLs as `{base_url}/v1/<endpoint>`. To keep\n//! all four input forms working, we strip a trailing `/v1` (and any trailing\n//! slashes) at provider construction time. The adapter then appends its own\n//! `/v1/<endpoint>` cleanly.\n//!\n//! This also handles compound prefixes like `https://gateway.example.com/openai/v1`\n//! → strips to `https://gateway.example.com/openai` → final URL is\n//! `https://gateway.example.com/openai/v1/chat/completions`.\n\n/// Normalize a provider base URL so the adapter can append `/v1/<endpoint>`\n/// without producing duplicated path segments.\npub fn normalize_base_url(url: &str) -> String {\n let trimmed = url.trim().trim_end_matches('/');\n\n if let Some(stripped) = trimmed.strip_suffix(\"/v1\") {\n return stripped.to_string();\n }\n\n trimmed.to_string()\n}\n\n#[cfg(test)]\nmod tests {\n use super::*;\n\n #[test]\n fn plain_root_is_unchanged() {\n assert_eq!(\n normalize_base_url(\"https://api.deepseek.com\"),\n \"https://api.deepseek.com\"\n );\n }\n\n #[test]\n fn trailing_slash_is_stripped() {\n assert_eq!(\n normalize_base_url(\"https://api.deepseek.com/\"),\n \"https://api.deepseek.com\"\n );\n }\n\n #[test]\n fn trailing_v1_is_stripped() {\n assert_eq!(\n normalize_base_url(\"https://api.deepseek.com/v1\"),\n \"https://api.deepseek.com\"\n );\n }\n\n #[test]\n fn trailing_v1_with_slash_is_stripped() {\n assert_eq!(\n normalize_base_url(\"https://api.deepseek.com/v1/\"),\n \"https://api.deepseek.com\"\n );\n }\n\n #[test]\n fn multiple_trailing_slashes_are_stripped() {\n assert_eq!(\n normalize_base_url(\"https://api.deepseek.com///\"),\n \"https://api.deepseek.com\"\n );\n }\n\n #[test]\n fn compound_prefix_with_trailing_v1() {\n assert_eq!(\n normalize_base_url(\"https://gateway.example.com/openai/v1\"),\n \"https://gateway.example.com/openai\"\n );\n }\n\n #[test]\n fn v1_not_at_end_is_preserved() {\n // /v1 appears mid-path; treat as part of an unusual but intentional route.\n assert_eq!(\n normalize_base_url(\"https://gateway.example.com/v1/proxy\"),\n \"https://gateway.example.com/v1/proxy\"\n );\n }\n\n #[test]\n fn surrounding_whitespace_is_trimmed() {\n assert_eq!(\n normalize_base_url(\" https://api.deepseek.com/v1 \"),\n \"https://api.deepseek.com\"\n );\n }\n\n #[test]\n fn empty_string_returns_empty() {\n assert_eq!(normalize_base_url(\"\"), \"\");\n }\n}\n"}, "c9cac055445ec2983236520791e9ff83854bdd6e": {"t": 1785761083.8600352, "n": 2958, "h": "1b79b3d859b70399", "c": "//! Base-URL normalization shared by all OpenAI-/Anthropic-compatible adapters.\n//!\n//! Users commonly enter a provider's base URL in one of several forms:\n//!\n//! https://api.deepseek.com\n//! https://api.deepseek.com/\n//! https://api.deepseek.com/v1\n//! https://api.deepseek.com/v1/\n//!\n//! Our adapters construct request URLs as `{base_url}/v1/<endpoint>`. To keep\n//! all four input forms working, we strip a trailing `/v1` (and any trailing\n//! slashes) at provider construction time. The adapter then appends its own\n//! `/v1/<endpoint>` cleanly.\n//!\n//! This also handles compound prefixes like `https://gateway.example.com/openai/v1`\n//! → strips to `https://gateway.example.com/openai` → final URL is\n//! `https://gateway.example.com/openai/v1/chat/completions`.\n\n/// Normalize a provider base URL so the adapter can append `/v1/<endpoint>`\n/// without producing duplicated path segments.\npub fn normalize_base_url(url: &str) -> String {\n let trimmed = url.trim().trim_end_matches('/');\n\n if let Some(stripped) = trimmed.strip_suffix(\"/v1\") {\n return stripped.to_string();\n }\n\n trimmed.to_string()\n}\n\n#[cfg(test)]\nmod tests {\n use super::*;\n\n #[test]\n fn plain_root_is_unchanged() {\n assert_eq!(\n normalize_base_url(\"https://api.deepseek.com\"),\n \"https://api.deepseek.com\"\n );\n }\n\n #[test]\n fn trailing_slash_is_stripped() {\n assert_eq!(\n normalize_base_url(\"https://api.deepseek.com/\"),\n \"https://api.deepseek.com\"\n );\n }\n\n #[test]\n fn trailing_v1_is_stripped() {\n assert_eq!(\n normalize_base_url(\"https://api.deepseek.com/v1\"),\n \"https://api.deepseek.com\"\n );\n }\n\n #[test]\n fn trailing_v1_with_slash_is_stripped() {\n assert_eq!(\n normalize_base_url(\"https://api.deepseek.com/v1/\"),\n \"https://api.deepseek.com\"\n );\n }\n\n #[test]\n fn multiple_trailing_slashes_are_stripped() {\n assert_eq!(\n normalize_base_url(\"https://api.deepseek.com///\"),\n \"https://api.deepseek.com\"\n );\n }\n\n #[test]\n fn compound_prefix_with_trailing_v1() {\n assert_eq!(\n normalize_base_url(\"https://gateway.example.com/openai/v1\"),\n \"https://gateway.example.com/openai\"\n );\n }\n\n #[test]\n fn v1_not_at_end_is_preserved() {\n // /v1 appears mid-path; treat as part of an unusual but intentional route.\n assert_eq!(\n normalize_base_url(\"https://gateway.example.com/v1/proxy\"),\n \"https://gateway.example.com/v1/proxy\"\n );\n }\n\n #[test]\n fn surrounding_whitespace_is_trimmed() {\n assert_eq!(\n normalize_base_url(\" https://api.deepseek.com/v1 \"),\n \"https://api.deepseek.com\"\n );\n }\n\n #[test]\n fn empty_string_returns_empty() {\n assert_eq!(normalize_base_url(\"\"), \"\");\n }\n}\n"}}}}