Stable thread updates, folded work view, always-on composer

- Track selection by thread id and sort active threads by run start, so
  reordering no longer resubscribes and reloads the open thread
- Cache blocks per turn item, reuse unchanged segments, memoize views,
  and batch streamed updates to one redraw per frame
- Keep left threads subscribed for a minute so switching back is instant
- Fold runs of tool calls and reasoning into one summary line; clip long
  user messages; e expands both
- Always-visible multi-line composer with per-thread drafts and
  typing/navigation modes
This commit is contained in:
Callum Alpass committed 2026-11-02 17:06:06 +11:00
1 parent 3b7ed56e16
commit cea1409f40
9 files changed
+500 -212

No files matched your search

+94 -12
View File
@@ -12,28 +12,110 @@ export type Block =
export type Tone = "normal" | "muted" | "good" | "bad" | "warn";
/** Tool calls, reasoning and minor notices: the agent's working, as opposed to what it says. */
export type Step = Extract<Block, { kind: "tool" | "reasoning" | "notice" }>;
/** What the conversation view renders: messages stand alone, runs of work fold into one segment. */
export type Segment =
| Extract<Block, { kind: "user" | "assistant" | "approval" | "question" }>
| { readonly kind: "work"; readonly id: string; readonly steps: readonly Step[]; readonly active: boolean }
| { readonly kind: "alert"; readonly id: string; readonly text: string; readonly tone: Tone };
export interface Transcript {
readonly blocks: readonly Block[];
readonly segments: readonly Segment[];
/** Turn items left out because only the tail is shown. */
readonly hiddenCount: number;
readonly pendingRequestIds: ReadonlySet<string>;
}
const OUTPUT_TAIL_LINES = 3;
export function buildTranscript(projection: OrchestrationV2ThreadProjection, options: { readonly tail?: number } = {}): Transcript {
const pendingRequestIds = new Set(projection.runtimeRequests.filter((request) => request.status === "pending").map((request) => request.id as string));
const items = projection.visibleTurnItems;
const tail = options.tail ?? 150;
const start = Math.max(0, items.length - tail);
const blocks: Block[] = [];
for (const row of items.slice(start)) {
const block = blockFor(row.item, pendingRequestIds);
if (block) blocks.push(block);
/** Blocks keyed by turn item object: the projection reducer keeps unchanged items identical. */
const blockCache = new WeakMap<object, Block | null>();
function cachedBlock(item: OrchestrationV2TurnItem, pending: ReadonlySet<string>): Block | null {
// Approval and question blocks depend on request state, not just the item.
if (item.type === "approval_request" || item.type === "user_input_request") return blockFor(item, pending);
let block = blockCache.get(item);
if (block === undefined) {
block = blockFor(item, pending);
blockCache.set(item, block);
}
return { blocks, hiddenCount: start, pendingRequestIds };
return block;
}
export function buildTranscript(
projection: OrchestrationV2ThreadProjection,
options: { readonly tail?: number; readonly running?: boolean; readonly previous?: Transcript | null } = {},
): Transcript {
const pending = new Set(projection.runtimeRequests.filter((request) => request.status === "pending").map((request) => request.id as string));
const items = projection.visibleTurnItems;
const tail = options.tail ?? 200;
const start = Math.max(0, items.length - tail);
const segments: Segment[] = [];
let work: Step[] | null = null;
const flushWork = () => {
if (work && work.length > 0) segments.push({ kind: "work", id: `work-${work[0]!.id}`, steps: work, active: false });
work = null;
};
for (let index = start; index < items.length; index++) {
const block = cachedBlock(items[index]!.item, pending);
if (!block) continue;
if (block.kind === "tool" || block.kind === "reasoning" || (block.kind === "notice" && block.tone !== "bad" && block.tone !== "warn")) {
(work ??= []).push(block);
continue;
}
flushWork();
segments.push(block.kind === "notice" ? { kind: "alert", id: block.id, text: block.text, tone: block.tone } : block);
}
flushWork();
const last = segments.at(-1);
if (options.running && last?.kind === "work") segments[segments.length - 1] = { ...last, active: true };
return { segments: reuseUnchanged(segments, options.previous ?? null), hiddenCount: start };
}
/** Keeps the previous segment object when nothing in it changed, so memoized views skip it. */
function reuseUnchanged(segments: Segment[], previous: Transcript | null): Segment[] {
if (!previous) return segments;
const byId = new Map(previous.segments.map((segment) => [segment.id, segment]));
return segments.map((segment) => {
const old = byId.get(segment.id);
return old && sameSegment(old, segment) ? old : segment;
});
}
function sameSegment(a: Segment, b: Segment): boolean {
if (a === b) return true;
if (a.kind !== b.kind) return false;
if (a.kind === "work" && b.kind === "work") {
return a.active === b.active && a.steps.length === b.steps.length && a.steps.every((step, index) => step === b.steps[index]);
}
return shallowEqual(a as unknown as Record<string, unknown>, b as unknown as Record<string, unknown>);
}
function shallowEqual(a: Record<string, unknown>, b: Record<string, unknown>): boolean {
const keys = Object.keys(a);
return keys.length === Object.keys(b).length && keys.every((key) => Object.is(a[key], b[key]) || (Array.isArray(a[key]) && Array.isArray(b[key]) && shallowEqualArray(a[key] as unknown[], b[key] as unknown[])));
}
function shallowEqualArray(a: unknown[], b: unknown[]): boolean {
return a.length === b.length && a.every((value, index) => Object.is(value, b[index]));
}
/** One line describing a folded run of work, e.g. "5 commands · 3 edits · 2 searches". */
export function summarizeWork(steps: readonly Step[]): string {
const counts = new Map<string, number>();
const bump = (label: string) => counts.set(label, (counts.get(label) ?? 0) + 1);
for (const step of steps) {
if (step.kind === "reasoning") continue;
if (step.kind === "notice") bump("update");
else bump(STEP_LABELS[step.icon] ?? "tool call");
}
const parts = [...counts].sort((a, b) => b[1] - a[1]).map(([label, count]) => `${count} ${count === 1 ? label : `${label}s`}`);
return parts.length > 0 ? parts.join(" · ") : "thinking";
}
const STEP_LABELS: Record<string, string> = { $: "command", "✎": "edit", "⌕": "search", "⚙": "tool call", "↳": "subagent", "☐": "plan update" };
export function blockFor(item: OrchestrationV2TurnItem, pending: ReadonlySet<string>): Block | null {
const id = item.id;
switch (item.type) {