storage

package
v0.21.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 6, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotFound          = errors.New("message not found")
	ErrSessionNotFound   = errors.New("session not found")
	ErrInvalidMessage    = errors.New("invalid message")
	ErrStorageNotEnabled = errors.New("storage not enabled")
	ErrConnectionFailed  = errors.New("database connection failed")
	ErrQueryFailed       = errors.New("query execution failed")
	ErrStoreFailed       = errors.New("message store failed")
	ErrInvalidConfig     = errors.New("invalid storage configuration")
	ErrUnsupportedType   = errors.New("unsupported storage type")
	ErrSessionClosed     = errors.New("storage session closed")
	ErrTransactionFailed = errors.New("transaction failed")
)

Common storage errors

Functions

func BackupStorage

func BackupStorage(store ChatAppMessageStore, backupPath string) error

BackupStorage 备份存储

func BuildSessionID

func BuildSessionID(platform, userID, channelID string) string

BuildSessionID 构建会话ID

func ExportToJSON

func ExportToJSON(store ChatAppMessageStore, outputPath string, query *MessageQuery) error

ExportToJSON 将消息导出为 JSON

func FormatTimestamp

func FormatTimestamp(t time.Time) string

FormatTimestamp 格式化时间戳

func GenerateProviderSessionID

func GenerateProviderSessionID() string

GenerateProviderSessionID 生成 Provider 会话ID

func ImportFromJSON

func ImportFromJSON(store ChatAppMessageStore, inputPath string) (int, error)

ImportFromJSON 从 JSON 导入消息

func IsConfigError

func IsConfigError(err error) bool

IsConfigError 检查是否为配置错误

func IsConnectionError

func IsConnectionError(err error) bool

IsConnectionError 检查是否为连接错误

func IsNotFound

func IsNotFound(err error) bool

IsNotFound 检查是否为未找到错误

func MaskSensitiveData

func MaskSensitiveData(data string) string

MaskSensitiveData 脱敏敏感数据

func ParseMessageType

func ParseMessageType(s string) string

ParseMessageType 解析消息类型

func SanitizeContent

func SanitizeContent(content string, maxLength int) string

SanitizeContent 清理消息内容

func TruncateForLog

func TruncateForLog(content string, maxLen int) string

TruncateForLog 截断日志内容

func ValidateMessage

func ValidateMessage(msg *ChatAppMessage) error

ValidateMessage 验证消息必填字段

Types

type ChatAppMessage

type ChatAppMessage struct {
	ID                string
	ChatSessionID     string
	ChatPlatform      string
	ChatUserID        string
	ChatBotUserID     string
	ChatChannelID     string
	ChatThreadID      string
	EngineSessionID   uuid.UUID
	EngineNamespace   string
	ProviderSessionID string
	ProviderType      string
	MessageType       types.MessageType
	FromUserID        string
	FromUserName      string
	ToUserID          string
	Content           string
	Metadata          map[string]any
	CreatedAt         time.Time
	UpdatedAt         time.Time
	Deleted           bool
	DeletedAt         *time.Time
}

ChatAppMessage 存储层消息实体

type ChatAppMessageStore

type ChatAppMessageStore interface {
	ReadOnlyStore
	WriteOnlyStore
	SessionStore
	Initialize(ctx context.Context) error
	Close() error
	Name() string
	Version() string
}

ChatAppMessageStore 完整接口

type Check

type Check struct {
	Status  string        `json:"status"` // pass, fail, warn
	Message string        `json:"message"`
	Latency time.Duration `json:"latency"`
}

Check 单项检查结果

type ConfigLoader

type ConfigLoader struct {
	// contains filtered or unexported fields
}

ConfigLoader 配置加载器

func NewConfigLoader

func NewConfigLoader(path string) *ConfigLoader

NewConfigLoader 创建配置加载器

func (*ConfigLoader) LoadStorageConfig

func (l *ConfigLoader) LoadStorageConfig() (*StorageConfig, error)

LoadStorageConfig 从 YAML 文件加载存储配置

type DefaultStrategy

type DefaultStrategy struct{}

DefaultStrategy 默认策略

func NewDefaultStrategy

func NewDefaultStrategy() *DefaultStrategy

func (*DefaultStrategy) AfterStore

func (s *DefaultStrategy) AfterStore(ctx context.Context, msg *ChatAppMessage) error

