store

package
v0.35.0 Latest Latest
Warning

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

Go to latest
Published: Dec 29, 2025 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotFound 资源未找到错误
	ErrNotFound = &StoreError{Code: "not_found", Message: "resource not found"}
	// ErrAlreadyExists 资源已存在错误
	ErrAlreadyExists = &StoreError{Code: "already_exists", Message: "resource already exists"}
)

Functions

func DecodeValue

func DecodeValue(src any, dest any) error

DecodeValue 将 any 解码为具体类型

Types

type AgentInfo added in v0.32.0

type AgentInfo struct {
	ID        uint      `gorm:"primaryKey"`
	AgentID   string    `gorm:"uniqueIndex;size:255"`
	Data      string    `gorm:"type:longtext"` // JSON 存储
	CreatedAt time.Time `gorm:"autoCreateTime"`
	UpdatedAt time.Time `gorm:"autoUpdateTime"`
}

type AgentMessage added in v0.32.0

type AgentMessage struct {
	ID        uint      `gorm:"primaryKey"`
	AgentID   string    `gorm:"index;size:255"`
	Messages  string    `gorm:"type:longtext"` // JSON 存储
	CreatedAt time.Time `gorm:"autoCreateTime"`
	UpdatedAt time.Time `gorm:"autoUpdateTime"`
}

MySQL 数据模型

type AgentSnapshot added in v0.32.0

type AgentSnapshot struct {
	ID         uint      `gorm:"primaryKey"`
	AgentID    string    `gorm:"index;size:255"`
	SnapshotID string    `gorm:"index;size:255"`
	Data       string    `gorm:"type:longtext"` // JSON 存储
	CreatedAt  time.Time `gorm:"autoCreateTime"`
}

type AgentTodo added in v0.32.0

type AgentTodo struct {
	ID        uint      `gorm:"primaryKey"`
	AgentID   string    `gorm:"uniqueIndex;size:255"`
	Data      string    `gorm:"type:longtext"` // JSON 存储
	CreatedAt time.Time `gorm:"autoCreateTime"`
	UpdatedAt time.Time `gorm:"autoUpdateTime"`
}

type AgentToolRecord added in v0.32.0

type AgentToolRecord struct {
	ID        uint      `gorm:"primaryKey"`
	AgentID   string    `gorm:"index;size:255"`
	Records   string    `gorm:"type:longtext"` // JSON 存储
	CreatedAt time.Time `gorm:"autoCreateTime"`
	UpdatedAt time.Time `gorm:"autoUpdateTime"`
}

type CollectionItem added in v0.32.0

type CollectionItem struct {
	ID         uint      `gorm:"primaryKey"`
	Collection string    `gorm:"index;size:255"`
	Key        string    `gorm:"index;size:255"`
	Data       string    `gorm:"type:longtext"` // JSON 存储
	CreatedAt  time.Time `gorm:"autoCreateTime"`
	UpdatedAt  time.Time `gorm:"autoUpdateTime"`
}

func (CollectionItem) TableName added in v0.32.0

func (CollectionItem) TableName() string

type Config added in v0.31.0

type Config struct {
	Type StoreType `json:"type" yaml:"type"` // Store 类型: json, redis, mysql

	// JSON Store 配置
	DataDir string `json:"data_dir,omitempty" yaml:"data_dir,omitempty"` // 数据目录

	// Redis Store 配置
	RedisAddr     string        `json:"redis_addr,omitempty" yaml:"redis_addr,omitempty"`         // Redis 地址
	RedisPassword string        `json:"redis_password,omitempty" yaml:"redis_password,omitempty"` // Redis 密码
	RedisDB       int           `json:"redis_db,omitempty" yaml:"redis_db,omitempty"`             // Redis 数据库
	RedisPrefix   string        `json:"redis_prefix,omitempty" yaml:"redis_prefix,omitempty"`     // Redis Key 前缀
	RedisTTL      time.Duration `json:"redis_ttl,omitempty" yaml:"redis_ttl,omitempty"`           // Redis 数据过期时间

	// MySQL Store 配置
	MySQLDSN          string        `json:"mysql_dsn,omitempty" yaml:"mysql_dsn,omitempty"`                       // MySQL DSN
	MySQLMaxOpenConns int           `json:"mysql_max_open_conns,omitempty" yaml:"mysql_max_open_conns,omitempty"` // 最大打开连接数
	MySQLMaxIdleConns int           `json:"mysql_max_idle_conns,omitempty" yaml:"mysql_max_idle_conns,omitempty"` // 最大空闲连接数
	MySQLMaxLifetime  time.Duration `json:"mysql_max_lifetime,omitempty" yaml:"mysql_max_lifetime,omitempty"`     // 连接最大生命周期
}

