feat(characters): support character-scoped conversations
This commit is contained in:
@@ -4,6 +4,8 @@ import { type Dispatch, type ReactNode, useMemo } from "react";
|
||||
import { createActorContext, shallowEqual } from "@xstate/react";
|
||||
import type { SnapshotFrom } from "xstate";
|
||||
|
||||
import { DEFAULT_CHARACTER_ID } from "@/data/constants/character";
|
||||
|
||||
import { chatMachine } from "./chat-machine";
|
||||
import type { ChatEvent, ChatState as MachineContext } from "./chat-machine";
|
||||
import { appendPromotionMessage } from "./helper/promotion";
|
||||
@@ -15,6 +17,7 @@ import { appendPromotionMessage } from "./helper/promotion";
|
||||
* 消费方(chat-screen)通过 `useAuthState()` 取 loginStatus,本地派生 isGuest。
|
||||
*/
|
||||
interface ChatState {
|
||||
characterId: string;
|
||||
messages: MachineContext["messages"];
|
||||
historyMessages: MachineContext["messages"];
|
||||
promotion: MachineContext["promotion"];
|
||||
@@ -43,10 +46,18 @@ const ChatActorContext = createActorContext(chatMachine);
|
||||
|
||||
export interface ChatProviderProps {
|
||||
children: ReactNode;
|
||||
characterId?: string;
|
||||
}
|
||||
|
||||
export function ChatProvider({ children }: ChatProviderProps) {
|
||||
return <ChatActorContext.Provider>{children}</ChatActorContext.Provider>;
|
||||
export function ChatProvider({
|
||||
children,
|
||||
characterId = DEFAULT_CHARACTER_ID,
|
||||
}: ChatProviderProps) {
|
||||
return (
|
||||
<ChatActorContext.Provider options={{ input: { characterId } }}>
|
||||
{children}
|
||||
</ChatActorContext.Provider>
|
||||
);
|
||||
}
|
||||
|
||||
export function useChatState(): ChatState {
|
||||
@@ -73,6 +84,7 @@ type SelectedChatState = Omit<ChatState, "messages">;
|
||||
|
||||
function selectChatState(state: ChatSnapshot): SelectedChatState {
|
||||
return {
|
||||
characterId: state.context.characterId,
|
||||
historyMessages: state.context.messages,
|
||||
promotion: state.context.promotion,
|
||||
outgoingMessageRevision: state.context.outgoingMessageRevision,
|
||||
|
||||
Reference in New Issue
Block a user