2026-06-14 17:52:21 +08:00
|
|
|
|
package store
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"context"
|
|
|
|
|
|
"errors"
|
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/hhs/camtalk/internal/models"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
|
// ErrMessageNotFound 消息不存在。
|
|
|
|
|
|
ErrMessageNotFound = errors.New("message not found")
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// MessageRepository 消息持久化接口。
|
|
|
|
|
|
type MessageRepository interface {
|
|
|
|
|
|
// SaveMessage 保存一条消息。
|
|
|
|
|
|
SaveMessage(ctx context.Context, sessionID string, msg models.Message, tokensUsed int) error
|
|
|
|
|
|
|
|
|
|
|
|
// GetMessages 获取会话的消息列表(分页,按 created_at 升序)。
|
|
|
|
|
|
// beforeID 为 0 时从最新开始查询。
|
|
|
|
|
|
GetMessages(ctx context.Context, sessionID string, limit int, beforeID int64) ([]StoredMessage, error)
|
|
|
|
|
|
|
|
|
|
|
|
// GetLastMessage 获取会话的最后一条消息。
|
|
|
|
|
|
GetLastMessage(ctx context.Context, sessionID string) (*StoredMessage, error)
|
|
|
|
|
|
|
|
|
|
|
|
// GetMessageCount 获取会话的消息总数。
|
|
|
|
|
|
GetMessageCount(ctx context.Context, sessionID string) (int, error)
|
2026-06-14 17:58:42 +08:00
|
|
|
|
|
|
|
|
|
|
// GetSessionMessageStats 批量查询多个会话的消息统计(last_message + message_count)。
|
|
|
|
|
|
// 返回的 map key 为 sessionID,仅包含有消息的会话。
|
|
|
|
|
|
GetSessionMessageStats(ctx context.Context, sessionIDs []string) (map[string]SessionMessageStats, error)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// SessionMessageStats 单个会话的消息统计(SQL 聚合查询结果)。
|
|
|
|
|
|
type SessionMessageStats struct {
|
|
|
|
|
|
LastMessage string
|
|
|
|
|
|
MessageCount int
|
2026-06-14 17:52:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// StoredMessage 持久化消息模型(store 层)。
|
|
|
|
|
|
type StoredMessage struct {
|
|
|
|
|
|
ID int64 `json:"id"`
|
|
|
|
|
|
SessionID string `json:"-"`
|
|
|
|
|
|
Role string `json:"role"`
|
|
|
|
|
|
Content string `json:"content"`
|
|
|
|
|
|
TokensUsed int `json:"tokens_used"`
|
|
|
|
|
|
CreatedAt time.Time `json:"created_at"`
|
|
|
|
|
|
}
|