/** * Renders the whole app against invented demo data (the interaction-test * harness's fake session) and writes the settled frame as an HTML fragment * for the landing page: dark, dark in a standalone page, and light. * * Run with `bun run scripts/demo-capture.tsx`. */ import { mkdir, writeFile } from "node:fs/promises"; import { join } from "node:path"; import { type CapturedFrame, type RGBA, TextAttributes } from "@opentui/core"; import type { OrchestrationV2ShellSnapshot, OrchestrationV2ThreadProjection } from "@t3tools/contracts"; import * as DateTime from "effect/DateTime"; import { applyTheme, colors } from "../src/ui/theme.ts"; import { fakeSession, renderApp } from "../test/app/harness.tsx"; import { v2Project, v2Projection, v2ThreadShell } from "../vendor/t3code/packages/client-runtime/src/state/orchestrationV2TestFixtures.ts"; /** Where the HTML goes: DEMO_OUT, or dist/demo. */ const OUT_DIR = process.env.DEMO_OUT ?? join(import.meta.dir, "../dist/demo"); const NOW = Date.now(); const MIN = 60_000; const HOUR = 60 * MIN; const DAY = 24 * HOUR; /** A DateTime `ms` before the capture, so every relative time reads naturally. */ const ago = (ms: number) => DateTime.makeUnsafe(NOW - ms); // --- The demo shell: 3 projects, 8 threads --------------------------------- const project = (id: string, title: string, workspaceRoot: string) => ({ ...v2Project, id, title, workspaceRoot }); const OPEN_THREAD = "thread-bundle"; const APPROVAL_ID = "approval-1"; const thread = (extra: Record) => ({ ...v2ThreadShell, status: "completed", activeRunId: null, latestRunId: "run-1", latestRunRequestedAt: ago(3 * HOUR), latestRunStartedAt: ago(3 * HOUR), latestRunCompletedAt: ago(3 * HOUR), pendingRuntimeRequest: null, pinnedAt: null, lastVisitedAt: ago(2 * HOUR), ...extra, }); const snapshot = { schemaVersion: 1, snapshotSequence: 0, projects: [ project("project-t3tui", "t3tui", "/home/dev/projects/t3tui"), project("project-acme", "acme-api", "/home/dev/projects/acme-api"), project("project-dotfiles", "dotfiles", "/home/dev/dotfiles"), ], threads: [ // The open thread: pinned, running, and waiting on a command approval. thread({ id: OPEN_THREAD, projectId: "project-t3tui", title: "Bundle into dist/", runtimeMode: "approval-required", status: "running", latestRunId: "run-2", activeRunId: "run-2", latestRunRequestedAt: ago(3 * MIN), latestRunStartedAt: ago(3 * MIN), latestRunCompletedAt: null, activityRunStartedAt: ago(3 * MIN), pendingRuntimeRequest: { id: APPROVAL_ID, kind: "command", createdAt: ago(40_000) }, latestUserMessageAt: ago(3 * MIN), pinnedAt: ago(2 * DAY), pinOrderKey: "a", createdAt: ago(2 * DAY), updatedAt: ago(40_000), }), // Working, six minutes in. thread({ id: "thread-flaky", projectId: "project-acme", title: "Fix flaky auth test", modelSelection: { instanceId: "claude", model: "claude-sonnet-5" }, status: "running", latestRunId: "run-flaky", activeRunId: "run-flaky", latestRunRequestedAt: ago(6 * MIN), latestRunStartedAt: ago(6 * MIN), latestRunCompletedAt: null, activityRunStartedAt: ago(6 * MIN), latestUserMessageAt: ago(6 * MIN), createdAt: ago(5 * HOUR), updatedAt: ago(20_000), }), // Waiting for an answer. thread({ id: "thread-resize", projectId: "project-t3tui", title: "Resize the thread list", status: "running", latestRunId: "run-resize", activeRunId: "run-resize", latestRunRequestedAt: ago(12 * MIN), latestRunStartedAt: ago(12 * MIN), latestRunCompletedAt: null, pendingRuntimeRequest: { id: "question-1", kind: "user_input", createdAt: ago(9 * MIN) }, latestUserMessageAt: ago(12 * MIN), createdAt: ago(DAY), updatedAt: ago(9 * MIN), }), // Finished while you were looking elsewhere. thread({ id: "thread-rate-limit", projectId: "project-acme", title: "Add rate-limit headers", latestRunRequestedAt: ago(40 * MIN), latestRunStartedAt: ago(40 * MIN), latestRunCompletedAt: ago(18 * MIN), latestUserMessageAt: ago(40 * MIN), lastVisitedAt: ago(2 * HOUR), createdAt: ago(DAY), updatedAt: ago(18 * MIN), }), thread({ id: "thread-sighup", projectId: "project-dotfiles", title: "Exit on SIGHUP", latestRunRequestedAt: ago(HOUR), latestRunStartedAt: ago(HOUR), latestRunCompletedAt: ago(47 * MIN), latestUserMessageAt: ago(HOUR), lastVisitedAt: ago(3 * HOUR), createdAt: ago(2 * DAY), updatedAt: ago(47 * MIN), }), // Idle and read. thread({ id: "thread-tmux", projectId: "project-t3tui", title: "Add tmux mirror", latestRunRequestedAt: ago(3 * HOUR + 10 * MIN), latestRunStartedAt: ago(3 * HOUR + 10 * MIN), latestRunCompletedAt: ago(3 * HOUR), latestUserMessageAt: ago(3 * HOUR + 10 * MIN), lastVisitedAt: ago(2 * HOUR + 50 * MIN), createdAt: ago(3 * DAY), updatedAt: ago(3 * HOUR), }), thread({ id: "thread-aliases", projectId: "project-dotfiles", title: "Tidy shell aliases", latestRunRequestedAt: ago(3 * DAY), latestRunStartedAt: ago(3 * DAY), latestRunCompletedAt: ago(3 * DAY), latestUserMessageAt: ago(3 * DAY), lastVisitedAt: ago(3 * DAY - HOUR), createdAt: ago(6 * DAY), updatedAt: ago(3 * DAY), }), // Put away: the Settled section folds to a count at the bottom of the list. thread({ id: "thread-deps", projectId: "project-acme", title: "Bump dependencies", settledOverride: "settled", settledAt: ago(DAY), latestRunRequestedAt: ago(2 * DAY), latestRunStartedAt: ago(2 * DAY), latestRunCompletedAt: ago(2 * DAY), latestUserMessageAt: ago(2 * DAY), lastVisitedAt: ago(DAY), createdAt: ago(4 * DAY), updatedAt: ago(DAY), }), ], archivedThreads: [], } as unknown as OrchestrationV2ShellSnapshot; // --- The open thread's conversation ----------------------------------------- const USER_1 = "Bundle the CLI into dist/ so it can run without the repo checkout. Keep the vendor link step on postinstall."; const ASSISTANT_1 = [ "Done — the bundle step is **self-contained** now:", "", "- `scripts/build.ts` bundles `src/bin.ts` into `dist/`", "- the vendor symlink is created on `postinstall`", "- `bun run build` passes typecheck and the test suite", "", "Say the word and I'll cut a clean rebuild.", ].join("\n"); const USER_2 = "Now rebuild dist/ from scratch and check the bundled binary starts."; const run = (id: string, ordinal: number, userMessageId: string, status: string, requestedMsAgo: number, completedMsAgo: number | null) => ({ id, threadId: OPEN_THREAD, ordinal, providerInstanceId: "codex", modelSelection: { instanceId: "codex", model: "gpt-5.4" }, providerThreadId: null, userMessageId, rootNodeId: null, activeAttemptId: null, status, requestedAt: ago(requestedMsAgo), startedAt: ago(requestedMsAgo), completedAt: completedMsAgo === null ? null : ago(completedMsAgo), checkpointId: null, contextHandoffId: null, }); let ordinal = 0; const item = (runId: string, startMsAgo: number, work: Record, status = "completed", spanMs = 5_000) => ({ id: `item-${ordinal}`, threadId: OPEN_THREAD, runId, nodeId: null, providerThreadId: null, providerTurnId: null, nativeItemRef: null, parentItemId: null, ordinal: ordinal++, status, title: null, startedAt: ago(startMsAgo), completedAt: status === "completed" ? ago(startMsAgo - spanMs) : null, updatedAt: status === "completed" ? ago(startMsAgo - spanMs) : ago(startMsAgo), ...work, }); const turnItems = [ item("run-1", 26 * MIN, { type: "user_message", createdBy: "user", creationSource: "web", messageId: "msg-user-1", inputIntent: "turn_start", text: USER_1, attachments: [] }, "completed", 0), item("run-1", 25 * MIN + 50_000, { type: "reasoning", text: "The build needs to inline the vendored packages and keep the symlink step for local development.", streaming: false }), item("run-1", 25 * MIN + 20_000, { type: "file_search", pattern: "Bun.build", results: [{ fileName: "scripts/build.ts" }, { fileName: "package.json" }] }), item("run-1", 24 * MIN + 40_000, { type: "file_change", fileName: "scripts/build.ts", additions: 42, deletions: 6 }), item("run-1", 24 * MIN, { type: "command_execution", input: "bun run typecheck", output: "tsc --noEmit\n(no errors)", exitCode: 0 }, "completed", 20_000), item("run-1", 23 * MIN + 20_000, { type: "command_execution", input: "bun test", output: "128 pass\n0 fail\n412 expect() calls", exitCode: 0 }, "completed", 30_000), // 3m 12s after the first user message: the turn folds to "Worked for 3m 12s". item("run-1", 22 * MIN + 48_000, { type: "assistant_message", messageId: "msg-assistant-1", text: ASSISTANT_1, streaming: false }, "completed", 0), item("run-2", 3 * MIN, { type: "user_message", createdBy: "user", creationSource: "web", messageId: "msg-user-2", inputIntent: "turn_start", text: USER_2, attachments: [] }, "completed", 0), item("run-2", 2 * MIN + 50_000, { type: "reasoning", text: "A clean rebuild means removing dist/ first, then re-running the bundle and smoke-testing the binary.", streaming: false }), item("run-2", 2 * MIN + 30_000, { type: "command_execution", input: "git status --porcelain", output: " M scripts/build.ts\n M package.json", exitCode: 0 }, "completed", 3_000), item("run-2", 40_000, { type: "approval_request", requestId: APPROVAL_ID, requestKind: "command", prompt: "rm -rf dist && bun run scripts/build.ts", reason: "Deletes dist/ and rebuilds it from a clean tree." }, "waiting", 0), ]; const message = (id: string, runId: string, role: string, text: string, atMsAgo: number) => ({ createdBy: "user", creationSource: "web", id, threadId: OPEN_THREAD, runId, nodeId: null, role, text, attachments: [], streaming: false, createdAt: ago(atMsAgo), updatedAt: ago(atMsAgo), }); const projection = { ...v2Projection, thread: { ...v2Projection.thread, id: OPEN_THREAD, projectId: "project-t3tui", title: "Bundle into dist/", runtimeMode: "approval-required", createdAt: ago(2 * DAY), updatedAt: ago(40_000), }, runs: [run("run-1", 1, "msg-user-1", "completed", 26 * MIN, 22 * MIN + 48_000), run("run-2", 2, "msg-user-2", "running", 3 * MIN, null)], runtimeRequests: [ { id: APPROVAL_ID, nodeId: "node-1", providerTurnId: null, nativeRequestRef: null, kind: "command", status: "pending", responseCapability: { type: "live", providerSessionId: "session-1" }, createdAt: ago(40_000), resolvedAt: null }, ], messages: [ message("msg-user-1", "run-1", "user", USER_1, 26 * MIN), message("msg-assistant-1", "run-1", "assistant", ASSISTANT_1, 22 * MIN + 48_000), message("msg-user-2", "run-2", "user", USER_2, 3 * MIN), ], turnItems, visibleTurnItems: turnItems.map((entry, position) => ({ position, visibility: "local", sourceThreadId: OPEN_THREAD, sourceItemId: entry.id, item: entry })), updatedAt: ago(40_000), } as unknown as OrchestrationV2ThreadProjection; // --- Captured frame -> HTML --------------------------------------------------- function hex(color: RGBA): string { const [r, g, b] = color.toInts(); return `#${[r, g, b].map((value) => Math.max(0, Math.min(255, value)).toString(16).padStart(2, "0")).join("")}`; } function escapeHtml(text: string): string { return text.replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">").replaceAll('"', """); } interface StyledSpan { readonly text: string; readonly style: string; } /** One frame as a `
`, one line per row, styled spans merged. */
function frameToHtml(frame: CapturedFrame, baseBg: string): string {
  const lines = frame.lines.map((line) => {
    const styled: StyledSpan[] = [];
    for (const span of line.spans) {
      if (!span.text) continue;
      const attrs = span.attributes & 0xff;
      const inverse = (attrs & TextAttributes.INVERSE) !== 0;
      const fg = hex(inverse ? span.bg : span.fg);
      const bg = hex(inverse ? span.fg : span.bg);
      const declarations = [`color:${fg}`];
      if (bg !== baseBg) declarations.push(`background:${bg}`);
      if ((attrs & TextAttributes.BOLD) !== 0) declarations.push("font-weight:bold");
      if ((attrs & TextAttributes.ITALIC) !== 0) declarations.push("font-style:italic");
      const decorations = [
        (attrs & TextAttributes.UNDERLINE) !== 0 ? "underline" : "",
        (attrs & TextAttributes.STRIKETHROUGH) !== 0 ? "line-through" : "",
      ].filter(Boolean);
      if (decorations.length > 0) declarations.push(`text-decoration:${decorations.join(" ")}`);
      if ((attrs & TextAttributes.DIM) !== 0) declarations.push("opacity:.65");
      const style = declarations.join(";");
      const last = styled.at(-1);
      if (last && last.style === style) styled[styled.length - 1] = { text: last.text + span.text, style };
      else styled.push({ text: span.text, style });
    }
    // Trailing blank spans in the base colour add nothing over the pre's background.
    while (styled.length > 0) {
      const last = styled.at(-1)!;
      if (last.text.trim() !== "" || last.style.includes("background:")) break;
      styled.pop();
    }
    return styled.map((span) => `${escapeHtml(span.text)}`).join("");
  });
  return `
