Files
cozsweet-frontend-nextjs/src/stores/chat/chat-state.ts
T

101 lines
2.9 KiB
TypeScript

import type { UiMessage } from "@/stores/chat/ui-message";
import type { PendingChatUnlockKind } from "@/lib/navigation/chat_unlock_session";
import type { ChatLockType } from "@/data/schemas/chat";
import type { PendingChatPromotion } from "@/data/storage/navigation";
import {
DEFAULT_CHARACTER,
DEFAULT_CHARACTER_ID,
} from "@/data/constants/character";
import type { ChatPromotionState } from "./helper/promotion";
import type { CharacterErrorCode } from "@/data/services/api";
export type ChatUpgradeReason = "insufficient_credits";
export interface ChatUnlockMessageRequest {
displayMessageId: string;
messageId?: string;
kind: PendingChatUnlockKind;
lockType?: ChatLockType;
clientLockId?: string;
}
export interface ChatUnlockPaywallRequest
extends ChatUnlockMessageRequest {
promotion?: PendingChatPromotion;
reason: string;
creditBalance: number;
requiredCredits: number;
shortfallCredits: number;
}
export interface ChatState {
characterId: string;
characterErrorCode: CharacterErrorCode | null;
emptyChatGreeting: string;
messages: UiMessage[];
promotion: ChatPromotionState | null;
outgoingMessageRevision: number;
isReplyingAI: boolean;
pendingReplyCount: number;
upgradePromptVisible: boolean;
upgradeReason: ChatUpgradeReason | null;
canSendMessage: boolean;
creditBalance: number;
creditsCharged: number;
requiredCredits: number;
shortfallCredits: number;
/** history 首屏可展示标志(本地快照加载完成即可进入 ready)。 */
historyLoaded: boolean;
historyTotal: number;
historyLimit: number;
nextHistoryOffset: number;
isLoadingMoreHistory: boolean;
paymentUnlockPending: boolean;
unlockHistoryPromptVisible: boolean;
lockedHistoryCount: number;
unlockHistoryError: string | null;
unlockingMessage: ChatUnlockMessageRequest | null;
unlockMessageError: string | null;
unlockPaywallRequest: ChatUnlockPaywallRequest | null;
}
export function createInitialChatState(
characterId: string,
emptyChatGreeting: string,
): ChatState {
return {
characterId,
characterErrorCode: null,
emptyChatGreeting,
messages: [],
promotion: null,
outgoingMessageRevision: 0,
isReplyingAI: false,
pendingReplyCount: 0,
upgradePromptVisible: false,
upgradeReason: null,
canSendMessage: true,
creditBalance: 0,
creditsCharged: 0,
requiredCredits: 0,
shortfallCredits: 0,
historyLoaded: false,
historyTotal: 0,
historyLimit: 50,
nextHistoryOffset: 0,
isLoadingMoreHistory: false,
paymentUnlockPending: false,
unlockHistoryPromptVisible: false,
lockedHistoryCount: 0,
unlockHistoryError: null,
unlockingMessage: null,
unlockMessageError: null,
unlockPaywallRequest: null,
};
}
export const initialState: ChatState = createInitialChatState(
DEFAULT_CHARACTER_ID,
DEFAULT_CHARACTER.emptyChatGreeting,
);