postgres

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: 12 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:uuid;default:gen_random_uuid()"`
	SessionID string    `gorm:"type:uuid;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:bytea"`
	MimeType  string    `gorm:"type:varchar(100)"`
	Size      int64     `gorm:"default:0"`
	CreatedAt time.Time `gorm:"not null;default:now()"`

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

ArtifactModel 工件版本数据库模型 对应表: session_artifacts 支持 EventActions.ArtifactDelta

func (ArtifactModel) BeforeCreate

func (ArtifactModel) BeforeCreate() error

添加复合唯一索引

func (ArtifactModel) TableName

func (ArtifactModel) TableName() string

TableName 指定表名

type Config

type Config struct {
	// DSN 数据库连接字符串
	// 例如: "host=localhost user=postgres password=postgres dbname=agentsdk port=5432 sslmode=disable"
	DSN string

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

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

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

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

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

Config PostgreSQL 配置

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig 返回默认配置

type EventModel

type EventModel struct {
	ID           string    `gorm:"primaryKey;type:uuid;default:gen_random_uuid()"`
	SessionID    string    `gorm:"type:uuid;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:now();index:idx_session_events"`

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

	// 动作 - JSONB 存储
	Actions []byte `gorm:"type:jsonb"`

	// 长时运行工具 ID 列表
	LongRunningToolIDs pq.StringArray `gorm:"type:text[]"`

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

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

EventModel 事件数据库模型 对应表: session_events

func (EventModel) TableName

func (EventModel) TableName() string

TableName 指定表名

type Service

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

Service PostgreSQL Session 服务实现 参考 Google ADK-Go 的 session/database/ 实现

func NewService

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

NewService 创建 PostgreSQL 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]any, 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:uuid;default:gen_random_uuid()"`
	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:now()"`
	UpdatedAt time.Time `gorm:"not null;default:now();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 会话数据库模型 对应表: sessions

func (SessionModel) TableName

func (SessionModel) TableName() string

TableName 指定表名

type StateModel

type StateModel struct {
	SessionID string    `gorm:"primaryKey;type:uuid"`
	Scope     string    `gorm:"primaryKey;type:varchar(50)"` // app, user, session, temp
	Key       string    `gorm:"primaryKey;type:varchar(255)"`
	Value     []byte    `gorm:"type:jsonb;not null"` // JSONB 存储
	CreatedAt time.Time `gorm:"not null;default:now()"`
	UpdatedAt time.Time `gorm:"not null;default:now()"`

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

StateModel 状态数据库模型 对应表: session_states 支持分层状态: app/user/session/temp

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