-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
382 lines (290 loc) · 7.7 KB
/
main.go
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
package main
import (
"flag"
"fmt"
"io"
"log"
"os"
"strings"
"github.com/charmbracelet/bubbles/help"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/textarea"
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/glamour"
"github.com/charmbracelet/lipgloss"
"github.com/muesli/reflow/wrap"
)
var (
Program *tea.Program
senderStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("6"))
botStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("5"))
footerStyle = lipgloss.NewStyle().
Height(1).
BorderTop(true).
BorderStyle(lipgloss.NormalBorder()).
BorderForeground(lipgloss.Color("8")).
Faint(true)
)
type KeyMap struct {
Up key.Binding
Down key.Binding
}
type keyMap struct {
Up key.Binding
Down key.Binding
Submit key.Binding
Clear key.Binding
Reload key.Binding
Quit key.Binding
}
var keys = keyMap{
Up: key.NewBinding(
key.WithKeys("ctrl+p", "pgup"), // actual keybindings
key.WithHelp("ctrl+p", "move up"), // corresponding help text
),
Down: key.NewBinding(
key.WithKeys("ctrl+n", "pgdown"),
key.WithHelp("ctrl+n", "move down"),
),
Submit: key.NewBinding(
key.WithKeys("ctrl+j"),
key.WithHelp("ctrl+j", "send message"),
),
Clear: key.NewBinding(
key.WithKeys("ctrl+l"),
key.WithHelp("ctrl+l", "clear chat history"),
),
Reload: key.NewBinding(
key.WithKeys("ctrl+r"),
key.WithHelp("ctrl+r", "(debug) reload copilot token"),
),
Quit: key.NewBinding(
key.WithKeys("ctrl+c"),
key.WithHelp("ctrl+c", "quit"),
),
}
var viewportKeyMap = viewport.KeyMap{
Up: keys.Up,
Down: keys.Down,
}
type HistoryMessage struct {
Content string `json:"content"`
Role string `json:"role"`
}
func (k keyMap) ShortHelp() []key.Binding {
return []key.Binding{k.Submit, k.Up, k.Down, k.Quit, k.Clear, k.Reload}
}
func (k keyMap) FullHelp() [][]key.Binding {
return [][]key.Binding{
{k.Up, k.Down, k.Submit, k.Quit}, // first column
{k.Clear, k.Reload}, // second column
}
}
type model struct {
messages []HistoryMessage
history []HistoryMessage
textarea textarea.Model
viewport viewport.Model
copilotRequest CopilotRequest
answering bool
keys keyMap
help help.Model
ready bool
width int
}
func initialModel() model {
ta := textarea.New()
ta.Placeholder = "Write your query..."
ta.Focus()
ta.Prompt = "┃ "
ta.CharLimit = 1000
ta.SetHeight(4)
ta.ShowLineNumbers = true
// Remove cursor line styling
ta.FocusedStyle.CursorLine = lipgloss.NewStyle()
initialModel := model{
messages: []HistoryMessage{
createBotHistoryEntry("How can I assist you today?"),
},
textarea: ta,
copilotRequest: generateCopilotRequest(),
answering: false,
keys: keys,
help: help.New(),
}
initialModel.history = append(initialModel.history, createSystemHistoryEntry(SYSTEM_PROMPT))
return initialModel
}
func (m model) Init() tea.Cmd {
cmds := []tea.Cmd{textarea.Blink}
return tea.Batch(cmds...)
}
func (m model) headerView() string {
return lipgloss.JoinHorizontal(lipgloss.Center, m.viewport.View())
}
func (m model) View() string {
var views []string
views = append(views, m.viewport.View())
views = append(views, m.footerView())
views = append(views, m.helpView())
return lipgloss.JoinVertical(lipgloss.Top, views...)
}
func (m model) helpView() string {
help := m.help.View(m.keys)
return help
}
func createHistoryEntry(msg string) HistoryMessage {
return HistoryMessage{
Content: msg,
Role: "user",
}
}
func createBotHistoryEntry(msg string) HistoryMessage {
return HistoryMessage{
Content: msg,
Role: "assistant",
}
}
func createSystemHistoryEntry(msg string) HistoryMessage {
return HistoryMessage{
Content: msg,
Role: "system",
}
}
type LoadingMsg struct{}
type ResponseMsg struct{}
type AnswerMsg struct {
content string
done bool
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var (
cmd tea.Cmd
cmds []tea.Cmd
)
m.textarea, cmd = m.textarea.Update(msg)
cmds = append(cmds, cmd)
m.viewport, cmd = m.viewport.Update(msg)
cmds = append(cmds, cmd)
switch msg := msg.(type) {
case LoadingMsg:
message := m.textarea.Value()
m.history = append(m.history, createHistoryEntry(message))
m.messages = append(m.messages, createHistoryEntry(message))
m.textarea.SetValue("Loading...")
cmds = append(cmds, func() tea.Msg { return ResponseMsg{} })
case AnswerMsg:
m.messages[len(m.messages)-1].Content = msg.content
str := renderMessages(m.messages, m.width)
m.viewport.SetContent(str)
m.viewport.GotoBottom()
if msg.done {
m.textarea.Reset()
break
}
case ResponseMsg:
if m.answering {
break
}
m.messages = append(m.messages, createBotHistoryEntry("Thinking..."))
cmds = append(cmds, func() tea.Msg {
getResponse(&m, func(reply string, done bool, isError bool) {
if done && !isError {
m.answering = false
return
}
Program.Send(AnswerMsg{content: reply, done: true})
})
m.answering = true
return nil
})
case tea.WindowSizeMsg:
footerHeight := lipgloss.Height(m.footerView())
viewportHeight := msg.Height - footerHeight - lipgloss.Height(m.helpView())
m.width = msg.Width
m.textarea.SetWidth(msg.Width)
if !m.ready {
m.ready = true
m.viewport = viewport.New(msg.Width, viewportHeight)
m.viewport.KeyMap = viewportKeyMap
} else {
m.viewport.Width = msg.Width
m.viewport.Height = viewportHeight
}
str := renderMessages(m.messages, msg.Width)
m.viewport.SetContent(str)
case tea.KeyMsg:
switch {
case key.Matches(msg, m.keys.Quit):
return m, tea.Quit
case key.Matches(msg, m.keys.Clear):
m.history = m.history[:1]
m.messages = m.messages[:1]
m.viewport.GotoBottom()
str := renderMessages(m.messages, m.width)
m.viewport.SetContent(str)
case key.Matches(msg, m.keys.Reload):
m.copilotRequest = generateCopilotRequest()
case key.Matches(msg, m.keys.Submit):
if m.textarea.Value() != "" {
cmds = append(cmds, func() tea.Msg { return LoadingMsg{} })
}
}
}
return m, tea.Batch(cmds...)
}
func (m model) footerView() string {
line := strings.Repeat("─", m.viewport.Width)
return lipgloss.JoinVertical(lipgloss.Bottom, line, m.textarea.View())
}
func main() {
debug := flag.Bool("d", false, "Enable debug mode")
flag.Parse()
if *debug {
file, err := os.OpenFile("gopilot.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
if err != nil {
log.Fatal(err)
}
defer file.Close()
log.SetOutput(file)
} else {
log.SetOutput(io.Discard)
}
p := tea.NewProgram(initialModel(), tea.WithAltScreen())
Program = p
if _, err := p.Run(); err != nil {
fmt.Printf("Error running the program: %v", err)
os.Exit(1)
}
}
func renderText(str string, width int) string {
str = wrap.String(str, width)
renderer, _ := glamour.NewTermRenderer(
glamour.WithAutoStyle(),
glamour.WithWordWrap(width),
)
response, _ := renderer.Render(str)
return response
}
func renderBotText(str string, width int) string {
return botStyle.Render("GitHub Copilot: ") + renderText(str, width)
}
func renderUserText(str string, width int) string {
return senderStyle.Render("You: ") + renderText(str, width)
}
func renderMessages(messages []HistoryMessage, width int) string {
wrappedStrings := make([]string, len(messages))
for i, message := range messages {
if message.Role == "assistant" {
wrappedStrings[i] = renderBotText(message.Content, width)
continue
}
if message.Role == "user" {
wrappedStrings[i] = renderUserText(message.Content, width)
continue
}
wrappedStrings[i] = renderText(message.Content, width)
}
return strings.Join(wrappedStrings, "")
}