@agentshouse/mdruntime 0.1.0-c2c52158b7381 → 0.1.0-c2e8a6f99866d

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.
Files changed (97) hide show
  1. package/AGENTS.md +14 -10
  2. package/dist/adapter.d.ts +14 -5
  3. package/dist/adapter.d.ts.map +1 -1
  4. package/dist/bootstrap.d.ts +13 -6
  5. package/dist/bootstrap.d.ts.map +1 -1
  6. package/dist/bootstrap.js +86 -88
  7. package/dist/chain.d.ts +11 -0
  8. package/dist/chain.d.ts.map +1 -0
  9. package/dist/chain.js +64 -0
  10. package/dist/change-set.d.ts +5 -9
  11. package/dist/change-set.d.ts.map +1 -1
  12. package/dist/change-set.js +10 -86
  13. package/dist/chat/list.d.ts.map +1 -1
  14. package/dist/chat/list.js +1 -1
  15. package/dist/chat/message.d.ts.map +1 -1
  16. package/dist/chat/message.js +8 -1
  17. package/dist/chat/messages.d.ts +1 -0
  18. package/dist/chat/messages.d.ts.map +1 -1
  19. package/dist/chat/messages.js +9 -1
  20. package/dist/chat/provider.d.ts +2 -0
  21. package/dist/chat/provider.d.ts.map +1 -1
  22. package/dist/chat/provider.js +17 -1
  23. package/dist/client.d.ts +1 -0
  24. package/dist/client.d.ts.map +1 -1
  25. package/dist/client.js +1 -0
  26. package/dist/collection.d.ts +5 -19
  27. package/dist/collection.d.ts.map +1 -1
  28. package/dist/collection.js +22 -360
  29. package/dist/compile.d.ts +11 -0
  30. package/dist/compile.d.ts.map +1 -0
  31. package/dist/compile.js +712 -0
  32. package/dist/connection.d.ts +2 -2
  33. package/dist/connection.d.ts.map +1 -1
  34. package/dist/connection.js +1396 -624
  35. package/dist/conversation.d.ts +1 -1
  36. package/dist/conversation.d.ts.map +1 -1
  37. package/dist/conversation.js +26 -17
  38. package/dist/entry.d.ts +2 -1
  39. package/dist/entry.d.ts.map +1 -1
  40. package/dist/entry.js +17 -23
  41. package/dist/index.d.ts +1 -1
  42. package/dist/index.d.ts.map +1 -1
  43. package/dist/live.d.ts +22 -0
  44. package/dist/live.d.ts.map +1 -0
  45. package/dist/live.js +123 -0
  46. package/dist/memory.d.ts +1 -0
  47. package/dist/memory.d.ts.map +1 -1
  48. package/dist/memory.js +12 -0
  49. package/dist/observation.d.ts +12 -0
  50. package/dist/observation.d.ts.map +1 -1
  51. package/dist/observation.js +84 -0
  52. package/dist/overlay.d.ts +0 -3
  53. package/dist/overlay.d.ts.map +1 -1
  54. package/dist/overlay.js +1 -151
  55. package/dist/reach.d.ts.map +1 -1
  56. package/dist/reach.js +15 -35
  57. package/dist/reads.js +1 -1
  58. package/dist/refresh.d.ts +10 -13
  59. package/dist/refresh.d.ts.map +1 -1
  60. package/dist/refresh.js +74 -70
  61. package/dist/replica.d.ts +55 -38
  62. package/dist/replica.d.ts.map +1 -1
  63. package/dist/replica.js +273 -214
  64. package/dist/rows.d.ts +29 -31
  65. package/dist/rows.d.ts.map +1 -1
  66. package/dist/rows.js +113 -69
  67. package/dist/save.d.ts +9 -10
  68. package/dist/save.d.ts.map +1 -1
  69. package/dist/save.js +59 -57
  70. package/dist/search.d.ts +2 -2
  71. package/dist/search.d.ts.map +1 -1
  72. package/dist/search.js +60 -28
  73. package/dist/shared.d.ts +0 -3
  74. package/dist/shared.d.ts.map +1 -1
  75. package/dist/shared.js +1 -31
  76. package/dist/store.d.ts +79 -62
  77. package/dist/store.d.ts.map +1 -1
  78. package/dist/store.js +164 -330
  79. package/dist/stored.d.ts +143 -0
  80. package/dist/stored.d.ts.map +1 -0
  81. package/dist/stored.js +561 -0
  82. package/dist/tables.d.ts +246 -168
  83. package/dist/tables.d.ts.map +1 -1
  84. package/dist/tables.js +156 -25
  85. package/dist/turn.d.ts +1 -1
  86. package/dist/turn.d.ts.map +1 -1
  87. package/dist/turn.js +28 -20
  88. package/dist/validation.d.ts +30 -0
  89. package/dist/validation.d.ts.map +1 -0
  90. package/dist/validation.js +162 -0
  91. package/dist/windows.d.ts +2 -1
  92. package/dist/windows.d.ts.map +1 -1
  93. package/dist/windows.js +30 -25
  94. package/package.json +2 -3
  95. package/dist/read-side.d.ts +0 -27
  96. package/dist/read-side.d.ts.map +0 -1
  97. package/dist/read-side.js +0 -1179
