Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

fix(ai): rewind a chat message from the view it was sent in

EtienneLescot57b746d860ea
diff --git a/electron/ai-edition/chat-service.toolloop.test.ts b/electron/ai-edition/chat-service.toolloop.test.ts
index d140cf468..11719179d 100644
--- a/electron/ai-edition/chat-service.toolloop.test.ts
+++ b/electron/ai-edition/chat-service.toolloop.test.ts
@@ -200,6 +200,47 @@ describe("runChat tool loop", () => {
expect(undo.prompt).toBe("cut the silence");
});
+ it("reports the id it stored the user's message under, which a rewind accepts", async () => {
+ // The panel shows the message before the turn answers, under a local id the session does
+ // not know. Without this id it could only rewind after reloading the conversation.
+ const session = createSession("proj_rewind_sent");
+ const result = await runChat(
+ "proj_rewind_sent",
+ session.id,
+ "cut the silence",
+ stubConfig(),
+ fixtureDocument(),
+ );
+ const { selectSession } = await import("./chat-service");
+ const stored = selectSession("proj_rewind_sent", session.id)?.messages[0];
+ expect(result.userMessageId).toBe(stored?.id);
+ expect(result.userMessageCheckpointId).toBe(stored?.id);
+
+ const rewound = rewindToMessage("proj_rewind_sent", session.id, result.userMessageId ?? "");
+ expect(rewound.success).toBe(true);
+ });
+
+ it("reports the stored user message id when the model answers nothing", async () => {
+ invokeMock.mockImplementationOnce(async (args) => ({
+ text: "",
+ document: args.document,
+ mutated: false,
+ }));
+ const session = createSession("proj_empty_reply");
+ const result = await runChat(
+ "proj_empty_reply",
+ session.id,
+ "hi",
+ stubConfig(),
+ fixtureDocument(),
+ );
+ const { selectSession } = await import("./chat-service");
+ expect(result.success).toBe(false);
+ expect(result.userMessageId).toBe(
+ selectSession("proj_empty_reply", session.id)?.messages[0].id,
+ );
+ });
+
it("rewindToMessage refuses when the target has no checkpoint", async () => {
const { createSession, selectSession, runChat } = await import("./chat-service");
const session = createSession("proj_rewind_no_cp");
diff --git a/electron/ai-edition/chat-service.ts b/electron/ai-edition/chat-service.ts
index ab57b273b..e056f04b1 100644
--- a/electron/ai-edition/chat-service.ts
+++ b/electron/ai-edition/chat-service.ts
@@ -484,6 +484,9 @@ export async function runChat(
return {
success: false,
error: result.reason ?? "Empty response from model.",
+ // The message is stored all the same, and can be rewound to retry it.
+ userMessageId: userMessage.id,
+ userMessageCheckpointId: userMessage.checkpointId ?? undefined,
};
}
@@ -508,6 +511,7 @@ export async function runChat(
document: result.mutated && editsAllowed ? result.document : undefined,
toolCalls: appliedToolCalls.length ? appliedToolCalls : undefined,
userMessageCheckpointId: userMessage.checkpointId ?? undefined,
+ userMessageId: userMessage.id,
};
}
diff --git a/src/components/ai-edition/LeftPanel.tsx b/src/components/ai-edition/LeftPanel.tsx
index 84c5227b7..4b534ed6d 100644
--- a/src/components/ai-edition/LeftPanel.tsx
+++ b/src/components/ai-edition/LeftPanel.tsx
@@ -26,6 +26,7 @@ import {
} from "../../../electron/ai-edition/provider-registry";
import { ChatWelcome } from "./ChatWelcome";
import { canSendChat } from "./chatAvailability";
+import { adoptStoredUserMessage } from "./chatTranscript";
import { ChatHistoryModal } from "./Modals";
import styles from "./NewEditorShell.module.css";
import { useChatBudget } from "./useChatBudget";
@@ -634,6 +635,7 @@ export function ChatStripPanel() {
const { result, applyDocument } = await runAgentTurn((documentSnapshot) =>
nativeBridgeClient.aiEdition.chatRun(projectId, sessionId, text, documentSnapshot),
);
+ setMessages((prev) => adoptStoredUserMessage(prev, optimisticUserId, result));
const assistant = result.assistantMessage;
if (result.success && assistant) {
if (result.document) {
diff --git a/src/components/ai-edition/chatTranscript.test.ts b/src/components/ai-edition/chatTranscript.test.ts
new file mode 100644
index 000000000..6e8b19249
--- /dev/null
+++ b/src/components/ai-edition/chatTranscript.test.ts
@@ -0,0 +1,36 @@
+import { describe, expect, it } from "vitest";
+import { adoptStoredUserMessage } from "./chatTranscript";
+
+const local = {
+ id: "local_1_u",
+ role: "user",
+ content: "cut the silence",
+ checkpointId: "local_1_u",
+};
+const earlier = { id: "m0", role: "assistant", content: "hi", checkpointId: null };
+
+describe("adoptStoredUserMessage", () => {
+ it("gives the message just sent the id the session stored it under, so it can be rewound", () => {
+ const next = adoptStoredUserMessage([earlier, local], "local_1_u", {
+ userMessageId: "stored-1",
+ userMessageCheckpointId: "stored-1",
+ });
+ expect(next[1]).toMatchObject({
+ id: "stored-1",
+ checkpointId: "stored-1",
+ content: "cut the silence",
+ });
+ expect(next[0]).toBe(earlier);
+ });
+
+ it("hides the rewind of a message stored without a checkpoint", () => {
+ // A text-only turn (no document open) records no snapshot: there is nothing to rewind to.
+ const next = adoptStoredUserMessage([local], "local_1_u", { userMessageId: "stored-1" });
+ expect(next[0]).toMatchObject({ id: "stored-1", checkpointId: null });
+ });
+
+ it("leaves the transcript as it is when the turn never stored the message", () => {
+ const messages = [earlier, local];
+ expect(adoptStoredUserMessage(messages, "local_1_u", {})).toBe(messages);
+ });
+});
diff --git a/src/components/ai-edition/chatTranscript.ts b/src/components/ai-edition/chatTranscript.ts
new file mode 100644
index 000000000..2d70460c1
--- /dev/null
+++ b/src/components/ai-edition/chatTranscript.ts
@@ -0,0 +1,21 @@
+import type { AiEditionChatResult } from "@/native/contracts";
+
+/**
+ * The panel shows a sent message before the main process has stored it, under a `local_…` id
+ * the session does not know. Once the turn answers with the stored id, the message takes it,
+ * with its real checkpoint: a rewind names the message by that id, and with the local one it
+ * failed with "Message not found in this session" until the conversation was reloaded.
+ */
+export function adoptStoredUserMessage<M extends { id?: string; checkpointId?: string | null }>(
+ messages: M[],
+ localId: string,
+ result: Pick<AiEditionChatResult, "userMessageId" | "userMessageCheckpointId">,
+): M[] {
+ const storedId = result.userMessageId;
+ if (!storedId) return messages;
+ return messages.map((message) =>
+ message.id === localId
+ ? { ...message, id: storedId, checkpointId: result.userMessageCheckpointId ?? null }
+ : message,
+ );
+}
diff --git a/src/native/contracts.ts b/src/native/contracts.ts
index c837b216e..521cc993e 100644
--- a/src/native/contracts.ts
+++ b/src/native/contracts.ts
@@ -407,6 +407,9 @@ export interface AiEditionChatResult {
error?: string;
/** Document checkpoint id recorded for the user message that triggered this turn (axcut parity). */
userMessageCheckpointId?: string;
+ /** Id the session stored the user's message under, once it did. The panel shows the message
+ * before that under a local id, which a rewind cannot name. */
+ userMessageId?: string;
}
export interface AiEditionChatRewindResult {