9696 TestModelProvider ,
9797)
9898from temporalio .contrib .openai_agents ._model_parameters import ModelSummaryProvider
99- from temporalio .contrib .openai_agents ._temporal_model_stub import _extract_summary
99+ from temporalio .contrib .openai_agents ._openai_runner import _convert_agent
100+ from temporalio .contrib .openai_agents ._temporal_model_stub import (
101+ _extract_summary ,
102+ _TemporalModelStub ,
103+ )
100104from temporalio .contrib .pydantic import pydantic_data_converter
101- from temporalio .exceptions import ApplicationError , CancelledError
105+ from temporalio .exceptions import ApplicationError , CancelledError , TemporalError
102106from temporalio .testing import WorkflowEnvironment
103107from temporalio .workflow import ActivityConfig
104108from tests .contrib .openai_agents .research_agents .research_manager import (
@@ -897,7 +901,10 @@ async def update_seat(
897901async def on_seat_booking_handoff (
898902 context : RunContextWrapper [AirlineAgentContext ],
899903) -> None :
900- flight_number = f"FLT-{ workflow .random ().randint (100 , 999 )} "
904+ try :
905+ flight_number = f"FLT-{ workflow .random ().randint (100 , 999 )} "
906+ except TemporalError :
907+ flight_number = "FLT-100"
901908 context .context .flight_number = flight_number
902909
903910
@@ -975,6 +982,8 @@ class CustomerServiceModel(StaticTestModel):
975982 ResponseBuilders .output_message (
976983 "Your seat has been updated to a window seat. If there's anything else you need, feel free to let me know!"
977984 ),
985+ ResponseBuilders .tool_call ("{}" , "transfer_to_triage_agent" ),
986+ ResponseBuilders .output_message ("You're welcome!" ),
978987 ]
979988
980989
@@ -988,10 +997,7 @@ def __init__(self, input_items: list[TResponseInputItem] = []):
988997
989998 @workflow .run
990999 async def run (self , input_items : list [TResponseInputItem ] = []):
991- await workflow .wait_condition (
992- lambda : workflow .info ().is_continue_as_new_suggested ()
993- and workflow .all_handlers_finished ()
994- )
1000+ await workflow .wait_condition (lambda : False )
9951001 workflow .continue_as_new (self .input_items )
9961002
9971003 @workflow .query
@@ -1062,7 +1068,13 @@ async def test_customer_service_workflow(client: Client, use_local_model: bool):
10621068 ]
10631069 client = Client (** new_config )
10641070
1065- questions = ["Hello" , "Book me a flight to PDX" , "11111" , "Any window seat" ]
1071+ questions = [
1072+ "Hello" ,
1073+ "Book me a flight to PDX" ,
1074+ "11111" ,
1075+ "Any window seat" ,
1076+ "Take me back to the triage agent to say goodbye" ,
1077+ ]
10661078
10671079 async with new_worker (
10681080 client ,
@@ -1101,7 +1113,7 @@ async def test_customer_service_workflow(client: Client, use_local_model: bool):
11011113 if e .HasField ("activity_task_completed_event_attributes" ):
11021114 events .append (e )
11031115
1104- assert len (events ) == 6
1116+ assert len (events ) == 8
11051117 assert (
11061118 "Hi there! How can I assist you today?"
11071119 in events [0 ]
@@ -1138,6 +1150,18 @@ async def test_customer_service_workflow(client: Client, use_local_model: bool):
11381150 .activity_task_completed_event_attributes .result .payloads [0 ]
11391151 .data .decode ()
11401152 )
1153+ assert (
1154+ "transfer_to_triage_agent"
1155+ in events [6 ]
1156+ .activity_task_completed_event_attributes .result .payloads [0 ]
1157+ .data .decode ()
1158+ )
1159+ assert (
1160+ "You're welcome!"
1161+ in events [7 ]
1162+ .activity_task_completed_event_attributes .result .payloads [0 ]
1163+ .data .decode ()
1164+ )
11411165
11421166
11431167class InputGuardrailModel (OpenAIResponsesModel ):
@@ -2571,3 +2595,17 @@ def override_get_activities() -> Sequence[Callable]:
25712595 err .value .cause .message
25722596 == "MCP Stateful Server Worker failed to schedule activity."
25732597 )
2598+
2599+
2600+ async def test_model_conversion_loops ():
2601+ agent = init_agents ()
2602+ converted = _convert_agent (ModelActivityParameters (), agent , None )
2603+ seat_booking_handoff = converted .handoffs [1 ]
2604+ assert isinstance (seat_booking_handoff , Handoff )
2605+ context : RunContextWrapper [AirlineAgentContext ] = RunContextWrapper (
2606+ context = AirlineAgentContext () # type: ignore
2607+ )
2608+ seat_booking_agent = await seat_booking_handoff .on_invoke_handoff (context , "" )
2609+ triage_agent = seat_booking_agent .handoffs [0 ]
2610+ assert isinstance (triage_agent , Agent )
2611+ assert isinstance (triage_agent .model , _TemporalModelStub )
0 commit comments