Platform for stateful agents: AI with advanced memory that can learn and self-improve over time.
fix: Fix voice agent and tests (#2305)
Matthew Zhou3f6a7108945e
diff --git a/letta/agents/voice_agent.py b/letta/agents/voice_agent.pyindex 7f1240384..5451dc6ce 100644--- a/letta/agents/voice_agent.py+++ b/letta/agents/voice_agent.py@@ -299,7 +299,7 @@ class VoiceAgent(BaseAgent):num_messages: int | None = None,num_archival_memories: int | None = None,) -> List[Message]:- return super()._rebuild_memory_async(+ return await super()._rebuild_memory_async(in_context_messages, agent_state, num_messages=self.num_messages, num_archival_memories=self.num_archival_memories)diff --git a/tests/integration_test_voice_agent.py b/tests/integration_test_voice_agent.pyindex 835a09e6e..b3fc86dcd 100644--- a/tests/integration_test_voice_agent.py+++ b/tests/integration_test_voice_agent.py@@ -1,10 +1,10 @@import os-import threading+import subprocess+import sysfrom unittest.mock import MagicMockimport pytestfrom dotenv import load_dotenv-from letta_client import AsyncLettafrom openai import AsyncOpenAIfrom openai.types.chat import ChatCompletionChunk@@ -35,7 +35,7 @@ from letta.services.summarizer.summarizer import Summarizerfrom letta.services.tool_manager import ToolManagerfrom letta.services.user_manager import UserManagerfrom letta.utils import get_persona_text-from tests.utils import wait_for_server+from tests.utils import create_tool_from_func, wait_for_serverMESSAGE_TRANSCRIPTS = ["user: Hey, I’ve been thinking about planning a road trip up the California coast next month.",@@ -92,17 +92,6 @@ You’re a memory-recall helper for an AI that can only keep the last 4 messages# --- Server Management --- #[email protected](scope="module")-def server():- config = LettaConfig.load()- print("CONFIG PATH", config.config_path)-- config.save()-- server = SyncServer()- return server--def _run_server():"""Starts the Letta server in a background thread."""load_dotenv()@@ -111,31 +100,66 @@ def _run_server():start_server(debug=True)[email protected](scope="session")[email protected](scope="module")def server_url():- """Ensures a server is running and returns its base URL."""- url = os.getenv("LETTA_SERVER_URL", "http://localhost:8283")+ """+ Starts the Letta HTTP server in a separate process using the 'uvicorn' CLI,+ so its event loop and DB pool stay completely isolated from pytest-asyncio.+ """+ url = os.getenv("LETTA_SERVER_URL", "http://127.0.0.1:8283")+ # Only spawn our own server if the user hasn't overridden LETTA_SERVER_URLif not os.getenv("LETTA_SERVER_URL"):- thread = threading.Thread(target=_run_server, daemon=True)- thread.start()- wait_for_server(url) # Allow server startup time+ # Build the command to launch uvicorn on your FastAPI app+ cmd = [+ sys.executable,+ "-m",+ "uvicorn",+ "letta.server.rest_api.app:app",+ "--host",+ "127.0.0.1",+ "--port",+ "8283",+ ]+ # If you need TLS or reload settings from start_server(), you can add+ # "--reload" or "--ssl-keyfile", "--ssl-certfile" here as well.++ server_proc = subprocess.Popen(+ cmd,+ stdout=subprocess.DEVNULL,+ stderr=subprocess.DEVNULL,+ )++ # wait until the HTTP port is accepting connections+ wait_for_server(url)++ yield url++ # Teardown: kill the subprocess if we started it+ server_proc.terminate()+ server_proc.wait(timeout=10)+ else:+ yield url+- return url[email protected](scope="module")+def server():+ config = LettaConfig.load()+ print("CONFIG PATH", config.config_path)+ config.save()-# --- Client Setup --- #+ server = SyncServer()+ actor = server.user_manager.get_user_or_default()+ server.tool_manager.upsert_base_tools(actor=actor)+ return server[email protected](scope="session")-def client(server_url):- """Creates a REST client for testing."""- client = AsyncLetta(base_url=server_url)- yield client+# --- Client Setup --- #[email protected](scope="function")-async def roll_dice_tool(client):+async def roll_dice_tool(server):def roll_dice():"""Rolls a 6 sided die.@@ -145,13 +169,13 @@ async def roll_dice_tool(client):"""return "Rolled a 10!"- tool = await client.tools.upsert_from_function(func=roll_dice)- # Yield the created tool+ actor = server.user_manager.get_user_or_default()+ tool = server.tool_manager.create_or_update_tool(create_tool_from_func(func=roll_dice), actor=actor)yield tool[email protected](scope="function")-async def weather_tool(client):+async def weather_tool(server):def get_weather(location: str) -> str:"""Fetches the current weather for a given location.@@ -176,22 +200,20 @@ async def weather_tool(client):else:raise RuntimeError(f"Failed to get weather data, status code: {response.status_code}")- tool = await client.tools.upsert_from_function(func=get_weather)- # Yield the created tool+ actor = server.user_manager.get_user_or_default()+ tool = server.tool_manager.create_or_update_tool(create_tool_from_func(func=get_weather), actor=actor)yield tool[email protected](scope="function")def composio_gmail_get_profile_tool(default_user):tool_create = ToolCreate.from_composio(action_name="GMAIL_GET_PROFILE")tool = ToolManager().create_or_update_composio_tool(tool_create=tool_create, actor=default_user)yield tool[email protected](scope="function")def voice_agent(server, actor):- server.tool_manager.upsert_base_tools(actor=actor)-main_agent = server.create_agent(request=CreateAgent(agent_type=AgentType.voice_convo_agent,@@ -268,9 +290,9 @@ def _assert_valid_chunk(chunk, idx, chunks):# --- Tests --- #[email protected](loop_scope="session")[email protected](loop_scope="module")@pytest.mark.parametrize("model", ["openai/gpt-4o-mini", "anthropic/claude-3-5-sonnet-20241022"])-async def test_model_compatibility(disable_e2b_api_key, voice_agent, model, server, group_id, actor):+async def test_model_compatibility(disable_e2b_api_key, voice_agent, model, server, server_url, group_id, actor):request = _get_chat_request("How are you?")server.tool_manager.upsert_base_tools(actor=actor)@@ -303,10 +325,10 @@ async def test_model_compatibility(disable_e2b_api_key, voice_agent, model, servprint(chunk.choices[0].delta.content)[email protected](loop_scope="session")[email protected](loop_scope="module")@pytest.mark.parametrize("message", ["Use search memory tool to recall what my name is."])@pytest.mark.parametrize("endpoint", ["v1/voice-beta"])-async def test_voice_recall_memory(disable_e2b_api_key, voice_agent, message, endpoint):+async def test_voice_recall_memory(disable_e2b_api_key, voice_agent, message, endpoint, server_url):"""Tests chat completion streaming using the Async OpenAI client."""request = _get_chat_request(message)@@ -318,9 +340,9 @@ async def test_voice_recall_memory(disable_e2b_api_key, voice_agent, message, enprint(chunk.choices[0].delta.content)[email protected](loop_scope="session")[email protected](loop_scope="module")@pytest.mark.parametrize("endpoint", ["v1/voice-beta"])-async def test_trigger_summarization(disable_e2b_api_key, server, voice_agent, group_id, endpoint, actor):+async def test_trigger_summarization(disable_e2b_api_key, server, voice_agent, group_id, endpoint, actor, server_url):server.group_manager.modify_group(group_id=group_id,group_update=GroupUpdate(@@ -350,8 +372,8 @@ async def test_trigger_summarization(disable_e2b_api_key, server, voice_agent, gprint(chunk.choices[0].delta.content)[email protected](loop_scope="session")-async def test_summarization(disable_e2b_api_key, voice_agent):[email protected](loop_scope="module")+async def test_summarization(disable_e2b_api_key, voice_agent, server_url):agent_manager = AgentManager()user_manager = UserManager()actor = user_manager.get_default_user()@@ -422,8 +444,8 @@ async def test_summarization(disable_e2b_api_key, voice_agent):summarizer.fire_and_forget.assert_called_once()[email protected](loop_scope="session")-async def test_voice_sleeptime_agent(disable_e2b_api_key, voice_agent):[email protected](loop_scope="module")+async def test_voice_sleeptime_agent(disable_e2b_api_key, voice_agent, server_url):"""Tests chat completion streaming using the Async OpenAI client."""agent_manager = AgentManager()tool_manager = ToolManager()@@ -488,8 +510,8 @@ async def test_voice_sleeptime_agent(disable_e2b_api_key, voice_agent):assert not missing, f"Did not see calls to: {', '.join(missing)}"[email protected](loop_scope="session")-async def test_init_voice_convo_agent(voice_agent, server, actor):[email protected](loop_scope="module")+async def test_init_voice_convo_agent(voice_agent, server, actor, server_url):assert voice_agent.enable_sleeptime == Truemain_agent_tools = [tool.name for tool in voice_agent.tools]diff --git a/tests/test_letta_agent_batch.py b/tests/test_letta_agent_batch.pyindex 700051336..da2a6666c 100644--- a/tests/test_letta_agent_batch.py+++ b/tests/test_letta_agent_batch.py@@ -1,5 +1,5 @@from datetime import datetime, timezone-from typing import List, Optional, Tuple+from typing import Tuplefrom unittest.mock import AsyncMock, patchimport pytest@@ -15,7 +15,6 @@ from anthropic.types.beta.messages import (from letta.agents.letta_agent_batch import LettaAgentBatchfrom letta.config import LettaConfig-from letta.functions.functions import parse_source_codefrom letta.helpers import ToolRulesSolverfrom letta.jobs.llm_batch_job_polling import poll_running_llm_batchesfrom letta.orm import Base@@ -25,10 +24,10 @@ from letta.schemas.job import BatchJobfrom letta.schemas.letta_message_content import TextContentfrom letta.schemas.letta_request import LettaBatchRequestfrom letta.schemas.message import MessageCreate-from letta.schemas.tool import Toolfrom letta.schemas.tool_rule import InitToolRulefrom letta.server.db import db_contextfrom letta.server.server import SyncServer+from tests.utils import create_tool_from_func# --------------------------------------------------------------------------- ## Test Constants / Helpers@@ -45,24 +44,6 @@ MODELS = {EXPECTED_ROLES = ["system", "assistant", "tool", "user", "user"]-def create_tool_from_func(- func,- tags: Optional[List[str]] = None,- description: Optional[str] = None,-):- source_code = parse_source_code(func)- source_type = "python"- if not tags:- tags = []-- return Tool(- source_type=source_type,- source_code=source_code,- tags=tags,- description=description,- )--# --------------------------------------------------------------------------- ## Test Fixtures# --------------------------------------------------------------------------- #diff --git a/tests/utils.py b/tests/utils.pyindex 65c3ee2ff..37b8ed6af 100644--- a/tests/utils.py+++ b/tests/utils.py@@ -4,15 +4,17 @@ import stringimport timefrom datetime import datetime, timezonefrom importlib import util-from typing import Dict, Iterator, List, Tuple+from typing import Dict, Iterator, List, Optional, Tupleimport requestsfrom letta.config import LettaConfigfrom letta.data_sources.connectors import DataConnector+from letta.functions.functions import parse_source_codefrom letta.schemas.enums import MessageRolefrom letta.schemas.file import FileMetadatafrom letta.schemas.message import Message+from letta.schemas.tool import Toolfrom letta.settings import TestSettingsfrom .constants import TIMEOUT@@ -199,3 +201,21 @@ def wait_for_server(url, timeout=30, interval=0.5):def random_string(length: int) -> str:return "".join(random.choices(string.ascii_letters + string.digits, k=length))+++def create_tool_from_func(+ func,+ tags: Optional[List[str]] = None,+ description: Optional[str] = None,+):+ source_code = parse_source_code(func)+ source_type = "python"+ if not tags:+ tags = []++ return Tool(+ source_type=source_type,+ source_code=source_code,+ tags=tags,+ description=description,+ )