@agentshouse/mdruntime 0.1.0-ca9afacd5aebd → 0.1.0-cac707d698ca2

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