|
@@ -51,10 +51,10 @@ func (m *WsChat) Handle(ws *ghttp.WebSocket, msg []byte) {
|
|
|
PersonId: jSession.PositionId,
|
|
|
CreateTime: time.Now().Format(date.Date_Full_Layout),
|
|
|
})
|
|
|
- content, buf, replyId, errMsg := func() (string, *bufio.Reader, int64, error) {
|
|
|
+ content, res, replyId, errMsg := func() (string, io.ReadCloser, int64, error) {
|
|
|
var (
|
|
|
err error
|
|
|
- buf *bufio.Reader
|
|
|
+ res io.ReadCloser
|
|
|
reply string
|
|
|
)
|
|
|
errReply := func() string {
|
|
@@ -75,7 +75,7 @@ func (m *WsChat) Handle(ws *ghttp.WebSocket, msg []byte) {
|
|
|
if errReply != "" {
|
|
|
reply, from = errReply, -1
|
|
|
} else {
|
|
|
- reply, buf, from, err = Question.DetailQuestion(m.Ctx, req)
|
|
|
+ reply, res, from, err = Question.DetailQuestion(m.Ctx, req)
|
|
|
if err != nil {
|
|
|
g.Log().Error(m.Ctx, "问答异常", err)
|
|
|
reply, from = g.Cfg().MustGet(m.Ctx, "limit.errMsg").String(), -1
|
|
@@ -83,7 +83,7 @@ func (m *WsChat) Handle(ws *ghttp.WebSocket, msg []byte) {
|
|
|
}
|
|
|
|
|
|
if from == Answer_ChatGPT {
|
|
|
- return reply, buf, 0, nil
|
|
|
+ return reply, res, 0, nil
|
|
|
}
|
|
|
if reply == "" {
|
|
|
reply, from = g.Cfg().MustGet(m.Ctx, "limit.emptyMsg").String(), -1
|
|
@@ -105,39 +105,45 @@ func (m *WsChat) Handle(ws *ghttp.WebSocket, msg []byte) {
|
|
|
}
|
|
|
return reply, nil, replyId, nil
|
|
|
}()
|
|
|
+ if res != nil {
|
|
|
+ defer res.Close()
|
|
|
+ }
|
|
|
if from != Answer_ChatGPT {
|
|
|
if errMsg != nil {
|
|
|
_ = ws.WriteJSON(g.Map{"error_code": -1, "error_msg": errMsg.Error(), "data": nil})
|
|
|
} else {
|
|
|
_ = ws.WriteJSON(g.Map{"error_code": 0, "error_msg": "", "data": g.Map{"id": encrypt.SE.Encode2Hex(fmt.Sprintf("%d", replyId)), "reply": content, "isEnd": true}})
|
|
|
}
|
|
|
- } else if buf != nil {
|
|
|
+ } else if res != nil {
|
|
|
+ buf, lastData := bufio.NewReader(res), &BufRes{}
|
|
|
for {
|
|
|
line, _, err := buf.ReadLine()
|
|
|
if err == io.EOF {
|
|
|
+ replyId := ChatHistory.Save(m.Ctx, &ChatRecord{
|
|
|
+ Content: lastData.Response,
|
|
|
+ Type: 2,
|
|
|
+ Actions: 1,
|
|
|
+ QuestionId: questionId,
|
|
|
+ PersonId: jSession.PositionId,
|
|
|
+ Item: Answer_ChatGPT,
|
|
|
+ CreateTime: time.Now().Format(date.Date_Full_Layout),
|
|
|
+ })
|
|
|
+ _ = ws.WriteJSON(g.Map{"error_code": 0, "error_msg": "", "data": g.Map{"id": encrypt.SE.Encode2Hex(fmt.Sprintf("%d", replyId)), "reply": lastData.Response, "isEnd": lastData.Finished}})
|
|
|
break
|
|
|
}
|
|
|
if _, data := parseEventStream(line); data != nil {
|
|
|
data.Response = fsw.Repl(data.Response)
|
|
|
- if data.Finished {
|
|
|
- replyId := ChatHistory.Save(m.Ctx, &ChatRecord{
|
|
|
- Content: data.Response,
|
|
|
- Type: 2,
|
|
|
- Actions: 1,
|
|
|
- QuestionId: questionId,
|
|
|
- PersonId: jSession.PositionId,
|
|
|
- Item: Answer_ChatGPT,
|
|
|
- CreateTime: time.Now().Format(date.Date_Full_Layout),
|
|
|
- })
|
|
|
- _ = ws.WriteJSON(g.Map{"error_code": 0, "error_msg": "", "data": g.Map{"id": encrypt.SE.Encode2Hex(fmt.Sprintf("%d", replyId)), "reply": data.Response, "isEnd": data.Finished}})
|
|
|
- } else {
|
|
|
- _ = ws.WriteJSON(g.Map{"error_code": 0, "error_msg": "", "data": g.Map{"reply": data.Response, "isEnd": data.Finished}})
|
|
|
- }
|
|
|
+ lastData = data
|
|
|
+ _ = ws.WriteJSON(g.Map{"error_code": 0, "error_msg": "", "data": g.Map{"reply": lastData.Response, "isEnd": false}})
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+func saveAndReturnMap() {
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
func parseEventStream(line []byte) (event string, date *BufRes) {
|
|
|
// 如果行以 "event:" 开头,表示这是一个事件的标识符
|
|
|
if len(line) > 6 && string(line[:6]) == "event:" {
|