editor.go 3.4 KB

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