@agentshouse/mdruntime 0.1.0-alpha.9 → 0.1.0-c037654ea7e04

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 (133) hide show
  1. package/AGENTS.md +232 -0
  2. package/README.md +44 -0
  3. package/dist/adapter.d.ts +207 -36
  4. package/dist/adapter.d.ts.map +1 -1
  5. package/dist/adapter.js +1 -10
  6. package/dist/attached-model.d.ts +11 -0
  7. package/dist/attached-model.d.ts.map +1 -0
  8. package/dist/attached-model.js +34 -0
  9. package/dist/attachment.d.ts +3 -0
  10. package/dist/attachment.d.ts.map +1 -0
  11. package/dist/attachment.js +8 -0
  12. package/dist/bootstrap.d.ts +7 -9
  13. package/dist/bootstrap.d.ts.map +1 -1
  14. package/dist/bootstrap.js +30 -28
  15. package/dist/change-set.d.ts +33 -0
  16. package/dist/change-set.d.ts.map +1 -0
  17. package/dist/change-set.js +83 -0
  18. package/dist/change.d.ts +9 -0
  19. package/dist/change.d.ts.map +1 -1
  20. package/dist/change.js +6 -5
  21. package/dist/client.d.ts +39 -32
  22. package/dist/client.d.ts.map +1 -1
  23. package/dist/client.js +114 -183
  24. package/dist/collection.d.ts +13 -16
  25. package/dist/collection.d.ts.map +1 -1
  26. package/dist/collection.js +45 -88
  27. package/dist/connection.d.ts +51 -46
  28. package/dist/connection.d.ts.map +1 -1
  29. package/dist/connection.js +1551 -1161
  30. package/dist/conversation.d.ts +57 -24
  31. package/dist/conversation.d.ts.map +1 -1
  32. package/dist/conversation.js +631 -330
  33. package/dist/durability.js +6 -6
  34. package/dist/file.d.ts +2 -2
  35. package/dist/file.d.ts.map +1 -1
  36. package/dist/file.js +48 -71
  37. package/dist/host.d.ts +12 -2
  38. package/dist/host.d.ts.map +1 -1
  39. package/dist/host.js +97 -119
  40. package/dist/identity.d.ts +1 -0
  41. package/dist/identity.d.ts.map +1 -1
  42. package/dist/identity.js +10 -9
  43. package/dist/index.d.ts +12 -11
  44. package/dist/index.d.ts.map +1 -1
  45. package/dist/index.js +1 -3
  46. package/dist/log.js +5 -6
  47. package/dist/memory.d.ts +16 -7
  48. package/dist/memory.d.ts.map +1 -1
  49. package/dist/memory.js +267 -220
  50. package/dist/model.d.ts +7 -29
  51. package/dist/model.d.ts.map +1 -1
  52. package/dist/model.js +19 -21
  53. package/dist/observation.d.ts +27 -0
  54. package/dist/observation.d.ts.map +1 -0
  55. package/dist/observation.js +95 -0
  56. package/dist/outcome.d.ts +10 -29
  57. package/dist/outcome.d.ts.map +1 -1
  58. package/dist/outcome.js +1 -23
  59. package/dist/overlay.d.ts +9 -2
  60. package/dist/overlay.d.ts.map +1 -1
  61. package/dist/overlay.js +187 -43
  62. package/dist/paths.d.ts +0 -2
  63. package/dist/paths.d.ts.map +1 -1
  64. package/dist/paths.js +6 -29
  65. package/dist/query.d.ts +215 -10
  66. package/dist/query.d.ts.map +1 -1
  67. package/dist/query.js +429 -15
  68. package/dist/reach.d.ts +40 -0
  69. package/dist/reach.d.ts.map +1 -0
  70. package/dist/reach.js +270 -0
  71. package/dist/react.d.ts +31 -0
  72. package/dist/react.d.ts.map +1 -0
  73. package/dist/react.js +62 -0
  74. package/dist/read-side.d.ts +18 -0
  75. package/dist/read-side.d.ts.map +1 -0
  76. package/dist/read-side.js +377 -0
  77. package/dist/refresh.d.ts +5 -5
  78. package/dist/refresh.d.ts.map +1 -1
  79. package/dist/refresh.js +49 -30
  80. package/dist/replica.d.ts +25 -5
  81. package/dist/replica.d.ts.map +1 -1
  82. package/dist/replica.js +227 -38
  83. package/dist/room-draft.d.ts +127 -0
  84. package/dist/room-draft.d.ts.map +1 -0
  85. package/dist/room-draft.js +329 -0
  86. package/dist/rows.d.ts +41 -0
  87. package/dist/rows.d.ts.map +1 -0
  88. package/dist/rows.js +97 -0
  89. package/dist/save.d.ts +7 -4
  90. package/dist/save.d.ts.map +1 -1
  91. package/dist/save.js +314 -356
  92. package/dist/search.d.ts +61 -0
  93. package/dist/search.d.ts.map +1 -0
  94. package/dist/search.js +125 -0
  95. package/dist/status.d.ts +4 -6
  96. package/dist/status.d.ts.map +1 -1
  97. package/dist/status.js +9 -13
  98. package/dist/store.d.ts +60 -32
  99. package/dist/store.d.ts.map +1 -1
  100. package/dist/store.js +338 -134
  101. package/dist/transfer.d.ts +5 -0
  102. package/dist/transfer.d.ts.map +1 -1
  103. package/dist/transfer.js +1 -5
  104. package/dist/wire.d.ts +52 -16
  105. package/dist/wire.d.ts.map +1 -1
  106. package/dist/wire.js +78 -108
  107. package/package.json +17 -11
  108. package/dist/adapter.js.map +0 -1
  109. package/dist/bootstrap.js.map +0 -1
  110. package/dist/change.js.map +0 -1
  111. package/dist/client.js.map +0 -1
  112. package/dist/collection.js.map +0 -1
  113. package/dist/connection.js.map +0 -1
  114. package/dist/conversation.js.map +0 -1
  115. package/dist/durability.js.map +0 -1
  116. package/dist/file.js.map +0 -1
  117. package/dist/host.js.map +0 -1
  118. package/dist/identity.js.map +0 -1
  119. package/dist/index.js.map +0 -1
  120. package/dist/log.js.map +0 -1
  121. package/dist/memory.js.map +0 -1
  122. package/dist/model.js.map +0 -1
  123. package/dist/outcome.js.map +0 -1
  124. package/dist/overlay.js.map +0 -1
  125. package/dist/paths.js.map +0 -1
  126. package/dist/query.js.map +0 -1
  127. package/dist/refresh.js.map +0 -1
  128. package/dist/replica.js.map +0 -1
  129. package/dist/save.js.map +0 -1
  130. package/dist/status.js.map +0 -1
  131. package/dist/store.js.map +0 -1
  132. package/dist/transfer.js.map +0 -1
  133. package/dist/wire.js.map +0 -1
