Add dashboard, attach view, list and tmux commands
This commit is contained in:
1 parent
c93244e7f4
commit
3b7ed56e16
15 files changed
+1056
-50
No files matched your search
+222
@@ -0,0 +1,222 @@
|
||||
import type { InputRenderable, ScrollBoxRenderable } 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 { colors } from "./theme.ts";
|
||||
import { ThreadList } from "./ThreadList.tsx";
|
||||
import { ThreadView } from "./ThreadView.tsx";
|
||||
|
||||
type Focus = "list" | "thread" | "composer" | "filter";
|
||||
|
||||
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 [filter, setFilter] = useState("");
|
||||
const [showReasoning, setShowReasoning] = useState(false);
|
||||
const [flash, setFlash] = useState<{ text: string; bad?: boolean } | null>(null);
|
||||
const [draft, setDraft] = useState("");
|
||||
const scrollRef = useRef<ScrollBoxRenderable | null>(null);
|
||||
const composerRef = useRef<InputRenderable | null>(null);
|
||||
|
||||
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;
|
||||
const threadShell = shell.snapshot?.threads.find((thread) => thread.id === threadId);
|
||||
|
||||
// Keep the selection on the same thread when the list reorders under it.
|
||||
const selectedIdRef = useRef<string | undefined>(undefined);
|
||||
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]);
|
||||
|
||||
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]);
|
||||
|
||||
const transcript = useMemo(() => (thread?.projection ? buildTranscript(thread.projection) : null), [thread?.projection]);
|
||||
const pendingRequest = thread?.projection?.runtimeRequests.findLast((request) => request.status === "pending" && request.kind !== "user_input");
|
||||
|
||||
const say = (text: string, bad = false) => {
|
||||
setFlash({ text, bad });
|
||||
setTimeout(() => setFlash((current) => (current?.text === text ? null : current)), 4000);
|
||||
};
|
||||
const run = (label: string, action: () => Promise<void>) => {
|
||||
action().then(
|
||||
() => say(label),
|
||||
(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));
|
||||
};
|
||||
|
||||
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();
|
||||
process.exit(0);
|
||||
});
|
||||
};
|
||||
|
||||
const counts = useMemo(() => {
|
||||
let needs = 0;
|
||||
let running = 0;
|
||||
for (const row of threadRows(shell.snapshot, { recentLimit: 0 })) {
|
||||
if (row.group === "needs-you") needs++;
|
||||
else if (row.group === "running") running++;
|
||||
}
|
||||
return { needs, running };
|
||||
}, [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 subtitle = threadShell
|
||||
? [threadShell.status, threadShell.modelSelection.model, threadShell.branch, relativeTime(lastActivity(threadShell))].filter(Boolean).join(" · ")
|
||||
: "";
|
||||
|
||||
return (
|
||||
<box flexDirection="column" width={width} height={height} backgroundColor={colors.bg}>
|
||||
<box flexDirection="row" paddingLeft={1} paddingRight={1} height={1} flexShrink={0}>
|
||||
<text fg={colors.accent}>
|
||||
<strong>tui3</strong>
|
||||
</text>
|
||||
<text fg={colors.muted}>{` ${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>
|
||||
</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} />}
|
||||
<box flexDirection="column" flexGrow={1} minHeight={0}>
|
||||
{threadId ? (
|
||||
<ThreadView
|
||||
title={threadShell?.title ?? "thread"}
|
||||
subtitle={subtitle}
|
||||
transcript={transcript}
|
||||
loading={thread?.status === "loading"}
|
||||
error={thread?.error}
|
||||
focused={focus === "thread"}
|
||||
showReasoning={showReasoning}
|
||||
scrollRef={scrollRef}
|
||||
/>
|
||||
) : (
|
||||
<box border borderColor={colors.border} flexGrow={1}>
|
||||
<text fg={colors.muted}>{shell.snapshot ? "Select a thread." : "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)} />
|
||||
</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")} />
|
||||
</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>
|
||||
</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");
|
||||
if (pending) parts.unshift("y accept · Y session · n decline");
|
||||
parts.push("q quit");
|
||||
return parts.join(" · ");
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
import { GROUP_LABELS, lastActivity, relativeTime, type ThreadRow } from "../model/threads.ts";
|
||||
import { colors } from "./theme.ts";
|
||||
|
||||
const STATUS_GLYPH: Record<string, { glyph: string; color: string }> = {
|
||||
"needs-you": { glyph: "●", color: colors.warn },
|
||||
running: { glyph: "◐", color: colors.accent },
|
||||
failed: { glyph: "✕", color: colors.bad },
|
||||
interrupted: { glyph: "◌", color: colors.muted },
|
||||
completed: { glyph: "✓", color: colors.faint },
|
||||
idle: { glyph: "·", color: colors.faint },
|
||||
};
|
||||
|
||||
function glyphFor(row: ThreadRow) {
|
||||
if (row.group !== "recent") return STATUS_GLYPH[row.group]!;
|
||||
return STATUS_GLYPH[row.thread.status] ?? STATUS_GLYPH.idle!;
|
||||
}
|
||||
|
||||
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 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);
|
||||
const now = Date.now();
|
||||
const lines: React.ReactNode[] = [];
|
||||
let previousGroup = start > 0 ? rows[start - 1]?.group : undefined;
|
||||
windowRows.forEach((row, offset) => {
|
||||
const index = start + offset;
|
||||
if (row.group !== previousGroup) {
|
||||
lines.push(
|
||||
<text key={`h-${row.group}`} fg={row.group === "needs-you" ? colors.warn : colors.muted}>
|
||||
<strong>{GROUP_LABELS[row.group]}</strong>
|
||||
</text>,
|
||||
);
|
||||
previousGroup = row.group;
|
||||
}
|
||||
const { glyph, color } = glyphFor(row);
|
||||
const isSelected = index === selected;
|
||||
const title = truncate(row.thread.title || "(untitled)", titleWidth);
|
||||
lines.push(
|
||||
<box key={row.thread.id} flexDirection="row" backgroundColor={isSelected ? (focused ? colors.selectedBg : colors.border) : "transparent"}>
|
||||
<text fg={color}>{` ${glyph} `}</text>
|
||||
<box flexGrow={1} flexDirection="column">
|
||||
<text fg={isSelected ? colors.text : colors.text}>{title}</text>
|
||||
<text fg={colors.faint}>{truncate(`${row.projectName} · ${relativeTime(lastActivity(row.thread), now)}`, titleWidth)}</text>
|
||||
</box>
|
||||
</box>,
|
||||
);
|
||||
});
|
||||
return (
|
||||
<box
|
||||
title={props.filter ? ` threads /${props.filter} ` : " threads "}
|
||||
border
|
||||
borderColor={focused ? colors.borderFocused : colors.border}
|
||||
width={width}
|
||||
flexShrink={0}
|
||||
overflow="hidden"
|
||||
flexDirection="column"
|
||||
paddingLeft={0}
|
||||
>
|
||||
{rows.length === 0 ? <text fg={colors.muted}> no threads</text> : lines}
|
||||
</box>
|
||||
);
|
||||
}
|
||||
|
||||
function truncate(text: string, width: number): string {
|
||||
const single = text.replace(/\s+/g, " ");
|
||||
return single.length > width ? `${single.slice(0, Math.max(0, width - 1))}…` : single;
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
import type { ScrollBoxRenderable } from "@opentui/core";
|
||||
import { useMemo, type RefObject } from "react";
|
||||
|
||||
import type { Block, Transcript } from "../model/transcript.ts";
|
||||
import { colors, getMarkdownStyle, toneColor } from "./theme.ts";
|
||||
|
||||
export function ThreadView(props: {
|
||||
readonly title: string;
|
||||
readonly subtitle: string;
|
||||
readonly transcript: Transcript | null;
|
||||
readonly loading: boolean;
|
||||
readonly error?: string | undefined;
|
||||
readonly focused: boolean;
|
||||
readonly showReasoning: 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]);
|
||||
|
||||
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}
|
||||
</scrollbox>
|
||||
</box>
|
||||
);
|
||||
}
|
||||
|
||||
function BlockView({ block, showReasoning }: { readonly block: Block; readonly showReasoning: boolean }) {
|
||||
switch (block.kind) {
|
||||
case "user":
|
||||
return (
|
||||
<box marginTop={1} flexDirection="row">
|
||||
<text fg={colors.user}>› </text>
|
||||
<text fg={colors.user}>
|
||||
<strong>{block.text.trim()}</strong>
|
||||
</text>
|
||||
</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>
|
||||
);
|
||||
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}
|
||||
</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}
|
||||
</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>
|
||||
</text>
|
||||
{block.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}
|
||||
</box>
|
||||
);
|
||||
case "notice":
|
||||
return <text fg={toneColor[block.tone]}>{` ${block.text}`}</text>;
|
||||
}
|
||||
}
|
||||
|
||||
function firstLine(text: string): string {
|
||||
const line = (text.trim().split("\n")[0] ?? "").replace(/\*\*(.+?)\*\*/g, "$1");
|
||||
return line.length > 120 ? `${line.slice(0, 117)}…` : line;
|
||||
}
|
||||
|
||||
function indent(text: string, prefix: string): string {
|
||||
return text
|
||||
.split("\n")
|
||||
.map((line) => prefix + line)
|
||||
.join("\n");
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
import { SyntaxStyle } from "@opentui/core";
|
||||
|
||||
import type { Tone } from "../model/transcript.ts";
|
||||
|
||||
export const colors = {
|
||||
bg: "#16161e",
|
||||
panel: "#1a1b26",
|
||||
border: "#2f334d",
|
||||
borderFocused: "#7aa2f7",
|
||||
text: "#c0caf5",
|
||||
muted: "#737aa2",
|
||||
faint: "#545c7e",
|
||||
accent: "#7aa2f7",
|
||||
user: "#7dcfff",
|
||||
good: "#9ece6a",
|
||||
bad: "#f7768e",
|
||||
warn: "#e0af68",
|
||||
selectedBg: "#283457",
|
||||
} as const;
|
||||
|
||||
export const toneColor: Record<Tone, string> = {
|
||||
normal: colors.text,
|
||||
muted: colors.muted,
|
||||
good: colors.good,
|
||||
bad: colors.bad,
|
||||
warn: colors.warn,
|
||||
};
|
||||
|
||||
let markdownStyle: SyntaxStyle | undefined;
|
||||
|
||||
/** Created lazily: SyntaxStyle needs the native renderer library loaded. */
|
||||
export function getMarkdownStyle(): SyntaxStyle {
|
||||
markdownStyle ??= SyntaxStyle.fromStyles({
|
||||
default: { fg: colors.text },
|
||||
"markup.heading": { fg: colors.accent, bold: true },
|
||||
"markup.strong": { bold: true },
|
||||
"markup.italic": { italic: true },
|
||||
"markup.strikethrough": { dim: true },
|
||||
"markup.link": { fg: colors.user, underline: true },
|
||||
"markup.link.label": { fg: colors.user },
|
||||
"markup.link.url": { fg: colors.faint, underline: true },
|
||||
"markup.quote": { fg: colors.muted, italic: true },
|
||||
"markup.raw": { fg: colors.warn },
|
||||
"markup.raw.block": { fg: colors.text },
|
||||
});
|
||||
return markdownStyle;
|
||||
}
|
||||
Reference in new issue
Block a user