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
|
package main
import (
"fmt"
"path"
"strconv"
"time"
"unicode"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
var (
botRespMode = false
editMode = false
botMsg = "no"
selectedIndex = int(-1)
indexLine = "Esc: send msg; PgUp/Down: switch focus; F1: manage chats; F2: regen last; F3:delete last msg; F4: edit msg; F5: toggle system; F6: interrupt bot resp; Row: [yellow]%d[white], Column: [yellow]%d; bot resp mode: %v"
focusSwitcher = map[tview.Primitive]tview.Primitive{}
)
func isASCII(s string) bool {
for i := 0; i < len(s); i++ {
if s[i] > unicode.MaxASCII {
return false
}
}
return true
}
func main() {
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)
updateStatusLine := func() {
fromRow, fromColumn, toRow, toColumn := textArea.GetCursor()
if fromRow == toRow && fromColumn == toColumn {
position.SetText(fmt.Sprintf(indexLine, fromRow, fromColumn, botRespMode))
} else {
position.SetText(fmt.Sprintf("Esc: send msg; PgUp/Down: switch focus; F1: manage chats; F2: regen last; F3:delete last msg; F4: edit msg; F5: toggle system; F6: interrupt bot resp; Row: [yellow]%d[white], Column: [yellow]%d[white] - [red]To[white] Row: [yellow]%d[white], To Column: [yellow]%d; bot resp mode: %v", fromRow, fromColumn, toRow, toColumn, botRespMode))
}
}
chatOpts := []string{"cancel", "new"}
fList, err := loadHistoryChats()
if err != nil {
panic(err)
}
chatOpts = append(chatOpts, fList...)
chatActModal := tview.NewModal().
SetText("Chat actions:").
AddButtons(chatOpts).
SetDoneFunc(func(buttonIndex int, buttonLabel string) {
switch buttonLabel {
case "new":
// set chat body
chatBody.Messages = defaultStarter
textView.SetText(chatToText(showSystemMsgs))
activeChatName = path.Join(historyDir, fmt.Sprintf("%d_chat.json", time.Now().Unix()))
pages.RemovePage("history")
return
// set text
case "cancel":
pages.RemovePage("history")
return
default:
fn := buttonLabel
history, err := loadHistoryChat(fn)
if err != nil {
logger.Error("failed to read history file", "filename", fn)
pages.RemovePage("history")
return
}
chatBody.Messages = history
textView.SetText(chatToText(showSystemMsgs))
activeChatName = fn
pages.RemovePage("history")
return
}
})
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 == "" {
notifyUser("edit", "no edit provided")
pages.RemovePage("editArea")
editMode = false
return nil
}
chatBody.Messages[selectedIndex].Content = editedMsg
// change textarea
textView.SetText(chatToText(showSystemMsgs))
pages.RemovePage("editArea")
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("getIndex")
return
})
indexPickWindow.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
si := indexPickWindow.GetText()
selectedIndex, err = strconv.Atoi(si)
if err != nil {
logger.Error("failed to convert provided index", "error", err, "si", si)
}
if len(chatBody.Messages) <= selectedIndex && selectedIndex < 0 {
logger.Warn("chosen index is out of bounds", "index", selectedIndex)
return nil
}
m := chatBody.Messages[selectedIndex]
if editMode && event.Key() == tcell.KeyEnter {
pages.AddPage("editArea", editArea, true, true)
editArea.SetText(m.Content, true)
}
if !editMode && event.Key() == tcell.KeyEnter {
// TODO: add notification that text was copied
copyToClipboard(m.Content)
notification := fmt.Sprintf("msg '%s' was copied to the clipboard", m.Content[:30])
notifyUser("copied", notification)
}
return event
})
//
textArea.SetMovedFunc(updateStatusLine)
updateStatusLine()
textView.SetText(chatToText(showSystemMsgs))
textView.ScrollToEnd()
app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
if event.Key() == tcell.KeyF1 {
// fList, err := listHistoryFiles(historyDir)
fList, err := loadHistoryChats()
if err != nil {
panic(err)
}
chatOpts = append(chatOpts, fList...)
pages.AddPage("history", chatActModal, true, true)
return nil
}
if event.Key() == tcell.KeyF2 {
// regen last msg
chatBody.Messages = chatBody.Messages[:len(chatBody.Messages)-1]
textView.SetText(chatToText(showSystemMsgs))
go chatRound("", userRole, textView)
return nil
}
if event.Key() == tcell.KeyF3 {
// delete last msg
chatBody.Messages = chatBody.Messages[:len(chatBody.Messages)-1]
textView.SetText(chatToText(showSystemMsgs))
botRespMode = false // hmmm; is that correct?
return nil
}
if event.Key() == tcell.KeyF4 {
// edit msg
editMode = true
pages.AddPage("getIndex", indexPickWindow, true, true)
return nil
}
if event.Key() == tcell.KeyF5 {
// switch showSystemMsgs
showSystemMsgs = !showSystemMsgs
textView.SetText(chatToText(showSystemMsgs))
}
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]
copyToClipboard(m.Content)
notification := fmt.Sprintf("msg '%s' was copied to the clipboard", m.Content[:30])
notifyUser("copied", notification)
return nil
}
if event.Key() == tcell.KeyF8 {
// copy msg to clipboard
editMode = false
pages.AddPage("getIndex", indexPickWindow, true, true)
return nil
}
// cannot send msg in editMode or botRespMode
if event.Key() == tcell.KeyEscape && !editMode && !botRespMode {
fromRow, fromColumn, _, _ := textArea.GetCursor()
position.SetText(fmt.Sprintf(indexLine, fromRow, fromColumn, botRespMode))
// read all text into buffer
msgText := textArea.GetText()
if msgText != "" {
fmt.Fprintf(textView, "\n(%d) <user>: %s\n", len(chatBody.Messages), msgText)
textArea.SetText("", true)
textView.ScrollToEnd()
}
// update statue line
go chatRound(msgText, userRole, textView)
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 {
// botRespMode = false
// fromRow, fromColumn, _, _ := textArea.GetCursor()
// position.SetText(fmt.Sprintf(indexLine, fromRow, fromColumn, botRespMode))
return event
}
return event
})
pages.AddPage("main", flex, true, true)
if err := app.SetRoot(pages,
true).EnableMouse(true).Run(); err != nil {
panic(err)
}
}
|