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:
1 parent
3b7ed56e16
commit
cea1409f40
9 files changed
+500
-212
No files matched your search
+172
-123
@@ -1,58 +1,83 @@
|
||||
import type { InputRenderable, ScrollBoxRenderable } from "@opentui/core";
|
||||
import type { KeyEvent, ScrollBoxRenderable, TextareaRenderable } from "@opentui/core";
|
||||
import { useKeyboard, useRenderer, useTerminalDimensions } from "@opentui/react";
|
||||
import type { ProviderApprovalDecision, ThreadId } from "@t3tools/contracts";
|
||||
import { useEffect, useMemo, useRef, useState } from "react";
|
||||
|
||||
import type { ShellState, T3Session, ThreadState } from "../client/session.ts";
|
||||
import { buildTranscript } from "../model/transcript.ts";
|
||||
import { lastActivity, relativeTime, threadRows } from "../model/threads.ts";
|
||||
import { buildTranscript, type Transcript } from "../model/transcript.ts";
|
||||
import { isRunning, lastActivity, needsUser, relativeTime, threadRows } from "../model/threads.ts";
|
||||
import { colors } from "./theme.ts";
|
||||
import { ThreadList } from "./ThreadList.tsx";
|
||||
import { ThreadView } from "./ThreadView.tsx";
|
||||
|
||||
type Focus = "list" | "thread" | "composer" | "filter";
|
||||
/** "input": typing goes to the composer. "nav": single keys move, scroll, approve. */
|
||||
type Mode = "input" | "nav" | "filter";
|
||||
|
||||
/** Holding j/k skims past threads; only subscribe to one you stop on. */
|
||||
const SUBSCRIBE_DELAY_MS = 120;
|
||||
const MAX_COMPOSER_LINES = 6;
|
||||
|
||||
const COMPOSER_KEYS = [
|
||||
{ name: "return", action: "submit" },
|
||||
{ name: "return", shift: true, action: "newline" },
|
||||
{ name: "return", meta: true, action: "newline" },
|
||||
{ name: "j", ctrl: true, action: "newline" },
|
||||
] as const;
|
||||
|
||||
export function App(props: { readonly session: T3Session; readonly attachThreadId?: ThreadId }) {
|
||||
const { session, attachThreadId } = props;
|
||||
const renderer = useRenderer();
|
||||
const { width, height } = useTerminalDimensions();
|
||||
const [shell, setShell] = useState<ShellState>(session.getShell());
|
||||
const [selected, setSelected] = useState(0);
|
||||
const [focus, setFocus] = useState<Focus>(attachThreadId ? "thread" : "list");
|
||||
const [selectedId, setSelectedId] = useState<ThreadId | undefined>(attachThreadId);
|
||||
const [mode, setMode] = useState<Mode>(attachThreadId ? "input" : "nav");
|
||||
const [filter, setFilter] = useState("");
|
||||
const [showReasoning, setShowReasoning] = useState(false);
|
||||
const [expandWork, setExpandWork] = useState(false);
|
||||
const [flash, setFlash] = useState<{ text: string; bad?: boolean } | null>(null);
|
||||
const [draft, setDraft] = useState("");
|
||||
const [composerLines, setComposerLines] = useState(1);
|
||||
const scrollRef = useRef<ScrollBoxRenderable | null>(null);
|
||||
const composerRef = useRef<InputRenderable | null>(null);
|
||||
const composerRef = useRef<TextareaRenderable | null>(null);
|
||||
const drafts = useRef(new Map<string, string>());
|
||||
|
||||
useEffect(() => session.onShell(setShell), [session]);
|
||||
|
||||
const rows = useMemo(() => threadRows(shell.snapshot, { query: filter }), [shell.snapshot, filter]);
|
||||
const selectedRow = rows[Math.min(selected, rows.length - 1)];
|
||||
const threadId = attachThreadId ?? selectedRow?.thread.id;
|
||||
// Selection is a thread id, not an index, so the list can reorder underneath it.
|
||||
const selectedIndex = Math.max(0, rows.findIndex((row) => row.thread.id === selectedId));
|
||||
const threadId = attachThreadId ?? rows[selectedIndex]?.thread.id;
|
||||
const threadShell = shell.snapshot?.threads.find((thread) => thread.id === threadId);
|
||||
// Pin the initial selection to a thread, or it would follow whatever sorts first.
|
||||
useEffect(() => {
|
||||
if (selectedId === undefined && rows[0]) setSelectedId(rows[0].thread.id);
|
||||
}, [selectedId, rows]);
|
||||
const move = (delta: number) => {
|
||||
const next = rows[Math.min(rows.length - 1, Math.max(0, selectedIndex + delta))];
|
||||
if (next) setSelectedId(next.thread.id);
|
||||
};
|
||||
|
||||
// Keep the selection on the same thread when the list reorders under it.
|
||||
const selectedIdRef = useRef<string | undefined>(undefined);
|
||||
const [watchedId, setWatchedId] = useState(threadId);
|
||||
useEffect(() => {
|
||||
const id = selectedIdRef.current;
|
||||
if (!id) return;
|
||||
const index = rows.findIndex((row) => row.thread.id === id);
|
||||
if (index >= 0 && index !== selected) setSelected(index);
|
||||
}, [rows]);
|
||||
useEffect(() => {
|
||||
selectedIdRef.current = rows[selected]?.thread.id;
|
||||
}, [selected, rows]);
|
||||
if (threadId === watchedId) return;
|
||||
const timer = setTimeout(() => setWatchedId(threadId), SUBSCRIBE_DELAY_MS);
|
||||
return () => clearTimeout(timer);
|
||||
}, [threadId, watchedId]);
|
||||
|
||||
const [thread, setThread] = useState<ThreadState | null>(null);
|
||||
useEffect(() => {
|
||||
setThread(null);
|
||||
if (!threadId || shell.status.type === "error" || !shell.serverUrl) return;
|
||||
return session.watchThread(threadId, setThread);
|
||||
}, [session, threadId, shell.serverUrl]);
|
||||
if (!watchedId || !shell.serverUrl) return;
|
||||
return session.watchThread(watchedId, setThread);
|
||||
}, [session, watchedId, shell.serverUrl]);
|
||||
|
||||
const transcript = useMemo(() => (thread?.projection ? buildTranscript(thread.projection) : null), [thread?.projection]);
|
||||
const running = threadShell ? isRunning(threadShell) && !needsUser(threadShell) : false;
|
||||
const previousTranscript = useRef<{ id: string; transcript: Transcript } | null>(null);
|
||||
const transcript = useMemo(() => {
|
||||
if (!thread?.projection || !watchedId) return null;
|
||||
const previous = previousTranscript.current?.id === watchedId ? previousTranscript.current.transcript : null;
|
||||
const next = buildTranscript(thread.projection, { running, previous });
|
||||
previousTranscript.current = { id: watchedId, transcript: next };
|
||||
return next;
|
||||
}, [thread?.projection, watchedId, running]);
|
||||
const pendingRequest = thread?.projection?.runtimeRequests.findLast((request) => request.status === "pending" && request.kind !== "user_input");
|
||||
|
||||
const say = (text: string, bad = false) => {
|
||||
@@ -65,76 +90,21 @@ export function App(props: { readonly session: T3Session; readonly attachThreadI
|
||||
(error) => say(`${label} failed: ${error instanceof Error ? error.message : String(error)}`, true),
|
||||
);
|
||||
};
|
||||
|
||||
const respond = (decision: ProviderApprovalDecision) => {
|
||||
if (!threadId || !pendingRequest) return say("Nothing is waiting for approval.", true);
|
||||
run(decision === "decline" ? "Declined" : "Approved", () => session.respond(threadId, pendingRequest.id, decision));
|
||||
};
|
||||
|
||||
const submitDraft = (value: string) => {
|
||||
const text = value.trim();
|
||||
if (!text || !threadId) return;
|
||||
setDraft("");
|
||||
if (composerRef.current) composerRef.current.value = "";
|
||||
setFocus("thread");
|
||||
run("Sent", () => session.sendMessage(threadId, text));
|
||||
const submit = () => {
|
||||
const composer = composerRef.current;
|
||||
const text = composer?.plainText.trim() ?? "";
|
||||
if (!composer || !text || !threadId) return;
|
||||
composer.setText("");
|
||||
drafts.current.delete(threadId);
|
||||
setComposerLines(1);
|
||||
run(running ? "Queued for after the current run" : "Sent", () => session.sendMessage(threadId, text));
|
||||
};
|
||||
|
||||
useKeyboard((key) => {
|
||||
if (key.ctrl && key.name === "c") return quit();
|
||||
if (focus === "composer" || focus === "filter") {
|
||||
if (key.name === "escape") setFocus(attachThreadId ? "thread" : focus === "filter" ? "list" : "thread");
|
||||
return;
|
||||
}
|
||||
const scroll = scrollRef.current;
|
||||
switch (key.name) {
|
||||
case "q":
|
||||
return quit();
|
||||
case "j":
|
||||
case "down":
|
||||
if (focus === "list") return setSelected((index) => Math.min(index + 1, rows.length - 1));
|
||||
return scroll?.scrollBy(1);
|
||||
case "k":
|
||||
case "up":
|
||||
if (focus === "list") return setSelected((index) => Math.max(index - 1, 0));
|
||||
return scroll?.scrollBy(-1);
|
||||
case "pagedown":
|
||||
return scroll?.scrollBy(Math.max(1, height - 8));
|
||||
case "pageup":
|
||||
return scroll?.scrollBy(-Math.max(1, height - 8));
|
||||
case "g":
|
||||
if (key.shift) return scroll?.scrollTo(scroll.scrollHeight);
|
||||
return scroll?.scrollTo(0);
|
||||
case "return":
|
||||
case "tab":
|
||||
case "l":
|
||||
case "right":
|
||||
if (!attachThreadId) setFocus(focus === "list" ? "thread" : "list");
|
||||
return;
|
||||
case "h":
|
||||
case "left":
|
||||
case "escape":
|
||||
if (!attachThreadId) setFocus("list");
|
||||
return;
|
||||
case "i":
|
||||
case "m":
|
||||
if (threadId) setFocus("composer");
|
||||
return;
|
||||
case "/":
|
||||
if (!attachThreadId) setFocus("filter");
|
||||
return;
|
||||
case "r":
|
||||
return setShowReasoning((value) => !value);
|
||||
case "y":
|
||||
return respond(key.shift ? "acceptForSession" : "accept");
|
||||
case "n":
|
||||
return respond("decline");
|
||||
case "s":
|
||||
if (threadId && key.shift) run("Stop requested", () => session.stopThread(threadId));
|
||||
return;
|
||||
}
|
||||
});
|
||||
|
||||
const quit = () => {
|
||||
void session.stop().finally(() => {
|
||||
renderer.destroy();
|
||||
@@ -142,22 +112,75 @@ export function App(props: { readonly session: T3Session; readonly attachThreadI
|
||||
});
|
||||
};
|
||||
|
||||
const scrollPage = (direction: 1 | -1) => scrollRef.current?.scrollBy(direction * Math.max(1, height - 10));
|
||||
|
||||
/** Keys that work in every mode. Returns true when handled. */
|
||||
const globalKey = (key: KeyEvent): boolean => {
|
||||
if (key.ctrl && key.name === "c") return quit(), true;
|
||||
if (key.name === "pagedown" || (key.ctrl && key.name === "d")) return scrollPage(1), true;
|
||||
if (key.name === "pageup" || (key.ctrl && key.name === "u")) return scrollPage(-1), true;
|
||||
if (!attachThreadId && ((key.ctrl && key.name === "n") || (key.meta && key.name === "down"))) return move(1), true;
|
||||
if (!attachThreadId && ((key.ctrl && key.name === "p") || (key.meta && key.name === "up"))) return move(-1), true;
|
||||
return false;
|
||||
};
|
||||
|
||||
useKeyboard((key) => {
|
||||
if (globalKey(key)) return key.preventDefault();
|
||||
if (mode === "input" || mode === "filter") {
|
||||
if (key.name === "escape") {
|
||||
setMode("nav");
|
||||
key.preventDefault();
|
||||
}
|
||||
return;
|
||||
}
|
||||
const scroll = scrollRef.current;
|
||||
switch (key.name) {
|
||||
case "q":
|
||||
return quit();
|
||||
case "j":
|
||||
case "down":
|
||||
return attachThreadId ? scroll?.scrollBy(1) : move(1);
|
||||
case "k":
|
||||
case "up":
|
||||
return attachThreadId ? scroll?.scrollBy(-1) : move(-1);
|
||||
case "g":
|
||||
return key.shift ? scroll?.scrollTo(scroll.scrollHeight) : scroll?.scrollTo(0);
|
||||
case "e":
|
||||
return setExpandWork((value) => !value);
|
||||
case "y":
|
||||
return respond(key.shift ? "acceptForSession" : "accept");
|
||||
case "n":
|
||||
return respond("decline");
|
||||
case "s":
|
||||
if (threadId && key.shift) run("Stop requested", () => session.stopThread(threadId));
|
||||
return;
|
||||
case "/":
|
||||
if (!attachThreadId) setMode("filter");
|
||||
return key.preventDefault();
|
||||
case "i":
|
||||
case "return":
|
||||
case "tab":
|
||||
if (threadId) setMode("input");
|
||||
return key.preventDefault();
|
||||
}
|
||||
});
|
||||
|
||||
const counts = useMemo(() => {
|
||||
let needs = 0;
|
||||
let running = 0;
|
||||
let active = 0;
|
||||
for (const row of threadRows(shell.snapshot, { recentLimit: 0 })) {
|
||||
if (row.group === "needs-you") needs++;
|
||||
else if (row.group === "running") running++;
|
||||
else if (row.group === "running") active++;
|
||||
}
|
||||
return { needs, running };
|
||||
return { needs, active };
|
||||
}, [shell.snapshot]);
|
||||
|
||||
const status =
|
||||
shell.status.type === "error" ? `error: ${shell.status.message}` : shell.status.type === "connecting" ? "connecting…" : shell.serverUrl ?? "";
|
||||
const listWidth = attachThreadId ? 0 : Math.min(48, Math.max(28, Math.floor(width * 0.32)));
|
||||
const status = shell.status.type === "error" ? `error: ${shell.status.message}` : shell.status.type === "connecting" ? "connecting…" : (shell.serverUrl ?? "");
|
||||
const listWidth = attachThreadId ? 0 : Math.min(46, Math.max(28, Math.floor(width * 0.3)));
|
||||
const subtitle = threadShell
|
||||
? [threadShell.status, threadShell.modelSelection.model, threadShell.branch, relativeTime(lastActivity(threadShell))].filter(Boolean).join(" · ")
|
||||
? [threadShell.modelSelection.model === "default" ? "" : threadShell.modelSelection.model, threadShell.branch, relativeTime(lastActivity(threadShell))].filter(Boolean).join(" · ")
|
||||
: "";
|
||||
const composerHeight = Math.min(MAX_COMPOSER_LINES, composerLines) + 2;
|
||||
|
||||
return (
|
||||
<box flexDirection="column" width={width} height={height} backgroundColor={colors.bg}>
|
||||
@@ -165,58 +188,84 @@ export function App(props: { readonly session: T3Session; readonly attachThreadI
|
||||
<text fg={colors.accent}>
|
||||
<strong>tui3</strong>
|
||||
</text>
|
||||
<text fg={colors.muted}>{` ${status}`}</text>
|
||||
<text fg={colors.faint}>{` ${status}`}</text>
|
||||
<box flexGrow={1} />
|
||||
{counts.needs > 0 ? <text fg={colors.warn}>{`● ${counts.needs} need you `}</text> : null}
|
||||
<text fg={colors.accent}>{`◐ ${counts.running} running`}</text>
|
||||
{counts.needs > 0 ? <text fg={colors.warn}>{`● ${counts.needs} need you `}</text> : null}
|
||||
<text fg={colors.muted}>{`◐ ${counts.active} running`}</text>
|
||||
</box>
|
||||
<box flexDirection="row" flexGrow={1} flexShrink={1} minHeight={0} overflow="hidden">
|
||||
{attachThreadId ? null : <ThreadList rows={rows} selected={selected} focused={focus === "list"} width={listWidth} height={height - 4} filter={filter} />}
|
||||
{attachThreadId ? null : <ThreadList rows={rows} selected={selectedIndex} focused={mode === "nav"} width={listWidth} height={height - 2} filter={filter} />}
|
||||
<box flexDirection="column" flexGrow={1} minHeight={0}>
|
||||
{threadId ? (
|
||||
<ThreadView
|
||||
title={threadShell?.title ?? "thread"}
|
||||
subtitle={subtitle}
|
||||
transcript={transcript}
|
||||
loading={thread?.status === "loading"}
|
||||
transcript={watchedId === threadId ? transcript : null}
|
||||
loading={watchedId !== threadId || thread?.status === "loading"}
|
||||
error={thread?.error}
|
||||
focused={focus === "thread"}
|
||||
showReasoning={showReasoning}
|
||||
working={running}
|
||||
expandWork={expandWork}
|
||||
scrollRef={scrollRef}
|
||||
/>
|
||||
) : (
|
||||
<box border borderColor={colors.border} flexGrow={1}>
|
||||
<text fg={colors.muted}>{shell.snapshot ? "Select a thread." : "Waiting for threads…"}</text>
|
||||
<box flexGrow={1} paddingLeft={2} paddingTop={1}>
|
||||
<text fg={colors.muted}>{shell.snapshot ? "No thread selected." : "Waiting for threads…"}</text>
|
||||
</box>
|
||||
)}
|
||||
{focus === "composer" ? (
|
||||
<box title=" message (enter to send, esc to cancel) " border borderColor={colors.borderFocused} height={3} flexShrink={0}>
|
||||
<input ref={composerRef} focused placeholder="Queue a message for this thread…" value={draft} onInput={setDraft} onSubmit={(value: unknown) => submitDraft(typeof value === "string" ? value : draft)} />
|
||||
{mode === "filter" ? (
|
||||
<box title=" filter " border borderStyle="rounded" borderColor={colors.borderFocused} height={3} flexShrink={0} marginLeft={1} marginRight={1}>
|
||||
<input focused placeholder="title or project…" value={filter} onInput={(value: string) => setFilter(value)} onSubmit={() => setMode("nav")} />
|
||||
</box>
|
||||
) : null}
|
||||
{focus === "filter" ? (
|
||||
<box title=" filter threads (esc to close) " border borderColor={colors.borderFocused} height={3} flexShrink={0}>
|
||||
<input focused placeholder="title or project…" value={filter} onInput={(value: string) => { setFilter(value); setSelected(0); }} onSubmit={() => setFocus("list")} />
|
||||
) : threadId ? (
|
||||
<box
|
||||
border
|
||||
borderStyle="rounded"
|
||||
borderColor={mode === "input" ? colors.borderFocused : colors.border}
|
||||
height={composerHeight}
|
||||
flexShrink={0}
|
||||
marginLeft={1}
|
||||
marginRight={1}
|
||||
paddingLeft={1}
|
||||
paddingRight={1}
|
||||
>
|
||||
<textarea
|
||||
key={threadId}
|
||||
ref={composerRef}
|
||||
focused={mode === "input"}
|
||||
initialValue={drafts.current.get(threadId) ?? ""}
|
||||
placeholder={mode === "input" ? (running ? "Message (queued until the current run finishes)" : "Message") : "Press i to type"}
|
||||
placeholderColor={colors.faint}
|
||||
textColor={colors.text}
|
||||
keyBindings={[...COMPOSER_KEYS]}
|
||||
onSubmit={submit}
|
||||
onContentChange={() => {
|
||||
const text = composerRef.current?.plainText ?? "";
|
||||
if (text) drafts.current.set(threadId, text);
|
||||
else drafts.current.delete(threadId);
|
||||
setComposerLines(Math.max(1, text.split("\n").length));
|
||||
}}
|
||||
/>
|
||||
</box>
|
||||
) : null}
|
||||
</box>
|
||||
</box>
|
||||
<box flexDirection="row" paddingLeft={1} height={1} flexShrink={0}>
|
||||
{flash ? (
|
||||
<text fg={flash.bad ? colors.bad : colors.good}>{flash.text}</text>
|
||||
) : (
|
||||
<text fg={colors.faint}>{hints(focus, Boolean(pendingRequest), Boolean(attachThreadId))}</text>
|
||||
)}
|
||||
<box flexDirection="row" paddingLeft={2} height={1} flexShrink={0}>
|
||||
{flash ? <text fg={flash.bad ? colors.bad : colors.good}>{flash.text}</text> : <text fg={colors.faint}>{hints(mode, Boolean(pendingRequest), Boolean(attachThreadId))}</text>}
|
||||
</box>
|
||||
</box>
|
||||
);
|
||||
}
|
||||
|
||||
function hints(focus: Focus, pending: boolean, attached: boolean): string {
|
||||
if (focus === "composer" || focus === "filter") return "enter submit · esc cancel";
|
||||
const parts = attached ? ["j/k scroll", "g/G top/bottom"] : focus === "list" ? ["j/k move", "enter open", "/ filter"] : ["j/k scroll", "esc list", "g/G top/bottom"];
|
||||
parts.push("i message", "r reasoning", "S stop");
|
||||
function hints(mode: Mode, pending: boolean, attached: boolean): string {
|
||||
if (mode === "filter") return "enter done · esc close";
|
||||
if (mode === "input") {
|
||||
const parts = ["enter send", "shift+enter newline", "esc navigate"];
|
||||
if (!attached) parts.push("ctrl+n/p switch thread");
|
||||
if (pending) parts.unshift("approval waiting: esc, then y/n");
|
||||
return parts.join(" · ");
|
||||
}
|
||||
const parts = attached ? ["j/k scroll"] : ["j/k threads", "/ filter"];
|
||||
parts.push("i type", "e expand work", "pgup/pgdn scroll", "S stop", "q quit");
|
||||
if (pending) parts.unshift("y accept · Y session · n decline");
|
||||
parts.push("q quit");
|
||||
return parts.join(" · ");
|
||||
}
|
||||
@@ -18,7 +18,7 @@ function glyphFor(row: ThreadRow) {
|
||||
export function ThreadList(props: { readonly rows: readonly ThreadRow[]; readonly selected: number; readonly focused: boolean; readonly width: number; readonly height: number; readonly filter: string }) {
|
||||
const { rows, selected, focused, width, height } = props;
|
||||
// Rows take two lines and up to three group headers take one each; keep the selection in view.
|
||||
const visibleCount = Math.max(1, Math.floor((height - 2 - 3) / 2));
|
||||
const visibleCount = Math.max(1, Math.floor((height - 3) / 2));
|
||||
const start = Math.max(0, Math.min(selected - Math.floor(visibleCount / 2), rows.length - visibleCount));
|
||||
const windowRows = rows.slice(start, start + visibleCount);
|
||||
const titleWidth = Math.max(8, width - 12);
|
||||
@@ -50,15 +50,15 @@ export function ThreadList(props: { readonly rows: readonly ThreadRow[]; readonl
|
||||
});
|
||||
return (
|
||||
<box
|
||||
title={props.filter ? ` threads /${props.filter} ` : " threads "}
|
||||
border
|
||||
borderColor={focused ? colors.borderFocused : colors.border}
|
||||
border={["right"]}
|
||||
borderColor={colors.border}
|
||||
width={width}
|
||||
flexShrink={0}
|
||||
overflow="hidden"
|
||||
flexDirection="column"
|
||||
paddingLeft={0}
|
||||
>
|
||||
{props.filter ? <text fg={colors.accent}>{` /${props.filter}`}</text> : null}
|
||||
{rows.length === 0 ? <text fg={colors.muted}> no threads</text> : lines}
|
||||
</box>
|
||||
);
|
||||
|
||||
+94
-54
@@ -1,7 +1,7 @@
|
||||
import type { ScrollBoxRenderable } from "@opentui/core";
|
||||
import { useMemo, type RefObject } from "react";
|
||||
import { memo, type RefObject } from "react";
|
||||
|
||||
import type { Block, Transcript } from "../model/transcript.ts";
|
||||
import { summarizeWork, type Segment, type Step, type Transcript } from "../model/transcript.ts";
|
||||
import { colors, getMarkdownStyle, toneColor } from "./theme.ts";
|
||||
|
||||
export function ThreadView(props: {
|
||||
@@ -10,86 +10,126 @@ export function ThreadView(props: {
|
||||
readonly transcript: Transcript | null;
|
||||
readonly loading: boolean;
|
||||
readonly error?: string | undefined;
|
||||
readonly focused: boolean;
|
||||
readonly showReasoning: boolean;
|
||||
readonly working: boolean;
|
||||
readonly expandWork: boolean;
|
||||
readonly scrollRef: RefObject<ScrollBoxRenderable | null>;
|
||||
}) {
|
||||
const { transcript } = props;
|
||||
const body = useMemo(() => {
|
||||
if (!transcript) return null;
|
||||
return transcript.blocks.map((block) => <BlockView key={block.id} block={block} showReasoning={props.showReasoning} />);
|
||||
}, [transcript, props.showReasoning]);
|
||||
|
||||
const last = transcript?.segments.at(-1);
|
||||
// The folded work line already says it's working; otherwise show it at the bottom.
|
||||
const showWorking = props.working && !(last?.kind === "work" && last.active) && !(last?.kind === "assistant" && last.streaming);
|
||||
return (
|
||||
<box title={` ${props.title} `} border borderColor={props.focused ? colors.borderFocused : colors.border} flexGrow={1} minHeight={0} flexDirection="column">
|
||||
<text fg={colors.faint} flexShrink={0} height={1}>{props.subtitle}</text>
|
||||
{props.error ? <text fg={colors.bad}>{props.error}</text> : null}
|
||||
{props.loading && !transcript ? <text fg={colors.muted}>loading…</text> : null}
|
||||
<scrollbox ref={props.scrollRef} flexGrow={1} flexShrink={1} minHeight={0} stickyScroll stickyStart="bottom" focused={props.focused}>
|
||||
{transcript && transcript.hiddenCount > 0 ? <text fg={colors.faint}>{`… ${transcript.hiddenCount} earlier items not shown`}</text> : null}
|
||||
{body}
|
||||
<box flexGrow={1} minHeight={0} flexDirection="column" paddingLeft={1} paddingRight={1}>
|
||||
<box flexDirection="row" flexShrink={0} height={1}>
|
||||
<text fg={colors.text}>
|
||||
<strong>{props.title}</strong>
|
||||
</text>
|
||||
<text fg={colors.faint}>{props.subtitle ? ` ${props.subtitle}` : ""}</text>
|
||||
</box>
|
||||
{props.error ? <text fg={colors.bad} flexShrink={0}>{props.error}</text> : null}
|
||||
<scrollbox ref={props.scrollRef} flexGrow={1} flexShrink={1} minHeight={0} stickyScroll stickyStart="bottom">
|
||||
{props.loading && !transcript ? <text fg={colors.muted}>loading…</text> : null}
|
||||
{transcript && transcript.hiddenCount > 0 ? <text fg={colors.faint}>{`… ${transcript.hiddenCount} earlier items`}</text> : null}
|
||||
{transcript?.segments.map((segment) => <SegmentView key={segment.id} segment={segment} expandWork={props.expandWork} />)}
|
||||
{showWorking ? <text fg={colors.accent} marginTop={1}>◐ working…</text> : null}
|
||||
</scrollbox>
|
||||
</box>
|
||||
);
|
||||
}
|
||||
|
||||
function BlockView({ block, showReasoning }: { readonly block: Block; readonly showReasoning: boolean }) {
|
||||
switch (block.kind) {
|
||||
case "user":
|
||||
/** Memoized: unchanged segments keep their object identity across updates, so only what changed re-renders. */
|
||||
const SegmentView = memo(function SegmentView({ segment, expandWork }: { readonly segment: Segment; readonly expandWork: boolean }) {
|
||||
switch (segment.kind) {
|
||||
case "user": {
|
||||
const { text, hidden } = expandWork ? { text: segment.text.trim(), hidden: 0 } : clip(segment.text.trim());
|
||||
return (
|
||||
<box marginTop={1} flexDirection="row">
|
||||
<text fg={colors.user}>› </text>
|
||||
<text fg={colors.user}>
|
||||
<strong>{block.text.trim()}</strong>
|
||||
</text>
|
||||
<box marginTop={1} border={["left"]} borderColor={colors.user} backgroundColor={colors.userBg} paddingLeft={1} paddingRight={1} flexDirection="column">
|
||||
<text fg={colors.text}>{text}</text>
|
||||
{hidden > 0 ? <text fg={colors.faint}>{`… ${hidden} more lines (e to expand)`}</text> : null}
|
||||
</box>
|
||||
);
|
||||
}
|
||||
case "assistant":
|
||||
return (
|
||||
<box marginTop={1}>
|
||||
<markdown content={block.markdown || "…"} syntaxStyle={getMarkdownStyle()} streaming={block.streaming} conceal />
|
||||
</box>
|
||||
);
|
||||
case "reasoning":
|
||||
if (!showReasoning) return <text fg={colors.faint}>{` ∴ ${firstLine(block.text)}`}</text>;
|
||||
return (
|
||||
<text fg={colors.faint}>
|
||||
<i>{block.text.trim()}</i>
|
||||
</text>
|
||||
);
|
||||
case "tool":
|
||||
return (
|
||||
<box flexDirection="column">
|
||||
<text fg={toneColor[block.tone]}>{` ${block.icon} ${block.label}`}</text>
|
||||
{block.detail ? <text fg={colors.faint}>{indent(block.detail, " ")}</text> : null}
|
||||
<box marginTop={1} paddingLeft={1}>
|
||||
<markdown content={segment.markdown || "…"} syntaxStyle={getMarkdownStyle()} streaming={segment.streaming} conceal />
|
||||
</box>
|
||||
);
|
||||
case "work":
|
||||
return expandWork ? <ExpandedWork steps={segment.steps} /> : <FoldedWork steps={segment.steps} active={segment.active} />;
|
||||
case "approval":
|
||||
return (
|
||||
<box marginTop={1} border borderColor={block.pending ? colors.warn : colors.border} flexDirection="column" paddingLeft={1}>
|
||||
<text fg={block.pending ? colors.warn : colors.muted}>
|
||||
<strong>{block.pending ? "Approval needed: " : "Approval: "}</strong>
|
||||
{block.title}
|
||||
<box marginTop={1} border borderStyle="rounded" borderColor={segment.pending ? colors.warn : colors.border} flexDirection="column" paddingLeft={1} paddingRight={1}>
|
||||
<text fg={segment.pending ? colors.warn : colors.muted}>
|
||||
<strong>{segment.pending ? "Needs approval" : "Approval"}</strong>
|
||||
</text>
|
||||
{block.detail ? <text fg={colors.muted}>{block.detail}</text> : null}
|
||||
{block.pending ? <text fg={colors.text}>[y] accept [Y] accept for session [n] decline</text> : null}
|
||||
<text fg={colors.text}>{segment.title}</text>
|
||||
{segment.detail ? <text fg={colors.muted}>{segment.detail}</text> : null}
|
||||
{segment.pending ? <text fg={colors.faint}>esc, then y accept · Y accept for session · n decline</text> : null}
|
||||
</box>
|
||||
);
|
||||
case "question":
|
||||
return (
|
||||
<box marginTop={1} border borderColor={block.pending ? colors.warn : colors.border} flexDirection="column" paddingLeft={1}>
|
||||
<text fg={block.pending ? colors.warn : colors.muted}>
|
||||
<strong>{block.pending ? "Question for you" : "Question"}</strong>
|
||||
<box marginTop={1} border borderStyle="rounded" borderColor={segment.pending ? colors.warn : colors.border} flexDirection="column" paddingLeft={1} paddingRight={1}>
|
||||
<text fg={segment.pending ? colors.warn : colors.muted}>
|
||||
<strong>{segment.pending ? "Question for you" : "Question"}</strong>
|
||||
</text>
|
||||
{block.questions.map((question, index) => (
|
||||
<text key={index} fg={colors.text}>{`• ${question}`}</text>
|
||||
{segment.questions.map((question, index) => (
|
||||
<text key={index} fg={colors.text}>{question}</text>
|
||||
))}
|
||||
{block.pending ? <text fg={colors.muted}>Answer in the T3 Code app for now.</text> : null}
|
||||
{segment.pending ? <text fg={colors.faint}>Answer this in the T3 Code app for now.</text> : null}
|
||||
</box>
|
||||
);
|
||||
case "notice":
|
||||
return <text fg={toneColor[block.tone]}>{` ${block.text}`}</text>;
|
||||
case "alert":
|
||||
return <text marginTop={1} paddingLeft={1} fg={toneColor[segment.tone]}>{segment.text}</text>;
|
||||
}
|
||||
});
|
||||
|
||||
function FoldedWork({ steps, active }: { readonly steps: readonly Step[]; readonly active: boolean }) {
|
||||
const latest = active ? steps.at(-1) : undefined;
|
||||
return (
|
||||
<box marginTop={1} paddingLeft={1} flexDirection="column">
|
||||
<text fg={active ? colors.accent : colors.faint}>{`${active ? "◐" : "▸"} ${summarizeWork(steps)}`}</text>
|
||||
{latest ? <text fg={colors.muted}>{` ${stepLine(latest)}`}</text> : null}
|
||||
</box>
|
||||
);
|
||||
}
|
||||
|
||||
function ExpandedWork({ steps }: { readonly steps: readonly Step[] }) {
|
||||
return (
|
||||
<box marginTop={1} paddingLeft={1} flexDirection="column">
|
||||
{steps.map((step) => (
|
||||
<box key={step.id} flexDirection="column">
|
||||
<text fg={step.kind === "reasoning" ? colors.faint : toneColor[step.tone]}>{stepLine(step)}</text>
|
||||
{step.kind === "tool" && step.detail ? <text fg={colors.faint}>{indent(step.detail, " ")}</text> : null}
|
||||
</box>
|
||||
))}
|
||||
</box>
|
||||
);
|
||||
}
|
||||
|
||||
function stepLine(step: Step): string {
|
||||
switch (step.kind) {
|
||||
case "tool":
|
||||
return `${step.icon} ${step.label}`;
|
||||
case "reasoning":
|
||||
return `∴ ${firstLine(step.text)}`;
|
||||
case "notice":
|
||||
return `· ${step.text}`;
|
||||
}
|
||||
}
|
||||
|
||||
const CLIP_LINES = 6;
|
||||
const CLIP_CHARS = 480;
|
||||
|
||||
/** Long prompts (delegation briefs, pasted logs) shrink to their opening lines. */
|
||||
function clip(text: string): { readonly text: string; readonly hidden: number } {
|
||||
const lines = text.split("\n");
|
||||
let kept = lines.slice(0, CLIP_LINES).join("\n");
|
||||
if (kept.length > CLIP_CHARS) kept = `${kept.slice(0, CLIP_CHARS).trimEnd()}…`;
|
||||
const keptLines = kept.split("\n").length;
|
||||
const hidden = lines.length - keptLines;
|
||||
return kept.length < text.length ? { text: kept, hidden: Math.max(hidden, 1) } : { text, hidden: 0 };
|
||||
}
|
||||
|
||||
function firstLine(text: string): string {
|
||||
|
||||
@@ -12,6 +12,7 @@ export const colors = {
|
||||
faint: "#545c7e",
|
||||
accent: "#7aa2f7",
|
||||
user: "#7dcfff",
|
||||
userBg: "#1f2335",
|
||||
good: "#9ece6a",
|
||||
bad: "#f7768e",
|
||||
warn: "#e0af68",
|
||||
|
||||
Reference in new issue
Block a user