sandbox

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 15, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FormatResult

func FormatResult(r *ExecResult) string

func RegisterSandboxTools

func RegisterSandboxTools(registry *tools.ToolRegistry, sb Sandbox, langs map[string]LangDef) error

RegisterSandboxTools 将沙箱能力注册为 Agent 可调用的工具

Types

type EnvMode added in v1.1.0

type EnvMode string
const (
	EnvModeBlacklist   EnvMode = "blacklist"
	EnvModeWhitelist   EnvMode = "whitelist"
	EnvModePassthrough EnvMode = "passthrough"
)

type ExecRequest

type ExecRequest struct {
	Command string            `json:"command"`            // 要执行的命令
	Args    []string          `json:"args,omitempty"`     // 命令参数
	Timeout time.Duration     `json:"timeout,omitempty"`  // 超时,0 用默认值
	Env     map[string]string `json:"env,omitempty"`      // 额外环境变量
	Files   []InputFile       `json:"files,omitempty"`    // 执行前在工作目录创建的文件
	WorkDir string            `json:"work_dir,omitempty"` // 指定工作目录(空 = 临时目录)
}

ExecRequest 执行请求

type ExecResult

type ExecResult struct {
	Stdout    string        `json:"stdout"`
	Stderr    string        `json:"stderr"`
	ExitCode  int           `json:"exit_code"`
	Duration  time.Duration `json:"duration"`
	TimedOut  bool          `json:"timed_out,omitempty"`
	Truncated bool          `json:"truncated,omitempty"`
}

ExecResult 执行结果

type InputFile

type InputFile struct {
	Path    string `json:"path"`    // 相对于工作目录的路径
	Content string `json:"content"` // 文件内容
}

InputFile 执行前注入的文件

type LangDef added in v1.1.0

type LangDef struct {
	Command   string
	Args      []string
	Ext       string
	InitFiles map[string]string
}

LangDef 工具层的语言定义

type ProcessConfig

type ProcessConfig struct {
	DefaultTimeout time.Duration     // 默认超时(默认 30s)
	MaxOutputBytes int               // 最大输出字节数(默认 1MB)
	PreloadEnv     map[string]string // 每次执行都注入的环境变量
	EnvMode        EnvMode           // 环境变量过滤模式(默认 EnvModeBlacklist)
	EnvAllowList   []string          // 白名单模式下的允许变量名列表
	EnvBlockList   []string          // 黑名单模式下的额外拦截变量名前缀
}

ProcessConfig 沙箱配置

type ProcessSandbox

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

ProcessSandbox 基于子进程的沙箱实现

func NewProcessSandbox

func NewProcessSandbox(config ProcessConfig) *ProcessSandbox

func (*ProcessSandbox) Close

func (s *ProcessSandbox) Close() error

func (*ProcessSandbox) Execute

func (s *ProcessSandbox) Execute(ctx context.Context, req ExecRequest) (*ExecResult, error)

type Sandbox

type Sandbox interface {
	Execute(ctx context.Context, req ExecRequest) (*ExecResult, error)
	Close() error
}

Sandbox 子进程沙箱接口

Jump to

Keyboard shortcuts

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