diff --git a/agents-api/agents_api/autogen/Entries.py b/agents-api/agents_api/autogen/Entries.py index 2252be69c..d56b333d8 100644 --- a/agents-api/agents_api/autogen/Entries.py +++ b/agents-api/agents_api/autogen/Entries.py @@ -11,6 +11,48 @@ from .Tools import ChosenToolCall, Tool, ToolResponse +class BaseEntry(BaseModel): + model_config = ConfigDict( + populate_by_name=True, + ) + role: Literal[ + "user", + "agent", + "system", + "function", + "function_response", + "function_call", + "auto", + ] + """ + ChatML role (system|assistant|user|function_call|function|function_response|auto) + """ + name: str | None = None + content: ( + list[ChatMLTextContentPart | ChatMLImageContentPart] + | Tool + | ChosenToolCall + | str + | ToolResponse + | list[ + list[ChatMLTextContentPart | ChatMLImageContentPart] + | Tool + | ChosenToolCall + | str + | ToolResponse + ] + ) + source: Literal[ + "api_request", "api_response", "tool_response", "internal", "summarizer", "meta" + ] + tokenizer: str | None = None + token_count: int | None = None + timestamp: Annotated[float, Field(ge=0.0)] + """ + This is the time that this event refers to. + """ + + class ChatMLImageContentPart(BaseModel): model_config = ConfigDict( populate_by_name=True, @@ -73,44 +115,10 @@ class ChatMLTextContentPart(BaseModel): """ -class Entry(BaseModel): +class Entry(BaseEntry): model_config = ConfigDict( populate_by_name=True, ) - role: Literal[ - "user", - "agent", - "system", - "function", - "function_response", - "function_call", - "auto", - ] - """ - ChatML role (system|assistant|user|function_call|function|function_response|auto) - """ - name: str | None = None - content: ( - list[ChatMLTextContentPart | ChatMLImageContentPart] - | Tool - | ChosenToolCall - | str - | ToolResponse - | list[ - list[ChatMLTextContentPart | ChatMLImageContentPart] - | Tool - | ChosenToolCall - | str - | ToolResponse - ] - ) - source: Literal[ - "api_request", "api_response", "tool_response", "internal", "summarizer", "meta" - ] - timestamp: Annotated[float, Field(ge=0.0)] - """ - This is the time that this event refers to. - """ created_at: Annotated[AwareDatetime, Field(json_schema_extra={"readOnly": True})] """ When this resource was created as UTC date-time @@ -122,7 +130,7 @@ class History(BaseModel): model_config = ConfigDict( populate_by_name=True, ) - entries: list[Entry] + entries: list[BaseEntry] relations: list[Relation] session_id: Annotated[UUID, Field(json_schema_extra={"readOnly": True})] created_at: Annotated[AwareDatetime, Field(json_schema_extra={"readOnly": True})] diff --git a/agents-api/agents_api/autogen/openapi_model.py b/agents-api/agents_api/autogen/openapi_model.py index b7e96fc41..7d81019e0 100644 --- a/agents-api/agents_api/autogen/openapi_model.py +++ b/agents-api/agents_api/autogen/openapi_model.py @@ -34,17 +34,13 @@ "metadata", ) -ChatMLRole = Entry.model_fields["role"].annotation +ChatMLRole = BaseEntry.model_fields["role"].annotation -class CreateEntryRequest(Entry): - id: Annotated[UUID | None, Field(default=None)] - created_at: Annotated[AwareDatetime | None, Field(default=None)] +class CreateEntryRequest(BaseEntry): timestamp: Annotated[ float, Field(ge=0.0, default_factory=lambda: utcnow().timestamp()) ] - tokenizer: str - token_count: int def make_session( @@ -72,3 +68,5 @@ def make_session( case _: cls = MultiAgentMultiUserSession participants = {"agents": agents, "users": users} + + return cls(**{**data, **participants}) diff --git a/agents-api/agents_api/common/protocol/entries.py b/agents-api/agents_api/common/protocol/entries.py index f4543aa54..8920afd17 100644 --- a/agents-api/agents_api/common/protocol/entries.py +++ b/agents-api/agents_api/common/protocol/entries.py @@ -1,8 +1,8 @@ import json from typing import Literal -from uuid import UUID, uuid4 +from uuid import UUID -from pydantic import BaseModel, Field, computed_field +from pydantic import Field, computed_field from ...autogen.openapi_model import ( ChatMLImageContentPart, @@ -12,7 +12,6 @@ from ...autogen.openapi_model import ( Entry as BaseEntry, ) -from ...common.utils.datetime import utcnow EntrySource = Literal["api_request", "api_response", "internal", "summarizer"] Tokenizer = Literal["character_count"] diff --git a/agents-api/agents_api/common/protocol/sessions.py b/agents-api/agents_api/common/protocol/sessions.py index a8741dad3..756423d21 100644 --- a/agents-api/agents_api/common/protocol/sessions.py +++ b/agents-api/agents_api/common/protocol/sessions.py @@ -3,11 +3,18 @@ It includes definitions for session settings and session data models. """ -from typing import Dict, Optional -from uuid import UUID +from typing import Optional from pydantic import BaseModel +from ...autogen.openapi_model import ( + Agent, + Entry, + Session, + Settings, + Tool, + User, +) from .agents import AgentDefaultSettings @@ -22,26 +29,19 @@ class SessionSettings(AgentDefaultSettings): class SessionData(BaseModel): """ - Represents the data associated with a session, including identifiers for the agent, user, and session itself, - along with session-specific information such as situation, summary, and timestamps. + Represents the data associated with a session, including for agents, and users. """ - agent_id: UUID - user_id: Optional[UUID] - session_id: UUID - situation: str - summary: Optional[str] - user_name: Optional[str] - user_about: Optional[str] - agent_name: Optional[str] - agent_about: str - updated_at: float - created_at: float - model: str - default_settings: SessionSettings - render_templates: bool = False - metadata: Dict = {} - user_metadata: Optional[Dict] = None - agent_metadata: Dict = {} - token_budget: int | None = None - context_overflow: str | None = None + session: Session + agents: list[Agent] + users: list[User] = [] + settings: Optional[Settings] = None + + +class ChatContext(SessionData): + """ + Represents the data associated with a context, including for agents, and users. + """ + + entries: list[Entry] + tools: list[Tool] diff --git a/agents-api/agents_api/common/protocol/tasks.py b/agents-api/agents_api/common/protocol/tasks.py index 75f0a18c6..ad7b2fff4 100644 --- a/agents-api/agents_api/common/protocol/tasks.py +++ b/agents-api/agents_api/common/protocol/tasks.py @@ -18,7 +18,7 @@ User, YieldStep, ) -from ...models.execution.prepare_execution_data import get_execution_input_query +from ...models.execution.prepare_execution_data import prepare_execution_data from ..utils.cozo import uuid_int_list_to_uuid4 WorkflowStep = ( @@ -117,7 +117,7 @@ class ExecutionInput(BaseModel): def fetch( cls, *, developer_id: UUID4, task_id: UUID4, execution_id: UUID4, client: Any ) -> "ExecutionInput": - [data] = get_execution_input_query( + [data] = prepare_execution_data( task_id=task_id, execution_id=execution_id, client=client, diff --git a/agents-api/agents_api/models/agent/list_agents.py b/agents-api/agents_api/models/agent/list_agents.py index 1b38e7454..7ace0f1c0 100644 --- a/agents-api/agents_api/models/agent/list_agents.py +++ b/agents-api/agents_api/models/agent/list_agents.py @@ -32,7 +32,7 @@ def list_agents( developer_id: UUID, limit: int = 100, offset: int = 0, - sort_by: Literal["created_at", "updated_at", "deleted_at"] = "created_at", + sort_by: Literal["created_at", "updated_at"] = "created_at", direction: Literal["asc", "desc"] = "desc", metadata_filter: dict[str, Any] = {}, ) -> tuple[str, dict]: diff --git a/agents-api/agents_api/models/entry/create_entries.py b/agents-api/agents_api/models/entry/create_entries.py index cc725d479..b4a1680f9 100644 --- a/agents-api/agents_api/models/entry/create_entries.py +++ b/agents-api/agents_api/models/entry/create_entries.py @@ -5,7 +5,7 @@ from pycozo.client import QueryException from pydantic import ValidationError -from ...autogen.openapi_model import CreateEntryRequest, Entry +from ...autogen.openapi_model import CreateEntryRequest, Entry, Relation from ...common.utils.cozo import cozo_process_mutate_data from ...common.utils.datetime import utcnow from ...common.utils.messages import content_to_json @@ -39,7 +39,7 @@ def create_entries( *, developer_id: UUID, session_id: UUID, - data: list[Entry], + data: list[CreateEntryRequest], ) -> tuple[str, dict]: developer_id = str(developer_id) session_id = str(session_id) @@ -78,3 +78,44 @@ def create_entries( query = f"{{ {query} }}" return (query, {"rows": rows}) + + +@rewrap_exceptions( + { + QueryException: partialclass(HTTPException, status_code=400), + ValidationError: partialclass(HTTPException, status_code=400), + TypeError: partialclass(HTTPException, status_code=400), + } +) +@wrap_in_class(Relation) +@cozo_query +@beartype +def add_entry_relations( + *, + developer_id: UUID, + data: list[Relation], +) -> tuple[str, dict]: + developer_id = str(developer_id) + + data_dicts = [item.model_dump(mode="json") for item in data] + cols, rows = cozo_process_mutate_data(data_dicts) + + create_query = f""" + ?[{cols}] <- $rows + + :insert relations {{ + {cols} + }} + + :returning + """ + + queries = [ + verify_developer_id_query(developer_id), + create_query, + ] + + query = "}\n\n{\n".join(queries) + query = f"{{ {query} }}" + + return (query, {"rows": rows}) diff --git a/agents-api/agents_api/models/entry/delete_entries.py b/agents-api/agents_api/models/entry/delete_entries.py index 6e1f1f84a..aa102e049 100644 --- a/agents-api/agents_api/models/entry/delete_entries.py +++ b/agents-api/agents_api/models/entry/delete_entries.py @@ -1,24 +1,52 @@ 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 +from ...autogen.openapi_model import ResourceDeletedResponse +from ...common.utils.datetime import utcnow +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), + IndexError: partialclass(HTTPException, status_code=404), + } +) +@wrap_in_class( + ResourceDeletedResponse, + one=True, + transform=lambda d: { + "id": UUID(d.pop("session_id")), # Only return session cleared + "deleted_at": utcnow(), + "jobs": [], + }, +) @cozo_query @beartype -def delete_entries_query(session_id: UUID) -> tuple[str, dict]: +def delete_entries_for_session( + *, developer_id: UUID, session_id: UUID +) -> tuple[str, dict]: """ Constructs and returns a datalog query for deleting entries associated with a given session ID from the 'cozodb' database. Parameters: - session_id (UUID): The unique identifier of the session whose entries are to be deleted. - - Returns: - - pd.DataFrame: A DataFrame containing the results of the deletion query. """ - query = """ - { + + delete_query = """ input[session_id] <- [[ to_uuid($session_id), ]] @@ -26,46 +54,61 @@ def delete_entries_query(session_id: UUID) -> tuple[str, dict]: ?[ session_id, entry_id, - role, - name, - content, source, - token_count, - created_at, - timestamp, + role, ] := input[session_id], *entries{ session_id, entry_id, - role, - name, - content, source, - token_count, - created_at, - timestamp, + role, } :delete entries { session_id, entry_id, - role, - name, - content, source, - token_count, - created_at, - timestamp, + role, } - }""" + + :returning + """ + + queries = [ + verify_developer_id_query(developer_id), + verify_developer_owns_resource_query( + developer_id, "sessions", session_id=session_id + ), + delete_query, + ] + + query = "}\n\n{\n".join(queries) + query = f"{{ {query} }}" return (query, {"session_id": str(session_id)}) -@cozo_query -def delete_entries(entry_ids: list[UUID]) -> tuple[str, dict]: - query = """ +@rewrap_exceptions( { + QueryException: partialclass(HTTPException, status_code=400), + ValidationError: partialclass(HTTPException, status_code=400), + TypeError: partialclass(HTTPException, status_code=400), + } +) +@wrap_in_class( + ResourceDeletedResponse, + transform=lambda d: { + "id": UUID(d.pop("entry_id")), + "deleted_at": utcnow(), + "jobs": [], + }, +) +@cozo_query +@beartype +def delete_entries( + *, developer_id: UUID, session_id: UUID, entry_ids: list[UUID] +) -> tuple[str, dict]: + delete_query = """ input[entry_id_str] <- $entry_ids ?[ @@ -73,42 +116,35 @@ def delete_entries(entry_ids: list[UUID]) -> tuple[str, dict]: session_id, source, role, - name, - content, - token_count, - tokenizer, - created_at, - timestamp, ] := input[entry_id_str], entry_id = to_uuid(entry_id_str), *entries { - entry_id, session_id, + entry_id, source, role, - name, - content, - token_count, - tokenizer, - created_at, - timestamp, } :delete entries { - entry_id, session_id, + entry_id, source, role, - name, - content, - token_count, - tokenizer, - created_at, - timestamp, } :returning - }""" + """ + + queries = [ + verify_developer_id_query(developer_id), + verify_developer_owns_resource_query( + developer_id, "sessions", session_id=session_id + ), + delete_query, + ] + + query = "}\n\n{\n".join(queries) + query = f"{{ {query} }}" return (query, {"entry_ids": [[str(id)] for id in entry_ids]}) diff --git a/agents-api/agents_api/models/entry/entries_summarization.py b/agents-api/agents_api/models/entry/entries_summarization.py deleted file mode 100644 index 4a94bdc19..000000000 --- a/agents-api/agents_api/models/entry/entries_summarization.py +++ /dev/null @@ -1,133 +0,0 @@ -"""This module contains functions for querying and summarizing entry data in the 'cozodb' database.""" - -from uuid import UUID - -from beartype import beartype - -from ...common.protocol.entries import Entry -from ...common.utils.messages import content_to_json -from ..utils import cozo_query - - -@cozo_query -@beartype -def get_toplevel_entries_query(session_id: UUID) -> tuple[str, dict]: - """ - Retrieves top-level entries from the database for a given session. - - Parameters: - - session_id (UUID): The unique identifier for the session. - - Returns: - - pd.DataFrame: A DataFrame containing the queried top-level entries. - """ - - query = """ - # Construct a datalog query to retrieve entries not summarized by any other entry. - input[session_id] <- [[to_uuid($session_id)]] - # Define an input table with the session ID to filter entries related to the specific session. - - # Query to retrieve top-level entries that are not summarized by any other entry, ensuring uniqueness. - ?[ - entry_id, - session_id, - source, - role, - name, - content, - token_count, - created_at, - timestamp, - ] := - input[session_id], - *entries{ - entry_id, - session_id, - source, - role, - name, - content, - token_count, - created_at, - timestamp, - }, - not *relations { - relation: "summary_of", - tail: entry_id, - } - - :sort timestamp - """ - - return (query, {"session_id": str(session_id)}) - - -@cozo_query -@beartype -def entries_summarization_query( - session_id: UUID, new_entry: Entry, old_entry_ids: list[UUID] -) -> tuple[str, dict]: - """ - Inserts a new entry and its summarization relations into the database. - - Parameters: - - session_id (UUID): The session identifier. - - new_entry (Entry): The new entry to be inserted. - - old_entry_ids (list[UUID]): List of entry IDs that the new entry summarizes. - - Returns: - - pd.DataFrame: A DataFrame containing the result of the insertion operation. - """ - - # Prepare relations data for insertion, marking the new entry as a summary of the old entries. - relations = [ - [str(new_entry.id), "summary_of", str(old_id)] for old_id in old_entry_ids - ] - # Create a list of relations indicating which entries the new entry summarizes. - - # Convert the new entry's source information into JSON format for storage. - - entries = [ - [ - str(new_entry.id), - str(session_id), - new_entry.source, - new_entry.role, - new_entry.name or "", - content_to_json(new_entry.content), - new_entry.token_count, - new_entry.tokenizer, - new_entry.created_at, - new_entry.timestamp, - ] - ] - - query = """ - { - ?[entry_id, session_id, source, role, name, content, token_count, tokenizer, created_at, timestamp] <- $entries - - :insert entries { - entry_id, - session_id, - source, - role, - name, => - content, - token_count, - tokenizer, - created_at, - timestamp, - } - } - { - ?[head, relation, tail] <- $relations - - :insert relations { - head, - relation, - tail, - } - } - """ - - return (query, {"relations": relations, "entries": entries}) diff --git a/agents-api/agents_api/models/entry/get_history.py b/agents-api/agents_api/models/entry/get_history.py new file mode 100644 index 000000000..43f8c5cdc --- /dev/null +++ b/agents-api/agents_api/models/entry/get_history.py @@ -0,0 +1,138 @@ +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 History +from ...common.utils.cozo import uuid_int_list_to_uuid4 as fix_uuid +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( + History, + one=True, + transform=lambda d: { + "relations": [ + { + # This is needed because cozo has a bug: + # https://github.com/cozodb/cozo/issues/269 + "head": fix_uuid(r["head"]), + "relation": r["relation"], + "tail": fix_uuid(r["tail"]), + } + for r in d.pop("relations") + ], + **d, + }, +) +@cozo_query +@beartype +def get_history( + *, + developer_id: UUID, + session_id: UUID, + allowed_sources: list[str] = ["api_request", "api_response"], +) -> tuple[str, dict]: + developer_id = str(developer_id) + session_id = str(session_id) + + history_query = """ + session_entries[collect(entry)] := + *entries { + session_id, + entry_id, + role, + name, + content, + source, + token_count, + created_at, + timestamp, + }, + source in $allowed_sources, + session_id = to_uuid($session_id), + entry = { + "session_id": session_id, + "id": entry_id, + "role": role, + "name": name, + "content": content, + "source": source, + "token_count": token_count, + "created_at": created_at, + "timestamp": timestamp + } + + session_relations[unique(item)] := + session_id = to_uuid($session_id), + *entries { + session_id, + entry_id: head + }, + + *relations { + head, + relation, + tail + }, + + item = { + "head": head, + "relation": relation, + "tail": tail + } + + session_relations[unique(item)] := + session_id = to_uuid($session_id), + *entries { + session_id, + entry_id: tail + }, + + *relations { + head, + relation, + tail + }, + + item = { + "head": head, + "relation": relation, + "tail": tail + } + + ?[entries, relations, session_id, created_at] := + session_entries[entries], + session_relations[relations], + session_id = to_uuid($session_id), + created_at = now() + """ + + queries = [ + verify_developer_id_query(developer_id), + verify_developer_owns_resource_query( + developer_id, "sessions", session_id=session_id + ), + history_query, + ] + + query = "}\n\n{\n".join(queries) + query = f"{{ {query} }}" + + return (query, {"session_id": session_id, "allowed_sources": allowed_sources}) diff --git a/agents-api/agents_api/models/entry/list_entries.py b/agents-api/agents_api/models/entry/list_entries.py index 9091b25d8..a4a772008 100644 --- a/agents-api/agents_api/models/entry/list_entries.py +++ b/agents-api/agents_api/models/entry/list_entries.py @@ -6,10 +6,7 @@ from pycozo.client import QueryException from pydantic import ValidationError -from ...autogen.openapi_model import CreateEntryRequest, Entry -from ...common.utils.cozo import cozo_process_mutate_data -from ...common.utils.datetime import utcnow -from ...common.utils.messages import content_to_json +from ...autogen.openapi_model import Entry from ..utils import ( cozo_query, partialclass, @@ -27,38 +24,42 @@ TypeError: partialclass(HTTPException, status_code=400), } ) -@wrap_in_class( - Entry, - transform=lambda d: { - "id": UUID(d.pop("entry_id")), - **d, - }, -) +@wrap_in_class(Entry) @cozo_query @beartype def list_entries( *, developer_id: UUID, session_id: UUID, - limit: int = 100, allowed_sources: list[str] = ["api_request", "api_response"], + limit: int = -1, + offset: int = 0, + sort_by: Literal["created_at", "timestamp"] = "timestamp", + direction: Literal["asc", "desc"] = "asc", + exclude_relations: list[str] = [], ) -> tuple[str, dict]: """ Constructs and executes a query to retrieve entries from the 'cozodb' database. - - Parameters: - session_id (UUID): The session ID to filter entries. - limit (int): The maximum number of entries to return. Defaults to 100. - offset (int): The offset from which to start returning entries. Defaults to 0. - """ + developer_id = str(developer_id) session_id = str(session_id) - list_query = """ + sort = f"{'-' if direction == 'desc' else ''}{sort_by}" + + exclude_relations_query = """ + not *relations { + relation, + tail: id, + }, + relation in $exclude_relations, + # !is_in(relation, $exclude_relations), + """ + + list_query = f""" ?[ session_id, - entry_id, + id, role, name, content, @@ -66,9 +67,9 @@ def list_entries( token_count, created_at, timestamp, - ] := *entries{ + ] := *entries {{ session_id, - entry_id, + entry_id: id, role, name, content, @@ -76,15 +77,17 @@ def list_entries( token_count, created_at, timestamp, - }, + }}, + {exclude_relations_query if exclude_relations else ''} source in $allowed_sources, session_id = to_uuid($session_id), - :sort timestamp + :sort {sort} """ if limit > 0: list_query += f"\n:limit {limit}" + list_query += f"\n:offset {offset}" queries = [ verify_developer_id_query(developer_id), @@ -97,4 +100,11 @@ def list_entries( query = "}\n\n{\n".join(queries) query = f"{{ {query} }}" - return (query, {"session_id": session_id, "allowed_sources": allowed_sources}) + return ( + query, + { + "session_id": session_id, + "allowed_sources": allowed_sources, + "exclude_relations": exclude_relations, + }, + ) diff --git a/agents-api/agents_api/models/entry/proc_mem_context.py b/agents-api/agents_api/models/entry/proc_mem_context.py deleted file mode 100644 index 765af932c..000000000 --- a/agents-api/agents_api/models/entry/proc_mem_context.py +++ /dev/null @@ -1,360 +0,0 @@ -from uuid import UUID - -from beartype import beartype - -from ..utils import cozo_query - - -@cozo_query -@beartype -def proc_mem_context_query( - session_id: UUID, - tool_query_embedding: list[float], - doc_query_embedding: list[float], - tools_confidence: float = 0, - docs_confidence: float = 0.4, - k_tools: int = 3, - k_docs: int = 3, -): - """Executes a complex query to retrieve memory context based on session ID, tool and document embeddings. - - Parameters: - session_id (UUID), - doc_query_embedding (list[float]), - tools_confidence (float), - docs_confidence (float), - k_tools (int), - k_docs (int), - client (CozoClient). - - Return type: - A pandas DataFrame containing the query results. - """ - VECTOR_SIZE = 1024 - session_id = str(session_id) - - tools_radius: float = 1.0 - tools_confidence - docs_radius: float = 1.0 - docs_confidence - - # Define the datalog query to collect memory context. - query = f""" - {{ - # Input table for the query - # (This is temporary to this query) - input[session_id, doc_query] <- [[ - to_uuid($session_id), - # $tool_query_embedding, - $doc_query_embedding, - ]] - - ?[session_id, doc_query, agent_id, user_id] := - input[session_id, doc_query], - *session_lookup{{ - session_id, - agent_id, - user_id, - }} - - :create _input {{ - session_id: Uuid, - agent_id: Uuid, - user_id: Uuid?, - # tool_query: , - doc_query: , - }} - }} {{ - # Collect situation details based on session ID. - # Collect situation - ?[role, name, content, token_count, created_at, index] := - *_input{{session_id}}, - *sessions{{ - session_id, - situation: situation_text, - created_at, - @ "NOW" - }}, - content = [{{"type": "text", "text": situation_text}}], - index = 0, # Situation entry should be the first entry - role = "system", - name = "situation", - num_chars = length(content), - token_count = to_int(num_chars / 3.5), - num_chars > 0 - - # and agent description - ?[role, name, content, token_count, created_at, index] := - *_input{{agent_id}}, - *agents{{ - agent_id, - name: agent_name, - about, - updated_at: created_at, - }}, - index = 1, - role = "system", - name = "information", - about_text = concat('About me (', agent_name, ') '), - content = [{{"type": "text", "text": about_text}}], - num_chars = length(about_text), - token_count = to_int(num_chars / 3.5), - num_chars > 0 - - # and user description - ?[role, name, content, token_count, created_at, index] := - *_input{{user_id}}, - *users{{ - user_id, - name: user_name, - about, - updated_at: created_at, - }}, - !is_null(user_id), - index = 2, - role = "system", - name = "information", - about_text = concat('About the user ', if(length(user_name) > 0, concat('(', user_name, ') '), ""), about), - content = [{{"type": "text", "text": about_text}}], - num_chars = length(about_text), - token_count = to_int(num_chars / 3.5), - num_chars > 0 - - # Save in temp table - :create _preamble {{ - role: String, - name: String?, - content: [Json], - token_count: Int, - created_at: Float, - index: Float, - }} - }} {{ - # Collect tool information based on agent ID and tool query embedding. - # Collect all tools - - # Search for tools - ?[role, name, content, token_count, created_at, index] := - *_input{{agent_id}}, - # ~agent_functions:embedding_space {{ - # agent_id, - # name: fn_name, - # description, - # parameters, - # updated_at: created_at | - # query: tool_query, - # k: $k_tools, - # ef: 128, - # radius: $tools_radius, - # bind_distance: distance, - # }}, - *agent_functions {{ - agent_id, - name: fn_name, - description, - parameters, - updated_at: created_at, - }}, - - role = "system", - name = "functions", - fn_data = {{ - "name": fn_name, - "description": description, - "parameters": parameters - }}, - fn_json = dump_json(fn_data), - content = [{{"type": "json", "text": fn_json}}], - num_chars = length(fn_json), - token_count = to_int(num_chars / 3.5), - index = 4 - - # Save in temp table - :create _tools {{ - role: String, - name: String?, - content: [Json], - token_count: Int, - created_at: Float, - index: Float, - }} - }} {{ - # Collect document information based on agent ID and document query embedding. - # Collect agent docs - - # Search for agent docs - ?[role, name, content, token_count, created_at, index, agent_doc_id, user_doc_id] := - *_input{{agent_id, doc_query}}, - *agent_docs {{ - agent_id, - doc_id: agent_doc_id, - created_at, - }}, - ~information_snippets:embedding_space {{ - doc_id: agent_doc_id, - snippet_idx, - title, - snippet | - query: doc_query, - k: $k_docs, - ef: 128, - radius: $docs_radius, - bind_distance: distance, - }}, - role = "system", - name = "information", - information_text = concat(title, ':\n...', snippet), - content = [{{"type": "text", "text": information_text}}], - num_chars = length(information_text), - token_count = to_int(num_chars / 3.5), - index = 5 + (snippet_idx * 0.01), - user_doc_id = null, - - # Save in temp table - :create _agent_docs {{ - role: String, - content: [Json], - token_count: Int, - created_at: Float, - index: Float, - name: String? default null, - agent_doc_id: Uuid? default null, - user_doc_id: Uuid? default null, - }} - }} {{ - # Collect document information based on user ID and document query embedding. - # Collect user docs - - # Search for user docs - ?[role, name, content, token_count, created_at, index, user_doc_id, agent_doc_id] := - *_input{{user_id, doc_query}}, - *user_docs {{ - user_id, - doc_id: user_doc_id, - created_at, - }}, - ~information_snippets:embedding_space {{ - doc_id: user_doc_id, - snippet_idx, - title, - snippet | - query: doc_query, - k: $k_docs, - ef: 128, - radius: $docs_radius, - bind_distance: distance, - }}, - role = "system", - name = "information", - information_text = concat(title, ':\n...', snippet), - content = [{{"type": "text", "text": information_text}}], - num_chars = length(information_text), - token_count = to_int(num_chars / 3.5), - index = 5 + (snippet_idx * 0.01), - agent_doc_id = null, - - # Save in temp table - :create _user_docs {{ - role: String, - content: [Json], - token_count: Int, - created_at: Float, - index: Float, - name: String? default null, - agent_doc_id: Uuid? default null, - user_doc_id: Uuid? default null, - }} - }} {{ - # Collect all entries related to the session. - # Collect all entries - ?[role, name, content, token_count, created_at, index] := - *_input{{session_id}}, - *entries{{ - entry_id, - session_id, - source, - role, - name, - content, - token_count, - created_at, - }}, - not *relations {{ - relation: "summary_of", - tail: entry_id, - }}, - index = 6, - source == "api_request" || source == "api_response" || source == "summarizer", - - # Save in temp table - :create _entries {{ - role: String, - name: String?, - content: [Json], - token_count: Int, - created_at: Float, - index: Float, - }} - }} {{ - # Combine all collected data into a structured format. - # Combine all - ?[role, name, content, token_count, created_at, index, agent_doc_id, user_doc_id] := - *_preamble{{ - role, name, content, token_count, created_at, index, - }}, - agent_doc_id = null, user_doc_id = null, - - # Now let's get instructions - ?[role, name, content, token_count, created_at, index, agent_doc_id, user_doc_id] := - *_input{{agent_id}}, - *agents{{ - agent_id, - instructions, - created_at, - }}, - role = "system", - name = "instruction", - index = 3, - content = [{{"type": "text", "text": instruction}}], - token_count = round(length(instruction) / 3.5), - instruction in instructions, - agent_doc_id = null, user_doc_id = null, - - ?[role, name, content, token_count, created_at, index, agent_doc_id, user_doc_id] := - *_tools{{ - role, name, content, token_count, created_at, index - }}, - agent_doc_id = null, user_doc_id = null, - - ?[role, name, content, token_count, created_at, index, agent_doc_id, user_doc_id] := - *_agent_docs {{ - role, name, content, token_count, created_at, index, agent_doc_id - }}, - user_doc_id = null, - - ?[role, name, content, token_count, created_at, index, agent_doc_id, user_doc_id] := - *_user_docs {{ - role, name, content, token_count, created_at, index, user_doc_id - }}, - agent_doc_id = null, - - ?[role, name, content, token_count, created_at, index, agent_doc_id, user_doc_id] := - *_entries{{ - role, name, content, token_count, created_at, index - }}, - agent_doc_id = null, user_doc_id = null, - - :sort index, created_at - }} - """ - - return ( - query, - { - "session_id": session_id, - "tool_query_embedding": tool_query_embedding, - "doc_query_embedding": doc_query_embedding, - "k_tools": k_tools, - "tools_radius": round(tools_radius, 2), - "k_docs": k_docs, - "docs_radius": round(docs_radius, 2), - }, - ) diff --git a/agents-api/agents_api/models/execution/list_execution_transitions.py b/agents-api/agents_api/models/execution/list_execution_transitions.py index fdee95af5..a36135f2d 100644 --- a/agents-api/agents_api/models/execution/list_execution_transitions.py +++ b/agents-api/agents_api/models/execution/list_execution_transitions.py @@ -25,7 +25,7 @@ def list_execution_transitions( execution_id: UUID, limit: int = 100, offset: int = 0, - sort_by: Literal["created_at", "updated_at", "deleted_at"] = "created_at", + sort_by: Literal["created_at", "updated_at"] = "created_at", direction: Literal["asc", "desc"] = "desc", ) -> tuple[str, dict]: sort = f"{'-' if direction == 'desc' else ''}{sort_by}" diff --git a/agents-api/agents_api/models/execution/list_executions.py b/agents-api/agents_api/models/execution/list_executions.py index ea2d7c923..eaef685aa 100644 --- a/agents-api/agents_api/models/execution/list_executions.py +++ b/agents-api/agents_api/models/execution/list_executions.py @@ -33,7 +33,7 @@ def list_executions( task_id: UUID, limit: int = 100, offset: int = 0, - sort_by: Literal["created_at", "updated_at", "deleted_at"] = "created_at", + sort_by: Literal["created_at", "updated_at"] = "created_at", direction: Literal["asc", "desc"] = "desc", ) -> tuple[str, dict]: sort = f"{'-' if direction == 'desc' else ''}{sort_by}" diff --git a/agents-api/agents_api/models/session/get_session.py b/agents-api/agents_api/models/session/get_session.py index 43eb1e5a4..262718df1 100644 --- a/agents-api/agents_api/models/session/get_session.py +++ b/agents-api/agents_api/models/session/get_session.py @@ -42,8 +42,6 @@ def get_session( developer_id = str(developer_id) # This query retrieves session information by using `input` to pass parameters, - # projects specific fields from the `sessions` and `session_lookup` relations, - # and converts `updated_at` to an integer for easier handling. get_query = """ input[developer_id, session_id] <- [[ to_uuid($developer_id), diff --git a/agents-api/agents_api/models/session/list_sessions.py b/agents-api/agents_api/models/session/list_sessions.py index 21f41ec11..653a2677f 100644 --- a/agents-api/agents_api/models/session/list_sessions.py +++ b/agents-api/agents_api/models/session/list_sessions.py @@ -34,7 +34,7 @@ def list_sessions( developer_id: UUID, limit: int = 100, offset: int = 0, - sort_by: Literal["created_at", "updated_at", "deleted_at"] = "created_at", + sort_by: Literal["created_at", "updated_at"] = "created_at", direction: Literal["asc", "desc"] = "desc", metadata_filter: dict[str, Any] = {}, ) -> tuple[str, dict]: diff --git a/agents-api/agents_api/models/session/prepare_chat_context.py b/agents-api/agents_api/models/session/prepare_chat_context.py new file mode 100644 index 000000000..d3aaba27c --- /dev/null +++ b/agents-api/agents_api/models/session/prepare_chat_context.py @@ -0,0 +1,162 @@ +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 make_session +from ...common.protocol.sessions import ChatContext +from ...common.utils.cozo import uuid_int_list_to_uuid4 as fix_uuid +from ..entry.list_entries import list_entries +from ..tools.list_tools import list_tools +from ..utils import ( + cozo_query, + fix_uuid_list, + partialclass, + rewrap_exceptions, + verify_developer_id_query, + verify_developer_owns_resource_query, + wrap_in_class, +) +from .prepare_session_data import prepare_session_data + + +def make_cozo_json_query(fields): + return ", ".join(f'"{field}": {field}' for field in fields).strip() + + +@rewrap_exceptions( + { + QueryException: partialclass(HTTPException, status_code=400), + ValidationError: partialclass(HTTPException, status_code=400), + TypeError: partialclass(HTTPException, status_code=400), + } +) +@wrap_in_class( + ChatContext, + one=True, + transform=lambda d: { + "agents": fix_uuid_list(d["agents"]), + "users": fix_uuid_list(d["users"]), + "entries": fix_uuid_list(d["entries"]), + "tools": fix_uuid_list(d["tools"]), + "session": make_session( + agents=[fix_uuid(a["id"]) for a in d["agents"]], + users=[fix_uuid(u["id"]) for u in d["users"]], + id=fix_uuid(d["session"].pop("id")), + **d["session"], + ), + }, +) +@cozo_query +@beartype +def prepare_chat_context( + *, + developer_id: UUID, + agent_id: UUID, + session_id: UUID, + # doc_query_embedding: list[float], + # docs_confidence: float = 0.4, + # k_docs: int = 3, +): + """ + Executes a complex query to retrieve memory context based on session ID, tool and document embeddings. + """ + # VECTOR_SIZE = 1024 + # docs_radius: float = 1.0 - docs_confidence + + session_data_query, sd_vars = prepare_session_data.__wrapped__( + developer_id=developer_id, session_id=session_id + ) + + # Remove the outer curly braces + session_data_query = session_data_query.strip()[1:-1] + + session_data_fields = ("session", "agents", "users") + + session_data_query += """ + :create _session_data_json { + agents: [Json], + users: [Json], + session: Json, + } + """ + + tools_query, t_vars = list_tools.__wrapped__( + developer_id=developer_id, agent_id=agent_id + ) + + # Remove the outer curly braces + tools_query = tools_query.strip()[1:-1] + + tools_fields = ("name", "type", "spec") + + tools_query += f""" + :create _tools {{ + {', '.join(tools_fields)} + }} + """ + + # TODO: Implement the following queries + # docs_query = ... + + entries_query, e_vars = list_entries.__wrapped__( + developer_id=developer_id, + session_id=session_id, + allowed_sources=["api_request", "api_response", "summarizer"], + exclude_relations=["summary_of"], + ) + + # Remove the outer curly braces + entries_query = entries_query.strip()[1:-1] + + entries_fields = ("source", "role", "name", "content", "token_count", "timestamp") + + entries_query += f""" + :create _entries {{ + {', '.join(entries_fields)} + }} + """ + + combine_query = f""" + tools_json[collect(tool)] := + *_tools {{ {', '.join(tools_fields)} }}, + tool = {{ {make_cozo_json_query(tools_fields)} }} + + entries_json[collect(entry)] := + *_entries {{ {', '.join(entries_fields)} }}, + entry = {{ {make_cozo_json_query(entries_fields)} }} + + ?[{', '.join(session_data_fields)}, tools, entries] := + *_session_data_json {{ {', '.join(session_data_fields)} }}, + tools_json[tools], + entries_json[entries] + """ + + queries = [ + verify_developer_id_query(developer_id), + verify_developer_owns_resource_query( + developer_id, "sessions", session_id=session_id + ), + session_data_query, + tools_query, + entries_query, + combine_query, + ] + + query = "}\n\n{\n".join(queries) + query = f"{{ {query} }}" + + return ( + query, + { + "session_id": str(session_id), + **sd_vars, + **t_vars, + **e_vars, + # "doc_query_embedding": doc_query_embedding, + # "k_docs": k_docs, + # "docs_radius": round(docs_radius, 2), + }, + ) diff --git a/agents-api/agents_api/models/session/prepare_session_data.py b/agents-api/agents_api/models/session/prepare_session_data.py new file mode 100644 index 000000000..8ed07a2f6 --- /dev/null +++ b/agents-api/agents_api/models/session/prepare_session_data.py @@ -0,0 +1,203 @@ +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 make_session +from ...common.protocol.sessions import SessionData +from ...common.utils.cozo import uuid_int_list_to_uuid4 as fix_uuid +from ..utils import ( + cozo_query, + fix_uuid_list, + 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( + SessionData, + one=True, + transform=lambda d: { + "agents": fix_uuid_list(d["agents"]), + "users": fix_uuid_list(d["users"]), + "session": make_session( + agents=[fix_uuid(a["id"]) for a in d["agents"]], + users=[fix_uuid(u["id"]) for u in d["users"]], + id=fix_uuid(d["session"].pop("id")), + **d["session"], + ), + }, +) +@cozo_query +@beartype +def prepare_session_data( + *, + developer_id: UUID, + session_id: UUID, +) -> tuple[str, dict]: + """Constructs and executes a datalog query to retrieve session data from the 'cozodb' database. + + Parameters: + developer_id (UUID): The developer's unique identifier. + session_id (UUID): The session's unique identifier. + """ + session_id = str(session_id) + developer_id = str(developer_id) + + # This query retrieves session information by using `input` to pass parameters, + get_query = """ + input[session_id] <- [[ + to_uuid($session_id), + ]] + + participants[collect(participant_id), participant_type] := + input[session_id], + *session_lookup{ + session_id, + participant_id, + participant_type, + } + + agents[agent_ids] := participants[agent_ids, "agent"] + + # We have to do this dance because users can be zero or more + users[user_ids] := + participants[user_ids, "user"] + + users[user_ids] := + not participants[_, "user"], + user_ids = [] + + settings_data[agent_id, settings] := + *agent_default_settings { + agent_id, + frequency_penalty, + presence_penalty, + length_penalty, + repetition_penalty, + top_p, + temperature, + min_p, + preset, + }, + settings = { + "frequency_penalty": frequency_penalty, + "presence_penalty": presence_penalty, + "length_penalty": length_penalty, + "repetition_penalty": repetition_penalty, + "top_p": top_p, + "temperature": temperature, + "min_p": min_p, + "preset": preset, + } + + agent_data[collect(record)] := + agents[agent_ids], + agent_id in agent_ids, + *agents{ + agent_id, + model, + name, + about, + created_at, + updated_at, + metadata, + instructions, + }, + settings_data[agent_id, default_settings], + record = { + "id": agent_id, + "name": name, + "model": model, + "about": about, + "created_at": created_at, + "updated_at": updated_at, + "metadata": metadata, + "default_settings": default_settings, + "instructions": instructions, + } + + user_data[collect(record)] := + users[user_ids], + user_id in user_ids, + *users{ + user_id, + name, + about, + created_at, + updated_at, + metadata, + }, + record = { + "id": user_id, + "name": name, + "about": about, + "created_at": created_at, + "updated_at": updated_at, + "metadata": metadata, + } + + session_data[record] := + input[session_id], + *sessions{ + session_id, + situation, + summary, + created_at, + updated_at: validity, + metadata, + render_templates, + token_budget, + context_overflow, + @ "NOW" + }, + updated_at = to_int(validity), + record = { + "id": session_id, + "situation": situation, + "summary": summary, + "created_at": created_at, + "updated_at": updated_at, + "metadata": metadata, + "render_templates": render_templates, + "token_budget": token_budget, + "context_overflow": context_overflow, + } + + ?[ + agents, + users, + session, + ] := + session_data[session], + user_data[users], + agent_data[agents] + """ + + queries = [ + verify_developer_id_query(developer_id), + verify_developer_owns_resource_query( + developer_id, "sessions", session_id=session_id + ), + get_query, + ] + + query = "}\n\n{\n".join(queries) + query = f"{{ {query} }}" + + return ( + query, + {"developer_id": developer_id, "session_id": session_id}, + ) diff --git a/agents-api/agents_api/models/session/session_data.py b/agents-api/agents_api/models/session/session_data.py deleted file mode 100644 index 404a13c44..000000000 --- a/agents-api/agents_api/models/session/session_data.py +++ /dev/null @@ -1,205 +0,0 @@ -from uuid import UUID - -from beartype import beartype -from pycozo.client import Client as CozoClient - -from ...clients.cozo import client as cozo_client -from ...common.protocol.sessions import SessionData -from ..utils import cozo_query - - -@cozo_query -@beartype -def session_data_query( - developer_id: UUID, - session_id: UUID, -) -> tuple[str, dict]: - """Constructs and executes a datalog query to retrieve session data from the 'cozodb' database. - - Parameters: - developer_id (UUID): The developer's unique identifier. - session_id (UUID): The session's unique identifier. - - Returns: - pd.DataFrame: A DataFrame containing the query results. - """ - # This query retrieves detailed session information, including metadata and default settings for agents involved in the session. - query = """ - input[developer_id, session_id] <- [[ - to_uuid($developer_id), - to_uuid($session_id), - ]] - - ?[ - agent_id, - user_id, - session_id, - situation, - summary, - updated_at, - created_at, - user_name, - user_about, - agent_name, - agent_about, - model, - default_settings, - metadata, - render_templates, - token_budget, - context_overflow, - user_metadata, - agent_metadata, - ] := input[developer_id, session_id], - *sessions{ - developer_id, - session_id, - situation, - summary, - created_at, - updated_at: validity, - metadata, - render_templates, - token_budget, - context_overflow, - @ "NOW" - }, - *session_lookup{ - agent_id, - user_id, - session_id, - }, updated_at = to_int(validity), - *users{ - user_id, - name: user_name, - about: user_about, - metadata: user_metadata, - }, - *agents{ - agent_id, - name: agent_name, - about: agent_about, - model, - metadata: agent_metadata, - }, - *agent_default_settings { - agent_id, - frequency_penalty, - presence_penalty, - length_penalty, - repetition_penalty, - top_p, - temperature, - min_p, - preset, - }, - default_settings = { - "frequency_penalty": frequency_penalty, - "presence_penalty": presence_penalty, - "length_penalty": length_penalty, - "repetition_penalty": repetition_penalty, - "top_p": top_p, - "temperature": temperature, - "min_p": min_p, - "preset": preset, - } - ?[ - agent_id, - user_id, - session_id, - situation, - summary, - updated_at, - created_at, - user_name, - user_about, - agent_name, - agent_about, - model, - default_settings, - metadata, - render_templates, - token_budget, - context_overflow, - user_metadata, - agent_metadata, - ] := input[developer_id, session_id], - *sessions{ - developer_id, - session_id, - situation, - summary, - created_at, - updated_at: validity, - metadata, - render_templates, - token_budget, - context_overflow, - @ "NOW" - }, - *session_lookup{ - agent_id, - user_id, - session_id, - }, updated_at = to_int(validity), - not *users{ - user_id - }, user_name=null, user_about=null, user_metadata=null, - *agents{ - agent_id, - name: agent_name, - about: agent_about, - model, - metadata: agent_metadata, - }, - *agent_default_settings { - agent_id, - frequency_penalty, - presence_penalty, - length_penalty, - repetition_penalty, - top_p, - temperature, - min_p, - preset, - }, - default_settings = { - "frequency_penalty": frequency_penalty, - "presence_penalty": presence_penalty, - "length_penalty": length_penalty, - "repetition_penalty": repetition_penalty, - "top_p": top_p, - "temperature": temperature, - "min_p": min_p, - "preset": preset, - } - - - """ - - return (query, {"developer_id": str(developer_id), "session_id": str(session_id)}) - - -def get_session_data( - developer_id: UUID, session_id: UUID, client: CozoClient = cozo_client -) -> SessionData | None: - """Calls `session_data_query` to get session data and processes the result. - - If data is found, returns a `SessionData` object; otherwise, returns `None`. - - Parameters: - developer_id (UUID): The developer's unique identifier. - session_id (UUID): The session's unique identifier. - client (CozoClient): The database client used to execute the query. - - Returns: - SessionData | None: A SessionData object if data is found, otherwise `None`. - """ - result = session_data_query(developer_id, session_id, client=client) - # Check if the query returned any data; return `None` if not. - if result.empty: - return None - - data = result.iloc[0].to_dict() - - return SessionData(**data) diff --git a/agents-api/agents_api/models/task/list_tasks.py b/agents-api/agents_api/models/task/list_tasks.py index 699f879d3..6de99a981 100644 --- a/agents-api/agents_api/models/task/list_tasks.py +++ b/agents-api/agents_api/models/task/list_tasks.py @@ -33,7 +33,7 @@ def list_tasks( agent_id: UUID, limit: int = 100, offset: int = 0, - sort_by: Literal["created_at", "updated_at", "deleted_at"] = "created_at", + sort_by: Literal["created_at", "updated_at"] = "created_at", direction: Literal["asc", "desc"] = "desc", ) -> tuple[str, dict]: sort = f"{'-' if direction == 'desc' else ''}{sort_by}" diff --git a/agents-api/agents_api/models/tools/list_tools.py b/agents-api/agents_api/models/tools/list_tools.py index d997c0aa6..af0184560 100644 --- a/agents-api/agents_api/models/tools/list_tools.py +++ b/agents-api/agents_api/models/tools/list_tools.py @@ -33,7 +33,7 @@ def list_tools( agent_id: UUID, limit: int = 100, offset: int = 0, - sort_by: Literal["created_at", "updated_at", "deleted_at"] = "created_at", + sort_by: Literal["created_at", "updated_at"] = "created_at", direction: Literal["asc", "desc"] = "desc", ) -> tuple[str, dict]: agent_id = str(agent_id) diff --git a/agents-api/agents_api/models/user/list_users.py b/agents-api/agents_api/models/user/list_users.py index 648496189..1284a680a 100644 --- a/agents-api/agents_api/models/user/list_users.py +++ b/agents-api/agents_api/models/user/list_users.py @@ -32,7 +32,7 @@ def list_users( developer_id: UUID, limit: int = 100, offset: int = 0, - sort_by: Literal["created_at", "updated_at", "deleted_at"] = "created_at", + sort_by: Literal["created_at", "updated_at"] = "created_at", direction: Literal["asc", "desc"] = "desc", metadata_filter: dict[str, Any] = {}, ) -> tuple[str, dict]: diff --git a/agents-api/agents_api/models/utils.py b/agents-api/agents_api/models/utils.py index 49a5bbb55..8dd479110 100644 --- a/agents-api/agents_api/models/utils.py +++ b/agents-api/agents_api/models/utils.py @@ -7,10 +7,15 @@ from pydantic import BaseModel from ..clients.cozo import client as cozo_client +from ..common.utils.cozo import uuid_int_list_to_uuid4 as fix_uuid P = ParamSpec("P") +def fix_uuid_list(x, attr="id"): + return [{**i, attr: fix_uuid(i[attr])} for i in x] + + def partialclass(cls, *args, **kwargs): cls_signature = inspect.signature(cls) bound = cls_signature.bind_partial(*args, **kwargs) @@ -172,7 +177,7 @@ def wrapper(*args, **kwargs): else transform(error) ) - raise transform(error) from error + raise transform(new_error) from error raise diff --git a/agents-api/agents_api/routers/agents/create_agent.py b/agents-api/agents_api/routers/agents/create_agent.py index 8e03c316c..104851203 100644 --- a/agents-api/agents_api/routers/agents/create_agent.py +++ b/agents-api/agents_api/routers/agents/create_agent.py @@ -6,7 +6,6 @@ from starlette.status import HTTP_201_CREATED from ...autogen.openapi_model import CreateAgentRequest, ResourceCreatedResponse -from ...common.utils.datetime import utcnow from ...dependencies.developer_id import get_developer_id from ...models.agent.create_agent import create_agent_query from .router import router diff --git a/agents-api/agents_api/routers/agents/list_agents.py b/agents-api/agents_api/routers/agents/list_agents.py index f22a50e05..652a26de7 100644 --- a/agents-api/agents_api/routers/agents/list_agents.py +++ b/agents-api/agents_api/routers/agents/list_agents.py @@ -5,7 +5,7 @@ from ...autogen.openapi_model import Agent from ...dependencies.developer_id import get_developer_id -from ...models.agent.list_agents import list_agents as list_agents_query +from ...models.agent.list_agents import list_agents from .router import router @@ -16,7 +16,7 @@ async def list_agents( offset: int = 0, metadata_filter: str = "{}", ) -> List[Agent]: - agents = list_agents_query( + agents = list_agents( developer_id=x_developer_id, limit=limit, offset=offset, diff --git a/agents-api/agents_api/routers/sessions/list_sessions.py b/agents-api/agents_api/routers/sessions/list_sessions.py index 5ede0fce6..e93c22228 100644 --- a/agents-api/agents_api/routers/sessions/list_sessions.py +++ b/agents-api/agents_api/routers/sessions/list_sessions.py @@ -7,7 +7,7 @@ from ...autogen.openapi_model import Session from ...dependencies.developer_id import get_developer_id -from ...models.session.list_sessions import list_sessions as list_sessions_query +from ...models.session.list_sessions import list_sessions from .router import router @@ -16,7 +16,7 @@ class SessionList(BaseModel): @router.get("/sessions", tags=["sessions"]) -async def list_sessions( +async def list_sessions_route( x_developer_id: Annotated[UUID4, Depends(get_developer_id)], limit: int = 100, offset: int = 0, @@ -30,7 +30,7 @@ async def list_sessions( detail="metadata_filter is not a valid JSON", ) - query_results = list_sessions_query( + query_results = list_sessions( developer_id=x_developer_id, limit=limit, offset=offset, diff --git a/agents-api/agents_api/routers/sessions/session.py b/agents-api/agents_api/routers/sessions/session.py index 51f2418d7..ddd78b315 100644 --- a/agents-api/agents_api/routers/sessions/session.py +++ b/agents-api/agents_api/routers/sessions/session.py @@ -36,10 +36,10 @@ load_context, validate_and_extract_tool_calls, ) -from ...models.entry.create_entries import create_entries as create_entries_query -from ...models.entry.proc_mem_context import proc_mem_context_query +from ...models.entry.create_entries import create_entries +from ...models.session.prepare_chat_context import prepare_chat_context from ...models.session.get_cached_response import get_cached_response -from ...models.session.session_data import get_session_data +from ...models.session.prepare_session_data import prepare_session_data from ...models.session.set_cached_response import set_cached_response from .exceptions import InputTooBigError from .protocol import Settings diff --git a/agents-api/agents_api/routers/tasks/routers.py b/agents-api/agents_api/routers/tasks/routers.py index 37c9a29d1..19e56c77c 100644 --- a/agents-api/agents_api/routers/tasks/routers.py +++ b/agents-api/agents_api/routers/tasks/routers.py @@ -18,7 +18,6 @@ ResourceCreatedResponse, ResourceUpdatedResponse, Task, - UpdateExecutionTransitionRequest, ) from agents_api.clients.cozo import client as cozo_client from agents_api.clients.temporal import run_task_execution_workflow diff --git a/agents-api/agents_api/routers/users/list_users.py b/agents-api/agents_api/routers/users/list_users.py index c7e7b33c4..08af1159a 100644 --- a/agents-api/agents_api/routers/users/list_users.py +++ b/agents-api/agents_api/routers/users/list_users.py @@ -7,7 +7,7 @@ from ...autogen.openapi_model import User from ...dependencies.developer_id import get_developer_id -from ...models.user.list_users import list_users as list_users_query +from ...models.user.list_users import list_users_query from .router import router diff --git a/agents-api/agents_api/worker/__main__.py b/agents-api/agents_api/worker/__main__.py index 364e11200..07dce7af1 100644 --- a/agents-api/agents_api/worker/__main__.py +++ b/agents-api/agents_api/worker/__main__.py @@ -19,14 +19,13 @@ from ..activities.salient_questions import salient_questions from ..activities.summarization import summarization from ..activities.task_steps import ( - prompt_step, - evaluate_step, - yield_step, # tool_call_step, # error_step, if_else_step, + prompt_step, transition_step, # evaluate_step, + yield_step, ) from ..activities.truncation import truncation from ..env import ( diff --git a/agents-api/agents_api/workflows/task_execution.py b/agents-api/agents_api/workflows/task_execution.py index 31614e9d9..f4adc20fb 100644 --- a/agents-api/agents_api/workflows/task_execution.py +++ b/agents-api/agents_api/workflows/task_execution.py @@ -20,6 +20,7 @@ ToolCallWorkflowStep, # ErrorWorkflowStep, IfElseWorkflowStep, + PromptWorkflowStep, StepContext, TransitionInfo, YieldWorkflowStep, diff --git a/agents-api/poetry.lock b/agents-api/poetry.lock index 5af8153a3..16b8c4553 100644 --- a/agents-api/poetry.lock +++ b/agents-api/poetry.lock @@ -2186,13 +2186,13 @@ typing-extensions = ">=4.0.0,<5.0.0" [[package]] name = "jupyter-ai" -version = "2.19.1" +version = "2.20.0" description = "A generative AI extension for JupyterLab" optional = false python-versions = ">=3.8" files = [ - {file = "jupyter_ai-2.19.1-py3-none-any.whl", hash = "sha256:9521cdfaada7c2e9c660cdbd7084235ebfaa7dfbf4f734f639f88fa892661e74"}, - {file = "jupyter_ai-2.19.1.tar.gz", hash = "sha256:4a4f61411d41ac16bc5838970ed4c54c2dd64c9b56d9277430c6a83ebd792f3d"}, + {file = "jupyter_ai-2.20.0-py3-none-any.whl", hash = "sha256:e65fb9d3d566bd67e9846716fba0a712955f8ea9b2779dc0902c99e4d4766a3c"}, + {file = "jupyter_ai-2.20.0.tar.gz", hash = "sha256:3544c8906a1ea15aa012a862964832277bd899e9ca2e715b571ce9c3ea69fa23"}, ] [package.dependencies] @@ -2576,13 +2576,13 @@ extended-testing = ["beautifulsoup4 (>=4.12.3,<5.0.0)", "lxml (>=4.9.3,<6.0)"] [[package]] name = "langsmith" -version = "0.1.93" +version = "0.1.94" description = "Client library to connect to the LangSmith LLM Tracing and Evaluation Platform." optional = false python-versions = "<4.0,>=3.8.1" files = [ - {file = "langsmith-0.1.93-py3-none-any.whl", hash = "sha256:811210b9d5f108f36431bd7b997eb9476a9ecf5a2abd7ddbb606c1cdcf0f43ce"}, - {file = "langsmith-0.1.93.tar.gz", hash = "sha256:285b6ad3a54f50fa8eb97b5f600acc57d0e37e139dd8cf2111a117d0435ba9b4"}, + {file = "langsmith-0.1.94-py3-none-any.whl", hash = "sha256:0d01212086d58699f75814117b026784218042f7859877ce08a248a98d84aa8d"}, + {file = "langsmith-0.1.94.tar.gz", hash = "sha256:e44afcdc9eee6f238f6a87a02bba83111bd5fad376d881ae299834e06d39d712"}, ] [package.dependencies] @@ -4741,13 +4741,13 @@ tornado = ["tornado (>=5)"] [[package]] name = "setuptools" -version = "71.1.0" +version = "72.1.0" description = "Easily download, build, install, upgrade, and uninstall Python packages" optional = false python-versions = ">=3.8" files = [ - {file = "setuptools-71.1.0-py3-none-any.whl", hash = "sha256:33874fdc59b3188304b2e7c80d9029097ea31627180896fb549c578ceb8a0855"}, - {file = "setuptools-71.1.0.tar.gz", hash = "sha256:032d42ee9fb536e33087fb66cac5f840eb9391ed05637b3f2a76a7c8fb477936"}, + {file = "setuptools-72.1.0-py3-none-any.whl", hash = "sha256:5a03e1860cf56bb6ef48ce186b0e557fdba433237481a9a625176c2831be15d1"}, + {file = "setuptools-72.1.0.tar.gz", hash = "sha256:8d243eff56d095e5817f796ede6ae32941278f542e0f941867cc05ae52b162ec"}, ] [package.extras] diff --git a/openapi.yaml b/openapi.yaml new file mode 120000 index 000000000..f381c1085 --- /dev/null +++ b/openapi.yaml @@ -0,0 +1 @@ +typespec/tsp-output/@typespec/openapi3/openapi-0.4.0.yaml \ No newline at end of file diff --git a/sdks/python/julep/api/__init__.py b/sdks/python/julep/api/__init__.py index 0c7e39653..7106c2d00 100644 --- a/sdks/python/julep/api/__init__.py +++ b/sdks/python/julep/api/__init__.py @@ -78,6 +78,13 @@ DocsTextOnlyDocSearchRequestText, DocsVectorDocSearchRequest, DocsVectorDocSearchRequestVector, + EntriesBaseEntry, + EntriesBaseEntryContent, + EntriesBaseEntryContentItem, + EntriesBaseEntryContentItemItem, + EntriesBaseEntryContentItemItem_ImageUrl, + EntriesBaseEntryContentItemItem_Text, + EntriesBaseEntrySource, EntriesChatMlImageContentPart, EntriesChatMlMessage, EntriesChatMlMessageContent, @@ -87,12 +94,6 @@ EntriesChatMlRole, EntriesChatMlTextContentPart, EntriesEntry, - EntriesEntryContent, - EntriesEntryContentItem, - EntriesEntryContentItemItem, - EntriesEntryContentItemItem_ImageUrl, - EntriesEntryContentItemItem_Text, - EntriesEntrySource, EntriesHistory, EntriesImageDetail, EntriesImageUrl, @@ -268,6 +269,13 @@ "DocsTextOnlyDocSearchRequestText", "DocsVectorDocSearchRequest", "DocsVectorDocSearchRequestVector", + "EntriesBaseEntry", + "EntriesBaseEntryContent", + "EntriesBaseEntryContentItem", + "EntriesBaseEntryContentItemItem", + "EntriesBaseEntryContentItemItem_ImageUrl", + "EntriesBaseEntryContentItemItem_Text", + "EntriesBaseEntrySource", "EntriesChatMlImageContentPart", "EntriesChatMlMessage", "EntriesChatMlMessageContent", @@ -277,12 +285,6 @@ "EntriesChatMlRole", "EntriesChatMlTextContentPart", "EntriesEntry", - "EntriesEntryContent", - "EntriesEntryContentItem", - "EntriesEntryContentItemItem", - "EntriesEntryContentItemItem_ImageUrl", - "EntriesEntryContentItemItem_Text", - "EntriesEntrySource", "EntriesHistory", "EntriesImageDetail", "EntriesImageUrl", diff --git a/sdks/python/julep/api/types/__init__.py b/sdks/python/julep/api/types/__init__.py index c1397222b..fbce21ee3 100644 --- a/sdks/python/julep/api/types/__init__.py +++ b/sdks/python/julep/api/types/__init__.py @@ -105,6 +105,15 @@ from .docs_text_only_doc_search_request_text import DocsTextOnlyDocSearchRequestText from .docs_vector_doc_search_request import DocsVectorDocSearchRequest from .docs_vector_doc_search_request_vector import DocsVectorDocSearchRequestVector +from .entries_base_entry import EntriesBaseEntry +from .entries_base_entry_content import EntriesBaseEntryContent +from .entries_base_entry_content_item import EntriesBaseEntryContentItem +from .entries_base_entry_content_item_item import ( + EntriesBaseEntryContentItemItem, + EntriesBaseEntryContentItemItem_ImageUrl, + EntriesBaseEntryContentItemItem_Text, +) +from .entries_base_entry_source import EntriesBaseEntrySource from .entries_chat_ml_image_content_part import EntriesChatMlImageContentPart from .entries_chat_ml_message import EntriesChatMlMessage from .entries_chat_ml_message_content import EntriesChatMlMessageContent @@ -116,14 +125,6 @@ from .entries_chat_ml_role import EntriesChatMlRole from .entries_chat_ml_text_content_part import EntriesChatMlTextContentPart from .entries_entry import EntriesEntry -from .entries_entry_content import EntriesEntryContent -from .entries_entry_content_item import EntriesEntryContentItem -from .entries_entry_content_item_item import ( - EntriesEntryContentItemItem, - EntriesEntryContentItemItem_ImageUrl, - EntriesEntryContentItemItem_Text, -) -from .entries_entry_source import EntriesEntrySource from .entries_history import EntriesHistory from .entries_image_detail import EntriesImageDetail from .entries_image_url import EntriesImageUrl @@ -326,6 +327,13 @@ "DocsTextOnlyDocSearchRequestText", "DocsVectorDocSearchRequest", "DocsVectorDocSearchRequestVector", + "EntriesBaseEntry", + "EntriesBaseEntryContent", + "EntriesBaseEntryContentItem", + "EntriesBaseEntryContentItemItem", + "EntriesBaseEntryContentItemItem_ImageUrl", + "EntriesBaseEntryContentItemItem_Text", + "EntriesBaseEntrySource", "EntriesChatMlImageContentPart", "EntriesChatMlMessage", "EntriesChatMlMessageContent", @@ -335,12 +343,6 @@ "EntriesChatMlRole", "EntriesChatMlTextContentPart", "EntriesEntry", - "EntriesEntryContent", - "EntriesEntryContentItem", - "EntriesEntryContentItemItem", - "EntriesEntryContentItemItem_ImageUrl", - "EntriesEntryContentItemItem_Text", - "EntriesEntrySource", "EntriesHistory", "EntriesImageDetail", "EntriesImageUrl", diff --git a/sdks/python/julep/api/types/agent_docs_route_list_request_sort_by.py b/sdks/python/julep/api/types/agent_docs_route_list_request_sort_by.py index 48208aafe..a8034d1f5 100644 --- a/sdks/python/julep/api/types/agent_docs_route_list_request_sort_by.py +++ b/sdks/python/julep/api/types/agent_docs_route_list_request_sort_by.py @@ -3,5 +3,5 @@ import typing AgentDocsRouteListRequestSortBy = typing.Union[ - typing.Literal["created_at", "updated_at", "deleted_at"], typing.Any + typing.Literal["created_at", "updated_at"], typing.Any ] diff --git a/sdks/python/julep/api/types/agent_tools_route_list_request_sort_by.py b/sdks/python/julep/api/types/agent_tools_route_list_request_sort_by.py index 5584ba780..ed9d76149 100644 --- a/sdks/python/julep/api/types/agent_tools_route_list_request_sort_by.py +++ b/sdks/python/julep/api/types/agent_tools_route_list_request_sort_by.py @@ -3,5 +3,5 @@ import typing AgentToolsRouteListRequestSortBy = typing.Union[ - typing.Literal["created_at", "updated_at", "deleted_at"], typing.Any + typing.Literal["created_at", "updated_at"], typing.Any ] diff --git a/sdks/python/julep/api/types/agents_docs_search_route_search_request_sort_by.py b/sdks/python/julep/api/types/agents_docs_search_route_search_request_sort_by.py index c687edfeb..a85bdee6c 100644 --- a/sdks/python/julep/api/types/agents_docs_search_route_search_request_sort_by.py +++ b/sdks/python/julep/api/types/agents_docs_search_route_search_request_sort_by.py @@ -3,5 +3,5 @@ import typing AgentsDocsSearchRouteSearchRequestSortBy = typing.Union[ - typing.Literal["created_at", "updated_at", "deleted_at"], typing.Any + typing.Literal["created_at", "updated_at"], typing.Any ] diff --git a/sdks/python/julep/api/types/agents_route_list_request_sort_by.py b/sdks/python/julep/api/types/agents_route_list_request_sort_by.py index 88dcc2fcf..9b605faef 100644 --- a/sdks/python/julep/api/types/agents_route_list_request_sort_by.py +++ b/sdks/python/julep/api/types/agents_route_list_request_sort_by.py @@ -3,5 +3,5 @@ import typing AgentsRouteListRequestSortBy = typing.Union[ - typing.Literal["created_at", "updated_at", "deleted_at"], typing.Any + typing.Literal["created_at", "updated_at"], typing.Any ] diff --git a/sdks/python/julep/api/types/entries_base_entry.py b/sdks/python/julep/api/types/entries_base_entry.py new file mode 100644 index 000000000..cd9a8158f --- /dev/null +++ b/sdks/python/julep/api/types/entries_base_entry.py @@ -0,0 +1,54 @@ +# This file was auto-generated by Fern from our API Definition. + +import datetime as dt +import typing + +from ..core.datetime_utils import serialize_datetime +from ..core.pydantic_utilities import deep_union_pydantic_dicts, pydantic_v1 +from .entries_base_entry_content import EntriesBaseEntryContent +from .entries_base_entry_source import EntriesBaseEntrySource +from .entries_chat_ml_role import EntriesChatMlRole + + +class EntriesBaseEntry(pydantic_v1.BaseModel): + role: EntriesChatMlRole + name: typing.Optional[str] = None + content: EntriesBaseEntryContent + source: EntriesBaseEntrySource + tokenizer: typing.Optional[str] = None + token_count: typing.Optional[int] = None + timestamp: float = pydantic_v1.Field() + """ + This is the time that this event refers to. + """ + + def json(self, **kwargs: typing.Any) -> str: + kwargs_with_defaults: typing.Any = { + "by_alias": True, + "exclude_unset": True, + **kwargs, + } + return super().json(**kwargs_with_defaults) + + def dict(self, **kwargs: typing.Any) -> typing.Dict[str, typing.Any]: + kwargs_with_defaults_exclude_unset: typing.Any = { + "by_alias": True, + "exclude_unset": True, + **kwargs, + } + kwargs_with_defaults_exclude_none: typing.Any = { + "by_alias": True, + "exclude_none": True, + **kwargs, + } + + return deep_union_pydantic_dicts( + super().dict(**kwargs_with_defaults_exclude_unset), + super().dict(**kwargs_with_defaults_exclude_none), + ) + + class Config: + frozen = True + smart_union = True + extra = pydantic_v1.Extra.allow + json_encoders = {dt.datetime: serialize_datetime} diff --git a/sdks/python/julep/api/types/entries_entry_content.py b/sdks/python/julep/api/types/entries_base_entry_content.py similarity index 59% rename from sdks/python/julep/api/types/entries_entry_content.py rename to sdks/python/julep/api/types/entries_base_entry_content.py index 8f33f71ee..163557eb0 100644 --- a/sdks/python/julep/api/types/entries_entry_content.py +++ b/sdks/python/julep/api/types/entries_base_entry_content.py @@ -2,16 +2,16 @@ import typing -from .entries_entry_content_item import EntriesEntryContentItem +from .entries_base_entry_content_item import EntriesBaseEntryContentItem from .tools_chosen_tool_call import ToolsChosenToolCall from .tools_tool import ToolsTool from .tools_tool_response import ToolsToolResponse -EntriesEntryContent = typing.Union[ - typing.List[EntriesEntryContentItem], +EntriesBaseEntryContent = typing.Union[ + typing.List[EntriesBaseEntryContentItem], ToolsTool, ToolsChosenToolCall, str, ToolsToolResponse, - typing.List[EntriesEntryContentItem], + typing.List[EntriesBaseEntryContentItem], ] diff --git a/sdks/python/julep/api/types/entries_entry_content_item.py b/sdks/python/julep/api/types/entries_base_entry_content_item.py similarity index 62% rename from sdks/python/julep/api/types/entries_entry_content_item.py rename to sdks/python/julep/api/types/entries_base_entry_content_item.py index 2d4adf723..ebb36d166 100644 --- a/sdks/python/julep/api/types/entries_entry_content_item.py +++ b/sdks/python/julep/api/types/entries_base_entry_content_item.py @@ -2,13 +2,13 @@ import typing -from .entries_entry_content_item_item import EntriesEntryContentItemItem +from .entries_base_entry_content_item_item import EntriesBaseEntryContentItemItem from .tools_chosen_tool_call import ToolsChosenToolCall from .tools_tool import ToolsTool from .tools_tool_response import ToolsToolResponse -EntriesEntryContentItem = typing.Union[ - typing.List[EntriesEntryContentItemItem], +EntriesBaseEntryContentItem = typing.Union[ + typing.List[EntriesBaseEntryContentItemItem], ToolsTool, ToolsChosenToolCall, str, diff --git a/sdks/python/julep/api/types/entries_entry_content_item_item.py b/sdks/python/julep/api/types/entries_base_entry_content_item_item.py similarity index 90% rename from sdks/python/julep/api/types/entries_entry_content_item_item.py rename to sdks/python/julep/api/types/entries_base_entry_content_item_item.py index 609757926..369a71bc8 100644 --- a/sdks/python/julep/api/types/entries_entry_content_item_item.py +++ b/sdks/python/julep/api/types/entries_base_entry_content_item_item.py @@ -10,7 +10,7 @@ from .entries_image_url import EntriesImageUrl -class EntriesEntryContentItemItem_Text(pydantic_v1.BaseModel): +class EntriesBaseEntryContentItemItem_Text(pydantic_v1.BaseModel): text: str type: typing.Literal["text"] = "text" @@ -46,7 +46,7 @@ class Config: json_encoders = {dt.datetime: serialize_datetime} -class EntriesEntryContentItemItem_ImageUrl(pydantic_v1.BaseModel): +class EntriesBaseEntryContentItemItem_ImageUrl(pydantic_v1.BaseModel): image_url: EntriesImageUrl type: typing.Literal["image_url"] = "image_url" @@ -82,6 +82,6 @@ class Config: json_encoders = {dt.datetime: serialize_datetime} -EntriesEntryContentItemItem = typing.Union[ - EntriesEntryContentItemItem_Text, EntriesEntryContentItemItem_ImageUrl +EntriesBaseEntryContentItemItem = typing.Union[ + EntriesBaseEntryContentItemItem_Text, EntriesBaseEntryContentItemItem_ImageUrl ] diff --git a/sdks/python/julep/api/types/entries_entry_source.py b/sdks/python/julep/api/types/entries_base_entry_source.py similarity index 84% rename from sdks/python/julep/api/types/entries_entry_source.py rename to sdks/python/julep/api/types/entries_base_entry_source.py index 134e34fd7..94a81cae9 100644 --- a/sdks/python/julep/api/types/entries_entry_source.py +++ b/sdks/python/julep/api/types/entries_base_entry_source.py @@ -2,7 +2,7 @@ import typing -EntriesEntrySource = typing.Union[ +EntriesBaseEntrySource = typing.Union[ typing.Literal[ "api_request", "api_response", "tool_response", "internal", "summarizer", "meta" ], diff --git a/sdks/python/julep/api/types/entries_entry.py b/sdks/python/julep/api/types/entries_entry.py index 2bc5f5c3f..c90ed873e 100644 --- a/sdks/python/julep/api/types/entries_entry.py +++ b/sdks/python/julep/api/types/entries_entry.py @@ -6,21 +6,10 @@ from ..core.datetime_utils import serialize_datetime from ..core.pydantic_utilities import deep_union_pydantic_dicts, pydantic_v1 from .common_uuid import CommonUuid -from .entries_chat_ml_role import EntriesChatMlRole -from .entries_entry_content import EntriesEntryContent -from .entries_entry_source import EntriesEntrySource +from .entries_base_entry import EntriesBaseEntry -class EntriesEntry(pydantic_v1.BaseModel): - role: EntriesChatMlRole - name: typing.Optional[str] = None - content: EntriesEntryContent - source: EntriesEntrySource - timestamp: float = pydantic_v1.Field() - """ - This is the time that this event refers to. - """ - +class EntriesEntry(EntriesBaseEntry): created_at: dt.datetime = pydantic_v1.Field() """ When this resource was created as UTC date-time @@ -56,5 +45,7 @@ def dict(self, **kwargs: typing.Any) -> typing.Dict[str, typing.Any]: class Config: frozen = True smart_union = True + allow_population_by_field_name = True + populate_by_name = True extra = pydantic_v1.Extra.allow json_encoders = {dt.datetime: serialize_datetime} diff --git a/sdks/python/julep/api/types/entries_history.py b/sdks/python/julep/api/types/entries_history.py index d3d124ffe..78c56e424 100644 --- a/sdks/python/julep/api/types/entries_history.py +++ b/sdks/python/julep/api/types/entries_history.py @@ -6,12 +6,12 @@ from ..core.datetime_utils import serialize_datetime from ..core.pydantic_utilities import deep_union_pydantic_dicts, pydantic_v1 from .common_uuid import CommonUuid -from .entries_entry import EntriesEntry +from .entries_base_entry import EntriesBaseEntry from .entries_relation import EntriesRelation class EntriesHistory(pydantic_v1.BaseModel): - entries: typing.List[EntriesEntry] + entries: typing.List[EntriesBaseEntry] relations: typing.List[EntriesRelation] session_id: CommonUuid created_at: dt.datetime = pydantic_v1.Field() diff --git a/sdks/python/julep/api/types/execution_transitions_route_list_request_sort_by.py b/sdks/python/julep/api/types/execution_transitions_route_list_request_sort_by.py index ee56f1700..8c3e0d5e0 100644 --- a/sdks/python/julep/api/types/execution_transitions_route_list_request_sort_by.py +++ b/sdks/python/julep/api/types/execution_transitions_route_list_request_sort_by.py @@ -3,5 +3,5 @@ import typing ExecutionTransitionsRouteListRequestSortBy = typing.Union[ - typing.Literal["created_at", "updated_at", "deleted_at"], typing.Any + typing.Literal["created_at", "updated_at"], typing.Any ] diff --git a/sdks/python/julep/api/types/sessions_route_list_request_sort_by.py b/sdks/python/julep/api/types/sessions_route_list_request_sort_by.py index add82a7be..476b15b3d 100644 --- a/sdks/python/julep/api/types/sessions_route_list_request_sort_by.py +++ b/sdks/python/julep/api/types/sessions_route_list_request_sort_by.py @@ -3,5 +3,5 @@ import typing SessionsRouteListRequestSortBy = typing.Union[ - typing.Literal["created_at", "updated_at", "deleted_at"], typing.Any + typing.Literal["created_at", "updated_at"], typing.Any ] diff --git a/sdks/python/julep/api/types/task_executions_route_list_request_sort_by.py b/sdks/python/julep/api/types/task_executions_route_list_request_sort_by.py index 32907e8bc..f6171336a 100644 --- a/sdks/python/julep/api/types/task_executions_route_list_request_sort_by.py +++ b/sdks/python/julep/api/types/task_executions_route_list_request_sort_by.py @@ -3,5 +3,5 @@ import typing TaskExecutionsRouteListRequestSortBy = typing.Union[ - typing.Literal["created_at", "updated_at", "deleted_at"], typing.Any + typing.Literal["created_at", "updated_at"], typing.Any ] diff --git a/sdks/python/julep/api/types/tasks_route_list_request_sort_by.py b/sdks/python/julep/api/types/tasks_route_list_request_sort_by.py index 4c6430a35..4c7b3b4ae 100644 --- a/sdks/python/julep/api/types/tasks_route_list_request_sort_by.py +++ b/sdks/python/julep/api/types/tasks_route_list_request_sort_by.py @@ -3,5 +3,5 @@ import typing TasksRouteListRequestSortBy = typing.Union[ - typing.Literal["created_at", "updated_at", "deleted_at"], typing.Any + typing.Literal["created_at", "updated_at"], typing.Any ] diff --git a/sdks/python/julep/api/types/user_docs_route_list_request_sort_by.py b/sdks/python/julep/api/types/user_docs_route_list_request_sort_by.py index 862c6e9e7..f2078d2a1 100644 --- a/sdks/python/julep/api/types/user_docs_route_list_request_sort_by.py +++ b/sdks/python/julep/api/types/user_docs_route_list_request_sort_by.py @@ -3,5 +3,5 @@ import typing UserDocsRouteListRequestSortBy = typing.Union[ - typing.Literal["created_at", "updated_at", "deleted_at"], typing.Any + typing.Literal["created_at", "updated_at"], typing.Any ] diff --git a/sdks/python/julep/api/types/user_docs_search_route_search_request_sort_by.py b/sdks/python/julep/api/types/user_docs_search_route_search_request_sort_by.py index 4a604b920..8cf9538a6 100644 --- a/sdks/python/julep/api/types/user_docs_search_route_search_request_sort_by.py +++ b/sdks/python/julep/api/types/user_docs_search_route_search_request_sort_by.py @@ -3,5 +3,5 @@ import typing UserDocsSearchRouteSearchRequestSortBy = typing.Union[ - typing.Literal["created_at", "updated_at", "deleted_at"], typing.Any + typing.Literal["created_at", "updated_at"], typing.Any ] diff --git a/sdks/python/julep/api/types/users_route_list_request_sort_by.py b/sdks/python/julep/api/types/users_route_list_request_sort_by.py index 2e45a9857..b4c94602b 100644 --- a/sdks/python/julep/api/types/users_route_list_request_sort_by.py +++ b/sdks/python/julep/api/types/users_route_list_request_sort_by.py @@ -3,5 +3,5 @@ import typing UsersRouteListRequestSortBy = typing.Union[ - typing.Literal["created_at", "updated_at", "deleted_at"], typing.Any + typing.Literal["created_at", "updated_at"], typing.Any ] diff --git a/sdks/python/poetry.lock b/sdks/python/poetry.lock index 2e75c4e07..1e942872f 100644 --- a/sdks/python/poetry.lock +++ b/sdks/python/poetry.lock @@ -2755,13 +2755,13 @@ win32 = ["pywin32"] [[package]] name = "setuptools" -version = "71.1.0" +version = "72.1.0" description = "Easily download, build, install, upgrade, and uninstall Python packages" optional = false python-versions = ">=3.8" files = [ - {file = "setuptools-71.1.0-py3-none-any.whl", hash = "sha256:33874fdc59b3188304b2e7c80d9029097ea31627180896fb549c578ceb8a0855"}, - {file = "setuptools-71.1.0.tar.gz", hash = "sha256:032d42ee9fb536e33087fb66cac5f840eb9391ed05637b3f2a76a7c8fb477936"}, + {file = "setuptools-72.1.0-py3-none-any.whl", hash = "sha256:5a03e1860cf56bb6ef48ce186b0e557fdba433237481a9a625176c2831be15d1"}, + {file = "setuptools-72.1.0.tar.gz", hash = "sha256:8d243eff56d095e5817f796ede6ae32941278f542e0f941867cc05ae52b162ec"}, ] [package.extras] diff --git a/sdks/ts/src/api/index.ts b/sdks/ts/src/api/index.ts index cdd3a21f6..9d3cf081b 100644 --- a/sdks/ts/src/api/index.ts +++ b/sdks/ts/src/api/index.ts @@ -57,6 +57,7 @@ export type { Docs_EmbedQueryResponse } from "./models/Docs_EmbedQueryResponse"; export type { Docs_HybridDocSearchRequest } from "./models/Docs_HybridDocSearchRequest"; export type { Docs_TextOnlyDocSearchRequest } from "./models/Docs_TextOnlyDocSearchRequest"; export type { Docs_VectorDocSearchRequest } from "./models/Docs_VectorDocSearchRequest"; +export type { Entries_BaseEntry } from "./models/Entries_BaseEntry"; export type { Entries_ChatMLImageContentPart } from "./models/Entries_ChatMLImageContentPart"; export type { Entries_ChatMLMessage } from "./models/Entries_ChatMLMessage"; export type { Entries_ChatMLRole } from "./models/Entries_ChatMLRole"; @@ -166,6 +167,7 @@ export { $Docs_EmbedQueryResponse } from "./schemas/$Docs_EmbedQueryResponse"; export { $Docs_HybridDocSearchRequest } from "./schemas/$Docs_HybridDocSearchRequest"; export { $Docs_TextOnlyDocSearchRequest } from "./schemas/$Docs_TextOnlyDocSearchRequest"; export { $Docs_VectorDocSearchRequest } from "./schemas/$Docs_VectorDocSearchRequest"; +export { $Entries_BaseEntry } from "./schemas/$Entries_BaseEntry"; export { $Entries_ChatMLImageContentPart } from "./schemas/$Entries_ChatMLImageContentPart"; export { $Entries_ChatMLMessage } from "./schemas/$Entries_ChatMLMessage"; export { $Entries_ChatMLRole } from "./schemas/$Entries_ChatMLRole"; diff --git a/sdks/ts/src/api/models/Common_PaginationOptions_sort_by.ts b/sdks/ts/src/api/models/Common_PaginationOptions_sort_by.ts index 407284e12..806753d4f 100644 --- a/sdks/ts/src/api/models/Common_PaginationOptions_sort_by.ts +++ b/sdks/ts/src/api/models/Common_PaginationOptions_sort_by.ts @@ -5,7 +5,4 @@ /** * Sort by a field */ -export type Common_PaginationOptions_sort_by = - | "created_at" - | "updated_at" - | "deleted_at"; +export type Common_PaginationOptions_sort_by = "created_at" | "updated_at"; diff --git a/sdks/ts/src/api/models/Entries_BaseEntry.ts b/sdks/ts/src/api/models/Entries_BaseEntry.ts new file mode 100644 index 000000000..e77df13c3 --- /dev/null +++ b/sdks/ts/src/api/models/Entries_BaseEntry.ts @@ -0,0 +1,26 @@ +/* generated using openapi-typescript-codegen -- do no edit */ +/* istanbul ignore file */ +/* tslint:disable */ +/* eslint-disable */ +import type { Entries_ChatMLRole } from "./Entries_ChatMLRole"; +import type { Tools_ChosenToolCall } from "./Tools_ChosenToolCall"; +import type { Tools_Tool } from "./Tools_Tool"; +import type { Tools_ToolResponse } from "./Tools_ToolResponse"; +export type Entries_BaseEntry = { + role: Entries_ChatMLRole; + name: string | null; + content: Tools_Tool | Tools_ChosenToolCall | string | Tools_ToolResponse; + source: + | "api_request" + | "api_response" + | "tool_response" + | "internal" + | "summarizer" + | "meta"; + tokenizer?: string; + token_count?: number; + /** + * This is the time that this event refers to. + */ + timestamp: number; +}; diff --git a/sdks/ts/src/api/models/Entries_Entry.ts b/sdks/ts/src/api/models/Entries_Entry.ts index f3e40ce03..ce518e93a 100644 --- a/sdks/ts/src/api/models/Entries_Entry.ts +++ b/sdks/ts/src/api/models/Entries_Entry.ts @@ -3,25 +3,8 @@ /* tslint:disable */ /* eslint-disable */ import type { Common_uuid } from "./Common_uuid"; -import type { Entries_ChatMLRole } from "./Entries_ChatMLRole"; -import type { Tools_ChosenToolCall } from "./Tools_ChosenToolCall"; -import type { Tools_Tool } from "./Tools_Tool"; -import type { Tools_ToolResponse } from "./Tools_ToolResponse"; -export type Entries_Entry = { - role: Entries_ChatMLRole; - name: string | null; - content: Tools_Tool | Tools_ChosenToolCall | string | Tools_ToolResponse; - source: - | "api_request" - | "api_response" - | "tool_response" - | "internal" - | "summarizer" - | "meta"; - /** - * This is the time that this event refers to. - */ - timestamp: number; +import type { Entries_BaseEntry } from "./Entries_BaseEntry"; +export type Entries_Entry = Entries_BaseEntry & { /** * When this resource was created as UTC date-time */ diff --git a/sdks/ts/src/api/models/Entries_History.ts b/sdks/ts/src/api/models/Entries_History.ts index 784972435..6550afff3 100644 --- a/sdks/ts/src/api/models/Entries_History.ts +++ b/sdks/ts/src/api/models/Entries_History.ts @@ -3,10 +3,10 @@ /* tslint:disable */ /* eslint-disable */ import type { Common_uuid } from "./Common_uuid"; -import type { Entries_Entry } from "./Entries_Entry"; +import type { Entries_BaseEntry } from "./Entries_BaseEntry"; import type { Entries_Relation } from "./Entries_Relation"; export type Entries_History = { - entries: Array; + entries: Array; relations: Array; readonly session_id: Common_uuid; /** diff --git a/sdks/ts/src/api/schemas/$Entries_BaseEntry.ts b/sdks/ts/src/api/schemas/$Entries_BaseEntry.ts new file mode 100644 index 000000000..6aad5206a --- /dev/null +++ b/sdks/ts/src/api/schemas/$Entries_BaseEntry.ts @@ -0,0 +1,51 @@ +/* generated using openapi-typescript-codegen -- do no edit */ +/* istanbul ignore file */ +/* tslint:disable */ +/* eslint-disable */ +export const $Entries_BaseEntry = { + properties: { + role: { + type: "Entries_ChatMLRole", + isRequired: true, + }, + name: { + type: "string", + isRequired: true, + isNullable: true, + }, + content: { + type: "any-of", + contains: [ + { + type: "Tools_Tool", + }, + { + type: "Tools_ChosenToolCall", + }, + { + type: "string", + }, + { + type: "Tools_ToolResponse", + }, + ], + isRequired: true, + }, + source: { + type: "Enum", + isRequired: true, + }, + tokenizer: { + type: "string", + }, + token_count: { + type: "number", + format: "uint16", + }, + timestamp: { + type: "number", + description: `This is the time that this event refers to.`, + isRequired: true, + }, + }, +} as const; diff --git a/sdks/ts/src/api/schemas/$Entries_Entry.ts b/sdks/ts/src/api/schemas/$Entries_Entry.ts index 843a1dea1..b4b6adec5 100644 --- a/sdks/ts/src/api/schemas/$Entries_Entry.ts +++ b/sdks/ts/src/api/schemas/$Entries_Entry.ts @@ -3,59 +3,31 @@ /* tslint:disable */ /* eslint-disable */ export const $Entries_Entry = { - properties: { - role: { - type: "Entries_ChatMLRole", - isRequired: true, + type: "all-of", + contains: [ + { + type: "Entries_BaseEntry", }, - name: { - type: "string", - isRequired: true, - isNullable: true, - }, - content: { - type: "any-of", - contains: [ - { - type: "Tools_Tool", - }, - { - type: "Tools_ChosenToolCall", - }, - { + { + properties: { + created_at: { type: "string", + description: `When this resource was created as UTC date-time`, + isReadOnly: true, + isRequired: true, + format: "date-time", }, - { - type: "Tools_ToolResponse", - }, - ], - isRequired: true, - }, - source: { - type: "Enum", - isRequired: true, - }, - timestamp: { - type: "number", - description: `This is the time that this event refers to.`, - isRequired: true, - }, - created_at: { - type: "string", - description: `When this resource was created as UTC date-time`, - isReadOnly: true, - isRequired: true, - format: "date-time", - }, - id: { - type: "all-of", - contains: [ - { - type: "Common_uuid", + id: { + type: "all-of", + contains: [ + { + type: "Common_uuid", + }, + ], + isReadOnly: true, + isRequired: true, }, - ], - isReadOnly: true, - isRequired: true, + }, }, - }, + ], } as const; diff --git a/sdks/ts/src/api/schemas/$Entries_History.ts b/sdks/ts/src/api/schemas/$Entries_History.ts index c75c70678..208d12b46 100644 --- a/sdks/ts/src/api/schemas/$Entries_History.ts +++ b/sdks/ts/src/api/schemas/$Entries_History.ts @@ -7,7 +7,7 @@ export const $Entries_History = { entries: { type: "array", contains: { - type: "Entries_Entry", + type: "Entries_BaseEntry", }, isRequired: true, }, diff --git a/sdks/ts/src/api/services/DefaultService.ts b/sdks/ts/src/api/services/DefaultService.ts index 0c8e756e9..2c8b9be9a 100644 --- a/sdks/ts/src/api/services/DefaultService.ts +++ b/sdks/ts/src/api/services/DefaultService.ts @@ -77,7 +77,7 @@ export class DefaultService { /** * Sort by a field */ - sortBy?: "created_at" | "updated_at" | "deleted_at"; + sortBy?: "created_at" | "updated_at"; /** * Sort direction */ @@ -260,7 +260,7 @@ export class DefaultService { /** * Sort by a field */ - sortBy?: "created_at" | "updated_at" | "deleted_at"; + sortBy?: "created_at" | "updated_at"; /** * Sort direction */ @@ -322,7 +322,7 @@ export class DefaultService { /** * Sort by a field */ - sortBy?: "created_at" | "updated_at" | "deleted_at"; + sortBy?: "created_at" | "updated_at"; /** * Sort direction */ @@ -379,7 +379,7 @@ export class DefaultService { /** * Sort by a field */ - sortBy?: "created_at" | "updated_at" | "deleted_at"; + sortBy?: "created_at" | "updated_at"; /** * Sort direction */ @@ -548,7 +548,7 @@ export class DefaultService { /** * Sort by a field */ - sortBy?: "created_at" | "updated_at" | "deleted_at"; + sortBy?: "created_at" | "updated_at"; /** * Sort direction */ @@ -827,7 +827,7 @@ export class DefaultService { /** * Sort by a field */ - sortBy?: "created_at" | "updated_at" | "deleted_at"; + sortBy?: "created_at" | "updated_at"; /** * Sort direction */ @@ -900,7 +900,7 @@ export class DefaultService { /** * Sort by a field */ - sortBy?: "created_at" | "updated_at" | "deleted_at"; + sortBy?: "created_at" | "updated_at"; /** * Sort direction */ @@ -1400,7 +1400,7 @@ export class DefaultService { /** * Sort by a field */ - sortBy?: "created_at" | "updated_at" | "deleted_at"; + sortBy?: "created_at" | "updated_at"; /** * Sort direction */ @@ -1509,7 +1509,7 @@ export class DefaultService { /** * Sort by a field */ - sortBy?: "created_at" | "updated_at" | "deleted_at"; + sortBy?: "created_at" | "updated_at"; /** * Sort direction */ @@ -1692,7 +1692,7 @@ export class DefaultService { /** * Sort by a field */ - sortBy?: "created_at" | "updated_at" | "deleted_at"; + sortBy?: "created_at" | "updated_at"; /** * Sort direction */ @@ -1754,7 +1754,7 @@ export class DefaultService { /** * Sort by a field */ - sortBy?: "created_at" | "updated_at" | "deleted_at"; + sortBy?: "created_at" | "updated_at"; /** * Sort direction */ diff --git a/typespec/common/scalars.tsp b/typespec/common/scalars.tsp index b1c710ce2..177f2ccd6 100644 --- a/typespec/common/scalars.tsp +++ b/typespec/common/scalars.tsp @@ -29,7 +29,7 @@ scalar limit extends uint16; scalar offset extends uint32; /** Sort by a field */ -alias sortBy = "created_at" | "updated_at" | "deleted_at"; +alias sortBy = "created_at" | "updated_at"; /** Sort direction */ alias sortDirection = "asc" | "desc"; diff --git a/typespec/entries/models.tsp b/typespec/entries/models.tsp index 314bb8e71..1a614d449 100644 --- a/typespec/entries/models.tsp +++ b/typespec/entries/models.tsp @@ -85,16 +85,21 @@ model InputChatMLMessage { alias EntryContent = ChatMLContentPart[] | Tool | ChosenToolCall | string | ToolResponse; -model Entry { +model BaseEntry { role: ChatMLRole; name: string | null = null; content: EntryContent | EntryContent[]; source: entrySource; + tokenizer?: string; + token_count?: uint16; + /** This is the time that this event refers to. */ @minValue(0) timestamp: float; - +} + +model Entry extends BaseEntry { /** This is the time it was added to the database */ ...HasCreatedAt; ...HasId; @@ -107,7 +112,7 @@ model Relation { } model History { - entries: Entry[]; + entries: BaseEntry[]; relations: Relation[]; session_id: Session.id;