@agentshouse/mdruntime 0.1.0-ca68e8b4bbebc → 0.1.0-ca69b8fe1fa9f

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 (233) hide show
  1. package/README.md +19 -16
  2. package/dist/adapter.d.ts +142 -70
  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/binary.d.ts +0 -5
  8. package/dist/binary.d.ts.map +1 -1
  9. package/dist/binary.js +1 -1
  10. package/dist/bootstrap.d.ts +26 -11
  11. package/dist/bootstrap.d.ts.map +1 -1
  12. package/dist/bootstrap.js +181 -103
  13. package/dist/chain.d.ts +12 -0
  14. package/dist/chain.d.ts.map +1 -0
  15. package/dist/chain.js +64 -0
  16. package/dist/change-set.d.ts +9 -16
  17. package/dist/change-set.d.ts.map +1 -1
  18. package/dist/change-set.js +11 -89
  19. package/dist/change.d.ts +31 -1
  20. package/dist/change.d.ts.map +1 -1
  21. package/dist/change.js +10 -1
  22. package/dist/client.d.ts +19 -10
  23. package/dist/client.d.ts.map +1 -1
  24. package/dist/client.js +86 -9
  25. package/dist/collection.d.ts +11 -21
  26. package/dist/collection.d.ts.map +1 -1
  27. package/dist/collection.js +39 -291
  28. package/dist/compile.d.ts +11 -0
  29. package/dist/compile.d.ts.map +1 -0
  30. package/dist/compile.js +589 -0
  31. package/dist/connection.d.ts +28 -17
  32. package/dist/connection.d.ts.map +1 -1
  33. package/dist/connection.js +3147 -1396
  34. package/dist/context.d.ts +2 -0
  35. package/dist/context.d.ts.map +1 -0
  36. package/dist/{chat/context.js → context.js} +1 -4
  37. package/dist/conversation.d.ts +29 -14
  38. package/dist/conversation.d.ts.map +1 -1
  39. package/dist/conversation.js +423 -198
  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 +2 -5
  44. package/dist/durability.d.ts.map +1 -1
  45. package/dist/durability.js +5 -0
  46. package/dist/entry.d.ts +5 -2
  47. package/dist/entry.d.ts.map +1 -1
  48. package/dist/entry.js +31 -27
  49. package/dist/file.d.ts +2 -19
  50. package/dist/file.d.ts.map +1 -1
  51. package/dist/file.js +7 -122
  52. package/dist/host.d.ts +3 -10
  53. package/dist/host.d.ts.map +1 -1
  54. package/dist/host.js +65 -39
  55. package/dist/identity.d.ts +0 -1
  56. package/dist/identity.d.ts.map +1 -1
  57. package/dist/identity.js +0 -3
  58. package/dist/index.d.ts +19 -13
  59. package/dist/index.d.ts.map +1 -1
  60. package/dist/index.js +8 -2
  61. package/dist/job.d.ts +14 -0
  62. package/dist/job.d.ts.map +1 -0
  63. package/dist/job.js +28 -0
  64. package/dist/live.d.ts +23 -0
  65. package/dist/live.d.ts.map +1 -0
  66. package/dist/live.js +127 -0
  67. package/dist/media.d.ts +8 -0
  68. package/dist/media.d.ts.map +1 -0
  69. package/dist/media.js +218 -0
  70. package/dist/memory.d.ts +12 -6
  71. package/dist/memory.d.ts.map +1 -1
  72. package/dist/memory.js +202 -103
  73. package/dist/messages.d.ts +28 -0
  74. package/dist/messages.d.ts.map +1 -0
  75. package/dist/messages.js +89 -0
  76. package/dist/model.d.ts +7 -3
  77. package/dist/model.d.ts.map +1 -1
  78. package/dist/model.js +7 -13
  79. package/dist/name.d.ts +4 -0
  80. package/dist/name.d.ts.map +1 -0
  81. package/dist/name.js +122 -0
  82. package/dist/observation.d.ts +17 -2
  83. package/dist/observation.d.ts.map +1 -1
  84. package/dist/observation.js +100 -5
  85. package/dist/opened.d.ts +22 -0
  86. package/dist/opened.d.ts.map +1 -0
  87. package/dist/opened.js +107 -0
  88. package/dist/outcome.d.ts +45 -7
  89. package/dist/outcome.d.ts.map +1 -1
  90. package/dist/outcome.js +39 -0
  91. package/dist/overlay.d.ts +2 -5
  92. package/dist/overlay.d.ts.map +1 -1
  93. package/dist/overlay.js +87 -172
  94. package/dist/paths.d.ts +1 -0
  95. package/dist/paths.d.ts.map +1 -1
  96. package/dist/paths.js +4 -0
  97. package/dist/query.d.ts +16 -190
  98. package/dist/query.d.ts.map +1 -1
  99. package/dist/query.js +3 -470
  100. package/dist/reach.d.ts +17 -8
  101. package/dist/reach.d.ts.map +1 -1
  102. package/dist/reach.js +219 -90
  103. package/dist/react.d.ts +4 -3
  104. package/dist/react.d.ts.map +1 -1
  105. package/dist/react.js +2 -14
  106. package/dist/readiness.d.ts +14 -17
  107. package/dist/readiness.d.ts.map +1 -1
  108. package/dist/readiness.js +2 -8
  109. package/dist/recovery.d.ts +3 -2
  110. package/dist/recovery.d.ts.map +1 -1
  111. package/dist/recovery.js +34 -7
  112. package/dist/refresh.d.ts +18 -15
  113. package/dist/refresh.d.ts.map +1 -1
  114. package/dist/refresh.js +155 -58
  115. package/dist/replica.d.ts +56 -33
  116. package/dist/replica.d.ts.map +1 -1
  117. package/dist/replica.js +301 -191
  118. package/dist/room-draft.d.ts +23 -4
  119. package/dist/room-draft.d.ts.map +1 -1
  120. package/dist/room-draft.js +158 -71
  121. package/dist/rows.d.ts +22 -30
  122. package/dist/rows.d.ts.map +1 -1
  123. package/dist/rows.js +63 -114
  124. package/dist/save.d.ts +53 -14
  125. package/dist/save.d.ts.map +1 -1
  126. package/dist/save.js +529 -185
  127. package/dist/search.d.ts +12 -82
  128. package/dist/search.d.ts.map +1 -1
  129. package/dist/search.js +10 -207
  130. package/dist/seed.d.ts.map +1 -1
  131. package/dist/seed.js +18 -4
  132. package/dist/shared.d.ts +7 -3
  133. package/dist/shared.d.ts.map +1 -1
  134. package/dist/shared.js +119 -66
  135. package/dist/status.d.ts +4 -3
  136. package/dist/status.d.ts.map +1 -1
  137. package/dist/status.js +3 -1
  138. package/dist/store.d.ts +92 -98
  139. package/dist/store.d.ts.map +1 -1
  140. package/dist/store.js +330 -675
  141. package/dist/stored.d.ts +162 -0
  142. package/dist/stored.d.ts.map +1 -0
  143. package/dist/stored.js +543 -0
  144. package/dist/tables.d.ts +353 -0
  145. package/dist/tables.d.ts.map +1 -0
  146. package/dist/tables.js +588 -0
  147. package/dist/transfer.d.ts +0 -5
  148. package/dist/transfer.d.ts.map +1 -1
  149. package/dist/turn.d.ts +5 -3
  150. package/dist/turn.d.ts.map +1 -1
  151. package/dist/turn.js +51 -21
  152. package/dist/validation.d.ts +29 -0
  153. package/dist/validation.d.ts.map +1 -0
  154. package/dist/validation.js +104 -0
  155. package/dist/windows.d.ts +11 -0
  156. package/dist/windows.d.ts.map +1 -0
  157. package/dist/windows.js +16 -0
  158. package/dist/wire.d.ts +34 -5
  159. package/dist/wire.d.ts.map +1 -1
  160. package/dist/wire.js +10 -2
  161. package/package.json +18 -19
  162. package/AGENTS.md +0 -299
  163. package/dist/acquisition.d.ts +0 -14
  164. package/dist/acquisition.d.ts.map +0 -1
  165. package/dist/acquisition.js +0 -24
  166. package/dist/chat/acts.d.ts +0 -33
  167. package/dist/chat/acts.d.ts.map +0 -1
  168. package/dist/chat/acts.js +0 -36
  169. package/dist/chat/context.d.ts +0 -3
  170. package/dist/chat/context.d.ts.map +0 -1
  171. package/dist/chat/input.d.ts +0 -9
  172. package/dist/chat/input.d.ts.map +0 -1
  173. package/dist/chat/input.js +0 -99
  174. package/dist/chat/kit.d.ts +0 -61
  175. package/dist/chat/kit.d.ts.map +0 -1
  176. package/dist/chat/kit.js +0 -123
  177. package/dist/chat/lasting.d.ts +0 -6
  178. package/dist/chat/lasting.d.ts.map +0 -1
  179. package/dist/chat/lasting.js +0 -19
  180. package/dist/chat/list.d.ts +0 -27
  181. package/dist/chat/list.d.ts.map +0 -1
  182. package/dist/chat/list.js +0 -63
  183. package/dist/chat/markdown.d.ts +0 -20
  184. package/dist/chat/markdown.d.ts.map +0 -1
  185. package/dist/chat/markdown.js +0 -188
  186. package/dist/chat/message.d.ts +0 -6
  187. package/dist/chat/message.d.ts.map +0 -1
  188. package/dist/chat/message.js +0 -192
  189. package/dist/chat/messages.d.ts +0 -22
  190. package/dist/chat/messages.d.ts.map +0 -1
  191. package/dist/chat/messages.js +0 -155
  192. package/dist/chat/name.d.ts +0 -6
  193. package/dist/chat/name.d.ts.map +0 -1
  194. package/dist/chat/name.js +0 -37
  195. package/dist/chat/opened.d.ts +0 -11
  196. package/dist/chat/opened.d.ts.map +0 -1
  197. package/dist/chat/opened.js +0 -17
  198. package/dist/chat/overlay.d.ts +0 -31
  199. package/dist/chat/overlay.d.ts.map +0 -1
  200. package/dist/chat/overlay.js +0 -97
  201. package/dist/chat/parts.d.ts +0 -57
  202. package/dist/chat/parts.d.ts.map +0 -1
  203. package/dist/chat/parts.js +0 -119
  204. package/dist/chat/process.d.ts +0 -20
  205. package/dist/chat/process.d.ts.map +0 -1
  206. package/dist/chat/process.js +0 -32
  207. package/dist/chat/provider.d.ts +0 -63
  208. package/dist/chat/provider.d.ts.map +0 -1
  209. package/dist/chat/provider.js +0 -629
  210. package/dist/chat/question.d.ts +0 -9
  211. package/dist/chat/question.d.ts.map +0 -1
  212. package/dist/chat/question.js +0 -80
  213. package/dist/chat/questions.d.ts +0 -31
  214. package/dist/chat/questions.d.ts.map +0 -1
  215. package/dist/chat/questions.js +0 -111
  216. package/dist/chat/read.d.ts +0 -5
  217. package/dist/chat/read.d.ts.map +0 -1
  218. package/dist/chat/read.js +0 -13
  219. package/dist/chat/ready.d.ts +0 -34
  220. package/dist/chat/ready.d.ts.map +0 -1
  221. package/dist/chat/ready.js +0 -91
  222. package/dist/chat/style.d.ts +0 -4
  223. package/dist/chat/style.d.ts.map +0 -1
  224. package/dist/chat/style.js +0 -302
  225. package/dist/chat.d.ts +0 -14
  226. package/dist/chat.d.ts.map +0 -1
  227. package/dist/chat.js +0 -8
  228. package/dist/read-side.d.ts +0 -25
  229. package/dist/read-side.d.ts.map +0 -1
  230. package/dist/read-side.js +0 -642
  231. package/dist/reads.d.ts +0 -11
  232. package/dist/reads.d.ts.map +0 -1
  233. package/dist/reads.js +0 -98
