Selaa lähdekoodia

wip:空回复不能点击有用无用处理

wangkaiyue 2 vuotta sitten
vanhempi
commit
e9d9a9155b
1 muutettua tiedostoa jossa 11 lisäystä ja 5 poistoa
  1. 11 5
      internal/model/ws.go

+ 11 - 5
internal/model/ws.go

@@ -81,12 +81,12 @@ func (m *WsChat) Handle(ws *ghttp.WebSocket, msg []byte) {
 				reply, from = g.Cfg().MustGet(m.Ctx, "limit.errMsg").String(), -1
 			}
 		}
-
 		if from == Answer_ChatGPT {
 			return reply, res, 0, nil
 		}
 		if reply == "" {
 			reply = g.Cfg().MustGet(m.Ctx, "limit.emptyMsg").String()
+			errReply = g.Cfg().MustGet(m.Ctx, "limit.emptyMsg").String()
 		}
 		replyId := ChatHistory.Save(m.Ctx, &ChatRecord{
 			Content:    reply,
@@ -94,7 +94,7 @@ func (m *WsChat) Handle(ws *ghttp.WebSocket, msg []byte) {
 			Actions:    gconv.Int(If(errReply == "", 1, 0)),
 			QuestionId: questionId,
 			PersonId:   jSession.PositionId,
-			Item:       gconv.Int(If(errReply == "", from, -1)),
+			Item:       from,
 			CreateTime: time.Now().Format(date.Date_Full_Layout),
 		})
 		if replyId <= 0 {
@@ -116,25 +116,31 @@ func (m *WsChat) Handle(ws *ghttp.WebSocket, msg []byte) {
 		}
 	} else if res != nil {
 		buf, lastData := bufio.NewReader(res), &BufRes{}
+		isEmpty := true
 		for {
 			line, _, err := buf.ReadLine()
 			if err == io.EOF {
 				//放回链接池
 				ChatGptPool.Add()
-				finalReply := If(lastData.Response != "", lastData.Response, g.Cfg().MustGet(m.Ctx, "limit.emptyMsg").String()).(string)
+				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:    1,
+					Actions:    gconv.Int(If(isEmpty, 0, 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": finalReply, "isEnd": true}})
+				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})
+				}
 				break
 			}
 			if _, data := parseEventStream(line); data != nil {
+				isEmpty = false
 				data.Response = fsw.Repl(data.Response)
 				lastData = data
 				_ = ws.WriteJSON(g.Map{"error_code": 0, "error_msg": "", "data": g.Map{"reply": lastData.Response, "isEnd": false}})