diff --git a/src/model/timeline.ts b/src/model/timeline.ts index 53860ba..74844cf 100644 --- a/src/model/timeline.ts +++ b/src/model/timeline.ts @@ -1,11 +1,13 @@ import { turnTargets, type TurnTarget } from "./turns.ts"; import type { OrchestrationV2ThreadProjection, RunAttemptId, RunId } from "@t3tools/contracts"; +import * as DateTime from "effect/DateTime"; import { deriveRunlessWorkStartedAt, deriveThreadActivityRun, deriveThreadStructureRuntime, } from "@t3tools/client-runtime/state/thread-execution"; import { derivePendingThreadRequests, type PendingThreadRequests } from "@t3tools/client-runtime/state/thread-requests"; +import { deriveThreadCheckpointSummaries } from "@t3tools/client-runtime/state/thread-checkpoints"; // The web app's own timeline derivation: tui3 shows the same rows, folds and labels. import { @@ -26,6 +28,12 @@ export type { MessagesTimelineRow } from "../../vendor/t3code/apps/web/src/compo export type { WorkLogEntry } from "../../vendor/t3code/apps/web/src/session-logic.ts"; export { liveWorkEntryLabel, workEntryDisplayLabel } from "../../vendor/t3code/apps/web/src/components/chat/MessagesTimeline.logic.ts"; +/** What a turn footer needs of its run: the model, and how long it ran. */ +export interface RunMeta { + readonly model: string; + readonly durationMs: number | null; +} + export interface Timeline { readonly rows: readonly MessagesTimelineRow[]; readonly pending: PendingThreadRequests; @@ -33,18 +41,28 @@ export interface Timeline { readonly isWorking: boolean; /** When the current run started working, for "Working for 12s". */ readonly workStartedAt: string | null; + /** Footers by run, keyed as messages key their runs. */ + readonly runMeta: ReadonlyMap; /** Carried into the next build so unchanged rows keep their identity. */ readonly cache: TimelineCache; } +type TurnDiffSummaries = ReturnType; + interface TimelineCache { readonly entries: TimelineEntriesProjection; readonly rows: { readonly input: MessagesTimelineRowsInput; readonly rows: MessagesTimelineRow[] }; readonly stable: StableMessagesTimelineRowsState; readonly expandAll: boolean; readonly expanded: ReadonlySet; + /** The last summaries and what they were derived from, so unchanged replies keep their row identity. */ + readonly turnDiffSummaries: TurnDiffSummaries; + readonly checkpoints: OrchestrationV2ThreadProjection["checkpoints"]; + readonly checkpointMessages: OrchestrationV2ThreadProjection["messages"]; } +const sameItems = (a: readonly T[], b: readonly T[]): boolean => a.length === b.length && a.every((item, index) => item === b[index]); + /** Answers yes for every id: "expand everything" without knowing the group ids up front. */ class EverySet extends Set { override has(): boolean { @@ -78,6 +96,11 @@ export function buildTimeline( const isWorking = derivePhase(runtime) === "running" || runlessStartedAt !== null; const workStartedAt = deriveActiveWorkStartedAt(latestRun, runtime, null) ?? runlessStartedAt; const pending = derivePendingThreadRequests(projection); + const previousCache = previous?.cache; + const turnDiffSummaries = + previousCache && sameItems(previousCache.checkpoints, projection.checkpoints) && sameItems(previousCache.checkpointMessages, projection.messages) + ? previousCache.turnDiffSummaries + : deriveThreadCheckpointSummaries(projection); const expanded = options.expandAll ? EVERYTHING : opened; const rows = deriveMessagesTimelineRowsWithState( { @@ -92,7 +115,7 @@ export function buildTimeline( isWorking, runlessWorkActive: runlessStartedAt !== null, activeTurnStartedAt: workStartedAt, - turnDiffSummaries: [], + turnDiffSummaries, supportsConversationRollback: false, }, cache?.rows ?? null, @@ -104,6 +127,13 @@ export function buildTimeline( turns: turnTargets(projection, entries.entries, isWorking ? runtime?.activeRunId ?? null : null, options.historyComplete), isWorking, workStartedAt, - cache: { entries, rows, stable, expandAll: options.expandAll, expanded: opened }, + runMeta: new Map( + projection.runs.map((run) => { + const started = run.workStartedAt ?? run.startedAt ?? run.requestedAt; + const durationMs = run.completedAt ? DateTime.toEpochMillis(run.completedAt) - DateTime.toEpochMillis(started) : null; + return [run.id as string, { model: run.modelSelection.model, durationMs: durationMs !== null && durationMs >= 0 ? durationMs : null }] as const; + }), + ), + cache: { entries, rows, stable, expandAll: options.expandAll, expanded: opened, turnDiffSummaries, checkpoints: projection.checkpoints, checkpointMessages: projection.messages }, }; } diff --git a/src/ui/App.tsx b/src/ui/App.tsx index 91118f9..75695a4 100644 --- a/src/ui/App.tsx +++ b/src/ui/App.tsx @@ -17,7 +17,8 @@ import { paletteEntries, paletteGroups, pickerGroups, type Picker, type PickerIt import { pullRequestRepository } from "../model/pullRequests.ts"; import { serverSettingLines, settingRows } from "../model/settings.ts"; import { resetCreditOutcome, usageLimits, usageSummaryLines } from "../model/usage.ts"; -import { buildTimeline, type Timeline } from "../model/timeline.ts"; +import { buildTimeline, liveWorkEntryLabel, type Timeline } from "../model/timeline.ts"; +import { formatWorkingDurationLabel } from "@t3tools/client-runtime/state/models"; import { statusLabel, type Section } from "../model/threads.ts"; import { ComposerMenu } from "./ComposerMenu.tsx"; import { ConfirmBar, type Confirmation } from "./ConfirmBar.tsx"; @@ -32,6 +33,8 @@ import { useQuestion } from "./useQuestion.ts"; import { useThreadMenu } from "./useThreadMenu.ts"; import { editInEditor } from "./editor.ts"; import { Palette } from "./Palette.tsx"; +import { ApprovalPanel } from "./ApprovalPanel.tsx"; +import { LiveStatus } from "./LiveStatus.tsx"; import { QuestionPanel } from "./QuestionPanel.tsx"; import { QueuePanel } from "./QueuePanel.tsx"; import { SecretPanel } from "./SecretPanel.tsx"; @@ -253,6 +256,12 @@ export function App(props: { readonly session: T3Session; readonly config: Tui3C const pendingApproval = current?.pending.approvals[0]; const pendingInput = current?.pending.userInputs[0]; const working = current?.isWorking ?? false; + // The status line above the composer: the timeline's working/thinking rows live here now. + const liveRow = current?.rows.findLast((row) => row.kind === "work-live"); + const liveTool = liveRow?.kind === "work-live" ? truncate(firstLine(liveWorkEntryLabel(liveRow.entry, threadShell?.worktreePath ?? undefined, liveRow.active)), 60) : null; + const liveVerb = current?.rows.some((row) => row.kind === "working" && row.compacting) ? "Compacting" : !liveRow && current?.rows.some((row) => row.kind === "thinking") ? "Thinking" : "Working"; + const liveSince = current?.workStartedAt ? Date.parse(current.workStartedAt) : NaN; + const liveLabel = Number.isFinite(liveSince) ? `${liveVerb} for ${formatWorkingDurationLabel(now - liveSince)}` : `${liveVerb}…`; // A thread opens at its latest message: once its first snapshot renders, jump to the bottom. const loaded = current !== null; @@ -910,7 +919,6 @@ export function App(props: { readonly session: T3Session; readonly config: Tui3C // The mouse in the conversation: stable callbacks, since its rows are memoized. const conversationMouse: ConversationMouse = { - respond: useEvent((decision: ProviderApprovalDecision) => respond(decision)), selectTurn: useEvent((messageId: string) => turns.select(messageId)), toggleFold: useEvent((id: string) => setExpandedIds((previous) => { @@ -921,6 +929,7 @@ export function App(props: { readonly session: T3Session; readonly config: Tui3C ), loadEarlier: useEvent(() => loadEarlier()), menu: useEvent((target: ConversationTarget) => openConversationMenu(target)), + openTurnDiff: useEvent((checkpointTurnCount: number) => turns.openTurnDiff(checkpointTurnCount)), }; const copy = (label: string, text: string) => { copyText(renderer, text); @@ -944,6 +953,7 @@ export function App(props: { readonly session: T3Session; readonly config: Tui3C ] : [ { id: "copy-message", title: "Copy message", run: () => copy("the message", target.text) }, + ...(target.checkpointTurnCount !== undefined ? [{ id: "turn-diff", title: "View changes from this turn", run: () => turns.openTurnDiff(target.checkpointTurnCount!) }] : []), ...(target.role === "user" ? turns.menuFor(target.messageId) : []), ]; if (target.kind === "message" && target.role === "user") turns.select(target.messageId); @@ -1122,6 +1132,7 @@ export function App(props: { readonly session: T3Session; readonly config: Tui3C loading={watchedId !== threadId || thread?.status === "loading"} error={thread?.error} workspaceRoot={threadShell?.worktreePath ?? undefined} + timestampFormat={config.timestampFormat} now={now} scrollRef={scrollRef} parent={parent} @@ -1168,8 +1179,14 @@ export function App(props: { readonly session: T3Session; readonly config: Tui3C