fix: stabilize usage loading and session search

This commit is contained in:
shiyue
2026-08-05 10:41:50 +08:00
parent c61b7434d2
commit 3f00cfe7a4
22 changed files with 109 additions and 47 deletions

View File

@@ -322,6 +322,17 @@ function updatedTime(doc) {
return Number.isFinite(time) ? time : 0;
}
// “最新”按本次命中的消息时间排序;没有消息时间时才回退到会话更新时间。
function resultTime(result) {
const matchTimes = (result?.matches || [])
.map((match) => new Date(match?.timestamp || 0).getTime())
.filter((time) => Number.isFinite(time) && time > 0);
if (matchTimes.length > 0) return Math.max(...matchTimes);
const directTime = new Date(result?.timestamp || 0).getTime();
if (Number.isFinite(directTime) && directTime > 0) return directTime;
return updatedTime(result);
}
function buildQuery(rawQuery, options) {
const maxQueryChars = clampInteger(
options.maxQueryChars,
@@ -735,6 +746,8 @@ function createSessionSearchIndex(options = {}) {
messageIndex: firstMatch ? firstMatch.messageIndex : null,
role: firstMatch ? firstMatch.role : null,
timestamp: firstMatch ? firstMatch.timestamp : null,
matchTimestamp: firstMatch ? firstMatch.timestamp : null,
sessionUpdated: doc.updated,
title: doc.title,
projectName: doc.projectName,
updated: doc.updated,
@@ -773,8 +786,8 @@ function createSessionSearchIndex(options = {}) {
}
results.sort((a, b) => {
if (sort === 'newest') return updatedTime(b) - updatedTime(a) || b.score - a.score || a.sessionId.localeCompare(b.sessionId);
return b.score - a.score || updatedTime(b) - updatedTime(a) || a.sessionId.localeCompare(b.sessionId);
if (sort === 'newest') return resultTime(b) - resultTime(a) || b.score - a.score || a.sessionId.localeCompare(b.sessionId);
return b.score - a.score || resultTime(b) - resultTime(a) || a.sessionId.localeCompare(b.sessionId);
});
return {