@agentshouse/mdruntime 0.1.0-cf6d5f0a5c9c8 → 0.1.0-cf890bb538119

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