Skip to content

sensai

SensAI API router for the Dojo dashboard.

HistoryEntry

Bases: BaseModel

A single prior turn in the conversation.

ChatRequest

Bases: BaseModel

Request body for the chat endpoint.

message instance-attribute

Python
message: str

The user's message.

message_history class-attribute instance-attribute

Python
message_history: list[HistoryEntry] = Field(
    default_factory=list
)

Prior conversation turns, oldest first, used to maintain context across requests.

all_columns class-attribute instance-attribute

Python
all_columns: list[str] = Field(default_factory=list)

All signal column names currently loaded in the trial viewer.

rotatable_vectors class-attribute instance-attribute

Python
rotatable_vectors: list[str] = Field(default_factory=list)

Signal columns that can be rotated using a quaternion.

available_quats class-attribute instance-attribute

Python
available_quats: list[str] = Field(default_factory=list)

Quaternion column names available for rotating rotatable_vectors.

current_plot_config_json class-attribute instance-attribute

Python
current_plot_config_json: str | None = None

JSON-serialized PlotConfig currently active in the trial viewer, or None.

versus class-attribute instance-attribute

Python
versus: VersusState | None = None

Versus mode as the trial viewer has it, or None when the message did not come from a trial viewer.

get_config async

Python
get_config() -> SensAISettings

Returns the current SensAI settings.

Source code in src/mujoco_mojo/utils/layers/dojo/routers/sensai.py
Python
@router.get("/config")
async def get_config() -> SensAISettings:
    """Returns the current SensAI settings."""
    return MujocoMojoSettings().dojo.sensai

post_config async

Python
post_config(body: SensAISettings) -> SensAISettings

Persists updated SensAI settings to disk.

Source code in src/mujoco_mojo/utils/layers/dojo/routers/sensai.py
Python
@router.post("/config")
async def post_config(body: SensAISettings) -> SensAISettings:
    """Persists updated SensAI settings to disk."""
    settings = MujocoMojoSettings()
    updated = settings.model_copy(
        update={"dojo": settings.dojo.model_copy(update={"sensai": body})}
    )
    updated.save()
    return updated.dojo.sensai

post_chat async

Python
post_chat(body: ChatRequest) -> EventSourceResponse

Runs the SensAI agents and streams the response as SSE.

