11from datetime import timedelta
22
33from pydantic import BaseModel , Field
4- from restack_ai .agent import agent , import_functions , log
4+ from restack_ai .agent import agent , import_functions , log , NonRetryableError
55
66with import_functions ():
77 from src .functions .livekit_dispatch import LivekitDispatchInput , livekit_dispatch
@@ -29,14 +29,18 @@ def __init__(self) -> None:
2929 async def messages (self , messages_event : MessagesEvent ) -> list [Message ]:
3030 log .info (f"Received message: { messages_event .messages } " )
3131 self .messages .extend (messages_event .messages )
32-
33- assistant_message = await agent .step (
34- function = llm_chat ,
35- function_input = LlmChatInput (messages = self .messages ),
36- start_to_close_timeout = timedelta (minutes = 2 ),
37- )
38- self .messages .append (Message (role = "assistant" , content = str (assistant_message )))
39- return self .messages
32+ try :
33+ assistant_message = await agent .step (
34+ function = llm_chat ,
35+ function_input = LlmChatInput (messages = self .messages ),
36+ start_to_close_timeout = timedelta (minutes = 2 ),
37+ )
38+ except Exception as e :
39+ error_message = f"Error during llm_chat: { e } "
40+ raise NonRetryableError (error_message ) from e
41+ else :
42+ self .messages .append (Message (role = "assistant" , content = str (assistant_message )))
43+ return self .messages
4044
4145 @agent .event
4246 async def end (self , end : EndEvent ) -> EndEvent :
@@ -48,9 +52,13 @@ async def end(self, end: EndEvent) -> EndEvent:
4852 async def run (self , agent_input : AgentVoiceInput ) -> None :
4953 log .info ("Run" , agent_input = agent_input )
5054 room_id = agent_input .room_id
51-
52- await agent .step (
53- function = livekit_dispatch ,
54- function_input = LivekitDispatchInput (room_id = room_id ),
55- )
56- await agent .condition (lambda : self .end )
55+ try :
56+ await agent .step (
57+ function = livekit_dispatch ,
58+ function_input = LivekitDispatchInput (room_id = room_id ),
59+ )
60+ except Exception as e :
61+ error_message = f"Error during livekit_dispatch: { e } "
62+ raise NonRetryableError (error_message ) from e
63+ else :
64+ await agent .condition (lambda : self .end )
0 commit comments