Config Store 配置

type JSONStore

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

JSONStore JSON文件存储实现

func NewJSONStore

func NewJSONStore(baseDir string) (*JSONStore, error)

NewJSONStore 创建JSON存储

func (*JSONStore) Delete

func (js *JSONStore) Delete(ctx context.Context, collection, key string) error

Delete 删除资源

func (*JSONStore) DeleteAgent

func (js *JSONStore) DeleteAgent(ctx context.Context, agentID string) error

DeleteAgent 删除Agent所有数据

func (*JSONStore) Exists

func (js *JSONStore) Exists(ctx context.Context, collection, key string) (bool, error)

Exists 检查资源是否存在

func (*JSONStore) Get

func (js *JSONStore) Get(ctx context.Context, collection, key string, dest any) error

Get 获取单个资源

func (*JSONStore) List

func (js *JSONStore) List(ctx context.Context, collection string) ([]any, error)

List 列出资源

func (*JSONStore) ListAgents

func (js *JSONStore) ListAgents(ctx context.Context) ([]string, error)

ListAgents 列出所有Agent

func (*JSONStore) ListSnapshots

func (js *JSONStore) ListSnapshots(ctx context.Context, agentID string) ([]types.Snapshot, error)

ListSnapshots 列出快照

func (*JSONStore) LoadInfo

func (js *JSONStore) LoadInfo(ctx context.Context, agentID string) (*types.AgentInfo, error)

LoadInfo 加载Agent元信息

func (*JSONStore) LoadMessages

func (js *JSONStore) LoadMessages(ctx context.Context, agentID string) ([]types.Message, error)

LoadMessages 加载消息列表

func (*JSONStore) LoadSnapshot

func (js *JSONStore) LoadSnapshot(ctx context.Context, agentID string, snapshotID string) (*types.Snapshot, error)

LoadSnapshot 加载快照

func (*JSONStore) LoadTodos

func (js *JSONStore) LoadTodos(ctx context.Context, agentID string) (any, error)

LoadTodos 加载Todo列表

func (*JSONStore) LoadToolCallRecords

func (js *JSONStore) LoadToolCallRecords(ctx context.Context, agentID string) ([]types.ToolCallRecord, error)

LoadToolCallRecords 加载工具调用记录

func (*JSONStore) SaveInfo

func (js *JSONStore) SaveInfo(ctx context.Context, agentID string, info types.AgentInfo) error

SaveInfo 保存Agent元信息

func (*JSONStore) SaveMessages

func (js *JSONStore) SaveMessages(ctx context.Context, agentID string, messages []types.Message) error

SaveMessages 保存消息列表

func (*JSONStore) SaveSnapshot

func (js *JSONStore) SaveSnapshot(ctx context.Context, agentID string, snapshot types.Snapshot) error

SaveSnapshot 保存快照

func (*JSONStore) SaveTodos

func (js *JSONStore) SaveTodos(ctx context.Context, agentID string, todos any) error

SaveTodos 保存Todo列表

func (*JSONStore) SaveToolCallRecords

func (js *JSONStore) SaveToolCallRecords(ctx context.Context, agentID string, records []types.ToolCallRecord) error

SaveToolCallRecords 保存工具调用记录

func (*JSONStore) Set

func (js *JSONStore) Set(ctx context.Context, collection, key string, value any) error

Set 设置资源

func (*JSONStore) TrimMessages added in v0.31.0

func (js *JSONStore) TrimMessages(ctx context.Context, agentID string, maxMessages int) error

