重写数据获取与数据返回结构

This commit is contained in:
junleea 2025-03-22 17:11:42 +08:00
parent 5e17788a34
commit b81ab8412d
3 changed files with 25 additions and 19 deletions

View File

@ -101,7 +101,7 @@ func subscribeAndHandleIMMessages(ws *websocket.Conn, userId int) {
log.Println("Read error:", err)
break
}
var data proto.WSMessage
var data proto.WSMessageReq
err1 := json.Unmarshal(message, &data)
if err1 != nil {
log.Println("Error parsing JSON:", err1)
@ -132,17 +132,11 @@ func subscribeAndHandleIMMessages(ws *websocket.Conn, userId int) {
}()
go func() {
var data proto.WSMessage
//从redis订阅消息
for m := range ch {
msg := m.Payload // 获取消息,消息格式为json
if msg != "" {
data.Msg = msg
data.Type = "msg"
data.SessionID = int(sessionID)
data.ToID = 0
msg_, _ := json.Marshal(data)
err2 := ws.WriteMessage(websocket.TextMessage, msg_)
err2 := ws.WriteMessage(websocket.TextMessage, []byte(msg))
if err2 != nil {
// 发生错误,删除连接
clientsMux.Lock()

View File

@ -1,18 +1,27 @@
package proto
import "github.com/ollama/ollama/api"
type AIQueueMessage struct {
Type string `json:"type"` //声明不同消息类型
ID uint `json:"id"` //消息id
Msg string `json:"msg"` //原始json消息
Type string `json:"type"` //声明不同消息类型
ID uint `json:"id"` //消息id
Msg api.GenerateResponse `json:"msg"` //原始消息
}
type WSMessage struct {
Type string `json:"type"` //接收及发送消息类型
Msg string `json:"msg"` //消息内容,只进行转发,不做处理
SessionID int `json:"session_id"` //应用层会话id
ToID int `json:"to_id"` //接收者id
Type string `json:"type"` //接收及发送消息类型
Msg AIQueueMessage `json:"msg"` //消息内容,只进行转发,不做处理
SessionID int `json:"session_id"` //应用层会话id
ToID int `json:"to_id"` //接收者id
}
type ModelParam struct {
Model string `json:"model"` //模型名称
}
type WSMessageReq struct {
Type string `json:"type"` //接收及发送消息类型
Msg string `json:"msg"` //消息内容,只进行转发,不做处理
SessionID int `json:"session_id"` //应用层会话id
ToID int `json:"to_id"` //接收者id
}

View File

@ -79,7 +79,7 @@ func CreateGeneralMessageService(fromID, toID, msgType, sessionID int, msg strin
var client *api.Client
var actxMap map[int][]int
func WSReceiveMessageService(userID, sessionID int, channel string, msg proto.WSMessage) (error, uint) {
func WSReceiveMessageService(userID, sessionID int, channel string, msg proto.WSMessageReq) (error, uint) {
var resErr error
var resID uint
//处理消息
@ -101,10 +101,10 @@ func WSReceiveMessageService(userID, sessionID int, channel string, msg proto.WS
ctx := context.Background()
robotMsg := ""
var aiMsg proto.AIQueueMessage
var data proto.WSMessage
respFunc := func(resp api.GenerateResponse) error {
aiMsg.Type = "ollama"
respStr, _ := json.Marshal(resp)
aiMsg.Msg = string(respStr)
aiMsg.Msg = resp
robotMsg += resp.Response
if resp.Done { //该消息完成
actx = resp.Context
@ -121,7 +121,10 @@ func WSReceiveMessageService(userID, sessionID int, channel string, msg proto.WS
resID = msgID
}
//发送消息
aiMsgStr, _ := json.Marshal(aiMsg)
data.Msg = aiMsg
data.SessionID = sessionID
data.ToID = userID
aiMsgStr, _ := json.Marshal(data)
worker.Publish(channel, string(aiMsgStr), time.Second*60)
return nil
}