@agentshouse/mdruntime 0.1.0-ce131cb435f8b → 0.1.0-ce41fec66d91b

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 (229) hide show
  1. package/README.md +18 -15
  2. package/dist/adapter.d.ts +110 -72
  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 +25 -10
  11. package/dist/bootstrap.d.ts.map +1 -1
  12. package/dist/bootstrap.js +180 -103
  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 -89
  19. package/dist/change.d.ts +31 -1
  20. package/dist/change.d.ts.map +1 -1
  21. package/dist/change.js +10 -1
  22. package/dist/client.d.ts +11 -8
  23. package/dist/client.d.ts.map +1 -1
  24. package/dist/client.js +80 -7
  25. package/dist/collection.d.ts +10 -22
  26. package/dist/collection.d.ts.map +1 -1
  27. package/dist/collection.js +39 -356
  28. package/dist/compile.d.ts +16 -0
  29. package/dist/compile.d.ts.map +1 -0
  30. package/dist/compile.js +786 -0
  31. package/dist/connection.d.ts +17 -11
  32. package/dist/connection.d.ts.map +1 -1
  33. package/dist/connection.js +2779 -1329
  34. package/dist/conversation.d.ts +19 -11
  35. package/dist/conversation.d.ts.map +1 -1
  36. package/dist/conversation.js +298 -166
  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 +2 -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 -24
  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 +370 -0
  52. package/dist/host.d.ts +3 -5
  53. package/dist/host.d.ts.map +1 -1
  54. package/dist/host.js +48 -21
  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 +6 -2
  61. package/dist/live.d.ts +22 -0
  62. package/dist/live.d.ts.map +1 -0
  63. package/dist/live.js +123 -0
  64. package/dist/media.d.ts +8 -0
  65. package/dist/media.d.ts.map +1 -0
  66. package/dist/media.js +218 -0
  67. package/dist/memory.d.ts +10 -6
  68. package/dist/memory.d.ts.map +1 -1
  69. package/dist/memory.js +195 -102
  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 +6 -1
  74. package/dist/model.d.ts.map +1 -1
  75. package/dist/model.js +7 -10
  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 +13 -0
  80. package/dist/observation.d.ts.map +1 -1
  81. package/dist/observation.js +84 -0
  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 +40 -8
  86. package/dist/outcome.d.ts.map +1 -1
  87. package/dist/outcome.js +38 -2
  88. package/dist/overlay.d.ts +2 -6
  89. package/dist/overlay.d.ts.map +1 -1
  90. package/dist/overlay.js +87 -176
  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 +11 -1
  95. package/dist/query.d.ts.map +1 -1
  96. package/dist/query.js +13 -1
  97. package/dist/reach.d.ts +13 -4
  98. package/dist/reach.d.ts.map +1 -1
  99. package/dist/reach.js +200 -78
  100. package/dist/readiness.d.ts +14 -15
  101. package/dist/readiness.d.ts.map +1 -1
  102. package/dist/readiness.js +2 -8
  103. package/dist/recovery.d.ts +3 -2
  104. package/dist/recovery.d.ts.map +1 -1
  105. package/dist/recovery.js +34 -7
  106. package/dist/refresh.d.ts +18 -15
  107. package/dist/refresh.d.ts.map +1 -1
  108. package/dist/refresh.js +136 -58
  109. package/dist/replica.d.ts +55 -38
  110. package/dist/replica.d.ts.map +1 -1
  111. package/dist/replica.js +278 -198
  112. package/dist/room-draft.d.ts +21 -3
  113. package/dist/room-draft.d.ts.map +1 -1
  114. package/dist/room-draft.js +102 -28
  115. package/dist/rows.d.ts +38 -31
  116. package/dist/rows.d.ts.map +1 -1
  117. package/dist/rows.js +173 -101
  118. package/dist/save.d.ts +52 -15
  119. package/dist/save.d.ts.map +1 -1
  120. package/dist/save.js +504 -182
  121. package/dist/search.d.ts +11 -43
  122. package/dist/search.d.ts.map +1 -1
  123. package/dist/search.js +0 -213
  124. package/dist/shared.d.ts +4 -3
  125. package/dist/shared.d.ts.map +1 -1
  126. package/dist/shared.js +62 -35
  127. package/dist/status.d.ts +4 -3
  128. package/dist/status.d.ts.map +1 -1
  129. package/dist/status.js +3 -1
  130. package/dist/store.d.ts +81 -101
  131. package/dist/store.d.ts.map +1 -1
  132. package/dist/store.js +300 -693
  133. package/dist/stored.d.ts +130 -0
  134. package/dist/stored.d.ts.map +1 -0
  135. package/dist/stored.js +560 -0
  136. package/dist/tables.d.ts +325 -0
  137. package/dist/tables.d.ts.map +1 -0
  138. package/dist/tables.js +578 -0
  139. package/dist/transfer.d.ts +0 -5
  140. package/dist/transfer.d.ts.map +1 -1
  141. package/dist/turn.d.ts +2 -2
  142. package/dist/turn.d.ts.map +1 -1
  143. package/dist/turn.js +34 -23
  144. package/dist/validation.d.ts +29 -0
  145. package/dist/validation.d.ts.map +1 -0
  146. package/dist/validation.js +101 -0
  147. package/dist/windows.d.ts +2 -6
  148. package/dist/windows.d.ts.map +1 -1
  149. package/dist/windows.js +30 -34
  150. package/dist/wire.d.ts +17 -5
  151. package/dist/wire.d.ts.map +1 -1
  152. package/dist/wire.js +5 -2
  153. package/dist/words.d.ts +19 -0
  154. package/dist/words.d.ts.map +1 -0
  155. package/dist/words.js +156 -0
  156. package/package.json +18 -19
  157. package/AGENTS.md +0 -299
  158. package/dist/chat/acts.d.ts +0 -33
  159. package/dist/chat/acts.d.ts.map +0 -1
  160. package/dist/chat/acts.js +0 -36
  161. package/dist/chat/context.d.ts +0 -3
  162. package/dist/chat/context.d.ts.map +0 -1
  163. package/dist/chat/context.js +0 -10
  164. package/dist/chat/earlier.d.ts +0 -10
  165. package/dist/chat/earlier.d.ts.map +0 -1
  166. package/dist/chat/earlier.js +0 -54
  167. package/dist/chat/input.d.ts +0 -9
  168. package/dist/chat/input.d.ts.map +0 -1
  169. package/dist/chat/input.js +0 -99
  170. package/dist/chat/kit.d.ts +0 -61
  171. package/dist/chat/kit.d.ts.map +0 -1
  172. package/dist/chat/kit.js +0 -123
  173. package/dist/chat/lasting.d.ts +0 -6
  174. package/dist/chat/lasting.d.ts.map +0 -1
  175. package/dist/chat/lasting.js +0 -19
  176. package/dist/chat/list.d.ts +0 -27
  177. package/dist/chat/list.d.ts.map +0 -1
  178. package/dist/chat/list.js +0 -99
  179. package/dist/chat/markdown.d.ts +0 -20
  180. package/dist/chat/markdown.d.ts.map +0 -1
  181. package/dist/chat/markdown.js +0 -188
  182. package/dist/chat/message.d.ts +0 -6
  183. package/dist/chat/message.d.ts.map +0 -1
  184. package/dist/chat/message.js +0 -192
  185. package/dist/chat/messages.d.ts +0 -22
  186. package/dist/chat/messages.d.ts.map +0 -1
  187. package/dist/chat/messages.js +0 -155
  188. package/dist/chat/name.d.ts +0 -6
  189. package/dist/chat/name.d.ts.map +0 -1
  190. package/dist/chat/name.js +0 -37
  191. package/dist/chat/opened.d.ts +0 -11
  192. package/dist/chat/opened.d.ts.map +0 -1
  193. package/dist/chat/opened.js +0 -17
  194. package/dist/chat/overlay.d.ts +0 -31
  195. package/dist/chat/overlay.d.ts.map +0 -1
  196. package/dist/chat/overlay.js +0 -97
  197. package/dist/chat/parts.d.ts +0 -57
  198. package/dist/chat/parts.d.ts.map +0 -1
  199. package/dist/chat/parts.js +0 -119
  200. package/dist/chat/process.d.ts +0 -20
  201. package/dist/chat/process.d.ts.map +0 -1
  202. package/dist/chat/process.js +0 -32
  203. package/dist/chat/provider.d.ts +0 -63
  204. package/dist/chat/provider.d.ts.map +0 -1
  205. package/dist/chat/provider.js +0 -628
  206. package/dist/chat/question.d.ts +0 -9
  207. package/dist/chat/question.d.ts.map +0 -1
  208. package/dist/chat/question.js +0 -69
  209. package/dist/chat/questions.d.ts +0 -31
  210. package/dist/chat/questions.d.ts.map +0 -1
  211. package/dist/chat/questions.js +0 -111
  212. package/dist/chat/read.d.ts +0 -5
  213. package/dist/chat/read.d.ts.map +0 -1
  214. package/dist/chat/read.js +0 -13
  215. package/dist/chat/ready.d.ts +0 -34
  216. package/dist/chat/ready.d.ts.map +0 -1
  217. package/dist/chat/ready.js +0 -91
  218. package/dist/chat/style.d.ts +0 -4
  219. package/dist/chat/style.d.ts.map +0 -1
  220. package/dist/chat/style.js +0 -302
  221. package/dist/chat.d.ts +0 -14
  222. package/dist/chat.d.ts.map +0 -1
  223. package/dist/chat.js +0 -8
  224. package/dist/read-side.d.ts +0 -27
  225. package/dist/read-side.d.ts.map +0 -1
  226. package/dist/read-side.js +0 -824
  227. package/dist/reads.d.ts +0 -11
  228. package/dist/reads.d.ts.map +0 -1
  229. package/dist/reads.js +0 -98
