import { turnTargets, type TurnTarget } from "./turns.ts"; import type { OrchestrationV2ThreadProjection } from "@t3tools/contracts"; import { deriveRunlessWorkStartedAt, deriveThreadActivityRun, deriveThreadStructureRuntime, } from "@t3tools/client-runtime/state/thread-execution"; import { derivePendingThreadRequests, type PendingThreadRequests } from "@t3tools/client-runtime/state/thread-requests"; // The web app's own timeline derivation: tui3 shows the same rows, folds and labels. import { computeStableMessagesTimelineRows, deriveMessagesTimelineRowsWithState, type MessagesTimelineRow, type MessagesTimelineRowsInput, type StableMessagesTimelineRowsState, } from "../../vendor/t3code/apps/web/src/components/chat/MessagesTimeline.logic.ts"; import { deriveActiveWorkStartedAt, derivePhase, deriveTimelineEntriesFromVisibleTurnItemsWithState, type TimelineEntriesProjection, } from "../../vendor/t3code/apps/web/src/session-logic.ts"; export type { MessagesTimelineRow } from "../../vendor/t3code/apps/web/src/components/chat/MessagesTimeline.logic.ts"; 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"; export interface Timeline { readonly rows: readonly MessagesTimelineRow[]; readonly pending: PendingThreadRequests; readonly turns: readonly TurnTarget[]; readonly isWorking: boolean; /** When the current run started working, for "Working for 12s". */ readonly workStartedAt: string | null; /** Carried into the next build so unchanged rows keep their identity. */ readonly cache: TimelineCache; } interface TimelineCache { readonly entries: TimelineEntriesProjection; readonly rows: { readonly input: MessagesTimelineRowsInput; readonly rows: MessagesTimelineRow[] }; readonly stable: StableMessagesTimelineRowsState; readonly expandAll: boolean; } /** Answers yes for every id: "expand everything" without knowing the group ids up front. */ class EverySet extends Set { override has(): boolean { return true; } } const EVERYTHING = new EverySet(); const NOTHING = new Set(); export function buildTimeline( projection: OrchestrationV2ThreadProjection, options: { readonly expandAll: boolean; readonly historyComplete: boolean }, previous: Timeline | null, ): Timeline { const cache = previous?.cache.expandAll === options.expandAll ? previous.cache : null; const entries = deriveTimelineEntriesFromVisibleTurnItemsWithState( { visibleTurnItems: projection.visibleTurnItems, optimisticMessages: [], attempts: projection.attempts, nodes: projection.nodes, plans: projection.plans, }, cache?.entries ?? null, ); const runtime = deriveThreadStructureRuntime(projection); const latestRun = deriveThreadActivityRun(projection); const runlessStartedAt = deriveRunlessWorkStartedAt(projection); const isWorking = derivePhase(runtime) === "running" || runlessStartedAt !== null; const workStartedAt = deriveActiveWorkStartedAt(latestRun, runtime, null) ?? runlessStartedAt; const pending = derivePendingThreadRequests(projection); const expanded = options.expandAll ? EVERYTHING : NOTHING; const rows = deriveMessagesTimelineRowsWithState( { timelineEntries: entries.entries, pendingApprovalRequestIds: new Set(pending.approvals.map((approval) => approval.requestId)), latestRun, runningRunId: runtime?.activeRunId ?? null, expandedRunIds: expanded, expandedAttemptIds: expanded, expandedWorkGroupIds: expanded, isWorking, runlessWorkActive: runlessStartedAt !== null, activeTurnStartedAt: workStartedAt, turnDiffSummaries: [], supportsConversationRollback: false, }, cache?.rows ?? null, ); const stable = computeStableMessagesTimelineRows(rows.rows, cache?.stable ?? { byId: new Map(), result: [] }); return { rows: stable.result, pending, turns: turnTargets(projection, entries.entries, isWorking ? runtime?.activeRunId ?? null : null, options.historyComplete), isWorking, workStartedAt, cache: { entries, rows, stable, expandAll: options.expandAll }, }; }