editor.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. "golang.org/x/net/context"
  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. cancelMessage context.CancelFunc
  31. }
  32. type editorKeyMap struct {
  33. SendMessage key.Binding
  34. SendMessageI key.Binding
  35. CancelMessage key.Binding
  36. InsertMode key.Binding
  37. NormaMode key.Binding
  38. VisualMode key.Binding
  39. VisualLineMode key.Binding
  40. }
  41. var editorKeyMapValue = editorKeyMap{
  42. SendMessage: key.NewBinding(
  43. key.WithKeys("enter"),
  44. key.WithHelp("enter", "send message normal mode"),
  45. ),
  46. SendMessageI: key.NewBinding(
  47. key.WithKeys("ctrl+s"),
  48. key.WithHelp("ctrl+s", "send message insert mode"),
  49. ),
  50. CancelMessage: key.NewBinding(
  51. key.WithKeys("ctrl+x"),
  52. key.WithHelp("ctrl+x", "cancel current message"),
  53. ),
  54. InsertMode: key.NewBinding(
  55. key.WithKeys("i"),
  56. key.WithHelp("i", "insert mode"),
  57. ),
  58. NormaMode: key.NewBinding(
  59. key.WithKeys("esc"),
  60. key.WithHelp("esc", "normal mode"),
  61. ),
  62. VisualMode: key.NewBinding(
  63. key.WithKeys("v"),
  64. key.WithHelp("v", "visual mode"),
  65. ),
  66. VisualLineMode: key.NewBinding(
  67. key.WithKeys("V"),
  68. key.WithHelp("V", "visual line mode"),
  69. ),
  70. }
  71. func (m *editorCmp) Init() tea.Cmd {
  72. return m.editor.Init()
  73. }
  74. func (m *editorCmp) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
  75. switch msg := msg.(type) {
  76. case vimtea.EditorModeMsg:
  77. m.editorMode = msg.Mode
  78. case SelectedSessionMsg:
  79. if msg.SessionID != m.sessionID {
  80. m.sessionID = msg.SessionID
  81. }
  82. }
  83. if m.IsFocused() {
  84. switch msg := msg.(type) {
  85. case tea.KeyMsg:
  86. switch {
  87. case key.Matches(msg, editorKeyMapValue.SendMessage):
  88. if m.editorMode == vimtea.ModeNormal {
  89. return m, m.Send()
  90. }
  91. case key.Matches(msg, editorKeyMapValue.SendMessageI):
  92. if m.editorMode == vimtea.ModeInsert {
  93. return m, m.Send()
  94. }
  95. case key.Matches(msg, editorKeyMapValue.CancelMessage):
  96. return m, m.Cancel()
  97. }
  98. }
  99. u, cmd := m.editor.Update(msg)
  100. m.editor = u.(vimtea.Editor)
  101. return m, cmd
  102. }
  103. return m, nil
  104. }
  105. func (m *editorCmp) Blur() tea.Cmd {
  106. m.focused = false
  107. return nil
  108. }
  109. func (m *editorCmp) BorderText() map[layout.BorderPosition]string {
  110. title := "New Message"
  111. if m.focused {
  112. title = lipgloss.NewStyle().Foreground(styles.Primary).Render(title)
  113. }
  114. return map[layout.BorderPosition]string{
  115. layout.BottomLeftBorder: title,
  116. }
  117. }
  118. func (m *editorCmp) Focus() tea.Cmd {
  119. m.focused = true
  120. return m.editor.Tick()
  121. }
  122. func (m *editorCmp) GetSize() (int, int) {
  123. return m.width, m.height
  124. }
  125. func (m *editorCmp) IsFocused() bool {
  126. return m.focused
  127. }
  128. func (m *editorCmp) SetSize(width int, height int) {
  129. m.width = width
  130. m.height = height
  131. m.editor.SetSize(width, height)
  132. }
  133. func (m *editorCmp) Cancel() tea.Cmd {
  134. if m.cancelMessage == nil {
  135. return util.ReportWarn("No message to cancel")
  136. }
  137. m.cancelMessage()
  138. m.cancelMessage = nil
  139. return util.ReportWarn("Message cancelled")
  140. }
  141. func (m *editorCmp) Send() tea.Cmd {
  142. if m.cancelMessage != nil {
  143. return util.ReportWarn("Assistant is still working on the previous message")
  144. }
  145. messages, err := m.app.Messages.List(m.sessionID)
  146. if err != nil {
  147. return util.ReportError(err)
  148. }
  149. if hasUnfinishedMessages(messages) {
  150. return util.ReportWarn("Assistant is still working on the previous message")
  151. }
  152. a, err := agent.NewCoderAgent(m.app)
  153. if err != nil {
  154. return util.ReportError(err)
  155. }
  156. content := strings.Join(m.editor.GetBuffer().Lines(), "\n")
  157. if len(content) == 0 {
  158. return util.ReportWarn("Message is empty")
  159. }
  160. ctx, cancel := context.WithCancel(m.app.Context)
  161. m.cancelMessage = cancel
  162. go func() {
  163. defer cancel()
  164. a.Generate(ctx, m.sessionID, content)
  165. m.cancelMessage = nil
  166. }()
  167. return m.editor.Reset()
  168. }
  169. func (m *editorCmp) View() string {
  170. return m.editor.View()
  171. }
  172. func (m *editorCmp) BindingKeys() []key.Binding {
  173. return layout.KeyMapToSlice(editorKeyMapValue)
  174. }
  175. func NewEditorCmp(app *app.App) EditorCmp {
  176. editor := vimtea.NewEditor(
  177. vimtea.WithFileName("message.md"),
  178. )
  179. return &editorCmp{
  180. app: app,
  181. editor: editor,
  182. }
  183. }