package/dist/store.js CHANGED
@@ -1,34 +1,11 @@
1
- import { sameBinary, sameChange } from './overlay.js';
2
- import { differences, keysIn } from './shared.js';
3
- import { inUnits, ROW_WEIGHT, stepped } from './turn.js';
4
1
  import { precedes } from './windows.js';
5
2
  import { binaryLocalId } from './identity.js';
6
3
  const DATABASE = 'mdruntime';
7
- const ROOMS = 'rooms';
8
- const DOCUMENTS = 'documents';
9
- const BINARY_FILES = 'binaryFiles';
10
- const TRANSFERS = 'transfers';
11
- const RECEIVED = 'received';
12
- const RECEIVED_PARTS = 'receivedParts';
13
- const DECLARATIONS = 'declarations';
14
- const PRESENTATION = 'presentation';
15
4
  const CONVERSATIONS = 'conversations';
16
5
  const MESSAGES = 'messages';
17
- const CURRENT = 'current';
18
- const MEMBER = 'member';
19
- const TARGETS = 'targets';
20
6
  const FILE = '\u0000file';
21
- export const STORE_SCHEMA = 25;
22
- const REPLICA_SHAPE = 23;
23
- const REPLICA_STORES = [
24
- [ROOMS, { keyPath: 'locator' }],
25
- [DOCUMENTS, { keyPath: ['locator', 'transfer', 'path'] }],
26
- [BINARY_FILES, { keyPath: ['locator', 'transfer', 'path'] }],
27
- [TRANSFERS, { keyPath: 'locator' }],
28
- [RECEIVED, { keyPath: 'key' }],
29
- [RECEIVED_PARTS, { keyPath: ['key', 'index'] }],
30
- [DECLARATIONS, undefined],
31
- [PRESENTATION, { keyPath: 'app' }],
7
+ export const STORE_SCHEMA = 26;
8
+ const STORES = [
32
9
  [CONVERSATIONS, { keyPath: 'conversation' }],
33
10
  [MESSAGES, { keyPath: ['conversation', 'identity'] }],
34
11
  ];
@@ -92,136 +69,9 @@ export function transacted(database, stores, mode, work) {
92
69
  return Promise.all([value, committed]).then(([held]) => held);
93
70
  });
94
71
  }
95
- const EMPTY_DECLARATIONS = {
96
- lastCompleted: null,
97
- providerLimits: [],
98
- capabilities: [],
99
- };
100
- function declaration(database) {
101
- return transacted(database, DECLARATIONS, 'readonly', (transaction) => settled(transaction.objectStore(DECLARATIONS).get(CURRENT))).then((record) => record ?? EMPTY_DECLARATIONS);
102
- }
103
- function updateDeclaration(database, update) {
104
- return transacted(database, DECLARATIONS, 'readwrite', (transaction) => {
105
- const store = transaction.objectStore(DECLARATIONS);
106
- const prior = store.get(CURRENT);
107
- prior.onsuccess = () => {
108
- store.put(update(prior.result ?? EMPTY_DECLARATIONS), CURRENT);
109
- };
110
- });
111
- }
112
72
  function accepted({ conversation: _held, ...message }) {
113
73
  return message;
114
74
  }
