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