sandbox

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 23, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultLangs

func DefaultLangs() map[string]LangConfig

func FormatResult

func FormatResult(r *ExecResult) string

FormatResult 格式化执行结果为 LLM 可读文本

func RegisterSandboxTools

func RegisterSandboxTools(registry *tools.ToolRegistry, sb Sandbox) error

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

Types

type ExecRequest

type ExecRequest struct {
	Language string            `json:"language"`          // python, node, go, shell
	Code     string            `json:"code"`              // 代码或命令
	Timeout  time.Duration     `json:"timeout,omitempty"` // 超时,0 用默认值
	Env      map[string]string `json:"env,omitempty"`     // 额外环境变量
	Files    []InputFile       `json:"files,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 LangConfig

type LangConfig struct {
	Command   string            `json:"command"`              // 可执行命令(如 python3、node)
	Args      []string          `json:"args,omitempty"`       // 前置参数(如 ["run"] for go run)
	Ext       string            `json:"ext,omitempty"`        // 代码文件扩展名(空 = shell 模式)
	InitFiles map[string]string `json:"init_files,omitempty"` // 预创建文件(如 go.mod)
}

LangConfig 语言配置

type ProcessConfig

type ProcessConfig struct {
	DefaultTimeout time.Duration         // 默认超时(默认 30s)
	MaxOutputBytes int                   // 最大输出字节数(默认 1MB)
	AllowedLangs   map[string]LangConfig // 语言配置(nil = 使用默认值)
}

ProcessConfig 进程沙箱配置

type ProcessSandbox

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

ProcessSandbox 基于子进程的沙箱实现

func NewProcessSandbox

func NewProcessSandbox(config ProcessConfig) *ProcessSandbox

func (*ProcessSandbox) AddLang

func (s *ProcessSandbox) AddLang(name string, config LangConfig)

AddLang 动态添加语言支持

func (*ProcessSandbox) CheckLang

func (s *ProcessSandbox) CheckLang(lang string) error

CheckLang 检查语言是否可用

func (*ProcessSandbox) Close

func (s *ProcessSandbox) Close() error

Close 关闭沙箱

func (*ProcessSandbox) Execute

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

func (*ProcessSandbox) ListLangs

func (s *ProcessSandbox) ListLangs() []string

ListLangs 列出已配置的语言

type Sandbox

type Sandbox interface {
	// Execute 执行代码/命令
	Execute(ctx context.Context, req ExecRequest) (*ExecResult, error)
	// CheckLang 检查语言是否可用
	CheckLang(lang string) error
	// ListLangs 列出可用语言
	ListLangs() []string
	// Close 关闭沙箱
	Close() error
}

Sandbox 代码执行沙箱接口

Jump to

Keyboard shortcuts

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