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
//! Live backends for the multi-provider [`crate::backend_router::Router`].
//!
//! Bridges the router's synchronous [`ChatBackend`] trait to Axiom's real
//! generators: the local TTT pipeline, the (sync) Claude backend, and an
//! OpenAI-compatible endpoint over blocking HTTP (so GPT — or a local OpenDrop
//! server — is a first-class router provider). [`router_from_env`] assembles a
//! router when `AXIOM_BACKEND` selects `router`, `openai`, or `opendrop`,
//! registering whichever providers are configured.
//!
//! **Blocking-client caveat:** the OpenAI adapter and Claude backend build
//! `reqwest::blocking` clients, which spin up their own runtime and *panic* if
//! constructed on a thread already inside a Tokio runtime. Callers in the async
//! server therefore build the router inside `tokio::task::spawn_blocking` (see
//! `server.rs`).
use std::sync::{Arc, RwLock};
use serde_json::json;
use crate::backend_router::{BackendError, ChatBackend, Provider, RoutePolicy, Router};
use crate::claude_backend::{ClaudeBackend, DEFAULT_CLAUDE_MODEL};
use crate::inference::InferencePipeline;
/// Default OpenDrop endpoint for the router's OpenAI-compatible provider. The
/// blocking adapter appends `/chat/completions`, so this base includes `/v1`
/// (cf. the forwarder path in `openai_forwarder.rs`, which appends the full
/// `/v1/chat/completions` and therefore uses a bare host).
const OPENDROP_ROUTER_BASE_URL: &str = "http://127.0.0.1:11434/v1";
/// Local TTT pipeline as a router backend (always-available fallback).
struct LocalPipelineBackend {
pipeline: Arc<RwLock<InferencePipeline>>,
}
impl ChatBackend for LocalPipelineBackend {
fn complete(&self, prompt: &str, max_tokens: usize) -> Result<String, BackendError> {
let pipeline = self
.pipeline
.read()
.map_err(|_| BackendError::Upstream("pipeline lock poisoned".into()))?;
pipeline
.generate(prompt, max_tokens)
.map_err(|e| BackendError::Upstream(format!("local generation failed: {e}")))
}
}
/// Anthropic (Claude) as a router backend, wrapping the existing sync backend.
struct ClaudeChatBackend {
backend: ClaudeBackend,
}
impl ChatBackend for ClaudeChatBackend {
fn complete(&self, prompt: &str, max_tokens: usize) -> Result<String, BackendError> {
self.backend
.generate(prompt, max_tokens)
.map_err(BackendError::Upstream)
}
}
/// OpenAI-compatible endpoint (cloud OpenAI or a local OpenDrop/Ollama/vLLM
/// server) over blocking HTTP — consistent with `run_generation`'s existing
/// blocking-Claude call path.
struct OpenAiBlockingBackend {
client: reqwest::blocking::Client,
api_key: Option<String>,
base_url: String,
model: String,
}
impl ChatBackend for OpenAiBlockingBackend {
fn complete(&self, prompt: &str, max_tokens: usize) -> Result<String, BackendError> {
let url = format!("{}/chat/completions", self.base_url.trim_end_matches('/'));
let body = json!({
"model": self.model,
"messages": [{ "role": "user", "content": prompt }],
"max_tokens": max_tokens,
});
let mut req = self.client.post(&url).json(&body);
if let Some(key) = &self.api_key {
req = req.bearer_auth(key);
}
let resp = req
.send()
.map_err(|e| BackendError::Upstream(format!("openai request failed: {e}")))?;
if !resp.status().is_success() {
let status = resp.status();
let text = resp.text().unwrap_or_default();
return Err(BackendError::Upstream(format!("openai {status}: {text}")));
}
let v: serde_json::Value = resp
.json()
.map_err(|e| BackendError::Upstream(format!("openai bad json: {e}")))?;
v.get("choices")
.and_then(|c| c.get(0))
.and_then(|c| c.get("message"))
.and_then(|m| m.get("content"))
.and_then(|t| t.as_str())
.map(|s| s.to_string())
.ok_or_else(|| BackendError::Upstream("openai response missing content".into()))
}
}
/// Which backend mode is selected, if any of the router-capable ones.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum RouterMode {
/// Multi-provider routing (code→Claude, else→GPT) with failover.
Router,
/// Single OpenAI-compatible endpoint (cloud or local), local fallback.
OpenAi,
/// OpenDrop preset: OpenAI-compatible at the local default base.
OpenDrop,
}
fn router_mode_from(backend: &str) -> Option<RouterMode> {
match backend.to_ascii_lowercase().as_str() {
"router" => Some(RouterMode::Router),
"openai" => Some(RouterMode::OpenAi),
"opendrop" => Some(RouterMode::OpenDrop),
_ => None,
}
}
/// Resolve the OpenAI-compatible base URL for a given mode.
fn openai_base_url_for(mode: RouterMode) -> String {
if let Ok(url) = std::env::var("OPENAI_BASE_URL") {
return url;
}
if let Ok(url) = std::env::var("OPENAI_API_BASE") {
return url;
}
match mode {
RouterMode::OpenDrop => OPENDROP_ROUTER_BASE_URL.to_string(),
RouterMode::Router | RouterMode::OpenAi => "https://api.openai.com/v1".to_string(),
}
}
/// Build a Claude backend straight from `ANTHROPIC_API_KEY` (router mode does
/// not require `AXIOM_BACKEND=claude`, which is what `ClaudeBackend::from_env`
/// gates on). Returns `None` when no key is configured.
fn claude_from_key() -> Option<ClaudeBackend> {
let api_key = std::env::var("ANTHROPIC_API_KEY")
.ok()
.filter(|k| !k.trim().is_empty())?;
let model =
std::env::var("AXIOM_CLAUDE_MODEL").unwrap_or_else(|_| DEFAULT_CLAUDE_MODEL.to_string());
let default_system = std::env::var("AXIOM_CLAUDE_SYSTEM").ok();
// Note: a custom `ANTHROPIC_BASE_URL` is only honored on the
// `AXIOM_BACKEND=claude` path (`ClaudeBackend::from_env`), which has the
// base-url setter; the router uses the default Anthropic base.
Some(ClaudeBackend::new(model, api_key, default_system))
}
/// Build a [`Router`] from the environment for the router-capable backends
/// (`router`, `openai`, `opendrop`).
///
/// * `router` registers the local pipeline (always), Claude (if
/// `ANTHROPIC_API_KEY` is set), and OpenAI (if `OPENAI_API_KEY` is set or a
/// custom `OPENAI_BASE_URL`/`OPENAI_API_BASE` points at a local server).
/// * `openai` / `opendrop` register a single OpenAI-compatible provider plus the
/// local pipeline as fallback, with a policy that routes every task to it.
///
/// Returns `None` for any other backend so callers fall back to their default
/// single-backend path.
///
/// **Must be called off the Tokio runtime** (see module docs): it constructs
/// blocking `reqwest` clients.
pub fn router_from_env(pipeline: Arc<RwLock<InferencePipeline>>) -> Option<Router> {
let backend = std::env::var("AXIOM_BACKEND").unwrap_or_default();
let mode = router_mode_from(&backend)?;
let consensus = std::env::var("AXIOM_ROUTER_CONSENSUS")
.ok()
.map(|v| !matches!(v.trim(), "0" | "false" | "off" | ""))
.unwrap_or(false);
match mode {
RouterMode::Router => {
let policy = RoutePolicy {
consensus,
..RoutePolicy::default()
};
let mut router = Router::new(policy).with(
Provider::Local,
Box::new(LocalPipelineBackend { pipeline }),
);
if let Some(claude) = claude_from_key() {
router = router.with(
Provider::Anthropic,
Box::new(ClaudeChatBackend { backend: claude }),
);
}
let openai_key = std::env::var("OPENAI_API_KEY")
.ok()
.filter(|k| !k.trim().is_empty());
let has_custom_base = std::env::var("OPENAI_BASE_URL").is_ok()
|| std::env::var("OPENAI_API_BASE").is_ok();
if openai_key.is_some() || has_custom_base {
let model =
std::env::var("AXIOM_OPENAI_MODEL").unwrap_or_else(|_| "gpt-4o".to_string());
router = router.with(
Provider::OpenAi,
Box::new(OpenAiBlockingBackend {
client: reqwest::blocking::Client::new(),
api_key: openai_key,
base_url: openai_base_url_for(mode),
model,
}),
);
}
Some(router)
}
RouterMode::OpenAi | RouterMode::OpenDrop => {
// Route every task to the single OpenAI-compatible provider, with
// the local pipeline as deterministic last-resort fallback.
let policy = RoutePolicy {
code_repair: Provider::OpenAi,
reasoning: Provider::OpenAi,
general: Provider::OpenAi,
failover: vec![Provider::OpenAi, Provider::Local],
consensus,
};
let openai_key = std::env::var("OPENAI_API_KEY")
.ok()
.filter(|k| !k.trim().is_empty());
let model =
std::env::var("AXIOM_OPENAI_MODEL").unwrap_or_else(|_| "gpt-4o".to_string());
let router = Router::new(policy)
.with(
Provider::OpenAi,
Box::new(OpenAiBlockingBackend {
client: reqwest::blocking::Client::new(),
api_key: openai_key,
base_url: openai_base_url_for(mode),
model,
}),
)
.with(
Provider::Local,
Box::new(LocalPipelineBackend { pipeline }),
);
Some(router)
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn router_mode_parsing() {
assert_eq!(router_mode_from("router"), Some(RouterMode::Router));
assert_eq!(router_mode_from("OpenAI"), Some(RouterMode::OpenAi));
assert_eq!(router_mode_from("opendrop"), Some(RouterMode::OpenDrop));
assert_eq!(router_mode_from("bootstrap"), None);
assert_eq!(router_mode_from("claude"), None);
}
#[test]
fn consensus_env_var_parsing() {
let truthy = ["1", "true", "yes", "on"];
let falsy = ["0", "false", "off", ""];
for v in truthy {
let enabled = !matches!(v.trim(), "0" | "false" | "off" | "");
assert!(enabled, "expected {v:?} to enable consensus");
}
for v in falsy {
let enabled = !matches!(v.trim(), "0" | "false" | "off" | "");
assert!(!enabled, "expected {v:?} to disable consensus");
}
}
#[test]
fn opendrop_base_url_defaults_local() {
// Only meaningful when no env override is present; assert the mode-mapped
// default rather than mutating process env (parallel-safe).
if std::env::var("OPENAI_BASE_URL").is_err()
&& std::env::var("OPENAI_API_BASE").is_err()
{
assert_eq!(
openai_base_url_for(RouterMode::OpenDrop),
OPENDROP_ROUTER_BASE_URL
);
assert!(openai_base_url_for(RouterMode::OpenAi).contains("api.openai.com"));
}
}
}