axiom_engine 0.5.0

Axiom-TTT Inference Engine – Rust implementation using candle
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
//! Outbound bridge to OpenAI-compatible Chat Completions APIs used by the
//! context-compression pipeline.
//!
//! This mirrors the Anthropic bridge for Codex/OpenAI clients: when active
//! compression is enabled, `/v1/chat/completions` payloads are compacted by
//! Axiom first, then forwarded to the configured upstream with client auth
//! relayed or a proxy-owned key injected.

use std::time::Duration;

use reqwest::Client;
use serde_json::Value;

const DEFAULT_BASE_URL: &str = "https://api.openai.com";
/// Default endpoint for a local [OpenDrop](https://github.com/fernandogarzaaa/OpenDrop)
/// server (OpenAI-compatible). Used when `AXIOM_BACKEND=opendrop` and no explicit
/// base URL is set, so `opendrop run <model>` + `AXIOM_BACKEND=opendrop` "just
/// works" against a local model with zero per-token cost.
///
/// This remains a bare host for consistency with the default OpenAI base.
const OPENDROP_DEFAULT_BASE_URL: &str = "http://127.0.0.1:11434";

/// Responses endpoint on the ChatGPT backend. Codex authenticated with a
/// ChatGPT subscription (OAuth) can only call this backend — its token has no
/// `api.responses.write` scope on the platform API (api.openai.com).
const CHATGPT_CODEX_RESPONSES_URL: &str = "https://chatgpt.com/backend-api/codex/responses";

fn api_root(base: &str) -> &str {
    let base = base.trim_end_matches('/');
    base.strip_suffix("/v1").unwrap_or(base)
}

fn chat_completions_url(base: &str) -> String {
    format!("{}/v1/chat/completions", api_root(base))
}

fn responses_url(base: &str) -> String {
    format!("{}/v1/responses", api_root(base))
}

/// Pick the Responses upstream for a specific request.
///
/// Precedence:
/// 1. `AXIOM_OPENAI_RESPONSES_UPSTREAM` env var — full URL, used verbatim.
/// 2. Client sent `chatgpt-account-id` (Codex in ChatGPT-subscription mode)
///    — route to the ChatGPT backend, the only upstream its token works on.
/// 3. Default platform API: `{base}/v1/responses`.
fn responses_upstream_url(base: &str, auth: &OpenAiClientAuth) -> String {
    responses_upstream_url_with(
        std::env::var("AXIOM_OPENAI_RESPONSES_UPSTREAM").ok().as_deref(),
        base,
        auth,
    )
}

/// Pure core of [`responses_upstream_url`] — env override injected as a
/// parameter so routing is unit-testable without process-global mutation.
fn responses_upstream_url_with(
    override_url: Option<&str>,
    base: &str,
    auth: &OpenAiClientAuth,
) -> String {
    if let Some(explicit) = override_url {
        let trimmed = explicit.trim();
        if !trimmed.is_empty() {
            return trimmed.to_string();
        }
    }
    let has_chatgpt_account = auth
        .extra_headers
        .iter()
        .any(|(name, _)| name.eq_ignore_ascii_case("chatgpt-account-id"));
    if has_chatgpt_account {
        return CHATGPT_CODEX_RESPONSES_URL.to_string();
    }
    responses_url(base)
}

/// Resolve the effective OpenAI-compatible base URL from env signals. An explicit
/// `OPENAI_BASE_URL`/`OPENAI_API_BASE` always wins; otherwise `AXIOM_BACKEND=opendrop`
/// selects the local OpenDrop endpoint; otherwise `None` (caller falls back to the
/// real OpenAI default). Pure so it is unit-testable without touching process env.
fn resolve_base_url(
    backend: Option<&str>,
    openai_base_url: Option<String>,
    openai_api_base: Option<String>,
) -> Option<String> {
    if let Some(explicit) = openai_base_url.or(openai_api_base) {
        return Some(explicit);
    }
    if backend.map(|b| b.eq_ignore_ascii_case("opendrop")).unwrap_or(false) {
        return Some(OPENDROP_DEFAULT_BASE_URL.to_string());
    }
    None
}

/// Auth / relay headers captured from the inbound OpenAI-compatible request.
#[derive(Clone, Debug, Default)]
pub struct OpenAiClientAuth {
    pub authorization: Option<String>,
    /// Additional client headers relayed verbatim upstream (lowercase names).
    /// Codex in ChatGPT-subscription mode needs `chatgpt-account-id`,
    /// `openai-beta`, `session_id`, and `originator` to reach the ChatGPT
    /// backend; hop-by-hop and body-encoding headers must never be included.
    pub extra_headers: Vec<(String, String)>,
}

/// Active outbound bridge. Cheap to clone (`reqwest::Client` is Arc-backed).
#[derive(Clone)]
pub struct OpenAiForwarder {
    api_key: Option<String>,
    base_url: String,
    client: Client,
    streaming_client: Client,
}

