获取session添加该会话消息数量参数

This commit is contained in:
junleea 2025-03-25 13:26:51 +08:00
parent 3d9dea3ea7
commit 2731a5dfec
3 changed files with 45 additions and 3 deletions

View File

@ -98,3 +98,20 @@ func FindMessageBySessionID(sessionID int) []Message {
} }
return messages return messages
} }
type SessionMessageCount struct {
SessionID int `gorm:"column:session_id"`
Count int `gorm:"column:count"`
}
// 获取属于用户的会话消息数量
func FindSessionMessageCountByUserID(userID int) []SessionMessageCount {
var sessionMessageCounts []SessionMessageCount
if proto.Config.SERVER_SQL_LOG {
//DB.Debug().Exec("Select session_id, count(*) as count from messages where to_id = ? or from_id=? group by session_id", userID,userID).Scan(&sessionMessageCounts)
DB.Debug().Table("messages").Select("session_id, count(*) as count").Where("to_id = ? or from_id= ?", userID, userID).Group("session_id").Scan(&sessionMessageCounts)
} else {
DB.Table("messages").Select("session_id, count(*) as count").Where("to_id = ? or from_id= ?", userID, userID).Group("session_id").Scan(&sessionMessageCounts)
}
return sessionMessageCounts
}

View File

@ -1,6 +1,9 @@
package proto package proto
import "github.com/ollama/ollama/api" import (
"github.com/ollama/ollama/api"
"gorm.io/gorm"
)
type AIQueueMessage struct { type AIQueueMessage struct {
Type string `json:"type"` //声明不同消息类型 Type string `json:"type"` //声明不同消息类型
@ -31,3 +34,11 @@ type ModelParam struct {
APISecret string `json:"apiSecret"` //应用密钥 APISecret string `json:"apiSecret"` //应用密钥
APIKey string `json:"apiKey"` //应用key APIKey string `json:"apiKey"` //应用key
} }
type SessionResponse struct {
gorm.Model
UserID int `gorm:"column:user_id"` //只能由用户创建
Name string `gorm:"column:name"`
Context []int `gorm:"column:context;type:json"` //会话上下文
MsgCount int `gorm:"column:msg_count"` //消息数量
}

View File

@ -19,9 +19,23 @@ func CreateSession(userID int, name string) (error, uint) {
} }
// 查找用户的会话列表 // 查找用户的会话列表
func FindSessionByUserID(userID int) []dao.Session { func FindSessionByUserID(userID int) []proto.SessionResponse {
sessions := dao.FindSessionByUserID(userID) sessions := dao.FindSessionByUserID(userID)
return sessions sessionsMsgCounts := dao.FindSessionMessageCountByUserID(userID)
smap := make(map[int]int)
for _, v := range sessionsMsgCounts {
smap[v.SessionID] = v.Count
}
var res []proto.SessionResponse
for _, v := range sessions {
var session proto.SessionResponse
session.ID = v.ID
session.Name = v.Name
session.UserID = v.UserID
session.MsgCount = smap[int(v.ID)]
res = append(res, session)
}
return res
} }
func FindSessionByID(id, userID int) (error, []dao.Session) { func FindSessionByID(id, userID int) (error, []dao.Session) {
session := dao.FindSessionByID(id) session := dao.FindSessionByID(id)