-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathregistry.rs
More file actions
1129 lines (973 loc) · 28.1 KB
/
registry.rs
File metadata and controls
1129 lines (973 loc) · 28.1 KB
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
//! Command registry for cortex-tui slash commands.
//!
//! This module provides the `CommandRegistry` which stores all available
//! commands and provides lookup by name or alias.
use std::collections::HashMap;
use super::types::{CommandCategory, CommandDef};
// ============================================================
// COMMAND REGISTRY
// ============================================================
/// Registry storing all available slash commands.
///
/// Provides efficient lookup by command name or alias, and supports
/// filtering by category.
pub struct CommandRegistry {
/// Map from primary command name to definition.
commands: HashMap<&'static str, CommandDef>,
/// Map from alias to primary command name.
aliases: HashMap<&'static str, &'static str>,
}
impl CommandRegistry {
/// Creates a new empty command registry.
pub fn new() -> Self {
Self {
commands: HashMap::new(),
aliases: HashMap::new(),
}
}
/// Registers a command in the registry.
///
/// Also registers all aliases to point to the primary command name.
pub fn register(&mut self, def: CommandDef) {
// Register aliases
for alias in def.aliases {
self.aliases.insert(alias, def.name);
}
// Register the command
self.commands.insert(def.name, def);
}
/// Gets a command by name or alias.
///
/// The lookup is case-insensitive.
pub fn get(&self, name: &str) -> Option<&CommandDef> {
let name_lower = name.to_lowercase();
// Try direct lookup first
if let Some(def) = self.commands.get(name_lower.as_str()) {
return Some(def);
}
// Try alias lookup
if let Some(&primary) = self.aliases.get(name_lower.as_str()) {
return self.commands.get(primary);
}
// Try case-insensitive scan as fallback
for (cmd_name, def) in &self.commands {
if cmd_name.eq_ignore_ascii_case(&name_lower) {
return Some(def);
}
}
for (alias, primary) in &self.aliases {
if alias.eq_ignore_ascii_case(&name_lower) {
return self.commands.get(primary);
}
}
None
}
/// Gets all commands in a specific category.
///
/// Results are sorted by command name.
pub fn get_by_category(&self, category: CommandCategory) -> Vec<&CommandDef> {
let mut commands: Vec<_> = self
.commands
.values()
.filter(|def| def.category == category && !def.hidden)
.collect();
commands.sort_by_key(|def| def.name);
commands
}
/// Gets all visible (non-hidden) commands.
///
/// Results are sorted by command name.
pub fn all(&self) -> Vec<&CommandDef> {
let mut commands: Vec<_> = self.commands.values().filter(|def| !def.hidden).collect();
commands.sort_by_key(|def| def.name);
commands
}
/// Gets all commands including hidden ones.
pub fn all_including_hidden(&self) -> Vec<&CommandDef> {
let mut commands: Vec<_> = self.commands.values().collect();
commands.sort_by_key(|def| def.name);
commands
}
/// Checks if a command exists by name or alias.
pub fn exists(&self, name: &str) -> bool {
self.get(name).is_some()
}
/// Returns the number of registered commands.
pub fn len(&self) -> usize {
self.commands.len()
}
/// Returns true if no commands are registered.
pub fn is_empty(&self) -> bool {
self.commands.is_empty()
}
/// Returns all command names (primary names only).
pub fn command_names(&self) -> Vec<&'static str> {
let mut names: Vec<_> = self.commands.keys().copied().collect();
names.sort();
names
}
/// Returns all names including aliases.
pub fn all_names(&self) -> Vec<&'static str> {
let mut names: Vec<_> = self
.commands
.keys()
.copied()
.chain(self.aliases.keys().copied())
.collect();
names.sort();
names.dedup();
names
}
}
impl Default for CommandRegistry {
fn default() -> Self {
let mut registry = Self::new();
register_builtin_commands(&mut registry);
registry
}
}
// ============================================================
// BUILTIN COMMANDS
// ============================================================
/// Registers all builtin commands in the registry.
pub fn register_builtin_commands(registry: &mut CommandRegistry) {
// ========================================
// GENERAL COMMANDS
// ========================================
registry.register(CommandDef::new(
"help",
&["h", "?"],
"Show help information",
"/help [topic]",
CommandCategory::General,
true,
));
registry.register(CommandDef::new(
"quit",
&["q", "exit"],
"Quit the application",
"/quit",
CommandCategory::General,
false,
));
registry.register(CommandDef::new(
"version",
&["v"],
"Show version information",
"/version",
CommandCategory::General,
false,
));
registry.register(CommandDef::new(
"upgrade",
&["update"],
"Check for and install updates",
"/upgrade",
CommandCategory::General,
false,
));
registry.register(CommandDef::new(
"settings",
&["config", "prefs"],
"Open settings panel",
"/settings",
CommandCategory::General,
false,
));
// Config reload command (#2806)
registry.register(CommandDef::new(
"reload-config",
&["reload"],
"Reload configuration from disk",
"/reload-config",
CommandCategory::General,
false,
));
registry.register(CommandDef::new(
"copy",
&["cp"],
"Show how to copy text",
"/copy",
CommandCategory::General,
false,
));
registry.register(CommandDef::new(
"theme",
&[],
"Change color theme",
"/theme [name]",
CommandCategory::General,
true,
));
registry.register(CommandDef::new(
"compact",
&[],
"Toggle compact display mode",
"/compact",
CommandCategory::General,
false,
));
registry.register(CommandDef::new(
"palette",
&["cmd"],
"Open command palette",
"/palette",
CommandCategory::General,
false,
));
registry.register(CommandDef::new(
"init",
&[],
"Initialize AGENTS.md in project directory",
"/init [--force]",
CommandCategory::General,
true,
));
registry.register(CommandDef::new(
"commands",
&["cmds"],
"List all available custom commands",
"/commands",
CommandCategory::General,
false,
));
registry.register(CommandDef::new(
"agents",
&["subagents"],
"List and manage custom agents",
"/agents",
CommandCategory::General,
false,
));
registry.register(CommandDef::new(
"tasks",
&["bg", "background"],
"View and manage background tasks",
"/tasks",
CommandCategory::General,
false,
));
registry.register(CommandDef::new(
"skills",
&["sk"],
"List and manage skills",
"/skills",
CommandCategory::General,
false,
));
registry.register(CommandDef::new(
"skill",
&["invoke"],
"Invoke a skill by name",
"/skill <name> [args...]",
CommandCategory::General,
true,
));
registry.register(CommandDef::new(
"skill-reload",
&["sr"],
"Reload skills from disk",
"/skill-reload",
CommandCategory::General,
false,
));
registry.register(CommandDef::new(
"share",
&[],
"Generate a share link for current session",
"/share [duration]",
CommandCategory::Session,
true,
));
// ========================================
// AUTH COMMANDS
// ========================================
registry.register(CommandDef::new(
"login",
&["signin"],
"Authenticate with Cortex",
"/login",
CommandCategory::Auth,
false,
));
registry.register(CommandDef::new(
"logout",
&["signout"],
"Clear stored credentials",
"/logout",
CommandCategory::Auth,
false,
));
registry.register(CommandDef::new(
"account",
&["whoami", "me"],
"Show account information",
"/account",
CommandCategory::Auth,
false,
));
// ========================================
// BILLING COMMANDS
// ========================================
registry.register(CommandDef::new(
"billing",
&["plan", "subscription"],
"Show billing status and credits",
"/billing",
CommandCategory::Billing,
false,
));
registry.register(CommandDef::new(
"usage",
&["stats", "credits"],
"Show detailed usage breakdown",
"/usage [--from YYYY-MM-DD] [--to YYYY-MM-DD]",
CommandCategory::Billing,
true,
));
registry.register(CommandDef::new(
"refresh",
&["retry"],
"Refresh billing status after adding payment method",
"/refresh",
CommandCategory::Billing,
false,
));
// ========================================
// SESSION COMMANDS
// ========================================
registry.register(CommandDef::new(
"session",
&["info"],
"Show current session info",
"/session",
CommandCategory::Session,
false,
));
registry.register(CommandDef::new(
"clear",
&["cls"],
"Clear current conversation",
"/clear",
CommandCategory::Session,
false,
));
registry.register(CommandDef::new(
"new",
&["n"],
"Start a new session",
"/new",
CommandCategory::Session,
false,
));
registry.register(CommandDef::new(
"resume",
&["r", "load"],
"Resume a previous session",
"/resume [session-id]",
CommandCategory::Session,
true,
));
registry.register(CommandDef::new(
"sessions",
&["list", "ls-sessions"],
"List all sessions",
"/sessions",
CommandCategory::Session,
false,
));
registry.register(CommandDef::new(
"fork",
&["branch"],
"Fork current session",
"/fork [name]",
CommandCategory::Session,
true,
));
registry.register(CommandDef::new(
"rename",
&["mv"],
"Rename current session",
"/rename <name>",
CommandCategory::Session,
true,
));
registry.register(CommandDef::new(
"favorite",
&["fav", "star"],
"Mark session as favorite",
"/favorite",
CommandCategory::Session,
false,
));
registry.register(CommandDef::new(
"unfavorite",
&["unfav", "unstar"],
"Remove favorite mark",
"/unfavorite",
CommandCategory::Session,
false,
));
registry.register(CommandDef::new(
"export",
&["save"],
"Export session to file",
"/export [format]",
CommandCategory::Session,
true,
));
registry.register(CommandDef::new(
"timeline",
&["tl"],
"View session timeline",
"/timeline",
CommandCategory::Session,
false,
));
registry.register(CommandDef::new(
"rewind",
&["rw"],
"Rewind to a previous point",
"/rewind [steps]",
CommandCategory::Session,
true,
));
registry.register(CommandDef::new(
"undo",
&["u"],
"Undo last action",
"/undo",
CommandCategory::Session,
false,
));
registry.register(CommandDef::new(
"redo",
&[],
"Redo last undone action",
"/redo",
CommandCategory::Session,
false,
));
registry.register(CommandDef::new(
"delete",
&["rm"],
"Delete a session",
"/delete [session-id]",
CommandCategory::Session,
true,
));
// ========================================
// NAVIGATION COMMANDS
// ========================================
registry.register(CommandDef::new(
"diff",
&["d"],
"Show file diff",
"/diff [file]",
CommandCategory::Navigation,
true,
));
registry.register(CommandDef::new(
"transcript",
&["tr"],
"View session transcript",
"/transcript",
CommandCategory::Navigation,
false,
));
registry.register(CommandDef::new(
"history",
&["hist"],
"View command history",
"/history",
CommandCategory::Navigation,
false,
));
registry.register(CommandDef::new(
"scroll",
&[],
"Scroll to position",
"/scroll <top|bottom|n>",
CommandCategory::Navigation,
true,
));
registry.register(CommandDef::new(
"goto",
&["g"],
"Go to message number",
"/goto <n>",
CommandCategory::Navigation,
true,
));
// ========================================
// FILE COMMANDS
// ========================================
registry.register(CommandDef::new(
"add",
&["a", "include"],
"Add file to context",
"/add <file>...",
CommandCategory::Files,
true,
));
registry.register(CommandDef::new(
"remove",
&["rm-file", "exclude"],
"Remove file from context",
"/remove <file>...",
CommandCategory::Files,
true,
));
registry.register(CommandDef::new(
"search",
&["find", "grep"],
"Search in files",
"/search <pattern>",
CommandCategory::Files,
true,
));
registry.register(CommandDef::new(
"ls",
&["dir", "files"],
"List files in directory",
"/ls [path]",
CommandCategory::Files,
true,
));
registry.register(CommandDef::new(
"mention",
&["@", "ref"],
"Mention a file or symbol",
"/mention <file|symbol>",
CommandCategory::Files,
true,
));
registry.register(CommandDef::new(
"images",
&["img", "pics"],
"Add images to context",
"/images <file>...",
CommandCategory::Files,
true,
));
registry.register(CommandDef::new(
"tree",
&[],
"Show directory tree",
"/tree [path]",
CommandCategory::Files,
true,
));
registry.register(CommandDef::new(
"context",
&["ctx"],
"Show current context files",
"/context",
CommandCategory::Files,
false,
));
// ========================================
// MODEL COMMANDS
// ========================================
registry.register(CommandDef::new(
"model",
&["m"],
"Switch to a model",
"/model <name>",
CommandCategory::Model,
true,
));
registry.register(CommandDef::new(
"models",
&["lm", "list-models"],
"List available models",
"/models",
CommandCategory::Model,
false,
));
registry.register(CommandDef::new(
"approval",
&["approve"],
"Set tool approval mode",
"/approval <ask|session|always>",
CommandCategory::Model,
true,
));
registry.register(CommandDef::new(
"sandbox",
&["sb"],
"Toggle sandbox mode",
"/sandbox [on|off]",
CommandCategory::Model,
true,
));
registry.register(CommandDef::new(
"auto",
&["autopilot"],
"Toggle auto-approve mode",
"/auto [on|off]",
CommandCategory::Model,
true,
));
// NOTE: /provider and /providers are deprecated. Cortex is now a unified platform.
// These commands are hidden from help but still work (with deprecation warning).
registry.register(CommandDef::hidden(
"provider",
&["prov"],
"[Deprecated] Switch provider - Cortex is now a unified platform",
"/provider <name>",
CommandCategory::Model,
true,
));
registry.register(CommandDef::hidden(
"providers",
&["lp", "list-providers"],
"[Deprecated] List available providers - Cortex is now a unified platform",
"/providers",
CommandCategory::Model,
false,
));
registry.register(CommandDef::new(
"temperature",
&["temp"],
"Set temperature",
"/temperature <0.0-2.0>",
CommandCategory::Model,
true,
));
registry.register(CommandDef::new(
"tokens",
&["max-tokens"],
"Set max tokens",
"/tokens <n>",
CommandCategory::Model,
true,
));
// ========================================
// MCP COMMANDS
// ========================================
registry.register(CommandDef::new(
"mcp",
&[],
"MCP server management (interactive)",
"/mcp",
CommandCategory::Mcp,
false,
));
// NOTE: /mcp-tools, /mcp-auth, /mcp-reload, /mcp-logs are deprecated.
// All MCP management is now centralized in the interactive /mcp panel.
// These commands are hidden but still work for backwards compatibility.
registry.register(CommandDef::hidden(
"mcp-tools",
&["tools", "lt"],
"[Deprecated] Use /mcp instead - List MCP tools",
"/mcp-tools [server]",
CommandCategory::Mcp,
true,
));
registry.register(CommandDef::hidden(
"mcp-auth",
&["auth"],
"[Deprecated] Use /mcp instead - Authenticate MCP server",
"/mcp-auth <server>",
CommandCategory::Mcp,
true,
));
registry.register(CommandDef::hidden(
"mcp-reload",
&["reload"],
"[Deprecated] Use /mcp instead - Reload MCP servers",
"/mcp-reload [server]",
CommandCategory::Mcp,
true,
));
registry.register(CommandDef::hidden(
"mcp-logs",
&[],
"[Deprecated] Use /mcp instead - View MCP server logs",
"/mcp-logs [server]",
CommandCategory::Mcp,
true,
));
// ========================================
// DEBUG COMMANDS
// ========================================
registry.register(CommandDef::new(
"debug",
&["dbg"],
"Toggle debug mode",
"/debug [on|off]",
CommandCategory::Debug,
true,
));
registry.register(CommandDef::new(
"status",
&["stat"],
"Show application status",
"/status",
CommandCategory::Debug,
false,
));
registry.register(CommandDef::new(
"config",
&["cfg"],
"Show configuration",
"/config [key]",
CommandCategory::Debug,
true,
));
registry.register(CommandDef::new(
"logs",
&["log"],
"View application logs",
"/logs [level]",
CommandCategory::Debug,
true,
));
registry.register(CommandDef::new(
"dump",
&[],
"Dump session state",
"/dump [file]",
CommandCategory::Debug,
true,
));
registry.register(CommandDef::new(
"metrics",
&["perf"],
"Show performance metrics",
"/metrics",
CommandCategory::Debug,
false,
));
registry.register(CommandDef::hidden(
"crash",
&[],
"Test crash handler",
"/crash",
CommandCategory::Debug,
false,
));
registry.register(CommandDef::hidden(
"eval",
&[],
"Evaluate expression",
"/eval <expr>",
CommandCategory::Debug,
true,
));
}
// ============================================================
// TESTS
// ============================================================
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_registry_new() {
let registry = CommandRegistry::new();
assert!(registry.is_empty());
}
#[test]
fn test_registry_default_has_commands() {
let registry = CommandRegistry::default();
assert!(!registry.is_empty());
assert!(registry.len() >= 49);
}
#[test]
fn test_registry_get_by_name() {
let registry = CommandRegistry::default();
let help = registry.get("help").unwrap();
assert_eq!(help.name, "help");
assert_eq!(help.category, CommandCategory::General);
}
#[test]
fn test_registry_get_by_alias() {
let registry = CommandRegistry::default();
let help = registry.get("h").unwrap();
assert_eq!(help.name, "help");
let help = registry.get("?").unwrap();
assert_eq!(help.name, "help");
}
#[test]
fn test_registry_case_insensitive() {
let registry = CommandRegistry::default();
assert!(registry.get("HELP").is_some());
assert!(registry.get("Help").is_some());
assert!(registry.get("H").is_some());
}
#[test]
fn test_registry_get_nonexistent() {
let registry = CommandRegistry::default();
assert!(registry.get("nonexistent").is_none());
}
#[test]
fn test_registry_exists() {
let registry = CommandRegistry::default();
assert!(registry.exists("help"));
assert!(registry.exists("h"));
assert!(!registry.exists("nonexistent"));
}
#[test]
fn test_registry_get_by_category() {
let registry = CommandRegistry::default();
let general = registry.get_by_category(CommandCategory::General);
assert!(!general.is_empty());
assert!(
general
.iter()
.all(|cmd| cmd.category == CommandCategory::General)
);
let session = registry.get_by_category(CommandCategory::Session);
assert!(!session.is_empty());
assert!(
session
.iter()
.all(|cmd| cmd.category == CommandCategory::Session)
);
}
#[test]
fn test_registry_all_sorted() {
let registry = CommandRegistry::default();
let all = registry.all();
// Check sorted order
for i in 1..all.len() {
assert!(all[i - 1].name <= all[i].name);
}
}
#[test]
fn test_registry_hidden_commands() {
let registry = CommandRegistry::default();
// Hidden commands should exist
assert!(registry.exists("crash"));
// But not appear in all()
let all = registry.all();
assert!(!all.iter().any(|cmd| cmd.name == "crash"));
// But appear in all_including_hidden()
let all_hidden = registry.all_including_hidden();
assert!(all_hidden.iter().any(|cmd| cmd.name == "crash"));
}
#[test]
fn test_registry_command_names() {
let registry = CommandRegistry::default();
let names = registry.command_names();
assert!(names.contains(&"help"));
assert!(names.contains(&"quit"));
assert!(!names.contains(&"h")); // alias, not primary
}
#[test]
fn test_registry_all_names() {
let registry = CommandRegistry::default();
let names = registry.all_names();
assert!(names.contains(&"help"));
assert!(names.contains(&"h")); // includes aliases
assert!(names.contains(&"?"));
}