@lark-base-open/base-database-toolchain 1.4.1 → 1.5.1
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 +55 -0
- package/README.md +8 -2
- package/dist/contract-conversion.js +1 -0
- package/dist/data/config-schema.json +7 -38
- package/dist/dev-diagnostics.d.mts +4 -0
- package/dist/dev-diagnostics.mjs +73 -0
- package/dist/dev-worker.mjs +4 -1
- package/dist/function-batch.d.ts +4 -1
- package/dist/function-batch.js +5 -4
- package/dist/function-params.d.ts +1 -1
- package/dist/function-params.js +10 -5
- package/dist/function-schema.d.ts +5 -0
- package/dist/function-schema.js +174 -22
- package/dist/go-adapter.mjs +216 -275
- package/dist/project-layout.d.ts +2 -7
- package/dist/project-layout.js +12 -29
- package/dist/sdk-codegen.js +21 -15
- package/npm-shrinkwrap.json +11 -11
- package/package.json +3 -3
package/dist/function-schema.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { RE2JS } from 're2js';
|
|
2
|
-
import { checkParameterDescription, } from './function-params.js';
|
|
2
|
+
import { checkParameterDescription, checkDeclaredParameters, sqlEnumType, } from './function-params.js';
|
|
3
3
|
const JSON_SCHEMA_DRAFT_2020_12 = 'https://json-schema.org/draft/2020-12/schema';
|
|
4
4
|
const MAX_SCHEMA_BYTES = 32 * 1024;
|
|
5
5
|
const MAX_SCHEMA_DEPTH = 16;
|
|
@@ -249,21 +249,90 @@ function validateSchemaNode(value, path, depth, state) {
|
|
|
249
249
|
}
|
|
250
250
|
}
|
|
251
251
|
}
|
|
252
|
+
const SCHEMA_MAPS = new Set(['properties', 'patternProperties', '$defs']);
|
|
253
|
+
const SCHEMA_LISTS = new Set(['oneOf', 'anyOf', 'allOf', 'prefixItems']);
|
|
254
|
+
const SCHEMA_SINGLES = new Set([
|
|
255
|
+
'items',
|
|
256
|
+
'additionalProperties',
|
|
257
|
+
'propertyNames',
|
|
258
|
+
'contains',
|
|
259
|
+
'not',
|
|
260
|
+
]);
|
|
261
|
+
// Zod writes literals as `const`, which the Edge inputSchema subset does not
|
|
262
|
+
// accept; a single-value `enum` validates the same values. Output and Durable
|
|
263
|
+
// schemas accept `const` and keep it, so their generated contracts stay stable.
|
|
264
|
+
export function inputConstToEnum(node) {
|
|
265
|
+
if (!node || typeof node !== 'object' || Array.isArray(node))
|
|
266
|
+
return;
|
|
267
|
+
const schema = node;
|
|
268
|
+
if (Object.hasOwn(schema, 'const') && !Object.hasOwn(schema, 'enum')) {
|
|
269
|
+
schema.enum = [schema.const];
|
|
270
|
+
delete schema.const;
|
|
271
|
+
}
|
|
272
|
+
for (const [key, child] of Object.entries(schema)) {
|
|
273
|
+
if (SCHEMA_MAPS.has(key) && child && typeof child === 'object')
|
|
274
|
+
Object.values(child).forEach(inputConstToEnum);
|
|
275
|
+
else if (SCHEMA_LISTS.has(key) && Array.isArray(child))
|
|
276
|
+
child.forEach(inputConstToEnum);
|
|
277
|
+
else if (SCHEMA_SINGLES.has(key))
|
|
278
|
+
inputConstToEnum(child);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
const KEYWORD_REPLACEMENTS = {
|
|
282
|
+
const: 'use enum with one value',
|
|
283
|
+
format: 'describe the format and validate it in the handler',
|
|
284
|
+
$ref: 'inline the type; recursive or reused schemas such as z.lazy() and z.json() are not supported in inputs',
|
|
285
|
+
$defs: 'inline the type; recursive or reused schemas such as z.lazy() and z.json() are not supported in inputs',
|
|
286
|
+
propertyNames: 'declare known properties with z.object(); records are not supported in inputs',
|
|
287
|
+
};
|
|
288
|
+
// Lists every unsupported keyword at once, walking the positions the validator
|
|
289
|
+
// descends into, so one generate run names all of them instead of the first.
|
|
290
|
+
function collectUnsupportedKeywords(value, path, out) {
|
|
291
|
+
if (!isRecord(value))
|
|
292
|
+
return;
|
|
293
|
+
for (const keyword of Object.keys(value)) {
|
|
294
|
+
if (ALLOWED_KEYWORDS.has(keyword))
|
|
295
|
+
continue;
|
|
296
|
+
const replacement = KEYWORD_REPLACEMENTS[keyword];
|
|
297
|
+
out.push(`${path}: ${JSON.stringify(keyword)}${replacement ? ` (${replacement})` : ''}`);
|
|
298
|
+
}
|
|
299
|
+
if (isRecord(value.properties))
|
|
300
|
+
for (const [name, child] of Object.entries(value.properties))
|
|
301
|
+
collectUnsupportedKeywords(child, `${path}.properties[${JSON.stringify(name)}]`, out);
|
|
302
|
+
collectUnsupportedKeywords(value.items, `${path}.items`, out);
|
|
303
|
+
collectUnsupportedKeywords(value.additionalProperties, `${path}.additionalProperties`, out);
|
|
304
|
+
for (const key of ['prefixItems', 'oneOf', 'anyOf']) {
|
|
305
|
+
const children = value[key];
|
|
306
|
+
if (Array.isArray(children))
|
|
307
|
+
children.forEach((child, index) => collectUnsupportedKeywords(child, `${path}.${key}[${index}]`, out));
|
|
308
|
+
}
|
|
309
|
+
}
|
|
252
310
|
export function normalizeFunctionInputSchema(value, source) {
|
|
253
311
|
const schema = JSON.stringify(value);
|
|
312
|
+
const unsupported = [];
|
|
313
|
+
collectUnsupportedKeywords(JSON.parse(schema), source, unsupported);
|
|
314
|
+
if (unsupported.length)
|
|
315
|
+
throw new Error(`Invalid inputSchema: ${unsupported.length} unsupported keyword${unsupported.length > 1 ? 's' : ''}: ${unsupported.join('; ')}. Supported keywords: ${[...ALLOWED_KEYWORDS].join(', ')}`);
|
|
254
316
|
validateSchemaNode(JSON.parse(schema), source, 1, { nodes: 0 });
|
|
255
317
|
if (Buffer.byteLength(schema, 'utf8') > MAX_SCHEMA_BYTES) {
|
|
256
318
|
schemaError(source, 'exceeds 32 KiB');
|
|
257
319
|
}
|
|
258
320
|
return schema;
|
|
259
321
|
}
|
|
322
|
+
const allowedSQLKinds = `Allowed: ${[...SQL_SCALAR_PARAM_KINDS].join(', ')}, or any of these with one [] suffix (e.g. TEXT[]).`;
|
|
323
|
+
function sqlKindProblem(kind, index) {
|
|
324
|
+
if (typeof kind !== 'string')
|
|
325
|
+
return `meta.params[${index}].kind must be a string. ${allowedSQLKinds}`;
|
|
326
|
+
if (!SQL_SCALAR_PARAM_KINDS.has(kind.endsWith('[]') ? kind.slice(0, -2) : kind)) {
|
|
327
|
+
return `Unsupported Database Function query parameter kind at meta.params[${index}].kind: ${JSON.stringify(kind)}. ${allowedSQLKinds}`;
|
|
328
|
+
}
|
|
329
|
+
}
|
|
260
330
|
function parseSQLParamKind(kind, index) {
|
|
331
|
+
const problem = sqlKindProblem(kind, index);
|
|
332
|
+
if (problem)
|
|
333
|
+
throw new Error(problem);
|
|
261
334
|
const isArray = kind.endsWith('[]');
|
|
262
|
-
|
|
263
|
-
if (!SQL_SCALAR_PARAM_KINDS.has(scalarKind)) {
|
|
264
|
-
throw new Error(`Unsupported Database Function query parameter kind at index ${index}: ${kind}`);
|
|
265
|
-
}
|
|
266
|
-
return { scalarKind, isArray };
|
|
335
|
+
return { scalarKind: isArray ? kind.slice(0, -2) : kind, isArray };
|
|
267
336
|
}
|
|
268
337
|
function sqlScalarParamSchema(kind, inputDescription, index) {
|
|
269
338
|
let schema;
|
|
@@ -304,7 +373,7 @@ function sqlScalarParamSchema(kind, inputDescription, index) {
|
|
|
304
373
|
default: {
|
|
305
374
|
const members = COMPOSITE_PARAM_MEMBERS[kind];
|
|
306
375
|
if (!members)
|
|
307
|
-
throw new Error(`Unsupported scalar parameter kind: ${kind}`);
|
|
376
|
+
throw new Error(`Unsupported scalar parameter kind: ${kind}. ${allowedSQLKinds}`);
|
|
308
377
|
schema = {
|
|
309
378
|
type: ['object', 'null'],
|
|
310
379
|
properties: Object.fromEntries(Object.entries(members).map(([name, type]) => [
|
|
@@ -374,7 +443,7 @@ export function namedSQLParamSchema(param, index) {
|
|
|
374
443
|
: {}),
|
|
375
444
|
};
|
|
376
445
|
}
|
|
377
|
-
function sqlPlaceholderIndexes(sql) {
|
|
446
|
+
function sqlPlaceholderIndexes(sql, problems) {
|
|
378
447
|
const indexes = new Set();
|
|
379
448
|
let mode = 'normal';
|
|
380
449
|
for (let index = 0; index < sql.length; index += 1) {
|
|
@@ -430,24 +499,101 @@ function sqlPlaceholderIndexes(sql) {
|
|
|
430
499
|
end += 1;
|
|
431
500
|
const placeholder = Number(sql.slice(index + 1, end));
|
|
432
501
|
if (!Number.isSafeInteger(placeholder) || placeholder < 1) {
|
|
433
|
-
|
|
502
|
+
problems.push(`Invalid query placeholder: ${sql.slice(index, end)}. Use a positive, safe integer starting at $1.`);
|
|
434
503
|
}
|
|
435
|
-
|
|
504
|
+
else
|
|
505
|
+
indexes.add(placeholder);
|
|
436
506
|
index = end - 1;
|
|
437
507
|
}
|
|
438
508
|
}
|
|
439
509
|
return indexes;
|
|
440
510
|
}
|
|
441
|
-
function
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
for (let
|
|
447
|
-
|
|
448
|
-
|
|
511
|
+
function placeholderRange(first, last) {
|
|
512
|
+
return first === last ? `$${first}` : `$${first}..$${last}`;
|
|
513
|
+
}
|
|
514
|
+
function placeholderRanges(indexes) {
|
|
515
|
+
const ranges = [];
|
|
516
|
+
for (let start = 0; start < indexes.length; start++) {
|
|
517
|
+
let end = start;
|
|
518
|
+
while (end + 1 < indexes.length && indexes[end + 1] === indexes[end] + 1)
|
|
519
|
+
end++;
|
|
520
|
+
ranges.push(placeholderRange(indexes[start], indexes[end]));
|
|
521
|
+
start = end;
|
|
522
|
+
}
|
|
523
|
+
return ranges.join(', ') || '(none)';
|
|
524
|
+
}
|
|
525
|
+
function checkSQLPlaceholders(sql, params, problems) {
|
|
526
|
+
const used = sqlPlaceholderIndexes(sql, problems);
|
|
527
|
+
const indexes = [...used].sort((a, b) => a - b);
|
|
528
|
+
if (used.size === params.length && indexes.every((n, i) => n === i + 1))
|
|
529
|
+
return;
|
|
530
|
+
const names = params.map((p) => isRecord(p) && typeof p.name === 'string'
|
|
531
|
+
? JSON.stringify(p.name)
|
|
532
|
+
: '<unnamed>');
|
|
533
|
+
const unused = names.flatMap((name, i) => used.has(i + 1) ? [] : [`$${i + 1} (${name})`]);
|
|
534
|
+
const undeclared = indexes.filter((i) => i > params.length);
|
|
535
|
+
const differences = [];
|
|
536
|
+
const gaps = [];
|
|
537
|
+
let next = 1;
|
|
538
|
+
for (const n of indexes) {
|
|
539
|
+
if (n > next)
|
|
540
|
+
gaps.push(placeholderRange(next, n - 1));
|
|
541
|
+
next = n + 1;
|
|
542
|
+
}
|
|
543
|
+
if (gaps.length)
|
|
544
|
+
differences.push(`Missing from the SQL sequence: ${gaps.join(', ')}.`);
|
|
545
|
+
if (unused.length)
|
|
546
|
+
differences.push(`Declared but unused in SQL: ${unused.join(', ')}.`);
|
|
547
|
+
if (undeclared.length)
|
|
548
|
+
differences.push(`Used in SQL without a declaration: ${placeholderRanges(undeclared)}.`);
|
|
549
|
+
problems.push(`main.sql uses ${placeholderRanges(indexes)}; meta.params declares ${params.length} in order: [${names.join(', ')}]. ${differences.join(' ')} Fix: align SQL placeholders and declarations by position: meta.params[i] supplies $(i+1), starting with meta.params[0] for $1; use consecutive positions without gaps.`);
|
|
550
|
+
}
|
|
551
|
+
/** Authoring diagnostics for the named SQL contract. Metadata-only codegen may
|
|
552
|
+
* run before main.sql exists, in which case there are no placeholders to check.
|
|
553
|
+
*/
|
|
554
|
+
export function checkSQLFunctionParameters(params, sql, file) {
|
|
555
|
+
if (!Array.isArray(params))
|
|
556
|
+
return {
|
|
557
|
+
problems: [
|
|
558
|
+
'meta.params must be an array of parameter objects; use [] for a function without parameters.',
|
|
559
|
+
],
|
|
560
|
+
hints: [],
|
|
561
|
+
};
|
|
562
|
+
const result = checkDeclaredParameters(params, {
|
|
563
|
+
label: 'meta.params',
|
|
564
|
+
pointer: '/meta/params',
|
|
565
|
+
file,
|
|
566
|
+
enumType: sqlEnumType,
|
|
567
|
+
});
|
|
568
|
+
if (sql !== undefined)
|
|
569
|
+
checkSQLPlaceholders(sql, params, result.problems);
|
|
570
|
+
params.forEach((param, index) => {
|
|
571
|
+
if (!isRecord(param))
|
|
572
|
+
return;
|
|
573
|
+
// The existing metadata-only named projection coerces kind to a string.
|
|
574
|
+
const kind = sql === undefined ? String(param.kind) : param.kind;
|
|
575
|
+
const problem = sqlKindProblem(kind, index);
|
|
576
|
+
if (problem)
|
|
577
|
+
result.problems.push(problem);
|
|
578
|
+
const hint = (key, message) => result.hints.push({
|
|
579
|
+
kind: 'parameter_declaration',
|
|
580
|
+
path: `${file}#/meta/params/${index}/${key}`,
|
|
581
|
+
message: `meta.params[${index}].${key}: ${message}`,
|
|
582
|
+
});
|
|
583
|
+
if (param.nullable !== undefined && typeof param.nullable !== 'boolean')
|
|
584
|
+
hint('nullable', 'Allowed: true or false as a JSON boolean, or omit nullable.');
|
|
585
|
+
for (const key of ['minimum', 'maximum']) {
|
|
586
|
+
if (param[key] === undefined)
|
|
587
|
+
continue;
|
|
588
|
+
if (typeof param[key] !== 'number' ||
|
|
589
|
+
!Number.isFinite(param[key]) ||
|
|
590
|
+
!['INT', 'DOUBLE', 'TIMESTAMP'].includes(typeof kind === 'string' && kind.endsWith('[]')
|
|
591
|
+
? kind.slice(0, -2)
|
|
592
|
+
: String(kind)))
|
|
593
|
+
hint(key, 'Allowed: a finite JSON number on numeric values (INT, DOUBLE, or TIMESTAMP epoch milliseconds, including array elements); otherwise omit this bound.');
|
|
449
594
|
}
|
|
450
|
-
}
|
|
595
|
+
});
|
|
596
|
+
return result;
|
|
451
597
|
}
|
|
452
598
|
/**
|
|
453
599
|
* Local Page input contract for the positional `invoke_function` route.
|
|
@@ -461,7 +607,7 @@ export function deriveSQLInputSchema(meta, sql) {
|
|
|
461
607
|
}
|
|
462
608
|
const params = rawParams.map((value, index) => {
|
|
463
609
|
if (!isRecord(value) || typeof value.kind !== 'string') {
|
|
464
|
-
throw new Error(`Database Function meta.params[${index}].kind must be a string`);
|
|
610
|
+
throw new Error(`Database Function meta.params[${index}].kind must be a string. ${allowedSQLKinds}`);
|
|
465
611
|
}
|
|
466
612
|
if (value.description !== undefined &&
|
|
467
613
|
typeof value.description !== 'string') {
|
|
@@ -473,7 +619,10 @@ export function deriveSQLInputSchema(meta, sql) {
|
|
|
473
619
|
...(Array.isArray(value.enum) ? { enum: value.enum } : {}),
|
|
474
620
|
};
|
|
475
621
|
});
|
|
476
|
-
|
|
622
|
+
const problems = [];
|
|
623
|
+
checkSQLPlaceholders(sql, rawParams, problems);
|
|
624
|
+
if (problems.length)
|
|
625
|
+
throw new Error(problems.join(' '));
|
|
477
626
|
return JSON.stringify({
|
|
478
627
|
$schema: JSON_SCHEMA_DRAFT_2020_12,
|
|
479
628
|
type: 'array',
|
|
@@ -491,7 +640,10 @@ export function deriveSQLInputSchema(meta, sql) {
|
|
|
491
640
|
export function checkFaaSInputSchemaContract(value, file) {
|
|
492
641
|
const result = { problems: [], hints: [] };
|
|
493
642
|
if (!isRecord(value) || value.type !== 'object') {
|
|
494
|
-
result.problems.push(
|
|
643
|
+
result.problems.push(isRecord(value) &&
|
|
644
|
+
(value.oneOf !== undefined || value.anyOf !== undefined)
|
|
645
|
+
? 'inputSchema.type must be "object"; a Zod union at the root becomes oneOf/anyOf. Keep the root a z.object() and put the union in a property, for example z.object({ command: z.discriminatedUnion("action", [...]).describe("...") })'
|
|
646
|
+
: 'inputSchema.type must be "object"');
|
|
495
647
|
return result;
|
|
496
648
|
}
|
|
497
649
|
const properties = isRecord(value.properties) ? value.properties : {};
|