src/agents/model.ts is the presentation model of the agents package. It is pure: wire records in, flat display records out, no network and no clock. Two functions do most of the work: chatRows builds the transcript and runRow builds a run card. Both are called from sources and streams.
Types
Type | Meaning |
|---|---|
|
|
|
|
| One transcript row. Matches |
| A descendant run shown under a plan step or under the card |
| One run card. Matches |
Every ChatRow has all 14 fields set (strings default to ""), because a Contract shape has no optional fields.
chatRows
chatRows(events, triggerContext?) makes one pass to index tool_result and tool_spawn events by toolCallId and to collect known call ids, then a second pass in seq order. The kind field decides how the session screen draws a row:
Kind | Produced from |
|---|---|
| A text part or message text. |
| Text whose actor is |
| A |
| The session's trigger context, placed first with |
| A system message starting with |
| An |
| A message's |
| Anything else: unknown parts and event types, structured |
Rules worth knowing:
Row ids. The first part of a message keeps the event id; later parts are <id>-part-<i>; legacy tool calls are <id>-tool-<i>. Contract tests rely on these ids (message-mock-session-17, tool-mock-session-20).
Tool rows. tool renames execute_code to execute, and drops plan and status tools from the chat (inferred reason: the plan is shown on the run card). Status is Failed when the result has an error or isError is true, Done when a result or output exists, else Running. Title, label, link target and source chip come from tool-summary. A spawn attaches childSessionId and childTitle.
Continuations. textRow parses <predecessor session="..." title="..."> and the <handoff> body, shows the first non-heading line, and links to /s/<predecessor>.
Trigger framing is hidden. A user message containing <trigger_context> is cut at that tag, <trigger_data_warning> is removed, and <trigger_instructions> is moved once into the trigger row's detail.
Nothing is dropped silently. Unknown content becomes a raw row with its JSON in detail.
Linear time. Results are joined through maps. A test builds 40 000 events and requires chatRows to finish in under 150 ms, named for the 100 ms turn budget.
upsertEvent replaces by id and sorts by seq, so "Replay is idempotent and out-of-order delivery cannot scramble the transcript".
Truncation limit
detail(value) pretty-prints JSON with two spaces and cuts it at 4 000 characters, appending a line that says it was truncated. It is used for toolInput, toolOutput and every raw row. This is the only size limit in the module. The row list itself is not capped.
runRow
runRow(run, sessionPlan?, tree = []) converts one run record:
Field | Rule |
|---|---|
|
|
| Status is |
|
|
|
|
| The plan's steps. When the run is no longer live, a |
| Descendants not attached to any step |
| For the live duration and the "Waking in" countdown |
|
|
Plan ownership
A session has one plan record, but several runs. The plan shown on a card is run.plan if present; otherwise the session plan when its ownerRunId equals this run; otherwise, when the session plan names no owner, only a live run shows it. A finished run therefore never displays a later run's plan.
Child runs
Descendants are the records in tree whose rootRunId matches. A child attaches to a step by planStepId when it has one, else by stepLabel equal to the step's label. Unattached children stay visible under the card.
Pinned runs
pinned is live and (status is waiting or the plan is not frozen). A plan is frozen when it has settledAt and every step is done, failed or skipped. The session screen places pinned cards after the last message, and unpinned cards in the timeline at at, which is the frozen time, else finishedAt, else updatedAt. So a working run stays at the bottom, a waiting run stays reachable, and a completed plan settles into place before the closing reply.
Tests
agents.test.ts, group transcript and run presentation, six tests: replay and tool joins, pending and failed tools with hidden planning tools, frozen plans and the owning run, continuations, settled plans and pinned waiting runs, token thresholds.
parity.test.ts: child runs by step id then label, tool spawns, trigger context, structured parts, legacy embedded tools, inspectable structured content.
review.test.ts: the linear-time test.
Do you like what you are reading? Subscribe to receive updates.
Unsubscribe anytime