()
| 546 | raise tool_error |
| 547 | |
| 548 | async def _run_with_trace(): |
| 549 | nonlocal function_args, detected_error_type |
| 550 | |
| 551 | # Step 1: Check if plugin before_tool_callback overrides the function |
| 552 | # response. |
| 553 | function_response = ( |
| 554 | await invocation_context.plugin_manager.run_before_tool_callback( |
| 555 | tool=tool, tool_args=function_args, tool_context=tool_context |
| 556 | ) |
| 557 | ) |
| 558 | |
| 559 | # Step 2: If no overrides are provided from the plugins, further run the |
| 560 | # canonical callback. |
| 561 | if function_response is None: |
| 562 | for callback in agent.canonical_before_tool_callbacks: |
| 563 | function_response = callback( |
| 564 | tool=tool, args=function_args, tool_context=tool_context |
| 565 | ) |
| 566 | if inspect.isawaitable(function_response): |
| 567 | function_response = await function_response |
| 568 | if function_response: |
| 569 | break |
| 570 | |
| 571 | # Step 3: Otherwise, proceed calling the tool normally. |
| 572 | if function_response is None: |
| 573 | try: |
| 574 | function_response = await __call_tool_async( |
| 575 | tool, args=function_args, tool_context=tool_context |
| 576 | ) |
| 577 | except Exception as tool_error: |
| 578 | error_response = await _run_on_tool_error_callbacks( |
| 579 | tool=tool, |
| 580 | tool_args=function_args, |
| 581 | tool_context=tool_context, |
| 582 | error=tool_error, |
| 583 | ) |
| 584 | if error_response is not None: |
| 585 | function_response = error_response |
| 586 | else: |
| 587 | raise tool_error |
| 588 | |
| 589 | # Step 4: Check if plugin after_tool_callback overrides the function |
| 590 | # response. |
| 591 | altered_function_response = ( |
| 592 | await invocation_context.plugin_manager.run_after_tool_callback( |
| 593 | tool=tool, |
| 594 | tool_args=function_args, |
| 595 | tool_context=tool_context, |
| 596 | result=function_response, |
| 597 | ) |
| 598 | ) |
| 599 | |
| 600 | # Step 5: If no overrides are provided from the plugins, further run the |
| 601 | # canonical after_tool_callbacks. |
| 602 | if altered_function_response is None: |
| 603 | for callback in agent.canonical_after_tool_callbacks: |
| 604 | altered_function_response = callback( |
| 605 | tool=tool, |
no test coverage detected