diff --git a/agents-api/agents_api/models/execution/count_executions.py b/agents-api/agents_api/models/execution/count_executions.py deleted file mode 100644 index d130f0359..000000000 --- a/agents-api/agents_api/models/execution/count_executions.py +++ /dev/null @@ -1,61 +0,0 @@ -from typing import Any, TypeVar -from uuid import UUID - -from beartype import beartype -from fastapi import HTTPException -from pycozo.client import QueryException -from pydantic import ValidationError - -from ..utils import ( - cozo_query, - partialclass, - rewrap_exceptions, - verify_developer_id_query, - verify_developer_owns_resource_query, - wrap_in_class, -) - -ModelT = TypeVar("ModelT", bound=Any) -T = TypeVar("T") - - -@rewrap_exceptions( - { - QueryException: partialclass(HTTPException, status_code=400), - ValidationError: partialclass(HTTPException, status_code=400), - TypeError: partialclass(HTTPException, status_code=400), - } -) -@wrap_in_class(dict, one=True) -@cozo_query -@beartype -def count_executions( - *, - developer_id: UUID, - task_id: UUID, -) -> tuple[list[str], dict]: - count_query = """ - input[task_id] <- [[to_uuid($task_id)]] - - counter[count(id)] := - input[task_id], - *executions:task_id_execution_id_idx { - task_id, - execution_id: id, - } - - ?[count] := counter[count] - """ - - queries = [ - verify_developer_id_query(developer_id), - verify_developer_owns_resource_query( - developer_id, - "tasks", - task_id=task_id, - parents=[("agents", "agent_id")], - ), - count_query, - ] - - return (queries, {"task_id": str(task_id)}) diff --git a/agents-api/agents_api/models/execution/get_execution.py b/agents-api/agents_api/models/execution/get_execution.py deleted file mode 100644 index db0279b1f..000000000 --- a/agents-api/agents_api/models/execution/get_execution.py +++ /dev/null @@ -1,78 +0,0 @@ -from typing import Any, TypeVar -from uuid import UUID - -from beartype import beartype -from fastapi import HTTPException -from pycozo.client import QueryException -from pydantic import ValidationError - -from ...autogen.openapi_model import Execution -from ..utils import ( - cozo_query, - partialclass, - rewrap_exceptions, - wrap_in_class, -) -from .constants import OUTPUT_UNNEST_KEY - -ModelT = TypeVar("ModelT", bound=Any) -T = TypeVar("T") - - -@rewrap_exceptions( - { - AssertionError: partialclass(HTTPException, status_code=404), - QueryException: partialclass(HTTPException, status_code=400), - ValidationError: partialclass(HTTPException, status_code=400), - TypeError: partialclass(HTTPException, status_code=400), - } -) -@wrap_in_class( - Execution, - one=True, - transform=lambda d: { - **d, - "output": d["output"][OUTPUT_UNNEST_KEY] - if isinstance(d["output"], dict) and OUTPUT_UNNEST_KEY in d["output"] - else d["output"], - }, -) -@cozo_query -@beartype -def get_execution( - *, - execution_id: UUID, -) -> tuple[str, dict]: - # Executions are allowed direct GET access if they have execution_id - - # NOTE: Do not remove outer curly braces - query = """ - { - input[execution_id] <- [[to_uuid($execution_id)]] - - ?[id, task_id, status, input, output, error, session_id, metadata, created_at, updated_at] := - input[execution_id], - *executions { - task_id, - execution_id, - status, - input, - output, - error, - session_id, - metadata, - created_at, - updated_at, - }, - id = execution_id - - :limit 1 - } - """ - - return ( - query, - { - "execution_id": str(execution_id), - }, - ) diff --git a/agents-api/agents_api/models/execution/__init__.py b/agents-api/agents_api/queries/executions/__init__.py similarity index 100% rename from agents-api/agents_api/models/execution/__init__.py rename to agents-api/agents_api/queries/executions/__init__.py diff --git a/agents-api/agents_api/models/execution/constants.py b/agents-api/agents_api/queries/executions/constants.py similarity index 100% rename from agents-api/agents_api/models/execution/constants.py rename to agents-api/agents_api/queries/executions/constants.py diff --git a/agents-api/agents_api/queries/executions/count_executions.py b/agents-api/agents_api/queries/executions/count_executions.py new file mode 100644 index 000000000..5ec29a8b6 --- /dev/null +++ b/agents-api/agents_api/queries/executions/count_executions.py @@ -0,0 +1,39 @@ +from typing import Any, TypeVar +from uuid import UUID + +import sqlvalidator +from beartype import beartype + +from ..utils import ( + pg_query, + wrap_in_class, +) + +ModelT = TypeVar("ModelT", bound=Any) +T = TypeVar("T") + +sql_query = sqlvalidator.parse( + """ +SELECT COUNT(*) FROM executions +WHERE + developer_id = $1 + AND task_id = $2 +""" +) + +# @rewrap_exceptions( +# { +# QueryException: partialclass(HTTPException, status_code=400), +# ValidationError: partialclass(HTTPException, status_code=400), +# TypeError: partialclass(HTTPException, status_code=400), +# } +# ) +@wrap_in_class(dict, one=True) +@pg_query +@beartype +def count_executions( + *, + developer_id: UUID, + task_id: UUID, +) -> tuple[list[str], dict]: + return (sql_query.format(), [developer_id, task_id]) diff --git a/agents-api/agents_api/models/execution/create_execution.py b/agents-api/agents_api/queries/executions/create_execution.py similarity index 100% rename from agents-api/agents_api/models/execution/create_execution.py rename to agents-api/agents_api/queries/executions/create_execution.py diff --git a/agents-api/agents_api/models/execution/create_execution_transition.py b/agents-api/agents_api/queries/executions/create_execution_transition.py similarity index 100% rename from agents-api/agents_api/models/execution/create_execution_transition.py rename to agents-api/agents_api/queries/executions/create_execution_transition.py diff --git a/agents-api/agents_api/models/execution/create_temporal_lookup.py b/agents-api/agents_api/queries/executions/create_temporal_lookup.py similarity index 100% rename from agents-api/agents_api/models/execution/create_temporal_lookup.py rename to agents-api/agents_api/queries/executions/create_temporal_lookup.py diff --git a/agents-api/agents_api/queries/executions/get_execution.py b/agents-api/agents_api/queries/executions/get_execution.py new file mode 100644 index 000000000..474e0c63d --- /dev/null +++ b/agents-api/agents_api/queries/executions/get_execution.py @@ -0,0 +1,52 @@ +from typing import Any, TypeVar +from uuid import UUID + +from beartype import beartype + +import sqlvalidator +from ...autogen.openapi_model import Execution +from ..utils import ( + pg_query, + wrap_in_class, +) +from .constants import OUTPUT_UNNEST_KEY + +ModelT = TypeVar("ModelT", bound=Any) +T = TypeVar("T") + +sql_query = sqlvalidator.parse(""" +SELECT * FROM executions +WHERE + execution_id = $1 +LIMIT 1 +""") + + +# @rewrap_exceptions( +# { +# AssertionError: partialclass(HTTPException, status_code=404), +# QueryException: partialclass(HTTPException, status_code=400), +# ValidationError: partialclass(HTTPException, status_code=400), +# TypeError: partialclass(HTTPException, status_code=400), +# } +# ) +@wrap_in_class( + Execution, + one=True, + transform=lambda d: { + **d, + "output": d["output"][OUTPUT_UNNEST_KEY] + if isinstance(d["output"], dict) and OUTPUT_UNNEST_KEY in d["output"] + else d["output"], + }, +) +@pg_query +@beartype +def get_execution( + *, + execution_id: UUID, +) -> tuple[str, dict]: + return ( + sql_query.format(), + [execution_id], + ) diff --git a/agents-api/agents_api/models/execution/get_execution_transition.py b/agents-api/agents_api/queries/executions/get_execution_transition.py similarity index 100% rename from agents-api/agents_api/models/execution/get_execution_transition.py rename to agents-api/agents_api/queries/executions/get_execution_transition.py diff --git a/agents-api/agents_api/models/execution/get_paused_execution_token.py b/agents-api/agents_api/queries/executions/get_paused_execution_token.py similarity index 100% rename from agents-api/agents_api/models/execution/get_paused_execution_token.py rename to agents-api/agents_api/queries/executions/get_paused_execution_token.py diff --git a/agents-api/agents_api/models/execution/get_temporal_workflow_data.py b/agents-api/agents_api/queries/executions/get_temporal_workflow_data.py similarity index 100% rename from agents-api/agents_api/models/execution/get_temporal_workflow_data.py rename to agents-api/agents_api/queries/executions/get_temporal_workflow_data.py diff --git a/agents-api/agents_api/models/execution/list_execution_transitions.py b/agents-api/agents_api/queries/executions/list_execution_transitions.py similarity index 100% rename from agents-api/agents_api/models/execution/list_execution_transitions.py rename to agents-api/agents_api/queries/executions/list_execution_transitions.py diff --git a/agents-api/agents_api/models/execution/list_executions.py b/agents-api/agents_api/queries/executions/list_executions.py similarity index 100% rename from agents-api/agents_api/models/execution/list_executions.py rename to agents-api/agents_api/queries/executions/list_executions.py diff --git a/agents-api/agents_api/models/execution/lookup_temporal_data.py b/agents-api/agents_api/queries/executions/lookup_temporal_data.py similarity index 100% rename from agents-api/agents_api/models/execution/lookup_temporal_data.py rename to agents-api/agents_api/queries/executions/lookup_temporal_data.py diff --git a/agents-api/agents_api/models/execution/prepare_execution_input.py b/agents-api/agents_api/queries/executions/prepare_execution_input.py similarity index 100% rename from agents-api/agents_api/models/execution/prepare_execution_input.py rename to agents-api/agents_api/queries/executions/prepare_execution_input.py diff --git a/agents-api/agents_api/models/execution/update_execution.py b/agents-api/agents_api/queries/executions/update_execution.py similarity index 100% rename from agents-api/agents_api/models/execution/update_execution.py rename to agents-api/agents_api/queries/executions/update_execution.py