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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
|
package main
import (
"elefant/models"
"elefant/pngmeta"
"fmt"
"strconv"
"strings"
"time"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
var (
app *tview.Application
pages *tview.Pages
textArea *tview.TextArea
editArea *tview.TextArea
textView *tview.TextView
position *tview.TextView
helpView *tview.TextView
flex *tview.Flex
// chatActModal *tview.Modal
sysModal *tview.Modal
indexPickWindow *tview.InputField
renameWindow *tview.InputField
// pages
historyPage = "historyPage"
agentPage = "agentPage"
editMsgPage = "editMsgPage"
indexPage = "indexPage"
helpPage = "helpPage"
renamePage = "renamePage"
// help text
helpText = `
[yellow]Esc[white]: send msg
[yellow]PgUp/Down[white]: switch focus
[yellow]F1[white]: manage chats
[yellow]F2[white]: regen last
[yellow]F3[white]: delete last msg
[yellow]F4[white]: edit msg
[yellow]F5[white]: toggle system
[yellow]F6[white]: interrupt bot resp
[yellow]F7[white]: copy last msg to clipboard (linux xclip)
[yellow]F8[white]: copy n msg to clipboard (linux xclip)
[yellow]Ctrl+s[white]: load new char/agent
[yellow]Ctrl+e[white]: export chat to json file
[yellow]Ctrl+n[white]: start a new chat
[yellow]Ctrl+c[white]: close programm
Press Enter to go back
`
)
func makeChatTable(chatList []string) *tview.Table {
actions := []string{"load", "rename", "delete"}
rows, cols := len(chatList), len(actions)+1
chatActTable := tview.NewTable().
SetBorders(true)
for r := 0; r < rows; r++ {
for c := 0; c < cols; c++ {
color := tcell.ColorWhite
if c < 1 {
chatActTable.SetCell(r, c,
tview.NewTableCell(chatList[r]).
SetTextColor(color).
SetAlign(tview.AlignCenter))
} else {
chatActTable.SetCell(r, c,
tview.NewTableCell(actions[c-1]).
SetTextColor(color).
SetAlign(tview.AlignCenter))
}
}
}
chatActTable.Select(0, 0).SetFixed(1, 1).SetDoneFunc(func(key tcell.Key) {
if key == tcell.KeyEsc || key == tcell.KeyF1 {
pages.RemovePage(historyPage)
return
}
if key == tcell.KeyEnter {
chatActTable.SetSelectable(true, true)
}
}).SetSelectedFunc(func(row int, column int) {
tc := chatActTable.GetCell(row, column)
tc.SetTextColor(tcell.ColorRed)
chatActTable.SetSelectable(false, false)
selectedChat := chatList[row]
// notification := fmt.Sprintf("chat: %s; action: %s", selectedChat, tc.Text)
switch tc.Text {
case "load":
history, err := loadHistoryChat(selectedChat)
if err != nil {
logger.Error("failed to read history file", "chat", selectedChat)
pages.RemovePage(historyPage)
return
}
chatBody.Messages = history
textView.SetText(chatToText(cfg.ShowSys))
activeChatName = selectedChat
pages.RemovePage(historyPage)
colorText()
updateStatusLine()
return
case "rename":
pages.RemovePage(historyPage)
pages.AddPage(renamePage, renameWindow, true, true)
return
case "delete":
sc, ok := chatMap[selectedChat]
if !ok {
// no chat found
pages.RemovePage(historyPage)
return
}
if err := store.RemoveChat(sc.ID); err != nil {
logger.Error("failed to remove chat from db", "chat_id", sc.ID, "chat_name", sc.Name)
}
if err := notifyUser("chat deleted", selectedChat+" was deleted"); err != nil {
logger.Error("failed to send notification", "error", err)
}
pages.RemovePage(historyPage)
return
default:
pages.RemovePage(historyPage)
return
}
})
return chatActTable
}
// // code block colors get interrupted by " & *
// func codeBlockColor(text string) string {
// fi := strings.Index(text, "```")
// if fi < 0 {
// return text
// }
// li := strings.LastIndex(text, "```")
// if li == fi { // only openning backticks
// return text
// }
// return strings.Replace(text, "```", "```[blue:black:i]", 1)
// }
func colorText() {
// INFO: is there a better way to markdown?
tv := textView.GetText(false)
cq := quotesRE.ReplaceAllString(tv, `[orange:-:-]$1[-:-:-]`)
// cb := codeBlockColor(cq)
// cb := codeBlockRE.ReplaceAllString(cq, `[blue:black:i]$1[-:-:-]`)
textView.SetText(starRE.ReplaceAllString(cq, `[turquoise::i]$1[-:-:-]`))
}
func updateStatusLine() {
position.SetText(fmt.Sprintf(indexLine, botRespMode, cfg.AssistantRole, activeChatName))
}
func initSysCards() ([]string, error) {
labels := []string{}
labels = append(labels, sysLabels...)
cards, err := pngmeta.ReadDirCards(cfg.SysDir, cfg.UserRole)
if err != nil {
logger.Error("failed to read sys dir", "error", err)
return nil, err
}
for _, cc := range cards {
sysMap[cc.Role] = cc
labels = append(labels, cc.Role)
}
return labels, nil
}
func startNewChat() {
id, err := store.ChatGetMaxID()
if err != nil {
logger.Error("failed to get chat id", "error", err)
}
// TODO: get the current agent and it's starter
if ok := charToStart(cfg.AssistantRole); !ok {
logger.Warn("no such sys msg", "name", cfg.AssistantRole)
}
// set chat body
chatBody.Messages = defaultStarter
textView.SetText(chatToText(cfg.ShowSys))
newChat := &models.Chat{
ID: id + 1,
Name: fmt.Sprintf("%v_%v", "new", time.Now().Unix()),
Msgs: string(defaultStarterBytes),
Agent: cfg.AssistantRole,
}
activeChatName = newChat.Name
chatMap[newChat.Name] = newChat
updateStatusLine()
colorText()
}
func init() {
theme := tview.Theme{
PrimitiveBackgroundColor: tcell.ColorDefault,
ContrastBackgroundColor: tcell.ColorGray,
MoreContrastBackgroundColor: tcell.ColorNavy,
BorderColor: tcell.ColorGray,
TitleColor: tcell.ColorRed,
GraphicsColor: tcell.ColorBlue,
PrimaryTextColor: tcell.ColorLightGray,
SecondaryTextColor: tcell.ColorYellow,
TertiaryTextColor: tcell.ColorOrange,
InverseTextColor: tcell.ColorPurple,
ContrastSecondaryTextColor: tcell.ColorLime,
}
tview.Styles = theme
app = tview.NewApplication()
pages = tview.NewPages()
textArea = tview.NewTextArea().
SetPlaceholder("Type your prompt...")
textArea.SetBorder(true).SetTitle("input")
textView = tview.NewTextView().
SetDynamicColors(true).
SetRegions(true).
SetChangedFunc(func() {
app.Draw()
})
textView.SetBorder(true).SetTitle("chat")
focusSwitcher[textArea] = textView
focusSwitcher[textView] = textArea
position = tview.NewTextView().
SetDynamicColors(true).
SetTextAlign(tview.AlignCenter)
flex = tview.NewFlex().SetDirection(tview.FlexRow).
AddItem(textView, 0, 40, false).
AddItem(textArea, 0, 10, true).
AddItem(position, 0, 1, false)
sysModal = tview.NewModal().
SetText("Switch sys msg:").
SetDoneFunc(func(buttonIndex int, buttonLabel string) {
switch buttonLabel {
case "cancel":
pages.RemovePage(agentPage)
sysModal.ClearButtons()
return
default:
if ok := charToStart(buttonLabel); !ok {
logger.Warn("no such sys msg", "name", buttonLabel)
pages.RemovePage(agentPage)
return
}
// replace textview
textView.SetText(chatToText(cfg.ShowSys))
colorText()
updateStatusLine()
sysModal.ClearButtons()
pages.RemovePage(agentPage)
app.SetFocus(textArea)
}
})
editArea = tview.NewTextArea().
SetPlaceholder("Replace msg...")
editArea.SetBorder(true).SetTitle("input")
editArea.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
if event.Key() == tcell.KeyEscape && editMode {
editedMsg := editArea.GetText()
if editedMsg == "" {
if err := notifyUser("edit", "no edit provided"); err != nil {
logger.Error("failed to send notification", "error", err)
}
pages.RemovePage(editMsgPage)
editMode = false
return nil
}
chatBody.Messages[selectedIndex].Content = editedMsg
// change textarea
textView.SetText(chatToText(cfg.ShowSys))
pages.RemovePage(editMsgPage)
editMode = false
return nil
}
return event
})
indexPickWindow = tview.NewInputField().
SetLabel("Enter a msg index: ").
SetFieldWidth(4).
SetAcceptanceFunc(tview.InputFieldInteger).
SetDoneFunc(func(key tcell.Key) {
pages.RemovePage(indexPage)
})
indexPickWindow.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
switch event.Key() {
case tcell.KeyBackspace:
return event
case tcell.KeyEnter:
si := indexPickWindow.GetText()
siInt, err := strconv.Atoi(si)
if err != nil {
logger.Error("failed to convert provided index", "error", err, "si", si)
if err := notifyUser("cancel", "no index provided"); err != nil {
logger.Error("failed to send notification", "error", err)
}
pages.RemovePage(indexPage)
return event
}
selectedIndex = siInt
if len(chatBody.Messages)+1 < selectedIndex || selectedIndex < 0 {
msg := "chosen index is out of bounds"
logger.Warn(msg, "index", selectedIndex)
if err := notifyUser("error", msg); err != nil {
logger.Error("failed to send notification", "error", err)
}
pages.RemovePage(indexPage)
return event
}
m := chatBody.Messages[selectedIndex]
if editMode && event.Key() == tcell.KeyEnter {
pages.AddPage(editMsgPage, editArea, true, true)
editArea.SetText(m.Content, true)
}
if !editMode && event.Key() == tcell.KeyEnter {
if err := copyToClipboard(m.Content); err != nil {
logger.Error("failed to copy to clipboard", "error", err)
}
previewLen := 30
if len(m.Content) < 30 {
previewLen = len(m.Content)
}
notification := fmt.Sprintf("msg '%s' was copied to the clipboard", m.Content[:previewLen])
if err := notifyUser("copied", notification); err != nil {
logger.Error("failed to send notification", "error", err)
}
}
return event
default:
return event
}
})
//
renameWindow = tview.NewInputField().
SetLabel("Enter a msg index: ").
SetFieldWidth(20).
SetAcceptanceFunc(tview.InputFieldMaxLength(100)).
SetDoneFunc(func(key tcell.Key) {
pages.RemovePage(renamePage)
})
renameWindow.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
if event.Key() == tcell.KeyEnter {
nname := renameWindow.GetText()
if nname == "" {
return event
}
currentChat := chatMap[activeChatName]
delete(chatMap, activeChatName)
currentChat.Name = nname
activeChatName = nname
chatMap[activeChatName] = currentChat
_, err := store.UpsertChat(currentChat)
if err != nil {
logger.Error("failed to upsert chat", "error", err, "chat", currentChat)
}
notification := fmt.Sprintf("renamed chat to '%s'", activeChatName)
if err := notifyUser("renamed", notification); err != nil {
logger.Error("failed to send notification", "error", err)
}
}
return event
})
//
helpView = tview.NewTextView().SetDynamicColors(true).SetText(helpText).SetDoneFunc(func(key tcell.Key) {
pages.RemovePage(helpPage)
})
helpView.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
switch event.Key() {
case tcell.KeyEsc, tcell.KeyEnter:
return event
}
return nil
})
//
textArea.SetMovedFunc(updateStatusLine)
updateStatusLine()
textView.SetText(chatToText(cfg.ShowSys))
colorText()
textView.ScrollToEnd()
_, err := initSysCards()
if err != nil {
logger.Error("failed to init sys cards", "error", err)
}
app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
if event.Key() == tcell.KeyF1 {
chatList, err := loadHistoryChats()
if err != nil {
logger.Error("failed to load chat history", "error", err)
return nil
}
chatActTable := makeChatTable(chatList)
pages.AddPage(historyPage, chatActTable, true, true)
return nil
}
if event.Key() == tcell.KeyF2 {
// regen last msg
chatBody.Messages = chatBody.Messages[:len(chatBody.Messages)-1]
textView.SetText(chatToText(cfg.ShowSys))
go chatRound("", cfg.UserRole, textView, true)
return nil
}
if event.Key() == tcell.KeyF3 && !botRespMode {
// delete last msg
// check textarea text; if it ends with bot icon delete only icon:
text := textView.GetText(true)
if strings.HasSuffix(text, cfg.AssistantIcon) {
logger.Info("deleting assistant icon", "icon", cfg.AssistantIcon)
textView.SetText(strings.TrimSuffix(text, cfg.AssistantIcon))
colorText()
return nil
}
chatBody.Messages = chatBody.Messages[:len(chatBody.Messages)-1]
textView.SetText(chatToText(cfg.ShowSys))
colorText()
return nil
}
if event.Key() == tcell.KeyF4 {
// edit msg
editMode = true
pages.AddPage(indexPage, indexPickWindow, true, true)
return nil
}
if event.Key() == tcell.KeyF5 {
// switch cfg.ShowSys
cfg.ShowSys = !cfg.ShowSys
textView.SetText(chatToText(cfg.ShowSys))
colorText()
}
if event.Key() == tcell.KeyF6 {
interruptResp = true
botRespMode = false
return nil
}
if event.Key() == tcell.KeyF7 {
// copy msg to clipboard
editMode = false
m := chatBody.Messages[len(chatBody.Messages)-1]
if err := copyToClipboard(m.Content); err != nil {
logger.Error("failed to copy to clipboard", "error", err)
}
previewLen := 30
if len(m.Content) < 30 {
previewLen = len(m.Content)
}
notification := fmt.Sprintf("msg '%s' was copied to the clipboard", m.Content[:previewLen])
if err := notifyUser("copied", notification); err != nil {
logger.Error("failed to send notification", "error", err)
}
return nil
}
if event.Key() == tcell.KeyF8 {
// copy msg to clipboard
editMode = false
pages.AddPage(indexPage, indexPickWindow, true, true)
return nil
}
if event.Key() == tcell.KeyF12 {
// help window cheatsheet
pages.AddPage(helpPage, helpView, true, true)
return nil
}
if event.Key() == tcell.KeyCtrlE {
// export loaded chat into json file
if err := exportChat(); err != nil {
logger.Error("failed to export chat;", "error", err, "chat_name", activeChatName)
return nil
}
if err := notifyUser("exported chat", "chat: "+activeChatName+" was exported"); err != nil {
logger.Error("failed to send notification", "error", err)
}
return nil
}
if event.Key() == tcell.KeyCtrlA {
textArea.SetText("pressed ctrl+a", true)
return nil
}
if event.Key() == tcell.KeyCtrlN {
startNewChat()
return nil
}
if event.Key() == tcell.KeyCtrlS {
// switch sys prompt
labels, err := initSysCards()
if err != nil {
logger.Error("failed to read sys dir", "error", err)
if err := notifyUser("error", "failed to read: "+cfg.SysDir); err != nil {
logger.Debug("failed to notify user", "error", err)
}
return nil
}
sysModal.AddButtons(labels)
// load all chars
pages.AddPage(agentPage, sysModal, true, true)
updateStatusLine()
return nil
}
// cannot send msg in editMode or botRespMode
if event.Key() == tcell.KeyEscape && !editMode && !botRespMode {
position.SetText(fmt.Sprintf(indexLine, botRespMode, cfg.AssistantRole, activeChatName))
// read all text into buffer
msgText := textArea.GetText()
// TODO: check whose message was latest (user icon / assistant)
// in order to decide if assistant new icon is needed
nl := "\n"
prevText := textView.GetText(true)
// strings.LastIndex()
// newline is not needed is prev msg ends with one
if strings.HasSuffix(prevText, nl) {
nl = ""
}
if msgText != "" {
fmt.Fprintf(textView, "%s[-:-:b](%d) <%s>: [-:-:-]\n%s\n",
nl, len(chatBody.Messages), cfg.UserRole, msgText)
textArea.SetText("", true)
textView.ScrollToEnd()
colorText()
}
// update statue line
go chatRound(msgText, cfg.UserRole, textView, false)
return nil
}
if event.Key() == tcell.KeyPgUp || event.Key() == tcell.KeyPgDn {
currentF := app.GetFocus()
app.SetFocus(focusSwitcher[currentF])
return nil
}
if isASCII(string(event.Rune())) && !botRespMode {
return event
}
return event
})
}
|