115
- function roomRecord(record) {
116
- const { documents, binaries, revisions, fileIds, changes, written, ...room } = record;
117
- return room;
118
- }
119
- function underTransfer(locator, transfer, after) {
120
- return after === undefined
121
- ? IDBKeyRange.bound([locator, transfer], [locator, transfer, []])
122
- : IDBKeyRange.bound([locator, transfer, after], [locator, transfer, []], true);
123
- }
124
- function underRoom(locator) {
125
- return IDBKeyRange.bound([locator], [locator, []]);
126
- }
127
- async function readTransfer(store, locator, transfer, receive) {
128
- let range = underTransfer(locator, transfer);
129
- for (;;) {
130
- const batch = await settled(store.getAll(range, READ_BATCH));
131
- for (const record of batch)
132
- receive(record);
133
- if (batch.length < READ_BATCH)
134
- return;
135
- range = underTransfer(locator, transfer, batch[batch.length - 1].path);
136
- }
137
- }
138
- function emptyLayer() {
139
- return { documents: {}, binaries: {}, revisions: {}, fileIds: {}, changes: {}, written: {} };
140
- }
141
- async function readLayer(transaction, locator, transfer) {
142
- const layer = emptyLayer();
143
- const head = (held) => {
144
- if (held.revision !== null)
145
- layer.revisions[held.path] = held.revision;
146
- if (held.fileId !== null)
147
- layer.fileIds[held.path] = held.fileId;
148
- if (held.change !== null)
149
- layer.changes[held.path] = held.change;
150
- };
151
- await readTransfer(transaction.objectStore(DOCUMENTS), locator, transfer, (document) => {
152
- layer.documents[document.path] = document.content;
153
- if (document.written !== null)
154
- layer.written[document.path] = document.written;
155
- head(document);
156
- });
157
- await readTransfer(transaction.objectStore(BINARY_FILES), locator, transfer, (file) => {
158
- layer.binaries[file.path] = file.binary;
159
- head(file);
160
- });
161
- return layer;
162
- }
163
- function documentRecord(locator, transfer, layer, path) {
164
- return {
165
- locator,
166
- transfer,
167
- path,
168
- content: layer.documents[path],
169
- revision: layer.revisions[path] ?? null,
170
- fileId: layer.fileIds[path] ?? null,
171
- change: layer.changes[path] ?? null,
172
- written: layer.written[path] ?? null,
173
- };
174
- }
175
- function binaryFileRecord(locator, transfer, layer, path) {
176
- return {
177
- locator,
178
- transfer,
179
- path,
180
- revision: layer.revisions[path] ?? null,
181
- fileId: layer.fileIds[path] ?? null,
182
- change: layer.changes[path] ?? null,
183
- binary: layer.binaries[path],
184
- };
185
- }
186
- function* changedIn(next, base) {
187
- if (base === null)
188
- return yield* keysIn(next.documents, next.binaries);
189
- return new Set([
190
- ...(yield* differences(base.documents, next.documents)),
191
- ...(yield* differences(base.revisions, next.revisions)),
192
- ...(yield* differences(base.fileIds, next.fileIds)),
193
- ...(yield* differences(base.written, next.written)),
194
- ...(yield* differences(base.changes, next.changes, sameChange)),
195
- ...(yield* differences(base.binaries, next.binaries, sameBinary)),
196
- ]);
197
- }
198
- function writeLayer(transaction, locator, transfer, layer) {
199
- const { next, base, paths } = layer;
200
- const documents = transaction.objectStore(DOCUMENTS);
201
- const files = transaction.objectStore(BINARY_FILES);
202
- let last = null;
203
- return inUnits(paths, (path) => {
204
- if (next.documents[path] !== undefined) {
205
- last = documents.put(documentRecord(locator, transfer, next, path));
206
- }
207
- else if (base?.documents[path] !== undefined) {
208
- last = documents.delete([locator, transfer, path]);
209
- }
210
- if (next.binaries[path] !== undefined) {
211
- last = files.put(binaryFileRecord(locator, transfer, next, path));
212
- }
213
- else if (base?.binaries[path] !== undefined) {
214
- last = files.delete([locator, transfer, path]);
215
- }
216
- return ROW_WEIGHT + (next.documents[path]?.length ?? 0);
217
- }, () => { }, () => {
218
- const request = last;
219
- last = null;
220
- return request === null
221
- ? Promise.resolve()
222
- : new Promise((resolve) => request.addEventListener('success', () => resolve()));
223
- });
224
- }
225
75
  function removeDatabase(name) {
226
76
  return new Promise((resolve, reject) => {
227
77
  const removal = indexedDB.deleteDatabase(name);
@@ -249,14 +99,14 @@ export function openDatabase(name, version, upgrade) {
249
99
  };
250
100
  });
251
101
  }
