优化历史会话排序逻辑,按首条消息时间排序
Build Push and Deploy / docker-image (push) Failing after 41s
Build Push and Deploy / deploy-fallback-log (push) Successful in 0s

This commit is contained in:
2026-05-19 16:48:56 +08:00
parent 9106b8d4a9
commit 2fbfba118f
3 changed files with 79 additions and 18 deletions
+31 -12
View File
@@ -51,6 +51,28 @@ const sanitizeMessages = (messages: Message[] | undefined) =>
const sanitizeBranchGroups = (branchGroups: BranchGroup[] | undefined) =>
Array.isArray(branchGroups) ? cloneBranchGroups(branchGroups) : [];
const hasChatContent = (state: {
messages: Message[];
branchGroups: BranchGroup[];
sessionId?: string;
}) =>
state.messages.length > 0 ||
state.branchGroups.length > 0 ||
Boolean(state.sessionId);
const compareSessionsByAnchorTime = (
left: Pick<ChatSessionRecord, "id" | "createdAt" | "updatedAt">,
right: Pick<ChatSessionRecord, "id" | "createdAt" | "updatedAt">,
) => {
const createdAtDiff = right.createdAt - left.createdAt;
if (createdAtDiff !== 0) return createdAtDiff;
const updatedAtDiff = right.updatedAt - left.updatedAt;
if (updatedAtDiff !== 0) return updatedAtDiff;
return right.id.localeCompare(left.id);
};
const toLoadedChatState = (session: ChatSessionRecord | undefined): LoadedChatState => {
if (!session) return emptyLoadedChatState();
return {
@@ -131,7 +153,7 @@ const getLatestSession = async () => {
const db = await getDb();
const sessions = await db.getAll(SESSION_STORE);
if (sessions.length === 0) return undefined;
return sessions.sort((left, right) => right.updatedAt - left.updatedAt)[0];
return sessions.sort(compareSessionsByAnchorTime)[0];
};
const migrateLegacyLocalStorage = async () => {
@@ -215,10 +237,7 @@ export const saveActiveChatState = async (
): Promise<string | undefined> => {
if (typeof window === "undefined") return state.storageSessionId;
const hasContent =
state.messages.length > 0 ||
state.branchGroups.length > 0 ||
Boolean(state.sessionId);
const hasContent = hasChatContent(state);
const db = await getDb();
const existingSession = state.storageSessionId
@@ -241,11 +260,15 @@ export const saveActiveChatState = async (
const storageSessionId = state.storageSessionId ?? createId();
const preferredTitle = state.title?.trim();
const finalTitle = preferredTitle || existingSession?.title || "新对话";
const shouldAnchorCreatedAtToFirstMessage =
Boolean(existingSession) && !hasChatContent(existingSession) && hasContent;
const nextRecord: ChatSessionRecord = {
id: storageSessionId,
title: finalTitle,
isTitleManuallyEdited: state.isTitleManuallyEdited ?? existingSession?.isTitleManuallyEdited ?? false,
createdAt: existingSession?.createdAt ?? now,
createdAt: shouldAnchorCreatedAtToFirstMessage
? now
: existingSession?.createdAt ?? now,
updatedAt: now,
sessionId: state.sessionId,
messages: sanitizeMessages(state.messages),
@@ -268,9 +291,7 @@ export const listChatSessions = async (): Promise<ChatSessionSummary[]> => {
const db = await getDb();
const sessions = await db.getAll(SESSION_STORE);
return sessions
.sort((left, right) => right.updatedAt - left.updatedAt)
.map(toSessionSummary);
return sessions.sort(compareSessionsByAnchorTime).map(toSessionSummary);
};
export const updateChatSessionTitle = async (
@@ -353,9 +374,7 @@ export const deleteChatSession = async (sessionId: string): Promise<string | und
await db.delete(SESSION_STORE, sessionId);
const remainingSessions = await db.getAll(SESSION_STORE);
const nextActiveSession = remainingSessions.sort(
(left, right) => right.updatedAt - left.updatedAt,
)[0];
const nextActiveSession = remainingSessions.sort(compareSessionsByAnchorTime)[0];
const meta = await getMeta();
await setMeta({