mysql

package
v0.12.1 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2025 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ArtifactModel

type ArtifactModel struct {
	ID        string    `gorm:"primaryKey;type:varchar(36)"`
	SessionID string    `gorm:"type:varchar(36);not null;index:idx_artifacts"`
	Name      string    `gorm:"type:varchar(255);not null;index:idx_artifacts"`
	Version   int       `gorm:"not null;index:idx_artifacts"`
	Content   []byte    `gorm:"type:longblob"`
	MimeType  string    `gorm:"type:varchar(100)"`
	Size      int64     `gorm:"default:0"`
	CreatedAt time.Time `gorm:"not null;default:CURRENT_TIMESTAMP"`

	// 唯一约束:同一 session 下,同一工件的同一版本只能有一条记录
	Session SessionModel `gorm:"foreignKey:SessionID;references:ID"`
}

ArtifactModel MySQL 工件模型 对应表: session_artifacts

func (ArtifactModel) BeforeCreate

func (ArtifactModel) BeforeCreate() error

BeforeCreate GORM 钩子

func (ArtifactModel) TableName

func (ArtifactModel) TableName() string

TableName 指定表名

type Config

type Config struct {
	// DSN 数据库连接字符串
	// 例如: "user:password@tcp(127.0.0.1:3306)/agentsdk?charset=utf8mb4&parseTime=True&loc=Local"
	DSN string

	// MaxIdleConns 最大空闲连接数
	MaxIdleConns int

	// MaxOpenConns 最大打开连接数
	MaxOpenConns int

	// ConnMaxLifetime 连接最大生命周期
	ConnMaxLifetime time.Duration

	// LogLevel GORM 日志级别
	LogLevel logger.LogLevel

	// AutoMigrate 是否自动迁移表结构
	AutoMigrate bool
}

Config MySQL 配置

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig 返回默认配置

type EventModel

type EventModel struct {
	ID           string    `gorm:"primaryKey;type:varchar(36)"`
	SessionID    string    `gorm:"type:varchar(36);not null;index:idx_session_events"`
	InvocationID string    `gorm:"type:varchar(255);not null;index:idx_invocation_events"`
	Branch       string    `gorm:"type:varchar(500);not null;index:idx_branch_events"`
	Author       string    `gorm:"type:varchar(255);not null"`
	AgentID      string    `gorm:"type:varchar(255);not null"`
	Timestamp    time.Time `gorm:"not null;default:CURRENT_TIMESTAMP;index:idx_session_events"`

	// 内容 - JSON 存储
	Content []byte `gorm:"type:json;not null"`

	// 动作 - JSON 存储
	Actions []byte `gorm:"type:json"`

	// 长时运行工具 ID 列表 - JSON 数组存储
	LongRunningToolIDs []byte `gorm:"type:json"`

	// 元数据 - JSON 存储
	Metadata []byte `gorm:"type:json"`

	// 关联关系
	Session SessionModel `gorm:"foreignKey:SessionID;references:ID"`
}

EventModel MySQL 事件模型 对应表: session_events

func (EventModel) TableName

func (EventModel) TableName() string

TableName 指定表名

type Service

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

Service MySQL Session 服务实现 支持 MySQL 8.0+ (JSON 列类型)

func NewService

func NewService(cfg *Config) (*Service, error)

NewService 创建 MySQL Session 服务

func (*Service) AppendEvent

func (s *Service) AppendEvent(ctx context.Context, sessionID string, event *session.Event) error

AppendEvent 实现 session.Service 接口

func (*Service) Close

func (s *Service) Close() error

Close 关闭数据库连接

func (*Service) Create

Create 实现 session.Service 接口

func (*Service) Delete

func (s *Service) Delete(ctx context.Context, sessionID string) error

Delete 实现 session.Service 接口

func (*Service) Get

func (s *Service) Get(ctx context.Context, sessionID string) (*session.SessionData, error)

Get 实现 session.Service 接口

func (*Service) GetEvents

func (s *Service) GetEvents(ctx context.Context, sessionID string, opts *session.EventOptions) ([]*session.Event, error)

GetEvents 获取会话的所有事件

func (*Service) GetState

func (s *Service) GetState(ctx context.Context, sessionID string, scope string) (map[string]interface{}, error)

GetState 获取会话状态

func (*Service) List

func (s *Service) List(ctx context.Context, userID string, opts *session.ListOptions) ([]*session.SessionData, error)

List 实现 session.Service 接口

type SessionModel

type SessionModel struct {
	ID        string    `gorm:"primaryKey;type:varchar(36)"`
	AppName   string    `gorm:"type:varchar(255);not null;index:idx_app_sessions"`
	UserID    string    `gorm:"type:varchar(255);not null;index:idx_user_sessions"`
	AgentID   string    `gorm:"type:varchar(255);not null;index:idx_agent_sessions"`
	CreatedAt time.Time `gorm:"not null;default:CURRENT_TIMESTAMP"`
	UpdatedAt time.Time `gorm:"not null;default:CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;index:idx_user_sessions,idx_app_sessions"`

	// 关联关系
	States    []StateModel    `gorm:"foreignKey:SessionID;constraint:OnDelete:CASCADE"`
	Events    []EventModel    `gorm:"foreignKey:SessionID;constraint:OnDelete:CASCADE"`
	Artifacts []ArtifactModel `gorm:"foreignKey:SessionID;constraint:OnDelete:CASCADE"`
}

SessionModel MySQL 会话模型 对应表: sessions

func (SessionModel) TableName

func (SessionModel) TableName() string

TableName 指定表名

type StateModel

type StateModel struct {
	SessionID string    `gorm:"primaryKey;type:varchar(36)"`
	Scope     string    `gorm:"primaryKey;type:varchar(50)"`
	Key       string    `gorm:"primaryKey;type:varchar(255);column:key"` // key 是 MySQL 保留字,需要转义
	Value     []byte    `gorm:"type:json;not null"`                      // MySQL 8.0+ JSON 类型
	CreatedAt time.Time `gorm:"not null;default:CURRENT_TIMESTAMP"`
	UpdatedAt time.Time `gorm:"not null;default:CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP"`

	// 关联关系
	Session SessionModel `gorm:"foreignKey:SessionID;references:ID"`
}

StateModel MySQL 状态模型 对应表: session_states

func (StateModel) TableName

func (StateModel) TableName() string

TableName 指定表名

Jump to

Keyboard shortcuts

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