execution

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 20, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package execution 提供执行器、沙箱与检查点管理能力。

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotFound      = errors.New("checkpoint not found")
	ErrInvalidThread = errors.New("invalid thread id")
	ErrStorageFull   = errors.New("storage capacity exceeded")
)

Functions

func EnsureImages

func EnsureImages(ctx context.Context, languages []Language, logger *zap.Logger) error

保证图像为沙盒拉出所有所需的图像 。

func PullImage

func PullImage(ctx context.Context, image string, logger *zap.Logger) error

PullImage 若不显示则会拉出一个多克图像 。

Types

type Checkpoint

type Checkpoint struct {
	ID        string         `json:"id"`
	ThreadID  string         `json:"thread_id"`
	ParentID  string         `json:"parent_id,omitempty"`
	State     map[string]any `json:"state"`
	Metadata  Metadata       `json:"metadata"`
	CreatedAt time.Time      `json:"created_at"`
}

检查点代表一个时间点的保存状态.

type Checkpointer

type Checkpointer interface {
	// 设取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取出取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取出取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取取
	Put(ctx context.Context, cp *Checkpoint) error

	// 以身份证取回检查站
	Get(ctx context.Context, threadID, checkpointID string) (*Checkpoint, error)

	// GetLatest 为线索检索最近的检查点 。
	GetLatest(ctx context.Context, threadID string) (*Checkpoint, error)

	// 按创建时间顺序列出返回检查点。
	List(ctx context.Context, threadID string, opts ListOptions) ([]*Checkpoint, error)

	// 删除一个检查站。
	Delete(ctx context.Context, threadID, checkpointID string) error

	// 删除 Thread 为线索删除所有检查点 。
	DeleteThread(ctx context.Context, threadID string) error
}

检查站定义了检查站存储后端的接口.

type CheckpointerConfig

type CheckpointerConfig struct {
	MaxCheckpointsPerThread int           `json:"max_checkpoints_per_thread"`
	TTL                     time.Duration `json:"ttl"`
	Serde                   Serde         `json:"-"`
}

检查器Config配置检查器行为.

func DefaultCheckpointerConfig

func DefaultCheckpointerConfig() CheckpointerConfig

默认检查指定符 Config 返回合理的默认值 。

type CodeValidator

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

代码Validator执行前验证代码.

func NewCodeValidator

func NewCodeValidator() *CodeValidator

NewCodeValidator创建了代码验证器.

func (*CodeValidator) Validate

func (v *CodeValidator) Validate(lang Language, code string) []string

验证危险模式的检查代码 。

type DockerBackend

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

DockerBackend使用Docker执行Backend.

func NewDockerBackend

func NewDockerBackend(logger *zap.Logger) *DockerBackend

NewDockerBackend创建了多克执行后端.

func NewDockerBackendWithConfig

func NewDockerBackendWithConfig(logger *zap.Logger, cfg DockerBackendConfig) *DockerBackend

NewDockerBackendWithConfig创建了自定义配置的多克后端.

func (*DockerBackend) Cleanup

func (d *DockerBackend) Cleanup() error

func (*DockerBackend) Execute

func (*DockerBackend) Name

func (d *DockerBackend) Name() string

type DockerBackendConfig

type DockerBackendConfig struct {
	ContainerPrefix string              // Prefix for container names
	CleanupOnExit   bool                // Remove containers after execution
	CustomImages    map[Language]string // Override default images
}

DockerBackendConfig配置了多克后端.

type ExecutionBackend

type ExecutionBackend interface {
	Execute(ctx context.Context, req *ExecutionRequest, config SandboxConfig) (*ExecutionResult, error)
	Cleanup() error
	Name() string
}

ExecutiveBackend定义执行后端的接口.

type ExecutionMode

type ExecutionMode string

ExecutiveMode定义了沙盒执行模式.

const (
	ModeDocker ExecutionMode = "docker"
	ModeWASM   ExecutionMode = "wasm"
	ModeNative ExecutionMode = "native" // For trusted environments only
)

type ExecutionRequest

type ExecutionRequest struct {
	ID       string            `json:"id"`
	Language Language          `json:"language"`
	Code     string            `json:"code"`
	Stdin    string            `json:"stdin,omitempty"`
	Args     []string          `json:"args,omitempty"`
	EnvVars  map[string]string `json:"env_vars,omitempty"`
	Files    map[string]string `json:"files,omitempty"` // filename -> content
	Timeout  time.Duration     `json:"timeout,omitempty"`
}