package/dist/overlay.js CHANGED
@@ -1,4 +1,12 @@
1
1
  import { apply, parse, serialize } from '@agentshouse/mdmodel';
2
+ export function sameChange(left, right) {
3
+ if (left === undefined || right === undefined)
4
+ return left === right;
5
+ return (left.time === right.time &&
6
+ left.author?.kind === right.author?.kind &&
7
+ left.author?.id === right.author?.id &&
8
+ left.author?.name === right.author?.name);
9
+ }
2
10
  const EMPTY_FRONTMATTER = '---\n---\n';
3
11
  function at(path, name) {
4
12
  return `${path} ${name}`;
@@ -7,7 +15,7 @@ function priorsOf(preconditions) {
7
15
  const fields = new Map();
8
16
  const sections = new Map();
9
17
  const preambles = new Map();
10
- for (const precondition of preconditions){
18
+ for (const precondition of preconditions) {
11
19
  if (precondition.kind === 'field') {
12
20
  fields.set(at(precondition.path, precondition.name), precondition.value);
13
21
  }
@@ -19,25 +27,24 @@ function priorsOf(preconditions) {
19
27
  }
20
28
  }
21
29
  return {
22
- field: (path, name)=>fields.get(at(path, name)),
23
- section: (path, name)=>sections.get(at(path, name)) ?? '',
24
- preamble: (path)=>preambles.get(path) ?? ''
30
+ field: (path, name) => fields.get(at(path, name)),
31
+ section: (path, name) => sections.get(at(path, name)) ?? '',
32
+ preamble: (path) => preambles.get(path) ?? '',
25
33
  };
26
34
  }
27
35
  function compose(fields, sections) {
28
- const body = Object.entries(sections).map(([name, value])=>`\n## ${name}\n${value}`).join('');
36
+ const body = Object.entries(sections)
37
+ .map(([name, value]) => `\n## ${name}\n${value}`)
38
+ .join('');
29
39
  const skeleton = `${EMPTY_FRONTMATTER}${body}`;
30
40
  const opened = parse(skeleton);
31
- if (!opened.ok) return skeleton;
41
+ if (!opened.ok)
42
+ return skeleton;
32
43
  let document = opened.document;
33
- for (const [name, value] of Object.entries(fields)){
34
- const next = apply(document, {
35
- kind: 'field',
36
- name,
37
- value,
38
- prior: undefined
39
- });
40
- if (next.outcome === 'applied') document = next.document;
44
+ for (const [name, value] of Object.entries(fields)) {
45
+ const next = apply(document, { kind: 'field', name, value, prior: undefined });
46
+ if (next.outcome === 'applied')
47
+ document = next.document;
41
48
  }
42
49
  return serialize(document);
43
50
  }
@@ -47,50 +54,65 @@ function edited(document, change, priors) {
47
54
  kind: 'field',
48
55
  name: change.name,
49
56
  value: change.value,
50
- prior: priors.field(change.path, change.name)
57
+ prior: priors.field(change.path, change.name),
51
58
  });
52
59
  }
53
60
  if (change.kind === 'preamble') {
54
61
  return apply(document, {
55
62
  kind: 'preamble',
56
63
  value: change.value,
57
- prior: priors.preamble(change.path)
64
+ prior: priors.preamble(change.path),
58
65
  });
59
66
  }
60
- if (change.kind !== 'section') return null;
67
+ if (change.kind !== 'section')
68
+ return null;
61
69
  return apply(document, {
62
70
  kind: 'section',
63
71
  name: change.name,
64
72
  value: change.value,
65
- prior: priors.section(change.path, change.name)
73
+ prior: priors.section(change.path, change.name),
66
74
  });
67
75
  }
