commands.go

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