Skip to main content

systemprompt_models/profile/providers/
protocol.rs

1//! Wire-protocol family for an upstream AI provider.
2//!
3//! [`WireProtocol`] names the *request/response shape* a provider speaks, not
4//! the vendor. It is the single key that both the gateway's outbound adapters
5//! and the agent-flow provider clients resolve a wire codec from: `minimax`
6//! speaks [`WireProtocol::Anthropic`]; `moonshot` and `qwen` speak
7//! [`WireProtocol::OpenAiChat`]. Decoupling the protocol from the provider name
8//! is what lets a new vendor reuse an existing codec by declaring its protocol.
9//!
10//! Copyright (c) systemprompt.io — Business Source License 1.1.
11//! See <https://systemprompt.io> for licensing details.
12
13use serde::{Deserialize, Serialize};
14
15use super::surface::ApiSurface;
16use crate::schema::ProviderCapabilities;
17
18#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, schemars::JsonSchema)]
19pub enum WireProtocol {
20    #[serde(rename = "anthropic")]
21    Anthropic,
22    #[serde(rename = "openai-chat", alias = "openai_chat", alias = "openai")]
23    OpenAiChat,
24    #[serde(rename = "openai-responses", alias = "openai_responses")]
25    OpenAiResponses,
26    #[serde(rename = "gemini")]
27    Gemini,
28}
29
30impl WireProtocol {
31    #[must_use]
32    pub const fn as_tag(self) -> &'static str {
33        match self {
34            Self::Anthropic => "anthropic",
35            Self::OpenAiChat => "openai-chat",
36            Self::OpenAiResponses => "openai-responses",
37            Self::Gemini => "gemini",
38        }
39    }
40
41    #[must_use]
42    pub fn from_tag(tag: &str) -> Option<Self> {
43        match tag {
44            "anthropic" => Some(Self::Anthropic),
45            "openai-chat" | "openai_chat" | "openai" => Some(Self::OpenAiChat),
46            "openai-responses" | "openai_responses" => Some(Self::OpenAiResponses),
47            "gemini" => Some(Self::Gemini),
48            _ => None,
49        }
50    }
51
52    /// The *implied* default surface only — the authoritative one is the
53    /// explicit [`super::ProviderEntry::surface`] field, which can advertise a
54    /// provider under a different family or none at all (`backend`).
55    #[must_use]
56    pub const fn surface(self) -> ApiSurface {
57        match self {
58            Self::Anthropic => ApiSurface::Anthropic,
59            Self::OpenAiChat | Self::OpenAiResponses => ApiSurface::OpenAi,
60            Self::Gemini => ApiSurface::Gemini,
61        }
62    }
63
64    #[must_use]
65    pub const fn schema_capabilities(self) -> ProviderCapabilities {
66        match self {
67            Self::Anthropic => ProviderCapabilities::anthropic(),
68            Self::OpenAiChat | Self::OpenAiResponses => ProviderCapabilities::openai(),
69            Self::Gemini => ProviderCapabilities::gemini(),
70        }
71    }
72}
73
74impl std::fmt::Display for WireProtocol {
75    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
76        f.write_str(self.as_tag())
77    }
78}