-
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.
fix(typespec,agents-api): Update metadata_filter to have object type (#…
…586) Signed-off-by: Diwank Singh Tomer <[email protected]> <!-- ELLIPSIS_HIDDEN --> ---- > [!IMPORTANT] > Refactors metadata filtering by introducing `FilterModel` and `create_filter_extractor`, updating API endpoints and typespec for structured metadata filtering. > > - **Behavior**: > - Introduces `FilterModel` and `create_filter_extractor` in `query_filter.py` for structured metadata filtering. > - Updates `list_agents`, `list_user_docs`, `list_agent_docs`, and `list_sessions` to use `FilterModel` for `metadata_filter`. > - Removes JSON string parsing for `metadata_filter` in `list_agents.py`, `list_docs.py`, and `list_sessions.py`. > - **Types**: > - Adds `concreteType` alias in `scalars.tsp`. > - Updates `MetadataFilter` alias in `types.tsp` to use `concreteType`. > - Changes `metadata_filter` in `PaginationOptions` model in `types.tsp` to use `MetadataFilter`. > > <sup>This description was created by </sup>[<img alt="Ellipsis" src="https://img.shields.io/badge/Ellipsis-blue?color=175173">](https://www.ellipsis.dev?ref=julep-ai%2Fjulep&utm_source=github&utm_medium=referral)<sup> for 63eda8b. It will automatically update as commits are pushed.</sup> <!-- ELLIPSIS_HIDDEN --> --------- Signed-off-by: Diwank Singh Tomer <[email protected]> Co-authored-by: creatorrr <[email protected]>
- Loading branch information
Showing
10 changed files
with
127 additions
and
84 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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
from typing import Any, Callable | ||
|
||
from fastapi import Request | ||
|
||
|
||
def convert_value(value: str) -> Any: | ||
""" | ||
Attempts to convert a string value to an int or float. Returns the original string if conversion fails. | ||
""" | ||
for convert in (int, float): | ||
try: | ||
return convert(value) | ||
except ValueError: | ||
continue | ||
return value | ||
|
||
|
||
def create_filter_extractor( | ||
prefix: str = "filter", | ||
) -> Callable[[Request], dict[str, Any]]: | ||
""" | ||
Creates a dependency function to extract filter parameters with a given prefix. | ||
Args: | ||
prefix (str): The prefix to identify filter parameters. | ||
Returns: | ||
Callable[[Request], dict[str, Any]]: The dependency function. | ||
""" | ||
|
||
# Add a dot to the prefix to allow for nested filters | ||
prefix += "." | ||
|
||
def extract_filters(request: Request) -> dict[str, Any]: | ||
""" | ||
Extracts query parameters that start with the specified prefix and returns them as a dictionary. | ||
Args: | ||
request (Request): The incoming HTTP request. | ||
Returns: | ||
dict[str, Any]: A dictionary containing the filter parameters. | ||
""" | ||
|
||
filters: dict[str, Any] = {} | ||
|
||
for key, value in request.query_params.items(): | ||
if key.startswith(prefix): | ||
filter_key = key[len(prefix) :] | ||
filters[filter_key] = convert_value(value) | ||
|
||
return filters | ||
|
||
return extract_filters |
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,40 +1,36 @@ | ||
import json | ||
from json import JSONDecodeError | ||
from typing import Annotated, Literal | ||
from uuid import UUID | ||
|
||
from fastapi import Depends, HTTPException, status | ||
from fastapi import Depends | ||
|
||
from ...autogen.openapi_model import Agent, ListResponse | ||
from ...dependencies.developer_id import get_developer_id | ||
from ...dependencies.query_filter import create_filter_extractor | ||
from ...models.agent.list_agents import list_agents as list_agents_query | ||
from .router import router | ||
|
||
|
||
@router.get("/agents", tags=["agents"]) | ||
async def list_agents( | ||
x_developer_id: Annotated[UUID, Depends(get_developer_id)], | ||
# Expects the dot notation of object in query params | ||
# Example: | ||
# > ?metadata_filter.name=John&metadata_filter.age=30 | ||
metadata_filter: Annotated[ | ||
dict, Depends(create_filter_extractor("metadata_filter")) | ||
], | ||
limit: int = 100, | ||
offset: int = 0, | ||
sort_by: Literal["created_at", "updated_at"] = "created_at", | ||
direction: Literal["asc", "desc"] = "desc", | ||
metadata_filter: str = "{}", | ||
) -> ListResponse[Agent]: | ||
try: | ||
metadata_filter = json.loads(metadata_filter) | ||
except JSONDecodeError: | ||
raise HTTPException( | ||
status_code=status.HTTP_400_BAD_REQUEST, | ||
detail="metadata_filter is not a valid JSON", | ||
) | ||
|
||
agents = list_agents_query( | ||
developer_id=x_developer_id, | ||
limit=limit, | ||
offset=offset, | ||
sort_by=sort_by, | ||
direction=direction, | ||
metadata_filter=metadata_filter, | ||
metadata_filter=metadata_filter or {}, | ||
) | ||
|
||
return ListResponse[Agent](items=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
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,40 +1,33 @@ | ||
import json | ||
from json import JSONDecodeError | ||
from typing import Annotated, Literal | ||
from uuid import UUID | ||
|
||
from fastapi import Depends, HTTPException, status | ||
from fastapi import Depends | ||
|
||
from ...autogen.openapi_model import ListResponse, Session | ||
from ...dependencies.developer_id import get_developer_id | ||
from ...dependencies.query_filter import create_filter_extractor | ||
from ...models.session.list_sessions import list_sessions as list_sessions_query | ||
from .router import router | ||
|
||
|
||
@router.get("/sessions", tags=["sessions"]) | ||
async def list_sessions( | ||
x_developer_id: Annotated[UUID, Depends(get_developer_id)], | ||
metadata_filter: Annotated[ | ||
dict, Depends(create_filter_extractor("metadata_filter")) | ||
] = {}, | ||
limit: int = 100, | ||
offset: int = 0, | ||
sort_by: Literal["created_at", "updated_at"] = "created_at", | ||
direction: Literal["asc", "desc"] = "desc", | ||
metadata_filter: str = "{}", | ||
) -> ListResponse[Session]: | ||
try: | ||
metadata_filter = json.loads(metadata_filter) | ||
except JSONDecodeError: | ||
raise HTTPException( | ||
status_code=status.HTTP_400_BAD_REQUEST, | ||
detail="metadata_filter is not a valid JSON", | ||
) | ||
|
||
sessions = list_sessions_query( | ||
developer_id=x_developer_id, | ||
limit=limit, | ||
offset=offset, | ||
sort_by=sort_by, | ||
direction=direction, | ||
metadata_filter=metadata_filter, | ||
metadata_filter=metadata_filter or {}, | ||
) | ||
|
||
return ListResponse[Session](items=sessions) |
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 |
---|---|---|
|
@@ -136,7 +136,7 @@ interface ChildLimitOffsetPagination< | |
|
||
...PaginationOptions, | ||
): { | ||
results: T[]; | ||
items: T[]; | ||
}; | ||
} | ||
|
||
|
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
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
Oops, something went wrong.