Files
letta-server/letta/server/rest_api/routers/v1/organizations.py
cthomas 8bce540466 chore: bump v0.8.16 (#2724)
Co-authored-by: Kian Jones <11655409+kianjones9@users.noreply.github.com>
Co-authored-by: Sarah Wooders <sarahwooders@gmail.com>
Co-authored-by: Matthew Zhou <mattzh1314@gmail.com>
Co-authored-by: Andy Li <55300002+cliandy@users.noreply.github.com>
Co-authored-by: jnjpng <jin@letta.com>
Co-authored-by: Jin Peng <jinjpeng@Jins-MacBook-Pro.local>
Co-authored-by: cpacker <packercharles@gmail.com>
Co-authored-by: Shubham Naik <shub@letta.com>
Co-authored-by: Shubham Naik <shub@memgpt.ai>
Co-authored-by: Kevin Lin <klin5061@gmail.com>
Co-authored-by: Eric Ly <111820150+lyeric2022@users.noreply.github.com>
Co-authored-by: Eric Ly <lyyeric@letta.com>
2025-07-21 00:06:16 -07:00

80 lines
2.9 KiB
Python

from typing import TYPE_CHECKING, List, Optional
from fastapi import APIRouter, Body, Depends, HTTPException, Query
from letta.schemas.organization import Organization, OrganizationCreate, OrganizationUpdate
from letta.server.rest_api.utils import get_letta_server
if TYPE_CHECKING:
from letta.server.server import SyncServer
router = APIRouter(prefix="/orgs", tags=["organization", "admin"])
@router.get("/", tags=["admin"], response_model=List[Organization], operation_id="list_orgs")
async def get_all_orgs(
after: Optional[str] = Query(None),
limit: Optional[int] = Query(50),
server: "SyncServer" = Depends(get_letta_server),
):
"""
Get a list of all orgs in the database
"""
try:
orgs = await server.organization_manager.list_organizations_async(after=after, limit=limit)
except HTTPException:
raise
except Exception as e:
raise HTTPException(status_code=500, detail=f"{e}")
return orgs
@router.post("/", tags=["admin"], response_model=Organization, operation_id="create_organization")
async def create_org(
request: OrganizationCreate = Body(...),
server: "SyncServer" = Depends(get_letta_server),
):
"""
Create a new org in the database
"""
org = Organization(**request.model_dump())
org = await server.organization_manager.create_organization_async(pydantic_org=org)
return org
@router.delete("/", tags=["admin"], response_model=Organization, operation_id="delete_organization_by_id")
async def delete_org(
org_id: str = Query(..., description="The org_id key to be deleted."),
server: "SyncServer" = Depends(get_letta_server),
):
# TODO make a soft deletion, instead of a hard deletion
try:
org = await server.organization_manager.get_organization_by_id_async(org_id=org_id)
if org is None:
raise HTTPException(status_code=404, detail="Organization does not exist")
await server.organization_manager.delete_organization_by_id_async(org_id=org_id)
except HTTPException:
raise
except Exception as e:
raise HTTPException(status_code=500, detail=f"{e}")
return org
@router.patch("/", tags=["admin"], response_model=Organization, operation_id="update_organization")
async def update_org(
org_id: str = Query(..., description="The org_id key to be updated."),
request: OrganizationUpdate = Body(...),
server: "SyncServer" = Depends(get_letta_server),
):
try:
org = await server.organization_manager.get_organization_by_id_async(org_id=org_id)
if org is None:
raise HTTPException(status_code=404, detail="Organization does not exist")
org = await server.organization_manager.update_organization_async(org_id=org_id, name=request.name)
except HTTPException:
raise
except Exception as e:
raise HTTPException(status_code=500, detail=f"{e}")
return org