/
convo.go
302 lines (273 loc) · 8.64 KB
/
convo.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
package convo
import (
"context"
"encoding/json"
"fmt"
"net/url"
"os"
"os/signal"
"strings"
"sync"
"syscall"
"time"
cache "github.com/go-pkgz/expirable-cache/v2"
"github.com/gorilla/websocket"
"github.com/rkonfj/hu60bot/pkg/hu60"
"github.com/sashabaranov/go-openai"
"github.com/sirupsen/logrus"
)
type ConversationManager struct {
hu60Client *hu60.Client
openaiClient *openai.Client
options ConversationOptions
conversationContext cache.Cache[string, []openai.ChatCompletionMessage]
openaiTokensUsage openai.Usage
botReadyTask sync.WaitGroup
botUid int
botSid string
botMsgChan chan hu60.Msg
}
type SeWsEvent struct {
Event string `json:"event"`
Data any `json:"data"`
}
type WsEvent struct {
Event string `json:"event"`
Data WsHu60Msg `json:"data"`
}
type WsHu60Msg struct {
ID int `json:"id"`
ByUID int `json:"byuid"`
ToUID int `json:"touid"`
Type int `json:"type"`
Read int `json:"isread"`
Content string `json:"content"`
}
func NewConversationManager(options ConversationOptions) *ConversationManager {
openaiCfg := openai.DefaultConfig(options.OpenaiToken)
openaiCfg.BaseURL = options.OpenaiAPIURL
cm := ConversationManager{
hu60Client: hu60.NewClient(options.Hu60APIURL),
openaiClient: openai.NewClientWithConfig(openaiCfg),
options: options,
conversationContext: cache.NewCache[string, []openai.ChatCompletionMessage]().
WithMaxKeys(4096).WithTTL(time.Second * 30),
botReadyTask: sync.WaitGroup{},
botMsgChan: make(chan hu60.Msg, 64),
}
cm.updateConversationStats()
cm.botReadyTask.Add(1)
return &cm
}
func (cm *ConversationManager) Run() {
defer close(cm.botMsgChan)
resp, err := cm.hu60Client.Login(
context.Background(),
hu60.LoginRequest{Username: cm.options.Hu60Username, Password: cm.options.Hu60Password},
)
if err != nil {
logrus.Fatal(err)
}
cm.botSid = resp.Sid
cm.botUid = resp.Uid
cm.botReadyTask.Done()
cm.processMissedMsgs()
for msg := range cm.botMsgChan {
logrus.Debug("watched msg: ", msg)
if msg.Content[0].Type != "atMsg" {
logrus.Warn("skip non @ msg")
continue
}
conversationKey := fmt.Sprintf("%d", msg.ByUID)
content := getTextMessage(msg.Content)
answer, isNewConvo, err := cm.Ask(content, conversationKey)
if err != nil {
logrus.Error("askAI error: ", err.Error())
answerHu60(cm.hu60Client, resp.Sid, msg, err.Error(), isNewConvo)
continue
}
answerHu60(cm.hu60Client, resp.Sid, msg, answer, isNewConvo)
}
}
func (cm *ConversationManager) OnCanalStartFailed() {
go func() {
// websocket
wsurl, err := url.Parse(cm.options.Hu60WSURL)
if err != nil {
logrus.Fatal("invalid hu60wap6's websocket endpoint url")
}
cm.botReadyTask.Wait()
err = cm.connectWs()
if err == nil {
return
}
logrus.Infof("bot connect websocket (%s) error, fallback to http loop", wsurl.String())
logrus.Infof("bot watching for chat now. sid is %s, conversation window is %s", cm.botSid, cm.options.ConversationWindow.String())
// http loop
cm.hu60Client.WatchMsg(context.Background(), cm.botSid, func(msg hu60.Msg) {
cm.botMsgChan <- msg
})
}()
}
func (cm *ConversationManager) OnCanalStartSucceed() {
cm.startTheMarkMsgReadTask()
logrus.Infof("bot watching for chat now. conversation window is %s", cm.options.ConversationWindow.String())
}
func (cm *ConversationManager) processMissedMsgs() {
resp, err := cm.hu60Client.ListMsg(context.Background(), cm.botSid, hu60.ListMsgOptions{PageSize: 64})
if err != nil {
logrus.Warnf("missed msgs process failed. (%w)", err)
return
}
logrus.Debugf("bot processed %d missed msgs", len(resp.MsgList))
for i, j := 0, len(resp.MsgList)-1; i < j; i, j = i+1, j-1 {
resp.MsgList[i], resp.MsgList[j] = resp.MsgList[j], resp.MsgList[i]
}
for _, msg := range resp.MsgList {
cm.botMsgChan <- msg
}
}
func (cm *ConversationManager) startTheMarkMsgReadTask() {
go func() {
for {
time.Sleep(time.Minute)
r, err := cm.hu60Client.SetMsgIsRead(context.Background(), cm.botSid, 1)
if err != nil {
logrus.Trace("hu60.setMsgIsRead error: ", err)
}
logrus.Trace("hu60.setMsgIsRead response: ", r)
}
}()
go func() {
signals := make(chan os.Signal, 1)
defer close(signals)
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
for sig := range signals {
r, _ := cm.hu60Client.SetMsgIsRead(context.Background(), cm.botSid, 1)
logrus.Infof("signal: %s, mark all at msgs is read (%d msgs). done", sig, r.Result.Update)
os.Exit(0)
}
}()
}
func (cm *ConversationManager) startHeartbeatTask(conn *websocket.Conn) {
go func(c *websocket.Conn) {
for {
time.Sleep(65 * time.Second)
logrus.Trace(`bot ping... {"action": "ping"}`)
if err := c.WriteMessage(websocket.TextMessage, []byte(`{"action": "ping"}`)); err != nil {
c.Close()
break
}
}
}(conn)
}
func (cm *ConversationManager) connectWs() error {
conn, _, err := websocket.DefaultDialer.Dial(fmt.Sprintf("%s?_sid=%s", cm.options.Hu60WSURL, cm.botSid), nil)
if err == nil {
logrus.Infof("bot connect websocket (%s) succeed", cm.options.Hu60WSURL)
logrus.Infof("bot watching for chat now. sid is %s, conversation window is %s",
cm.botSid, cm.options.ConversationWindow.String())
cm.startHeartbeatTask(conn)
cm.startTheMarkMsgReadTask()
defer conn.Close()
for {
_, message, err := conn.ReadMessage()
if err != nil {
logrus.Error("websocket error: ", err)
time.Sleep(5 * time.Second)
return cm.connectWs()
}
var seEvent SeWsEvent
err = json.Unmarshal(message, &seEvent)
if err != nil {
logrus.Error("connectWs.ReadMessage: invalid hu60wap6 ws event format: ", err)
continue
}
if seEvent.Event != "msg" {
if seEvent.Event == "ping" {
logrus.Trace("bot pong ", string(message))
}
continue
}
var event WsEvent
err = json.Unmarshal(message, &event)
if err != nil {
logrus.Error("connectWs.ReadMessage: invalid hu60wap6 ws event format: ", err)
continue
}
if event.Data.Type != 1 {
continue
}
var c []hu60.MsgContent
err = json.Unmarshal([]byte(event.Data.Content), &c)
if err != nil {
logrus.Error("connectWs.ReadMessage: invalid hu60wap6 msg content format: ", err)
continue
}
cm.botMsgChan <- hu60.Msg{
ID: event.Data.ID,
ByUID: event.Data.ByUID,
ToUID: event.Data.ToUID,
Type: event.Data.Type,
Read: event.Data.Read,
Content: c,
}
}
}
return err
}
func (cm *ConversationManager) OnHu60Msg(msg hu60.Msg) {
if msg.ToUID == cm.botUid {
cm.botMsgChan <- msg
return
}
}
func (cm *ConversationManager) Ask(words, conversationKey string) (answer string, isNewConvo bool, err error) {
logrus.Debugf("convoKey: %s, askAI request: %s", conversationKey, words)
var conversationMsgs []openai.ChatCompletionMessage
if msgs, ok := cm.conversationContext.Get(conversationKey); ok {
conversationMsgs = msgs
} else {
isNewConvo = true
conversationMsgs = []openai.ChatCompletionMessage{}
}
conversationMsgs = append(conversationMsgs, openai.ChatCompletionMessage{
Role: openai.ChatMessageRoleUser,
Content: words,
})
answer, usage, err := askAI(cm.openaiClient, cm.options.OpenaiModel, cm.options.OpenaiRequestTimeout, conversationMsgs)
if err != nil {
if strings.Contains(err.Error(), "reduce the length") {
err = fmt.Errorf("答案丢失!会话长度触限,下一次对话将开启新会话。(%s)", err.Error())
cm.MarkExpired(conversationKey)
}
return
}
logrus.Debugf("convoKey: %s, askAI response: %s", conversationKey, answer)
conversationMsgs = append(conversationMsgs, openai.ChatCompletionMessage{
Role: openai.ChatMessageRoleAssistant,
Content: answer,
})
cm.conversationContext.Set(conversationKey, conversationMsgs, cm.options.ConversationWindow)
cm.openaiTokensUsage.PromptTokens += usage.PromptTokens
cm.openaiTokensUsage.CompletionTokens += usage.CompletionTokens
cm.openaiTokensUsage.TotalTokens += usage.TotalTokens
cm.updateConversationStats()
return
}
func (cm *ConversationManager) MarkExpired(conversationKey string) {
cm.conversationContext.Invalidate(conversationKey)
}
func (cm *ConversationManager) updateConversationStats() {
stats := fmt.Sprintf(`conversation: %d/%d, openai tokens usage: %d/%d/%d`,
cm.conversationContext.Stat().Added, cm.conversationContext.Stat().Evicted,
cm.openaiTokensUsage.PromptTokens, cm.openaiTokensUsage.CompletionTokens, cm.openaiTokensUsage.TotalTokens)
logrus.Debug(stats)
stats += "\n\n"
stats += strings.Join(cm.conversationContext.Keys(), "\n")
stats += "\n"
err := os.WriteFile("conversation.stat.txt", []byte(stats), 0644)
if err != nil {
logrus.Error("update ConversationStats error: ", err.Error())
}
}