Skip to main content

byokey_proxy/handler/amp/
mod.rs

1//! Amp CLI compatibility layer — served on the main byokey port alongside
2//! the REST AI proxy and `ConnectRPC` management service.
3//!
4//! Routes (all paths are served **without** an `/amp` prefix, matching
5//! what the amp CLI sends on the wire):
6//! - `GET  /v1/login`              -> 302 redirect to ampcode.com/login.
7//! - `GET  /auth/cli-login`        -> 302 redirect to ampcode.com/auth/cli-login.
8//! - `ANY  /v0/management/{*path}` -> proxy to ampcode.com/v0/management/*.
9//! - `POST /api/provider/*`        -> provider-specific handlers.
10//! - `ANY  /api/{*path}`           -> catch-all proxy to ampcode.com.
11//!
12//! Submodules:
13//! - [`provider`] — `AmpCode` provider-namespaced AI endpoints (`/api/provider/*`).
14//! - [`threads`]  — parser + in-memory index for local Amp CLI thread files
15//!   (consumed by the `ConnectRPC` management service).
16
17pub mod provider;
18pub mod threads;
19
20use axum::{
21    extract::RawQuery,
22    http::{HeaderValue, StatusCode},
23    response::IntoResponse,
24};
25
26/// Redirects Amp CLI to the web login page.
27pub async fn login_redirect() -> impl IntoResponse {
28    (
29        StatusCode::FOUND,
30        [(
31            axum::http::header::LOCATION,
32            HeaderValue::from_static("https://ampcode.com/login"),
33        )],
34    )
35}
36
37/// Handles `GET /auth/cli-login?authToken=...&callbackPort=...`
38///
39/// `amp login` opens this URL in the browser. We forward it to `AmpCode`'s
40/// own login endpoint so `AmpCode` can authenticate the user and then
41/// callback to `http://localhost:{callbackPort}/...` directly.
42pub async fn cli_login_redirect(RawQuery(query): RawQuery) -> impl IntoResponse {
43    let url = match query {
44        Some(q) => format!("https://ampcode.com/auth/cli-login?{q}"),
45        None => "https://ampcode.com/auth/cli-login".to_string(),
46    };
47    let location = HeaderValue::from_str(&url)
48        .unwrap_or_else(|_| HeaderValue::from_static("https://ampcode.com/amp/auth/cli-login"));
49    (
50        StatusCode::FOUND,
51        [(axum::http::header::LOCATION, location)],
52    )
53}