11from datetime import timedelta
22
33from pydantic import BaseModel
4- from restack_ai .agent import agent , agent_info , import_functions , log
4+ from restack_ai .agent import NonRetryableError , agent , agent_info , import_functions , log
55
66with import_functions ():
77 from src .functions .livekit_call import LivekitCallInput , livekit_call
@@ -34,14 +34,18 @@ def __init__(self) -> None:
3434 async def messages (self , messages_event : MessagesEvent ) -> list [Message ]:
3535 log .info (f"Received message: { messages_event .messages } " )
3636 self .messages .extend (messages_event .messages )
37-
38- assistant_message = await agent .step (
39- function = llm_chat ,
40- function_input = LlmChatInput (messages = self .messages ),
41- start_to_close_timeout = timedelta (seconds = 120 ),
42- )
43- self .messages .append (Message (role = "assistant" , content = str (assistant_message )))
44- return self .messages
37+ try :
38+ assistant_message = await agent .step (
39+ function = llm_chat ,
40+ function_input = LlmChatInput (messages = self .messages ),
41+ start_to_close_timeout = timedelta (seconds = 120 ),
42+ )
43+ except Exception as e :
44+ error_message = f"Error during llm_chat: { e } "
45+ raise NonRetryableError (error_message ) from e
46+ else :
47+ self .messages .append (Message (role = "assistant" , content = str (assistant_message )))
48+ return self .messages
4549
4650 @agent .event
4751 async def call (self , call_input : CallInput ) -> None :
@@ -50,18 +54,29 @@ async def call(self, call_input: CallInput) -> None:
5054 agent_name = agent_info ().workflow_type
5155 agent_id = agent_info ().workflow_id
5256 run_id = agent_info ().run_id
53- sip_trunk_id = await agent .step (function = livekit_outbound_trunk )
54- return await agent .step (
55- function = livekit_call ,
56- function_input = LivekitCallInput (
57- sip_trunk_id = sip_trunk_id ,
58- phone_number = phone_number ,
59- room_id = self .room_id ,
60- agent_name = agent_name ,
61- agent_id = agent_id ,
62- run_id = run_id ,
63- ),
64- )
57+ try :
58+ sip_trunk_id = await agent .step (function = livekit_outbound_trunk )
59+ except Exception as e :
60+ error_message = f"Error during livekit_outbound_trunk: { e } "
61+ raise NonRetryableError (error_message ) from e
62+ else :
63+ try :
64+ result = await agent .step (
65+ function = livekit_call ,
66+ function_input = LivekitCallInput (
67+ sip_trunk_id = sip_trunk_id ,
68+ phone_number = phone_number ,
69+ room_id = self .room_id ,
70+ agent_name = agent_name ,
71+ agent_id = agent_id ,
72+ run_id = run_id ,
73+ ),
74+ )
75+ except Exception as e :
76+ error_message = f"Error during livekit_call: { e } "
77+ raise NonRetryableError (error_message ) from e
78+ else :
79+ return result
6580
6681 @agent .event
6782 async def end (self , end : EndEvent ) -> EndEvent :
@@ -73,9 +88,13 @@ async def end(self, end: EndEvent) -> EndEvent:
7388 async def run (self ) -> None :
7489 room = await agent .step (function = livekit_room )
7590 self .room_id = room .name
76-
77- await agent .step (
78- function = livekit_dispatch ,
79- function_input = LivekitDispatchInput (room_id = self .room_id ),
80- )
81- await agent .condition (lambda : self .end )
91+ try :
92+ await agent .step (
93+ function = livekit_dispatch ,
94+ function_input = LivekitDispatchInput (room_id = self .room_id ),
95+ )
96+ except Exception as e :
97+ error_message = f"Error during livekit_dispatch: { e } "
98+ raise NonRetryableError (error_message ) from e
99+ else :
100+ await agent .condition (lambda : self .end )
0 commit comments