storage

package
v0.2.5 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DialectMySQL MySQL方言
	DialectMySQL string = "mysql"
	// DialectPostgreSQL PostgreSQL方言
	DialectPostgreSQL string = "postgres"
	// DialectSQLite SQLite方言
	DialectSQLite string = "sqlite"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type ConversationMessageModel

type ConversationMessageModel struct {
	ID        string `gorm:"primaryKey;size:255" json:"id"`
	SessionID string `gorm:"size:255;not null;index:idx_session_user" json:"sessionId"`
	UserID    string `gorm:"size:255;not null;index:idx_session_user;index:idx_user_session" json:"userId"`
	Role      string `gorm:"size:50;not null" json:"role"`
	// 保留Content字段用于向后兼容
	Content string `gorm:"type:text" json:"content,omitempty"`
	// 多部分内容,使用自定义类型直接存储
	Parts     MessageParts `gorm:"type:text" json:"parts,omitempty"`
	CreatedAt time.Time    `gorm:"autoCreateTime;index:idx_user_session" json:"createdAt"`
}

ConversationMessageModel GORM模型 - 对话消息表

func (*ConversationMessageModel) FromConversationMessage

func (m *ConversationMessageModel) FromConversationMessage(message *builtin.ConversationMessage)

FromConversationMessage 将业务模型转换为数据库模型

func (*ConversationMessageModel) ToConversationMessage

func (m *ConversationMessageModel) ToConversationMessage() *builtin.ConversationMessage

ToConversationMessage 将数据库模型转换为业务模型

type FileStore

type FileStore struct {
	*MemoryStore
	// contains filtered or unexported fields
}

FileStore 基于文件的记忆存储实现 分三个文件分别存储不同类型的数据,并在新增消息时支持修剪历史避免文件无限增长。

func NewFileStore

func NewFileStore(dirPath string, maxSessionMessages int) (*FileStore, error)

NewFileStore 创建新的基于文件的存储实例 dirPath: 保存数据的目录路径 maxSessionMessages: 每个会话最大保存的消息数量,超过会裁剪旧消息。如果传 <= 0,默认保留100条。

func (*FileStore) CleanupMessagesByLimit

func (f *FileStore) CleanupMessagesByLimit(ctx context.Context, userID, sessionID string, keepLimit int) error

func (*FileStore) CleanupOldMessages

func (f *FileStore) CleanupOldMessages(ctx context.Context, userID string, before time.Time) error

func (*FileStore) ClearUserMemory

func (f *FileStore) ClearUserMemory(ctx context.Context, userID string) error

ClearUserMemory 清空用户记忆

func (*FileStore) DeleteMessages

func (f *FileStore) DeleteMessages(ctx context.Context, sessionID string, userID string) error

func (*FileStore) DeleteSessionSummary

func (f *FileStore) DeleteSessionSummary(ctx context.Context, sessionID string, userID string) error

func (*FileStore) SaveMessage

func (f *FileStore) SaveMessage(ctx context.Context, message *builtin.ConversationMessage) error

func (*FileStore) SaveSessionSummary

func (f *FileStore) SaveSessionSummary(ctx context.Context, summary *builtin.SessionSummary) error

func (*FileStore) UpdateSessionSummary

func (f *FileStore) UpdateSessionSummary(ctx context.Context, summary *builtin.SessionSummary) error

func (*FileStore) UpsertUserMemory

func (f *FileStore) UpsertUserMemory(ctx context.Context, userMemory *builtin.UserMemory) error

UpsertUserMemory 创建或更新用户记忆 由于每个用户只有一条记录,直接全量重写文件(记录数极少,开销可忽略)

type MemoryStore

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

MemoryStore 内存存储实现 这是一个基于内存的记忆存储实现,适合测试和开发环境

func NewMemoryStore

func NewMemoryStore() *MemoryStore

NewMemoryStore 创建新的内存存储实例

func (*MemoryStore) AutoMigrate

func (m *MemoryStore) AutoMigrate() error

func (*MemoryStore) CleanupMessagesByLimit

func (m *MemoryStore) CleanupMessagesByLimit(ctx context.Context, userID, sessionID string, keepLimit int) error

CleanupMessagesByLimit 按数量限制清理消息,保留最新的N条

func (*MemoryStore) CleanupOldMessages

func (m *MemoryStore) CleanupOldMessages(ctx context.Context, userID string, before time.Time) error

CleanupOldMessages 清理指定时间之前的消息

func (*MemoryStore) ClearUserMemory

func (m *MemoryStore) ClearUserMemory(ctx context.Context, userID string) error

ClearUserMemory 清空用户记忆

func (*MemoryStore) Close

func (m *MemoryStore) Close() error

Close 关闭存储连接(内存存储无需关闭)

func (*MemoryStore) DeleteMessages

func (m *MemoryStore) DeleteMessages(ctx context.Context, sessionID string, userID string) error

DeleteMessages 删除会话的消息历史

