refactor(chat): wire sendMessageHttpActor for HTTP message sending and update response handling
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
*
|
||||
* 从 `chat-machine.ts` 抽出的"Actors"段:
|
||||
* - `chatInitActor`(fromPromise,**仅**游客有意义)
|
||||
* - `sendMessageHttpActor`(fromPromise,**已定义但**未 wire —— 消息**不**真发后端)
|
||||
* - `sendMessageHttpActor`(fromPromise,**已** wire —— "一次发送 = 一次返回")
|
||||
* - `loadMoreHistoryActor`(fromPromise)
|
||||
* - `chatWebSocketActor`(fromCallback,长生命周期 —— **仅** userSession 期间 invoke)
|
||||
*
|
||||
@@ -24,6 +24,7 @@ import {
|
||||
chatRepo,
|
||||
localMessagesToUi,
|
||||
readInitData,
|
||||
sendResponseToUiMessage,
|
||||
type InitResult,
|
||||
} from "./chat-machine.helpers";
|
||||
import type { ChatEvent } from "./chat-events";
|
||||
@@ -34,12 +35,13 @@ import type { ChatEvent } from "./chat-events";
|
||||
export const chatInitActor = fromPromise<InitResult>(async () => readInitData());
|
||||
|
||||
/**
|
||||
* HTTP 发送消息(**已定义但未** wire —— 消息**不**真发后端)
|
||||
* - 任何 state 转移**未** invoke 此 actor
|
||||
* - 保留全链日志以便排查
|
||||
* HTTP 发送消息(**一次发送 = 一次返回**)
|
||||
* - 后端响应**就**是 AI 回复(`ChatSendResponse.reply: string`)
|
||||
* - **不**再调 `getLocalMessages()`(**多此一举**)
|
||||
* - 返回 `{ reply: UiMessage }` —— `sending.onDone` 把它**追加**到 `context.messages`
|
||||
*/
|
||||
export const sendMessageHttpActor = fromPromise<
|
||||
{ messages: UiMessage[] },
|
||||
{ reply: UiMessage },
|
||||
{ content: string }
|
||||
>(async ({ input, self }) => {
|
||||
console.log("[chat-machine] sendMessageHttpActor ENTRY", {
|
||||
@@ -48,10 +50,6 @@ export const sendMessageHttpActor = fromPromise<
|
||||
selfId: self.id,
|
||||
selfPath: "<actor path>",
|
||||
});
|
||||
console.warn(
|
||||
"[chat-machine] sendMessageHttpActor invoked —— actor 已定义但**未** invoke(消息**不**真发后端)",
|
||||
{ contentLength: input.content.length, contentPreview: input.content.slice(0, 50) },
|
||||
);
|
||||
console.log("[chat-machine] sendMessageHttpActor calling chatRepo.sendMessage");
|
||||
const result = await chatRepo.sendMessage(input.content);
|
||||
console.log("[chat-machine] sendMessageHttpActor chatRepo.sendMessage DONE", {
|
||||
@@ -63,18 +61,18 @@ export const sendMessageHttpActor = fromPromise<
|
||||
throw result.error;
|
||||
}
|
||||
|
||||
console.log("[chat-machine] sendMessageHttpActor fetching local messages");
|
||||
const local = await chatRepo.getLocalMessages();
|
||||
console.log("[chat-machine] sendMessageHttpActor local messages DONE", {
|
||||
success: local.success,
|
||||
messagesCount: local.success && local.data ? local.data.length : 0,
|
||||
// 一次发送 = 一次返回 —— 直接转 reply → UiMessage
|
||||
console.log("[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,
|
||||
});
|
||||
if (Result.isOk(local) && local.data) {
|
||||
const messages = localMessagesToUi(local.data);
|
||||
console.log("[chat-machine] sendMessageHttpActor done", { messagesCount: messages.length });
|
||||
return { messages };
|
||||
}
|
||||
return { messages: [] };
|
||||
const reply = sendResponseToUiMessage(result.data);
|
||||
console.log("[chat-machine] sendMessageHttpActor done", {
|
||||
replyContentLength: reply.content.length,
|
||||
});
|
||||
return { reply };
|
||||
});
|
||||
|
||||
/** 翻历史(pagination) */
|
||||
|
||||
Reference in New Issue
Block a user