From ed3e800245ac6f3ceafedb9361b41ea61f82839f Mon Sep 17 00:00:00 2001 From: chenhang Date: Wed, 15 Jul 2026 10:59:23 +0800 Subject: [PATCH] refactor(chat): reorganize helper functions and update imports --- .../{chat-machine.helpers.test.ts => chat-helpers.test.ts} | 2 +- src/stores/chat/__tests__/chat-machine.test-utils.ts | 2 +- src/stores/chat/__tests__/chat-promotion.test.ts | 2 +- src/stores/chat/chat-context.tsx | 2 +- src/stores/chat/chat-events.ts | 2 +- src/stores/chat/chat-history-sync.ts | 6 ++---- src/stores/chat/chat-state.ts | 2 +- .../chat/{chat-machine.helpers.ts => helper/history.ts} | 7 +------ src/stores/chat/helper/index.ts | 5 +++++ .../{chat-message-mappers.ts => helper/message-mappers.ts} | 0 src/stores/chat/{chat-promotion.ts => helper/promotion.ts} | 2 +- .../chat/{chat-send-state.ts => helper/send-state.ts} | 2 +- .../chat/{chat-unlock-helpers.ts => helper/unlock.ts} | 0 src/stores/chat/index.ts | 2 +- src/stores/chat/machine/actors/send.ts | 2 +- src/stores/chat/machine/actors/unlock.ts | 2 +- src/stores/chat/machine/history-flow.ts | 4 +++- src/stores/chat/machine/send-flow.ts | 2 +- src/stores/chat/machine/session-flow.ts | 6 ++---- src/stores/chat/machine/unlock-flow.ts | 4 ++-- 20 files changed, 27 insertions(+), 29 deletions(-) rename src/stores/chat/__tests__/{chat-machine.helpers.test.ts => chat-helpers.test.ts} (99%) rename src/stores/chat/{chat-machine.helpers.ts => helper/history.ts} (80%) create mode 100644 src/stores/chat/helper/index.ts rename src/stores/chat/{chat-message-mappers.ts => helper/message-mappers.ts} (100%) rename src/stores/chat/{chat-promotion.ts => helper/promotion.ts} (98%) rename src/stores/chat/{chat-send-state.ts => helper/send-state.ts} (97%) rename src/stores/chat/{chat-unlock-helpers.ts => helper/unlock.ts} (100%) diff --git a/src/stores/chat/__tests__/chat-machine.helpers.test.ts b/src/stores/chat/__tests__/chat-helpers.test.ts similarity index 99% rename from src/stores/chat/__tests__/chat-machine.helpers.test.ts rename to src/stores/chat/__tests__/chat-helpers.test.ts index ea5c4714..60a2b6d8 100644 --- a/src/stores/chat/__tests__/chat-machine.helpers.test.ts +++ b/src/stores/chat/__tests__/chat-helpers.test.ts @@ -7,7 +7,7 @@ import { countLockedHistoryMessages, localMessagesToUi, sendResponseToUiMessage, -} from "@/stores/chat/chat-machine.helpers"; +} from "@/stores/chat/helper"; function makeResponse( overrides: Partial[0]> = {}, diff --git a/src/stores/chat/__tests__/chat-machine.test-utils.ts b/src/stores/chat/__tests__/chat-machine.test-utils.ts index a25f019b..0cf2c630 100644 --- a/src/stores/chat/__tests__/chat-machine.test-utils.ts +++ b/src/stores/chat/__tests__/chat-machine.test-utils.ts @@ -10,7 +10,7 @@ import type { ChatEvent } from "@/stores/chat/chat-events"; import type { UnlockMessageOutput as MachineUnlockMessageOutput, UnlockMessageRequest, -} from "@/stores/chat/chat-machine.helpers"; +} from "@/stores/chat/helper/unlock"; export interface SendMessageHttpOutput { response: ChatSendResponse; diff --git a/src/stores/chat/__tests__/chat-promotion.test.ts b/src/stores/chat/__tests__/chat-promotion.test.ts index 06402167..352a9f0e 100644 --- a/src/stores/chat/__tests__/chat-promotion.test.ts +++ b/src/stores/chat/__tests__/chat-promotion.test.ts @@ -6,7 +6,7 @@ import { appendPromotionMessage, applyPromotionUnlockOutput, createChatPromotionState, -} from "@/stores/chat/chat-promotion"; +} from "@/stores/chat/helper/promotion"; const promotion: PendingChatPromotion = { promotionType: "image", diff --git a/src/stores/chat/chat-context.tsx b/src/stores/chat/chat-context.tsx index 02150ea5..b6069d77 100644 --- a/src/stores/chat/chat-context.tsx +++ b/src/stores/chat/chat-context.tsx @@ -6,7 +6,7 @@ import type { SnapshotFrom } from "xstate"; import { chatMachine } from "./chat-machine"; import type { ChatEvent, ChatState as MachineContext } from "./chat-machine"; -import { appendPromotionMessage } from "./chat-promotion"; +import { appendPromotionMessage } from "./helper/promotion"; /** * 对外暴露的 State 形状 diff --git a/src/stores/chat/chat-events.ts b/src/stores/chat/chat-events.ts index bf50d502..c4cc4bb9 100644 --- a/src/stores/chat/chat-events.ts +++ b/src/stores/chat/chat-events.ts @@ -57,6 +57,6 @@ export type ChatEvent = | { type: "ChatQueuedSendStarted" } | { type: "ChatQueuedHttpDone"; - output: import("./chat-machine.helpers").HttpSendOutput; + output: import("./helper/send-state").HttpSendOutput; } | { type: "ChatQueuedSendError"; content: string; errorMessage: string }; diff --git a/src/stores/chat/chat-history-sync.ts b/src/stores/chat/chat-history-sync.ts index 15003ef0..d828a4f1 100644 --- a/src/stores/chat/chat-history-sync.ts +++ b/src/stores/chat/chat-history-sync.ts @@ -5,10 +5,8 @@ import { Logger } from "@/utils/logger"; import { Result } from "@/utils/result"; import { todayString } from "@/utils/date"; -import { - CHAT_HISTORY_LIMIT, - localMessagesToUi, -} from "./chat-machine.helpers"; +import { CHAT_HISTORY_LIMIT } from "./helper/history"; +import { localMessagesToUi } from "./helper/message-mappers"; const log = new Logger("StoresChatChatHistorySync"); diff --git a/src/stores/chat/chat-state.ts b/src/stores/chat/chat-state.ts index cd34cfee..d95e2977 100644 --- a/src/stores/chat/chat-state.ts +++ b/src/stores/chat/chat-state.ts @@ -2,7 +2,7 @@ import type { UiMessage } from "@/data/dto/chat"; import type { PendingChatUnlockKind } from "@/lib/navigation/chat_unlock_session"; import type { ChatLockType } from "@/data/schemas/chat"; import type { PendingChatPromotion } from "@/data/storage/navigation"; -import type { ChatPromotionState } from "./chat-promotion"; +import type { ChatPromotionState } from "./helper/promotion"; export type ChatUpgradeReason = "insufficient_credits"; diff --git a/src/stores/chat/chat-machine.helpers.ts b/src/stores/chat/helper/history.ts similarity index 80% rename from src/stores/chat/chat-machine.helpers.ts rename to src/stores/chat/helper/history.ts index c57b6e04..f943ed2f 100644 --- a/src/stores/chat/chat-machine.helpers.ts +++ b/src/stores/chat/helper/history.ts @@ -1,15 +1,10 @@ import type { UiMessage } from "@/data/dto/chat"; -import type { ChatState } from "./chat-state"; +import type { ChatState } from "../chat-state"; // The initial history request remains bounded even though pagination is disabled. export const CHAT_HISTORY_LIMIT = 50; -export * from "./chat-message-mappers"; -export * from "./chat-promotion"; -export * from "./chat-send-state"; -export * from "./chat-unlock-helpers"; - export function applyHistoryLoadedOutput( output: { messages: UiMessage[]; diff --git a/src/stores/chat/helper/index.ts b/src/stores/chat/helper/index.ts new file mode 100644 index 00000000..c71e8642 --- /dev/null +++ b/src/stores/chat/helper/index.ts @@ -0,0 +1,5 @@ +export * from "./history"; +export * from "./message-mappers"; +export * from "./promotion"; +export * from "./send-state"; +export * from "./unlock"; diff --git a/src/stores/chat/chat-message-mappers.ts b/src/stores/chat/helper/message-mappers.ts similarity index 100% rename from src/stores/chat/chat-message-mappers.ts rename to src/stores/chat/helper/message-mappers.ts diff --git a/src/stores/chat/chat-promotion.ts b/src/stores/chat/helper/promotion.ts similarity index 98% rename from src/stores/chat/chat-promotion.ts rename to src/stores/chat/helper/promotion.ts index 640501b4..b1d7ffb6 100644 --- a/src/stores/chat/chat-promotion.ts +++ b/src/stores/chat/helper/promotion.ts @@ -5,7 +5,7 @@ import { todayString } from "@/utils/date"; import { applySingleUnlockOutput, type UnlockMessageOutput, -} from "./chat-unlock-helpers"; +} from "./unlock"; const PROMOTION_COPY = { voice: "I left a voice message for you. Unlock it to listen.", diff --git a/src/stores/chat/chat-send-state.ts b/src/stores/chat/helper/send-state.ts similarity index 97% rename from src/stores/chat/chat-send-state.ts rename to src/stores/chat/helper/send-state.ts index 149469c2..310d870b 100644 --- a/src/stores/chat/chat-send-state.ts +++ b/src/stores/chat/helper/send-state.ts @@ -1,6 +1,6 @@ import { type ChatSendResponse, type UiMessage } from "@/data/dto/chat"; -import type { ChatState, ChatUpgradeReason } from "./chat-state"; +import type { ChatState, ChatUpgradeReason } from "../chat-state"; export type HttpSendOutput = { response: ChatSendResponse; diff --git a/src/stores/chat/chat-unlock-helpers.ts b/src/stores/chat/helper/unlock.ts similarity index 100% rename from src/stores/chat/chat-unlock-helpers.ts rename to src/stores/chat/helper/unlock.ts diff --git a/src/stores/chat/index.ts b/src/stores/chat/index.ts index 20c7c620..60372e7f 100644 --- a/src/stores/chat/index.ts +++ b/src/stores/chat/index.ts @@ -4,6 +4,6 @@ export * from "./chat-context"; export * from "./chat-events"; -export * from "./chat-machine.helpers"; +export * from "./helper"; export * from "./chat-machine"; export * from "./chat-state"; diff --git a/src/stores/chat/machine/actors/send.ts b/src/stores/chat/machine/actors/send.ts index b73883b9..6794be25 100644 --- a/src/stores/chat/machine/actors/send.ts +++ b/src/stores/chat/machine/actors/send.ts @@ -9,7 +9,7 @@ import { Logger } from "@/utils/logger"; import { Result } from "@/utils/result"; import type { ChatEvent } from "../../chat-events"; -import { sendResponseToUiMessage } from "../../chat-machine.helpers"; +import { sendResponseToUiMessage } from "../../helper/message-mappers"; const log = new Logger("StoresChatChatSendFlow"); diff --git a/src/stores/chat/machine/actors/unlock.ts b/src/stores/chat/machine/actors/unlock.ts index 8d259a02..d0c9e427 100644 --- a/src/stores/chat/machine/actors/unlock.ts +++ b/src/stores/chat/machine/actors/unlock.ts @@ -9,7 +9,7 @@ import { readAndSyncHistory } from "../../chat-history-sync"; import { type UnlockMessageRequest, type UnlockMessageOutput, -} from "../../chat-machine.helpers"; +} from "../../helper/unlock"; const log = new Logger("StoresChatChatUnlockFlow"); diff --git a/src/stores/chat/machine/history-flow.ts b/src/stores/chat/machine/history-flow.ts index 9d1c6657..d6cb42f3 100644 --- a/src/stores/chat/machine/history-flow.ts +++ b/src/stores/chat/machine/history-flow.ts @@ -1,9 +1,11 @@ import { applyHistoryLoadedOutput, applyNetworkHistoryLoadedOutput, +} from "../helper/history"; +import { countLockedHistoryMessages, shouldPromptUnlockHistory, -} from "../chat-machine.helpers"; +} from "../helper/unlock"; import { baseChatMachineSetup } from "./setup"; const applyLocalHistoryLoadedAction = baseChatMachineSetup.assign( diff --git a/src/stores/chat/machine/send-flow.ts b/src/stores/chat/machine/send-flow.ts index 60cbbcd2..be28ceca 100644 --- a/src/stores/chat/machine/send-flow.ts +++ b/src/stores/chat/machine/send-flow.ts @@ -8,7 +8,7 @@ import { beginPendingReply, finishPendingReply, type HttpSendOutput, -} from "../chat-machine.helpers"; +} from "../helper/send-state"; import { historyMachineSetup } from "./history-flow"; import { createChatActorActionSetup } from "./setup"; diff --git a/src/stores/chat/machine/session-flow.ts b/src/stores/chat/machine/session-flow.ts index d4f0f421..04d08ff6 100644 --- a/src/stores/chat/machine/session-flow.ts +++ b/src/stores/chat/machine/session-flow.ts @@ -1,7 +1,5 @@ -import { - createChatPromotionState, - shouldPromptUnlockHistory, -} from "../chat-machine.helpers"; +import { createChatPromotionState } from "../helper/promotion"; +import { shouldPromptUnlockHistory } from "../helper/unlock"; import { initialState } from "../chat-state"; import { guestInitializingState, diff --git a/src/stores/chat/machine/unlock-flow.ts b/src/stores/chat/machine/unlock-flow.ts index a1c576db..1fdc6617 100644 --- a/src/stores/chat/machine/unlock-flow.ts +++ b/src/stores/chat/machine/unlock-flow.ts @@ -1,11 +1,11 @@ import { type DoneActorEvent } from "xstate"; +import { applyPromotionUnlockOutput } from "../helper/promotion"; import { - applyPromotionUnlockOutput, applySingleUnlockOutput, countLockedHistoryMessages, type UnlockMessageOutput, -} from "../chat-machine.helpers"; +} from "../helper/unlock"; import type { UnlockHistoryOutput } from "./actors/unlock"; import { sendMachineSetup } from "./send-flow"; import { createChatActorActionSetup } from "./setup";