-
Notifications
You must be signed in to change notification settings - Fork 894
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(agents-api): Rework routers to split routes into individual …
…files (#422) * refactor(agents-api): Rework routers to split routes into individual files * feat(agents-api): Refactor routers.py files Signed-off-by: Diwank Tomer <[email protected]> --------- Signed-off-by: Diwank Tomer <[email protected]> Co-authored-by: Diwank Tomer <[email protected]>
- Loading branch information
Showing
27 changed files
with
636 additions
and
1,252 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,8 @@ | ||
from .routers import router # noqa: F401 | ||
from .router import router # noqa: F401 | ||
|
||
from .create_agent import create_agent # noqa: F401 | ||
from .delete_agent import delete_agent # noqa: F401 | ||
from .get_agent_details import get_agent_details # noqa: F401 | ||
from .list_agents import list_agents # noqa: F401 | ||
from .update_agent import update_agent # noqa: F401 | ||
from .patch_agent import patch_agent # noqa: F401 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from typing import Annotated | ||
|
||
from fastapi import Depends | ||
from pydantic import UUID4 | ||
from starlette.status import HTTP_201_CREATED | ||
|
||
from ...dependencies.developer_id import get_developer_id | ||
from ...models.agent.create_agent import create_agent_query | ||
from ...autogen.openapi_model import CreateAgentRequest, ResourceCreatedResponse | ||
from ...common.utils.datetime import utcnow | ||
|
||
from .router import router | ||
|
||
|
||
@router.post("/agents", status_code=HTTP_201_CREATED, tags=["agents"]) | ||
async def create_agent( | ||
request: CreateAgentRequest, | ||
x_developer_id: Annotated[UUID4, Depends(get_developer_id)], | ||
) -> ResourceCreatedResponse: | ||
agent_id = create_agent_query( | ||
developer_id=x_developer_id, | ||
name=request.name, | ||
about=request.about, | ||
instructions=request.instructions, | ||
model=request.model, | ||
default_settings=request.default_settings, | ||
metadata=request.metadata, | ||
) | ||
return ResourceCreatedResponse(id=agent_id, created_at=utcnow()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from typing import Annotated | ||
|
||
from fastapi import Depends, HTTPException | ||
from pydantic import UUID4 | ||
from starlette.status import HTTP_202_ACCEPTED, HTTP_404_NOT_FOUND | ||
|
||
from ...dependencies.developer_id import get_developer_id | ||
from ...models.agent.delete_agent import delete_agent_query | ||
from ...common.exceptions.agents import AgentNotFoundError | ||
from ...common.utils.datetime import utcnow | ||
from ...autogen.openapi_model import ResourceDeletedResponse | ||
|
||
from .router import router | ||
|
||
|
||
@router.delete("/agents/{agent_id}", status_code=HTTP_202_ACCEPTED, tags=["agents"]) | ||
async def delete_agent( | ||
agent_id: UUID4, x_developer_id: Annotated[UUID4, Depends(get_developer_id)] | ||
) -> ResourceDeletedResponse: | ||
try: | ||
delete_agent_query(x_developer_id, agent_id) | ||
except AgentNotFoundError as e: | ||
raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail=str(e)) | ||
return ResourceDeletedResponse(id=agent_id, deleted_at=utcnow()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from typing import Annotated | ||
|
||
from fastapi import Depends, HTTPException | ||
from pydantic import UUID4 | ||
from starlette.status import HTTP_404_NOT_FOUND | ||
|
||
from ...dependencies.developer_id import get_developer_id | ||
from ...models.agent.get_agent import get_agent_query | ||
from ...common.exceptions.agents import AgentNotFoundError | ||
from ...autogen.openapi_model import Agent | ||
|
||
from .router import router | ||
|
||
|
||
@router.get("/agents/{agent_id}", tags=["agents"]) | ||
async def get_agent_details( | ||
agent_id: UUID4, | ||
x_developer_id: Annotated[UUID4, Depends(get_developer_id)], | ||
) -> Agent: | ||
try: | ||
agent = get_agent_query(developer_id=x_developer_id, agent_id=agent_id) | ||
if not agent: | ||
raise AgentNotFoundError(x_developer_id, agent_id) | ||
return Agent(**agent) | ||
except AgentNotFoundError as e: | ||
raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail=str(e)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from typing import List, Annotated | ||
|
||
from fastapi import Depends | ||
from pydantic import UUID4 | ||
|
||
from ...dependencies.developer_id import get_developer_id | ||
from ...models.agent.list_agents import list_agents_query | ||
from ...autogen.openapi_model import Agent | ||
|
||
from .router import router | ||
|
||
|
||
@router.get("/agents", tags=["agents"]) | ||
async def list_agents( | ||
x_developer_id: Annotated[UUID4, Depends(get_developer_id)], | ||
limit: int = 100, | ||
offset: int = 0, | ||
metadata_filter: str = "{}", | ||
) -> List[Agent]: | ||
agents = list_agents_query( | ||
developer_id=x_developer_id, | ||
limit=limit, | ||
offset=offset, | ||
metadata_filter=metadata_filter, | ||
) | ||
return [Agent(**agent) for agent in agents] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from typing import Annotated | ||
|
||
from fastapi import Depends, HTTPException | ||
from pydantic import UUID4 | ||
from starlette.status import HTTP_404_NOT_FOUND, HTTP_200_OK | ||
|
||
from ...dependencies.developer_id import get_developer_id | ||
from ...models.agent.patch_agent import patch_agent_query | ||
from ...common.exceptions.agents import AgentNotFoundError | ||
from ...autogen.openapi_model import PatchAgentRequest, ResourceUpdatedResponse | ||
|
||
from .router import router | ||
|
||
|
||
@router.patch( | ||
"/agents/{agent_id}", | ||
response_model=ResourceUpdatedResponse, | ||
status_code=HTTP_200_OK, | ||
tags=["agents"], | ||
) | ||
async def patch_agent( | ||
agent_id: UUID4, | ||
request: PatchAgentRequest, | ||
x_developer_id: Annotated[UUID4, Depends(get_developer_id)], | ||
) -> ResourceUpdatedResponse: | ||
try: | ||
updated_agent = patch_agent_query( | ||
agent_id=agent_id, | ||
developer_id=x_developer_id, | ||
default_settings=request.default_settings, | ||
name=request.name, | ||
about=request.about, | ||
model=request.model, | ||
metadata=request.metadata, | ||
instructions=request.instructions, | ||
) | ||
return ResourceUpdatedResponse(**updated_agent) | ||
except AgentNotFoundError as e: | ||
raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail=str(e)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from fastapi import APIRouter | ||
|
||
router = APIRouter() |
Oops, something went wrong.