@agentshouse/mdruntime 0.1.0-cbb8d8acc9d19 → 0.1.0-cc56db96f86ae

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 (223) hide show
  1. package/AGENTS.md +285 -225
  2. package/README.md +21 -10
  3. package/dist/adapter.d.ts +114 -26
  4. package/dist/adapter.d.ts.map +1 -1
  5. package/dist/attempt.d.ts +16 -0
  6. package/dist/attempt.d.ts.map +1 -0
  7. package/dist/attempt.js +70 -0
  8. package/dist/binary.d.ts +23 -0
  9. package/dist/binary.d.ts.map +1 -0
  10. package/dist/binary.js +8 -0
  11. package/dist/bootstrap.d.ts +18 -5
  12. package/dist/bootstrap.d.ts.map +1 -1
  13. package/dist/bootstrap.js +147 -40
  14. package/dist/change-set.d.ts +7 -2
  15. package/dist/change-set.d.ts.map +1 -1
  16. package/dist/change-set.js +50 -16
  17. package/dist/change.d.ts +45 -0
  18. package/dist/change.d.ts.map +1 -1
  19. package/dist/change.js +34 -0
  20. package/dist/chat/acts.d.ts +33 -0
  21. package/dist/chat/acts.d.ts.map +1 -0
  22. package/dist/chat/acts.js +36 -0
  23. package/dist/chat/context.d.ts +3 -0
  24. package/dist/chat/context.d.ts.map +1 -0
  25. package/dist/chat/context.js +10 -0
  26. package/dist/chat/earlier.d.ts +10 -0
  27. package/dist/chat/earlier.d.ts.map +1 -0
  28. package/dist/chat/earlier.js +54 -0
  29. package/dist/chat/house.d.ts +25 -0
  30. package/dist/chat/house.d.ts.map +1 -0
  31. package/dist/chat/house.js +38 -0
  32. package/dist/chat/input.d.ts +11 -0
  33. package/dist/chat/input.d.ts.map +1 -0
  34. package/dist/chat/input.js +92 -0
  35. package/dist/chat/kit.d.ts +61 -0
  36. package/dist/chat/kit.d.ts.map +1 -0
  37. package/dist/chat/kit.js +123 -0
  38. package/dist/chat/lasting.d.ts +6 -0
  39. package/dist/chat/lasting.d.ts.map +1 -0
  40. package/dist/chat/lasting.js +19 -0
  41. package/dist/chat/list.d.ts +28 -0
  42. package/dist/chat/list.d.ts.map +1 -0
  43. package/dist/chat/list.js +103 -0
  44. package/dist/chat/markdown.d.ts +21 -0
  45. package/dist/chat/markdown.d.ts.map +1 -0
  46. package/dist/chat/markdown.js +246 -0
  47. package/dist/chat/message.d.ts +7 -0
  48. package/dist/chat/message.d.ts.map +1 -0
  49. package/dist/chat/message.js +237 -0
  50. package/dist/chat/messages.d.ts +29 -0
  51. package/dist/chat/messages.d.ts.map +1 -0
  52. package/dist/chat/messages.js +190 -0
  53. package/dist/chat/name.d.ts +7 -0
  54. package/dist/chat/name.d.ts.map +1 -0
  55. package/dist/chat/name.js +37 -0
  56. package/dist/chat/opened.d.ts +12 -0
  57. package/dist/chat/opened.d.ts.map +1 -0
  58. package/dist/chat/opened.js +19 -0
  59. package/dist/chat/overlay.d.ts +32 -0
  60. package/dist/chat/overlay.d.ts.map +1 -0
  61. package/dist/chat/overlay.js +103 -0
  62. package/dist/chat/parts.d.ts +58 -0
  63. package/dist/chat/parts.d.ts.map +1 -0
  64. package/dist/chat/parts.js +120 -0
  65. package/dist/chat/pieces.d.ts +62 -0
  66. package/dist/chat/pieces.d.ts.map +1 -0
  67. package/dist/chat/pieces.js +1110 -0
  68. package/dist/chat/process.d.ts +20 -0
  69. package/dist/chat/process.d.ts.map +1 -0
  70. package/dist/chat/process.js +32 -0
  71. package/dist/chat/provider.d.ts +68 -0
  72. package/dist/chat/provider.d.ts.map +1 -0
  73. package/dist/chat/provider.js +619 -0
  74. package/dist/chat/question.d.ts +9 -0
  75. package/dist/chat/question.d.ts.map +1 -0
  76. package/dist/chat/question.js +69 -0
  77. package/dist/chat/questions.d.ts +31 -0
  78. package/dist/chat/questions.d.ts.map +1 -0
  79. package/dist/chat/questions.js +111 -0
  80. package/dist/chat/read.d.ts +5 -0
  81. package/dist/chat/read.d.ts.map +1 -0
  82. package/dist/chat/read.js +13 -0
  83. package/dist/chat/ready.d.ts +37 -0
  84. package/dist/chat/ready.d.ts.map +1 -0
  85. package/dist/chat/ready.js +93 -0
  86. package/dist/chat/room-file.d.ts +8 -0
  87. package/dist/chat/room-file.d.ts.map +1 -0
  88. package/dist/chat/room-file.js +67 -0
  89. package/dist/chat/steady.d.ts +3 -0
  90. package/dist/chat/steady.d.ts.map +1 -0
  91. package/dist/chat/steady.js +26 -0
  92. package/dist/chat/style.d.ts +4 -0
  93. package/dist/chat/style.d.ts.map +1 -0
  94. package/dist/chat/style.js +306 -0
  95. package/dist/chat.d.ts +15 -0
  96. package/dist/chat.d.ts.map +1 -0
  97. package/dist/chat.js +8 -0
  98. package/dist/client.d.ts +33 -13
  99. package/dist/client.d.ts.map +1 -1
  100. package/dist/client.js +81 -16
  101. package/dist/collection.d.ts +23 -13
  102. package/dist/collection.d.ts.map +1 -1
  103. package/dist/collection.js +367 -38
  104. package/dist/connection.d.ts +54 -16
  105. package/dist/connection.d.ts.map +1 -1
  106. package/dist/connection.js +2582 -486
  107. package/dist/conversation.d.ts +39 -12
  108. package/dist/conversation.d.ts.map +1 -1
  109. package/dist/conversation.js +546 -171
  110. package/dist/digest.d.ts +2 -0
  111. package/dist/digest.d.ts.map +1 -0
  112. package/dist/digest.js +100 -0
  113. package/dist/durability.d.ts +7 -1
  114. package/dist/durability.d.ts.map +1 -1
  115. package/dist/entry.d.ts +17 -0
  116. package/dist/entry.d.ts.map +1 -0
  117. package/dist/entry.js +157 -0
  118. package/dist/expectation.d.ts +21 -0
  119. package/dist/expectation.d.ts.map +1 -0
  120. package/dist/expectation.js +70 -0
  121. package/dist/failure.d.ts +13 -0
  122. package/dist/failure.d.ts.map +1 -0
  123. package/dist/failure.js +19 -0
  124. package/dist/fence.d.ts +6 -0
  125. package/dist/fence.d.ts.map +1 -0
  126. package/dist/fence.js +10 -0
  127. package/dist/file.d.ts +2 -10
  128. package/dist/file.d.ts.map +1 -1
  129. package/dist/file.js +3 -120
  130. package/dist/host.d.ts +15 -3
  131. package/dist/host.d.ts.map +1 -1
  132. package/dist/host.js +107 -21
  133. package/dist/identity.d.ts +5 -0
  134. package/dist/identity.d.ts.map +1 -1
  135. package/dist/identity.js +19 -1
  136. package/dist/index.d.ts +20 -10
  137. package/dist/index.d.ts.map +1 -1
  138. package/dist/index.js +7 -0
  139. package/dist/memory.d.ts +11 -4
  140. package/dist/memory.d.ts.map +1 -1
  141. package/dist/memory.js +92 -27
  142. package/dist/model.d.ts +7 -4
  143. package/dist/model.d.ts.map +1 -1
  144. package/dist/model.js +10 -1
  145. package/dist/observation.d.ts +7 -2
  146. package/dist/observation.d.ts.map +1 -1
  147. package/dist/observation.js +29 -12
  148. package/dist/open.d.ts +2 -0
  149. package/dist/open.d.ts.map +1 -0
  150. package/dist/open.js +1 -0
  151. package/dist/outcome.d.ts +57 -3
  152. package/dist/outcome.d.ts.map +1 -1
  153. package/dist/outcome.js +67 -1
  154. package/dist/overlay.d.ts +18 -5
  155. package/dist/overlay.d.ts.map +1 -1
  156. package/dist/overlay.js +271 -165
  157. package/dist/query.d.ts +61 -4
  158. package/dist/query.d.ts.map +1 -1
  159. package/dist/query.js +40 -7
  160. package/dist/reach.d.ts +13 -4
  161. package/dist/reach.d.ts.map +1 -1
  162. package/dist/reach.js +201 -47
  163. package/dist/react.d.ts +31 -20
  164. package/dist/react.d.ts.map +1 -1
  165. package/dist/react.js +13 -21
  166. package/dist/read-side.d.ts +18 -9
  167. package/dist/read-side.d.ts.map +1 -1
  168. package/dist/read-side.js +903 -157
  169. package/dist/readiness.d.ts +53 -0
  170. package/dist/readiness.d.ts.map +1 -0
  171. package/dist/readiness.js +33 -0
  172. package/dist/reads.d.ts +11 -0
  173. package/dist/reads.d.ts.map +1 -0
  174. package/dist/reads.js +98 -0
  175. package/dist/recovery.d.ts +25 -0
  176. package/dist/recovery.d.ts.map +1 -0
  177. package/dist/recovery.js +198 -0
  178. package/dist/refresh.d.ts +13 -3
  179. package/dist/refresh.d.ts.map +1 -1
  180. package/dist/refresh.js +73 -43
  181. package/dist/replica.d.ts +16 -8
  182. package/dist/replica.d.ts.map +1 -1
  183. package/dist/replica.js +198 -91
  184. package/dist/room-draft.d.ts +40 -9
  185. package/dist/room-draft.d.ts.map +1 -1
  186. package/dist/room-draft.js +153 -37
  187. package/dist/rows.d.ts +10 -13
  188. package/dist/rows.d.ts.map +1 -1
  189. package/dist/rows.js +19 -14
  190. package/dist/save.d.ts +72 -4
  191. package/dist/save.d.ts.map +1 -1
  192. package/dist/save.js +812 -145
  193. package/dist/search.d.ts +52 -8
  194. package/dist/search.d.ts.map +1 -1
  195. package/dist/search.js +242 -46
  196. package/dist/seed.d.ts +16 -0
  197. package/dist/seed.d.ts.map +1 -0
  198. package/dist/seed.js +75 -0
  199. package/dist/shared.d.ts +19 -0
  200. package/dist/shared.d.ts.map +1 -0
  201. package/dist/shared.js +461 -0
  202. package/dist/status.d.ts +6 -2
  203. package/dist/status.d.ts.map +1 -1
  204. package/dist/status.js +0 -8
  205. package/dist/store.d.ts +90 -23
  206. package/dist/store.d.ts.map +1 -1
  207. package/dist/store.js +675 -295
  208. package/dist/tables.d.ts +181 -0
  209. package/dist/tables.d.ts.map +1 -0
  210. package/dist/tables.js +309 -0
  211. package/dist/turn.d.ts +9 -0
  212. package/dist/turn.d.ts.map +1 -0
  213. package/dist/turn.js +67 -0
  214. package/dist/windows.d.ts +21 -0
  215. package/dist/windows.d.ts.map +1 -0
  216. package/dist/windows.js +84 -0
  217. package/dist/wire.d.ts +165 -33
  218. package/dist/wire.d.ts.map +1 -1
  219. package/dist/wire.js +67 -31
  220. package/package.json +30 -14
  221. package/dist/attachment.d.ts +0 -3
  222. package/dist/attachment.d.ts.map +0 -1
  223. package/dist/attachment.js +0 -8
