Skip to content

tool_trace

A record of the tool calls made while SensAI answers one message, so the chat can show how it got to its reply.

ARGS_LENGTH module-attribute

Python
ARGS_LENGTH = 200

The most characters of a call's arguments kept for the chat, which shows them on one line.

RESULT_LENGTH module-attribute

Python
RESULT_LENGTH = 300

The most characters of a call's result kept for the chat. A job report or the column list can be thousands, and the chat only previews it.

CHAT_AGENT module-attribute

Python
CHAT_AGENT = 'chat'

The agent of a call the chat agent made itself, which is a hand-off to a specialist. A specialist's own calls carry the specialist's route (job_status, plot_config, ...).

ToolStatus module-attribute

Python
ToolStatus = Literal['running', 'ok', 'rejected', 'failed']

How a call went: still running, returned a result, turned away by the tool with a reason the model was told (a refused plot edit, bad arguments), or never returned (the specialist gave up or the message hit its request limit).

Emit module-attribute

Python
Emit = Callable[[str, dict[str, Any]], Awaitable[None]]

Sends one event to the chat as it happens, by name and payload.

ToolCallRecord

Bases: BaseModel

One tool call, as the chat shows it.

id instance-attribute

Python
id: int

Its place in the message, from 1, in the order the calls started.

name instance-attribute

Python
name: str

The tool's name, such as edit_plot_config.

agent instance-attribute

Python
agent: str

Who called it: chat for a hand-off, else the specialist's route.

args instance-attribute

Python
args: str

The arguments the model sent, as one line of JSON, cut to ARGS_LENGTH.

result class-attribute instance-attribute

Python
result: str | None = None

What the tool returned, or why it was refused, as one line cut to RESULT_LENGTH. None while it runs and when it never returned.

ToolTrace dataclass

Python
ToolTrace(
    emit: Emit | None = None,
    calls: list[ToolCallRecord] = list(),
)

The tool calls one chat message made, from the chat agent and from every specialist it handed the request to.

Each agent's run gets a handler from handler (the chat agent's own goes through tap), which records a call when it starts and its outcome when it ends, and tells emit so the chat can show each one live. calls is the whole list once the message is done.

emit class-attribute instance-attribute

Python
emit: Emit | None = None

Called with tool_start as a call begins and tool_end as it finishes, or None to only record.

calls class-attribute instance-attribute

Python
calls: list[ToolCallRecord] = field(default_factory=list)

Every call so far, in the order it started.

tap async

Python
tap(
    agent: str, events: AsyncIterable[AgentStreamEvent]
) -> AsyncIterator[AgentStreamEvent]

Records the tool calls in events as they pass and gives every event on, unchanged, so a caller that reads the events for something else can use it in place of events.

Source code in src/mujoco_mojo/utils/layers/dojo/sensai/tool_trace.py
Python
async def tap(
    self, agent: str, events: AsyncIterable[AgentStreamEvent]
) -> AsyncIterator[AgentStreamEvent]:
    """Records the tool calls in `events` as they pass and gives every event on, unchanged, so a caller that reads the events for something else can use it in place of `events`."""
    async for event in events:
        await self._record(agent, event)
        yield event

handler

Python
handler(
    agent: str,
) -> Callable[
    [RunContext[Any], AsyncIterable[AgentStreamEvent]],
    Awaitable[None],
]

An event_stream_handler for the run of agent that records its tool calls and does nothing else.

Source code in src/mujoco_mojo/utils/layers/dojo/sensai/tool_trace.py
Python
def handler(
    self, agent: str
) -> Callable[[RunContext[Any], AsyncIterable[AgentStreamEvent]], Awaitable[None]]:
    """An `event_stream_handler` for the run of `agent` that records its tool calls and does nothing else."""

    async def record(
        _: RunContext[Any], events: AsyncIterable[AgentStreamEvent]
    ) -> None:
        async for _event in self.tap(agent, events):
            pass

    return record

finish

Python
finish() -> None

Marks every call that never returned as failed. Call it when the message is over, since a specialist that gave up or a message that ran out of requests leaves its call open.

Source code in src/mujoco_mojo/utils/layers/dojo/sensai/tool_trace.py
Python
def finish(self) -> None:
    """Marks every call that never returned as failed. Call it when the message is over, since a specialist that gave up or a message that ran out of requests leaves its call open."""
    for record in self._running.values():
        record.status = "failed"
    self._running.clear()