Skip to content

Commit 7eb70b7

Browse files
committed
Update agent-voice example
1 parent 8b24242 commit 7eb70b7

File tree

4 files changed

+28
-20
lines changed

4 files changed

+28
-20
lines changed

agent_voice/livekit/agent/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ dependencies = [
1010
"watchfiles>=1.0.4",
1111
"python-dotenv==1.0.1",
1212
"openai>=1.61.0",
13-
"restack-ai>=0.0.77",
1413
"livekit-api>=0.8.2",
14+
"restack-ai>=0.0.78",
1515
]
1616

1717
[project.scripts]

agent_voice/livekit/agent/src/agents/agent.py

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from datetime import timedelta
22

33
from 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

66
with 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)

agent_voice/livekit/agent/src/functions/livekit_dispatch.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from livekit import api
55
from livekit.protocol.agent_dispatch import AgentDispatch
6-
from restack_ai.function import function, function_info, log
6+
from restack_ai.function import function, function_info, log, NonRetryableError
77

88

99
@dataclass
@@ -38,7 +38,7 @@ async def livekit_dispatch(function_input: LivekitDispatchInput) -> AgentDispatc
3838

3939
except Exception as e:
4040
log.error("livekit_dispatch function failed", error=str(e))
41-
raise
41+
raise NonRetryableError(f"Livekit dispatch failed: {e}") from e
4242

4343
else:
4444
return dispatch

agent_voice/livekit/agent/src/functions/llm_chat.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from openai import OpenAI
55
from pydantic import BaseModel, Field
6-
from restack_ai.function import function, log, stream_to_websocket
6+
from restack_ai.function import function, log, stream_to_websocket, NonRetryableError
77

88
from src.client import api_address
99

@@ -49,4 +49,4 @@ async def llm_chat(function_input: LlmChatInput) -> str:
4949

5050
except Exception as e:
5151
log.error("llm_chat function failed", error=str(e))
52-
raise
52+
raise NonRetryableError(f"LLM chat failed: {e}") from e

0 commit comments

Comments
 (0)