func (*DefaultStrategy) BeforeStore

func (s *DefaultStrategy) BeforeStore(ctx context.Context, msg *ChatAppMessage) error

func (*DefaultStrategy) ShouldStore

func (s *DefaultStrategy) ShouldStore(msg *ChatAppMessage) bool

type HealthCheckResult

type HealthCheckResult struct {
	Status    string           `json:"status"` // healthy, degraded, unhealthy
	Latency   time.Duration    `json:"latency"`
	Timestamp time.Time        `json:"timestamp"`
	Checks    map[string]Check `json:"checks"`
}

HealthCheckResult 健康检查结果

func DefaultHealthCheck

func DefaultHealthCheck(store ChatAppMessageStore) *HealthCheckResult

DefaultHealthCheck 执行默认健康检查

type HealthChecker

type HealthChecker interface {
	HealthCheck(ctx context.Context) (*HealthCheckResult, error)
}

HealthChecker 健康检查接口

type MemoryFactory

type MemoryFactory struct{}

func (*MemoryFactory) Create

func (f *MemoryFactory) Create(config PluginConfig) (ChatAppMessageStore, error)

type MemoryStorage

type MemoryStorage struct {
	// contains filtered or unexported fields
}

MemoryStorage 内存存储实现

func (*MemoryStorage) Close

func (m *MemoryStorage) Close() error

func (*MemoryStorage) Count

func (m *MemoryStorage) Count(ctx context.Context, query *MessageQuery) (int64, error)

func (*MemoryStorage) DeleteSession

func (m *MemoryStorage) DeleteSession(ctx context.Context, chatSessionID string) error

func (*MemoryStorage) Get

func (m *MemoryStorage) Get(ctx context.Context, messageID string) (*ChatAppMessage, error)

func (*MemoryStorage) GetSessionMeta

func (m *MemoryStorage) GetSessionMeta(ctx context.Context, chatSessionID string) (*SessionMeta, error)

func (*MemoryStorage) GetStrategy

func (m *MemoryStorage) GetStrategy() StorageStrategy

func (*MemoryStorage) Initialize

func (m *MemoryStorage) Initialize(ctx context.Context) error

func (*MemoryStorage) List

func (m *MemoryStorage) List(ctx context.Context, query *MessageQuery) ([]*ChatAppMessage, error)

func (*MemoryStorage) ListUserSessions

func (m *MemoryStorage) ListUserSessions(ctx context.Context, platform, userID string) ([]string, error)

func (*MemoryStorage) Name

func (m *MemoryStorage) Name() string

func (*MemoryStorage) SetStrategy

func (m *MemoryStorage) SetStrategy(s StorageStrategy) error

func (*MemoryStorage) StoreBotResponse

func (m *MemoryStorage) StoreBotResponse(ctx context.Context, msg *ChatAppMessage) error

func (*MemoryStorage) StoreUserMessage

func (m *MemoryStorage) StoreUserMessage(ctx context.Context, msg *ChatAppMessage) error

func (*MemoryStorage) Version

func (m *MemoryStorage) Version() string

type MessageQuery

type MessageQuery struct {
	ChatSessionID     string
	EngineSessionID   uuid.UUID
	ProviderType      string
	ProviderSessionID string
	StartTime         *time.Time
	EndTime           *time.Time
	MessageTypes      []types.MessageType
	Limit             int
	Offset            int
	Ascending         bool
	IncludeDeleted    bool
}

MessageQuery 消息查询条件

type PluginConfig

type PluginConfig map[string]any

PluginConfig 插件配置

type PluginFactory

type PluginFactory interface {
	Create(config PluginConfig) (ChatAppMessageStore, error)
}

PluginFactory 插件工厂接口

type PluginRegistry

type PluginRegistry struct {
	// contains filtered or unexported fields
}

PluginRegistry 插件注册表

func GlobalRegistry

func GlobalRegistry() *PluginRegistry

func NewPluginRegistry

func NewPluginRegistry() *PluginRegistry

func (*PluginRegistry) Get

func (*PluginRegistry) List

func (r *PluginRegistry) List() []string

func (*PluginRegistry) Register

func (r *PluginRegistry) Register(name string, factory PluginFactory)

type PostgreFactory

type PostgreFactory struct{}

