shell

package
v0.5.11 Latest Latest
Warning

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

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

Documentation

Overview

Package shell 提供 AI Agent 的 Shell 命令执行工具

本包实现了安全的命令行执行工具,支持:

  • 命令执行 (Execute)
  • 超时控制
  • 输出大小限制

安全特性:

  • 命令白名单/黑名单
  • 执行超时
  • 输出截断

使用示例:

executor := shell.New(shell.DefaultConfig())
tool := executor.Tool()
result, _ := tool.Execute(ctx, map[string]any{"command": "ls -la"})

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CurlTool

func CurlTool() tool.Tool

CurlTool 返回 Curl 命令工具

func GitTool

func GitTool(workDir string) tool.Tool

GitTool 返回 Git 命令工具

func GrepTool

func GrepTool() tool.Tool

GrepTool 返回搜索工具

func LsTool

func LsTool() tool.Tool

LsTool 返回列出文件工具

Types

type Config

type Config struct {
	// Shell Shell 程序路径
	Shell string

	// Timeout 默认超时时间
	Timeout time.Duration

	// WorkDir 工作目录
	WorkDir string

	// Env 环境变量
	Env []string

	// AllowedCommands 允许的命令(空表示允许所有)
	AllowedCommands []string

	// DeniedCommands 禁止的命令
	DeniedCommands []string

	// MaxOutputSize 最大输出大小(字节)
	MaxOutputSize int
}

Config Shell 工具配置

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig 返回默认配置

type ExecuteInput

type ExecuteInput struct {
	Command string   `json:"command" desc:"要执行的命令" required:"true"`
	Args    []string `json:"args" desc:"命令参数"`
	WorkDir string   `json:"work_dir" desc:"工作目录"`
	Timeout int      `json:"timeout" desc:"超时时间(秒)"`
}

ExecuteInput 执行命令输入

type ExecuteOutput

type ExecuteOutput struct {
	Stdout   string `json:"stdout"`
	Stderr   string `json:"stderr"`
	ExitCode int    `json:"exit_code"`
	Duration string `json:"duration"`
}

ExecuteOutput 执行命令输出

func Exec

func Exec(ctx context.Context, command string) (*ExecuteOutput, error)

Exec 便捷执行函数

func ExecWithTimeout

func ExecWithTimeout(ctx context.Context, command string, timeout time.Duration) (*ExecuteOutput, error)

ExecWithTimeout 带超时的便捷执行函数

type Tool

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

Tool Shell 工具

func New

func New(config *Config) *Tool

New 创建 Shell 工具

func (*Tool) ExecuteTool

func (t *Tool) ExecuteTool() tool.Tool

ExecuteTool 返回执行命令工具

Jump to

Keyboard shortcuts

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