package/dist/shared.js ADDED
@@ -0,0 +1,461 @@
1
+ import { finished, ROW_WEIGHT } from './turn.js';
2
+ const BITS = 5;
3
+ const MASK = (1 << BITS) - 1;
4
+ const roots = new WeakMap();
5
+ const tallies = new WeakMap();
6
+ function hashOf(key) {
7
+ let hash = 0x811c9dc5;
8
+ for (let index = 0; index < key.length; index += 1) {
9
+ hash ^= key.charCodeAt(index);
10
+ hash = Math.imul(hash, 0x01000193);
11
+ }
12
+ return hash >>> 0;
13
+ }
14
+ function isLeaf(node) {
15
+ return 'key' in node;
16
+ }
17
+ function isCollision(node) {
18
+ return 'leaves' in node;
19
+ }
20
+ function fragment(hash, shift) {
21
+ return (hash >>> shift) & MASK;
22
+ }
23
+ function count(bits) {
24
+ let left = bits - ((bits >>> 1) & 0x55555555);
25
+ left = (left & 0x33333333) + ((left >>> 2) & 0x33333333);
26
+ return (((left + (left >>> 4)) & 0x0f0f0f0f) * 0x01010101) >>> 24;
27
+ }
28
+ function slot(bitmap, bit) {
29
+ return count(bitmap & (bit - 1));
30
+ }
31
+ function lookup(root, key) {
32
+ const hash = hashOf(key);
33
+ let node = root.node;
34
+ let shift = 0;
35
+ while (node !== undefined) {
36
+ if (isLeaf(node))
37
+ return node.key === key ? node : undefined;
38
+ if (isCollision(node))
39
+ return node.leaves.find((leaf) => leaf.key === key);
40
+ const bit = 1 << fragment(hash, shift);
41
+ if ((node.bitmap & bit) === 0)
42
+ return undefined;
43
+ node = node.children[slot(node.bitmap, bit)];
44
+ shift += BITS;
45
+ }
46
+ return undefined;
47
+ }
48
+ function branchOf(node, shift) {
49
+ return { bitmap: 1 << fragment(node.hash, shift), children: [node] };
50
+ }
51
+ function joined(left, right, shift) {
52
+ if (left.hash === right.hash) {
53
+ const leaves = isCollision(left) ? left.leaves : [left];
54
+ return { hash: right.hash, leaves: [...leaves, right] };
55
+ }
56
+ return put(branchOf(left, shift), right, shift);
57
+ }
58
+ function put(node, leaf, shift) {
59
+ if (node === undefined)
60
+ return leaf;
61
+ if (isLeaf(node)) {
62
+ if (node.key !== leaf.key)
63
+ return joined(node, leaf, shift);
64
+ return node.value === leaf.value ? node : leaf;
65
+ }
66
+ if (isCollision(node)) {
67
+ if (node.hash !== leaf.hash)
68
+ return joined(node, leaf, shift);
69
+ const at = node.leaves.findIndex((held) => held.key === leaf.key);
70
+ if (at === -1)
71
+ return { hash: node.hash, leaves: [...node.leaves, leaf] };
72
+ if (node.leaves[at].value === leaf.value)
73
+ return node;
74
+ return {
75
+ hash: node.hash,
76
+ leaves: node.leaves.map((held, index) => (index === at ? leaf : held)),
77
+ };
78
+ }
79
+ const bit = 1 << fragment(leaf.hash, shift);
80
+ const at = slot(node.bitmap, bit);
81
+ if ((node.bitmap & bit) === 0) {
82
+ return {
83
+ bitmap: node.bitmap | bit,
84
+ children: [...node.children.slice(0, at), leaf, ...node.children.slice(at)],
85
+ };
86
+ }
87
+ const child = node.children[at];
88
+ const next = put(child, leaf, shift + BITS);
89
+ if (next === child)
90
+ return node;
91
+ return {
92
+ bitmap: node.bitmap,
93
+ children: node.children.map((held, index) => (index === at ? next : held)),
94
+ };
95
+ }
96
+ function lone(node) {
97
+ if (node.children.length !== 1)
98
+ return null;
99
+ const [only] = node.children;
100
+ return isLeaf(only) || isCollision(only) ? only : null;
101
+ }
102
+ function taken(node, hash, key, shift) {
103
+ if (isLeaf(node))
104
+ return node.key === key ? undefined : node;
105
+ if (isCollision(node)) {
106
+ const leaves = node.leaves.filter((leaf) => leaf.key !== key);
107
+ if (leaves.length === node.leaves.length)
108
+ return node;
109
+ return leaves.length === 1 ? leaves[0] : { hash: node.hash, leaves };
110
+ }
111
+ const bit = 1 << fragment(hash, shift);
112
+ if ((node.bitmap & bit) === 0)
113
+ return node;
114
+ const at = slot(node.bitmap, bit);
115
+ const child = node.children[at];
116
+ const next = taken(child, hash, key, shift + BITS);
117
+ if (next === child)
118
+ return node;
119
+ if (next === undefined) {
120
+ if (node.children.length === 1)
121
+ return undefined;
122
+ const rest = {
123
+ bitmap: node.bitmap & ~bit,
124
+ children: node.children.filter((_, index) => index !== at),
125
+ };
126
+ return lone(rest) ?? rest;
127
+ }
128
+ const replaced = {
129
+ bitmap: node.bitmap,
130
+ children: node.children.map((held, index) => (index === at ? next : held)),
131
+ };
132
+ return lone(replaced) ?? replaced;
133
+ }
134
+ function* leavesOf(node) {
135
+ if (node === undefined)
136
+ return;
137
+ if (isLeaf(node)) {
138
+ yield node;
139
+ return;
140
+ }
141
+ if (isCollision(node)) {
142
+ yield* node.leaves;
143
+ return;
144
+ }
145
+ for (const child of node.children)
146
+ yield* leavesOf(child);
147
+ }
148
+ function facade(root) {
149
+ let keys;
150
+ const target = {};
151
+ const map = new Proxy(target, {
152
+ get(held, property, receiver) {
153
+ if (typeof property === 'string') {
154
+ const leaf = lookup(root, property);
155
+ if (leaf !== undefined)
156
+ return leaf.value;
157
+ }
158
+ return Reflect.get(held, property, receiver);
159
+ },
160
+ has(held, property) {
161
+ return ((typeof property === 'string' && lookup(root, property) !== undefined) ||
162
+ Reflect.has(held, property));
163
+ },
164
+ ownKeys() {
165
+ keys ??= [...leavesOf(root.node)].map((leaf) => leaf.key);
166
+ return keys;
167
+ },
168
+ getOwnPropertyDescriptor(_held, property) {
169
+ if (typeof property !== 'string')
170
+ return undefined;
171
+ const leaf = lookup(root, property);
172
+ if (leaf === undefined)
173
+ return undefined;
174
+ return { value: leaf.value, writable: false, enumerable: true, configurable: true };
175
+ },
176
+ set: () => false,
177
+ defineProperty: () => false,
178
+ deleteProperty: () => false,
179
+ });
180
+ roots.set(map, root);
181
+ return map;
182
+ }
183
+ export function* entriesIn(map) {
184
+ const root = roots.get(map);
185
+ if (root === undefined) {
186
+ for (const key of Object.keys(map))
187
+ yield [key, map[key]];
188
+ return;
189
+ }
190
+ for (const leaf of leavesOf(root.node))
191
+ yield [leaf.key, leaf.value];
192
+ }
193
+ function* rooted(map) {
194
+ const held = roots.get(map);
195
+ if (held !== undefined)
196
+ return held;
197
+ let node;
198
+ let size = 0;
199
+ for (const [key, value] of entriesIn(map)) {
200
+ node = put(node, { key, hash: hashOf(key), value }, 0);
201
+ size += 1;
202
+ yield ROW_WEIGHT;
203
+ }
204
+ return { node, size };
205
+ }
206
+ export function* keysIn(...maps) {
207
+ const keys = new Set();
208
+ for (const map of maps) {
209
+ for (const [key] of entriesIn(map)) {
210
+ keys.add(key);
211
+ yield ROW_WEIGHT;
212
+ }
213
+ }
214
+ return keys;
215
+ }
216
+ const RUN = 1 << 10;
217
+ function* merged(left, right) {
218
+ const into = [];
219
+ let at = 0;
220
+ let from = 0;
221
+ while (at < left.length || from < right.length) {
222
+ into.push(from === right.length || (at < left.length && left[at] < right[from])
223
+ ? left[at++]
224
+ : right[from++]);
225
+ yield ROW_WEIGHT;
226
+ }
227
+ return into;
228
+ }
229
+ function* pathsOf(map) {
230
+ for (const [key] of entriesIn(map))
231
+ yield key;
232
+ }
233
+ export function* sortedKeys(map) {
234
+ return yield* sorting(pathsOf(map));
235
+ }
236
+ export function* sorting(keys) {
237
+ let runs = [];
238
+ let run = [];
239
+ for (const key of keys) {
240
+ run.push(key);
241
+ yield ROW_WEIGHT;
242
+ if (run.length === RUN) {
243
+ runs.push(run.sort());
244
+ run = [];
245
+ }
246
+ }
247
+ if (run.length > 0)
248
+ runs.push(run.sort());
249
+ while (runs.length > 1) {
250
+ const next = [];
251
+ for (let at = 0; at < runs.length; at += 2) {
252
+ next.push(at + 1 === runs.length ? runs[at] : yield* merged(runs[at], runs[at + 1]));
253
+ }
254
+ runs = next;
255
+ }
256
+ return runs[0] ?? [];
257
+ }
258
+ export function* sharing(map) {
259
+ return roots.has(map) ? map : facade(yield* rooted(map));
260
+ }
261
+ export function shared(map) {
262
+ return finished(sharing(map));
263
+ }
264
+ export function patched(base, set, removed = []) {
265
+ return finished(patching(base, set, removed));
266
+ }
267
+ export function* patching(base, set, removed = []) {
268
+ const root = yield* rooted(base);
269
+ const weighed = [...(tallies.get(base) ?? [])];
270
+ let node = root.node;
271
+ let size = root.size;
272
+ const shift = (key, before, after) => {
273
+ for (const entry of weighed) {
274
+ entry[1] +=
275
+ (after === undefined ? 0 : entry[0](key, after)) -
276
+ (before === undefined ? 0 : entry[0](key, before.value));
277
+ }
278
+ };
279
+ for (const key of removed) {
280
+ if (node === undefined)
281
+ break;
282
+ const before = lookup({ node, size }, key);
283
+ if (before === undefined)
284
+ continue;
285
+ node = taken(node, before.hash, key, 0);
286
+ size -= 1;
287
+ shift(key, before, undefined);
288
+ yield ROW_WEIGHT;
289
+ }
290
+ for (const [key, value] of set) {
291
+ const before = lookup({ node, size }, key);
292
+ node = put(node, { key, hash: hashOf(key), value }, 0);
293
+ if (before === undefined)
294
+ size += 1;
295
+ shift(key, before, value);
296
+ yield ROW_WEIGHT;
297
+ }
298
+ if (node === root.node && roots.has(base))
299
+ return base;
300
+ const map = facade({ node, size });
301
+ if (weighed.length > 0)
302
+ tallies.set(map, new Map(weighed));
303
+ return map;
304
+ }
305
+ export function* tallying(map, weigh) {
306
+ const known = tallies.get(map)?.get(weigh);
307
+ if (known !== undefined)
308
+ return known;
309
+ let total = 0;
310
+ for (const [key, value] of entriesIn(map)) {
311
+ total += weigh(key, value);
312
+ yield ROW_WEIGHT;
313
+ }
314
+ if (roots.has(map)) {
315
+ const kept = tallies.get(map) ?? new Map();
316
+ kept.set(weigh, total);
317
+ tallies.set(map, kept);
318
+ }
319
+ return total;
320
+ }
321
+ export function sizeOf(map) {
322
+ return roots.get(map)?.size ?? Object.keys(map).length;
323
+ }
324
+ function* compared(left, right, same, into) {
325
+ const pending = [[left, right]];
326
+ for (let pair = pending.pop(); pair !== undefined; pair = pending.pop()) {
327
+ const [one, other] = pair;
328
+ if (one === other)
329
+ continue;
330
+ if (one !== undefined &&
331
+ other !== undefined &&
332
+ !isLeaf(one) &&
333
+ !isCollision(one) &&
334
+ !isLeaf(other) &&
335
+ !isCollision(other)) {
336
+ const children = [];
337
+ for (let remaining = one.bitmap | other.bitmap; remaining !== 0; remaining &= remaining - 1) {
338
+ const bit = remaining & -remaining;
339
+ children.push([
340
+ (one.bitmap & bit) === 0 ? undefined : one.children[slot(one.bitmap, bit)],
341
+ (other.bitmap & bit) === 0 ? undefined : other.children[slot(other.bitmap, bit)],
342
+ ]);
343
+ }
344
+ pending.push(...children.reverse());
345
+ continue;
346
+ }
347
+ const before = new Map();
348
+ for (const leaf of leavesOf(one)) {
349
+ before.set(leaf.key, leaf.value);
350
+ yield ROW_WEIGHT;
351
+ }
352
+ for (const leaf of leavesOf(other)) {
353
+ const held = before.get(leaf.key);
354
+ before.delete(leaf.key);
355
+ if (held === undefined || !same(held, leaf.value))
356
+ into.add(leaf.key);
357
+ yield ROW_WEIGHT;
358
+ }
359
+ for (const key of before.keys())
360
+ into.add(key);
361
+ }
362
+ }
363
+ export function* differences(left, right, same = Object.is) {
364
+ const into = new Set();
365
+ if (left === right)
366
+ return into;
367
+ const leftRoot = roots.get(left);
368
+ const rightRoot = roots.get(right);
369
+ if (leftRoot !== undefined && rightRoot !== undefined) {
370
+ yield* compared(leftRoot.node, rightRoot.node, same, into);
371
+ return into;
372
+ }
373
+ for (const [key, value] of entriesIn(left)) {
374
+ if (!Object.hasOwn(right, key) || !same(value, right[key]))
375
+ into.add(key);
376
+ yield ROW_WEIGHT;
377
+ }
378
+ for (const [key] of entriesIn(right)) {
379
+ if (!Object.hasOwn(left, key))
380
+ into.add(key);
381
+ yield ROW_WEIGHT;
382
+ }
383
+ return into;
384
+ }
385
+ export function differing(left, right, same = Object.is) {
386
+ return finished(differences(left, right, same));
387
+ }
388
+ const drafts = new WeakMap();
389
+ const GONE = Symbol('gone');
390
+ export function drafted(base) {
391
+ const written = new Map();
392
+ const read = (key) => {
393
+ const held = written.get(key);
394
+ if (held === GONE)
395
+ return undefined;
396
+ return held ?? (Object.hasOwn(base, key) ? base[key] : undefined);
397
+ };
398
+ const draft = new Proxy({}, {
399
+ get(held, property, receiver) {
400
+ if (typeof property === 'string') {
401
+ const value = read(property);
402
+ if (value !== undefined)
403
+ return value;
404
+ }
405
+ return Reflect.get(held, property, receiver);
406
+ },
407
+ has(held, property) {
408
+ return ((typeof property === 'string' && read(property) !== undefined) ||
409
+ Reflect.has(held, property));
410
+ },
411
+ ownKeys() {
412
+ const keys = new Set(Object.keys(base));
413
+ for (const [key, value] of written) {
414
+ if (value === GONE)
415
+ keys.delete(key);
416
+ else
417
+ keys.add(key);
418
+ }
419
+ return [...keys];
420
+ },
421
+ getOwnPropertyDescriptor(_held, property) {
422
+ if (typeof property !== 'string')
423
+ return undefined;
424
+ const value = read(property);
425
+ if (value === undefined)
426
+ return undefined;
427
+ return { value, writable: true, enumerable: true, configurable: true };
428
+ },
429
+ set(_held, property, value) {
430
+ if (typeof property !== 'string')
431
+ return false;
432
+ written.set(property, value);
433
+ return true;
434
+ },
435
+ deleteProperty(_held, property) {
436
+ if (typeof property !== 'string')
437
+ return false;
438
+ written.set(property, GONE);
439
+ return true;
440
+ },
441
+ defineProperty: () => false,
442
+ });
443
+ drafts.set(draft, { base, written });
444
+ return draft;
445
+ }
446
+ export function settled(draft) {
447
+ return finished(settling(draft));
448
+ }
449
+ export function* settling(draft) {
450
+ const held = drafts.get(draft);
451
+ const set = [];
452
+ const removed = [];
453
+ for (const [key, value] of held.written) {
454
+ if (value === GONE)
455
+ removed.push(key);
456
+ else
457
+ set.push([key, value]);
458
+ yield ROW_WEIGHT;
459
+ }
460
+ return yield* patching(held.base, set, removed);
461
+ }
package/dist/status.d.ts CHANGED
@@ -1,11 +1,16 @@
1
+ import type { Open } from './open.js';
2
+ import type { ProviderRefusal } from './adapter.js';
1
3
  import type { ReturnReason } from './room-draft.js';
