Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/_ravnar/api/agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from collections.abc import AsyncIterator, Callable
from typing import TYPE_CHECKING, Annotated, Any

import ag_ui.core
import fastsse
from fastapi import Depends, Path

Expand All @@ -28,7 +27,7 @@ async def list_agents(
async def create_stateless_run(
*,
agent_id: Annotated[str, Path(alias="agentId")],
run_agent_input: ag_ui.core.RunAgentInput,
run_agent_input: schema.AugmentedRunAgentInput,
user: User = Depends(authorized_user_with("agents:read")), # noqa: B008
) -> fastsse.Response:
return await agent_handler.run(agent_id, run_agent_input, user=user)
Expand Down
15 changes: 7 additions & 8 deletions src/_ravnar/api/threads.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,20 +119,19 @@ async def create_run(
user_id=user.id, thread_id=thread_id, run_id=data.parent_run_id
)

augmented_messages_ta = pydantic.TypeAdapter(list[schema.AugmentedMessage])
augmented_messages = augmented_messages_ta.validate_python(parent_messages, from_attributes=True)
augmented_messages.extend(data.messages)
messages = [
*pydantic.TypeAdapter(list[schema.AugmentedMessage]).validate_python(parent_messages, from_attributes=True),
*data.messages,
]

await hydrate_files(augmented_messages, user=user, file_handler=file_handler)
await hydrate_files(messages, user=user, file_handler=file_handler)

run_agent_input = ag_ui.core.RunAgentInput(
run_agent_input = schema.AugmentedRunAgentInput(
thread_id=thread_id,
run_id=data.id,
parent_run_id=parent_run.id if parent_run is not None else None,
state=parent_run.state if parent_run is not None else None,
messages=pydantic.TypeAdapter(list[ag_ui.core.Message]).validate_python(
augmented_messages_ta.dump_python(augmented_messages)
),
messages=messages,
tools=data.tools,
context=data.context,
forwarded_props=data.forwarded_props,
Expand Down
2 changes: 1 addition & 1 deletion src/_ravnar/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ def _sse_encoder(self, data: fastsse.Data) -> bytes:
async def run(
self,
agent_id: str,
run_agent_input: ag_ui.core.RunAgentInput,
run_agent_input: schema.AugmentedRunAgentInput,
*,
user: User,
callback: Callable[[EventProcessor], Awaitable[None]] | None = None,
Expand Down
5 changes: 3 additions & 2 deletions src/_ravnar/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import structlog
from opentelemetry import trace

from _ravnar import schema
from _ravnar.file_storage import WrappedMetadata
from _ravnar.observability import LazyValue

Expand Down Expand Up @@ -74,7 +75,7 @@ class ReasoningData:


class EventProcessor:
def __init__(self, *, run_agent_input: ag_ui.core.RunAgentInput):
def __init__(self, *, run_agent_input: schema.AugmentedRunAgentInput):
self._run_agent_input = run_agent_input

self._state = run_agent_input.state
Expand All @@ -94,7 +95,7 @@ def __init__(self, *, run_agent_input: ag_ui.core.RunAgentInput):
parent_run_id=run_agent_input.parent_run_id,
)

def _convert_input_messages(self, messages: list[ag_ui.core.Message]) -> dict[str, orm.Message]:
def _convert_input_messages(self, messages: list[schema.AugmentedMessage]) -> dict[str, orm.Message]:
message_uids = {m.id: uuid.uuid4() for m in messages}

tool_calls = {
Expand Down
2 changes: 2 additions & 0 deletions src/_ravnar/schema/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"AugmentedDeveloperMessage",
"AugmentedMessage",
"AugmentedReasoningMessage",
"AugmentedRunAgentInput",
"AugmentedSystemMessage",
"AugmentedToolMessage",
"AugmentedUserMessage",
Expand Down Expand Up @@ -34,6 +35,7 @@
AugmentedDeveloperMessage,
AugmentedMessage,
AugmentedReasoningMessage,
AugmentedRunAgentInput,
AugmentedSystemMessage,
AugmentedToolMessage,
AugmentedUserMessage,
Expand Down
7 changes: 7 additions & 0 deletions src/_ravnar/schema/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"AugmentedDeveloperMessage",
"AugmentedMessage",
"AugmentedReasoningMessage",
"AugmentedRunAgentInput",
"AugmentedSystemMessage",
"AugmentedToolMessage",
"AugmentedUserMessage",
Expand All @@ -16,6 +17,7 @@
"DeleteThreadsData",
"Event",
"QuickPrompt",
"RegisterAgentData",
"RenameThreadData",
"Run",
"Thread",
Expand Down Expand Up @@ -176,6 +178,11 @@ class AugmentedReasoningMessage(AugmentedMessageMixin, ag_ui.core.ReasoningMessa
Field(discriminator="role"),
]


class AugmentedRunAgentInput(ag_ui.core.RunAgentInput):
messages: list[AugmentedMessage] # type: ignore[assignment]


Event = Annotated[ag_ui.core.Event, Field(title="Event")]


Expand Down
16 changes: 16 additions & 0 deletions tests/api/test_agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,3 +281,19 @@ def test_register_agent_with_denied_env_var(self, client):
)
assert response.status_code == status.HTTP_400_BAD_REQUEST
assert response.json()["detail"] == "Invalid configuration"


class TestStatelessRun:
def test_smoke(self, app_client):
app_client.post(
f"/api/agents/{app_client.any_agent_id}/run",
json={
"threadId": "thread-id",
"runId": "run-id",
"state": {},
"messages": [{"id": "message-id", "role": "user", "content": "hello"}],
"tools": [],
"context": [],
"forwardedProps": {},
},
).raise_for_status()
Loading