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