1package modes
2
3import (
4 "strings"
5
6 "github.com/patriceckhart/zot/internal/tui"
7)
8
9// slashCommand is one entry in the autocomplete popup.
10type slashCommand struct {
11 Name string // with leading "/"
12 Desc string
13}
14
15// slashCancelsTurn reports whether the named slash command, when run
16// while a turn is in flight, requires the active turn to be cancelled
17// first. The destructive commands (those that mutate the transcript
18// or rebuild the agent) need a quiet state; the rest run alongside
19// the streaming response without trouble.
20func slashCancelsTurn(head string) bool {
21 switch head {
22 case "/clear", "/compact", "/logout", "/login", "/model":
23 return true
24 }
25 return false
26}
27
28// slashCatalog lists every slash command the interactive mode handles.
29// Keep in sync with runSlash().
30var slashCatalog = []slashCommand{
31 {"/help", "show key bindings and commands"},
32 {"/login", "log in via api key or subscription"},
33 {"/logout", "clear a provider's credentials"},
34 {"/model", "pick a model (or /model <id>)"},
35 {"/sessions", "resume a previous session for this directory"},
36 {"/jump", "scroll the chat to a previous turn (or /jump <text>)"},
37 {"/compact", "summarize and replace the transcript to free up context"},
38 {"/lock", "confine tools to the current directory"},
39 {"/unlock", "allow tools to touch paths outside this directory"},
40 {"/clear", "clear the chat transcript"},
41 {"/exit", "exit zot"},
42}
43
44// slashSuggester renders the popup that appears when the editor starts
45// with "/". It does not own any input state — the editor drives.
46type slashSuggester struct {
47 cursor int
48}
49
50// looksLikeSlashCommand reports whether text is an attempt at a slash
51// command (valid or not). Returns true for things like "/foo" or
52// "/bar baz" but false for paths ("/Users/pat/...") and regexes
53// ("/foo.bar/") so those can be sent to the model as-is.
54//
55// The head after "/" must be a single simple word: only letters,
56// digits, hyphens, and underscores. That excludes paths (contain "/"),
57// regexes (contain "."), and URLs.
58func looksLikeSlashCommand(text string) bool {
59 text = strings.TrimSpace(text)
60 if len(text) < 2 || text[0] != '/' {
61 return false
62 }
63 head := text[1:]
64 if i := strings.IndexAny(head, " \t\n"); i >= 0 {
65 head = head[:i]
66 }
67 if head == "" {
68 return false
69 }
70 for _, r := range head {
71 if !(r >= 'a' && r <= 'z') && !(r >= 'A' && r <= 'Z') &&
72 !(r >= '0' && r <= '9') && r != '-' && r != '_' {
73 return false
74 }
75 }
76 return true
77}
78
79// isKnownSlashCommand reports whether text's head matches a registered
80// slash command name in slashCatalog.
81func isKnownSlashCommand(text string) bool {
82 text = strings.TrimSpace(text)
83 if text == "" || text[0] != '/' {
84 return false
85 }
86 head := text
87 if i := strings.IndexAny(text, " \t\n"); i >= 0 {
88 head = text[:i]
89 }
90 for _, c := range slashCatalog {
91 if c.Name == head {
92 return true
93 }
94 }
95 return false
96}
97
98func newSlashSuggester() *slashSuggester { return &slashSuggester{} }
99
100// matches returns the commands whose name has input as a prefix.
101// If input is just "/", everything is shown.
102func (s *slashSuggester) matches(input string) []slashCommand {
103 input = strings.TrimRight(input, " ")
104 if input == "" || !strings.HasPrefix(input, "/") {
105 return nil
106 }
107 // If there is a space, the user has moved past the command name.
108 if idx := strings.IndexByte(input, ' '); idx >= 0 {
109 return nil
110 }
111 var out []slashCommand
112 for _, c := range slashCatalog {
113 if strings.HasPrefix(c.Name, input) {
114 out = append(out, c)
115 }
116 }
117 return out
118}
119
120// clampCursor keeps the cursor inside the current match list.
121func (s *slashSuggester) clampCursor(n int) {
122 if n <= 0 {
123 s.cursor = 0
124 return
125 }
126 if s.cursor < 0 {
127 s.cursor = 0
128 }
129 if s.cursor >= n {
130 s.cursor = n - 1
131 }
132}
133
134// Up / Down navigate the suggestion list.
135func (s *slashSuggester) Up() { s.cursor-- }
136func (s *slashSuggester) Down() { s.cursor++ }
137
138// Active reports whether the popup is visible for the given input.
139func (s *slashSuggester) Active(input string) bool {
140 return len(s.matches(input)) > 0
141}
142
143// Selection returns the currently highlighted command for input, or "".
144func (s *slashSuggester) Selection(input string) string {
145 m := s.matches(input)
146 if len(m) == 0 {
147 return ""
148 }
149 s.clampCursor(len(m))
150 return m[s.cursor].Name
151}
152
153// Render returns the popup lines or nil.
154func (s *slashSuggester) Render(input string, th tui.Theme, width int) []string {
155 m := s.matches(input)
156 if len(m) == 0 {
157 return nil
158 }
159 s.clampCursor(len(m))
160 var lines []string
161 for i, c := range m {
162 name := c.Name
163 if len(name) < 10 {
164 name = name + strings.Repeat(" ", 10-len(name))
165 }
166 plain := " " + name + " " + c.Desc
167 if i == s.cursor {
168 lines = append(lines, th.PadHighlight(plain, width))
169 } else {
170 lines = append(lines, th.FG256(th.Muted, plain))
171 }
172 }
173 lines = append(lines, th.FG256(th.Muted, " ↑/↓ navigate · tab complete · enter run"))
174 return lines
175}
176
177// Reset puts the cursor back to the first match. Call this whenever the
178// input changes in a way that reshapes the match list.
179func (s *slashSuggester) Reset() { s.cursor = 0 }