@@ -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,14 +325,13 @@ 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);
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();
290
335
  }
291
336
  export function partsAfter(markdown, parts) {
292
337
  const fields = named(parts.fields);
@@ -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,39 +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
- folder: string;
17
- text: string | null;
18
- change: HeadChange | null;
19
- fileId: string | null;
20
- 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;
21
10
  };
22
- export type ReferenceRow = {
23
- key: string;
24
- kind: 'reference';
25
- room: string;
26
- source: string;
27
- target: string;
28
- field: string;
29
- forms: readonly string[];
30
- path: string;
31
- 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[];
32
29
  };
33
- export type Row = DocumentRow | ReferenceRow;
34
30
  export declare function documentKey(room: string, path: string): string;
35
31
  export declare function roomOfKey(key: string): string;
32
+ export declare function pathOfKey(key: string): string;
36
33
  export declare function markdownPath(path: string): boolean;
37
- export declare function rowsOf(room: string, path: string, document: Frontmatter | null, composition: Composition, change: HeadChange | null, fileId: string | 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;
38
45
  export {};
39
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,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IACpB,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,SAAS,OAAO,EAAE,CAAC,CAAC,CAAC;IACtD,WAAW,EAAE,SAAS,MAAM,EAAE,CAAC;IAC/B,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IACpB,MAAM,EAAE,UAAU,GAAG,IAAI,CAAC;IAC1B,MAAM,EA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AgB,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,125 +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 folderOf(path) {
68
- return path.slice(0, Math.max(path.lastIndexOf('/'), 0));
69
- }
70
- function directoriesOf(path) {
71
- const parts = path.split('/').slice(0, -1);
72
- return ['', ...parts.map((_, index) => parts.slice(0, index + 1).join('/'))];
73
- }
74
- export function markdownPath(path) {
75
- return path.endsWith('.md');
76
- }
77
- export function rowsOf(room, path, document, composition, change, fileId, binary) {
78
- const key = documentKey(room, path);
79
- if (document === null || !markdownPath(path)) {
80
- return [
81
- {
82
- key,
83
- kind: 'document',
84
- room,
85
- path,
86
- type: null,
87
- fields: {},
88
- values: {},
89
- reached: {},
90
- directories: directoriesOf(path),
91
- folder: folderOf(path),
92
- text: null,
93
- change,
94
- fileId,
95
- binary,
96
- },
97
- ];
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: '' });
98
148
  }
