@agentshouse/mdruntime 0.1.0-c2aaa9c0e3ca4 → 0.1.0-c2c52158b7381

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 (199) hide show
  1. package/AGENTS.md +285 -276
  2. package/README.md +9 -8
  3. package/dist/adapter.d.ts +80 -21
  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.map +1 -1
  9. package/dist/attempt.js +14 -1
  10. package/dist/bootstrap.d.ts +3 -2
  11. package/dist/bootstrap.d.ts.map +1 -1
  12. package/dist/bootstrap.js +83 -26
  13. package/dist/change-set.d.ts +7 -2
  14. package/dist/change-set.d.ts.map +1 -1
  15. package/dist/change-set.js +37 -10
  16. package/dist/change.d.ts +31 -1
  17. package/dist/change.d.ts.map +1 -1
  18. package/dist/change.js +10 -1
  19. package/dist/chat/acts.d.ts +3 -4
  20. package/dist/chat/acts.d.ts.map +1 -1
  21. package/dist/chat/acts.js +10 -10
  22. package/dist/chat/earlier.d.ts +10 -0
  23. package/dist/chat/earlier.d.ts.map +1 -0
  24. package/dist/chat/earlier.js +54 -0
  25. package/dist/chat/house.d.ts +25 -0
  26. package/dist/chat/house.d.ts.map +1 -0
  27. package/dist/chat/house.js +38 -0
  28. package/dist/chat/input.d.ts +3 -1
  29. package/dist/chat/input.d.ts.map +1 -1
  30. package/dist/chat/input.js +11 -18
  31. package/dist/chat/kit.d.ts +2 -1
  32. package/dist/chat/kit.d.ts.map +1 -1
  33. package/dist/chat/kit.js +3 -3
  34. package/dist/chat/list.d.ts +9 -1
  35. package/dist/chat/list.d.ts.map +1 -1
  36. package/dist/chat/list.js +53 -9
  37. package/dist/chat/markdown.d.ts +3 -1
  38. package/dist/chat/markdown.d.ts.map +1 -1
  39. package/dist/chat/markdown.js +85 -26
  40. package/dist/chat/message.d.ts +1 -0
  41. package/dist/chat/message.d.ts.map +1 -1
  42. package/dist/chat/message.js +57 -12
  43. package/dist/chat/messages.d.ts +9 -1
  44. package/dist/chat/messages.d.ts.map +1 -1
  45. package/dist/chat/messages.js +67 -28
  46. package/dist/chat/name.d.ts +1 -0
  47. package/dist/chat/name.d.ts.map +1 -1
  48. package/dist/chat/name.js +1 -1
  49. package/dist/chat/opened.d.ts +1 -0
  50. package/dist/chat/opened.d.ts.map +1 -1
  51. package/dist/chat/opened.js +2 -0
  52. package/dist/chat/overlay.d.ts +1 -0
  53. package/dist/chat/overlay.d.ts.map +1 -1
  54. package/dist/chat/overlay.js +11 -5
  55. package/dist/chat/parts.d.ts +1 -0
  56. package/dist/chat/parts.d.ts.map +1 -1
  57. package/dist/chat/parts.js +3 -2
  58. package/dist/chat/pieces.d.ts +62 -0
  59. package/dist/chat/pieces.d.ts.map +1 -0
  60. package/dist/chat/pieces.js +1110 -0
  61. package/dist/chat/provider.d.ts +7 -2
  62. package/dist/chat/provider.d.ts.map +1 -1
  63. package/dist/chat/provider.js +129 -155
  64. package/dist/chat/question.d.ts +1 -1
  65. package/dist/chat/question.d.ts.map +1 -1
  66. package/dist/chat/question.js +7 -18
  67. package/dist/chat/ready.d.ts +5 -1
  68. package/dist/chat/ready.d.ts.map +1 -1
  69. package/dist/chat/ready.js +9 -7
  70. package/dist/chat/room-file.d.ts +8 -0
  71. package/dist/chat/room-file.d.ts.map +1 -0
  72. package/dist/chat/room-file.js +72 -0
  73. package/dist/chat/steady.d.ts +3 -0
  74. package/dist/chat/steady.d.ts.map +1 -0
  75. package/dist/chat/steady.js +26 -0
  76. package/dist/chat/style.d.ts.map +1 -1
  77. package/dist/chat/style.js +8 -4
  78. package/dist/chat.d.ts +3 -2
  79. package/dist/chat.d.ts.map +1 -1
  80. package/dist/chat.js +2 -2
  81. package/dist/client.d.ts +21 -9
  82. package/dist/client.d.ts.map +1 -1
  83. package/dist/client.js +47 -8
  84. package/dist/collection.d.ts +21 -6
  85. package/dist/collection.d.ts.map +1 -1
  86. package/dist/collection.js +332 -40
  87. package/dist/connection.d.ts +39 -11
  88. package/dist/connection.d.ts.map +1 -1
  89. package/dist/connection.js +2294 -492
  90. package/dist/conversation.d.ts +28 -9
  91. package/dist/conversation.d.ts.map +1 -1
  92. package/dist/conversation.js +500 -165
  93. package/dist/digest.d.ts +7 -0
  94. package/dist/digest.d.ts.map +1 -0
  95. package/dist/digest.js +113 -0
  96. package/dist/durability.d.ts +8 -1
  97. package/dist/durability.d.ts.map +1 -1
  98. package/dist/entry.d.ts +18 -0
  99. package/dist/entry.d.ts.map +1 -0
  100. package/dist/entry.js +157 -0
  101. package/dist/fence.d.ts +6 -0
  102. package/dist/fence.d.ts.map +1 -0
  103. package/dist/fence.js +10 -0
  104. package/dist/file.d.ts +2 -10
  105. package/dist/file.d.ts.map +1 -1
  106. package/dist/file.js +3 -120
  107. package/dist/host.d.ts +15 -3
  108. package/dist/host.d.ts.map +1 -1
  109. package/dist/host.js +103 -13
  110. package/dist/identity.d.ts +2 -0
  111. package/dist/identity.d.ts.map +1 -1
  112. package/dist/identity.js +6 -0
  113. package/dist/index.d.ts +15 -9
  114. package/dist/index.d.ts.map +1 -1
  115. package/dist/index.js +5 -0
  116. package/dist/memory.d.ts +8 -2
  117. package/dist/memory.d.ts.map +1 -1
  118. package/dist/memory.js +43 -12
  119. package/dist/model.d.ts +4 -2
  120. package/dist/model.d.ts.map +1 -1
  121. package/dist/model.js +10 -1
  122. package/dist/name.d.ts +4 -0
  123. package/dist/name.d.ts.map +1 -0
  124. package/dist/name.js +122 -0
  125. package/dist/observation.d.ts +7 -2
  126. package/dist/observation.d.ts.map +1 -1
  127. package/dist/observation.js +29 -12
  128. package/dist/opened.d.ts +22 -0
  129. package/dist/opened.d.ts.map +1 -0
  130. package/dist/opened.js +107 -0
  131. package/dist/outcome.d.ts +39 -4
  132. package/dist/outcome.d.ts.map +1 -1
  133. package/dist/outcome.js +29 -0
  134. package/dist/overlay.d.ts +6 -4
  135. package/dist/overlay.d.ts.map +1 -1
  136. package/dist/overlay.js +229 -161
  137. package/dist/query.d.ts +57 -4
  138. package/dist/query.d.ts.map +1 -1
  139. package/dist/query.js +37 -7
  140. package/dist/reach.d.ts +11 -4
  141. package/dist/reach.d.ts.map +1 -1
  142. package/dist/reach.js +192 -41
  143. package/dist/react.d.ts +8 -6
  144. package/dist/react.d.ts.map +1 -1
  145. package/dist/react.js +7 -17
  146. package/dist/read-side.d.ts +18 -9
  147. package/dist/read-side.d.ts.map +1 -1
  148. package/dist/read-side.js +894 -155
  149. package/dist/readiness.d.ts +53 -0
  150. package/dist/readiness.d.ts.map +1 -0
  151. package/dist/readiness.js +33 -0
  152. package/dist/reads.d.ts +11 -0
  153. package/dist/reads.d.ts.map +1 -0
  154. package/dist/reads.js +98 -0
  155. package/dist/recovery.d.ts +25 -0
  156. package/dist/recovery.d.ts.map +1 -0
  157. package/dist/recovery.js +198 -0
  158. package/dist/refresh.d.ts +4 -4
  159. package/dist/refresh.d.ts.map +1 -1
  160. package/dist/refresh.js +46 -20
  161. package/dist/replica.d.ts +12 -7
  162. package/dist/replica.d.ts.map +1 -1
  163. package/dist/replica.js +151 -64
  164. package/dist/room-draft.d.ts +16 -2
  165. package/dist/room-draft.d.ts.map +1 -1
  166. package/dist/room-draft.js +32 -25
  167. package/dist/rows.d.ts +6 -13
  168. package/dist/rows.d.ts.map +1 -1
  169. package/dist/rows.js +10 -13
  170. package/dist/save.d.ts +52 -10
  171. package/dist/save.d.ts.map +1 -1
  172. package/dist/save.js +519 -159
  173. package/dist/search.d.ts +49 -6
  174. package/dist/search.d.ts.map +1 -1
  175. package/dist/search.js +242 -46
  176. package/dist/seed.d.ts +16 -0
  177. package/dist/seed.d.ts.map +1 -0
  178. package/dist/seed.js +75 -0
  179. package/dist/shared.d.ts +19 -0
  180. package/dist/shared.d.ts.map +1 -0
  181. package/dist/shared.js +461 -0
  182. package/dist/status.d.ts +4 -0
  183. package/dist/status.d.ts.map +1 -1
  184. package/dist/store.d.ts +61 -17
  185. package/dist/store.d.ts.map +1 -1
  186. package/dist/store.js +581 -310
  187. package/dist/tables.d.ts +242 -0
  188. package/dist/tables.d.ts.map +1 -0
  189. package/dist/tables.js +406 -0
  190. package/dist/turn.d.ts +9 -0
  191. package/dist/turn.d.ts.map +1 -0
  192. package/dist/turn.js +67 -0
  193. package/dist/windows.d.ts +21 -0
  194. package/dist/windows.d.ts.map +1 -0
  195. package/dist/windows.js +84 -0
  196. package/dist/wire.d.ts +168 -32
  197. package/dist/wire.d.ts.map +1 -1
  198. package/dist/wire.js +56 -31
  199. package/package.json +10 -2
