Skip to content

Commit aa77a95

Browse files
committed
Simplify the "warm transfer" demo to use the new "function" action type in lieu of a bunch of custom code
1 parent ec087d0 commit aa77a95

File tree

1 file changed

+11
-50
lines changed

1 file changed

+11
-50
lines changed

examples/static/warm_transfer.py

Lines changed: 11 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -148,14 +148,11 @@ async def pre_transferring_to_human_agent(action: dict, flow_manager: FlowManage
148148
}
149149
)
150150

151-
async def queue_post_transferring_to_human_agent(action: dict, flow_manager: FlowManager):
152-
task = flow_manager.task
153-
await task.queue_frame(PostTransferringToHumanAgentFrame())
154-
155151
# NOTE: this isn't a "real" post-action because it needs to run after the "transferring you to a
156152
# human agent" speech is actually spoken, not just after sending the LLM the instruction to do so.
157-
async def post_transferring_to_human_agent(transport: DailyTransport):
153+
async def post_transferring_to_human_agent(action: dict, flow_manager: FlowManager):
158154
"""Post-action after starting transferring to the human agent."""
155+
transport: DailyTransport = flow_manager.transport
159156
customer_participant_id = get_customer_participant_id(transport=transport)
160157

161158
# Update the customer:
@@ -178,15 +175,12 @@ async def post_transferring_to_human_agent(transport: DailyTransport):
178175
}
179176
)
180177

181-
async def queue_post_end_human_agent_conversation(action: dict, flow_manager: FlowManager):
182-
task = flow_manager.task
183-
await task.queue_frame(PostEndHumanAgentConversationFrame())
184-
185178
# NOTE: this isn't a "real" post-action because it needs to run after the "I'm patching you through
186179
# to the customer" speech is actually spoken, not just after sending the LLM the instruction to do
187180
# so.
188-
async def post_end_human_agent_conversation(transport: DailyTransport):
181+
async def post_end_human_agent_conversation(action: dict, flow_manager: FlowManager):
189182
"""Post-action after starting to end the conversation with the human agent, when the agent is being patched through to the customer."""
183+
transport: DailyTransport = flow_manager.transport
190184
customer_participant_id = get_customer_participant_id(transport=transport)
191185
agent_participant_id = get_human_agent_participant_id(transport=transport)
192186

@@ -368,15 +362,14 @@ def create_transferring_to_human_agent_node() -> NodeConfig:
368362
functions=[],
369363
pre_actions=[
370364
ActionConfig(
371-
type="pre_transferring_to_human_agent",
365+
type="function",
372366
handler=pre_transferring_to_human_agent
373-
)
367+
),
374368
],
375369
post_actions=[
376-
# NOTE: "real" post action (post_transferring_to_human_agent) is triggered by CustomControlProcessor
377370
ActionConfig(
378-
type="queue_post_transferring_to_human_agent",
379-
handler=queue_post_transferring_to_human_agent
371+
type="function",
372+
handler=post_transferring_to_human_agent
380373
)
381374
]
382375
)
@@ -443,10 +436,9 @@ def create_end_human_agent_conversation_node() -> NodeConfig:
443436
],
444437
functions=[],
445438
post_actions=[
446-
# NOTE: "real" post action (post_end_human_agent_conversation) is triggered by CustomControlProcessor
447439
ActionConfig(
448-
type="queue_post_end_human_agent_conversation",
449-
handler=queue_post_end_human_agent_conversation
440+
type="function",
441+
handler=post_end_human_agent_conversation
450442
),
451443
ActionConfig(type="end_conversation")
452444
]
@@ -543,35 +535,6 @@ async def get_token(user_id: str, permissions: dict, daily_rest_helper: DailyRES
543535
))
544536
)
545537

546-
@dataclass
547-
class PostTransferringToHumanAgentFrame(ControlFrame):
548-
"""
549-
Indicates that the bot has finished speaking the "transferring you to human agent" speech.
550-
"""
551-
pass
552-
553-
@dataclass
554-
class PostEndHumanAgentConversationFrame(ControlFrame):
555-
"""
556-
Indicates that the bot has finished speaking the "I'm patching you through to the customer" speech.
557-
"""
558-
pass
559-
560-
class CustomControlProcessor(FrameProcessor):
561-
def __init__(self, transport: DailyTransport):
562-
super().__init__()
563-
self.__transport = transport
564-
565-
async def process_frame(self, frame: Frame, direction: FrameDirection):
566-
await super().process_frame(frame, direction)
567-
568-
if isinstance(frame, PostTransferringToHumanAgentFrame):
569-
await post_transferring_to_human_agent(transport=self.__transport)
570-
elif isinstance(frame, PostEndHumanAgentConversationFrame):
571-
await post_end_human_agent_conversation(transport=self.__transport)
572-
573-
await self.push_frame(frame, direction)
574-
575538
async def main():
576539
"""Main function to set up and run the bot."""
577540

@@ -596,7 +559,6 @@ async def main():
596559
voice_id="820a3788-2b37-4d21-847a-b65d8a68c99a", # Salesman
597560
)
598561
llm = GoogleLLMService(api_key=os.getenv("GOOGLE_API_KEY"))
599-
custom_control_processor = CustomControlProcessor(transport=transport)
600562

601563
# Initialize context
602564
context = OpenAILLMContext()
@@ -611,8 +573,7 @@ async def main():
611573
llm,
612574
tts,
613575
transport.output(),
614-
context_aggregator.assistant(),
615-
custom_control_processor
576+
context_aggregator.assistant()
616577
]
617578
)
618579
task = PipelineTask(pipeline=pipeline, params=PipelineParams(allow_interruptions=True))

0 commit comments

Comments
 (0)