${lines.join("\n")}\n
\n`; } function standalonePage(fragment: string, pageBg: string): string { return ` t3tui — demo screen ${fragment} `; } // --- Render both themes --------------------------------------------------------- async function capture(mode: "dark" | "light"): Promise<{ frame: CapturedFrame; chars: string; baseBg: string }> { applyTheme(mode); const app = await renderApp({ session: fakeSession({ snapshot, projection }), width: 140, height: 34, }); try { // A running run ticks a spinner and a duration, so settle() gives up after // its retries; one more short wait lands on whatever frame is current. await app.settle(120); await app.renderOnce(); return { frame: app.captureSpans(), chars: app.frame(), baseBg: colors.bg }; } finally { app.destroy(); } } await mkdir(OUT_DIR, { recursive: true }); const dark = await capture("dark"); console.log(dark.chars); const darkFragment = frameToHtml(dark.frame, dark.baseBg); await writeFile(join(OUT_DIR, "demo-screen.html"), darkFragment); await writeFile(join(OUT_DIR, "demo-screen-page.html"), standalonePage(darkFragment, "#161616")); const light = await capture("light"); const lightFragment = frameToHtml(light.frame, light.baseBg); await writeFile(join(OUT_DIR, "demo-screen-light.html"), lightFragment); await writeFile(join(OUT_DIR, "demo-screen-light-page.html"), standalonePage(lightFragment, "#e8e8e8")); console.log(`wrote ${join(OUT_DIR, "demo-screen.html")}`); console.log(`wrote ${join(OUT_DIR, "demo-screen-page.html")}`); console.log(`wrote ${join(OUT_DIR, "demo-screen-light.html")}`); process.exit(0);