|
| 1 | +package dialog |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + |
| 7 | + "github.com/charmbracelet/bubbles/v2/key" |
| 8 | + tea "github.com/charmbracelet/bubbletea/v2" |
| 9 | + "github.com/charmbracelet/lipgloss/v2" |
| 10 | + |
| 11 | + "github.com/docker/cagent/pkg/app" |
| 12 | + "github.com/docker/cagent/pkg/tui/core" |
| 13 | +) |
| 14 | + |
| 15 | +// maxIterationsDialog implements DialogModel for max iterations confirmation |
| 16 | +type maxIterationsDialog struct { |
| 17 | + width, height int |
| 18 | + maxIterations int |
| 19 | + app *app.App |
| 20 | + keyMap maxIterationsKeyMap |
| 21 | +} |
| 22 | + |
| 23 | +// SetSize implements Dialog. |
| 24 | +func (d *maxIterationsDialog) SetSize(width, height int) tea.Cmd { |
| 25 | + d.width = width |
| 26 | + d.height = height |
| 27 | + return nil |
| 28 | +} |
| 29 | + |
| 30 | +// maxIterationsKeyMap defines key bindings for max iterations confirmation dialog |
| 31 | +type maxIterationsKeyMap struct { |
| 32 | + Yes key.Binding |
| 33 | + No key.Binding |
| 34 | +} |
| 35 | + |
| 36 | +// defaultMaxIterationsKeyMap returns default key bindings |
| 37 | +func defaultMaxIterationsKeyMap() maxIterationsKeyMap { |
| 38 | + return maxIterationsKeyMap{ |
| 39 | + Yes: key.NewBinding( |
| 40 | + key.WithKeys("y", "Y"), |
| 41 | + key.WithHelp("Y", "continue"), |
| 42 | + ), |
| 43 | + No: key.NewBinding( |
| 44 | + key.WithKeys("n", "N"), |
| 45 | + key.WithHelp("N", "stop"), |
| 46 | + ), |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +// NewMaxIterationsDialog creates a new max iterations confirmation dialog |
| 51 | +func NewMaxIterationsDialog(maxIterations int, appInstance *app.App) Dialog { |
| 52 | + return &maxIterationsDialog{ |
| 53 | + maxIterations: maxIterations, |
| 54 | + app: appInstance, |
| 55 | + keyMap: defaultMaxIterationsKeyMap(), |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +// Init initializes the max iterations confirmation dialog |
| 60 | +func (d *maxIterationsDialog) Init() tea.Cmd { |
| 61 | + return nil |
| 62 | +} |
| 63 | + |
| 64 | +// Update handles messages for the max iterations confirmation dialog |
| 65 | +func (d *maxIterationsDialog) Update(msg tea.Msg) (tea.Model, tea.Cmd) { |
| 66 | + switch msg := msg.(type) { |
| 67 | + case tea.WindowSizeMsg: |
| 68 | + d.width = msg.Width |
| 69 | + d.height = msg.Height |
| 70 | + return d, nil |
| 71 | + |
| 72 | + case tea.KeyPressMsg: |
| 73 | + switch { |
| 74 | + case key.Matches(msg, d.keyMap.Yes): |
| 75 | + if d.app != nil { |
| 76 | + d.app.Resume("approve") |
| 77 | + } |
| 78 | + return d, core.CmdHandler(CloseDialogMsg{}) |
| 79 | + case key.Matches(msg, d.keyMap.No): |
| 80 | + if d.app != nil { |
| 81 | + d.app.Resume("reject") |
| 82 | + } |
| 83 | + return d, core.CmdHandler(CloseDialogMsg{}) |
| 84 | + } |
| 85 | + |
| 86 | + if msg.String() == "ctrl+c" { |
| 87 | + return d, tea.Quit |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + return d, nil |
| 92 | +} |
| 93 | + |
| 94 | +// Position returns the dialog position (centered) |
| 95 | +func (d *maxIterationsDialog) Position() (row, col int) { |
| 96 | + // Render the dialog content to measure its actual dimensions |
| 97 | + dialogContent := d.View() |
| 98 | + |
| 99 | + // Get the actual rendered dimensions |
| 100 | + dialogWidth := lipgloss.Width(dialogContent) |
| 101 | + dialogHeight := lipgloss.Height(dialogContent) |
| 102 | + |
| 103 | + // Calculate centered position |
| 104 | + col = max(0, (d.width-dialogWidth)/2) |
| 105 | + row = max(0, (d.height-dialogHeight)/2) |
| 106 | + |
| 107 | + // Ensure dialog fits on screen |
| 108 | + if col+dialogWidth > d.width { |
| 109 | + col = max(0, d.width-dialogWidth) |
| 110 | + } |
| 111 | + if row+dialogHeight > d.height { |
| 112 | + row = max(0, d.height-dialogHeight) |
| 113 | + } |
| 114 | + |
| 115 | + return row, col |
| 116 | +} |
| 117 | + |
| 118 | +// View renders the max iterations confirmation dialog |
| 119 | +func (d *maxIterationsDialog) View() string { |
| 120 | + // clamped width: ~60% of screen, bounded by [36, 84] and screen margin |
| 121 | + dialogWidth := d.width * 60 / 100 |
| 122 | + if dialogWidth < 36 { |
| 123 | + dialogWidth = max(20, min(d.width-4, 36)) |
| 124 | + } |
| 125 | + if dialogWidth > 84 { |
| 126 | + dialogWidth = min(84, d.width-4) |
| 127 | + } |
| 128 | + |
| 129 | + padX := 2 |
| 130 | + padY := 1 |
| 131 | + |
| 132 | + // Border takes one character on each side when present |
| 133 | + frameHorizontal := (padX * 2) + 2 |
| 134 | + contentWidth := max(10, dialogWidth-frameHorizontal) |
| 135 | + |
| 136 | + dialogStyle := lipgloss.NewStyle(). |
| 137 | + Border(lipgloss.RoundedBorder()). |
| 138 | + BorderForeground(lipgloss.Color("#f59e0b")). |
| 139 | + Foreground(lipgloss.Color("#d1d5db")). |
| 140 | + Padding(padY, padX). |
| 141 | + Width(dialogWidth). |
| 142 | + Align(lipgloss.Left) |
| 143 | + |
| 144 | + titleStyle := lipgloss.NewStyle(). |
| 145 | + Bold(true). |
| 146 | + Foreground(lipgloss.Color("#f59e0b")). |
| 147 | + Align(lipgloss.Center). |
| 148 | + Width(contentWidth) |
| 149 | + |
| 150 | + messageStyle := lipgloss.NewStyle(). |
| 151 | + Foreground(lipgloss.Color("#d1d5db")). |
| 152 | + Width(contentWidth) |
| 153 | + |
| 154 | + questionStyle := lipgloss.NewStyle(). |
| 155 | + Bold(true). |
| 156 | + Foreground(lipgloss.Color("#d1d5db")). |
| 157 | + Align(lipgloss.Center). |
| 158 | + Width(contentWidth) |
| 159 | + |
| 160 | + optionsStyle := lipgloss.NewStyle(). |
| 161 | + Foreground(lipgloss.Color("#9ca3af")). |
| 162 | + Align(lipgloss.Center). |
| 163 | + Width(contentWidth) |
| 164 | + |
| 165 | + title := titleStyle.Render("Maximum Iterations Reached") |
| 166 | + |
| 167 | + separatorWidth := max(1, contentWidth) |
| 168 | + separator := lipgloss.NewStyle(). |
| 169 | + Foreground(lipgloss.Color("#4b5563")). |
| 170 | + Align(lipgloss.Center). |
| 171 | + Width(contentWidth). |
| 172 | + Render(strings.Repeat("─", separatorWidth)) |
| 173 | + |
| 174 | + // Info section |
| 175 | + infoText := fmt.Sprintf("Max Iterations: %d", d.maxIterations) |
| 176 | + infoWrapped := wrapDisplayText(infoText, contentWidth) |
| 177 | + infoSection := lipgloss.NewStyle(). |
| 178 | + Foreground(lipgloss.Color("#d1d5db")). |
| 179 | + Render(infoWrapped) |
| 180 | + |
| 181 | + // Message section |
| 182 | + message := messageStyle.Render(wrapDisplayText("The agent may be stuck in a loop. This can happen with smaller or less capable models.", contentWidth)) |
| 183 | + |
| 184 | + // Question section |
| 185 | + question := questionStyle.Render(wrapDisplayText("Do you want to continue for 10 more iterations?", contentWidth)) |
| 186 | + |
| 187 | + // Options section |
| 188 | + options := optionsStyle.Render(wrapDisplayText("[Y]es [N]o", contentWidth)) |
| 189 | + |
| 190 | + // Combine all parts with proper spacing |
| 191 | + parts := []string{title, separator, infoSection, "", message, "", question, "", options} |
| 192 | + |
| 193 | + content := lipgloss.JoinVertical(lipgloss.Left, parts...) |
| 194 | + return dialogStyle.Render(content) |
| 195 | +} |
| 196 | + |
| 197 | +// width-aware wrapping based on display cell width |
| 198 | +func wrapDisplayText(text string, maxWidth int) string { |
| 199 | + if maxWidth <= 0 { |
| 200 | + return text |
| 201 | + } |
| 202 | + words := strings.Fields(text) |
| 203 | + if len(words) == 0 { |
| 204 | + return text |
| 205 | + } |
| 206 | + var lines []string |
| 207 | + var current string |
| 208 | + for _, w := range words { |
| 209 | + if lipgloss.Width(current) == 0 { |
| 210 | + current = w |
| 211 | + continue |
| 212 | + } |
| 213 | + if lipgloss.Width(current+" "+w) <= maxWidth { |
| 214 | + current += " " + w |
| 215 | + } else { |
| 216 | + lines = append(lines, current) |
| 217 | + current = w |
| 218 | + } |
| 219 | + } |
| 220 | + if current != "" { |
| 221 | + lines = append(lines, current) |
| 222 | + } |
| 223 | + return strings.Join(lines, "\n") |
| 224 | +} |
0 commit comments