func (*MemoryStore) DeleteSessionSummary

func (m *MemoryStore) DeleteSessionSummary(ctx context.Context, sessionID string, userID string) error

DeleteSessionSummary 删除会话摘要

func (*MemoryStore) GetMessageCount

func (m *MemoryStore) GetMessageCount(ctx context.Context, userID, sessionID string) (int, error)

GetMessageCount 获取消息总数

func (*MemoryStore) GetMessageCountAfter added in v0.2.3

func (m *MemoryStore) GetMessageCountAfter(ctx context.Context, sessionID string, userID string, afterMessageID string, afterTime time.Time) (int, error)

GetMessageCountAfter 获取游标之后的会话消息数量。

func (*MemoryStore) GetMessages

func (m *MemoryStore) GetMessages(ctx context.Context, sessionID string, userID string, limit int) ([]*builtin.ConversationMessage, error)

GetMessages 获取会话的消息历史

func (*MemoryStore) GetMessagesAfter added in v0.2.3

func (m *MemoryStore) GetMessagesAfter(ctx context.Context, sessionID string, userID string, afterMessageID string, afterTime time.Time, limit int) ([]*builtin.ConversationMessage, error)

GetMessagesAfter 获取游标之后的会话消息历史。

func (*MemoryStore) GetSessionSummary

func (m *MemoryStore) GetSessionSummary(ctx context.Context, sessionID string, userID string) (*builtin.SessionSummary, error)

GetSessionSummary 获取会话摘要

func (*MemoryStore) GetUserMemory

func (m *MemoryStore) GetUserMemory(ctx context.Context, userID string) (*builtin.UserMemory, error)

GetUserMemory 获取用户的记忆

func (*MemoryStore) SaveMessage

func (m *MemoryStore) SaveMessage(ctx context.Context, message *builtin.ConversationMessage) error

SaveMessage 保存对话消息

func (*MemoryStore) SaveSessionSummary

func (m *MemoryStore) SaveSessionSummary(ctx context.Context, summary *builtin.SessionSummary) error

SaveSessionSummary 保存会话摘要

func (*MemoryStore) UpdateSessionSummary

func (m *MemoryStore) UpdateSessionSummary(ctx context.Context, summary *builtin.SessionSummary) error

UpdateSessionSummary 更新会话摘要

func (*MemoryStore) UpsertUserMemory

func (m *MemoryStore) UpsertUserMemory(ctx context.Context, userMemory *builtin.UserMemory) error

UpsertUserMemory 创建或更新用户记忆(每个用户一条记录)

type MessageParts

type MessageParts []schema.MessageInputPart

MessageParts 自定义 GORM 类型,用于处理 []schema.MessageInputPart 的序列化

func (MessageParts) GormDataType

func (mp MessageParts) GormDataType() string

GormValue 为 GORM 提供特定的数据类型支持

func (*MessageParts) Scan

func (mp *MessageParts) Scan(value interface{}) error

Scan 实现 sql.Scanner 接口,用于从数据库读取数据

func (MessageParts) Value

func (mp MessageParts) Value() (driver.Value, error)

Value 实现 driver.Valuer 接口,用于将数据存入数据库

type SQLStore

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

SQLStore 通用SQL存储实现 支持MySQL、PostgreSQL和SQLite

func NewGormStorage

func NewGormStorage(db *gorm.DB) (*SQLStore, error)

NewGormStorage 创建新的SQL存储实例

func (*SQLStore) AutoMigrate

func (s *SQLStore) AutoMigrate() error

AutoMigrate 自动迁移表结构

func (*SQLStore) CleanupMessagesByLimit

func (s *SQLStore) CleanupMessagesByLimit(ctx context.Context, userID, sessionID string, keepLimit int) error

CleanupMessagesByLimit 按数量限制清理消息,保留最新的N条

func (*SQLStore) CleanupOldMessages

func (s *SQLStore) CleanupOldMessages(ctx context.Context, userID string, before time.Time) error

CleanupOldMessages 清理指定时间之前的消息

func (*SQLStore) ClearUserMemory

func (s *SQLStore) ClearUserMemory(ctx context.Context, userID string) error

ClearUserMemory 清空用户记忆

func (*SQLStore) Close

func (s *SQLStore) Close() error

Close 关闭数据库连接

func (*SQLStore) DeleteMessages

func (s *SQLStore) DeleteMessages(ctx context.Context, sessionID string, userID string) error

DeleteMessages 删除会话的消息历史

func (*SQLStore) DeleteSessionSummary

func (s *SQLStore) DeleteSessionSummary(ctx context.Context, sessionID string, userID string) error

DeleteSessionSummary 删除会话摘要

func (*SQLStore) GetMessageCount

func (s *SQLStore) GetMessageCount(ctx context.Context, userID, sessionID string) (int, error)

GetMessageCount 获取消息总数

func (*SQLStore) GetMessageCountAfter added in v0.2.3

