@burdenoff/fe-libs 2026.926.5 → 2026.927.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/shared/assistant/conversation.d.ts +91 -3
- package/dist/shared/assistant/conversation.d.ts.map +1 -1
- package/dist/shared/assistant/gatewayClient.d.ts +15 -0
- package/dist/shared/assistant/gatewayClient.d.ts.map +1 -1
- package/dist/shared/assistant/gatewayClient.js +1 -1
- package/dist/shared/assistant/guardedStorage.d.ts +4 -2
- package/dist/shared/assistant/guardedStorage.d.ts.map +1 -1
- package/dist/shared/assistant/guardedStorage.js +1 -1
- package/dist/shared/assistant/history/historyApi.d.ts +38 -0
- package/dist/shared/assistant/history/historyApi.d.ts.map +1 -0
- package/dist/shared/assistant/history/historyApi.js +153 -0
- package/dist/shared/assistant/history/historyEngine.d.ts +96 -0
- package/dist/shared/assistant/history/historyEngine.d.ts.map +1 -0
- package/dist/shared/assistant/history/historyEngine.js +774 -0
- package/dist/shared/assistant/history/historyErrors.d.ts +72 -0
- package/dist/shared/assistant/history/historyErrors.d.ts.map +1 -0
- package/dist/shared/assistant/history/historyErrors.js +74 -0
- package/dist/shared/assistant/history/historyList.d.ts +115 -0
- package/dist/shared/assistant/history/historyList.d.ts.map +1 -0
- package/dist/shared/assistant/history/historyList.js +116 -0
- package/dist/shared/assistant/history/historyMapping.d.ts +65 -0
- package/dist/shared/assistant/history/historyMapping.d.ts.map +1 -0
- package/dist/shared/assistant/history/historyMapping.js +383 -0
- package/dist/shared/assistant/history/historyMerge.d.ts +65 -0
- package/dist/shared/assistant/history/historyMerge.d.ts.map +1 -0
- package/dist/shared/assistant/history/historyMerge.js +97 -0
- package/dist/shared/assistant/history/historyOperations.d.ts +24 -0
- package/dist/shared/assistant/history/historyOperations.d.ts.map +1 -0
- package/dist/shared/assistant/history/historyOperations.js +125 -0
- package/dist/shared/assistant/history/historyResume.d.ts +56 -0
- package/dist/shared/assistant/history/historyResume.d.ts.map +1 -0
- package/dist/shared/assistant/history/historyResume.js +55 -0
- package/dist/shared/assistant/history/historyStatus.d.ts +76 -0
- package/dist/shared/assistant/history/historyStatus.d.ts.map +1 -0
- package/dist/shared/assistant/history/historyStatus.js +47 -0
- package/dist/shared/assistant/history/historyStore.d.ts +192 -0
- package/dist/shared/assistant/history/historyStore.d.ts.map +1 -0
- package/dist/shared/assistant/history/historyStore.js +503 -0
- package/dist/shared/assistant/history/historyTimestamps.d.ts +11 -0
- package/dist/shared/assistant/history/historyTimestamps.d.ts.map +1 -0
- package/dist/shared/assistant/history/historyTimestamps.js +9 -0
- package/dist/shared/assistant/history/historyTypes.d.ts +256 -0
- package/dist/shared/assistant/history/historyTypes.d.ts.map +1 -0
- package/dist/shared/assistant/history/historyTypes.js +20 -0
- package/dist/shared/assistant/history/profileBoundStorage.d.ts +36 -0
- package/dist/shared/assistant/history/profileBoundStorage.d.ts.map +1 -0
- package/dist/shared/assistant/history/profileBoundStorage.js +74 -0
- package/dist/shared/assistant/history/stampRegistry.d.ts +84 -0
- package/dist/shared/assistant/history/stampRegistry.d.ts.map +1 -0
- package/dist/shared/assistant/history/stampRegistry.js +58 -0
- package/dist/shared/assistant/history/useAssistantServerHistory.d.ts +18 -0
- package/dist/shared/assistant/history/useAssistantServerHistory.d.ts.map +1 -0
- package/dist/shared/assistant/history/useAssistantServerHistory.js +12 -0
- package/dist/shared/assistant/i18nManifest.d.ts +18 -0
- package/dist/shared/assistant/i18nManifest.d.ts.map +1 -1
- package/dist/shared/assistant/i18nManifest.js +18 -0
- package/dist/shared/assistant/index.d.ts +16 -0
- package/dist/shared/assistant/index.d.ts.map +1 -1
- package/dist/shared/assistant/liveTurns.d.ts +7 -0
- package/dist/shared/assistant/liveTurns.d.ts.map +1 -1
- package/dist/shared/assistant/liveTurns.js +35 -32
- package/dist/shared/assistant/spokenTurns.d.ts +20 -0
- package/dist/shared/assistant/spokenTurns.d.ts.map +1 -1
- package/dist/shared/assistant/spokenTurns.js +13 -8
- package/dist/shared/assistant/store.d.ts +35 -1
- package/dist/shared/assistant/store.d.ts.map +1 -1
- package/dist/shared/assistant/store.js +93 -24
- package/dist/shared/assistant/ui/AIAssistantPanel.d.ts +16 -0
- package/dist/shared/assistant/ui/AIAssistantPanel.d.ts.map +1 -1
- package/dist/shared/assistant/ui/AIAssistantPanel.js +63 -60
- package/dist/shared/assistant/ui/AssistantChatArea.d.ts.map +1 -1
- package/dist/shared/assistant/ui/AssistantChatArea.js +730 -672
- package/dist/shared/assistant/ui/AssistantConversationList.d.ts +17 -0
- package/dist/shared/assistant/ui/AssistantConversationList.d.ts.map +1 -1
- package/dist/shared/assistant/ui/AssistantConversationList.js +324 -145
- package/dist/shared/assistant/ui/AssistantHistoryNotices.d.ts +36 -0
- package/dist/shared/assistant/ui/AssistantHistoryNotices.d.ts.map +1 -0
- package/dist/shared/assistant/ui/AssistantHistoryNotices.js +103 -0
- package/dist/shared/assistant/ui/AssistantOverlay.d.ts +4 -0
- package/dist/shared/assistant/ui/AssistantOverlay.d.ts.map +1 -1
- package/dist/shared/assistant/ui/AssistantOverlay.js +97 -94
- package/dist/shared/assistant/ui/AssistantSpokenTurnsNotice.d.ts +5 -3
- package/dist/shared/assistant/ui/AssistantSpokenTurnsNotice.d.ts.map +1 -1
- package/dist/shared/assistant/ui/composer/AssistantComposer.d.ts +6 -0
- package/dist/shared/assistant/ui/composer/AssistantComposer.d.ts.map +1 -1
- package/dist/shared/assistant/ui/composer/AssistantComposer.js +54 -54
- package/dist/shared/assistant/ui/voice/VoiceTranscriptDrawer.d.ts +9 -0
- package/dist/shared/assistant/ui/voice/VoiceTranscriptDrawer.d.ts.map +1 -1
- package/dist/shared/assistant/ui/voice/VoiceTranscriptDrawer.js +37 -32
- package/dist/shared/assistant/voice/turnVisibility.d.ts +7 -5
- package/dist/shared/assistant/voice/turnVisibility.d.ts.map +1 -1
- package/dist/shared/assistant/voice/voiceTranscript.d.ts +3 -2
- package/dist/shared/assistant/voice/voiceTranscript.d.ts.map +1 -1
- package/dist/shared-assistant.js +153 -137
- package/package.json +1 -1
|
@@ -0,0 +1,774 @@
|
|
|
1
|
+
import { PROFILE_STORAGE_CHANGE_EVENT as e } from "../../providers/shell/ProfileStorageManager.js";
|
|
2
|
+
import { isHistoryConversationBusy as t, setAssistantStorageFullHandler as n, setHistoryOpenFilterSource as r, setHistoryStampSource as i } from "./stampRegistry.js";
|
|
3
|
+
import { useAssistantStore as a } from "../store.js";
|
|
4
|
+
import { isTurnInFlight as o } from "../turnState.js";
|
|
5
|
+
import { getActorId as s, getWorkspaceId as c } from "../context.js";
|
|
6
|
+
import { createGatewayHistoryTransport as l } from "./historyApi.js";
|
|
7
|
+
import { ASSISTANT_HISTORY_QUOTA_RETRY_MS as u, ASSISTANT_HISTORY_SCHEMA_PAUSE_MS as d, assistantHistoryBackoffMs as ee, classifyAssistantHistoryError as f, planAssistantHistoryRetry as te } from "./historyErrors.js";
|
|
8
|
+
import { assistantHistoryScopeKey as p, completeAssistantHistoryScope as ne, isLegacyHistoryConvKey as re } from "./historyTypes.js";
|
|
9
|
+
import { historyConvKeyOf as ie, historyHeaderOf as m, historyScopeKeyOfStamp as h, materializeHistoryHeader as ae, selectHistoryEvictions as oe } from "./historyList.js";
|
|
10
|
+
import { fromHistoryEntry as se, historyEntriesMatchKind as ce, isHistoryClientId as le, toHistoryEntries as ue } from "./historyMapping.js";
|
|
11
|
+
import { applyHistoryPage as de, planHistoryEarlierLoad as fe, planHistoryOpenLoad as pe } from "./historyMerge.js";
|
|
12
|
+
import { assistantHistoryGenerationOf as me, isCurrentAssistantHistoryGeneration as he, useAssistantHistoryStatus as ge } from "./historyStatus.js";
|
|
13
|
+
import { assistantHistoryStore as g } from "./historyStore.js";
|
|
14
|
+
//#region src/shared/assistant/history/historyEngine.ts
|
|
15
|
+
var _e = 1e3, ve = 3e4, ye = 2e3, be = 15e3, xe = 5 * 6e4, Se = 2e3, Ce = 100, we = 10, Te = 100, Ee = 5, De = 3, Oe = 2, ke = [10, 0], Ae = 3e4, _ = 1440 * 60 * 1e3;
|
|
16
|
+
function v() {
|
|
17
|
+
let e = /* @__PURE__ */ new Map(), t = 0;
|
|
18
|
+
return {
|
|
19
|
+
now: () => Date.now(),
|
|
20
|
+
setTimeout(n, r) {
|
|
21
|
+
t += 1;
|
|
22
|
+
let i = t;
|
|
23
|
+
return e.set(i, setTimeout(() => {
|
|
24
|
+
e.delete(i), n();
|
|
25
|
+
}, r)), i;
|
|
26
|
+
},
|
|
27
|
+
clearTimeout(t) {
|
|
28
|
+
if (typeof t != "number") return;
|
|
29
|
+
let n = e.get(t);
|
|
30
|
+
n !== void 0 && clearTimeout(n), e.delete(t);
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
function je() {
|
|
35
|
+
let e = typeof navigator > "u" ? void 0 : navigator.locks;
|
|
36
|
+
return !e || typeof e.request != "function" ? null : { request: (t, n) => new Promise((r, i) => {
|
|
37
|
+
e.request(t, { ifAvailable: !0 }, (e) => n(e !== null).then(r, i)).catch(i);
|
|
38
|
+
}) };
|
|
39
|
+
}
|
|
40
|
+
function Me(e, t) {
|
|
41
|
+
try {
|
|
42
|
+
return e();
|
|
43
|
+
} catch {
|
|
44
|
+
return t;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
var Ne = (e, t) => {
|
|
48
|
+
console.warn(`[assistant] history: ${e}`, t ?? "");
|
|
49
|
+
}, Pe = (e) => e.entries.map((e) => e.clientMessageId);
|
|
50
|
+
function y(l) {
|
|
51
|
+
let { transport: g, storage: _, clock: v } = l, y = _.useStore, b = l.mainStore ?? a, x = l.status ?? ge, Fe = l.locks === void 0 ? je() : l.locks, Ie = l.random ?? Math.random, S = () => Me(l.readActorId ?? s, null), Le = () => Me(l.readWorkspaceId ?? c, ""), Re = l.isConversationBusy ?? t, C = l.log ?? Ne, w = /* @__PURE__ */ new Map(), T = null, E = null, D = null, O = 0, k = !1, ze = !1, A = !1, Be = null, Ve = !1, He = !1, j = 0, Ue = -Infinity, M = null, We = !1, N = null, Ge = /* @__PURE__ */ new Set(), P = /* @__PURE__ */ new Map(), F = /* @__PURE__ */ new Map(), I = /* @__PURE__ */ new Map(), L = /* @__PURE__ */ new Set(), R = [], z = () => v.now(), B = () => E ? p(E) : null, Ke = () => z() < O, V = () => T !== null && D === null && _.isBoundToActiveProfile(), H = () => V() && E !== null && !Ke() && !k, U = (e) => (L.add(e), e.finally(() => L.delete(e)).catch(() => void 0), e), W = (e, t, n, r = !1) => {
|
|
52
|
+
let i = z() + Math.max(0, t), a = I.get(e);
|
|
53
|
+
if (a && !r && a.at <= i) return;
|
|
54
|
+
a && v.clearTimeout(a.handle);
|
|
55
|
+
let o = v.setTimeout(() => {
|
|
56
|
+
I.get(e)?.handle === o && I.delete(e), n();
|
|
57
|
+
}, Math.max(0, t));
|
|
58
|
+
I.set(e, {
|
|
59
|
+
handle: o,
|
|
60
|
+
at: i
|
|
61
|
+
});
|
|
62
|
+
}, qe = (e) => {
|
|
63
|
+
let t = I.get(e);
|
|
64
|
+
t && v.clearTimeout(t.handle), I.delete(e);
|
|
65
|
+
}, Je = () => me(x.getState(), _.boundKey()), G = (e) => T !== null && _.isBoundToActiveProfile() && he(e, x.getState(), _.boundKey()), K = (e) => b.getState().conversations.find((t) => t.id === e), q = (e) => o(b.getState().processingByConversationId[e]?.status) || Me(() => Re(e), !0), Ye = (e, t) => {
|
|
66
|
+
let n = y.getState().scopes[e]?.headers[t];
|
|
67
|
+
if (n) return n;
|
|
68
|
+
let r = x.getState().olderHeaders;
|
|
69
|
+
return r?.scopeKey === e ? r.headers[t] ?? null : null;
|
|
70
|
+
}, Xe = () => {
|
|
71
|
+
if (D) return D;
|
|
72
|
+
if (_.isBoundToActiveProfile()) return Promise.resolve(!0);
|
|
73
|
+
if (_.heldByNewerBuild() || _.activeProfileKey() === null) return Promise.resolve(!1);
|
|
74
|
+
x.getState().noteUnsavedDelete(void 0);
|
|
75
|
+
let e = U(_.rebind().then(() => _.isBoundToActiveProfile(), () => !1));
|
|
76
|
+
return D = e, e.then(() => {
|
|
77
|
+
D === e && (D = null);
|
|
78
|
+
}), e;
|
|
79
|
+
}, Ze = (e) => {
|
|
80
|
+
let t = e ? p(e) : null, n = t !== B();
|
|
81
|
+
E = e, x.getState().setHistoryScope(T, T === null ? null : t), !(!n || T === null) && (k = !1, qe("auth"), j = 0, Ue = -Infinity, M = null, N = null, P.clear(), U(Xe().then((t) => {
|
|
82
|
+
!t || E !== e || (Y(), X(), $("scope"), Q());
|
|
83
|
+
})));
|
|
84
|
+
}, J = () => {
|
|
85
|
+
if (T === null) return;
|
|
86
|
+
let e = ne(S(), Le(), T);
|
|
87
|
+
if ((e ? p(e) : null) !== B()) {
|
|
88
|
+
Ze(e);
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
let t = _.activeProfileKey();
|
|
92
|
+
if (t === null || t === _.boundKey()) {
|
|
93
|
+
Y();
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
U(Xe().then((e) => {
|
|
97
|
+
e && (X(), Y(), $("bind"), Q());
|
|
98
|
+
}));
|
|
99
|
+
};
|
|
100
|
+
function Y() {
|
|
101
|
+
let e = B();
|
|
102
|
+
if (e === null) return;
|
|
103
|
+
let t = b.getState(), n = t.conversations.find((e) => e.id === t.activeConversationId);
|
|
104
|
+
!n?.sync || h(n.sync) === e || q(n.id) || t.setActiveConversation(null);
|
|
105
|
+
}
|
|
106
|
+
function X() {
|
|
107
|
+
if (!_.isBoundToActiveProfile()) return;
|
|
108
|
+
let e = y.getState().tombstones, t = x.getState().olderHeaders;
|
|
109
|
+
t && Object.keys(t.headers).some((t) => t in e) && x.getState().setOlderHeaders({
|
|
110
|
+
...t,
|
|
111
|
+
headers: Object.fromEntries(Object.entries(t.headers).filter(([t]) => !(t in e)))
|
|
112
|
+
});
|
|
113
|
+
let n = b.getState(), r = n.conversations.filter((t) => {
|
|
114
|
+
let n = e[t.id];
|
|
115
|
+
return t.sync !== void 0 && n !== void 0 && h(t.sync) === n.scopeKey;
|
|
116
|
+
}).map((e) => e.id);
|
|
117
|
+
if (r.length === 0) return;
|
|
118
|
+
let i = n.activeConversationId;
|
|
119
|
+
n.removeConversationsSilently(r);
|
|
120
|
+
for (let e of r) x.getState().noteRemoteDelete({
|
|
121
|
+
conversationId: e,
|
|
122
|
+
wasActive: e === i,
|
|
123
|
+
at: z()
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
function Qe(e, t, n) {
|
|
127
|
+
let r = y.getState();
|
|
128
|
+
r.recordHistoryTombstone(e, {
|
|
129
|
+
scopeKey: t,
|
|
130
|
+
at: z(),
|
|
131
|
+
confirmed: !0,
|
|
132
|
+
serverId: n
|
|
133
|
+
}), r.dropHistoryItems(e, () => !0, z()), X();
|
|
134
|
+
}
|
|
135
|
+
function Z(e) {
|
|
136
|
+
x.getState().setHistoryPaused(Ke() ? O : void 0, e.class);
|
|
137
|
+
}
|
|
138
|
+
function $e(e, t) {
|
|
139
|
+
O = Math.max(O, z() + e), x.getState().setHistoryPaused(O, t.class), W("pause", O - z(), () => {
|
|
140
|
+
x.getState().setHistoryPaused(void 0, t.class), $("resume"), Q();
|
|
141
|
+
}, !0);
|
|
142
|
+
}
|
|
143
|
+
function et(e) {
|
|
144
|
+
k = !0, Z(e), W("auth", xe, () => {
|
|
145
|
+
k = !1, $("auth-recheck"), Q();
|
|
146
|
+
}, !0);
|
|
147
|
+
}
|
|
148
|
+
function tt(e) {
|
|
149
|
+
switch (e.class) {
|
|
150
|
+
case "schema-unavailable": return $e(d, e), !0;
|
|
151
|
+
case "auth": return et(e), !0;
|
|
152
|
+
case "scope-inactive": return Z(e), J(), !0;
|
|
153
|
+
default: return Z(e), !1;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
let nt = (e) => e.product === T && S() === e.actorId, rt = (e, t, n, r) => {
|
|
157
|
+
if (T === null) return;
|
|
158
|
+
let i = K(e), a = i?.sync;
|
|
159
|
+
if (!i || !a || a.origin !== "created" && a.origin !== "remote" || !nt(a)) return;
|
|
160
|
+
if (r !== a.workspaceId) {
|
|
161
|
+
b.getState().patchConversationSync(e, (e) => ({
|
|
162
|
+
...e,
|
|
163
|
+
blocked: "scope"
|
|
164
|
+
}));
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
let s = t.map((e) => i.messages.find((t) => t.id === e));
|
|
168
|
+
if (s.length === 0 || !le(e)) return;
|
|
169
|
+
let c = s.filter((e) => e !== void 0);
|
|
170
|
+
if (c.length !== s.length) return;
|
|
171
|
+
let l = ue(c, {
|
|
172
|
+
productId: a.product,
|
|
173
|
+
clientConversationId: e,
|
|
174
|
+
kind: n
|
|
175
|
+
});
|
|
176
|
+
if (!ce(n, l)) {
|
|
177
|
+
C("a committed turn does not have the shape the server stores; kept on this device", {
|
|
178
|
+
kind: n,
|
|
179
|
+
roles: l.map((e) => e.role)
|
|
180
|
+
});
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
let u = {
|
|
184
|
+
id: `save:${e}:${t.join("+")}`,
|
|
185
|
+
type: "save",
|
|
186
|
+
scopeKey: h(a),
|
|
187
|
+
convKey: e,
|
|
188
|
+
kind: n,
|
|
189
|
+
entries: l,
|
|
190
|
+
attempts: 0
|
|
191
|
+
}, d = () => {
|
|
192
|
+
if (!_.isBoundToActiveProfile() || !nt(a)) return;
|
|
193
|
+
let n = y.getState();
|
|
194
|
+
e in n.tombstones || (n.enqueueHistoryItem(u), y.getState().outbox.some((e) => e.id === u.id) && (b.getState().markHistoryState(e, t, "pending"), Q()));
|
|
195
|
+
}, ee = () => {
|
|
196
|
+
V() ? d() : U(Xe().then((e) => e ? d() : void 0));
|
|
197
|
+
}, f = b.getState().processingByConversationId[e];
|
|
198
|
+
if (n === "NOTE" && o(f?.status)) {
|
|
199
|
+
F.set(e, [...F.get(e) ?? [], ee]);
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
ee();
|
|
203
|
+
};
|
|
204
|
+
function it(e) {
|
|
205
|
+
for (let [t, n] of [...F]) o(e.processingByConversationId[t]?.status) || (F.delete(t), U(Promise.resolve().then(() => {
|
|
206
|
+
if (T !== null) for (let e of n) e();
|
|
207
|
+
})));
|
|
208
|
+
}
|
|
209
|
+
let at = (e) => {
|
|
210
|
+
let t = b.getState(), n = t.conversations.find((t) => t.id === e), r = n?.sync, i = B();
|
|
211
|
+
if (n && !r || T === null || !_.isBoundToActiveProfile()) {
|
|
212
|
+
n && t.deleteConversation(e);
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
let a = r ? h(r) : i;
|
|
216
|
+
if (a === null) {
|
|
217
|
+
n && t.deleteConversation(e);
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
let o = Ye(a, e);
|
|
221
|
+
if (!n && !o) return;
|
|
222
|
+
let s = r?.origin === "legacy" || o?.legacy === !0 || re(e), c = r?.serverId ?? o?.serverId ?? (re(e) ? e.slice(7) : void 0), l = y.getState(), u = z(), d = !s || c ? {
|
|
223
|
+
id: `delete:${e}:${u}`,
|
|
224
|
+
type: "delete",
|
|
225
|
+
scopeKey: a,
|
|
226
|
+
convKey: e,
|
|
227
|
+
legacy: s,
|
|
228
|
+
...c ? { serverId: c } : {},
|
|
229
|
+
attempts: 0
|
|
230
|
+
} : null;
|
|
231
|
+
n && b.getState().deleteConversation(e), l.recordHistoryDelete(e, {
|
|
232
|
+
scopeKey: a,
|
|
233
|
+
at: u,
|
|
234
|
+
confirmed: d === null,
|
|
235
|
+
...c ? { serverId: c } : {}
|
|
236
|
+
}, d), d && _.persistFailing() && x.getState().noteUnsavedDelete({
|
|
237
|
+
conversationId: e,
|
|
238
|
+
at: u
|
|
239
|
+
}), Q();
|
|
240
|
+
}, ot = (e) => e.nextAttemptAt === void 0 || e.nextAttemptAt <= z(), Q = () => {
|
|
241
|
+
if (!H()) return;
|
|
242
|
+
if (ze) {
|
|
243
|
+
A = !0, Be?.();
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
246
|
+
ze = !0, A = !1;
|
|
247
|
+
let e = B();
|
|
248
|
+
U((async () => {
|
|
249
|
+
if (e === null) return;
|
|
250
|
+
let t = `bf-assistant-history:${_.boundKey() ?? ""}:${e}`;
|
|
251
|
+
if (!Fe) {
|
|
252
|
+
await st(e);
|
|
253
|
+
return;
|
|
254
|
+
}
|
|
255
|
+
await Fe.request(t, async (t) => {
|
|
256
|
+
t ? await st(e) : W("drain", be, Q);
|
|
257
|
+
});
|
|
258
|
+
})().catch((e) => C("the save queue stopped unexpectedly", e)).finally(() => {
|
|
259
|
+
ze = !1, A && Q();
|
|
260
|
+
}));
|
|
261
|
+
};
|
|
262
|
+
async function st(e) {
|
|
263
|
+
let t = /* @__PURE__ */ new Map();
|
|
264
|
+
for (; !(!H() || B() !== e);) {
|
|
265
|
+
A = !1;
|
|
266
|
+
let n = null, r = /* @__PURE__ */ new Map();
|
|
267
|
+
for (let t of y.getState().outbox) t.scopeKey === e && !r.has(t.convKey) && r.set(t.convKey, t);
|
|
268
|
+
for (let [i, a] of r) if (!t.has(i)) {
|
|
269
|
+
if (!ot(a)) {
|
|
270
|
+
let e = a.nextAttemptAt ?? z();
|
|
271
|
+
n = n === null ? e : Math.min(n, e);
|
|
272
|
+
continue;
|
|
273
|
+
}
|
|
274
|
+
t.size >= Oe || t.set(i, ct(i, e).finally(() => {
|
|
275
|
+
t.delete(i);
|
|
276
|
+
}));
|
|
277
|
+
}
|
|
278
|
+
if (n !== null && W("drain", n - z(), Q), t.size === 0) break;
|
|
279
|
+
let i = new Promise((e) => {
|
|
280
|
+
Be = e;
|
|
281
|
+
});
|
|
282
|
+
await Promise.race([...t.values(), i]), Be = null;
|
|
283
|
+
}
|
|
284
|
+
await Promise.all(t.values());
|
|
285
|
+
}
|
|
286
|
+
async function ct(e, t) {
|
|
287
|
+
for (;;) {
|
|
288
|
+
if (!H() || B() !== t) return;
|
|
289
|
+
let n = y.getState().outbox.find((n) => n.convKey === e && n.scopeKey === t);
|
|
290
|
+
if (!n || !ot(n) || (n.type === "save" ? await lt(n) : await dt(n)) === "stop") return;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
async function lt(e) {
|
|
294
|
+
if (e.convKey in y.getState().tombstones) return y.getState().completeHistoryItems([e.id], z()), "next";
|
|
295
|
+
let t = E;
|
|
296
|
+
if (!t || p(t) !== e.scopeKey) return "stop";
|
|
297
|
+
let n = _.boundKey(), r;
|
|
298
|
+
try {
|
|
299
|
+
r = await g.save(t, {
|
|
300
|
+
clientConversationId: e.convKey,
|
|
301
|
+
kind: e.kind,
|
|
302
|
+
entries: e.entries
|
|
303
|
+
});
|
|
304
|
+
} catch (t) {
|
|
305
|
+
return ft(e, t, n);
|
|
306
|
+
}
|
|
307
|
+
return ut(e, r, n), "next";
|
|
308
|
+
}
|
|
309
|
+
function ut(e, t, n) {
|
|
310
|
+
if (_.boundKey() !== n) return;
|
|
311
|
+
let r = y.getState(), i = r.outbox.some((t) => t.id === e.id);
|
|
312
|
+
if (e.convKey in r.tombstones) {
|
|
313
|
+
r.completeHistoryItems([e.id], z());
|
|
314
|
+
return;
|
|
315
|
+
}
|
|
316
|
+
if (t.outcome === "CONVERSATION_DELETED") {
|
|
317
|
+
Qe(e.convKey, e.scopeKey, t.conversation.id), $("deleted");
|
|
318
|
+
return;
|
|
319
|
+
}
|
|
320
|
+
if (r.completeHistoryItems([e.id], z()), !i) return;
|
|
321
|
+
t.droppedMetadataKeys.length > 0 && C("the server did not keep some message details", t.droppedMetadataKeys), r.applyHistoryHeaders(e.scopeKey, { [e.convKey]: m(t.conversation) });
|
|
322
|
+
let a = K(e.convKey);
|
|
323
|
+
if (a?.sync && h(a.sync) === e.scopeKey) {
|
|
324
|
+
let n = new Set(Pe(e)), r = {};
|
|
325
|
+
for (let e of t.entries) e.clientMessageId && n.has(e.clientMessageId) && e.seq !== null && (r[e.clientMessageId] = e.seq);
|
|
326
|
+
let i = Object.values(r);
|
|
327
|
+
b.getState().markHistoryState(e.convKey, [...n], "saved", r), b.getState().patchConversationSync(e.convKey, (e) => {
|
|
328
|
+
let n = e.lastSeq ?? 0, r = i.length > 0 && Math.min(...i) === n + 1 ? Math.max(...i) : n;
|
|
329
|
+
return {
|
|
330
|
+
...e,
|
|
331
|
+
serverId: t.conversation.id,
|
|
332
|
+
revision: Math.max(e.revision ?? 0, t.conversation.revision),
|
|
333
|
+
lastSeq: r,
|
|
334
|
+
...e.blocked === "quota" ? { blocked: void 0 } : {}
|
|
335
|
+
};
|
|
336
|
+
}, { title: t.conversation.title });
|
|
337
|
+
}
|
|
338
|
+
W("pull-after-save", ye, () => $("applied"), !0);
|
|
339
|
+
}
|
|
340
|
+
async function dt(e) {
|
|
341
|
+
let t = E;
|
|
342
|
+
if (!t || p(t) !== e.scopeKey) return "stop";
|
|
343
|
+
let n = e.legacy && e.serverId ? { conversationId: e.serverId } : { clientConversationId: e.convKey }, r = _.boundKey(), i;
|
|
344
|
+
try {
|
|
345
|
+
i = await g.remove(t, n);
|
|
346
|
+
} catch (t) {
|
|
347
|
+
return ft(e, t, r);
|
|
348
|
+
}
|
|
349
|
+
if (_.boundKey() !== r) return "stop";
|
|
350
|
+
let a = y.getState();
|
|
351
|
+
a.completeHistoryItems([e.id], z());
|
|
352
|
+
let o = i.conversation?.id ?? e.serverId;
|
|
353
|
+
return a.recordHistoryTombstone(e.convKey, {
|
|
354
|
+
scopeKey: e.scopeKey,
|
|
355
|
+
at: z(),
|
|
356
|
+
confirmed: !0,
|
|
357
|
+
...o ? { serverId: o } : {}
|
|
358
|
+
}), x.getState().unsavedDelete?.conversationId === e.convKey && x.getState().noteUnsavedDelete(void 0), "next";
|
|
359
|
+
}
|
|
360
|
+
function ft(e, t, n) {
|
|
361
|
+
let r = f(t);
|
|
362
|
+
if (_.boundKey() !== n) return "stop";
|
|
363
|
+
let i = te(r, e, Ie), a = y.getState();
|
|
364
|
+
switch (i.action) {
|
|
365
|
+
case "drop": return a.completeHistoryItems([e.id], z()), b.getState().patchConversationSync(e.convKey, (e) => ({
|
|
366
|
+
...e,
|
|
367
|
+
blocked: "invalid"
|
|
368
|
+
})), Z(r), C("the server refused a saved turn; it stays on this device", r), "next";
|
|
369
|
+
case "pause": return tt(r), "stop";
|
|
370
|
+
case "park": return a.updateHistoryItem(e.id, (e) => ({
|
|
371
|
+
...e,
|
|
372
|
+
attempts: e.attempts + 1,
|
|
373
|
+
...e.type === "save" ? { parked: "quota" } : {},
|
|
374
|
+
nextAttemptAt: z() + Math.max(i.delayMs, u)
|
|
375
|
+
})), b.getState().patchConversationSync(e.convKey, (e) => ({
|
|
376
|
+
...e,
|
|
377
|
+
blocked: "quota"
|
|
378
|
+
})), Z(r), "stop";
|
|
379
|
+
case "hold": return tt(r), "stop";
|
|
380
|
+
case "retry": return a.updateHistoryItem(e.id, (e) => ({
|
|
381
|
+
...e,
|
|
382
|
+
attempts: e.attempts + 1,
|
|
383
|
+
nextAttemptAt: z() + i.delayMs
|
|
384
|
+
})), Z(r), "stop";
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
let $ = (e) => {
|
|
388
|
+
if (H() && !((e === "focus" || e === "visibility") && z() - Ue < ve)) {
|
|
389
|
+
if (Ve) {
|
|
390
|
+
He = !0;
|
|
391
|
+
return;
|
|
392
|
+
}
|
|
393
|
+
Ve = !0, He = !1, Ue = z(), U(pt().catch((e) => C("a history pull stopped unexpectedly", e)).finally(() => {
|
|
394
|
+
Ve = !1, He && $("again");
|
|
395
|
+
}));
|
|
396
|
+
}
|
|
397
|
+
};
|
|
398
|
+
async function pt() {
|
|
399
|
+
let e = E, t = Je();
|
|
400
|
+
if (!e || !t) return;
|
|
401
|
+
gt(t.scopeKey);
|
|
402
|
+
let n = !1;
|
|
403
|
+
for (let r = 0; r < we; r += 1) {
|
|
404
|
+
let i = y.getState().scopes[t.scopeKey], a = i?.gen ?? 0, o;
|
|
405
|
+
try {
|
|
406
|
+
o = await g.changes(e, {
|
|
407
|
+
first: Ce,
|
|
408
|
+
after: i?.cursor ?? null
|
|
409
|
+
});
|
|
410
|
+
} catch (e) {
|
|
411
|
+
G(t) && yt(e);
|
|
412
|
+
return;
|
|
413
|
+
}
|
|
414
|
+
if (!G(t)) return;
|
|
415
|
+
if (j = 0, o.resetRequired && !ht(i)) {
|
|
416
|
+
if (n) return;
|
|
417
|
+
n = !0;
|
|
418
|
+
let e = y.getState().resetHistoryScope(t.scopeKey, z());
|
|
419
|
+
N = {
|
|
420
|
+
scopeKey: t.scopeKey,
|
|
421
|
+
gen: e,
|
|
422
|
+
candidates: _t(t.scopeKey),
|
|
423
|
+
seen: /* @__PURE__ */ new Set()
|
|
424
|
+
}, r = -1;
|
|
425
|
+
continue;
|
|
426
|
+
}
|
|
427
|
+
if (!mt(t.scopeKey, a, o)) return;
|
|
428
|
+
if (!o.pageInfo.hasNextPage) {
|
|
429
|
+
vt(t.scopeKey), kt();
|
|
430
|
+
return;
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
W("pull-more", 0, () => $("more"));
|
|
434
|
+
}
|
|
435
|
+
function mt(e, t, n) {
|
|
436
|
+
let r = y.getState(), i = {}, a = {}, o = [], s = r.scopes[e]?.cursorRev ?? 0;
|
|
437
|
+
for (let t of n.nodes) {
|
|
438
|
+
let n = ie(t);
|
|
439
|
+
if (s = Math.max(s, t.revision), t.deleted) {
|
|
440
|
+
a[n] = { serverId: t.id };
|
|
441
|
+
continue;
|
|
442
|
+
}
|
|
443
|
+
N?.scopeKey === e && N.seen.add(n), !(n in r.tombstones) && (i[n] = m(t), o.push({
|
|
444
|
+
convKey: n,
|
|
445
|
+
serverId: t.id,
|
|
446
|
+
revision: t.revision,
|
|
447
|
+
title: t.title
|
|
448
|
+
}));
|
|
449
|
+
}
|
|
450
|
+
if (!r.applyHistoryFeedPage(e, {
|
|
451
|
+
gen: t,
|
|
452
|
+
headers: i,
|
|
453
|
+
deleted: a,
|
|
454
|
+
cursor: n.pageInfo.endCursor ?? r.scopes[e]?.cursor ?? null,
|
|
455
|
+
cursorRev: s,
|
|
456
|
+
last: !n.pageInfo.hasNextPage,
|
|
457
|
+
at: z()
|
|
458
|
+
})) return !1;
|
|
459
|
+
for (let e of Object.keys(a)) y.getState().dropHistoryItems(e, (e) => e.type === "delete", z());
|
|
460
|
+
X();
|
|
461
|
+
let c = b.getState();
|
|
462
|
+
for (let t of o) {
|
|
463
|
+
let n = c.conversations.find((e) => e.id === t.convKey);
|
|
464
|
+
!n?.sync || h(n.sync) !== e || b.getState().patchConversationSync(t.convKey, (e) => ({
|
|
465
|
+
...e,
|
|
466
|
+
serverId: t.serverId,
|
|
467
|
+
revision: Math.max(e.revision ?? 0, t.revision)
|
|
468
|
+
}), { title: t.title });
|
|
469
|
+
}
|
|
470
|
+
return !0;
|
|
471
|
+
}
|
|
472
|
+
function ht(e) {
|
|
473
|
+
if (e?.resetAt === void 0) return !1;
|
|
474
|
+
let t = z() - e.resetAt;
|
|
475
|
+
return t >= 0 && t < 864e5;
|
|
476
|
+
}
|
|
477
|
+
function gt(e) {
|
|
478
|
+
let t = y.getState().scopes[e], n = t?.gen ?? 0;
|
|
479
|
+
if (t?.rescanning && !(N?.scopeKey === e && N.gen === n)) {
|
|
480
|
+
if (t.hasOlderHeaders) {
|
|
481
|
+
N = null;
|
|
482
|
+
return;
|
|
483
|
+
}
|
|
484
|
+
N = {
|
|
485
|
+
scopeKey: e,
|
|
486
|
+
gen: n,
|
|
487
|
+
candidates: _t(e),
|
|
488
|
+
seen: new Set(Object.keys(t.headers))
|
|
489
|
+
};
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
function _t(e) {
|
|
493
|
+
return new Set(b.getState().conversations.filter((t) => t.sync !== void 0 && !!t.sync.serverId && h(t.sync) === e).map((e) => e.id));
|
|
494
|
+
}
|
|
495
|
+
function vt(e) {
|
|
496
|
+
let t = N;
|
|
497
|
+
if (!t || t.scopeKey !== e || (N = null, (y.getState().scopes[e]?.gen ?? 0) !== t.gen)) return;
|
|
498
|
+
let n = new Set(y.getState().outbox.map((e) => e.convKey)), r = b.getState(), i = r.conversations.filter((r) => t.candidates.has(r.id) && r.sync !== void 0 && h(r.sync) === e && !t.seen.has(r.id) && !n.has(r.id) && !q(r.id)).map((e) => e.id);
|
|
499
|
+
if (i.length === 0) return;
|
|
500
|
+
let a = r.activeConversationId;
|
|
501
|
+
r.removeConversationsSilently(i), a !== null && i.includes(a) && x.getState().noteRemoteDelete({
|
|
502
|
+
conversationId: a,
|
|
503
|
+
wasActive: !0,
|
|
504
|
+
at: z()
|
|
505
|
+
});
|
|
506
|
+
}
|
|
507
|
+
function yt(e) {
|
|
508
|
+
let t = f(e);
|
|
509
|
+
if (t.class === "rejected") {
|
|
510
|
+
$e(d, t);
|
|
511
|
+
return;
|
|
512
|
+
}
|
|
513
|
+
tt(t) || (j += 1, W("pull-retry", Math.max(ee(j, Ie), t.retryAfterMs ?? 0), () => $("retry")));
|
|
514
|
+
}
|
|
515
|
+
let bt = () => {
|
|
516
|
+
let e = E, t = Je();
|
|
517
|
+
!H() || !e || !t || We || (We = !0, U((async () => {
|
|
518
|
+
let n = x.getState().olderHeaders, r = n?.scopeKey === t.scopeKey ? { ...n.headers } : {}, i = M?.scopeKey === t.scopeKey ? M.cursor : null, a = !0;
|
|
519
|
+
for (let n = 0; n < Ee && a; n += 1) {
|
|
520
|
+
let n = await g.conversations(e, {
|
|
521
|
+
first: Te,
|
|
522
|
+
after: i
|
|
523
|
+
});
|
|
524
|
+
if (!G(t)) return;
|
|
525
|
+
let o = y.getState(), s = o.scopes[t.scopeKey]?.headers ?? {}, c = 0;
|
|
526
|
+
for (let e of n.nodes) {
|
|
527
|
+
let t = ie(e);
|
|
528
|
+
e.deleted || t in o.tombstones || t in s || t in r || (r[t] = m(e), c += 1);
|
|
529
|
+
}
|
|
530
|
+
if (i = n.pageInfo.endCursor ?? i, a = n.pageInfo.hasNextPage, M = {
|
|
531
|
+
scopeKey: t.scopeKey,
|
|
532
|
+
cursor: i
|
|
533
|
+
}, c > 0) break;
|
|
534
|
+
}
|
|
535
|
+
let o = y.getState().tombstones, s = Object.fromEntries(Object.entries(r).filter(([e]) => !(e in o)));
|
|
536
|
+
x.getState().setOlderHeaders({
|
|
537
|
+
scopeKey: t.scopeKey,
|
|
538
|
+
headers: s,
|
|
539
|
+
hasMore: a
|
|
540
|
+
});
|
|
541
|
+
})().catch((e) => {
|
|
542
|
+
G(t) && yt(e);
|
|
543
|
+
}).finally(() => {
|
|
544
|
+
We = !1;
|
|
545
|
+
})));
|
|
546
|
+
}, xt = (e, t = {}) => {
|
|
547
|
+
let n = b.getState(), r = n.conversations.find((t) => t.id === e), i = B();
|
|
548
|
+
if (T === null || i === null || !E) {
|
|
549
|
+
r && n.setActiveConversation(e);
|
|
550
|
+
return;
|
|
551
|
+
}
|
|
552
|
+
if (!(_.isBoundToActiveProfile() && e in y.getState().tombstones)) {
|
|
553
|
+
if (!r) {
|
|
554
|
+
let a = Ye(i, e);
|
|
555
|
+
if (!a || a.gone || (y.getState().applyHistoryHeaders(i, { [e]: a }), n.upsertRemoteConversation(ae(e, a, E, {
|
|
556
|
+
modes: t.modes ?? [],
|
|
557
|
+
autoRouting: n.autoRouting,
|
|
558
|
+
openedAt: new Date(z()).toISOString()
|
|
559
|
+
})), r = K(e), !r)) return;
|
|
560
|
+
}
|
|
561
|
+
r.sync && h(r.sync) !== i || (b.getState().setActiveConversation(e), r.sync && (b.getState().patchConversationSync(e, (e) => ({
|
|
562
|
+
...e,
|
|
563
|
+
openedAt: new Date(z()).toISOString()
|
|
564
|
+
})), St(e, "open")), kt());
|
|
565
|
+
}
|
|
566
|
+
}, St = (e, t) => {
|
|
567
|
+
let n = E, r = B();
|
|
568
|
+
if (T === null || !n || r === null || Ge.has(e) || _.heldByNewerBuild()) return;
|
|
569
|
+
if (!H()) {
|
|
570
|
+
Et(e, t);
|
|
571
|
+
return;
|
|
572
|
+
}
|
|
573
|
+
let i = K(e)?.sync;
|
|
574
|
+
if (!i || h(i) !== r || e in y.getState().tombstones) return;
|
|
575
|
+
if (q(e)) {
|
|
576
|
+
Et(e, t);
|
|
577
|
+
return;
|
|
578
|
+
}
|
|
579
|
+
let a = Ye(r, e), o = t === "earlier" ? fe(i) : pe(i, a ? a.lastMessageSeq : null);
|
|
580
|
+
if (!o) return;
|
|
581
|
+
let s = Je();
|
|
582
|
+
s && (Ge.add(e), x.getState().setConversationLoading(e, !0), x.getState().setConversationLoadError(e, !1), U(Ct(e, n, i.origin === "legacy" ? { conversationId: i.serverId ?? e.slice(7) } : { clientConversationId: e }, o, s, t).catch((t) => Tt(e, i, t, s)).finally(() => {
|
|
583
|
+
Ge.delete(e), G(s) && x.getState().setConversationLoading(e, !1);
|
|
584
|
+
})));
|
|
585
|
+
};
|
|
586
|
+
async function Ct(e, t, n, r, i, a) {
|
|
587
|
+
let o = r.paging;
|
|
588
|
+
for (let s = 0; s < De; s += 1) {
|
|
589
|
+
let s = await g.entries(t, n, o);
|
|
590
|
+
if (!G(i)) return;
|
|
591
|
+
if (s.conversation.deleted) {
|
|
592
|
+
Qe(e, i.scopeKey, s.conversation.id);
|
|
593
|
+
return;
|
|
594
|
+
}
|
|
595
|
+
let c = wt(e, i.scopeKey, s, r);
|
|
596
|
+
if (c === "deferred") {
|
|
597
|
+
Et(e, a);
|
|
598
|
+
return;
|
|
599
|
+
}
|
|
600
|
+
if (c !== "applied" || r.kind !== "catch-up" || !s.pageInfo.hasNextPage) return;
|
|
601
|
+
let l = K(e)?.sync?.lastSeq ?? 0, u = s.nodes.at(-1)?.seq ?? null;
|
|
602
|
+
if (u === null || l < u) return;
|
|
603
|
+
o = {
|
|
604
|
+
first: 100,
|
|
605
|
+
sinceSeq: l
|
|
606
|
+
};
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
function wt(e, t, n, r) {
|
|
610
|
+
if (e in y.getState().tombstones) return "skipped";
|
|
611
|
+
let i = K(e), a = i?.sync;
|
|
612
|
+
if (!i || !a || h(a) !== t) return "skipped";
|
|
613
|
+
if (q(e)) return "deferred";
|
|
614
|
+
y.getState().applyHistoryHeaders(t, { [e]: m(n.conversation) });
|
|
615
|
+
let o = n.nodes.map(se), { messages: s, syncPatch: c } = de(a, i.messages, o, n, r);
|
|
616
|
+
return b.getState().applyHistoryEntries(e, s, c), "applied";
|
|
617
|
+
}
|
|
618
|
+
function Tt(e, t, n, r) {
|
|
619
|
+
if (!G(r)) return;
|
|
620
|
+
let i = f(n);
|
|
621
|
+
if (i.class === "not-found") {
|
|
622
|
+
if (t.origin === "created") return;
|
|
623
|
+
y.getState().markHistoryHeaderGone(r.scopeKey, e), x.getState().setConversationLoadError(e, !0), C("the server does not have a conversation the list showed", e);
|
|
624
|
+
return;
|
|
625
|
+
}
|
|
626
|
+
x.getState().setConversationLoadError(e, !0), i.class === "rejected" ? $e(d, i) : tt(i);
|
|
627
|
+
}
|
|
628
|
+
function Et(e, t) {
|
|
629
|
+
P.get(e) !== "open" && P.set(e, t), W("deferred-loads", Se, Dt);
|
|
630
|
+
}
|
|
631
|
+
function Dt() {
|
|
632
|
+
if (!H()) {
|
|
633
|
+
P.size > 0 && T !== null && W("deferred-loads", Se, Dt);
|
|
634
|
+
return;
|
|
635
|
+
}
|
|
636
|
+
for (let [e, t] of [...P]) q(e) || (P.delete(e), St(e, t));
|
|
637
|
+
P.size > 0 && W("deferred-loads", Se, Dt);
|
|
638
|
+
}
|
|
639
|
+
function Ot(e) {
|
|
640
|
+
if (!_.isBoundToActiveProfile()) return [];
|
|
641
|
+
let t = y.getState(), n = new Set(t.outbox.map((e) => e.convKey)), r = b.getState(), i = oe({
|
|
642
|
+
conversations: r.conversations,
|
|
643
|
+
activeConversationId: r.activeConversationId,
|
|
644
|
+
max: e,
|
|
645
|
+
isProtected: (e) => {
|
|
646
|
+
if (n.has(e.id) || q(e.id) || !e.sync) return !0;
|
|
647
|
+
let r = t.scopes[h(e.sync)]?.headers[e.id];
|
|
648
|
+
return !r || r.gone === !0;
|
|
649
|
+
}
|
|
650
|
+
});
|
|
651
|
+
return i.length > 0 && r.evictConversations(i), i;
|
|
652
|
+
}
|
|
653
|
+
function kt() {
|
|
654
|
+
Ot(25);
|
|
655
|
+
}
|
|
656
|
+
function At(e) {
|
|
657
|
+
for (let t of ke) if (Ot(t).length !== 0 && !e()) return;
|
|
658
|
+
}
|
|
659
|
+
function jt() {
|
|
660
|
+
if (_.isBoundToActiveProfile()) {
|
|
661
|
+
for (let e of ke) if (Ot(e).length !== 0 && _.flush()) return;
|
|
662
|
+
W("history-flush", Ae, Mt);
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
function Mt() {
|
|
666
|
+
!_.isBoundToActiveProfile() || !_.persistFailing() || _.flush();
|
|
667
|
+
}
|
|
668
|
+
function Nt(e) {
|
|
669
|
+
if (e) {
|
|
670
|
+
jt();
|
|
671
|
+
return;
|
|
672
|
+
}
|
|
673
|
+
qe("history-flush"), x.getState().unsavedDelete && x.getState().noteUnsavedDelete(void 0);
|
|
674
|
+
}
|
|
675
|
+
function Pt() {
|
|
676
|
+
let e = T === null ? null : ne(S(), Le(), T), t = e ? p(e) : null, n = _.isBoundToActiveProfile() ? y.getState().tombstones : {};
|
|
677
|
+
return (e) => !e.sync || t !== null && p(e.sync) === t && !(e.id in n);
|
|
678
|
+
}
|
|
679
|
+
function Ft() {
|
|
680
|
+
if (l.watchScope === !1) return;
|
|
681
|
+
let t = () => {
|
|
682
|
+
J(), W("scope-poll", _e, t, !0);
|
|
683
|
+
};
|
|
684
|
+
if (W("scope-poll", _e, t, !0), typeof window > "u" || typeof window.addEventListener != "function") return;
|
|
685
|
+
let n = () => {
|
|
686
|
+
J(), $("focus"), Q();
|
|
687
|
+
}, r = () => {
|
|
688
|
+
typeof document < "u" && document.visibilityState !== "visible" || (J(), $("visibility"), Q());
|
|
689
|
+
}, i = () => {
|
|
690
|
+
J(), Mt(), zt(), $("online"), Q();
|
|
691
|
+
}, a = () => J(), o = () => Mt();
|
|
692
|
+
window.addEventListener("focus", n), window.addEventListener("online", i), window.addEventListener("pagehide", o), window.addEventListener("popstate", a), window.addEventListener("storage", a), window.addEventListener(e, a), typeof document < "u" && document.addEventListener("visibilitychange", r), R.push(() => {
|
|
693
|
+
window.removeEventListener("focus", n), window.removeEventListener("online", i), window.removeEventListener("pagehide", o), window.removeEventListener("popstate", a), window.removeEventListener("storage", a), window.removeEventListener(e, a), typeof document < "u" && document.removeEventListener("visibilitychange", r);
|
|
694
|
+
});
|
|
695
|
+
}
|
|
696
|
+
function It(e) {
|
|
697
|
+
T = e, i(() => T === null || _.heldByNewerBuild() ? null : ne(S(), Le(), T)), n(At), r(Pt), _.setWriteListener(Nt), R.push(y.subscribe((e, t) => {
|
|
698
|
+
if (e.tombstones !== t.tombstones && X(), e.outbox !== t.outbox) {
|
|
699
|
+
let n = new Set(t.outbox.map((e) => e.id));
|
|
700
|
+
e.outbox.some((e) => !n.has(e.id)) && Q();
|
|
701
|
+
}
|
|
702
|
+
}), b.subscribe((e, t) => {
|
|
703
|
+
let n = e.activeConversationId, r = n !== null && o(t.processingByConversationId[n]?.status) && !o(e.processingByConversationId[n]?.status);
|
|
704
|
+
(n !== t.activeConversationId || r) && (J(), Y()), F.size > 0 && e.processingByConversationId !== t.processingByConversationId && it(e), P.size > 0 && e.processingByConversationId !== t.processingByConversationId && Dt();
|
|
705
|
+
})), Ft(), J();
|
|
706
|
+
}
|
|
707
|
+
function Lt() {
|
|
708
|
+
T = null, E = null, i(null), n(null), r(null), _.setWriteListener(null), x.getState().noteUnsavedDelete(void 0);
|
|
709
|
+
for (let e of R) e();
|
|
710
|
+
R = [];
|
|
711
|
+
for (let e of [...I.keys()]) qe(e);
|
|
712
|
+
P.clear(), F.clear(), k = !1, x.getState().setHistoryScope(null, null);
|
|
713
|
+
}
|
|
714
|
+
let Rt = (e) => {
|
|
715
|
+
w.set(e, (w.get(e) ?? 0) + 1), T === null ? It(e) : T !== e && C("a second product asked for server history in the same page; the first keeps it", {
|
|
716
|
+
active: T,
|
|
717
|
+
asked: e
|
|
718
|
+
});
|
|
719
|
+
let t = !1;
|
|
720
|
+
return () => {
|
|
721
|
+
if (t) return;
|
|
722
|
+
t = !0;
|
|
723
|
+
let n = (w.get(e) ?? 1) - 1;
|
|
724
|
+
if (n > 0 ? w.set(e, n) : w.delete(e), e !== T || w.has(e)) return;
|
|
725
|
+
Lt();
|
|
726
|
+
let r = w.keys().next();
|
|
727
|
+
r.done || It(r.value);
|
|
728
|
+
};
|
|
729
|
+
};
|
|
730
|
+
function zt() {
|
|
731
|
+
if (!V()) return;
|
|
732
|
+
let e = y.getState();
|
|
733
|
+
for (let t of e.outbox) t.nextAttemptAt === void 0 || t.type === "save" && t.parked || e.updateHistoryItem(t.id, (e) => ({
|
|
734
|
+
...e,
|
|
735
|
+
nextAttemptAt: void 0
|
|
736
|
+
}));
|
|
737
|
+
}
|
|
738
|
+
return {
|
|
739
|
+
enable: Rt,
|
|
740
|
+
setScope: Ze,
|
|
741
|
+
refreshScope: J,
|
|
742
|
+
recordCommittedEntries: rt,
|
|
743
|
+
deleteConversation: at,
|
|
744
|
+
openConversation: xt,
|
|
745
|
+
loadEarlier: (e) => St(e, "earlier"),
|
|
746
|
+
loadOlderHeaders: bt,
|
|
747
|
+
notePanelOpened: () => {
|
|
748
|
+
if (!V()) return;
|
|
749
|
+
let e = y.getState();
|
|
750
|
+
for (let t of e.outbox) t.type !== "save" || !t.parked || e.updateHistoryItem(t.id, (e) => ({
|
|
751
|
+
...e,
|
|
752
|
+
parked: void 0,
|
|
753
|
+
nextAttemptAt: void 0,
|
|
754
|
+
attempts: e.attempts + 1
|
|
755
|
+
}));
|
|
756
|
+
$("history-open"), Q();
|
|
757
|
+
},
|
|
758
|
+
pull: $,
|
|
759
|
+
drain: Q,
|
|
760
|
+
async settled() {
|
|
761
|
+
for (let e = 0; e < 1e3 && L.size > 0; e += 1) await Promise.allSettled([...L]);
|
|
762
|
+
},
|
|
763
|
+
dispose() {
|
|
764
|
+
w.clear(), T !== null && Lt();
|
|
765
|
+
}
|
|
766
|
+
};
|
|
767
|
+
}
|
|
768
|
+
var b = y({
|
|
769
|
+
transport: l(),
|
|
770
|
+
storage: g,
|
|
771
|
+
clock: v()
|
|
772
|
+
});
|
|
773
|
+
//#endregion
|
|
774
|
+
export { _ as ASSISTANT_HISTORY_RESET_TRUST_MS, b as assistantHistory, y as createAssistantHistoryEngine, v as createSystemHistoryClock };
|