package tui import ( "albedo/cli/internal/daemon" "slices" "strings" "unicode/utf8" tea "charm.land/bubbletea/v2" "github.com/charmbracelet/x/ansi" ) type ChatCommand struct { Name string Description string } var CommonCommands = []ChatCommand{ {Name: "/a", Description: "browse sessions"}, {Name: "/t", Description: "show or hide thinking"}, {Name: "/v", Description: "show tool calls as cards with their code and output"}, {Name: "/status", Description: "show state, workspace and usage"}, {Name: "/q", Description: "quit"}, } var AppCommands = []ChatCommand{ {Name: "/link", Description: "inspect and confirm linked workspace groups"}, {Name: "/ttl", Description: "inspect provider cache policy"}, {Name: "/quota", Description: "inspect observed provider quota"}, {Name: "/usage", Description: "inspect what the provider calls used, today and by day"}, {Name: "/requests", Description: "inspect provider request diagnostics"}, {Name: "/raise-cap", Description: "raise or restore the model context cap"}, {Name: "/effort", Description: "choose reasoning effort"}, {Name: "/reload", Description: "apply selected configuration and refresh models"}, {Name: "/compact-at", Description: "show or set this session's compaction window"}, {Name: "/compact", Description: "compact durable context"}, {Name: "/kernel", Description: "upgrade or restart the session kernel"}, {Name: "/model", Description: "choose a model"}, {Name: "/context", Description: "inspect prepared context"}, {Name: "/login", Description: "add or select a model provider"}, {Name: "/new", Description: "start a new coding session"}, {Name: "/sessions", Description: "switch to another session"}, {Name: "/agents", Description: "watch and message this session's agents (ctrl+o)"}, {Name: "/extensions", Description: "manage this session's extension plugins"}, {Name: "/skills", Description: "manage loaded skills and per-skill defaults"}, {Name: "/instructions", Description: "manage AGENTS.md and other instruction files"}, {Name: "/mcp", Description: "manage MCP servers and authentication"}, {Name: "/webhooks", Description: "manage signed webhooks and agent access"}, {Name: "/discord", Description: "give sessions Discord bots: channels, DMs and invites"}, {Name: "/config", Description: "edit settings"}, {Name: "/tree", Description: "rewind this session to before a user message"}, {Name: "/fork", Description: "branch this session from a history checkpoint"}, {Name: "/btw", Description: "ask a side question without interrupting this session"}, {Name: "/cd", Description: "move this session to another folder or ssh host"}, } type CommandMenuModel struct { Dismissed menuDismissal Catalog []daemon.SessionCommand Selected int } // menuDismissal is the popup Escape closed: the draft, and the cursor when the // popup completes a $mention, since one draft can hold several. A slash // command completes the whole draft, so its dismissal ignores the cursor. type menuDismissal struct { input string cursor int } func dismissalAt(input string, cursor int) menuDismissal { if strings.HasPrefix(input, "/") { cursor = -1 } return menuDismissal{input: input, cursor: cursor} } func NewCommandMenuModel() CommandMenuModel { return CommandMenuModel{} } func (m CommandMenuModel) Matches(input string, cursor int) []ChatCommand { if cursor < 0 || m.Dismissed == dismissalAt(input, cursor) { return nil } if !strings.HasPrefix(input, "/") { mention, ok := skillMentionAt(input, cursor) if !ok { return nil } var matches []ChatCommand for _, command := range m.Catalog { if command.SkillName != "" && strings.HasPrefix(command.SkillName, mention.prefix) { matches = append(matches, ChatCommand{Name: "$" + command.SkillName, Description: command.Description}) } } return matches } if strings.Contains(input, "\n") { return nil } if name, rest, ok := strings.Cut(input, " "); ok { words := strings.Fields(rest) argumentIndex := len(words) partial := "" if rest != "" && !strings.HasSuffix(rest, " ") { argumentIndex-- partial = words[argumentIndex] } prefix := name + " " if partial != "" { prefix += strings.TrimSuffix(rest, partial) } seen := make(map[string]bool) var direct, others []ChatCommand for _, command := range m.Catalog { if command.Name != name || argumentIndex >= len(command.Arguments) { continue } argument := command.Arguments[argumentIndex] description := argument.Description if description == "" { description = argument.Name } for _, choice := range argument.Choices { if seen[choice] || !strings.HasPrefix(choice, partial) { continue } seen[choice] = true match := ChatCommand{Name: prefix + choice, Description: description} if match.Name == input { direct = append(direct, match) } else { others = append(others, match) } } } return append(direct, others...) } all := slices.Clone(AppCommands) for _, cmd := range m.Catalog { all = append(all, ChatCommand{ Name: cmd.Name, Description: cmd.Description, }) } all = append(all, CommonCommands...) // De-duplicate by name (last occurrence wins), placing exact match first. seen := make(map[string]bool, len(all)) var direct, others []ChatCommand for _, cmd := range slices.Backward(all) { if seen[cmd.Name] { continue } seen[cmd.Name] = true if cmd.Name == input { direct = append(direct, cmd) } else if strings.HasPrefix(cmd.Name, input) { others = append(others, cmd) } } slices.Reverse(others) return append(direct, others...) } // OnKey handles navigation within the command popup. Returns true if consumed. func (m *CommandMenuModel) OnKey(msg tea.KeyPressMsg, input string, cursor int, replace func(string, int), submit func(string)) bool { matches := m.Matches(input, cursor) if len(matches) == 0 { return false } idx := max(0, min(m.Selected, len(matches)-1)) // Outside a slash command, the matches complete the $mention at the cursor. if !strings.HasPrefix(input, "/") && (msg.String() == "tab" || msg.String() == "enter") { mention, _ := skillMentionAt(input, cursor) runes := []rune(input) if msg.String() == "enter" && string(runes[mention.start:mention.end]) == matches[idx].Name { return false } name := matches[idx].Name if mention.end == len(runes) { name += " " } replace(string(runes[:mention.start])+name+string(runes[mention.end:]), mention.start+utf8.RuneCountInString(name)) return true } switch msg.String() { case "esc": m.Dismissed = dismissalAt(input, cursor) case "up": if idx > 0 { m.Selected = idx - 1 } case "down": if idx < len(matches)-1 { m.Selected = idx + 1 } case "tab": replace(matches[idx].Name, utf8.RuneCountInString(matches[idx].Name)) case "enter": submit(matches[idx].Name) default: m.Selected = 0 return false } return true } func (m CommandMenuModel) View(input string, cursor, width int) string { matches := m.Matches(input, cursor) if len(matches) == 0 { return "" } idx := max(0, min(m.Selected, len(matches)-1)) start := max(0, idx-3) end := min(len(matches), max(4, idx+1)) var b strings.Builder for i := start; i < end; i++ { cmd := matches[i] desc := ansi.Truncate(cmd.Name+" "+DefaultStyles.Faint.Render(cmd.Description), max(0, width-2), "…") if i == idx { b.WriteString(selectedLine(selectBar()+" "+desc, 0)) } else { b.WriteString(" ") b.WriteString(desc) } b.WriteByte('\n') } return b.String() }