Skip to content

chat_agent

SensAI top-level chat agent - delegates to the domain agents and replies in plain text.

PlotEditFailed

Bases: Exception

The plot agent could not make the change. The message is what to tell the user, and the chat agent is not asked to put it in its own words: a model that was told the change failed would keep sending the same request to the plot agent again.

usage_limits

Python
usage_limits(max_requests: int) -> UsageLimits

The cap on model requests for one chat message, to pass to the chat agent's run and to each specialist's run so that the specialists count against the same total (they share the run's usage).

max_requests is dojo.sensai.max_requests. A request takes one to three (route it, edit, answer) and a refused edit adds one to three more, so the default of 12 leaves room to correct a mistake or two. When a model loops, or rate limits make every request cost several attempts, it is what stops the message from sending dozens.

Source code in src/mujoco_mojo/utils/layers/dojo/sensai/chat_agent.py
Python
def usage_limits(max_requests: int) -> UsageLimits:
    """
    The cap on model requests for one chat message, to pass to the chat agent's run and to each specialist's run so that the specialists count against the same total (they share the run's `usage`).

    `max_requests` is `dojo.sensai.max_requests`. A request takes one to three (route it, edit, answer) and a refused edit adds one to three more, so the default of 12 leaves room to correct a mistake or two. When a model loops, or rate limits make every request cost several attempts, it is what stops the message from sending dozens.
    """
    return UsageLimits(request_limit=max_requests)

ask_job_status_agent async

Python
ask_job_status_agent(
    ctx: RunContext[SensAIDeps], request: str
) -> str

Forward a job-status request to the job-status agent and return its answer. It knows the job's progress, trials, timing, errors and logs, and the input distributions the trials were sampled from and what each trial drew.

Parameters:

Name Type Description Default
ctx RunContext[SensAIDeps]

The calling agent's run context - ctx.model (the same FallbackModel this run was given) has to be passed through explicitly, since job_status_agent has no model= of its own to fall back on; ctx.deps/ctx.usage share dashboard state and token accounting with the nested run the same way.

required
request str

The request to hand off, in natural language (e.g. "how far along is the job?", "which trials are running?", "which trial took the longest?" or "what range is the wind speed sampled from?" or "which trials drew a high wind speed?").

required

Returns:

Type Description
str

The answer, in words, with the numbers and trial numbers from the job's status. Say it as it is: do not round or change them.

Source code in src/mujoco_mojo/utils/layers/dojo/sensai/chat_agent.py
Python
@chat_agent.tool
async def ask_job_status_agent(ctx: RunContext[SensAIDeps], request: str) -> str:
    """
    Forward a job-status request to the job-status agent and return its answer. It knows the job's progress, trials, timing, errors and logs, and the input distributions the trials were sampled from and what each trial drew.

    Args:
        ctx: The calling agent's run context - `ctx.model` (the same `FallbackModel` this run was given) has to be passed through explicitly, since `job_status_agent` has no `model=` of its own to fall back on; `ctx.deps`/`ctx.usage` share dashboard state and token accounting with the nested run the same way.
        request: The request to hand off, in natural language (e.g. "how far along is the job?", "which trials are running?", "which trial took the longest?" or "what range is the wind speed sampled from?" or "which trials drew a high wind speed?").

    Returns:
        The answer, in words, with the numbers and trial numbers from the job's status. Say it as it is: do not round or change them.

    """
    result = await job_status_agent.run(
        request,
        model=_delegate_model(ctx),
        deps=ctx.deps,
        usage=ctx.usage,
        usage_limits=usage_limits(ctx.deps.max_requests),
        event_stream_handler=_watch(ctx, "job_status"),
    )
    return result.output

ask_column_manifest_agent async

Python
ask_column_manifest_agent(
    ctx: RunContext[SensAIDeps], request: str
) -> ColumnManifest

Forward a signal-column request to the column-manifest agent and return its answer.

Parameters:

Name Type Description Default
ctx RunContext[SensAIDeps]

The calling agent's run context - ctx.model (the same FallbackModel this run was given) has to be passed through explicitly, since column_manifest_agent has no model= of its own to fall back on; ctx.deps/ctx.usage share dashboard state and token accounting with the nested run the same way.

required
request str

The request to hand off, in natural language (e.g. "what signals can I plot?").

required

Returns:

Type Description
ColumnManifest

The current column manifest.

Source code in src/mujoco_mojo/utils/layers/dojo/sensai/chat_agent.py
Python
@chat_agent.tool
async def ask_column_manifest_agent(
    ctx: RunContext[SensAIDeps], request: str
) -> ColumnManifest:
    """
    Forward a signal-column request to the column-manifest agent and return its answer.

    Args:
        ctx: The calling agent's run context - `ctx.model` (the same `FallbackModel` this run was given) has to be passed through explicitly, since `column_manifest_agent` has no `model=` of its own to fall back on; `ctx.deps`/`ctx.usage` share dashboard state and token accounting with the nested run the same way.
        request: The request to hand off, in natural language (e.g. "what signals can I plot?").

    Returns:
        The current column manifest.

    """
    result = await column_manifest_agent.run(
        request,
        model=_delegate_model(ctx),
        deps=ctx.deps,
        usage=ctx.usage,
        usage_limits=usage_limits(ctx.deps.max_requests),
        event_stream_handler=_watch(ctx, "column_manifest"),
    )
    return result.output

ask_docs_agent async

Python
ask_docs_agent(
    ctx: RunContext[SensAIDeps], request: str
) -> str

Forward a question about how to use MuJoCo Mojo, or what changed in a release, to the docs agent and return its answer.

Use this for "how do I..." and "what is..." questions about Mojo itself: installing it, starting a project, the generate and runtime scripts, running Monte Carlo jobs, SLURM, the Dojo, Reloaded, requirements, optimization, the examples, which setting does what, and what changed in a release. It answers from the Mojo docs (the user guides, the command line reference and the changelog) and the list of settings, and links the page.

Parameters:

Name Type Description Default
ctx RunContext[SensAIDeps]

The calling agent's run context - ctx.model (the same FallbackModel this run was given) has to be passed through explicitly, since docs_agent has no model= of its own to fall back on; ctx.deps/ctx.usage share dashboard state and token accounting with the nested run the same way.

required
request str

The question, in natural language, such as "how do I submit a job to SLURM?".

required

Returns:

Type Description
str

The answer, with the steps or code it found and a link to the page. Pass it on to the user as it is.

Source code in src/mujoco_mojo/utils/layers/dojo/sensai/chat_agent.py
Python
@chat_agent.tool
async def ask_docs_agent(ctx: RunContext[SensAIDeps], request: str) -> str:
    """
    Forward a question about how to use MuJoCo Mojo, or what changed in a release, to the docs agent and return its answer.

    Use this for "how do I..." and "what is..." questions about Mojo itself: installing it, starting a project, the generate and runtime scripts, running Monte Carlo jobs, SLURM, the Dojo, Reloaded, requirements, optimization, the examples, which setting does what, and what changed in a release. It answers from the Mojo docs (the user guides, the command line reference and the changelog) and the list of settings, and links the page.

    Args:
        ctx: The calling agent's run context - `ctx.model` (the same `FallbackModel` this run was given) has to be passed through explicitly, since `docs_agent` has no `model=` of its own to fall back on; `ctx.deps`/`ctx.usage` share dashboard state and token accounting with the nested run the same way.
        request: The question, in natural language, such as "how do I submit a job to SLURM?".

    Returns:
        The answer, with the steps or code it found and a link to the page. Pass it on to the user as it is.

    """
    result = await docs_agent.run(
        request,
        model=_delegate_model(ctx),
        deps=ctx.deps,
        usage=ctx.usage,
        usage_limits=usage_limits(ctx.deps.max_requests),
        event_stream_handler=_watch(ctx, "docs"),
    )
    return result.output

ask_plot_config_agent async

Python
ask_plot_config_agent(
    ctx: RunContext[SensAIDeps], request: str
) -> str

Forward a plot request to the plot-config agent and return what it did or found.

Use this for any request about the trial viewer plot: reading its current state, changing it (colors, axes, grid, signals, filters, ...), loading a saved plot profile, or versus mode (showing other trials, such as the ones that failed a requirement, behind this one). The plot itself is updated by the agent; you only report what it says.

Parameters:

Name Type Description Default
ctx RunContext[SensAIDeps]

The calling agent's run context - ctx.model (the same FallbackModel this run was given) has to be passed through explicitly, since plot_config_agent has no model= of its own to fall back on; ctx.deps/ctx.usage share dashboard state and token accounting with the nested run the same way.

required
request str

The request to hand off, in natural language (e.g. "make the box_acc:y line red" or "save this as robotics/baseline").

required

Returns:

Type Description
str

A short description of what was changed or found.

Source code in src/mujoco_mojo/utils/layers/dojo/sensai/chat_agent.py
Python
@chat_agent.tool
async def ask_plot_config_agent(ctx: RunContext[SensAIDeps], request: str) -> str:
    """
    Forward a plot request to the plot-config agent and return what it did or found.

    Use this for any request about the trial viewer plot: reading its current state, changing \
    it (colors, axes, grid, signals, filters, ...), loading a saved plot profile, or versus mode \
    (showing other trials, such as the ones that failed a requirement, behind this one). The plot \
    itself is updated by the agent; you only report what it says.

    Args:
        ctx: The calling agent's run context - `ctx.model` (the same `FallbackModel` this run was given) has to be passed through explicitly, since `plot_config_agent` has no `model=` of its own to fall back on; `ctx.deps`/`ctx.usage` share dashboard state and token accounting with the nested run the same way.
        request: The request to hand off, in natural language (e.g. "make the box_acc:y line red" or "save this as robotics/baseline").

    Returns:
        A short description of what was changed or found.

    """
    ctx.deps.plot_edit_refusal = None
    try:
        result = await plot_config_agent.run(
            request,
            model=_delegate_model(ctx),
            deps=ctx.deps,
            usage=ctx.usage,
            usage_limits=usage_limits(ctx.deps.max_requests),
            event_stream_handler=_watch(ctx, "plot_config"),
        )
    except UnexpectedModelBehavior as exc:
        # the model kept repeating a refused call until it ran out of tries; its reason
        # is the reply, so it does not fail the message and the chat agent cannot retry
        reason = ctx.deps.plot_edit_refusal or "the plot-config agent could not finish."
        logger.info("SensAI plot agent gave up: %s", reason)
        raise PlotEditFailed(f"The last change could not be applied: {reason}") from exc
    return result.output