storage

package
v0.0.13 Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2026 License: MIT Imports: 14 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:"index;size:255;not null" json:"sessionId"`
	UserID    string `gorm:"index;size:255;not null" 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" json:"createdAt"`
}

ConversationMessageModel GORM模型 - 对话消息表

func (*ConversationMessageModel) FromConversationMessage

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

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

func (*ConversationMessageModel) ToConversationMessage

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

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

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

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

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

func (*MemoryStore) CleanupOldMessages added in v0.0.10

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

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

func (*MemoryStore) ClearUserMemories

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

ClearUserMemories 清空用户的所有记忆

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) DeleteUserMemoriesByIds

func (m *MemoryStore) DeleteUserMemoriesByIds(ctx context.Context, userID string, memoryIDs []string) error

func (*MemoryStore) DeleteUserMemory

func (m *MemoryStore) DeleteUserMemory(ctx context.Context, memoryID string) error

DeleteUserMemory 删除用户记忆

func (*MemoryStore) GetMessageCount added in v0.0.10

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

GetMessageCount 获取消息总数

func (*MemoryStore) GetMessages

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

GetMessages 获取会话的消息历史

func (*MemoryStore) GetSessionSummary

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

GetSessionSummary 获取会话摘要

func (*MemoryStore) GetUserMemories

func (m *MemoryStore) GetUserMemories(ctx context.Context, userID string, limit int, retrieval memory.MemoryRetrieval) ([]*memory.UserMemory, error)

GetUserMemories 获取用户的记忆列表

func (*MemoryStore) Health

func (m *MemoryStore) Health(ctx context.Context) error

Health 检查存储健康状态

func (*MemoryStore) SaveMessage

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

SaveMessage 保存对话消息

func (*MemoryStore) SaveSessionSummary

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

SaveSessionSummary 保存会话摘要

func (*MemoryStore) SaveUserMemory

func (m *MemoryStore) SaveUserMemory(ctx context.Context, userMemory *memory.UserMemory) error

SaveUserMemory 保存用户记忆

func (*MemoryStore) SearchUserMemories

func (m *MemoryStore) SearchUserMemories(ctx context.Context, userID string, query string, limit int) ([]*memory.UserMemory, error)

SearchUserMemories 搜索用户记忆(简单的文本匹配实现)

func (*MemoryStore) SetTablePrefix

func (m *MemoryStore) SetTablePrefix(prefix string)

func (*MemoryStore) UpdateSessionSummary

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

UpdateSessionSummary 更新会话摘要

func (*MemoryStore) UpdateUserMemory

func (m *MemoryStore) UpdateUserMemory(ctx context.Context, memory *memory.UserMemory) error

UpdateUserMemory 更新用户记忆

type MessageParts added in v0.0.10

type MessageParts []schema.MessageInputPart

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

func (MessageParts) GormDataType added in v0.0.10

func (mp MessageParts) GormDataType() string

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

func (*MessageParts) Scan added in v0.0.10

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

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

func (MessageParts) Value added in v0.0.10

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

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

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

func (*SQLStore) CleanupOldMessages added in v0.0.10

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

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

func (*SQLStore) ClearUserMemories

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

ClearUserMemories 清空用户的所有记忆

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) DeleteUserMemoriesByIds

func (s *SQLStore) DeleteUserMemoriesByIds(ctx context.Context, userID string, memoryIDs []string) error

func (*SQLStore) DeleteUserMemory

func (s *SQLStore) DeleteUserMemory(ctx context.Context, memoryID string) error

DeleteUserMemory 删除用户记忆

func (*SQLStore) GetMessageCount added in v0.0.10

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

GetMessageCount 获取消息总数

func (*SQLStore) GetMessages

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

GetMessages 获取会话的消息历史

func (*SQLStore) GetSessionSummary

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

GetSessionSummary 获取会话摘要

func (*SQLStore) GetUserMemories

func (s *SQLStore) GetUserMemories(ctx context.Context, userID string, limit int, retrieval memory.MemoryRetrieval) ([]*memory.UserMemory, error)

GetUserMemories 获取用户的记忆列表

func (*SQLStore) Health

func (s *SQLStore) Health(ctx context.Context) error

Health 检查数据库健康状态

func (*SQLStore) SaveMessage

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

SaveMessage 保存对话消息

func (*SQLStore) SaveSessionSummary

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

SaveSessionSummary 保存会话摘要

func (*SQLStore) SaveUserMemory

func (s *SQLStore) SaveUserMemory(ctx context.Context, memory *memory.UserMemory) error

SaveUserMemory 保存用户记忆

func (*SQLStore) SearchUserMemories

func (s *SQLStore) SearchUserMemories(ctx context.Context, userID string, query string, limit int) ([]*memory.UserMemory, error)

SearchUserMemories 搜索用户记忆(基于内容关键词搜索)

func (*SQLStore) SetTablePrefix

func (s *SQLStore) SetTablePrefix(prefix string)

func (*SQLStore) UpdateSessionSummary

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

UpdateSessionSummary 更新会话摘要

func (*SQLStore) UpdateUserMemory

func (s *SQLStore) UpdateUserMemory(ctx context.Context, userMemory *memory.UserMemory) error

UpdateUserMemory 更新用户记忆

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"`
	CreatedAt time.Time `gorm:"autoCreateTime" json:"createdAt"`
	UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updatedAt"`
}

SessionSummaryModel GORM模型 - 会话摘要表

func (*SessionSummaryModel) FromSessionSummary

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

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

func (*SessionSummaryModel) ToSessionSummary

func (m *SessionSummaryModel) ToSessionSummary() *memory.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 {
	ID        string    `gorm:"primaryKey;size:255" json:"id"`
	UserID    string    `gorm:"index;size:255;not null" json:"userId"`
	Memory    string    `gorm:"type:text;not null" json:"memory"`
	Input     string    `gorm:"type:text" json:"input"`
	CreatedAt time.Time `gorm:"autoCreateTime" json:"createdAt"`
	UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updatedAt"`
}

UserMemoryModel GORM模型 - 用户记忆表

func (*UserMemoryModel) FromUserMemory

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

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

func (*UserMemoryModel) ToUserMemory

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

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

Jump to

Keyboard shortcuts

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