PostgreFactory PostgreSQL 工厂

func (*PostgreFactory) Create

type PostgreSQLConfig

type PostgreSQLConfig struct {
	Host         string
	Port         int
	User         string
	Password     string
	Database     string
	SSLMode      string
	MaxOpenConns int
	MaxIdleConns int
	MaxLifetime  time.Duration
}

PostgreSQLConfig PostgreSQL 配置

type PostgreStorage

type PostgreStorage struct {
	// contains filtered or unexported fields
}

PostgreStorage PostgreSQL 存储实现 (Level 2: Partitioned for 100M+ rows)

func NewPostgreStorage

func NewPostgreStorage(pgConfig PostgreSQLConfig, pluginConfig PluginConfig) (*PostgreStorage, error)

NewPostgreStorage 创建 PostgreSQL 存储

func (*PostgreStorage) Close

func (p *PostgreStorage) Close() error

Close 关闭数据库连接

func (*PostgreStorage) Count

func (p *PostgreStorage) Count(ctx context.Context, query *MessageQuery) (int64, error)

Count 统计消息数量

func (*PostgreStorage) DeleteSession

func (p *PostgreStorage) DeleteSession(ctx context.Context, chatSessionID string) error

DeleteSession 删除会话

func (*PostgreStorage) Get

func (p *PostgreStorage) Get(ctx context.Context, messageID string) (*ChatAppMessage, error)

Get 根据 ID 获取消息

func (*PostgreStorage) GetSessionMeta

func (p *PostgreStorage) GetSessionMeta(ctx context.Context, chatSessionID string) (*SessionMeta, error)

GetSessionMeta 获取会话元数据

func (*PostgreStorage) GetStrategy

func (p *PostgreStorage) GetStrategy() StorageStrategy

GetStrategy 获取存储策略

func (*PostgreStorage) Initialize

func (p *PostgreStorage) Initialize(ctx context.Context) error

Initialize 初始化数据库表结构

func (*PostgreStorage) List

func (p *PostgreStorage) List(ctx context.Context, query *MessageQuery) ([]*ChatAppMessage, error)

List 查询消息列表

func (*PostgreStorage) ListUserSessions

func (p *PostgreStorage) ListUserSessions(ctx context.Context, platform, userID string) ([]string, error)

ListUserSessions 列出用户的所有会话

func (*PostgreStorage) Name

func (p *PostgreStorage) Name() string

Name 返回存储名称

func (*PostgreStorage) SetStrategy

func (p *PostgreStorage) SetStrategy(s StorageStrategy) error

SetStrategy 设置存储策略

func (*PostgreStorage) StoreBotResponse

func (p *PostgreStorage) StoreBotResponse(ctx context.Context, msg *ChatAppMessage) error

StoreBotResponse 存储机器人响应

func (*PostgreStorage) StoreUserMessage

func (p *PostgreStorage) StoreUserMessage(ctx context.Context, msg *ChatAppMessage) error

StoreUserMessage 存储用户消息

func (*PostgreStorage) Version

func (p *PostgreStorage) Version() string

Version 返回版本

type ReadOnlyStore

type ReadOnlyStore interface {
	Get(ctx context.Context, messageID string) (*ChatAppMessage, error)
	List(ctx context.Context, query *MessageQuery) ([]*ChatAppMessage, error)
	Count(ctx context.Context, query *MessageQuery) (int64, error)
}

ReadOnlyStore 只读接口 (ISP)

type SQLiteConfig

type SQLiteConfig struct {
	Path      string `json:"path"`
	MaxSizeMB int    `json:"max_size_mb"`
}

SQLiteConfig SQLite 配置

type SQLiteFactory

type SQLiteFactory struct{}

func (*SQLiteFactory) Create

func (f *SQLiteFactory) Create(config PluginConfig) (ChatAppMessageStore, error)

type SQLiteStorage

type SQLiteStorage struct {
	// contains filtered or unexported fields
}

SQLiteStorage SQLite 存储实现

func (*SQLiteStorage) Close

func (s *SQLiteStorage) Close() error

func (*SQLiteStorage) Count

func (s *SQLiteStorage) Count(ctx context.Context, query *MessageQuery) (int64, error)

func (*SQLiteStorage) DeleteSession

func (s *SQLiteStorage) DeleteSession(ctx context.Context, chatSessionID string) error