TrimMessages 修剪消息列表,保留最近的 N 条消息 如果 maxMessages <= 0,则不修剪

type MySQLConfig added in v0.32.0

type MySQLConfig struct {
	DSN          string        // MySQL DSN
	MaxOpenConns int           // 最大打开连接数
	MaxIdleConns int           // 最大空闲连接数
	MaxLifetime  time.Duration // 连接最大生命周期
}

MySQLConfig MySQL 存储配置

type MySQLStore added in v0.32.0

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

MySQLStore MySQL 存储实现

func NewMySQLStore added in v0.32.0

func NewMySQLStore(config MySQLConfig) (*MySQLStore, error)

NewMySQLStore 创建 MySQL 存储

func (*MySQLStore) Close added in v0.32.0

func (s *MySQLStore) Close() error

Close 关闭数据库连接

func (*MySQLStore) Delete added in v0.32.0

func (s *MySQLStore) Delete(ctx context.Context, collection, key string) error

Delete 删除资源

func (*MySQLStore) DeleteAgent added in v0.32.0

func (s *MySQLStore) DeleteAgent(ctx context.Context, agentID string) error

DeleteAgent 删除Agent所有数据

func (*MySQLStore) Exists added in v0.32.0

func (s *MySQLStore) Exists(ctx context.Context, collection, key string) (bool, error)

Exists 检查资源是否存在

func (*MySQLStore) Get added in v0.32.0

func (s *MySQLStore) Get(ctx context.Context, collection, key string, dest any) error

Get 获取单个资源

func (*MySQLStore) List added in v0.32.0

func (s *MySQLStore) List(ctx context.Context, collection string) ([]any, error)

List 列出资源

func (*MySQLStore) ListAgents added in v0.32.0

func (s *MySQLStore) ListAgents(ctx context.Context) ([]string, error)

ListAgents 列出所有Agent

func (*MySQLStore) ListSnapshots added in v0.32.0

func (s *MySQLStore) ListSnapshots(ctx context.Context, agentID string) ([]types.Snapshot, error)

ListSnapshots 列出快照

func (*MySQLStore) LoadInfo added in v0.32.0

func (s *MySQLStore) LoadInfo(ctx context.Context, agentID string) (*types.AgentInfo, error)

LoadInfo 加载Agent元信息

func (*MySQLStore) LoadMessages added in v0.32.0

func (s *MySQLStore) LoadMessages(ctx context.Context, agentID string) ([]types.Message, error)

LoadMessages 加载消息列表

func (*MySQLStore) LoadSnapshot added in v0.32.0

func (s *MySQLStore) LoadSnapshot(ctx context.Context, agentID string, snapshotID string) (*types.Snapshot, error)

LoadSnapshot 加载快照

func (*MySQLStore) LoadTodos added in v0.32.0

func (s *MySQLStore) LoadTodos(ctx context.Context, agentID string) (any, error)

LoadTodos 加载Todo列表

func (*MySQLStore) LoadToolCallRecords added in v0.32.0

func (s *MySQLStore) LoadToolCallRecords(ctx context.Context, agentID string) ([]types.ToolCallRecord, error)

LoadToolCallRecords 加载工具调用记录

func (*MySQLStore) SaveInfo added in v0.32.0

func (s *MySQLStore) SaveInfo(ctx context.Context, agentID string, info types.AgentInfo) error

SaveInfo 保存Agent元信息

func (*MySQLStore) SaveMessages added in v0.32.0

func (s *MySQLStore) SaveMessages(ctx context.Context, agentID string, messages []types.Message) error

SaveMessages 保存消息列表

func (*MySQLStore) SaveSnapshot added in v0.32.0

func (s *MySQLStore) SaveSnapshot(ctx context.Context, agentID string, snapshot types.Snapshot) error

SaveSnapshot 保存快照

func (*MySQLStore) SaveTodos added in v0.32.0

func (s *MySQLStore) SaveTodos(ctx context.Context, agentID string, todos any) error

SaveTodos 保存Todo列表

func (*MySQLStore) SaveToolCallRecords added in v0.32.0

func (s *MySQLStore) SaveToolCallRecords(ctx context.Context, agentID string, records []types.ToolCallRecord) error

