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