feat: AI问答对话列表云同步 — 登录用户对话按id跨设备合并(上限30), localStorage变化3s防抖自动上传, 打开页面已登录自动拉取一次, 本机3s轮询感知云端回写实时刷新

This commit is contained in:
personal82555 committed 2026-10-13 10:29:40 +08:00
1 parent c68ad09c65
commit d16fbb1667
3 files changed
+46 -2

No files matched your search

+20
View File
@@ -6,6 +6,7 @@ const SYNC_KEYS = [
'it-tools-recent-tools',
'it-tools-bookmarks:sites',
'menu-tool-option:collapsed-categories',
'it-tools-ai-chat-convs',
];
function collectLocal(): Record<string, any> {
@@ -66,6 +67,21 @@ function mergeJsonArrays(a: string, b: string, keyField: string | null): string
}
}
/** 会话列表按 updatedAt 降序截断到 30 条 */
function trimConvs(jsonStr: string): string {
try {
const arr = JSON.parse(jsonStr);
if (!Array.isArray(arr)) {
return jsonStr;
}
arr.sort((a: any, b: any) => (b?.updatedAt ?? 0) - (a?.updatedAt ?? 0));
return JSON.stringify(arr.slice(0, 30));
}
catch {
return jsonStr;
}
}
function mergeData(server: Record<string, any>, mine: Record<string, any>): Record<string, any> {
const out: Record<string, any> = { ...server };
for (const [k, v] of Object.entries(mine)) {
@@ -80,6 +96,10 @@ function mergeData(server: Record<string, any>, mine: Record<string, any>): Reco
if (k === 'favoriteToolsName' || k === 'it-tools-recent-tools' || k === 'it-tools-bookmarks:sites') {
out[k] = mergeJsonArrays(out[k], v, k === 'it-tools-recent-tools' ? 'path' : null);
}
else if (k === 'it-tools-ai-chat-convs') {
// 对话列表按 id 合并(多设备各自的会话都保留),上限 30 条
out[k] = trimConvs(mergeJsonArrays(out[k], v, 'id'));
}
else {
out[k] = v; // scalar configs: local wins
}