Skip to main content

systemprompt_models/bridge/
profile.rs

1//! Wire contract for `GET /v1/bridge/profile`.
2//!
3//! The desktop bridge (`bin/bridge`) fetches this to render host configuration
4//! and to decide which provider models each host advertises. The server
5//! (`crates/entry/api`) produces it and the bridge consumes it through these
6//! exact types, so the two sides cannot drift.
7//!
8//! Every field is derived in [`build`] from
9//! [`ProviderRegistry::advertised_providers`], the single bearer of the
10//! advertisement rule ([`ApiSurface::is_advertised`]). A `surface: backend`
11//! provider is therefore structurally absent from both `providers` and the
12//! flat `models` front door — the flat list is a projection of the same
13//! advertised set, so it can never disagree with `providers`.
14//!
15//! Copyright (c) systemprompt.io — Business Source License 1.1.
16//! See <https://systemprompt.io> for licensing details.
17
18use serde::{Deserialize, Serialize};
19
20use crate::profile::{ApiSurface, ProviderRegistry};
21
22#[derive(Debug, Clone, Serialize, Deserialize)]
23pub struct BridgeProfileResponse {
24    pub inference_gateway_base_url: String,
25    pub auth_scheme: String,
26    #[serde(default)]
27    pub models: Vec<String>,
28    // Why: `None` leaves the choice to the client, which is the behaviour
29    // before this field existed.
30    #[serde(default, skip_serializing_if = "Option::is_none")]
31    pub default_model: Option<String>,
32    #[serde(default)]
33    pub organization_uuid: Option<String>,
34    #[serde(default)]
35    pub providers: Vec<ProviderHealth>,
36}
37
38/// A provider whose credential secret is absent is flagged
39/// (`configured = false`) rather than dropped silently.
40#[derive(Debug, Clone, Serialize, Deserialize)]
41pub struct ProviderHealth {
42    pub name: String,
43    pub surface: ApiSurface,
44    pub configured: bool,
45    #[serde(default)]
46    pub models: Vec<String>,
47    #[serde(default, skip_serializing_if = "Option::is_none")]
48    pub config_issue: Option<String>,
49}
50
51pub fn provider_health(
52    registry: &ProviderRegistry,
53    secret_present: impl Fn(&str) -> bool,
54) -> Vec<ProviderHealth> {
55    registry
56        .advertised_providers()
57        .map(|entry| {
58            let secret = entry.api_key_secret.as_str();
59            let configured = secret_present(secret);
60            ProviderHealth {
61                name: entry.name.as_str().to_owned(),
62                surface: entry.surface,
63                configured,
64                models: entry
65                    .models
66                    .iter()
67                    .flat_map(|m| {
68                        std::iter::once(m.id.as_str().to_owned())
69                            .chain(m.aliases.iter().map(|a| a.as_str().to_owned()))
70                    })
71                    .collect(),
72                config_issue: (!configured)
73                    .then(|| format!("API key secret '{secret}' is not configured")),
74            }
75        })
76        .collect()
77}
78
79#[derive(Debug, Clone)]
80pub struct BridgeProfileParams<'a> {
81    pub inference_gateway_base_url: String,
82    pub auth_scheme: String,
83    pub organization_uuid: Option<String>,
84    pub default_model: Option<String>,
85    pub registry: &'a ProviderRegistry,
86}
87
88#[must_use]
89pub fn build(
90    params: BridgeProfileParams<'_>,
91    secret_present: impl Fn(&str) -> bool,
92) -> BridgeProfileResponse {
93    let BridgeProfileParams {
94        inference_gateway_base_url,
95        auth_scheme,
96        organization_uuid,
97        default_model,
98        registry,
99    } = params;
100    BridgeProfileResponse {
101        inference_gateway_base_url,
102        auth_scheme,
103        models: registry.advertised_model_ids(&[ApiSurface::Anthropic]),
104        default_model,
105        organization_uuid,
106        providers: provider_health(registry, secret_present),
107    }
108}