Skip to content
Open
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
2 changes: 1 addition & 1 deletion packages/narada-core/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "narada-core"
version = "0.0.21"
version = "0.0.22"
description = "Code shared by the `narada` and `narada-pyodide` packages."
license = "Apache-2.0"
readme = "README.md"
Expand Down
6 changes: 6 additions & 0 deletions packages/narada-core/src/narada_core/tracing/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
NonNegativeInt,
TypeAdapter,
ValidationError,
field_validator,
model_validator,
)

Expand Down Expand Up @@ -244,6 +245,11 @@ class PythonSubAgentCallEvent(BaseModel):
error_message: str | None = None
action_trace: ActionTrace | None = None

@field_validator("agent_type", mode="before")
@classmethod
def _coerce_agent_type(cls, agent_type: Any) -> str:
return str(agent_type)

@model_validator(mode="after")
def _check_ts_ordering(self) -> PythonSubAgentCallEvent:
if self.ts_end < self.ts_start:
Expand Down
4 changes: 2 additions & 2 deletions packages/narada-pyodide/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@

[project]
name = "narada-pyodide"
version = "0.0.49"
version = "0.0.50"
description = "Pyodide-compatible Python client SDK for Narada"
license = "Apache-2.0"
readme = "README.md"
authors = [{ name = "Narada", email = "support@narada.ai" }]
requires-python = ">=3.12"
dependencies = [
"narada-core==0.0.21",
"narada-core==0.0.22",
# Must be a supported version in https://pyodide.org/en/stable/usage/packages-in-pyodide.html
"packaging==24.2",
]
Expand Down
14 changes: 13 additions & 1 deletion packages/narada-pyodide/src/narada/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,18 @@ async def _narada_get_id_token() -> str: ...

_ResponseModel = TypeVar("_ResponseModel", bound=BaseModel)

_TRACE_AGENT_TYPE_BY_AGENT = {
Agent.PRODUCTIVITY: "generalist",
Agent.OPERATOR: "operator",
Agent.CORE_AGENT: "coreAgent",
}


def _agent_type_for_trace(agent: Agent | str) -> str:
if isinstance(agent, Agent):
return _TRACE_AGENT_TYPE_BY_AGENT[agent]
return str(agent)


def _normalize_narada_env(env: str | None) -> Literal["prod", "dev", None]:
if env is not None and env not in ("prod", "dev"):
Expand Down Expand Up @@ -352,7 +364,7 @@ async def dispatch_request(
# exit (successful return, timeout, or non-timeout failure) produces a
# ``subAgentCall`` trace event with matching status. See `_trace.py`.
trace_start_ms = _trace.now_ms()
agent_type_str = agent.value if isinstance(agent, Agent) else str(agent)
agent_type_str = _agent_type_for_trace(agent)

deadline = time.monotonic() + timeout

Expand Down
111 changes: 110 additions & 1 deletion packages/narada-pyodide/tests/test_cloud_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import pytest
from packaging.version import InvalidVersion

PROJECT_ROOT = Path("/Users/zizheng/Projects/narada-python-sdk")
PROJECT_ROOT = Path(__file__).resolve().parents[3]
PYODIDE_SRC = PROJECT_ROOT / "packages" / "narada-pyodide" / "src"
CORE_SRC = PROJECT_ROOT / "packages" / "narada-core" / "src"

Expand Down Expand Up @@ -277,6 +277,115 @@ async def test_cloud_browser_window_dispatch_request_omits_parent_run_ids(
assert "parentRunIds" not in payload


@pytest.mark.asyncio
async def test_cloud_browser_window_trace_uses_string_agent_type_for_builtin_agent(
monkeypatch: pytest.MonkeyPatch,
) -> None:
pyfetch = AsyncMock(
side_effect=[
_FakeResponse(json_data={"requestId": "req-123"}),
_FakeResponse(json_data={"status": "success", "response": None}),
]
)
_, _, window_module = _import_pyodide_narada(monkeypatch, pyfetch=pyfetch)
emitted_events: list[dict[str, object]] = []
monkeypatch.setattr(
window_module._trace,
"_narada_emit_trace_event",
lambda event_json: emitted_events.append(json.loads(event_json)),
raising=False,
)

window = window_module.CloudBrowserWindow(
browser_window_id="browser-window-123",
session_id="session-123",
api_key="test-api-key",
)
await window.dispatch_request(prompt="hello from cloud browser")

trace_event = emitted_events[0]
assert trace_event["kind"] == "subAgentCall"
assert trace_event["agent_type"] == "operator"


def test_parse_action_trace_accepts_historical_numeric_sub_agent_type(
monkeypatch: pytest.MonkeyPatch,
) -> None:
_, _, window_module = _import_pyodide_narada(monkeypatch, pyfetch=AsyncMock())

trace = window_module.parse_action_trace(
[
{
"step_type": "pythonAgentRun",
"url": "",
"status": "success",
"duration_ms": 10,
"events": [
{
"kind": "subAgentCall",
"ts_start": 1,
"ts_end": 2,
"agent_type": 2,
"prompt": "legacy operator call",
"status": "success",
}
],
}
]
)

assert trace[0].events[0].agent_type == "2"


def test_parse_action_trace_accepts_historical_numeric_nested_sub_agent_type(
monkeypatch: pytest.MonkeyPatch,
) -> None:
_, _, window_module = _import_pyodide_narada(monkeypatch, pyfetch=AsyncMock())

trace = window_module.parse_action_trace(
[
{
"step_type": "pythonAgentRun",
"url": "",
"status": "success",
"duration_ms": 10,
"events": [
{
"kind": "subAgentCall",
"ts_start": 1,
"ts_end": 2,
"agent_type": "/$USER/childWorkflow",
"prompt": "child workflow call",
"status": "success",
"action_trace": [
{
"step_type": "pythonAgentRun",
"url": "",
"status": "success",
"duration_ms": 5,
"events": [
{
"kind": "subAgentCall",
"ts_start": 3,
"ts_end": 4,
"agent_type": 2,
"prompt": "nested operator call",
"status": "success",
}
],
}
],
}
],
}
]
)

nested_trace = trace[0].events[0].action_trace
assert nested_trace is not None
assert nested_trace[0].events[0].agent_type == "2"


@pytest.mark.asyncio
async def test_cloud_browser_window_dispatch_request_preserves_current_file_variable_shape(
monkeypatch: pytest.MonkeyPatch,
Expand Down
4 changes: 2 additions & 2 deletions packages/narada/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
[project]
name = "narada"
version = "0.1.51"
version = "0.1.52"
description = "Python client SDK for Narada"
license = "Apache-2.0"
readme = "README.md"
authors = [{ name = "Narada", email = "support@narada.ai" }]
requires-python = ">=3.12"
dependencies = [
"narada-core==0.0.21",
"narada-core==0.0.22",
"aiohttp>=3.12.13",
"playwright>=1.53.0",
"rich>=14.0.0",
Expand Down
6 changes: 3 additions & 3 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading