Skip to main content

firstpass_proxy/
upstream.rs

1//! Forwarding to the upstream Anthropic-compatible provider.
2//!
3//! Observe mode is a transparent proxy: the request body and BYOK auth headers go upstream
4//! byte-for-byte, and the response comes back byte-for-byte. Firstpass never inspects,
5//! injects, or logs the API key.
6
7use axum::http::{HeaderMap, StatusCode, header};
8use bytes::Bytes;
9
10use crate::error::ProxyError;
11
12/// Request headers forwarded verbatim to the upstream provider. Anything not in this list
13/// (in particular hop-by-hop headers) is dropped rather than relayed.
14const FORWARDED_REQUEST_HEADERS: &[&str] = &[
15    "x-api-key",
16    "authorization",
17    "anthropic-version",
18    "anthropic-beta",
19    "content-type",
20];
21
22/// Build an upstream POST request to `{base}{path}`, forwarding the allow-listed headers and the
23/// caller's body byte-for-byte. Shared by the buffered and streaming paths, and by both the
24/// Anthropic (`/v1/messages`) and OpenAI (`/v1/chat/completions`) observe-passthrough paths.
25fn build_request(
26    client: &reqwest::Client,
27    base: &str,
28    path: &str,
29    headers: &HeaderMap,
30    body: Bytes,
31) -> reqwest::RequestBuilder {
32    let url = format!("{}{path}", base.trim_end_matches('/'));
33    let mut req = client.post(url);
34    for name in FORWARDED_REQUEST_HEADERS {
35        if let Some(value) = headers.get(*name) {
36            req = req.header(*name, value.clone());
37        }
38    }
39    req.body(body)
40}
41
42/// Copy the response's content-type (the only response header observe mode relays).
43fn passthrough_headers(response: &reqwest::Response) -> HeaderMap {
44    // reqwest and axum both build on the same `http` crate, so header types carry over
45    // without re-encoding.
46    let mut out = HeaderMap::new();
47    if let Some(content_type) = response.headers().get(header::CONTENT_TYPE) {
48        out.insert(header::CONTENT_TYPE, content_type.clone());
49    }
50    out
51}
52
53/// Forward one `POST /v1/messages` request and return its response **buffered** (full body).
54/// Used for non-streaming requests, where the assembled body also feeds the audit trace.
55///
56/// # Errors
57/// Returns [`ProxyError::Upstream`] if the upstream request fails at the transport level
58/// (DNS, connect, timeout). A non-2xx HTTP response from upstream is *not* an error here —
59/// it is returned to the caller as-is, matching observe mode's "forward unchanged" contract.
60pub async fn forward_anthropic(
61    client: &reqwest::Client,
62    base: &str,
63    headers: &HeaderMap,
64    body: Bytes,
65) -> Result<(StatusCode, HeaderMap, Bytes), ProxyError> {
66    let response = build_request(client, base, "/v1/messages", headers, body)
67        .send()
68        .await?;
69    let status = response.status();
70    let out_headers = passthrough_headers(&response);
71    let body = response.bytes().await?;
72    Ok((status, out_headers, body))
73}
74
75/// Forward one `POST /v1/messages` request and return the raw response for **streaming**
76/// relay (`stream: true`) — the caller pipes `response.bytes_stream()` to the client without
77/// buffering, preserving SSE chunk-by-chunk and keeping time-to-first-byte low.
78///
79/// # Errors
80/// Returns [`ProxyError::Upstream`] on a transport-level failure (see [`forward_anthropic`]).
81pub async fn forward_anthropic_streaming(
82    client: &reqwest::Client,
83    base: &str,
84    headers: &HeaderMap,
85    body: Bytes,
86) -> Result<(StatusCode, HeaderMap, reqwest::Response), ProxyError> {
87    let response = build_request(client, base, "/v1/messages", headers, body)
88        .send()
89        .await?;
90    let status = response.status();
91    let out_headers = passthrough_headers(&response);
92    Ok((status, out_headers, response))
93}
94
95/// The OpenAI-compatible inbound paths this proxy relays for.
96pub const OPENAI_CHAT_PATH: &str = "/v1/chat/completions";
97/// The Responses API path. A Responses-shaped body relayed to [`OPENAI_CHAT_PATH`] is rejected by
98/// the upstream, so the passthrough has to preserve which endpoint the client actually called.
99pub const OPENAI_RESPONSES_PATH: &str = "/v1/responses";
100
101/// Forward one OpenAI-compatible request to an explicit upstream path and return its response
102/// **buffered**. Observe-mode passthrough — a byte-identical relay.
103///
104/// The path is explicit because a Responses-shaped body relayed to [`OPENAI_CHAT_PATH`] is
105/// rejected upstream; the caller knows which endpoint the client actually called.
106///
107/// # Errors
108/// Returns [`ProxyError::Upstream`] on a transport-level failure.
109pub async fn forward_openai_path(
110    client: &reqwest::Client,
111    base: &str,
112    path: &str,
113    headers: &HeaderMap,
114    body: Bytes,
115) -> Result<(StatusCode, HeaderMap, Bytes), ProxyError> {
116    let response = build_request(client, base, path, headers, body)
117        .send()
118        .await?;
119    let status = response.status();
120    let out_headers = passthrough_headers(&response);
121    let body = response.bytes().await?;
122    Ok((status, out_headers, body))
123}
124
125/// Forward one OpenAI-compatible request to an explicit upstream path and return the raw response
126/// for streaming relay.
127///
128/// # Errors
129/// Returns [`ProxyError::Upstream`] on a transport-level failure.
130pub async fn forward_openai_streaming_path(
131    client: &reqwest::Client,
132    base: &str,
133    path: &str,
134    headers: &HeaderMap,
135    body: Bytes,
136) -> Result<(StatusCode, HeaderMap, reqwest::Response), ProxyError> {
137    let response = build_request(client, base, path, headers, body)
138        .send()
139        .await?;
140    let status = response.status();
141    let out_headers = passthrough_headers(&response);
142    Ok((status, out_headers, response))
143}