package/dist/overlay.js CHANGED
@@ -1,6 +1,9 @@
1
- import { apply, parse, serialize } from '@agentshouse/mdmodel';
2
- export function acceptedBinaries(files) {
3
- return Object.fromEntries(Object.entries(files).map(([path, file]) => [path, { state: 'accepted', ...file }]));
1
+ import { apply, frontmatterValue, parse, serialize, } from '@agentshouse/mdmodel';
2
+ import { differences, drafted, entriesIn, patching, settling } from './shared.js';
3
+ import { ROW_WEIGHT } from './turn.js';
4
+ export function* acceptedBinaries(files) {
5
+ for (const [path, file] of entriesIn(files))
6
+ yield [path, { state: 'accepted', ...file }];
4
7
  }
5
8
  export function sameBinary(left, right) {
6
9
  if (left === undefined || right === undefined)
@@ -22,10 +25,13 @@ export function sameChange(left, right) {
22
25
  left.author?.name === right.author?.name);
23
26
  }
24
27
  const EMPTY_FRONTMATTER = '---\n---\n';
28
+ function isPart(change) {
29
+ return change.kind === 'field' || change.kind === 'section' || change.kind === 'preamble';
30
+ }
25
31
  function at(path, name) {
26
32
  return `${path} ${name}`;
27
33
  }
28
- function priorsOf(preconditions) {
34
+ function* priorsOf(preconditions) {
29
35
  const fields = new Map();
30
36
  const sections = new Map();
31
37
  const preambles = new Map();
@@ -39,6 +45,7 @@ function priorsOf(preconditions) {
39
45
  if (precondition.kind === 'preamble') {
40
46
  preambles.set(precondition.path, precondition.value);
41
47
  }
48
+ yield ROW_WEIGHT;
42
49
  }
43
50
  return {
44
51
  field: (path, name) => fields.get(at(path, name)),
@@ -54,51 +61,50 @@ function compose(fields, sections) {
54
61
  const opened = parse(skeleton);
55
62
  if (!opened.ok)
56
63
  return skeleton;
57
- let document = opened.document;
58
- for (const [name, value] of Object.entries(fields)) {
59
- const next = apply(document, { kind: 'field', name, value, prior: undefined });
60
- if (next.outcome === 'applied')
61
- document = next.document;
62
- }
63
- return serialize(document);
64
+ const next = apply(opened.document, Object.entries(fields).flatMap(([name, value]) => frontmatterValue(value) ? [{ kind: 'field', name, value, prior: undefined }] : []));
65
+ return serialize(next.outcome === 'applied' ? next.document : opened.document);
64
66
  }
65
- function edited(document, change, priors) {
66
- if (change.kind === 'field') {
67
- return apply(document, {
67
+ function edited(change, priors) {
68
+ if (change.kind === 'field')
69
+ return {
68
70
  kind: 'field',
69
71
  name: change.name,
70
72
  value: change.value,
71
73
  prior: priors.field(change.path, change.name),
72
- });
73
- }
74
- if (change.kind === 'preamble') {
75
- return apply(document, {
76
- kind: 'preamble',
77
- value: change.value,
78
- prior: priors.preamble(change.path),
79
- });
80
- }
81
- if (change.kind !== 'section')
82
- return null;
83
- return apply(document, {
74
+ };
75
+ if (change.kind === 'preamble')
76
+ return { kind: 'preamble', value: change.value, prior: priors.preamble(change.path) };
77
+ return {
84
78
  kind: 'section',
85
79
  name: change.name,
86
80
  value: change.value,
87
81
  prior: priors.section(change.path, change.name),
88
- });
82
+ };
89
83
  }
90
- function edit(staged, change, priors) {
91
- const text = staged.documents[change.path];
84
+ function edit(staged, changes, priors) {
85
+ const path = changes[0].path;
86
+ delete staged.revisions[path];
87
+ const text = staged.documents[path];
92
88
  if (text === undefined)
93
- return false;
94
- const parsed = parse(text, change.path);
89
+ return changes.length;
90
+ const parsed = parse(text, path);
95
91
  if (!parsed.ok)
96
- return false;
97
- const next = edited(parsed.document, change, priors);
98
- if (next?.outcome !== 'applied')
99
- return false;
100
- staged.documents[change.path] = serialize(next.document);
101
- return true;
92
+ return changes.length;
93
+ const wanted = changes.map((change) => edited(change, priors));
94
+ const first = apply(parsed.document, wanted);
95
+ if (first.outcome === 'refuse')
96
+ return changes.length;
97
+ if (first.outcome === 'applied') {
98
+ staged.documents[path] = serialize(first.document);
99
+ return 0;
100
+ }
101
+ const stale = new Set(first.stale);
102
+ const kept = wanted.filter((_, at) => !stale.has(at));
103
+ const rest = apply(parsed.document, kept);
104
+ if (rest.outcome !== 'applied')
105
+ return changes.length;
106
+ staged.documents[path] = serialize(rest.document);
107
+ return first.stale.length;
102
108
  }
103
109
  function relocate(staged, from, to) {
104
110
  const text = staged.documents[from];
@@ -118,32 +124,37 @@ function relocate(staged, from, to) {
118
124
  }
119
125
  function moveHead(heads, from, to) {
120
126
  const revision = heads.revisions[from];
127
+ const fileId = heads.fileIds[from];
121
128
  const change = heads.changes[from];
122
129
  delete heads.revisions[from];
130
+ delete heads.fileIds[from];
123
131
  delete heads.changes[from];
132
+ delete heads.fileIds[to];
124
133
  delete heads.changes[to];
125
134
  if (revision !== undefined)
126
135
  heads.revisions[to] = revision;
136
+ if (fileId !== undefined)
137
+ heads.fileIds[to] = fileId;
127
138
  if (change !== undefined)
128
139
  heads.changes[to] = change;
129
140
  }
130
141
  function erase(staged, path) {
131
142
  delete staged.documents[path];
132
143
  delete staged.revisions[path];
144
+ delete staged.fileIds[path];
133
145
  delete staged.changes[path];
134
146
  delete staged.binaries[path];
135
147
  }
136
- function stage(staged, change, priors) {
137
- if (change.kind === 'field' || change.kind === 'section' || change.kind === 'preamble') {
138
- return edit(staged, change, priors);
139
- }
148
+ function stage(staged, change) {
140
149
  if (change.kind === 'create') {
141
150
  staged.documents[change.path] =
142
151
  'content' in change ? change.content : compose(change.fields, change.sections);
152
+ delete staged.fileIds[change.path];
143
153
  delete staged.changes[change.path];
144
154
  return true;
145
155
  }
146
156
  if (change.kind === 'replace') {
157
+ delete staged.revisions[change.path];
147
158
  if (staged.documents[change.path] === undefined)
148
159
  return false;
149
160
  staged.documents[change.path] = change.content;
@@ -166,156 +177,213 @@ function stage(staged, change, priors) {
166
177
  if (change.kind === 'move') {
167
178
  return relocate(staged, change.path, change.to);
168
179
  }
180
+ if (change.kind === 'arrival') {
181
+ erase(staged, change.path);
182
+ if ('content' in change)
183
+ staged.documents[change.path] = change.content;
184
+ else
185
+ staged.binaries[change.path] = { state: 'accepted', ...change.binary };
186
+ arrive(staged, change);
187
+ return true;
188
+ }
169
189
  erase(staged, change.path);
170
190
  return true;
171
191
  }
172
- export function overlay(base, queue, unapplied) {
192
+ function arrive(heads, change) {
193
+ if (change.revision !== undefined)
194
+ heads.revisions[change.path] = change.revision;
195
+ if (change.fileId !== undefined)
196
+ heads.fileIds[change.path] = change.fileId;
197
+ if (change.change !== undefined)
198
+ heads.changes[change.path] = change.change;
199
+ }
200
+ export function* overlaying(base, queue, unapplied) {
173
201
  const staged = {
174
- documents: { ...base.documents },
175
- binaries: { ...base.binaries },
176
- revisions: { ...base.revisions },
177
- changes: { ...base.changes },
202
+ documents: drafted(base.documents),
203
+ binaries: drafted(base.binaries),
204
+ revisions: drafted(base.revisions),
205
+ fileIds: drafted(base.fileIds),
206
+ changes: drafted(base.changes),
178
207
  };
179
208
  for (const batch of queue) {
180
- const priors = priorsOf(batch.preconditions);
181
- for (const change of batch.changes) {
182
- if (!stage(staged, change, priors))
209
+ const priors = yield* priorsOf(batch.preconditions);
210
+ for (let index = 0; index < batch.changes.length; index += 1) {
211
+ const change = batch.changes[index];
212
+ if (!isPart(change)) {
213
+ if (!stage(staged, change))
214
+ unapplied?.();
215
+ yield ROW_WEIGHT;
216
+ continue;
217
+ }
218
+ const run = [change];
219
+ while (index + 1 < batch.changes.length) {
220
+ const next = batch.changes[index + 1];
221
+ if (!isPart(next) || next.path !== change.path)
222
+ break;
223
+ run.push(next);
224
+ index += 1;
225
+ }
226
+ const text = staged.documents[change.path] ?? '';
227
+ for (let left = edit(staged, run, priors); left > 0; left -= 1)
183
228
  unapplied?.();
229
+ yield ROW_WEIGHT + text.length;
184
230
  }
185
231
  }
186
232
  return {
187
- documents: staged.documents,
188
- binaries: staged.binaries,
189
- revisions: staged.revisions,
190
- changes: staged.changes,
233
+ documents: yield* settling(staged.documents),
234
+ binaries: yield* settling(staged.binaries),
235
+ revisions: yield* settling(staged.revisions),
236
+ fileIds: yield* settling(staged.fileIds),
237
+ changes: yield* settling(staged.changes),
191
238
  };
192
239
  }
193
- function pathsOf(batch) {
194
- return batch.changes.flatMap((change) => change.kind === 'move' ? [change.path, change.to] : [change.path]);
240
+ function* pathsOf(batch) {
241
+ const paths = new Set();
242
+ for (const change of batch.changes) {
243
+ paths.add(change.path);
244
+ if (change.kind === 'move')
245
+ paths.add(change.to);
246
+ yield ROW_WEIGHT;
247
+ }
248
+ return paths;
195
249
  }
196
- export function sameLayerText(left, right) {
197
- const paths = Object.keys(left.documents);
198
- return (paths.length === Object.keys(right.documents).length &&
199
- paths.every((path) => left.documents[path] === right.documents[path]) &&
200
- sameBinaries(left.binaries, right.binaries));
250
+ export function* sameText(left, right) {
251
+ return ((yield* differences(left.documents, right.documents)).size === 0 &&
252
+ (yield* differences(left.binaries, right.binaries, sameBinary)).size === 0);
201
253
  }
202
- function sameBinaries(left, right) {
203
- const paths = Object.keys(left);
204
- return (paths.length === Object.keys(right).length &&
205
- paths.every((path) => sameBinary(left[path], right[path])));
254
+ export function* changedPaths(left, right) {
255
+ return new Set([
256
+ ...(yield* differences(left.documents, right.documents)),
257
+ ...(yield* differences(left.revisions, right.revisions)),
258
+ ...(yield* differences(left.fileIds, right.fileIds)),
259
+ ...(yield* differences(left.changes, right.changes, sameChange)),
260
+ ...(yield* differences(left.binaries, right.binaries, sameBinary)),
261
+ ]);
206
262
  }
207
- function replayHeads(base, queue) {
208
- const heads = { revisions: { ...base.revisions }, changes: { ...base.changes } };
209
- for (const batch of queue) {
210
- for (const change of batch.changes) {
211
- if (change.kind === 'remove') {
212
- delete heads.revisions[change.path];
213
- delete heads.changes[change.path];
214
- }
215
- else if (change.kind === 'create') {
216
- delete heads.changes[change.path];
217
- }
218
- else if (change.kind === 'binary') {
219
- delete heads.revisions[change.path];
220
- delete heads.changes[change.path];
221
- }
222
- else if (change.kind === 'move') {
223
- moveHead(heads, change.path, change.to);
224
- }
263
+ function* sameItems(left, right) {
264
+ if (left.length !== right.length)
265
+ return false;
266
+ for (let index = 0; index < left.length; index += 1) {
267
+ const item = JSON.stringify(left[index]);
268
+ if (item !== JSON.stringify(right[index]))
269
+ return false;
270
+ yield ROW_WEIGHT + item.length;
271
+ }
272
+ return true;
273
+ }
274
+ function* sameBatch(left, right) {
275
+ if (left === right)
276
+ return true;
277
+ const { changes, preconditions, ...rest } = left;
278
+ const { changes: others, preconditions: priors, ...held } = right;
279
+ return (JSON.stringify(rest) === JSON.stringify(held) &&
280
+ (yield* sameItems(changes, others)) &&
281
+ (yield* sameItems(preconditions, priors)));
282
+ }
283
+ function* replayHeads(base, batch) {
284
+ const heads = {
285
+ revisions: drafted(base.revisions),
286
+ fileIds: drafted(base.fileIds),
287
+ changes: drafted(base.changes),
288
+ };
289
+ for (const change of batch.changes) {
290
+ if (change.kind === 'remove') {
291
+ delete heads.revisions[change.path];
292
+ delete heads.fileIds[change.path];
293
+ delete heads.changes[change.path];
225
294
  }
295
+ else if (change.kind === 'create') {
296
+ delete heads.fileIds[change.path];
297
+ delete heads.changes[change.path];
298
+ }
299
+ else if (change.kind === 'replace' ||
300
+ change.kind === 'field' ||
301
+ change.kind === 'section' ||
302
+ change.kind === 'preamble') {
303
+ delete heads.revisions[change.path];
304
+ }
305
+ else if (change.kind === 'binary') {
306
+ delete heads.revisions[change.path];
307
+ delete heads.changes[change.path];
308
+ }
309
+ else if (change.kind === 'move') {
310
+ moveHead(heads, change.path, change.to);
311
+ }
312
+ else if (change.kind === 'arrival') {
313
+ delete heads.revisions[change.path];
314
+ delete heads.fileIds[change.path];
315
+ delete heads.changes[change.path];
316
+ arrive(heads, change);
317
+ }
318
+ yield ROW_WEIGHT;
226
319
  }
227
- return heads;
320
+ return {
321
+ revisions: yield* settling(heads.revisions),
322
+ fileIds: yield* settling(heads.fileIds),
323
+ changes: yield* settling(heads.changes),
324
+ };
228
325
  }
229
- export function overlayChanged(base, queue, previousBase, previousQueue, previousVisible) {
230
- const before = previousQueue.map((batch) => JSON.stringify(batch));
231
- const after = queue.map((batch) => JSON.stringify(batch));
232
- if (before.length === after.length &&
233
- before.every((id, index) => id === after[index]) &&
234
- sameLayerText(base, previousBase)) {
326
+ function* repatched(held, paths, value) {
327
+ const set = [];
328
+ const removed = [];
329
+ for (const path of paths) {
330
+ const found = value(path);
331
+ if (found === undefined)
332
+ removed.push(path);
333
+ else
334
+ set.push([path, found]);
335
+ yield ROW_WEIGHT;
336
+ }
337
+ return yield* patching(held, set, removed);
338
+ }
339
+ export function* overlayChanged(base, batch, previousBase, previousBatch, previousVisible) {
340
+ const same = yield* sameBatch(batch, previousBatch);
341
+ if (same && (yield* sameText(base, previousBase))) {
235
342
  return {
236
343
  documents: previousVisible.documents,
237
344
  binaries: previousVisible.binaries,
238
- ...replayHeads(base, queue),
345
+ ...(yield* replayHeads(base, batch)),
239
346
  };
240
347
  }
241
- const affected = new Set();
242
- const paths = new Set([
243
- ...Object.keys(base.documents),
244
- ...Object.keys(previousBase.documents),
245
- ...Object.keys(base.revisions),
246
- ...Object.keys(previousBase.revisions),
247
- ...Object.keys(base.changes),
248
- ...Object.keys(previousBase.changes),
249
- ...Object.keys(base.binaries),
250
- ...Object.keys(previousBase.binaries),
251
- ]);
252
- for (const path of paths) {
253
- if (base.documents[path] !== previousBase.documents[path] ||
254
- base.revisions[path] !== previousBase.revisions[path] ||
255
- !sameChange(base.changes[path], previousBase.changes[path]) ||
256
- !sameBinary(base.binaries[path], previousBase.binaries[path]))
348
+ const affected = new Set(yield* changedPaths(base, previousBase));
349
+ const touched = yield* pathsOf(batch);
350
+ let replayed = !same;
351
+ if (replayed)
352
+ for (const path of yield* pathsOf(previousBatch))
257
353
  affected.add(path);
354
+ for (const path of touched) {
355
+ if (affected.has(path))
356
+ replayed = true;
357
+ yield ROW_WEIGHT;
258
358
  }
259
- const oldIds = new Set(before);
260
- const newIds = new Set(after);
261
- for (const batch of previousQueue) {
262
- if (!newIds.has(JSON.stringify(batch)))
263
- for (const path of pathsOf(batch))
264
- affected.add(path);
265
- }
266
- for (const batch of queue) {
267
- if (!oldIds.has(JSON.stringify(batch)))
268
- for (const path of pathsOf(batch))
269
- affected.add(path);
270
- }
271
- let expanded = true;
272
- while (expanded) {
273
- expanded = false;
274
- for (const batch of queue) {
275
- const touched = pathsOf(batch);
276
- if (!touched.some((path) => affected.has(path)))
277
- continue;
278
- for (const path of touched) {
279
- if (affected.has(path))
280
- continue;
281
- affected.add(path);
282
- expanded = true;
283
- }
284
- }
285
- }
286
- const documents = {};
287
- const revisions = {};
288
- const changes = {};
289
- const binaries = {};
290
- for (const path of affected) {
359
+ const restricted = {
360
+ documents: {},
361
+ revisions: {},
362
+ fileIds: {},
363
+ changes: {},
364
+ binaries: {},
365
+ };
366
+ for (const path of replayed ? touched : []) {
367
+ affected.add(path);
291
368
  if (base.documents[path] !== undefined)
292
- documents[path] = base.documents[path];
369
+ restricted.documents[path] = base.documents[path];
293
370
  if (base.revisions[path] !== undefined)
294
- revisions[path] = base.revisions[path];
371
+ restricted.revisions[path] = base.revisions[path];
372
+ if (base.fileIds[path] !== undefined)
373
+ restricted.fileIds[path] = base.fileIds[path];
295
374
  if (base.changes[path] !== undefined)
296
- changes[path] = base.changes[path];
375
+ restricted.changes[path] = base.changes[path];
297
376
  if (base.binaries[path] !== undefined)
298
- binaries[path] = base.binaries[path];
299
- }
300
- const staged = overlay({ documents, revisions, changes, binaries }, queue.filter((batch) => pathsOf(batch).some((path) => affected.has(path))));
301
- const mergedDocuments = { ...previousVisible.documents };
302
- const mergedRevisions = { ...previousVisible.revisions };
303
- const mergedChanges = { ...previousVisible.changes };
304
- const mergedBinaries = { ...previousVisible.binaries };
305
- for (const path of affected) {
306
- delete mergedDocuments[path];
307
- delete mergedRevisions[path];
308
- delete mergedChanges[path];
309
- delete mergedBinaries[path];
377
+ restricted.binaries[path] = base.binaries[path];
378
+ yield ROW_WEIGHT;
310
379
  }
311
- Object.assign(mergedDocuments, staged.documents);
312
- Object.assign(mergedRevisions, staged.revisions);
313
- Object.assign(mergedChanges, staged.changes);
314
- Object.assign(mergedBinaries, staged.binaries);
380
+ const staged = replayed ? yield* overlaying(restricted, [batch]) : base;
381
+ const from = (path) => (touched.has(path) ? staged : base);
315
382
  return {
316
- documents: mergedDocuments,
317
- revisions: mergedRevisions,
318
- changes: mergedChanges,
319
- binaries: mergedBinaries,
383
+ documents: yield* repatched(previousVisible.documents, affected, (path) => from(path).documents[path]),
384
+ revisions: yield* repatched(previousVisible.revisions, affected, (path) => from(path).revisions[path]),
385
+ fileIds: yield* repatched(previousVisible.fileIds, affected, (path) => from(path).fileIds[path]),
386
+ changes: yield* repatched(previousVisible.changes, affected, (path) => from(path).changes[path]),
387
+ binaries: yield* repatched(previousVisible.binaries, affected, (path) => from(path).binaries[path]),
320
388
  };
321
389
  }
package/dist/query.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { type Definition } from '@agentshouse/mdmodel';
2
2
  import type { HeadChange } from './adapter.js';
3
3
  import type { BinaryFileRecord } from './binary.js';
4
+ import type { Open } from './open.js';
4
5
  export type Value = string | number | boolean | null;
5
6
  export type Predicate = Value | {
6
7
  $eq: Value;
@@ -37,6 +38,8 @@ export type Filter = {
37
38
  types?: readonly string[];
38
39
  where?: Where;
39
40
  pathPrefix?: string;
41
+ folder?: string;
42
+ paths?: readonly string[];
40
43
  hop?: Related & {
41
44
  field: string;
42
45
  };
@@ -44,7 +47,7 @@ export type Filter = {
44
47
  field?: string;
45
48
  };
46
49
  };
47
- export type Key = 'room' | 'path' | 'type' | {
50
+ export type Key = 'room' | 'path' | 'type' | 'folder' | {
48
51
  field: string;
49
52
  };
50
53
  export type Select = 'all' | {
@@ -54,6 +57,7 @@ export type Select = 'all' | {
54
57
  markdown?: true;
55
58
  backlinks?: true;
56
59
  change?: true;
60
+ fileId?: true;
57
61
  };
58
62
  export type OrderOptions = {
59
63
  nulls?: 'first' | 'last';
@@ -69,6 +73,8 @@ export type DocumentQuery = Filter & {
69
73
  direction?: Direction;
70
74
  } & OrderOptions)[];
71
75
  limit?: number;
76
+ after?: string;
77
+ total?: true;
72
78
  };
73
79
  export type GroupQuery = Filter & {
74
80
  groupBy: readonly Exclude<Key, 'path'>[];
@@ -77,6 +83,8 @@ export type GroupQuery = Filter & {
77
83
  direction?: Direction;
78
84
  }[];
79
85
  limit?: number;
86
+ after?: string;
87
+ total?: true;
80
88
  };
81
89
  export type LocalQuery = DocumentQuery | GroupQuery;
82
90
  export type Backlink = {
@@ -94,19 +102,49 @@ export type QueryDocument = {
94
102
  markdown?: string;
95
103
  backlinks?: readonly Backlink[];
96
104
  change?: HeadChange | null;
105
+ fileId?: string | null;
97
106
  binaryFile?: BinaryFileRecord;
98
107
  };
99
108
  export type QueryGroup = {
100
109
  key: readonly unknown[];
101
110
  count: number;
102
111
  };
103
- export type DocumentResult = {
112
+ export type CoveredRoom = {
113
+ room: string;
114
+ generation: string;
115
+ current: boolean;
116
+ };
117
+ export type QueryCoverage = {
118
+ rooms: readonly CoveredRoom[];
119
+ indexes: readonly string[];
120
+ overlay: readonly string[];
121
+ };
122
+ export type Awaits = Open<'acquisition' | 'preparation' | 'index' | 'unlock'>;
123
+ export type PendingRoom = {
124
+ room: string;
125
+ awaits: Awaits;
126
+ };
127
+ export type PendingResult = {
128
+ pending: readonly PendingRoom[];
129
+ };
130
+ export type DocumentRows = {
104
131
  documents: readonly QueryDocument[];
132
+ next?: string;
133
+ total?: number;
105
134
  };
106
- export type GroupResult = {
135
+ export type GroupRows = {
107
136
  groups: readonly QueryGroup[];
137
+ next?: string;
138
+ total?: number;
139
+ };
140
+ export type QueryRows = DocumentRows | GroupRows;
141
+ export type DocumentResult = DocumentRows & {
142
+ coverage: QueryCoverage;
143
+ };
144
+ export type GroupResult = GroupRows & {
145
+ coverage: QueryCoverage;
108
146
  };
109
- export type QueryResult = DocumentResult | GroupResult;
147
+ export type QueryResult = DocumentResult | GroupResult | PendingResult;
110
148
  export type QueryErrorCode = 'invalid-query' | 'unknown-type' | 'unknown-field' | 'wrong-kind';
111
149
  export type RoomFacts = {
112
150
  locator: string;
@@ -161,6 +199,8 @@ export type KeyPlan = {
161
199
  of: 'path';
162
200
  } | {
163
201
  of: 'type';
202
+ } | {
203
+ of: 'folder';
164
204
  } | {
165
205
  of: 'field';
166
206
  field: string;
@@ -173,6 +213,7 @@ export type DocumentShape = {
173
213
  markdown: boolean;
174
214
  backlinks: boolean;
175
215
  change: boolean;
216
+ fileId: boolean;
176
217
  orderBy: readonly {
177
218
  key: KeyPlan;
178
219
  options: OrderOptions & {
@@ -192,10 +233,18 @@ export type PlanReach = {
192
233
  rooms: readonly string[];
193
234
  documents: readonly string[];
194
235
  };
236
+ export type Continuation = {
237
+ text: string;
238
+ values: readonly unknown[] | null;
239
+ };
195
240
  export type Plan = {
241
+ key: string;
196
242
  rooms: readonly string[] | null;
197
243
  reach: PlanReach | null;
244
+ locked: readonly string[];
198
245
  root: Scope;
246
+ folder: string | null;
247
+ paths: readonly string[] | null;
199
248
  hop: (Scope & {
200
249
  field: string;
201
250
  }) | null;
@@ -204,6 +253,8 @@ export type Plan = {
204
253
  }) | null;
205
254
  shape: DocumentShape | GroupShape;
206
255
  limit: number | null;
256
+ after: Continuation | null;
257
+ total: boolean;
207
258
  };
208
259
  export declare class QueryError extends Error {
209
260
  readonly code: QueryErrorCode;
@@ -214,9 +265,11 @@ type Entries = Readonly<Record<string, unknown>>;
214
265
  export declare function invalid(at: string, problem: string): never;
215
266
  export declare function plain(value: unknown): value is Entries;
216
267
  export declare function known(value: Entries, allowed: readonly string[], at: string): void;
268
+ export declare function flag(input: unknown, at: string): boolean;
217
269
  export declare function planOf(input: unknown, facts: HeldRooms, reach?: PlanReach | null): Plan;
218
270
  export declare function limitOf(input: unknown): number | null;
219
271
  export declare function filterOf(input: unknown, facts: HeldRooms, reach?: PlanReach | null): Plan;
272
+ export declare function queryKey(query: unknown): string;
220
273
  export declare function identityOf(plan: Plan): string;
221
274
  export {};
222
275
  //# sourceMappingURL=query.d.ts.map