mirror of
https://wget.la/https://github.com/leookun/cursor-byok
synced 2026-10-05 12:13:05 +08:00
merge old config
This commit is contained in:
@@ -0,0 +1,508 @@
|
||||
use std::{fmt, str::FromStr};
|
||||
|
||||
use reqwest::Url;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sha2::{Digest, Sha256};
|
||||
|
||||
use crate::{Error, Result};
|
||||
|
||||
pub const OPENAI_RESPONSES_ENDPOINT: &str = "/v1/responses";
|
||||
pub const OPENAI_CHAT_ENDPOINT: &str = "/v1/chat/completions";
|
||||
|
||||
#[derive(Clone, Copy, Debug, Deserialize, Serialize, PartialEq, Eq)]
|
||||
pub enum ProviderType {
|
||||
#[serde(rename = "openai-chat")]
|
||||
OpenAiChat,
|
||||
#[serde(rename = "openai-responses")]
|
||||
OpenAiResponses,
|
||||
#[serde(rename = "anthropic")]
|
||||
Anthropic,
|
||||
}
|
||||
|
||||
impl ProviderType {
|
||||
pub fn as_str(self) -> &'static str {
|
||||
match self {
|
||||
Self::OpenAiChat => "openai-chat",
|
||||
Self::OpenAiResponses => "openai-responses",
|
||||
Self::Anthropic => "anthropic",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for ProviderType {
|
||||
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
formatter.write_str(self.as_str())
|
||||
}
|
||||
}
|
||||
|
||||
impl FromStr for ProviderType {
|
||||
type Err = Error;
|
||||
|
||||
fn from_str(value: &str) -> Result<Self> {
|
||||
match value {
|
||||
"openai-chat" => Ok(Self::OpenAiChat),
|
||||
"openai-responses" => Ok(Self::OpenAiResponses),
|
||||
"anthropic" => Ok(Self::Anthropic),
|
||||
_ => Err(Error::Config(format!("unsupported provider type: {value}"))),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Deserialize, Serialize, PartialEq, Eq)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum ModelType {
|
||||
OpenAi,
|
||||
Anthropic,
|
||||
}
|
||||
|
||||
impl ModelType {
|
||||
pub fn as_str(self) -> &'static str {
|
||||
match self {
|
||||
Self::OpenAi => "openai",
|
||||
Self::Anthropic => "anthropic",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl FromStr for ModelType {
|
||||
type Err = Error;
|
||||
|
||||
fn from_str(value: &str) -> Result<Self> {
|
||||
match value {
|
||||
"openai" => Ok(Self::OpenAi),
|
||||
"anthropic" => Ok(Self::Anthropic),
|
||||
_ => Err(Error::Config(format!("unsupported model type: {value}"))),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||
pub struct ModelConfigInput {
|
||||
#[serde(default)]
|
||||
pub sort_order: i64,
|
||||
pub display_name: String,
|
||||
#[serde(rename = "type")]
|
||||
pub model_type: ModelType,
|
||||
pub base_url: String,
|
||||
#[serde(default)]
|
||||
pub use_full_url: bool,
|
||||
pub api_key: String,
|
||||
pub tooltip_data: String,
|
||||
pub model_id: String,
|
||||
#[serde(default)]
|
||||
pub reasoning_effort: Option<String>,
|
||||
#[serde(default)]
|
||||
pub openai_endpoint: String,
|
||||
#[serde(default)]
|
||||
pub openai_extra_params_enabled: bool,
|
||||
#[serde(default = "empty_object")]
|
||||
pub openai_extra_params: serde_json::Value,
|
||||
#[serde(default)]
|
||||
pub custom_headers_enabled: bool,
|
||||
#[serde(default = "empty_object")]
|
||||
pub custom_headers: serde_json::Value,
|
||||
#[serde(default)]
|
||||
pub anthropic_extra_params_enabled: bool,
|
||||
#[serde(default = "empty_object")]
|
||||
pub anthropic_extra_params: serde_json::Value,
|
||||
pub context_window_tokens: Option<u64>,
|
||||
pub max_completion_tokens: Option<u64>,
|
||||
pub anthropic_max_tokens: Option<u64>,
|
||||
#[serde(default)]
|
||||
pub anthropic_thinking_effort: Option<String>,
|
||||
pub thinking_budget_tokens: Option<u64>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize)]
|
||||
pub struct ModelConfig {
|
||||
pub model_hash: String,
|
||||
pub sort_order: i64,
|
||||
pub display_name: String,
|
||||
#[serde(rename = "type")]
|
||||
pub model_type: ModelType,
|
||||
pub base_url: String,
|
||||
pub use_full_url: bool,
|
||||
pub api_key: String,
|
||||
pub tooltip_data: String,
|
||||
pub model_id: String,
|
||||
pub reasoning_effort: Option<String>,
|
||||
pub openai_endpoint: String,
|
||||
pub openai_extra_params_enabled: bool,
|
||||
pub openai_extra_params: serde_json::Value,
|
||||
pub custom_headers_enabled: bool,
|
||||
pub custom_headers: serde_json::Value,
|
||||
pub anthropic_extra_params_enabled: bool,
|
||||
pub anthropic_extra_params: serde_json::Value,
|
||||
pub context_window_tokens: Option<u64>,
|
||||
pub max_completion_tokens: Option<u64>,
|
||||
pub anthropic_max_tokens: Option<u64>,
|
||||
pub anthropic_thinking_effort: Option<String>,
|
||||
pub thinking_budget_tokens: Option<u64>,
|
||||
pub created_at_ms: i64,
|
||||
pub updated_at_ms: i64,
|
||||
}
|
||||
|
||||
impl ModelConfig {
|
||||
pub fn provider_type(&self) -> ProviderType {
|
||||
match self.model_type {
|
||||
ModelType::Anthropic => ProviderType::Anthropic,
|
||||
ModelType::OpenAi if self.openai_endpoint == OPENAI_RESPONSES_ENDPOINT => {
|
||||
ProviderType::OpenAiResponses
|
||||
}
|
||||
ModelType::OpenAi => ProviderType::OpenAiChat,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn request_url(&self) -> Result<String> {
|
||||
resolve_request_url(
|
||||
self.model_type,
|
||||
&self.base_url,
|
||||
&self.openai_endpoint,
|
||||
self.use_full_url,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn max_output_tokens(&self) -> Option<u64> {
|
||||
match self.model_type {
|
||||
ModelType::OpenAi => self.max_completion_tokens,
|
||||
ModelType::Anthropic => self.anthropic_max_tokens.or(self.max_completion_tokens),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn extra_params(&self) -> &serde_json::Value {
|
||||
match self.model_type {
|
||||
ModelType::OpenAi if self.openai_extra_params_enabled => &self.openai_extra_params,
|
||||
ModelType::Anthropic if self.anthropic_extra_params_enabled => {
|
||||
&self.anthropic_extra_params
|
||||
}
|
||||
_ => empty_object_ref(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn configure(&self, model: &mut super::ModelSpec) {
|
||||
model.display_name = Some(self.display_name.clone());
|
||||
if model.reasoning.effort.is_none() {
|
||||
model.reasoning.effort = match self.model_type {
|
||||
ModelType::OpenAi => self.reasoning_effort.clone(),
|
||||
ModelType::Anthropic => self.anthropic_thinking_effort.clone(),
|
||||
};
|
||||
}
|
||||
model.reasoning.enabled |= model.reasoning.effort.is_some();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn normalize_model_input(input: &ModelConfigInput) -> Result<ModelConfigInput> {
|
||||
let display_name = required(&input.display_name, "model display name")?;
|
||||
let base_url = normalize_request_url(&input.base_url)?;
|
||||
let api_key = required(&input.api_key, "model API key")?;
|
||||
let tooltip_data = required(&input.tooltip_data, "model tooltip")?;
|
||||
let model_id = required(&input.model_id, "model id")?;
|
||||
let reasoning_effort = normalize_effort(input.reasoning_effort.as_deref(), true)?;
|
||||
let anthropic_thinking_effort = match input.model_type {
|
||||
ModelType::Anthropic => Some(
|
||||
normalize_effort(
|
||||
input.anthropic_thinking_effort.as_deref().or(Some("xhigh")),
|
||||
false,
|
||||
)?
|
||||
.expect("Anthropic effort has a default"),
|
||||
),
|
||||
ModelType::OpenAi => None,
|
||||
};
|
||||
let openai_endpoint = match input.model_type {
|
||||
ModelType::OpenAi => normalize_openai_endpoint(&input.openai_endpoint)?,
|
||||
ModelType::Anthropic => String::new(),
|
||||
};
|
||||
validate_object(&input.openai_extra_params, "OpenAI extra params")?;
|
||||
validate_object(&input.anthropic_extra_params, "Anthropic extra params")?;
|
||||
validate_headers(&input.custom_headers)?;
|
||||
|
||||
let normalized = ModelConfigInput {
|
||||
sort_order: input.sort_order.max(0),
|
||||
display_name,
|
||||
model_type: input.model_type,
|
||||
base_url,
|
||||
use_full_url: input.use_full_url,
|
||||
api_key,
|
||||
tooltip_data,
|
||||
model_id,
|
||||
reasoning_effort: (input.model_type == ModelType::OpenAi)
|
||||
.then_some(reasoning_effort)
|
||||
.flatten(),
|
||||
openai_endpoint,
|
||||
openai_extra_params_enabled: input.model_type == ModelType::OpenAi
|
||||
&& input.openai_extra_params_enabled,
|
||||
openai_extra_params: if input.model_type == ModelType::OpenAi {
|
||||
input.openai_extra_params.clone()
|
||||
} else {
|
||||
empty_object()
|
||||
},
|
||||
custom_headers_enabled: input.custom_headers_enabled,
|
||||
custom_headers: input.custom_headers.clone(),
|
||||
anthropic_extra_params_enabled: input.model_type == ModelType::Anthropic
|
||||
&& input.anthropic_extra_params_enabled,
|
||||
anthropic_extra_params: if input.model_type == ModelType::Anthropic {
|
||||
input.anthropic_extra_params.clone()
|
||||
} else {
|
||||
empty_object()
|
||||
},
|
||||
context_window_tokens: positive(input.context_window_tokens, "context window")?,
|
||||
max_completion_tokens: positive(input.max_completion_tokens, "max completion tokens")?,
|
||||
anthropic_max_tokens: positive(input.anthropic_max_tokens, "Anthropic max tokens")?,
|
||||
anthropic_thinking_effort,
|
||||
thinking_budget_tokens: positive(input.thinking_budget_tokens, "thinking budget")?,
|
||||
};
|
||||
resolve_request_url(
|
||||
normalized.model_type,
|
||||
&normalized.base_url,
|
||||
&normalized.openai_endpoint,
|
||||
normalized.use_full_url,
|
||||
)?;
|
||||
Ok(normalized)
|
||||
}
|
||||
|
||||
pub fn model_hash(input: &ModelConfigInput) -> Result<String> {
|
||||
let normalized = normalize_model_input(input)?;
|
||||
let request_url = resolve_request_url(
|
||||
normalized.model_type,
|
||||
&normalized.base_url,
|
||||
&normalized.openai_endpoint,
|
||||
normalized.use_full_url,
|
||||
)?;
|
||||
let mut parts = vec![
|
||||
request_url,
|
||||
normalized.model_id,
|
||||
normalized.api_key,
|
||||
normalized.display_name,
|
||||
];
|
||||
if normalized.model_type == ModelType::OpenAi {
|
||||
parts.push(normalized.openai_endpoint);
|
||||
}
|
||||
let digest = Sha256::digest(parts.join("\n").as_bytes());
|
||||
Ok(hex::encode(&digest[..8]))
|
||||
}
|
||||
|
||||
pub fn normalize_request_url(value: &str) -> Result<String> {
|
||||
let value = value.trim();
|
||||
let url = Url::parse(value)
|
||||
.map_err(|error| Error::Config(format!("invalid model request URL: {error}")))?;
|
||||
if !matches!(url.scheme(), "http" | "https") || url.host_str().is_none() {
|
||||
return Err(Error::Config(
|
||||
"model request URL must be an HTTP(S) URL with a host".into(),
|
||||
));
|
||||
}
|
||||
if url.fragment().is_some() {
|
||||
return Err(Error::Config(
|
||||
"model request URL cannot contain a fragment".into(),
|
||||
));
|
||||
}
|
||||
Ok(value.into())
|
||||
}
|
||||
|
||||
pub fn resolve_request_url(
|
||||
model_type: ModelType,
|
||||
base_url: &str,
|
||||
openai_endpoint: &str,
|
||||
use_full_url: bool,
|
||||
) -> Result<String> {
|
||||
let base_url = normalize_request_url(base_url)?;
|
||||
let endpoint = match model_type {
|
||||
ModelType::OpenAi => normalize_openai_endpoint(openai_endpoint)?,
|
||||
ModelType::Anthropic => "/v1/messages".into(),
|
||||
};
|
||||
if use_full_url {
|
||||
return Ok(base_url);
|
||||
}
|
||||
append_standard_endpoint(&base_url, &endpoint)
|
||||
}
|
||||
|
||||
fn append_standard_endpoint(base_url: &str, endpoint: &str) -> Result<String> {
|
||||
let mut url = Url::parse(base_url)
|
||||
.map_err(|error| Error::Config(format!("invalid model server URL: {error}")))?;
|
||||
let base_path = url.path().trim_end_matches('/').to_string();
|
||||
let endpoint = if has_trailing_version(&base_path) {
|
||||
endpoint.strip_prefix("/v1").unwrap_or(endpoint)
|
||||
} else {
|
||||
endpoint
|
||||
};
|
||||
url.set_path(&format!("{base_path}{endpoint}"));
|
||||
normalize_request_url(url.as_str())
|
||||
}
|
||||
|
||||
fn has_trailing_version(path: &str) -> bool {
|
||||
let Some(segment) = path.rsplit('/').next() else {
|
||||
return false;
|
||||
};
|
||||
segment.strip_prefix('v').is_some_and(|digits| {
|
||||
!digits.is_empty() && digits.bytes().all(|byte| byte.is_ascii_digit())
|
||||
})
|
||||
}
|
||||
|
||||
pub fn is_sensitive_header(name: &str) -> bool {
|
||||
matches!(
|
||||
name.to_ascii_lowercase().as_str(),
|
||||
"authorization" | "proxy-authorization" | "x-api-key" | "api-key" | "cookie" | "set-cookie"
|
||||
)
|
||||
}
|
||||
|
||||
fn normalize_openai_endpoint(value: &str) -> Result<String> {
|
||||
match value.trim() {
|
||||
"" | OPENAI_RESPONSES_ENDPOINT => Ok(OPENAI_RESPONSES_ENDPOINT.into()),
|
||||
OPENAI_CHAT_ENDPOINT => Ok(OPENAI_CHAT_ENDPOINT.into()),
|
||||
value => Err(Error::Config(format!(
|
||||
"unsupported OpenAI endpoint: {value}"
|
||||
))),
|
||||
}
|
||||
}
|
||||
|
||||
fn normalize_effort(value: Option<&str>, allow_empty: bool) -> Result<Option<String>> {
|
||||
let value = value.unwrap_or_default().trim().to_ascii_lowercase();
|
||||
if value.is_empty() && allow_empty {
|
||||
return Ok(None);
|
||||
}
|
||||
if matches!(value.as_str(), "low" | "medium" | "high" | "xhigh" | "max") {
|
||||
Ok(Some(value))
|
||||
} else {
|
||||
Err(Error::Config(format!(
|
||||
"unsupported reasoning effort: {value}"
|
||||
)))
|
||||
}
|
||||
}
|
||||
|
||||
fn positive(value: Option<u64>, label: &str) -> Result<Option<u64>> {
|
||||
match value {
|
||||
Some(0) => Err(Error::Config(format!("{label} must be greater than zero"))),
|
||||
value => Ok(value),
|
||||
}
|
||||
}
|
||||
|
||||
fn required(value: &str, label: &str) -> Result<String> {
|
||||
let value = value.trim();
|
||||
if value.is_empty() {
|
||||
Err(Error::Config(format!("{label} cannot be empty")))
|
||||
} else {
|
||||
Ok(value.into())
|
||||
}
|
||||
}
|
||||
|
||||
fn validate_object(value: &serde_json::Value, label: &str) -> Result<()> {
|
||||
if value.is_object() {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(Error::Config(format!("{label} must be a JSON object")))
|
||||
}
|
||||
}
|
||||
|
||||
fn validate_headers(value: &serde_json::Value) -> Result<()> {
|
||||
validate_object(value, "custom headers")?;
|
||||
for (name, value) in value.as_object().expect("validated object") {
|
||||
if name.trim().is_empty() || !value.is_string() {
|
||||
return Err(Error::Config(
|
||||
"custom headers must have non-empty names and string values".into(),
|
||||
));
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn empty_object() -> serde_json::Value {
|
||||
serde_json::json!({})
|
||||
}
|
||||
|
||||
fn empty_object_ref() -> &'static serde_json::Value {
|
||||
static EMPTY: std::sync::OnceLock<serde_json::Value> = std::sync::OnceLock::new();
|
||||
EMPTY.get_or_init(empty_object)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn input() -> ModelConfigInput {
|
||||
ModelConfigInput {
|
||||
sort_order: 1,
|
||||
display_name: "Model A".into(),
|
||||
model_type: ModelType::OpenAi,
|
||||
base_url: "https://example.com/custom/generate".into(),
|
||||
use_full_url: true,
|
||||
api_key: "secret".into(),
|
||||
tooltip_data: "Model A".into(),
|
||||
model_id: "model-a".into(),
|
||||
reasoning_effort: Some("high".into()),
|
||||
openai_endpoint: OPENAI_RESPONSES_ENDPOINT.into(),
|
||||
openai_extra_params_enabled: false,
|
||||
openai_extra_params: empty_object(),
|
||||
custom_headers_enabled: false,
|
||||
custom_headers: empty_object(),
|
||||
anthropic_extra_params_enabled: false,
|
||||
anthropic_extra_params: empty_object(),
|
||||
context_window_tokens: Some(200_000),
|
||||
max_completion_tokens: None,
|
||||
anthropic_max_tokens: None,
|
||||
anthropic_thinking_effort: None,
|
||||
thinking_budget_tokens: None,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hash_matches_the_v0049_channel_identity() {
|
||||
let input = input();
|
||||
let expected = Sha256::digest(
|
||||
"https://example.com/custom/generate\nmodel-a\nsecret\nModel A\n/v1/responses"
|
||||
.as_bytes(),
|
||||
);
|
||||
assert_eq!(model_hash(&input).unwrap(), hex::encode(&expected[..8]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn request_url_is_exact_and_protocol_does_not_depend_on_its_path() {
|
||||
assert_eq!(
|
||||
resolve_request_url(
|
||||
ModelType::OpenAi,
|
||||
"https://example.com/custom/generate?api-version=2026-01-01",
|
||||
OPENAI_RESPONSES_ENDPOINT,
|
||||
true,
|
||||
)
|
||||
.unwrap(),
|
||||
"https://example.com/custom/generate?api-version=2026-01-01"
|
||||
);
|
||||
assert_eq!(
|
||||
resolve_request_url(
|
||||
ModelType::OpenAi,
|
||||
"https://example.com/another/arbitrary/path",
|
||||
OPENAI_CHAT_ENDPOINT,
|
||||
true,
|
||||
)
|
||||
.unwrap(),
|
||||
"https://example.com/another/arbitrary/path"
|
||||
);
|
||||
assert_eq!(
|
||||
resolve_request_url(ModelType::Anthropic, "https://example.com/claude", "", true)
|
||||
.unwrap(),
|
||||
"https://example.com/claude"
|
||||
);
|
||||
assert_eq!(
|
||||
resolve_request_url(
|
||||
ModelType::Anthropic,
|
||||
"https://example.com/claude/",
|
||||
"",
|
||||
true
|
||||
)
|
||||
.unwrap(),
|
||||
"https://example.com/claude/"
|
||||
);
|
||||
assert_eq!(
|
||||
resolve_request_url(
|
||||
ModelType::OpenAi,
|
||||
"https://example.com/v1",
|
||||
OPENAI_RESPONSES_ENDPOINT,
|
||||
false,
|
||||
)
|
||||
.unwrap(),
|
||||
"https://example.com/v1/responses"
|
||||
);
|
||||
assert_eq!(
|
||||
resolve_request_url(ModelType::Anthropic, "https://example.com/v1", "", false).unwrap(),
|
||||
"https://example.com/v1/messages"
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
mod configuration;
|
||||
mod conversation;
|
||||
mod cursor_trace;
|
||||
mod inference;
|
||||
@@ -6,13 +7,13 @@ mod message;
|
||||
mod model_spec;
|
||||
mod overview;
|
||||
mod projection;
|
||||
mod provider;
|
||||
mod run;
|
||||
mod runtime_tag;
|
||||
mod token_count;
|
||||
mod tool;
|
||||
mod usage;
|
||||
|
||||
pub use configuration::*;
|
||||
pub use conversation::*;
|
||||
pub use cursor_trace::*;
|
||||
pub use inference::*;
|
||||
@@ -21,7 +22,6 @@ pub use message::*;
|
||||
pub use model_spec::*;
|
||||
pub use overview::*;
|
||||
pub use projection::*;
|
||||
pub use provider::*;
|
||||
pub use run::*;
|
||||
pub use runtime_tag::*;
|
||||
pub(crate) use token_count::*;
|
||||
|
||||
@@ -1,341 +0,0 @@
|
||||
use std::{fmt, str::FromStr};
|
||||
|
||||
use reqwest::Url;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sha2::{Digest, Sha256};
|
||||
|
||||
use crate::{Error, Result};
|
||||
|
||||
#[derive(Clone, Copy, Debug, Deserialize, Serialize, PartialEq, Eq)]
|
||||
pub enum ProviderType {
|
||||
#[serde(rename = "openai-chat")]
|
||||
OpenAiChat,
|
||||
#[serde(rename = "openai-responses")]
|
||||
OpenAiResponses,
|
||||
#[serde(rename = "anthropic")]
|
||||
Anthropic,
|
||||
}
|
||||
|
||||
impl ProviderType {
|
||||
pub fn as_str(self) -> &'static str {
|
||||
match self {
|
||||
Self::OpenAiChat => "openai-chat",
|
||||
Self::OpenAiResponses => "openai-responses",
|
||||
Self::Anthropic => "anthropic",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for ProviderType {
|
||||
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
formatter.write_str(self.as_str())
|
||||
}
|
||||
}
|
||||
|
||||
impl FromStr for ProviderType {
|
||||
type Err = Error;
|
||||
|
||||
fn from_str(value: &str) -> Result<Self> {
|
||||
match value {
|
||||
"openai-chat" => Ok(Self::OpenAiChat),
|
||||
"openai-responses" => Ok(Self::OpenAiResponses),
|
||||
"anthropic" => Ok(Self::Anthropic),
|
||||
_ => Err(Error::Config(format!("unsupported provider type: {value}"))),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize)]
|
||||
pub struct ProviderEndpoint {
|
||||
pub provider_id: i64,
|
||||
pub name: String,
|
||||
pub provider_type: ProviderType,
|
||||
pub base_url: String,
|
||||
pub api_key: Option<String>,
|
||||
pub has_api_key: bool,
|
||||
pub custom_headers: serde_json::Value,
|
||||
pub extra_params: serde_json::Value,
|
||||
pub created_at_ms: i64,
|
||||
pub updated_at_ms: i64,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct ProviderEndpointSecret {
|
||||
pub endpoint: ProviderEndpoint,
|
||||
pub custom_headers: serde_json::Value,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize)]
|
||||
pub struct ProviderEndpointInput {
|
||||
pub name: String,
|
||||
pub provider_type: ProviderType,
|
||||
pub base_url: String,
|
||||
#[serde(default)]
|
||||
pub api_key: Option<String>,
|
||||
#[serde(default = "empty_object")]
|
||||
pub custom_headers: serde_json::Value,
|
||||
#[serde(default = "empty_object")]
|
||||
pub extra_params: serde_json::Value,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||
pub struct ProviderModelInput {
|
||||
pub model_id: String,
|
||||
pub display_name: String,
|
||||
pub endpoint_type: ProviderType,
|
||||
#[serde(default)]
|
||||
pub request_url: String,
|
||||
#[serde(default = "enabled")]
|
||||
pub enabled: bool,
|
||||
#[serde(default)]
|
||||
pub sort_order: i64,
|
||||
pub context_window_tokens: Option<u64>,
|
||||
pub max_output_tokens: Option<u64>,
|
||||
#[serde(default)]
|
||||
pub reasoning_enabled: bool,
|
||||
pub reasoning_effort: Option<String>,
|
||||
#[serde(default)]
|
||||
pub supports_image_generation: bool,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize)]
|
||||
pub struct ProviderModel {
|
||||
pub model_hash: String,
|
||||
pub provider_id: i64,
|
||||
pub model_id: String,
|
||||
pub display_name: String,
|
||||
pub endpoint_type: ProviderType,
|
||||
pub request_url: String,
|
||||
pub enabled: bool,
|
||||
pub sort_order: i64,
|
||||
pub context_window_tokens: Option<u64>,
|
||||
pub max_output_tokens: Option<u64>,
|
||||
pub reasoning_enabled: bool,
|
||||
pub reasoning_effort: Option<String>,
|
||||
pub supports_image_generation: bool,
|
||||
pub created_at_ms: i64,
|
||||
pub updated_at_ms: i64,
|
||||
}
|
||||
|
||||
impl ProviderModel {
|
||||
pub fn configure(&self, model: &mut super::ModelSpec) {
|
||||
model.display_name = Some(self.display_name.clone());
|
||||
model.supports_image_generation = self.supports_image_generation;
|
||||
model.reasoning.enabled |= self.reasoning_enabled;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn normalize_base_url(value: &str) -> Result<String> {
|
||||
let mut url = Url::parse(value.trim())
|
||||
.map_err(|error| Error::Config(format!("invalid provider base URL: {error}")))?;
|
||||
if url.query().is_some() || url.fragment().is_some() {
|
||||
return Err(Error::Config(
|
||||
"provider base URL cannot contain query or fragment".into(),
|
||||
));
|
||||
}
|
||||
let path = url.path().trim_end_matches('/').to_string();
|
||||
url.set_path(if path.is_empty() { "/" } else { &path });
|
||||
Ok(url.as_str().trim_end_matches('/').to_string())
|
||||
}
|
||||
|
||||
pub fn model_hash(
|
||||
base_url: &str,
|
||||
api_key: &str,
|
||||
provider_type: ProviderType,
|
||||
model_id: &str,
|
||||
) -> Result<String> {
|
||||
let base_url = normalize_base_url(base_url)?;
|
||||
let model_id = model_id.trim();
|
||||
if model_id.is_empty() {
|
||||
return Err(Error::Config("model id cannot be empty".into()));
|
||||
}
|
||||
let mut digest = Sha256::new();
|
||||
digest.update(base_url.as_bytes());
|
||||
digest.update([0]);
|
||||
digest.update(api_key.as_bytes());
|
||||
digest.update([0]);
|
||||
digest.update(provider_type.as_str().as_bytes());
|
||||
digest.update([0]);
|
||||
digest.update(model_id.as_bytes());
|
||||
Ok(hex::encode(&digest.finalize()[..4]))
|
||||
}
|
||||
|
||||
pub fn resolve_request_url(
|
||||
base_url: &str,
|
||||
endpoint_type: ProviderType,
|
||||
request_url: &str,
|
||||
) -> Result<String> {
|
||||
let base_url = normalize_base_url(base_url)?;
|
||||
let request_url = request_url.trim();
|
||||
let combined = if request_url.starts_with("http://") || request_url.starts_with("https://") {
|
||||
let url = Url::parse(request_url)
|
||||
.map_err(|error| Error::Config(format!("invalid model request URL: {error}")))?;
|
||||
if url.host_str().is_none() {
|
||||
return Err(Error::Config(
|
||||
"model request URL must contain a host".into(),
|
||||
));
|
||||
}
|
||||
url.to_string()
|
||||
} else {
|
||||
let path = if request_url.is_empty() {
|
||||
match endpoint_type {
|
||||
ProviderType::OpenAiChat => "/v1/chat/completions",
|
||||
ProviderType::OpenAiResponses => "/v1/responses",
|
||||
ProviderType::Anthropic => "/v1/messages",
|
||||
}
|
||||
} else if request_url.starts_with('/') {
|
||||
request_url
|
||||
} else {
|
||||
return Err(Error::Config(
|
||||
"model request URL must be an HTTP(S) URL or start with /".into(),
|
||||
));
|
||||
};
|
||||
format!("{}{}", base_url.trim_end_matches('/'), path)
|
||||
};
|
||||
let mut normalized = combined;
|
||||
while normalized.contains("/v1/v1") {
|
||||
normalized = normalized.replace("/v1/v1", "/v1");
|
||||
}
|
||||
Ok(normalized)
|
||||
}
|
||||
|
||||
pub fn is_sensitive_header(name: &str) -> bool {
|
||||
matches!(
|
||||
name.to_ascii_lowercase().as_str(),
|
||||
"authorization" | "proxy-authorization" | "x-api-key" | "api-key" | "cookie" | "set-cookie"
|
||||
)
|
||||
}
|
||||
|
||||
fn empty_object() -> serde_json::Value {
|
||||
serde_json::json!({})
|
||||
}
|
||||
|
||||
fn enabled() -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn hash_uses_normalized_url_key_type_and_model() {
|
||||
let first = model_hash(
|
||||
"HTTPS://Example.COM/v1/",
|
||||
"secret",
|
||||
ProviderType::OpenAiChat,
|
||||
"model-a",
|
||||
)
|
||||
.unwrap();
|
||||
let second = model_hash(
|
||||
"https://example.com/v1",
|
||||
"secret",
|
||||
ProviderType::OpenAiChat,
|
||||
"model-a",
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(first, second);
|
||||
assert_ne!(
|
||||
first,
|
||||
model_hash(
|
||||
"https://example.com/v1",
|
||||
"different-secret",
|
||||
ProviderType::OpenAiChat,
|
||||
"model-a",
|
||||
)
|
||||
.unwrap()
|
||||
);
|
||||
assert_ne!(
|
||||
first,
|
||||
model_hash(
|
||||
"https://example.com/v1",
|
||||
"secret",
|
||||
ProviderType::Anthropic,
|
||||
"model-a",
|
||||
)
|
||||
.unwrap()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn provider_type_json_uses_public_identifiers() {
|
||||
for (value, provider_type) in [
|
||||
("openai-chat", ProviderType::OpenAiChat),
|
||||
("openai-responses", ProviderType::OpenAiResponses),
|
||||
("anthropic", ProviderType::Anthropic),
|
||||
] {
|
||||
assert_eq!(
|
||||
serde_json::from_str::<ProviderType>(&format!("\"{value}\"")).unwrap(),
|
||||
provider_type
|
||||
);
|
||||
assert_eq!(
|
||||
serde_json::to_string(&provider_type).unwrap(),
|
||||
format!("\"{value}\"")
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn resolves_default_relative_and_absolute_model_urls() {
|
||||
assert_eq!(
|
||||
resolve_request_url("https://example.com/v1", ProviderType::OpenAiChat, "").unwrap(),
|
||||
"https://example.com/v1/chat/completions"
|
||||
);
|
||||
assert_eq!(
|
||||
resolve_request_url("https://example.com/v1", ProviderType::OpenAiResponses, "")
|
||||
.unwrap(),
|
||||
"https://example.com/v1/responses"
|
||||
);
|
||||
assert_eq!(
|
||||
resolve_request_url("https://example.com", ProviderType::Anthropic, "").unwrap(),
|
||||
"https://example.com/v1/messages"
|
||||
);
|
||||
assert_eq!(
|
||||
resolve_request_url(
|
||||
"https://example.com",
|
||||
ProviderType::OpenAiChat,
|
||||
"/v2/chat/completions"
|
||||
)
|
||||
.unwrap(),
|
||||
"https://example.com/v2/chat/completions"
|
||||
);
|
||||
assert_eq!(
|
||||
resolve_request_url(
|
||||
"https://example.com/v1",
|
||||
ProviderType::OpenAiChat,
|
||||
"https://gateway.example/v1/v1/custom"
|
||||
)
|
||||
.unwrap(),
|
||||
"https://gateway.example/v1/custom"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn requested_runtime_limits_are_not_overridden_by_provider_config() {
|
||||
let provider = ProviderModel {
|
||||
model_hash: "12345678".into(),
|
||||
provider_id: 1,
|
||||
model_id: "model".into(),
|
||||
display_name: "Model".into(),
|
||||
endpoint_type: ProviderType::OpenAiResponses,
|
||||
request_url: String::new(),
|
||||
enabled: true,
|
||||
sort_order: 0,
|
||||
context_window_tokens: Some(200_000),
|
||||
max_output_tokens: None,
|
||||
reasoning_enabled: false,
|
||||
reasoning_effort: None,
|
||||
supports_image_generation: false,
|
||||
created_at_ms: 0,
|
||||
updated_at_ms: 0,
|
||||
};
|
||||
let mut selected = super::super::ModelSpec::new("12345678");
|
||||
selected.context_window_tokens = Some(800_000);
|
||||
provider.configure(&mut selected);
|
||||
assert_eq!(selected.context_window_tokens, Some(800_000));
|
||||
|
||||
let mut defaulted = super::super::ModelSpec::new("12345678");
|
||||
provider.configure(&mut defaulted);
|
||||
assert_eq!(defaulted.context_window_tokens, None);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user