(ctx context.Context, input map[string]any, tc *tools.ToolContext)
| 103 | } |
| 104 | |
| 105 | func (t *BashTool) Execute(ctx context.Context, input map[string]any, tc *tools.ToolContext) (any, error) { |
| 106 | // 验证必需参数 |
| 107 | if err := ValidateRequired(input, []string{"command"}); err != nil { |
| 108 | return NewClaudeErrorResponse(err), nil |
| 109 | } |
| 110 | |
| 111 | command := GetStringParam(input, "command", "") |
| 112 | timeoutMs := GetIntParam(input, "timeout", 120000) // 默认2分钟 |
| 113 | description := GetStringParam(input, "description", "") |
| 114 | workingDir := GetStringParam(input, "working_dir", "") |
| 115 | shellID := GetStringParam(input, "shell_id", "") |
| 116 | background := GetBoolParam(input, "background", false) |
| 117 | captureOutput := GetBoolParam(input, "capture_output", true) |
| 118 | shellType := GetStringParam(input, "shell", "bash") |
| 119 | |
| 120 | // 限制最大超时时间为 600000ms (10分钟) |
| 121 | if timeoutMs > 600000 { |
| 122 | timeoutMs = 600000 |
| 123 | } |
| 124 | |
| 125 | if command == "" { |
| 126 | return NewClaudeErrorResponse(errors.New("command cannot be empty")), nil |
| 127 | } |
| 128 | |
| 129 | // Git 安全检查 |
| 130 | gitCheck := GetGlobalGitSafetyValidator().Check(command) |
| 131 | if gitCheck.IsGitCommand { |
| 132 | if gitCheck.Blocked { |
| 133 | return map[string]any{ |
| 134 | "ok": false, |
| 135 | "blocked": true, |
| 136 | "is_git_command": true, |
| 137 | "risk": gitCheck.RiskName, |
| 138 | "reason": gitCheck.Reason, |
| 139 | "warnings": gitCheck.Warnings, |
| 140 | "recommendations": gitCheck.Recommendations, |
| 141 | "message": gitCheck.FormatCheckResult(), |
| 142 | "command": command, |
| 143 | }, nil |
| 144 | } |
| 145 | |
| 146 | if gitCheck.RequiresApproval { |
| 147 | return map[string]any{ |
| 148 | "ok": false, |
| 149 | "requires_approval": true, |
| 150 | "is_git_command": true, |
| 151 | "risk": gitCheck.RiskName, |
| 152 | "warnings": gitCheck.Warnings, |
| 153 | "recommendations": gitCheck.Recommendations, |
| 154 | "message": gitCheck.FormatCheckResult(), |
| 155 | "command": command, |
| 156 | "approval_prompt": fmt.Sprintf("Git command requires approval [%s risk]: %s", gitCheck.RiskName, command), |
| 157 | }, nil |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | // 验证命令安全性 |
| 162 | if err := t.validateCommand(command); err != nil { |
nothing calls this directly
no test coverage detected