Skip to content

armgit / letta

Public Imported from GitHub
0

Platform for stateful agents: AI with advanced memory that can learn and self-improve over time.

feat: Upsert on list with no pagination (#2875)

Matthew Zhou878b7395328c
diff --git a/letta/server/rest_api/routers/v1/tools.py b/letta/server/rest_api/routers/v1/tools.py
index 98e6f300b..afc14166a 100644
--- a/letta/server/rest_api/routers/v1/tools.py
+++ b/letta/server/rest_api/routers/v1/tools.py
@@ -91,6 +91,8 @@ async def list_tools(
if name is not None:
tool = await server.tool_manager.get_tool_by_name_async(tool_name=name, actor=actor)
return [tool] if tool else []
+
+ # Get the list of tools
return await server.tool_manager.list_tools_async(actor=actor, after=after, limit=limit)
except Exception as e:
# Log or print the full exception here for debugging
diff --git a/letta/services/tool_manager.py b/letta/services/tool_manager.py
index 78daed8a4..9e0d1a266 100644
--- a/letta/services/tool_manager.py
+++ b/letta/services/tool_manager.py
@@ -240,6 +240,28 @@ class ToolManager:
@enforce_types
@trace_method
async def list_tools_async(self, actor: PydanticUser, after: Optional[str] = None, limit: Optional[int] = 50) -> List[PydanticTool]:
+ """List all tools with optional pagination."""
+ tools = await self._list_tools_async(actor=actor, after=after, limit=limit)
+
+ # Check if all base tools are present if we requested all the tools w/o cursor
+ # TODO: This is a temporary hack to resolve this issue
+ # TODO: This requires a deeper rethink about how we keep all our internal tools up-to-date
+ if not after:
+ existing_tool_names = {tool.name for tool in tools}
+ missing_base_tools = LETTA_TOOL_SET - existing_tool_names
+
+ # If any base tools are missing, upsert all base tools
+ if missing_base_tools:
+ logger.info(f"Missing base tools detected: {missing_base_tools}. Upserting all base tools.")
+ await self.upsert_base_tools_async(actor=actor)
+ # Re-fetch the tools list after upserting base tools
+ tools = await self._list_tools_async(actor=actor, after=after, limit=limit)
+
+ return tools
+
+ @enforce_types
+ @trace_method
+ async def _list_tools_async(self, actor: PydanticUser, after: Optional[str] = None, limit: Optional[int] = 50) -> List[PydanticTool]:
"""List all tools with optional pagination."""
tools_to_delete = []
async with db_registry.async_session() as session:
diff --git a/tests/test_sdk_client.py b/tests/test_sdk_client.py
index b0bbf1e22..f7d4a89bb 100644
--- a/tests/test_sdk_client.py
+++ b/tests/test_sdk_client.py
@@ -717,3 +717,47 @@ def test_include_return_message_types(client: LettaSDKClient, agent: AgentState,
# cleanup
client.agents.delete(agent.id)
+
+
+def test_base_tools_upsert_on_list(client: LettaSDKClient):
+ """Test that base tools are automatically upserted when missing on tools list call"""
+ from letta.constants import LETTA_TOOL_SET
+
+ # First, get the initial list of tools to establish baseline
+ initial_tools = client.tools.list()
+ initial_tool_names = {tool.name for tool in initial_tools}
+
+ # Find which base tools might be missing initially
+ missing_base_tools = LETTA_TOOL_SET - initial_tool_names
+
+ # If all base tools are already present, we need to delete some to test the upsert functionality
+ # We'll delete a few base tools if they exist to create the condition for testing
+ tools_to_delete = []
+ if not missing_base_tools:
+ # Pick a few base tools to delete for testing
+ test_base_tools = ["send_message", "conversation_search"]
+ for tool_name in test_base_tools:
+ for tool in initial_tools:
+ if tool.name == tool_name:
+ tools_to_delete.append(tool)
+ client.tools.delete(tool_id=tool.id)
+ break
+
+ # Now call list_tools() which should trigger the base tools check and upsert
+ updated_tools = client.tools.list()
+ updated_tool_names = {tool.name for tool in updated_tools}
+
+ # Verify that all base tools are now present
+ missing_after_upsert = LETTA_TOOL_SET - updated_tool_names
+ assert not missing_after_upsert, f"Base tools still missing after upsert: {missing_after_upsert}"
+
+ # Verify that the base tools are actually in the list
+ for base_tool_name in LETTA_TOOL_SET:
+ assert base_tool_name in updated_tool_names, f"Base tool {base_tool_name} not found after upsert"
+
+ # Cleanup: restore any tools we deleted for testing (they should already be restored by the upsert)
+ # This is just a double-check that our test cleanup is proper
+ final_tools = client.tools.list()
+ final_tool_names = {tool.name for tool in final_tools}
+ for deleted_tool in tools_to_delete:
+ assert deleted_tool.name in final_tool_names, f"Deleted tool {deleted_tool.name} was not properly restored"