ui / src / components / agent-chat / agent-events.tsmain at d68d7e1
// The agent's log as the agent-UI reducer (agent-ui-reducer.ts) reads it: `reduceAgentUi`
// folds every committed event, the agent's and the context's script runs (`itx/run-requested` /
// `itx/run-settled`, identified by the request's offset) alike, into messages and activities (an
// LLM step that wrote a script, the code step that ran it, grouped into rounds). The fold reads
// the current version of the path alone (`foldAgentLog`).
import { z } from "zod";
import { RunRequested, RunSettled, type RunSettlement } from "iterate/stream/run";
import { sliceText, type StreamText } from "./chunked-text.ts";
import { AGENT_FACT, scriptCallOf } from "./agent-facts.ts";
import {
initialAgentUiState,
reduceAgentUi,
settleAgentUiAtIdleBoundary,
withoutRunningRequests,
type AgentUiItem,
type AgentUiState,
type AgentUiStep,
} from "./agent-ui-reducer.ts";
import type { StreamEvent } from "./stream-event.ts";
// Loose: the Events view is the raw log, so every envelope field the wire carries survives.
const Committed = z.looseObject({
offset: z.number().int().positive(),
type: z.string(),
createdAt: z.string(),
payload: z.unknown().optional(),
metadata: z.record(z.string(), z.unknown()).optional(),
idempotencyKey: z.string().optional(),
});
/** A row of the log as the reducer's `StreamEvent`, or null when it is not a committed row. */
export function toAgentEvent(raw: unknown): StreamEvent | null {
const parsed = Committed.safeParse(raw);
return parsed.success ? parsed.data : null;
}
/** THE BORDER OF A VERSION. `itx.cd(path).newVersion()` starts a path over: its log goes on, and
* this fact ends the earlier version (the next fact, `itx/created`, names the new version). The
* facts at lower offsets are another conversation. The kernel appends `itx/started-over`; a log
* that took a new version under the earlier name holds `itx/version-ended`, for ever. */
const STARTED_OVER = new Set([
"events.iterate.com/itx/started-over",
"events.iterate.com/itx/version-ended",
]);
/** A stretch of the fold, kept so that an older page does not fold the held log again: the state
* it began from, the events it folded, the reducer's items for them (before the runs are joined)
* and the state it ended in. The reducer is a pure function of a state and an event, so a chunk
* that begins from an equal state gives these same items and this same end. */
type FoldChunk = {
start: AgentUiState;
events: readonly StreamEvent[];
items: readonly AgentUiItem[];
end: AgentUiState;
};
/** How many events a chunk holds at most. */
const CHUNK = 256;
/** The log folded so far, which the next call goes on from. */
export type AgentLogFold = {
/** The rows folded, as the caller's array held them: how the next call tells what grew. */
log: readonly unknown[];
/** The committed events of the current version, in offset order. */
events: readonly StreamEvent[];
state: AgentUiState;
items: readonly AgentUiItem[];
/** The offset of the newest `itx/started-over` held, or null: the current version starts after
* it, and the chat reads no page older than it. */
versionEndedAt: number | null;
/** The fold in stretches, for an older page (`FoldChunk`). */
chunks?: readonly FoldChunk[];
};
const NOTHING_FOLDED: AgentLogFold = {
log: [],
events: [],
state: initialAgentUiState(),
items: [],
versionEndedAt: null,
chunks: [],
};
/** `events` folded from `start`, in chunks. */
function chunksOf(start: AgentUiState, events: readonly StreamEvent[]): FoldChunk[] {
const chunks: FoldChunk[] = [];
let state = start;
for (let at = 0; at < events.length; at += CHUNK) {
const own = events.slice(at, at + CHUNK);
const items: AgentUiItem[] = [];
const began = state;
for (const event of own) {
const reduced = reduceAgentUi(state, event);
state = reduced.endState;
items.push(...reduced.items);
}
chunks.push({ start: began, events: own, items, end: state });
}
return chunks;
}
/** Two states that the reducer cannot tell apart: plain data, compared part by part, and no
* further than the first difference. A state can be large (an answer that runs holds its scripts
* and its words), and two states that differ do so near their top (the count of the open
* activity's steps). As text they cost a second of a frame on a long chat (a profile of the
* engineering manager's chat, 28 Nov 2026: 978 ms of one landing in `JSON.stringify`). */
function sameState(one: unknown, other: unknown): boolean {
if (one === other) return true;
if (typeof one !== "object" || typeof other !== "object" || one === null || other === null)
return false;
if (Array.isArray(one) !== Array.isArray(other)) return false;
if (Array.isArray(one)) {
const others = other as unknown[];
if (one.length !== others.length) return false;
for (let at = 0; at < one.length; at += 1) if (!sameState(one[at], others[at])) return false;
return true;
}
const mine = one as Record<string, unknown>;
const theirs = other as Record<string, unknown>;
// a key that holds nothing is no key: the reducer reads `undefined` and a missing key alike
const keys = Object.keys(mine).filter((key) => mine[key] !== undefined);
if (keys.length !== Object.keys(theirs).filter((key) => theirs[key] !== undefined).length)
return false;
for (const key of keys) if (!sameState(mine[key], theirs[key])) return false;
return true;
}
/** AN OLDER PAGE, WITHOUT FOLDING THE HELD LOG AGAIN. The page is folded from the start, and then
* each held chunk again only until the state reaches what a held chunk began from: from that
* chunk on, the held items and the held end are the fold's own. In a log of turns that is the
* first held chunk or the second, since a turn that ended leaves the state as it began. Measured
* 28 Nov 2026 on the engineering manager's chat: each older page that landed folded the whole
* held log, 13 to 87 ms of a frame, more with each page. Null: the rows are no older page of
* what is held. */
function withOlderPage(held: AgentLogFold, log: readonly unknown[]): AgentLogFold | null {
const chunks = held.chunks;
const added = log.length - held.log.length;
if (
!chunks ||
held.log.length === 0 ||
added <= 0 ||
log[added] !== held.log[0] ||
log.at(-1) !== held.log.at(-1)
)
return null;
// the held log is one version's from a border on: no page below the border is read
if (held.versionEndedAt !== null) return { ...held, log };
const arrived = log.slice(0, added).flatMap((row) => toAgentEvent(row) ?? []);
const border = arrived.findLast((event) => STARTED_OVER.has(event.type));
const older = border ? arrived.slice(arrived.indexOf(border) + 1) : arrived;
const fresh = chunksOf(initialAgentUiState(), older);
let state = fresh.at(-1)?.end ?? initialAgentUiState();
let reused = 0;
for (; reused < chunks.length; reused += 1) {
const chunk = chunks[reused]!;
if (sameState(state, chunk.start)) break;
const [again] = chunksOf(state, chunk.events);
if (!again) continue;
fresh.push(again);
state = again.end;
}
const all = fresh.concat(chunks.slice(reused));
const rows = inRuns(all.flatMap((chunk) => chunk.items));
return {
log,
events: older.length > 0 ? older.concat(held.events) : held.events,
state: reused < chunks.length ? held.state : state,
items: keepingHeldMessages(rows, held.items),
versionEndedAt: border ? border.offset : null,
chunks: all,
};
}
/** `log`, sorted by offset, folded through the reducer: on from `held` when the log only grew at its
* end, so a live tail costs what arrived; with an older page before what is held, only that page
* and the held chunks that it changes (`withOlderPage`); from the start otherwise. Either way a
* settled row keeps its identity, so a memoised row skips the re-render that a streamed frame or an
* older page would bring and its markdown is not parsed again. ONLY THE CURRENT VERSION is folded:
* the events after the newest `itx/started-over` held. The feed, the live line and the traces
* read this fold, so they agree. */
export function foldAgentLog(
held: AgentLogFold | undefined,
log: readonly unknown[],
): AgentLogFold {
const grewAtEnd =
held &&
held.log.length > 0 &&
log[0] === held.log[0] &&
log[held.log.length - 1] === held.log.at(-1);
if (held && !grewAtEnd) {
// an older page alone, or one with new rows at the end too: the page first, then the rows
const from = held.log.length > 0 ? log.indexOf(held.log[0]) : -1;
const through = from + held.log.length;
if (from > 0 && through <= log.length && log[through - 1] === held.log.at(-1)) {
const paged = withOlderPage(held, log.slice(0, through));
if (paged)
return through === log.length
? paged
: foldAgentLog({ ...paged, log: log.slice(0, through) }, log);
}
}
const from = grewAtEnd ? held : NOTHING_FOLDED;
const arrived = log.slice(from.log.length).flatMap((row) => toAgentEvent(row) ?? []);
const border = arrived.findLast((event) => STARTED_OVER.has(event.type));
// a border starts the fold over after it: the facts at lower offsets are another version's
const start = border ? { ...NOTHING_FOLDED, versionEndedAt: border.offset } : from;
const fresh = border ? arrived.slice(arrived.indexOf(border) + 1) : arrived;
// what arrived goes on in the last chunk while that has room, so a live tail makes no chunk for
// each event; a fold with no chunks (an older caller's) keeps none
const before = start.chunks;
const last = before?.at(-1);
const joins = last && last.events.length + fresh.length <= CHUNK ? last : undefined;
const added = chunksOf(start.state, fresh);
const chunks =
before === undefined
? undefined
: fresh.length === 0
? before
: joins
? [
...before.slice(0, -1),
{
start: joins.start,
events: joins.events.concat(fresh),
items: joins.items.concat(added.flatMap((chunk) => chunk.items)),
end: added.at(-1)!.end,
},
]
: before.concat(added);
const state = added.at(-1)?.end ?? start.state;
const rows = inRuns(added.flatMap((chunk) => chunk.items));
return {
log,
events: fresh.length > 0 ? start.events.concat(fresh) : start.events,
state,
items:
rows.length > 0
? grewAtEnd && !border
? goingOn(start.items, rows)
: keepingHeldMessages(rows, held?.items)
: start.items,
versionEndedAt: start.versionEndedAt,
chunks,
};
}
/** `items` with each run of inputs that started no turn joined into its first one's row. */
function inRuns(items: readonly AgentUiItem[]) {
const rows: AgentUiItem[] = [];
for (const item of items) {
const last = rows.at(-1);
if (item.kind === "background" && last?.kind === "background")
last.messages.push(...item.messages);
else rows.push(item.kind === "background" ? { ...item, messages: [...item.messages] } : item);
}
return rows;
}
/** `rows` after `held`, where a run that ends `held` goes on: its row keeps its id as it grows. */
function goingOn(held: readonly AgentUiItem[], rows: readonly AgentUiItem[]) {
const last = held.at(-1);
const [first, ...rest] = rows;
if (last?.kind !== "background" || first?.kind !== "background") return held.concat(rows);
const grown = { ...last, messages: [...last.messages, ...first.messages] };
return [...held.slice(0, -1), grown, ...rest];
}
/** `items`, with each row that `held` has, and that says the same, as the object `held` has: a
* memoised row is then not drawn again. An activity can gain steps from an older page, so it is
* new. A message is the same only when it says the same: a message whose sender stands in an older
* page was drawn without the sender until that page came (the first row of a held log, found on a
* real log 28 Nov 2026: the id was the same and the words were not, and the earlier row stayed). */
function keepingHeldMessages(items: AgentUiItem[], held: readonly AgentUiItem[] = []) {
const heldById = new Map(held.map((item) => [item.id, item]));
return items.map((item) => {
const kept = heldById.get(item.id);
if (!kept || kept === item || item.kind === "activity" || kept.kind !== item.kind) return item;
return JSON.stringify(kept) === JSON.stringify(item) ? kept : item;
});
}
/** The feed a fold shows, given what the agent facet says of the turn. When no request can be open,
* one still running in the log never opened. With the facet `idle`, the turn boundary flushes what
* waits behind an activity whose every step has settled. */
export function agentFeedOf(
fold: AgentLogFold,
idle: boolean,
requestMayBeOpen = true,
): { state: AgentUiState; items: readonly AgentUiItem[] } {
const state = requestMayBeOpen ? fold.state : withoutRunningRequests(fold.state);
if (!idle) return { state, items: fold.items };
const settled = settleAgentUiAtIdleBoundary(state);
const items = settled.items.length > 0 ? goingOn(fold.items, inRuns(settled.items)) : fold.items;
return { state: settled.endState, items };
}
/** The whole feed from the log, folded from its first row. */
export function reduceAgentFeed(
events: readonly StreamEvent[],
idle: boolean,
requestMayBeOpen?: boolean,
) {
return agentFeedOf(foldAgentLog(undefined, events), idle, requestMayBeOpen);
}
/** The model's own code inside the agent's envelope (core/configs/default agent/script-envelope.ts
* wraps it between these markers), or null for a script that has no envelope. */
export function authoredScript(code: string): string | null {
const open = "//#region your script";
const start = code.indexOf(open);
const end = code.lastIndexOf("//#endregion your script");
if (start === -1 || end < start) return null;
return code.slice(start + open.length, end).trim();
}
// ── display formatters ──
/** CLI-style elapsed clock for the live phase indicator: one decimal, no space (`0.9s`, `12.3s`). */
export function formatElapsedSeconds(durationMs: number): string {
return `${(Math.max(0, durationMs) / 1000).toFixed(1)}s`;
}
export function formatClockTime(timestampMs: number): string {
return new Date(timestampMs).toLocaleTimeString([], {
hour: "2-digit",
minute: "2-digit",
second: "2-digit",
});
}
export function formatDateTime(timestampMs: number): string {
return new Date(timestampMs).toLocaleString([], { dateStyle: "medium", timeStyle: "medium" });
}
export function formatFileSize(size: number): string {
if (size < 1024) return `${String(size)} B`;
const kilobytes = size / 1024;
if (kilobytes < 1024) return `${kilobytes.toFixed(1).replace(/\.0$/, "")} KB`;
return `${(kilobytes / 1024).toFixed(1).replace(/\.0$/, "")} MB`;
}
const CODE_START_PATTERN = /^\s*(async|await|function|const|let|import)\b/;
const CODEMODE_TAG_PATTERN = /^[ \t]*<codemode(\s|>)/m;
/** A codemode answer renders as code; prose renders as markdown. */
export function looksLikeCode(text: StreamText): boolean {
const prefix = typeof text === "string" ? text : sliceText(text, 0, 4096);
return (
prefix.includes("```") || CODE_START_PATTERN.test(prefix) || CODEMODE_TAG_PATTERN.test(prefix)
);
}
/** What the live activity is doing, from its running steps. */
export function liveActivityLabel(runningSteps: readonly AgentUiStep[]): string {
if (runningSteps.some((step) => step.kind === "code")) return "Running code";
const llm = runningSteps.findLast((step) => step.kind === "llm");
if (!llm) return "Working…";
if (llm.kind === "llm" && llm.responseText.length > 0) return "Writing code";
// the provider says the model reasons, or reasoning words have arrived
if (llm.kind === "llm" && (llm.thinking || llm.thinkingText.length > 0)) return "Thinking";
return "Waiting for a response";
}
/** The items a request of the agent adds to the conversation: a script's result is one of them. */
const RequestItems = z.object({
itemsAdded: z.array(
z.looseObject({
atOffset: z.number().optional(),
rank: z.number().optional(),
tag: z.string().optional(),
text: z.string(),
}),
),
});
type ScriptTrace = {
requestOffset: number;
code: string;
requestedAtMs: number;
settlement?: { atMs: number; value: RunSettlement };
/** What the model was told of the outcome: the script's result as a later request of the agent
* gives it. */
seen?: string;
};
/** One script run by its `itx/run-requested` offset: the code, its `itx/run-settled`, and the
* result a later request gave the model. The agent places that result at the request whose
* response wrote the script, after the response (`rank` 1 is its first script): the run's key
* names both (agent-facts.ts). */
export function scriptTrace(
events: readonly StreamEvent[],
requestOffset: number,
): ScriptTrace | null {
const requested = events.find(
(event) =>
event.offset === requestOffset && event.type === "events.iterate.com/itx/run-requested",
);
const request = RunRequested.safeParse(requested?.payload);
if (!requested || !request.success) return null;
const [settlement] = events.flatMap((event) => {
if (event.type !== "events.iterate.com/itx/run-settled") return [];
const settled = RunSettled.safeParse(event.payload);
return settled.success && settled.data.requestOffset === requestOffset
? [{ atMs: Date.parse(event.createdAt), value: settled.data.settlement }]
: [];
});
const call = scriptCallOf(requested.idempotencyKey);
const [seen] = call
? events.flatMap((event) => {
if (event.type !== AGENT_FACT.llmRequested) return [];
const asked = RequestItems.safeParse(event.payload);
if (!asked.success) return [];
const result = asked.data.itemsAdded.find(
(item) =>
item.tag === "script-result" &&
item.atOffset === call.requestOffset &&
item.rank === 1 + call.index,
);
return result ? [result.text] : [];
})
: [];
return {
requestOffset,
code: request.data.code,
requestedAtMs: Date.parse(requested.createdAt),
settlement,
seen,
};
}