@agentshouse/mdruntime 0.1.0-c1db74d14e5fa → 0.1.0-c1ff51b87bc3d

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (233) hide show
  1. package/README.md +18 -15
  2. package/dist/adapter.d.ts +91 -39
  3. package/dist/adapter.d.ts.map +1 -1
  4. package/dist/attached-model.d.ts +5 -1
  5. package/dist/attached-model.d.ts.map +1 -1
  6. package/dist/attached-model.js +38 -4
  7. package/dist/binary.d.ts +0 -5
  8. package/dist/binary.d.ts.map +1 -1
  9. package/dist/binary.js +1 -1
  10. package/dist/bootstrap.d.ts +27 -8
  11. package/dist/bootstrap.d.ts.map +1 -1
  12. package/dist/bootstrap.js +174 -98
  13. package/dist/chain.d.ts +11 -0
  14. package/dist/chain.d.ts.map +1 -0
  15. package/dist/chain.js +64 -0
  16. package/dist/change-set.d.ts +9 -16
  17. package/dist/change-set.d.ts.map +1 -1
  18. package/dist/change-set.js +11 -88
  19. package/dist/change.d.ts +31 -1
  20. package/dist/change.d.ts.map +1 -1
  21. package/dist/change.js +10 -1
  22. package/dist/client.d.ts +15 -6
  23. package/dist/client.d.ts.map +1 -1
  24. package/dist/client.js +96 -10
  25. package/dist/collection.d.ts +11 -19
  26. package/dist/collection.d.ts.map +1 -1
  27. package/dist/collection.js +53 -289
  28. package/dist/compile.d.ts +16 -0
  29. package/dist/compile.d.ts.map +1 -0
  30. package/dist/compile.js +765 -0
  31. package/dist/connection.d.ts +29 -13
  32. package/dist/connection.d.ts.map +1 -1
  33. package/dist/connection.js +2967 -1070
  34. package/dist/conversation.d.ts +21 -5
  35. package/dist/conversation.d.ts.map +1 -1
  36. package/dist/conversation.js +434 -126
  37. package/dist/digest.d.ts +7 -0
  38. package/dist/digest.d.ts.map +1 -0
  39. package/dist/digest.js +113 -0
  40. package/dist/durability.d.ts +3 -5
  41. package/dist/durability.d.ts.map +1 -1
  42. package/dist/durability.js +5 -0
  43. package/dist/entry.d.ts +4 -1
  44. package/dist/entry.d.ts.map +1 -1
  45. package/dist/entry.js +18 -23
  46. package/dist/file.d.ts +2 -19
  47. package/dist/file.d.ts.map +1 -1
  48. package/dist/file.js +7 -122
  49. package/dist/fulltext.d.ts +27 -0
  50. package/dist/fulltext.d.ts.map +1 -0
  51. package/dist/fulltext.js +368 -0
  52. package/dist/host.d.ts +10 -8
  53. package/dist/host.d.ts.map +1 -1
  54. package/dist/host.js +97 -18
  55. package/dist/identity.d.ts +0 -1
  56. package/dist/identity.d.ts.map +1 -1
  57. package/dist/identity.js +0 -3
  58. package/dist/index.d.ts +14 -11
  59. package/dist/index.d.ts.map +1 -1
  60. package/dist/index.js +5 -1
  61. package/dist/live.d.ts +22 -0
  62. package/dist/live.d.ts.map +1 -0
  63. package/dist/live.js +124 -0
  64. package/dist/media.d.ts +8 -0
  65. package/dist/media.d.ts.map +1 -0
  66. package/dist/media.js +208 -0
  67. package/dist/memory.d.ts +9 -4
  68. package/dist/memory.d.ts.map +1 -1
  69. package/dist/memory.js +86 -32
  70. package/dist/messages.d.ts +28 -0
  71. package/dist/messages.d.ts.map +1 -0
  72. package/dist/messages.js +90 -0
  73. package/dist/model.d.ts +9 -2
  74. package/dist/model.d.ts.map +1 -1
  75. package/dist/model.js +8 -2
  76. package/dist/name.d.ts +4 -0
  77. package/dist/name.d.ts.map +1 -0
  78. package/dist/name.js +122 -0
  79. package/dist/observation.d.ts +15 -0
  80. package/dist/observation.d.ts.map +1 -1
  81. package/dist/observation.js +93 -3
  82. package/dist/opened.d.ts +22 -0
  83. package/dist/opened.d.ts.map +1 -0
  84. package/dist/opened.js +107 -0
  85. package/dist/outcome.d.ts +45 -4
  86. package/dist/outcome.d.ts.map +1 -1
  87. package/dist/outcome.js +39 -0
  88. package/dist/overlay.d.ts +3 -5
  89. package/dist/overlay.d.ts.map +1 -1
  90. package/dist/overlay.js +95 -156
  91. package/dist/paths.d.ts +1 -0
  92. package/dist/paths.d.ts.map +1 -1
  93. package/dist/paths.js +4 -0
  94. package/dist/query.d.ts +39 -2
  95. package/dist/query.d.ts.map +1 -1
  96. package/dist/query.js +48 -7
  97. package/dist/reach.d.ts +14 -6
  98. package/dist/reach.d.ts.map +1 -1
  99. package/dist/reach.js +213 -71
  100. package/dist/react.d.ts +3 -3
  101. package/dist/react.d.ts.map +1 -1
  102. package/dist/react.js +7 -17
  103. package/dist/readiness.d.ts +19 -9
  104. package/dist/readiness.d.ts.map +1 -1
  105. package/dist/readiness.js +6 -6
  106. package/dist/reads.d.ts +11 -0
  107. package/dist/reads.d.ts.map +1 -0
  108. package/dist/reads.js +91 -0
  109. package/dist/recovery.d.ts +2 -2
  110. package/dist/recovery.d.ts.map +1 -1
  111. package/dist/recovery.js +8 -7
  112. package/dist/refresh.d.ts +16 -15
  113. package/dist/refresh.d.ts.map +1 -1
  114. package/dist/refresh.js +113 -50
  115. package/dist/replica.d.ts +56 -33
  116. package/dist/replica.d.ts.map +1 -1
  117. package/dist/replica.js +298 -179
  118. package/dist/room-draft.d.ts +34 -4
  119. package/dist/room-draft.d.ts.map +1 -1
  120. package/dist/room-draft.js +105 -31
  121. package/dist/rows.d.ts +38 -29
  122. package/dist/rows.d.ts.map +1 -1
  123. package/dist/rows.js +173 -94
  124. package/dist/save.d.ts +53 -14
  125. package/dist/save.d.ts.map +1 -1
  126. package/dist/save.js +529 -185
  127. package/dist/search.d.ts +32 -43
  128. package/dist/search.d.ts.map +1 -1
  129. package/dist/search.js +37 -168
  130. package/dist/seed.d.ts +16 -0
  131. package/dist/seed.d.ts.map +1 -0
  132. package/dist/seed.js +75 -0
  133. package/dist/shared.d.ts +8 -3
  134. package/dist/shared.d.ts.map +1 -1
  135. package/dist/shared.js +129 -66
  136. package/dist/status.d.ts +5 -0
  137. package/dist/status.d.ts.map +1 -1
  138. package/dist/status.js +3 -1
  139. package/dist/store.d.ts +97 -89
  140. package/dist/store.d.ts.map +1 -1
  141. package/dist/store.js +329 -641
  142. package/dist/stored.d.ts +135 -0
  143. package/dist/stored.d.ts.map +1 -0
  144. package/dist/stored.js +574 -0
  145. package/dist/tables.d.ts +352 -0
  146. package/dist/tables.d.ts.map +1 -0
  147. package/dist/tables.js +621 -0
  148. package/dist/turn.d.ts +5 -3
  149. package/dist/turn.d.ts.map +1 -1
  150. package/dist/turn.js +51 -21
  151. package/dist/validation.d.ts +30 -0
  152. package/dist/validation.d.ts.map +1 -0
  153. package/dist/validation.js +162 -0
  154. package/dist/windows.d.ts +22 -0
  155. package/dist/windows.d.ts.map +1 -0
  156. package/dist/windows.js +89 -0
  157. package/dist/wire.d.ts +167 -33
  158. package/dist/wire.d.ts.map +1 -1
  159. package/dist/wire.js +55 -32
  160. package/dist/words.d.ts +19 -0
  161. package/dist/words.d.ts.map +1 -0
  162. package/dist/words.js +156 -0
  163. package/package.json +18 -19
  164. package/AGENTS.md +0 -300
  165. package/dist/acquisition.d.ts +0 -14
  166. package/dist/acquisition.d.ts.map +0 -1
  167. package/dist/acquisition.js +0 -24
  168. package/dist/chat/acts.d.ts +0 -33
  169. package/dist/chat/acts.d.ts.map +0 -1
  170. package/dist/chat/acts.js +0 -36
  171. package/dist/chat/context.d.ts +0 -3
  172. package/dist/chat/context.d.ts.map +0 -1
  173. package/dist/chat/context.js +0 -10
  174. package/dist/chat/input.d.ts +0 -9
  175. package/dist/chat/input.d.ts.map +0 -1
  176. package/dist/chat/input.js +0 -99
  177. package/dist/chat/kit.d.ts +0 -60
  178. package/dist/chat/kit.d.ts.map +0 -1
  179. package/dist/chat/kit.js +0 -123
  180. package/dist/chat/lasting.d.ts +0 -6
  181. package/dist/chat/lasting.d.ts.map +0 -1
  182. package/dist/chat/lasting.js +0 -19
  183. package/dist/chat/list.d.ts +0 -26
  184. package/dist/chat/list.d.ts.map +0 -1
  185. package/dist/chat/list.js +0 -62
  186. package/dist/chat/markdown.d.ts +0 -20
  187. package/dist/chat/markdown.d.ts.map +0 -1
  188. package/dist/chat/markdown.js +0 -188
  189. package/dist/chat/message.d.ts +0 -6
  190. package/dist/chat/message.d.ts.map +0 -1
  191. package/dist/chat/message.js +0 -192
  192. package/dist/chat/messages.d.ts +0 -21
  193. package/dist/chat/messages.d.ts.map +0 -1
  194. package/dist/chat/messages.js +0 -151
  195. package/dist/chat/name.d.ts +0 -6
  196. package/dist/chat/name.d.ts.map +0 -1
  197. package/dist/chat/name.js +0 -37
  198. package/dist/chat/opened.d.ts +0 -11
  199. package/dist/chat/opened.d.ts.map +0 -1
  200. package/dist/chat/opened.js +0 -17
  201. package/dist/chat/overlay.d.ts +0 -31
  202. package/dist/chat/overlay.d.ts.map +0 -1
  203. package/dist/chat/overlay.js +0 -97
  204. package/dist/chat/parts.d.ts +0 -57
  205. package/dist/chat/parts.d.ts.map +0 -1
  206. package/dist/chat/parts.js +0 -119
  207. package/dist/chat/process.d.ts +0 -20
  208. package/dist/chat/process.d.ts.map +0 -1
  209. package/dist/chat/process.js +0 -32
  210. package/dist/chat/provider.d.ts +0 -63
  211. package/dist/chat/provider.d.ts.map +0 -1
  212. package/dist/chat/provider.js +0 -620
  213. package/dist/chat/question.d.ts +0 -9
  214. package/dist/chat/question.d.ts.map +0 -1
  215. package/dist/chat/question.js +0 -80
  216. package/dist/chat/questions.d.ts +0 -31
  217. package/dist/chat/questions.d.ts.map +0 -1
  218. package/dist/chat/questions.js +0 -111
  219. package/dist/chat/read.d.ts +0 -5
  220. package/dist/chat/read.d.ts.map +0 -1
  221. package/dist/chat/read.js +0 -13
  222. package/dist/chat/ready.d.ts +0 -33
  223. package/dist/chat/ready.d.ts.map +0 -1
  224. package/dist/chat/ready.js +0 -91
  225. package/dist/chat/style.d.ts +0 -4
  226. package/dist/chat/style.d.ts.map +0 -1
  227. package/dist/chat/style.js +0 -302
  228. package/dist/chat.d.ts +0 -14
  229. package/dist/chat.d.ts.map +0 -1
  230. package/dist/chat.js +0 -8
  231. package/dist/read-side.d.ts +0 -25
  232. package/dist/read-side.d.ts.map +0 -1
  233. package/dist/read-side.js +0 -639