func (s *SQLStore) GetMessageCountAfter(ctx context.Context, sessionID string, userID string, afterMessageID string, afterTime time.Time) (int, error)

GetMessageCountAfter 获取游标之后的会话消息数量,避免加载完整消息列表。

func (*SQLStore) GetMessages

func (s *SQLStore) GetMessages(ctx context.Context, sessionID string, userID string, limit int) ([]*builtin.ConversationMessage, error)

GetMessages 获取会话的消息历史

func (*SQLStore) GetMessagesAfter added in v0.2.3

func (s *SQLStore) GetMessagesAfter(ctx context.Context, sessionID string, userID string, afterMessageID string, afterTime time.Time, limit int) ([]*builtin.ConversationMessage, error)

GetMessagesAfter 获取游标之后的会话消息历史。 Results are returned in chronological order (oldest first). Internally the query uses DESC ordering with LIMIT to fetch the most recent N messages efficiently, then reverses them for caller convenience.

func (*SQLStore) GetSessionSummary

func (s *SQLStore) GetSessionSummary(ctx context.Context, sessionID string, userID string) (*builtin.SessionSummary, error)

GetSessionSummary 获取会话摘要

func (*SQLStore) GetUserMemory

func (s *SQLStore) GetUserMemory(ctx context.Context, userID string) (*builtin.UserMemory, error)

GetUserMemory 获取用户的记忆

func (*SQLStore) SaveMessage

func (s *SQLStore) SaveMessage(ctx context.Context, message *builtin.ConversationMessage) error

SaveMessage 保存对话消息

func (*SQLStore) SaveSessionSummary

func (s *SQLStore) SaveSessionSummary(ctx context.Context, summary *builtin.SessionSummary) error

SaveSessionSummary 保存会话摘要

func (*SQLStore) UpdateSessionSummary

func (s *SQLStore) UpdateSessionSummary(ctx context.Context, summary *builtin.SessionSummary) error

UpdateSessionSummary 更新会话摘要

func (*SQLStore) UpsertUserMemory

func (s *SQLStore) UpsertUserMemory(ctx context.Context, userMemory *builtin.UserMemory) error

UpsertUserMemory 创建或更新用户记忆(每个用户一条记录)

type SessionSummaryModel

type SessionSummaryModel struct {
	SessionID               string    `gorm:"primaryKey;size:255" json:"sessionId"`
	UserID                  string    `gorm:"primaryKey;size:255" json:"userId"`
	Summary                 string    `gorm:"type:text;not null" json:"summary"`
	LastSummarizedMessageID string    `gorm:"size:255" json:"lastSummarizedMessageId,omitempty"`
	LastSummarizedMessageAt time.Time `json:"lastSummarizedMessageAt,omitempty"`
	CreatedAt               time.Time `gorm:"autoCreateTime" json:"createdAt"`
	UpdatedAt               time.Time `gorm:"autoUpdateTime" json:"updatedAt"`
}

SessionSummaryModel GORM模型 - 会话摘要表

func (*SessionSummaryModel) FromSessionSummary

func (m *SessionSummaryModel) FromSessionSummary(sessionSummary *builtin.SessionSummary)

FromSessionSummary 将业务模型转换为数据库模型

func (*SessionSummaryModel) ToSessionSummary

func (m *SessionSummaryModel) ToSessionSummary() *builtin.SessionSummary

ToSessionSummary 将数据库模型转换为业务模型

type TableNameProvider

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

TableNameProvider provides table names with configurable prefix

func NewTableNameProvider

func NewTableNameProvider(prefix string) *TableNameProvider

NewTableNameProvider creates a new table name provider with the given prefix

func (*TableNameProvider) GetConversationMessageTableName

func (p *TableNameProvider) GetConversationMessageTableName() string

GetConversationMessageTableName returns the table name for conversation messages

func (*TableNameProvider) GetSessionSummaryTableName

func (p *TableNameProvider) GetSessionSummaryTableName() string

GetSessionSummaryTableName returns the table name for session summaries

func (*TableNameProvider) GetUserMemoryTableName

func (p *TableNameProvider) GetUserMemoryTableName() string

GetUserMemoryTableName returns the table name for user memories

type UserMemoryModel

type UserMemoryModel struct {
	UserID    string    `gorm:"primaryKey;size:255" json:"userId"`
	Memory    string    `gorm:"type:text;not null" json:"memory"`
	CreatedAt time.Time `gorm:"autoCreateTime" json:"createdAt"`
	UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updatedAt"`
}

UserMemoryModel GORM模型 - 用户记忆表(每个用户一条记录)

func (*UserMemoryModel) FromUserMemory

func (m *UserMemoryModel) FromUserMemory(userMemory *builtin.UserMemory)

FromUserMemory 将业务模型转换为数据库模型

func (*UserMemoryModel) ToUserMemory

func (m *UserMemoryModel) ToUserMemory() *builtin.UserMemory

ToUserMemory 将数据库模型转换为业务模型

Jump to

Keyboard shortcuts

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