package/dist/replica.js CHANGED
@@ -1,208 +1,198 @@
1
1
  import { equalValue, parse } from '@agentshouse/mdmodel';
2
2
  import { immutable, landing } from './change.js';
3
- import { changedCleanly, collectionOf, unreadable, validateCollection, } from './collection.js';
4
- import { acceptedBinaries, overlay, overlayChanged, sameLayerText, } from './overlay.js';
5
- import { providerRewritten } from './save.js';
6
- import { drafted, patched, settled, shared } from './shared.js';
7
- export function compositionOf(record) {
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) {
8
9
  return {
9
10
  model: record.model,
10
11
  selection: record.typedSelection === null
11
12
  ? null
12
- : { roots: record.typedSelection.roots, model: record.selectionModel },
13
+ : { roots: record.typedSelection.roots, model: builtIn },
14
+ typedPaths,
13
15
  };
14
16
  }
15
- function held(record) {
16
- const documents = shared(record.documents);
17
- const binaries = shared(record.binaries);
18
- const revisions = shared(record.revisions);
19
- const fileIds = shared(record.fileIds);
20
- const changes = shared(record.changes);
21
- const written = shared(record.written);
22
- return documents === record.documents &&
23
- binaries === record.binaries &&
24
- revisions === record.revisions &&
25
- fileIds === record.fileIds &&
26
- changes === record.changes &&
27
- written === record.written
28
- ? record
29
- : { ...record, documents, binaries, revisions, fileIds, changes, written };
17
+ export function storedRecord(record) {
18
+ const { key: _key, ...stored } = record;
19
+ return JSON.stringify({ ...stored, model: storedModel(stored.model) });
30
20
  }
31
- export function confirmedOf(record, previous) {
32
- const kept = held(record);
33
- return {
34
- record: kept,
35
- collection: collectionOf(kept.documents, compositionOf(kept), previous?.collection),
36
- };
37
- }
38
- export function sameMembership(record, other) {
39
- return (JSON.stringify(record.typedSelection?.roots ?? null) ===
40
- JSON.stringify(other.typedSelection?.roots ?? null));
41
- }
42
- function cleanly(record, previous) {
43
- if (previous === undefined)
44
- return false;
45
- const before = previous.record;
46
- return changedCleanly({ text: record.documents, binaries: record.binaries }, { text: before.documents, binaries: before.binaries, composition: compositionOf(before) }, compositionOf(record));
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
+ }
47
32
  }
