1package editor
2
3import (
4 "context"
5 "fmt"
6 "math/rand"
7 "net/http"
8 "os"
9 "os/exec"
10 "path/filepath"
11 "runtime"
12 "slices"
13 "strings"
14 "time"
15 "unicode"
16
17 "charm.land/bubbles/v2/key"
18 "charm.land/bubbles/v2/textarea"
19 tea "charm.land/bubbletea/v2"
20 "charm.land/lipgloss/v2"
21 "github.com/charmbracelet/crush/internal/app"
22 "github.com/charmbracelet/crush/internal/fsext"
23 "github.com/charmbracelet/crush/internal/message"
24 "github.com/charmbracelet/crush/internal/session"
25 "github.com/charmbracelet/crush/internal/tui/components/chat"
26 "github.com/charmbracelet/crush/internal/tui/components/completions"
27 "github.com/charmbracelet/crush/internal/tui/components/core/layout"
28 "github.com/charmbracelet/crush/internal/tui/components/dialogs"
29 "github.com/charmbracelet/crush/internal/tui/components/dialogs/commands"
30 "github.com/charmbracelet/crush/internal/tui/components/dialogs/filepicker"
31 "github.com/charmbracelet/crush/internal/tui/components/dialogs/quit"
32 "github.com/charmbracelet/crush/internal/tui/styles"
33 "github.com/charmbracelet/crush/internal/tui/util"
34)
35
36type Editor interface {
37 util.Model
38 layout.Sizeable
39 layout.Focusable
40 layout.Help
41 layout.Positional
42
43 SetSession(session session.Session) tea.Cmd
44 IsCompletionsOpen() bool
45 HasAttachments() bool
46 Cursor() *tea.Cursor
47}
48
49type FileCompletionItem struct {
50 Path string // The file path
51}
52
53type editorCmp struct {
54 width int
55 height int
56 x, y int
57 app *app.App
58 session session.Session
59 textarea textarea.Model
60 attachments []message.Attachment
61 deleteMode bool
62 readyPlaceholder string
63 workingPlaceholder string
64
65 keyMap EditorKeyMap
66
67 // File path completions
68 currentQuery string
69 completionsStartIndex int
70 isCompletionsOpen bool
71
72 history History
73}
74
75var DeleteKeyMaps = DeleteAttachmentKeyMaps{
76 AttachmentDeleteMode: key.NewBinding(
77 key.WithKeys("ctrl+r"),
78 key.WithHelp("ctrl+r+{i}", "delete attachment at index i"),
79 ),
80 Escape: key.NewBinding(
81 key.WithKeys("esc", "alt+esc"),
82 key.WithHelp("esc", "cancel delete mode"),
83 ),
84 DeleteAllAttachments: key.NewBinding(
85 key.WithKeys("r"),
86 key.WithHelp("ctrl+r+r", "delete all attachments"),
87 ),
88}
89
90const (
91 maxAttachments = 5
92 maxFileResults = 25
93)
94
95type LoadHistoryMsg struct{}
96
97type CloseHistoryMsg struct {
98 valueToSet string
99}
100
101type ScrollHistoryUp struct{}
102
103type ScrollHistoryDown struct{}
104
105type OpenEditorMsg struct {
106 Text string
107}
108
109func (m *editorCmp) openEditor(value string) tea.Cmd {
110 editor := os.Getenv("EDITOR")
111 if editor == "" {
112 // Use platform-appropriate default editor
113 if runtime.GOOS == "windows" {
114 editor = "notepad"
115 } else {
116 editor = "nvim"
117 }
118 }
119
120 tmpfile, err := os.CreateTemp("", "msg_*.md")
121 if err != nil {
122 return util.ReportError(err)
123 }
124 defer tmpfile.Close() //nolint:errcheck
125 if _, err := tmpfile.WriteString(value); err != nil {
126 return util.ReportError(err)
127 }
128 c := exec.CommandContext(context.TODO(), editor, tmpfile.Name())
129 c.Stdin = os.Stdin
130 c.Stdout = os.Stdout
131 c.Stderr = os.Stderr
132 return tea.ExecProcess(c, func(err error) tea.Msg {
133 if err != nil {
134 return util.ReportError(err)
135 }
136 content, err := os.ReadFile(tmpfile.Name())
137 if err != nil {
138 return util.ReportError(err)
139 }
140 if len(content) == 0 {
141 return util.ReportWarn("Message is empty")
142 }
143 os.Remove(tmpfile.Name())
144 return OpenEditorMsg{
145 Text: strings.TrimSpace(string(content)),
146 }
147 })
148}
149
150func (m *editorCmp) Init() tea.Cmd {
151 return nil
152}
153
154func (m *editorCmp) send() tea.Cmd {
155 value := m.textarea.Value()
156 value = strings.TrimSpace(value)
157
158 switch value {
159 case "exit", "quit":
160 m.textarea.Reset()
161 return util.CmdHandler(dialogs.OpenDialogMsg{Model: quit.NewQuitDialog()})
162 }
163
164 m.textarea.Reset()
165 attachments := m.attachments
166
167 m.attachments = nil
168 if value == "" {
169 return nil
170 }
171
172 // Change the placeholder when sending a new message.
173 m.randomizePlaceholders()
174
175 return tea.Batch(
176 util.CmdHandler(chat.SendMsg{
177 Text: value,
178 Attachments: attachments,
179 }),
180 )
181}
182
183func (m *editorCmp) repositionCompletions() tea.Msg {
184 x, y := m.completionsPosition()
185 return completions.RepositionCompletionsMsg{X: x, Y: y}
186}
187
188func (m *editorCmp) inHistoryMode() bool {
189 return m.history != nil
190}
191
192func (m *editorCmp) Update(msg tea.Msg) (util.Model, tea.Cmd) {
193 var cmd tea.Cmd
194 var cmds []tea.Cmd
195 switch msg := msg.(type) {
196 case LoadHistoryMsg:
197 if m.inHistoryMode() {
198 return m, nil
199 }
200 msgs, err := m.getUserMessagesAsText()
201 if err != nil {
202 // TODO(tauraamui): handle error for loading history better in the UI later
203 }
204 if len(msgs) == 0 {
205 break
206 }
207 m.history = InitialiseHistory(m.textarea.Value(), msgs)
208 m.textarea.SetValue(m.history.Value())
209 return m, nil
210 case CloseHistoryMsg:
211 m.textarea.SetValue(msg.valueToSet)
212 m.history = nil
213 return m, nil
214 case ScrollHistoryUp:
215 if m.inHistoryMode() {
216 m.history.ScrollUp()
217 m.textarea.SetValue(m.history.Value())
218 }
219 case ScrollHistoryDown:
220 if m.inHistoryMode() {
221 m.history.ScrollDown()
222 m.textarea.SetValue(m.history.Value())
223 }
224 case tea.WindowSizeMsg:
225 return m, m.repositionCompletions
226 case filepicker.FilePickedMsg:
227 if len(m.attachments) >= maxAttachments {
228 return m, util.ReportError(fmt.Errorf("cannot add more than %d images", maxAttachments))
229 }
230 m.attachments = append(m.attachments, msg.Attachment)
231 return m, nil
232 case completions.CompletionsOpenedMsg:
233 m.isCompletionsOpen = true
234 case completions.CompletionsClosedMsg:
235 m.isCompletionsOpen = false
236 m.currentQuery = ""
237 m.completionsStartIndex = 0
238 case completions.SelectCompletionMsg:
239 if !m.isCompletionsOpen {
240 return m, nil
241 }
242 if item, ok := msg.Value.(FileCompletionItem); ok {
243 word := m.textarea.Word()
244 // If the selected item is a file, insert its path into the textarea
245 value := m.textarea.Value()
246 value = value[:m.completionsStartIndex] + // Remove the current query
247 item.Path + // Insert the file path
248 value[m.completionsStartIndex+len(word):] // Append the rest of the value
249 // XXX: This will always move the cursor to the end of the textarea.
250 m.textarea.SetValue(value)
251 m.textarea.MoveToEnd()
252 if !msg.Insert {
253 m.isCompletionsOpen = false
254 m.currentQuery = ""
255 m.completionsStartIndex = 0
256 }
257 }
258
259 case commands.OpenExternalEditorMsg:
260 if m.app.AgentCoordinator.IsSessionBusy(m.session.ID) {
261 return m, util.ReportWarn("Agent is working, please wait...")
262 }
263 return m, m.openEditor(m.textarea.Value())
264 case OpenEditorMsg:
265 m.textarea.SetValue(msg.Text)
266 m.textarea.MoveToEnd()
267 case tea.PasteMsg:
268 path := strings.ReplaceAll(msg.Content, "\\ ", " ")
269 // try to get an image
270 path, err := filepath.Abs(strings.TrimSpace(path))
271 if err != nil {
272 m.textarea, cmd = m.textarea.Update(msg)
273 return m, cmd
274 }
275 isAllowedType := false
276 for _, ext := range filepicker.AllowedTypes {
277 if strings.HasSuffix(path, ext) {
278 isAllowedType = true
279 break
280 }
281 }
282 if !isAllowedType {
283 m.textarea, cmd = m.textarea.Update(msg)
284 return m, cmd
285 }
286 tooBig, _ := filepicker.IsFileTooBig(path, filepicker.MaxAttachmentSize)
287 if tooBig {
288 m.textarea, cmd = m.textarea.Update(msg)
289 return m, cmd
290 }
291
292 content, err := os.ReadFile(path)
293 if err != nil {
294 m.textarea, cmd = m.textarea.Update(msg)
295 return m, cmd
296 }
297 mimeBufferSize := min(512, len(content))
298 mimeType := http.DetectContentType(content[:mimeBufferSize])
299 fileName := filepath.Base(path)
300 attachment := message.Attachment{FilePath: path, FileName: fileName, MimeType: mimeType, Content: content}
301 return m, util.CmdHandler(filepicker.FilePickedMsg{
302 Attachment: attachment,
303 })
304
305 case commands.ToggleYoloModeMsg:
306 m.setEditorPrompt()
307 return m, nil
308 case tea.KeyPressMsg:
309 cur := m.textarea.Cursor()
310 curIdx := m.textarea.Width()*cur.Y + cur.X
311
312 // history
313 if m.inHistoryMode() {
314 switch true {
315 case key.Matches(msg, m.keyMap.Escape):
316 return m, util.CmdHandler(CloseHistoryMsg{valueToSet: m.history.ExistingValue()})
317 case key.Matches(msg, m.keyMap.Previous):
318 return m, util.CmdHandler(ScrollHistoryUp{})
319 case key.Matches(msg, m.keyMap.Next):
320 return m, util.CmdHandler(ScrollHistoryDown{})
321 }
322 cmds = append(cmds, util.CmdHandler(CloseHistoryMsg{
323 valueToSet: m.textarea.Value(),
324 }))
325 }
326
327 switch {
328 // Open command palette when "/" is pressed on empty prompt
329 case msg.String() == "/" && len(strings.TrimSpace(m.textarea.Value())) == 0:
330 return m, util.CmdHandler(dialogs.OpenDialogMsg{
331 Model: commands.NewCommandDialog(m.session.ID),
332 })
333 // Completions
334 case msg.String() == "@" && !m.isCompletionsOpen &&
335 // only show if beginning of prompt, or if previous char is a space or newline:
336 (len(m.textarea.Value()) == 0 || unicode.IsSpace(rune(m.textarea.Value()[len(m.textarea.Value())-1]))):
337 m.isCompletionsOpen = true
338 m.currentQuery = ""
339 m.completionsStartIndex = curIdx
340 cmds = append(cmds, m.startCompletions)
341 case m.isCompletionsOpen && curIdx <= m.completionsStartIndex:
342 cmds = append(cmds, util.CmdHandler(completions.CloseCompletionsMsg{}))
343 }
344
345 if key.Matches(msg, DeleteKeyMaps.AttachmentDeleteMode) {
346 m.deleteMode = true
347 return m, nil
348 }
349 if key.Matches(msg, DeleteKeyMaps.DeleteAllAttachments) && m.deleteMode {
350 m.deleteMode = false
351 m.attachments = nil
352 return m, nil
353 }
354 if key.Matches(msg, m.keyMap.Previous) || key.Matches(msg, m.keyMap.Next) {
355 if !m.inHistoryMode() {
356 cmds = append(cmds, util.CmdHandler(LoadHistoryMsg{}))
357 } else {
358 if key.Matches(msg, m.keyMap.Previous) {
359 cmds = append(cmds, util.CmdHandler(ScrollHistoryUp{}))
360 } else if key.Matches(msg, m.keyMap.Next) {
361 cmds = append(cmds, util.CmdHandler(ScrollHistoryDown{}))
362 }
363 }
364 }
365 rune := msg.Code
366 if m.deleteMode && unicode.IsDigit(rune) {
367 num := int(rune - '0')
368 m.deleteMode = false
369 if num < 10 && len(m.attachments) > num {
370 if num == 0 {
371 m.attachments = m.attachments[num+1:]
372 } else {
373 m.attachments = slices.Delete(m.attachments, num, num+1)
374 }
375 return m, nil
376 }
377 }
378 if key.Matches(msg, m.keyMap.OpenEditor) {
379 if m.app.AgentCoordinator.IsSessionBusy(m.session.ID) {
380 return m, util.ReportWarn("Agent is working, please wait...")
381 }
382 return m, m.openEditor(m.textarea.Value())
383 }
384 if key.Matches(msg, DeleteKeyMaps.Escape) && m.deleteMode {
385 m.deleteMode = false
386 return m, nil
387 }
388 if key.Matches(msg, m.keyMap.Newline) {
389 m.textarea.InsertRune('\n')
390 cmds = append(cmds, util.CmdHandler(completions.CloseCompletionsMsg{}))
391 }
392 // Handle Enter key
393 if m.textarea.Focused() && key.Matches(msg, m.keyMap.SendMessage) {
394 value := m.textarea.Value()
395 if strings.HasSuffix(value, "\\") {
396 // If the last character is a backslash, remove it and add a newline.
397 m.textarea.SetValue(strings.TrimSuffix(value, "\\"))
398 } else {
399 // Otherwise, send the message
400 return m, m.send()
401 }
402 }
403 }
404
405 m.textarea, cmd = m.textarea.Update(msg)
406 cmds = append(cmds, cmd)
407
408 if m.textarea.Focused() {
409 kp, ok := msg.(tea.KeyPressMsg)
410 if ok {
411 if kp.String() == "space" || m.textarea.Value() == "" {
412 m.isCompletionsOpen = false
413 m.currentQuery = ""
414 m.completionsStartIndex = 0
415 cmds = append(cmds, util.CmdHandler(completions.CloseCompletionsMsg{}))
416 } else {
417 word := m.textarea.Word()
418 if strings.HasPrefix(word, "@") {
419 // XXX: wont' work if editing in the middle of the field.
420 m.completionsStartIndex = strings.LastIndex(m.textarea.Value(), word)
421 m.currentQuery = word[1:]
422 x, y := m.completionsPosition()
423 x -= len(m.currentQuery)
424 m.isCompletionsOpen = true
425 cmds = append(cmds,
426 util.CmdHandler(completions.FilterCompletionsMsg{
427 Query: m.currentQuery,
428 Reopen: m.isCompletionsOpen,
429 X: x,
430 Y: y,
431 }),
432 )
433 } else if m.isCompletionsOpen {
434 m.isCompletionsOpen = false
435 m.currentQuery = ""
436 m.completionsStartIndex = 0
437 cmds = append(cmds, util.CmdHandler(completions.CloseCompletionsMsg{}))
438 }
439 }
440 }
441 }
442
443 return m, tea.Batch(cmds...)
444}
445
446func (m *editorCmp) setEditorPrompt() {
447 if m.app.Permissions.SkipRequests() {
448 m.textarea.SetPromptFunc(4, yoloPromptFunc)
449 return
450 }
451 m.textarea.SetPromptFunc(4, normalPromptFunc)
452}
453
454func (m *editorCmp) completionsPosition() (int, int) {
455 cur := m.textarea.Cursor()
456 if cur == nil {
457 return m.x, m.y + 1 // adjust for padding
458 }
459 x := cur.X + m.x
460 y := cur.Y + m.y + 1 // adjust for padding
461 return x, y
462}
463
464func (m *editorCmp) Cursor() *tea.Cursor {
465 cursor := m.textarea.Cursor()
466 if cursor != nil {
467 cursor.X = cursor.X + m.x + 1
468 cursor.Y = cursor.Y + m.y + 1 // adjust for padding
469 }
470 return cursor
471}
472
473var readyPlaceholders = [...]string{
474 "Ready!",
475 "Ready...",
476 "Ready?",
477 "Ready for instructions",
478}
479
480var workingPlaceholders = [...]string{
481 "Working!",
482 "Working...",
483 "Brrrrr...",
484 "Prrrrrrrr...",
485 "Processing...",
486 "Thinking...",
487}
488
489func (m *editorCmp) randomizePlaceholders() {
490 m.workingPlaceholder = workingPlaceholders[rand.Intn(len(workingPlaceholders))]
491 m.readyPlaceholder = readyPlaceholders[rand.Intn(len(readyPlaceholders))]
492}
493
494func (m *editorCmp) View() string {
495 t := styles.CurrentTheme()
496 // Update placeholder
497 if m.app.AgentCoordinator != nil && m.app.AgentCoordinator.IsBusy() {
498 m.textarea.Placeholder = m.workingPlaceholder
499 } else {
500 m.textarea.Placeholder = m.readyPlaceholder
501 }
502 if m.app.Permissions.SkipRequests() {
503 m.textarea.Placeholder = "Yolo mode!"
504 }
505 if len(m.attachments) == 0 {
506 content := t.S().Base.Padding(1).Render(
507 m.textarea.View(),
508 )
509 return content
510 }
511 content := t.S().Base.Padding(0, 1, 1, 1).Render(
512 lipgloss.JoinVertical(lipgloss.Top,
513 m.attachmentsContent(),
514 m.textarea.View(),
515 ),
516 )
517 return content
518}
519
520func (m *editorCmp) getUserMessagesAsText() ([]string, error) {
521 ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
522 defer cancel()
523
524 allMessages, err := m.app.Messages.FullList(ctx)
525 if err != nil {
526 return nil, err
527 }
528
529 var userMessages []string
530 for _, msg := range allMessages {
531 if msg.Role == message.User {
532 userMessages = append(userMessages, msg.Content().Text)
533 }
534 }
535
536 return userMessages, nil
537}
538
539func (m *editorCmp) SetSize(width, height int) tea.Cmd {
540 m.width = width
541 m.height = height
542 m.textarea.SetWidth(width - 2) // adjust for padding
543 m.textarea.SetHeight(height - 2) // adjust for padding
544 return nil
545}
546
547func (m *editorCmp) GetSize() (int, int) {
548 return m.textarea.Width(), m.textarea.Height()
549}
550
551func (m *editorCmp) attachmentsContent() string {
552 var styledAttachments []string
553 t := styles.CurrentTheme()
554 attachmentStyles := t.S().Base.
555 MarginLeft(1).
556 Background(t.FgMuted).
557 Foreground(t.FgBase)
558 for i, attachment := range m.attachments {
559 var filename string
560 if len(attachment.FileName) > 10 {
561 filename = fmt.Sprintf(" %s %s...", styles.DocumentIcon, attachment.FileName[0:7])
562 } else {
563 filename = fmt.Sprintf(" %s %s", styles.DocumentIcon, attachment.FileName)
564 }
565 if m.deleteMode {
566 filename = fmt.Sprintf("%d%s", i, filename)
567 }
568 styledAttachments = append(styledAttachments, attachmentStyles.Render(filename))
569 }
570 content := lipgloss.JoinHorizontal(lipgloss.Left, styledAttachments...)
571 return content
572}
573
574func (m *editorCmp) SetPosition(x, y int) tea.Cmd {
575 m.x = x
576 m.y = y
577 return nil
578}
579
580func (m *editorCmp) startCompletions() tea.Msg {
581 ls := m.app.Config().Options.TUI.Completions
582 depth, limit := ls.Limits()
583 files, _, _ := fsext.ListDirectory(".", nil, depth, limit)
584 slices.Sort(files)
585 completionItems := make([]completions.Completion, 0, len(files))
586 for _, file := range files {
587 file = strings.TrimPrefix(file, "./")
588 completionItems = append(completionItems, completions.Completion{
589 Title: file,
590 Value: FileCompletionItem{
591 Path: file,
592 },
593 })
594 }
595
596 x, y := m.completionsPosition()
597 return completions.OpenCompletionsMsg{
598 Completions: completionItems,
599 X: x,
600 Y: y,
601 MaxResults: maxFileResults,
602 }
603}
604
605// Blur implements Container.
606func (c *editorCmp) Blur() tea.Cmd {
607 c.textarea.Blur()
608 return nil
609}
610
611// Focus implements Container.
612func (c *editorCmp) Focus() tea.Cmd {
613 return c.textarea.Focus()
614}
615
616// IsFocused implements Container.
617func (c *editorCmp) IsFocused() bool {
618 return c.textarea.Focused()
619}
620
621// Bindings implements Container.
622func (c *editorCmp) Bindings() []key.Binding {
623 return c.keyMap.KeyBindings()
624}
625
626// TODO: most likely we do not need to have the session here
627// we need to move some functionality to the page level
628func (c *editorCmp) SetSession(session session.Session) tea.Cmd {
629 c.session = session
630 return nil
631}
632
633func (c *editorCmp) IsCompletionsOpen() bool {
634 return c.isCompletionsOpen
635}
636
637func (c *editorCmp) HasAttachments() bool {
638 return len(c.attachments) > 0
639}
640
641func normalPromptFunc(info textarea.PromptInfo) string {
642 t := styles.CurrentTheme()
643 if info.LineNumber == 0 {
644 return " > "
645 }
646 if info.Focused {
647 return t.S().Base.Foreground(t.GreenDark).Render("::: ")
648 }
649 return t.S().Muted.Render("::: ")
650}
651
652func yoloPromptFunc(info textarea.PromptInfo) string {
653 t := styles.CurrentTheme()
654 if info.LineNumber == 0 {
655 if info.Focused {
656 return fmt.Sprintf("%s ", t.YoloIconFocused)
657 } else {
658 return fmt.Sprintf("%s ", t.YoloIconBlurred)
659 }
660 }
661 if info.Focused {
662 return fmt.Sprintf("%s ", t.YoloDotsFocused)
663 }
664 return fmt.Sprintf("%s ", t.YoloDotsBlurred)
665}
666
667func New(app *app.App) Editor {
668 t := styles.CurrentTheme()
669 ta := textarea.New()
670 ta.SetStyles(t.S().TextArea)
671 ta.ShowLineNumbers = false
672 ta.CharLimit = -1
673 ta.SetVirtualCursor(false)
674 ta.Focus()
675 e := &editorCmp{
676 // TODO: remove the app instance from here
677 app: app,
678 textarea: ta,
679 keyMap: DefaultEditorKeyMap(),
680 }
681 e.setEditorPrompt()
682
683 e.randomizePlaceholders()
684 e.textarea.Placeholder = e.readyPlaceholder
685
686 return e
687}