Platform for stateful agents: AI with advanced memory that can learn and self-improve over time.
feat(asyncify): migrate add base tools (#2238)
cthomas3fc7b239e908
diff --git a/letta/server/rest_api/routers/v1/tools.py b/letta/server/rest_api/routers/v1/tools.pyindex 50c4d262b..073e1e152 100644--- a/letta/server/rest_api/routers/v1/tools.py+++ b/letta/server/rest_api/routers/v1/tools.py@@ -181,15 +181,15 @@ def modify_tool(@router.post("/add-base-tools", response_model=List[Tool], operation_id="add_base_tools")-def upsert_base_tools(+async def upsert_base_tools(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):"""Upsert base tools"""- actor = server.user_manager.get_user_or_default(user_id=actor_id)- return server.tool_manager.upsert_base_tools(actor=actor)+ actor = await server.user_manager.get_actor_or_default_async(actor_id=actor_id)+ return await server.tool_manager.upsert_base_tools_async(actor=actor)@router.post("/run", response_model=ToolReturnMessage, operation_id="run_tool_from_source")diff --git a/letta/services/tool_manager.py b/letta/services/tool_manager.pyindex ad7a61e6b..d8e71ba79 100644--- a/letta/services/tool_manager.py+++ b/letta/services/tool_manager.py@@ -59,6 +59,32 @@ class ToolManager:return tool+ @enforce_types+ async def create_or_update_tool_async(self, pydantic_tool: PydanticTool, actor: PydanticUser) -> PydanticTool:+ """Create a new tool based on the ToolCreate schema."""+ tool_id = await self.get_tool_id_by_name_async(tool_name=pydantic_tool.name, actor=actor)+ if tool_id:+ # Put to dict and remove fields that should not be reset+ update_data = pydantic_tool.model_dump(exclude_unset=True, exclude_none=True)++ # If there's anything to update+ if update_data:+ # In case we want to update the tool type+ # Useful if we are shuffling around base tools+ updated_tool_type = None+ if "tool_type" in update_data:+ updated_tool_type = update_data.get("tool_type")+ tool = await self.update_tool_by_id_async(tool_id, ToolUpdate(**update_data), actor, updated_tool_type=updated_tool_type)+ else:+ printd(+ f"`create_or_update_tool` was called with user_id={actor.id}, organization_id={actor.organization_id}, name={pydantic_tool.name}, but found existing tool with nothing to update."+ )+ tool = await self.get_tool_by_id_async(tool_id, actor=actor)+ else:+ tool = await self.create_tool_async(pydantic_tool, actor=actor)++ return tool+@enforce_typesdef create_or_update_mcp_tool(self, tool_create: ToolCreate, mcp_server_name: str, actor: PydanticUser) -> PydanticTool:metadata = {MCP_TOOL_TAG_NAME_PREFIX: {"server_name": mcp_server_name}}@@ -96,6 +122,21 @@ class ToolManager:tool.create(session, actor=actor) # Re-raise other database-related errorsreturn tool.to_pydantic()+ @enforce_types+ async def create_tool_async(self, pydantic_tool: PydanticTool, actor: PydanticUser) -> PydanticTool:+ """Create a new tool based on the ToolCreate schema."""+ async with db_registry.async_session() as session:+ # Set the organization id at the ORM layer+ pydantic_tool.organization_id = actor.organization_id+ # Auto-generate description if not provided+ if pydantic_tool.description is None:+ pydantic_tool.description = pydantic_tool.json_schema.get("description", None)+ tool_data = pydantic_tool.model_dump(to_orm=True)++ tool = ToolModel(**tool_data)+ await tool.create_async(session, actor=actor) # Re-raise other database-related errors+ return tool.to_pydantic()+@enforce_typesdef get_tool_by_id(self, tool_id: str, actor: PydanticUser) -> PydanticTool:"""Fetch a tool by its ID."""@@ -213,6 +254,34 @@ class ToolManager:# Save the updated tool to the databasereturn tool.update(db_session=session, actor=actor).to_pydantic()+ @enforce_types+ async def update_tool_by_id_async(+ self, tool_id: str, tool_update: ToolUpdate, actor: PydanticUser, updated_tool_type: Optional[ToolType] = None+ ) -> PydanticTool:+ """Update a tool by its ID with the given ToolUpdate object."""+ async with db_registry.async_session() as session:+ # Fetch the tool by ID+ tool = await ToolModel.read_async(db_session=session, identifier=tool_id, actor=actor)++ # Update tool attributes with only the fields that were explicitly set+ update_data = tool_update.model_dump(to_orm=True, exclude_none=True)+ for key, value in update_data.items():+ setattr(tool, key, value)++ # If source code is changed and a new json_schema is not provided, we want to auto-refresh the schema+ if "source_code" in update_data.keys() and "json_schema" not in update_data.keys():+ pydantic_tool = tool.to_pydantic()+ new_schema = derive_openai_json_schema(source_code=pydantic_tool.source_code)++ tool.json_schema = new_schema+ tool.name = new_schema["name"]++ if updated_tool_type:+ tool.tool_type = updated_tool_type++ # Save the updated tool to the database+ return await tool.update_async(db_session=session, actor=actor).to_pydantic()+@enforce_typesdef delete_tool_by_id(self, tool_id: str, actor: PydanticUser) -> None:"""Delete a tool by its ID."""@@ -284,3 +353,65 @@ class ToolManager:# TODO: Delete any base tools that are stalereturn tools++ @enforce_types+ async def upsert_base_tools_async(self, actor: PydanticUser) -> List[PydanticTool]:+ """Add default tools in base.py and multi_agent.py"""+ functions_to_schema = {}+ module_names = ["base", "multi_agent", "voice"]++ for module_name in module_names:+ full_module_name = f"letta.functions.function_sets.{module_name}"+ try:+ module = importlib.import_module(full_module_name)+ except Exception as e:+ # Handle other general exceptions+ raise e++ try:+ # Load the function set+ functions_to_schema.update(load_function_set(module))+ except ValueError as e:+ err = f"Error loading function set '{module_name}': {e}"+ warnings.warn(err)++ # create tool in db+ tools = []+ for name, schema in functions_to_schema.items():+ if name in LETTA_TOOL_SET:+ if name in BASE_TOOLS:+ tool_type = ToolType.LETTA_CORE+ tags = [tool_type.value]+ elif name in BASE_MEMORY_TOOLS:+ tool_type = ToolType.LETTA_MEMORY_CORE+ tags = [tool_type.value]+ elif name in MULTI_AGENT_TOOLS:+ tool_type = ToolType.LETTA_MULTI_AGENT_CORE+ tags = [tool_type.value]+ elif name in BASE_SLEEPTIME_TOOLS:+ tool_type = ToolType.LETTA_SLEEPTIME_CORE+ tags = [tool_type.value]+ elif name in BASE_VOICE_SLEEPTIME_TOOLS or name in BASE_VOICE_SLEEPTIME_CHAT_TOOLS:+ tool_type = ToolType.LETTA_VOICE_SLEEPTIME_CORE+ tags = [tool_type.value]+ else:+ raise ValueError(+ f"Tool name {name} is not in the list of base tool names: {BASE_TOOLS + BASE_MEMORY_TOOLS + MULTI_AGENT_TOOLS + BASE_SLEEPTIME_TOOLS + BASE_VOICE_SLEEPTIME_TOOLS + BASE_VOICE_SLEEPTIME_CHAT_TOOLS}"+ )++ # create to tool+ tools.append(+ self.create_or_update_tool(+ PydanticTool(+ name=name,+ tags=tags,+ source_type="python",+ tool_type=tool_type,+ return_char_limit=BASE_FUNCTION_RETURN_CHAR_LIMIT,+ ),+ actor=actor,+ )+ )++ # TODO: Delete any base tools that are stale+ return tools