执行请求代表代码执行请求.

type ExecutionResult

type ExecutionResult struct {
	ID         string        `json:"id"`
	Success    bool          `json:"success"`
	ExitCode   int           `json:"exit_code"`
	Stdout     string        `json:"stdout"`
	Stderr     string        `json:"stderr"`
	Error      string        `json:"error,omitempty"`
	Duration   time.Duration `json:"duration"`
	MemoryUsed int64         `json:"memory_used_bytes,omitempty"`
	Truncated  bool          `json:"truncated,omitempty"`
}

执行Result代表代码执行的结果.

type ExecutorStats

type ExecutorStats struct {
	TotalExecutions   int64         `json:"total_executions"`
	SuccessExecutions int64         `json:"success_executions"`
	FailedExecutions  int64         `json:"failed_executions"`
	TimeoutExecutions int64         `json:"timeout_executions"`
	TotalDuration     time.Duration `json:"total_duration"`
}

执行者Stats跟踪执行统计.

type JSONSerde

type JSONSerde struct{}

JSONSerde执行JSON系列化.

func (JSONSerde) Deserialize

func (JSONSerde) Deserialize(data []byte, v any) error

func (JSONSerde) Serialize

func (JSONSerde) Serialize(v any) ([]byte, error)

type Language

type Language string

语言代表支持的编程语言.

const (
	LangPython     Language = "python"
	LangJavaScript Language = "javascript"
	LangTypeScript Language = "typescript"
	LangGo         Language = "go"
	LangRust       Language = "rust"
	LangBash       Language = "bash"
)

type ListOptions

type ListOptions struct {
	Limit  int       `json:"limit"`
	Before time.Time `json:"before,omitempty"`
	After  time.Time `json:"after,omitempty"`
}

列表选项配置检查站列表 。

type MemoryCheckpointer

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

内存检查点器执行内存检查点存储.

func NewMemoryCheckpointer

func NewMemoryCheckpointer(maxSize int) *MemoryCheckpointer

新记忆检查点创建了新的记忆检查点.

func (*MemoryCheckpointer) Delete

func (m *MemoryCheckpointer) Delete(ctx context.Context, threadID, checkpointID string) error

func (*MemoryCheckpointer) DeleteThread

func (m *MemoryCheckpointer) DeleteThread(ctx context.Context, threadID string) error

func (*MemoryCheckpointer) Get

func (m *MemoryCheckpointer) Get(ctx context.Context, threadID, checkpointID string) (*Checkpoint, error)

func (*MemoryCheckpointer) GetLatest

func (m *MemoryCheckpointer) GetLatest(ctx context.Context, threadID string) (*Checkpoint, error)

func (*MemoryCheckpointer) List

func (m *MemoryCheckpointer) List(ctx context.Context, threadID string, opts ListOptions) ([]*Checkpoint, error)

func (*MemoryCheckpointer) Put

type Metadata

type Metadata struct {
	Step   int               `json:"step"`
	NodeID string            `json:"node_id,omitempty"`
	Tags   []string          `json:"tags,omitempty"`
	Custom map[string]string `json:"custom,omitempty"`
}

元数据包含检查站元数据。

type ProcessBackend

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

processBackend使用本地进程执行 ElectionBackend. 警告: 不如多克安全 - 只在信任的环境中使用.

func NewProcessBackend

func NewProcessBackend(logger *zap.Logger) *ProcessBackend

NewProcessBackend创建基于进程的执行后端.

func NewProcessBackendWithConfig

func NewProcessBackendWithConfig(logger *zap.Logger, cfg ProcessBackendConfig) *ProcessBackend

NewProcessBackendWithConfig创建了自定义配置的流程后端.

func (*ProcessBackend) Cleanup

func (p *ProcessBackend) Cleanup() error

func (*ProcessBackend) Execute

func (*ProcessBackend) Name

func (p *ProcessBackend) Name() string

type ProcessBackendConfig

type ProcessBackendConfig struct {
	WorkDir            string // Working directory for execution
	Enabled            bool   // Must explicitly enable (security)
	CustomInterpreters map[Language]string
}

processBackendConfig 配置进程后端.

type RealDockerBackend

type RealDockerBackend struct {
	*DockerBackend
}

RealDockerBackend使用实际的多克CLI执行ExecutiveBackend.

func NewRealDockerBackend

func NewRealDockerBackend(logger *zap.Logger) *RealDockerBackend

NewReal DockerBackend创建了一个实际执行代码的Docker后端.

func (*RealDockerBackend) Cleanup

func (d *RealDockerBackend) Cleanup() error

清除所有活动容器。

func (*RealDockerBackend) Execute

执行在真正的多克容器中运行代码 。

type RealProcessBackend

type RealProcessBackend struct {
	*ProcessBackend
	// contains filtered or unexported fields
}

RealProcessBackend 执行 ExecutiveBackend 使用实际的 os/ exec.

func NewRealProcessBackend

func NewRealProcessBackend(logger *zap.Logger, enabled bool) *RealProcessBackend

New Real ProcessBackend 创建一个进程后端,可以实际执行代码. 警告(Warning):只在可信任的环境中使用,在OS级别有适当的沙箱.

func (*RealProcessBackend) Execute

使用本地进程执行代码 。

type SandboxConfig

type SandboxConfig struct {
	Mode             ExecutionMode     `json:"mode"`
	Timeout          time.Duration     `json:"timeout"`
	MaxMemoryMB      int               `json:"max_memory_mb"`
	MaxCPUPercent    int               `json:"max_cpu_percent"`
	NetworkEnabled   bool              `json:"network_enabled"`
	AllowedHosts     []string          `json:"allowed_hosts,omitempty"`
	MountPaths       map[string]string `json:"mount_paths,omitempty"` // host:container
	EnvVars          map[string]string `json:"env_vars,omitempty"`
	MaxOutputBytes   int               `json:"max_output_bytes"`
	AllowedLanguages []Language        `json:"allowed_languages"`
}

Sandbox Config 配置了 sandbox 执行器 。

func DefaultSandboxConfig

func DefaultSandboxConfig() SandboxConfig

默认SandboxConfig返回安全默认 。

type SandboxExecutor

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

SandboxExecutor在孤立的环境中执行代码.

func NewSandboxExecutor

func NewSandboxExecutor(config SandboxConfig, backend ExecutionBackend, logger *zap.Logger) *SandboxExecutor

NewSandboxExecutor创建了新的沙盒执行器.

func (*SandboxExecutor) Cleanup

func (s *SandboxExecutor) Cleanup() error

清理释放出资源.

func (*SandboxExecutor) Execute

执行在沙盒中运行代码.

func (*SandboxExecutor) Stats

func (s *SandboxExecutor) Stats() ExecutorStats

Stats 返回执行统计 。

type SandboxTool

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

Sandbox Tool将 sandbox 执行器包成代理工具.

func NewSandboxTool

func NewSandboxTool(executor *SandboxExecutor, logger *zap.Logger) *SandboxTool

NewSandbox Tool创建了沙盒工具.

func (*SandboxTool) Execute

func (t *SandboxTool) Execute(ctx context.Context, args json.RawMessage) (json.RawMessage, error)

执行通过沙盒执行代码.

type Serde

type Serde interface {
	Serialize(v any) ([]byte, error)
	Deserialize(data []byte, v any) error
}

Serde定义了序列化/去序列化接口.

type ThreadManager

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

ThreadManager通过检查站管理多条线程.

func NewThreadManager

func NewThreadManager(cp Checkpointer, config CheckpointerConfig) *ThreadManager

NewThreadManager 创建了新线程管理器.

func (*ThreadManager) GetHistory

func (tm *ThreadManager) GetHistory(ctx context.Context, threadID string, limit int) ([]*Checkpoint, error)

GetHistory 返回检查点历史为一线程 。

func (*ThreadManager) LoadState

func (tm *ThreadManager) LoadState(ctx context.Context, threadID string) (map[string]any, error)

装入状态为线程加载最新状态 。

func (*ThreadManager) Rollback

func (tm *ThreadManager) Rollback(ctx context.Context, threadID, checkpointID string) (*Checkpoint, error)

倒滚回某个特定的检查站。

func (*ThreadManager) SaveState

func (tm *ThreadManager) SaveState(ctx context.Context, threadID string, state map[string]any, meta Metadata) (*Checkpoint, error)

保存状态保存为线程 。

Jump to

Keyboard shortcuts

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