-
Notifications
You must be signed in to change notification settings - Fork 895
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Diwank Tomer <[email protected]>
- Loading branch information
Diwank Tomer
committed
Jul 26, 2024
1 parent
9bb58c3
commit c31f563
Showing
13 changed files
with
323 additions
and
114 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
144 changes: 144 additions & 0 deletions
144
agents-api/agents_api/models/session/create_or_update_session.py
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,144 @@ | ||
from beartype import beartype | ||
|
||
from uuid import UUID | ||
|
||
|
||
from ...autogen.openapi_model import ( | ||
CreateOrUpdateSessionRequest, | ||
ResourceUpdatedResponse, | ||
) | ||
from ...common.utils.cozo import cozo_process_mutate_data | ||
from ..utils import ( | ||
cozo_query, | ||
verify_developer_id_query, | ||
verify_developer_owns_resource_query, | ||
wrap_in_class, | ||
) | ||
|
||
|
||
@wrap_in_class( | ||
ResourceUpdatedResponse, | ||
one=True, | ||
transform=lambda d: { | ||
"id": d["session_id"], | ||
"updated_at": d.pop("updated_at")[0], | ||
"jobs": [], | ||
**d, | ||
}, | ||
) | ||
@cozo_query(debug=True) | ||
@beartype | ||
def create_or_update_session_query( | ||
*, | ||
session_id: UUID, | ||
developer_id: UUID, | ||
create_or_update_session: CreateOrUpdateSessionRequest, | ||
) -> tuple[str, dict]: | ||
|
||
create_or_update_session.metadata = create_or_update_session.metadata or {} | ||
session_data = create_or_update_session.model_dump() | ||
|
||
user = session_data.pop("user") | ||
agent = session_data.pop("agent") | ||
users = session_data.pop("users") | ||
agents = session_data.pop("agents") | ||
|
||
# Only one of agent or agents should be provided. | ||
if agent and agents: | ||
raise ValueError("Only one of 'agent' or 'agents' should be provided.") | ||
|
||
agents = agents or ([agent] if agent else []) | ||
assert len(agents) > 0, "At least one agent must be provided." | ||
|
||
# Users are zero or more, so we default to an empty list if not provided. | ||
if not (user or users): | ||
users = [] | ||
|
||
else: | ||
users = users or [user] | ||
|
||
participants = [ | ||
*[("user", str(user)) for user in users], | ||
*[("agent", str(agent)) for agent in agents], | ||
] | ||
|
||
# Construct the datalog query for creating a new session and its lookup. | ||
clear_lookup_query = """ | ||
input[session_id] <- [[$session_id]] | ||
?[session_id, participant_id, participant_type] := | ||
input[session_id], | ||
*session_lookup { | ||
session_id, | ||
participant_type, | ||
participant_id, | ||
}, | ||
:delete session_lookup { | ||
session_id, | ||
participant_type, | ||
participant_id, | ||
} | ||
""" | ||
|
||
lookup_query = """ | ||
# This section creates a new session lookup to ensure uniqueness and manage session metadata. | ||
session[session_id] <- [[$session_id]] | ||
participants[participant_type, participant_id] <- $participants | ||
?[session_id, participant_id, participant_type] := | ||
session[session_id], | ||
participants[participant_type, participant_id], | ||
:put session_lookup { | ||
session_id, | ||
participant_id, | ||
participant_type, | ||
} | ||
""" | ||
|
||
session_update_cols, session_update_vals = cozo_process_mutate_data( | ||
{k: v for k, v in session_data.items() if v is not None} | ||
) | ||
|
||
# Construct the datalog query for creating or updating session information. | ||
update_query = f""" | ||
input[{session_update_cols}] <- $session_update_vals | ||
ids[session_id, developer_id] <- [[to_uuid($session_id), to_uuid($developer_id)]] | ||
?[{session_update_cols}, session_id, developer_id] := | ||
input[{session_update_cols}], | ||
ids[session_id, developer_id], | ||
:put sessions {{ | ||
{session_update_cols}, session_id, developer_id | ||
}} | ||
:returning | ||
""" | ||
|
||
queries = [ | ||
verify_developer_id_query(developer_id), | ||
*[ | ||
verify_developer_owns_resource_query( | ||
developer_id, | ||
f"{participant_type}s", | ||
**{f"{participant_type}_id": participant_id}, | ||
) | ||
for participant_type, participant_id in participants | ||
], | ||
clear_lookup_query, | ||
lookup_query, | ||
update_query, | ||
] | ||
|
||
query = "}\n\n{\n".join(queries) | ||
query = f"{{ {query} }}" | ||
|
||
return ( | ||
query, | ||
{ | ||
"session_update_vals": session_update_vals, | ||
"session_id": str(session_id), | ||
"developer_id": str(developer_id), | ||
"participants": participants, | ||
}, | ||
) |
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.