48
- export function readStaged(staged, previous) {
49
- const record = held(staged);
50
- const textChanged = previous === undefined || !sameLayerText(record, previous.record);
51
- const modeled = record.model.status !== 'unavailable' || record.selectionModel.status !== 'unavailable';
52
- if (modeled &&
53
- (textChanged ||
54
- record.model !== previous?.record.model ||
55
- record.selectionModel !== previous?.record.selectionModel) &&
56
- !cleanly(record, previous)) {
57
- const verdict = validateCollection(record.documents, Object.keys(record.binaries), compositionOf(record));
58
- if (verdict.verdict === 'refuse' && verdict.rule !== 'model-unavailable') {
59
- 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);
60
37
  }
61
- if (verdict.verdict === 'refuse') {
62
- const refused = unreadable(record.documents, previous?.record.documents);
63
- if (refused !== null)
64
- return refused;
65
- }
66
- }
67
- else if (textChanged) {
68
- const refused = unreadable(record.documents, previous?.record.documents);
69
- if (refused !== null)
70
- return refused;
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
+ });
53
+ }
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;
71
78
  }
72
- return { kind: 'staged', confirmed: confirmedOf(record, previous) };
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
+ };
73
88
  }
74
- export function readReclassified(record, previous) {
75
- const verdict = validateCollection(record.documents, Object.keys(record.binaries), compositionOf(record));
76
- if (verdict.verdict !== 'refuse')
77
- return { kind: 'staged', confirmed: confirmedOf(record, previous) };
78
- return verdict.rule === 'model-unavailable'
79
- ? { kind: 'deferred' }
80
- : { kind: 'refused', refusal: verdict };
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,
103
+ };
81
104
  }
