From d87bfcc8596cfd0287241ed785f4854deca11afa Mon Sep 17 00:00:00 2001 From: chenhang Date: Thu, 18 Jun 2026 15:47:24 +0800 Subject: [PATCH] refactor(chat): remove commented-out code in guestSession --- src/stores/chat/chat-machine.actors.ts | 142 +++---------------------- src/stores/chat/chat-machine.ts | 8 -- 2 files changed, 15 insertions(+), 135 deletions(-) diff --git a/src/stores/chat/chat-machine.actors.ts b/src/stores/chat/chat-machine.actors.ts index 33a9e736..5774920e 100644 --- a/src/stores/chat/chat-machine.actors.ts +++ b/src/stores/chat/chat-machine.actors.ts @@ -53,10 +53,7 @@ export const loadQuotaActor = fromPromise<{ remaining: number; total: number; }>(async () => { - log.debug("[chat-machine] loadQuotaActor ENTRY"); - const result = await readGuestQuota(); - log.debug("[chat-machine] loadQuotaActor DONE", result); - return result; + return readGuestQuota(); }); // ============================================================ @@ -76,16 +73,7 @@ export const loadHistoryActor = fromPromise<{ localCount: number; networkCount: number; }>(async () => { - log.debug("[chat-machine] loadHistoryActor ENTRY"); - const result = await readAndSyncHistory(); - log.debug("[chat-machine] loadHistoryActor DONE", { - finalCount: result.messages.length, - localCount: result.localCount, - networkCount: result.networkCount, - hasMore: result.hasMore, - localOverwritten: result.localOverwritten, - }); - return result; + return readAndSyncHistory(); }); // ============================================================ @@ -100,83 +88,30 @@ export const loadHistoryActor = fromPromise<{ export const sendMessageHttpActor = fromPromise< { reply: UiMessage }, { content: string } ->(async ({ input, self }) => { - log.debug("[chat-machine] sendMessageHttpActor ENTRY", { - contentLength: input.content.length, - contentPreview: input.content.slice(0, 50), - selfId: self.id, - selfPath: "", - }); - log.debug("[chat-machine] sendMessageHttpActor calling chatRepo.sendMessage"); +>(async ({ input }) => { const result = await chatRepo.sendMessage(input.content); - log.debug("[chat-machine] sendMessageHttpActor chatRepo.sendMessage DONE", { - success: result.success, - error: result.success ? null : Result.isErr(result) ? result.error : null, - }); if (Result.isErr(result)) { - log.error("[chat-machine] sendMessageHttpActor result isErr, throwing"); + log.error("[chat-machine] sendMessageHttpActor failed", { error: result.error }); throw result.error; } - - // 一次发送 = 一次返回 —— 直接转 reply → UiMessage - log.debug("[chat-machine] sendMessageHttpActor converting reply to UiMessage", { - replyLength: result.data.reply.length, - replyPreview: result.data.reply.slice(0, 50), - messageId: result.data.messageId, - timestamp: result.data.timestamp, - }); - - const reply = sendResponseToUiMessage(result.data); - log.debug("[chat-machine] sendMessageHttpActor done", { - replyContentLength: reply.content.length, - }); - return { reply }; + return { reply: sendResponseToUiMessage(result.data) }; }); /** 翻历史(pagination)—— 不走 local-first,纯 server fetch */ export const loadMoreHistoryActor = fromPromise< { messages: UiMessage[]; hasMore: boolean; newOffset: number }, { offset: number } ->(async ({ input, self }) => { - log.debug("[chat-machine] loadMoreHistoryActor ENTRY", { - inputOffset: input.offset, - pageSize: PAGE_SIZE, - selfPath: "", - }); - log.debug("[chat-machine] loadMoreHistoryActor calling chatRepo.getHistory", { - pageSize: PAGE_SIZE, - offset: input.offset, - }); +>(async ({ input }) => { const result = await chatRepo.getHistory(PAGE_SIZE, input.offset); - log.debug("[chat-machine] loadMoreHistoryActor chatRepo.getHistory DONE", { - success: result.success, - error: result.success ? null : Result.isErr(result) ? result.error : null, - responseType: result.success ? typeof result.data : "err", - }); if (Result.isErr(result)) { - log.error("[chat-machine] loadMoreHistoryActor result isErr, throwing", { - error: result.error, - }); + log.error("[chat-machine] loadMoreHistoryActor failed", { error: result.error }); throw result.error; } - log.debug("[chat-machine] loadMoreHistoryActor result data", { - rawMessagesCount: result.data.messages.length, - }); const page = localMessagesToUi(result.data.messages); - log.debug("[chat-machine] loadMoreHistoryActor AFTER UiMessage mapping", { - uiMessagesCount: page.length, - }); - const hasMore = page.length >= PAGE_SIZE; - const newOffset = input.offset + page.length; - log.debug("[chat-machine] loadMoreHistoryActor result", { - pageSize: page.length, - hasMore, - newOffset, - }); return { messages: page, - hasMore, - newOffset, + hasMore: page.length >= PAGE_SIZE, + newOffset: input.offset + page.length, }; }); @@ -196,40 +131,14 @@ export const loadMoreHistoryActor = fromPromise< */ export const chatWebSocketActor = fromCallback( ({ sendBack, input }) => { - log.debug("[chat-machine] chatWebSocketActor ENTRY", { - hasToken: !!input.token, - tokenLength: input.token?.length ?? 0, - tokenPrefix: input.token?.slice(0, 10) ?? "EMPTY", - selfPath: "", - }); - log.debug("[chat-machine] chatWebSocketActor creating ChatWebSocket instance"); const ws = createChatWebSocket(input.token); - log.debug("[chat-machine] chatWebSocketActor createChatWebSocket DONE", { - wsType: ws.constructor.name, - }); - - // 写入 module-level 共享引用 —— sendMessageWsActor 借此调 ws.sendMessage() activeChatWebSocket = ws; - log.debug("[chat-machine] chatWebSocketActor activeChatWebSocket SET", { - isConnected: ws.isConnected, - }); ws.onConnected = (userId) => { - log.debug("[chat-machine] WS onConnected", { - userId, - userIdPrefix: userId?.slice(0, 8) ?? "EMPTY", - }); + log.debug("[chat-machine] WS connected", { userId }); sendBack({ type: "ChatWebSocketConnected", userId }); }; ws.onSentence = (p) => { - log.debug("[chat-machine] WS onSentence", { - index: p.index, - total: p.total, - done: p.done, - textLength: p.text.length, - textPreview: p.text.slice(0, 50), - textSuffix: p.text.slice(-20), - }); sendBack({ type: "ChatAISentenceReceived", index: p.index, @@ -239,23 +148,14 @@ export const chatWebSocketActor = fromCallback( }); }; ws.onError = (msg) => { - log.error("[chat-machine] WS onError", { - errorMessage: msg, - errorLength: msg?.length ?? 0, - }); + log.error("[chat-machine] WS error", { errorMessage: msg }); sendBack({ type: "ChatWebSocketError", errorMessage: msg }); }; - log.debug("[chat-machine] chatWebSocketActor calling ws.connect()"); ws.connect(); - log.debug("[chat-machine] chatWebSocketActor ws.connect() called"); return () => { - log.debug( - "[chat-machine] chatWebSocketActor CLEANUP (parent state exit / logout / cross-transition / unmount)", - ); - // 清空 module-level 引用 —— 严格用 `=== ws` 判等,避免覆盖更新的实例 + log.debug("[chat-machine] chatWebSocketActor cleanup"); if (activeChatWebSocket === ws) { activeChatWebSocket = null; - log.debug("[chat-machine] chatWebSocketActor activeChatWebSocket CLEARED"); } ws.disconnect(); }; @@ -283,31 +183,19 @@ export const chatWebSocketActor = fromCallback( */ export const sendMessageWsActor = fromPromise( async ({ input }) => { - log.debug("[chat-machine] sendMessageWsActor ENTRY", { - contentLength: input.content.length, - contentPreview: input.content.slice(0, 50), - }); const ws = getActiveChatWebSocket(); if (!ws) { - log.error("[chat-machine] sendMessageWsActor no activeChatWebSocket"); + log.error("[chat-machine] sendMessageWsActor no active WebSocket"); throw new Error("[chat-machine] sendMessageWs: no active WebSocket (actor not running)"); } - log.debug("[chat-machine] sendMessageWsActor calling ws.sendMessage", { - isConnected: ws.isConnected, - }); const ok = ws.sendMessage(input.content); if (!ok) { - log.error( - "[chat-machine] sendMessageWsActor ws.sendMessage returned false (not OPEN)", - ); + log.error("[chat-machine] sendMessageWsActor WebSocket not OPEN"); throw new Error("[chat-machine] sendMessageWs: WebSocket not OPEN"); } - log.debug( - "[chat-machine] sendMessageWsActor ws.sendMessage OK, awaiting AI sentence stream", - ); // resolve 不返值 —— AI reply 由 chatWebSocketActor 的 ChatAISentenceReceived 流回 }, ); // Re-export `UiMessage` type for the chat-machine.ts public API -type UiMessage = import("@/data/dto/chat").UiMessage; +type UiMessage = import("@/data/dto/chat").UiMessage; \ No newline at end of file diff --git a/src/stores/chat/chat-machine.ts b/src/stores/chat/chat-machine.ts index 2e6dfc53..8b16f351 100644 --- a/src/stores/chat/chat-machine.ts +++ b/src/stores/chat/chat-machine.ts @@ -219,9 +219,6 @@ export const chatMachine = setup({ }, }, - // ======================================================================== - // guestSession(游客会话 —— 不连 WS,2 个 init 任务并行) - // ======================================================================== guestSession: { on: { ChatLogout: "#chat.idle", @@ -342,13 +339,9 @@ export const chatMachine = setup({ }, }, }, - // 删除 loadingMore(游客无翻页) }, }, - // ======================================================================== - // userSession(非游客会话 —— WS 父级 + history 子级) - // ======================================================================== userSession: { // 父级 on:把 WS / AI 流句 事件从 ready.on 上提到 userSession.on // —— 任何 child state(initializing / ready / sendingViaWs / sendingViaHttp / loadingMore) @@ -360,7 +353,6 @@ export const chatMachine = setup({ // —— XState v5: child handler 替换 parent;复制 action on: { ChatLogout: "#chat.idle", - ChatGuestLogin: "#chat.guestSession", ChatNonGuestLogin: { target: "#chat.userSession", reenter: true,