-
Notifications
You must be signed in to change notification settings - Fork 924
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(agents-api): Implement doc* models (#442)
* refactor(agents-api): Minor refactors Signed-off-by: Diwank Tomer <[email protected]> * feat(typespec): Add create-doc endpoint Signed-off-by: Diwank Tomer <[email protected]> * feat(agents-api): Add migrations for unifying the owner-docs tables Signed-off-by: Diwank Tomer <[email protected]> * feat(agents-api): Implement doc* models 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
57 changed files
with
1,780 additions
and
570 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
from typing import Literal | ||
from uuid import UUID, uuid4 | ||
|
||
from beartype import beartype | ||
from fastapi import HTTPException | ||
from pycozo.client import QueryException | ||
from pydantic import ValidationError | ||
|
||
from ...autogen.openapi_model import CreateDocRequest, Doc | ||
from ...common.utils.cozo import cozo_process_mutate_data | ||
from ..utils import ( | ||
cozo_query, | ||
partialclass, | ||
rewrap_exceptions, | ||
verify_developer_id_query, | ||
verify_developer_owns_resource_query, | ||
wrap_in_class, | ||
) | ||
|
||
|
||
@rewrap_exceptions( | ||
{ | ||
QueryException: partialclass(HTTPException, status_code=400), | ||
ValidationError: partialclass(HTTPException, status_code=400), | ||
TypeError: partialclass(HTTPException, status_code=400), | ||
} | ||
) | ||
@wrap_in_class( | ||
Doc, | ||
one=True, | ||
transform=lambda d: { | ||
"id": UUID(d["doc_id"]), | ||
"content": [], # <-- Note: we do not return content on creation | ||
**d, | ||
}, | ||
) | ||
@cozo_query | ||
@beartype | ||
def create_doc( | ||
*, | ||
developer_id: UUID, | ||
owner_type: Literal["user", "agent"], | ||
owner_id: UUID, | ||
doc_id: UUID | None = None, | ||
data: CreateDocRequest, | ||
) -> tuple[list[str], dict]: | ||
""" | ||
Constructs and executes a datalog query to create a new document and its associated snippets in the 'cozodb' database. | ||
Parameters: | ||
- owner_type (Literal["user", "agent"]): The type of the owner of the document. | ||
- owner_id (UUID): The UUID of the document owner. | ||
- id (UUID): The UUID of the document to be created. | ||
- data (CreateDocRequest): The content of the document. | ||
""" | ||
|
||
doc_id = str(doc_id or uuid4()) | ||
owner_id = str(owner_id) | ||
|
||
if isinstance(data.content, str): | ||
data.content = [data.content] | ||
|
||
data.metadata = data.metadata or {} | ||
|
||
doc_data = data.model_dump() | ||
content = doc_data.pop("content") | ||
|
||
doc_data["owner_type"] = owner_type | ||
doc_data["owner_id"] = owner_id | ||
doc_data["doc_id"] = doc_id | ||
|
||
doc_cols, doc_rows = cozo_process_mutate_data(doc_data) | ||
|
||
snippet_cols, snippet_rows = "", [] | ||
|
||
# Process each content snippet and prepare data for the datalog query. | ||
for snippet_idx, snippet in enumerate(content): | ||
snippet_cols, new_snippet_rows = cozo_process_mutate_data( | ||
dict( | ||
doc_id=doc_id, | ||
index=snippet_idx, | ||
content=snippet, | ||
) | ||
) | ||
|
||
snippet_rows += new_snippet_rows | ||
|
||
create_snippets_query = f""" | ||
?[{snippet_cols}] <- $snippet_rows | ||
:insert snippets {{ {snippet_cols} }} | ||
:returning | ||
""" | ||
|
||
# Construct the datalog query for creating the document and its snippets. | ||
create_doc_query = f""" | ||
?[{doc_cols}] <- $doc_rows | ||
:insert docs {{ {doc_cols} }} | ||
:returning | ||
""" | ||
|
||
queries = [ | ||
verify_developer_id_query(developer_id), | ||
verify_developer_owns_resource_query( | ||
developer_id, f"{owner_type}s", **{f"{owner_type}_id": owner_id} | ||
), | ||
create_snippets_query, | ||
create_doc_query, | ||
] | ||
|
||
# Execute the constructed datalog query and return the results as a DataFrame. | ||
return ( | ||
queries, | ||
{ | ||
"doc_rows": doc_rows, | ||
"snippet_rows": snippet_rows, | ||
}, | ||
) |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.