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
10use serde::{Deserialize, Serialize};
11
12use super::surface::ApiSurface;
13use crate::schema::ProviderCapabilities;
14
15#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, schemars::JsonSchema)]
16pub enum WireProtocol {
17    #[serde(rename = "anthropic")]
18    Anthropic,
19    #[serde(rename = "openai-chat", alias = "openai_chat", alias = "openai")]
20    OpenAiChat,
21    #[serde(rename = "openai-responses", alias = "openai_responses")]
22    OpenAiResponses,
23    #[serde(rename = "gemini")]
24    Gemini,
25}
26
27impl WireProtocol {
28    #[must_use]
29    pub const fn as_tag(self) -> &'static str {
30        match self {
31            Self::Anthropic => "anthropic",
32            Self::OpenAiChat => "openai-chat",
33            Self::OpenAiResponses => "openai-responses",
34            Self::Gemini => "gemini",
35        }
36    }
37
38    #[must_use]
39    pub fn from_tag(tag: &str) -> Option<Self> {
40        match tag {
41            "anthropic" => Some(Self::Anthropic),
42            "openai-chat" | "openai_chat" | "openai" => Some(Self::OpenAiChat),
43            "openai-responses" | "openai_responses" => Some(Self::OpenAiResponses),
44            "gemini" => Some(Self::Gemini),
45            _ => None,
46        }
47    }
48
49    /// The *implied* default surface only — the authoritative one is the
50    /// explicit [`super::ProviderEntry::surface`] field, which can advertise a
51    /// provider under a different family or none at all (`backend`).
52    #[must_use]
53    pub const fn surface(self) -> ApiSurface {
54        match self {
55            Self::Anthropic => ApiSurface::Anthropic,
56            Self::OpenAiChat | Self::OpenAiResponses => ApiSurface::OpenAi,
57            Self::Gemini => ApiSurface::Gemini,
58        }
59    }
60
61    #[must_use]
62    pub const fn schema_capabilities(self) -> ProviderCapabilities {
63        match self {
64            Self::Anthropic => ProviderCapabilities::anthropic(),
65            Self::OpenAiChat | Self::OpenAiResponses => ProviderCapabilities::openai(),
66            Self::Gemini => ProviderCapabilities::gemini(),
67        }
68    }
69}
70
71impl std::fmt::Display for WireProtocol {
72    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
73        f.write_str(self.as_tag())
74    }
75}