@@ -1,6 +1,6 @@
1
- import { apply, equalValue, frontmatterValue, parse, serialize } from '@agentshouse/mdmodel';
1
+ import { apply, equalValue, frontmatterValue, parse, serialize, } from '@agentshouse/mdmodel';
2
2
  import { runtimeError } from './failure.js';
3
- import { hexDigest } from './file.js';
3
+ import { sha256 } from './digest.js';
4
4
  import { binaryLocalId } from './identity.js';
5
5
  import { normalize } from './paths.js';
6
6
  export function returnedBy(kind) {
@@ -90,7 +90,12 @@ export function readParts(value) {
90
90
  if (!plain(value.move))
91
91
  malformed();
92
92
  const base = markdownBase(value.move.base);
93
- parts.move = { to: text(value.move.to), ...(base === undefined ? {} : { base }) };
93
+ const room = optionalText(value.move.room);
94
+ parts.move = {
95
+ to: text(value.move.to),
96
+ ...(room === undefined ? {} : { room }),
97
+ ...(base === undefined ? {} : { base }),
98
+ };
94
99
  }
95
100
  if (value.removal !== undefined) {
96
101
  if (!plain(value.removal))
@@ -103,7 +108,8 @@ export function readParts(value) {
103
108
  malformed();
104
109
  if (Object.keys(value).length > 1)
105
110
  throw runtimeError('room-draft-file-exclusive');
106
- parts.file = { blob: value.file.blob };
111
+ const base = markdownBase(value.file.base);
112
+ parts.file = base === undefined ? { blob: value.file.blob } : { blob: value.file.blob, base };
107
113
  }
108
114
  return parts;
109
115
  }
@@ -120,11 +126,51 @@ function kept(prior, base) {
120
126
  throw runtimeError('room-draft-base-missing');
121
127
  return base;
122
128
  }
129
+ function movePart(current, move) {
130
+ return {
131
+ to: move.to,
132
+ ...(move.room === undefined ? {} : { room: move.room }),
133
+ base: kept(current, move.base),
134
+ };
135
+ }
123
136
  function editsText(parts) {
124
137
  return (parts.preamble !== undefined ||
125
138
  named(parts.fields).length > 0 ||
126
139
  named(parts.sections).length > 0);
127
140
  }
141
+ function partNames(parts) {
142
+ const names = [];
143
+ for (const [name, part] of Object.entries(parts)) {
144
+ if (name === 'preamble' || name === 'markdown')
145
+ names.push(name);
146
+ else if ((name === 'fields' || name === 'sections') && plain(part))
147
+ names.push(...Object.keys(part).map((one) => `${name}/${one}`));
148
+ else
149
+ return null;
150
+ }
151
+ return names.sort().join('\u0000');
152
+ }
153
+ function withBaseOf(prior, part) {
154
+ return plain(part) && plain(prior) && Object.hasOwn(prior, 'base')
155
+ ? { ...part, base: prior.base }
156
+ : part;
157
+ }
158
+ export function gatheredParts(earlier, later) {
159
+ if (!plain(earlier) || !plain(later))
160
+ return null;
161
+ const named = partNames(earlier);
162
+ if (named === null || named !== partNames(later))
163
+ return null;
164
+ return Object.fromEntries(Object.entries(later).map(([name, part]) => [
165
+ name,
166
+ name === 'fields' || name === 'sections'
167
+ ? Object.fromEntries(Object.entries(part).map(([one, value]) => [
168
+ one,
169
+ withBaseOf(earlier[name][one], value),
170
+ ]))
171
+ : withBaseOf(earlier[name], part),
172
+ ]));
173
+ }
128
174
  export function held(current, parts, file) {
129
175
  if (parts.markdown !== undefined && editsText(parts)) {
130
176
  throw runtimeError('room-draft-markdown-exclusive');
@@ -144,9 +190,7 @@ export function held(current, parts, file) {
144
190
  }
145
191
  return {
146
192
  file: current.file,
147
- ...(parts.move === undefined
148
- ? {}
149
- : { move: { to: parts.move.to, base: kept(current.move, parts.move.base) } }),
193
+ ...(parts.move === undefined ? {} : { move: movePart(current.move, parts.move) }),
150
194
  ...(parts.removal === undefined
151
195
  ? {}
152
196
  : { removal: { base: kept(current.removal, parts.removal.base) } }),
@@ -181,9 +225,8 @@ export function held(current, parts, file) {
181
225
  }
182
226
  if (parts.creation !== undefined)
183
227
  next.creation = parts.creation;
184
- if (parts.move !== undefined) {
185
- next.move = { to: parts.move.to, base: kept(current?.move, parts.move.base) };
186
- }
228
+ if (parts.move !== undefined)
229
+ next.move = movePart(current?.move, parts.move);
187
230
  if (parts.removal !== undefined) {
188
231
  next.removal = { base: kept(current?.removal, parts.removal.base) };
189
232
  }
@@ -240,29 +283,32 @@ export function underneath(parts, path, visible) {
240
283
  }
241
284
  const UNTYPED_MEDIA_TYPE = 'application/octet-stream';
242
285
  export async function heldFile(blob, base) {
243
- const bytes = await blob.arrayBuffer();
244
286
  return {
245
- blob,
246
287
  localId: binaryLocalId(),
247
- byteLength: bytes.byteLength,
288
+ ...(blob instanceof File ? { name: blob.name } : {}),
289
+ byteLength: blob.size,
248
290
  mediaType: blob.type === '' ? UNTYPED_MEDIA_TYPE : blob.type,
249
- sha256: hexDigest(await crypto.subtle.digest('SHA-256', bytes)),
291
+ sha256: await sha256(blob),
250
292
  base,
251
293
  };
252
294
  }
253
295
  export function binaryBase(visible, path) {
254
- return visible?.binaries[path] === undefined ? null : (visible.revisions[path] ?? null);
296
+ const binary = visible?.binaries[path];
297
+ if (binary === undefined)
298
+ return null;
299
+ return visible?.revisions[path] ?? (binary.state === 'pending' ? binary.localId : null);
255
300
  }
256
- export function changedParts(path, report) {
301
+ export function changedParts(path, parts, visible) {
302
+ const report = underneath(parts, path, visible);
303
+ const made = parts.file?.base === null;
257
304
  return [
258
- ...(report.creation === undefined ? [] : [{ path, part: 'creation' }]),
259
305
  ...named(report.fields).map(([name]) => ({ path, part: 'field', name })),
260
306
  ...named(report.sections).map(([name]) => ({ path, part: 'section', name })),
261
307
  ...(report.preamble === undefined ? [] : [{ path, part: 'preamble' }]),
262
308
  ...(report.markdown === undefined ? [] : [{ path, part: 'markdown' }]),
263
309
  ...(report.move === undefined ? [] : [{ path, part: 'move' }]),
264
310
  ...(report.removal === undefined ? [] : [{ path, part: 'removal' }]),
265
- ...(report.file === undefined ? [] : [{ path, part: 'file' }]),
311
+ ...(report.file === undefined || made ? [] : [{ path, part: 'file' }]),
266
312
  ];
267
313
  }
268
314
  function joined(existing, returned) {
@@ -279,16 +325,15 @@ function frontmatterOf(fields) {
279
325
  const opened = parse('---\n---\n');
280
326
  if (!opened.ok)
281
327
  return malformed();
282
- let document = opened.document;
283
- for (const [name, part] of fields) {
284
- const next = apply(document, { kind: 'field', name, value: part.value, prior: undefined });
285
- if (next.outcome !== 'applied')
286
- return malformed();
287
- document = next.document;
288
- }
289
- return serialize(document);
290
- }
291
- function concatenated(markdown, parts) {
328
+ const next = apply(opened.document, fields.map(([name, part]) => ({
329
+ kind: 'field',
330
+ name,
331
+ value: part.value,
332
+ prior: undefined,
333
+ })));
334
+ return next.outcome === 'applied' ? serialize(next.document) : malformed();
335
+ }
336
+ export function partsAfter(markdown, parts) {
292
337
  const fields = named(parts.fields);
293
338
  const pieces = [
294
339
  ...(fields.length === 0 ? [] : [frontmatterOf(fields)]),
@@ -319,7 +364,7 @@ export function returnedInto(existing, returned) {
319
364
  delete next.fields;
320
365
  delete next.sections;
321
366
  delete next.preamble;
322
- const value = concatenated(whole.value, existing.markdown === undefined ? existing : returned);
367
+ const value = partsAfter(whole.value, existing.markdown === undefined ? existing : returned);
323
368
  next.markdown = { value, base: whole.base };
324
369
  }
325
370
  const creation = existing.creation ?? returned.creation;
@@ -340,6 +385,35 @@ export function moveTarget(move) {
340
385
  const to = normalize(move.to);
341
386
  return to === null || to === '' ? move.to : to;
342
387
  }
388
+ export function readSavedParts(named) {
389
+ const read = named.map(({ path, parts }) => ({ path, parts: readParts(parts) }));
390
+ if (read.some(({ path }, at) => path === '' ||
391
+ normalize(path) !== path ||
392
+ read.findIndex((one) => one.path === path) !== at)) {
393
+ throw runtimeError('room-draft-malformed');
394
+ }
395
+ return read;
396
+ }
397
+ export function partsChanges(named) {
398
+ const changes = [];
399
+ const preconditions = [];
400
+ for (const { path, parts } of readSavedParts(named)) {
401
+ const parted = held(undefined, parts);
402
+ const made = changesOf(path, parted);
403
+ changes.push(...made.changes);
404
+ preconditions.push(...made.bases);
405
+ for (const change of made.changes) {
406
+ if (change.kind === 'create')
407
+ preconditions.push({ kind: 'absent', path });
408
+ if (change.kind === 'move')
409
+ preconditions.push({ kind: 'absent', path: change.to });
410
+ }
411
+ const revision = parted.markdown?.base ?? parted.move?.base ?? parted.removal?.base;
412
+ if (typeof revision === 'string')
413
+ preconditions.push({ kind: 'revision', path, revision });
414
+ }
415
+ return { changes, preconditions };
416
+ }
343
417
  export function changesOf(path, parts) {
344
418
  const changes = [];
345
419
  const bases = [];
@@ -370,12 +444,12 @@ export function changesOf(path, parts) {
370
444
  changes.push({ kind: 'replace', path, content: parts.markdown.value });
371
445
  }
372
446
  if (parts.file !== undefined && parts.removal === undefined) {
373
- const { base, ...file } = parts.file;
447
+ const { base, name: _name, ...file } = parts.file;
374
448
  const at = parts.move === undefined ? path : moveTarget(parts.move);
375
449
  if (base !== null && at !== path)
376
450
  changes.push({ kind: 'move', path, to: at });
377
451
  changes.push({ kind: 'binary', path: at, ...file });
378
- if (base === null && at !== path)
452
+ if (base === null)
379
453
  bases.push({ kind: 'absent', path: at });
380
454
  return { changes, bases };
381
455
  }
package/dist/rows.d.ts CHANGED
@@ -1,37 +1,46 @@
1
- import { type Frontmatter } from '@agentshouse/mdmodel';
2
- import type { HeadChange } from './adapter.js';
3
- import type { BinaryFileRecord } from './binary.js';
4
1
  import { type Composition } from './collection.js';
5
- type Fields = Readonly<Record<string, unknown>>;
6
- export type DocumentRow = {
7
- key: string;
8
- kind: 'document';
9
- room: string;
10
- path: string;
11
- type: string | null;
12
- fields: Fields;
13
- values: Fields;
14
- reached: Readonly<Record<string, readonly unknown[]>>;
15
- directories: readonly string[];
16
- text: string | null;
17
- change: HeadChange | null;
18
- binary: BinaryFileRecord | null;
2
+ export type ValueKind = 'string' | 'number' | 'boolean' | 'null' | 'array' | 'object' | 'undefined';
3
+ type HeldValue = {
4
+ readonly keys: string;
5
+ readonly places: string;
6
+ readonly item: number;
7
+ readonly kind: ValueKind;
8
+ readonly value: string | number | null;
9
+ readonly size: number | null;
19
10
  };
20
- export type ReferenceRow = {
21
- key: string;
22
- kind: 'reference';
23
- room: string;
24
- source: string;
25
- target: string;
26
- field: string;
27
- forms: readonly string[];
28
- path: string;
29
- type: string;
11
+ type LinkKind = 'field' | 'section' | 'body';
12
+ type HeldLink = {
13
+ readonly kind: LinkKind;
14
+ readonly keys: string;
15
+ readonly places: string;
16
+ readonly target: string;
17
+ readonly field: string;
18
+ };
19
+ export type Described = {
20
+ readonly type: string | null;
21
+ readonly declared: string | null;
22
+ readonly queried: boolean;
23
+ readonly folder: string;
24
+ readonly fields: string | null;
25
+ };
26
+ export type Derived = Described & {
27
+ readonly values: readonly HeldValue[];
28
+ readonly links: readonly HeldLink[];
30
29
  };
31
- export type Row = DocumentRow | ReferenceRow;
32
30
  export declare function documentKey(room: string, path: string): string;
33
31
  export declare function roomOfKey(key: string): string;
32
+ export declare function pathOfKey(key: string): string;
34
33
  export declare function markdownPath(path: string): boolean;
35
- export declare function rowsOf(room: string, path: string, document: Frontmatter | null, composition: Composition, change: HeadChange | null, binary: BinaryFileRecord | null): readonly Row[];
34
+ export declare function folderOf(path: string): string;
35
+ export type Reach = 'exact' | 'value' | 'link';
36
+ export type Placed = {
37
+ readonly keys: string;
38
+ readonly places: string;
39
+ readonly glob: boolean;
40
+ };
41
+ export declare function placedOf(pointer: string, reach: Reach): Placed;
42
+ export declare function kindOf(value: unknown): ValueKind;
43
+ export declare function described(path: string, content: string | null): Described;
44
+ export declare function derived(path: string, content: string | null, composition: Composition): Derived;
36
45
  export {};
37
46
  //# sourceMappingURL=rows.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"rows.d.ts","sourceRoot":"","sources":["../src/rows.ts"],"names":[],"mappings":"AAAA,OAAO,EAKL,KAAK,WAAW,EACjB,MAAM,sBAAsB,CAAC;AAC9B,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAC/C,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AACpD,OAAO,EAAa,KAAK,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAG9D,KAAK,MAAM,GAAG,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;AAEhD,MAAM,MAAM,WAAW,GAAG;IACxB,GAAG,EAAE,MAAM,CAAC;IACZ,IAAI,EAAE,UAAU,CAAC;IACjB,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MA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,MAAM,EAAE,SAAS,SAAS,EAAE,CAAC;IACtC,QAAQ,CAAC,KAAK,EAAE,SAAS,QAAQ,EAAE,CAAC;CACrC,CAAC;AAMF,wBAAgB,WAAW,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,CAE9D;AAED,wBAAgB,SAAS,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAE7C;AAED,wBAAgB,SAAS,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAE7C;AAED,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAElD;AAED,wBAAgB,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAE7C;AAWD,MAAM,MAAM,KAAK,GAAG,OAAO,GAAG,OAAO,GAAG,MAAM,CAAC;AAE/C,MAAM,MAAM,MAAM,GAAG;IAAE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAA;CAAE,CAAC;AAEhG,wBAAgB,QAAQ,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK,GAAG,MAAM,CAuB9D;AAwBD,wBAAgB,MAAM,CAAC,KAAK,EAAE,OAAO,GAAG,SAAS,CAMhD;AAiJD,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,CAEzE;AAED,wBAAgB,OAAO,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,IAAI,EAAE,WAAW,EAAE,WAAW,GAAG,OAAO,CAO/F"}
package/dist/rows.js CHANGED
@@ -1,4 +1,4 @@
1
- import { ARRAY_INDEX, escapePointer, references, target, } from '@agentshouse/mdmodel';
1
+ import { ARRAY_INDEX, bodyAfterFrontmatter, bodyLinks, escapePointer, parseFrontmatter, references, sectionReferences, target, } from '@agentshouse/mdmodel';
2
2
  import { governing } from './collection.js';
3
3
  import { definitionOf } from './model.js';
4
4
  function keyOf(...parts) {
@@ -10,6 +10,50 @@ export function documentKey(room, path) {
10
10
  export function roomOfKey(key) {
11
11
  return key.split('\u0000')[1];
12
12
  }
13
+ export function pathOfKey(key) {
14
+ return key.split('\u0000')[2];
15
+ }
16
+ export function markdownPath(path) {
17
+ return path.endsWith('.md');
18
+ }
19
+ export function folderOf(path) {
20
+ return path.slice(0, Math.max(path.lastIndexOf('/'), 0));
21
+ }
22
+ export function placedOf(pointer, reach) {
23
+ const [first = '', ...rest] = pointer.split('/').slice(1);
24
+ const keys = [first];
25
+ const gaps = [[]];
26
+ for (const segment of rest) {
27
+ if (ARRAY_INDEX.test(segment)) {
28
+ gaps.at(-1).push(segment);
29
+ continue;
30
+ }
31
+ keys.push(segment);
32
+ gaps.push([]);
33
+ }
34
+ const places = gaps
35
+ .map((gap, at) => {
36
+ const open = at < gaps.length - 1 ? reach !== 'exact' : reach === 'link';
37
+ return `/${gap.map((number) => `[.:]${number}`).join('')}.${open ? '*' : ''}`;
38
+ })
39
+ .join('');
40
+ return {
41
+ keys: keys.map((key) => `/${key}`).join(''),
42
+ places,
43
+ glob: /[[*]/u.test(places),
44
+ };
45
+ }
46
+ function placesOf({ done, head, tail }) {
47
+ return `${done}/${head}${tail}.`;
48
+ }
49
+ function throughOf(keys, places) {
50
+ const gaps = places.split('/').slice(1);
51
+ return keys
52
+ .split('/')
53
+ .slice(1)
54
+ .map((key, at) => `/${key}${gaps[at].replace(/\.\d*/gu, '').replaceAll(':', '/')}`)
55
+ .join('');
56
+ }
13
57
  function plainObject(value) {
14
58
  return typeof value === 'object' && value !== null && !Array.isArray(value);
15
59
  }
@@ -18,118 +62,153 @@ function compared(reading, exact, value) {
18
62
  return value;
19
63
  return (typeof value === 'string' ? target(reading.path, value) : null) ?? undefined;
20
64
  }
21
- function record(reading, node, forms, exact) {
22
- const value = Array.isArray(node)
23
- ? node.map((item, index) => compared(reading, `${exact}/${index}`, item))
24
- : compared(reading, exact, node);
25
- for (const form of forms) {
26
- if (form.pending)
27
- continue;
28
- (reading.reached[form.text] ??= []).push(value);
29
- if (!form.through && value !== undefined && !plainObject(value)) {
30
- reading.values[form.text] = value;
31
- }
32
- }
33
- if (Array.isArray(node) || plainObject(node) || !reading.references.has(exact))
65
+ export function kindOf(value) {
66
+ if (value === undefined)
67
+ return 'undefined';
68
+ if (value === null)
69
+ return 'null';
70
+ if (Array.isArray(value))
71
+ return 'array';
72
+ if (typeof value === 'object')
73
+ return 'object';
74
+ return typeof value;
75
+ }
76
+ function scalarOf(value) {
77
+ if (typeof value === 'string' || typeof value === 'number')
78
+ return value;
79
+ if (typeof value === 'boolean')
80
+ return value ? 1 : 0;
81
+ if (Array.isArray(value))
82
+ return JSON.stringify(value);
83
+ return null;
84
+ }
85
+ function held(reading, keys, places, value) {
86
+ reading.values.push({
87
+ keys,
88
+ places,
89
+ item: -1,
90
+ kind: kindOf(value),
91
+ value: scalarOf(value),
92
+ size: Array.isArray(value) ? value.length : null,
93
+ });
94
+ if (!Array.isArray(value))
34
95
  return;
35
- const found = compared(reading, exact, node);
36
- if (typeof found !== 'string')
96
+ value.forEach((item, index) => {
97
+ const itemKind = kindOf(item);
98
+ reading.values.push({
99
+ keys,
100
+ places,
101
+ item: index,
102
+ kind: itemKind,
103
+ value: itemKind === 'array' ? null : scalarOf(item),
104
+ size: null,
105
+ });
106
+ });
107
+ }
108
+ function record(reading, node, at) {
109
+ const value = Array.isArray(node)
110
+ ? node.map((item, index) => compared(reading, `${at.exact}/${index}`, item))
111
+ : compared(reading, at.exact, node);
112
+ const places = placesOf(at);
113
+ held(reading, at.keys, places, value);
114
+ if (typeof value !== 'string' || !reading.references.has(at.exact))
37
115
  return;
38
- const field = forms
39
- .map((form) => form.text)
40
- .reduce((left, right) => (right.length < left.length ? right : left));
41
- const known = reading.targets.get(`${field}\u0000${found}`);
42
- const kept = known ?? { field, target: found, forms: new Set() };
43
- for (const form of forms)
44
- kept.forms.add(form.text);
45
- reading.targets.set(`${field}\u0000${found}`, kept);
46
- }
47
- function walk(reading, node, forms, exact) {
48
- record(reading, node, forms, exact);
116
+ reading.links.push({
117
+ kind: 'field',
118
+ keys: at.keys,
119
+ places,
120
+ target: value,
121
+ field: throughOf(at.keys, places),
122
+ });
123
+ }
124
+ function walk(reading, node, at) {
125
+ record(reading, node, at);
49
126
  if (Array.isArray(node)) {
50
- node.forEach((item, index) => walk(reading, item, forms.flatMap((form) => [
51
- ...(form.pending
52
- ? []
53
- : [{ text: `${form.text}/${index}`, through: form.through, pending: false }]),
54
- { text: form.text, through: true, pending: true },
55
- ]), `${exact}/${index}`));
127
+ node.forEach((item, index) => walk(reading, item, { ...at, tail: `${at.tail}.${index}`, exact: `${at.exact}/${index}` }));
56
128
  return;
57
129
  }
58
130
  if (!plainObject(node))
59
131
  return;
60
132
  for (const [key, value] of Object.entries(node)) {
61
133
  const segment = escapePointer(key);
62
- walk(reading, value, forms.flatMap((form) => form.pending && ARRAY_INDEX.test(key)
63
- ? []
64
- : [{ text: `${form.text}/${segment}`, through: form.through, pending: false }]), `${exact}/${segment}`);
134
+ const exact = `${at.exact}/${segment}`;
135
+ walk(reading, value, ARRAY_INDEX.test(segment)
136
+ ? { ...at, head: `${at.head}${at.tail.replaceAll('.', ':')}:${segment}`, tail: '', exact }
137
+ : { keys: `${at.keys}/${segment}`, done: placesOf(at), head: '', tail: '', exact });
65
138
  }
66
139
  }
67
- function directoriesOf(path) {
68
- const parts = path.split('/').slice(0, -1);
69
- return ['', ...parts.map((_, index) => parts.slice(0, index + 1).join('/'))];
70
- }
71
- export function markdownPath(path) {
72
- return path.endsWith('.md');
73
- }
74
- export function rowsOf(room, path, document, composition, change, binary) {
75
- const key = documentKey(room, path);
76
- if (document === null || !markdownPath(path)) {
77
- return [
78
- {
79
- key,
80
- kind: 'document',
81
- room,
82
- path,
83
- type: null,
84
- fields: {},
85
- values: {},
86
- reached: {},
87
- directories: directoriesOf(path),
88
- text: null,
89
- change,
90
- binary,
91
- },
92
- ];
140
+ function bodyTargets(path, content) {
141
+ const seen = new Set();
142
+ const found = [];
143
+ for (const { target: reached } of bodyLinks(bodyAfterFrontmatter(content), path)) {
144
+ if (seen.has(reached))
145
+ continue;
146
+ seen.add(reached);
147
+ found.push({ kind: 'body', keys: '', places: '', target: reached, field: '' });
93
148
  }
94
- const type = document.type ?? 'any';
149
+ return found;
150
+ }
151
+ function typedRows(path, content, document, composition) {
95
152
  const definition = definitionOf(governing(composition, path), document.type);
96
153
  const reading = {
97
154
  path,
98
155
  references: new Set(definition === undefined ? [] : references(document, definition).map((one) => one.pointer)),
99
- values: {},
100
- reached: {},
101
- targets: new Map(),
156
+ values: [],
157
+ links: [],
102
158
  };
103
159
  for (const [name, value] of Object.entries(document.fields)) {
104
160
  const segment = escapePointer(name);
105
- walk(reading, value, [{ text: `/${segment}`, through: false, pending: false }], `/${segment}`);
161
+ walk(reading, value, {
162
+ keys: `/${segment}`,
163
+ done: '',
164
+ head: '',
165
+ tail: '',
166
+ exact: `/${segment}`,
167
+ });
106
168
  }
107
- const { values, reached } = reading;
108
- return [
109
- {
110
- key,
111
- kind: 'document',
112
- room,
113
- path,
114
- type,
115
- fields: document.fields,
116
- values,
117
- reached,
118
- directories: directoriesOf(path),
119
- text: document.content,
120
- change,
121
- binary,
169
+ const links = reading.links;
170
+ if (definition !== undefined) {
171
+ const sectioned = new Set();
172
+ for (const { target: reached } of sectionReferences(path, content, definition)) {
173
+ if (reached === null || sectioned.has(reached))
174
+ continue;
175
+ sectioned.add(reached);
176
+ links.push({ kind: 'section', keys: '', places: '', target: reached, field: '' });
177
+ }
178
+ }
179
+ return { values: reading.values, links: [...links, ...bodyTargets(path, content)] };
180
+ }
181
+ function read(path, content) {
182
+ const folder = folderOf(path);
183
+ const unread = { fields: null, folder, type: null };
184
+ if (content === null) {
185
+ return { described: { ...unread, declared: null, queried: true }, document: null };
186
+ }
187
+ const parsed = parseFrontmatter(content, path);
188
+ const declared = parsed.ok ? (parsed.frontmatter.type ?? 'any') : null;
189
+ if (!markdownPath(path) || !parsed.ok) {
190
+ return { described: { ...unread, declared, queried: !markdownPath(path) }, document: null };
191
+ }
192
+ const document = parsed.frontmatter;
193
+ return {
194
+ described: {
195
+ type: document.type ?? 'any',
196
+ declared,
197
+ queried: true,
198
+ folder,
199
+ fields: JSON.stringify(document.fields),
122
200
  },
123
- ...[...reading.targets.values()].map(({ field, target: found, forms }) => ({
124
- key: keyOf('r', room, path, field, found),
125
- kind: 'reference',
126
- room,
127
- source: key,
128
- target: documentKey(room, found),
129
- field,
130
- forms: [...forms].sort(),
131
- path,
132
- type,
133
- })),
134
- ];
201
+ document,
202
+ };
203
+ }
204
+ export function described(path, content) {
205
+ return read(path, content).described;
206
+ }
207
+ export function derived(path, content, composition) {
208
+ const { described: columns, document } = read(path, content);
209
+ if (document !== null) {
210
+ return { ...columns, ...typedRows(path, content, document, composition) };
211
+ }
212
+ const links = content === null || markdownPath(path) ? [] : bodyTargets(path, content);
213
+ return { ...columns, values: [], links };
135
214
  }