Add turn navigation, checkpoint diffs and confirmed rewind
This commit is contained in:
1 parent
43347d1d58
commit
7eb976f7ac
16 files changed
+634
-39
No files matched your search
@@ -0,0 +1,34 @@
|
||||
import { createTwoFilesPatch, parsePatch } from "diff";
|
||||
import type { ReviewDiffFileContentsInput } from "@t3tools/contracts";
|
||||
|
||||
export interface PatchFile {
|
||||
readonly path: string;
|
||||
readonly oldPath: string;
|
||||
readonly changeType: ReviewDiffFileContentsInput["changeType"];
|
||||
readonly patch: string;
|
||||
}
|
||||
|
||||
/** Retains both paths so renamed and deleted files expand against the right revisions. */
|
||||
export function splitPatch(patch: string): PatchFile[] {
|
||||
return patch.split(/^(?=diff --git )/m).filter((chunk) => chunk.startsWith("diff --git ")).map((chunk) => {
|
||||
let parsed;
|
||||
try { parsed = parsePatch(chunk.split(/^@@ /m)[0]!)[0]; } catch { /* A truncated preview can end inside a file header. */ }
|
||||
const oldPath = cleanPath(parsed?.oldFileName ?? /^diff --git a\/(.+?) b\//.exec(chunk)?.[1] ?? "file");
|
||||
const newPath = cleanPath(parsed?.newFileName ?? oldPath);
|
||||
const renamed = /^rename from /m.test(chunk);
|
||||
return { path: newPath === "/dev/null" ? oldPath : newPath, oldPath: oldPath === "/dev/null" ? newPath : oldPath,
|
||||
changeType: parsed?.isCreate || oldPath === "/dev/null" ? "new" : parsed?.isDelete || newPath === "/dev/null" ? "deleted" : renamed ? /^@@ /m.test(chunk) ? "rename-changed" : "rename-pure" : "change", patch: chunk };
|
||||
});
|
||||
}
|
||||
|
||||
function cleanPath(path: string): string {
|
||||
return path.replace(/^[ab]\//, "");
|
||||
}
|
||||
|
||||
/** The same unified renderer can show all unchanged lines after loading both file versions. */
|
||||
export function wholeFilePatch(file: PatchFile, contents: { readonly oldContents: string; readonly newContents: string }): string {
|
||||
const patch = createTwoFilesPatch(file.oldPath, file.path, contents.oldContents, contents.newContents, undefined, undefined, { context: Infinity });
|
||||
if (contents.oldContents !== contents.newContents || contents.newContents.length === 0) return patch;
|
||||
const lines = contents.newContents.replace(/\n$/, "").split("\n");
|
||||
return `${patch}@@ -1,${lines.length} +1,${lines.length} @@\n${lines.map((line) => ` ${line}`).join("\n")}\n`;
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
import { turnTargets, type TurnTarget } from "./turns.ts";
|
||||
import type { OrchestrationV2ThreadProjection } from "@t3tools/contracts";
|
||||
import {
|
||||
deriveRunlessWorkStartedAt,
|
||||
@@ -28,6 +29,7 @@ export { liveWorkEntryLabel, workEntryDisplayLabel } from "../../vendor/t3code/a
|
||||
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;
|
||||
@@ -53,7 +55,7 @@ const NOTHING = new Set<never>();
|
||||
|
||||
export function buildTimeline(
|
||||
projection: OrchestrationV2ThreadProjection,
|
||||
options: { readonly expandAll: boolean },
|
||||
options: { readonly expandAll: boolean; readonly historyComplete: boolean },
|
||||
previous: Timeline | null,
|
||||
): Timeline {
|
||||
const cache = previous?.cache.expandAll === options.expandAll ? previous.cache : null;
|
||||
@@ -95,6 +97,7 @@ export function buildTimeline(
|
||||
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 },
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
import type { MessageId, OrchestrationV2ThreadProjection, RunId } from "@t3tools/contracts";
|
||||
import { checkpointFileTotals, deriveThreadCheckpointSummaries } from "@t3tools/client-runtime/state/thread-checkpoints";
|
||||
import { deriveRevertTurnCountByUserMessageId, findRevertCheckpoint, type TimelineEntriesProjection } from "../../vendor/t3code/apps/web/src/session-logic.ts";
|
||||
|
||||
export { findRevertCheckpoint };
|
||||
export type TurnCheckpoint = ReturnType<typeof deriveThreadCheckpointSummaries>[number];
|
||||
export interface TurnTarget {
|
||||
readonly messageId: MessageId;
|
||||
readonly runId: RunId;
|
||||
readonly text: string;
|
||||
readonly hasAttachments: boolean;
|
||||
readonly checkpoint: TurnCheckpoint | undefined;
|
||||
readonly revertTurnCount: number | undefined;
|
||||
}
|
||||
|
||||
/** Uses the web's rewind mapping; run ordinals can have gaps after a rollback. */
|
||||
export function turnTargets(projection: OrchestrationV2ThreadProjection, entries: TimelineEntriesProjection["entries"], unsettledRunId: RunId | null, historyComplete: boolean): TurnTarget[] {
|
||||
const checkpoints = deriveThreadCheckpointSummaries(projection);
|
||||
const revertCounts = deriveRevertTurnCountByUserMessageId({
|
||||
timelineEntries: entries,
|
||||
checkpoints,
|
||||
threadStartCheckpointReady: historyComplete && findRevertCheckpoint(projection.checkpoints, 0) !== undefined,
|
||||
availableRevertTurnCounts: availableRevertTurnCounts(projection),
|
||||
unsettledRunId,
|
||||
});
|
||||
const seen = new Set<RunId>();
|
||||
return entries.flatMap((entry) => {
|
||||
if (entry.kind !== "message" || entry.message.role !== "user" || !entry.message.runId || seen.has(entry.message.runId)) return [];
|
||||
const message = entry.message;
|
||||
const runId = entry.message.runId;
|
||||
seen.add(runId);
|
||||
return [{ messageId: message.id, runId, text: message.text, hasAttachments: (message.attachments?.length ?? 0) > 0,
|
||||
checkpoint: checkpoints.findLast((checkpoint) => checkpoint.runId === message.runId), revertTurnCount: revertCounts.get(message.id) }];
|
||||
});
|
||||
}
|
||||
|
||||
/** Same provider-turn eligibility as ChatView.logic, without importing its browser state. */
|
||||
function availableRevertTurnCounts(projection: OrchestrationV2ThreadProjection): Set<number> {
|
||||
const available = new Set<number>();
|
||||
const providerThread = projection.providerThreads.find((thread) => thread.id === projection.thread.activeProviderThreadId);
|
||||
if (!providerThread?.providerSessionId || providerThread.providerInstanceId !== projection.thread.modelSelection.instanceId) return available;
|
||||
available.add(0);
|
||||
for (const run of projection.runs) {
|
||||
if (run.activeAttemptId === null) continue;
|
||||
const attempt = projection.attempts.find((candidate) => candidate.id === run.activeAttemptId);
|
||||
const turn = projection.providerTurns.filter((candidate) => candidate.runAttemptId === run.activeAttemptId)
|
||||
.reduce<(typeof projection.providerTurns)[number] | undefined>((latest, candidate) => !latest || candidate.ordinal > latest.ordinal ? candidate : latest, undefined)
|
||||
?? projection.providerTurns.find((candidate) => candidate.id === attempt?.providerTurnId);
|
||||
if (turn?.providerThreadId === providerThread.id) available.add(run.ordinal);
|
||||
}
|
||||
return available;
|
||||
}
|
||||
|
||||
/** With no cursor, diff the most recent checkpoint that actually changed files. */
|
||||
export function diffTurn(turns: readonly TurnTarget[], selectedMessageId: string | null): TurnCheckpoint | undefined {
|
||||
return selectedMessageId !== null
|
||||
? turns.find((turn) => turn.messageId === selectedMessageId)?.checkpoint
|
||||
: turns.map((turn) => turn.checkpoint).filter((checkpoint): checkpoint is TurnCheckpoint => checkpoint !== undefined && checkpointFileTotals(checkpoint).count > 0)
|
||||
.sort((left, right) => right.checkpointTurnCount - left.checkpointTurnCount)[0];
|
||||
}
|
||||
|
||||
/** A cursor stays on its message when older history is prepended. */
|
||||
export function adjacentTurn(turns: readonly TurnTarget[], selectedMessageId: string | null, direction: -1 | 1): TurnTarget | undefined {
|
||||
const index = turns.findIndex((turn) => turn.messageId === selectedMessageId);
|
||||
return turns[index < 0 ? turns.length - 1 : Math.max(0, Math.min(turns.length - 1, index + direction))];
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user