-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterface.go
147 lines (125 loc) · 3.48 KB
/
interface.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
package main
import (
"context"
"fmt"
"strings"
"time"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
var (
chatbotOutput *tview.TextView
userInput *tview.TextArea
app *tview.Application
typingCtx context.Context
cancelTyping context.CancelFunc
)
func setupUI() {
typingCtx, cancelTyping = context.WithCancel(context.Background())
tview.Styles.PrimitiveBackgroundColor = tcell.ColorDefault
app = tview.NewApplication()
userInput = tview.NewTextArea()
chatbotOutput = tview.NewTextView()
userInput.SetBorder(true).SetBorderColor(tcell.ColorGreen)
userInput.SetBackgroundColor(tcell.ColorDefault)
userInput.SetTextStyle(tcell.StyleDefault)
chatbotOutput.SetBorder(true).SetBorderColor(tcell.ColorGreen)
chatbotOutput.SetBackgroundColor(tcell.ColorDefault)
chatbotOutput.SetDynamicColors(true)
chatbotOutput.SetDisabled(true)
userInput.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
if event.Key() == tcell.KeyEnter {
currentText := userInput.GetText()
sf.Reset()
if handleCmd(currentText) {
return nil
}
}
return event
})
app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
if event.Key() == tcell.KeyBacktab {
if userInput.HasFocus() {
app.SetFocus(chatbotOutput)
} else {
app.SetFocus(userInput)
}
return nil
}
return event
})
flex := tview.NewFlex().SetDirection(tview.FlexRow).
AddItem(userInput, 0, 1, true).
AddItem(chatbotOutput, 0, 5, false)
app.SetRoot(flex, true).EnableMouse(true)
if err := app.Run(); err != nil {
panic(err)
}
}
func handleCmd(input string) bool {
if strings.Contains(input, config.SubmitCmd) {
input = strings.Replace(input, "!fin", "", -1)
processInput(input)
userInput.SetText("", true)
} else if strings.Contains(input, config.ClearCmd) {
cancelTyping()
time.Sleep(time.Second * 1)
typingCtx, cancelTyping = context.WithCancel(context.Background())
chatbotOutput.SetText("")
userInput.SetText("", true)
} else if strings.Contains(input, config.PermCmd) {
input = strings.Replace(input, "!perm", "", -1)
session.Permanent = input
userInput.SetText("", true)
} else if strings.Contains(input, config.ResetCmd) {
session.Dynamic = []ChatMessage{}
userInput.SetText("", true)
} else if strings.Contains(input, config.ExitCmd) {
app.Stop()
} else {
return false
}
return true
}
func writeChatbotMessage(char rune, color string) {
coloredChar := fmt.Sprintf("[%s]%s[white]", color, string(char))
chatbotOutput.Write([]byte(string(coloredChar)))
app.Draw()
time.Sleep(time.Millisecond * 10)
}
func processInput(userInput string) {
var apiOutput = make(chan string, 10000)
session.Dynamic = append(session.Dynamic, ChatMessage{
Role: "user",
Content: userInput,
})
go streamCompletion(config, session, apiOutput)
go typeResponse(apiOutput)
}
func typeResponse(apiOutput chan string) {
var response strings.Builder
for token := range apiOutput {
select {
case <-typingCtx.Done():
return
default:
response.WriteString(token)
for _, char := range token {
sf.Print(char)
}
}
}
sf.Print('\n')
sf.Print('\n')
sf.Print('\n')
sf.Print('\n')
if config.Context > 0 {
session.Dynamic = append(session.Dynamic, ChatMessage{
Role: "assistant",
Content: response.String(),
})
} else {
session.Dynamic = []ChatMessage{}
}
updateSession()
}