editor.go 3.2 KB

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