@agentshouse/mdruntime 0.1.0-cbb8d8acc9d19 → 0.1.0-cbe358f60d1d3

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 (184) hide show
  1. package/README.md +24 -11
  2. package/dist/adapter.d.ts +177 -51
  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 +38 -4
  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 +12 -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 +42 -17
  26. package/dist/client.d.ts.map +1 -1
  27. package/dist/client.js +158 -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 +11 -0
  32. package/dist/compile.d.ts.map +1 -0
  33. package/dist/compile.js +586 -0
  34. package/dist/connection.d.ts +63 -21
  35. package/dist/connection.d.ts.map +1 -1
  36. package/dist/connection.js +3827 -896
  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 +50 -20
  41. package/dist/conversation.d.ts.map +1 -1
  42. package/dist/conversation.js +714 -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 +10 -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 +159 -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 -3
  65. package/dist/host.d.ts.map +1 -1
  66. package/dist/host.js +132 -25
  67. package/dist/identity.d.ts +3 -0
  68. package/dist/identity.d.ts.map +1 -1
  69. package/dist/identity.js +13 -1
  70. package/dist/index.d.ts +24 -10
  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 +252 -98
  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 +13 -6
  89. package/dist/model.d.ts.map +1 -1
  90. package/dist/model.js +8 -5
  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/query.d.ts +42 -181
  113. package/dist/query.d.ts.map +1 -1
  114. package/dist/query.js +3 -461
  115. package/dist/reach.d.ts +18 -4
  116. package/dist/reach.d.ts.map +1 -1
  117. package/dist/reach.js +236 -69
  118. package/dist/react.d.ts +33 -20
  119. package/dist/react.d.ts.map +1 -1
  120. package/dist/react.js +13 -21
  121. package/dist/readiness.d.ts +50 -0
  122. package/dist/readiness.d.ts.map +1 -0
  123. package/dist/readiness.js +27 -0
  124. package/dist/recovery.d.ts +26 -0
  125. package/dist/recovery.d.ts.map +1 -0
  126. package/dist/recovery.js +224 -0
  127. package/dist/refresh.d.ts +20 -7
  128. package/dist/refresh.d.ts.map +1 -1
  129. package/dist/refresh.js +148 -59
  130. package/dist/replica.d.ts +59 -33
  131. package/dist/replica.d.ts.map +1 -1
  132. package/dist/replica.js +320 -156
  133. package/dist/room-draft.d.ts +59 -11
  134. package/dist/room-draft.d.ts.map +1 -1
  135. package/dist/room-draft.js +276 -80
  136. package/dist/rows.d.ts +23 -35
  137. package/dist/rows.d.ts.map +1 -1
  138. package/dist/rows.js +61 -117
  139. package/dist/save.d.ts +73 -4
  140. package/dist/save.d.ts.map +1 -1
  141. package/dist/save.js +825 -150
  142. package/dist/search.d.ts +15 -54
  143. package/dist/search.d.ts.map +1 -1
  144. package/dist/search.js +10 -121
  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 +15 -0
  149. package/dist/shared.d.ts.map +1 -0
  150. package/dist/shared.js +421 -0
  151. package/dist/status.d.ts +7 -2
  152. package/dist/status.d.ts.map +1 -1
  153. package/dist/status.js +3 -9
  154. package/dist/store.d.ts +107 -60
  155. package/dist/store.d.ts.map +1 -1
  156. package/dist/store.js +319 -344
  157. package/dist/stored.d.ts +129 -0
  158. package/dist/stored.d.ts.map +1 -0
  159. package/dist/stored.js +495 -0
  160. package/dist/tables.d.ts +328 -0
  161. package/dist/tables.d.ts.map +1 -0
  162. package/dist/tables.js +587 -0
  163. package/dist/transfer.d.ts +0 -5
  164. package/dist/transfer.d.ts.map +1 -1
  165. package/dist/turn.d.ts +9 -0
  166. package/dist/turn.d.ts.map +1 -0
  167. package/dist/turn.js +75 -0
  168. package/dist/validation.d.ts +29 -0
  169. package/dist/validation.d.ts.map +1 -0
  170. package/dist/validation.js +104 -0
  171. package/dist/windows.d.ts +11 -0
  172. package/dist/windows.d.ts.map +1 -0
  173. package/dist/windows.js +16 -0
  174. package/dist/wire.d.ts +178 -33
  175. package/dist/wire.d.ts.map +1 -1
  176. package/dist/wire.js +71 -31
  177. package/package.json +26 -15
  178. package/AGENTS.md +0 -244
  179. package/dist/attachment.d.ts +0 -3
  180. package/dist/attachment.d.ts.map +0 -1
  181. package/dist/attachment.js +0 -8
  182. package/dist/read-side.d.ts +0 -18
  183. package/dist/read-side.d.ts.map +0 -1
  184. package/dist/read-side.js +0 -433
