Documentation
¶
Overview ¶
Package artifact 提供 Agent 生成文件的版本化管理
Artifact 管理系统参考 Google ADK Go 设计,用于:
- 保存 Agent 执行过程中生成的文件和二进制数据
- 版本化管理,支持按版本回溯
- 与 Session 关联,支持按会话查询
使用示例:
store := NewMemoryStore()
svc := NewService(store)
// 保存 artifact
id, err := svc.Save(ctx, Artifact{
SessionID: "session-123",
Name: "report.pdf",
MimeType: "application/pdf",
Data: pdfBytes,
})
// 获取最新版本
art, err := svc.GetLatest(ctx, "session-123", "report.pdf")
// 列出所有版本
versions, err := svc.ListVersions(ctx, "session-123", "report.pdf")
Index ¶
- type Artifact
- type MemoryStore
- func (s *MemoryStore) Delete(_ context.Context, id string) error
- func (s *MemoryStore) DeleteAll(_ context.Context, sessionID string) error
- func (s *MemoryStore) Get(_ context.Context, id string) (*Artifact, error)
- func (s *MemoryStore) GetByVersion(_ context.Context, sessionID, name string, version int) (*Artifact, error)
- func (s *MemoryStore) GetLatest(_ context.Context, sessionID, name string) (*Artifact, error)
- func (s *MemoryStore) List(_ context.Context, sessionID string) ([]Artifact, error)
- func (s *MemoryStore) ListVersions(_ context.Context, sessionID, name string) ([]Artifact, error)
- func (s *MemoryStore) Save(_ context.Context, art Artifact) (string, error)
- func (s *MemoryStore) SaveNextVersion(_ context.Context, art Artifact) (string, error)
- type Option
- type Service
- func (s *Service) Delete(ctx context.Context, id string) error
- func (s *Service) DeleteAll(ctx context.Context, sessionID string) error
- func (s *Service) Get(ctx context.Context, id string) (*Artifact, error)
- func (s *Service) GetLatest(ctx context.Context, sessionID, name string) (*Artifact, error)
- func (s *Service) List(ctx context.Context, sessionID string) ([]Artifact, error)
- func (s *Service) ListVersions(ctx context.Context, sessionID, name string) ([]Artifact, error)
- func (s *Service) Save(ctx context.Context, art Artifact) (string, error)
- type Store
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Artifact ¶
type Artifact struct {
// ID 唯一标识
ID string `json:"id"`
// SessionID 关联的会话 ID
SessionID string `json:"session_id"`
// Name 文件名称(同名同 session 的多个 artifact 视为不同版本)
Name string `json:"name"`
// MimeType MIME 类型
MimeType string `json:"mime_type"`
// Data 文件内容
Data []byte `json:"data,omitempty"`
// Version 版本号(从 1 开始自增)
Version int `json:"version"`
// Size 文件大小(字节)
Size int64 `json:"size"`
// Metadata 额外元数据
Metadata map[string]any `json:"metadata,omitempty"`
// CreatedAt 创建时间
CreatedAt time.Time `json:"created_at"`
// CreatedBy 创建者(Agent ID)
CreatedBy string `json:"created_by,omitempty"`
}
Artifact 代表 Agent 生成的一个文件或数据对象
type MemoryStore ¶
type MemoryStore struct {
// contains filtered or unexported fields
}
MemoryStore 内存存储 适用于开发和测试环境
func (*MemoryStore) DeleteAll ¶
func (s *MemoryStore) DeleteAll(_ context.Context, sessionID string) error
func (*MemoryStore) GetByVersion ¶
func (*MemoryStore) ListVersions ¶
func (*MemoryStore) SaveNextVersion ¶
SaveNextVersion 原子地为 (SessionID, Name) 分配下一个版本号并写入。
在单次写锁内完成 "扫描当前最大版本号 -> +1 -> 生成 ID -> 写入", 从而消除 Service.Save 默认路径中 "读-改-写" 之间的并发竞态。
type Option ¶
type Option func(*Service)
Option 是 Service 的可选配置项。
采用 functional option 模式,保证 NewService 既有签名 NewService(store) 不被破坏, 后续新增配置只需追加 Option 而无需修改调用方。
func WithFailOnConflict ¶
func WithFailOnConflict() Option
WithFailOnConflict 让 Save 在目标 ID 已存在时返回显式冲突错误,而非静默覆盖。
适用于需要严格区分 create / overwrite 语义的场景:当调用方自带 ID 且该 ID 已存在时,默认的 upsert 行为会无声覆盖已有 artifact;开启本选项后将改为报错, 避免误覆盖任意已存在数据。
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service artifact 管理服务
func (*Service) ListVersions ¶
ListVersions 列出所有版本
type Store ¶
type Store interface {
// Save 保存 artifact,返回 ID
Save(ctx context.Context, art Artifact) (string, error)
// Get 按 ID 获取 artifact
Get(ctx context.Context, id string) (*Artifact, error)
// GetByVersion 按名称和版本获取
GetByVersion(ctx context.Context, sessionID, name string, version int) (*Artifact, error)
// GetLatest 获取最新版本
GetLatest(ctx context.Context, sessionID, name string) (*Artifact, error)
// List 列出会话下所有 artifact(最新版本)
List(ctx context.Context, sessionID string) ([]Artifact, error)
// ListVersions 列出指定名称的所有版本
ListVersions(ctx context.Context, sessionID, name string) ([]Artifact, error)
// Delete 删除指定 artifact
Delete(ctx context.Context, id string) error
// DeleteAll 删除会话下所有 artifact
DeleteAll(ctx context.Context, sessionID string) error
}
Store artifact 存储接口
Click to show internal directories.
Click to hide internal directories.