func (*SQLiteStorage) Get

func (s *SQLiteStorage) Get(ctx context.Context, messageID string) (*ChatAppMessage, error)

func (*SQLiteStorage) GetSessionMeta

func (s *SQLiteStorage) GetSessionMeta(ctx context.Context, chatSessionID string) (*SessionMeta, error)

func (*SQLiteStorage) GetStrategy

func (s *SQLiteStorage) GetStrategy() StorageStrategy

func (*SQLiteStorage) Initialize

func (s *SQLiteStorage) Initialize(ctx context.Context) error

func (*SQLiteStorage) List

func (s *SQLiteStorage) List(ctx context.Context, query *MessageQuery) ([]*ChatAppMessage, error)

func (*SQLiteStorage) ListUserSessions

func (s *SQLiteStorage) ListUserSessions(ctx context.Context, platform, userID string) ([]string, error)

func (*SQLiteStorage) Name

func (s *SQLiteStorage) Name() string

func (*SQLiteStorage) SetStrategy

func (s *SQLiteStorage) SetStrategy(st StorageStrategy) error

func (*SQLiteStorage) StoreBotResponse

func (s *SQLiteStorage) StoreBotResponse(ctx context.Context, msg *ChatAppMessage) error

func (*SQLiteStorage) StoreUserMessage

func (s *SQLiteStorage) StoreUserMessage(ctx context.Context, msg *ChatAppMessage) error

func (*SQLiteStorage) Version

func (s *SQLiteStorage) Version() string

type SessionMeta

type SessionMeta struct {
	ChatSessionID string
	ChatPlatform  string
	ChatUserID    string
	LastMessageID string
	LastMessageAt time.Time
	MessageCount  int64
	UpdatedAt     time.Time
}

SessionMeta 会话元数据

type SessionStore

type SessionStore interface {
	GetSessionMeta(ctx context.Context, chatSessionID string) (*SessionMeta, error)
	ListUserSessions(ctx context.Context, platform, userID string) ([]string, error)
	DeleteSession(ctx context.Context, chatSessionID string) error
}

SessionStore 会话管理接口

type StorageConfig

type StorageConfig struct {
	Enabled    bool             `json:"enabled"`
	Type       string           `json:"type"`
	SQLite     SQLiteConfig     `json:"sqlite"`
	PostgreSQL PostgreSQLConfig `json:"postgres"`
	Strategy   string           `json:"strategy"`
	Streaming  StreamingConfig  `json:"streaming"`
}

StorageConfig 存储配置

type StorageError

type StorageError struct {
	Code    string
	Message string
	Err     error
}

StorageError 存储错误结构

func NewStorageError

func NewStorageError(code, message string, err error) *StorageError

NewStorageError 创建新的存储错误

func (*StorageError) Error

func (e *StorageError) Error() string

func (*StorageError) Unwrap

func (e *StorageError) Unwrap() error

type StorageMetrics

type StorageMetrics struct {
	TotalMessages    int64         `json:"total_messages"`
	TotalSessions    int64         `json:"total_sessions"`
	StorageSizeBytes int64         `json:"storage_size_bytes"`
	Uptime           time.Duration `json:"uptime"`
}

StorageMetrics 存储指标

func GetMetrics

func GetMetrics(store ChatAppMessageStore) (*StorageMetrics, error)

GetMetrics 获取存储指标

type StorageStrategy

type StorageStrategy interface {
	ShouldStore(msg *ChatAppMessage) bool
	BeforeStore(ctx context.Context, msg *ChatAppMessage) error
	AfterStore(ctx context.Context, msg *ChatAppMessage) error
}

StorageStrategy 存储策略接口 (OCP)

type StreamingConfig

type StreamingConfig struct {
	Enabled       bool   `json:"enabled"`
	BufferSize    int    `json:"buffer_size"`
	TimeoutSec    int    `json:"timeout_seconds"`
	StoragePolicy string `json:"storage_policy"`
}

StreamingConfig 流式配置

type WriteOnlyStore

type WriteOnlyStore interface {
	StoreUserMessage(ctx context.Context, msg *ChatAppMessage) error
	StoreBotResponse(ctx context.Context, msg *ChatAppMessage) error
}

WriteOnlyStore 只写接口 (ISP)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL