源码: conversation.rs:218-268
(
self,
tool_use_id: str,
tool_name: str,
tool_input: str,
prompter: Optional[PermissionPrompter],
)
| 408 | # -------------------------------------------------------- |
| 409 | |
| 410 | def _process_tool_use( |
| 411 | self, |
| 412 | tool_use_id: str, |
| 413 | tool_name: str, |
| 414 | tool_input: str, |
| 415 | prompter: Optional[PermissionPrompter], |
| 416 | ) -> Message: |
| 417 | """源码: conversation.rs:218-268""" |
| 418 | # 第一层: 权限校验 — conversation.rs:219-224 |
| 419 | perm_result = self._permission_policy.authorize( |
| 420 | tool_name, tool_input, prompter, |
| 421 | ) |
| 422 | |
| 423 | if not perm_result.is_allowed: |
| 424 | # 权限拒绝 → 返回 deny reason — conversation.rs:265-267 |
| 425 | return Message.tool_result( |
| 426 | tool_use_id, tool_name, perm_result.reason, True, |
| 427 | ) |
| 428 | |
| 429 | # 第二层: PreToolUse hook — conversation.rs:228-236 |
| 430 | pre_hook = self._hook_runner.run_pre_tool_use(tool_name, tool_input) |
| 431 | if pre_hook.denied: |
| 432 | deny_msg = f"PreToolUse hook denied tool `{tool_name}`" |
| 433 | output = _format_hook_message(pre_hook, deny_msg) |
| 434 | return Message.tool_result( |
| 435 | tool_use_id, tool_name, output, True, |
| 436 | ) |
| 437 | |
| 438 | # 第三层: 执行工具 — conversation.rs:238-242 |
| 439 | try: |
| 440 | output = self._tool_executor.execute(tool_name, tool_input) |
| 441 | is_error = False |
| 442 | except (ToolError, Exception) as exc: |
| 443 | output = str(exc) |
| 444 | is_error = True |
| 445 | |
| 446 | # 合并 pre hook 反馈 — conversation.rs:243 |
| 447 | output = merge_hook_feedback(pre_hook.messages, output, False) |
| 448 | |
| 449 | # PostToolUse hook — conversation.rs:245-255 |
| 450 | post_hook = self._hook_runner.run_post_tool_use( |
| 451 | tool_name, tool_input, output, is_error, |
| 452 | ) |
| 453 | if post_hook.denied: |
| 454 | is_error = True |
| 455 | output = merge_hook_feedback( |
| 456 | post_hook.messages, output, post_hook.denied, |
| 457 | ) |
| 458 | |
| 459 | return Message.tool_result( |
| 460 | tool_use_id, tool_name, output, is_error, |
| 461 | ) |
| 462 | |
| 463 | # -------------------------------------------------------- |
| 464 | # _maybe_auto_compact — 自动压缩 |
no test coverage detected