1package dialog
2
3import (
4 "os"
5 "strings"
6
7 "charm.land/bubbles/v2/help"
8 "charm.land/bubbles/v2/key"
9 "charm.land/bubbles/v2/spinner"
10 "charm.land/bubbles/v2/textinput"
11 tea "charm.land/bubbletea/v2"
12 "github.com/charmbracelet/crush/internal/commands"
13 "github.com/charmbracelet/crush/internal/config"
14 "github.com/charmbracelet/crush/internal/ui/common"
15 "github.com/charmbracelet/crush/internal/ui/list"
16 "github.com/charmbracelet/crush/internal/ui/styles"
17 uv "github.com/charmbracelet/ultraviolet"
18)
19
20// CommandsID is the identifier for the commands dialog.
21const CommandsID = "commands"
22
23// CommandType represents the type of commands being displayed.
24type CommandType uint
25
26// String returns the string representation of the CommandType.
27func (c CommandType) String() string { return []string{"System", "User", "MCP"}[c] }
28
29const (
30 sidebarCompactModeBreakpoint = 120
31 defaultCommandsDialogMaxWidth = 70
32)
33
34const (
35 SystemCommands CommandType = iota
36 UserCommands
37 MCPPrompts
38)
39
40// Commands represents a dialog that shows available commands.
41type Commands struct {
42 com *common.Common
43 keyMap struct {
44 Select,
45 UpDown,
46 Next,
47 Previous,
48 Tab,
49 ShiftTab,
50 Close key.Binding
51 }
52
53 sessionID string // can be empty for non-session-specific commands
54 selected CommandType
55
56 spinner spinner.Model
57 loading bool
58
59 help help.Model
60 input textinput.Model
61 list *list.FilterableList
62
63 windowWidth int
64
65 customCommands []commands.CustomCommand
66 mcpPrompts []commands.MCPPrompt
67}
68
69var _ Dialog = (*Commands)(nil)
70
71// NewCommands creates a new commands dialog.
72func NewCommands(com *common.Common, sessionID string, customCommands []commands.CustomCommand, mcpPrompts []commands.MCPPrompt) (*Commands, error) {
73 c := &Commands{
74 com: com,
75 selected: SystemCommands,
76 sessionID: sessionID,
77 customCommands: customCommands,
78 mcpPrompts: mcpPrompts,
79 }
80
81 help := help.New()
82 help.Styles = com.Styles.DialogHelpStyles()
83
84 c.help = help
85
86 c.list = list.NewFilterableList()
87 c.list.Focus()
88 c.list.SetSelected(0)
89
90 c.input = textinput.New()
91 c.input.SetVirtualCursor(false)
92 c.input.Placeholder = "Type to filter"
93 c.input.SetStyles(com.Styles.TextInput)
94 c.input.Focus()
95
96 c.keyMap.Select = key.NewBinding(
97 key.WithKeys("enter", "ctrl+y"),
98 key.WithHelp("enter", "confirm"),
99 )
100 c.keyMap.UpDown = key.NewBinding(
101 key.WithKeys("up", "down"),
102 key.WithHelp("↑/↓", "choose"),
103 )
104 c.keyMap.Next = key.NewBinding(
105 key.WithKeys("down"),
106 key.WithHelp("↓", "next item"),
107 )
108 c.keyMap.Previous = key.NewBinding(
109 key.WithKeys("up", "ctrl+p"),
110 key.WithHelp("↑", "previous item"),
111 )
112 c.keyMap.Tab = key.NewBinding(
113 key.WithKeys("tab"),
114 key.WithHelp("tab", "switch selection"),
115 )
116 c.keyMap.ShiftTab = key.NewBinding(
117 key.WithKeys("shift+tab"),
118 key.WithHelp("shift+tab", "switch selection prev"),
119 )
120 closeKey := CloseKey
121 closeKey.SetHelp("esc", "cancel")
122 c.keyMap.Close = closeKey
123
124 // Set initial commands
125 c.setCommandItems(c.selected)
126
127 s := spinner.New()
128 s.Spinner = spinner.Dot
129 s.Style = com.Styles.Dialog.Spinner
130 c.spinner = s
131
132 return c, nil
133}
134
135// ID implements Dialog.
136func (c *Commands) ID() string {
137 return CommandsID
138}
139
140// HandleMsg implements [Dialog].
141func (c *Commands) HandleMsg(msg tea.Msg) Action {
142 switch msg := msg.(type) {
143 case spinner.TickMsg:
144 if c.loading {
145 var cmd tea.Cmd
146 c.spinner, cmd = c.spinner.Update(msg)
147 return ActionCmd{Cmd: cmd}
148 }
149 case tea.KeyPressMsg:
150 switch {
151 case key.Matches(msg, c.keyMap.Close):
152 return ActionClose{}
153 case key.Matches(msg, c.keyMap.Previous):
154 c.list.Focus()
155 if c.list.IsSelectedFirst() {
156 c.list.SelectLast()
157 c.list.ScrollToBottom()
158 break
159 }
160 c.list.SelectPrev()
161 c.list.ScrollToSelected()
162 case key.Matches(msg, c.keyMap.Next):
163 c.list.Focus()
164 if c.list.IsSelectedLast() {
165 c.list.SelectFirst()
166 c.list.ScrollToTop()
167 break
168 }
169 c.list.SelectNext()
170 c.list.ScrollToSelected()
171 case key.Matches(msg, c.keyMap.Select):
172 if selectedItem := c.list.SelectedItem(); selectedItem != nil {
173 if item, ok := selectedItem.(*CommandItem); ok && item != nil {
174 return item.Action()
175 }
176 }
177 case key.Matches(msg, c.keyMap.Tab):
178 if len(c.customCommands) > 0 || len(c.mcpPrompts) > 0 {
179 c.selected = c.nextCommandType()
180 c.setCommandItems(c.selected)
181 }
182 case key.Matches(msg, c.keyMap.ShiftTab):
183 if len(c.customCommands) > 0 || len(c.mcpPrompts) > 0 {
184 c.selected = c.previousCommandType()
185 c.setCommandItems(c.selected)
186 }
187 default:
188 var cmd tea.Cmd
189 for _, item := range c.list.FilteredItems() {
190 if item, ok := item.(*CommandItem); ok && item != nil {
191 if msg.String() == item.Shortcut() {
192 return item.Action()
193 }
194 }
195 }
196 c.input, cmd = c.input.Update(msg)
197 value := c.input.Value()
198 c.list.SetFilter(value)
199 c.list.ScrollToTop()
200 c.list.SetSelected(0)
201 return ActionCmd{cmd}
202 }
203 }
204 return nil
205}
206
207// Cursor returns the cursor position relative to the dialog.
208func (c *Commands) Cursor() *tea.Cursor {
209 return InputCursor(c.com.Styles, c.input.Cursor())
210}
211
212// commandsRadioView generates the command type selector radio buttons.
213func commandsRadioView(sty *styles.Styles, selected CommandType, hasUserCmds bool, hasMCPPrompts bool) string {
214 if !hasUserCmds && !hasMCPPrompts {
215 return ""
216 }
217
218 selectedFn := func(t CommandType) string {
219 if t == selected {
220 return sty.RadioOn.Padding(0, 1).Render() + sty.HalfMuted.Render(t.String())
221 }
222 return sty.RadioOff.Padding(0, 1).Render() + sty.HalfMuted.Render(t.String())
223 }
224
225 parts := []string{
226 selectedFn(SystemCommands),
227 }
228
229 if hasUserCmds {
230 parts = append(parts, selectedFn(UserCommands))
231 }
232 if hasMCPPrompts {
233 parts = append(parts, selectedFn(MCPPrompts))
234 }
235
236 return strings.Join(parts, " ")
237}
238
239// Draw implements [Dialog].
240func (c *Commands) Draw(scr uv.Screen, area uv.Rectangle) *tea.Cursor {
241 t := c.com.Styles
242 width := max(0, min(defaultCommandsDialogMaxWidth, area.Dx()))
243 height := max(0, min(defaultDialogHeight, area.Dy()))
244 if area.Dx() != c.windowWidth && c.selected == SystemCommands {
245 c.windowWidth = area.Dx()
246 // since some items in the list depend on width (e.g. toggle sidebar command),
247 // we need to reset the command items when width changes
248 c.setCommandItems(c.selected)
249 }
250
251 innerWidth := width - c.com.Styles.Dialog.View.GetHorizontalFrameSize()
252 heightOffset := t.Dialog.Title.GetVerticalFrameSize() + titleContentHeight +
253 t.Dialog.InputPrompt.GetVerticalFrameSize() + inputContentHeight +
254 t.Dialog.HelpView.GetVerticalFrameSize() +
255 t.Dialog.View.GetVerticalFrameSize()
256
257 c.input.SetWidth(max(0, innerWidth-t.Dialog.InputPrompt.GetHorizontalFrameSize()-1)) // (1) cursor padding
258
259 c.list.SetSize(innerWidth, height-heightOffset)
260 c.help.SetWidth(innerWidth)
261
262 rc := NewRenderContext(t, width)
263 rc.Title = "Commands"
264 rc.TitleInfo = commandsRadioView(t, c.selected, len(c.customCommands) > 0, len(c.mcpPrompts) > 0)
265 inputView := t.Dialog.InputPrompt.Render(c.input.View())
266 rc.AddPart(inputView)
267 listView := t.Dialog.List.Height(c.list.Height()).Render(c.list.Render())
268 rc.AddPart(listView)
269 rc.Help = c.help.View(c)
270
271 if c.loading {
272 rc.Help = c.spinner.View() + " Generating Prompt..."
273 }
274
275 view := rc.Render()
276
277 cur := c.Cursor()
278 DrawCenterCursor(scr, area, view, cur)
279 return cur
280}
281
282// ShortHelp implements [help.KeyMap].
283func (c *Commands) ShortHelp() []key.Binding {
284 return []key.Binding{
285 c.keyMap.Tab,
286 c.keyMap.UpDown,
287 c.keyMap.Select,
288 c.keyMap.Close,
289 }
290}
291
292// FullHelp implements [help.KeyMap].
293func (c *Commands) FullHelp() [][]key.Binding {
294 return [][]key.Binding{
295 {c.keyMap.Select, c.keyMap.Next, c.keyMap.Previous, c.keyMap.Tab},
296 {c.keyMap.Close},
297 }
298}
299
300// nextCommandType returns the next command type in the cycle.
301func (c *Commands) nextCommandType() CommandType {
302 switch c.selected {
303 case SystemCommands:
304 if len(c.customCommands) > 0 {
305 return UserCommands
306 }
307 if len(c.mcpPrompts) > 0 {
308 return MCPPrompts
309 }
310 fallthrough
311 case UserCommands:
312 if len(c.mcpPrompts) > 0 {
313 return MCPPrompts
314 }
315 fallthrough
316 case MCPPrompts:
317 return SystemCommands
318 default:
319 return SystemCommands
320 }
321}
322
323// previousCommandType returns the previous command type in the cycle.
324func (c *Commands) previousCommandType() CommandType {
325 switch c.selected {
326 case SystemCommands:
327 if len(c.mcpPrompts) > 0 {
328 return MCPPrompts
329 }
330 if len(c.customCommands) > 0 {
331 return UserCommands
332 }
333 return SystemCommands
334 case UserCommands:
335 return SystemCommands
336 case MCPPrompts:
337 if len(c.customCommands) > 0 {
338 return UserCommands
339 }
340 return SystemCommands
341 default:
342 return SystemCommands
343 }
344}
345
346// setCommandItems sets the command items based on the specified command type.
347func (c *Commands) setCommandItems(commandType CommandType) {
348 c.selected = commandType
349
350 commandItems := []list.FilterableItem{}
351 switch c.selected {
352 case SystemCommands:
353 for _, cmd := range c.defaultCommands() {
354 commandItems = append(commandItems, cmd)
355 }
356 case UserCommands:
357 for _, cmd := range c.customCommands {
358 action := ActionRunCustomCommand{
359 Content: cmd.Content,
360 Arguments: cmd.Arguments,
361 }
362 commandItems = append(commandItems, NewCommandItem(c.com.Styles, "custom_"+cmd.ID, cmd.Name, "", action))
363 }
364 case MCPPrompts:
365 for _, cmd := range c.mcpPrompts {
366 action := ActionRunMCPPrompt{
367 Title: cmd.Title,
368 Description: cmd.Description,
369 PromptID: cmd.PromptID,
370 ClientID: cmd.ClientID,
371 Arguments: cmd.Arguments,
372 }
373 commandItems = append(commandItems, NewCommandItem(c.com.Styles, "mcp_"+cmd.ID, cmd.PromptID, "", action))
374 }
375 }
376
377 c.list.SetItems(commandItems...)
378 c.list.SetFilter("")
379 c.list.ScrollToTop()
380 c.list.SetSelected(0)
381 c.input.SetValue("")
382}
383
384// defaultCommands returns the list of default system commands.
385func (c *Commands) defaultCommands() []*CommandItem {
386 commands := []*CommandItem{
387 NewCommandItem(c.com.Styles, "new_session", "New Session", "ctrl+n", ActionNewSession{}),
388 NewCommandItem(c.com.Styles, "switch_session", "Sessions", "ctrl+s", ActionOpenDialog{SessionsID}),
389 NewCommandItem(c.com.Styles, "switch_model", "Switch Model", "ctrl+l", ActionOpenDialog{ModelsID}),
390 }
391
392 // Only show compact command if there's an active session
393 if c.sessionID != "" {
394 commands = append(commands, NewCommandItem(c.com.Styles, "summarize", "Summarize Session", "", ActionSummarize{SessionID: c.sessionID}))
395 }
396
397 // Add reasoning toggle for models that support it
398 cfg := c.com.Config()
399 if agentCfg, ok := cfg.Agents[config.AgentCoder]; ok {
400 providerCfg := cfg.GetProviderForModel(agentCfg.Model)
401 model := cfg.GetModelByType(agentCfg.Model)
402 if providerCfg != nil && model != nil && model.CanReason {
403 selectedModel := cfg.Models[agentCfg.Model]
404
405 // Anthropic models: thinking toggle
406 if model.CanReason && len(model.ReasoningLevels) == 0 {
407 status := "Enable"
408 if selectedModel.Think {
409 status = "Disable"
410 }
411 commands = append(commands, NewCommandItem(c.com.Styles, "toggle_thinking", status+" Thinking Mode", "", ActionToggleThinking{}))
412 }
413
414 // OpenAI models: reasoning effort dialog
415 if len(model.ReasoningLevels) > 0 {
416 commands = append(commands, NewCommandItem(c.com.Styles, "select_reasoning_effort", "Select Reasoning Effort", "", ActionOpenDialog{
417 DialogID: ReasoningID,
418 }))
419 }
420 }
421 }
422 // Only show toggle compact mode command if window width is larger than compact breakpoint (120)
423 if c.windowWidth >= sidebarCompactModeBreakpoint && c.sessionID != "" {
424 commands = append(commands, NewCommandItem(c.com.Styles, "toggle_sidebar", "Toggle Sidebar", "", ActionToggleCompactMode{}))
425 }
426 if c.sessionID != "" {
427 cfg := c.com.Config()
428 agentCfg := cfg.Agents[config.AgentCoder]
429 model := cfg.GetModelByType(agentCfg.Model)
430 if model != nil && model.SupportsImages {
431 commands = append(commands, NewCommandItem(c.com.Styles, "file_picker", "Open File Picker", "ctrl+f", ActionOpenDialog{
432 // TODO: Pass in the file picker dialog id
433 }))
434 }
435 }
436
437 // Add external editor command if $EDITOR is available
438 // TODO: Use [tea.EnvMsg] to get environment variable instead of os.Getenv
439 if os.Getenv("EDITOR") != "" {
440 commands = append(commands, NewCommandItem(c.com.Styles, "open_external_editor", "Open External Editor", "ctrl+o", ActionExternalEditor{}))
441 }
442
443 return append(commands,
444 NewCommandItem(c.com.Styles, "toggle_yolo", "Toggle Yolo Mode", "", ActionToggleYoloMode{}),
445 NewCommandItem(c.com.Styles, "toggle_help", "Toggle Help", "ctrl+g", ActionToggleHelp{}),
446 NewCommandItem(c.com.Styles, "init", "Initialize Project", "", ActionInitializeProject{}),
447 NewCommandItem(c.com.Styles, "quit", "Quit", "ctrl+c", tea.QuitMsg{}),
448 )
449}
450
451// SetCustomCommands sets the custom commands and refreshes the view if user commands are currently displayed.
452func (c *Commands) SetCustomCommands(customCommands []commands.CustomCommand) {
453 c.customCommands = customCommands
454 if c.selected == UserCommands {
455 c.setCommandItems(c.selected)
456 }
457}
458
459// SetMCPPrompts sets the MCP prompts and refreshes the view if MCP prompts are currently displayed.
460func (c *Commands) SetMCPPrompts(mcpPrompts []commands.MCPPrompt) {
461 c.mcpPrompts = mcpPrompts
462 if c.selected == MCPPrompts {
463 c.setCommandItems(c.selected)
464 }
465}
466
467// StartLoading implements [LoadingDialog].
468func (a *Commands) StartLoading() tea.Cmd {
469 if a.loading {
470 return nil
471 }
472 a.loading = true
473 return a.spinner.Tick
474}
475
476// StopLoading implements [LoadingDialog].
477func (a *Commands) StopLoading() {
478 a.loading = false
479}