82
- function returnedBinaries(held, submitted, accepted) {
83
- const binaries = drafted(held);
84
- for (const change of submitted.changes) {
85
- if (change.kind !== 'move')
86
- continue;
87
- const moved = binaries[change.path];
88
- if (moved === undefined || accepted.revisions[change.to] === undefined)
89
- continue;
90
- binaries[change.to] = moved;
91
- delete binaries[change.path];
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;
92
112
  }
93
- Object.assign(binaries, acceptedBinaries(accepted.binaryFiles ?? {}));
94
- for (const path of accepted.removals)
95
- delete binaries[path];
96
- return settled(binaries);
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]);
97
118
  }
98
- export function applyReturned(confirmed, submitted, accepted, roomVersion, writtenAt) {
99
- const { documents, revisions, fileIds, changes, removals } = accepted;
100
- const nextDocuments = drafted(confirmed.record.documents);
101
- const nextRevisions = drafted(confirmed.record.revisions);
102
- const nextFileIds = drafted(confirmed.record.fileIds);
103
- const nextChanges = drafted(confirmed.record.changes);
104
- const nextWritten = drafted(confirmed.record.written);
105
- Object.assign(nextDocuments, documents);
106
- Object.assign(nextRevisions, revisions);
107
- for (const path of Object.keys({ ...documents, ...revisions }))
108
- delete nextFileIds[path];
109
- Object.assign(nextFileIds, fileIds);
110
- for (const path of Object.keys(documents))
111
- nextWritten[path] = writtenAt;
112
- for (const [path, revision] of Object.entries(revisions)) {
113
- if (confirmed.record.revisions[path] !== revision)
114
- delete nextChanges[path];
115
- }
116
- Object.assign(nextChanges, changes);
117
- for (const path of removals) {
118
- delete nextDocuments[path];
119
- delete nextRevisions[path];
120
- delete nextFileIds[path];
121
- delete nextChanges[path];
122
- delete nextWritten[path];
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);
123
132
  }
