refactor(chat): export flow capabilities individually

This commit is contained in:
2026-07-06 15:13:19 +08:00
parent 99ac24e901
commit 3b53fc9bad
6 changed files with 245 additions and 197 deletions
+5 -10
View File
@@ -17,8 +17,7 @@ const log = new Logger("StoresChatChatHistoryFlow");
type UiMessage = import("@/data/dto/chat").UiMessage;
export const chatHistoryActors = {
loadHistory: fromCallback<ChatEvent>(({ sendBack }) => {
export const loadHistoryActor = fromCallback<ChatEvent>(({ sendBack }) => {
let cancelled = false;
void (async () => {
@@ -46,12 +45,12 @@ export const chatHistoryActors = {
return () => {
cancelled = true;
};
}),
});
loadMoreHistory: fromPromise<
export const loadMoreHistoryActor = fromPromise<
{ messages: UiMessage[]; hasMore: boolean; newOffset: number },
{ offset: number }
>(async ({ input }) => {
>(async ({ input }) => {
const chatRepo = getChatRepository();
const result = await chatRepo.getHistory(PAGE_SIZE, input.offset);
if (Result.isErr(result)) {
@@ -67,8 +66,4 @@ export const chatHistoryActors = {
hasMore: page.length >= PAGE_SIZE,
newOffset: input.offset + page.length,
};
}),
};
export const loadHistoryActor = chatHistoryActors.loadHistory;
export const loadMoreHistoryActor = chatHistoryActors.loadMoreHistory;
});
-3
View File
@@ -7,17 +7,14 @@
* - chat-unlock-flow
*/
export {
chatHistoryActors,
loadHistoryActor,
loadMoreHistoryActor,
} from "./chat-history-flow";
export {
chatSendActors,
httpMessageQueueActor,
sendMessageHttpActor,
} from "./chat-send-flow";
export {
chatUnlockActors,
unlockHistoryActor,
unlockMessageActor,
type UnlockMessageOutput,
+56 -10
View File
@@ -39,10 +39,38 @@ import {
shouldAutoUnlockHistory,
shouldPromptUnlockHistory,
} from "./chat-machine.helpers";
import { chatHistoryActors } from "./chat-history-flow";
import { chatMediaActions } from "./chat-media-flow";
import { chatSendActions, chatSendActors } from "./chat-send-flow";
import { chatUnlockActions, chatUnlockActors } from "./chat-unlock-flow";
import {
loadHistoryActor,
loadMoreHistoryActor,
} from "./chat-history-flow";
import {
appendGuestUserImageAction,
appendUserImageAction,
} from "./chat-media-flow";
import {
appendGuestUserMessageAction,
appendQueuedSendErrorMessageAction,
appendUserMessageAction,
applyQueuedHttpOutputAction,
clearUpgradePromptAction,
httpMessageQueueActor,
markQueuedSendStartedAction,
sendMessageHttpActor,
} from "./chat-send-flow";
import {
applyUnlockMessageSucceededAction,
clearUnlockPaywallRequestAction,
dismissUnlockHistoryPromptAction,
markPaymentUnlockPendingAction,
markUnlockHistoryFailedAction,
markUnlockHistoryStartedAction,
markUnlockMessageStartedAction,
requestUnlockPaymentFromErrorAction,
requestUnlockPaymentFromOutputAction,
showUnlockHistoryPromptAction,
unlockHistoryActor,
unlockMessageActor,
} from "./chat-unlock-flow";
// 重新导出 State / Event 类型,保持 machine 文件的公共 API 不变
export type { ChatState } from "./chat-state";
@@ -58,14 +86,32 @@ export const chatMachine = setup({
events: {} as ChatEvent,
},
actors: {
...chatHistoryActors,
...chatSendActors,
...chatUnlockActors,
loadHistory: loadHistoryActor,
loadMoreHistory: loadMoreHistoryActor,
sendMessageHttp: sendMessageHttpActor,
httpMessageQueue: httpMessageQueueActor,
unlockHistory: unlockHistoryActor,
unlockMessage: unlockMessageActor,
},
actions: {
...chatSendActions,
...chatMediaActions,
...chatUnlockActions,
appendGuestUserMessage: appendGuestUserMessageAction,
appendUserMessage: appendUserMessageAction,
appendQueuedSendErrorMessage: appendQueuedSendErrorMessageAction,
markQueuedSendStarted: markQueuedSendStartedAction,
applyQueuedHttpOutput: applyQueuedHttpOutputAction,
clearUpgradePrompt: clearUpgradePromptAction,
appendGuestUserImage: appendGuestUserImageAction,
appendUserImage: appendUserImageAction,
markPaymentUnlockPending: markPaymentUnlockPendingAction,
showUnlockHistoryPrompt: showUnlockHistoryPromptAction,
dismissUnlockHistoryPrompt: dismissUnlockHistoryPromptAction,
markUnlockHistoryStarted: markUnlockHistoryStartedAction,
markUnlockHistoryFailed: markUnlockHistoryFailedAction,
markUnlockMessageStarted: markUnlockMessageStartedAction,
applyUnlockMessageSucceeded: applyUnlockMessageSucceededAction,
requestUnlockPaymentFromOutput: requestUnlockPaymentFromOutputAction,
requestUnlockPaymentFromError: requestUnlockPaymentFromErrorAction,
clearUnlockPaywallRequest: clearUnlockPaywallRequestAction,
enqueueMessage: sendTo("messageQueue", ({ event }) => event),
startGuestSession: assign(() => ({
+8 -6
View File
@@ -5,8 +5,8 @@ import { beginPendingReply } from "./chat-machine.helpers";
const log = new Logger("StoresChatChatMediaFlow");
export const chatMediaActions = {
appendGuestUserImage: chatAssign(({ context, event }: ChatActionArgs) => {
export const appendGuestUserImageAction = chatAssign(
({ context, event }: ChatActionArgs) => {
if (event.type !== "ChatSendImage") return {};
const today = todayString();
@@ -28,9 +28,11 @@ export const chatMediaActions = {
upgradePromptVisible: false,
upgradeReason: null,
};
}),
},
);
appendUserImage: chatAssign(({ context, event }: ChatActionArgs) => {
export const appendUserImageAction = chatAssign(
({ context, event }: ChatActionArgs) => {
if (event.type !== "ChatSendImage") return {};
const today = todayString();
@@ -52,5 +54,5 @@ export const chatMediaActions = {
upgradePromptVisible: false,
upgradeReason: null,
};
}),
};
},
);
+28 -24
View File
@@ -23,21 +23,21 @@ const log = new Logger("StoresChatChatSendFlow");
type UiMessage = import("@/data/dto/chat").UiMessage;
export const chatSendActors = {
sendMessageHttp: fromPromise<
export const sendMessageHttpActor = fromPromise<
{ response: ChatSendResponse; reply: UiMessage | null },
{ content: string }
>(async ({ input }) => {
>(async ({ input }) => {
return sendMessageViaHttp(input.content);
}),
});
httpMessageQueue: fromCallback<ChatEvent>(({ sendBack, receive }) => {
export const httpMessageQueueActor = fromCallback<ChatEvent>(
({ sendBack, receive }) => {
return createMessageQueueActor(sendBack, receive);
}),
};
},
);
export const chatSendActions = {
appendGuestUserMessage: chatAssign(({ context, event }: ChatActionArgs) => {
export const appendGuestUserMessageAction = chatAssign(
({ context, event }: ChatActionArgs) => {
if (event.type !== "ChatSendMessage") return {};
const today = todayString();
@@ -60,9 +60,11 @@ export const chatSendActions = {
upgradePromptVisible: false,
upgradeReason: null,
};
}),
},
);
appendUserMessage: chatAssign(({ context, event }: ChatActionArgs) => {
export const appendUserMessageAction = chatAssign(
({ context, event }: ChatActionArgs) => {
if (event.type !== "ChatSendMessage") return {};
const today = todayString();
@@ -84,9 +86,11 @@ export const chatSendActions = {
upgradePromptVisible: false,
upgradeReason: null,
};
}),
},
);
appendQueuedSendErrorMessage: chatAssign(({ context }: ChatContextArgs) => {
export const appendQueuedSendErrorMessageAction = chatAssign(
({ context }: ChatContextArgs) => {
const messages = [
...context.messages,
{
@@ -96,29 +100,29 @@ export const chatSendActions = {
},
];
return { messages, ...finishPendingReply(context) };
}),
},
);
markQueuedSendStarted: chatAssign(({ context }: ChatContextArgs) =>
export const markQueuedSendStartedAction = chatAssign(
({ context }: ChatContextArgs) =>
beginPendingReply(context),
),
);
applyQueuedHttpOutput: chatAssign(({ context, event }: ChatActionArgs) => {
export const applyQueuedHttpOutputAction = chatAssign(
({ context, event }: ChatActionArgs) => {
if (event.type !== "ChatQueuedHttpDone") return {};
return applyHttpSendOutput(context, event.output);
}),
},
);
clearUpgradePrompt: chatAssign(() => ({
export const clearUpgradePromptAction = chatAssign(() => ({
upgradePromptVisible: false,
upgradeReason: null,
canSendMessage: true,
cannotSendReason: null,
requiredCredits: 0,
shortfallCredits: 0,
})),
};
export const sendMessageHttpActor = chatSendActors.sendMessageHttp;
export const httpMessageQueueActor = chatSendActors.httpMessageQueue;
}));
function createMessageQueueActor(
sendBack: (event: ChatEvent) => void,
+38 -34
View File
@@ -19,15 +19,14 @@ const log = new Logger("StoresChatChatUnlockFlow");
type UiMessage = import("@/data/dto/chat").UiMessage;
export const chatUnlockActors = {
unlockHistory: fromPromise<{
export const unlockHistoryActor = fromPromise<{
unlocked: boolean;
reason: string;
shortfallCredits: number;
messages: UiMessage[];
hasMore: boolean;
newOffset: number;
}>(async () => {
}>(async () => {
const chatRepo = getChatRepository();
const unlockResult = await chatRepo.unlockHistory();
if (Result.isErr(unlockResult)) {
@@ -46,10 +45,12 @@ export const chatUnlockActors = {
hasMore: history.hasMore,
newOffset: history.newOffset,
};
}),
});
unlockMessage: fromPromise<UnlockMessageOutput, { messageId: string }>(
async ({ input }) => {
export const unlockMessageActor = fromPromise<
UnlockMessageOutput,
{ messageId: string }
>(async ({ input }) => {
const chatRepo = getChatRepository();
const unlockResult = await chatRepo.unlockPrivateMessage(input.messageId);
if (Result.isErr(unlockResult)) {
@@ -77,43 +78,43 @@ export const chatUnlockActors = {
messageId: input.messageId,
response: unlockResult.data,
};
},
),
};
});
export const chatUnlockActions = {
markPaymentUnlockPending: chatAssign(() => ({
export const markPaymentUnlockPendingAction = chatAssign(() => ({
paymentUnlockPending: true,
unlockHistoryError: null,
})),
}));
showUnlockHistoryPrompt: chatAssign(({ context }: ChatContextArgs) => ({
export const showUnlockHistoryPromptAction = chatAssign(
({ context }: ChatContextArgs) => ({
paymentUnlockPending: false,
unlockHistoryPromptVisible: true,
lockedHistoryCount: countLockedHistoryMessages(context.messages),
unlockHistoryError: null,
})),
}),
);
dismissUnlockHistoryPrompt: chatAssign(() => ({
export const dismissUnlockHistoryPromptAction = chatAssign(() => ({
paymentUnlockPending: false,
unlockHistoryPromptVisible: false,
unlockHistoryError: null,
})),
}));
markUnlockHistoryStarted: chatAssign(() => ({
export const markUnlockHistoryStartedAction = chatAssign(() => ({
paymentUnlockPending: false,
unlockHistoryPromptVisible: false,
isUnlockingHistory: true,
unlockHistoryError: null,
})),
}));
markUnlockHistoryFailed: chatAssign(() => ({
export const markUnlockHistoryFailedAction = chatAssign(() => ({
isUnlockingHistory: false,
unlockHistoryPromptVisible: true,
unlockHistoryError: "Failed to unlock messages. Please try again.",
})),
}));
markUnlockMessageStarted: chatAssign(({ event }: ChatActionArgs) => {
export const markUnlockMessageStartedAction = chatAssign(
({ event }: ChatActionArgs) => {
if (event.type !== "ChatUnlockMessageRequested") return {};
return {
isUnlockingMessage: true,
@@ -122,9 +123,11 @@ export const chatUnlockActions = {
unlockMessageError: null,
unlockPaywallRequest: null,
};
}),
},
);
applyUnlockMessageSucceeded: chatAssign(({ context, event }: ChatActionArgs) => {
export const applyUnlockMessageSucceededAction = chatAssign(
({ context, event }: ChatActionArgs) => {
const output = (event as { output?: UnlockMessageOutput }).output;
if (!output) return {};
return {
@@ -139,9 +142,11 @@ export const chatUnlockActions = {
requiredCredits: 0,
shortfallCredits: 0,
};
}),
},
);
requestUnlockPaymentFromOutput: chatAssign(({ context, event }: ChatActionArgs) => {
export const requestUnlockPaymentFromOutputAction = chatAssign(
({ context, event }: ChatActionArgs) => {
const output = (event as { output?: UnlockMessageOutput }).output;
if (!output) return {};
return {
@@ -161,9 +166,11 @@ export const chatUnlockActions = {
requiredCredits: output.response.requiredCredits,
shortfallCredits: output.response.shortfallCredits,
};
}),
},
);
requestUnlockPaymentFromError: chatAssign(({ context }: ChatContextArgs) => ({
export const requestUnlockPaymentFromErrorAction = chatAssign(
({ context }: ChatContextArgs) => ({
isUnlockingMessage: false,
unlockMessageError: "unlock_failed",
unlockPaywallRequest:
@@ -179,13 +186,10 @@ export const chatUnlockActions = {
: null,
unlockingMessageId: null,
unlockingMessageKind: null,
})),
}),
);
clearUnlockPaywallRequest: chatAssign(() => ({
export const clearUnlockPaywallRequestAction = chatAssign(() => ({
unlockPaywallRequest: null,
})),
};
export const unlockHistoryActor = chatUnlockActors.unlockHistory;
export const unlockMessageActor = chatUnlockActors.unlockMessage;
}));
export type { UnlockMessageOutput } from "./chat-machine.helpers";