@agentshouse/mdruntime 0.1.0-cfbf80d8d4644 → 0.1.0-cfcf93be6310b

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