252
- function openReplica() {
102
+ function openConversations() {
253
103
  return openDatabase(DATABASE, STORE_SCHEMA, (opening, oldVersion) => {
254
104
  const database = opening.result;
255
- if (oldVersion < REPLICA_SHAPE) {
105
+ if (oldVersion < STORE_SCHEMA) {
256
106
  for (const name of [...database.objectStoreNames])
257
107
  database.deleteObjectStore(name);
258
108
  }
259
- for (const [name, parameters] of REPLICA_STORES) {
109
+ for (const [name, parameters] of STORES) {
260
110
  if (database.objectStoreNames.contains(name))
261
111
  continue;
262
112
  database.createObjectStore(name, parameters);
@@ -277,7 +127,7 @@ function draftFiles(drafts) {
277
127
  function roomDraftRow(record) {
278
128
  return { path: record.path, record: JSON.stringify(record), files: draftFiles([record]) };
279
129
  }
280
- function authoredRows({ locator, queue, founded, saved, roomDrafts, consumedRoomDrafts }, held) {
130
+ function authoredRows({ locator, queue, founded, saved, roomDrafts, consumedRoomDrafts, accepted }, held) {
281
131
  return {
282
132
  locator,
283
133
  ...(queue === undefined
@@ -311,6 +161,14 @@ function authoredRows({ locator, queue, founded, saved, roomDrafts, consumedRoom
311
161
  }),
312
162
  ...(roomDrafts === undefined ? {} : { roomDrafts: roomDrafts.map(roomDraftRow) }),
313
163
  ...(consumedRoomDrafts === undefined ? {} : { consumedRoomDrafts }),
164
+ ...(accepted === undefined
165
+ ? {}
166
+ : {
167
+ amendments: accepted.map(({ id, roomVersion }) => ({
168
+ id,
169
+ amendment: JSON.stringify({ accepted: roomVersion }),
170
+ })),
171
+ }),
314
172
  };
315
173
  }
316
174
  function marked(value) {
@@ -345,8 +203,11 @@ async function filed(value, read) {
345
203
  restored[name] = await filed(item, read);
346
204
  return restored;
347
205
  }
206
+ function json(value, fallback) {
207
+ return value === undefined ? fallback : JSON.parse(value);
208
+ }
348
209
  export async function openRuntimeStore(fenced, authored) {
349
- const database = await openReplica();
210
+ const database = await openConversations();
350
211
  const keys = new WeakMap();
351
212
  const read = async (key, type) => {
352
213
  const blob = await authored.ask({ kind: 'authored.file', key, type });
@@ -366,6 +227,8 @@ export async function openRuntimeStore(fenced, authored) {
366
227
  };
367
228
  const fact = (name) => authored.ask({ kind: 'authored.fact', name });
368
229
  const queued = new Map();
230
+ const changed = new Map();
231
+ const change = (key) => changed.set(key, (changed.get(key) ?? 0) + 1);
369
232
  const rows = (writes) => writes.map((write) => authoredRows(write, queued.get(write.locator)));
370
233
  const noted = (writes) => {
371
234
  for (const { locator, queue } of writes) {
@@ -374,107 +237,144 @@ export async function openRuntimeStore(fenced, authored) {
374
237
  }
375
238
  };
376
239
  return {
377
- all() {
378
- return transacted(database, [ROOMS, DOCUMENTS, BINARY_FILES], 'readonly', async (transaction) => {
379
- const rooms = await settled(transaction.objectStore(ROOMS).getAll());
380
- const records = [];
381
- for (const room of rooms.filter((held) => !fenced(held.locator))) {
382
- records.push({
383
- ...room,
384
- ...(await readLayer(transaction, room.locator, room.transfer)),
385
- });
386
- }
387
- return records;
388
- });
240
+ async opened() {
241
+ const held = await authored.ask({ kind: 'replica.open' });
242
+ const unprepared = new Set(held.keys.flatMap(({ key, rederiving }) => (rederiving === null ? [] : [key])));
243
+ const overlaid = new Map();
244
+ for (const { key, path } of held.overlaid) {
245
+ const paths = overlaid.get(key) ?? new Set();
246
+ paths.add(path);
247
+ overlaid.set(key, paths);
248
+ }
249
+ return {
250
+ rooms: new Map(held.rooms
251
+ .filter(({ locator }) => !fenced(locator))
252
+ .map(({ locator, key, record }) => [
253
+ locator,
254
+ { ...JSON.parse(record), key },
255
+ ])),
256
+ transfers: new Map(held.transfers
257
+ .filter(({ locator }) => !fenced(locator))
258
+ .map(({ locator, key, record }) => [
259
+ locator,
260
+ { ...JSON.parse(record), key },
261
+ ])),
262
+ unprepared,
263
+ overlaid,
264
+ erasing: held.erasing.length > 0,
265
+ orphans: held.orphans,
266
+ lastCompleted: json(held.facts.lastCompleted, null),
267
+ providerLimits: json(held.facts.providerLimits, []),
268
+ capabilities: json(held.facts.capabilities, []),
269
+ member: json(held.facts.member, null),
270
+ targets: json(held.facts.targets, {}),
271
+ presentation: held.presentation.map(({ app, value }) => ({
272
+ app,
273
+ value: JSON.parse(value),
274
+ })),
275
+ };
389
276
  },
390
- async keepAuthored(...writes) {
391
- const kept = writes.filter(({ locator }) => !fenced(locator));
392
- if (kept.length === 0)
393
- return;
394
- await authored.ask({ kind: 'authored.keep', writes: rows(kept), files: [], whole: false });
395
- noted(kept);
396
- },
397
- async saveAuthored(files, ...writes) {
398
- const refused = writes.find(({ locator }) => fenced(locator));
399
- if (refused !== undefined)
400
- return refused.locator;
401
- const fencedThere = await authored.ask({
402
- kind: 'authored.keep',
403
- writes: rows(writes),
404
- files,
405
- whole: true,
406
- });
407
- if (fencedThere === null)
408
- noted(writes);
409
- return fencedThere;
277
+ counts(rooms) {
278
+ return authored.ask({ kind: 'replica.counts', rooms });
410
279
  },
411
- async write({ next, previous, stored }) {
412
- if (next === previous || fenced(next.locator))
413
- return;
414
- const moved = previous?.transfer !== next.transfer;
415
- const base = moved ? (stored ?? null) : previous;
416
- const paths = await stepped(changedIn(next, base));
417
- if (fenced(next.locator))
418
- return;
419
- await transacted(database, [ROOMS, DOCUMENTS, BINARY_FILES, TRANSFERS], 'readwrite', (transaction) => {
420
- transaction.objectStore(ROOMS).put(roomRecord(next));
421
- if (moved && previous !== null) {
422
- const range = underTransfer(previous.locator, previous.transfer);
423
- transaction.objectStore(DOCUMENTS).delete(range);
424
- transaction.objectStore(BINARY_FILES).delete(range);
425
- }
426
- if (moved)
427
- transaction.objectStore(TRANSFERS).delete(next.locator);
428
- return writeLayer(transaction, next.locator, next.transfer, { next, base, paths });
429
- });
280
+ query(plan, rooms, budget) {
281
+ return authored.ask({ kind: 'replica.query', plan, rooms, budget });
430
282
  },
431
- async revokeModels(records) {
432
- await transacted(database, ROOMS, 'readwrite', (transaction) => {
433
- const rooms = transaction.objectStore(ROOMS);
434
- const revoked = { status: 'unavailable', reason: 'revoked' };
435
- for (const record of records) {
436
- if (fenced(record.locator))
437
- continue;
438
- rooms.put(roomRecord({ ...record, model: revoked, selectionModel: revoked }));
439
- }
440
- });
283
+ changes(keys) {
284
+ return keys.reduce((total, key) => total + (changed.get(key) ?? 0), 0);
441
285
  },
442
- async lastCompleted() {
443
- return (await declaration(database)).lastCompleted;
286
+ entries(key, view, paths) {
287
+ return authored.ask({ kind: 'replica.entries', key, view, paths });
444
288
  },
445
- complete(at) {
446
- return updateDeclaration(database, (record) => ({ ...record, lastCompleted: at }));
289
+ typed(key, view, paths) {
290
+ return authored.ask({ kind: 'replica.typed', key, view, paths });
447
291
  },
448
- async providerLimits() {
449
- return (await declaration(database)).providerLimits;
292
+ referrers(key, view, targets, kinds) {
293
+ return authored.ask({ kind: 'replica.referrers', key, view, targets, kinds });
450
294
  },
451
- async capabilities() {
452
- return (await declaration(database)).capabilities;
295
+ walk(key, view, after, bytes) {
296
+ return authored.ask({ kind: 'replica.walk', key, view, after, bytes });
453
297
  },
454
- declare(limits, capabilities) {
455
- return updateDeclaration(database, (record) => ({
456
- ...record,
457
- providerLimits: limits,
458
- capabilities,
459
- }));
298
+ files(key, view, after, limit) {
299
+ return authored.ask({ kind: 'replica.files', key, view, after, limit });
460
300
  },
461
- async member() {
462
- const held = await transacted(database, DECLARATIONS, 'readonly', (transaction) => settled(transaction.objectStore(DECLARATIONS).get(MEMBER)));
463
- return held ?? null;
301
+ typedWithin(key, view, roots, within) {
302
+ return authored.ask({ kind: 'replica.typedWithin', key, view, roots, within });
303
+ },
304
+ searched(key, locator, after, limit, paths) {
305
+ return authored.ask({ kind: 'replica.searched', key, locator, after, limit, paths });
306
+ },
307
+ async stage(record, composition, page, key, last) {
308
+ if (fenced(record.locator))
309
+ return null;
310
+ const { key: _key, ...kept } = record;
311
+ return authored.ask({
312
+ kind: 'replica.stage',
313
+ locator: record.locator,
314
+ transfer: record.transfer,
315
+ record: last ? JSON.stringify(kept) : null,
316
+ key,
317
+ composition,
318
+ entries: page.entries,
319
+ folders: page.folders,
320
+ });
464
321
  },
465
- async keepMember(member) {
466
- await transacted(database, DECLARATIONS, 'readwrite', (transaction) => {
467
- transaction.objectStore(DECLARATIONS).put(member, MEMBER);
322
+ async refresh(key, page) {
323
+ await authored.ask({
324
+ kind: 'replica.refresh',
325
+ key,
326
+ entries: page.entries,
327
+ removals: page.removals,
328
+ folders: page.folders,
329
+ folderRemovals: page.folderRemovals,
468
330
  });
469
331
  },
470
- async targets() {
471
- const held = await transacted(database, DECLARATIONS, 'readonly', (transaction) => settled(transaction.objectStore(DECLARATIONS).get(TARGETS)));
472
- return held ?? {};
332
+ async unrefresh(key) {
333
+ await authored.ask({ kind: 'replica.unrefresh', key });
473
334
  },
474
- async keepTargets(targets) {
475
- await transacted(database, DECLARATIONS, 'readwrite', (transaction) => {
476
- transaction.objectStore(DECLARATIONS).put(targets, TARGETS);
335
+ async commit({ rooms, writes = [], files = [], whole = false, discards = [] }) {
336
+ const kept = whole ? writes : writes.filter(({ locator }) => !fenced(locator));
337
+ const refused = whole ? kept.find(({ locator }) => fenced(locator)) : undefined;
338
+ if (refused !== undefined) {
339
+ return { fenced: refused.locator, counts: {}, changed: {}, prepared: {} };
340
+ }
341
+ for (const room of rooms)
342
+ change(room.key);
343
+ const committed = await authored.ask({
344
+ kind: 'replica.commit',
345
+ ...(kept.length === 0 && files.length === 0 && discards.length === 0
346
+ ? {}
347
+ : { authored: { writes: rows(kept), files, whole, discards } }),
348
+ rooms: rooms.filter(({ locator }) => !fenced(locator)),
477
349
  });
350
+ if (committed.fenced === null || !whole)
351
+ noted(kept);
352
+ for (const locator of discards)
353
+ queued.delete(locator);
354
+ return committed;
355
+ },
356
+ async fence(locators) {
357
+ await authored.ask({ kind: 'replica.fence', locators });
358
+ for (const locator of locators)
359
+ queued.delete(locator);
360
+ },
361
+ async erase(locator, limit) {
362
+ return (await authored.ask({ kind: 'replica.erase', locator, limit })).remaining;
363
+ },
364
+ async abandon(locator, kept = false) {
365
+ if (fenced(locator))
366
+ return null;
367
+ return authored.ask({ kind: 'replica.abandon', locator, kept });
368
+ },
369
+ async release(key) {
370
+ await authored.ask({ kind: 'replica.release', key });
371
+ },
372
+ async rederive(key, limit) {
373
+ change(key);
374
+ return (await authored.ask({ kind: 'replica.rederive', key, limit })).done;
375
+ },
376
+ async fact(name, value) {
377
+ await authored.ask({ kind: 'replica.fact', name, value: JSON.stringify(value) });
478
378
  },
479
379
  async queues() {
480
380
  const { queues, pending } = await authored.ask({ kind: 'authored.queues' });
@@ -500,9 +400,6 @@ export async function openRuntimeStore(fenced, authored) {
500
400
  submit(locator, id, submission, lost) {
501
401
  return amend(locator, id, { submission, lost });
502
402
  },
503
- accept(locator, id, roomVersion) {
504
- return amend(locator, id, { accepted: roomVersion });
505
- },
506
403
  keepUpload(locator, id, upload) {
507
404
  return amend(locator, id, { upload });
508
405
  },
@@ -512,12 +409,11 @@ export async function openRuntimeStore(fenced, authored) {
512
409
  .map((record) => JSON.parse(record))
513
410
  .filter((record) => !fenced(record.locator));
514
411
  },
515
- presentation() {
516
- return transacted(database, PRESENTATION, 'readonly', (transaction) => settled(transaction.objectStore(PRESENTATION).getAll()));
517
- },
518
412
  async present(record) {
519
- await transacted(database, PRESENTATION, 'readwrite', (transaction) => {
520
- transaction.objectStore(PRESENTATION).put(record);
413
+ await authored.ask({
414
+ kind: 'replica.present',
415
+ app: record.app,
416
+ value: JSON.stringify(record.value ?? null),
521
417
  });
522
418
  },
523
419
  conversations() {
@@ -605,8 +501,8 @@ export async function openRuntimeStore(fenced, authored) {
605
501
  return held.map(accepted);
606
502
  },
607
503
  async messageIdentities(conversation) {
608
- const keys = await transacted(database, MESSAGES, 'readonly', (transaction) => settled(transaction.objectStore(MESSAGES).index('conversation').getAllKeys(conversation)));
609
- return keys.map((key) => key[1]);
504
+ const found = await transacted(database, MESSAGES, 'readonly', (transaction) => settled(transaction.objectStore(MESSAGES).index('conversation').getAllKeys(conversation)));
505
+ return found.map((key) => key[1]);
610
506
  },
611
507
  async turnArrived(conversation, turn) {
612
508
  const held = await transacted(database, MESSAGES, 'readonly', (transaction) => settled(transaction.objectStore(MESSAGES).index('turn').count([conversation, turn])));
@@ -618,9 +514,9 @@ export async function openRuntimeStore(fenced, authored) {
618
514
  const messages = transaction.objectStore(MESSAGES);
619
515
  for (const conversation of conversations) {
620
516
  transaction.objectStore(CONVERSATIONS).delete(conversation);
621
- const keys = messages.index('conversation').getAllKeys(conversation);
622
- keys.onsuccess = () => {
623
- for (const key of keys.result)
517
+ const held = messages.index('conversation').getAllKeys(conversation);
518
+ held.onsuccess = () => {
519
+ for (const key of held.result)
624
520
  messages.delete(key);
625
521
  };
626
522
  }
@@ -678,86 +574,24 @@ export async function openRuntimeStore(fenced, authored) {
678
574
  fences() {
679
575
  return authored.ask({ kind: 'authored.fences' });
680
576
  },
681
- async erase(locators) {
682
- await authored.ask({ kind: 'authored.fence', locators });
683
- for (const locator of locators)
684
- queued.delete(locator);
685
- await transacted(database, [ROOMS, DOCUMENTS, BINARY_FILES, TRANSFERS], 'readwrite', (transaction) => {
686
- for (const locator of locators) {
687
- transaction.objectStore(ROOMS).delete(locator);
688
- transaction.objectStore(TRANSFERS).delete(locator);
689
- transaction.objectStore(DOCUMENTS).delete(underRoom(locator));
690
- transaction.objectStore(BINARY_FILES).delete(underRoom(locator));
691
- }
692
- });
693
- await authored.ask({ kind: 'authored.unfence', locators });
694
- },
695
- async discard(locator) {
696
- await authored.ask({ kind: 'authored.discard', locator });
697
- queued.delete(locator);
698
- },
699
- async transfers() {
700
- const held = await transacted(database, TRANSFERS, 'readonly', (transaction) => settled(transaction.objectStore(TRANSFERS).getAll()));
701
- return held.filter((record) => !fenced(record.locator));
702
- },
703
- staged(record) {
704
- return transacted(database, [DOCUMENTS, BINARY_FILES], 'readonly', (transaction) => readLayer(transaction, record.locator, record.transfer));
705
- },
706
- async stage(record, page) {
707
- if (fenced(record.locator))
708
- return;
709
- const paths = await stepped(changedIn(page, null));
710
- if (fenced(record.locator))
711
- return;
712
- await transacted(database, [TRANSFERS, DOCUMENTS, BINARY_FILES], 'readwrite', (transaction) => {
713
- transaction.objectStore(TRANSFERS).put(record);
714
- return writeLayer(transaction, record.locator, record.transfer, {
715
- next: page,
716
- base: null,
717
- paths,
718
- });
719
- });
720
- },
721
- async abandon(locator) {
722
- if (fenced(locator))
723
- return;
724
- await transacted(database, [TRANSFERS, DOCUMENTS, BINARY_FILES], 'readwrite', (transaction) => {
725
- const transfers = transaction.objectStore(TRANSFERS);
726
- const held = transfers.get(locator);
727
- held.onsuccess = () => {
728
- const record = held.result;
729
- if (record === undefined)
730
- return;
731
- transfers.delete(locator);
732
- const range = underTransfer(locator, record.transfer);
733
- transaction.objectStore(DOCUMENTS).delete(range);
734
- transaction.objectStore(BINARY_FILES).delete(range);
735
- };
736
- });
737
- },
738
577
  receivedHeads() {
739
- return transacted(database, RECEIVED, 'readonly', (transaction) => settled(transaction.objectStore(RECEIVED).getAll()));
578
+ return authored.ask({ kind: 'replica.received' });
740
579
  },
741
- async receivedParts(key, count) {
742
- const kept = await transacted(database, RECEIVED_PARTS, 'readonly', (transaction) => settled(transaction
743
- .objectStore(RECEIVED_PARTS)
744
- .getAll(IDBKeyRange.bound([key, 0], [key, count], false, true))));
745
- return kept.map((part) => part.value);
580
+ receivedParts(key, count) {
581
+ return authored.ask({ kind: 'replica.receivedParts', key, count });
746
582
  },
747
583
  async keepReceived(key, head, part) {
748
- await transacted(database, [RECEIVED, RECEIVED_PARTS], 'readwrite', (transaction) => {
749
- transaction.objectStore(RECEIVED).put({ key, head });
750
- if (part !== undefined)
751
- transaction.objectStore(RECEIVED_PARTS).put({ key, ...part });
584
+ await authored.ask({
585
+ kind: 'replica.receive',
586
+ key,
587
+ head: JSON.stringify(head),
588
+ ...(part === undefined
589
+ ? {}
590
+ : { part: { index: part.index, value: JSON.stringify(part.value) } }),
752
591
  });
753
592
  },
754
593
  async dropReceived(keys) {
755
- await transacted(database, [RECEIVED, RECEIVED_PARTS], 'readwrite', (transaction) => {
756
- for (const key of keys) {
757
- transaction.objectStore(RECEIVED).delete(key);
758
- transaction.objectStore(RECEIVED_PARTS).delete(IDBKeyRange.bound([key], [key, []]));
759
- }
760
- });
594
+ await authored.ask({ kind: 'replica.unreceive', keys });
761
595
  },
762
596
  generation() {
763
597
  return fact('generation');