editor.go 3.9 KB

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