@altopelago/sansa 0.9.0
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/CHANGELOG.md +41 -0
- package/CODE_OF_CONDUCT.md +28 -0
- package/CONTRIBUTING.md +52 -0
- package/GOVERNANCE.md +35 -0
- package/LICENSE +21 -0
- package/README.md +146 -0
- package/SECURITY.md +38 -0
- package/docs/api-contract.md +788 -0
- package/docs/capabilities.json +80 -0
- package/docs/query-tool.md +228 -0
- package/docs/releasing.md +37 -0
- package/fixtures/address-v1.json +316 -0
- package/fixtures/query-inventory.aeon +69 -0
- package/fixtures/query-inventory.json +671 -0
- package/fixtures/query-params-single.json +10 -0
- package/package.json +70 -0
- package/scripts/cts-address.mjs +107 -0
- package/scripts/cts-query.mjs +271 -0
- package/scripts/cts-resolve.mjs +151 -0
- package/scripts/cts-value-semantics.mjs +83 -0
- package/scripts/query-web.mjs +145 -0
- package/scripts/query.mjs +579 -0
- package/scripts/stress-addresses.mjs +65 -0
- package/src/index.d.ts +575 -0
- package/src/index.js +3389 -0
- package/stress-tests/README.md +19 -0
- package/stress-tests/address/invalid.sansa-cases +60 -0
- package/stress-tests/address/valid.sansa-cases +76 -0
- package/tools/query-web/app.js +266 -0
- package/tools/query-web/examples.mjs +478 -0
- package/tools/query-web/index.html +117 -0
- package/tools/query-web/runtime.mjs +796 -0
- package/tools/query-web/styles.css +251 -0
|
@@ -0,0 +1,796 @@
|
|
|
1
|
+
import { existsSync } from 'node:fs';
|
|
2
|
+
import { createRequire } from 'node:module';
|
|
3
|
+
import { resolve as resolvePath } from 'node:path';
|
|
4
|
+
import { fileURLToPath, pathToFileURL } from 'node:url';
|
|
5
|
+
import { evaluateQuery, parseQuery } from '../../src/index.js';
|
|
6
|
+
|
|
7
|
+
const devAeonCoreUrl = new URL('../../../aeon/implementations/typescript/packages/core/dist/index.js', import.meta.url);
|
|
8
|
+
const requireFromCwd = createRequire(resolvePath(process.cwd(), 'package.json'));
|
|
9
|
+
const QUERY_VALUE_METADATA_PROPERTY = '__sansaQueryValueMetadata';
|
|
10
|
+
const QUERY_OBJECT_FIELD_METADATA_PROPERTY = '__sansaObjectFieldMetadata';
|
|
11
|
+
|
|
12
|
+
export function parseQueryForWorkbench(querySource) {
|
|
13
|
+
const result = parseQuery(querySource);
|
|
14
|
+
if (!result.ok) {
|
|
15
|
+
const errors = normalizeDiagnostics(result.errors);
|
|
16
|
+
return {
|
|
17
|
+
ok: false,
|
|
18
|
+
mode: 'parse',
|
|
19
|
+
text: renderDiagnosticText(errors),
|
|
20
|
+
errors,
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return {
|
|
25
|
+
ok: true,
|
|
26
|
+
mode: 'parse',
|
|
27
|
+
text: result.query.canonical,
|
|
28
|
+
inspect: renderQueryInspect(result.query),
|
|
29
|
+
query: summarizeQuery(result.query),
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export async function evaluateQueryForWorkbench({
|
|
34
|
+
sourceKind,
|
|
35
|
+
source,
|
|
36
|
+
query,
|
|
37
|
+
paramsSource = '',
|
|
38
|
+
policy = '',
|
|
39
|
+
transformExtensions = true,
|
|
40
|
+
budget = {},
|
|
41
|
+
}) {
|
|
42
|
+
const namespaceResult = sourceKind === 'json'
|
|
43
|
+
? namespaceFromJsonSource(source)
|
|
44
|
+
: await namespaceFromAeonSource(source);
|
|
45
|
+
|
|
46
|
+
if (!namespaceResult.ok) {
|
|
47
|
+
return namespaceResult;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const mounted = await mountParamsLocalSpace(namespaceResult.namespace, paramsSource);
|
|
51
|
+
if (!mounted.ok) {
|
|
52
|
+
return {
|
|
53
|
+
...mounted,
|
|
54
|
+
sourceKind,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const sourceDiagnostics = [
|
|
59
|
+
...(namespaceResult.diagnostics ?? []),
|
|
60
|
+
...(mounted.diagnostics ?? []),
|
|
61
|
+
];
|
|
62
|
+
|
|
63
|
+
const options = {
|
|
64
|
+
...(policy === 'validation' ? { policy: 'validation' } : {}),
|
|
65
|
+
...(transformExtensions === false ? { extensions: { transform: false } } : {}),
|
|
66
|
+
...queryBudgetOption(budget),
|
|
67
|
+
};
|
|
68
|
+
const result = evaluateQuery(query, mounted.namespace, options);
|
|
69
|
+
if (!result.ok) {
|
|
70
|
+
const errors = normalizeDiagnostics(result.errors);
|
|
71
|
+
return {
|
|
72
|
+
ok: false,
|
|
73
|
+
mode: 'evaluate',
|
|
74
|
+
sourceKind,
|
|
75
|
+
text: renderDiagnosticText(errors),
|
|
76
|
+
errors,
|
|
77
|
+
sourceDiagnostics,
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return {
|
|
82
|
+
ok: true,
|
|
83
|
+
mode: 'evaluate',
|
|
84
|
+
sourceKind,
|
|
85
|
+
count: result.results.length,
|
|
86
|
+
text: renderTextResults(result.results),
|
|
87
|
+
inspect: renderInspectResults(result.results),
|
|
88
|
+
results: result.results.map((entry) => ({
|
|
89
|
+
type: entry.type,
|
|
90
|
+
...(entry.address === undefined ? {} : { address: entry.address }),
|
|
91
|
+
binding: summarizeBinding(entry.binding),
|
|
92
|
+
value: summarizeQueryValue(entry.value),
|
|
93
|
+
})),
|
|
94
|
+
sourceDiagnostics,
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export function namespaceFromJsonSource(source) {
|
|
99
|
+
try {
|
|
100
|
+
const fixture = JSON.parse(source);
|
|
101
|
+
const root = fixture.root ?? fixture;
|
|
102
|
+
return {
|
|
103
|
+
ok: true,
|
|
104
|
+
namespace: {
|
|
105
|
+
root,
|
|
106
|
+
children: (binding) => binding.children ?? [],
|
|
107
|
+
attributeSpace: (binding) => binding.attributeSpace ?? binding.attributes,
|
|
108
|
+
localSpace: (binding, name) => binding.localSpaces?.[name],
|
|
109
|
+
},
|
|
110
|
+
diagnostics: [],
|
|
111
|
+
};
|
|
112
|
+
} catch (error) {
|
|
113
|
+
return {
|
|
114
|
+
ok: false,
|
|
115
|
+
mode: 'evaluate',
|
|
116
|
+
sourceKind: 'json',
|
|
117
|
+
text: renderDiagnosticText([{
|
|
118
|
+
code: 'SANSA_QUERY_WORKBENCH_INVALID_JSON',
|
|
119
|
+
message: error.message,
|
|
120
|
+
}]),
|
|
121
|
+
errors: [{
|
|
122
|
+
code: 'SANSA_QUERY_WORKBENCH_INVALID_JSON',
|
|
123
|
+
message: error.message,
|
|
124
|
+
}],
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export async function namespaceFromAeonSource(source) {
|
|
130
|
+
const loaded = await loadAeonCore();
|
|
131
|
+
if (!loaded.ok) {
|
|
132
|
+
const errors = [{
|
|
133
|
+
code: 'SANSA_QUERY_WORKBENCH_AEON_RUNTIME_UNAVAILABLE',
|
|
134
|
+
message: loaded.message,
|
|
135
|
+
}];
|
|
136
|
+
return {
|
|
137
|
+
ok: false,
|
|
138
|
+
mode: 'evaluate',
|
|
139
|
+
sourceKind: 'aeon',
|
|
140
|
+
text: renderDiagnosticText(errors),
|
|
141
|
+
errors,
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
const aeonCore = loaded.module;
|
|
145
|
+
|
|
146
|
+
const compiled = aeonCore.compile(source, {
|
|
147
|
+
datatypePolicy: 'allow_custom',
|
|
148
|
+
mode: 'custom',
|
|
149
|
+
recovery: false,
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
if (compiled.errors.length > 0) {
|
|
153
|
+
const errors = compiled.errors.map(normalizeAeonError);
|
|
154
|
+
return {
|
|
155
|
+
ok: false,
|
|
156
|
+
mode: 'evaluate',
|
|
157
|
+
sourceKind: 'aeon',
|
|
158
|
+
text: renderDiagnosticText(errors),
|
|
159
|
+
errors,
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
return {
|
|
164
|
+
ok: true,
|
|
165
|
+
namespace: buildNamespaceFromEvents(compiled.events, aeonCore.formatPath),
|
|
166
|
+
diagnostics: [],
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
async function mountParamsLocalSpace(namespace, paramsSource) {
|
|
171
|
+
if (String(paramsSource).trim().length === 0) {
|
|
172
|
+
return { ok: true, namespace, diagnostics: [] };
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
const loaded = await loadAeonCore();
|
|
176
|
+
if (!loaded.ok) {
|
|
177
|
+
const errors = [{
|
|
178
|
+
code: 'SANSA_QUERY_WORKBENCH_AEON_RUNTIME_UNAVAILABLE',
|
|
179
|
+
message: `Could not load AEON TypeScript core build for params: ${loaded.message}`,
|
|
180
|
+
}];
|
|
181
|
+
return {
|
|
182
|
+
ok: false,
|
|
183
|
+
mode: 'evaluate',
|
|
184
|
+
text: renderDiagnosticText(errors),
|
|
185
|
+
errors,
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
const aeonCore = loaded.module;
|
|
189
|
+
|
|
190
|
+
const compiled = aeonCore.compile(String(paramsSource), {
|
|
191
|
+
datatypePolicy: 'allow_custom',
|
|
192
|
+
mode: 'custom',
|
|
193
|
+
recovery: false,
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
if (compiled.errors.length > 0) {
|
|
197
|
+
const errors = compiled.errors.map(normalizeAeonError).map((error) => ({
|
|
198
|
+
...error,
|
|
199
|
+
code: `PARAMS_${error.code}`,
|
|
200
|
+
}));
|
|
201
|
+
return {
|
|
202
|
+
ok: false,
|
|
203
|
+
mode: 'evaluate',
|
|
204
|
+
text: renderDiagnosticText(errors),
|
|
205
|
+
errors,
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
const paramsNamespace = buildNamespaceFromEvents(compiled.events, aeonCore.formatPath);
|
|
210
|
+
const paramsBinding = remapLocalSpaceBinding(paramsNamespace.root, '$.<"params">');
|
|
211
|
+
const root = typeof namespace.root === 'function' ? namespace.root() : namespace.root;
|
|
212
|
+
if (!root) {
|
|
213
|
+
const errors = [{
|
|
214
|
+
code: 'SANSA_QUERY_WORKBENCH_PARAMS_MISSING_ROOT',
|
|
215
|
+
message: 'Cannot mount params local space because the source namespace has no root binding.',
|
|
216
|
+
}];
|
|
217
|
+
return {
|
|
218
|
+
ok: false,
|
|
219
|
+
mode: 'evaluate',
|
|
220
|
+
text: renderDiagnosticText(errors),
|
|
221
|
+
errors,
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
root.localSpaces = {
|
|
226
|
+
...(root.localSpaces ?? {}),
|
|
227
|
+
params: paramsBinding,
|
|
228
|
+
};
|
|
229
|
+
const localSpace = namespace.localSpace;
|
|
230
|
+
|
|
231
|
+
return {
|
|
232
|
+
ok: true,
|
|
233
|
+
namespace: {
|
|
234
|
+
...namespace,
|
|
235
|
+
localSpace: (binding, name) => {
|
|
236
|
+
if (binding === root && name === 'params') return root.localSpaces?.params;
|
|
237
|
+
return localSpace?.(binding, name) ?? binding.localSpaces?.[name];
|
|
238
|
+
},
|
|
239
|
+
},
|
|
240
|
+
diagnostics: [{
|
|
241
|
+
code: 'SANSA_QUERY_WORKBENCH_PARAMS_MOUNTED',
|
|
242
|
+
message: 'Mounted $.<"params"> local space.',
|
|
243
|
+
}],
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
async function loadAeonCore() {
|
|
248
|
+
const candidates = aeonCoreCandidates();
|
|
249
|
+
const failures = [];
|
|
250
|
+
for (const candidate of candidates) {
|
|
251
|
+
try {
|
|
252
|
+
return { ok: true, module: await import(candidate.href) };
|
|
253
|
+
} catch (error) {
|
|
254
|
+
failures.push(`${candidate.label}: ${error.message}`);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
return {
|
|
259
|
+
ok: false,
|
|
260
|
+
message: [
|
|
261
|
+
'Could not load an AEON TypeScript core runtime.',
|
|
262
|
+
'Install @altopelago/aeon-core in the calling project, set SANSA_AEON_CORE_MODULE to a module path or specifier, or run from the aeon-family development workspace.',
|
|
263
|
+
...(failures.length === 0 ? [] : [`Tried: ${failures.join('; ')}`]),
|
|
264
|
+
].join(' '),
|
|
265
|
+
};
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
function aeonCoreCandidates() {
|
|
269
|
+
const candidates = [];
|
|
270
|
+
if (process.env.SANSA_AEON_CORE_MODULE) {
|
|
271
|
+
candidates.push({
|
|
272
|
+
label: 'SANSA_AEON_CORE_MODULE',
|
|
273
|
+
href: moduleHref(process.env.SANSA_AEON_CORE_MODULE),
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
try {
|
|
278
|
+
candidates.push({
|
|
279
|
+
label: '@altopelago/aeon-core',
|
|
280
|
+
href: pathToFileURL(requireFromCwd.resolve('@altopelago/aeon-core')).href,
|
|
281
|
+
});
|
|
282
|
+
} catch {
|
|
283
|
+
// Optional integration. JSON fixtures and pure Query parsing do not need AEON Core.
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
const devPath = fileURLToPath(devAeonCoreUrl);
|
|
287
|
+
if (existsSync(devPath)) {
|
|
288
|
+
candidates.push({
|
|
289
|
+
label: 'aeon-family development path',
|
|
290
|
+
href: devAeonCoreUrl.href,
|
|
291
|
+
});
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
return candidates;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
function moduleHref(specifier) {
|
|
298
|
+
if (specifier.startsWith('file:')) return specifier;
|
|
299
|
+
if (specifier.startsWith('.') || specifier.startsWith('/')) {
|
|
300
|
+
return pathToFileURL(resolvePath(specifier)).href;
|
|
301
|
+
}
|
|
302
|
+
try {
|
|
303
|
+
return pathToFileURL(requireFromCwd.resolve(specifier)).href;
|
|
304
|
+
} catch {
|
|
305
|
+
return specifier;
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
function remapLocalSpaceBinding(binding, targetAddress) {
|
|
310
|
+
return remapBindingTree({
|
|
311
|
+
...binding,
|
|
312
|
+
address: '$',
|
|
313
|
+
representationKind: binding.representationKind ?? 'object',
|
|
314
|
+
}, '$', targetAddress);
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
function remapBindingTree(binding, sourceBase, targetBase) {
|
|
318
|
+
const address = remapAddress(binding.address, sourceBase, targetBase);
|
|
319
|
+
const output = {
|
|
320
|
+
...binding,
|
|
321
|
+
address,
|
|
322
|
+
children: (binding.children ?? []).map((child) => remapBindingTree(child, sourceBase, targetBase)),
|
|
323
|
+
};
|
|
324
|
+
if (binding.attributeSpace) {
|
|
325
|
+
output.attributeSpace = remapBindingTree(binding.attributeSpace, sourceBase, targetBase);
|
|
326
|
+
}
|
|
327
|
+
if (binding.localSpaces) {
|
|
328
|
+
output.localSpaces = Object.fromEntries(Object.entries(binding.localSpaces).map(([name, localSpace]) => (
|
|
329
|
+
[name, remapBindingTree(localSpace, sourceBase, targetBase)]
|
|
330
|
+
)));
|
|
331
|
+
}
|
|
332
|
+
return output;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
function remapAddress(address, sourceBase, targetBase) {
|
|
336
|
+
if (typeof address !== 'string') return address;
|
|
337
|
+
if (address === sourceBase) return targetBase;
|
|
338
|
+
if (address.startsWith(`${sourceBase}.`)) return `${targetBase}${address.slice(sourceBase.length)}`;
|
|
339
|
+
return address;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
function buildNamespaceFromEvents(events, formatPath) {
|
|
343
|
+
const root = {
|
|
344
|
+
address: '$',
|
|
345
|
+
representationKind: 'object',
|
|
346
|
+
children: [],
|
|
347
|
+
};
|
|
348
|
+
const byAddress = new Map([['$', root]]);
|
|
349
|
+
const parents = new Map([[root, null]]);
|
|
350
|
+
|
|
351
|
+
for (const event of events) {
|
|
352
|
+
const address = formatPath(event.path);
|
|
353
|
+
const parentAddress = parentPathAddress(event.path, formatPath);
|
|
354
|
+
const parent = byAddress.get(parentAddress) ?? root;
|
|
355
|
+
const segment = event.path.segments[event.path.segments.length - 1];
|
|
356
|
+
const binding = byAddress.get(address) ?? {
|
|
357
|
+
address,
|
|
358
|
+
children: [],
|
|
359
|
+
};
|
|
360
|
+
|
|
361
|
+
if (segment?.type === 'member') binding.name = segment.key;
|
|
362
|
+
if (segment?.type === 'index') binding.index = segment.index;
|
|
363
|
+
binding.semanticType = event.datatype ?? semanticTypeFromValue(event.value);
|
|
364
|
+
binding.representationKind = representationKindFromValue(event.value);
|
|
365
|
+
binding.scalarKind = scalarKindFromValue(event.value);
|
|
366
|
+
const nullReason = nullReasonFromValue(event.value);
|
|
367
|
+
if (nullReason !== undefined) binding.nullReason = nullReason;
|
|
368
|
+
|
|
369
|
+
const scalar = scalarFromAeonValue(event.value);
|
|
370
|
+
if (scalar.ok) binding.value = scalar.value;
|
|
371
|
+
|
|
372
|
+
if (event.annotations?.size) {
|
|
373
|
+
binding.attributeSpace = buildAttributeSpace(address, event.annotations, parents, binding);
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
if (!byAddress.has(address)) {
|
|
377
|
+
byAddress.set(address, binding);
|
|
378
|
+
parents.set(binding, parent);
|
|
379
|
+
parent.children.push(binding);
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
return {
|
|
384
|
+
root,
|
|
385
|
+
children: (binding) => binding.children ?? [],
|
|
386
|
+
attributeSpace: (binding) => binding.attributeSpace,
|
|
387
|
+
localSpace: (binding, name) => binding.localSpaces?.[name],
|
|
388
|
+
parent: (binding) => parents.get(binding) ?? binding.parent,
|
|
389
|
+
};
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
function parentPathAddress(path, formatPath) {
|
|
393
|
+
if (path.segments.length <= 1) return '$';
|
|
394
|
+
return formatPath({ segments: path.segments.slice(0, -1) });
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
function buildAttributeSpace(ownerAddress, annotations, parents, parent) {
|
|
398
|
+
const attributeSpace = {
|
|
399
|
+
address: `${ownerAddress}.@`,
|
|
400
|
+
representationKind: 'attributeSpace',
|
|
401
|
+
children: [],
|
|
402
|
+
};
|
|
403
|
+
parents.set(attributeSpace, parent);
|
|
404
|
+
attributeSpace.children = [...annotations.entries()].map(([name, entry]) => {
|
|
405
|
+
const binding = {
|
|
406
|
+
name,
|
|
407
|
+
address: appendMember(`${ownerAddress}.@`, name),
|
|
408
|
+
semanticType: entry.datatype ?? semanticTypeFromValue(entry.value),
|
|
409
|
+
representationKind: representationKindFromValue(entry.value),
|
|
410
|
+
scalarKind: scalarKindFromValue(entry.value),
|
|
411
|
+
children: [],
|
|
412
|
+
};
|
|
413
|
+
const nullReason = nullReasonFromValue(entry.value);
|
|
414
|
+
if (nullReason !== undefined) binding.nullReason = nullReason;
|
|
415
|
+
const scalar = scalarFromAeonValue(entry.value);
|
|
416
|
+
if (scalar.ok) binding.value = scalar.value;
|
|
417
|
+
if (entry.annotations?.size) {
|
|
418
|
+
binding.attributeSpace = buildAttributeSpace(binding.address, entry.annotations, parents, binding);
|
|
419
|
+
}
|
|
420
|
+
parents.set(binding, attributeSpace);
|
|
421
|
+
return binding;
|
|
422
|
+
});
|
|
423
|
+
return attributeSpace;
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
function appendMember(base, name) {
|
|
427
|
+
return /^[A-Za-z_][A-Za-z0-9_]*$/.test(name)
|
|
428
|
+
? `${base}.${name}`
|
|
429
|
+
: `${base}.[${JSON.stringify(name)}]`;
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
function semanticTypeFromValue(value) {
|
|
433
|
+
switch (value.type) {
|
|
434
|
+
case 'StringLiteral':
|
|
435
|
+
return 'string';
|
|
436
|
+
case 'NumberLiteral':
|
|
437
|
+
return 'number';
|
|
438
|
+
case 'InfinityLiteral':
|
|
439
|
+
return 'infinity';
|
|
440
|
+
case 'NaNLiteral':
|
|
441
|
+
return 'nan';
|
|
442
|
+
case 'BooleanLiteral':
|
|
443
|
+
return 'boolean';
|
|
444
|
+
case 'NullLiteral':
|
|
445
|
+
return 'null';
|
|
446
|
+
case 'SansaAddressLiteral':
|
|
447
|
+
return 'sansa';
|
|
448
|
+
default:
|
|
449
|
+
return undefined;
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
function representationKindFromValue(value) {
|
|
454
|
+
switch (value.type) {
|
|
455
|
+
case 'TypedValue':
|
|
456
|
+
return representationKindFromValue(value.value);
|
|
457
|
+
case 'ObjectNode':
|
|
458
|
+
return 'object';
|
|
459
|
+
case 'ListNode':
|
|
460
|
+
return 'list';
|
|
461
|
+
case 'TupleLiteral':
|
|
462
|
+
return 'tuple';
|
|
463
|
+
case 'NodeLiteral':
|
|
464
|
+
return 'node';
|
|
465
|
+
case 'StringLiteral':
|
|
466
|
+
case 'DateLiteral':
|
|
467
|
+
case 'DateTimeLiteral':
|
|
468
|
+
case 'TimeLiteral':
|
|
469
|
+
case 'HexLiteral':
|
|
470
|
+
case 'RadixLiteral':
|
|
471
|
+
case 'EncodingLiteral':
|
|
472
|
+
case 'SeparatorLiteral':
|
|
473
|
+
case 'SansaAddressLiteral':
|
|
474
|
+
return 'string';
|
|
475
|
+
case 'NumberLiteral':
|
|
476
|
+
return 'number';
|
|
477
|
+
case 'InfinityLiteral':
|
|
478
|
+
return 'infinity';
|
|
479
|
+
case 'NaNLiteral':
|
|
480
|
+
return 'nan';
|
|
481
|
+
case 'BooleanLiteral':
|
|
482
|
+
return 'boolean';
|
|
483
|
+
case 'NullLiteral':
|
|
484
|
+
return 'null';
|
|
485
|
+
case 'ToggleLiteral':
|
|
486
|
+
return 'toggle';
|
|
487
|
+
case 'CloneReference':
|
|
488
|
+
return 'cloneReference';
|
|
489
|
+
case 'PointerReference':
|
|
490
|
+
return 'pointerReference';
|
|
491
|
+
default:
|
|
492
|
+
return value.type;
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
function scalarFromAeonValue(value) {
|
|
497
|
+
switch (value.type) {
|
|
498
|
+
case 'TypedValue':
|
|
499
|
+
return scalarFromAeonValue(value.value);
|
|
500
|
+
case 'StringLiteral':
|
|
501
|
+
case 'DateLiteral':
|
|
502
|
+
case 'DateTimeLiteral':
|
|
503
|
+
case 'TimeLiteral':
|
|
504
|
+
case 'HexLiteral':
|
|
505
|
+
case 'RadixLiteral':
|
|
506
|
+
case 'EncodingLiteral':
|
|
507
|
+
case 'SeparatorLiteral':
|
|
508
|
+
return { ok: true, value: value.value };
|
|
509
|
+
case 'SansaAddressLiteral':
|
|
510
|
+
return {
|
|
511
|
+
ok: true,
|
|
512
|
+
value: {
|
|
513
|
+
type: 'SansaAddressLiteral',
|
|
514
|
+
address: value.address ?? value.canonical ?? value.value,
|
|
515
|
+
...(value.canonical === undefined ? {} : { canonical: value.canonical }),
|
|
516
|
+
},
|
|
517
|
+
};
|
|
518
|
+
case 'NumberLiteral':
|
|
519
|
+
return { ok: true, value: Number(value.value) };
|
|
520
|
+
case 'InfinityLiteral':
|
|
521
|
+
return { ok: true, value: value.value === '-Infinity' ? -Infinity : Infinity };
|
|
522
|
+
case 'NaNLiteral':
|
|
523
|
+
return { ok: true, value: Number.NaN };
|
|
524
|
+
case 'BooleanLiteral':
|
|
525
|
+
return { ok: true, value: value.value };
|
|
526
|
+
case 'ToggleLiteral':
|
|
527
|
+
return { ok: true, value: value.value };
|
|
528
|
+
case 'NullLiteral':
|
|
529
|
+
return { ok: true, value: null };
|
|
530
|
+
default:
|
|
531
|
+
return { ok: false };
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
function scalarKindFromValue(value) {
|
|
536
|
+
switch (value.type) {
|
|
537
|
+
case 'TypedValue':
|
|
538
|
+
return scalarKindFromValue(value.value);
|
|
539
|
+
case 'NullLiteral':
|
|
540
|
+
return 'null';
|
|
541
|
+
case 'InfinityLiteral':
|
|
542
|
+
return 'infinity';
|
|
543
|
+
case 'NaNLiteral':
|
|
544
|
+
return 'nan';
|
|
545
|
+
default:
|
|
546
|
+
return undefined;
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
function nullReasonFromValue(value) {
|
|
551
|
+
switch (value.type) {
|
|
552
|
+
case 'TypedValue':
|
|
553
|
+
return nullReasonFromValue(value.value);
|
|
554
|
+
case 'NullLiteral':
|
|
555
|
+
return value.value;
|
|
556
|
+
default:
|
|
557
|
+
return undefined;
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
function renderTextResults(results) {
|
|
562
|
+
if (results.length === 0) return '(no results)';
|
|
563
|
+
return results.flatMap((entry) => renderQueryValueLines(entry.value, entry.binding)).join('\n');
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
function renderQueryInspect(query) {
|
|
567
|
+
return [
|
|
568
|
+
`canonical: ${query.canonical}`,
|
|
569
|
+
`from: ${summarizeQueryFrom(query.from)}`,
|
|
570
|
+
...(query.where ? [`where: ${query.where.expression}`] : []),
|
|
571
|
+
...(query.orderBy ? query.orderBy.keys.map((key, index) => (
|
|
572
|
+
`order[${index}]: ${key.expression} ${key.direction}`
|
|
573
|
+
)) : []),
|
|
574
|
+
...(query.offset ? [`offset: ${query.offset.value}`] : []),
|
|
575
|
+
...(query.limit ? [`limit: ${query.limit.value}`] : []),
|
|
576
|
+
`select: ${query.select.expression}`,
|
|
577
|
+
].join('\n');
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
function renderInspectResults(results) {
|
|
581
|
+
if (results.length === 0) return '(no results)';
|
|
582
|
+
return results.map((entry, index) => [
|
|
583
|
+
`Result ${index + 1}`,
|
|
584
|
+
`candidate: ${entry.address ?? entry.binding.address ?? '<binding>'}`,
|
|
585
|
+
...renderBindingInspectLines(entry.binding, 'candidate'),
|
|
586
|
+
...renderQueryValueInspectLines(entry.value),
|
|
587
|
+
].join('\n')).join('\n\n');
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
function renderDiagnosticText(errors) {
|
|
591
|
+
if (errors.length === 0) return '(no diagnostics)';
|
|
592
|
+
return errors.map((error) => {
|
|
593
|
+
const phase = typeof error.phase === 'string' ? ` [${error.phase}]` : '';
|
|
594
|
+
const candidate = typeof error.candidateAddress === 'string' ? ` at ${error.candidateAddress}` : '';
|
|
595
|
+
const location = Number.isInteger(error.index)
|
|
596
|
+
? ` index ${error.index}`
|
|
597
|
+
: Number.isInteger(error.selectorIndex)
|
|
598
|
+
? ` selector ${error.selectorIndex}`
|
|
599
|
+
: '';
|
|
600
|
+
return `${error.code}${phase}${candidate}${location}: ${error.message}`;
|
|
601
|
+
}).join('\n');
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
function renderQueryValueLines(value, sourceBinding) {
|
|
605
|
+
switch (value.type) {
|
|
606
|
+
case 'scalar':
|
|
607
|
+
return [`${sourceBinding.address ?? '<binding>'} = ${renderAeonValue(value.value, value[QUERY_VALUE_METADATA_PROPERTY])}`];
|
|
608
|
+
case 'object':
|
|
609
|
+
return [`${sourceBinding.address ?? '<binding>'} = ${renderAeonValue(value.value, undefined, value.value?.[QUERY_OBJECT_FIELD_METADATA_PROPERTY])}`];
|
|
610
|
+
case 'bindingSet':
|
|
611
|
+
if (value.bindings.length === 0) return [`${sourceBinding.address ?? '<binding>'} -> (empty)`];
|
|
612
|
+
return value.bindings.map((binding) => {
|
|
613
|
+
const scalar = scalarFromBinding(binding);
|
|
614
|
+
return scalar.ok
|
|
615
|
+
? `${binding.address ?? '<binding>'} = ${renderAeonValue(scalar.value, scalarMetadataFromBinding(binding))}`
|
|
616
|
+
: `${binding.address ?? '<binding>'}`;
|
|
617
|
+
});
|
|
618
|
+
default:
|
|
619
|
+
return [`${sourceBinding.address ?? '<binding>'} = ${renderAeonValue(value)}`];
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
function renderAeonValue(value, metadata, fieldMetadata) {
|
|
624
|
+
if ((metadata?.kind === 'null' || value === null) && typeof metadata?.nullReason === 'string') {
|
|
625
|
+
return `!${metadata.nullReason}`;
|
|
626
|
+
}
|
|
627
|
+
if (metadata?.kind === 'nan' || (typeof value === 'number' && Number.isNaN(value))) return 'NaN';
|
|
628
|
+
if (metadata?.kind === 'infinity' || value === Infinity) return 'Infinity';
|
|
629
|
+
if (value === -Infinity) return '-Infinity';
|
|
630
|
+
if (value === null) return 'null';
|
|
631
|
+
if (typeof value === 'string') return JSON.stringify(value);
|
|
632
|
+
if (typeof value === 'number' || typeof value === 'boolean') return String(value);
|
|
633
|
+
if (value?.type === 'SansaAddressLiteral') {
|
|
634
|
+
return JSON.stringify(value.canonical ?? value.value ?? value.address?.canonical ?? value.address);
|
|
635
|
+
}
|
|
636
|
+
if (Array.isArray(value)) {
|
|
637
|
+
return `[${value.map((entry) => renderAeonValue(entry)).join(',')}]`;
|
|
638
|
+
}
|
|
639
|
+
if (value && typeof value === 'object') {
|
|
640
|
+
return `{${Object.entries(value).map(([key, entry]) => (
|
|
641
|
+
`${JSON.stringify(key)}:${renderAeonValue(entry, fieldMetadata?.[key])}`
|
|
642
|
+
)).join(',')}}`;
|
|
643
|
+
}
|
|
644
|
+
return JSON.stringify(value);
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
function scalarMetadataFromBinding(binding) {
|
|
648
|
+
const metadata = {};
|
|
649
|
+
const kind = binding.scalarKind ?? binding.valueKind ?? binding.literalKind ?? binding.representationKind ?? binding.kind ?? binding.type;
|
|
650
|
+
if (typeof kind === 'string') metadata.kind = lowerFirst(kind);
|
|
651
|
+
if (binding.nullReason !== undefined) metadata.nullReason = binding.nullReason;
|
|
652
|
+
return Object.keys(metadata).length > 0 ? metadata : undefined;
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
function lowerFirst(value) {
|
|
656
|
+
return value ? value[0].toLowerCase() + value.slice(1) : value;
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
function renderQueryValueInspectLines(value) {
|
|
660
|
+
switch (value.type) {
|
|
661
|
+
case 'scalar':
|
|
662
|
+
return [`value: scalar ${JSON.stringify(sanitizeJsonValue(value.value))}`];
|
|
663
|
+
case 'object':
|
|
664
|
+
return [`value: object ${JSON.stringify(sanitizeJsonValue(value.value))}`];
|
|
665
|
+
case 'bindingSet':
|
|
666
|
+
if (value.bindings.length === 0) return ['value: bindingSet (0 bindings)'];
|
|
667
|
+
return [
|
|
668
|
+
`value: bindingSet (${value.bindings.length} binding${value.bindings.length === 1 ? '' : 's'})`,
|
|
669
|
+
...value.bindings.flatMap((binding) => [
|
|
670
|
+
`- ${binding.address ?? '<binding>'}`,
|
|
671
|
+
...renderBindingInspectLines(binding, ' binding'),
|
|
672
|
+
]),
|
|
673
|
+
];
|
|
674
|
+
default:
|
|
675
|
+
return [`value: ${JSON.stringify(sanitizeJsonValue(value))}`];
|
|
676
|
+
}
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
function renderBindingInspectLines(binding, label) {
|
|
680
|
+
const summary = summarizeBinding(binding);
|
|
681
|
+
return [
|
|
682
|
+
...(summary.name === undefined ? [] : [`${label}.name: ${summary.name}`]),
|
|
683
|
+
...(summary.index === undefined ? [] : [`${label}.index: ${summary.index}`]),
|
|
684
|
+
...(summary.semanticType === undefined ? [] : [`${label}.semanticType: ${summary.semanticType}`]),
|
|
685
|
+
...(summary.representationKind === undefined ? [] : [`${label}.representationKind: ${summary.representationKind}`]),
|
|
686
|
+
...(summary.scalarKind === undefined ? [] : [`${label}.scalarKind: ${summary.scalarKind}`]),
|
|
687
|
+
...(summary.nullReason === undefined ? [] : [`${label}.nullReason: ${summary.nullReason}`]),
|
|
688
|
+
...(summary.value === undefined ? [] : [`${label}.value: ${JSON.stringify(summary.value)}`]),
|
|
689
|
+
];
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
function summarizeQuery(query) {
|
|
693
|
+
return {
|
|
694
|
+
canonical: query.canonical,
|
|
695
|
+
clauses: query.clauses,
|
|
696
|
+
from: summarizeQueryFrom(query.from),
|
|
697
|
+
...(query.where ? { where: query.where.expression } : {}),
|
|
698
|
+
...(query.orderBy ? {
|
|
699
|
+
orderBy: query.orderBy.keys.map((key) => ({
|
|
700
|
+
expression: key.expression,
|
|
701
|
+
direction: key.direction,
|
|
702
|
+
})),
|
|
703
|
+
} : {}),
|
|
704
|
+
...(query.offset ? { offset: query.offset.value } : {}),
|
|
705
|
+
...(query.limit ? { limit: query.limit.value } : {}),
|
|
706
|
+
select: query.select.expression,
|
|
707
|
+
};
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
function summarizeQueryFrom(from) {
|
|
711
|
+
return from.source === 'expression'
|
|
712
|
+
? from.expression
|
|
713
|
+
: from.address.canonical;
|
|
714
|
+
}
|
|
715
|
+
|
|
716
|
+
function summarizeQueryValue(value) {
|
|
717
|
+
switch (value.type) {
|
|
718
|
+
case 'bindingSet':
|
|
719
|
+
return {
|
|
720
|
+
type: value.type,
|
|
721
|
+
bindings: value.bindings.map(summarizeBinding),
|
|
722
|
+
};
|
|
723
|
+
case 'scalar':
|
|
724
|
+
case 'object':
|
|
725
|
+
return sanitizeJsonValue(value);
|
|
726
|
+
default:
|
|
727
|
+
return sanitizeJsonValue(value);
|
|
728
|
+
}
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
function summarizeBinding(binding) {
|
|
732
|
+
const scalar = scalarFromBinding(binding);
|
|
733
|
+
return {
|
|
734
|
+
...(binding.address === undefined ? {} : { address: binding.address }),
|
|
735
|
+
...(binding.name === undefined ? {} : { name: binding.name }),
|
|
736
|
+
...(binding.index === undefined ? {} : { index: binding.index }),
|
|
737
|
+
...(binding.semanticType === undefined ? {} : { semanticType: binding.semanticType }),
|
|
738
|
+
...(binding.representationKind === undefined ? {} : { representationKind: binding.representationKind }),
|
|
739
|
+
...(binding.scalarKind === undefined ? {} : { scalarKind: binding.scalarKind }),
|
|
740
|
+
...(binding.nullReason === undefined ? {} : { nullReason: binding.nullReason }),
|
|
741
|
+
...(scalar.ok ? { value: sanitizeJsonValue(scalar.value) } : {}),
|
|
742
|
+
};
|
|
743
|
+
}
|
|
744
|
+
|
|
745
|
+
function scalarFromBinding(binding) {
|
|
746
|
+
if (Object.hasOwn(binding, 'value')) return { ok: true, value: binding.value };
|
|
747
|
+
if (Object.hasOwn(binding, 'scalar')) return { ok: true, value: binding.scalar };
|
|
748
|
+
return { ok: false };
|
|
749
|
+
}
|
|
750
|
+
|
|
751
|
+
function normalizeDiagnostics(errors) {
|
|
752
|
+
return errors.map((error) => ({
|
|
753
|
+
code: error.code,
|
|
754
|
+
message: error.message,
|
|
755
|
+
...(typeof error.phase === 'string' ? { phase: error.phase } : {}),
|
|
756
|
+
...(typeof error.candidateAddress === 'string' ? { candidateAddress: error.candidateAddress } : {}),
|
|
757
|
+
...(typeof error.extension === 'string' ? { extension: error.extension } : {}),
|
|
758
|
+
...(typeof error.budget === 'string' ? { budget: error.budget } : {}),
|
|
759
|
+
...(Number.isSafeInteger(error.limit) ? { limit: error.limit } : {}),
|
|
760
|
+
...(Number.isSafeInteger(error.observed) ? { observed: error.observed } : {}),
|
|
761
|
+
...(Number.isInteger(error.index) ? { index: error.index } : {}),
|
|
762
|
+
...(Number.isInteger(error.selectorIndex) ? { selectorIndex: error.selectorIndex } : {}),
|
|
763
|
+
}));
|
|
764
|
+
}
|
|
765
|
+
|
|
766
|
+
function queryBudgetOption(value) {
|
|
767
|
+
if (!value || typeof value !== 'object') return {};
|
|
768
|
+
const budget = {};
|
|
769
|
+
for (const key of ['maxFromBindings', 'maxWhereCandidates', 'maxOrderCandidates', 'maxResultRecords']) {
|
|
770
|
+
const limit = value[key];
|
|
771
|
+
if (Number.isSafeInteger(limit) && limit >= 0) budget[key] = limit;
|
|
772
|
+
}
|
|
773
|
+
return Object.keys(budget).length === 0 ? {} : { budget };
|
|
774
|
+
}
|
|
775
|
+
|
|
776
|
+
function normalizeAeonError(error) {
|
|
777
|
+
return {
|
|
778
|
+
code: error.code ?? error.name ?? 'AEON_ERROR',
|
|
779
|
+
message: error.message,
|
|
780
|
+
...(error.span ? {
|
|
781
|
+
span: {
|
|
782
|
+
start: error.span.start,
|
|
783
|
+
end: error.span.end,
|
|
784
|
+
},
|
|
785
|
+
} : {}),
|
|
786
|
+
};
|
|
787
|
+
}
|
|
788
|
+
|
|
789
|
+
function sanitizeJsonValue(value) {
|
|
790
|
+
if (typeof value === 'number' && !Number.isFinite(value)) return String(value);
|
|
791
|
+
if (Array.isArray(value)) return value.map(sanitizeJsonValue);
|
|
792
|
+
if (value && typeof value === 'object') {
|
|
793
|
+
return Object.fromEntries(Object.entries(value).map(([key, entry]) => [key, sanitizeJsonValue(entry)]));
|
|
794
|
+
}
|
|
795
|
+
return value;
|
|
796
|
+
}
|