1package chat
2
3import (
4 "context"
5 "strings"
6 "time"
7
8 "github.com/charmbracelet/bubbles/v2/key"
9 tea "github.com/charmbracelet/bubbletea/v2"
10 "github.com/charmbracelet/crush/internal/app"
11 "github.com/charmbracelet/crush/internal/config"
12 "github.com/charmbracelet/crush/internal/llm/models"
13 "github.com/charmbracelet/crush/internal/message"
14 "github.com/charmbracelet/crush/internal/session"
15 "github.com/charmbracelet/crush/internal/tui/components/chat"
16 "github.com/charmbracelet/crush/internal/tui/components/chat/editor"
17 "github.com/charmbracelet/crush/internal/tui/components/chat/header"
18 "github.com/charmbracelet/crush/internal/tui/components/chat/sidebar"
19 "github.com/charmbracelet/crush/internal/tui/components/core/layout"
20 "github.com/charmbracelet/crush/internal/tui/components/dialogs/commands"
21 "github.com/charmbracelet/crush/internal/tui/page"
22 "github.com/charmbracelet/crush/internal/tui/styles"
23 "github.com/charmbracelet/crush/internal/tui/util"
24 "github.com/charmbracelet/crush/internal/version"
25 "github.com/charmbracelet/lipgloss/v2"
26)
27
28var ChatPageID page.PageID = "chat"
29
30const CompactModeBreakpoint = 120 // Width at which the chat page switches to compact mode
31
32type (
33 OpenFilePickerMsg struct{}
34 ChatFocusedMsg struct {
35 Focused bool // True if the chat input is focused, false otherwise
36 }
37 CancelTimerExpiredMsg struct{}
38)
39
40type ChatPage interface {
41 util.Model
42 layout.Help
43}
44
45type chatPage struct {
46 wWidth, wHeight int // Window dimensions
47 app *app.App
48
49 layout layout.SplitPaneLayout
50
51 session session.Session
52
53 keyMap KeyMap
54
55 chatFocused bool
56
57 compactMode bool
58 forceCompactMode bool // Force compact mode regardless of window size
59 showDetails bool // Show details in the header
60 header header.Header
61 compactSidebar layout.Container
62
63 cancelPending bool // True if ESC was pressed once and waiting for second press
64}
65
66func (p *chatPage) Init() tea.Cmd {
67 return tea.Batch(
68 p.layout.Init(),
69 p.compactSidebar.Init(),
70 p.layout.FocusPanel(layout.BottomPanel), // Focus on the bottom panel (editor),
71 )
72}
73
74// cancelTimerCmd creates a command that expires the cancel timer after 2 seconds
75func (p *chatPage) cancelTimerCmd() tea.Cmd {
76 return tea.Tick(2*time.Second, func(time.Time) tea.Msg {
77 return CancelTimerExpiredMsg{}
78 })
79}
80
81func (p *chatPage) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
82 var cmds []tea.Cmd
83 switch msg := msg.(type) {
84 case CancelTimerExpiredMsg:
85 p.cancelPending = false
86 return p, nil
87 case tea.WindowSizeMsg:
88 h, cmd := p.header.Update(msg)
89 cmds = append(cmds, cmd)
90 p.header = h.(header.Header)
91 cmds = append(cmds, p.compactSidebar.SetSize(msg.Width-4, 0))
92 // the mode is only relevant when there is a session
93 if p.session.ID != "" {
94 // Only auto-switch to compact mode if not forced
95 if !p.forceCompactMode {
96 if msg.Width <= CompactModeBreakpoint && p.wWidth > CompactModeBreakpoint {
97 p.wWidth = msg.Width
98 p.wHeight = msg.Height
99 cmds = append(cmds, p.setCompactMode(true))
100 return p, tea.Batch(cmds...)
101 } else if msg.Width > CompactModeBreakpoint && p.wWidth <= CompactModeBreakpoint {
102 p.wWidth = msg.Width
103 p.wHeight = msg.Height
104 return p, p.setCompactMode(false)
105 }
106 }
107 }
108 p.wWidth = msg.Width
109 p.wHeight = msg.Height
110 layoutHeight := msg.Height
111 if p.compactMode {
112 // make space for the header
113 layoutHeight -= 1
114 }
115 cmd = p.layout.SetSize(msg.Width, layoutHeight)
116 cmds = append(cmds, cmd)
117 return p, tea.Batch(cmds...)
118
119 case chat.SendMsg:
120 cmd := p.sendMessage(msg.Text, msg.Attachments)
121 if cmd != nil {
122 return p, cmd
123 }
124 case commands.ToggleCompactModeMsg:
125 // Only allow toggling if window width is larger than compact breakpoint
126 if p.wWidth > CompactModeBreakpoint {
127 p.forceCompactMode = !p.forceCompactMode
128 // If force compact mode is enabled, switch to compact mode
129 // If force compact mode is disabled, switch based on window size
130 if p.forceCompactMode {
131 return p, p.setCompactMode(true)
132 } else {
133 // Return to auto mode based on window size
134 shouldBeCompact := p.wWidth <= CompactModeBreakpoint
135 return p, p.setCompactMode(shouldBeCompact)
136 }
137 }
138 case commands.CommandRunCustomMsg:
139 // Check if the agent is busy before executing custom commands
140 if p.app.CoderAgent.IsBusy() {
141 return p, util.ReportWarn("Agent is busy, please wait before executing a command...")
142 }
143
144 // Handle custom command execution
145 cmd := p.sendMessage(msg.Content, nil)
146 if cmd != nil {
147 return p, cmd
148 }
149 case chat.SessionSelectedMsg:
150 if p.session.ID == "" {
151 cmd := p.setMessages()
152 if cmd != nil {
153 cmds = append(cmds, cmd)
154 }
155 }
156 needsModeChange := p.session.ID == ""
157 p.session = msg
158 p.header.SetSession(msg)
159 if needsModeChange && (p.wWidth <= CompactModeBreakpoint || p.forceCompactMode) {
160 cmds = append(cmds, p.setCompactMode(true))
161 }
162 case tea.KeyPressMsg:
163 switch {
164 case key.Matches(msg, p.keyMap.NewSession):
165 p.session = session.Session{}
166 return p, tea.Batch(
167 p.clearMessages(),
168 util.CmdHandler(chat.SessionClearedMsg{}),
169 p.setCompactMode(false),
170 )
171 case key.Matches(msg, p.keyMap.AddAttachment):
172 cfg := config.Get()
173 agentCfg := cfg.Agents[config.AgentCoder]
174 selectedModelID := agentCfg.Model
175 model := models.SupportedModels[selectedModelID]
176 if model.SupportsAttachments {
177 return p, util.CmdHandler(OpenFilePickerMsg{})
178 } else {
179 return p, util.ReportWarn("File attachments are not supported by the current model: " + string(selectedModelID))
180 }
181 case key.Matches(msg, p.keyMap.Tab):
182 if p.session.ID == "" {
183 return p, nil
184 }
185 p.chatFocused = !p.chatFocused
186 if p.chatFocused {
187 cmds = append(cmds, p.layout.FocusPanel(layout.LeftPanel))
188 cmds = append(cmds, util.CmdHandler(ChatFocusedMsg{Focused: true}))
189 } else {
190 cmds = append(cmds, p.layout.FocusPanel(layout.BottomPanel))
191 cmds = append(cmds, util.CmdHandler(ChatFocusedMsg{Focused: false}))
192 }
193 return p, tea.Batch(cmds...)
194 case key.Matches(msg, p.keyMap.Cancel):
195 if p.session.ID != "" {
196 if p.cancelPending {
197 // Second ESC press - actually cancel the session
198 p.cancelPending = false
199 p.app.CoderAgent.Cancel(p.session.ID)
200 return p, nil
201 } else {
202 // First ESC press - start the timer
203 p.cancelPending = true
204 return p, p.cancelTimerCmd()
205 }
206 }
207 case key.Matches(msg, p.keyMap.Details):
208 if p.session.ID == "" || !p.compactMode {
209 return p, nil // No session to show details for
210 }
211 p.showDetails = !p.showDetails
212 p.header.SetDetailsOpen(p.showDetails)
213 if p.showDetails {
214 return p, tea.Batch()
215 }
216
217 return p, nil
218 }
219 }
220 u, cmd := p.layout.Update(msg)
221 cmds = append(cmds, cmd)
222 p.layout = u.(layout.SplitPaneLayout)
223 h, cmd := p.header.Update(msg)
224 p.header = h.(header.Header)
225 cmds = append(cmds, cmd)
226 s, cmd := p.compactSidebar.Update(msg)
227 p.compactSidebar = s.(layout.Container)
228 cmds = append(cmds, cmd)
229 return p, tea.Batch(cmds...)
230}
231
232func (p *chatPage) setMessages() tea.Cmd {
233 messagesContainer := layout.NewContainer(
234 chat.NewMessagesListCmp(p.app),
235 layout.WithPadding(1, 1, 0, 1),
236 )
237 return tea.Batch(p.layout.SetLeftPanel(messagesContainer), messagesContainer.Init())
238}
239
240func (p *chatPage) setSidebar() tea.Cmd {
241 sidebarContainer := sidebarCmp(p.app, false)
242 sidebarContainer.Init()
243 return p.layout.SetRightPanel(sidebarContainer)
244}
245
246func (p *chatPage) clearMessages() tea.Cmd {
247 return p.layout.ClearLeftPanel()
248}
249
250func (p *chatPage) setCompactMode(compact bool) tea.Cmd {
251 p.compactMode = compact
252 var cmds []tea.Cmd
253 if compact {
254 // add offset for the header
255 p.layout.SetOffset(0, 1)
256 // make space for the header
257 cmds = append(cmds, p.layout.SetSize(p.wWidth, p.wHeight-1))
258 // remove the sidebar
259 cmds = append(cmds, p.layout.ClearRightPanel())
260 return tea.Batch(cmds...)
261 } else {
262 // remove the offset for the header
263 p.layout.SetOffset(0, 0)
264 // restore the original size
265 cmds = append(cmds, p.layout.SetSize(p.wWidth, p.wHeight))
266 // set the sidebar
267 cmds = append(cmds, p.setSidebar())
268 l, cmd := p.layout.Update(chat.SessionSelectedMsg(p.session))
269 p.layout = l.(layout.SplitPaneLayout)
270 cmds = append(cmds, cmd)
271
272 return tea.Batch(cmds...)
273 }
274}
275
276func (p *chatPage) sendMessage(text string, attachments []message.Attachment) tea.Cmd {
277 var cmds []tea.Cmd
278 if p.session.ID == "" {
279 session, err := p.app.Sessions.Create(context.Background(), "New Session")
280 if err != nil {
281 return util.ReportError(err)
282 }
283
284 p.session = session
285 cmd := p.setMessages()
286 if cmd != nil {
287 cmds = append(cmds, cmd)
288 }
289 cmds = append(cmds, util.CmdHandler(chat.SessionSelectedMsg(session)))
290 }
291
292 _, err := p.app.CoderAgent.Run(context.Background(), p.session.ID, text, attachments...)
293 if err != nil {
294 return util.ReportError(err)
295 }
296 return tea.Batch(cmds...)
297}
298
299func (p *chatPage) SetSize(width, height int) tea.Cmd {
300 return p.layout.SetSize(width, height)
301}
302
303func (p *chatPage) GetSize() (int, int) {
304 return p.layout.GetSize()
305}
306
307func (p *chatPage) View() tea.View {
308 if !p.compactMode || p.session.ID == "" {
309 // If not in compact mode or there is no session, we don't show the header
310 return p.layout.View()
311 }
312 layoutView := p.layout.View()
313 chatView := strings.Join(
314 []string{
315 p.header.View().String(),
316 layoutView.String(),
317 }, "\n",
318 )
319 layers := []*lipgloss.Layer{
320 lipgloss.NewLayer(chatView).X(0).Y(0),
321 }
322 if p.showDetails {
323 t := styles.CurrentTheme()
324 style := t.S().Base.
325 Border(lipgloss.RoundedBorder()).
326 BorderForeground(t.BorderFocus)
327 version := t.S().Subtle.Padding(0, 1).AlignHorizontal(lipgloss.Right).Width(p.wWidth - 4).Render(version.Version)
328 details := style.Render(
329 lipgloss.JoinVertical(
330 lipgloss.Left,
331 p.compactSidebar.View().String(),
332 version,
333 ),
334 )
335 layers = append(layers, lipgloss.NewLayer(details).X(1).Y(1))
336 }
337 canvas := lipgloss.NewCanvas(
338 layers...,
339 )
340 view := tea.NewView(canvas.Render())
341 view.SetCursor(layoutView.Cursor())
342 return view
343}
344
345func (p *chatPage) Bindings() []key.Binding {
346 bindings := []key.Binding{
347 p.keyMap.NewSession,
348 p.keyMap.AddAttachment,
349 }
350 if p.app.CoderAgent.IsBusy() {
351 cancelBinding := p.keyMap.Cancel
352 if p.cancelPending {
353 cancelBinding = key.NewBinding(
354 key.WithKeys("esc"),
355 key.WithHelp("esc", "press again to cancel"),
356 )
357 }
358 bindings = append([]key.Binding{cancelBinding}, bindings...)
359 }
360
361 if p.chatFocused {
362 bindings = append([]key.Binding{
363 key.NewBinding(
364 key.WithKeys("tab"),
365 key.WithHelp("tab", "focus editor"),
366 ),
367 }, bindings...)
368 } else {
369 bindings = append([]key.Binding{
370 key.NewBinding(
371 key.WithKeys("tab"),
372 key.WithHelp("tab", "focus chat"),
373 ),
374 }, bindings...)
375 }
376
377 bindings = append(bindings, p.layout.Bindings()...)
378 return bindings
379}
380
381func sidebarCmp(app *app.App, compact bool) layout.Container {
382 padding := layout.WithPadding(1, 1, 1, 1)
383 if compact {
384 padding = layout.WithPadding(0, 1, 1, 1)
385 }
386 return layout.NewContainer(
387 sidebar.NewSidebarCmp(app.History, app.LSPClients, compact),
388 padding,
389 )
390}
391
392func NewChatPage(app *app.App) ChatPage {
393 editorContainer := layout.NewContainer(
394 editor.NewEditorCmp(app),
395 )
396 return &chatPage{
397 app: app,
398 layout: layout.NewSplitPane(
399 layout.WithRightPanel(sidebarCmp(app, false)),
400 layout.WithBottomPanel(editorContainer),
401 layout.WithFixedBottomHeight(5),
402 layout.WithFixedRightWidth(31),
403 ),
404 compactSidebar: sidebarCmp(app, true),
405 keyMap: DefaultKeyMap(),
406 header: header.New(app.LSPClients),
407 }
408}