SaveToolCallRecords 保存工具调用记录

func (*MySQLStore) Set added in v0.32.0

func (s *MySQLStore) Set(ctx context.Context, collection, key string, value any) error

Set 设置资源

func (*MySQLStore) TrimMessages added in v0.32.0

func (s *MySQLStore) TrimMessages(ctx context.Context, agentID string, maxMessages int) error

TrimMessages 修剪消息列表

type RedisConfig added in v0.31.0

type RedisConfig struct {
	Addr     string        // Redis 地址,格式: "host:port"
	Password string        // 密码
	DB       int           // 数据库编号 (0-15)
	Prefix   string        // Key 前缀,默认 "aster:"
	TTL      time.Duration // 数据过期时间,默认 7 天
}

RedisConfig Redis 配置

type RedisStore added in v0.31.0

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

RedisStore Redis 持久化存储 适用于分布式环境,支持多节点共享状态

func NewRedisStore added in v0.31.0

func NewRedisStore(config RedisConfig) (*RedisStore, error)

NewRedisStore 创建 Redis Store

func (*RedisStore) Close added in v0.31.0

func (rs *RedisStore) Close() error

Close 关闭 Redis 连接

func (*RedisStore) Delete added in v0.31.0

func (rs *RedisStore) Delete(ctx context.Context, collection, key string) error

Delete 删除资源

func (*RedisStore) DeleteAgent added in v0.31.0

func (rs *RedisStore) DeleteAgent(ctx context.Context, agentID string) error

DeleteAgent 删除 Agent 所有数据

func (*RedisStore) Exists added in v0.31.0

func (rs *RedisStore) Exists(ctx context.Context, collection, key string) (bool, error)

Exists 检查资源是否存在

func (*RedisStore) Get added in v0.31.0

func (rs *RedisStore) Get(ctx context.Context, collection, key string, dest any) error

Get 获取单个资源

func (*RedisStore) List added in v0.31.0

func (rs *RedisStore) List(ctx context.Context, collection string) ([]any, error)

List 列出资源

func (*RedisStore) ListAgents added in v0.31.0

func (rs *RedisStore) ListAgents(ctx context.Context) ([]string, error)

ListAgents 列出所有 Agent

func (*RedisStore) ListSnapshots added in v0.31.0

func (rs *RedisStore) ListSnapshots(ctx context.Context, agentID string) ([]types.Snapshot, error)

ListSnapshots 列出快照

func (*RedisStore) LoadInfo added in v0.31.0

func (rs *RedisStore) LoadInfo(ctx context.Context, agentID string) (*types.AgentInfo, error)

LoadInfo 加载 Agent 元信息

func (*RedisStore) LoadMessages added in v0.31.0

func (rs *RedisStore) LoadMessages(ctx context.Context, agentID string) ([]types.Message, error)

LoadMessages 加载消息列表

func (*RedisStore) LoadSnapshot added in v0.31.0

func (rs *RedisStore) LoadSnapshot(ctx context.Context, agentID string, snapshotID string) (*types.Snapshot, error)

LoadSnapshot 加载快照

func (*RedisStore) LoadTodos added in v0.31.0

func (rs *RedisStore) LoadTodos(ctx context.Context, agentID string) (any, error)

LoadTodos 加载 Todo 列表

func (*RedisStore) LoadToolCallRecords added in v0.31.0

func (rs *RedisStore) LoadToolCallRecords(ctx context.Context, agentID string) ([]types.ToolCallRecord, error)

LoadToolCallRecords 加载工具调用记录

func (*RedisStore) Ping added in v0.31.0

func (rs *RedisStore) Ping(ctx context.Context) error

Ping 检查 Redis 连接

func (*RedisStore) SaveInfo added in v0.31.0

func (rs *RedisStore) SaveInfo(ctx context.Context, agentID string, info types.AgentInfo) error

SaveInfo 保存 Agent 元信息

func (*RedisStore) SaveMessages added in v0.31.0

func (rs *RedisStore) SaveMessages(ctx context.Context, agentID string, messages []types.Message) error

SaveMessages 保存消息列表

func (*RedisStore) SaveSnapshot added in v0.31.0

