refactor(chat): reorganize helper functions and update imports

This commit is contained in:
2026-07-15 10:59:23 +08:00
parent 2b90f90ab0
commit ed3e800245
20 changed files with 27 additions and 29 deletions
@@ -7,7 +7,7 @@ import {
countLockedHistoryMessages, countLockedHistoryMessages,
localMessagesToUi, localMessagesToUi,
sendResponseToUiMessage, sendResponseToUiMessage,
} from "@/stores/chat/chat-machine.helpers"; } from "@/stores/chat/helper";
function makeResponse( function makeResponse(
overrides: Partial<Parameters<typeof ChatSendResponse.from>[0]> = {}, overrides: Partial<Parameters<typeof ChatSendResponse.from>[0]> = {},
@@ -10,7 +10,7 @@ import type { ChatEvent } from "@/stores/chat/chat-events";
import type { import type {
UnlockMessageOutput as MachineUnlockMessageOutput, UnlockMessageOutput as MachineUnlockMessageOutput,
UnlockMessageRequest, UnlockMessageRequest,
} from "@/stores/chat/chat-machine.helpers"; } from "@/stores/chat/helper/unlock";
export interface SendMessageHttpOutput { export interface SendMessageHttpOutput {
response: ChatSendResponse; response: ChatSendResponse;
@@ -6,7 +6,7 @@ import {
appendPromotionMessage, appendPromotionMessage,
applyPromotionUnlockOutput, applyPromotionUnlockOutput,
createChatPromotionState, createChatPromotionState,
} from "@/stores/chat/chat-promotion"; } from "@/stores/chat/helper/promotion";
const promotion: PendingChatPromotion = { const promotion: PendingChatPromotion = {
promotionType: "image", promotionType: "image",
+1 -1
View File
@@ -6,7 +6,7 @@ import type { SnapshotFrom } from "xstate";
import { chatMachine } from "./chat-machine"; import { chatMachine } from "./chat-machine";
import type { ChatEvent, ChatState as MachineContext } from "./chat-machine"; import type { ChatEvent, ChatState as MachineContext } from "./chat-machine";
import { appendPromotionMessage } from "./chat-promotion"; import { appendPromotionMessage } from "./helper/promotion";
/** /**
* 对外暴露的 State 形状 * 对外暴露的 State 形状
+1 -1
View File
@@ -57,6 +57,6 @@ export type ChatEvent =
| { type: "ChatQueuedSendStarted" } | { type: "ChatQueuedSendStarted" }
| { | {
type: "ChatQueuedHttpDone"; type: "ChatQueuedHttpDone";
output: import("./chat-machine.helpers").HttpSendOutput; output: import("./helper/send-state").HttpSendOutput;
} }
| { type: "ChatQueuedSendError"; content: string; errorMessage: string }; | { type: "ChatQueuedSendError"; content: string; errorMessage: string };
+2 -4
View File
@@ -5,10 +5,8 @@ import { Logger } from "@/utils/logger";
import { Result } from "@/utils/result"; import { Result } from "@/utils/result";
import { todayString } from "@/utils/date"; import { todayString } from "@/utils/date";
import { import { CHAT_HISTORY_LIMIT } from "./helper/history";
CHAT_HISTORY_LIMIT, import { localMessagesToUi } from "./helper/message-mappers";
localMessagesToUi,
} from "./chat-machine.helpers";
const log = new Logger("StoresChatChatHistorySync"); const log = new Logger("StoresChatChatHistorySync");
+1 -1
View File
@@ -2,7 +2,7 @@ import type { UiMessage } from "@/data/dto/chat";
import type { PendingChatUnlockKind } from "@/lib/navigation/chat_unlock_session"; import type { PendingChatUnlockKind } from "@/lib/navigation/chat_unlock_session";
import type { ChatLockType } from "@/data/schemas/chat"; import type { ChatLockType } from "@/data/schemas/chat";
import type { PendingChatPromotion } from "@/data/storage/navigation"; import type { PendingChatPromotion } from "@/data/storage/navigation";
import type { ChatPromotionState } from "./chat-promotion"; import type { ChatPromotionState } from "./helper/promotion";
export type ChatUpgradeReason = "insufficient_credits"; export type ChatUpgradeReason = "insufficient_credits";
@@ -1,15 +1,10 @@
import type { UiMessage } from "@/data/dto/chat"; 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. // The initial history request remains bounded even though pagination is disabled.
export const CHAT_HISTORY_LIMIT = 50; 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( export function applyHistoryLoadedOutput(
output: { output: {
messages: UiMessage[]; messages: UiMessage[];
+5
View File
@@ -0,0 +1,5 @@
export * from "./history";
export * from "./message-mappers";
export * from "./promotion";
export * from "./send-state";
export * from "./unlock";
@@ -5,7 +5,7 @@ import { todayString } from "@/utils/date";
import { import {
applySingleUnlockOutput, applySingleUnlockOutput,
type UnlockMessageOutput, type UnlockMessageOutput,
} from "./chat-unlock-helpers"; } from "./unlock";
const PROMOTION_COPY = { const PROMOTION_COPY = {
voice: "I left a voice message for you. Unlock it to listen.", voice: "I left a voice message for you. Unlock it to listen.",
@@ -1,6 +1,6 @@
import { type ChatSendResponse, type UiMessage } from "@/data/dto/chat"; 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 = { export type HttpSendOutput = {
response: ChatSendResponse; response: ChatSendResponse;
+1 -1
View File
@@ -4,6 +4,6 @@
export * from "./chat-context"; export * from "./chat-context";
export * from "./chat-events"; export * from "./chat-events";
export * from "./chat-machine.helpers"; export * from "./helper";
export * from "./chat-machine"; export * from "./chat-machine";
export * from "./chat-state"; export * from "./chat-state";
+1 -1
View File
@@ -9,7 +9,7 @@ import { Logger } from "@/utils/logger";
import { Result } from "@/utils/result"; import { Result } from "@/utils/result";
import type { ChatEvent } from "../../chat-events"; import type { ChatEvent } from "../../chat-events";
import { sendResponseToUiMessage } from "../../chat-machine.helpers"; import { sendResponseToUiMessage } from "../../helper/message-mappers";
const log = new Logger("StoresChatChatSendFlow"); const log = new Logger("StoresChatChatSendFlow");
+1 -1
View File
@@ -9,7 +9,7 @@ import { readAndSyncHistory } from "../../chat-history-sync";
import { import {
type UnlockMessageRequest, type UnlockMessageRequest,
type UnlockMessageOutput, type UnlockMessageOutput,
} from "../../chat-machine.helpers"; } from "../../helper/unlock";
const log = new Logger("StoresChatChatUnlockFlow"); const log = new Logger("StoresChatChatUnlockFlow");
+3 -1
View File
@@ -1,9 +1,11 @@
import { import {
applyHistoryLoadedOutput, applyHistoryLoadedOutput,
applyNetworkHistoryLoadedOutput, applyNetworkHistoryLoadedOutput,
} from "../helper/history";
import {
countLockedHistoryMessages, countLockedHistoryMessages,
shouldPromptUnlockHistory, shouldPromptUnlockHistory,
} from "../chat-machine.helpers"; } from "../helper/unlock";
import { baseChatMachineSetup } from "./setup"; import { baseChatMachineSetup } from "./setup";
const applyLocalHistoryLoadedAction = baseChatMachineSetup.assign( const applyLocalHistoryLoadedAction = baseChatMachineSetup.assign(
+1 -1
View File
@@ -8,7 +8,7 @@ import {
beginPendingReply, beginPendingReply,
finishPendingReply, finishPendingReply,
type HttpSendOutput, type HttpSendOutput,
} from "../chat-machine.helpers"; } from "../helper/send-state";
import { historyMachineSetup } from "./history-flow"; import { historyMachineSetup } from "./history-flow";
import { createChatActorActionSetup } from "./setup"; import { createChatActorActionSetup } from "./setup";
+2 -4
View File
@@ -1,7 +1,5 @@
import { import { createChatPromotionState } from "../helper/promotion";
createChatPromotionState, import { shouldPromptUnlockHistory } from "../helper/unlock";
shouldPromptUnlockHistory,
} from "../chat-machine.helpers";
import { initialState } from "../chat-state"; import { initialState } from "../chat-state";
import { import {
guestInitializingState, guestInitializingState,
+2 -2
View File
@@ -1,11 +1,11 @@
import { type DoneActorEvent } from "xstate"; import { type DoneActorEvent } from "xstate";
import { applyPromotionUnlockOutput } from "../helper/promotion";
import { import {
applyPromotionUnlockOutput,
applySingleUnlockOutput, applySingleUnlockOutput,
countLockedHistoryMessages, countLockedHistoryMessages,
type UnlockMessageOutput, type UnlockMessageOutput,
} from "../chat-machine.helpers"; } from "../helper/unlock";
import type { UnlockHistoryOutput } from "./actors/unlock"; import type { UnlockHistoryOutput } from "./actors/unlock";
import { sendMachineSetup } from "./send-flow"; import { sendMachineSetup } from "./send-flow";
import { createChatActorActionSetup } from "./setup"; import { createChatActorActionSetup } from "./setup";