package/dist/replica.js CHANGED
@@ -1,158 +1,196 @@
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
+ binary: binary ?? null,
98
+ revision: layer.revisions[path] ?? null,
99
+ fileId: layer.fileIds[path] ?? null,
100
+ change: layer.changes[path] ?? null,
101
+ written: layer.written?.[path] ?? null,
56
102
  };
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];
103
+ }
104
+ export function* slicingWith(slice, set, removed) {
105
+ const entries = new Map();
106
+ for (const path of slice.held) {
107
+ const entry = entryAt(slice, path);
108
+ if (entry !== null)
109
+ entries.set(path, { ...entry, type: null });
110
+ yield ROW_WEIGHT;
61
111
  }
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);
112
+ for (const path of removed)
113
+ entries.delete(path);
114
+ for (const entry of set)
115
+ entries.set(entry.path, { ...entry, type: null });
116
+ return yield* slicing([...entries.values()], [...slice.held, ...set.map(({ path }) => path), ...removed]);
117
+ }
118
+ function changePaths(change) {
119
+ return change.kind === 'move' ? [change.path, change.to] : [change.path];
120
+ }
121
+ export function touchedBy(queue) {
122
+ const paths = new Set();
123
+ for (const batch of queue) {
124
+ for (const change of batch.changes)
125
+ for (const path of changePaths(change))
126
+ paths.add(path);
127
+ for (const precondition of batch.preconditions)
128
+ paths.add(precondition.path);
129
+ for (const move of batch.between?.moves ?? [])
130
+ paths.add(move.path);
68
131
  }
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);
132
+ return paths;
81
133
  }
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
- });
134
+ export function movedBy(queue, locator) {
135
+ const paths = new Set();
136
+ for (const batch of queue) {
137
+ if (!rewriting(batch, locator))
138
+ continue;
139
+ for (const change of batch.changes)
140
+ if (change.kind === 'move')
141
+ paths.add(change.path);
142
+ if (batch.between?.source.room === locator) {
143
+ for (const move of batch.between.moves)
144
+ paths.add(move.path);
145
+ }
120
146
  }
121
- return { layer, replay };
147
+ return paths;
122
148
  }
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
- };
149
+ export function* replaying(confirmed, queue, locator, composition) {
150
+ let layer = confirmed;
151
+ const touched = new Set();
152
+ for (const batch of queue) {
153
+ const effective = rewriting(batch, locator)
154
+ ? yield* effectiveIn(batch, locator, {
155
+ ...layer,
156
+ collection: collectionOf(layer.documents, composition),
157
+ })
158
+ : batch;
159
+ for (const change of effective.changes)
160
+ for (const path of changePaths(change))
161
+ touched.add(path);
162
+ layer = yield* overlaying(layer, [effective]);
163
+ yield ROW_WEIGHT;
138
164
  }
139
- const { layer, replay } = predicted(confirmed, batches, previous);
140
165
  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
- },
166
+ visible: { ...layer, collection: collectionOf(layer.documents, composition) },
167
+ touched,
150
168
  };
151
169
  }
170
+ export function* overlayOf(visible, touched) {
171
+ const rows = [];
172
+ for (const path of touched) {
173
+ yield ROW_WEIGHT;
174
+ const entry = entryAt(visible, path);
175
+ rows.push(entry === null
176
+ ? {
177
+ path,
178
+ present: false,
179
+ content: null,
180
+ binary: null,
181
+ revision: null,
182
+ fileId: null,
183
+ change: null,
184
+ written: null,
185
+ }
186
+ : { ...entry, written: null, present: true });
187
+ }
188
+ return rows;
189
+ }
152
190
  function present(layer, path) {
153
- return layer.documents[path] !== undefined || layer.binaryTargets.includes(path);
191
+ return layer.documents[path] !== undefined || layer.binaries[path] !== undefined;
154
192
  }
