use serde::{Deserialize, Serialize};
#[derive(
Debug,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
Hash,
Serialize,
Deserialize,
clap::ValueEnum,
)]
#[serde(rename_all = "lowercase")]
pub enum HttpProvider {
Codex,
Openai,
Anthropic,
Gemini,
}
impl HttpProvider {
pub const ALL: [Self; 4] = [Self::Codex, Self::Openai, Self::Anthropic, Self::Gemini];
pub fn as_str(self) -> &'static str {
match self {
Self::Codex => "codex",
Self::Openai => "openai",
Self::Anthropic => "anthropic",
Self::Gemini => "gemini",
}
}
pub fn named(name: &str) -> Option<Self> {
Self::ALL
.into_iter()
.find(|provider| provider.as_str() == name)
}
}
impl std::fmt::Display for HttpProvider {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter.write_str(self.as_str())
}
}
const RESERVED_BUILTIN_NAMES: [&str; 6] = [
"codex",
"openai",
"anthropic",
"gemini",
"claude-agent",
"antigravity",
];
pub fn is_reserved_custom(name: &str) -> bool {
RESERVED_BUILTIN_NAMES.contains(&name)
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize)]
#[serde(transparent)]
pub struct CustomBackendName(String);
impl CustomBackendName {
pub fn as_str(&self) -> &str {
&self.0
}
}
impl std::str::FromStr for CustomBackendName {
type Err = String;
fn from_str(name: &str) -> Result<Self, Self::Err> {
if is_reserved_custom(name) {
return Err(format!(
"{name} is a built-in provider name and cannot be used as a custom HTTP backend."
));
}
const MAX_NAME_BYTES: usize = 64;
if name.is_empty()
|| name.len() > MAX_NAME_BYTES
|| !name.as_bytes()[0].is_ascii_alphanumeric()
|| !name
.bytes()
.all(|b| b.is_ascii_alphanumeric() || matches!(b, b'_' | b'-'))
{
return Err("custom backend must match [A-Za-z0-9][A-Za-z0-9_-]{0,63}.".into());
}
Ok(Self(name.to_owned()))
}
}
impl<'de> Deserialize<'de> for CustomBackendName {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
String::deserialize(deserializer)?
.parse()
.map_err(serde::de::Error::custom)
}
}
impl std::borrow::Borrow<str> for CustomBackendName {
fn borrow(&self) -> &str {
self.as_str()
}
}
impl std::fmt::Display for CustomBackendName {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter.write_str(&self.0)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "camelCase")]
pub enum DirectBackend {
Builtin {
provider: HttpProvider,
},
Custom {
name: CustomBackendName,
},
}
impl DirectBackend {
pub fn as_str(&self) -> &str {
match self {
Self::Builtin { provider } => provider.as_str(),
Self::Custom { name } => name.as_str(),
}
}
}
impl std::fmt::Display for DirectBackend {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter.write_str(self.as_str())
}
}
use std::{borrow::Borrow, fmt, ops::Deref, str::FromStr};
macro_rules! identity {
($name:ident, $valid:expr) => {
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize)]
#[serde(transparent)]
pub struct $name(String);
impl FromStr for $name {
type Err = String;
fn from_str(value: &str) -> Result<Self, Self::Err> {
if ($valid)(value) {
Ok(Self(value.to_owned()))
} else {
Err(format!("Invalid {}: {value:?}", stringify!($name)))
}
}
}
impl<'de> Deserialize<'de> for $name {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
use serde::de::Error;
String::deserialize(deserializer)?
.parse()
.map_err(D::Error::custom)
}
}
impl $name {
pub fn as_str(&self) -> &str {
&self.0
}
}
impl Deref for $name {
type Target = str;
fn deref(&self) -> &str {
&self.0
}
}
impl AsRef<str> for $name {
fn as_ref(&self) -> &str {
&self.0
}
}
impl Borrow<str> for $name {
fn borrow(&self) -> &str {
&self.0
}
}
impl Borrow<String> for $name {
fn borrow(&self) -> &String {
&self.0
}
}
impl fmt::Display for $name {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
impl PartialEq<str> for $name {
fn eq(&self, other: &str) -> bool {
self.0 == other
}
}
impl PartialEq<&str> for $name {
fn eq(&self, other: &&str) -> bool {
self.0 == *other
}
}
impl PartialEq<String> for $name {
fn eq(&self, other: &String) -> bool {
&self.0 == other
}
}
impl PartialEq<$name> for String {
fn eq(&self, other: &$name) -> bool {
*self == other.0
}
}
impl PartialEq<$name> for str {
fn eq(&self, other: &$name) -> bool {
self == other.0
}
}
impl PartialEq<$name> for &str {
fn eq(&self, other: &$name) -> bool {
*self == other.0
}
}
};
}
identity!(ModelId, |value: &str| !value.trim().is_empty());
pub(crate) const ITEM_ID_BYTES: usize = 1024;
const OAUTH_STATE_HEX_BYTES: usize = 32;
identity!(OAuthState, |value: &str| value.len()
== OAUTH_STATE_HEX_BYTES
&& value
.bytes()
.all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b)));
identity!(ItemId, |value: &str| value.len() <= ITEM_ID_BYTES);
identity!(CodexAccountId, |value: &str| !value.is_empty());