impl OpenAiForwarder {
    pub fn new(api_key: Option<String>, base_url: Option<String>) -> Self {
        let client = Client::builder()
            .timeout(Duration::from_secs(120))
            .build()
            .expect("reqwest async client should construct");
        // Streaming agent runs can legitimately exceed two minutes. This
        // client has no total timeout; disconnects and upstream errors still
        // terminate its response stream.
        let streaming_client = Client::builder()
            .build()
            .expect("reqwest streaming client should construct");
        Self {
            api_key,
            base_url: base_url.unwrap_or_else(|| DEFAULT_BASE_URL.to_string()),
            client,
            streaming_client,
        }
    }

    /// Env-driven activation. Always returns `Some(forwarder)` so the proxy
    /// works in both API-key mode and auth-passthrough mode.
    pub fn from_env() -> Option<Self> {
        let api_key = std::env::var("OPENAI_API_KEY")
            .ok()
            .filter(|k| !k.trim().is_empty());
        let base_url = resolve_base_url(
            std::env::var("AXIOM_BACKEND").ok().as_deref(),
            std::env::var("OPENAI_BASE_URL").ok(),
            std::env::var("OPENAI_API_BASE").ok(),
        );
        Some(Self::new(api_key, base_url))
    }

    /// Whether the proxy holds its own OpenAI API key.
    pub fn has_own_key(&self) -> bool {
        self.api_key.is_some()
    }

    /// POST to `/v1/chat/completions` on the configured OpenAI-compatible API.
    ///
    /// Auth precedence:
    /// 1. Client `Authorization` header, relayed verbatim.
    /// 2. Proxy-owned `OPENAI_API_KEY`, injected as bearer auth.
    pub async fn forward_chat_completions_text(
        &self,
        payload: &Value,
        auth: &OpenAiClientAuth,
    ) -> Result<OpenAiForwardedResponse, OpenAiForwarderError> {
        let url = chat_completions_url(&self.base_url);
        self.forward_json_text(url, payload, auth).await
    }

    /// POST to `/v1/responses` without translating the request or response.
    /// Native passthrough preserves function calls, reasoning items, and SSE
    /// event types that do not have lossless Chat Completions equivalents.
    pub async fn forward_responses(
        &self,
        payload: &Value,
        auth: &OpenAiClientAuth,
    ) -> Result<reqwest::Response, OpenAiForwarderError> {
        let url = responses_upstream_url(&self.base_url, auth);
        let client = if payload.get("stream").and_then(Value::as_bool) == Some(true) {
            &self.streaming_client
        } else {
            &self.client
        };
        self.send_json_with(client, url, payload, auth).await
    }

    async fn forward_json_text(
        &self,
        url: String,
        payload: &Value,
        auth: &OpenAiClientAuth,
    ) -> Result<OpenAiForwardedResponse, OpenAiForwarderError> {
        let response = self
            .send_json_with(&self.client, url, payload, auth)
            .await?;

        let status = response.status().as_u16();
        let content_type = response
            .headers()
            .get("content-type")
            .and_then(|v| v.to_str().ok())
            .map(str::to_string);
        let body = response
            .text()
            .await
            .map_err(|e| OpenAiForwarderError::Network(format!("body read failed: {e}")))?;

        Ok(OpenAiForwardedResponse {
            status,
            content_type,
            body,
        })
    }

    async fn send_json_with(
        &self,
        client: &Client,
        url: String,
        payload: &Value,
        auth: &OpenAiClientAuth,
    ) -> Result<reqwest::Response, OpenAiForwarderError> {
        let mut request = client.post(url).header("content-type", "application/json");

        if let Some(authz) = auth.authorization.as_deref() {
            request = request.header("authorization", authz);
        } else if let Some(key) = self.api_key.as_deref() {
            request = request.header("authorization", bearer_value(key));
        } else {
            return Err(OpenAiForwarderError::MissingAuth);
        }

        for (name, value) in &auth.extra_headers {
            if name.eq_ignore_ascii_case("authorization") {
                continue; // handled above
            }
            request = request.header(name.as_str(), value.as_str());
        }

        request
            .json(payload)
            .send()
            .await
            .map_err(|e| OpenAiForwarderError::Network(e.to_string()))
    }
}

fn bearer_value(key: &str) -> String {
    if key
        .get(..7)
        .map(|prefix| prefix.eq_ignore_ascii_case("bearer "))
        .unwrap_or(false)
    {
        key.to_string()
    } else {
        format!("Bearer {key}")
    }
}

pub struct OpenAiForwardedResponse {
    pub status: u16,
    pub content_type: Option<String>,
    pub body: String,
}

#[derive(Debug)]
pub enum OpenAiForwarderError {
    Network(String),
    MissingAuth,
}

impl std::fmt::Display for OpenAiForwarderError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            OpenAiForwarderError::Network(m) => write!(f, "network error: {m}"),
            OpenAiForwarderError::MissingAuth => write!(
                f,
                "no upstream credential: client sent no Authorization header \
                 and the proxy has no OPENAI_API_KEY configured"
            ),
        }
    }
}

impl std::error::Error for OpenAiForwarderError {}

