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 ¶
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 工具配置
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 ExecWithTimeout ¶
func ExecWithTimeout(ctx context.Context, command string, timeout time.Duration) (*ExecuteOutput, error)
ExecWithTimeout 带超时的便捷执行函数
Click to show internal directories.
Click to hide internal directories.