68
76
  function edit(staged, change, priors) {
69
77
  const text = staged.documents[change.path];
70
- if (text === undefined) return;
78
+ if (text === undefined)
79
+ return;
71
80
  const parsed = parse(text, change.path);
72
- if (!parsed.ok) return;
81
+ if (!parsed.ok)
82
+ return;
73
83
  const next = edited(parsed.document, change, priors);
74
- if (next?.outcome === 'applied') staged.documents[change.path] = serialize(next.document);
84
+ if (next?.outcome === 'applied')
85
+ staged.documents[change.path] = serialize(next.document);
75
86
  }
76
87
  function relocate(staged, from, to) {
77
88
  const text = staged.documents[from];
78
89
  if (text !== undefined) {
79
90
  delete staged.documents[from];
80
91
  staged.documents[to] = text;
81
- } else if (staged.binaryTargets.delete(from)) {
92
+ }
93
+ else if (staged.binaryTargets.delete(from)) {
82
94
  staged.binaryTargets.add(to);
83
- } else {
95
+ }
96
+ else {
84
97
  return;
85
98
  }
86
- const revision = staged.revisions[from];
87
- if (revision === undefined) return;
88
- delete staged.revisions[from];
89
- staged.revisions[to] = revision;
99
+ moveHead(staged, from, to);
100
+ }
101
+ function moveHead(heads, from, to) {
102
+ const revision = heads.revisions[from];
103
+ const change = heads.changes[from];
104
+ delete heads.revisions[from];
105
+ delete heads.changes[from];
106
+ delete heads.changes[to];
107
+ if (revision !== undefined)
108
+ heads.revisions[to] = revision;
109
+ if (change !== undefined)
110
+ heads.changes[to] = change;
90
111
  }
