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
//! `/router` — configure or inspect model router. Migrated off the legacy
//! `handle_slash_command` match.
use super::super::registry::SlashCommand;
use crate::app::agent_session::AgentSession;
use crate::tui::app::{AppState, NotificationKind};
use crate::tui::completion::{CompletionItem, CompletionKind};
use crate::tui::overlay::{self, router_integration};
use crate::tui::slash::{SlashCtx, SlashOutcome};
/// `/router` help text (shown for unknown `/router <sub>`).
fn router_help() -> String {
r#"Router Commands:
/router Configure router (opens setup) or show status
/router status Show routing status (tier, score, model, cost)
/router pin Pin current tier (low|medium|high|off)
/router enable Enable router (switch to router/auto)
/router disable Disable router, return to fixed model
Or select \"router/auto\" in /model"#
.to_string()
}
/// `/router [status|pin|enable|disable]` — configure or inspect model router.
pub(crate) struct RouterCommand;
impl SlashCommand for RouterCommand {
fn name(&self) -> &str {
"router"
}
fn description(&self) -> &str {
"Configure or inspect model router"
}
fn usage(&self) -> &str {
"/router <status|pin <low|medium|high|off>|enable|disable>"
}
fn execute(&self, args: &str, ctx: &mut SlashCtx<'_>) -> SlashOutcome {
let state = &mut *ctx.state;
let session = ctx.session;
let sub = args.trim();
if !sub.is_empty() {
let mut parts = sub.split_whitespace();
let cmd = parts.next().unwrap_or("");
match cmd {
"status" => {
if let Some(snap) = oxi_sdk::router::RouterProvider::get_snapshot() {
let content = format!(
"Router Status:\n\nProfile: {}\nTier: {:?}\nScore: {:.2}\nModel: {}\nProvider: {}\nCost: ${:.4}\nTurns: {}",
snap.profile.as_deref().unwrap_or("-"),
snap.last_tier
.unwrap_or(oxi_sdk::router::RouterTier::Medium),
snap.last_score,
snap.last_model.as_deref().unwrap_or("-"),
snap.last_provider.as_deref().unwrap_or("-"),
snap.accumulated_cost,
snap.turn_count,
);
state.overlay = None;
state.overlay_state =
Some(Box::new(overlay::text_viewer::TextViewerOverlay::new(
" Router Status ",
content,
)));
} else {
state.add_notification(
"Router not active. Use /router to configure.".to_string(),
NotificationKind::Warning,
);
}
}
"pin" => {
// /router pin <low|medium|high|off>
if let Some(tier_arg) = parts.next() {
match tier_arg.to_lowercase().as_str() {
"low" => {
oxi_sdk::router::set_router_pin(Some(
oxi_sdk::router::RouterTier::Low,
));
state.add_notification(
"Router pinned to LOW tier".to_string(),
NotificationKind::Success,
);
}
"medium" => {
oxi_sdk::router::set_router_pin(Some(
oxi_sdk::router::RouterTier::Medium,
));
state.add_notification(
"Router pinned to MEDIUM tier".to_string(),
NotificationKind::Success,
);
}
"high" => {
oxi_sdk::router::set_router_pin(Some(
oxi_sdk::router::RouterTier::High,
));
state.add_notification(
"Router pinned to HIGH tier".to_string(),
NotificationKind::Success,
);
}
"off" | "none" | "clear" => {
oxi_sdk::router::set_router_pin(None);
state.add_notification(
"Router pin cleared (auto-routing resumed)".to_string(),
NotificationKind::Success,
);
}
_ => {
state.add_notification(
"Usage: /router pin <low|medium|high|off>".to_string(),
NotificationKind::Info,
);
}
}
} else {
let current = oxi_sdk::router::get_router_pin();
let msg = match current {
Some(t) => format!("Router pin: {:?}", t),
None => "Router pin: none (auto)".to_string(),
};
state.add_notification(msg, NotificationKind::Info);
}
}
"disable" => {
// Switch away from router to the default model
let settings = crate::store::settings::Settings::load().unwrap_or_default();
if let Some(default_model) = settings.effective_model(None) {
let full_id = if default_model.contains('/') {
default_model.clone()
} else {
let p = settings.effective_provider(None).unwrap_or_default();
format!("{}/{}", p, default_model)
};
match session.set_model(&full_id) {
Ok(()) => {
state.footer_state.data.model_name = full_id.clone();
state.add_notification(
format!("Router disabled, using {}", full_id),
NotificationKind::Success,
);
}
Err(e) => {
state.add_notification(
format!("Error switching model: {}", e),
NotificationKind::Error,
);
}
}
} else {
state.add_notification(
"No default model configured".to_string(),
NotificationKind::Warning,
);
}
}
"enable" => {
// Switch to router/auto
match session.set_model("router/auto") {
Ok(()) => {
state.footer_state.data.model_name = "router/auto".to_string();
state.add_notification(
"Router enabled (router/auto)".to_string(),
NotificationKind::Success,
);
}
Err(e) => {
state.add_notification(
format!("Error enabling router: {}", e),
NotificationKind::Error,
);
}
}
}
_ => {
state.overlay = None;
state.overlay_state =
Some(Box::new(overlay::text_viewer::TextViewerOverlay::new(
" Router Help ",
router_help(),
)));
}
}
} else {
let global_dir = dirs::config_dir().unwrap_or_default().join("oxi");
let project_dir = std::env::current_dir().unwrap_or_default();
let has_config =
crate::store::router_config::load_router_config(&global_dir, &project_dir)
.is_some();
if has_config {
if let Some(snap) = oxi_sdk::router::RouterProvider::get_snapshot() {
let content = format!(
"Router Status:\n\nProfile: {}\nTier: {:?}\nScore: {:.2}\nModel: {}\nCost: ${:.4}\nTurns: {}",
snap.profile.as_deref().unwrap_or("-"),
snap.last_tier
.unwrap_or(oxi_sdk::router::RouterTier::Medium),
snap.last_score,
snap.last_model.as_deref().unwrap_or("-"),
snap.accumulated_cost,
snap.turn_count,
);
state.overlay = None;
state.overlay_state = Some(Box::new(
overlay::text_viewer::TextViewerOverlay::new(" Router Status ", content),
));
} else {
state.add_notification(
"Router configured but not yet active".to_string(),
NotificationKind::Info,
);
}
} else {
state.add_notification(
"Opening router setup...".to_string(),
NotificationKind::Info,
);
let auth = crate::store::auth_storage::shared_auth_storage();
let setup_models: Vec<String> = super::model::collect_catalog_models(state)
.into_iter()
.filter(|(provider, _)| auth.get_api_key(provider).is_some())
.map(|(provider, model_id)| format!("{}/{}", provider, model_id))
.collect();
let initial = overlay::RouterSetupData {
profile_name: "auto".to_string(),
..Default::default()
};
state.overlay = None;
state.overlay_state = Some(overlay::router_setup(
initial,
setup_models,
move |data: &overlay::RouterSetupData| {
let store_cfg = router_integration::save_router_config(data)?;
let ai_cfg = router_integration::store_config_to_ai_config(&store_cfg);
oxi_sdk::router::register_router(&ai_cfg);
Ok(())
},
|| {},
));
}
}
SlashOutcome::Handled
}
fn complete_arg(
&self,
prefix: &str,
_session: &AgentSession,
_state: &AppState,
) -> Vec<CompletionItem> {
// First token: status | pin | enable | disable.
// After `pin`: low | medium | high | off.
let mut tokens = vec!["status", "pin", "enable", "disable"];
let first = prefix.split_whitespace().next().unwrap_or("");
if prefix.starts_with("pin") && prefix.trim() != "pin" {
tokens = vec!["low", "medium", "high", "off"];
let last = prefix.rsplit(' ').next().unwrap_or("");
return tokens
.iter()
.filter(|t| t.starts_with(last))
.map(|t| CompletionItem {
text: t.to_string(),
label: t.to_string(),
description: None,
kind: CompletionKind::SlashArgument {
command: "router".to_string(),
},
})
.collect();
}
let _ = first;
tokens
.iter()
.filter(|t| t.starts_with(prefix))
.map(|t| CompletionItem {
text: t.to_string(),
label: t.to_string(),
description: None,
kind: CompletionKind::SlashArgument {
command: "router".to_string(),
},
})
.collect()
}
}