-
Notifications
You must be signed in to change notification settings - Fork 904
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
- Loading branch information
Showing
24 changed files
with
583 additions
and
1,251 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
from .routers import router # noqa: F401 | ||
from fastapi import APIRouter | ||
|
||
router = APIRouter() |
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 fastapi import APIRouter, Depends, HTTPException, status | ||
from pydantic import UUID4 | ||
from typing import Annotated | ||
|
||
from agents_api.dependencies.developer_id import get_developer_id | ||
from agents_api.models.agent.create_agent import create_agent_query | ||
from agents_api.autogen.openapi_model import CreateAgentRequest, ResourceCreatedResponse | ||
from agents_api.common.utils.datetime import utcnow | ||
|
||
router = APIRouter() | ||
|
||
@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,23 @@ | ||
from fastapi import APIRouter, Depends, HTTPException | ||
from pydantic import UUID4 | ||
from starlette.status import HTTP_202_ACCEPTED | ||
|
||
from agents_api.dependencies.developer_id import get_developer_id | ||
from agents_api.models.agent.delete_agent import delete_agent_query | ||
from agents_api.common.exceptions.agents import AgentNotFoundError | ||
from agents_api.common.utils.datetime import utcnow | ||
from agents_api.autogen.openapi_model import ResourceDeletedResponse | ||
|
||
router = APIRouter() | ||
|
||
@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 fastapi import APIRouter, Depends, HTTPException | ||
from pydantic import UUID4 | ||
from starlette.status import HTTP_404_NOT_FOUND | ||
|
||
from agents_api.dependencies.developer_id import get_developer_id | ||
from agents_api.models.agent.get_agent import get_agent_query | ||
from agents_api.common.exceptions.agents import AgentNotFoundError | ||
from agents_api.autogen.openapi_model import Agent | ||
|
||
router = APIRouter() | ||
|
||
@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(f"Agent with ID {agent_id} not found") | ||
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,24 @@ | ||
from fastapi import APIRouter, Depends | ||
from pydantic import UUID4 | ||
from typing import List | ||
|
||
from agents_api.dependencies.developer_id import get_developer_id | ||
from agents_api.models.agent.list_agents import list_agents_query | ||
from agents_api.autogen.openapi_model import Agent | ||
|
||
router = APIRouter() | ||
|
||
@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,34 @@ | ||
from fastapi import APIRouter, Depends, HTTPException | ||
from pydantic import UUID4 | ||
from starlette.status import HTTP_404_NOT_FOUND, HTTP_200_OK | ||
|
||
from agents_api.dependencies.developer_id import get_developer_id | ||
from agents_api.models.agent.patch_agent import patch_agent_query | ||
from agents_api.common.exceptions.agents import AgentNotFoundError | ||
from agents_api.autogen.openapi_model import PatchAgentRequest, ResourceUpdatedResponse | ||
|
||
router = APIRouter() | ||
|
||
@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) | ||
) |
Oops, something went wrong.