aboutsummaryrefslogtreecommitdiffstats
path: root/crates/atuin-ai/src/tui/slash.rs
blob: 7d5e6fa8a9af91c8741aacbebab21dbb13bb24f5 (plain) (blame)
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
#[derive(Debug, Clone)]
pub(crate) struct SlashCommand {
    pub name: String,
    pub description: String,
}

impl SlashCommand {
    pub fn new(name: &str, description: &str) -> Self {
        Self {
            name: name.to_string(),
            description: description.to_string(),
        }
    }
}

#[derive(Debug)]
pub(crate) struct SlashCommandRegistry {
    commands: Vec<SlashCommand>,
}

#[derive(Debug, Clone)]
pub(crate) struct SlashCommandSearchResult {
    pub command: SlashCommand,
    pub relevance: f32,
    pub span: (usize, usize),
}

impl SlashCommandRegistry {
    pub fn new() -> Self {
        Self {
            commands: Vec::new(),
        }
    }

    pub fn register(&mut self, command: SlashCommand) {
        self.commands.push(command);
    }

    pub fn get_commands(&self) -> &[SlashCommand] {
        &self.commands
    }

    pub fn search_fuzzy(&self, query: &str) -> Vec<SlashCommandSearchResult> {
        let query_lower = query.to_lowercase();

        self.commands
            .iter()
            .filter_map(|command| {
                let name_lower = command.name.to_lowercase();
                if let Some(start) = name_lower.find(&query_lower as &str) {
                    let end = start + query_lower.len();
                    Some((command, start, end))
                } else {
                    None
                }
            })
            .map(|(command, start, end)| {
                SlashCommandSearchResult {
                    command: command.clone(),
                    relevance: 1.0, // Simple relevance score for now
                    span: (start, end),
                }
            })
            .collect()
    }
}

impl Default for SlashCommandRegistry {
    fn default() -> Self {
        let mut registry = Self::new();
        registry.register(SlashCommand::new("help", "Show help information"));
        registry.register(SlashCommand::new(
            "new",
            "Start a new conversation, archiving the current one",
        ));

        registry
    }
}