Skip to main content

cpa_server/
lib.rs

1//! HTTP surface: routes and client-key auth over an axum-independent [`runtime`].
2
3mod access;
4mod affinity;
5pub mod body_limit;
6pub mod capabilities;
7mod classify;
8mod claude;
9mod codex_alpha;
10mod codex_models;
11mod config_diff;
12pub mod cooldown_store;
13pub mod dispatch;
14mod error_events;
15mod errors;
16mod fs_events;
17mod gemini;
18mod gojson;
19mod home_models;
20pub mod home_session;
21mod images;
22pub mod keepalive;
23pub mod lcp;
24pub mod listener;
25pub mod logging;
26pub mod management;
27pub mod model_updater;
28mod models;
29pub mod observability;
30mod openai;
31pub mod persist;
32pub mod plugins;
33pub mod pprof;
34mod realtime;
35mod refresh;
36pub mod registry;
37mod relay;
38pub mod remote;
39pub mod request_logging;
40mod resp;
41mod respond;
42pub mod runtime;
43pub mod safe_mode;
44mod sanitize;
45pub mod scheduler;
46mod session;
47mod speech;
48#[doc(hidden)]
49pub mod testing;
50pub mod usage;
51mod usage_record;
52mod videos;
53pub mod watching;
54mod websocket;
55mod websocket_requests;
56mod websocket_tools;
57
58use std::sync::Arc;
59
60use axum::extract::{DefaultBodyLimit, OriginalUri, State};
61use axum::http::{HeaderValue, Method, StatusCode, header};
62use axum::response::{IntoResponse, Response};
63use axum::routing::{get, post};
64use axum::{Router, middleware};
65
66pub use runtime::Runtime;
67
68// ponytail: fixed 64 MiB request cap so image-heavy Claude requests pass (axum's default
69// is 2 MiB; gin has none). Make it configurable if anyone needs more.
70pub const MAX_REQUEST_BYTES: usize = 64 * 1024 * 1024;
71
72/// The API routes alone; unregistered paths get gin's bare NoRoute 404.
73pub fn router(rt: Arc<Runtime>) -> Router {
74    gin(api(rt))
75}
76
77/// The served app: the API routes, with `rest` (the management router, which owns gin's
78/// NoRoute and its plugin routes) answering every path the API does not register. axum
79/// allows one fallback per merged router, so `rest` sits behind the API instead of being
80/// merged into it.
81pub fn app(rt: Arc<Runtime>, rest: Router) -> Router {
82    gin(api(rt).fallback_service(rest.layer(middleware::from_fn(request_logging::response))))
83}
84
85/// Around the whole router: axum sets `Allow` outside per-route layers.
86fn gin(api: Router) -> Router {
87    Router::new()
88        .fallback_service(api)
89        .layer(middleware::from_fn(no_route_allow))
90}
91
92fn api(rt: Arc<Runtime>) -> Router {
93    let auth = || middleware::from_fn_with_state(rt.clone(), access::require_client_key);
94    let v1 = Router::new()
95        .route("/v1/models", get(models::unified))
96        // Gin's `/models/*model` also matches the bare trailing slash, with an empty ID.
97        .route("/v1/models/", get(models::detail))
98        .route("/v1/models/{*model}", get(models::detail))
99        .route("/v1/chat/completions", post(openai::chat_completions))
100        .route("/v1/completions", post(openai::completions))
101        .route("/v1/images/generations", post(images::generations))
102        .route("/v1/images/edits", post(images::edits))
103        .route("/v1/audio/speech", post(speech::speech))
104        .route("/v1/tts", post(speech::speech))
105        .route("/v1/videos", post(videos::native_post))
106        .route("/v1/videos/generations", post(videos::native_post))
107        .route("/v1/videos/edits", post(videos::native_post))
108        .route("/v1/videos/extensions", post(videos::native_post))
109        .route("/v1/videos/{request_id}", get(videos::native_retrieve))
110        .route("/openai/v1/videos", post(videos::create))
111        .route("/openai/v1/videos/{video_id}/content", get(videos::content))
112        .route("/openai/v1/videos/{video_id}", get(videos::retrieve))
113        .route("/v1/messages", post(claude::messages))
114        .route("/v1/messages/count_tokens", post(claude::count_tokens))
115        .route("/v1/responses", post(openai::responses))
116        .route("/v1/responses/compact", post(openai::compact))
117        .route("/backend-api/codex/responses", post(openai::responses))
118        .route("/backend-api/codex/responses/compact", post(openai::compact))
119        .route("/v1beta/models", get(models::gemini_list))
120        .route("/v1beta/interactions", post(gemini::interactions))
121        .route("/v1beta/models/{*action}", post(gemini::action).get(models::gemini_get))
122        .merge(codex_alpha::routes())
123        .merge(websocket::routes())
124        // Only matched routes: gin's NoRoute runs no group middleware.
125        .route_layer(auth());
126    Router::new()
127        .route("/healthz", get(healthz).head(healthz))
128        .route("/", get(root))
129        .route("/anthropic/callback", get(callback))
130        .route("/codex/callback", get(callback))
131        .route("/antigravity/callback", get(callback))
132        .route("/callback", get(devin_callback))
133        .route("/devin/callback", get(devin_callback))
134        .merge(v1)
135        .merge(realtime::routes(&rt))
136        .merge(relay::routes(&rt))
137        // Matched API routes only: management answers its own HEADs (plugin routes).
138        .route_layer(middleware::from_fn(head_only_healthz))
139        .method_not_allowed_fallback(|| async { StatusCode::NOT_FOUND })
140        .layer(DefaultBodyLimit::max(MAX_REQUEST_BYTES))
141        .layer(middleware::from_fn(go_framing))
142        .layer(middleware::from_fn(request_logging::response))
143        .with_state(rt)
144}
145
146/// gin registers HEAD for `/healthz` only, so a HEAD never falls back to a GET handler;
147/// it is NoRoute's bare 404.
148async fn head_only_healthz(req: axum::extract::Request, next: middleware::Next) -> Response {
149    if req.method() == Method::HEAD && req.uri().path() != "/healthz" {
150        return StatusCode::NOT_FOUND.into_response();
151    }
152    next.run(req).await
153}
154
155/// gin runs without `HandleMethodNotAllowed`: an unregistered method on a known path
156/// is NoRoute, whose handler aborts with a bare 404 (no 405, no `Allow`).
157async fn no_route_allow(req: axum::extract::Request, next: middleware::Next) -> Response {
158    let mut res = next.run(req).await;
159    if res.status() == StatusCode::NOT_FOUND {
160        res.headers_mut().remove(header::ALLOW);
161    }
162    res
163}
164
165/// Go net/http framing for handlers that set no Content-Length (gin's `c.JSON` and
166/// `c.Data`): a body over 2048 bytes (`bufferBeforeChunkingSize`) goes out chunked, and
167/// a HEAD response with no body carries neither Content-Length nor Transfer-Encoding.
168async fn go_framing(req: axum::extract::Request, next: middleware::Next) -> Response {
169    use axum::body::{Body, HttpBody};
170    let head = req.method() == Method::HEAD;
171    let res = next.run(req).await;
172    if res.headers().contains_key(header::CONTENT_LENGTH) || res.headers().contains_key(header::TRANSFER_ENCODING) {
173        return res;
174    }
175    // Streams have no exact size and are already chunked.
176    let Some(len) = res.body().size_hint().exact() else {
177        return res;
178    };
179    if len <= 2048 && !(head && len == 0) {
180        return res;
181    }
182    let (parts, body) = res.into_parts();
183    let Ok(bytes) = axum::body::to_bytes(body, usize::MAX).await else {
184        return Response::from_parts(parts, Body::empty());
185    };
186    let chunks = futures_util::stream::iter((!bytes.is_empty()).then_some(Ok::<_, std::convert::Infallible>(bytes)));
187    Response::from_parts(parts, Body::from_stream(chunks))
188}
189
190/// Installs the runtime's model registry as the translators' capability lookup
191/// (`cpa_core::registry::lookup_model`). Call once at startup.
192pub fn install_registry(rt: &Arc<Runtime>) {
193    cpa_core::registry::install_overlay(Some(Arc::new(registry::Overlay(Arc::downgrade(rt)))));
194}
195
196async fn healthz(method: Method) -> Response {
197    if method == Method::HEAD {
198        return StatusCode::OK.into_response();
199    }
200    respond::gin_json(200, r#"{"status":"ok"}"#.into())
201}
202
203async fn root() -> Response {
204    respond::gin_json(
205        200,
206        r#"{"endpoints":["POST /v1/chat/completions","POST /v1/completions","GET /v1/models"],"message":"CLI Proxy API Server"}"#.into(),
207    )
208}
209
210const CALLBACK_HTML: &str = r#"<html><head><meta charset="utf-8"><title>Authentication successful</title><script>setTimeout(function(){window.close();},5000);</script></head><body><h1>Authentication successful!</h1><p>You can close this window.</p><p>This window will close automatically in 5 seconds.</p></body></html>"#;
211
212fn html() -> Response {
213    (
214        [(
215            header::CONTENT_TYPE,
216            HeaderValue::from_static("text/html; charset=utf-8"),
217        )],
218        CALLBACK_HTML,
219    )
220        .into_response()
221}
222
223/// `code`, `state` and `error` (else `error_description`) of an OAuth redirect. Go's
224/// Devin handler trims each value before the fallback; the other handlers do not
225/// (server_routes.go).
226fn callback_query(query: &str, trim: bool) -> (String, String, String) {
227    let get = |name| {
228        let value = access::query_get(query, name)
229            .map(|v| String::from_utf8_lossy(&v).into_owned())
230            .unwrap_or_default();
231        if trim { gojson::trim(&value).to_owned() } else { value }
232    };
233    let mut error = get("error");
234    if error.is_empty() {
235        error = get("error_description");
236    }
237    (get("code"), get("state"), error)
238}
239
240/// Provider OAuth redirects on the main port: the code is handed to a pending
241/// management login if one exists; the browser always sees the success page.
242async fn callback(State(rt): State<Arc<Runtime>>, OriginalUri(uri): OriginalUri) -> Response {
243    let provider = match uri.path() {
244        "/anthropic/callback" => "anthropic",
245        "/codex/callback" => "codex",
246        _ => "antigravity",
247    };
248    let (code, state, error) = callback_query(uri.query().unwrap_or_default(), false);
249    if !state.is_empty() {
250        let delivery = rt.deliver_oauth_callback(&runtime::OAuthCallback {
251            provider,
252            state,
253            code,
254            error,
255        });
256        // Go v8.0.35 `ae3ee3be`: only the Codex route reports the refusal.
257        if provider == "codex" && delivery == runtime::OAuthDelivery::RequiresV8 {
258            return respond::gin_json(
259                409,
260                r#"{"error":"SIWC callbacks require /v8/management/oauth/callback"}"#.into(),
261            );
262        }
263    }
264    html()
265}
266
267async fn devin_callback(State(rt): State<Arc<Runtime>>, OriginalUri(uri): OriginalUri) -> Response {
268    let (code, state, error) = callback_query(uri.query().unwrap_or_default(), true);
269    let no_store = |mut res: Response| {
270        res.headers_mut()
271            .insert(header::CACHE_CONTROL, HeaderValue::from_static("no-store"));
272        res
273    };
274    if code.is_empty() && error.is_empty() {
275        return no_store(respond::gin_json(
276            400,
277            r#"{"error":"code or error is required"}"#.into(),
278        ));
279    }
280    let delivered = rt.deliver_oauth_callback(&runtime::OAuthCallback {
281        provider: "devin",
282        state,
283        code,
284        error,
285    });
286    if delivered != runtime::OAuthDelivery::Delivered {
287        return no_store(respond::gin_json(
288            400,
289            r#"{"error":"invalid or expired OAuth callback"}"#.into(),
290        ));
291    }
292    no_store(html())
293}