|
1 | 1 | from __future__ import annotations as _annotations |
2 | 2 |
|
| 3 | +import asyncio |
| 4 | +from datetime import timedelta |
| 5 | + |
3 | 6 | from agents import ( |
4 | | - Agent, |
| 7 | + HandoffCallItem, |
5 | 8 | HandoffOutputItem, |
6 | 9 | ItemHelpers, |
7 | 10 | MessageOutputItem, |
|
12 | 15 | TResponseInputItem, |
13 | 16 | trace, |
14 | 17 | ) |
| 18 | +from pydantic import BaseModel, dataclasses |
15 | 19 | from temporalio import workflow |
16 | 20 |
|
17 | 21 | from openai_agents.workflows.customer_service import ( |
|
21 | 25 | ) |
22 | 26 |
|
23 | 27 |
|
| 28 | +@dataclasses.dataclass |
| 29 | +class CustomerServiceWorkflowState: |
| 30 | + printed_history: list[str] |
| 31 | + current_agent_name: str |
| 32 | + context: AirlineAgentContext |
| 33 | + input_items: list[dict] # Store as plain dictionaries to avoid serialization issues |
| 34 | + |
| 35 | + |
24 | 36 | @workflow.defn |
25 | 37 | class CustomerServiceWorkflow: |
26 | 38 | @workflow.init |
27 | | - def __init__(self, input_items: list[TResponseInputItem] | None = None): |
| 39 | + def __init__( |
| 40 | + self, customer_service_state: CustomerServiceWorkflowState | None = None |
| 41 | + ): |
28 | 42 | 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 |
33 | 43 |
|
34 | | - @workflow.run |
35 | | - async def run(self, input_items: list[TResponseInputItem] | None = None): |
36 | | - await workflow.wait_condition( |
37 | | - lambda: workflow.info().is_continue_as_new_suggested() |
38 | | - and workflow.all_handlers_finished() |
| 44 | + starting_agent, self.agent_map = init_agents() |
| 45 | + self.current_agent = ( |
| 46 | + self.agent_map[customer_service_state.current_agent_name] |
| 47 | + if customer_service_state |
| 48 | + else starting_agent |
| 49 | + ) |
| 50 | + self.context = ( |
| 51 | + customer_service_state.context |
| 52 | + if customer_service_state |
| 53 | + else AirlineAgentContext() |
39 | 54 | ) |
40 | | - workflow.continue_as_new(self.input_items) |
| 55 | + |
| 56 | + self.printed_history: list[str] = ( |
| 57 | + customer_service_state.printed_history if customer_service_state else [] |
| 58 | + ) |
| 59 | + |
| 60 | + self.input_items = ( |
| 61 | + customer_service_state.input_items if customer_service_state else [] |
| 62 | + ) |
| 63 | + |
| 64 | + # Communication channels |
| 65 | + self.user_input_queue: asyncio.Queue[str] = asyncio.Queue() |
| 66 | + self.update_condition: asyncio.Condition = asyncio.Condition() |
| 67 | + |
| 68 | + @workflow.run |
| 69 | + async def run( |
| 70 | + self, customer_service_state: CustomerServiceWorkflowState | None = None |
| 71 | + ): |
| 72 | + while True: |
| 73 | + with trace("Customer service", group_id=workflow.info().workflow_id): |
| 74 | + user_input = await self.user_input_queue.get() |
| 75 | + self.input_items.append({"content": user_input, "role": "user"}) |
| 76 | + result = await Runner.run( |
| 77 | + self.current_agent, |
| 78 | + self.input_items, |
| 79 | + context=self.context, |
| 80 | + run_config=self.run_config, |
| 81 | + ) |
| 82 | + self.printed_history.append(f"Enter your message: {user_input}") |
| 83 | + for new_item in result.new_items: |
| 84 | + agent_name = new_item.agent.name |
| 85 | + if isinstance(new_item, MessageOutputItem): |
| 86 | + self.printed_history.append( |
| 87 | + f"{agent_name}: {ItemHelpers.text_message_output(new_item)}" |
| 88 | + ) |
| 89 | + elif isinstance(new_item, HandoffOutputItem): |
| 90 | + self.printed_history.append( |
| 91 | + f"Handed off from {new_item.source_agent.name} to {new_item.target_agent.name}" |
| 92 | + ) |
| 93 | + elif isinstance(new_item, HandoffCallItem): |
| 94 | + self.printed_history.append( |
| 95 | + f"{agent_name}: Handed off to tool {new_item.raw_item.name}" |
| 96 | + ) |
| 97 | + elif isinstance(new_item, ToolCallItem): |
| 98 | + self.printed_history.append(f"{agent_name}: Calling a tool") |
| 99 | + elif isinstance(new_item, ToolCallOutputItem): |
| 100 | + self.printed_history.append( |
| 101 | + f"{agent_name}: Tool call output: {new_item.output}" |
| 102 | + ) |
| 103 | + else: |
| 104 | + self.printed_history.append( |
| 105 | + f"{agent_name}: Skipping item: {new_item.__class__.__name__}" |
| 106 | + ) |
| 107 | + self.input_items = result.to_input_list() |
| 108 | + self.current_agent = result.last_agent |
| 109 | + async with self.update_condition: |
| 110 | + self.update_condition.notify_all() |
| 111 | + |
| 112 | + if workflow.info().is_continue_as_new_suggested(): |
| 113 | + await workflow.wait_condition( |
| 114 | + lambda: workflow.all_handlers_finished(), |
| 115 | + timeout=timedelta(seconds=10), |
| 116 | + timeout_summary="Continue as new timeout - deadlock avoidance", |
| 117 | + ) |
| 118 | + |
| 119 | + # Convert input_items to plain dictionaries for serialization |
| 120 | + serializable_input_items = [] |
| 121 | + for item in self.input_items: |
| 122 | + if hasattr(item, "model_dump"): |
| 123 | + # Convert Pydantic objects to dictionaries |
| 124 | + serializable_input_items.append(item.model_dump()) |
| 125 | + else: |
| 126 | + # Already a plain Python object |
| 127 | + serializable_input_items.append(item) |
| 128 | + workflow.continue_as_new( |
| 129 | + CustomerServiceWorkflowState( |
| 130 | + printed_history=self.printed_history, |
| 131 | + current_agent_name=self.current_agent.name, |
| 132 | + context=self.context, |
| 133 | + input_items=serializable_input_items, |
| 134 | + ) |
| 135 | + ) |
41 | 136 |
|
42 | 137 | @workflow.query |
43 | 138 | def get_chat_history(self) -> list[str]: |
44 | | - return self.chat_history |
| 139 | + return self.printed_history |
45 | 140 |
|
46 | 141 | @workflow.update |
47 | 142 | 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}") |
50 | | - with trace("Customer service", group_id=workflow.info().workflow_id): |
51 | | - self.input_items.append({"content": input.user_input, "role": "user"}) |
52 | | - result = await Runner.run( |
53 | | - self.current_agent, |
54 | | - self.input_items, |
55 | | - context=self.context, |
56 | | - run_config=self.run_config, |
| 143 | + length = len(self.printed_history) |
| 144 | + self.user_input_queue.put_nowait(input.user_input) |
| 145 | + async with self.update_condition: |
| 146 | + await self.update_condition.wait_for( |
| 147 | + lambda: len(self.printed_history) > length |
57 | 148 | ) |
58 | | - |
59 | | - for new_item in result.new_items: |
60 | | - agent_name = new_item.agent.name |
61 | | - if isinstance(new_item, MessageOutputItem): |
62 | | - self.chat_history.append( |
63 | | - f"{agent_name}: {ItemHelpers.text_message_output(new_item)}" |
64 | | - ) |
65 | | - elif isinstance(new_item, HandoffOutputItem): |
66 | | - self.chat_history.append( |
67 | | - f"Handed off from {new_item.source_agent.name} to {new_item.target_agent.name}" |
68 | | - ) |
69 | | - elif isinstance(new_item, ToolCallItem): |
70 | | - self.chat_history.append(f"{agent_name}: Calling a tool") |
71 | | - elif isinstance(new_item, ToolCallOutputItem): |
72 | | - self.chat_history.append( |
73 | | - f"{agent_name}: Tool call output: {new_item.output}" |
74 | | - ) |
75 | | - else: |
76 | | - self.chat_history.append( |
77 | | - f"{agent_name}: Skipping item: {new_item.__class__.__name__}" |
78 | | - ) |
79 | | - self.input_items = result.to_input_list() |
80 | | - self.current_agent = result.last_agent |
81 | | - workflow.set_current_details("\n\n".join(self.chat_history)) |
82 | | - return self.chat_history[length:] |
| 149 | + return self.printed_history[length:] |
83 | 150 |
|
84 | 151 | @process_user_message.validator |
85 | 152 | def validate_process_user_message(self, input: ProcessUserMessageInput) -> None: |
86 | 153 | if not input.user_input: |
87 | 154 | raise ValueError("User input cannot be empty.") |
88 | 155 | if len(input.user_input) > 1000: |
89 | 156 | raise ValueError("User input is too long. Please limit to 1000 characters.") |
90 | | - if input.chat_length != len(self.chat_history): |
| 157 | + if input.chat_length != len(self.printed_history): |
91 | 158 | raise ValueError("Stale chat history. Please refresh the chat.") |
0 commit comments