File size: 1,002 Bytes
b7a067b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
(stateJson) => {
    const state = JSON.parse(stateJson);
    const titles = {};

    for (const conv of (state.conversations || [])) {
        titles[conv.key] = { label: conv.label, last_updated: conv.last_updated || 0 };
        const ctx = (state.conversation_contexts || {})[conv.key];
        if (ctx !== undefined) {
            localStorage.setItem("chat_id_" + conv.key, JSON.stringify(ctx));
        }
    }

    // Remove stale chat_id_* keys that are no longer in conversations
    const validIds = new Set(Object.keys(titles));
    for (let i = 0; i < localStorage.length; i++) {
        const k = localStorage.key(i);
        if (k && k.startsWith("chat_id_")) {
            const id = k.slice("chat_id_".length);
            if (!validIds.has(id)) {
                localStorage.removeItem(k);
                i--;  // adjust index after removal
            }
        }
    }

    localStorage.setItem("titles", JSON.stringify(titles));
    return stateJson;
}