Skip to content

Commit 074c010

Browse files
committed
PR comments
1 parent bbbf3fd commit 074c010

1 file changed

Lines changed: 65 additions & 13 deletions

File tree

tests/contrib/openai_agents/test_openai.py

Lines changed: 65 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,13 @@
5858
TResponseStreamEvent,
5959
)
6060
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
6164
from openai import APIStatusError, AsyncOpenAI, BaseModel
6265
from openai.types.responses import (
6366
ResponseCodeInterpreterToolCall,
67+
ResponseCustomToolCall,
6468
ResponseFileSearchToolCall,
6569
ResponseFunctionWebSearch,
6670
)
@@ -83,6 +87,7 @@
8387
StatefulMCPServerProvider,
8488
StatelessMCPServerProvider,
8589
)
90+
from temporalio.contrib.openai_agents._invoke_model_activity import _build_tool
8691
from temporalio.contrib.openai_agents._model_parameters import ModelSummaryProvider
8792
from temporalio.contrib.openai_agents._openai_runner import _convert_agent
8893
from temporalio.contrib.openai_agents._temporal_model_stub import (
@@ -1996,6 +2001,66 @@ async def test_hosted_mcp_tool(client: Client):
19962001
assert result == "Some language"
19972002

19982003

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+
19992064
class AssertDifferentModelProvider(ModelProvider):
20002065
model_names: set[str | None]
20012066

@@ -2539,13 +2604,6 @@ async def test_model_conversion_loops():
25392604

25402605

25412606
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-
25492607
class FakeSandboxSession:
25502608
pass
25512609

@@ -2581,12 +2639,6 @@ class FakeSandboxSession:
25812639

25822640

25832641
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-
25902642
async def stub(_ctx: Any, _payload: str) -> str:
25912643
return ""
25922644

0 commit comments

Comments
 (0)