99
- const type = document.type ?? 'any';
149
+ return found;
150
+ }
151
+ function typedRows(path, content, document, composition) {
100
152
  const definition = definitionOf(governing(composition, path), document.type);
101
153
  const reading = {
102
154
  path,
103
155
  references: new Set(definition === undefined ? [] : references(document, definition).map((one) => one.pointer)),
104
- values: {},
105
- reached: {},
106
- targets: new Map(),
156
+ values: [],
157
+ links: [],
107
158
  };
108
159
  for (const [name, value] of Object.entries(document.fields)) {
109
160
  const segment = escapePointer(name);
110
- 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
+ });
168
+ }
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 };
111
186
  }
112
- const { values, reached } = reading;
113
- return [
114
- {
115
- key,
116
- kind: 'document',
117
- room,
118
- path,
119
- type,
120
- fields: document.fields,
121
- values,
122
- reached,
123
- directories: directoriesOf(path),
124
- folder: folderOf(path),
125
- text: document.content,
126
- change,
127
- fileId,
128
- binary,
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),
129
200
  },
130
- ...[...reading.targets.values()].map(({ field, target: found, forms }) => ({
131
- key: keyOf('r', room, path, field, found),
132
- kind: 'reference',
133
- room,
134
- source: key,
135
- target: documentKey(room, found),
136
- field,
137
- forms: [...forms].sort(),
138
- path,
139
- type,
140
- })),
141
- ];
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 };
142
214
  }