From b7af340f389817f62fe05104bd93e76148aa70c1 Mon Sep 17 00:00:00 2001 From: Eric Zawadski Date: Thu, 9 Jan 2025 10:36:21 -0800 Subject: [PATCH] feat(backend): refactored agent params into params directory --- src/backend/routers/agent.py | 54 +++++++++------------- src/backend/schemas/params/agent.py | 24 ++++++---- src/backend/schemas/params/file.py | 11 +++++ src/backend/schemas/params/organization.py | 11 +++++ src/backend/schemas/params/shared.py | 6 ++- 5 files changed, 63 insertions(+), 43 deletions(-) create mode 100644 src/backend/schemas/params/file.py create mode 100644 src/backend/schemas/params/organization.py diff --git a/src/backend/routers/agent.py b/src/backend/routers/agent.py index 4c6db8481f..2843a1e43e 100644 --- a/src/backend/routers/agent.py +++ b/src/backend/routers/agent.py @@ -1,7 +1,6 @@ import asyncio -from typing import Annotated, Optional -from fastapi import APIRouter, Depends, HTTPException, Query +from fastapi import APIRouter, Depends, HTTPException from fastapi import File as RequestFile from fastapi import UploadFile as FastAPIUploadFile @@ -42,10 +41,12 @@ UploadAgentFileResponse, ) from backend.schemas.params.agent import ( - agent_id_path, - agent_tool_metadata_id_path, - file_id_path, + AgentIdPathParam, + AgentToolMetadataIdPathParam, + VisibilityQueryParam, ) +from backend.schemas.params.file import FileIdPathParam +from backend.schemas.params.organization import OrganizationIdQueryParam from backend.schemas.params.shared import PaginationQueryParams from backend.services.agent import ( raise_db_error, @@ -129,23 +130,14 @@ async def create_agent( @router.get("", response_model=list[AgentPublic]) async def list_agents( *, - page_params: Annotated[PaginationQueryParams, Depends()], - visibility: Annotated[AgentVisibility, Query( - title="Visibility", - description="Agent visibility", - )] = AgentVisibility.ALL, - organization_id: Annotated[Optional[str], Query( - title="Organization ID", - description="Organization ID of the agent", - )] = None, + page_params: PaginationQueryParams, + visibility: VisibilityQueryParam = AgentVisibility.ALL, + organization_id: OrganizationIdQueryParam = None, session: DBSessionDep, ctx: Context = Depends(get_context), ) -> list[AgentPublic]: """ List all agents. - - Returns: - list[AgentPublic]: List of agents with no user ID or organization ID. """ # TODO: get organization_id from user user_id = ctx.get_user_id() @@ -173,7 +165,7 @@ async def list_agents( @router.get("/{agent_id}", response_model=AgentPublic) async def get_agent_by_id( - agent_id: Annotated[str, agent_id_path], + agent_id: AgentIdPathParam, session: DBSessionDep, ctx: Context = Depends(get_context) ) -> AgentPublic: @@ -208,7 +200,7 @@ async def get_agent_by_id( @router.get("/{agent_id}/deployments", response_model=list[DeploymentSchema]) async def get_agent_deployment( - agent_id: Annotated[str, agent_id_path], + agent_id: AgentIdPathParam, session: DBSessionDep, ctx: Context = Depends(get_context) ) -> DeploymentSchema: @@ -236,7 +228,7 @@ async def get_agent_deployment( ], ) async def update_agent( - agent_id: Annotated[str, agent_id_path], + agent_id: AgentIdPathParam, new_agent: UpdateAgentRequest, session: DBSessionDep, ctx: Context = Depends(get_context), @@ -284,7 +276,7 @@ async def update_agent( @router.delete("/{agent_id}", response_model=DeleteAgent) async def delete_agent( - agent_id: Annotated[str, agent_id_path], + agent_id: AgentIdPathParam, session: DBSessionDep, ctx: Context = Depends(get_context), ) -> DeleteAgent: @@ -380,7 +372,7 @@ async def update_or_create_tool_metadata( @router.get("/{agent_id}/tool-metadata", response_model=list[AgentToolMetadataPublic]) async def list_agent_tool_metadata( - agent_id: Annotated[str, agent_id_path], + agent_id: AgentIdPathParam, session: DBSessionDep, ctx: Context = Depends(get_context) ) -> list[AgentToolMetadataPublic]: @@ -406,7 +398,7 @@ async def list_agent_tool_metadata( response_model=AgentToolMetadataPublic, ) def create_agent_tool_metadata( - agent_id: Annotated[str, agent_id_path], + agent_id: AgentIdPathParam, agent_tool_metadata: CreateAgentToolMetadataRequest, session: DBSessionDep, ctx: Context = Depends(get_context), @@ -446,8 +438,8 @@ def create_agent_tool_metadata( @router.put("/{agent_id}/tool-metadata/{agent_tool_metadata_id}") async def update_agent_tool_metadata( - agent_id: Annotated[str, agent_id_path], - agent_tool_metadata_id: Annotated[str, agent_tool_metadata_id_path], + agent_id: AgentIdPathParam, + agent_tool_metadata_id: AgentToolMetadataIdPathParam, new_agent_tool_metadata: UpdateAgentToolMetadataRequest, session: DBSessionDep, ctx: Context = Depends(get_context), @@ -480,8 +472,8 @@ async def update_agent_tool_metadata( @router.delete("/{agent_id}/tool-metadata/{agent_tool_metadata_id}") async def delete_agent_tool_metadata( - agent_id: Annotated[str, agent_id_path], - agent_tool_metadata_id: Annotated[str, agent_tool_metadata_id_path], + agent_id: AgentIdPathParam, + agent_tool_metadata_id: AgentToolMetadataIdPathParam, session: DBSessionDep, ctx: Context = Depends(get_context), ) -> DeleteAgentToolMetadata: @@ -541,8 +533,8 @@ async def batch_upload_file( @router.get("/{agent_id}/files/{file_id}", response_model=FileMetadata) async def get_agent_file( - agent_id: Annotated[str, agent_id_path], - file_id: Annotated[str, file_id_path], + agent_id: AgentIdPathParam, + file_id: FileIdPathParam, session: DBSessionDep, ctx: Context = Depends(get_context), ) -> FileMetadata: @@ -580,8 +572,8 @@ async def get_agent_file( @router.delete("/{agent_id}/files/{file_id}") async def delete_agent_file( - agent_id: Annotated[str, agent_id_path], - file_id: Annotated[str, file_id_path], + agent_id: AgentIdPathParam, + file_id: FileIdPathParam, session: DBSessionDep, ctx: Context = Depends(get_context), ) -> DeleteAgentFileResponse: diff --git a/src/backend/schemas/params/agent.py b/src/backend/schemas/params/agent.py index 287a61f577..6cc57ff41a 100644 --- a/src/backend/schemas/params/agent.py +++ b/src/backend/schemas/params/agent.py @@ -1,19 +1,23 @@ """ Query and Path Parameters for Agents """ -from fastapi import Path +from typing import Annotated -agent_id_path = Path( +from fastapi import Path, Query + +from backend.schemas.agent import AgentVisibility + +VisibilityQueryParam = Annotated[AgentVisibility, Query( + title="Visibility", + description="Agent visibility", +)] + +AgentIdPathParam = Annotated[str, Path( title="Agent ID", description="Agent ID for agent in question", -) +)] -agent_tool_metadata_id_path = Path( +AgentToolMetadataIdPathParam = Annotated[str, Path( title="Agent Tool Metadata ID", description="Agent Tool Metadata ID for tool metadata in question", -) - -file_id_path = Path( - title="File ID", - description="File ID for file in question", -) +)] diff --git a/src/backend/schemas/params/file.py b/src/backend/schemas/params/file.py new file mode 100644 index 0000000000..bbd879a02c --- /dev/null +++ b/src/backend/schemas/params/file.py @@ -0,0 +1,11 @@ +""" +Query and Path Parameters for Files +""" +from typing import Annotated + +from fastapi import Path + +FileIdPathParam = Annotated[str, Path( + title="File ID", + description="File ID for file in question", +)] diff --git a/src/backend/schemas/params/organization.py b/src/backend/schemas/params/organization.py new file mode 100644 index 0000000000..5ea7417e7a --- /dev/null +++ b/src/backend/schemas/params/organization.py @@ -0,0 +1,11 @@ +""" +Query and Path Parameters for Organizations +""" +from typing import Annotated, Optional + +from fastapi import Query + +OrganizationIdQueryParam = Annotated[Optional[str], Query( + title="Organization ID", + description="Organization ID of the agent", +)] diff --git a/src/backend/schemas/params/shared.py b/src/backend/schemas/params/shared.py index aef0d5e879..fdaa112029 100644 --- a/src/backend/schemas/params/shared.py +++ b/src/backend/schemas/params/shared.py @@ -3,10 +3,10 @@ """ from typing import Annotated -from fastapi import Query +from fastapi import Depends, Query -class PaginationQueryParams: +class PaginationParams: """ Common pagination query parameters """ @@ -23,3 +23,5 @@ def __init__( ) -> None: self.offset = offset self.limit = limit + +PaginationQueryParams = Annotated[PaginationParams, Depends()]