func (rs *RedisStore) SaveSnapshot(ctx context.Context, agentID string, snapshot types.Snapshot) error

SaveSnapshot 保存快照

func (*RedisStore) SaveTodos added in v0.31.0

func (rs *RedisStore) SaveTodos(ctx context.Context, agentID string, todos any) error

SaveTodos 保存 Todo 列表

func (*RedisStore) SaveToolCallRecords added in v0.31.0

func (rs *RedisStore) SaveToolCallRecords(ctx context.Context, agentID string, records []types.ToolCallRecord) error

SaveToolCallRecords 保存工具调用记录

func (*RedisStore) Set added in v0.31.0

func (rs *RedisStore) Set(ctx context.Context, collection, key string, value any) error

Set 设置资源

func (*RedisStore) TrimMessages added in v0.31.0

func (rs *RedisStore) TrimMessages(ctx context.Context, agentID string, maxMessages int) error

TrimMessages 修剪消息列表(原子操作)

type Store

type Store interface {
	// SaveMessages 保存消息列表
	SaveMessages(ctx context.Context, agentID string, messages []types.Message) error

	// LoadMessages 加载消息列表
	LoadMessages(ctx context.Context, agentID string) ([]types.Message, error)

	// TrimMessages 修剪消息列表,保留最近的 N 条消息
	// 如果 maxMessages <= 0,则不修剪
	TrimMessages(ctx context.Context, agentID string, maxMessages int) error

	// SaveToolCallRecords 保存工具调用记录
	SaveToolCallRecords(ctx context.Context, agentID string, records []types.ToolCallRecord) error

	// LoadToolCallRecords 加载工具调用记录
	LoadToolCallRecords(ctx context.Context, agentID string) ([]types.ToolCallRecord, error)

	// SaveSnapshot 保存快照
	SaveSnapshot(ctx context.Context, agentID string, snapshot types.Snapshot) error

	// LoadSnapshot 加载快照
	LoadSnapshot(ctx context.Context, agentID string, snapshotID string) (*types.Snapshot, error)

	// ListSnapshots 列出快照
	ListSnapshots(ctx context.Context, agentID string) ([]types.Snapshot, error)

	// SaveInfo 保存Agent元信息
	SaveInfo(ctx context.Context, agentID string, info types.AgentInfo) error

	// LoadInfo 加载Agent元信息
	LoadInfo(ctx context.Context, agentID string) (*types.AgentInfo, error)

	// SaveTodos 保存Todo列表
	SaveTodos(ctx context.Context, agentID string, todos any) error

	// LoadTodos 加载Todo列表
	LoadTodos(ctx context.Context, agentID string) (any, error)

	// DeleteAgent 删除Agent所有数据
	DeleteAgent(ctx context.Context, agentID string) error

	// ListAgents 列出所有Agent
	ListAgents(ctx context.Context) ([]string, error)

	// Get 获取单个资源
	Get(ctx context.Context, collection, key string, dest any) error

	// Set 设置资源
	Set(ctx context.Context, collection, key string, value any) error

	// Delete 删除资源
	Delete(ctx context.Context, collection, key string) error

	// List 列出资源
	List(ctx context.Context, collection string) ([]any, error)

	// Exists 检查资源是否存在
	Exists(ctx context.Context, collection, key string) (bool, error)
}

Store 持久化存储接口

func MustNewStore added in v0.31.0

func MustNewStore(config Config) Store

MustNewStore 创建 Store,失败时 panic

func NewStore added in v0.31.0

func NewStore(config Config) (Store, error)

NewStore 创建 Store(工厂方法)

type StoreError

type StoreError struct {
	Code    string
	Message string
	Err     error
}

StoreError Store 错误类型

func (*StoreError) Error

func (e *StoreError) Error() string

func (*StoreError) Unwrap

func (e *StoreError) Unwrap() error

type StoreType added in v0.31.0

type StoreType string

StoreType Store 类型

const (
	StoreTypeJSON  StoreType = "json"
	StoreTypeRedis StoreType = "redis"
	StoreTypeMySQL StoreType = "mysql"
)

Jump to

Keyboard shortcuts

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