@pmate/chat-sdk 0.12.0-next.57 → 0.12.0-next.59

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.
@@ -1,3 +1,5 @@
1
+ import { prepareReadSchema } from "./readSchema.js";
2
+ import { ReadBatchJournal, lockReadOwner } from "./readBatchJournal.js";
1
3
  import { Dexie } from "dexie";
2
4
  import { isReadingControl } from "./readingContent.js";
3
5
  import { isValidChatTeamId } from "./thread.js";
@@ -206,7 +208,7 @@ export function removeReadTeam(state, teamId, knownThreadIds = []) {
206
208
  pending: state.pending.filter(operation => !removed.has(operation.threadId)),
207
209
  observedReadIntents: state.observedReadIntents?.filter(intent => !removed.has(intent.threadId)) };
208
210
  }
209
- /** Dexie preserves the legacy database/store and outbound [appId, profileId] keys. */
211
+ /** Preserves the database and logical keys; store renaming fences old format writers. */
210
212
  export class IndexedDbReadIndex {
211
213
  storageScope;
212
214
  migrateLegacy;
@@ -217,15 +219,22 @@ export class IndexedDbReadIndex {
217
219
  messageRows;
218
220
  positionRows;
219
221
  normalizedStates = new WeakSet();
222
+ orderedStates = new WeakSet();
223
+ legacyClean = new Set();
220
224
  observationMessages = new WeakMap();
221
225
  versions;
222
226
  closed = false;
227
+ journal;
228
+ factory;
229
+ schemaReady;
230
+ ownerTail = Promise.resolve();
223
231
  constructor(factory = globalThis.indexedDB, name = "pmate-social-read-index", storageScope, migrateLegacy = false, metadataOnly = false) {
224
232
  this.storageScope = storageScope;
225
233
  this.migrateLegacy = migrateLegacy;
226
234
  this.metadataOnly = metadataOnly;
227
235
  if (!factory)
228
236
  throw new Error("READ_STORAGE_UNAVAILABLE");
237
+ this.factory = factory;
229
238
  this.database = new Dexie(name, { indexedDB: factory, IDBKeyRange: globalThis.IDBKeyRange });
230
239
  // The empty schema retains the legacy out-of-line primary key and records.
231
240
  this.database.version(1).stores({ profiles: "" });
@@ -237,12 +246,39 @@ export class IndexedDbReadIndex {
237
246
  // Keep v4 indexes for already-open older SDKs, and make source discovery
238
247
  // dense for legitimate legacy rows whose optional team field is absent.
239
248
  this.database.version(5).stores({ readThreads: "[owner+id],owner,[owner+id+value.kind+value.teamId],[owner+value.conversationId+value.kind+value.teamId+id],[owner+id+value.conversationId],[owner+id+value.kind],[owner+value.conversationId+value.kind+id]" });
240
- this.positionRows = this.database.table("readPositions");
241
- this.threadRows = this.database.table("readThreads");
242
- this.messageRows = this.database.table("readMessages");
243
- this.profiles = this.database.table("profiles");
244
- this.versions = this.database.table("versions");
249
+ this.database.version(6).stores({ profiles: "unsupportedReadingFormat", versions: "unsupportedReadingFormat", readingProfiles: "", readingVersions: "", readThreads: null, readMessages: null, readPositions: null, readingThreads: "[owner+id],owner,[owner+id+value.kind+value.teamId],[owner+value.conversationId+value.kind+value.teamId+id],[owner+id+value.conversationId],[owner+id+value.kind],[owner+value.conversationId+value.kind+id]", readingMessages: "[owner+thread+id],[owner+thread],owner", readingPositions: "[owner+id],owner", readBatches: "[owner+key],owner" });
250
+ this.journal = new ReadBatchJournal(this.database, () => this.closed);
251
+ this.positionRows = this.database.table("readingPositions");
252
+ this.threadRows = this.database.table("readingThreads");
253
+ this.messageRows = this.database.table("readingMessages");
254
+ this.profiles = this.database.table("readingProfiles");
255
+ this.versions = this.database.table("readingVersions");
245
256
  this.database.on("versionchange", () => { this.database.close({ disableAutoOpen: false }); return false; });
257
+ this.schemaReady = prepareReadSchema(factory, name);
258
+ void this.schemaReady.catch(() => { });
259
+ }
260
+ async owned(appId, profileId, run) {
261
+ const previous = this.ownerTail;
262
+ let release;
263
+ this.ownerTail = new Promise(resolve => { release = resolve; });
264
+ await previous;
265
+ try {
266
+ if (this.closed)
267
+ throw new Error("READ_STORAGE_CLOSED");
268
+ await this.schemaReady;
269
+ if (this.closed)
270
+ throw new Error("READ_STORAGE_CLOSED");
271
+ const owner = JSON.stringify(this.key(appId, profileId));
272
+ return await lockReadOwner(this.factory, this.database.name, owner, async () => { await this.journal.recover(owner); return run(); }, work => this.journal.lease(owner, work));
273
+ }
274
+ catch (error) {
275
+ if (this.closed)
276
+ throw new Error("READ_STORAGE_CLOSED");
277
+ throw error;
278
+ }
279
+ finally {
280
+ release();
281
+ }
246
282
  }
247
283
  async transaction(mode, execute) {
248
284
  for (let attempt = 0; attempt < 2; attempt++) {
@@ -250,11 +286,15 @@ export class IndexedDbReadIndex {
250
286
  throw new Error("READ_STORAGE_CLOSED");
251
287
  let started = false;
252
288
  try {
253
- return await this.database.transaction(mode, [this.profiles, this.versions, this.threadRows, this.messageRows, this.positionRows], async () => {
289
+ return await this.database.transaction(mode, [this.profiles, this.versions, this.threadRows, this.messageRows, this.positionRows, this.database.table("readBatches")], async () => {
254
290
  if (this.closed)
255
291
  throw new Error("READ_STORAGE_CLOSED");
256
292
  started = true;
257
- return execute();
293
+ await this.journal.checkLeases();
294
+ const result = await execute();
295
+ if (mode === "rw")
296
+ await this.journal.checkLeases(true);
297
+ return result;
258
298
  });
259
299
  }
260
300
  catch (error) {
@@ -292,7 +332,7 @@ export class IndexedDbReadIndex {
292
332
  this.readPages(this.messageRows, owner, row => [row.owner, row.thread, row.id], row => {
293
333
  if (!byThread.has(row.thread))
294
334
  byThread.set(row.thread, new Map());
295
- byThread.get(row.thread).set(row.id, row.value);
335
+ byThread.get(row.thread).set(row.id, row);
296
336
  }),
297
337
  this.readPages(this.positionRows, owner, row => [row.owner, row.id], row => byPosition.set(row.id, row)),
298
338
  ]);
@@ -302,14 +342,27 @@ export class IndexedDbReadIndex {
302
342
  const position = byPosition.get(id);
303
343
  if (!row || !position)
304
344
  throw new Error("READ_STORAGE_INCOMPLETE");
305
- return { ...row.value, readThroughSeq: position.readThroughSeq, explicitReadMessageIds: position.explicitReadMessageIds, messages: row.messageIds.map(messageId => {
306
- const message = byThread.get(id)?.get(messageId);
345
+ const stored = byThread.get(id) ?? new Map();
346
+ let messages;
347
+ if (row.messageCount !== undefined && !row.messageIds.length) {
348
+ const ordered = [...stored.values()], ranked = ordered.every(row => Number.isSafeInteger(row.ordinal));
349
+ ordered.sort((a, b) => ranked ? a.ordinal - b.ordinal : a.value.seq - b.value.seq);
350
+ if (ordered.length !== row.messageCount)
351
+ throw new Error("READ_STORAGE_INCOMPLETE");
352
+ messages = ordered.map(row => row.value);
353
+ }
354
+ else
355
+ messages = row.messageIds.map(messageId => {
356
+ const message = stored.get(messageId);
307
357
  if (!message)
308
358
  throw new Error("READ_STORAGE_INCOMPLETE");
309
- return message;
310
- }) };
359
+ return message.value;
360
+ });
361
+ return { ...row.value, readThroughSeq: position.readThroughSeq, explicitReadMessageIds: position.explicitReadMessageIds, messages };
311
362
  }) };
312
363
  this.normalizedStates.add(result);
364
+ if ((threadOrder ?? []).every(id => byId.get(id)?.messageCount !== undefined && !byId.get(id).messageIds.length && [...(byThread.get(id)?.values() ?? [])].every(row => Number.isSafeInteger(row.ordinal))))
365
+ this.orderedStates.add(result);
313
366
  return result;
314
367
  }
315
368
  return current;
@@ -320,140 +373,88 @@ export class IndexedDbReadIndex {
320
373
  return current;
321
374
  const metadata = readingMetadata(current);
322
375
  if (metadata !== current)
323
- await this.persist(appId, profileId, current, metadata);
376
+ await this.writeState(appId, profileId, current, metadata);
324
377
  return metadata;
325
378
  }
326
379
  async loadStored(appId, profileId) {
327
- const current = await this.profiles.get(this.key(appId, profileId));
328
- if (current?.normalized)
329
- return this.hydrate(current, this.key(appId, profileId));
380
+ const current = await this.transaction("r", async () => {
381
+ if (this.storageScope === undefined && (await this.profiles.get([appId, profileId, "legacy-owner-v1"]))?.legacyOwner)
382
+ return undefined;
383
+ const row = await this.profiles.get(this.key(appId, profileId));
384
+ if (row && (row.appId !== appId || row.profileId !== profileId || row.schemaVersion !== 1))
385
+ throw new Error("READ_IDENTITY_MISMATCH");
386
+ return row?.normalized ? this.hydrate(row, this.key(appId, profileId)) : row;
387
+ });
330
388
  if (current) {
331
- const next = this.metadataOnly ? readingMetadata(current) : current;
332
- if (this.migrateLegacy || this.metadataOnly)
333
- await this.persist(appId, profileId, undefined, next, true);
334
- return next;
389
+ await this.cleanupClaimedLegacy(appId, profileId);
390
+ if (!this.normalizedStates.has(current) && (this.migrateLegacy || this.metadataOnly)) {
391
+ const next = this.metadataOnly ? readingMetadata(current) : current;
392
+ await this.writeState(appId, profileId, undefined, next);
393
+ return next;
394
+ }
395
+ return current;
335
396
  }
336
397
  if (!this.migrateLegacy || this.storageScope === undefined)
337
398
  return undefined;
338
- const markerKey = [appId, profileId, "legacy-owner-v1"];
339
- const marker = await this.profiles.get(markerKey);
340
- if (marker && marker.legacyOwner !== this.storageScope)
341
- return undefined;
342
- const legacyRow = await this.profiles.get([appId, profileId]);
343
- if (!legacyRow)
399
+ const legacyOwner = JSON.stringify([appId, profileId]);
400
+ const legacy = await lockReadOwner(this.factory, this.database.name, legacyOwner, async () => {
401
+ await this.journal.recover(legacyOwner);
402
+ const allowed = await this.transaction("rw", async () => {
403
+ const key = [appId, profileId, "legacy-owner-v1"], marker = await this.profiles.get(key);
404
+ if (marker && marker.legacyOwner !== this.storageScope)
405
+ return false;
406
+ const raw = await this.profiles.get([appId, profileId]);
407
+ if (!raw)
408
+ return false;
409
+ if (raw.appId !== appId || raw.profileId !== profileId || raw.schemaVersion !== 1)
410
+ throw new Error("READ_IDENTITY_MISMATCH");
411
+ if (!marker)
412
+ await this.profiles.put({ ...createReadIndex(appId, profileId), legacyOwner: this.storageScope }, key);
413
+ return true;
414
+ });
415
+ if (!allowed)
416
+ return undefined;
417
+ return this.transaction("r", async () => { const row = await this.profiles.get([appId, profileId]); return row?.normalized ? this.hydrate(row, [appId, profileId]) : row; });
418
+ }, work => this.journal.lease(legacyOwner, work));
419
+ if (!legacy)
344
420
  return undefined;
345
- const hydrated = await this.hydrate(legacyRow, [appId, profileId]);
346
- const legacy = this.metadataOnly ? readingMetadata(hydrated) : hydrated;
347
421
  if (legacy.appId !== appId || legacy.profileId !== profileId || legacy.schemaVersion !== 1)
348
422
  throw new Error("READ_IDENTITY_MISMATCH");
349
- const ownership = { ...createReadIndex(appId, profileId), legacyOwner: this.storageScope };
350
- await this.profiles.put(ownership, markerKey);
351
- await this.persist(appId, profileId, undefined, legacy);
352
- await this.profiles.delete([appId, profileId]);
353
- const oldOwner = JSON.stringify([appId, profileId]);
354
- await this.threadRows.where("owner").equals(oldOwner).delete();
355
- await this.messageRows.where("owner").equals(oldOwner).delete();
356
- await this.positionRows.where("owner").equals(oldOwner).delete();
357
- return legacy;
423
+ const next = this.metadataOnly ? readingMetadata(legacy) : legacy;
424
+ await this.writeState(appId, profileId, undefined, next);
425
+ await this.cleanupClaimedLegacy(appId, profileId);
426
+ return next;
358
427
  }
359
- /** Message bodies are immutable for local read observations: keep them out of
360
- * profile/cursor writes, and update only changed normalized rows on sync. */
361
- async persist(appId, profileId, previous, next, replaceLegacy = false) {
362
- if (previous && !this.normalizedStates.has(previous)) {
363
- previous = undefined;
364
- replaceLegacy = true;
365
- }
366
- const owner = JSON.stringify(this.key(appId, profileId));
367
- const oldThreads = new Map(previous?.threads.map(thread => [thread.id, thread]) ?? []);
368
- const nextIds = new Set(next.threads.map(thread => thread.id));
369
- if (nextIds.size !== next.threads.length)
370
- throw new Error("DUPLICATE_READ_THREAD");
371
- const threadWrites = [], messageWrites = [], positionWrites = [];
372
- const messageDeletes = [];
373
- for (const thread of next.threads) {
374
- const old = oldThreads.get(thread.id);
375
- if (old === thread)
376
- continue;
377
- const { messages, readThroughSeq, explicitReadMessageIds, ...value } = thread;
378
- const position = { owner, id: thread.id, readThroughSeq, explicitReadMessageIds };
379
- if (!old || old.readThroughSeq !== readThroughSeq || JSON.stringify(old.explicitReadMessageIds) !== JSON.stringify(explicitReadMessageIds))
380
- positionWrites.push(position);
381
- const oldValue = old ? (({ messages: _messages, readThroughSeq: _seq, explicitReadMessageIds: _explicit, ...rest }) => rest)(old) : undefined;
382
- if (!old || old.messages !== messages || JSON.stringify(oldValue) !== JSON.stringify(value)) {
383
- const metadata = { owner, id: thread.id, value, messageIds: messages.map(message => message.id) };
384
- const oldMetadata = old ? { owner, id: old.id, value: oldValue, messageIds: old.messages.map(message => message.id) } : undefined;
385
- if (JSON.stringify(metadata) !== JSON.stringify(oldMetadata))
386
- threadWrites.push(metadata);
387
- }
388
- if (old?.messages === messages)
389
- continue;
390
- const byId = new Map(old?.messages.map(message => [message.id, message]) ?? []);
391
- const ids = new Set();
392
- for (const message of messages) {
393
- if (ids.has(message.id))
394
- throw new Error("INVALID_READ_MESSAGE");
395
- ids.add(message.id);
396
- const prior = byId.get(message.id);
397
- if (prior !== message && JSON.stringify(prior) !== JSON.stringify(message))
398
- messageWrites.push({ owner, thread: thread.id, id: message.id, value: message });
399
- }
400
- for (const id of byId.keys())
401
- if (!ids.has(id))
402
- messageDeletes.push([owner, thread.id, id]);
428
+ async cleanupClaimedLegacy(appId, profileId) {
429
+ if (this.storageScope === undefined || !this.migrateLegacy)
430
+ return;
431
+ const key = JSON.stringify(this.key(appId, profileId));
432
+ if (this.legacyClean.has(key))
433
+ return;
434
+ const marker = await this.transaction("r", () => this.profiles.get([appId, profileId, "legacy-owner-v1"]));
435
+ if (marker?.legacyOwner === this.storageScope) {
436
+ await this.transaction("rw", () => this.profiles.delete([appId, profileId]));
437
+ await this.cleanUnreachable(JSON.stringify([appId, profileId]), undefined);
403
438
  }
404
- for (const id of oldThreads.keys())
405
- if (!nextIds.has(id)) {
406
- await this.threadRows.delete([owner, id]);
407
- await this.positionRows.delete([owner, id]);
408
- await this.messageRows.where("[owner+thread]").equals([owner, id]).delete();
409
- }
410
- // A legacy document can replace the full profile after normalization. Its
411
- // replacement is authoritative for the row set; discard unreachable bodies
412
- // in the same transaction before installing the rebuilt normalized records.
413
- if (replaceLegacy) {
414
- await this.messageRows.where("owner").equals(owner).delete();
415
- await this.threadRows.where("owner").equals(owner).delete();
416
- await this.positionRows.where("owner").equals(owner).delete();
417
- }
418
- // Bound each IndexedDB bulk request. A snapshot and its cursor still commit
419
- // atomically; bounding total snapshot transaction bytes is separate work.
420
- for (let offset = 0; offset < positionWrites.length; offset += 128)
421
- await this.positionRows.bulkPut(positionWrites.slice(offset, offset + 128));
422
- for (let offset = 0; offset < threadWrites.length; offset += 128)
423
- await this.threadRows.bulkPut(threadWrites.slice(offset, offset + 128));
424
- for (let offset = 0; offset < messageWrites.length; offset += 128)
425
- await this.messageRows.bulkPut(messageWrites.slice(offset, offset + 128));
426
- for (let offset = 0; offset < messageDeletes.length; offset += 128)
427
- await this.messageRows.bulkDelete(messageDeletes.slice(offset, offset + 128));
428
- const header = (state) => ({ ...state, threads: [], normalized: true, threadOrder: state.threads.map(thread => thread.id) });
429
- const nextHeader = header(next);
430
- if (!previous || JSON.stringify(header(previous)) !== JSON.stringify(nextHeader))
431
- await this.profiles.put(nextHeader, this.key(appId, profileId));
432
- this.normalizedStates.add(next);
439
+ this.legacyClean.add(key);
433
440
  }
434
441
  async load(appId, profileId) {
435
- return this.transaction(this.migrateLegacy || this.metadataOnly ? "rw" : "r", () => this.loadCurrent(appId, profileId));
442
+ return this.owned(appId, profileId, () => this.loadCurrent(appId, profileId));
436
443
  }
437
444
  async loadVersioned(appId, profileId) {
438
- return this.transaction(this.migrateLegacy || this.metadataOnly ? "rw" : "r", async () => ({
439
- state: await this.loadCurrent(appId, profileId), versions: (await this.versions.get(this.key(appId, profileId)))?.teams ?? {},
440
- }));
445
+ return this.owned(appId, profileId, async () => ({ state: await this.loadCurrent(appId, profileId), versions: await this.transaction("r", async () => (await this.versions.get(this.key(appId, profileId)))?.teams ?? {}) }));
441
446
  }
442
447
  async loadVersions(appId, profileId) {
443
- return this.transaction("r", async () => (await this.versions.get(this.key(appId, profileId)))?.teams ?? {});
448
+ return this.owned(appId, profileId, () => this.transaction("r", async () => (await this.versions.get(this.key(appId, profileId)))?.teams ?? {}));
444
449
  }
445
450
  async invalidateTeam(appId, profileId, teamId, version, knownThreadIds = []) {
446
451
  if (!isValidChatTeamId(teamId) || !Number.isSafeInteger(version) || version < 1)
447
452
  throw new Error("INVALID_READ_SCOPE_VERSION");
448
- return this.transaction("rw", async () => {
449
- const state = await this.loadCurrent(appId, profileId) ?? createReadIndex(appId, profileId);
450
- const teams = (await this.versions.get(this.key(appId, profileId)))?.teams ?? {};
453
+ return this.owned(appId, profileId, async () => {
454
+ const teams = await this.transaction("r", async () => (await this.versions.get(this.key(appId, profileId)))?.teams ?? {});
451
455
  if (readTeamVersion(teams, teamId) >= version)
452
- return state;
453
- const next = removeReadTeam(state, teamId, knownThreadIds);
454
- await this.versions.put({ teams: { ...teams, [teamId]: version } }, this.key(appId, profileId));
455
- await this.persist(appId, profileId, state, next);
456
- return next;
456
+ return await this.loadCurrent(appId, profileId) ?? createReadIndex(appId, profileId);
457
+ return this.updateOwned(appId, profileId, state => removeReadTeam(state, teamId, knownThreadIds), undefined, { teams: { ...teams, [teamId]: version } });
457
458
  });
458
459
  }
459
460
  async observe(appId, profileId, transform, scope) {
@@ -545,6 +546,8 @@ export class IndexedDbReadIndex {
545
546
  if (thread.readThroughSeq !== before.readThroughSeq || JSON.stringify(thread.explicitReadMessageIds) !== JSON.stringify(before.explicitReadMessageIds))
546
547
  writes.push({ owner, id: thread.id, readThroughSeq: thread.readThroughSeq, explicitReadMessageIds: thread.explicitReadMessageIds });
547
548
  }
549
+ if (writes.length > 126)
550
+ return undefined;
548
551
  if (writes.length)
549
552
  await this.positionRows.bulkPut(writes);
550
553
  if (JSON.stringify(header.pending) !== JSON.stringify(result.pending) || JSON.stringify(header.observedReadIntents) !== JSON.stringify(result.observedReadIntents))
@@ -564,22 +567,97 @@ export class IndexedDbReadIndex {
564
567
  return { ...base, threads, pending: result.pending, observedReadIntents: result.observedReadIntents };
565
568
  }
566
569
  async update(appId, profileId, transform, observation) {
567
- return this.transaction("rw", async () => {
568
- if (observation) {
569
- const next = await this.observe(appId, profileId, transform, observation);
570
- if (next)
571
- return next;
572
- }
573
- const previous = await this.loadCurrent(appId, profileId);
574
- const transformed = transform(previous ?? createReadIndex(appId, profileId), (await this.versions.get(this.key(appId, profileId)))?.teams ?? {});
575
- const next = this.metadataOnly ? readingMetadata(transformed) : transformed;
576
- if (next.appId !== appId || next.profileId !== profileId || next.schemaVersion !== 1)
577
- throw new Error("READ_IDENTITY_MISMATCH");
578
- await this.persist(appId, profileId, previous, next);
570
+ return this.owned(appId, profileId, () => this.updateOwned(appId, profileId, transform, observation));
571
+ }
572
+ async updateOwned(appId, profileId, transform, observation, versions) {
573
+ if (observation) {
574
+ const observed = await this.transaction("rw", () => this.observe(appId, profileId, transform, observation));
575
+ if (observed)
576
+ return observed;
577
+ }
578
+ if (this.storageScope === undefined && (await this.profiles.get([appId, profileId, "legacy-owner-v1"]))?.legacyOwner)
579
+ throw new Error("READ_LEGACY_OWNERSHIP_CHANGED");
580
+ const loaded = { previous: await this.loadCurrent(appId, profileId), versions: await this.transaction("r", async () => (await this.versions.get(this.key(appId, profileId)))?.teams ?? {}) };
581
+ const previous = loaded.previous, transformed = transform(previous ?? createReadIndex(appId, profileId), loaded.versions);
582
+ const next = this.metadataOnly ? readingMetadata(transformed) : transformed;
583
+ if (next.appId !== appId || next.profileId !== profileId || next.schemaVersion !== 1)
584
+ throw new Error("READ_IDENTITY_MISMATCH");
585
+ if (next === previous && !versions)
579
586
  return next;
580
- });
587
+ await this.writeState(appId, profileId, previous, next, versions);
588
+ return next;
589
+ }
590
+ async writeState(appId, profileId, previous, next, versions) {
591
+ const owner = JSON.stringify(this.key(appId, profileId)), header = { ...next, threads: [], normalized: true, threadOrder: next.threads.map(thread => thread.id) };
592
+ if (!previous || !this.orderedStates.has(previous))
593
+ await this.cleanUnreachable(owner, previous);
594
+ await this.journal.commit(owner, this.changes(owner, previous, next), { profileKey: this.key(appId, profileId), header, versions });
595
+ this.normalizedStates.add(next);
596
+ this.orderedStates.add(next);
597
+ }
598
+ async cleanUnreachable(owner, previous) {
599
+ const threads = new Map(previous?.threads.map(thread => [thread.id, new Set(thread.messages.map(message => message.id))]) ?? []);
600
+ for (const name of ["readingThreads", "readingPositions", "readingMessages"]) {
601
+ const table = this.database.table(name);
602
+ let after;
603
+ for (;;) {
604
+ const keys = await table.where(":id").between(after ?? [owner, Dexie.minKey], [owner, Dexie.maxKey], !after, true).limit(126).primaryKeys();
605
+ if (!keys.length)
606
+ break;
607
+ const dead = keys.filter(key => !threads.has(key[1]) || name === "readingMessages" && !threads.get(key[1]).has(key[2]));
608
+ if (dead.length)
609
+ await this.transaction("rw", () => table.bulkDelete(dead));
610
+ after = keys[keys.length - 1];
611
+ if (keys.length < 126)
612
+ break;
613
+ }
614
+ }
615
+ }
616
+ *changes(owner, previous, next) {
617
+ const oldThreads = new Map(previous?.threads.map(thread => [thread.id, thread]) ?? []), ids = new Set(), modern = previous !== undefined && this.orderedStates.has(previous);
618
+ for (const thread of next.threads) {
619
+ if (ids.has(thread.id))
620
+ throw new Error("DUPLICATE_READ_THREAD");
621
+ ids.add(thread.id);
622
+ const old = oldThreads.get(thread.id);
623
+ if (old === thread && modern)
624
+ continue;
625
+ const { messages, readThroughSeq, explicitReadMessageIds, ...value } = thread;
626
+ if (!modern || !old || old.readThroughSeq !== readThroughSeq || JSON.stringify(old.explicitReadMessageIds) !== JSON.stringify(explicitReadMessageIds))
627
+ yield { table: "readingPositions", key: [owner, thread.id], value: { owner, id: thread.id, readThroughSeq, explicitReadMessageIds } };
628
+ const metadata = { owner, id: thread.id, value, messageIds: [], messageCount: messages.length };
629
+ const oldMetadata = old ? { owner, id: old.id, value: (({ messages: _m, readThroughSeq: _r, explicitReadMessageIds: _e, ...rest }) => rest)(old), messageIds: modern ? [] : old.messages.map(message => message.id), ...(modern ? { messageCount: old.messages.length } : {}) } : undefined;
630
+ if (JSON.stringify(metadata) !== JSON.stringify(oldMetadata))
631
+ yield { table: "readingThreads", key: [owner, thread.id], value: metadata };
632
+ if (old?.messages === messages && modern)
633
+ continue;
634
+ const byId = new Map(old?.messages.map(message => [message.id, message]) ?? []), ranks = new Map(old?.messages.map((message, index) => [message.id, index]) ?? []), seen = new Set();
635
+ for (const [ordinal, message] of messages.entries()) {
636
+ if (seen.has(message.id))
637
+ throw new Error("INVALID_READ_MESSAGE");
638
+ seen.add(message.id);
639
+ const prior = byId.get(message.id);
640
+ if (!modern || ranks.get(message.id) !== ordinal || prior !== message && JSON.stringify(prior) !== JSON.stringify(message))
641
+ yield { table: "readingMessages", key: [owner, thread.id, message.id], value: { owner, thread: thread.id, id: message.id, value: message, ordinal } };
642
+ }
643
+ for (const id of byId.keys())
644
+ if (!seen.has(id))
645
+ yield { table: "readingMessages", key: [owner, thread.id, id] };
646
+ }
647
+ for (const old of oldThreads.values())
648
+ if (!ids.has(old.id)) {
649
+ yield { table: "readingThreads", key: [owner, old.id] };
650
+ yield { table: "readingPositions", key: [owner, old.id] };
651
+ for (const message of old.messages)
652
+ yield { table: "readingMessages", key: [owner, old.id, message.id] };
653
+ }
654
+ }
655
+ async close() { this.closed = true; try {
656
+ await this.journal.releaseLeases();
581
657
  }
582
- async close() { this.closed = true; this.database.close(); }
658
+ finally {
659
+ this.database.close();
660
+ } }
583
661
  }
584
662
  export function sourcesInConversation(state, conversationId) {
585
663
  return state.threads.filter(thread => (thread.conversationId ?? thread.id) === conversationId);
@@ -0,0 +1,2 @@
1
+ /** Metadata-only store renaming fences SDKs that cannot read chunked snapshots. */
2
+ export declare function prepareReadSchema(factory: IDBFactory, name: string): Promise<void>;
@@ -0,0 +1,58 @@
1
+ /** Metadata-only store renaming fences SDKs that cannot read chunked snapshots. */
2
+ export async function prepareReadSchema(factory, name) {
3
+ const existing = await new Promise((resolve, reject) => {
4
+ const request = factory.open(name);
5
+ let fresh = false;
6
+ request.onupgradeneeded = event => { if (event.oldVersion === 0) {
7
+ fresh = true;
8
+ request.transaction.abort();
9
+ } };
10
+ request.onerror = event => { event.preventDefault(); if (fresh)
11
+ resolve(undefined);
12
+ else
13
+ reject(request.error); };
14
+ request.onsuccess = () => resolve(request.result);
15
+ });
16
+ if (!existing)
17
+ return;
18
+ const prepared = existing.objectStoreNames.contains("readingProfiles") && existing.objectStoreNames.contains("readingMessages");
19
+ const version = existing.version;
20
+ existing.close();
21
+ if (version >= 60 && prepared)
22
+ return;
23
+ await new Promise((resolve, reject) => {
24
+ const request = factory.open(name, Math.max(60, version + 1));
25
+ request.onerror = () => reject(request.error);
26
+ request.onupgradeneeded = () => {
27
+ const db = request.result, tx = request.transaction;
28
+ for (const [before, after] of [["profiles", "readingProfiles"], ["versions", "readingVersions"], ["readThreads", "readingThreads"], ["readMessages", "readingMessages"], ["readPositions", "readingPositions"]]) {
29
+ if (db.objectStoreNames.contains(before)) {
30
+ if (db.objectStoreNames.contains(after)) {
31
+ tx.abort();
32
+ return;
33
+ }
34
+ tx.objectStore(before).name = after;
35
+ }
36
+ }
37
+ if (!db.objectStoreNames.contains("readingProfiles"))
38
+ db.createObjectStore("readingProfiles");
39
+ if (!db.objectStoreNames.contains("readingVersions"))
40
+ db.createObjectStore("readingVersions");
41
+ // Explicit-key writes from older SDKs must fail rather than recreate a cache
42
+ // beside a partially applied transition or silently return an empty history.
43
+ db.createObjectStore("profiles", { keyPath: "unsupportedReadingFormat" });
44
+ db.createObjectStore("versions", { keyPath: "unsupportedReadingFormat" });
45
+ const define = (name, keyPath, indexes) => {
46
+ const store = db.objectStoreNames.contains(name) ? tx.objectStore(name) : db.createObjectStore(name, { keyPath });
47
+ for (const [index, path] of Object.entries(indexes))
48
+ if (!store.indexNames.contains(index))
49
+ store.createIndex(index, path);
50
+ };
51
+ define("readingThreads", ["owner", "id"], { owner: "owner", "[owner+id+value.kind+value.teamId]": ["owner", "id", "value.kind", "value.teamId"], "[owner+value.conversationId+value.kind+value.teamId+id]": ["owner", "value.conversationId", "value.kind", "value.teamId", "id"], "[owner+id+value.conversationId]": ["owner", "id", "value.conversationId"], "[owner+id+value.kind]": ["owner", "id", "value.kind"], "[owner+value.conversationId+value.kind+id]": ["owner", "value.conversationId", "value.kind", "id"] });
52
+ define("readingMessages", ["owner", "thread", "id"], { owner: "owner", "[owner+thread]": ["owner", "thread"] });
53
+ define("readingPositions", ["owner", "id"], { owner: "owner" });
54
+ define("readBatches", ["owner", "key"], { owner: "owner" });
55
+ };
56
+ request.onsuccess = () => { request.result.close(); resolve(); };
57
+ });
58
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pmate/chat-sdk",
3
- "version": "0.12.0-next.57",
3
+ "version": "0.12.0-next.59",
4
4
  "description": "Cross-platform PMate chat input components, voice input primitives, and ASR helpers",
5
5
  "type": "module",
6
6
  "main": "./lib/index.js",