|
58 | 58 | TResponseStreamEvent, |
59 | 59 | ) |
60 | 60 | from agents.mcp import MCPServer, MCPServerStdio |
| 61 | +from agents.sandbox.capabilities.tools import SandboxApplyPatchTool |
| 62 | +from agents.tool import CustomTool |
| 63 | +from agents.tool_context import ToolContext |
61 | 64 | from openai import APIStatusError, AsyncOpenAI, BaseModel |
62 | 65 | from openai.types.responses import ( |
63 | 66 | ResponseCodeInterpreterToolCall, |
| 67 | + ResponseCustomToolCall, |
64 | 68 | ResponseFileSearchToolCall, |
65 | 69 | ResponseFunctionWebSearch, |
66 | 70 | ) |
|
83 | 87 | StatefulMCPServerProvider, |
84 | 88 | StatelessMCPServerProvider, |
85 | 89 | ) |
| 90 | +from temporalio.contrib.openai_agents._invoke_model_activity import _build_tool |
86 | 91 | from temporalio.contrib.openai_agents._model_parameters import ModelSummaryProvider |
87 | 92 | from temporalio.contrib.openai_agents._openai_runner import _convert_agent |
88 | 93 | from temporalio.contrib.openai_agents._temporal_model_stub import ( |
@@ -1996,6 +2001,66 @@ async def test_hosted_mcp_tool(client: Client): |
1996 | 2001 | assert result == "Some language" |
1997 | 2002 |
|
1998 | 2003 |
|
| 2004 | +def custom_tool_mock_model(): |
| 2005 | + return TestModel.returning_responses( |
| 2006 | + [ |
| 2007 | + ModelResponse( |
| 2008 | + output=[ |
| 2009 | + ResponseCustomToolCall( |
| 2010 | + call_id="c1", |
| 2011 | + input="ping", |
| 2012 | + name="echo", |
| 2013 | + type="custom_tool_call", |
| 2014 | + ) |
| 2015 | + ], |
| 2016 | + usage=Usage(), |
| 2017 | + response_id=None, |
| 2018 | + ), |
| 2019 | + ResponseBuilders.output_message("done"), |
| 2020 | + ] |
| 2021 | + ) |
| 2022 | + |
| 2023 | + |
| 2024 | +@workflow.defn |
| 2025 | +class CustomToolWorkflow: |
| 2026 | + @workflow.run |
| 2027 | + async def run(self) -> str: |
| 2028 | + captured: list[str] = [] |
| 2029 | + |
| 2030 | + async def echo(ctx: ToolContext[Any], input: str) -> str: # type: ignore[reportUnusedParameter] |
| 2031 | + captured.append(input) |
| 2032 | + return input |
| 2033 | + |
| 2034 | + agent = Agent[str]( |
| 2035 | + name="custom-tool-agent", |
| 2036 | + instructions="Use the echo tool.", |
| 2037 | + tools=[ |
| 2038 | + CustomTool( |
| 2039 | + name="echo", |
| 2040 | + description="Echo the input string back.", |
| 2041 | + on_invoke_tool=echo, |
| 2042 | + ) |
| 2043 | + ], |
| 2044 | + ) |
| 2045 | + result = await Runner.run(starting_agent=agent, input="say something") |
| 2046 | + return f"{result.final_output}:{captured[0]}" |
| 2047 | + |
| 2048 | + |
| 2049 | +async def test_custom_tool_workflow(client: Client): |
| 2050 | + async with AgentEnvironment(model=custom_tool_mock_model()) as env: |
| 2051 | + client = env.applied_on_client(client) |
| 2052 | + |
| 2053 | + async with new_worker(client, CustomToolWorkflow) as worker: |
| 2054 | + workflow_handle = await client.start_workflow( |
| 2055 | + CustomToolWorkflow.run, |
| 2056 | + id=f"custom-tool-workflow-{uuid.uuid4()}", |
| 2057 | + task_queue=worker.task_queue, |
| 2058 | + execution_timeout=timedelta(seconds=30), |
| 2059 | + ) |
| 2060 | + result = await workflow_handle.result() |
| 2061 | + assert result == "done:ping" |
| 2062 | + |
| 2063 | + |
1999 | 2064 | class AssertDifferentModelProvider(ModelProvider): |
2000 | 2065 | model_names: set[str | None] |
2001 | 2066 |
|
@@ -2539,13 +2604,6 @@ async def test_model_conversion_loops(): |
2539 | 2604 |
|
2540 | 2605 |
|
2541 | 2606 | def test_sandbox_apply_patch_tool_round_trips_through_activity_input(): |
2542 | | - from agents.sandbox.capabilities.tools import SandboxApplyPatchTool |
2543 | | - from agents.tool import CustomTool |
2544 | | - |
2545 | | - from temporalio.contrib.openai_agents._invoke_model_activity import ( |
2546 | | - _build_tool, |
2547 | | - ) |
2548 | | - |
2549 | 2607 | class FakeSandboxSession: |
2550 | 2608 | pass |
2551 | 2609 |
|
@@ -2581,12 +2639,6 @@ class FakeSandboxSession: |
2581 | 2639 |
|
2582 | 2640 |
|
2583 | 2641 | def test_custom_tool_with_defer_loading_round_trips_through_activity_input(): |
2584 | | - from agents.tool import CustomTool |
2585 | | - |
2586 | | - from temporalio.contrib.openai_agents._invoke_model_activity import ( |
2587 | | - _build_tool, |
2588 | | - ) |
2589 | | - |
2590 | 2642 | async def stub(_ctx: Any, _payload: str) -> str: |
2591 | 2643 | return "" |
2592 | 2644 |
|
|
0 commit comments