From 1b85f3a410e8b5917c6e0a4ccbd768898bd94c79 Mon Sep 17 00:00:00 2001 From: chenhang Date: Fri, 10 Jul 2026 10:48:40 +0800 Subject: [PATCH] fix(chat): keep single locked history message unchanged --- .../chat-machine.transitions.test.ts | 11 +++--- src/stores/chat/chat-machine.ts | 34 +++---------------- src/stores/chat/chat-unlock-helpers.ts | 4 --- 3 files changed, 10 insertions(+), 39 deletions(-) diff --git a/src/stores/chat/__tests__/chat-machine.transitions.test.ts b/src/stores/chat/__tests__/chat-machine.transitions.test.ts index 2b860a62..bfb88462 100644 --- a/src/stores/chat/__tests__/chat-machine.transitions.test.ts +++ b/src/stores/chat/__tests__/chat-machine.transitions.test.ts @@ -453,7 +453,7 @@ describe("chatMachine transitions", () => { actor.stop(); }); - it("auto unlocks a single locked image message after payment", async () => { + it("keeps a single locked image message locked after payment", async () => { const actor = createActor( createTestChatMachine({ historyMessages: [ @@ -495,16 +495,15 @@ describe("chatMachine transitions", () => { ); actor.send({ type: "ChatPaymentSucceeded" }); - await waitFor(actor, (snapshot) => - snapshot.matches({ userSession: "ready" }), - ); + expect(actor.getSnapshot().matches({ userSession: "ready" })).toBe(true); expect(actor.getSnapshot().context.unlockHistoryPromptVisible).toBe(false); expect(actor.getSnapshot().context.messages).toMatchObject([ { id: "msg-image-locked", - imageUrl: "https://example.com/unlocked.jpg", - locked: false, + imageUrl: "https://example.com/locked.jpg", + imagePaywalled: true, + locked: true, }, ]); diff --git a/src/stores/chat/chat-machine.ts b/src/stores/chat/chat-machine.ts index bace480e..24a64452 100644 --- a/src/stores/chat/chat-machine.ts +++ b/src/stores/chat/chat-machine.ts @@ -36,7 +36,6 @@ import { applyHistoryLoadedOutput, applyNetworkHistoryLoadedOutput, countLockedHistoryMessages, - shouldAutoUnlockHistory, shouldPromptUnlockHistory, } from "./chat-machine.helpers"; import { @@ -290,17 +289,6 @@ export const chatMachine = setup({ actions: "clearChatSession", }, ChatNetworkHistoryLoaded: [ - { - guard: ({ context, event }) => - event.type === "ChatNetworkHistoryLoaded" && - context.paymentUnlockPending && - shouldAutoUnlockHistory(event.output.messages), - target: ".unlockingHistory", - actions: [ - "applyNetworkHistoryLoadedAndClearPayment", - "markUnlockHistoryStarted", - ], - }, { guard: ({ context, event }) => event.type === "ChatNetworkHistoryLoaded" && @@ -309,6 +297,11 @@ export const chatMachine = setup({ target: ".ready", actions: "showUnlockHistoryPromptFromNetwork", }, + { + guard: ({ context }) => context.paymentUnlockPending, + target: ".ready", + actions: "applyNetworkHistoryLoadedAndClearPayment", + }, { guard: ({ context }) => !context.isUnlockingHistory && !context.isUnlockingMessage, @@ -328,12 +321,6 @@ export const chatMachine = setup({ actions: "appendQueuedSendErrorMessage", }, ChatPaymentSucceeded: [ - { - guard: ({ context }) => - context.historyLoaded && shouldAutoUnlockHistory(context.messages), - target: ".unlockingHistory", - actions: ["clearUpgradePrompt", "markUnlockHistoryStarted"], - }, { guard: ({ context }) => context.historyLoaded && @@ -369,17 +356,6 @@ export const chatMachine = setup({ actions: "applyLocalHistoryLoaded", }, ChatNetworkHistoryLoaded: [ - { - guard: ({ context, event }) => - event.type === "ChatNetworkHistoryLoaded" && - context.paymentUnlockPending && - shouldAutoUnlockHistory(event.output.messages), - target: "unlockingHistory", - actions: [ - "applyNetworkHistoryLoadedAndClearPayment", - "markUnlockHistoryStarted", - ], - }, { guard: ({ context, event }) => event.type === "ChatNetworkHistoryLoaded" && diff --git a/src/stores/chat/chat-unlock-helpers.ts b/src/stores/chat/chat-unlock-helpers.ts index 661a6386..6b8208bf 100644 --- a/src/stores/chat/chat-unlock-helpers.ts +++ b/src/stores/chat/chat-unlock-helpers.ts @@ -43,10 +43,6 @@ function shouldApplySingleUnlock(message: UiMessage, messageId: string): boolean ); } -export function shouldAutoUnlockHistory(messages: readonly UiMessage[]): boolean { - return countLockedHistoryMessages(messages) === 1; -} - export function shouldPromptUnlockHistory(messages: readonly UiMessage[]): boolean { return countLockedHistoryMessages(messages) > 1; }