editor.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package repl
  2. import (
  3. "strings"
  4. "github.com/charmbracelet/bubbles/key"
  5. tea "github.com/charmbracelet/bubbletea"
  6. "github.com/charmbracelet/lipgloss"
  7. "github.com/kujtimiihoxha/termai/internal/app"
  8. "github.com/kujtimiihoxha/termai/internal/llm/agent"
  9. "github.com/kujtimiihoxha/termai/internal/tui/layout"
  10. "github.com/kujtimiihoxha/termai/internal/tui/styles"
  11. "github.com/kujtimiihoxha/termai/internal/tui/util"
  12. "github.com/kujtimiihoxha/vimtea"
  13. )
  14. type EditorCmp interface {
  15. tea.Model
  16. layout.Focusable
  17. layout.Sizeable
  18. layout.Bordered
  19. layout.Bindings
  20. }
  21. type editorCmp struct {
  22. app *app.App
  23. editor vimtea.Editor
  24. editorMode vimtea.EditorMode
  25. sessionID string
  26. focused bool
  27. width int
  28. height int
  29. }
  30. type editorKeyMap struct {
  31. SendMessage key.Binding
  32. SendMessageI key.Binding
  33. InsertMode key.Binding
  34. NormaMode key.Binding
  35. VisualMode key.Binding
  36. VisualLineMode key.Binding
  37. }
  38. var editorKeyMapValue = editorKeyMap{
  39. SendMessage: key.NewBinding(
  40. key.WithKeys("enter"),
  41. key.WithHelp("enter", "send message normal mode"),
  42. ),
  43. SendMessageI: key.NewBinding(
  44. key.WithKeys("ctrl+s"),
  45. key.WithHelp("ctrl+s", "send message insert mode"),
  46. ),
  47. InsertMode: key.NewBinding(
  48. key.WithKeys("i"),
  49. key.WithHelp("i", "insert mode"),
  50. ),
  51. NormaMode: key.NewBinding(
  52. key.WithKeys("esc"),
  53. key.WithHelp("esc", "normal mode"),
  54. ),
  55. VisualMode: key.NewBinding(
  56. key.WithKeys("v"),
  57. key.WithHelp("v", "visual mode"),
  58. ),
  59. VisualLineMode: key.NewBinding(
  60. key.WithKeys("V"),
  61. key.WithHelp("V", "visual line mode"),
  62. ),
  63. }
  64. func (m *editorCmp) Init() tea.Cmd {
  65. return m.editor.Init()
  66. }
  67. func (m *editorCmp) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
  68. switch msg := msg.(type) {
  69. case vimtea.EditorModeMsg:
  70. m.editorMode = msg.Mode
  71. case SelectedSessionMsg:
  72. if msg.SessionID != m.sessionID {
  73. m.sessionID = msg.SessionID
  74. }
  75. }
  76. if m.IsFocused() {
  77. switch msg := msg.(type) {
  78. case tea.KeyMsg:
  79. switch {
  80. case key.Matches(msg, editorKeyMapValue.SendMessage):
  81. if m.editorMode == vimtea.ModeNormal {
  82. return m, m.Send()
  83. }
  84. case key.Matches(msg, editorKeyMapValue.SendMessageI):
  85. if m.editorMode == vimtea.ModeInsert {
  86. return m, m.Send()
  87. }
  88. }
  89. }
  90. u, cmd := m.editor.Update(msg)
  91. m.editor = u.(vimtea.Editor)
  92. return m, cmd
  93. }
  94. return m, nil
  95. }
  96. func (m *editorCmp) Blur() tea.Cmd {
  97. m.focused = false
  98. return nil
  99. }
  100. func (m *editorCmp) BorderText() map[layout.BorderPosition]string {
  101. title := "New Message"
  102. if m.focused {
  103. title = lipgloss.NewStyle().Foreground(styles.Primary).Render(title)
  104. }
  105. return map[layout.BorderPosition]string{
  106. layout.BottomLeftBorder: title,
  107. }
  108. }
  109. func (m *editorCmp) Focus() tea.Cmd {
  110. m.focused = true
  111. return m.editor.Tick()
  112. }
  113. func (m *editorCmp) GetSize() (int, int) {
  114. return m.width, m.height
  115. }
  116. func (m *editorCmp) IsFocused() bool {
  117. return m.focused
  118. }
  119. func (m *editorCmp) SetSize(width int, height int) {
  120. m.width = width
  121. m.height = height
  122. m.editor.SetSize(width, height)
  123. }
  124. func (m *editorCmp) Send() tea.Cmd {
  125. return func() tea.Msg {
  126. messages, _ := m.app.Messages.List(m.sessionID)
  127. if hasUnfinishedMessages(messages) {
  128. return util.ReportWarn("Assistant is still working on the previous message")
  129. }
  130. a, _ := agent.NewCoderAgent(m.app)
  131. content := strings.Join(m.editor.GetBuffer().Lines(), "\n")
  132. go a.Generate(m.sessionID, content)
  133. return m.editor.Reset()
  134. }
  135. }
  136. func (m *editorCmp) View() string {
  137. return m.editor.View()
  138. }
  139. func (m *editorCmp) BindingKeys() []key.Binding {
  140. return layout.KeyMapToSlice(editorKeyMapValue)
  141. }
  142. func NewEditorCmp(app *app.App) EditorCmp {
  143. editor := vimtea.NewEditor(
  144. vimtea.WithFileName("message.md"),
  145. )
  146. return &editorCmp{
  147. app: app,
  148. editor: editor,
  149. }
  150. }