|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from typing import Any |
| 4 | + |
| 5 | +from agents import ( |
| 6 | + AgentHookContext, |
| 7 | + FunctionTool, |
| 8 | + HandoffInputData, |
| 9 | + ItemHelpers, |
| 10 | + MultiProvider, |
| 11 | + RunConfig, |
| 12 | + ToolGuardrailFunctionOutput, |
| 13 | + ToolInputGuardrailData, |
| 14 | + ToolOutputGuardrailData, |
| 15 | + Usage, |
| 16 | + tool_input_guardrail, |
| 17 | + tool_output_guardrail, |
| 18 | +) |
| 19 | +from agents.tool_context import ToolContext |
| 20 | + |
| 21 | + |
| 22 | +def test_run_config_positional_arguments_remain_backward_compatible() -> None: |
| 23 | + async def keep_handoff_input(data: HandoffInputData) -> HandoffInputData: |
| 24 | + return data |
| 25 | + |
| 26 | + config = RunConfig(None, MultiProvider(), None, keep_handoff_input) |
| 27 | + |
| 28 | + assert config.handoff_input_filter is keep_handoff_input |
| 29 | + assert config.session_settings is None |
| 30 | + |
| 31 | + |
| 32 | +def test_function_tool_positional_arguments_keep_guardrail_positions() -> None: |
| 33 | + async def invoke(_ctx: ToolContext[Any], _args: str) -> str: |
| 34 | + return "ok" |
| 35 | + |
| 36 | + @tool_input_guardrail |
| 37 | + def allow_input(_data: ToolInputGuardrailData) -> ToolGuardrailFunctionOutput: |
| 38 | + return ToolGuardrailFunctionOutput.allow() |
| 39 | + |
| 40 | + @tool_output_guardrail |
| 41 | + def allow_output(_data: ToolOutputGuardrailData) -> ToolGuardrailFunctionOutput: |
| 42 | + return ToolGuardrailFunctionOutput.allow() |
| 43 | + |
| 44 | + input_guardrails = [allow_input] |
| 45 | + output_guardrails = [allow_output] |
| 46 | + |
| 47 | + tool = FunctionTool( |
| 48 | + "tool_name", |
| 49 | + "tool_description", |
| 50 | + {"type": "object", "properties": {}}, |
| 51 | + invoke, |
| 52 | + True, |
| 53 | + True, |
| 54 | + input_guardrails, |
| 55 | + output_guardrails, |
| 56 | + ) |
| 57 | + |
| 58 | + assert tool.needs_approval is False |
| 59 | + assert tool.tool_input_guardrails is not None |
| 60 | + assert tool.tool_output_guardrails is not None |
| 61 | + assert tool.tool_input_guardrails[0] is allow_input |
| 62 | + assert tool.tool_output_guardrails[0] is allow_output |
| 63 | + |
| 64 | + |
| 65 | +def test_agent_hook_context_third_positional_argument_is_turn_input() -> None: |
| 66 | + turn_input = ItemHelpers.input_to_new_input_list("hello") |
| 67 | + context = AgentHookContext(None, Usage(), turn_input) |
| 68 | + |
| 69 | + assert context.turn_input == turn_input |
| 70 | + assert isinstance(context._approvals, dict) |
0 commit comments