Skip to main content

systemprompt_models/profile/
gateway.rs

1use serde::{Deserialize, Serialize};
2use std::collections::HashMap;
3
4#[derive(Debug, Clone, Default, Serialize, Deserialize)]
5pub struct GatewayConfig {
6    #[serde(default)]
7    pub enabled: bool,
8    #[serde(default)]
9    pub routes: Vec<GatewayRoute>,
10}
11
12impl GatewayConfig {
13    pub fn find_route(&self, model: &str) -> Option<&GatewayRoute> {
14        self.routes.iter().find(|route| route.matches(model))
15    }
16}
17
18#[derive(Debug, Clone, Serialize, Deserialize)]
19pub struct GatewayRoute {
20    pub model_pattern: String,
21    pub provider: String,
22    pub endpoint: String,
23    pub api_key_secret: String,
24    #[serde(default, skip_serializing_if = "Option::is_none")]
25    pub upstream_model: Option<String>,
26    #[serde(default, skip_serializing_if = "HashMap::is_empty")]
27    pub extra_headers: HashMap<String, String>,
28}
29
30impl GatewayRoute {
31    pub fn matches(&self, model: &str) -> bool {
32        match_pattern(&self.model_pattern, model)
33    }
34
35    pub fn effective_upstream_model<'a>(&'a self, requested: &'a str) -> &'a str {
36        self.upstream_model.as_deref().unwrap_or(requested)
37    }
38}
39
40fn match_pattern(pattern: &str, model: &str) -> bool {
41    if pattern == "*" {
42        return true;
43    }
44    if let Some(prefix) = pattern.strip_suffix('*') {
45        return model.starts_with(prefix);
46    }
47    if let Some(suffix) = pattern.strip_prefix('*') {
48        return model.ends_with(suffix);
49    }
50    pattern == model
51}