@agentshouse/mdruntime 0.1.0-cefbc8bb8a1c0 → 0.1.0-cf1e7fdb9c600
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.
- package/README.md +1 -1
- package/dist/adapter.d.ts +69 -49
- package/dist/adapter.d.ts.map +1 -1
- package/dist/bootstrap.d.ts +5 -8
- package/dist/bootstrap.d.ts.map +1 -1
- package/dist/bootstrap.js +20 -21
- package/dist/chain.d.ts +2 -1
- package/dist/chain.d.ts.map +1 -1
- package/dist/client.d.ts +6 -5
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +0 -1
- package/dist/collection.d.ts +2 -4
- package/dist/collection.d.ts.map +1 -1
- package/dist/collection.js +1 -17
- package/dist/compile.d.ts +1 -6
- package/dist/compile.d.ts.map +1 -1
- package/dist/compile.js +97 -281
- package/dist/connection.d.ts +5 -7
- package/dist/connection.d.ts.map +1 -1
- package/dist/connection.js +816 -846
- package/dist/context.d.ts +2 -0
- package/dist/context.d.ts.map +1 -0
- package/dist/context.js +7 -0
- package/dist/conversation.d.ts +15 -13
- package/dist/conversation.d.ts.map +1 -1
- package/dist/conversation.js +133 -142
- package/dist/durability.d.ts +0 -1
- package/dist/durability.d.ts.map +1 -1
- package/dist/entry.d.ts +2 -2
- package/dist/entry.d.ts.map +1 -1
- package/dist/entry.js +14 -5
- package/dist/host.d.ts +3 -7
- package/dist/host.d.ts.map +1 -1
- package/dist/host.js +12 -26
- package/dist/index.d.ts +9 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -1
- package/dist/job.d.ts +14 -0
- package/dist/job.d.ts.map +1 -0
- package/dist/job.js +28 -0
- package/dist/live.d.ts +2 -1
- package/dist/live.d.ts.map +1 -1
- package/dist/live.js +10 -7
- package/dist/memory.d.ts +9 -4
- package/dist/memory.d.ts.map +1 -1
- package/dist/memory.js +132 -81
- package/dist/messages.d.ts +2 -2
- package/dist/messages.d.ts.map +1 -1
- package/dist/messages.js +15 -16
- package/dist/model.d.ts +1 -3
- package/dist/model.d.ts.map +1 -1
- package/dist/model.js +0 -12
- package/dist/observation.d.ts +2 -2
- package/dist/observation.d.ts.map +1 -1
- package/dist/observation.js +7 -2
- package/dist/outcome.d.ts +0 -3
- package/dist/outcome.d.ts.map +1 -1
- package/dist/query.d.ts +5 -209
- package/dist/query.d.ts.map +1 -1
- package/dist/query.js +3 -505
- package/dist/reach.d.ts +7 -7
- package/dist/reach.d.ts.map +1 -1
- package/dist/reach.js +31 -44
- package/dist/react.d.ts +3 -1
- package/dist/react.d.ts.map +1 -1
- package/dist/readiness.d.ts +2 -15
- package/dist/readiness.d.ts.map +1 -1
- package/dist/readiness.js +4 -10
- package/dist/refresh.d.ts +7 -5
- package/dist/refresh.d.ts.map +1 -1
- package/dist/refresh.js +46 -6
- package/dist/replica.d.ts +2 -2
- package/dist/replica.d.ts.map +1 -1
- package/dist/replica.js +5 -5
- package/dist/room-draft.d.ts +2 -1
- package/dist/room-draft.d.ts.map +1 -1
- package/dist/room-draft.js +65 -52
- package/dist/rows.d.ts +3 -19
- package/dist/rows.d.ts.map +1 -1
- package/dist/rows.js +36 -164
- package/dist/search.d.ts +12 -63
- package/dist/search.d.ts.map +1 -1
- package/dist/search.js +9 -52
- package/dist/shared.d.ts +0 -1
- package/dist/shared.d.ts.map +1 -1
- package/dist/shared.js +0 -10
- package/dist/status.d.ts +0 -4
- package/dist/status.d.ts.map +1 -1
- package/dist/store.d.ts +15 -19
- package/dist/store.d.ts.map +1 -1
- package/dist/store.js +40 -39
- package/dist/stored.d.ts +33 -6
- package/dist/stored.d.ts.map +1 -1
- package/dist/stored.js +87 -124
- package/dist/tables.d.ts +34 -33
- package/dist/tables.d.ts.map +1 -1
- package/dist/tables.js +21 -54
- package/dist/transfer.d.ts +0 -5
- package/dist/transfer.d.ts.map +1 -1
- package/dist/validation.d.ts +3 -4
- package/dist/validation.d.ts.map +1 -1
- package/dist/validation.js +39 -97
- package/dist/windows.d.ts +1 -12
- package/dist/windows.d.ts.map +1 -1
- package/dist/windows.js +2 -75
- package/dist/wire.d.ts +1 -4
- package/dist/wire.d.ts.map +1 -1
- package/dist/wire.js +1 -1
- package/package.json +2 -2
- package/dist/fulltext.d.ts +0 -27
- package/dist/fulltext.d.ts.map +0 -1
- package/dist/fulltext.js +0 -370
- package/dist/reads.d.ts +0 -11
- package/dist/reads.d.ts.map +0 -1
- package/dist/reads.js +0 -91
- package/dist/words.d.ts +0 -19
- package/dist/words.d.ts.map +0 -1
- package/dist/words.js +0 -156
package/dist/query.js
CHANGED
|
@@ -1,506 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
import { decodedCursor } from './windows.js';
|
|
5
|
-
export class QueryError extends Error {
|
|
6
|
-
code;
|
|
7
|
-
retryable = false;
|
|
8
|
-
constructor(code, at, problem) {
|
|
9
|
-
super(`${code} at ${at}: ${problem}`);
|
|
10
|
-
this.code = code;
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
const RANGE = ['$gt', '$gte', '$lt', '$lte'];
|
|
14
|
-
const SINGLE = ['$eq', '$ne', '$in', '$nin', '$all', '$size', '$exists'];
|
|
15
|
-
const FILTER = ['rooms', 'types', 'where', 'pathPrefix', 'folder', 'paths', 'hop', 'backlinks'];
|
|
16
|
-
const RELATED = ['types', 'where', 'pathPrefix', 'field'];
|
|
17
|
-
const PARTS = [
|
|
18
|
-
'fields',
|
|
19
|
-
'sections',
|
|
20
|
-
'preamble',
|
|
21
|
-
'markdown',
|
|
22
|
-
'backlinks',
|
|
23
|
-
'change',
|
|
24
|
-
'fileId',
|
|
25
|
-
'revision',
|
|
26
|
-
];
|
|
27
|
-
const ORDER = ['by', 'direction', 'nulls', 'stringSort', 'locale', 'localeOptions'];
|
|
28
|
-
const CALENDAR_DATE = /^(\d{4})-(\d{2})-(\d{2})$/;
|
|
29
|
-
const STRAY_ESCAPE = /~(?![01])/u;
|
|
30
|
-
export function invalid(at, problem) {
|
|
31
|
-
throw new QueryError('invalid-query', at, problem);
|
|
32
|
-
}
|
|
33
|
-
function place(at, name) {
|
|
34
|
-
return at === '' ? name : `${at}.${name}`;
|
|
35
|
-
}
|
|
36
|
-
export function plain(value) {
|
|
37
|
-
return (typeof value === 'object' &&
|
|
38
|
-
value !== null &&
|
|
39
|
-
!Array.isArray(value) &&
|
|
40
|
-
(Object.getPrototypeOf(value) === Object.prototype || Object.getPrototypeOf(value) === null));
|
|
41
|
-
}
|
|
42
|
-
export function known(value, allowed, at) {
|
|
43
|
-
for (const name of Object.keys(value)) {
|
|
44
|
-
if (!allowed.includes(name))
|
|
45
|
-
invalid(place(at, name), 'unknown part');
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
function list(input, at) {
|
|
49
|
-
if (!Array.isArray(input))
|
|
50
|
-
invalid(at, 'expects a list');
|
|
51
|
-
return input;
|
|
52
|
-
}
|
|
53
|
-
function value(input, at) {
|
|
54
|
-
if (Array.isArray(input))
|
|
55
|
-
invalid(at, 'a list is not a value');
|
|
56
|
-
if (input !== null &&
|
|
57
|
-
typeof input !== 'string' &&
|
|
58
|
-
typeof input !== 'boolean' &&
|
|
59
|
-
!(typeof input === 'number' && Number.isFinite(input))) {
|
|
60
|
-
invalid(at, 'not a value');
|
|
61
|
-
}
|
|
62
|
-
return input;
|
|
63
|
-
}
|
|
64
|
-
function values(input, at) {
|
|
65
|
-
return list(input, at).map((item, index) => value(item, `${at}[${index}]`));
|
|
66
|
-
}
|
|
67
|
-
function names(input, at) {
|
|
68
|
-
const listed = list(input, at);
|
|
69
|
-
if (listed.some((item) => typeof item !== 'string'))
|
|
70
|
-
invalid(at, 'expects a list of names');
|
|
71
|
-
return [...new Set(listed)].sort();
|
|
72
|
-
}
|
|
73
|
-
function pathsOf(input) {
|
|
74
|
-
const listed = list(input, 'paths');
|
|
75
|
-
if (listed.some((item) => typeof item !== 'string' || item === '' || normalize(item) !== item)) {
|
|
76
|
-
invalid('paths', 'expects a list of Paths');
|
|
77
|
-
}
|
|
78
|
-
return [...new Set(listed)].sort();
|
|
79
|
-
}
|
|
80
|
-
function directory(input, at) {
|
|
81
|
-
if (typeof input !== 'string' || normalize(input) !== input) {
|
|
82
|
-
invalid(at, 'expects a normalized directory');
|
|
83
|
-
}
|
|
84
|
-
return input;
|
|
85
|
-
}
|
|
86
|
-
function calendarDate(value) {
|
|
87
|
-
const match = CALENDAR_DATE.exec(value);
|
|
88
|
-
if (match === null)
|
|
89
|
-
return false;
|
|
90
|
-
const [year, month, day] = match.slice(1).map(Number);
|
|
91
|
-
const date = new Date(Date.UTC(year, month - 1, day));
|
|
92
|
-
return (date.getUTCFullYear() === year && date.getUTCMonth() === month - 1 && date.getUTCDate() === day);
|
|
93
|
-
}
|
|
94
|
-
function itemOf(facts) {
|
|
95
|
-
return facts.type === 'array' ? facts.items : facts;
|
|
96
|
-
}
|
|
97
|
-
function fits(facts, value) {
|
|
98
|
-
const item = itemOf(facts);
|
|
99
|
-
if (item?.type === undefined)
|
|
100
|
-
return true;
|
|
101
|
-
if (item.values !== undefined)
|
|
102
|
-
return item.values.includes(value);
|
|
103
|
-
if (item.type === 'number')
|
|
104
|
-
return typeof value === 'number';
|
|
105
|
-
if (item.type === 'integer')
|
|
106
|
-
return Number.isSafeInteger(value);
|
|
107
|
-
if (item.type === 'boolean')
|
|
108
|
-
return typeof value === 'boolean';
|
|
109
|
-
if (item.type !== 'string' || typeof value !== 'string')
|
|
110
|
-
return false;
|
|
111
|
-
if (item.reference !== undefined)
|
|
112
|
-
return value !== '' && normalize(value) === value;
|
|
113
|
-
if (item.format === 'date')
|
|
114
|
-
return calendarDate(value);
|
|
115
|
-
return true;
|
|
116
|
-
}
|
|
117
|
-
function typing(facts, rooms, types, at) {
|
|
118
|
-
const read = rooms === null ? facts.rooms : facts.rooms.filter((room) => rooms.includes(room.locator));
|
|
119
|
-
const acquiring = !facts.listed &&
|
|
120
|
-
(rooms === null ||
|
|
121
|
-
rooms.some((locator) => !facts.rooms.some((room) => room.locator === locator)));
|
|
122
|
-
const available = !acquiring && read.every((room) => room.available);
|
|
123
|
-
const declared = read.flatMap((room) => room.types);
|
|
124
|
-
for (const name of types ?? []) {
|
|
125
|
-
if (name === 'any' || !available || declared.some((type) => type.name === name))
|
|
126
|
-
continue;
|
|
127
|
-
throw new QueryError('unknown-type', place(at, 'types'), `no model it reads declares ${name}`);
|
|
128
|
-
}
|
|
129
|
-
return {
|
|
130
|
-
untyped: types === null || types.includes('any') || !available,
|
|
131
|
-
types: types === null ? declared : declared.filter((type) => types.includes(type.name)),
|
|
132
|
-
};
|
|
133
|
-
}
|
|
134
|
-
function pointer(input, at) {
|
|
135
|
-
if (typeof input !== 'string' || !input.startsWith('/') || STRAY_ESCAPE.test(input)) {
|
|
136
|
-
invalid(at, 'expects a JSON Pointer');
|
|
137
|
-
}
|
|
138
|
-
return input;
|
|
139
|
-
}
|
|
140
|
-
function specs(typed, input, at) {
|
|
141
|
-
const field = pointer(input, at);
|
|
142
|
-
const declared = typed.types.flatMap((type) => {
|
|
143
|
-
const spec = pointerFacts(type, field);
|
|
144
|
-
return spec === undefined ? [] : [spec];
|
|
145
|
-
});
|
|
146
|
-
if (declared.length === 0 && !typed.untyped) {
|
|
147
|
-
throw new QueryError('unknown-field', at, `no MD Type it reads declares ${field}`);
|
|
148
|
-
}
|
|
149
|
-
if (!declared.some((spec) => spec.conditional))
|
|
150
|
-
return declared;
|
|
151
|
-
return declared.map((spec) => ({
|
|
152
|
-
facts: { prose: false },
|
|
153
|
-
through: several(spec),
|
|
154
|
-
conditional: true,
|
|
155
|
-
}));
|
|
156
|
-
}
|
|
157
|
-
function several(spec) {
|
|
158
|
-
return spec.through || spec.facts.type === 'array';
|
|
159
|
-
}
|
|
160
|
-
function single(spec) {
|
|
161
|
-
return spec.facts.type !== undefined && spec.facts.type !== 'array';
|
|
162
|
-
}
|
|
163
|
-
function every(declared, holds) {
|
|
164
|
-
return declared.length > 0 && declared.every(holds);
|
|
165
|
-
}
|
|
166
|
-
function kind(declared, checked, at) {
|
|
167
|
-
if (checked === null ||
|
|
168
|
-
declared.length === 0 ||
|
|
169
|
-
declared.some((spec) => fits(spec.facts, checked))) {
|
|
170
|
-
return;
|
|
171
|
-
}
|
|
172
|
-
throw new QueryError('wrong-kind', at, `${JSON.stringify(checked)} is not of its field's kind`);
|
|
173
|
-
}
|
|
174
|
-
function test(declared, input, at) {
|
|
175
|
-
if (!plain(input)) {
|
|
176
|
-
const equal = value(input, at);
|
|
177
|
-
kind(declared, equal, at);
|
|
178
|
-
return { op: 'eq', value: equal };
|
|
179
|
-
}
|
|
180
|
-
const operators = Object.keys(input);
|
|
181
|
-
if (operators.length === 0)
|
|
182
|
-
invalid(at, 'names no operator');
|
|
183
|
-
if (operators.some((operator) => RANGE.includes(operator))) {
|
|
184
|
-
const stray = operators.find((operator) => !RANGE.includes(operator));
|
|
185
|
-
if (stray !== undefined)
|
|
186
|
-
invalid(place(at, stray), 'cannot join a range');
|
|
187
|
-
if (every(declared, (spec) => spec.facts.type === 'array'))
|
|
188
|
-
invalid(at, 'a range on a list');
|
|
189
|
-
if (every(declared, (spec) => spec.facts.type === 'boolean' || spec.facts.type === 'object')) {
|
|
190
|
-
throw new QueryError('wrong-kind', at, 'its field does not compare in order');
|
|
191
|
-
}
|
|
192
|
-
return {
|
|
193
|
-
op: 'range',
|
|
194
|
-
bounds: RANGE.filter((operator) => operators.includes(operator)).map((operator) => {
|
|
195
|
-
const bound = value(input[operator], place(at, operator));
|
|
196
|
-
if (bound === null)
|
|
197
|
-
invalid(place(at, operator), 'a range bound is never null');
|
|
198
|
-
kind(declared, bound, place(at, operator));
|
|
199
|
-
return [operator.slice(1), bound];
|
|
200
|
-
}),
|
|
201
|
-
};
|
|
202
|
-
}
|
|
203
|
-
if (operators.length > 1)
|
|
204
|
-
invalid(place(at, operators[1]), 'names a second operator');
|
|
205
|
-
const [operator] = operators;
|
|
206
|
-
const where = place(at, operator);
|
|
207
|
-
if (!SINGLE.includes(operator))
|
|
208
|
-
invalid(where, 'unknown operator');
|
|
209
|
-
const operand = input[operator];
|
|
210
|
-
if (operator === '$eq' || operator === '$ne') {
|
|
211
|
-
const compared = value(operand, where);
|
|
212
|
-
kind(declared, compared, where);
|
|
213
|
-
return operator === '$eq' ? { op: 'eq', value: compared } : { op: 'ne', value: compared };
|
|
214
|
-
}
|
|
215
|
-
if (operator === '$in' || operator === '$nin' || operator === '$all') {
|
|
216
|
-
const compared = values(operand, where);
|
|
217
|
-
compared.forEach((item, index) => kind(declared, item, `${where}[${index}]`));
|
|
218
|
-
if (operator === '$in')
|
|
219
|
-
return { op: 'in', values: compared };
|
|
220
|
-
if (operator === '$nin')
|
|
221
|
-
return { op: 'nin', values: compared };
|
|
222
|
-
if (compared.includes(null))
|
|
223
|
-
invalid(where, 'a list holds no null');
|
|
224
|
-
if (every(declared, single))
|
|
225
|
-
invalid(where, 'the field is not a list');
|
|
226
|
-
return { op: 'all', values: compared };
|
|
227
|
-
}
|
|
228
|
-
if (operator === '$size') {
|
|
229
|
-
if (!Number.isSafeInteger(operand) || operand < 0) {
|
|
230
|
-
invalid(where, 'expects a nonnegative integer');
|
|
231
|
-
}
|
|
232
|
-
if (every(declared, single))
|
|
233
|
-
invalid(where, 'the field is not a list');
|
|
234
|
-
return { op: 'size', size: operand };
|
|
235
|
-
}
|
|
236
|
-
if (typeof operand !== 'boolean')
|
|
237
|
-
invalid(where, 'expects true or false');
|
|
238
|
-
return { op: 'exists', exists: operand };
|
|
239
|
-
}
|
|
240
|
-
function condition(typed, input, at) {
|
|
241
|
-
if (!plain(input))
|
|
242
|
-
invalid(at, 'expects an object of field predicates');
|
|
243
|
-
const conditions = [];
|
|
244
|
-
for (const name of Object.keys(input).sort()) {
|
|
245
|
-
const where = name.startsWith('/') ? `${at}${name}` : place(at, name);
|
|
246
|
-
const entry = input[name];
|
|
247
|
-
if (name === '$and' || name === '$or') {
|
|
248
|
-
const nested = list(entry, where).map((item, index) => condition(typed, item, `${where}[${index}]`));
|
|
249
|
-
if (nested.length === 0)
|
|
250
|
-
invalid(where, 'expects a nonempty list');
|
|
251
|
-
conditions.push(name === '$and' ? { every: nested } : { some: nested });
|
|
252
|
-
continue;
|
|
253
|
-
}
|
|
254
|
-
if (name.startsWith('$'))
|
|
255
|
-
invalid(where, 'unknown operator');
|
|
256
|
-
conditions.push({ field: name, test: test(specs(typed, name, where), entry, where) });
|
|
257
|
-
}
|
|
258
|
-
if (conditions.length === 0)
|
|
259
|
-
invalid(at, 'names no field');
|
|
260
|
-
return conditions.length === 1 ? conditions[0] : { every: conditions };
|
|
261
|
-
}
|
|
262
|
-
function scope(facts, rooms, input, at) {
|
|
263
|
-
const types = input.types === undefined ? null : names(input.types, place(at, 'types'));
|
|
264
|
-
const typed = typing(facts, rooms, types, at);
|
|
265
|
-
return {
|
|
266
|
-
typed,
|
|
267
|
-
types,
|
|
268
|
-
where: input.where === undefined || (plain(input.where) && Object.keys(input.where).length === 0)
|
|
269
|
-
? null
|
|
270
|
-
: condition(typed, input.where, place(at, 'where')),
|
|
271
|
-
pathPrefix: input.pathPrefix === undefined ? null : directory(input.pathPrefix, place(at, 'pathPrefix')),
|
|
272
|
-
};
|
|
273
|
-
}
|
|
274
|
-
function referenceName(typed, field, at) {
|
|
275
|
-
const declared = specs(typed, field, at);
|
|
276
|
-
if (declared.length > 0 &&
|
|
277
|
-
!declared.some((spec) => spec.conditional || referenceField(spec.facts))) {
|
|
278
|
-
invalid(at, 'not a reference field');
|
|
279
|
-
}
|
|
280
|
-
return field;
|
|
281
|
-
}
|
|
282
|
-
function hopOf(facts, rooms, root, input) {
|
|
283
|
-
if (!plain(input))
|
|
284
|
-
invalid('hop', 'expects an object');
|
|
285
|
-
known(input, RELATED, 'hop');
|
|
286
|
-
const field = referenceName(root, input.field, 'hop.field');
|
|
287
|
-
const { types, where, pathPrefix } = scope(facts, rooms, input, 'hop');
|
|
288
|
-
return { types, where, pathPrefix, field };
|
|
289
|
-
}
|
|
290
|
-
function backlinksOf(facts, rooms, input) {
|
|
291
|
-
if (!plain(input))
|
|
292
|
-
invalid('backlinks', 'expects an object');
|
|
293
|
-
known(input, RELATED, 'backlinks');
|
|
294
|
-
const { typed, types, where, pathPrefix } = scope(facts, rooms, input, 'backlinks');
|
|
295
|
-
const field = input.field === undefined ? null : referenceName(typed, input.field, 'backlinks.field');
|
|
296
|
-
return { types, where, pathPrefix, field };
|
|
297
|
-
}
|
|
298
|
-
function keyPlan(typed, input, at, path) {
|
|
299
|
-
if (input === 'room' || input === 'type' || input === 'folder' || (path && input === 'path')) {
|
|
300
|
-
return { of: input };
|
|
301
|
-
}
|
|
302
|
-
if (plain(input) && Object.hasOwn(input, 'field') && Object.keys(input).length === 1) {
|
|
303
|
-
if (specs(typed, input.field, place(at, 'field')).some(several)) {
|
|
304
|
-
invalid(place(at, 'field'), 'a path that can hold several values');
|
|
305
|
-
}
|
|
306
|
-
return { of: 'field', field: input.field };
|
|
307
|
-
}
|
|
308
|
-
return invalid(at, path ? 'expects room, path, type, folder or a field' : 'expects room, type, folder or a field');
|
|
309
|
-
}
|
|
310
|
-
function direction(input, at) {
|
|
311
|
-
if (input === undefined)
|
|
312
|
-
return 'asc';
|
|
313
|
-
if (input !== 'asc' && input !== 'desc')
|
|
314
|
-
invalid(at, 'expects asc or desc');
|
|
315
|
-
return input;
|
|
316
|
-
}
|
|
317
|
-
function orderOptions(input, at) {
|
|
318
|
-
const options = {
|
|
319
|
-
direction: direction(input.direction, place(at, 'direction')),
|
|
320
|
-
};
|
|
321
|
-
if (input.nulls !== undefined) {
|
|
322
|
-
if (input.nulls !== 'first' && input.nulls !== 'last') {
|
|
323
|
-
invalid(place(at, 'nulls'), 'expects first or last');
|
|
324
|
-
}
|
|
325
|
-
options.nulls = input.nulls;
|
|
326
|
-
}
|
|
327
|
-
if (input.stringSort !== undefined) {
|
|
328
|
-
if (input.stringSort !== 'lexical' && input.stringSort !== 'locale') {
|
|
329
|
-
invalid(place(at, 'stringSort'), 'expects lexical or locale');
|
|
330
|
-
}
|
|
331
|
-
options.stringSort = input.stringSort;
|
|
332
|
-
}
|
|
333
|
-
if (input.locale !== undefined) {
|
|
334
|
-
if (typeof input.locale !== 'string' || !collates(input.locale, undefined)) {
|
|
335
|
-
invalid(place(at, 'locale'), 'expects a locale name');
|
|
336
|
-
}
|
|
337
|
-
options.locale = input.locale;
|
|
338
|
-
}
|
|
339
|
-
if (input.localeOptions !== undefined) {
|
|
340
|
-
if (!plain(input.localeOptions) || !collates(options.locale, input.localeOptions)) {
|
|
341
|
-
invalid(place(at, 'localeOptions'), 'expects collation options');
|
|
342
|
-
}
|
|
343
|
-
options.localeOptions = input.localeOptions;
|
|
344
|
-
}
|
|
345
|
-
return options;
|
|
346
|
-
}
|
|
347
|
-
function collates(locale, options) {
|
|
348
|
-
try {
|
|
349
|
-
''.localeCompare('', locale, options);
|
|
350
|
-
return true;
|
|
351
|
-
}
|
|
352
|
-
catch {
|
|
353
|
-
return false;
|
|
354
|
-
}
|
|
355
|
-
}
|
|
356
|
-
function parts(typed, input, at, fields) {
|
|
357
|
-
if (input === undefined)
|
|
358
|
-
return null;
|
|
359
|
-
if (input === 'all')
|
|
360
|
-
return 'all';
|
|
361
|
-
const named = list(input, at);
|
|
362
|
-
named.forEach((name, index) => {
|
|
363
|
-
if (fields)
|
|
364
|
-
specs(typed, name, `${at}[${index}]`);
|
|
365
|
-
else if (typeof name !== 'string')
|
|
366
|
-
invalid(`${at}[${index}]`, 'expects a name');
|
|
367
|
-
});
|
|
368
|
-
return named;
|
|
369
|
-
}
|
|
370
|
-
export function flag(input, at) {
|
|
371
|
-
if (input === undefined)
|
|
372
|
-
return false;
|
|
373
|
-
if (input !== true)
|
|
374
|
-
invalid(at, 'expects true');
|
|
375
|
-
return true;
|
|
376
|
-
}
|
|
377
|
-
function documentShape(typed, input) {
|
|
378
|
-
const orderBy = (input.orderBy === undefined ? [] : list(input.orderBy, 'orderBy')).map((clause, index) => {
|
|
379
|
-
const at = `orderBy[${index}]`;
|
|
380
|
-
if (!plain(clause))
|
|
381
|
-
invalid(at, 'expects an order clause');
|
|
382
|
-
known(clause, ORDER, at);
|
|
383
|
-
return {
|
|
384
|
-
key: keyPlan(typed, clause.by, place(at, 'by'), true),
|
|
385
|
-
options: orderOptions(clause, at),
|
|
386
|
-
};
|
|
387
|
-
});
|
|
388
|
-
const select = input.select;
|
|
389
|
-
if (select === 'all') {
|
|
390
|
-
return {
|
|
391
|
-
kind: 'documents',
|
|
392
|
-
fields: 'all',
|
|
393
|
-
sections: 'all',
|
|
394
|
-
preamble: true,
|
|
395
|
-
markdown: true,
|
|
396
|
-
backlinks: true,
|
|
397
|
-
change: true,
|
|
398
|
-
fileId: true,
|
|
399
|
-
revision: true,
|
|
400
|
-
orderBy,
|
|
401
|
-
};
|
|
402
|
-
}
|
|
403
|
-
if (!plain(select))
|
|
404
|
-
invalid('select', "expects 'all' or the parts to return");
|
|
405
|
-
known(select, PARTS, 'select');
|
|
406
|
-
return {
|
|
407
|
-
kind: 'documents',
|
|
408
|
-
fields: parts(typed, select.fields, 'select.fields', true),
|
|
409
|
-
sections: parts(typed, select.sections, 'select.sections', false),
|
|
410
|
-
preamble: flag(select.preamble, 'select.preamble'),
|
|
411
|
-
markdown: flag(select.markdown, 'select.markdown'),
|
|
412
|
-
backlinks: flag(select.backlinks, 'select.backlinks'),
|
|
413
|
-
change: flag(select.change, 'select.change'),
|
|
414
|
-
fileId: flag(select.fileId, 'select.fileId'),
|
|
415
|
-
revision: flag(select.revision, 'select.revision'),
|
|
416
|
-
orderBy,
|
|
417
|
-
};
|
|
418
|
-
}
|
|
419
|
-
function groupShape(typed, input) {
|
|
420
|
-
const groupBy = list(input.groupBy, 'groupBy').map((key, index) => keyPlan(typed, key, `groupBy[${index}]`, false));
|
|
421
|
-
if (groupBy.length === 0)
|
|
422
|
-
invalid('groupBy', 'names no key');
|
|
423
|
-
const grouped = groupBy.map((key) => JSON.stringify(key));
|
|
424
|
-
const orderBy = (input.orderBy === undefined ? [] : list(input.orderBy, 'orderBy')).map((clause, index) => {
|
|
425
|
-
const at = `orderBy[${index}]`;
|
|
426
|
-
if (!plain(clause))
|
|
427
|
-
invalid(at, 'expects an order clause');
|
|
428
|
-
known(clause, ['by', 'direction'], at);
|
|
429
|
-
const by = clause.by === 'count'
|
|
430
|
-
? 'count'
|
|
431
|
-
: grouped.indexOf(JSON.stringify(keyPlan(typed, clause.by, place(at, 'by'), false)));
|
|
432
|
-
if (by === -1)
|
|
433
|
-
invalid(place(at, 'by'), 'orders by a key the query does not group by');
|
|
434
|
-
return { by, direction: direction(clause.direction, place(at, 'direction')) };
|
|
435
|
-
});
|
|
436
|
-
return { kind: 'groups', groupBy, orderBy };
|
|
437
|
-
}
|
|
438
|
-
export function planOf(input, facts, reach = null) {
|
|
439
|
-
if (!plain(input))
|
|
440
|
-
invalid('query', 'expects a query object');
|
|
441
|
-
const grouped = input.groupBy !== undefined;
|
|
442
|
-
if (grouped && input.select !== undefined)
|
|
443
|
-
invalid('select', 'a grouped query selects nothing');
|
|
444
|
-
if (!grouped && input.select === undefined)
|
|
445
|
-
invalid('select', 'a Document query must select');
|
|
446
|
-
known(input, [...FILTER, 'orderBy', 'limit', 'after', 'total', grouped ? 'groupBy' : 'select'], '');
|
|
447
|
-
const limit = limitOf(input.limit);
|
|
448
|
-
const rooms = input.rooms === undefined ? null : names(input.rooms, 'rooms');
|
|
449
|
-
const { typed, ...root } = scope(facts, rooms, input, '');
|
|
450
|
-
const shape = grouped ? groupShape(typed, input) : documentShape(typed, input);
|
|
451
|
-
return {
|
|
452
|
-
key: queryKey(input),
|
|
453
|
-
rooms,
|
|
454
|
-
reach,
|
|
455
|
-
locked: [],
|
|
456
|
-
root,
|
|
457
|
-
folder: input.folder === undefined ? null : directory(input.folder, 'folder'),
|
|
458
|
-
paths: input.paths === undefined ? null : pathsOf(input.paths),
|
|
459
|
-
hop: input.hop === undefined ? null : hopOf(facts, rooms, typed, input.hop),
|
|
460
|
-
backlinks: input.backlinks === undefined ? null : backlinksOf(facts, rooms, input.backlinks),
|
|
461
|
-
shape,
|
|
462
|
-
limit,
|
|
463
|
-
after: continuationOf(input.after, shape.orderBy.length + 1),
|
|
464
|
-
total: flag(input.total, 'total'),
|
|
465
|
-
};
|
|
466
|
-
}
|
|
467
|
-
function continuationOf(input, arity) {
|
|
468
|
-
if (input === undefined)
|
|
469
|
-
return null;
|
|
470
|
-
const values = typeof input === 'string' ? decodedCursor(input) : null;
|
|
471
|
-
if (values === null || (values.length !== 0 && values.length !== arity)) {
|
|
472
|
-
invalid('after', 'expects the next an earlier result of this query returned');
|
|
473
|
-
}
|
|
474
|
-
return { text: input, values: values.length === 0 ? null : values };
|
|
475
|
-
}
|
|
476
|
-
export function limitOf(input) {
|
|
477
|
-
if (input === undefined)
|
|
478
|
-
return null;
|
|
479
|
-
if (!Number.isSafeInteger(input) || input < 0) {
|
|
480
|
-
invalid('limit', 'expects a nonnegative integer');
|
|
481
|
-
}
|
|
482
|
-
return input;
|
|
483
|
-
}
|
|
484
|
-
export function filterOf(input, facts, reach = null) {
|
|
485
|
-
if (!plain(input))
|
|
486
|
-
invalid('documents', 'expects a filter object');
|
|
487
|
-
known(input, FILTER, '');
|
|
488
|
-
return planOf({ ...input, select: {} }, facts, reach);
|
|
489
|
-
}
|
|
490
|
-
export function queryKey(query) {
|
|
491
|
-
return canonical(query);
|
|
492
|
-
}
|
|
493
|
-
function canonical(value) {
|
|
494
|
-
if (Array.isArray(value))
|
|
495
|
-
return `[${value.map(canonical).join(',')}]`;
|
|
496
|
-
if (typeof value === 'object' && value !== null) {
|
|
497
|
-
return `{${Object.keys(value)
|
|
498
|
-
.sort()
|
|
499
|
-
.map((name) => `${JSON.stringify(name)}:${canonical(value[name])}`)
|
|
500
|
-
.join(',')}}`;
|
|
501
|
-
}
|
|
502
|
-
return JSON.stringify(value);
|
|
503
|
-
}
|
|
504
|
-
export function identityOf(plan) {
|
|
505
|
-
return canonical(plan);
|
|
1
|
+
import { planOf } from '@agentshouse/mdmodel';
|
|
2
|
+
export function planWithin(input, facts, reach = null) {
|
|
3
|
+
return { ...planOf(input, facts), reach, locked: [] };
|
|
506
4
|
}
|
package/dist/reach.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { AppRoom, MdRuntime, ReadinessPort, RuntimeTopic } from './connection.js';
|
|
2
2
|
import type { Plan, PlanReach, QueryResult } from './query.js';
|
|
3
3
|
import type { Readiness } from './readiness.js';
|
|
4
|
-
import {
|
|
4
|
+
import type { SearchResult } from './search.js';
|
|
5
5
|
export type ReachedDocument = {
|
|
6
6
|
readonly room: string;
|
|
7
7
|
readonly path: string;
|
|
@@ -9,7 +9,7 @@ export type ReachedDocument = {
|
|
|
9
9
|
export type ReachedArea = {
|
|
10
10
|
readonly room: string;
|
|
11
11
|
readonly path: string;
|
|
12
|
-
readonly
|
|
12
|
+
readonly open: readonly string[];
|
|
13
13
|
};
|
|
14
14
|
export type PortReach = {
|
|
15
15
|
readonly rooms: readonly string[];
|
|
@@ -18,20 +18,20 @@ export type PortReach = {
|
|
|
18
18
|
readonly conversations: boolean;
|
|
19
19
|
};
|
|
20
20
|
export type Deliver = (message: unknown) => void;
|
|
21
|
-
export type
|
|
21
|
+
export type Fail = (error: unknown) => void;
|
|
22
|
+
export type Watch = (topic: RuntimeTopic, input: readonly unknown[], deliver: Deliver, fail: Fail) => () => void;
|
|
22
23
|
export type RuntimeView = {
|
|
23
24
|
md: MdRuntime;
|
|
24
25
|
watch: Watch;
|
|
25
26
|
};
|
|
26
27
|
export type NarrowedView = RuntimeView & {
|
|
27
28
|
narrow(reach: PortReach | null): void;
|
|
28
|
-
facts(port: ReadinessPort): void;
|
|
29
29
|
};
|
|
30
30
|
export type Reachable = RuntimeView & {
|
|
31
31
|
plan(input: unknown, reach: PortReach | null): Plan;
|
|
32
32
|
query(plan: Plan): Promise<QueryResult>;
|
|
33
|
-
watchPlan(plan: Plan, deliver:
|
|
34
|
-
search(input: unknown, reach: PortReach | null
|
|
33
|
+
watchPlan(plan: Plan, deliver: Deliver, fail: Fail): () => void;
|
|
34
|
+
search(input: unknown, reach: PortReach | null): Promise<SearchResult>;
|
|
35
35
|
watchReadiness(port: ReadinessPort, reach: PortReach | null, deliver: (readiness: Readiness) => void): () => void;
|
|
36
36
|
locked(room: string): boolean;
|
|
37
37
|
lockedRooms(): readonly string[];
|
|
@@ -50,5 +50,5 @@ export declare function governs(reach: PortReach | null, room: {
|
|
|
50
50
|
readonly selection: boolean;
|
|
51
51
|
};
|
|
52
52
|
export declare function planReach(reach: PortReach | null): PlanReach | null;
|
|
53
|
-
export declare function narrowed(runtime: Reachable, given: PortReach | null,
|
|
53
|
+
export declare function narrowed(runtime: Reachable, given: PortReach | null, port: ReadinessPort): NarrowedView;
|
|
54
54
|
//# sourceMappingURL=reach.d.ts.map
|
package/dist/reach.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"reach.d.ts","sourceRoot":"","sources":["../src/reach.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,OAAO,EACP,SAAS,EACT,aAAa,EAEb,YAAY,EAEb,MAAM,iBAAiB,CAAC;
|
|
1
|
+
{"version":3,"file":"reach.d.ts","sourceRoot":"","sources":["../src/reach.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,OAAO,EACP,SAAS,EACT,aAAa,EAEb,YAAY,EAEb,MAAM,iBAAiB,CAAC;AA0BzB,OAAO,KAAK,EAAiB,IAAI,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAS9E,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAChD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAGhD,MAAM,MAAM,eAAe,GAAG;IAAE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;CAAE,CAAC;AAE/E,MAAM,MAAM,WAAW,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