editor.go 4.5 KB

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