Help us shape the future of Go! Take the 2026 Go Developer Survey

storage

package
v0.3.3 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2026 License: MIT Imports: 18 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"`
	Embedding    []byte       `gorm:"type:blob" json:"embedding,omitempty"`
	EmbeddingDim int          `json:"embeddingDim,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 基于文件的记忆存储实现 按数据类型分文件保存。写入使用完整候选快照与原子替换, 消息文件较大或写入频繁时建议使用 SQL 存储。

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, limit 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

func (*FileStore) ClearUserMemoryEvents added in v0.3.0

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

func (*FileStore) DeleteMessages

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

func (*FileStore) DeleteSessionSummary

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

func (*FileStore) DeleteUserMemoryEvent added in v0.3.0

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

func (*FileStore) SaveMessage

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

func (*FileStore) SaveSessionSummary

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

func (*FileStore) SaveUserMemoryEvent added in v0.3.0

func (f *FileStore) SaveUserMemoryEvent(ctx context.Context, v *builtin.UserMemoryEvent) error

func (*FileStore) UpdateSessionSummary

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

func (*FileStore) UpsertUserMemory

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

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) ClearUserMemoryEvents added in v0.3.0

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

ClearUserMemoryEvents 清空用户的所有事件

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) DeleteUserMemoryEvent added in v0.3.0

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

DeleteUserMemoryEvent 删除指定事件

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) ListRecentUserMemoryEvents added in v0.3.0

func (m *MemoryStore) ListRecentUserMemoryEvents(ctx context.Context, userID string, limit int) ([]*builtin.UserMemoryEvent, error)

ListRecentUserMemoryEvents 返回该用户最近的 limit 条事件

func (*MemoryStore) SaveMessage

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

SaveMessage 保存对话消息(保存入参的深拷贝,ID/时间戳写在副本上)

func (*MemoryStore) SaveSessionSummary

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

SaveSessionSummary 保存会话摘要(保存入参的深拷贝,时间戳写在副本上)

func (*MemoryStore) SaveUserMemoryEvent added in v0.3.0

func (m *MemoryStore) SaveUserMemoryEvent(ctx context.Context, event *builtin.UserMemoryEvent) error

SaveUserMemoryEvent 保存一条用户记忆事件(保存入参的深拷贝)

func (*MemoryStore) SearchMessagesByKeywords added in v0.2.6

func (m *MemoryStore) SearchMessagesByKeywords(ctx context.Context, q *builtinsearch.SearchQuery) ([]*builtin.ConversationMessage, error)

func (*MemoryStore) SearchUserMemoryEvents added in v0.3.0

func (m *MemoryStore) SearchUserMemoryEvents(ctx context.Context, query *builtin.UserMemoryEventQuery) ([]*builtin.UserMemoryEvent, error)

SearchUserMemoryEvents 按查询条件检索用户事件

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 NewGormStorageWithPrefix added in v0.3.0

func NewGormStorageWithPrefix(db *gorm.DB, prefix string) (*SQLStore, error)

NewGormStorageWithPrefix 创建带自定义表名前缀的 SQL 存储实例。 prefix 为空时使用默认值 "aggo_mem"。

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) ClearUserMemoryEvents added in v0.3.0

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

ClearUserMemoryEvents 清空用户的事件

func (*SQLStore) Close

func (s *SQLStore) Close() error

Close 关闭数据库连接

func (*SQLStore) ConversationDB added in v0.2.6

func (s *SQLStore) ConversationDB() *gorm.DB

func (*SQLStore) ConversationMessageTableName added in v0.2.6

func (s *SQLStore) ConversationMessageTableName() string

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) DeleteUserMemoryEvent added in v0.3.0

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

DeleteUserMemoryEvent 删除指定事件

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) ListRecentUserMemoryEvents added in v0.3.0

func (s *SQLStore) ListRecentUserMemoryEvents(ctx context.Context, userID string, limit int) ([]*builtin.UserMemoryEvent, error)

ListRecentUserMemoryEvents 返回用户最近的事件,按 EventDate 倒序

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) SaveUserMemoryEvent added in v0.3.0

func (s *SQLStore) SaveUserMemoryEvent(ctx context.Context, event *builtin.UserMemoryEvent) error

SaveUserMemoryEvent 保存一条用户记忆事件

func (*SQLStore) SearchMessagesByKeywords added in v0.2.6

func (s *SQLStore) SearchMessagesByKeywords(ctx context.Context, q *builtinsearch.SearchQuery) ([]*builtin.ConversationMessage, error)

func (*SQLStore) SearchUserMemoryEvents added in v0.3.0

func (s *SQLStore) SearchUserMemoryEvents(ctx context.Context, query *builtin.UserMemoryEventQuery) ([]*builtin.UserMemoryEvent, error)

SearchUserMemoryEvents 按查询条件检索用户事件

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 StringSlice added in v0.3.0

type StringSlice []string

StringSlice 自定义 GORM 类型,存储字符串切片(JSON 编码)。

func (StringSlice) GormDataType added in v0.3.0

func (s StringSlice) GormDataType() string

func (*StringSlice) Scan added in v0.3.0

func (s *StringSlice) Scan(value interface{}) error

func (StringSlice) Value added in v0.3.0

func (s StringSlice) Value() (driver.Value, error)

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) GetUserMemoryEventTableName added in v0.3.0

func (p *TableNameProvider) GetUserMemoryEventTableName() string

GetUserMemoryEventTableName returns the table name for user memory events

func (*TableNameProvider) GetUserMemoryTableName

func (p *TableNameProvider) GetUserMemoryTableName() string

GetUserMemoryTableName returns the table name for user memories

type UserMemoryEventModel added in v0.3.0

type UserMemoryEventModel struct {
	ID        string      `gorm:"primaryKey;size:64" json:"id"`
	UserID    string      `gorm:"size:255;not null;index:idx_user_event_date" json:"userId"`
	Type      string      `gorm:"size:32;not null;index" json:"type"`
	EventDate time.Time   `gorm:"not null;index:idx_user_event_date" json:"eventDate"`
	Keywords  StringSlice `gorm:"type:text" json:"keywords,omitempty"`
	Summary   string      `gorm:"type:text;not null" json:"summary"`
	CreatedAt time.Time   `gorm:"autoCreateTime" json:"createdAt"`
}

UserMemoryEventModel GORM 模型 - 用户记忆事件表(事件检索模式)。

func (*UserMemoryEventModel) FromUserMemoryEvent added in v0.3.0

func (m *UserMemoryEventModel) FromUserMemoryEvent(event *builtin.UserMemoryEvent)

FromUserMemoryEvent 业务模型 -> 数据库模型

func (*UserMemoryEventModel) ToUserMemoryEvent added in v0.3.0

func (m *UserMemoryEventModel) ToUserMemoryEvent() *builtin.UserMemoryEvent

ToUserMemoryEvent 将数据库模型转为业务模型

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