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, head, 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        // Claude Code sends `HEAD /api/hello` to its base URL at start to warm the
129        // connection and ignores the answer; api.anthropic.com answers an empty 200.
130        // Go 404s it, which writes a forced error log for every Claude Code start
131        // (docs/DIFFERENCES-FROM-GO.md, #262).
132        .route("/api/hello", head(|| async { StatusCode::OK }))
133        .route("/", get(root))
134        .route("/anthropic/callback", get(callback))
135        .route("/codex/callback", get(callback))
136        .route("/antigravity/callback", get(callback))
137        .route("/callback", get(devin_callback))
138        .route("/devin/callback", get(devin_callback))
139        .merge(v1)
140        .merge(realtime::routes(&rt))
141        .merge(relay::routes(&rt))
142        // Matched API routes only: management answers its own HEADs (plugin routes).
143        .route_layer(middleware::from_fn(head_only_healthz))
144        .method_not_allowed_fallback(|| async { StatusCode::NOT_FOUND })
145        .layer(DefaultBodyLimit::max(MAX_REQUEST_BYTES))
146        .layer(middleware::from_fn(go_framing))
147        .layer(middleware::from_fn(request_logging::response))
148        .with_state(rt)
149}
150
151/// gin registers HEAD for `/healthz` only, so a HEAD never falls back to a GET handler;
152/// it is NoRoute's bare 404. cliproxy-rs adds `HEAD /api/hello`.
153async fn head_only_healthz(req: axum::extract::Request, next: middleware::Next) -> Response {
154    if req.method() == Method::HEAD && !matches!(req.uri().path(), "/healthz" | "/api/hello") {
155        return StatusCode::NOT_FOUND.into_response();
156    }
157    next.run(req).await
158}
159
160/// gin runs without `HandleMethodNotAllowed`: an unregistered method on a known path
161/// is NoRoute, whose handler aborts with a bare 404 (no 405, no `Allow`).
162async fn no_route_allow(req: axum::extract::Request, next: middleware::Next) -> Response {
163    let mut res = next.run(req).await;
164    if res.status() == StatusCode::NOT_FOUND {
165        res.headers_mut().remove(header::ALLOW);
166    }
167    res
168}
169
170/// Go net/http framing for handlers that set no Content-Length (gin's `c.JSON` and
171/// `c.Data`): a body over 2048 bytes (`bufferBeforeChunkingSize`) goes out chunked, and
172/// a HEAD response with no body carries neither Content-Length nor Transfer-Encoding.
173async fn go_framing(req: axum::extract::Request, next: middleware::Next) -> Response {
174    use axum::body::{Body, HttpBody};
175    let head = req.method() == Method::HEAD;
176    let res = next.run(req).await;
177    if res.headers().contains_key(header::CONTENT_LENGTH) || res.headers().contains_key(header::TRANSFER_ENCODING) {
178        return res;
179    }
180    // Streams have no exact size and are already chunked.
181    let Some(len) = res.body().size_hint().exact() else {
182        return res;
183    };
184    if len <= 2048 && !(head && len == 0) {
185        return res;
186    }
187    let (parts, body) = res.into_parts();
188    let Ok(bytes) = axum::body::to_bytes(body, usize::MAX).await else {
189        return Response::from_parts(parts, Body::empty());
190    };
191    let chunks = futures_util::stream::iter((!bytes.is_empty()).then_some(Ok::<_, std::convert::Infallible>(bytes)));
192    Response::from_parts(parts, Body::from_stream(chunks))
193}
194
195/// Installs the runtime's model registry as the translators' capability lookup
196/// (`cpa_core::registry::lookup_model`). Call once at startup.
197pub fn install_registry(rt: &Arc<Runtime>) {
198    cpa_core::registry::install_overlay(Some(Arc::new(registry::Overlay(Arc::downgrade(rt)))));
199}
200
201async fn healthz(method: Method) -> Response {
202    if method == Method::HEAD {
203        return StatusCode::OK.into_response();
204    }
205    respond::gin_json(200, r#"{"status":"ok"}"#.into())
206}
207
208async fn root() -> Response {
209    respond::gin_json(
210        200,
211        r#"{"endpoints":["POST /v1/chat/completions","POST /v1/completions","GET /v1/models"],"message":"CLI Proxy API Server"}"#.into(),
212    )
213}
214
215const 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>"#;
216
217fn html() -> Response {
218    (
219        [(
220            header::CONTENT_TYPE,
221            HeaderValue::from_static("text/html; charset=utf-8"),
222        )],
223        CALLBACK_HTML,
224    )
225        .into_response()
226}
227
228/// `code`, `state` and `error` (else `error_description`) of an OAuth redirect. Go's
229/// Devin handler trims each value before the fallback; the other handlers do not
230/// (server_routes.go).
231fn callback_query(query: &str, trim: bool) -> (String, String, String) {
232    let get = |name| {
233        let value = access::query_get(query, name)
234            .map(|v| String::from_utf8_lossy(&v).into_owned())
235            .unwrap_or_default();
236        if trim { gojson::trim(&value).to_owned() } else { value }
237    };
238    let mut error = get("error");
239    if error.is_empty() {
240        error = get("error_description");
241    }
242    (get("code"), get("state"), error)
243}
244
245/// Provider OAuth redirects on the main port: the code is handed to a pending
246/// management login if one exists; the browser always sees the success page.
247async fn callback(State(rt): State<Arc<Runtime>>, OriginalUri(uri): OriginalUri) -> Response {
248    let provider = match uri.path() {
249        "/anthropic/callback" => "anthropic",
250        "/codex/callback" => "codex",
251        _ => "antigravity",
252    };
253    let (code, state, error) = callback_query(uri.query().unwrap_or_default(), false);
254    if !state.is_empty() {
255        let delivery = rt.deliver_oauth_callback(&runtime::OAuthCallback {
256            provider,
257            state,
258            code,
259            error,
260        });
261        // Go v8.0.35 `ae3ee3be`: only the Codex route reports the refusal.
262        if provider == "codex" && delivery == runtime::OAuthDelivery::RequiresV8 {
263            return respond::gin_json(
264                409,
265                r#"{"error":"SIWC callbacks require /v8/management/oauth/callback"}"#.into(),
266            );
267        }
268    }
269    html()
270}
271
272async fn devin_callback(State(rt): State<Arc<Runtime>>, OriginalUri(uri): OriginalUri) -> Response {
273    let (code, state, error) = callback_query(uri.query().unwrap_or_default(), true);
274    let no_store = |mut res: Response| {
275        res.headers_mut()
276            .insert(header::CACHE_CONTROL, HeaderValue::from_static("no-store"));
277        res
278    };
279    if code.is_empty() && error.is_empty() {
280        return no_store(respond::gin_json(
281            400,
282            r#"{"error":"code or error is required"}"#.into(),
283        ));
284    }
285    let delivered = rt.deliver_oauth_callback(&runtime::OAuthCallback {
286        provider: "devin",
287        state,
288        code,
289        error,
290    });
291    if delivered != runtime::OAuthDelivery::Delivered {
292        return no_store(respond::gin_json(
293            400,
294            r#"{"error":"invalid or expired OAuth callback"}"#.into(),
295        ));
296    }
297    no_store(html())
298}