feat: Add sandbox_type filter when listing sandbox configurations (#567)

This commit is contained in:
Matthew Zhou 2025-01-09 10:48:21 -10:00 committed by GitHub
parent 46e1fe61b7
commit 23bc188270
3 changed files with 21 additions and 12 deletions

View File

@ -69,11 +69,12 @@ def delete_sandbox_config(
def list_sandbox_configs(
limit: int = Query(1000, description="Number of results to return"),
cursor: Optional[str] = Query(None, description="Pagination cursor to fetch the next set of results"),
sandbox_type: Optional[SandboxType] = Query(None, description="Filter for this specific sandbox type"),
server: SyncServer = Depends(get_letta_server),
user_id: str = Depends(get_user_id),
):
actor = server.user_manager.get_user_or_default(user_id=user_id)
return server.sandbox_config_manager.list_sandbox_configs(actor, limit=limit, cursor=cursor)
return server.sandbox_config_manager.list_sandbox_configs(actor, limit=limit, cursor=cursor, sandbox_type=sandbox_type)
### Sandbox Environment Variable Routes

View File

@ -111,16 +111,15 @@ class SandboxConfigManager:
@enforce_types
def list_sandbox_configs(
self, actor: PydanticUser, cursor: Optional[str] = None, limit: Optional[int] = 50
self, actor: PydanticUser, cursor: Optional[str] = None, limit: Optional[int] = 50, sandbox_type: Optional[SandboxType] = None
) -> List[PydanticSandboxConfig]:
"""List all sandbox configurations with optional pagination."""
kwargs = {"organization_id": actor.organization_id}
if sandbox_type:
kwargs.update({"type": sandbox_type})
with self.session_maker() as session:
sandboxes = SandboxConfigModel.list(
db_session=session,
cursor=cursor,
limit=limit,
organization_id=actor.organization_id,
)
sandboxes = SandboxConfigModel.list(db_session=session, cursor=cursor, limit=limit, **kwargs)
return [sandbox.to_pydantic() for sandbox in sandboxes]
@enforce_types

View File

@ -2057,16 +2057,16 @@ def test_get_sandbox_config_by_type(server: SyncServer, sandbox_config_fixture,
def test_list_sandbox_configs(server: SyncServer, default_user):
# Creating multiple sandbox configs
config_a = SandboxConfigCreate(
config_e2b_create = SandboxConfigCreate(
config=E2BSandboxConfig(),
)
config_b = SandboxConfigCreate(
config_local_create = SandboxConfigCreate(
config=LocalSandboxConfig(sandbox_dir=""),
)
server.sandbox_config_manager.create_or_update_sandbox_config(config_a, actor=default_user)
config_e2b = server.sandbox_config_manager.create_or_update_sandbox_config(config_e2b_create, actor=default_user)
if USING_SQLITE:
time.sleep(CREATE_DELAY_SQLITE)
server.sandbox_config_manager.create_or_update_sandbox_config(config_b, actor=default_user)
config_local = server.sandbox_config_manager.create_or_update_sandbox_config(config_local_create, actor=default_user)
# List configs without pagination
configs = server.sandbox_config_manager.list_sandbox_configs(actor=default_user)
@ -2080,6 +2080,15 @@ def test_list_sandbox_configs(server: SyncServer, default_user):
assert len(next_page) == 1
assert next_page[0].id != paginated_configs[0].id
# List configs using sandbox_type filter
configs = server.sandbox_config_manager.list_sandbox_configs(actor=default_user, sandbox_type=SandboxType.E2B)
assert len(configs) == 1
assert configs[0].id == config_e2b.id
configs = server.sandbox_config_manager.list_sandbox_configs(actor=default_user, sandbox_type=SandboxType.LOCAL)
assert len(configs) == 1
assert configs[0].id == config_local.id
# ======================================================================================================================
# SandboxConfigManager Tests - Environment Variables