Skip to main content

claude_codex/
provider.rs

1use crate::anthropic::schema::MessagesRequest;
2use crate::monitor::MonitorHandle;
3use crate::traffic::TrafficCapture;
4use anyhow::Result;
5use async_trait::async_trait;
6use axum::response::Response;
7use clap::Subcommand;
8use std::sync::Arc;
9
10#[derive(Debug, Clone, Subcommand)]
11pub enum AuthCommand {
12    /// Sign in using browser-based authentication
13    Login,
14    /// Sign in using a device code
15    Device,
16    /// Show the current authentication status
17    Status,
18    /// Delete stored authentication credentials
19    Logout,
20}
21
22#[async_trait]
23pub trait Provider: Send + Sync {
24    fn name(&self) -> &'static str;
25    fn supported_models(&self) -> Vec<String>;
26    fn cli(&self) -> &'static dyn CliHandlers;
27    async fn handle_messages(&self, body: MessagesRequest, ctx: RequestContext) -> Response;
28    async fn handle_count_tokens(&self, body: MessagesRequest, ctx: RequestContext) -> Response;
29}
30
31pub trait CliHandlers: Send + Sync {
32    fn login(&self) -> Result<()>;
33    fn device(&self) -> Result<()>;
34    fn status(&self) -> Result<()>;
35    fn logout(&self) -> Result<()>;
36}
37
38#[derive(Debug, Clone)]
39pub struct RequestContext {
40    pub req_id: String,
41    pub session_id: Option<String>,
42    pub session_seq: Option<u64>,
43    pub provider: String,
44    pub traffic: Option<Arc<TrafficCapture>>,
45    pub monitor: Option<MonitorHandle>,
46    /// Raw request material for byte-passthrough providers (the Anthropic backend).
47    /// Present on real HTTP requests; None in unit tests. Forwarding these verbatim
48    /// keeps the prompt-cache prefix byte-identical.
49    pub passthrough: Option<Passthrough>,
50}
51
52/// Untranslated request material needed to relay a request to an upstream verbatim.
53#[derive(Debug, Clone)]
54pub struct Passthrough {
55    /// Original request body bytes, forwarded without reserialization.
56    pub raw_body: axum::body::Bytes,
57    /// Original client request headers (carry Authorization + anthropic-beta).
58    pub headers: axum::http::HeaderMap,
59    /// Original path and query, e.g. `/v1/messages?beta=true`.
60    pub path_and_query: String,
61}