11from __future__ import annotations as _annotations
22
33from agents import (
4- Agent ,
4+ HandoffCallItem ,
55 HandoffOutputItem ,
66 ItemHelpers ,
77 MessageOutputItem ,
1212 TResponseInputItem ,
1313 trace ,
1414)
15+ from pydantic import dataclasses
1516from temporalio import workflow
1617
1718from openai_agents .customer_service .customer_service import (
2122)
2223
2324
25+ @dataclasses .dataclass
26+ class CustomerServiceWorkflowState :
27+ printed_history : list [str ]
28+ current_agent_name : str
29+ context : AirlineAgentContext
30+ input_items : list [TResponseInputItem ]
31+
32+
2433@workflow .defn
2534class CustomerServiceWorkflow :
2635 @workflow .init
27- def __init__ (self , input_items : list [TResponseInputItem ] | None = None ):
36+ def __init__ (
37+ self , customer_service_state : CustomerServiceWorkflowState | None = None
38+ ):
2839 self .run_config = RunConfig ()
29- self .chat_history : list [str ] = []
30- self .current_agent : Agent [AirlineAgentContext ] = init_agents ()
31- self .context = AirlineAgentContext ()
32- self .input_items = [] if input_items is None else input_items
40+
41+ starting_agent , self .agent_map = init_agents ()
42+ self .current_agent = (
43+ self .agent_map [customer_service_state .current_agent_name ]
44+ if customer_service_state
45+ else starting_agent
46+ )
47+ self .context = (
48+ customer_service_state .context
49+ if customer_service_state
50+ else AirlineAgentContext ()
51+ )
52+ self .printed_history : list [str ] = (
53+ customer_service_state .printed_history if customer_service_state else []
54+ )
55+ self .input_items = (
56+ customer_service_state .input_items if customer_service_state else []
57+ )
3358
3459 @workflow .run
35- async def run (self , input_items : list [TResponseInputItem ] | None = None ):
60+ async def run (
61+ self , customer_service_state : CustomerServiceWorkflowState | None = None
62+ ):
3663 await workflow .wait_condition (
3764 lambda : workflow .info ().is_continue_as_new_suggested ()
3865 and workflow .all_handlers_finished ()
3966 )
40- workflow .continue_as_new (self .input_items )
67+ workflow .continue_as_new (
68+ CustomerServiceWorkflowState (
69+ printed_history = self .printed_history ,
70+ current_agent_name = self .current_agent .name ,
71+ context = self .context ,
72+ input_items = self .input_items ,
73+ )
74+ )
4175
4276 @workflow .query
4377 def get_chat_history (self ) -> list [str ]:
44- return self .chat_history
78+ return self .printed_history
4579
4680 @workflow .update
4781 async def process_user_message (self , input : ProcessUserMessageInput ) -> list [str ]:
48- length = len (self .chat_history )
49- self .chat_history .append (f"User: { input .user_input } " )
82+ length = len (self .printed_history )
83+ self .printed_history .append (f"User: { input .user_input } " )
5084 with trace ("Customer service" , group_id = workflow .info ().workflow_id ):
5185 self .input_items .append ({"content" : input .user_input , "role" : "user" })
5286 result = await Runner .run (
@@ -59,33 +93,38 @@ async def process_user_message(self, input: ProcessUserMessageInput) -> list[str
5993 for new_item in result .new_items :
6094 agent_name = new_item .agent .name
6195 if isinstance (new_item , MessageOutputItem ):
62- self .chat_history .append (
96+ self .printed_history .append (
6397 f"{ agent_name } : { ItemHelpers .text_message_output (new_item )} "
6498 )
6599 elif isinstance (new_item , HandoffOutputItem ):
66- self .chat_history .append (
100+ self .printed_history .append (
67101 f"Handed off from { new_item .source_agent .name } to { new_item .target_agent .name } "
68102 )
103+ elif isinstance (new_item , HandoffCallItem ):
104+ self .printed_history .append (
105+ f"{ agent_name } : Handed off to tool { new_item .raw_item .name } "
106+ )
69107 elif isinstance (new_item , ToolCallItem ):
70- self .chat_history .append (f"{ agent_name } : Calling a tool" )
108+ self .printed_history .append (f"{ agent_name } : Calling a tool" )
71109 elif isinstance (new_item , ToolCallOutputItem ):
72- self .chat_history .append (
110+ self .printed_history .append (
73111 f"{ agent_name } : Tool call output: { new_item .output } "
74112 )
75113 else :
76- self .chat_history .append (
114+ self .printed_history .append (
77115 f"{ agent_name } : Skipping item: { new_item .__class__ .__name__ } "
78116 )
79117 self .input_items = result .to_input_list ()
80118 self .current_agent = result .last_agent
81- workflow .set_current_details ("\n \n " .join (self .chat_history ))
82- return self .chat_history [length :]
119+ workflow .set_current_details ("\n \n " .join (self .printed_history ))
120+
121+ return self .printed_history [length :]
83122
84123 @process_user_message .validator
85124 def validate_process_user_message (self , input : ProcessUserMessageInput ) -> None :
86125 if not input .user_input :
87126 raise ValueError ("User input cannot be empty." )
88127 if len (input .user_input ) > 1000 :
89128 raise ValueError ("User input is too long. Please limit to 1000 characters." )
90- if input .chat_length != len (self .chat_history ):
129+ if input .chat_length != len (self .printed_history ):
91130 raise ValueError ("Stale chat history. Please refresh the chat." )
0 commit comments