155
- function holds(layer, preconditions) {
193
+ export function holds(layer, preconditions) {
156
194
  const parsed = new Map();
157
195
  const documentAt = (path) => {
158
196
  if (!parsed.has(path)) {
@@ -171,6 +209,8 @@ function holds(layer, preconditions) {
171
209
  if (precondition.kind === 'revision') {
172
210
  return present(layer, path) && layer.revisions[path] === precondition.revision;
173
211
  }
212
+ if (precondition.kind === 'content')
213
+ return layer.documents[path] === precondition.content;
174
214
  const document = documentAt(path);
175
215
  if (document === null)
176
216
  return false;
@@ -215,53 +255,177 @@ function effects(change) {
215
255
  { kind: 'present', path: change.to },
216
256
  ];
217
257
  }
258
+ if (change.kind === 'binary' || change.kind === 'arrival')
259
+ return [{ kind: 'present', path }];
218
260
  return [{ kind: 'absent', path }];
219
261
  }
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
262
  export function landed(batch, confirmed, current = {}) {
233
- const layer = { ...confirmed.record, documents: { ...confirmed.record.documents, ...current } };
263
+ const documents = new Proxy(confirmed.documents, {
264
+ get(target, property, receiver) {
265
+ if (typeof property === 'string' && Object.hasOwn(current, property))
266
+ return current[property];
267
+ return Reflect.get(target, property, receiver);
268
+ },
269
+ });
270
+ const layer = { ...confirmed, documents };
234
271
  return batch.changes.every((change, index) => {
235
272
  if (change.kind === 'replace' || (change.kind === 'create' && 'content' in change)) {
236
- const at = after(batch.changes, index, change.path);
273
+ const at = landing(batch.changes, index, change.path);
237
274
  return at === null || layer.documents[at] === change.content;
238
275
  }
276
+ if (change.kind === 'binary') {
277
+ const at = landing(batch.changes, index, change.path);
278
+ return at === null || layer.binaries[at]?.sha256 === change.sha256;
279
+ }
239
280
  return effects(change).every((effect) => {
240
281
  if (effect.kind === 'absent')
241
282
  return holds(layer, [effect]);
242
- const at = after(batch.changes, index, effect.path);
283
+ const at = landing(batch.changes, index, effect.path);
243
284
  return at === null || holds(layer, [{ ...effect, path: at }]);
244
285
  });
245
286
  });
246
287
  }
247
- export function withdrawals(confirmed, queue, withdrawable) {
288
+ function founded(batch, layer) {
289
+ let changed = false;
290
+ const preconditions = batch.preconditions.map((precondition) => {
291
+ if (precondition.kind !== 'content')
292
+ return precondition;
293
+ const revision = layer.revisions[precondition.path];
294
+ if (revision === undefined || layer.documents[precondition.path] !== precondition.content) {
295
+ return precondition;
296
+ }
297
+ changed = true;
298
+ return { kind: 'revision', path: precondition.path, revision };
299
+ });
300
+ return changed ? immutable({ ...batch, preconditions }) : batch;
301
+ }
302
+ export function* standing(confirmed, queue, withdrawable, local, locator, composition) {
248
303
  if (!queue.some(withdrawable))
249
- return [];
250
- const composition = compositionOf(confirmed.record);
251
- let layer = confirmed.record;
304
+ return { withdrawn: [], kept: queue };
305
+ let layer = confirmed;
252
306
  const withdrawn = [];
307
+ const kept = [];
253
308
  for (const batch of queue) {
254
309
  if (withdrawable(batch) && !holds(layer, batch.preconditions)) {
255
310
  withdrawn.push(batch);
256
311
  continue;
257
312
  }
258
- const effective = batch.changes.some((change) => change.kind === 'move')
259
- ? providerRewritten(batch, {
313
+ const effective = rewriting(batch, locator)
314
+ ? yield* effectiveIn(batch, locator, {
260
315
  ...layer,
261
- collection: parseCollection(layer.documents, composition),
316
+ collection: collectionOf(layer.documents, composition),
262
317
  })
263
318
  : batch;
264
- layer = overlay(layer, [effective]);
319
+ let unapplied = false;
320
+ const next = yield* overlaying(layer, [effective], () => {
321
+ unapplied = true;
322
+ });
323
+ if (local && unapplied) {
324
+ withdrawn.push(batch);
325
+ continue;
326
+ }
327
+ kept.push(local || !withdrawable(batch) ? batch : founded(batch, layer));
328
+ layer = next;
329
+ }
330
+ return { withdrawn, kept };
331
+ }
332
+ export function returnedPaths(submitted, accepted) {
333
+ const paths = new Set([
334
+ ...Object.keys(accepted.documents),
335
+ ...Object.keys(accepted.revisions),
336
+ ...Object.keys(accepted.fileIds ?? {}),
337
+ ...Object.keys(accepted.changes),
338
+ ...Object.keys(accepted.binaryFiles ?? {}),
339
+ ...accepted.removals,
340
+ ]);
341
+ for (const change of submitted.changes) {
342
+ if (change.kind === 'move') {
343
+ paths.add(change.path);
344
+ paths.add(change.to);
345
+ }
346
+ if (change.kind === 'arrival')
347
+ paths.add(change.path);
348
+ }
349
+ return paths;
350
+ }
351
+ export function* returning(confirmed, submitted, accepted, writtenAt) {
352
+ const documents = new Map();
353
+ const binaries = new Map();
354
+ const revisions = new Map();
355
+ const fileIds = new Map();
356
+ const changes = new Map();
357
+ const written = new Map();
358
+ const read = (changed, held, path) => (changed.has(path) ? changed.get(path) : held[path]);
359
+ for (const [path, content] of Object.entries(accepted.documents)) {
360
+ yield ROW_WEIGHT;
361
+ documents.set(path, content);
362
+ fileIds.set(path, undefined);
363
+ written.set(path, writtenAt);
364
+ }
365
+ for (const [path, revision] of Object.entries(accepted.revisions)) {
366
+ yield ROW_WEIGHT;
367
+ revisions.set(path, revision);
368
+ fileIds.set(path, undefined);
369
+ if (confirmed.revisions[path] !== revision)
370
+ changes.set(path, undefined);
371
+ }
372
+ for (const [path, fileId] of Object.entries(accepted.fileIds ?? {}))
373
+ fileIds.set(path, fileId);
374
+ for (const [path, change] of Object.entries(accepted.changes))
375
+ changes.set(path, change);
376
+ for (const path of accepted.removals) {
377
+ documents.set(path, undefined);
378
+ revisions.set(path, undefined);
379
+ fileIds.set(path, undefined);
380
+ changes.set(path, undefined);
381
+ written.set(path, undefined);
382
+ }
383
+ for (const change of submitted.changes) {
384
+ if (change.kind === 'arrival' && 'binary' in change) {
385
+ if (accepted.revisions[change.path] !== undefined) {
386
+ binaries.set(change.path, { state: 'accepted', ...change.binary });
387
+ }
388
+ continue;
389
+ }
390
+ if (change.kind !== 'move')
391
+ continue;
392
+ const moved = read(binaries, confirmed.binaries, change.path);
393
+ if (moved === undefined || accepted.revisions[change.to] === undefined)
394
+ continue;
395
+ binaries.set(change.to, moved);
396
+ binaries.set(change.path, undefined);
397
+ }
398
+ for (const [path, binary] of acceptedBinaries(accepted.binaryFiles ?? {}))
399
+ binaries.set(path, binary);
400
+ for (const path of accepted.removals)
401
+ binaries.set(path, undefined);
402
+ const paths = new Set([
403
+ ...documents.keys(),
404
+ ...binaries.keys(),
405
+ ...revisions.keys(),
406
+ ...fileIds.keys(),
407
+ ...changes.keys(),
408
+ ...written.keys(),
409
+ ]);
410
+ const set = [];
411
+ const removed = [];
412
+ for (const path of paths) {
413
+ yield ROW_WEIGHT;
414
+ const content = read(documents, confirmed.documents, path);
415
+ const binary = read(binaries, confirmed.binaries, path);
416
+ if (content === undefined && binary === undefined) {
417
+ removed.push(path);
418
+ continue;
419
+ }
420
+ set.push({
421
+ path,
422
+ content: binary === undefined ? (content ?? null) : null,
423
+ binary: binary ?? null,
424
+ revision: read(revisions, confirmed.revisions, path) ?? null,
425
+ fileId: read(fileIds, confirmed.fileIds, path) ?? null,
426
+ change: read(changes, confirmed.changes, path) ?? null,
427
+ written: read(written, confirmed.written, path) ?? null,
428
+ });
265
429
  }
266
- return withdrawn;
430
+ return { set, removed };
267
431
  }
@@ -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' | 'withdrawn'>;
81
94
  };
95
+ export declare function returnedBy(kind: '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