124
- return readStaged({
125
- ...confirmed.record,
126
- roomVersion,
127
- documents: settled(nextDocuments),
128
- revisions: settled(nextRevisions),
129
- fileIds: settled(nextFileIds),
130
- changes: settled(nextChanges),
131
- written: settled(nextWritten),
132
- binaries: returnedBinaries(confirmed.record.binaries, submitted, accepted),
133
- }, confirmed);
133
+ return paths;
134
134
  }
135
- function predicted(confirmed, batches, previous) {
136
- const composition = compositionOf(confirmed.record);
137
- let layer = confirmed.record;
138
- const replay = [];
139
- const prior = new Map(previous?.replay.map((step) => [step.id, step]) ?? []);
140
- for (const batch of batches) {
141
- const before = layer;
142
- const old = prior.get(batch.id);
143
- const hasMove = batch.changes.some((change) => change.kind === 'move');
144
- const reuseRewrite = old?.collection !== undefined &&
145
- sameLayerText(before, old.before) &&
146
- old.collection.composition.model === composition.model &&
147
- old.collection.composition.selection?.model === composition.selection?.model &&
148
- JSON.stringify(old.collection.composition.selection?.roots) ===
149
- JSON.stringify(composition.selection?.roots);
150
- const collection = hasMove
151
- ? reuseRewrite
152
- ? old.collection
153
- : collectionOf(before.documents, composition, old?.collection ?? confirmed.collection)
154
- : undefined;
155
- const effective = collection === undefined
156
- ? batch
157
- : reuseRewrite
158
- ? old.effective
159
- : providerRewritten(batch, { ...before, collection });
160
- layer =
161
- old === undefined
162
- ? overlay(before, [effective])
163
- : overlayChanged(before, [effective], old.before, [old.effective], old.after);
164
- replay.push({
165
- id: batch.id,
166
- before,
167
- after: layer,
168
- effective,
169
- ...(collection === undefined ? {} : { collection }),
170
- });
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
+ }
171
147
  }
