Platform for stateful agents: AI with advanced memory that can learn and self-improve over time.
feat: add query parameter to exclude builtin tools for tool count (#1898)
Andy Li280f2c8d2ad9
diff --git a/letta/server/rest_api/routers/v1/sources.py b/letta/server/rest_api/routers/v1/sources.pyindex ac91d69b2..97a76eb36 100644--- a/letta/server/rest_api/routers/v1/sources.py+++ b/letta/server/rest_api/routers/v1/sources.py@@ -20,6 +20,17 @@ from letta.utils import sanitize_filenamerouter = APIRouter(prefix="/sources", tags=["sources"])[email protected]("/count", response_model=int, operation_id="count_sources")+def count_sources(+ server: "SyncServer" = Depends(get_letta_server),+ actor_id: Optional[str] = Header(None, alias="user_id"), # Extract user_id from header, default to None if not present+):+ """+ Count all data sources created by a user.+ """+ return server.source_manager.size(actor=server.user_manager.get_user_or_default(user_id=actor_id))++@router.get("/{source_id}", response_model=Source, operation_id="retrieve_source")def retrieve_source(source_id: str,@@ -67,17 +78,6 @@ def list_sources(return server.list_all_sources(actor=actor)[email protected]("/count", response_model=int, operation_id="count_sources")-def count_sources(- server: "SyncServer" = Depends(get_letta_server),- actor_id: Optional[str] = Header(None, alias="user_id"), # Extract user_id from header, default to None if not present-):- """- Count all data sources created by a user.- """- return server.source_manager.size(actor=server.user_manager.get_user_or_default(user_id=actor_id))--@router.post("/", response_model=Source, operation_id="create_source")def create_source(source_create: SourceCreate,diff --git a/letta/server/rest_api/routers/v1/tools.py b/letta/server/rest_api/routers/v1/tools.pyindex 06482175c..a1c7591b9 100644--- a/letta/server/rest_api/routers/v1/tools.py+++ b/letta/server/rest_api/routers/v1/tools.py@@ -9,7 +9,7 @@ from composio.exceptions import (EnumMetadataNotFound,EnumStringNotFound,)-from fastapi import APIRouter, Body, Depends, Header, HTTPException+from fastapi import APIRouter, Body, Depends, Header, HTTPException, Queryfrom letta.errors import LettaToolCreateErrorfrom letta.functions.mcp_client.exceptions import MCPTimeoutError@@ -40,6 +40,24 @@ def delete_tool(server.tool_manager.delete_tool_by_id(tool_id=tool_id, actor=actor)[email protected]("/count", response_model=int, operation_id="count_tools")+def count_tools(+ server: SyncServer = Depends(get_letta_server),+ actor_id: Optional[str] = Header(None, alias="user_id"),+ include_base_tools: Optional[bool] = Query(False, description="Include built-in Letta tools in the count"),+):+ """+ Get a count of all tools available to agents belonging to the org of the user.+ """+ try:+ return server.tool_manager.size(+ actor=server.user_manager.get_user_or_default(user_id=actor_id), include_base_tools=include_base_tools+ )+ except Exception as e:+ print(f"Error occurred: {e}")+ raise HTTPException(status_code=500, detail=str(e))++@router.get("/{tool_id}", response_model=Tool, operation_id="retrieve_tool")def retrieve_tool(tool_id: str,@@ -80,21 +98,6 @@ def list_tools(raise HTTPException(status_code=500, detail=str(e))[email protected]("/count", response_model=int, operation_id="count_tools")-def count_tools(- server: SyncServer = Depends(get_letta_server),- actor_id: Optional[str] = Header(None, alias="user_id"),-):- """- Get a count of all tools available to agents belonging to the org of the user- """- try:- return server.tool_manager.size(actor=server.user_manager.get_user_or_default(user_id=actor_id))- except Exception as e:- print(f"Error occurred: {e}")- raise HTTPException(status_code=500, detail=str(e))--@router.post("/", response_model=Tool, operation_id="create_tool")def create_tool(request: ToolCreate = Body(...),diff --git a/letta/services/tool_manager.py b/letta/services/tool_manager.pyindex 571e67d48..d03a36f84 100644--- a/letta/services/tool_manager.py+++ b/letta/services/tool_manager.py@@ -29,14 +29,6 @@ logger = get_logger(__name__)class ToolManager:"""Manager class to handle business logic related to Tools."""- BASE_TOOL_NAMES = [- "send_message",- "conversation_search",- "archival_memory_insert",- "archival_memory_search",- ]- BASE_MEMORY_TOOL_NAMES = ["core_memory_append", "core_memory_replace"]-def __init__(self):# Fetching the db_context similarly as in OrganizationManagerfrom letta.server.db import db_context@@ -149,12 +141,17 @@ class ToolManager:def size(self,actor: PydanticUser,+ include_base_tools: bool,) -> int:"""Get the total count of tools for the given user.++ If include_builtin is True, it will also count the built-in tools."""with self.session_maker() as session:- return ToolModel.size(db_session=session, actor=actor)+ if include_base_tools:+ return ToolModel.size(db_session=session, actor=actor)+ return ToolModel.size(db_session=session, actor=actor, name=LETTA_TOOL_SET)@enforce_typesdef update_tool_by_id(self, tool_id: str, tool_update: ToolUpdate, actor: PydanticUser) -> PydanticTool: