on main help sign in
Refactor Custom Command Arguments Dialog (#1869)
commitf2f63d1dfcd528a8fb64387f38f716cb0ca7378f authorKujtim Hoxha <kujtimii.h@gmail.com> dateThu 15 Jan 2026, 12:27 (9 months ago) parenta3a584c
8 files changed+684−97
internal/commands/commands.go+44−20 internal/ui/dialog/actions.go+11−13 internal/ui/dialog/api_key_input.go+4−4 internal/ui/dialog/arguments.go+399 internal/ui/dialog/commands.go+62−44 internal/ui/dialog/dialog.go+26−1 internal/ui/model/ui.go+118−15 internal/ui/styles/styles.go+20
internal/commands/commands.go+44−20
@@ -1,6 +1,7 @@
11package commands
22
33import (
4+ "context"
45 "io/fs"
56 "os"
67 "path/filepath"
@@ -19,18 +20,22 @@ const (
1920 projectCommandPrefix = "project:"
2021)
2122
22−// Argument represents a command argument with its name and required status.
23+// Argument represents a command argument with its metadata.
2324type Argument struct {
24− Name string
25− Required bool
25+ ID string
26+ Title string
27+ Description string
28+ Required bool
2629}
2730
28−// MCPCustomCommand represents a custom command loaded from an MCP server.
29−type MCPCustomCommand struct {
30− ID string
31− Name string
32− Client string
33− Arguments []Argument
31+// MCPPrompt represents a custom command loaded from an MCP server.
32+type MCPPrompt struct {
33+ ID string
34+ Title string
35+ Description string
36+ PromptID string
37+ ClientID string
38+ Arguments []Argument
3439}
3540
3641// CustomCommand represents a user-defined custom command loaded from markdown files.
@@ -52,22 +57,32 @@ func LoadCustomCommands(cfg *config.Config) ([]CustomCommand, error) {
5257 return loadAll(buildCommandSources(cfg))
5358}
5459
55−// LoadMCPCustomCommands loads custom commands from available MCP servers.
56−func LoadMCPCustomCommands() ([]MCPCustomCommand, error) {
57− var commands []MCPCustomCommand
60+// LoadMCPPrompts loads custom commands from available MCP servers.
61+func LoadMCPPrompts() ([]MCPPrompt, error) {
62+ var commands []MCPPrompt
5863 for mcpName, prompts := range mcp.Prompts() {
5964 for _, prompt := range prompts {
6065 key := mcpName + ":" + prompt.Name
6166 var args []Argument
6267 for _, arg := range prompt.Arguments {
63− args = append(args, Argument{Name: arg.Name, Required: arg.Required})
68+ title := arg.Title
69+ if title == "" {
70+ title = arg.Name
71+ }
72+ args = append(args, Argument{
73+ ID: arg.Name,
74+ Title: title,
75+ Description: arg.Description,
76+ Required: arg.Required,
77+ })
6478 }
65−
66− commands = append(commands, MCPCustomCommand{
67− ID: key,
68− Name: prompt.Name,
69− Client: mcpName,
70− Arguments: args,
79+ commands = append(commands, MCPPrompt{
80+ ID: key,
81+ Title: prompt.Title,
82+ Description: prompt.Description,
83+ PromptID: prompt.Name,
84+ ClientID: mcpName,
85+ Arguments: args,
7186 })
7287 }
7388 }
@@ -168,7 +183,7 @@ func extractArgNames(content string) []Argument {
168183 if !seen[arg] {
169184 seen[arg] = true
170185 // for normal custom commands, all args are required
171− args = append(args, Argument{Name: arg, Required: true})
186+ args = append(args, Argument{ID: arg, Title: arg, Required: true})
172187 }
173188 }
174189
@@ -211,3 +226,12 @@ func ensureDir(path string) error {
211226func isMarkdownFile(name string) bool {
212227 return strings.HasSuffix(strings.ToLower(name), ".md")
213228}
229+
230+func GetMCPPrompt(clientID, promptID string, args map[string]string) (string, error) {
231+ // TODO: we should pass the context down
232+ result, err := mcp.GetPromptMessages(context.Background(), clientID, promptID, args)
233+ if err != nil {
234+ return "", err
235+ }
236+ return strings.Join(result, " "), nil
237+}
internal/ui/dialog/actions.go+11−13
@@ -51,20 +51,18 @@ type (
5151 }
5252 // ActionRunCustomCommand is a message to run a custom command.
5353 ActionRunCustomCommand struct {
54− CommandID string
55− // Used when running a user-defined command
56− Content string
57− // Used when running a prompt from MCP
58− Client string
59− }
60− // ActionOpenCustomCommandArgumentsDialog is a message to open the custom command arguments dialog.
61− ActionOpenCustomCommandArgumentsDialog struct {
62− CommandID string
63− // Used when running a user-defined command
64− Content string
65− // Used when running a prompt from MCP
66− Client string
54+ Content string
6755 Arguments []commands.Argument
56+ Args map[string]string // Actual argument values
57+ }
58+ // ActionRunMCPPrompt is a message to run a custom command.
59+ ActionRunMCPPrompt struct {
60+ Title string
61+ Description string
62+ PromptID string
63+ ClientID string
64+ Arguments []commands.Argument
65+ Args map[string]string // Actual argument values
6866 }
6967)
7068
internal/ui/dialog/api_key_input.go+4−4
@@ -95,7 +95,7 @@ func (m *APIKeyInput) ID() string {
9595 return APIKeyInputID
9696}
9797
98−// Update implements tea.Model.
98+// HandleMsg implements [Dialog].
9999func (m *APIKeyInput) HandleMsg(msg tea.Msg) Action {
100100 switch msg := msg.(type) {
101101 case ActionChangeAPIKeyState:
@@ -149,7 +149,7 @@ func (m *APIKeyInput) HandleMsg(msg tea.Msg) Action {
149149 return nil
150150}
151151
152−// View implements tea.Model.
152+// Draw implements [Dialog].
153153func (m *APIKeyInput) Draw(scr uv.Screen, area uv.Rectangle) *tea.Cursor {
154154 t := m.com.Styles
155155
@@ -239,8 +239,8 @@ func (m *APIKeyInput) inputView() string {
239239}
240240
241241// Cursor returns the cursor position relative to the dialog.
242−func (c *APIKeyInput) Cursor() *tea.Cursor {
243− return InputCursor(c.com.Styles, c.input.Cursor())
242+func (m *APIKeyInput) Cursor() *tea.Cursor {
243+ return InputCursor(m.com.Styles, m.input.Cursor())
244244}
245245
246246// FullHelp returns the full help view.
internal/ui/dialog/arguments.gonew file+399
@@ -0,0 +1,399 @@
1+package dialog
2+
3+import (
4+ "strings"
5+
6+ "charm.land/bubbles/v2/help"
7+ "charm.land/bubbles/v2/key"
8+ "charm.land/bubbles/v2/spinner"
9+ "charm.land/bubbles/v2/textinput"
10+ "charm.land/bubbles/v2/viewport"
11+ tea "charm.land/bubbletea/v2"
12+ "charm.land/lipgloss/v2"
13+ "golang.org/x/text/cases"
14+ "golang.org/x/text/language"
15+
16+ "github.com/charmbracelet/crush/internal/commands"
17+ "github.com/charmbracelet/crush/internal/ui/common"
18+ "github.com/charmbracelet/crush/internal/uiutil"
19+ uv "github.com/charmbracelet/ultraviolet"
20+)
21+
22+// ArgumentsID is the identifier for the arguments dialog.
23+const ArgumentsID = "arguments"
24+
25+// Dialog sizing for arguments.
26+const (
27+ maxInputWidth = 120
28+ minInputWidth = 30
29+ maxViewportHeight = 20
30+ argumentsFieldHeight = 3 // label + input + spacing per field
31+)
32+
33+// Arguments represents a dialog for collecting command arguments.
34+type Arguments struct {
35+ com *common.Common
36+ title string
37+ arguments []commands.Argument
38+ inputs []textinput.Model
39+ focused int
40+ spinner spinner.Model
41+ loading bool
42+
43+ description string
44+ resultAction Action
45+
46+ help help.Model
47+ keyMap struct {
48+ Confirm,
49+ Next,
50+ Previous,
51+ ScrollUp,
52+ ScrollDown,
53+ Close key.Binding
54+ }
55+
56+ viewport viewport.Model
57+}
58+
59+var _ Dialog = (*Arguments)(nil)
60+
61+// NewArguments creates a new arguments dialog.
62+func NewArguments(com *common.Common, title, description string, arguments []commands.Argument, resultAction Action) *Arguments {
63+ a := &Arguments{
64+ com: com,
65+ title: title,
66+ description: description,
67+ arguments: arguments,
68+ resultAction: resultAction,
69+ }
70+
71+ a.help = help.New()
72+ a.help.Styles = com.Styles.DialogHelpStyles()
73+
74+ a.keyMap.Confirm = key.NewBinding(
75+ key.WithKeys("enter"),
76+ key.WithHelp("enter", "confirm"),
77+ )
78+ a.keyMap.Next = key.NewBinding(
79+ key.WithKeys("down", "tab"),
80+ key.WithHelp("↓/tab", "next"),
81+ )
82+ a.keyMap.Previous = key.NewBinding(
83+ key.WithKeys("up", "shift+tab"),
84+ key.WithHelp("↑/shift+tab", "previous"),
85+ )
86+ a.keyMap.Close = CloseKey
87+
88+ // Create input fields for each argument.
89+ a.inputs = make([]textinput.Model, len(arguments))
90+ for i, arg := range arguments {
91+ input := textinput.New()
92+ input.SetVirtualCursor(false)
93+ input.SetStyles(com.Styles.TextInput)
94+ input.Prompt = "> "
95+ // Use description as placeholder if available, otherwise title
96+ if arg.Description != "" {
97+ input.Placeholder = arg.Description
98+ } else {
99+ input.Placeholder = arg.Title
100+ }
101+
102+ if i == 0 {
103+ input.Focus()
104+ } else {
105+ input.Blur()
106+ }
107+
108+ a.inputs[i] = input
109+ }
110+ s := spinner.New()
111+ s.Spinner = spinner.Dot
112+ s.Style = com.Styles.Dialog.Spinner
113+ a.spinner = s
114+
115+ return a
116+}
117+
118+// ID implements Dialog.
119+func (a *Arguments) ID() string {
120+ return ArgumentsID
121+}
122+
123+// focusInput changes focus to a new input by index with wrap-around.
124+func (a *Arguments) focusInput(newIndex int) {
125+ a.inputs[a.focused].Blur()
126+
127+ // Wrap around: Go's modulo can return negative, so add len first.
128+ n := len(a.inputs)
129+ a.focused = ((newIndex % n) + n) % n
130+
131+ a.inputs[a.focused].Focus()
132+
133+ // Ensure the newly focused field is visible in the viewport
134+ a.ensureFieldVisible(a.focused)
135+}
136+
137+// isFieldVisible checks if a field at the given index is visible in the viewport.
138+func (a *Arguments) isFieldVisible(fieldIndex int) bool {
139+ fieldStart := fieldIndex * argumentsFieldHeight
140+ fieldEnd := fieldStart + argumentsFieldHeight - 1
141+ viewportTop := a.viewport.YOffset()
142+ viewportBottom := viewportTop + a.viewport.Height() - 1
143+
144+ return fieldStart >= viewportTop && fieldEnd <= viewportBottom
145+}
146+
147+// ensureFieldVisible scrolls the viewport to make the field visible.
148+func (a *Arguments) ensureFieldVisible(fieldIndex int) {
149+ if a.isFieldVisible(fieldIndex) {
150+ return
151+ }
152+
153+ fieldStart := fieldIndex * argumentsFieldHeight
154+ fieldEnd := fieldStart + argumentsFieldHeight - 1
155+ viewportTop := a.viewport.YOffset()
156+ viewportHeight := a.viewport.Height()
157+
158+ // If field is above viewport, scroll up to show it at top
159+ if fieldStart < viewportTop {
160+ a.viewport.SetYOffset(fieldStart)
161+ return
162+ }
163+
164+ // If field is below viewport, scroll down to show it at bottom
165+ if fieldEnd > viewportTop+viewportHeight-1 {
166+ a.viewport.SetYOffset(fieldEnd - viewportHeight + 1)
167+ }
168+}
169+
170+// findVisibleFieldByOffset returns the field index closest to the given viewport offset.
171+func (a *Arguments) findVisibleFieldByOffset(fromTop bool) int {
172+ offset := a.viewport.YOffset()
173+ if !fromTop {
174+ offset += a.viewport.Height() - 1
175+ }
176+
177+ fieldIndex := offset / argumentsFieldHeight
178+ if fieldIndex >= len(a.inputs) {
179+ return len(a.inputs) - 1
180+ }
181+ return fieldIndex
182+}
183+
184+// HandleMsg implements Dialog.
185+func (a *Arguments) HandleMsg(msg tea.Msg) Action {
186+ switch msg := msg.(type) {
187+ case spinner.TickMsg:
188+ if a.loading {
189+ var cmd tea.Cmd
190+ a.spinner, cmd = a.spinner.Update(msg)
191+ return ActionCmd{Cmd: cmd}
192+ }
193+ case tea.KeyPressMsg:
194+ switch {
195+ case key.Matches(msg, a.keyMap.Close):
196+ return ActionClose{}
197+ case key.Matches(msg, a.keyMap.Confirm):
198+ // If we're on the last input or there's only one input, submit.
199+ if a.focused == len(a.inputs)-1 || len(a.inputs) == 1 {
200+ args := make(map[string]string)
201+ var warning tea.Cmd
202+ for i, arg := range a.arguments {
203+ args[arg.ID] = a.inputs[i].Value()
204+ if arg.Required && strings.TrimSpace(a.inputs[i].Value()) == "" {
205+ warning = uiutil.ReportWarn("Required argument '" + arg.Title + "' is missing.")
206+ break
207+ }
208+ }
209+ if warning != nil {
210+ return ActionCmd{Cmd: warning}
211+ }
212+
213+ switch action := a.resultAction.(type) {
214+ case ActionRunCustomCommand:
215+ action.Args = args
216+ return action
217+ case ActionRunMCPPrompt:
218+ action.Args = args
219+ return action
220+ }
221+ }
222+ a.focusInput(a.focused + 1)
223+ case key.Matches(msg, a.keyMap.Next):
224+ a.focusInput(a.focused + 1)
225+ case key.Matches(msg, a.keyMap.Previous):
226+ a.focusInput(a.focused - 1)
227+ default:
228+ var cmd tea.Cmd
229+ a.inputs[a.focused], cmd = a.inputs[a.focused].Update(msg)
230+ return ActionCmd{Cmd: cmd}
231+ }
232+ case tea.MouseWheelMsg:
233+ a.viewport, _ = a.viewport.Update(msg)
234+ // If focused field scrolled out of view, focus the visible field
235+ if !a.isFieldVisible(a.focused) {
236+ a.focusInput(a.findVisibleFieldByOffset(msg.Button == tea.MouseWheelDown))
237+ }
238+ case tea.PasteMsg:
239+ var cmd tea.Cmd
240+ a.inputs[a.focused], cmd = a.inputs[a.focused].Update(msg)
241+ return ActionCmd{Cmd: cmd}
242+ }
243+ return nil
244+}
245+
246+// Cursor returns the cursor position relative to the dialog.
247+// we pass the description height to offset the cursor correctly.
248+func (a *Arguments) Cursor(descriptionHeight int) *tea.Cursor {
249+ cursor := InputCursor(a.com.Styles, a.inputs[a.focused].Cursor())
250+ if cursor == nil {
251+ return nil
252+ }
253+ cursor.Y += descriptionHeight + a.focused*argumentsFieldHeight - a.viewport.YOffset() + 1
254+ return cursor
255+}
256+
257+// Draw implements Dialog.
258+func (a *Arguments) Draw(scr uv.Screen, area uv.Rectangle) *tea.Cursor {
259+ s := a.com.Styles
260+
261+ dialogContentStyle := s.Dialog.Arguments.Content
262+ possibleWidth := area.Dx() - s.Dialog.View.GetHorizontalFrameSize() - dialogContentStyle.GetHorizontalFrameSize()
263+ // Build fields with label and input.
264+ caser := cases.Title(language.English)
265+
266+ var fields []string
267+ for i, arg := range a.arguments {
268+ isFocused := i == a.focused
269+
270+ // Try to pretty up the title for the label.
271+ title := strings.ReplaceAll(arg.Title, "_", " ")
272+ title = strings.ReplaceAll(title, "-", " ")
273+ titleParts := strings.Fields(title)
274+ for i, part := range titleParts {
275+ titleParts[i] = caser.String(strings.ToLower(part))
276+ }
277+ labelText := strings.Join(titleParts, " ")
278+
279+ markRequiredStyle := s.Dialog.Arguments.InputRequiredMarkBlurred
280+
281+ labelStyle := s.Dialog.Arguments.InputLabelBlurred
282+ if isFocused {
283+ labelStyle = s.Dialog.Arguments.InputLabelFocused
284+ markRequiredStyle = s.Dialog.Arguments.InputRequiredMarkFocused
285+ }
286+ if arg.Required {
287+ labelText += markRequiredStyle.String()
288+ }
289+ label := labelStyle.Render(labelText)
290+
291+ labelWidth := lipgloss.Width(labelText)
292+ placeholderWidth := lipgloss.Width(a.inputs[i].Placeholder)
293+
294+ inputWidth := max(placeholderWidth, labelWidth, minInputWidth)
295+ inputWidth = min(inputWidth, min(possibleWidth, maxInputWidth))
296+ a.inputs[i].SetWidth(inputWidth)
297+
298+ inputLine := a.inputs[i].View()
299+
300+ field := lipgloss.JoinVertical(lipgloss.Left, label, inputLine, "")
301+ fields = append(fields, field)
302+ }
303+
304+ renderedFields := lipgloss.JoinVertical(lipgloss.Left, fields...)
305+
306+ // Anchor width to the longest field, capped at maxInputWidth.
307+ const scrollbarWidth = 1
308+ width := lipgloss.Width(renderedFields)
309+ height := lipgloss.Height(renderedFields)
310+
311+ // Use standard header
312+ titleStyle := s.Dialog.Title
313+
314+ titleText := a.title
315+ if titleText == "" {
316+ titleText = "Arguments"
317+ }
318+
319+ header := common.DialogTitle(s, titleText, width)
320+
321+ // Add description if available.
322+ var description string
323+ if a.description != "" {
324+ descStyle := s.Dialog.Arguments.Description.Width(width)
325+ description = descStyle.Render(a.description)
326+ }
327+
328+ helpView := s.Dialog.HelpView.Width(width).Render(a.help.View(a))
329+ if a.loading {
330+ helpView = s.Dialog.HelpView.Width(width).Render(a.spinner.View() + " Generating Prompt...")
331+ }
332+
333+ availableHeight := area.Dy() - s.Dialog.View.GetVerticalFrameSize() - dialogContentStyle.GetVerticalFrameSize() - lipgloss.Height(header) - lipgloss.Height(description) - lipgloss.Height(helpView) - 2 // extra spacing
334+ viewportHeight := min(height, maxViewportHeight, availableHeight)
335+
336+ a.viewport.SetWidth(width) // -1 for scrollbar
337+ a.viewport.SetHeight(viewportHeight)
338+ a.viewport.SetContent(renderedFields)
339+
340+ scrollbar := common.Scrollbar(s, viewportHeight, a.viewport.TotalLineCount(), viewportHeight, a.viewport.YOffset())
341+ content := a.viewport.View()
342+ if scrollbar != "" {
343+ content = lipgloss.JoinHorizontal(lipgloss.Top, content, scrollbar)
344+ }
345+ contentParts := []string{}
346+ if description != "" {
347+ contentParts = append(contentParts, description)
348+ }
349+ contentParts = append(contentParts, content)
350+
351+ view := lipgloss.JoinVertical(
352+ lipgloss.Left,
353+ titleStyle.Render(header),
354+ dialogContentStyle.Render(lipgloss.JoinVertical(lipgloss.Left, contentParts...)),
355+ helpView,
356+ )
357+
358+ dialog := s.Dialog.View.Render(view)
359+
360+ descriptionHeight := 0
361+ if a.description != "" {
362+ descriptionHeight = lipgloss.Height(description)
363+ }
364+ cur := a.Cursor(descriptionHeight)
365+
366+ DrawCenterCursor(scr, area, dialog, cur)
367+ return cur
368+}
369+
370+// StartLoading implements [LoadingDialog].
371+func (a *Arguments) StartLoading() tea.Cmd {
372+ if a.loading {
373+ return nil
374+ }
375+ a.loading = true
376+ return a.spinner.Tick
377+}
378+
379+// StopLoading implements [LoadingDialog].
380+func (a *Arguments) StopLoading() {
381+ a.loading = false
382+}
383+
384+// ShortHelp implements help.KeyMap.
385+func (a *Arguments) ShortHelp() []key.Binding {
386+ return []key.Binding{
387+ a.keyMap.Confirm,
388+ a.keyMap.Next,
389+ a.keyMap.Close,
390+ }
391+}
392+
393+// FullHelp implements help.KeyMap.
394+func (a *Arguments) FullHelp() [][]key.Binding {
395+ return [][]key.Binding{
396+ {a.keyMap.Confirm, a.keyMap.Next, a.keyMap.Previous},
397+ {a.keyMap.Close},
398+ }
399+}
internal/ui/dialog/commands.go+62−44
@@ -6,6 +6,7 @@ import (
66
77 "charm.land/bubbles/v2/help"
88 "charm.land/bubbles/v2/key"
9+ "charm.land/bubbles/v2/spinner"
910 "charm.land/bubbles/v2/textinput"
1011 tea "charm.land/bubbletea/v2"
1112 "charm.land/lipgloss/v2"
@@ -52,26 +53,29 @@ type Commands struct {
5253 sessionID string // can be empty for non-session-specific commands
5354 selected CommandType
5455
56+ spinner spinner.Model
57+ loading bool
58+
5559 help help.Model
5660 input textinput.Model
5761 list *list.FilterableList
5862
5963 windowWidth int
6064
61− customCommands []commands.CustomCommand
62− mcpCustomCommands []commands.MCPCustomCommand
65+ customCommands []commands.CustomCommand
66+ mcpPrompts []commands.MCPPrompt
6367}
6468
6569var _ Dialog = (*Commands)(nil)
6670
6771// NewCommands creates a new commands dialog.
68−func NewCommands(com *common.Common, sessionID string, customCommands []commands.CustomCommand, mcpCustomCommands []commands.MCPCustomCommand) (*Commands, error) {
72+func NewCommands(com *common.Common, sessionID string, customCommands []commands.CustomCommand, mcpPrompts []commands.MCPPrompt) (*Commands, error) {
6973 c := &Commands{
70− com: com,
71− selected: SystemCommands,
72− sessionID: sessionID,
73− customCommands: customCommands,
74− mcpCustomCommands: mcpCustomCommands,
74+ com: com,
75+ selected: SystemCommands,
76+ sessionID: sessionID,
77+ customCommands: customCommands,
78+ mcpPrompts: mcpPrompts,
7579 }
7680
7781 help := help.New()
@@ -120,6 +124,11 @@ func NewCommands(com *common.Common, sessionID string, customCommands []commands
120124 // Set initial commands
121125 c.setCommandItems(c.selected)
122126
127+ s := spinner.New()
128+ s.Spinner = spinner.Dot
129+ s.Style = com.Styles.Dialog.Spinner
130+ c.spinner = s
131+
123132 return c, nil
124133}
125134
@@ -128,9 +137,15 @@ func (c *Commands) ID() string {
128137 return CommandsID
129138}
130139
131−// HandleMsg implements Dialog.
140+// HandleMsg implements [Dialog].
132141func (c *Commands) HandleMsg(msg tea.Msg) Action {
133142 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+ }
134149 case tea.KeyPressMsg:
135150 switch {
136151 case key.Matches(msg, c.keyMap.Close):
@@ -160,12 +175,12 @@ func (c *Commands) HandleMsg(msg tea.Msg) Action {
160175 }
161176 }
162177 case key.Matches(msg, c.keyMap.Tab):
163− if len(c.customCommands) > 0 || len(c.mcpCustomCommands) > 0 {
178+ if len(c.customCommands) > 0 || len(c.mcpPrompts) > 0 {
164179 c.selected = c.nextCommandType()
165180 c.setCommandItems(c.selected)
166181 }
167182 case key.Matches(msg, c.keyMap.ShiftTab):
168− if len(c.customCommands) > 0 || len(c.mcpCustomCommands) > 0 {
183+ if len(c.customCommands) > 0 || len(c.mcpPrompts) > 0 {
169184 c.selected = c.previousCommandType()
170185 c.setCommandItems(c.selected)
171186 }
@@ -242,12 +257,16 @@ func (c *Commands) Draw(scr uv.Screen, area uv.Rectangle) *tea.Cursor {
242257 c.list.SetSize(innerWidth, height-heightOffset)
243258 c.help.SetWidth(innerWidth)
244259
245− radio := commandsRadioView(t, c.selected, len(c.customCommands) > 0, len(c.mcpCustomCommands) > 0)
260+ radio := commandsRadioView(t, c.selected, len(c.customCommands) > 0, len(c.mcpPrompts) > 0)
246261 titleStyle := t.Dialog.Title
247262 dialogStyle := t.Dialog.View.Width(width)
248263 headerOffset := lipgloss.Width(radio) + titleStyle.GetHorizontalFrameSize() + dialogStyle.GetHorizontalFrameSize()
249264 helpView := ansi.Truncate(c.help.View(c), innerWidth, "")
250265 header := common.DialogTitle(t, "Commands", width-headerOffset) + radio
266+
267+ if c.loading {
268+ helpView = t.Dialog.HelpView.Width(width).Render(c.spinner.View() + " Generating Prompt...")
269+ }
251270 view := HeaderInputListHelpView(t, width, c.list.Height(), header,
252271 c.input.View(), c.list.Render(), helpView)
253272
@@ -281,12 +300,12 @@ func (c *Commands) nextCommandType() CommandType {
281300 if len(c.customCommands) > 0 {
282301 return UserCommands
283302 }
284− if len(c.mcpCustomCommands) > 0 {
303+ if len(c.mcpPrompts) > 0 {
285304 return MCPPrompts
286305 }
287306 fallthrough
288307 case UserCommands:
289− if len(c.mcpCustomCommands) > 0 {
308+ if len(c.mcpPrompts) > 0 {
290309 return MCPPrompts
291310 }
292311 fallthrough
@@ -301,7 +320,7 @@ func (c *Commands) nextCommandType() CommandType {
301320func (c *Commands) previousCommandType() CommandType {
302321 switch c.selected {
303322 case SystemCommands:
304− if len(c.mcpCustomCommands) > 0 {
323+ if len(c.mcpPrompts) > 0 {
305324 return MCPPrompts
306325 }
307326 if len(c.customCommands) > 0 {
@@ -332,37 +351,22 @@ func (c *Commands) setCommandItems(commandType CommandType) {
332351 }
333352 case UserCommands:
334353 for _, cmd := range c.customCommands {
335− var action Action
336− if len(cmd.Arguments) > 0 {
337− action = ActionOpenCustomCommandArgumentsDialog{
338− CommandID: cmd.ID,
339− Content: cmd.Content,
340− Arguments: cmd.Arguments,
341− }
342− } else {
343− action = ActionRunCustomCommand{
344− CommandID: cmd.ID,
345− Content: cmd.Content,
346− }
354+ action := ActionRunCustomCommand{
355+ Content: cmd.Content,
356+ Arguments: cmd.Arguments,
347357 }
348358 commandItems = append(commandItems, NewCommandItem(c.com.Styles, "custom_"+cmd.ID, cmd.Name, "", action))
349359 }
350360 case MCPPrompts:
351− for _, cmd := range c.mcpCustomCommands {
352− var action Action
353− if len(cmd.Arguments) > 0 {
354− action = ActionOpenCustomCommandArgumentsDialog{
355− CommandID: cmd.ID,
356− Client: cmd.Client,
357− Arguments: cmd.Arguments,
358− }
359− } else {
360− action = ActionRunCustomCommand{
361− CommandID: cmd.ID,
362− Client: cmd.Client,
363− }
361+ for _, cmd := range c.mcpPrompts {
362+ action := ActionRunMCPPrompt{
363+ Title: cmd.Title,
364+ Description: cmd.Description,
365+ PromptID: cmd.PromptID,
366+ ClientID: cmd.ClientID,
367+ Arguments: cmd.Arguments,
364368 }
365− commandItems = append(commandItems, NewCommandItem(c.com.Styles, "mcp_"+cmd.ID, cmd.Name, "", action))
369+ commandItems = append(commandItems, NewCommandItem(c.com.Styles, "mcp_"+cmd.ID, cmd.PromptID, "", action))
366370 }
367371 }
368372
@@ -448,10 +452,24 @@ func (c *Commands) SetCustomCommands(customCommands []commands.CustomCommand) {
448452 }
449453}
450454
451−// SetMCPCustomCommands sets the MCP custom commands and refreshes the view if MCP prompts are currently displayed.
452−func (c *Commands) SetMCPCustomCommands(mcpCustomCommands []commands.MCPCustomCommand) {
453− c.mcpCustomCommands = mcpCustomCommands
455+// SetMCPPrompts sets the MCP prompts and refreshes the view if MCP prompts are currently displayed.
456+func (c *Commands) SetMCPPrompts(mcpPrompts []commands.MCPPrompt) {
457+ c.mcpPrompts = mcpPrompts
454458 if c.selected == MCPPrompts {
455459 c.setCommandItems(c.selected)
456460 }
457461}
462+
463+// StartLoading implements [LoadingDialog].
464+func (a *Commands) StartLoading() tea.Cmd {
465+ if a.loading {
466+ return nil
467+ }
468+ a.loading = true
469+ return a.spinner.Tick
470+}
471+
472+// StopLoading implements [LoadingDialog].
473+func (a *Commands) StopLoading() {
474+ a.loading = false
475+}
internal/ui/dialog/dialog.go+26−1
@@ -27,7 +27,7 @@ var CloseKey = key.NewBinding(
2727)
2828
2929// Action represents an action taken in a dialog after handling a message.
30−type Action interface{}
30+type Action any
3131
3232// Dialog is a component that can be displayed on top of the UI.
3333type Dialog interface {
@@ -41,6 +41,12 @@ type Dialog interface {
4141 Draw(scr uv.Screen, area uv.Rectangle) *tea.Cursor
4242}
4343
44+// LoadingDialog is a dialog that can show a loading state.
45+type LoadingDialog interface {
46+ StartLoading() tea.Cmd
47+ StopLoading()
48+}
49+
4450// Overlay manages multiple dialogs as an overlay.
4551type Overlay struct {
4652 dialogs []Dialog
@@ -136,6 +142,25 @@ func (d *Overlay) Update(msg tea.Msg) tea.Msg {
136142 return dialog.HandleMsg(msg)
137143}
138144
145+// StartLoading starts the loading state for the front dialog if it
146+// implements [LoadingDialog].
147+func (d *Overlay) StartLoading() tea.Cmd {
148+ dialog := d.DialogLast()
149+ if ld, ok := dialog.(LoadingDialog); ok {
150+ return ld.StartLoading()
151+ }
152+ return nil
153+}
154+
155+// StopLoading stops the loading state for the front dialog if it
156+// implements [LoadingDialog].
157+func (d *Overlay) StopLoading() {
158+ dialog := d.DialogLast()
159+ if ld, ok := dialog.(LoadingDialog); ok {
160+ ld.StopLoading()
161+ }
162+}
163+
139164// DrawCenterCursor draws the given string view centered in the screen area and
140165// adjusts the cursor position accordingly.
141166func DrawCenterCursor(scr uv.Screen, area uv.Rectangle, view string, cur *tea.Cursor) {
internal/ui/model/ui.go+118−15
@@ -18,6 +18,7 @@ import (
1818
1919 "charm.land/bubbles/v2/help"
2020 "charm.land/bubbles/v2/key"
21+ "charm.land/bubbles/v2/spinner"
2122 "charm.land/bubbles/v2/textarea"
2223 tea "charm.land/bubbletea/v2"
2324 "charm.land/lipgloss/v2"
@@ -93,10 +94,19 @@ type (
9394 userCommandsLoadedMsg struct {
9495 Commands []commands.CustomCommand
9596 }
96− // mcpCustomCommandsLoadedMsg is sent when mcp prompts are loaded.
97− mcpCustomCommandsLoadedMsg struct {
98− Prompts []commands.MCPCustomCommand
97+ // mcpPromptsLoadedMsg is sent when mcp prompts are loaded.
98+ mcpPromptsLoadedMsg struct {
99+ Prompts []commands.MCPPrompt
99100 }
101+ // sendMessageMsg is sent to send a message.
102+ // currently only used for mcp prompts.
103+ sendMessageMsg struct {
104+ Content string
105+ Attachments []message.Attachment
106+ }
107+
108+ // closeDialogMsg is sent to close the current dialog.
109+ closeDialogMsg struct{}
100110)
101111
102112// UI represents the main user interface model.
@@ -167,8 +177,8 @@ type UI struct {
167177 sidebarLogo string
168178
169179 // custom commands & mcp commands
170− customCommands []commands.CustomCommand
171− mcpCustomCommands []commands.MCPCustomCommand
180+ customCommands []commands.CustomCommand
181+ mcpPrompts []commands.MCPPrompt
172182
173183 // forceCompactMode tracks whether compact mode is forced by user toggle
174184 forceCompactMode bool
@@ -282,15 +292,15 @@ func (m *UI) loadCustomCommands() tea.Cmd {
282292// loadMCPrompts loads the MCP prompts asynchronously.
283293func (m *UI) loadMCPrompts() tea.Cmd {
284294 return func() tea.Msg {
285− prompts, err := commands.LoadMCPCustomCommands()
295+ prompts, err := commands.LoadMCPPrompts()
286296 if err != nil {
287297 slog.Error("failed to load mcp prompts", "error", err)
288298 }
289299 if prompts == nil {
290300 // flag them as loaded even if there is none or an error
291− prompts = []commands.MCPCustomCommand{}
301+ prompts = []commands.MCPPrompt{}
292302 }
293− return mcpCustomCommandsLoadedMsg{Prompts: prompts}
303+ return mcpPromptsLoadedMsg{Prompts: prompts}
294304 }
295305}
296306
@@ -319,6 +329,9 @@ func (m *UI) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
319329 cmds = append(cmds, cmd)
320330 }
321331
332+ case sendMessageMsg:
333+ cmds = append(cmds, m.sendMessage(msg.Content, msg.Attachments...))
334+
322335 case userCommandsLoadedMsg:
323336 m.customCommands = msg.Commands
324337 dia := m.dialog.Dialog(dialog.CommandsID)
@@ -330,8 +343,8 @@ func (m *UI) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
330343 if ok {
331344 commands.SetCustomCommands(m.customCommands)
332345 }
333− case mcpCustomCommandsLoadedMsg:
334− m.mcpCustomCommands = msg.Prompts
346+ case mcpPromptsLoadedMsg:
347+ m.mcpPrompts = msg.Prompts
335348 dia := m.dialog.Dialog(dialog.CommandsID)
336349 if dia == nil {
337350 break
@@ -339,9 +352,12 @@ func (m *UI) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
339352
340353 commands, ok := dia.(*dialog.Commands)
341354 if ok {
342− commands.SetMCPCustomCommands(m.mcpCustomCommands)
355+ commands.SetMCPPrompts(m.mcpPrompts)
343356 }
344357
358+ case closeDialogMsg:
359+ m.dialog.CloseFrontDialog()
360+
345361 case pubsub.Event[message.Message]:
346362 // Check if this is a child session message for an agent tool.
347363 if m.session == nil {
@@ -374,7 +390,7 @@ func (m *UI) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
374390 break
375391 }
376392 }
377− if initialized && m.mcpCustomCommands == nil {
393+ if initialized && m.mcpPrompts == nil {
378394 cmds = append(cmds, m.loadMCPrompts())
379395 }
380396 case pubsub.Event[permission.PermissionRequest]:
@@ -492,6 +508,14 @@ func (m *UI) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
492508 cmds = append(cmds, cmd)
493509 }
494510 }
511+ case spinner.TickMsg:
512+ if m.dialog.HasDialogs() {
513+ // route to dialog
514+ if cmd := m.handleDialogMsg(msg); cmd != nil {
515+ cmds = append(cmds, cmd)
516+ }
517+ }
518+
495519 case tea.KeyPressMsg:
496520 if cmd := m.handleKeyPressMsg(msg); cmd != nil {
497521 cmds = append(cmds, cmd)
@@ -645,6 +669,11 @@ func (m *UI) loadNestedToolCalls(items []chat.MessageItem) {
645669// if the message is a tool result it will update the corresponding tool call message
646670func (m *UI) appendSessionMessage(msg message.Message) tea.Cmd {
647671 var cmds []tea.Cmd
672+ existing := m.chat.MessageItem(msg.ID)
673+ if existing != nil {
674+ // message already exists, skip
675+ return nil
676+ }
648677 switch msg.Role {
649678 case message.User, message.Assistant:
650679 items := chat.ExtractMessageItems(m.com.Styles, &msg, nil)
@@ -920,6 +949,44 @@ func (m *UI) handleDialogMsg(msg tea.Msg) tea.Cmd {
920949 case dialog.PermissionDeny:
921950 m.com.App.Permissions.Deny(msg.Permission)
922951 }
952+
953+ case dialog.ActionRunCustomCommand:
954+ if len(msg.Arguments) > 0 && msg.Args == nil {
955+ m.dialog.CloseFrontDialog()
956+ argsDialog := dialog.NewArguments(
957+ m.com,
958+ "Custom Command Arguments",
959+ "",
960+ msg.Arguments,
961+ msg, // Pass the action as the result
962+ )
963+ m.dialog.OpenDialog(argsDialog)
964+ break
965+ }
966+ content := msg.Content
967+ if msg.Args != nil {
968+ content = substituteArgs(content, msg.Args)
969+ }
970+ cmds = append(cmds, m.sendMessage(content))
971+ m.dialog.CloseFrontDialog()
972+ case dialog.ActionRunMCPPrompt:
973+ if len(msg.Arguments) > 0 && msg.Args == nil {
974+ m.dialog.CloseFrontDialog()
975+ title := msg.Title
976+ if title == "" {
977+ title = "MCP Prompt Arguments"
978+ }
979+ argsDialog := dialog.NewArguments(
980+ m.com,
981+ title,
982+ msg.Description,
983+ msg.Arguments,
984+ msg, // Pass the action as the result
985+ )
986+ m.dialog.OpenDialog(argsDialog)
987+ break
988+ }
989+ cmds = append(cmds, m.runMCPPrompt(msg.ClientID, msg.PromptID, msg.Args))
923990 default:
924991 cmds = append(cmds, uiutil.CmdHandler(msg))
925992 }
@@ -927,6 +994,15 @@ func (m *UI) handleDialogMsg(msg tea.Msg) tea.Cmd {
927994 return tea.Batch(cmds...)
928995}
929996
997+// substituteArgs replaces $ARG_NAME placeholders in content with actual values.
998+func substituteArgs(content string, args map[string]string) string {
999+ for name, value := range args {
1000+ placeholder := "$" + name
1001+ content = strings.ReplaceAll(content, placeholder, value)
1002+ }
1003+ return content
1004+}
1005+
9301006// openAPIKeyInputDialog opens the API key input dialog.
9311007func (m *UI) openAPIKeyInputDialog(provider catwalk.Provider, model config.SelectedModel, modelType config.SelectedModelType) tea.Cmd {
9321008 if m.dialog.ContainsDialog(dialog.APIKeyInputID) {
@@ -1055,7 +1131,7 @@ func (m *UI) handleKeyPressMsg(msg tea.KeyPressMsg) tea.Cmd {
10551131
10561132 m.randomizePlaceholders()
10571133
1058− return m.sendMessage(value, attachments)
1134+ return m.sendMessage(value, attachments...)
10591135 case key.Matches(msg, m.keyMap.Chat.NewSession):
10601136 if m.session == nil || m.session.ID == "" {
10611137 break
@@ -2013,7 +2089,7 @@ func (m *UI) renderSidebarLogo(width int) {
20132089}
20142090
20152091// sendMessage sends a message with the given content and attachments.
2016−func (m *UI) sendMessage(content string, attachments []message.Attachment) tea.Cmd {
2092+func (m *UI) sendMessage(content string, attachments ...message.Attachment) tea.Cmd {
20172093 if m.com.App.AgentCoordinator == nil {
20182094 return uiutil.ReportError(fmt.Errorf("coder agent is not initialized"))
20192095 }
@@ -2165,7 +2241,7 @@ func (m *UI) openCommandsDialog() tea.Cmd {
21652241 sessionID = m.session.ID
21662242 }
21672243
2168− commands, err := dialog.NewCommands(m.com, sessionID, m.customCommands, m.mcpCustomCommands)
2244+ commands, err := dialog.NewCommands(m.com, sessionID, m.customCommands, m.mcpPrompts)
21692245 if err != nil {
21702246 return uiutil.ReportError(err)
21712247 }
@@ -2393,6 +2469,33 @@ func (m *UI) drawSessionDetails(scr uv.Screen, area uv.Rectangle) {
23932469 ).Draw(scr, area)
23942470}
23952471
2472+func (m *UI) runMCPPrompt(clientID, promptID string, arguments map[string]string) tea.Cmd {
2473+ load := func() tea.Msg {
2474+ prompt, err := commands.GetMCPPrompt(clientID, promptID, arguments)
2475+ if err != nil {
2476+ // TODO: make this better
2477+ return uiutil.ReportError(err)()
2478+ }
2479+
2480+ if prompt == "" {
2481+ return nil
2482+ }
2483+ return sendMessageMsg{
2484+ Content: prompt,
2485+ }
2486+ }
2487+
2488+ var cmds []tea.Cmd
2489+ if cmd := m.dialog.StartLoading(); cmd != nil {
2490+ cmds = append(cmds, cmd)
2491+ }
2492+ cmds = append(cmds, load, func() tea.Msg {
2493+ return closeDialogMsg{}
2494+ })
2495+
2496+ return tea.Sequence(cmds...)
2497+}
2498+
23962499// renderLogo renders the Crush logo with the given styles and dimensions.
23972500func renderLogo(t *styles.Styles, compact bool, width int) string {
23982501 return logo.Render(version.Version, compact, logo.Opts{
internal/ui/styles/styles.go+20
@@ -333,6 +333,8 @@ type Styles struct {
333333
334334 List lipgloss.Style
335335
336+ Spinner lipgloss.Style
337+
336338 // ContentPanel is used for content blocks with subtle background.
337339 ContentPanel lipgloss.Style
338340
@@ -340,6 +342,16 @@ type Styles struct {
340342 ScrollbarThumb lipgloss.Style
341343 ScrollbarTrack lipgloss.Style
342344
345+ // Arguments
346+ Arguments struct {
347+ Content lipgloss.Style
348+ Description lipgloss.Style
349+ InputLabelBlurred lipgloss.Style
350+ InputLabelFocused lipgloss.Style
351+ InputRequiredMarkBlurred lipgloss.Style
352+ InputRequiredMarkFocused lipgloss.Style
353+ }
354+
343355 Commands struct{}
344356 }
345357
@@ -1205,9 +1217,17 @@ func DefaultStyles() Styles {
12051217
12061218 s.Dialog.List = base.Margin(0, 0, 1, 0)
12071219 s.Dialog.ContentPanel = base.Background(bgSubtle).Foreground(fgBase).Padding(1, 2)
1220+ s.Dialog.Spinner = base.Foreground(secondary)
12081221 s.Dialog.ScrollbarThumb = base.Foreground(secondary)
12091222 s.Dialog.ScrollbarTrack = base.Foreground(border)
12101223
1224+ s.Dialog.Arguments.Content = base.Padding(1)
1225+ s.Dialog.Arguments.Description = base.MarginBottom(1).MaxHeight(3)
1226+ s.Dialog.Arguments.InputLabelBlurred = base.Foreground(fgMuted)
1227+ s.Dialog.Arguments.InputLabelFocused = base.Bold(true)
1228+ s.Dialog.Arguments.InputRequiredMarkBlurred = base.Foreground(fgMuted).SetString("*")
1229+ s.Dialog.Arguments.InputRequiredMarkFocused = base.Foreground(primary).Bold(true).SetString("*")
1230+
12111231 s.Status.Help = lipgloss.NewStyle().Padding(0, 1)
12121232 s.Status.SuccessIndicator = base.Foreground(bgSubtle).Background(green).Padding(0, 1).Bold(true).SetString("OKAY!")
12131233 s.Status.InfoIndicator = s.Status.SuccessIndicator