artifact

package
v0.5.13 Latest Latest
Warning

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

Go to latest
Published: Aug 13, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

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

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 NewMemoryStore

func NewMemoryStore() *MemoryStore

NewMemoryStore 创建内存存储

func (*MemoryStore) Delete

func (s *MemoryStore) Delete(_ context.Context, id string) error

func (*MemoryStore) DeleteAll

func (s *MemoryStore) DeleteAll(_ context.Context, sessionID string) error

func (*MemoryStore) Get

func (s *MemoryStore) Get(_ context.Context, id string) (*Artifact, error)

func (*MemoryStore) GetByVersion

func (s *MemoryStore) GetByVersion(_ context.Context, sessionID, name string, version int) (*Artifact, error)

func (*MemoryStore) GetLatest

func (s *MemoryStore) GetLatest(_ context.Context, sessionID, name string) (*Artifact, error)

func (*MemoryStore) List

func (s *MemoryStore) List(_ context.Context, sessionID string) ([]Artifact, error)

func (*MemoryStore) ListVersions

func (s *MemoryStore) ListVersions(_ context.Context, sessionID, name string) ([]Artifact, error)

func (*MemoryStore) Save

func (s *MemoryStore) Save(_ context.Context, art Artifact) (string, error)

func (*MemoryStore) SaveNextVersion

func (s *MemoryStore) SaveNextVersion(_ context.Context, art Artifact) (string, error)

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 NewService

func NewService(store Store, opts ...Option) *Service

NewService 创建 artifact 管理服务

func (*Service) Delete

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

Delete 删除 artifact

func (*Service) DeleteAll

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

DeleteAll 删除会话下所有 artifact

func (*Service) Get

func (s *Service) Get(ctx context.Context, id string) (*Artifact, error)

Get 获取 artifact

func (*Service) GetLatest

func (s *Service) GetLatest(ctx context.Context, sessionID, name string) (*Artifact, error)

GetLatest 获取最新版本

func (*Service) List

func (s *Service) List(ctx context.Context, sessionID string) ([]Artifact, error)

List 列出会话下所有 artifact

func (*Service) ListVersions

func (s *Service) ListVersions(ctx context.Context, sessionID, name string) ([]Artifact, error)

ListVersions 列出所有版本

func (*Service) Save

func (s *Service) Save(ctx context.Context, art Artifact) (string, error)

Save 保存 artifact,自动生成 ID 和版本号

并发安全:若底层 Store 实现了 versionedSaver(如内置的 MemoryStore), 则"分配版本号 + 写入"会在 Store 内部单次加锁原子完成,避免并发版本竞态; 否则退化为非原子路径(读取最大版本号后 +1 再写入),仅适用于无并发写入的场景。

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 存储接口

Jump to

Keyboard shortcuts

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