33from temporalio import workflow
44
55with workflow .unsafe .imports_passed_through ():
6- from pydantic import BaseModel
76 from agents import (
87 Agent ,
98 HandoffOutputItem ,
109 ItemHelpers ,
1110 MessageOutputItem ,
11+ RunConfig ,
1212 RunContextWrapper ,
1313 Runner ,
1414 ToolCallItem ,
1515 ToolCallOutputItem ,
1616 TResponseInputItem ,
1717 function_tool ,
1818 handoff ,
19- trace , RunConfig ,
19+ trace ,
2020 )
2121 from agents .extensions .handoff_prompt import RECOMMENDED_PROMPT_PREFIX
22+ from pydantic import BaseModel
2223
2324
2425### CONTEXT
@@ -35,7 +36,8 @@ class AirlineAgentContext(BaseModel):
3536
3637
3738@function_tool (
38- name_override = "faq_lookup_tool" , description_override = "Lookup frequently asked questions."
39+ name_override = "faq_lookup_tool" ,
40+ description_override = "Lookup frequently asked questions." ,
3941)
4042async def faq_lookup_tool (question : str ) -> str :
4143 if "bag" in question or "baggage" in question :
@@ -57,7 +59,9 @@ async def faq_lookup_tool(question: str) -> str:
5759
5860@function_tool
5961async def update_seat (
60- context : RunContextWrapper [AirlineAgentContext ], confirmation_number : str , new_seat : str
62+ context : RunContextWrapper [AirlineAgentContext ],
63+ confirmation_number : str ,
64+ new_seat : str ,
6165) -> str :
6266 """
6367 Update the seat for a given confirmation number.
@@ -77,13 +81,16 @@ async def update_seat(
7781### HOOKS
7882
7983
80- async def on_seat_booking_handoff (context : RunContextWrapper [AirlineAgentContext ]) -> None :
84+ async def on_seat_booking_handoff (
85+ context : RunContextWrapper [AirlineAgentContext ],
86+ ) -> None :
8187 flight_number = f"FLT-{ workflow .random ().randint (100 , 999 )} "
8288 context .context .flight_number = flight_number
8389
8490
8591### AGENTS
8692
93+
8794def init_agents () -> Agent [AirlineAgentContext ]:
8895 """
8996 Initialize the agents for the airline customer service workflow.
@@ -141,7 +148,6 @@ class ProcessUserMessageInput(BaseModel):
141148
142149@workflow .defn
143150class CustomerServiceWorkflow :
144-
145151 def __init__ (self , input_items : list [TResponseInputItem ] = None ):
146152 self .run_config = RunConfig ()
147153 self .chat_history = []
@@ -152,7 +158,9 @@ def __init__(self, input_items: list[TResponseInputItem] = None):
152158 @workflow .run
153159 async def run (self , input_items : list [TResponseInputItem ] = None ):
154160 await workflow .wait_condition (
155- lambda : workflow .info ().is_continue_as_new_suggested () and workflow .all_handlers_finished ())
161+ lambda : workflow .info ().is_continue_as_new_suggested ()
162+ and workflow .all_handlers_finished ()
163+ )
156164 workflow .continue_as_new (self .input_items )
157165
158166 @workflow .query
@@ -165,23 +173,33 @@ async def process_user_message(self, input: ProcessUserMessageInput) -> list[str
165173 self .chat_history .append (f"User: { input .user_input } " )
166174 with trace ("Customer service" , group_id = workflow .info ().workflow_id ):
167175 self .input_items .append ({"content" : input .user_input , "role" : "user" })
168- result = await Runner .run (self .current_agent , self .input_items , context = self .context ,
169- run_config = self .run_config )
176+ result = await Runner .run (
177+ self .current_agent ,
178+ self .input_items ,
179+ context = self .context ,
180+ run_config = self .run_config ,
181+ )
170182
171183 for new_item in result .new_items :
172184 agent_name = new_item .agent .name
173185 if isinstance (new_item , MessageOutputItem ):
174- self .chat_history .append (f"{ agent_name } : { ItemHelpers .text_message_output (new_item )} " )
186+ self .chat_history .append (
187+ f"{ agent_name } : { ItemHelpers .text_message_output (new_item )} "
188+ )
175189 elif isinstance (new_item , HandoffOutputItem ):
176190 self .chat_history .append (
177191 f"Handed off from { new_item .source_agent .name } to { new_item .target_agent .name } "
178192 )
179193 elif isinstance (new_item , ToolCallItem ):
180194 self .chat_history .append (f"{ agent_name } : Calling a tool" )
181195 elif isinstance (new_item , ToolCallOutputItem ):
182- self .chat_history .append (f"{ agent_name } : Tool call output: { new_item .output } " )
196+ self .chat_history .append (
197+ f"{ agent_name } : Tool call output: { new_item .output } "
198+ )
183199 else :
184- self .chat_history .append (f"{ agent_name } : Skipping item: { new_item .__class__ .__name__ } " )
200+ self .chat_history .append (
201+ f"{ agent_name } : Skipping item: { new_item .__class__ .__name__ } "
202+ )
185203 self .input_items = result .to_input_list ()
186204 self .current_agent = result .last_agent
187205 workflow .set_current_details ("\n \n " .join (self .chat_history ))
0 commit comments