refactor(chat): remove commented-out code in guestSession
This commit is contained in:
@@ -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: "<actor path>",
|
||||
});
|
||||
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: "<actor path>",
|
||||
});
|
||||
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<ChatEvent, { token: string }>(
|
||||
({ sendBack, input }) => {
|
||||
log.debug("[chat-machine] chatWebSocketActor ENTRY", {
|
||||
hasToken: !!input.token,
|
||||
tokenLength: input.token?.length ?? 0,
|
||||
tokenPrefix: input.token?.slice(0, 10) ?? "EMPTY",
|
||||
selfPath: "<actor path>",
|
||||
});
|
||||
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<ChatEvent, { token: string }>(
|
||||
});
|
||||
};
|
||||
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<ChatEvent, { token: string }>(
|
||||
*/
|
||||
export const sendMessageWsActor = fromPromise<void, { content: string }>(
|
||||
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;
|
||||
Reference in New Issue
Block a user