Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions apps/web/src/components/ChatView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ import {
} from "../composerDraftStore";
import {
appendTerminalContextsToPrompt,
deriveDisplayedUserMessageState,
formatTerminalContextLabel,
insertInlineTerminalContextPlaceholder,
removeInlineTerminalContextPlaceholder,
Expand Down Expand Up @@ -2302,7 +2303,7 @@ export default function ChatView({ threadId }: ChatViewProps) {
};

const onRevertToTurnCount = useCallback(
async (turnCount: number) => {
async (turnCount: number, promptToRestore?: string) => {
const api = readNativeApi();
if (!api || !activeThread || isRevertingCheckpoint) return;

Expand All @@ -2323,6 +2324,7 @@ export default function ChatView({ threadId }: ChatViewProps) {

setIsRevertingCheckpoint(true);
setThreadError(activeThread.id, null);
let reverted = false;
try {
await api.orchestration.dispatchCommand({
type: "thread.checkpoint.revert",
Expand All @@ -2331,15 +2333,28 @@ export default function ChatView({ threadId }: ChatViewProps) {
turnCount,
createdAt: new Date().toISOString(),
});
reverted = true;
} catch (err) {
setThreadError(
activeThread.id,
err instanceof Error ? err.message : "Failed to revert thread state.",
);
}
setIsRevertingCheckpoint(false);

// Restore the reverted user prompt into the composer only on success.
// We use setTimeout to defer past the server state sync (throttled at ~100ms)
// so the restored prompt is not overwritten by re-renders from the sync.
if (reverted && promptToRestore) {
setTimeout(() => {
promptRef.current = promptToRestore;
setPrompt(promptToRestore);
setComposerCursor(collapseExpandedComposerCursor(promptToRestore, promptToRestore.length));
scheduleComposerFocus();
}, 300);
}
},
[activeThread, isConnecting, isRevertingCheckpoint, isSendBusy, phase, setThreadError],
[activeThread, isConnecting, isRevertingCheckpoint, isSendBusy, phase, scheduleComposerFocus, setPrompt, setThreadError],
);

const onSend = async (e?: { preventDefault: () => void }) => {
Expand Down Expand Up @@ -3431,7 +3446,14 @@ export default function ChatView({ threadId }: ChatViewProps) {
if (typeof targetTurnCount !== "number") {
return;
}
void onRevertToTurnCount(targetTurnCount);
// Find the user message and extract just the visible prompt text (stripping
// terminal context blocks and other metadata) so we can restore it into the
// composer after reverting.
const userMessage = timelineMessages.find((m) => m.id === messageId && m.role === "user");
const promptText = userMessage
? deriveDisplayedUserMessageState(userMessage.text).visibleText
: undefined;
void onRevertToTurnCount(targetTurnCount, promptText);
};

// Empty state: no active thread
Expand Down