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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
//! Slash command handling.
use super::app::{AppOverlay, AppState, NotificationKind, SetupStep, UiEvent};
use super::overlay::router_integration;
use crate::app::agent_session::{AgentSession, ScopedModel};
use crate::media::clipboard_write;
use crate::storage::export::{self, ExportMeta, HtmlExportOptions};
use oxi_tui::widgets::chat::{ContentBlock, MessageRole};
use std::path::PathBuf;
use tokio::sync::mpsc;
/// A slash command completion entry.
pub(crate) struct SlashCompletion {
pub name: String,
pub description: String,
}
/// Handle a slash command. Returns `true` if handled.
pub(crate) fn handle_slash_command(
input: &str,
session: &AgentSession,
state: &mut AppState,
running: &mut bool,
ui_tx: &mpsc::UnboundedSender<UiEvent>,
) -> bool {
let trimmed = input.trim();
let (cmd, arg) = if let Some(space) = trimmed.find(' ') {
(&trimmed[..space], Some(trimmed[space + 1..].trim()))
} else {
(trimmed, None)
};
let cmd_lower = cmd.to_lowercase();
match cmd_lower.as_str() {
"/help" | "/?" => {
state.overlay = None;
state.overlay_state = Some(super::overlay::help_overlay());
true
}
"/quit" | "/exit" | "/q" => {
*running = false;
true
}
"/model" => {
if let Some(model_id) = arg {
match session.set_model(model_id) {
Ok(()) => {
state.add_notification(
format!("Model: {}", model_id),
NotificationKind::Success,
);
state.footer_state.data.model_name = model_id.to_string();
// Also set provider_name if model_id is in "provider/model" format
if let Some((provider, _model)) = model_id.split_once('/') {
state.footer_state.data.provider_name = provider.to_string();
}
oxi_store::settings::Settings::save_last_used(model_id);
}
Err(e) => {
state.add_notification(format!("Error: {}", e), NotificationKind::Error);
}
}
} else {
let auth = oxi_store::auth_storage::shared_auth_storage();
// Static models filtered by API key
let mut all_models: Vec<String> = oxi_ai::model_db::get_all_models()
.filter(|entry| auth.get_api_key(entry.provider).is_some())
.map(|entry| format!("{}/{}", entry.provider, entry.id))
.collect();
// Dynamic models (custom providers + router/auto)
for dyn_model in oxi_ai::dynamic_models() {
let entry = format!("{}/{}", dyn_model.provider, dyn_model.id);
if !all_models.contains(&entry) {
all_models.push(entry);
}
}
if all_models.is_empty() {
state.add_notification(
format!("Model: {}", session.model_id()),
NotificationKind::Info,
);
} else {
state.overlay = None;
state.overlay_state =
Some(super::overlay::model_select(all_models, session, state));
}
}
true
}
"/compact" => {
let instructions = arg.map(|s| s.to_string());
let sh = session.clone_handle();
let tx = ui_tx.clone();
// The compaction events (CompactionStart/CompactionEnd) will be
// emitted by AgentSession::compact() and handled in handle_ui_event.
tokio::spawn(async move {
let result = sh.compact(instructions).await;
// For manual compaction, CompactionEnd is emitted by AgentSession.
// But we also send the result via SystemMessage as a fallback.
let _ = match &result {
Ok(r) => tx.send(UiEvent::SystemMessage(format!(
"Compacted from {} tokens",
r.tokens_before
))),
Err(e) => tx.send(UiEvent::SystemMessage(format!("Compaction failed: {}", e))),
};
});
true
}
"/session" => {
let stats = session.session_stats();
let content = format!(
"Session: {}\n\nMessages: {} ({} user, {} assistant)\nTools: {} calls, {} results\n\nModel: {}\nThinking: {:?}\n\nAuto-compact: {}\nAuto-retry: {}",
stats.session_id, stats.total_messages, stats.user_messages, stats.assistant_messages,
stats.tool_calls, stats.tool_results, session.model_id(),
session.thinking_level(), session.auto_compaction_enabled(), session.auto_retry_enabled(),
);
state.overlay = None;
state.overlay_state = Some(Box::new(
super::overlay::text_viewer::TextViewerOverlay::new(" Session Info ", content),
));
true
}
"/settings" => {
state.overlay_state = Some(super::overlay::settings_overlay(
&session.clone_handle(),
state,
));
true
}
"/tools" => {
let registry = session.agent_ref().tools();
let names = registry.names();
if let Some(action) = arg {
handle_tool_command(action, ®istry, state);
} else {
let mut out = "Available Tools:\n\n".to_string();
for name in &names {
if let Some(tool) = registry.get(name) {
out.push_str(&format!(" {} — {}\n", name, tool.label()));
}
}
out.push_str("\n/tools <name> Toggle tool on/off");
state.overlay = None;
state.overlay_state = Some(super::overlay::tools_overlay(out));
}
true
}
"/extensions" | "/ext" => {
state.overlay = None;
state.overlay_state = Some(super::overlay::extensions_overlay(session, state));
true
}
"/name" => {
if let Some(name) = arg {
session.set_session_name(name.to_string());
state.add_notification(format!("Session: {}", name), NotificationKind::Success);
} else {
state.add_notification("/name <name>".to_string(), NotificationKind::Info);
}
true
}
"/copy" => {
if let Some(ref code) = state.chat.last_code_block {
match clipboard_write::copy_to_clipboard(code) {
Ok(()) => state.add_notification(
"Code block copied to clipboard".to_string(),
NotificationKind::Success,
),
Err(e) => state
.add_notification(format!("Copy failed: {}", e), NotificationKind::Error),
}
} else {
let last = state
.messages()
.iter()
.rev()
.find(|m| m.role == MessageRole::Assistant);
if let Some(msg) = last {
let content: String = msg
.content_blocks
.iter()
.filter_map(|b| match b {
ContentBlock::Text { content } => Some(content.as_str()),
_ => None,
})
.collect::<Vec<_>>()
.join("\n");
if content.trim().is_empty() {
state.add_notification(
"No text content to copy".to_string(),
NotificationKind::Warning,
);
} else {
match clipboard_write::copy_to_clipboard(&content) {
Ok(()) => state.add_notification(
"Copied to clipboard".to_string(),
NotificationKind::Success,
),
Err(e) => state.add_notification(
format!("Copy failed: {}", e),
NotificationKind::Error,
),
}
}
} else {
state.add_notification(
"No assistant message".to_string(),
NotificationKind::Info,
);
}
}
true
}
"/changelog" => {
let paths = vec![
PathBuf::from("CHANGELOG.md"),
PathBuf::from("../CHANGELOG.md"),
];
let mut entries: Vec<crate::ui::changelog::ChangelogEntry> = Vec::new();
for path in &paths {
let parsed = crate::ui::changelog::parse_changelog(path);
if !parsed.is_empty() {
entries = parsed;
break;
}
}
if entries.is_empty() {
state.add_notification("No changelog found".to_string(), NotificationKind::Info);
} else {
let changelog_entries: Vec<(String, String)> = entries
.iter()
.take(10)
.map(|e| (e.version_string(), e.content.clone()))
.collect();
state.overlay = None;
state.overlay_state = Some(super::overlay::changelog_overlay(changelog_entries));
}
true
}
"/hotkeys" | "/keys" => {
state.overlay = None;
state.overlay_state = Some(super::overlay::hotkeys_overlay());
true
}
"/export" => {
let export_path = arg.map(PathBuf::from);
let meta = ExportMeta {
model: Some(session.model_id()),
provider: None,
exported_at: chrono::Utc::now().timestamp_millis(),
total_user_tokens: None,
total_assistant_tokens: None,
};
let entries: Vec<oxi_store::session::SessionEntry> = state
.messages()
.iter()
.map(|msg| {
let role = match msg.role {
MessageRole::User => "user",
MessageRole::Assistant => "assistant",
MessageRole::System => "system",
};
let content: String = msg
.content_blocks
.iter()
.filter_map(|b| match b {
ContentBlock::Text { content } => Some(content.as_str()),
_ => None,
})
.collect::<Vec<_>>()
.join("\n");
oxi_store::session::SessionEntry::simple_message(role, &content)
})
.collect();
match export::export_to_html(&entries, &meta, &HtmlExportOptions::default()) {
Ok(html) => {
if let Some(path) = export_path {
match std::fs::write(&path, &html) {
Ok(()) => state.add_notification(
format!("Exported: {}", path.display()),
NotificationKind::Success,
),
Err(e) => state.add_notification(
format!("Write failed: {}", e),
NotificationKind::Error,
),
}
} else {
// Auto-save to CWD with session-based filename
let sid = session.session_id();
let short_sid = &sid[..8.min(sid.len())];
let default_name = format!("oxi-export-{}.html", short_sid);
match std::fs::write(&default_name, &html) {
Ok(()) => state.add_notification(
format!("Exported: {} ({} bytes)", default_name, html.len()),
NotificationKind::Success,
),
Err(e) => state.add_notification(
format!("Write failed: {}", e),
NotificationKind::Error,
),
}
}
}
Err(e) => {
state.add_notification(format!("Export failed: {}", e), NotificationKind::Error)
}
}
true
}
"/share" => {
// Check if gh CLI is available
match std::process::Command::new("gh")
.arg("auth")
.arg("status")
.output()
{
Ok(output) if output.status.success() => {
// Export session to HTML first
let meta = ExportMeta {
model: Some(session.model_id()),
provider: None,
exported_at: chrono::Utc::now().timestamp_millis(),
total_user_tokens: None,
total_assistant_tokens: None,
};
let entries: Vec<oxi_store::session::SessionEntry> = state
.messages()
.iter()
.map(|msg| {
let role = match msg.role {
MessageRole::User => "user",
MessageRole::Assistant => "assistant",
MessageRole::System => "system",
};
let content: String = msg
.content_blocks
.iter()
.filter_map(|b| match b {
ContentBlock::Text { content } => Some(content.as_str()),
_ => None,
})
.collect::<Vec<_>>()
.join("\n");
oxi_store::session::SessionEntry::simple_message(role, &content)
})
.collect();
match export::export_to_html(&entries, &meta, &HtmlExportOptions::default()) {
Ok(html) => {
// Write to temp file and create gist
let temp_path = std::env::temp_dir().join("oxi-share.html");
match std::fs::write(&temp_path, &html) {
Ok(()) => {
state.add_notification(
"Creating Gist... (Esc to cancel)".to_string(),
NotificationKind::Info,
);
// Show loader overlay during gist creation
let _sh = session.clone_handle();
let tx = ui_tx.clone();
tokio::spawn(async move {
let result = tokio::process::Command::new("gh")
.args(["gist", "create", &temp_path.to_string_lossy()])
.output()
.await;
let _ = std::fs::remove_file(&temp_path);
match result {
Ok(output) if output.status.success() => {
let stdout =
String::from_utf8_lossy(&output.stdout);
let gist_url = stdout.trim().to_string();
let _ = tx.send(UiEvent::SystemMessage(format!(
"Gist created: {}",
gist_url
)));
}
Ok(output) => {
let stderr =
String::from_utf8_lossy(&output.stderr);
let _ = tx.send(UiEvent::SystemMessage(format!(
"Gist failed: {}",
stderr.trim()
)));
}
Err(e) => {
let _ = tx.send(UiEvent::SystemMessage(format!(
"Gist failed: {}",
e
)));
}
}
});
}
Err(e) => {
state.add_notification(
format!("Error: {}", e),
NotificationKind::Error,
);
}
}
}
Err(e) => {
state.add_notification(
format!("Export failed: {}", e),
NotificationKind::Error,
);
}
}
}
Ok(_output) => {
state.add_notification(
"GitHub CLI not authenticated. Run: gh auth login".to_string(),
NotificationKind::Warning,
);
}
Err(_e) => {
state.add_notification(
"GitHub CLI (gh) not found".to_string(),
NotificationKind::Error,
);
}
}
true
}
"/import" => {
if let Some(path) = arg {
let cwd = std::env::current_dir()
.map(|p| p.to_string_lossy().into_owned())
.unwrap_or_else(|_| ".".to_string());
match oxi_store::session::resolve_session_path(path, &cwd) {
Ok(resolved) => {
if !std::path::Path::new(&resolved).exists() {
state.add_notification(
format!("File not found: {}", resolved),
NotificationKind::Error,
);
} else {
state.next_action =
Some(super::app::TuiNextAction::SwitchSession(resolved.clone()));
state.add_notification(
format!("Importing session from {}...", resolved),
NotificationKind::Info,
);
}
}
Err(e) => {
state.add_notification(
format!("Error resolving path: {}", e),
NotificationKind::Error,
);
}
}
} else {
state.add_notification(
"/import <path> [path-to-jsonl]".to_string(),
NotificationKind::Info,
);
}
true
}
"/fork" => {
if let Some(ref path) = state.session_file_path {
let sm = oxi_store::session::SessionManager::open(path, None, None);
let branch = sm.get_branch(None);
let user_entries: Vec<_> = branch.iter().filter(|e| e.message.is_user()).collect();
if let Some(sel) = arg {
// Resolve the user's selection to a full entry ID.
// Accept: number (1-based index), short ID (prefix), or full ID.
let resolved_id = resolve_entry_id(sel, &user_entries);
match resolved_id {
Some(full_id) => match sm.branch_from_entry(&full_id) {
Ok(new_path) => {
state.next_action =
Some(super::app::TuiNextAction::SwitchSession(new_path));
state.add_notification(
format!("Forked from [{}]", &full_id[..8.min(full_id.len())]),
NotificationKind::Success,
);
}
Err(e) => {
state.add_notification(
format!("Error forking: {}", e),
NotificationKind::Error,
);
}
},
None => {
state.add_notification(
format!("Entry not found: {}", sel),
NotificationKind::Warning,
);
}
}
} else {
// No arg: open interactive fork selector overlay
if user_entries.is_empty() {
state.add_notification(
"No user messages to fork from.".to_string(),
NotificationKind::Info,
);
} else {
let entries: Vec<(String, String)> = user_entries
.iter()
.map(|e| {
let preview: String = e.content().chars().take(60).collect();
(e.id.clone(), preview)
})
.collect();
#[allow(clippy::arc_with_non_send_sync)]
let shared = std::sync::Arc::new(std::sync::Mutex::new(
state as *mut super::app::AppState,
));
state.overlay_state =
Some(Box::new(super::overlay::ForkSelectOverlay::new(
entries,
session.clone_handle(),
shared,
)));
}
}
} else {
state.add_notification(
"No session file available.".to_string(),
NotificationKind::Info,
);
}
true
}
"/clone" => {
if let Some(ref path) = state.session_file_path {
let cwd: String = std::env::current_dir()
.map(|p| p.to_string_lossy().into_owned())
.unwrap_or_else(|_| ".".to_string());
match oxi_store::session::SessionManager::fork_from(path, &cwd, None) {
Ok(new_sm) => {
if let Some(new_path) = new_sm.get_session_file() {
state.add_notification(
format!("Cloned: {}", new_path),
NotificationKind::Success,
);
} else {
state.add_notification(
"Session cloned".to_string(),
NotificationKind::Success,
);
}
}
Err(e) => {
state.add_notification(
format!("Clone failed: {}", e),
NotificationKind::Error,
);
}
}
} else {
state.add_notification("No session to clone.".to_string(), NotificationKind::Info);
}
true
}
"/tree" => {
if let Some(ref path) = state.session_file_path {
let sm = oxi_store::session::SessionManager::open(path, None, None);
match sm.get_tree(uuid::Uuid::nil()) {
Ok(roots) => {
if roots.is_empty() {
state.add_notification(
"Empty session.".to_string(),
NotificationKind::Info,
);
} else {
// Collect all entries from the tree for the overlay
let entries = collect_tree_entries(&roots);
state.overlay_state = Some(super::overlay::tree_navigator(
entries, None, // current leaf detection
session, state,
));
}
}
Err(e) => {
state.add_notification(
format!("Error reading tree: {}", e),
NotificationKind::Error,
);
}
}
} else {
state.add_notification(
"No session file available.".to_string(),
NotificationKind::Info,
);
}
true
}
"/provider" => {
if let Some(provider) = arg {
let parts: Vec<&str> = provider.splitn(2, ' ').collect();
if parts.len() == 2 {
try_provider_with_key(parts[0], parts[1], state);
} else {
state.overlay = Some(AppOverlay::ProviderConfig(SetupStep::EnterApiKey {
provider: parts[0].to_string(),
key: String::new(),
masked_cursor: 0,
}));
}
} else {
state.overlay = Some(AppOverlay::ProviderConfig(SetupStep::SelectAuthType {
auth_type: None,
selected: 0,
}));
}
true
}
"/router" => {
if let Some(sub) = arg {
let mut parts = sub.split_whitespace();
let cmd = parts.next().unwrap_or("");
match cmd {
"status" => {
if let Some(snap) = oxi_ai::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_ai::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(
super::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_ai::router::set_router_pin(Some(
oxi_ai::router::RouterTier::Low,
));
state.add_notification(
"Router pinned to LOW tier".to_string(),
NotificationKind::Success,
);
}
"medium" => {
oxi_ai::router::set_router_pin(Some(
oxi_ai::router::RouterTier::Medium,
));
state.add_notification(
"Router pinned to MEDIUM tier".to_string(),
NotificationKind::Success,
);
}
"high" => {
oxi_ai::router::set_router_pin(Some(
oxi_ai::router::RouterTier::High,
));
state.add_notification(
"Router pinned to HIGH tier".to_string(),
NotificationKind::Success,
);
}
"off" | "none" | "clear" => {
oxi_ai::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_ai::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 = oxi_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(
super::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 =
oxi_store::router_config::load_router_config(&global_dir, &project_dir)
.is_some();
if has_config {
if let Some(snap) = oxi_ai::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_ai::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(
super::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 = oxi_store::auth_storage::shared_auth_storage();
let setup_models: Vec<String> = oxi_ai::model_db::get_all_models()
.filter(|entry| auth.get_api_key(entry.provider).is_some())
.map(|entry| format!("{}/{}", entry.provider, entry.id))
.collect();
let initial = super::overlay::RouterSetupData {
profile_name: "auto".to_string(),
..Default::default()
};
state.overlay = None;
state.overlay_state = Some(super::overlay::router_setup(
initial,
setup_models,
move |data: &super::overlay::RouterSetupData| {
let store_cfg = router_integration::save_router_config(data)?;
let ai_cfg = router_integration::store_config_to_ai_config(&store_cfg);
oxi_ai::router::register_router(&ai_cfg);
Ok(())
},
|| {},
));
}
}
true
}
"/logout" => {
if let Some(provider) = arg {
oxi_store::auth_storage::shared_auth_storage().remove(provider);
state.add_notification(format!("Removed {}", provider), NotificationKind::Success);
} else {
let auth = oxi_store::auth_storage::shared_auth_storage();
let providers = auth.configured_providers();
if providers.is_empty() {
state.add_notification(
"No providers configured.".to_string(),
NotificationKind::Info,
);
} else {
state.overlay = None;
state.overlay_state = Some(super::overlay::logout_select(providers, state));
}
}
true
}
"/new" => {
// Reload settings so the next session uses latest config
let fresh = oxi_store::settings::Settings::load().unwrap_or_default();
session.set_thinking_level(fresh.thinking_level);
if let Some(m) = fresh.effective_model(None) {
if !m.is_empty() {
// effective_model may already include the provider ("provider/model")
// or be just a model id. Only prepend provider when needed.
let full_id = if m.contains('/') {
m.clone()
} else {
let p = fresh.effective_provider(None).unwrap_or_default();
format!("{}/{}", p, m)
};
if let Ok(()) = session.set_model(&full_id) {
let parts: Vec<&str> = full_id.splitn(2, '/').collect();
state.footer_state.data.model_name = full_id.clone();
if parts.len() == 2 {
state.footer_state.data.provider_name = parts[0].to_string();
}
}
}
}
state.chat.clear();
session.reset();
state.add_notification("New session started".to_string(), NotificationKind::Success);
state.next_action = Some(super::app::TuiNextAction::NewSession);
true
}
"/resume" => {
let cwd = std::env::current_dir()
.map(|p| p.to_string_lossy().to_string())
.unwrap_or_else(|_| ".".to_string());
// SessionManager::list is async but only does std::fs I/O.
// Use spawn_blocking to avoid "Cannot start a runtime from within
// a runtime" panic when called from inside the tokio runtime.
let list_result = std::thread::scope(|s| {
s.spawn(|| {
// Build a temp runtime on this OS thread — safe because it's
// a fresh thread, not the TUI's tokio worker thread.
let rt = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.expect("failed to build temp runtime");
rt.block_on(oxi_store::session::SessionManager::list(&cwd, None))
})
.join()
.unwrap_or_else(|e| {
let msg = e
.downcast_ref::<&str>()
.map(|s| s.to_string())
.or_else(|| e.downcast_ref::<String>().cloned())
.unwrap_or_else(|| "unknown panic".to_string());
Err(anyhow::anyhow!("thread panicked: {}", msg))
})
});
match list_result {
Ok(sessions) if sessions.is_empty() => {
state.add_notification(
"No previous sessions found.".to_string(),
NotificationKind::Info,
);
}
Ok(sessions) => {
let recent: Vec<_> = sessions.into_iter().take(15).collect();
state.overlay = None;
state.overlay_state = Some(super::overlay::resume_select(recent));
}
Err(e) => {
state.add_notification(
format!("Error listing sessions: {}", e),
NotificationKind::Error,
);
}
}
true
}
"/reload" => {
let reloaded = oxi_store::settings::Settings::load().unwrap_or_default();
let _theme_name = reloaded.theme.clone();
session.set_thinking_level(reloaded.thinking_level);
// Apply model change to the active agent session
if let Some(m) = reloaded.effective_model(None) {
if !m.is_empty() {
let full_id = if m.contains('/') {
m
} else {
let p = reloaded.effective_provider(None).unwrap_or_default();
format!("{}/{}", p, m)
};
match session.set_model(&full_id) {
Ok(()) => {
let parts: Vec<&str> = full_id.splitn(2, '/').collect();
state.footer_state.data.model_name = full_id.clone();
if parts.len() == 2 {
state.footer_state.data.provider_name = parts[0].to_string();
}
}
Err(e) => {
state.add_notification(
format!("Warning: Could not apply model: {}", e),
NotificationKind::Warning,
);
}
}
}
}
// Reload WASM extensions
if reloaded.extensions_enabled {
let cwd_path =
std::env::current_dir().unwrap_or_else(|_| std::path::PathBuf::from("."));
let wasm_paths = crate::extensions::WasmExtensionManager::discover(&cwd_path);
if !wasm_paths.is_empty() {
let mut mgr = crate::extensions::WasmExtensionManager::new();
let (loaded, errors) = mgr.load_all(&wasm_paths);
let loaded_count = loaded.len();
let error_count = errors.len();
if !mgr.is_empty() {
// Unregister old WASM tools
let tools = session.agent_ref().tools();
let old_names: Vec<String> = if let Some(ref old_ext) = state.wasm_ext {
old_ext
.all_tool_defs()
.iter()
.map(|d| d.name.clone())
.collect()
} else {
vec![]
};
for name in &old_names {
tools.unregister(name);
}
// Register new WASM tools
let arc_mgr = std::sync::Arc::new(mgr);
for tool_def in arc_mgr.all_tool_defs() {
let wasm_tool = crate::extensions::WasmTool::new(
arc_mgr.clone(),
tool_def.name.clone(),
tool_def.description.clone(),
tool_def.schema.clone(),
);
tools.register(wasm_tool);
}
state.wasm_ext = Some(arc_mgr);
state.add_notification(
format!("{} loaded, {} error(s)", loaded_count, error_count),
NotificationKind::Info,
);
} else {
state.wasm_ext = None;
}
} else {
state.wasm_ext = None;
}
} else {
state.wasm_ext = None;
}
// Reload skills
{
let new_mgr = crate::skills::SkillManager::discover_all(
&std::env::current_dir().unwrap_or_else(|_| std::path::PathBuf::from(".")),
&[],
)
.unwrap_or_else(|_| crate::skills::SkillManager::new());
let count = new_mgr.len();
*state.skills.write() = new_mgr;
if count > 0 {
state.add_notification(
format!("{} skill(s) loaded", count),
NotificationKind::Info,
);
}
}
state.add_notification("Settings reloaded".to_string(), NotificationKind::Success);
true
}
"/skill" => {
if let Some(sub) = arg {
let parts: Vec<&str> = sub.splitn(2, ' ').collect();
if parts[0].eq_ignore_ascii_case("off") {
// Deactivate a skill
let name = parts.get(1).unwrap_or(&"").trim();
if name.is_empty() {
state.add_notification(
"/skill off <name>".to_string(),
NotificationKind::Info,
);
} else {
let mut active = state.active_skills.write();
let name_lower = name.to_lowercase();
if active.iter().any(|n| n.eq_ignore_ascii_case(&name_lower)) {
active.retain(|n| !n.eq_ignore_ascii_case(&name_lower));
drop(active);
state.add_notification(
format!("Skill deactivated: {}", name),
NotificationKind::Success,
);
} else {
drop(active);
state.add_notification(
format!("Skill '{}' is not active", name),
NotificationKind::Warning,
);
}
}
} else {
// Activate a skill
let skills = state.skills.read();
if let Some(skill) = skills.get(sub.trim()) {
let name = skill.name.clone();
drop(skills);
let mut active = state.active_skills.write();
let name_lower = name.to_lowercase();
if active.iter().any(|n| n.eq_ignore_ascii_case(&name_lower)) {
drop(active);
state.add_notification(
format!("Skill '{}' is already active", name),
NotificationKind::Info,
);
} else {
active.push(name_lower);
drop(active);
state.add_notification(
format!("Skill activated: {}", name),
NotificationKind::Success,
);
}
} else {
drop(skills);
state.add_notification(
format!("Skill '{}' not found", sub.trim()),
NotificationKind::Warning,
);
}
}
} else {
// List all skills with status
let (is_empty, listing) = {
let skills = state.skills.read();
let active = state.active_skills.read();
if skills.is_empty() {
(true, String::new())
} else {
let mut out = String::from("Skills:\n\n");
for skill in skills.all() {
let is_active =
active.iter().any(|n| n.eq_ignore_ascii_case(&skill.name));
let status = if is_active { "\u{2713}" } else { " " };
out.push_str(&format!(
" [{}] {} — {}\n",
status, skill.name, skill.description
));
}
out.push_str("\n/skill <name> Activate a skill");
out.push_str("\n/skill off <name> Deactivate a skill");
(false, out)
}
};
if is_empty {
state.add_notification(
"No skills found. Place skills in ~/.oxi/skills/<name>/SKILL.md"
.to_string(),
NotificationKind::Info,
);
} else {
state.overlay = None;
state.overlay_state = Some(super::overlay::tools_overlay(listing));
}
}
true
}
"/scoped-models" | "/models" => {
if let Some(models_str) = arg {
let models: Vec<ScopedModel> = models_str
.split(',')
.filter_map(|s| {
let parts: Vec<&str> = s.trim().split('/').collect();
if parts.len() >= 2 {
Some(ScopedModel {
provider: parts[0].to_string(),
model_id: parts[1..].join("/"),
})
} else {
None
}
})
.collect();
if !models.is_empty() {
session.set_scoped_models(models.clone());
let names: Vec<String> = models
.iter()
.map(|m| format!("{}/{}", m.provider, m.model_id))
.collect();
state.add_notification(
format!("Scoped: {}", names.join(", ")),
NotificationKind::Info,
);
} else {
state.add_notification(
"Usage: /scoped-models provider/model1,provider/model2".to_string(),
NotificationKind::Info,
);
}
} else {
let scoped = session.scoped_models();
if scoped.is_empty() {
state.add_notification("No scoped models".to_string(), NotificationKind::Info);
} else {
let names: Vec<String> = scoped
.iter()
.map(|m| format!("{}/{}", m.provider, m.model_id))
.collect();
state.add_notification(
format!("Scoped: {}", names.join(", ")),
NotificationKind::Info,
);
}
}
true
}
_ => {
// Check WASM extension commands
if let Some(ref wasm_ext) = state.wasm_ext {
let commands = wasm_ext.all_command_defs();
let name = cmd.strip_prefix('/').unwrap_or(cmd);
if let Some((ext_name, _cmd)) = commands.iter().find(|(_, c)| c.name == name) {
let output = wasm_ext
.execute_command(name, arg.unwrap_or(""))
.unwrap_or_else(|e| format!("Error: {}", e));
state.add_notification(
format!("[{}] {}", ext_name, output),
NotificationKind::Info,
);
return true;
}
}
state.add_notification(
format!("Unknown command: {}", cmd),
NotificationKind::Warning,
);
false
}
}
}
// ── Entry ID resolution for /fork ─────────────────────────────────────────
/// Resolve a user-provided selector to a full entry ID.
///
/// Accepts:
/// - A 1-based number ("1", "2", ...) matching the displayed list
/// - A short ID prefix ("abc12345") that matches the start of a full ID
/// - A full UUID
///
/// Returns `None` if nothing matches.
fn resolve_entry_id(sel: &str, entries: &[&oxi_store::session::SessionEntry]) -> Option<String> {
// Try numeric index first (1-based)
if let Ok(idx) = sel.parse::<usize>() {
if idx >= 1 && idx <= entries.len() {
return Some(entries[idx - 1].id.clone());
}
}
// Try prefix match or full match on entry IDs
for entry in entries {
if entry.id == sel || entry.id.starts_with(sel) {
return Some(entry.id.clone());
}
}
None
}
// ── Help text ────────────────────────────────────────────────────────────────
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 (coming soon)
/router disable Disable router, return to fixed model
Or select \"router/auto\" in /model"#
.to_string()
}
#[allow(dead_code)]
fn format_help() -> String {
r#"
Session
/new Start a new session
/clone Duplicate current session
/resume Resume a previous session
/import <path> Import session from JSONL
/tree Show session tree
/fork List messages to fork from
/fork <number> Fork from a message by list number
/fork <id> Fork from a specific message ID
/session Show session info
/name <name> Set session name
Model
/model [id] Switch or show model
/scoped-models Models for Ctrl+P cycling
/router Configure model router
Context
/compact [instr] Compact context
Tools
/tools List active tools
/tools <name> Toggle tool on/off
/extensions List extensions & WASM tools
/ext Alias for /extensions
Export
/export [path] Export to HTML
/copy Copy code block / last reply
Auth
/provider [name] Configure API key
/logout [name] Remove key
Info
/help This help
/hotkeys Key shortcuts
/changelog Changelog
/settings Current settings
/reload Reload config
/quit Quit
Keys
Enter Send
Ctrl+C Interrupt / Quit
PageUp/Down Scroll
/ Slash commands
"#
.to_string()
}
#[allow(dead_code)]
fn format_hotkeys() -> String {
r#"
Navigation
Enter Submit input
Escape Cancel
PageUp/PageDown Scroll chat
Editor
←/→ Move cursor
Home/End Start/End of line
Backspace Delete char
Ctrl+←/→ Move by word
Session
Ctrl+C Interrupt / Quit
Ctrl+Y Copy last code block
Ctrl+P Cycle models
Shift+Ctrl+P Cycle models (reverse)
"#
.to_string()
}
// ── Interactive login ────────────────────────────────────────────────────
/// /provider <provider> <key> — save the key directly
fn try_provider_with_key(provider: &str, key: &str, state: &mut AppState) -> bool {
if key.is_empty() {
return false;
}
let auth = oxi_store::auth_storage::shared_auth_storage();
auth.set_api_key(provider, key.to_string());
state.add_notification(
format!("API key for {} saved.", provider),
NotificationKind::Success,
);
true
}
/// API key masking (first 6 chars + ... + last 4 chars)
#[allow(dead_code)]
fn mask_key(key: &str) -> String {
if key.len() <= 12 {
return "***".to_string();
}
format!("{}...{}", &key[..6], &key[key.len() - 4..])
}
// ── Tool toggle ─────────────────────────────────────────────────────────
/// Built-in tool definitions (name) for toggle validation.
const BUILTIN_TOOL_NAMES: &[&str] = &[
"read",
"write",
"edit",
"bash",
"grep",
"find",
"ls",
"web_search",
"get_search_results",
"github",
"github_search",
"subagent",
];
/// Handle `/tools <name>` — toggle a tool on/off.
fn handle_tool_command(
action: &str,
registry: &std::sync::Arc<oxi_agent::ToolRegistry>,
state: &mut AppState,
) {
let tool_name = action.trim().to_lowercase();
let is_known =
BUILTIN_TOOL_NAMES.contains(&tool_name.as_str()) || registry.get(&tool_name).is_some();
if !is_known {
state.add_notification(
format!("Unknown tool: {}", tool_name),
NotificationKind::Warning,
);
return;
}
if registry.get(&tool_name).is_some() {
if let Some(tool) = registry.get(&tool_name) {
if tool.essential() {
state.add_notification(
format!("Cannot disable essential tool: {}", tool_name),
NotificationKind::Warning,
);
return;
}
}
registry.unregister(&tool_name);
if tool_name == "web_search" {
registry.unregister("get_search_results");
}
state.add_notification(
format!("Tool disabled: {}", tool_name),
NotificationKind::Info,
);
} else {
let re_registered = try_re_register_tool(&tool_name, registry);
if re_registered {
state.add_notification(
format!("Tool enabled: {}", tool_name),
NotificationKind::Success,
);
} else {
state.add_notification(
format!("Cannot re-enable {}", tool_name),
NotificationKind::Warning,
);
}
}
}
/// Collect all entries from a tree of SessionTreeNodes into a flat Vec<SessionEntry>.
/// Preserves tree order (depth-first traversal).
fn collect_tree_entries(
roots: &[oxi_store::session::SessionTreeNode],
) -> Vec<oxi_store::session::SessionEntry> {
let mut entries = Vec::new();
fn visit(
node: &oxi_store::session::SessionTreeNode,
entries: &mut Vec<oxi_store::session::SessionEntry>,
) {
entries.push(node.entry.clone());
for child in &node.children {
visit(child, entries);
}
}
for root in roots {
visit(root, &mut entries);
}
entries
}
/// Try to re-register a previously disabled built-in tool.
fn try_re_register_tool(name: &str, registry: &std::sync::Arc<oxi_agent::ToolRegistry>) -> bool {
use std::sync::Arc;
match name {
"read" | "write" | "edit" | "bash" | "grep" | "find" | "ls" => return false,
"web_search" => {
let cache = Arc::new(oxi_agent::SearchCache::new());
registry.register(oxi_agent::WebSearchTool::new(cache.clone()));
registry.register(oxi_agent::GetSearchResultsTool::new(cache));
}
"get_search_results" => {
if registry.get("web_search").is_some() {
return false;
}
let cache = Arc::new(oxi_agent::SearchCache::new());
registry.register(oxi_agent::GetSearchResultsTool::new(cache));
}
"github" | "github_search" => {
let cache = Arc::new(oxi_agent::SearchCache::new());
registry.register(oxi_agent::GitHubTool::new(cache));
}
"subagent" => registry.register(oxi_agent::SubagentTool::with_cwd(
std::path::PathBuf::from("."),
)),
_ => return false,
}
true
}