#[cfg(test)]
mod tests {
    use super::{
        bearer_value, chat_completions_url, resolve_base_url, responses_url,
        OPENDROP_DEFAULT_BASE_URL,
    };

    #[test]
    fn opendrop_default_base_has_no_v1_suffix() {
        // The forwarder appends `/v1/chat/completions`; the base must NOT carry
        // its own `/v1` or the final URL doubles it.
        assert!(
            !OPENDROP_DEFAULT_BASE_URL.ends_with("/v1"),
            "opendrop base must be a bare host, got {OPENDROP_DEFAULT_BASE_URL}"
        );
        assert_eq!(
            chat_completions_url(OPENDROP_DEFAULT_BASE_URL),
            "http://127.0.0.1:11434/v1/chat/completions"
        );
    }

    #[test]
    fn chat_completions_url_appends_v1_path_once() {
        assert_eq!(
            chat_completions_url("https://api.openai.com"),
            "https://api.openai.com/v1/chat/completions"
        );
        // Trailing slash is trimmed (no double slash).
        assert_eq!(
            chat_completions_url("https://api.openai.com/"),
            "https://api.openai.com/v1/chat/completions"
        );
        assert_eq!(
            chat_completions_url("https://api.openai.com/v1/"),
            "https://api.openai.com/v1/chat/completions"
        );
    }

    #[test]
    fn chatgpt_account_header_routes_to_chatgpt_backend() {
        use super::{responses_upstream_url_with, OpenAiClientAuth, CHATGPT_CODEX_RESPONSES_URL};
        let auth = OpenAiClientAuth {
            authorization: Some("Bearer tok".into()),
            extra_headers: vec![("chatgpt-account-id".into(), "acct-123".into())],
        };
        assert_eq!(
            responses_upstream_url_with(None, "https://api.openai.com", &auth),
            CHATGPT_CODEX_RESPONSES_URL
        );
        // Case-insensitive header match.
        let auth_upper = OpenAiClientAuth {
            authorization: None,
            extra_headers: vec![("ChatGPT-Account-Id".into(), "acct-123".into())],
        };
        assert_eq!(
            responses_upstream_url_with(None, "https://api.openai.com", &auth_upper),
            CHATGPT_CODEX_RESPONSES_URL
        );
    }

    #[test]
    fn no_chatgpt_header_uses_platform_default() {
        use super::{responses_upstream_url_with, OpenAiClientAuth};
        let auth = OpenAiClientAuth::default();
        assert_eq!(
            responses_upstream_url_with(None, "https://api.openai.com", &auth),
            "https://api.openai.com/v1/responses"
        );
    }

    #[test]
    fn explicit_override_beats_header_routing() {
        use super::{responses_upstream_url_with, OpenAiClientAuth};
        let auth = OpenAiClientAuth {
            authorization: None,
            extra_headers: vec![("chatgpt-account-id".into(), "acct-123".into())],
        };
        assert_eq!(
            responses_upstream_url_with(Some("http://127.0.0.1:9999/x"), "https://api.openai.com", &auth),
            "http://127.0.0.1:9999/x"
        );
        // Blank override falls through to header routing.
        assert_eq!(
            responses_upstream_url_with(Some("  "), "https://api.openai.com", &auth),
            super::CHATGPT_CODEX_RESPONSES_URL
        );
    }

    #[test]
    fn responses_url_appends_v1_path_once() {
        assert_eq!(
            responses_url("https://api.openai.com/"),
            "https://api.openai.com/v1/responses"
        );
        assert_eq!(
            responses_url("https://api.openai.com/v1"),
            "https://api.openai.com/v1/responses"
        );
    }

    #[test]
    fn bearer_value_does_not_double_prefix() {
        assert_eq!(bearer_value("sk-test"), "Bearer sk-test");
        assert_eq!(bearer_value("Bearer sk-test"), "Bearer sk-test");
        assert_eq!(bearer_value("bearer sk-test"), "bearer sk-test");
    }

    #[test]
    fn opendrop_backend_defaults_to_local_endpoint() {
        assert_eq!(
            resolve_base_url(Some("opendrop"), None, None).as_deref(),
            Some(OPENDROP_DEFAULT_BASE_URL)
        );
        // Case-insensitive.
        assert_eq!(
            resolve_base_url(Some("OpenDrop"), None, None).as_deref(),
            Some(OPENDROP_DEFAULT_BASE_URL)
        );
    }

    #[test]
    fn explicit_base_url_always_wins() {
        assert_eq!(
            resolve_base_url(Some("opendrop"), Some("http://x:1/v1".into()), None).as_deref(),
            Some("http://x:1/v1")
        );
        assert_eq!(
            resolve_base_url(None, None, Some("http://y:2/v1".into())).as_deref(),
            Some("http://y:2/v1")
        );
    }

    #[test]
    fn no_signal_falls_back_to_default() {
        assert_eq!(resolve_base_url(None, None, None), None);
        assert_eq!(resolve_base_url(Some("openai"), None, None), None);
    }
}