172
- return { layer, replay };
148
+ return paths;
173
149
  }
174
- export function replicaOf(confirmed, queue, previous) {
175
- const batches = Object.freeze(queue.map(immutable));
176
- if (batches.length === 0) {
177
- return {
178
- confirmed,
179
- queue: batches,
180
- visible: {
181
- documents: confirmed.record.documents,
182
- binaries: confirmed.record.binaries,
183
- revisions: confirmed.record.revisions,
184
- fileIds: confirmed.record.fileIds,
185
- changes: confirmed.record.changes,
186
- collection: confirmed.collection,
187
- },
188
- replay: [],
189
- };
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;
190
165
  }
191
- const { layer, replay } = predicted(confirmed, batches, previous);
192
166
  return {
193
- confirmed,
194
- queue: batches,
195
- replay,
196
- visible: {
197
- ...layer,
198
- collection: collectionOf(layer.documents, compositionOf(confirmed.record), previous?.visible.collection ?? confirmed.collection),
199
- },
167
+ visible: { ...layer, collection: collectionOf(layer.documents, composition) },
168
+ touched,
200
169
  };
201
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
+ }
202
192
  function present(layer, path) {
203
193
  return layer.documents[path] !== undefined || layer.binaries[path] !== undefined;
204
194
  }
205
- function holds(layer, preconditions) {
195
+ export function holds(layer, preconditions) {
206
196
  const parsed = new Map();
207
197
  const documentAt = (path) => {
208
198
  if (!parsed.has(path)) {
@@ -267,15 +257,19 @@ function effects(change) {
267
257
  { kind: 'present', path: change.to },
268
258
  ];
269
259
  }
270
- if (change.kind === 'binary')
260
+ if (change.kind === 'binary' || change.kind === 'arrival')
271
261
  return [{ kind: 'present', path }];
272
262
  return [{ kind: 'absent', path }];
273
263
  }
274
264
  export function landed(batch, confirmed, current = {}) {
275
- const layer = {
276
- ...confirmed.record,
277
- documents: patched(confirmed.record.documents, Object.entries(current)),
278
- };
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 };
279
273
  return batch.changes.every((change, index) => {
280
274
  if (change.kind === 'replace' || (change.kind === 'create' && 'content' in change)) {
281
275
  const at = landing(batch.changes, index, change.path);
@@ -293,32 +287,148 @@ export function landed(batch, confirmed, current = {}) {
293
287
  });
294
288
  });
295
289
  }
296
- export function withdrawals(confirmed, queue, withdrawable, local) {
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) {
297
305
  if (!queue.some(withdrawable))
298
- return [];
299
- const composition = compositionOf(confirmed.record);
300
- let layer = confirmed.record;
306
+ return { withdrawn: [], kept: queue };
307
+ let layer = confirmed;
301
308
  const withdrawn = [];
309
+ const kept = [];
302
310
  for (const batch of queue) {
303
311
  if (withdrawable(batch) && !holds(layer, batch.preconditions)) {
304
312
  withdrawn.push(batch);
305
313
  continue;
306
314
  }
307
- const effective = batch.changes.some((change) => change.kind === 'move')
308
- ? providerRewritten(batch, {
315
+ const effective = rewriting(batch, locator)
316
+ ? yield* effectiveIn(batch, locator, {
309
317
  ...layer,
310
318
  collection: collectionOf(layer.documents, composition),
311
319
  })
312
320
  : batch;
313
321
  let unapplied = false;
314
- const next = overlay(layer, [effective], () => {
322
+ const next = yield* overlaying(layer, [effective], () => {
315
323
  unapplied = true;
316
324
  });
317
325
  if (local && unapplied) {
318
326
  withdrawn.push(batch);
319
327
  continue;
320
328
  }
329
+ kept.push(local || !withdrawable(batch) ? batch : founded(batch, layer));
321
330
  layer = next;
322
331
  }
323
- return withdrawn;
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
+ });
432
+ }
433
+ return { set, removed };
324
434
  }
@@ -32,6 +32,7 @@ export type RoomDraftParts = {
32
32
  readonly creation?: RoomDraftCreation;
33
33
  readonly move?: {
34
34
  readonly to: string;
35
+ readonly room?: string;
35
36
  readonly base?: string | null;
36
37
  };
37
38
  readonly removal?: {
@@ -39,11 +40,12 @@ export type RoomDraftParts = {
39
40
  };
40
41
  readonly file?: {
41
42
  readonly blob: Blob;
43
+ readonly base?: string | null;
42
44
  };
43
45
  };
44
46
  export type HeldFile = {
45
- readonly blob: Blob;
46
47
  readonly localId: string;
48
+ readonly name?: string;
47
49
  readonly byteLength: number;
48
50
  readonly mediaType: string;
49
51
  readonly sha256: string;
@@ -69,6 +71,7 @@ type HeldParts = {
69
71
  readonly creation?: RoomDraftCreation;
70
72
  readonly move?: {
71
73
  readonly to: string;
74
+ readonly room?: string;
72
75
  readonly base: string | null;
73
76
  };
74
77
  readonly removal?: {
@@ -110,7 +113,7 @@ export type RoomDraftWrite = {
110
113
  export declare function staleWrite(current: RoomDraft | null): RoomDraftWrite;
111
114
  export type ChangedPart = {
112
115
  readonly path: string;
113
- readonly part: Open<'field' | 'section' | 'preamble' | 'markdown' | 'creation' | 'move' | 'removal' | 'file'>;
116
+ readonly part: Open<'field' | 'section' | 'preamble' | 'markdown' | 'move' | 'removal' | 'file'>;
114
117
  readonly name?: string;
115
118
  };
116
119
  export type RoomDraftRecord = {
@@ -124,12 +127,14 @@ export type SavedDraft = {
124
127
  readonly path: string;
125
128
  readonly parts: HeldParts;
126
129
  };
127
- export declare function readParts(value: unknown): RoomDraftParts;
130
+ export declare function readParts(value: unknown, path: string): RoomDraftParts;
131
+ export declare function gatheredParts(earlier: unknown, later: unknown): unknown;
128
132
  export declare function held(current: HeldParts | undefined, parts: RoomDraftParts, file?: HeldFile): HeldParts;
129
133
  export declare function underneath(parts: HeldParts, path: string, visible: VisibleLayer | undefined): ChangedUnderneath;
130
134
  export declare function heldFile(blob: Blob, base: string | null): Promise<HeldFile>;
131
135
  export declare function binaryBase(visible: VisibleLayer | undefined, path: string): string | null;
132
- export declare function changedParts(path: string, report: ChangedUnderneath): readonly ChangedPart[];
136
+ export declare function changedParts(path: string, parts: HeldParts, visible: VisibleLayer | undefined): readonly ChangedPart[];
137
+ export declare function lineEnded(text: string): string;
133
138
  export type TextParts = {
134
139
  readonly fields?: Readonly<Record<string, {
135
140
  readonly value: unknown;
@@ -146,6 +151,20 @@ export declare function returnedInto(existing: HeldParts | undefined, returned:
146
151
  export declare function moveTarget(move: {
147
152
  readonly to: string;
148
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
+ };
149
168
  export declare function changesOf(path: string, parts: HeldParts): {
150
169
  changes: readonly Change[];
151
170
  bases: readonly Precondition[];