|
| 1 | +package commands |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + tea "github.com/charmbracelet/bubbletea/v2" |
| 7 | + |
| 8 | + "github.com/docker/cagent/pkg/app" |
| 9 | + "github.com/docker/cagent/pkg/tui/core" |
| 10 | +) |
| 11 | + |
| 12 | +// Session commands |
| 13 | +type ( |
| 14 | + NewSessionMsg struct{} |
| 15 | + EvalSessionMsg struct{} |
| 16 | + CompactSessionMsg struct{} |
| 17 | + CopySessionToClipboardMsg struct{} |
| 18 | +) |
| 19 | + |
| 20 | +// Agent commands |
| 21 | +type AgentCommandMsg struct { |
| 22 | + Command string |
| 23 | +} |
| 24 | + |
| 25 | +// CommandCategory represents a category of commands |
| 26 | +type Category struct { |
| 27 | + Name string |
| 28 | + Commands []Item |
| 29 | +} |
| 30 | + |
| 31 | +// Command represents a single command in the palette |
| 32 | +type Item struct { |
| 33 | + ID string |
| 34 | + Label string |
| 35 | + Description string |
| 36 | + Category string |
| 37 | + SlashCommand string |
| 38 | + Execute func() tea.Cmd |
| 39 | +} |
| 40 | + |
| 41 | +func BuiltInSessionCommands() []Item { |
| 42 | + return []Item{ |
| 43 | + { |
| 44 | + ID: "session.new", |
| 45 | + Label: "New", |
| 46 | + SlashCommand: "/new", |
| 47 | + Description: "Start a new conversation", |
| 48 | + Category: "Session", |
| 49 | + Execute: func() tea.Cmd { |
| 50 | + return core.CmdHandler(NewSessionMsg{}) |
| 51 | + }, |
| 52 | + }, |
| 53 | + { |
| 54 | + ID: "session.compact", |
| 55 | + Label: "Compact", |
| 56 | + SlashCommand: "/compact", |
| 57 | + Description: "Summarize the current conversation", |
| 58 | + Category: "Session", |
| 59 | + Execute: func() tea.Cmd { |
| 60 | + return core.CmdHandler(CompactSessionMsg{}) |
| 61 | + }, |
| 62 | + }, |
| 63 | + { |
| 64 | + ID: "session.clipboard", |
| 65 | + Label: "Copy", |
| 66 | + SlashCommand: "/copy", |
| 67 | + Description: "Copy the current conversation to the clipboard", |
| 68 | + Category: "Session", |
| 69 | + Execute: func() tea.Cmd { |
| 70 | + return core.CmdHandler(CopySessionToClipboardMsg{}) |
| 71 | + }, |
| 72 | + }, |
| 73 | + { |
| 74 | + ID: "session.eval", |
| 75 | + Label: "Eval", |
| 76 | + SlashCommand: "/eval", |
| 77 | + Description: "Create an evaluation report for the current conversation", |
| 78 | + Category: "Session", |
| 79 | + Execute: func() tea.Cmd { |
| 80 | + return core.CmdHandler(EvalSessionMsg{}) |
| 81 | + }, |
| 82 | + }, |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +// BuildCommandCategories builds the list of command categories for the command palette |
| 87 | +func BuildCommandCategories(ctx context.Context, application *app.App) []Category { |
| 88 | + categories := []Category{ |
| 89 | + { |
| 90 | + Name: "Session", |
| 91 | + Commands: BuiltInSessionCommands(), |
| 92 | + }, |
| 93 | + } |
| 94 | + |
| 95 | + agentCommands := application.CurrentAgentCommands(ctx) |
| 96 | + if len(agentCommands) == 0 { |
| 97 | + return categories |
| 98 | + } |
| 99 | + |
| 100 | + commands := make([]Item, 0, len(agentCommands)) |
| 101 | + for name, prompt := range agentCommands { |
| 102 | + |
| 103 | + // Truncate long descriptions to fit on one line |
| 104 | + description := prompt |
| 105 | + if len(description) > 60 { |
| 106 | + description = description[:57] + "..." |
| 107 | + } |
| 108 | + |
| 109 | + commands = append(commands, Item{ |
| 110 | + ID: "agent.command." + name, |
| 111 | + Label: name, |
| 112 | + Description: description, |
| 113 | + Category: "Agent Commands", |
| 114 | + Execute: func() tea.Cmd { |
| 115 | + return core.CmdHandler(AgentCommandMsg{Command: "/" + name}) |
| 116 | + }, |
| 117 | + }) |
| 118 | + } |
| 119 | + |
| 120 | + categories = append(categories, Category{ |
| 121 | + Name: "Agent Commands", |
| 122 | + Commands: commands, |
| 123 | + }) |
| 124 | + |
| 125 | + return categories |
| 126 | +} |
0 commit comments