fix(chat): normalize message date headers
This commit is contained in:
@@ -25,7 +25,7 @@ import { chatRepository } from "@/data/repositories/chat_repository";
|
||||
import type { IChatRepository } from "@/data/repositories/interfaces";
|
||||
import { ChatStorage } from "@/data/storage/chat/chat_storage";
|
||||
import { ChatSendResponse } from "@/data/dto/chat/chat_send_response";
|
||||
import { formatDate, Result, Logger } from "@/utils";
|
||||
import { todayString, Result, Logger } from "@/utils";
|
||||
|
||||
import type { ChatState } from "./chat-state";
|
||||
|
||||
@@ -61,10 +61,16 @@ export function localMessagesToUi(
|
||||
return records.map((m) => ({
|
||||
content: m.content,
|
||||
isFromAI: m.role === "assistant",
|
||||
date: m.createdAt,
|
||||
date: messageDateFromCreatedAt(m.createdAt),
|
||||
}));
|
||||
}
|
||||
|
||||
function messageDateFromCreatedAt(createdAt: string): string {
|
||||
const parsed = new Date(createdAt);
|
||||
if (Number.isNaN(parsed.getTime())) return todayString();
|
||||
return todayString(parsed);
|
||||
}
|
||||
|
||||
/**
|
||||
* ChatSendResponse → UiMessage(纯函数)
|
||||
* - 业务事实:后端响应就是 AI 的回复
|
||||
@@ -74,7 +80,7 @@ export function sendResponseToUiMessage(response: ChatSendResponse): UiMessage {
|
||||
return {
|
||||
content: response.reply,
|
||||
isFromAI: true,
|
||||
date: formatDate(new Date(response.timestamp)),
|
||||
date: todayString(new Date(response.timestamp)),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -201,7 +207,7 @@ export async function readAndSyncHistory(): Promise<{
|
||||
"there were things I couldn't say there. " +
|
||||
"Finally I can relax. How was your day out?",
|
||||
isFromAI: true,
|
||||
date: formatDate(),
|
||||
date: todayString(),
|
||||
};
|
||||
|
||||
// 1. 读 local
|
||||
|
||||
@@ -38,7 +38,7 @@
|
||||
import { setup, assign } from "xstate";
|
||||
|
||||
import { ChatStorage } from "@/data/storage/chat/chat_storage";
|
||||
import { formatDate, todayString, Logger } from "@/utils";
|
||||
import { todayString, Logger } from "@/utils";
|
||||
|
||||
import { ChatState, initialState } from "./chat-state";
|
||||
import type { ChatEvent } from "./chat-events";
|
||||
@@ -229,7 +229,7 @@ export const chatMachine = setup({
|
||||
messages.push({
|
||||
content: event.text,
|
||||
isFromAI: true,
|
||||
date: formatDate(),
|
||||
date: todayString(),
|
||||
});
|
||||
} else {
|
||||
const last = messages[messages.length - 1];
|
||||
@@ -255,7 +255,7 @@ export const chatMachine = setup({
|
||||
{
|
||||
content: "Something went wrong. Try sending again?",
|
||||
isFromAI: true,
|
||||
date: formatDate(),
|
||||
date: todayString(),
|
||||
},
|
||||
];
|
||||
return { messages, isReplyingAI: false };
|
||||
|
||||
Reference in New Issue
Block a user