91
112
  function erase(staged, path) {
92
113
  delete staged.documents[path];
93
114
  delete staged.revisions[path];
115
+ delete staged.changes[path];
94
116
  staged.binaryTargets.delete(path);
95
117
  }
96
118
  function stage(staged, change, priors) {
@@ -99,11 +121,14 @@ function stage(staged, change, priors) {
99
121
  return;
100
122
  }
101
123
  if (change.kind === 'create') {
102
- staged.documents[change.path] = compose(change.fields, change.sections);
124
+ staged.documents[change.path] =
125
+ 'content' in change ? change.content : compose(change.fields, change.sections);
126
+ delete staged.changes[change.path];
103
127
  return;
104
128
  }
105
129
  if (change.kind === 'replace') {
106
- if (staged.documents[change.path] === undefined) return;
130
+ if (staged.documents[change.path] === undefined)
131
+ return;
107
132
  staged.documents[change.path] = change.content;
108
133
  return;
109
134
  }
@@ -115,25 +140,144 @@ function stage(staged, change, priors) {
115
140
  }
116
141
  export function overlay(base, queue) {
117
142
  const staged = {
118
- documents: {
119
- ...base.documents
120
- },
143
+ documents: { ...base.documents },
121
144
  binaryTargets: new Set(base.binaryTargets),
122
- revisions: {
123
- ...base.revisions
124
- }
145
+ revisions: { ...base.revisions },
146
+ changes: { ...base.changes },
125
147
  };
126
- for (const batch of queue){
148
+ for (const batch of queue) {
127
149
  const priors = priorsOf(batch.preconditions);
128
- for (const change of batch.changes)stage(staged, change, priors);
150
+ for (const change of batch.changes)
151
+ stage(staged, change, priors);
129
152
  }
130
153
  return {
131
154
  documents: staged.documents,
132
- binaryTargets: [
133
- ...staged.binaryTargets
134
- ].sort(),
135
- revisions: staged.revisions
155
+ binaryTargets: [...staged.binaryTargets].sort(),
156
+ revisions: staged.revisions,
157
+ changes: staged.changes,
158
+ };
159
+ }
160
+ function pathsOf(batch) {
161
+ return batch.changes.flatMap((change) => change.kind === 'move' ? [change.path, change.to] : [change.path]);
162
+ }
163
+ export function sameLayerText(left, right) {
164
+ const paths = Object.keys(left.documents);
165
+ return (paths.length === Object.keys(right.documents).length &&
166
+ paths.every((path) => left.documents[path] === right.documents[path]) &&
167
+ left.binaryTargets.length === right.binaryTargets.length &&
168
+ left.binaryTargets.every((path) => right.binaryTargets.includes(path)));
169
+ }
170
+ function replayHeads(base, queue) {
171
+ const heads = { revisions: { ...base.revisions }, changes: { ...base.changes } };
172
+ for (const batch of queue) {
173
+ for (const change of batch.changes) {
174
+ if (change.kind === 'remove') {
175
+ delete heads.revisions[change.path];
176
+ delete heads.changes[change.path];
177
+ }
178
+ else if (change.kind === 'create') {
179
+ delete heads.changes[change.path];
180
+ }
181
+ else if (change.kind === 'move') {
182
+ moveHead(heads, change.path, change.to);
183
+ }
184
+ }
185
+ }
186
+ return heads;
187
+ }
188
+ export function overlayChanged(base, queue, previousBase, previousQueue, previousVisible) {
189
+ const before = previousQueue.map((batch) => JSON.stringify(batch));
190
+ const after = queue.map((batch) => JSON.stringify(batch));
191
+ if (before.length === after.length &&
192
+ before.every((id, index) => id === after[index]) &&
193
+ sameLayerText(base, previousBase)) {
194
+ return {
195
+ documents: previousVisible.documents,
196
+ binaryTargets: previousVisible.binaryTargets,
197
+ ...replayHeads(base, queue),
198
+ };
199
+ }
200
+ const affected = new Set();
201
+ const paths = new Set([
202
+ ...Object.keys(base.documents),
203
+ ...Object.keys(previousBase.documents),
204
+ ...Object.keys(base.revisions),
205
+ ...Object.keys(previousBase.revisions),
206
+ ...Object.keys(base.changes),
207
+ ...Object.keys(previousBase.changes),
208
+ ...base.binaryTargets,
209
+ ...previousBase.binaryTargets,
210
+ ]);
211
+ for (const path of paths) {
212
+ if (base.documents[path] !== previousBase.documents[path] ||
213
+ base.revisions[path] !== previousBase.revisions[path] ||
214
+ !sameChange(base.changes[path], previousBase.changes[path]) ||
215
+ base.binaryTargets.includes(path) !== previousBase.binaryTargets.includes(path))
216
+ affected.add(path);
217
+ }
218
+ const oldIds = new Set(before);
219
+ const newIds = new Set(after);
220
+ for (const batch of previousQueue) {
221
+ if (!newIds.has(JSON.stringify(batch)))
222
+ for (const path of pathsOf(batch))
223
+ affected.add(path);
224
+ }
225
+ for (const batch of queue) {
226
+ if (!oldIds.has(JSON.stringify(batch)))
227
+ for (const path of pathsOf(batch))
228
+ affected.add(path);
229
+ }
230
+ let expanded = true;
231
+ while (expanded) {
232
+ expanded = false;
233
+ for (const batch of queue) {
234
+ const touched = pathsOf(batch);
235
+ if (!touched.some((path) => affected.has(path)))
236
+ continue;
237
+ for (const path of touched) {
238
+ if (affected.has(path))
239
+ continue;
240
+ affected.add(path);
241
+ expanded = true;
242
+ }
243
+ }
244
+ }
245
+ const documents = {};
246
+ const revisions = {};
247
+ const changes = {};
248
+ for (const path of affected) {
249
+ if (base.documents[path] !== undefined)
250
+ documents[path] = base.documents[path];
251
+ if (base.revisions[path] !== undefined)
252
+ revisions[path] = base.revisions[path];
253
+ if (base.changes[path] !== undefined)
254
+ changes[path] = base.changes[path];
255
+ }
256
+ const staged = overlay({
257
+ documents,
258
+ revisions,
259
+ changes,
260
+ binaryTargets: base.binaryTargets.filter((path) => affected.has(path)),
261
+ }, queue.filter((batch) => pathsOf(batch).some((path) => affected.has(path))));
262
+ const mergedDocuments = { ...previousVisible.documents };
263
+ const mergedRevisions = { ...previousVisible.revisions };
264
+ const mergedChanges = { ...previousVisible.changes };
265
+ const mergedTargets = new Set(previousVisible.binaryTargets);
266
+ for (const path of affected) {
267
+ delete mergedDocuments[path];
268
+ delete mergedRevisions[path];
269
+ delete mergedChanges[path];
270
+ mergedTargets.delete(path);
271
+ }
272
+ Object.assign(mergedDocuments, staged.documents);
273
+ Object.assign(mergedRevisions, staged.revisions);
274
+ Object.assign(mergedChanges, staged.changes);
275
+ for (const path of staged.binaryTargets)
276
+ mergedTargets.add(path);
277
+ return {
278
+ documents: mergedDocuments,
279
+ revisions: mergedRevisions,
280
+ changes: mergedChanges,
281
+ binaryTargets: [...mergedTargets].sort(),
136
282
  };
137
283
  }
138
-
139
- //# sourceMappingURL=overlay.js.map
package/dist/paths.d.ts CHANGED
@@ -1,6 +1,4 @@
1
1
  export declare function normalize(path: string): string | null;
2
- export declare function resolve(from: string, reference: string): string | null;
3
- export declare function relative(from: string, target: string): string;
4
2
  export declare function beneath(prefix: string, path: string): boolean;
5
3
  export declare function covered(prefixes: readonly string[], path: string): boolean;
6
4
  //# sourceMappingURL=paths.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"paths.d.ts","sourceRoot":"","sources":["../src/paths.ts"],"names":[],"mappings":"AAKA,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAYrD;AAED,wBAAgB,OAAO,CAAC,IAAI,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAGtE;AAED,wBAAgB,QAAQ,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CAW7D;AAED,wBAAgB,OAAO,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAE7D;AAED,wBAAgB,OAAO,CAAC,QAAQ,EAAE,SAAS,MAAM,EAAE,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAK1E"}
1
+ {"version":3,"file":"paths.d.ts","sourceRoot":"","sources":["../src/paths.ts"],"names":[],"mappings":"AAAA,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAYrD;AAED,wBAAgB,OAAO,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAE7D;AAED,wBAAgB,OAAO,CAAC,QAAQ,EAAE,SAAS,MAAM,EAAE,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAK1E"}
package/dist/paths.js CHANGED
@@ -1,13 +1,11 @@
1
- function dirname(path) {
2
- const index = path.lastIndexOf('/');
3
- return index === -1 ? '' : path.slice(0, index);
4
- }
5
1
  export function normalize(path) {
6
2
  const parts = [];
7
- for (const part of path.split('/')){
8
- if (part === '' || part === '.') continue;
3
+ for (const part of path.split('/')) {
4
+ if (part === '' || part === '.')
5
+ continue;
9
6
  if (part === '..') {
10
- if (parts.length === 0) return null;
7
+ if (parts.length === 0)
8
+ return null;
11
9
  parts.pop();
12
10
  continue;
13
11
  }
@@ -15,33 +13,12 @@ export function normalize(path) {
15
13
  }
16
14
  return parts.join('/');
17
15
  }
18
- export function resolve(from, reference) {
19
- const base = dirname(from);
20
- return normalize(base === '' ? reference : `${base}/${reference}`);
21
- }
22
- export function relative(from, target) {
23
- const base = dirname(from).split('/').filter((part)=>part !== '');
24
- const parts = target.split('/');
25
- let shared = 0;
26
- while(shared < base.length && shared + 1 < parts.length && base[shared] === parts[shared]){
27
- shared += 1;
28
- }
29
- const up = Array.from({
30
- length: base.length - shared
31
- }, ()=>'..');
32
- return [
33
- ...up,
34
- ...parts.slice(shared)
35
- ].join('/');
36
- }
37
16
  export function beneath(prefix, path) {
38
17
  return path === prefix || path.startsWith(`${prefix}/`);
39
18
  }
40
19
  export function covered(prefixes, path) {
41
- return prefixes.some((prefix)=>{
20
+ return prefixes.some((prefix) => {
42
21
  const normalized = normalize(prefix);
43
22
  return normalized !== null && (normalized === '' || beneath(normalized, path));
44
23
  });
45
24
  }
46
-
47
- //# sourceMappingURL=paths.js.map
package/dist/query.d.ts CHANGED
@@ -1,13 +1,218 @@
1
- import type { QueryOptions, QueryResult } from '@agentshouse/query';
2
- import type { Document } from '@agentshouse/mdmodel';
3
- import type { TypesResult } from './model.js';
4
- export type QueryInput = {
1
+ import type { Definition } from '@agentshouse/mdmodel';
2
+ import type { HeadChange } from './adapter.js';
3
+ export type Value = string | number | boolean | null;
4
+ export type Predicate = Value | {
5
+ $eq: Value;
6
+ } | {
7
+ $ne: Value;
8
+ } | {
9
+ $in: readonly Value[];
10
+ } | {
11
+ $nin: readonly Value[];
12
+ } | {
13
+ $gt?: Value;
14
+ $gte?: Value;
15
+ $lt?: Value;
16
+ $lte?: Value;
17
+ } | {
18
+ $all: readonly Value[];
19
+ } | {
20
+ $size: number;
21
+ } | {
22
+ $exists: boolean;
23
+ };
24
+ export type Where = {
25
+ $and?: readonly Where[];
26
+ $or?: readonly Where[];
27
+ [field: string]: Predicate | readonly Where[] | undefined;
28
+ };
29
+ export type Related = {
30
+ types?: readonly string[];
31
+ where?: Where;
32
+ pathPrefix?: string;
33
+ };
34
+ export type Filter = {
35
+ rooms?: readonly string[];
36
+ types?: readonly string[];
37
+ where?: Where;
38
+ pathPrefix?: string;
39
+ hop?: Related & {
40
+ field: string;
41
+ };
42
+ backlinks?: Related & {
43
+ field?: string;
44
+ };
45
+ };
46
+ export type Key = 'room' | 'path' | 'type' | {
47
+ field: string;
48
+ };
49
+ export type Select = 'all' | {
50
+ fields?: 'all' | readonly string[];
51
+ sections?: 'all' | readonly string[];
52
+ preamble?: true;
53
+ markdown?: true;
54
+ backlinks?: true;
55
+ change?: true;
56
+ };
57
+ export type OrderOptions = {
58
+ nulls?: 'first' | 'last';
59
+ stringSort?: 'lexical' | 'locale';
60
+ locale?: string;
61
+ localeOptions?: Readonly<Record<string, unknown>>;
62
+ };
63
+ export type Direction = 'asc' | 'desc';
64
+ export type DocumentQuery = Filter & {
65
+ select: Select;
66
+ orderBy?: readonly ({
67
+ by: Key;
68
+ direction?: Direction;
69
+ } & OrderOptions)[];
70
+ limit?: number;
71
+ };
72
+ export type GroupQuery = Filter & {
73
+ groupBy: readonly Exclude<Key, 'path'>[];
74
+ orderBy?: readonly {
75
+ by: 'count' | Exclude<Key, 'path'>;
76
+ direction?: Direction;
77
+ }[];
78
+ limit?: number;
79
+ };
80
+ export type LocalQuery = DocumentQuery | GroupQuery;
81
+ export type Backlink = {
82
+ path: string;
83
+ type: string;
84
+ field: string;
85
+ };
86
+ export type QueryDocument = {
5
87
  room: string;
6
- roomVersion: string;
7
- documents: Readonly<Record<string, Document>>;
8
- model: TypesResult;
9
- typedSelection: TypesResult | null;
88
+ path: string;
89
+ type?: string;
90
+ fields?: Readonly<Record<string, unknown>>;
91
+ sections?: Readonly<Record<string, string>>;
92
+ preamble?: string;
93
+ markdown?: string;
94
+ backlinks?: readonly Backlink[];
95
+ change?: HeadChange | null;
96
+ };
97
+ export type QueryGroup = {
98
+ key: readonly unknown[];
99
+ count: number;
100
+ };
101
+ export type DocumentResult = {
102
+ documents: readonly QueryDocument[];
103
+ };
104
+ export type GroupResult = {
105
+ groups: readonly QueryGroup[];
106
+ };
107
+ export type QueryResult = DocumentResult | GroupResult;
108
+ export type QueryErrorCode = 'invalid-query' | 'unknown-type' | 'unknown-field' | 'wrong-kind';
109
+ export type RoomFacts = {
110
+ locator: string;
111
+ available: boolean;
112
+ types: readonly Definition[];
113
+ };
114
+ export type HeldRooms = {
115
+ listed: boolean;
116
+ rooms: readonly RoomFacts[];
117
+ };
118
+ export type Test = {
119
+ op: 'eq';
120
+ value: Value;
121
+ } | {
122
+ op: 'ne';
123
+ value: Value;
124
+ } | {
125
+ op: 'in';
126
+ values: readonly Value[];
127
+ } | {
128
+ op: 'nin';
129
+ values: readonly Value[];
130
+ } | {
131
+ op: 'range';
132
+ bounds: readonly (readonly ['gt' | 'gte' | 'lt' | 'lte', Value])[];
133
+ } | {
134
+ op: 'all';
135
+ values: readonly Value[];
136
+ } | {
137
+ op: 'size';
138
+ size: number;
139
+ } | {
140
+ op: 'exists';
141
+ exists: boolean;
142
+ };
143
+ export type Condition = {
144
+ every: readonly Condition[];
145
+ } | {
146
+ some: readonly Condition[];
147
+ } | {
148
+ field: string;
149
+ test: Test;
150
+ };
151
+ export type Scope = {
152
+ types: readonly string[] | null;
153
+ where: Condition | null;
154
+ pathPrefix: string | null;
155
+ };
156
+ export type KeyPlan = {
157
+ of: 'room';
158
+ } | {
159
+ of: 'path';
160
+ } | {
161
+ of: 'type';
162
+ } | {
163
+ of: 'field';
164
+ field: string;
165
+ };
166
+ export type DocumentShape = {
167
+ kind: 'documents';
168
+ fields: 'all' | readonly string[] | null;
169
+ sections: 'all' | readonly string[] | null;
170
+ preamble: boolean;
171
+ markdown: boolean;
172
+ backlinks: boolean;
173
+ change: boolean;
174
+ orderBy: readonly {
175
+ key: KeyPlan;
176
+ options: OrderOptions & {
177
+ direction: Direction;
178
+ };
179
+ }[];
180
+ };
181
+ export type GroupShape = {
182
+ kind: 'groups';
183
+ groupBy: readonly KeyPlan[];
184
+ orderBy: readonly {
185
+ by: 'count' | number;
186
+ direction: Direction;
187
+ }[];
188
+ };
189
+ export type PlanReach = {
190
+ rooms: readonly string[];
191
+ documents: readonly string[];
192
+ };
193
+ export type Plan = {
194
+ rooms: readonly string[] | null;
195
+ reach: PlanReach | null;
196
+ root: Scope;
197
+ hop: (Scope & {
198
+ field: string;
199
+ }) | null;
200
+ backlinks: (Scope & {
201
+ field: string | null;
202
+ }) | null;
203
+ shape: DocumentShape | GroupShape;
204
+ limit: number | null;
10
205
  };
11
- export type QueryBinding = (rooms: readonly QueryInput[], options: QueryOptions) => QueryResult;
12
- export declare function rawQuery(rooms: readonly QueryInput[]): QueryResult;
206
+ export declare class QueryError extends Error {
207
+ constructor(code: QueryErrorCode, at: string, problem: string);
208
+ }
209
+ type Entries = Readonly<Record<string, unknown>>;
210
+ export declare function invalid(at: string, problem: string): never;
211
+ export declare function plain(value: unknown): value is Entries;
212
+ export declare function known(value: Entries, allowed: readonly string[], at: string): void;
213
+ export declare function planOf(input: unknown, facts: HeldRooms, reach?: PlanReach | null): Plan;
214
+ export declare function limitOf(input: unknown): number | null;
215
+ export declare function filterOf(input: unknown, facts: HeldRooms, reach?: PlanReach | null): Plan;
216
+ export declare function identityOf(plan: Plan): string;
217
+ export {};
13
218
  //# sourceMappingURL=query.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"query.d.ts","sourceRoot":"","sources":["../src/query.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AACpE,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AACrD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAE9C,MAAM,MAAM,UAAU,GAAG;IACvB,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,MAAM,CAAC;IACpB,SAAS,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC,CAAC;IA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