@agentshouse/mdruntime 0.1.0-cf6041a06849c → 0.1.0-cf6e25bcd52f6

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 (185) hide show
  1. package/README.md +23 -10
  2. package/dist/adapter.d.ts +144 -29
  3. package/dist/adapter.d.ts.map +1 -1
  4. package/dist/attached-model.d.ts +5 -1
  5. package/dist/attached-model.d.ts.map +1 -1
  6. package/dist/attached-model.js +53 -6
  7. package/dist/attempt.d.ts +16 -0
  8. package/dist/attempt.d.ts.map +1 -0
  9. package/dist/attempt.js +70 -0
  10. package/dist/binary.d.ts +18 -0
  11. package/dist/binary.d.ts.map +1 -0
  12. package/dist/binary.js +8 -0
  13. package/dist/bootstrap.d.ts +41 -6
  14. package/dist/bootstrap.d.ts.map +1 -1
  15. package/dist/bootstrap.js +224 -45
  16. package/dist/chain.d.ts +11 -0
  17. package/dist/chain.d.ts.map +1 -0
  18. package/dist/chain.js +64 -0
  19. package/dist/change-set.d.ts +9 -14
  20. package/dist/change-set.d.ts.map +1 -1
  21. package/dist/change-set.js +11 -56
  22. package/dist/change.d.ts +45 -0
  23. package/dist/change.d.ts.map +1 -1
  24. package/dist/change.js +34 -0
  25. package/dist/client.d.ts +35 -14
  26. package/dist/client.d.ts.map +1 -1
  27. package/dist/client.js +156 -19
  28. package/dist/collection.d.ts +14 -13
  29. package/dist/collection.d.ts.map +1 -1
  30. package/dist/collection.js +66 -54
  31. package/dist/compile.d.ts +16 -0
  32. package/dist/compile.d.ts.map +1 -0
  33. package/dist/compile.js +765 -0
  34. package/dist/connection.d.ts +63 -20
  35. package/dist/connection.d.ts.map +1 -1
  36. package/dist/connection.js +3889 -743
  37. package/dist/conversation.d.ts +42 -12
  38. package/dist/conversation.d.ts.map +1 -1
  39. package/dist/conversation.js +642 -188
  40. package/dist/digest.d.ts +7 -0
  41. package/dist/digest.d.ts.map +1 -0
  42. package/dist/digest.js +113 -0
  43. package/dist/durability.d.ts +5 -1
  44. package/dist/durability.d.ts.map +1 -1
  45. package/dist/durability.js +5 -0
  46. package/dist/entry.d.ts +19 -0
  47. package/dist/entry.d.ts.map +1 -0
  48. package/dist/entry.js +151 -0
  49. package/dist/expectation.d.ts +21 -0
  50. package/dist/expectation.d.ts.map +1 -0
  51. package/dist/expectation.js +70 -0
  52. package/dist/failure.d.ts +13 -0
  53. package/dist/failure.d.ts.map +1 -0
  54. package/dist/failure.js +19 -0
  55. package/dist/fence.d.ts +6 -0
  56. package/dist/fence.d.ts.map +1 -0
  57. package/dist/fence.js +10 -0
  58. package/dist/file.d.ts +2 -19
  59. package/dist/file.d.ts.map +1 -1
  60. package/dist/file.js +7 -122
  61. package/dist/fulltext.d.ts +27 -0
  62. package/dist/fulltext.d.ts.map +1 -0
  63. package/dist/fulltext.js +368 -0
  64. package/dist/host.d.ts +15 -2
  65. package/dist/host.d.ts.map +1 -1
  66. package/dist/host.js +134 -21
  67. package/dist/identity.d.ts +4 -0
  68. package/dist/identity.d.ts.map +1 -1
  69. package/dist/identity.js +16 -1
  70. package/dist/index.d.ts +19 -9
  71. package/dist/index.d.ts.map +1 -1
  72. package/dist/index.js +8 -0
  73. package/dist/live.d.ts +22 -0
  74. package/dist/live.d.ts.map +1 -0
  75. package/dist/live.js +124 -0
  76. package/dist/media.d.ts +8 -0
  77. package/dist/media.d.ts.map +1 -0
  78. package/dist/media.js +208 -0
  79. package/dist/memory.d.ts +13 -5
  80. package/dist/memory.d.ts.map +1 -1
  81. package/dist/memory.js +140 -31
  82. package/dist/messages.d.ts +28 -0
  83. package/dist/messages.d.ts.map +1 -0
  84. package/dist/messages.js +90 -0
  85. package/dist/model.d.ts +20 -4
  86. package/dist/model.d.ts.map +1 -1
  87. package/dist/model.js +19 -2
  88. package/dist/name.d.ts +4 -0
  89. package/dist/name.d.ts.map +1 -0
  90. package/dist/name.js +122 -0
  91. package/dist/observation.d.ts +20 -2
  92. package/dist/observation.d.ts.map +1 -1
  93. package/dist/observation.js +113 -12
  94. package/dist/open.d.ts +2 -0
  95. package/dist/open.d.ts.map +1 -0
  96. package/dist/open.js +1 -0
  97. package/dist/opened.d.ts +22 -0
  98. package/dist/opened.d.ts.map +1 -0
  99. package/dist/opened.js +107 -0
  100. package/dist/outcome.d.ts +73 -3
  101. package/dist/outcome.d.ts.map +1 -1
  102. package/dist/outcome.js +81 -1
  103. package/dist/overlay.d.ts +15 -5
  104. package/dist/overlay.d.ts.map +1 -1
  105. package/dist/overlay.js +136 -180
  106. package/dist/paths.d.ts +1 -1
  107. package/dist/paths.d.ts.map +1 -1
  108. package/dist/paths.js +4 -16
  109. package/dist/query.d.ts +71 -5
  110. package/dist/query.d.ts.map +1 -1
  111. package/dist/query.js +100 -26
  112. package/dist/reach.d.ts +18 -4
  113. package/dist/reach.d.ts.map +1 -1
  114. package/dist/reach.js +247 -84
  115. package/dist/react.d.ts +31 -19
  116. package/dist/react.d.ts.map +1 -1
  117. package/dist/react.js +13 -21
  118. package/dist/readiness.d.ts +64 -0
  119. package/dist/readiness.d.ts.map +1 -0
  120. package/dist/readiness.js +33 -0
  121. package/dist/reads.d.ts +11 -0
  122. package/dist/reads.d.ts.map +1 -0
  123. package/dist/reads.js +100 -0
  124. package/dist/recovery.d.ts +25 -0
  125. package/dist/recovery.d.ts.map +1 -0
  126. package/dist/recovery.js +198 -0
  127. package/dist/refresh.d.ts +19 -8
  128. package/dist/refresh.d.ts.map +1 -1
  129. package/dist/refresh.js +125 -57
  130. package/dist/replica.d.ts +59 -33
  131. package/dist/replica.d.ts.map +1 -1
  132. package/dist/replica.js +322 -155
  133. package/dist/room-draft.d.ts +41 -9
  134. package/dist/room-draft.d.ts.map +1 -1
  135. package/dist/room-draft.js +186 -37
  136. package/dist/rows.d.ts +39 -34
  137. package/dist/rows.d.ts.map +1 -1
  138. package/dist/rows.js +197 -80
  139. package/dist/save.d.ts +74 -4
  140. package/dist/save.d.ts.map +1 -1
  141. package/dist/save.js +900 -178
  142. package/dist/search.d.ts +41 -29
  143. package/dist/search.d.ts.map +1 -1
  144. package/dist/search.js +37 -105
  145. package/dist/seed.d.ts +16 -0
  146. package/dist/seed.d.ts.map +1 -0
  147. package/dist/seed.js +75 -0
  148. package/dist/shared.d.ts +16 -0
  149. package/dist/shared.d.ts.map +1 -0
  150. package/dist/shared.js +431 -0
  151. package/dist/status.d.ts +11 -2
  152. package/dist/status.d.ts.map +1 -1
  153. package/dist/status.js +3 -9
  154. package/dist/store.d.ts +132 -58
  155. package/dist/store.d.ts.map +1 -1
  156. package/dist/store.js +379 -344
  157. package/dist/stored.d.ts +154 -0
  158. package/dist/stored.d.ts.map +1 -0
  159. package/dist/stored.js +578 -0
  160. package/dist/tables.d.ts +362 -0
  161. package/dist/tables.d.ts.map +1 -0
  162. package/dist/tables.js +627 -0
  163. package/dist/turn.d.ts +9 -0
  164. package/dist/turn.d.ts.map +1 -0
  165. package/dist/turn.js +75 -0
  166. package/dist/validation.d.ts +30 -0
  167. package/dist/validation.d.ts.map +1 -0
  168. package/dist/validation.js +162 -0
  169. package/dist/windows.d.ts +22 -0
  170. package/dist/windows.d.ts.map +1 -0
  171. package/dist/windows.js +89 -0
  172. package/dist/wire.d.ts +173 -33
  173. package/dist/wire.d.ts.map +1 -1
  174. package/dist/wire.js +69 -31
  175. package/dist/words.d.ts +19 -0
  176. package/dist/words.d.ts.map +1 -0
  177. package/dist/words.js +156 -0
  178. package/package.json +26 -15
  179. package/AGENTS.md +0 -231
  180. package/dist/attachment.d.ts +0 -3
  181. package/dist/attachment.d.ts.map +0 -1
  182. package/dist/attachment.js +0 -8
  183. package/dist/read-side.d.ts +0 -18
  184. package/dist/read-side.d.ts.map +0 -1
  185. package/dist/read-side.js +0 -377
