pub mod aistudio;
pub mod antigravity;
pub mod antigravity_auth;
mod antigravity_compaction;
pub mod antigravity_credits;
mod antigravity_models;
mod antigravity_replay;
mod antigravity_request;
pub mod antigravity_version;
pub mod claude;
pub mod claude_login;
pub mod codex;
mod codex_capture;
mod codex_client;
mod codex_images;
mod codex_json;
pub mod codex_live;
pub mod codex_oauth;
pub mod codex_quota;
pub mod codex_refresh;
mod codex_replay;
mod codex_request;
mod codex_response;
pub mod codex_siwc;
pub mod codex_siwc_bind;
mod codex_sse;
#[cfg(test)]
mod codex_testkit;
mod codex_tls;
mod codex_tokens;
mod codex_ws;
pub mod devin;
pub mod devin_auth;
pub mod devin_models;
mod devin_payload;
mod devin_request;
mod devin_wire;
pub mod gemini;
mod gemini_payload;
mod gemini_stream;
mod home_replay;
pub mod kimi;
pub mod kimi_auth;
#[cfg(test)]
mod kimi_fixture;
mod kimi_http;
mod kimi_replay;
pub mod meta;
pub mod meta_auth;
mod meta_codex;
mod meta_wire;
pub mod oauth;
pub mod openai_compat;
mod openai_compat_go;
mod openai_compat_http;
pub mod openai_compat_multipart;
mod openai_compat_payload;
#[cfg(test)]
mod openai_compat_usage;
mod payload_barrier;
mod payload_media;
pub mod proxy;
mod quota;
mod rawjson;
mod replay;
#[cfg(test)]
mod test_tls;
mod tls;
mod tokenizer;
mod tokens;
mod translate;
mod upstream;
pub mod vertex;
pub mod vertex_auth;
mod vertex_openapi;
mod wire;
mod ws_observation;
pub mod wsrelay;
pub mod xai;
pub mod xai_auth;
mod xai_replay;
mod xai_request;
mod xai_response;
pub mod xai_url;
mod xai_ws;
use cpa_core::config::Config;
use cpa_core::credential::{Credential, MetadataPatch};
use cpa_core::exec::{ExecError, ExecRequest, ExecResponse, ExecSession, FailureScope};
pub use kimi_http::{expiration, last_refresh, preferred_interval};
pub use openai_compat_payload::ensure_responses_usage_details;
pub use xai_ws::websockets_enabled;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Readiness {
Ready,
RefreshSoon,
PrepareNow,
}
pub struct Executors {
pub claude: claude::ClaudeExecutor,
pub codex: codex::CodexExecutor,
pub devices: DeviceExecutors,
pub openai: OpenAIExecutors,
pub google: GoogleExecutors,
}
#[derive(Default)]
pub struct GoogleExecutors {
pub gemini: gemini::GeminiExecutor,
pub vertex: vertex::VertexExecutor,
pub aistudio: aistudio::AiStudioExecutor,
pub antigravity: antigravity::AntigravityExecutor,
}
#[derive(Default)]
pub struct OpenAIExecutors {
pub compat: openai_compat::OpenAICompatExecutor,
pub xai: xai::XaiExecutor,
}
#[derive(Default)]
pub struct DeviceExecutors {
pub kimi: kimi::KimiExecutor,
pub meta: meta::MetaExecutor,
pub devin: devin::DevinExecutor,
}
impl Executors {
pub async fn execute(
&self,
credential: &Credential,
req: ExecRequest,
cfg: &Config,
) -> Result<ExecResponse, ExecError> {
match credential.provider.as_str() {
"claude" => self.claude.execute(credential, req, cfg).await,
"codex" => self.codex.execute(credential, req, cfg).await,
p if kimi::PROVIDERS.contains(&p) => self.devices.kimi.execute(&self.claude, credential, req, cfg).await,
meta::PROVIDER => self.devices.meta.execute(credential, req, cfg).await,
devin::PROVIDER => self.devices.devin.execute(credential, req, cfg).await,
p if openai_compat::handles(p) => self.openai.compat.execute(credential, req, cfg).await,
p if gemini::handles(p) => self.google.gemini.execute(credential, req, cfg).await,
p if vertex::handles(p) => self.google.vertex.execute(credential, req, cfg).await,
p if aistudio::handles(p) => self.google.aistudio.execute(credential, req, cfg).await,
antigravity::PROVIDER => self.google.antigravity.execute(credential, req, cfg).await,
xai::PROVIDER => self.openai.xai.execute(credential, req, cfg, false).await,
other => Err(no_executor(other)),
}
}
pub async fn images(
&self,
credential: &Credential,
req: ExecRequest,
request_path: &str,
cfg: &Config,
) -> Result<ExecResponse, ExecError> {
match credential.provider.as_str() {
"codex" => self.codex.images(credential, req, request_path, cfg).await,
p if openai_compat::handles(p) => self.openai.compat.images(credential, req, request_path, cfg).await,
xai::PROVIDER => self.openai.xai.images(credential, req, request_path, cfg).await,
other => Err(no_executor(other)),
}
}
pub async fn videos(
&self,
credential: &Credential,
req: ExecRequest,
request_path: &str,
cfg: &Config,
) -> Result<ExecResponse, ExecError> {
match credential.provider.as_str() {
xai::PROVIDER => self.openai.xai.videos(credential, req, request_path, cfg).await,
other => Err(no_executor(other)),
}
}
pub async fn speech(
&self,
credential: &Credential,
req: ExecRequest,
cfg: &Config,
) -> Result<ExecResponse, ExecError> {
match credential.provider.as_str() {
xai::PROVIDER => self.openai.xai.speech(credential, req, cfg).await,
other => Err(no_executor(other)),
}
}
pub async fn execute_in_session(
&self,
credential: &Credential,
req: ExecRequest,
cfg: &Config,
session: &ExecSession,
) -> Result<ExecResponse, ExecError> {
match credential.provider.as_str() {
"codex" => self.codex.execute_in_session(credential, req, cfg, session).await,
xai::PROVIDER => self.openai.xai.execute_in_session(credential, req, cfg, session).await,
_ if session.continuation => Err(ExecError::replay_required()),
_ => self.execute(credential, req, cfg).await,
}
}
pub fn session_upstream(&self, credential: &Credential) -> bool {
match credential.provider.as_str() {
"codex" => codex::CodexExecutor::upstream_websocket(credential),
xai::PROVIDER => xai::XaiExecutor::session_upstream(credential),
_ => false,
}
}
pub fn session_closed(&self, id: &str) -> impl std::future::Future<Output = ExecError> + Send + 'static {
let codex = self.codex.session_closed(id);
let xai = self.openai.xai.session_closed(id);
async move {
tokio::select! {
error = codex => error,
error = xai => error,
}
}
}
pub fn close_session(&self, id: &str) {
self.codex.close_session(id);
self.openai.xai.close_session(id);
}
pub fn credential_session_closer(&self) -> impl Fn(&str) + Send + Sync + 'static {
let codex = self.codex.ws.clone();
let xai = self.openai.xai.ws.clone();
move |id| {
codex.close_credential(id);
xai.close_credential(id);
}
}
pub fn supports(&self, provider: &str) -> bool {
matches!(
provider,
"claude" | "codex" | meta::PROVIDER | xai::PROVIDER | devin::PROVIDER
) || kimi::PROVIDERS.contains(&provider)
|| openai_compat::handles(provider)
|| gemini::handles(provider)
|| vertex::handles(provider)
|| aistudio::handles(provider)
|| antigravity::handles(provider)
}
pub fn has_refresh_credential(&self, credential: &Credential) -> bool {
let filled = |v: Option<&str>| v.is_some_and(|v| !v.trim().is_empty());
if filled(credential.str("refresh_token")) || filled(credential.str("refreshToken")) {
return true;
}
if credential.provider == "codex" && codex_siwc::refresh_due(credential, chrono::Utc::now()).is_some() {
return true;
}
credential.provider.trim().eq_ignore_ascii_case("meta")
&& (filled(credential.str("dca_token"))
|| filled(credential.attributes.get("dca_token").map(String::as_str)))
}
pub fn readiness(&self, credential: &Credential, cfg: &Config) -> Readiness {
if !self.needs_prepare(credential, cfg) {
return Readiness::Ready;
}
match credential.provider.as_str() {
"claude" if oauth::needs_identity(credential) => Readiness::PrepareNow,
meta::PROVIDER if self.devices.meta.must_mint(credential) => Readiness::PrepareNow,
antigravity::PROVIDER if self.google.antigravity.must_prepare(credential) => Readiness::PrepareNow,
_ => Readiness::RefreshSoon,
}
}
pub fn needs_prepare(&self, credential: &Credential, cfg: &Config) -> bool {
self.needs_prepare_at(credential, cfg, chrono::Utc::now())
}
pub fn needs_prepare_at(&self, credential: &Credential, cfg: &Config, now: chrono::DateTime<chrono::Utc>) -> bool {
match credential.provider.as_str() {
"claude" => self.claude.needs_prepare_at(credential, cfg, now),
"codex" => self.codex.needs_prepare_at(credential, cfg, now),
p if kimi::PROVIDERS.contains(&p) => self.devices.kimi.needs_prepare_at(credential, cfg, now),
meta::PROVIDER => self.devices.meta.needs_prepare_at(credential, cfg, now),
xai::PROVIDER => self.openai.xai.needs_prepare_at(credential, cfg, now),
devin::PROVIDER => self.devices.devin.needs_prepare_at(credential, cfg, now),
antigravity::PROVIDER => self.google.antigravity.needs_prepare_at(credential, cfg, now),
_ => false,
}
}
pub fn prepare_due(
&self,
credential: &Credential,
cfg: &Config,
now: chrono::DateTime<chrono::Utc>,
horizon: chrono::Duration,
) -> Option<chrono::DateTime<chrono::Utc>> {
if self.needs_prepare_at(credential, cfg, now) {
return Some(now);
}
let (mut lo, mut hi) = (now, now + horizon);
if !self.needs_prepare_at(credential, cfg, hi) {
return None;
}
while hi - lo > chrono::Duration::seconds(1) {
let mid = lo + (hi - lo) / 2;
if self.needs_prepare_at(credential, cfg, mid) {
hi = mid;
} else {
lo = mid;
}
}
Some(hi)
}
pub fn models_registered(&self, credentials: &[std::sync::Arc<Credential>], cfg: &Config) {
self.google.antigravity.models_registered(credentials, cfg);
}
pub async fn prepare_forced(&self, credential: &Credential, cfg: &Config) -> Result<MetadataPatch, ExecError> {
match credential.provider.as_str() {
"codex" => self.codex.refresh_now(credential, cfg).await,
_ => self.prepare(credential, cfg).await,
}
}
pub async fn prepare_management(&self, credential: &Credential, cfg: &Config) -> Result<MetadataPatch, ExecError> {
match credential.provider.as_str() {
"codex" => self.codex.refresh_all(credential, cfg).await,
_ => self.prepare_forced(credential, cfg).await,
}
}
pub async fn prepare(&self, credential: &Credential, cfg: &Config) -> Result<MetadataPatch, ExecError> {
match credential.provider.as_str() {
"claude" => self.claude.prepare(credential, cfg).await,
"codex" => self.codex.prepare(credential, cfg).await,
p if kimi::PROVIDERS.contains(&p) => self.devices.kimi.prepare(credential, cfg).await,
meta::PROVIDER => self.devices.meta.prepare(credential, cfg).await,
xai::PROVIDER => self.openai.xai.prepare(credential, cfg).await,
devin::PROVIDER => self.devices.devin.prepare(credential, cfg).await,
antigravity::PROVIDER => self.google.antigravity.prepare(credential, cfg).await,
other => Err(no_executor(other)),
}
}
}
fn no_executor(provider: &str) -> ExecError {
ExecError::local(
500,
FailureScope::Credential,
format!("no executor for provider {provider:?}"),
)
}
#[cfg(test)]
mod readiness_tests {
use super::{Executors, Readiness, claude};
use cpa_core::config::Config;
use cpa_core::credential::Credential;
use std::path::Path;
fn credential(metadata: serde_json::Value) -> Credential {
Credential::from_file(
Path::new("/auth"),
Path::new("/auth/a.json"),
metadata.as_object().unwrap().clone(),
)
.unwrap()
}
fn rfc3339(offset: chrono::Duration) -> String {
(chrono::Utc::now() + offset).to_rfc3339()
}
#[test]
fn only_missing_identity_blocks_requests() {
let executors = Executors {
claude: claude::ClaudeExecutor::new("http://127.0.0.1:1").unwrap(),
codex: Default::default(),
devices: Default::default(),
openai: Default::default(),
google: Default::default(),
};
let cfg = Config::default();
let pool = serde_json::json!(["a".repeat(64)]);
let identified = |expired: String| {
credential(serde_json::json!({
"type": "claude",
"access_token": "sk-ant-oat01-fake",
"refresh_token": "fake-refresh",
"claude_device_ids": pool,
"account_uuid": "fake-account",
"expired": expired,
}))
};
let cases = [
(identified(rfc3339(chrono::Duration::days(2))), Readiness::Ready),
(identified(rfc3339(chrono::Duration::hours(1))), Readiness::RefreshSoon),
(identified(rfc3339(-chrono::Duration::hours(1))), Readiness::RefreshSoon),
(
credential(serde_json::json!({
"type": "claude",
"access_token": "sk-ant-oat01-fake",
"refresh_token": "fake-refresh",
"expired": rfc3339(-chrono::Duration::hours(1)),
})),
Readiness::PrepareNow,
),
(
credential(serde_json::json!({"type": "claude", "access_token": "sk-ant-api03-fake"})),
Readiness::Ready,
),
(
credential(serde_json::json!({
"type": "kimi",
"access_token": "fake-access",
"refresh_token": "fake-refresh",
"expired": rfc3339(-chrono::Duration::hours(1)),
})),
Readiness::RefreshSoon,
),
(
credential(serde_json::json!({
"type": "antigravity",
"access_token": "fake-access",
"refresh_token": "fake-refresh",
"expired": rfc3339(chrono::Duration::hours(5)),
})),
Readiness::PrepareNow,
),
(
credential(serde_json::json!({
"type": "antigravity",
"project_id": "proj-1",
"access_token": "fake-access",
"refresh_token": "fake-refresh",
"expired": rfc3339(chrono::Duration::minutes(20)),
})),
Readiness::RefreshSoon,
),
(
credential(serde_json::json!({
"type": "antigravity",
"project_id": "proj-1",
"access_token": "fake-access",
"refresh_token": "fake-refresh",
"expired": rfc3339(chrono::Duration::minutes(6)),
})),
Readiness::RefreshSoon,
),
(
credential(serde_json::json!({
"type": "antigravity",
"project_id": "proj-1",
"access_token": "fake-access",
"refresh_token": "fake-refresh",
"expired": rfc3339(chrono::Duration::minutes(4)),
})),
Readiness::PrepareNow,
),
(
credential(serde_json::json!({
"type": "antigravity",
"project_id": "proj-1",
"refresh_token": "fake-refresh",
"expired": rfc3339(chrono::Duration::minutes(40)),
})),
Readiness::PrepareNow,
),
(
credential(serde_json::json!({
"type": "antigravity",
"project_id": "proj-1",
"access_token": "fake-access",
})),
Readiness::PrepareNow,
),
(
credential(serde_json::json!({
"type": "antigravity",
"project_id": "proj-1",
"access_token": "fake-access",
"refresh_token": "fake-refresh",
"expired": rfc3339(chrono::Duration::minutes(40)),
})),
Readiness::Ready,
),
];
for (i, (c, expected)) in cases.iter().enumerate() {
assert_eq!(executors.readiness(c, &cfg), *expected, "case {i}");
}
}
#[test]
fn prepare_due_finds_each_providers_deadline() {
let executors = Executors {
claude: claude::ClaudeExecutor::new("http://127.0.0.1:1").unwrap(),
codex: Default::default(),
devices: Default::default(),
openai: Default::default(),
google: Default::default(),
};
let cfg = Config::default();
let now = chrono::Utc::now();
let horizon = chrono::Duration::hours(1);
let with_expiry = |provider: &str, expiry: chrono::DateTime<chrono::Utc>| {
credential(serde_json::json!({
"type": provider,
"access_token": "fake-access",
"refresh_token": "fake-refresh",
"expired": expiry.to_rfc3339(),
}))
};
let near = |got: Option<chrono::DateTime<chrono::Utc>>, want: chrono::DateTime<chrono::Utc>| {
let got = got.expect("due inside the horizon");
assert!((got - want).num_milliseconds().abs() <= 1000, "{got} vs {want}");
};
let offset = chrono::Duration::seconds(1337);
let claude = with_expiry("claude", now + chrono::Duration::hours(4) + offset);
near(executors.prepare_due(&claude, &cfg, now, horizon), now + offset);
let kimi = with_expiry("kimi", now + chrono::Duration::minutes(5) + offset);
near(executors.prepare_due(&kimi, &cfg, now, horizon), now + offset);
let due = with_expiry("claude", now - chrono::Duration::hours(1));
assert_eq!(executors.prepare_due(&due, &cfg, now, horizon), Some(now));
let later = with_expiry("claude", now + chrono::Duration::hours(6));
assert_eq!(executors.prepare_due(&later, &cfg, now, horizon), None);
let api_key = credential(serde_json::json!({"type": "claude", "access_token": "sk-ant-api03-fake"}));
assert_eq!(executors.prepare_due(&api_key, &cfg, now, horizon), None);
}
#[test]
fn refresh_credentials_follow_go() {
let executors = Executors {
claude: claude::ClaudeExecutor::new("http://127.0.0.1:1").unwrap(),
codex: Default::default(),
openai: Default::default(),
google: Default::default(),
devices: Default::default(),
};
let has = |m: serde_json::Value| executors.has_refresh_credential(&credential(m));
assert!(has(serde_json::json!({"type": "claude", "refresh_token": "fake"})));
assert!(has(serde_json::json!({"type": "kimi", "refreshToken": "fake"})));
assert!(!has(serde_json::json!({"type": "claude", "refresh_token": " "})));
assert!(has(
serde_json::json!({"type": "codex", "siwc": {"refresh_token": "fake", "client_id": "c"}})
));
assert!(!has(
serde_json::json!({"type": "codex", "siwc": {"refresh_token": "fake"}})
));
assert!(
!has(serde_json::json!({"type": "claude", "siwc": {"refresh_token": "fake", "client_id": "c"}})),
"SIWC is Codex's"
);
assert!(has(serde_json::json!({"type": "meta", "dca_token": "fake"})));
assert!(
!has(serde_json::json!({"type": "claude", "dca_token": "fake"})),
"dca_token is Meta's"
);
let mut meta = credential(serde_json::json!({"type": "meta"}));
assert!(!executors.has_refresh_credential(&meta));
meta.attributes.insert("dca_token".into(), "fake".into());
assert!(executors.has_refresh_credential(&meta));
}
}