TypeScript
refactor(web): ChatView's revert, rollback, and fork live in useTimelineRewind (#23066)
Julius Marminge9f087f782c23
diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsxindex 2f23303fbc0..f9ca25f49c3 100644--- a/apps/web/src/components/ChatView.tsx+++ b/apps/web/src/components/ChatView.tsx@@ -1,3 +1,4 @@+import { useTimelineRewind } from "./chat/useTimelineRewind";import { useThreadLifecycle } from "./chat/useThreadLifecycle";import { useRightPanelActions } from "./chat/useRightPanelActions";import { type TerminalLaunchContext, useChatTerminalActions } from "./chat/useChatTerminalActions";@@ -119,7 +120,6 @@ import {} from "@t3tools/client-runtime/state/runtime";import { AsyncResult } from "effect/reactivity";import { isElectron } from "../env";-import { readLocalApi } from "../localApi";import { useDiffPanelStore } from "../diffPanelStore";import { useActiveThreadRef } from "../hooks/useActiveThreadRef";import { composerRequiresModifier } from "../composer-logic";@@ -208,7 +208,7 @@ import {deriveLogicalProjectKeyFromSettings,selectProjectGroupingSettings,} from "../logicalProject";-import { buildDraftThreadRouteParams, buildThreadRouteParams } from "../threadRoutes";+import { buildDraftThreadRouteParams } from "../threadRoutes";import {isSameSidebarThreadRef,useSidebarPendingFileDropStore,@@ -255,7 +255,6 @@ import {useQuietThreadShell,useThreadVisibleTurnItems,useThreadCheckpointSummaries,- waitForThreadShell,} from "../state/entities";import { useEnvironmentShellHasSnapshot } from "../state/shell";import { ChatComposer, type ChatComposerHandle } from "./chat/ChatComposer";@@ -266,7 +265,6 @@ import {DeviceSetupDialogHost,openBranchRestoreConfirm,openPullRequestThreadDialog,- openRevertConfirm,PullRequestThreadDialogHost,RevertConfirmHost,} from "./chat/ChatOverlayHosts";@@ -375,7 +373,7 @@ import {} from "./chat/useSystemComposerBanners";import { useDraftHeroLayoutTransition } from "./chat/useDraftHeroLayoutTransition";import { useLocalDispatchState } from "./chat/useLocalDispatchState";-import { draftFanoutSelectionsAtom, draftSendState, rewindToMessage } from "./chat/chatViewSend";+import { draftFanoutSelectionsAtom, draftSendState } from "./chat/chatViewSend";import {isCompactCommandMessage,type LocalThreadErrorEntry,@@ -467,12 +465,6 @@ export default function ChatView(props: ChatViewProps) {label: "load earlier thread history",reportFailure: false,});- const revertThreadCheckpoint = useOrchestrationCommand(threadEnvironment.revertCheckpoint, {- reportFailure: false,- });- const forkThreadFromRun = useAtomCommand(threadEnvironment.forkFromRun, {- reportFailure: false,- });const { environments } = useEnvironments();const providersByEnvironment = useEnvironmentProviders();const primaryEnvironment = usePrimaryEnvironment();@@ -3455,179 +3447,32 @@ export default function ChatView(props: ChatViewProps) {window.removeEventListener("paste", handler, true);};}, [activeThreadId, composerRef]);-- const onRevertToTurnCount = async (- turnCount: number,- messageId: MessageId,- restoreFiles?: boolean,- ) => {- const localApi = readLocalApi();- if (- !localApi ||- !activeThread ||- isRevertingCheckpoint ||- !readEnvironmentScope(environmentId, AuthOrchestrationOperateScope)- )- return;- const message = timelineMessages.find((message) => message.id === messageId);- if (!message || message.role !== "user") return;-- if (!supportsConversationRollback) {- setThreadError(- activeThread.id,- "This provider does not support reverting conversation history. Start a new thread instead.",- );- return;- }- if (activeEnvironmentUnavailable && activeEnvironmentUnavailableLabel) {- setThreadError(- activeThread.id,- `Reconnect ${activeEnvironmentUnavailableLabel} before reverting checkpoints.`,- );- return;- }- if (phase === "running" || isSendBusy || isConnecting) {- setThreadError(activeThread.id, "Interrupt the current turn before reverting checkpoints.");- return;- }- if (restoreFiles === undefined) {- openRevertConfirm(routeThreadKey, { turnCount, messageId });- return;- }-- useComposerDraftStore.setState((store) => ({- rewindingThreadKeys: new Set(store.rewindingThreadKeys).add(routeThreadKey),- }));- setThreadError(activeThread.id, null);- const rewind = await rewindToMessage({- message,- environmentId,- routeThreadRef,- threadId: activeThread.id,- turnCount,- restoreFiles,- // Keep the live checkpoint identity when command context refetches a bounded window.- checkpoint: findRevertCheckpoint(serverProjection?.checkpoints ?? [], turnCount),- draftTarget: composerDraftTarget,- composerHasPendingAttachments: composerRef.current?.hasPendingAttachments() === true,- createAssetUrl: createAttachmentAssetUrl,- revertThreadCheckpoint,- });- useComposerDraftStore.setState((store) => {- const remaining = new Set(store.rewindingThreadKeys);- remaining.delete(routeThreadKey);- return { rewindingThreadKeys: remaining };- });- if (rewind.error !== null) {- setThreadError(activeThread.id, rewind.error);- return;- }- if (currentRouteThreadKeyRef.current === routeThreadKey) {- const nextPrompt = rewind.prompt;- promptRef.current = nextPrompt;- composerRef.current?.resetCursorState({ prompt: nextPrompt, cursor: nextPrompt.length });- requestAnimationFrame(() => {- if (currentRouteThreadKeyRef.current === routeThreadKey) composerRef.current?.focusAtEnd();- });- }- };- const onRevertToTurnCountStable = useStableCallback(onRevertToTurnCount);- const onConfirmRevert = (turnCount: number, messageId: MessageId, restoreFiles: boolean) => {- void onRevertToTurnCount(turnCount, messageId, restoreFiles);- };- const onConfirmRevertStable = useStableCallback(onConfirmRevert);-- const onRollbackCheckpoint = async (input: {- readonly checkpointId: string;- readonly scopeId: string;- }) => {- if (!activeThread || isRevertingCheckpoint) return;- if (activeEnvironmentUnavailable && activeEnvironmentUnavailableLabel) {- setThreadError(- activeThread.id,- `Reconnect ${activeEnvironmentUnavailableLabel} before reverting checkpoints.`,- );- return;- }- if (phase === "running" || isSendBusy || isConnecting) {- setThreadError(activeThread.id, "Interrupt the current turn before reverting checkpoints.");- return;- }- const localApi = readLocalApi();- const confirmed =- localApi == null- ? window.confirm("Roll back this thread to the selected checkpoint?")- : await localApi.dialogs.confirm(- "Roll back this thread to the selected checkpoint?\nThis action cannot be undone.",- );- if (!confirmed) return;-- useComposerDraftStore.setState((store) => ({- rewindingThreadKeys: new Set(store.rewindingThreadKeys).add(routeThreadKey),- }));- setThreadError(activeThread.id, null);- const result = await revertThreadCheckpoint({- environmentId,- input: {- threadId: activeThread.id,- checkpointId: input.checkpointId,- scopeId: input.scopeId,- },- });- if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) {- const error = squashAtomCommandFailure(result);- setThreadError(- activeThread.id,- error instanceof Error ? error.message : "Failed to revert thread state.",- );- }- useComposerDraftStore.setState((store) => {- const remaining = new Set(store.rewindingThreadKeys);- remaining.delete(routeThreadKey);- return { rewindingThreadKeys: remaining };- });- };-- const onForkFromRun = async (input: {- readonly sourceThreadId: ThreadId;- readonly runId: RunId;- }) => {- if (!activeThread || activeEnvironmentUnavailable) return;- const targetThreadId = newThreadId();- const targetThreadRef = scopeThreadRef(environmentId, targetThreadId);- const result = await forkThreadFromRun({- environmentId,- input: {- sourceThreadId: input.sourceThreadId,- targetThreadId,- runId: input.runId,- title: `${activeThread.title} fork`,- },- });- if (result._tag === "Failure") {- if (!isAtomCommandInterrupted(result)) {- const error = squashAtomCommandFailure(result);- setThreadError(- activeThread.id,- error instanceof Error ? error.message : "Failed to fork this response.",- );- }- return;- }- const targetThreadReady = await waitForThreadShell(targetThreadRef);- if (!targetThreadReady) {- setThreadError(- activeThread.id,- "The fork was created, but its thread data did not reach this client. Reconnect and try opening it from the sidebar.",- );- return;- }- await navigate({- to: "/$environmentId/$threadId",- params: buildThreadRouteParams(targetThreadRef),- });- };- const onForkFromRunStable = useStableCallback(onForkFromRun);+ const {+ onConfirmRevertStable,+ onForkFromRunStable,+ onRevertTimelineTurnStable,+ onTimelineRollbackCheckpointStable,+ } = useTimelineRewind({+ environmentId,+ routeThreadRef,+ routeThreadKey,+ currentRouteThreadKeyRef,+ serverProjection,+ activeThread,+ timelineMessages,+ paintOnlyDisplayedTimeline,+ phase,+ isConnecting,+ isSendBusy,+ isRevertingCheckpoint,+ supportsConversationRollback,+ activeEnvironmentUnavailable,+ activeEnvironmentUnavailableLabel,+ promptRef,+ composerRef,+ composerDraftTarget,+ setThreadError,+ });const onCompactContext = () => {if (compactDisabled) return;void sendStandaloneCommand("/compact", "Failed to compact context.");@@ -3933,17 +3778,6 @@ export default function ChatView(props: ChatViewProps) {openExpandedImagePreview(routeThreadKey, preview);};const onExpandTimelineImageStable = useStableCallback(onExpandTimelineImage);- const onTimelineRollbackCheckpoint = (input: {- readonly checkpointId: string;- readonly scopeId: string;- }) => {- if (!paintOnlyDisplayedTimeline) void onRollbackCheckpoint(input);- };- const onTimelineRollbackCheckpointStable = useStableCallback(onTimelineRollbackCheckpoint);- const onRevertTimelineTurn = (targetTurnCount: number, messageId: MessageId) => {- void onRevertToTurnCountStable(targetTurnCount, messageId);- };- const onRevertTimelineTurnStable = useStableCallback(onRevertTimelineTurn);const pendingSidebarFileDrops = useSidebarPendingFileDropStore((state) => state.pending);const consumePendingFileDrop = useSidebarPendingFileDropStore(diff --git a/apps/web/src/components/chat/useTimelineRewind.ts b/apps/web/src/components/chat/useTimelineRewind.tsnew file mode 100644index 00000000000..6f9e042951b--- /dev/null+++ b/apps/web/src/components/chat/useTimelineRewind.ts@@ -0,0 +1,287 @@+import type { EnvironmentThread } from "@t3tools/client-runtime/state/shell";+import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell";+import type { ComposerThreadTarget } from "../../composerDraftStore";+import type { ComposerHandleRef } from "../../composerHandleContext";+import type { SessionPhase } from "../../types";+import { useStableCallback } from "../../hooks/useStableCallback";+import {+ AuthOrchestrationOperateScope,+ type EnvironmentId,+ type MessageId,+ type ScopedThreadRef,+ type ThreadId,+ type RunId,+} from "@t3tools/contracts";+import { scopeThreadRef } from "@t3tools/client-runtime/environment";+import { useNavigate } from "@tanstack/react-router";+import {+ isAtomCommandInterrupted,+ squashAtomCommandFailure,+} from "@t3tools/client-runtime/state/runtime";+import { readLocalApi } from "../../localApi";+import { findRevertCheckpoint } from "../../session-logic";+import { type ChatMessage } from "../../types";+import { newThreadId } from "~/lib/utils";+import { buildThreadRouteParams } from "../../threadRoutes";+import { useComposerDraftStore } from "../../composerDraftStore";+import { threadEnvironment } from "../../state/threads";+import { waitForThreadShell } from "../../state/entities";+import { openRevertConfirm } from "./ChatOverlayHosts";+import { assetEnvironment } from "../../state/assets";+import { readEnvironmentScope } from "../../state/session";+import { useAtomCommand } from "../../state/use-atom-command";+import { useOrchestrationCommand } from "../../state/use-orchestration-command";+import { useAtomQueryRunner } from "../../state/use-atom-query-runner";+import { rewindToMessage } from "./chatViewSend";+import { makeThreadErrorSetter } from "../ChatView.logic";+interface TimelineRewindInput {+ readonly environmentId: EnvironmentId;+ readonly routeThreadRef: ScopedThreadRef;+ readonly routeThreadKey: string;+ readonly currentRouteThreadKeyRef: { readonly current: string | null };+ readonly serverProjection: EnvironmentThread["projection"] | null;+ readonly activeThread: EnvironmentThreadShell | undefined;+ readonly timelineMessages: ReadonlyArray<ChatMessage>;+ readonly paintOnlyDisplayedTimeline: boolean;+ readonly phase: SessionPhase;+ readonly isConnecting: boolean;+ readonly isSendBusy: boolean;+ readonly isRevertingCheckpoint: boolean;+ readonly supportsConversationRollback: boolean;+ readonly activeEnvironmentUnavailable: boolean;+ readonly activeEnvironmentUnavailableLabel: string | null;+ readonly promptRef: { current: string };+ readonly composerRef: ComposerHandleRef;+ readonly composerDraftTarget: ComposerThreadTarget;+ readonly setThreadError: ReturnType<typeof makeThreadErrorSetter>;+}++/**+ * Going back in a thread's timeline: reverting to an earlier turn (restoring that message into+ * the composer), rolling back a provider checkpoint, and forking a new thread from a run.+ */+export function useTimelineRewind(input: TimelineRewindInput) {+ const {+ environmentId,+ routeThreadRef,+ routeThreadKey,+ currentRouteThreadKeyRef,+ serverProjection,+ activeThread,+ timelineMessages,+ paintOnlyDisplayedTimeline,+ phase,+ isConnecting,+ isSendBusy,+ isRevertingCheckpoint,+ supportsConversationRollback,+ activeEnvironmentUnavailable,+ activeEnvironmentUnavailableLabel,+ promptRef,+ composerRef,+ composerDraftTarget,+ setThreadError,+ } = input;+ const navigate = useNavigate();+ const createAttachmentAssetUrl = useAtomQueryRunner(assetEnvironment.createUrl, {+ reportFailure: false,+ refresh: true,+ });++ const revertThreadCheckpoint = useOrchestrationCommand(threadEnvironment.revertCheckpoint, {+ reportFailure: false,+ });+ const forkThreadFromRun = useAtomCommand(threadEnvironment.forkFromRun, {+ reportFailure: false,+ });++ const onRevertToTurnCount = async (+ turnCount: number,+ messageId: MessageId,+ restoreFiles?: boolean,+ ) => {+ const localApi = readLocalApi();+ if (+ !localApi ||+ !activeThread ||+ isRevertingCheckpoint ||+ !readEnvironmentScope(environmentId, AuthOrchestrationOperateScope)+ )+ return;+ const message = timelineMessages.find((message) => message.id === messageId);+ if (!message || message.role !== "user") return;++ if (!supportsConversationRollback) {+ setThreadError(+ activeThread.id,+ "This provider does not support reverting conversation history. Start a new thread instead.",+ );+ return;+ }+ if (activeEnvironmentUnavailable && activeEnvironmentUnavailableLabel) {+ setThreadError(+ activeThread.id,+ `Reconnect ${activeEnvironmentUnavailableLabel} before reverting checkpoints.`,+ );+ return;+ }+ if (phase === "running" || isSendBusy || isConnecting) {+ setThreadError(activeThread.id, "Interrupt the current turn before reverting checkpoints.");+ return;+ }+ if (restoreFiles === undefined) {+ openRevertConfirm(routeThreadKey, { turnCount, messageId });+ return;+ }++ useComposerDraftStore.setState((store) => ({+ rewindingThreadKeys: new Set(store.rewindingThreadKeys).add(routeThreadKey),+ }));+ setThreadError(activeThread.id, null);+ const rewind = await rewindToMessage({+ message,+ environmentId,+ routeThreadRef,+ threadId: activeThread.id,+ turnCount,+ restoreFiles,+ // Keep the live checkpoint identity when command context refetches a bounded window.+ checkpoint: findRevertCheckpoint(serverProjection?.checkpoints ?? [], turnCount),+ draftTarget: composerDraftTarget,+ composerHasPendingAttachments: composerRef.current?.hasPendingAttachments() === true,+ createAssetUrl: createAttachmentAssetUrl,+ revertThreadCheckpoint,+ });+ useComposerDraftStore.setState((store) => {+ const remaining = new Set(store.rewindingThreadKeys);+ remaining.delete(routeThreadKey);+ return { rewindingThreadKeys: remaining };+ });+ if (rewind.error !== null) {+ setThreadError(activeThread.id, rewind.error);+ return;+ }+ if (currentRouteThreadKeyRef.current === routeThreadKey) {+ const nextPrompt = rewind.prompt;+ promptRef.current = nextPrompt;+ composerRef.current?.resetCursorState({ prompt: nextPrompt, cursor: nextPrompt.length });+ requestAnimationFrame(() => {+ if (currentRouteThreadKeyRef.current === routeThreadKey) composerRef.current?.focusAtEnd();+ });+ }+ };+ const onRevertToTurnCountStable = useStableCallback(onRevertToTurnCount);+ const onConfirmRevert = (turnCount: number, messageId: MessageId, restoreFiles: boolean) => {+ void onRevertToTurnCount(turnCount, messageId, restoreFiles);+ };+ const onConfirmRevertStable = useStableCallback(onConfirmRevert);++ const onRollbackCheckpoint = async (input: {+ readonly checkpointId: string;+ readonly scopeId: string;+ }) => {+ if (!activeThread || isRevertingCheckpoint) return;+ if (activeEnvironmentUnavailable && activeEnvironmentUnavailableLabel) {+ setThreadError(+ activeThread.id,+ `Reconnect ${activeEnvironmentUnavailableLabel} before reverting checkpoints.`,+ );+ return;+ }+ if (phase === "running" || isSendBusy || isConnecting) {+ setThreadError(activeThread.id, "Interrupt the current turn before reverting checkpoints.");+ return;+ }+ const localApi = readLocalApi();+ const confirmed =+ localApi == null+ ? window.confirm("Roll back this thread to the selected checkpoint?")+ : await localApi.dialogs.confirm(+ "Roll back this thread to the selected checkpoint?\nThis action cannot be undone.",+ );+ if (!confirmed) return;++ useComposerDraftStore.setState((store) => ({+ rewindingThreadKeys: new Set(store.rewindingThreadKeys).add(routeThreadKey),+ }));+ setThreadError(activeThread.id, null);+ const result = await revertThreadCheckpoint({+ environmentId,+ input: {+ threadId: activeThread.id,+ checkpointId: input.checkpointId,+ scopeId: input.scopeId,+ },+ });+ if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) {+ const error = squashAtomCommandFailure(result);+ setThreadError(+ activeThread.id,+ error instanceof Error ? error.message : "Failed to revert thread state.",+ );+ }+ useComposerDraftStore.setState((store) => {+ const remaining = new Set(store.rewindingThreadKeys);+ remaining.delete(routeThreadKey);+ return { rewindingThreadKeys: remaining };+ });+ };++ const onForkFromRun = async (input: {+ readonly sourceThreadId: ThreadId;+ readonly runId: RunId;+ }) => {+ if (!activeThread || activeEnvironmentUnavailable) return;+ const targetThreadId = newThreadId();+ const targetThreadRef = scopeThreadRef(environmentId, targetThreadId);+ const result = await forkThreadFromRun({+ environmentId,+ input: {+ sourceThreadId: input.sourceThreadId,+ targetThreadId,+ runId: input.runId,+ title: `${activeThread.title} fork`,+ },+ });+ if (result._tag === "Failure") {+ if (!isAtomCommandInterrupted(result)) {+ const error = squashAtomCommandFailure(result);+ setThreadError(+ activeThread.id,+ error instanceof Error ? error.message : "Failed to fork this response.",+ );+ }+ return;+ }+ const targetThreadReady = await waitForThreadShell(targetThreadRef);+ if (!targetThreadReady) {+ setThreadError(+ activeThread.id,+ "The fork was created, but its thread data did not reach this client. Reconnect and try opening it from the sidebar.",+ );+ return;+ }+ await navigate({+ to: "/$environmentId/$threadId",+ params: buildThreadRouteParams(targetThreadRef),+ });+ };+ const onForkFromRunStable = useStableCallback(onForkFromRun);+ const onTimelineRollbackCheckpoint = (input: {+ readonly checkpointId: string;+ readonly scopeId: string;+ }) => {+ if (!paintOnlyDisplayedTimeline) void onRollbackCheckpoint(input);+ };+ const onTimelineRollbackCheckpointStable = useStableCallback(onTimelineRollbackCheckpoint);+ const onRevertTimelineTurn = (targetTurnCount: number, messageId: MessageId) => {+ void onRevertToTurnCountStable(targetTurnCount, messageId);+ };+ const onRevertTimelineTurnStable = useStableCallback(onRevertTimelineTurn);+ return {+ onConfirmRevertStable,+ onForkFromRunStable,+ onRevertTimelineTurnStable,+ onTimelineRollbackCheckpointStable,+ };+}