package/dist/replica.js CHANGED
@@ -1,158 +1,199 @@
1
- import { parse } from '@agentshouse/mdmodel';
2
- import { immutable } from './change.js';
3
- import { parseCollection, readCollection, validateCollection, } from './collection.js';
4
- import { overlay, overlayChanged, sameLayerText } from './overlay.js';
5
- import { providerRewritten } from './save.js';
6
- import { equalValue } from './room-draft.js';
7
- import { declaredAttachments } from './attachment.js';
8
- export function compositionOf(record) {
1
+ import { equalValue, parse } from '@agentshouse/mdmodel';
2
+ import { immutable, landing } from './change.js';
3
+ import { collectionOf } from './collection.js';
4
+ import { storedModel } from './model.js';
5
+ import { acceptedBinaries, overlaying } from './overlay.js';
6
+ import { effectiveIn, rewriting } from './save.js';
7
+ import { ROW_WEIGHT } from './turn.js';
8
+ export function compositionOf(record, builtIn) {
9
9
  return {
10
10
  model: record.model,
11
11
  selection: record.typedSelection === null
12
12
  ? null
13
- : { roots: record.typedSelection.roots, model: record.selectionModel },
13
+ : { roots: record.typedSelection.roots, model: builtIn },
14
14
  };
15
15
  }
16
- export function readConfirmed(record, previous) {
17
- const read = readCollection(record.documents, compositionOf(record), previous === undefined
18
- ? undefined
19
- : { text: previous.record.documents, collection: previous.collection });
20
- return read.kind === 'read'
21
- ? { kind: 'staged', confirmed: { record, collection: read.collection } }
22
- : read;
23
- }
24
16
  export function sameMembership(record, other) {
25
17
  return (JSON.stringify(record.typedSelection?.roots ?? null) ===
26
18
  JSON.stringify(other.typedSelection?.roots ?? null));
27
19
  }
28
- export function readStaged(record, previous) {
29
- if ((record.model.status !== 'unavailable' || record.selectionModel.status !== 'unavailable') &&
30
- (previous === undefined ||
31
- !sameLayerText(record, previous.record) ||
32
- record.model !== previous.record.model ||
33
- record.selectionModel !== previous.record.selectionModel)) {
34
- const verdict = validateCollection(record.documents, record.binaryTargets, compositionOf(record));
35
- if (verdict.verdict === 'refuse' && verdict.rule !== 'model-unavailable') {
36
- return { kind: 'refused', refusal: verdict };
37
- }
20
+ export function storedRecord(record) {
21
+ const { key: _key, ...stored } = record;
22
+ return JSON.stringify({ ...stored, model: storedModel(stored.model) });
23
+ }
24
+ const PROBES = new Set([
25
+ 'then',
26
+ 'toJSON',
27
+ 'constructor',
28
+ 'valueOf',
29
+ 'toString',
30
+ ]);
31
+ class UnreadPath extends Error {
32
+ constructor(path) {
33
+ super(`the replica slice holds no row for ${path}`);
38
34
  }
39
- return readConfirmed(record, previous);
40
35
  }
41
- export function readReclassified(record, previous) {
42
- const verdict = validateCollection(record.documents, record.binaryTargets, compositionOf(record));
43
- if (verdict.verdict !== 'refuse')
44
- return readConfirmed(record, previous);
45
- return verdict.rule === 'model-unavailable'
46
- ? { kind: 'deferred' }
47
- : { kind: 'refused', refusal: verdict };
36
+ function strict(values, held) {
37
+ const guard = (property) => {
38
+ if (typeof property === 'string' && !held.has(property) && !PROBES.has(property)) {
39
+ throw new UnreadPath(property);
40
+ }
41
+ };
42
+ return new Proxy(values, {
43
+ get(target, property, receiver) {
44
+ guard(property);
45
+ return Reflect.get(target, property, receiver);
46
+ },
47
+ has(target, property) {
48
+ guard(property);
49
+ return Reflect.has(target, property);
50
+ },
51
+ getOwnPropertyDescriptor(target, property) {
52
+ guard(property);
53
+ return Reflect.getOwnPropertyDescriptor(target, property);
54
+ },
55
+ });
48
56
  }
49
- export function applyReturned(confirmed, documents, revisions, removals, attachments, roomVersion, writtenAt) {
50
- const nextDocuments = { ...confirmed.record.documents, ...documents };
51
- const nextRevisions = { ...confirmed.record.revisions, ...revisions };
52
- const nextChanges = { ...confirmed.record.changes };
53
- const nextWritten = {
54
- ...confirmed.record.written,
55
- ...Object.fromEntries(Object.keys(documents).map((path) => [path, writtenAt])),
57
+ export function* slicing(entries, paths) {
58
+ const held = new Set(paths);
59
+ for (const entry of entries)
60
+ held.add(entry.path);
61
+ const documents = {};
62
+ const binaries = {};
63
+ const revisions = {};
64
+ const fileIds = {};
65
+ const changes = {};
66
+ const written = {};
67
+ for (const entry of entries) {
68
+ if (entry.binary !== null)
69
+ binaries[entry.path] = entry.binary;
70
+ else if (entry.content !== null)
71
+ documents[entry.path] = entry.content;
72
+ if (entry.revision !== null)
73
+ revisions[entry.path] = entry.revision;
74
+ if (entry.fileId !== null)
75
+ fileIds[entry.path] = entry.fileId;
76
+ if (entry.change !== null)
77
+ changes[entry.path] = entry.change;
78
+ if (entry.written !== null)
79
+ written[entry.path] = entry.written;
80
+ yield ROW_WEIGHT;
81
+ }
82
+ return {
83
+ documents: strict(documents, held),
84
+ binaries: strict(binaries, held),
85
+ revisions: strict(revisions, held),
86
+ fileIds: strict(fileIds, held),
87
+ changes: strict(changes, held),
88
+ written: strict(written, held),
89
+ held,
56
90
  };
57
- const binaryTargets = new Set(confirmed.record.binaryTargets);
58
- for (const [path, revision] of Object.entries(revisions)) {
59
- if (confirmed.record.revisions[path] !== revision)
60
- delete nextChanges[path];
91
+ }
92
+ export function entryAt(layer, path) {
93
+ const content = layer.documents[path];
94
+ const binary = layer.binaries[path];
95
+ if (content === undefined && binary === undefined)
96
+ return null;
97
+ return {
98
+ path,
99
+ content: content ?? null,
100
+ binary: binary ?? null,
101
+ revision: layer.revisions[path] ?? null,
102
+ fileId: layer.fileIds[path] ?? null,
103
+ change: layer.changes[path] ?? null,
104
+ written: layer.written?.[path] ?? null,
105
+ };
106
+ }
107
+ export function* slicingWith(slice, set, removed) {
108
+ const entries = new Map();
109
+ for (const path of slice.held) {
110
+ const entry = entryAt(slice, path);
111
+ if (entry !== null)
112
+ entries.set(path, { ...entry, type: null });
113
+ yield ROW_WEIGHT;
61
114
  }
62
- for (const path of removals) {
63
- delete nextDocuments[path];
64
- delete nextRevisions[path];
65
- delete nextChanges[path];
66
- delete nextWritten[path];
67
- binaryTargets.delete(path);
115
+ for (const path of removed)
116
+ entries.delete(path);
117
+ for (const entry of set)
118
+ entries.set(entry.path, { ...entry, type: null });
119
+ return yield* slicing([...entries.values()], [...slice.held, ...set.map(({ path }) => path), ...removed]);
120
+ }
121
+ function changePaths(change) {
122
+ return change.kind === 'move' ? [change.path, change.to] : [change.path];
123
+ }
124
+ export function touchedBy(queue) {
125
+ const paths = new Set();
126
+ for (const batch of queue) {
127
+ for (const change of batch.changes)
128
+ for (const path of changePaths(change))
129
+ paths.add(path);
130
+ for (const precondition of batch.preconditions)
131
+ paths.add(precondition.path);
132
+ for (const move of batch.between?.moves ?? [])
133
+ paths.add(move.path);
68
134
  }
69
- return readStaged({
70
- ...confirmed.record,
71
- roomVersion,
72
- documents: nextDocuments,
73
- revisions: nextRevisions,
74
- changes: nextChanges,
75
- written: nextWritten,
76
- binaryTargets: [...binaryTargets].sort(),
77
- attachments: attachments === undefined
78
- ? confirmed.record.attachments
79
- : declaredAttachments([attachments]),
80
- }, confirmed);
135
+ return paths;
81
136
  }
82
- function predicted(confirmed, batches, previous) {
83
- const composition = compositionOf(confirmed.record);
84
- let layer = confirmed.record;
85
- const replay = [];
86
- const prior = new Map(previous?.replay.map((step) => [step.id, step]) ?? []);
87
- for (const batch of batches) {
88
- const before = layer;
89
- const old = prior.get(batch.id);
90
- const hasMove = batch.changes.some((change) => change.kind === 'move');
91
- const reuseRewrite = old?.collection !== undefined &&
92
- sameLayerText(before, old.before) &&
93
- old.collection.composition.model === composition.model &&
94
- old.collection.composition.selection?.model === composition.selection?.model &&
95
- JSON.stringify(old.collection.composition.selection?.roots) ===
96
- JSON.stringify(composition.selection?.roots);
97
- const collection = hasMove
98
- ? reuseRewrite
99
- ? old.collection
100
- : parseCollection(before.documents, composition, old?.collection === undefined
101
- ? { text: confirmed.record.documents, collection: confirmed.collection }
102
- : { text: old.before.documents, collection: old.collection })
103
- : undefined;
104
- const effective = collection === undefined
105
- ? batch
106
- : reuseRewrite
107
- ? old.effective
108
- : providerRewritten(batch, { ...before, collection });
109
- layer =
110
- old === undefined
111
- ? overlay(before, [effective])
112
- : overlayChanged(before, [effective], old.before, [old.effective], old.after);
113
- replay.push({
114
- id: batch.id,
115
- before,
116
- after: layer,
117
- effective,
118
- ...(collection === undefined ? {} : { collection }),
119
- });
137
+ export function movedBy(queue, locator) {
138
+ const paths = new Set();
139
+ for (const batch of queue) {
140
+ if (!rewriting(batch, locator))
141
+ continue;
142
+ for (const change of batch.changes)
143
+ if (change.kind === 'move')
144
+ paths.add(change.path);
145
+ if (batch.between?.source.room === locator) {
146
+ for (const move of batch.between.moves)
147
+ paths.add(move.path);
148
+ }
120
149
  }
121
- return { layer, replay };
150
+ return paths;
122
151
  }
123
- export function replicaOf(confirmed, queue, previous) {
124
- const batches = Object.freeze(queue.map(immutable));
125
- if (batches.length === 0) {
126
- return {
127
- confirmed,
128
- queue: batches,
129
- visible: {
130
- documents: confirmed.record.documents,
131
- binaryTargets: confirmed.record.binaryTargets,
132
- revisions: confirmed.record.revisions,
133
- changes: confirmed.record.changes,
134
- collection: confirmed.collection,
135
- },
136
- replay: [],
137
- };
152
+ export function* replaying(confirmed, queue, locator, composition) {
153
+ let layer = confirmed;
154
+ const touched = new Set();
155
+ for (const batch of queue) {
156
+ const effective = rewriting(batch, locator)
157
+ ? yield* effectiveIn(batch, locator, {
158
+ ...layer,
159
+ collection: collectionOf(layer.documents, composition),
160
+ })
161
+ : batch;
162
+ for (const change of effective.changes)
163
+ for (const path of changePaths(change))
164
+ touched.add(path);
165
+ layer = yield* overlaying(layer, [effective]);
166
+ yield ROW_WEIGHT;
138
167
  }
139
- const { layer, replay } = predicted(confirmed, batches, previous);
140
168
  return {
141
- confirmed,
142
- queue: batches,
143
- replay,
144
- visible: {
145
- ...layer,
146
- collection: parseCollection(layer.documents, compositionOf(confirmed.record), previous === undefined
147
- ? undefined
148
- : { text: previous.visible.documents, collection: previous.visible.collection }),
149
- },
169
+ visible: { ...layer, collection: collectionOf(layer.documents, composition) },
170
+ touched,
150
171
  };
151
172
  }
173
+ export function* overlayOf(visible, touched) {
174
+ const rows = [];
175
+ for (const path of touched) {
176
+ yield ROW_WEIGHT;
177
+ const entry = entryAt(visible, path);
178
+ rows.push(entry === null
179
+ ? {
180
+ path,
181
+ present: false,
182
+ content: null,
183
+ binary: null,
184
+ revision: null,
185
+ fileId: null,
186
+ change: null,
187
+ written: null,
188
+ }
189
+ : { ...entry, written: null, present: true });
190
+ }
191
+ return rows;
192
+ }
152
193
  function present(layer, path) {
153
- return layer.documents[path] !== undefined || layer.binaryTargets.includes(path);
194
+ return layer.documents[path] !== undefined || layer.binaries[path] !== undefined;
154
195
  }
155
- function holds(layer, preconditions) {
196
+ export function holds(layer, preconditions) {
156
197
  const parsed = new Map();
157
198
  const documentAt = (path) => {
158
199
  if (!parsed.has(path)) {
@@ -171,6 +212,8 @@ function holds(layer, preconditions) {
171
212
  if (precondition.kind === 'revision') {
172
213
  return present(layer, path) && layer.revisions[path] === precondition.revision;
173
214
  }
215
+ if (precondition.kind === 'content')
216
+ return layer.documents[path] === precondition.content;
174
217
  const document = documentAt(path);
175
218
  if (document === null)
176
219
  return false;
@@ -215,53 +258,177 @@ function effects(change) {
215
258
  { kind: 'present', path: change.to },
216
259
  ];
217
260
  }
261
+ if (change.kind === 'binary' || change.kind === 'arrival')
262
+ return [{ kind: 'present', path }];
218
263
  return [{ kind: 'absent', path }];
219
264
  }
220
- function after(changes, index, path) {
221
- let at = path;
222
- for (const change of changes.slice(index + 1)) {
223
- if (change.path !== at)
224
- continue;
225
- if (change.kind === 'remove')
226
- return null;
227
- if (change.kind === 'move')
228
- at = change.to;
229
- }
230
- return at;
231
- }
232
- export function landed(batch, confirmed) {
233
- const layer = confirmed.record;
265
+ export function landed(batch, confirmed, current = {}) {
266
+ const documents = new Proxy(confirmed.documents, {
267
+ get(target, property, receiver) {
268
+ if (typeof property === 'string' && Object.hasOwn(current, property))
269
+ return current[property];
270
+ return Reflect.get(target, property, receiver);
271
+ },
272
+ });
273
+ const layer = { ...confirmed, documents };
234
274
  return batch.changes.every((change, index) => {
235
275
  if (change.kind === 'replace' || (change.kind === 'create' && 'content' in change)) {
236
- const at = after(batch.changes, index, change.path);
276
+ const at = landing(batch.changes, index, change.path);
237
277
  return at === null || layer.documents[at] === change.content;
238
278
  }
279
+ if (change.kind === 'binary') {
280
+ const at = landing(batch.changes, index, change.path);
281
+ return at === null || layer.binaries[at]?.sha256 === change.sha256;
282
+ }
239
283
  return effects(change).every((effect) => {
240
284
  if (effect.kind === 'absent')
241
285
  return holds(layer, [effect]);
242
- const at = after(batch.changes, index, effect.path);
286
+ const at = landing(batch.changes, index, effect.path);
243
287
  return at === null || holds(layer, [{ ...effect, path: at }]);
244
288
  });
245
289
  });
246
290
  }
247
- export function withdrawals(confirmed, queue, withdrawable) {
291
+ function founded(batch, layer) {
292
+ let changed = false;
293
+ const preconditions = batch.preconditions.map((precondition) => {
294
+ if (precondition.kind !== 'content')
295
+ return precondition;
296
+ const revision = layer.revisions[precondition.path];
297
+ if (revision === undefined || layer.documents[precondition.path] !== precondition.content) {
298
+ return precondition;
299
+ }
300
+ changed = true;
301
+ return { kind: 'revision', path: precondition.path, revision };
302
+ });
303
+ return changed ? immutable({ ...batch, preconditions }) : batch;
304
+ }
305
+ export function* standing(confirmed, queue, withdrawable, local, locator, composition) {
248
306
  if (!queue.some(withdrawable))
249
- return [];
250
- const composition = compositionOf(confirmed.record);
251
- let layer = confirmed.record;
307
+ return { withdrawn: [], kept: queue };
308
+ let layer = confirmed;
252
309
  const withdrawn = [];
310
+ const kept = [];
253
311
  for (const batch of queue) {
254
312
  if (withdrawable(batch) && !holds(layer, batch.preconditions)) {
255
313
  withdrawn.push(batch);
256
314
  continue;
257
315
  }
258
- const effective = batch.changes.some((change) => change.kind === 'move')
259
- ? providerRewritten(batch, {
316
+ const effective = rewriting(batch, locator)
317
+ ? yield* effectiveIn(batch, locator, {
260
318
  ...layer,
261
- collection: parseCollection(layer.documents, composition),
319
+ collection: collectionOf(layer.documents, composition),
262
320
  })
263
321
  : batch;
264
- layer = overlay(layer, [effective]);
322
+ let unapplied = false;
323
+ const next = yield* overlaying(layer, [effective], () => {
324
+ unapplied = true;
325
+ });
326
+ if (local && unapplied) {
327
+ withdrawn.push(batch);
328
+ continue;
329
+ }
330
+ kept.push(local || !withdrawable(batch) ? batch : founded(batch, layer));
331
+ layer = next;
332
+ }
333
+ return { withdrawn, kept };
334
+ }
335
+ export function returnedPaths(submitted, accepted) {
336
+ const paths = new Set([
337
+ ...Object.keys(accepted.documents),
338
+ ...Object.keys(accepted.revisions),
339
+ ...Object.keys(accepted.fileIds ?? {}),
340
+ ...Object.keys(accepted.changes),
341
+ ...Object.keys(accepted.binaryFiles ?? {}),
342
+ ...accepted.removals,
343
+ ]);
344
+ for (const change of submitted.changes) {
345
+ if (change.kind === 'move') {
346
+ paths.add(change.path);
347
+ paths.add(change.to);
348
+ }
349
+ if (change.kind === 'arrival')
350
+ paths.add(change.path);
351
+ }
352
+ return paths;
353
+ }
354
+ export function* returning(confirmed, submitted, accepted, writtenAt) {
355
+ const documents = new Map();
356
+ const binaries = new Map();
357
+ const revisions = new Map();
358
+ const fileIds = new Map();
359
+ const changes = new Map();
360
+ const written = new Map();
361
+ const read = (changed, held, path) => (changed.has(path) ? changed.get(path) : held[path]);
362
+ for (const [path, content] of Object.entries(accepted.documents)) {
363
+ yield ROW_WEIGHT;
364
+ documents.set(path, content);
365
+ fileIds.set(path, undefined);
366
+ written.set(path, writtenAt);
367
+ }
368
+ for (const [path, revision] of Object.entries(accepted.revisions)) {
369
+ yield ROW_WEIGHT;
370
+ revisions.set(path, revision);
371
+ fileIds.set(path, undefined);
372
+ if (confirmed.revisions[path] !== revision)
373
+ changes.set(path, undefined);
374
+ }
375
+ for (const [path, fileId] of Object.entries(accepted.fileIds ?? {}))
376
+ fileIds.set(path, fileId);
377
+ for (const [path, change] of Object.entries(accepted.changes))
378
+ changes.set(path, change);
379
+ for (const path of accepted.removals) {
380
+ documents.set(path, undefined);
381
+ revisions.set(path, undefined);
382
+ fileIds.set(path, undefined);
383
+ changes.set(path, undefined);
384
+ written.set(path, undefined);
385
+ }
386
+ for (const change of submitted.changes) {
387
+ if (change.kind === 'arrival' && 'binary' in change) {
388
+ if (accepted.revisions[change.path] !== undefined) {
389
+ binaries.set(change.path, { state: 'accepted', ...change.binary });
390
+ }
391
+ continue;
392
+ }
393
+ if (change.kind !== 'move')
394
+ continue;
395
+ const moved = read(binaries, confirmed.binaries, change.path);
396
+ if (moved === undefined || accepted.revisions[change.to] === undefined)
397
+ continue;
398
+ binaries.set(change.to, moved);
399
+ binaries.set(change.path, undefined);
400
+ }
401
+ for (const [path, binary] of acceptedBinaries(accepted.binaryFiles ?? {}))
402
+ binaries.set(path, binary);
403
+ for (const path of accepted.removals)
404
+ binaries.set(path, undefined);
405
+ const paths = new Set([
406
+ ...documents.keys(),
407
+ ...binaries.keys(),
408
+ ...revisions.keys(),
409
+ ...fileIds.keys(),
410
+ ...changes.keys(),
411
+ ...written.keys(),
412
+ ]);
413
+ const set = [];
414
+ const removed = [];
415
+ for (const path of paths) {
416
+ yield ROW_WEIGHT;
417
+ const content = read(documents, confirmed.documents, path);
418
+ const binary = read(binaries, confirmed.binaries, path);
419
+ if (content === undefined && binary === undefined) {
420
+ removed.push(path);
421
+ continue;
422
+ }
423
+ set.push({
424
+ path,
425
+ content: binary === undefined ? (content ?? null) : null,
426
+ binary: binary ?? null,
427
+ revision: read(revisions, confirmed.revisions, path) ?? null,
428
+ fileId: read(fileIds, confirmed.fileIds, path) ?? null,
429
+ change: read(changes, confirmed.changes, path) ?? null,
430
+ written: read(written, confirmed.written, path) ?? null,
431
+ });
265
432
  }
266
- return withdrawn;
433
+ return { set, removed };
267
434
  }
@@ -1,4 +1,5 @@
1
1
  import type { ProviderRefusal } from './adapter.js';
2
+ import type { Open } from './open.js';
2
3
  import type { Change, Precondition } from './change.js';
3
4
  import type { VisibleLayer } from './replica.js';
4
5
  export type FieldValue = {
@@ -31,11 +32,23 @@ export type RoomDraftParts = {
31
32
  readonly creation?: RoomDraftCreation;
32
33
  readonly move?: {
33
34
  readonly to: string;
35
+ readonly room?: string;
34
36
  readonly base?: string | null;
35
37
  };
36
38
  readonly removal?: {
37
39
  readonly base?: string | null;
38
40
  };
41
+ readonly file?: {
42
+ readonly blob: Blob;
43
+ };
44
+ };
45
+ export type HeldFile = {
46
+ readonly localId: string;
47
+ readonly name?: string;
48
+ readonly byteLength: number;
49
+ readonly mediaType: string;
50
+ readonly sha256: string;
51
+ readonly base: string | null;
39
52
  };
40
53
  type HeldParts = {
41
54
  readonly fields?: Readonly<Record<string, {
@@ -57,11 +70,13 @@ type HeldParts = {
57
70
  readonly creation?: RoomDraftCreation;
58
71
  readonly move?: {
59
72
  readonly to: string;
73
+ readonly room?: string;
60
74
  readonly base: string | null;
61
75
  };
62
76
  readonly removal?: {
63
77
  readonly base: string | null;
64
78
  };
79
+ readonly file?: HeldFile;
65
80
  };
66
81
  type ChangedUnderneath = {
67
82
  readonly fields?: Readonly<Record<string, FieldValue>>;
@@ -71,14 +86,12 @@ type ChangedUnderneath = {
71
86
  readonly creation?: string;
72
87
  readonly move?: string | null;
73
88
  readonly removal?: string | null;
89
+ readonly file?: string | null;
74
90
  };
75
- export type ReturnReason = (ProviderRefusal & {
76
- readonly kind: 'conflicted' | 'rejected';
77
- }) | {
78
- readonly kind: 'expired';
79
- } | {
80
- readonly kind: 'withdrawn';
91
+ export type ReturnReason = ProviderRefusal & {
92
+ readonly kind: Open<'conflicted' | 'rejected' | 'expired' | 'withdrawn'>;
81
93
  };
94
+ export declare function returnedBy(kind: 'expired' | 'withdrawn'): ReturnReason;
82
95
  export type RoomDraft = {
83
96
  readonly room: string;
84
97
  readonly path: string;
@@ -92,11 +105,14 @@ export type RoomDraftWrite = {
92
105
  readonly roomDraft: RoomDraft | null;
93
106
  } | {
94
107
  readonly outcome: 'stale';
108
+ readonly code: string;
109
+ readonly retryable: boolean;
95
110
  readonly current: RoomDraft | null;
96
111
  };
112
+ export declare function staleWrite(current: RoomDraft | null): RoomDraftWrite;
97
113
  export type ChangedPart = {
98
114
  readonly path: string;
99
- readonly part: 'field' | 'section' | 'preamble' | 'markdown' | 'creation' | 'move' | 'removal';
115
+ readonly part: Open<'field' | 'section' | 'preamble' | 'markdown' | 'creation' | 'move' | 'removal' | 'file'>;
100
116
  readonly name?: string;
101
117
  };
102
118
  export type RoomDraftRecord = {
@@ -111,10 +127,24 @@ export type SavedDraft = {
111
127
  readonly parts: HeldParts;
112
128
  };
113
129
  export declare function readParts(value: unknown): RoomDraftParts;
114
- export declare function held(current: HeldParts | undefined, parts: RoomDraftParts): HeldParts;
115
- export declare function equalValue(left: unknown, right: unknown): boolean;
130
+ export declare function gatheredParts(earlier: unknown, later: unknown): unknown;
131
+ export declare function held(current: HeldParts | undefined, parts: RoomDraftParts, file?: HeldFile): HeldParts;
116
132
  export declare function underneath(parts: HeldParts, path: string, visible: VisibleLayer | undefined): ChangedUnderneath;
133
+ export declare function heldFile(blob: Blob, base: string | null): Promise<HeldFile>;
134
+ export declare function binaryBase(visible: VisibleLayer | undefined, path: string): string | null;
117
135
  export declare function changedParts(path: string, report: ChangedUnderneath): readonly ChangedPart[];
136
+ export type TextParts = {
137
+ readonly fields?: Readonly<Record<string, {
138
+ readonly value: unknown;
139
+ }>>;
140
+ readonly sections?: Readonly<Record<string, {
141
+ readonly value: string;
142
+ }>>;
143
+ readonly preamble?: {
144
+ readonly value: string;
145
+ };
146
+ };
147
+ export declare function partsAfter(markdown: string, parts: TextParts): string;
118
148
  export declare function returnedInto(existing: HeldParts | undefined, returned: HeldParts): HeldParts;
119
149
  export declare function moveTarget(move: {
120
150
  readonly to: string;
@@ -123,5 +153,7 @@ export declare function changesOf(path: string, parts: HeldParts): {
123
153
  changes: readonly Change[];
124
154
  bases: readonly Precondition[];
125
155
  };
156
+ export declare function joinedDraft(carried: SavedDraft, saved: readonly SavedDraft[]): SavedDraft;
157
+ export declare function withdrawnDrafts(drafts: readonly SavedDraft[], changes: readonly Change[]): readonly SavedDraft[];
126
158
  export {};
127
159
  //# sourceMappingURL=room-draft.d.ts.map