executeSingleTool 执行单个工具
(ctx context.Context, tu *types.ToolUseBlock)
| 381 | |
| 382 | // executeSingleTool 执行单个工具 |
| 383 | func (a *Agent) executeSingleTool(ctx context.Context, tu *types.ToolUseBlock) types.ContentBlock { |
| 384 | // 检查工具输入是否有解析错误(流式响应被截断等情况) |
| 385 | if parseError, ok := tu.Input["__parse_error__"].(bool); ok && parseError { |
| 386 | errorMsg := "工具参数解析失败" |
| 387 | if msg, ok := tu.Input["__error_message__"].(string); ok { |
| 388 | errorMsg = msg |
| 389 | } |
| 390 | procLog.Error(ctx, "tool input parse error detected", map[string]any{ |
| 391 | "tool": tu.Name, |
| 392 | "id": tu.ID, |
| 393 | "error": errorMsg, |
| 394 | }) |
| 395 | a.eventBus.EmitProgress(&types.ProgressToolErrorEvent{ |
| 396 | Call: types.ToolCallSnapshot{ |
| 397 | ID: tu.ID, |
| 398 | Name: tu.Name, |
| 399 | State: types.ToolCallStateFailed, |
| 400 | Arguments: tu.Input, |
| 401 | }, |
| 402 | Error: errorMsg, |
| 403 | }) |
| 404 | return &types.ToolResultBlock{ |
| 405 | ToolUseID: tu.ID, |
| 406 | Content: fmt.Sprintf(`{"ok":false,"error":"%s","hint":"请重新调用工具,确保提供完整的参数"}`, errorMsg), |
| 407 | IsError: true, |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | // Plan 模式检查:验证工具调用是否允许 |
| 412 | if a.planMode != nil && a.planMode.IsActive() { |
| 413 | allowed, reason := a.planMode.ValidateToolCall(tu.Name, tu.Input) |
| 414 | if !allowed { |
| 415 | errorMsg := "Plan Mode restriction: " + reason |
| 416 | a.eventBus.EmitProgress(&types.ProgressToolErrorEvent{ |
| 417 | Call: types.ToolCallSnapshot{ |
| 418 | ID: tu.ID, |
| 419 | Name: tu.Name, |
| 420 | State: types.ToolCallStateFailed, |
| 421 | Arguments: tu.Input, |
| 422 | }, |
| 423 | Error: errorMsg, |
| 424 | }) |
| 425 | return &types.ToolResultBlock{ |
| 426 | ToolUseID: tu.ID, |
| 427 | Content: fmt.Sprintf(`{"ok":false,"error":"%s","plan_mode":true}`, errorMsg), |
| 428 | IsError: true, |
| 429 | } |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | // 权限检查 |
| 434 | if a.permissionInspector != nil { |
| 435 | call := &types.ToolCallSnapshot{ |
| 436 | ID: tu.ID, |
| 437 | Name: tu.Name, |
| 438 | Arguments: tu.Input, |
| 439 | } |
| 440 | checkResult, err := a.permissionInspector.Check(ctx, call) |
no test coverage detected