2
4
  export type AttentionEntry = {
3
5
  room: string;
4
6
  path: string;
5
7
  returned: ReturnReason;
8
+ } | {
9
+ room: string;
10
+ refusal: ProviderRefusal;
6
11
  };
7
12
  export type SyncStatus = {
8
- state: 'pending' | 'syncing' | 'synced';
13
+ state: Open<'pending' | 'syncing' | 'synced'>;
9
14
  lastCompleted: string | null;
10
15
  providerUnreachable: boolean;
11
16
  attentionRequired: readonly AttentionEntry[];
@@ -17,7 +22,6 @@ export type CycleProgress = {
17
22
  };
18
23
  export declare const STARTING: SyncStatus;
19
24
  export declare function transferring(status: SyncStatus): SyncStatus;
20
- export declare function settled(status: SyncStatus, progress: CycleProgress, at: string | null, queued: boolean, attention: Pick<SyncStatus, 'attentionRequired' | 'reset'>): SyncStatus;
21
25
  export declare function saving(status: SyncStatus): SyncStatus;
22
26
  export declare function same(one: SyncStatus, other: SyncStatus): boolean;
23
27
  //# sourceMappingURL=status.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"status.d.ts","sourceRoot":"","sources":["../src/status.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAEpD,MAAM,MAAM,cAAc,GAAG;IAC3B,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,YAAY,CAAC;CACxB,CAAC;AAEF,MAAM,MAAM,UAAU,GAAG;IACvB,KAAK,EAAE,SAAS,GAAG,SAAS,GAAG,QAAQ,CAAC;IACxC,aAAa,EAAE,MAAM,GAAG,IAAI,CAAC;IAC7B,mBAAmB,EAAE,OAAO,CAAC;IAC7B,iBAAiB,EAAE,SAAS,cAAc,EAAE,CAAC;IAC7C,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;CACtB,CAAC;AAEF,MAAM,MAAM,aAAa,GAAG;IAC1B,QAAQ,EAAE,OAAO,CAAC;IAClB,SAAS,EAAE,OAAO,CAAC;CACpB,CAAC;AAEF,eAAO,MAAM,QAAQ,EAAE,UAMtB,CAAC;AAEF,wBAAgB,YAAY,CAAC,MAAM,EAAE,UAAU,GAAG,UAAU,CAE3D;AAED,wBAAgB,OAAO,CACrB,MAAM,EAAE,UAAU,EAClB,QAAQ,EAAE,aAAa,EACvB,EAAE,EAAE,MAAM,GAAG,IAAI,EACjB,MAAM,EAAE,OAAO,EACf,SAAS,EAAE,IAAI,CAAC,UAAU,EAAE,mBAAmB,GAAG,OAAO,CAAC,GACzD,UAAU,CAOZ;AAED,wBAAgB,MAAM,CAAC,MAAM,EAAE,UAAU,GAAG,UAAU,CAErD;AAED,wBAAgB,IAAI,CAAC,GAAG,EAAE,UAAU,EAAE,KAAK,EAAE,UAAU,GAAG,OAAO,CAQhE"}
1
+ {"version":3,"file":"status.d.ts","sourceRoot":"","sources":["../src/status.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACtC,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AACpD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAEpD,MAAM,MAAM,cAAc,GACtB;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,YAAY,CAAA;CAAE,GACtD;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,eAAe,CAAA;CAAE,CAAC;AAE/C,MAAM,MAAM,UAAU,GAAG;IACvB,KAAK,EAAE,IAAI,CAAC,SAAS,GAAG,SAAS,GAAG,QAAQ,CAAC,CAAC;IAC9C,aAAa,EAAE,MAAM,GAAG,IAAI,CAAC;IAC7B,mBAAmB,EAAE,OAAO,CAAC;IAC7B,iBAAiB,EAAE,SAAS,cAAc,EAAE,CAAC;IAC7C,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;CACtB,CAAC;AAEF,MAAM,MAAM,aAAa,GAAG;IAC1B,QAAQ,EAAE,OAAO,CAAC;IAClB,SAAS,EAAE,OAAO,CAAC;CACpB,CAAC;AAEF,eAAO,MAAM,QAAQ,EAAE,UAMtB,CAAC;AAEF,wBAAgB,YAAY,CAAC,MAAM,EAAE,UAAU,GAAG,UAAU,CAE3D;AAED,wBAAgB,MAAM,CAAC,MAAM,EAAE,UAAU,GAAG,UAAU,CAErD;AAED,wBAAgB,IAAI,CAAC,GAAG,EAAE,UAAU,EAAE,KAAK,EAAE,UAAU,GAAG,OAAO,CAQhE"}
package/dist/status.js CHANGED
@@ -8,14 +8,6 @@ export const STARTING = {
8
8
  export function transferring(status) {
9
9
  return { ...status, state: 'syncing' };
10
10
  }
11
- export function settled(status, progress, at, queued, attention) {
12
- return {
13
- state: queued ? 'pending' : progress.complete ? 'synced' : status.state,
14
- lastCompleted: progress.complete ? at : status.lastCompleted,
15
- providerUnreachable: !progress.reachable,
16
- ...attention,
17
- };
18
- }
19
11
  export function saving(status) {
20
12
  return { ...status, state: 'pending' };
21
13
  }
package/dist/store.d.ts CHANGED
@@ -1,11 +1,13 @@
1
1
  import type { ModelState } from './model.js';
2
- import type { Capability, HeadChange, OperationScopes, ProviderLimit, ReplicaAdmission, RoomAttachment, TypedSelection } from './adapter.js';
3
- import type { AcceptedMessage } from './adapter.js';
2
+ import type { Capability, HeadChange, Member, OperationScopes, ProviderLimit, ReplicaAdmission, TypedSelection } from './adapter.js';
3
+ import type { AcceptedMessage, KeptReceipt } from './adapter.js';
4
4
  import type { ChangeBatch } from './change.js';
5
- import type { ChatDraftFile } from './file.js';
5
+ import { type HeldBinary } from './overlay.js';
6
+ import { type Later } from './turn.js';
6
7
  import type { Submission } from './outcome.js';
7
8
  import type { RoomDraftRecord, SavedDraft } from './room-draft.js';
8
- export declare const STORE_SCHEMA = 20;
9
+ import type { RuntimeDatabase, StoredFile } from './tables.js';
10
+ export declare const STORE_SCHEMA = 25;
9
11
  export type ReplicaRecord = {
10
12
  locator: string;
11
13
  name: string;
@@ -17,12 +19,26 @@ export type ReplicaRecord = {
17
19
  selectionModel: ModelState;
18
20
  providerOwnedPaths: readonly string[];
19
21
  roomVersion: string;
22
+ transfer: string;
20
23
  documents: Readonly<Record<string, string>>;
21
- binaryTargets: readonly string[];
24
+ binaries: Readonly<Record<string, HeldBinary>>;
22
25
  revisions: Readonly<Record<string, string>>;
26
+ fileIds: Readonly<Record<string, string>>;
23
27
  changes: Readonly<Record<string, HeadChange>>;
24
28
  written: Readonly<Record<string, string>>;
25
- attachments: readonly RoomAttachment[];
29
+ };
30
+ export type StagedLayer = Pick<ReplicaRecord, 'documents' | 'binaries' | 'revisions' | 'fileIds' | 'changes' | 'written'>;
31
+ export type TransferRecord = {
32
+ locator: string;
33
+ transfer: string;
34
+ selection: string;
35
+ from: string;
36
+ last: string;
37
+ pageToken: string | null;
38
+ models: {
39
+ collection: ModelState;
40
+ selection: ModelState;
41
+ };
26
42
  };
27
43
  export type PendingState = {
28
44
  readonly drafts: readonly SavedDraft[];
@@ -31,6 +47,9 @@ export type PendingState = {
31
47
  readonly upload?: unknown;
32
48
  readonly accepted?: string;
33
49
  };
50
+ export type TargetRecord = {
51
+ established: boolean;
52
+ };
34
53
  export type QueueRecord = {
35
54
  locator: string;
36
55
  pending: readonly {
@@ -45,41 +64,65 @@ export type PresentationRecord = {
45
64
  export type HeldConversation = {
46
65
  conversation: string;
47
66
  read: string | null | undefined;
48
- messages: readonly AcceptedMessage[];
67
+ partial: boolean;
68
+ };
69
+ export type MessageWindow = {
70
+ messages: AcceptedMessage[];
71
+ more: boolean;
49
72
  };
50
73
  export type ConversationWrite = {
51
74
  conversation: string;
52
75
  messages: readonly AcceptedMessage[];
53
- read?: string | null;
54
- sealed?: boolean;
76
+ read?: {
77
+ at: string | null;
78
+ partial: boolean;
79
+ };
80
+ };
81
+ export type ChatSubmission = {
82
+ readonly operation: string;
83
+ readonly text: string;
84
+ readonly turn: string | null;
55
85
  };
56
86
  export type ChatDraftRecord = {
57
87
  conversation: string;
58
88
  text: string;
59
- files: readonly ChatDraftFile[];
60
- turn: string | null;
89
+ revision: string;
90
+ submitted?: readonly ChatSubmission[];
91
+ moves?: readonly string[];
92
+ };
93
+ export type CallRecord = {
94
+ readonly operation: string;
95
+ readonly name: string;
96
+ readonly input: unknown;
61
97
  };
62
98
  export type RecordWrite = {
63
99
  next: ReplicaRecord;
64
100
  previous: ReplicaRecord | null;
65
- queue: readonly ChangeBatch[];
66
- previousQueue?: readonly ChangeBatch[];
67
- saved?: readonly SavedDraft[];
101
+ stored?: StagedLayer;
102
+ };
103
+ export type AuthoredWrite = {
104
+ locator: string;
105
+ queue?: readonly ChangeBatch[];
106
+ founded?: readonly ChangeBatch[];
107
+ saved?: ReadonlyMap<string, readonly SavedDraft[]>;
68
108
  roomDrafts?: readonly RoomDraftRecord[];
69
109
  consumedRoomDrafts?: readonly string[];
70
110
  };
71
111
  export type RuntimeStore = {
72
112
  all(): Promise<readonly ReplicaRecord[]>;
113
+ keepAuthored(...writes: readonly AuthoredWrite[]): Promise<void>;
114
+ saveAuthored(files: readonly StoredFile[], ...writes: readonly AuthoredWrite[]): Promise<string | null>;
73
115
  write(input: RecordWrite): Promise<void>;
74
- revokeModels(records: readonly {
75
- record: ReplicaRecord;
76
- queue: readonly ChangeBatch[];
77
- }[]): Promise<void>;
116
+ revokeModels(records: readonly ReplicaRecord[]): Promise<void>;
78
117
  lastCompleted(): Promise<string | null>;
79
118
  complete(at: string): Promise<void>;
80
119
  providerLimits(): Promise<readonly ProviderLimit[]>;
81
120
  capabilities(): Promise<readonly Capability[]>;
82
121
  declare(limits: readonly ProviderLimit[], capabilities: readonly Capability[]): Promise<void>;
122
+ member(): Promise<Member | null>;
123
+ keepMember(member: Member | null): Promise<void>;
124
+ targets(): Promise<Readonly<Record<string, TargetRecord>>>;
125
+ keepTargets(targets: Readonly<Record<string, TargetRecord>>): Promise<void>;
83
126
  queues(): Promise<readonly QueueRecord[]>;
84
127
  submit(locator: string, id: string, submission: Submission, lost: boolean): Promise<void>;
85
128
  accept(locator: string, id: string, roomVersion: string): Promise<void>;
@@ -89,17 +132,41 @@ export type RuntimeStore = {
89
132
  present(record: PresentationRecord): Promise<void>;
90
133
  conversations(): Promise<readonly HeldConversation[]>;
91
134
  keepMessages(write: ConversationWrite): Promise<void>;
135
+ messageWindow(conversation: string, limit: number, before: readonly [string, string] | null): Promise<MessageWindow>;
136
+ messageCount(conversation: string): Promise<number>;
137
+ messagesAfter(conversation: string, identity: string | null): Promise<AcceptedMessage[]>;
138
+ messageIdentities(conversation: string): Promise<readonly string[]>;
139
+ turnArrived(conversation: string, turn: string): Promise<boolean>;
92
140
  dropConversations(conversations: readonly string[]): Promise<void>;
93
141
  chatDrafts(): Promise<readonly ChatDraftRecord[]>;
94
- keepChatDraft(record: ChatDraftRecord): Promise<void>;
142
+ keepChatDrafts(records: readonly ChatDraftRecord[]): Promise<void>;
95
143
  dropChatDraft(conversation: string): Promise<void>;
96
- keepRoomDraft(record: RoomDraftRecord): Promise<void>;
144
+ calls(): Promise<readonly CallRecord[]>;
145
+ keepCall(record: CallRecord): Promise<void>;
146
+ dropCall(operation: string): Promise<void>;
147
+ keepRoomDraft(record: RoomDraftRecord, files: readonly StoredFile[]): Promise<void>;
97
148
  discardRoomDraft(locator: string, path: string): Promise<void>;
98
- dropRooms(locators: readonly string[]): Promise<void>;
149
+ fences(): Promise<readonly string[]>;
150
+ erase(locators: readonly string[]): Promise<void>;
151
+ discard(locator: string): Promise<void>;
152
+ transfers(): Promise<readonly TransferRecord[]>;
153
+ staged(record: TransferRecord): Promise<StagedLayer>;
154
+ stage(record: TransferRecord, page: StagedLayer): Promise<void>;
155
+ abandon(locator: string): Promise<void>;
156
+ receivedHeads(): Promise<readonly KeptReceipt[]>;
157
+ receivedParts(key: string, count: number): Promise<readonly unknown[]>;
158
+ keepReceived(key: string, head: unknown, part?: {
159
+ index: number;
160
+ value: unknown;
161
+ }): Promise<void>;
162
+ dropReceived(keys: readonly string[]): Promise<void>;
99
163
  generation(): Promise<string | null>;
100
164
  rememberGeneration(value: string): Promise<void>;
165
+ file(key: string, type: string): Promise<Blob>;
101
166
  close(): void;
102
167
  };
103
- export declare function removeRuntimeStore(): Promise<void>;
104
- export declare function openRuntimeStore(): Promise<RuntimeStore>;
168
+ export declare function transacted<T>(database: IDBDatabase, stores: string | readonly string[], mode: IDBTransactionMode, work: (transaction: IDBTransaction) => Later<T>): Promise<T>;
169
+ export declare function endRuntimeStore(database: RuntimeDatabase, holder: string | undefined): Promise<boolean>;
170
+ export declare function openDatabase(name: string, version: number, upgrade: (opening: IDBOpenDBRequest, oldVersion: number) => void): Promise<IDBDatabase>;
171
+ export declare function openRuntimeStore(fenced: (locator: string) => boolean, authored: RuntimeDatabase): Promise<RuntimeStore>;
105
172
  //# sourceMappingURL=store.d.ts.map