Source code in src/mujoco_mojo/utils/layers/dojo/routers/sensai.py
Python
@router.post("/chat")
async def post_chat(body: ChatRequest) -> EventSourceResponse:
    """Runs the SensAI agents and streams the response as SSE."""
    settings = MujocoMojoSettings()

    if not settings.dojo.sensai.enabled:
        raise HTTPException(
            status_code=status.HTTP_403_FORBIDDEN,
            detail="SensAI is not enabled. Set dojo.sensai.enabled = true in settings.",
        )

    current_plot_config: PlotConfig | None = None
    if body.current_plot_config_json:
        try:
            current_plot_config = PlotConfig.model_validate_json(
                body.current_plot_config_json
            )
        except Exception:
            logger.warning(
                "Failed to parse current_plot_config_json; proceeding without it."
            )
    column_manifest: ColumnManifest = {
        "all": body.all_columns,
        "rotatable_vectors": body.rotatable_vectors,
        "available_quats": body.available_quats,
        "column_metadata": body.column_metadata,
    }

    deps = SensAIDeps(
        job_status=shared.CURRENT_JOB,
        column_manifest=column_manifest,
        current_plot_config=current_plot_config,
        versus=body.versus,
        max_requests=settings.dojo.sensai.max_requests,
        max_versus_trials=settings.dojo.sensai.max_versus_trials,
    )

    model = build_fallback_model()

    message_history = _to_model_messages(body.message_history)
    context_block = _build_context_block(deps)
    augmented_message = f"{context_block}\n\n{body.message}"

    async def event_stream() -> AsyncIterator[dict[str, Any]]:
        # undo path: restore previous plot config via client-side history
        if _is_undo_intent(body.message):
            yield {
                "event": "result",
                "data": json.dumps(
                    {
                        "message": "Undid the last plot change.",
                        "plot_config_update": None,
                        "routed_to": "undo",
                    }
                ),
            }
            return

        # tool_call events (fired the moment a delegation tool starts, well before
        # its result/the reply text) and text_delta events (from the plain text
        # loop below) both need to reach the client in the order they actually
        # happen - a shared queue lets _ToolResultCapture push the former in
        # concurrently with the latter, rather than only being able to report
        # tool calls after the fact once the whole run finishes.
        queue: asyncio.Queue[dict[str, str] | None] = asyncio.Queue()

        async def emit(event: str, payload: dict[str, Any]) -> None:
            await queue.put({"event": event, "data": json.dumps(payload)})

        # every tool call of the message, the specialists' included, is recorded and
        # sent as it starts and ends, and the whole list goes with the reply
        trace = ToolTrace(emit=emit)
        deps.trace = trace
        capture = _ToolResultCapture(queue, trace)
        accumulated = ""
        answering_model: str | None = None
        stream_exc: Exception | None = None
        # one count for the whole message: the specialists' runs add to it, and it is
        # what the request limit is checked against
        usage = RunUsage()

        async def _produce_text() -> None:
            nonlocal accumulated, answering_model, stream_exc
            try:
                async with chat_agent.run_stream(
                    augmented_message,
                    model=model,
                    deps=deps,
                    message_history=message_history,
                    event_stream_handler=capture,
                    usage=usage,
                    usage_limits=usage_limits(deps.max_requests),
                ) as streamed:
                    async for delta in streamed.stream_text(
                        delta=True, debounce_by=None
                    ):
                        accumulated += delta
                        await queue.put(
                            {
                                "event": "text_delta",
                                "data": json.dumps({"delta": delta}),
                            }
                        )
                    answering_model = _answering_model(streamed.all_messages())
            except PlotEditFailed as exc:
                # the plot agent gave up: its reason is the reply, as it is, with no
                # model asked to rephrase it (that is what kept re-asking the plot agent)
                accumulated = str(exc)
                capture.routed_to = _DELEGATION_TOOL_ROUTES["ask_plot_config_agent"]
                await queue.put(
                    {"event": "text_delta", "data": json.dumps({"delta": accumulated})}
                )
            except UsageLimitExceeded as exc:
                stream_exc = exc
                logger.warning(
                    "SensAI chat stopped at the limit of %d model requests: %s",
                    deps.max_requests,
                    exc,
                )
            except Exception as exc:
                stream_exc = exc
                logger.warning("SensAI chat run_stream failed", exc_info=True)
            finally:
                logger.info("SensAI chat made %d model request(s).", usage.requests)
                trace.finish()
                await queue.put(None)

        producer = asyncio.create_task(_produce_text())
        try:
            while True:
                item = await queue.get()
                if item is None:
                    break
                yield item
        finally:
            if not producer.done():
                producer.cancel()
            with contextlib.suppress(asyncio.CancelledError):
                await producer

        # a run that failed is an error even when some text had already been sent: the
        # browser shows the text as it arrives, so a reply cut off after a word would
        # otherwise end as a finished reply that is one word long
        if stream_exc is not None or not accumulated:
            if isinstance(stream_exc, UsageLimitExceeded):
                detail = _stopped_message(capture.delegate, deps.max_requests)
            elif stream_exc and accumulated:
                detail = f"The reply was cut off: {stream_exc}"
            elif stream_exc:
                detail = f"No response from model: {stream_exc}"
            else:
                detail = "No response from model."
            if accumulated:
                # what arrived stays with the error, since it can be most of an answer
                detail += f"\n\nWhat the model had written:\n\n{accumulated}"
            yield {
                "event": "error",
                "data": json.dumps(
                    {"detail": detail, "tool_calls": _tool_calls(trace)}
                ),
            }
            return

        plot_config_update = _changed_plot_config(
            current_plot_config, deps.current_plot_config
        )
        versus_update = deps.versus if deps.versus != body.versus else None
        yield {
            "event": "result",
            "data": json.dumps(
                {
                    "message": accumulated,
                    "plot_config_update": (
                        plot_config_update.model_dump()
                        if plot_config_update is not None
                        else None
                    ),
                    "versus_update": (
                        versus_update.model_dump(mode="json")
                        if versus_update is not None
                        else None
                    ),
                    "routed_to": capture.routed_to,
                    "model": answering_model,
                    "tool_calls": _tool_calls(trace),
                }
            ),
        }

    return EventSourceResponse(event_stream())