@flamingo-stack/openframe-frontend-core 0.0.608-snapshot.20260908153450 → 0.0.608
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/{chunk-QX4ANESW.js → chunk-4LGYBV7U.js} +6 -6
- package/dist/{chunk-DKNXAJMP.cjs → chunk-4Y7LBWRN.cjs} +159 -159
- package/dist/{chunk-DKNXAJMP.cjs.map → chunk-4Y7LBWRN.cjs.map} +1 -1
- package/dist/{chunk-TCEALWE3.cjs → chunk-4ZBC4YSQ.cjs} +33 -33
- package/dist/{chunk-TCEALWE3.cjs.map → chunk-4ZBC4YSQ.cjs.map} +1 -1
- package/dist/{chunk-MVBRMQGU.js → chunk-5QHY4HCC.js} +2 -2
- package/dist/{chunk-L2JWC5VS.cjs → chunk-6T3252KL.cjs} +43 -43
- package/dist/{chunk-L2JWC5VS.cjs.map → chunk-6T3252KL.cjs.map} +1 -1
- package/dist/{chunk-3N4FXBVJ.cjs → chunk-7ADEWG65.cjs} +3 -3
- package/dist/{chunk-3N4FXBVJ.cjs.map → chunk-7ADEWG65.cjs.map} +1 -1
- package/dist/{chunk-D3DTC7SZ.cjs → chunk-7EETXACY.cjs} +15 -15
- package/dist/{chunk-D3DTC7SZ.cjs.map → chunk-7EETXACY.cjs.map} +1 -1
- package/dist/{chunk-HGVW7OHI.js → chunk-AQGQVYDC.js} +2 -2
- package/dist/{chunk-GMOBT5NP.cjs → chunk-BEAO6SUX.cjs} +70 -70
- package/dist/{chunk-GMOBT5NP.cjs.map → chunk-BEAO6SUX.cjs.map} +1 -1
- package/dist/{chunk-C7CTEUU7.js → chunk-BKZCYRGG.js} +2 -2
- package/dist/{chunk-ZRLFBLME.cjs → chunk-C3FIMKT7.cjs} +3 -3
- package/dist/{chunk-ZRLFBLME.cjs.map → chunk-C3FIMKT7.cjs.map} +1 -1
- package/dist/{chunk-JKM5FH63.js → chunk-FK5WVAC3.js} +3 -3
- package/dist/{chunk-RZM6FPOT.cjs → chunk-FRRNSQOG.cjs} +25 -25
- package/dist/{chunk-RZM6FPOT.cjs.map → chunk-FRRNSQOG.cjs.map} +1 -1
- package/dist/{chunk-JR7I2AGL.js → chunk-HONE2UX4.js} +3 -3
- package/dist/{chunk-6MLRFM3T.js → chunk-IK2MM26F.js} +5 -5
- package/dist/{chunk-BSMPCPOZ.js → chunk-JGSEBZYO.js} +38 -155
- package/dist/{chunk-BSMPCPOZ.js.map → chunk-JGSEBZYO.js.map} +1 -1
- package/dist/{chunk-45XXK2VU.cjs → chunk-JIPSQ4P7.cjs} +10 -10
- package/dist/{chunk-45XXK2VU.cjs.map → chunk-JIPSQ4P7.cjs.map} +1 -1
- package/dist/{chunk-NJUSDREV.cjs → chunk-JZBRMXB7.cjs} +22 -22
- package/dist/{chunk-NJUSDREV.cjs.map → chunk-JZBRMXB7.cjs.map} +1 -1
- package/dist/{chunk-O2M4T34Q.cjs → chunk-L77QUPE5.cjs} +4 -4
- package/dist/{chunk-O2M4T34Q.cjs.map → chunk-L77QUPE5.cjs.map} +1 -1
- package/dist/{chunk-SEW25Y2W.cjs → chunk-LJMTURDD.cjs} +10 -10
- package/dist/{chunk-SEW25Y2W.cjs.map → chunk-LJMTURDD.cjs.map} +1 -1
- package/dist/{chunk-LA2W5L6K.js → chunk-MDXSLZML.js} +2 -2
- package/dist/{chunk-FYG7QNAO.cjs → chunk-N25MIHYE.cjs} +5 -5
- package/dist/{chunk-FYG7QNAO.cjs.map → chunk-N25MIHYE.cjs.map} +1 -1
- package/dist/{chunk-VCYRJY7J.js → chunk-N6GFOA4J.js} +4 -4
- package/dist/{chunk-B7LYKBCD.js → chunk-ORGNII3T.js} +5 -5
- package/dist/{chunk-B3UXNGVM.js → chunk-PLPRL4YX.js} +3 -3
- package/dist/{chunk-BUXGWBTF.js → chunk-RN3DBEWX.js} +2 -2
- package/dist/{chunk-JOCVQZGT.js → chunk-UCKFVNYV.js} +5 -5
- package/dist/{chunk-XNDTYTVT.cjs → chunk-VK743MVL.cjs} +71 -188
- package/dist/chunk-VK743MVL.cjs.map +1 -0
- package/dist/{chunk-OTYV6SDN.js → chunk-WHHA3LVL.js} +3 -3
- package/dist/{chunk-CZTOSG7Q.cjs → chunk-WZROKAJT.cjs} +22 -22
- package/dist/{chunk-CZTOSG7Q.cjs.map → chunk-WZROKAJT.cjs.map} +1 -1
- package/dist/{chunk-TJ3GFULR.js → chunk-XATCZQGR.js} +3 -3
- package/dist/{chunk-3XNMDLGB.cjs → chunk-YTXKRN6K.cjs} +9 -9
- package/dist/{chunk-3XNMDLGB.cjs.map → chunk-YTXKRN6K.cjs.map} +1 -1
- package/dist/components/case-studies/index.cjs +9 -9
- package/dist/components/case-studies/index.js +3 -3
- package/dist/components/chat/index.cjs +2 -2
- package/dist/components/chat/index.js +1 -1
- package/dist/components/chat/utils/history-merge.d.ts +2 -4
- package/dist/components/chat/utils/history-merge.d.ts.map +1 -1
- package/dist/components/contact/index.cjs +4 -4
- package/dist/components/contact/index.js +3 -3
- package/dist/components/docs/index.cjs +7 -7
- package/dist/components/docs/index.js +6 -6
- package/dist/components/embeds/index.cjs +4 -4
- package/dist/components/embeds/index.js +3 -3
- package/dist/components/faq/index.cjs +4 -4
- package/dist/components/faq/index.js +3 -3
- package/dist/components/features/index.cjs +3 -3
- package/dist/components/features/index.js +2 -2
- package/dist/components/help-center-pages/index.cjs +23 -23
- package/dist/components/help-center-pages/index.js +14 -14
- package/dist/components/index.cjs +38 -38
- package/dist/components/index.js +13 -13
- package/dist/components/meeting-scheduler/index.cjs +24 -24
- package/dist/components/meeting-scheduler/index.js +2 -2
- package/dist/components/navigation/index.cjs +6 -6
- package/dist/components/navigation/index.js +5 -5
- package/dist/components/onboarding-guides/index.cjs +6 -6
- package/dist/components/onboarding-guides/index.js +5 -5
- package/dist/components/tickets/index.cjs +7 -7
- package/dist/components/tickets/index.js +6 -6
- package/dist/components/ui/index.cjs +3 -3
- package/dist/components/ui/index.js +2 -2
- package/dist/index.cjs +7 -7
- package/dist/index.js +6 -6
- package/package.json +1 -1
- package/src/components/chat/utils/__tests__/history-merge.test.ts +0 -292
- package/src/components/chat/utils/history-merge.ts +56 -276
- package/dist/chunk-XNDTYTVT.cjs.map +0 -1
- /package/dist/{chunk-QX4ANESW.js.map → chunk-4LGYBV7U.js.map} +0 -0
- /package/dist/{chunk-MVBRMQGU.js.map → chunk-5QHY4HCC.js.map} +0 -0
- /package/dist/{chunk-HGVW7OHI.js.map → chunk-AQGQVYDC.js.map} +0 -0
- /package/dist/{chunk-C7CTEUU7.js.map → chunk-BKZCYRGG.js.map} +0 -0
- /package/dist/{chunk-JKM5FH63.js.map → chunk-FK5WVAC3.js.map} +0 -0
- /package/dist/{chunk-JR7I2AGL.js.map → chunk-HONE2UX4.js.map} +0 -0
- /package/dist/{chunk-6MLRFM3T.js.map → chunk-IK2MM26F.js.map} +0 -0
- /package/dist/{chunk-LA2W5L6K.js.map → chunk-MDXSLZML.js.map} +0 -0
- /package/dist/{chunk-VCYRJY7J.js.map → chunk-N6GFOA4J.js.map} +0 -0
- /package/dist/{chunk-B7LYKBCD.js.map → chunk-ORGNII3T.js.map} +0 -0
- /package/dist/{chunk-B3UXNGVM.js.map → chunk-PLPRL4YX.js.map} +0 -0
- /package/dist/{chunk-BUXGWBTF.js.map → chunk-RN3DBEWX.js.map} +0 -0
- /package/dist/{chunk-JOCVQZGT.js.map → chunk-UCKFVNYV.js.map} +0 -0
- /package/dist/{chunk-OTYV6SDN.js.map → chunk-WHHA3LVL.js.map} +0 -0
- /package/dist/{chunk-TJ3GFULR.js.map → chunk-XATCZQGR.js.map} +0 -0
|
@@ -63,33 +63,6 @@ function isSyntheticRealtimeId(id: string): boolean {
|
|
|
63
63
|
return SYNTHETIC_REALTIME_ID_PREFIXES.some(prefix => id.startsWith(prefix));
|
|
64
64
|
}
|
|
65
65
|
|
|
66
|
-
/** Host-minted user bubble of a send in flight. Deliberately NOT one of the
|
|
67
|
-
* synthetic-realtime prefixes — see the note above them. */
|
|
68
|
-
function isOptimisticId(id: string): boolean {
|
|
69
|
-
return id.startsWith('optimistic-');
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
/** Peer `MESSAGE_REQUEST` echo (another session's send). Its persisted twin is
|
|
73
|
-
* the one row kind the backend never stamps with a `lastChunkStreamSeq`, so
|
|
74
|
-
* seq can never prove it in or out of a snapshot — every site that treats
|
|
75
|
-
* `user-` specially exists for that, and all of them go when the backend
|
|
76
|
-
* starts stamping user rows. */
|
|
77
|
-
function isUserRequestSyntheticId(id: string): boolean {
|
|
78
|
-
return id.startsWith('user-');
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
/** A row this client made up and never fetched. Everything else (`welcome-`
|
|
82
|
-
* included) reads as a persisted row where the thread's own shape cannot say
|
|
83
|
-
* otherwise. */
|
|
84
|
-
function isClientMintedId(id: string): boolean {
|
|
85
|
-
return isSyntheticRealtimeId(id) || isOptimisticId(id);
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/** The host's greeting on a fresh dialog: never persisted, belongs at the top. */
|
|
89
|
-
function isWelcomeId(id: string): boolean {
|
|
90
|
-
return id.startsWith('welcome-');
|
|
91
|
-
}
|
|
92
|
-
|
|
93
66
|
/** Rendered answer text of an assistant message (TEXT segments, or a plain string). Used to
|
|
94
67
|
* recognise a replayed synthetic as the twin of an already-persisted turn when the stream-seq
|
|
95
68
|
* signal can't prove it (see the trailing-turn fallback below). Empty for tool/approval-only
|
|
@@ -145,73 +118,15 @@ function turnRequestKeys(content: MessageContent): Set<string> {
|
|
|
145
118
|
return keys;
|
|
146
119
|
}
|
|
147
120
|
|
|
148
|
-
/** Whether any of
|
|
149
|
-
function sharesRequestKey(
|
|
150
|
-
if (keys.size === 0
|
|
151
|
-
for (const key of
|
|
152
|
-
if (
|
|
121
|
+
/** Whether `content` carries any of `keys` — see `turnRequestKeys`. */
|
|
122
|
+
function sharesRequestKey(content: MessageContent, keys: ReadonlySet<string>): boolean {
|
|
123
|
+
if (keys.size === 0) return false;
|
|
124
|
+
for (const key of turnRequestKeys(content)) {
|
|
125
|
+
if (keys.has(key)) return true;
|
|
153
126
|
}
|
|
154
127
|
return false;
|
|
155
128
|
}
|
|
156
129
|
|
|
157
|
-
/** A user row's identity is its text; empty text is no identity (system rows
|
|
158
|
-
* persist with `content: ''`). `''` for any other row. */
|
|
159
|
-
function userText(m: MergeableChatMessage): string {
|
|
160
|
-
return m.role === 'user' && typeof m.content === 'string' ? m.content : '';
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
/** What a set of rows says about the turns they render, so another copy of one
|
|
164
|
-
* of those turns can be recognised under a DIFFERENT id: the backend request
|
|
165
|
-
* ids their segments carry, their answer texts and their user texts. The text
|
|
166
|
-
* maps count occurrences for the callers that pair copies positionally (a
|
|
167
|
-
* repeated prompt twins the NEWEST unclaimed same-text row); `twinVerdict`
|
|
168
|
-
* itself reads them as presence. Empty user text carries no identity (system
|
|
169
|
-
* rows persist with `content: ''`) and is left out. */
|
|
170
|
-
interface TwinSignals {
|
|
171
|
-
requestKeys: Set<string>;
|
|
172
|
-
assistantTexts: Map<string, number>;
|
|
173
|
-
userContents: Map<string, number>;
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
function collectTwinSignals(rows: readonly MergeableChatMessage[]): TwinSignals {
|
|
177
|
-
const signals: TwinSignals = { requestKeys: new Set(), assistantTexts: new Map(), userContents: new Map() };
|
|
178
|
-
for (const row of rows) {
|
|
179
|
-
for (const key of turnRequestKeys(row.content)) signals.requestKeys.add(key);
|
|
180
|
-
if (row.role === 'assistant') {
|
|
181
|
-
const text = assistantAnswerText(row.content);
|
|
182
|
-
if (text !== '') signals.assistantTexts.set(text, (signals.assistantTexts.get(text) ?? 0) + 1);
|
|
183
|
-
continue;
|
|
184
|
-
}
|
|
185
|
-
const text = userText(row);
|
|
186
|
-
if (text !== '') signals.userContents.set(text, (signals.userContents.get(text) ?? 0) + 1);
|
|
187
|
-
}
|
|
188
|
-
return signals;
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
type TwinVerdict = 'twin' | 'distinct' | 'unknown';
|
|
192
|
-
|
|
193
|
-
/** Whether `m` renders one of the turns `signals` describe. 'twin': a shared
|
|
194
|
-
* request id, a user text held verbatim, or an answer text that is a prefix
|
|
195
|
-
* either way of one held — streaming only appends, so a copy cut short, or a
|
|
196
|
-
* persisted row still being written, is a prefix of the full one. 'unknown':
|
|
197
|
-
* the row carries nothing a twin could be recognised by (no user text, no
|
|
198
|
-
* request id, no answer text), so "no match" says nothing about it. */
|
|
199
|
-
function twinVerdict(signals: TwinSignals, m: MergeableChatMessage): TwinVerdict {
|
|
200
|
-
if (m.role !== 'assistant') {
|
|
201
|
-
const text = userText(m);
|
|
202
|
-
if (text === '') return 'unknown';
|
|
203
|
-
return signals.userContents.has(text) ? 'twin' : 'distinct';
|
|
204
|
-
}
|
|
205
|
-
const keys = turnRequestKeys(m.content);
|
|
206
|
-
if (keys.size > 0) return sharesRequestKey(keys, signals.requestKeys) ? 'twin' : 'distinct';
|
|
207
|
-
const text = assistantAnswerText(m.content);
|
|
208
|
-
if (text === '') return 'unknown';
|
|
209
|
-
for (const other of signals.assistantTexts.keys()) {
|
|
210
|
-
if (other.startsWith(text) || text.startsWith(other)) return 'twin';
|
|
211
|
-
}
|
|
212
|
-
return 'distinct';
|
|
213
|
-
}
|
|
214
|
-
|
|
215
130
|
/** Flattens DESC-sorted message pages (newest page first, newest message
|
|
216
131
|
* first within a page) into one chronological list. */
|
|
217
132
|
export function flattenMessagePagesChronological<T>(pages: readonly { messages: readonly T[] }[] | undefined): T[] {
|
|
@@ -393,7 +308,7 @@ export function mergeHistoryWithRealtime<M extends MergeableChatMessage>(input:
|
|
|
393
308
|
// `approvalRequestId`, widened to tool calls.
|
|
394
309
|
const trailingKeys = turnRequestKeys(historyTrailingAssistant.content);
|
|
395
310
|
const isSameTurn = (m: M): boolean =>
|
|
396
|
-
m.id === historyTrailingAssistant.id || sharesRequestKey(
|
|
311
|
+
m.id === historyTrailingAssistant.id || (trailingKeys.size > 0 && sharesRequestKey(m.content, trailingKeys));
|
|
397
312
|
const richerTwin = existingMessages.find(
|
|
398
313
|
m =>
|
|
399
314
|
m !== historyTrailingAssistant &&
|
|
@@ -424,112 +339,42 @@ export function mergeHistoryWithRealtime<M extends MergeableChatMessage>(input:
|
|
|
424
339
|
const trailingAssistantText =
|
|
425
340
|
lastProcessed && lastProcessed.role === 'assistant' ? assistantAnswerText(lastProcessed.content) : '';
|
|
426
341
|
|
|
427
|
-
//
|
|
428
|
-
//
|
|
429
|
-
// `
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
//
|
|
434
|
-
//
|
|
435
|
-
//
|
|
436
|
-
//
|
|
437
|
-
|
|
438
|
-
// older than the window as "a persisted row reached this seq" and DROPPED it,
|
|
439
|
-
// and an unordered keep appended a persisted row AFTER the newest reply. Both
|
|
440
|
-
// were the reported "conversation reverts to an earlier state after a
|
|
441
|
-
// refresh". Rows older than the window are kept verbatim, in their existing
|
|
442
|
-
// order, in FRONT of it.
|
|
443
|
-
//
|
|
444
|
-
// `existingMessages` is chronological (every write path appends, prepends an
|
|
445
|
-
// older page, or is this merge), so the thread's own shape decides first:
|
|
446
|
-
// - where it holds window rows, the first of them is the boundary. Rows
|
|
447
|
-
// from it on are the window and its live tail, whatever their id or
|
|
448
|
-
// clock says — a host-minted tail row such as a ticket preview must not
|
|
449
|
-
// be pulled to the top. One exception before it: a client-minted row
|
|
450
|
-
// that shares a request id with a window row IS that row's copy, placed
|
|
451
|
-
// by a previous merge while the row's stamp was missing, and goes back to
|
|
452
|
-
// coverage so the placement heals instead of being cemented.
|
|
453
|
-
// - where it holds none (every window row is the persisted twin of a
|
|
454
|
-
// synthetic, or the store was empty), the window IS the newest persisted
|
|
455
|
-
// rows, so a persisted id absent from it is older by construction — and
|
|
456
|
-
// so is everything before the last such row. "Persisted" needs the row
|
|
457
|
-
// to predate the fetch: a host-minted row newer than it (a ticket
|
|
458
|
-
// preview) is not history, whatever its id. Unless the thread holds no
|
|
459
|
-
// client-minted row at all: then nothing bridges those rows to the
|
|
460
|
-
// window, the gap is rows this client never saw, and the next older page
|
|
461
|
-
// would be prepended above them out of order — the window alone is the
|
|
462
|
-
// thread, the rest is refetchable (a `welcome-` bubble is not, and stays).
|
|
463
|
-
// Only rows past both anchors are judged on their own:
|
|
464
|
-
// - a seq-stamped synthetic below every stamped window row: seq is
|
|
465
|
-
// monotonic per dialog, but a copy CUT SHORT carries its last consumed
|
|
466
|
-
// chunk's seq, below its twin's stamp — so `twinVerdict` decides where it
|
|
467
|
-
// has a signal, and seq decides an 'unknown' only where it cannot
|
|
468
|
-
// mislead: not for a `user-` / optimistic copy (twin never stamped), nor
|
|
469
|
-
// for an assistant copy while the window holds an unstamped assistant
|
|
470
|
-
// row. A `direct-` / `system-` copy has a stamped twin, so seq is exact.
|
|
471
|
-
// - an unstamped synthetic / optimistic row: minted BEFORE the fetch
|
|
472
|
-
// (`historyFetchedAt`, client clock like the row's own stamp, so a send
|
|
473
|
-
// after the fetch never qualifies), timestamped before the window's
|
|
474
|
-
// oldest row (server clock — a client clock behind the server can still
|
|
475
|
-
// misread a pre-fetch row, hence the verdict too) and 'distinct'.
|
|
476
|
-
// Replay-minted synthetics carry fresh timestamps and never qualify.
|
|
477
|
-
const isInWindow = (m: M): boolean => processedIds.has(m.id) || (rawHistoryIds?.has(m.id) ?? false);
|
|
478
|
-
const firstWindowIndex = existingMessages.findIndex(isInWindow);
|
|
479
|
-
const isPersistedOlder = (m: M): boolean =>
|
|
480
|
-
!isClientMintedId(m.id) && (m.timestamp?.getTime() ?? 0) <= historyFetchedAt;
|
|
481
|
-
let lastOlderPersistedIndex = -1;
|
|
482
|
-
if (firstWindowIndex < 0) {
|
|
483
|
-
for (const [index, m] of existingMessages.entries()) {
|
|
484
|
-
if (isPersistedOlder(m) && !pinnedSyntheticIds.has(m.id) && !droppedSyntheticIds.has(m.id)) {
|
|
485
|
-
lastOlderPersistedIndex = index;
|
|
486
|
-
}
|
|
487
|
-
}
|
|
488
|
-
}
|
|
489
|
-
let windowMinSeq = Number.POSITIVE_INFINITY;
|
|
490
|
-
let windowMinTime = Number.POSITIVE_INFINITY;
|
|
491
|
-
let windowHasUnstampedAssistant = false;
|
|
342
|
+
// Positional content multiset for `user-` synthetic dedup. The backend
|
|
343
|
+
// persists user MESSAGE_REQUEST rows WITHOUT a `lastChunkStreamSeq`, so a
|
|
344
|
+
// replayed/echoed `user-` synthetic can be covered by neither seq (its twin
|
|
345
|
+
// adds 0 to every seq max) nor wall-clock (replay re-mints a fresh
|
|
346
|
+
// timestamp). Match it by TEXT against persisted user rows, positionally: the
|
|
347
|
+
// k-th `user-` synthetic with a text is the twin of the k-th persisted user
|
|
348
|
+
// row with that text. This drops a replay/echo once its own row is in the
|
|
349
|
+
// snapshot (kills the client→viewer duplicate, trailing twin or not) while
|
|
350
|
+
// keeping a LATER same-text turn whose row hasn't persisted yet — which the
|
|
351
|
+
// old first-match `findIndex` dropped, losing a repeated message.
|
|
352
|
+
const persistedUserContentCounts = new Map<string, number>();
|
|
492
353
|
for (const pm of processedToUse) {
|
|
493
|
-
if (typeof pm.
|
|
494
|
-
|
|
495
|
-
} else if (pm.role === 'assistant') {
|
|
496
|
-
windowHasUnstampedAssistant = true;
|
|
354
|
+
if (pm.role === 'user' && typeof pm.content === 'string') {
|
|
355
|
+
persistedUserContentCounts.set(pm.content, (persistedUserContentCounts.get(pm.content) ?? 0) + 1);
|
|
497
356
|
}
|
|
498
|
-
const time = pm.timestamp?.getTime();
|
|
499
|
-
if (typeof time === 'number' && time < windowMinTime) windowMinTime = time;
|
|
500
357
|
}
|
|
501
|
-
const
|
|
502
|
-
const seqCanMislead = (m: M): boolean =>
|
|
503
|
-
m.role === 'assistant' ? windowHasUnstampedAssistant : isUserRequestSyntheticId(m.id) || isOptimisticId(m.id);
|
|
504
|
-
const isOlderThanWindow = (m: M, index: number): boolean => {
|
|
505
|
-
if (m.id === streamingMessageId) return false;
|
|
506
|
-
if (firstWindowIndex >= 0) {
|
|
507
|
-
if (index >= firstWindowIndex) return false;
|
|
508
|
-
return !(isClientMintedId(m.id) && sharesRequestKey(turnRequestKeys(m.content), windowSignals.requestKeys));
|
|
509
|
-
}
|
|
510
|
-
if (index < lastOlderPersistedIndex) return true;
|
|
511
|
-
if (!isClientMintedId(m.id)) return isPersistedOlder(m);
|
|
512
|
-
if (typeof m.streamSeq === 'number') {
|
|
513
|
-
if (!Number.isFinite(windowMinSeq) || m.streamSeq >= windowMinSeq) return false;
|
|
514
|
-
const verdict = twinVerdict(windowSignals, m);
|
|
515
|
-
return verdict === 'unknown' ? !seqCanMislead(m) : verdict === 'distinct';
|
|
516
|
-
}
|
|
517
|
-
const time = m.timestamp?.getTime();
|
|
518
|
-
if (typeof time !== 'number' || time > historyFetchedAt) return false;
|
|
519
|
-
if (!Number.isFinite(windowMinTime) || time >= windowMinTime) return false;
|
|
520
|
-
return twinVerdict(windowSignals, m) === 'distinct';
|
|
521
|
-
};
|
|
358
|
+
const seenUserSyntheticByContent = new Map<string, number>();
|
|
522
359
|
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
360
|
+
const realtimeMessages = existingMessages.filter(m => {
|
|
361
|
+
// Pin wins over everything: the twin may carry a persisted Mongo id (the
|
|
362
|
+
// chunk processors ADOPT an in-progress trailing assistant after a prior
|
|
363
|
+
// merge), in which case `processedIds`/`rawHistoryIds` would drop it even
|
|
364
|
+
// though the pin branch above already removed history's copy — vanishing
|
|
365
|
+
// the whole turn.
|
|
366
|
+
if (pinnedSyntheticIds.has(m.id)) return true;
|
|
367
|
+
if (droppedSyntheticIds.has(m.id)) return false;
|
|
368
|
+
if (processedIds.has(m.id)) return false;
|
|
369
|
+
if (rawHistoryIds?.has(m.id)) return false;
|
|
370
|
+
if (m.role === 'user' && m.id.startsWith('optimistic-') && typeof m.content === 'string') {
|
|
526
371
|
// Content-dedup only when the message predates the snapshot — a
|
|
527
372
|
// just-sent message whose text repeats an earlier turn ("yes", "ok")
|
|
528
373
|
// must not vanish against stale history. Wall-clock, not seq coverage:
|
|
529
374
|
// optimistic messages are minted on send, never by chunk replay, so
|
|
530
375
|
// their timestamps are trustworthy.
|
|
531
376
|
const canBeInSnapshot = (m.timestamp?.getTime() ?? 0) <= historyFetchedAt;
|
|
532
|
-
return !(canBeInSnapshot &&
|
|
377
|
+
return !(canBeInSnapshot && processedToUse.some(pm => pm.role === 'user' && pm.content === m.content));
|
|
533
378
|
}
|
|
534
379
|
// Freshness rule: a synthetic whose turn is represented in the snapshot
|
|
535
380
|
// (under its persisted Mongo id) must be dropped or the turn renders
|
|
@@ -549,7 +394,7 @@ export function mergeHistoryWithRealtime<M extends MergeableChatMessage>(input:
|
|
|
549
394
|
// which carry the chunk seq), so seq coverage can NEVER see them (the twin
|
|
550
395
|
// adds 0 to every seq max) and wall-clock can't either (replay re-mints a
|
|
551
396
|
// fresh timestamp). Dedup them purely by TEXT, matched positionally
|
|
552
|
-
// against persisted user rows (`
|
|
397
|
+
// against persisted user rows (see `persistedUserContentCounts`): drop the
|
|
553
398
|
// synthetic once its own row is in the snapshot — trailing twin or not,
|
|
554
399
|
// killing the client→viewer duplicate — while keeping a later same-text
|
|
555
400
|
// turn whose row hasn't persisted yet. `direct-` / `system-` synthetics
|
|
@@ -559,11 +404,11 @@ export function mergeHistoryWithRealtime<M extends MergeableChatMessage>(input:
|
|
|
559
404
|
// `lastChunkStreamSeq`; the durable fix is the backend stamping it (as it
|
|
560
405
|
// already does for DIRECT/SYSTEM), which also stops the replay at the
|
|
561
406
|
// source via a correct `optStartSeq`.
|
|
562
|
-
if (m.role === 'user' &&
|
|
407
|
+
if (m.role === 'user' && m.id.startsWith('user-') && typeof m.content === 'string') {
|
|
563
408
|
const seen = (seenUserSyntheticByContent.get(m.content) ?? 0) + 1;
|
|
564
409
|
seenUserSyntheticByContent.set(m.content, seen);
|
|
565
410
|
// No persisted row for THIS occurrence slot → keep (nothing renders it).
|
|
566
|
-
if ((
|
|
411
|
+
if ((persistedUserContentCounts.get(m.content) ?? 0) < seen) return true;
|
|
567
412
|
// A same-text persisted twin exists for this slot. User rows carry no
|
|
568
413
|
// persisted seq, so that twin may be THIS message's OWN row (drop — it
|
|
569
414
|
// renders the message) OR an OLDER identical-text turn while this is a
|
|
@@ -626,32 +471,9 @@ export function mergeHistoryWithRealtime<M extends MergeableChatMessage>(input:
|
|
|
626
471
|
if (covered) return false;
|
|
627
472
|
}
|
|
628
473
|
return true;
|
|
629
|
-
};
|
|
474
|
+
});
|
|
630
475
|
|
|
631
|
-
|
|
632
|
-
const realtimeMessages: M[] = [];
|
|
633
|
-
for (const [index, m] of existingMessages.entries()) {
|
|
634
|
-
// Pin wins over everything: the twin may carry a persisted Mongo id (the
|
|
635
|
-
// chunk processors ADOPT an in-progress trailing assistant after a prior
|
|
636
|
-
// merge), in which case `processedIds`/`rawHistoryIds` would drop it even
|
|
637
|
-
// though the pin branch above already removed history's copy — vanishing
|
|
638
|
-
// the whole turn.
|
|
639
|
-
if (pinnedSyntheticIds.has(m.id)) {
|
|
640
|
-
realtimeMessages.push(m);
|
|
641
|
-
continue;
|
|
642
|
-
}
|
|
643
|
-
if (droppedSyntheticIds.has(m.id) || isInWindow(m)) continue;
|
|
644
|
-
// Persisted rows disjoint from the window with nothing live between — see
|
|
645
|
-
// the boundary note: the gap is rows this client never saw.
|
|
646
|
-
if (firstWindowIndex < 0 && !threadHasClientMintedRow && isPersistedOlder(m) && !isWelcomeId(m.id)) continue;
|
|
647
|
-
if (isOlderThanWindow(m, index)) {
|
|
648
|
-
olderThanWindowMessages.push(m);
|
|
649
|
-
continue;
|
|
650
|
-
}
|
|
651
|
-
if (keepRealtime(m)) realtimeMessages.push(m);
|
|
652
|
-
}
|
|
653
|
-
|
|
654
|
-
return [...olderThanWindowMessages, ...processedToUse, ...realtimeMessages];
|
|
476
|
+
return [...processedToUse, ...realtimeMessages];
|
|
655
477
|
}
|
|
656
478
|
|
|
657
479
|
export interface HistoryPrependResult<M extends MergeableChatMessage> {
|
|
@@ -662,81 +484,39 @@ export interface HistoryPrependResult<M extends MergeableChatMessage> {
|
|
|
662
484
|
|
|
663
485
|
/** Pagination path (an older page arrived via fetchNextPage): everything on
|
|
664
486
|
* screen stays; collect only the messages above the first already-known id,
|
|
665
|
-
* plus a content refresh for that boundary message if it changed.
|
|
666
|
-
*
|
|
667
|
-
* left out (see the note in the body). Returns null when there is nothing to
|
|
668
|
-
* apply. */
|
|
487
|
+
* plus a content refresh for that boundary message if it changed. Returns
|
|
488
|
+
* null when there is nothing to apply. */
|
|
669
489
|
export function computeHistoryPrepend<M extends MergeableChatMessage>(
|
|
670
490
|
processedHistory: M[],
|
|
671
491
|
existingMessages: M[],
|
|
672
492
|
): HistoryPrependResult<M> | null {
|
|
673
493
|
const existingIds = new Set(existingMessages.map(m => m.id));
|
|
674
|
-
const
|
|
675
|
-
|
|
676
|
-
const boundaryIndexInExisting =
|
|
677
|
-
boundaryMessageIndex >= 0
|
|
678
|
-
? existingMessages.findIndex(m => m.id === processedHistory[boundaryMessageIndex].id)
|
|
679
|
-
: -1;
|
|
494
|
+
const newMessages: M[] = [];
|
|
495
|
+
let boundaryMessageIndex = -1;
|
|
680
496
|
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
// each such turn twice, so they are left out — the persisted copy takes over
|
|
686
|
-
// at the next full merge, whose window then reaches them. Matched from the
|
|
687
|
-
// boundary backwards and one claim per live row, because the live rows are
|
|
688
|
-
// the NEWEST turns before it: a repeated prompt or a shared opener further up
|
|
689
|
-
// the page is a different turn and stays. The page is persisted in full, so
|
|
690
|
-
// an answer text twins only in the cut-short direction (the live copy is a
|
|
691
|
-
// prefix of the persisted one).
|
|
692
|
-
const liveAboveBoundary =
|
|
693
|
-
boundaryIndexInExisting > 0
|
|
694
|
-
? existingMessages.slice(0, boundaryIndexInExisting).filter(m => isClientMintedId(m.id))
|
|
695
|
-
: [];
|
|
696
|
-
const twinIndices = new Set<number>();
|
|
697
|
-
if (liveAboveBoundary.length > 0) {
|
|
698
|
-
const liveSignals = collectTwinSignals(liveAboveBoundary);
|
|
699
|
-
const userTwinsLeft = new Map(liveSignals.userContents);
|
|
700
|
-
const assistantTwinsLeft = new Map(liveSignals.assistantTexts);
|
|
701
|
-
for (let i = newEnd - 1; i >= 0; i--) {
|
|
702
|
-
const pm = processedHistory[i];
|
|
703
|
-
if (pm.role === 'user') {
|
|
704
|
-
const text = userText(pm);
|
|
705
|
-
const left = text === '' ? 0 : (userTwinsLeft.get(text) ?? 0);
|
|
706
|
-
if (left === 0) continue;
|
|
707
|
-
userTwinsLeft.set(text, left - 1);
|
|
708
|
-
twinIndices.add(i);
|
|
709
|
-
} else if (pm.role === 'assistant') {
|
|
710
|
-
const keys = turnRequestKeys(pm.content);
|
|
711
|
-
if (keys.size > 0) {
|
|
712
|
-
if (sharesRequestKey(keys, liveSignals.requestKeys)) twinIndices.add(i);
|
|
713
|
-
continue;
|
|
714
|
-
}
|
|
715
|
-
const text = assistantAnswerText(pm.content);
|
|
716
|
-
if (text === '') continue;
|
|
717
|
-
for (const [liveText, left] of assistantTwinsLeft) {
|
|
718
|
-
if (left === 0 || !text.startsWith(liveText)) continue;
|
|
719
|
-
assistantTwinsLeft.set(liveText, left - 1);
|
|
720
|
-
twinIndices.add(i);
|
|
721
|
-
break;
|
|
722
|
-
}
|
|
723
|
-
}
|
|
497
|
+
for (let i = 0; i < processedHistory.length; i++) {
|
|
498
|
+
if (existingIds.has(processedHistory[i].id)) {
|
|
499
|
+
boundaryMessageIndex = i;
|
|
500
|
+
break;
|
|
724
501
|
}
|
|
502
|
+
newMessages.push(processedHistory[i]);
|
|
725
503
|
}
|
|
726
|
-
const newMessages = processedHistory.slice(0, newEnd).filter((_, i) => !twinIndices.has(i));
|
|
727
504
|
|
|
728
505
|
let boundaryMessageId: string | undefined;
|
|
729
506
|
let boundaryUpdates: { content: MessageContent } | undefined;
|
|
730
507
|
|
|
731
|
-
if (
|
|
508
|
+
if (boundaryMessageIndex >= 0) {
|
|
732
509
|
const boundaryMessage = processedHistory[boundaryMessageIndex];
|
|
733
|
-
const existingBoundary = existingMessages
|
|
734
|
-
const existingContent = JSON.stringify(existingBoundary.content);
|
|
735
|
-
const newContent = JSON.stringify(boundaryMessage.content);
|
|
510
|
+
const existingBoundary = existingMessages.find(m => m.id === boundaryMessage.id);
|
|
736
511
|
|
|
737
|
-
if (
|
|
738
|
-
|
|
739
|
-
|
|
512
|
+
if (existingBoundary) {
|
|
513
|
+
const existingContent = JSON.stringify(existingBoundary.content);
|
|
514
|
+
const newContent = JSON.stringify(boundaryMessage.content);
|
|
515
|
+
|
|
516
|
+
if (existingContent !== newContent) {
|
|
517
|
+
boundaryMessageId = boundaryMessage.id;
|
|
518
|
+
boundaryUpdates = { content: boundaryMessage.content };
|
|
519
|
+
}
|
|
740
520
|
}
|
|
741
521
|
}
|
|
742
522
|
|