|
@@ -14,6 +14,7 @@ import (
|
|
|
"github.com/gogf/gf/v2/net/ghttp"
|
|
|
"github.com/gogf/gf/v2/util/gconv"
|
|
|
"io"
|
|
|
+ "strings"
|
|
|
"time"
|
|
|
)
|
|
|
|
|
@@ -119,33 +120,31 @@ func (m *WsChat) Handle(ws *ghttp.WebSocket, msg []byte) {
|
|
|
isEmpty := true
|
|
|
for {
|
|
|
line, _, err := buf.ReadLine()
|
|
|
- if err == io.EOF {
|
|
|
- //放回链接池
|
|
|
- ChatGptPool.Add()
|
|
|
- finalReply := If(isEmpty, g.Cfg().MustGet(m.Ctx, "limit.emptyMsg").String(), lastData.Response).(string)
|
|
|
- replyId := ChatHistory.Save(m.Ctx, &ChatRecord{
|
|
|
- Content: finalReply,
|
|
|
- Type: 2,
|
|
|
- Actions: gconv.Int(If(isEmpty, 0, 1)),
|
|
|
- QuestionId: questionId,
|
|
|
- PersonId: jSession.PositionId,
|
|
|
- Item: Answer_ChatGPT,
|
|
|
- CreateTime: time.Now().Format(date.Date_Full_Layout),
|
|
|
- })
|
|
|
- if !isEmpty {
|
|
|
- _ = ws.WriteJSON(g.Map{"error_code": 0, "error_msg": "", "data": g.Map{"id": encrypt.SE.Encode2Hex(fmt.Sprintf("%d", replyId)), "reply": finalReply, "isEnd": true}})
|
|
|
- } else {
|
|
|
- _ = ws.WriteJSON(g.Map{"error_code": -1, "error_msg": finalReply})
|
|
|
- }
|
|
|
+ if err == nil {
|
|
|
break
|
|
|
}
|
|
|
- if _, data := parseEventStream(line); data != nil {
|
|
|
- isEmpty = false
|
|
|
+ if _, data := parseEventStream(line); data != nil && strings.TrimSpace(data.Response) != "" {
|
|
|
data.Response = fsw.Repl(data.Response)
|
|
|
- lastData = data
|
|
|
+ lastData, isEmpty = data, false
|
|
|
_ = ws.WriteJSON(g.Map{"error_code": 0, "error_msg": "", "data": g.Map{"reply": lastData.Response, "isEnd": false}})
|
|
|
}
|
|
|
}
|
|
|
+ ChatGptPool.Add() //放回链接池
|
|
|
+ finalReply := If(isEmpty, g.Cfg().MustGet(m.Ctx, "limit.emptyMsg").String(), lastData.Response).(string)
|
|
|
+ replyId := ChatHistory.Save(m.Ctx, &ChatRecord{
|
|
|
+ Content: finalReply,
|
|
|
+ Type: 2,
|
|
|
+ Actions: gconv.Int(If(isEmpty, 0, 1)),
|
|
|
+ QuestionId: questionId,
|
|
|
+ PersonId: jSession.PositionId,
|
|
|
+ Item: Answer_ChatGPT,
|
|
|
+ CreateTime: time.Now().Format(date.Date_Full_Layout),
|
|
|
+ })
|
|
|
+ if !isEmpty {
|
|
|
+ _ = ws.WriteJSON(g.Map{"error_code": 0, "error_msg": "", "data": g.Map{"id": encrypt.SE.Encode2Hex(fmt.Sprintf("%d", replyId)), "reply": finalReply, "isEnd": true}})
|
|
|
+ } else {
|
|
|
+ _ = ws.WriteJSON(g.Map{"error_code": -1, "error_msg": finalReply})
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|