@kubohiroya/turbowarp-named-functions 0.1.2 → 0.3.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/README.ja.md +150 -7
- package/README.md +192 -16
- package/dist/composition.js +371 -25
- package/dist/extension-manifest.json +144 -0
- package/dist/turbowarp-named-functions.js +1476 -904
- package/dist/types/block-literals.d.ts +16 -0
- package/dist/types/composition.d.ts +11 -1
- package/dist/types/extension.d.ts +17 -1
- package/dist/types/function-registry.d.ts +8 -1
- package/dist/types/runtime-types.d.ts +2 -0
- package/dist/types/schema-builder.d.ts +52 -0
- package/package.json +8 -8
- package/src/block-definitions.json +199 -2
- package/src/block-literals.ts +62 -0
- package/src/composition.ts +40 -5
- package/src/extension.ts +64 -2
- package/src/function-registry.ts +43 -32
- package/src/runtime-types.ts +2 -0
- package/src/schema-builder.ts +458 -0
package/dist/composition.js
CHANGED
|
@@ -258,6 +258,46 @@ function parseJsonOrText(text) {
|
|
|
258
258
|
}
|
|
259
259
|
}
|
|
260
260
|
//#endregion
|
|
261
|
+
//#region src/block-literals.ts
|
|
262
|
+
function readLiteralInput(block, blocks, inputName) {
|
|
263
|
+
const input = block.inputs?.[inputName];
|
|
264
|
+
if (!input) throw new Error(`${inputName} is missing`);
|
|
265
|
+
if (input.block && input.block !== input.shadow) throw new Error(`${inputName} must be literal text, not a reporter block`);
|
|
266
|
+
const shadowId = input.shadow ?? input.block;
|
|
267
|
+
const shadow = shadowId ? blocks[shadowId] : void 0;
|
|
268
|
+
const field = shadow?.fields ? Object.values(shadow.fields)[0] : void 0;
|
|
269
|
+
if (!field) throw new Error(`${inputName} is missing`);
|
|
270
|
+
return String(field.value ?? "");
|
|
271
|
+
}
|
|
272
|
+
function readOptionalLiteralInput(block, blocks, inputName) {
|
|
273
|
+
if (!block.inputs?.[inputName]) return void 0;
|
|
274
|
+
return readLiteralInput(block, blocks, inputName);
|
|
275
|
+
}
|
|
276
|
+
/** Reads a dropdown that does not accept reporters; the VM stores it as a field on the block. */
|
|
277
|
+
function readMenuField(block, fieldName) {
|
|
278
|
+
const field = block.fields?.[fieldName];
|
|
279
|
+
if (!field) throw new Error(`${fieldName} is missing`);
|
|
280
|
+
return String(field.value ?? "");
|
|
281
|
+
}
|
|
282
|
+
/**
|
|
283
|
+
* The blocks stacked under `block`, in order. Shadow blocks are skipped, and a `next` chain that
|
|
284
|
+
* loops or leaves the container stops the walk instead of hanging.
|
|
285
|
+
*/
|
|
286
|
+
function stackBelow(block, blocks) {
|
|
287
|
+
const stack = [];
|
|
288
|
+
const seen = /* @__PURE__ */ new Set([block.id]);
|
|
289
|
+
let nextId = block.next ?? null;
|
|
290
|
+
while (nextId) {
|
|
291
|
+
if (seen.has(nextId)) break;
|
|
292
|
+
seen.add(nextId);
|
|
293
|
+
const next = blocks[nextId];
|
|
294
|
+
if (!next) break;
|
|
295
|
+
if (next.shadow !== true) stack.push(next);
|
|
296
|
+
nextId = next.next ?? null;
|
|
297
|
+
}
|
|
298
|
+
return stack;
|
|
299
|
+
}
|
|
300
|
+
//#endregion
|
|
261
301
|
//#region src/function-registry.ts
|
|
262
302
|
var FUNCTION_NAME_PATTERN = /^[A-Za-z0-9_-]{1,64}$/u;
|
|
263
303
|
var MAX_DESCRIPTION_LENGTH = 1024;
|
|
@@ -265,7 +305,7 @@ var MAX_DESCRIPTION_LENGTH = 1024;
|
|
|
265
305
|
* Reads every `define function` hat in the project. NAME, DESCRIPTION, and SCHEMA are part of the
|
|
266
306
|
* tool contract sent to the model before any script runs, so they must be literal text.
|
|
267
307
|
*/
|
|
268
|
-
function scanFunctionDefinitions(targets, hatOpcode) {
|
|
308
|
+
function scanFunctionDefinitions(targets, hatOpcode, options = {}) {
|
|
269
309
|
const functions = [];
|
|
270
310
|
const errors = [];
|
|
271
311
|
const seen = /* @__PURE__ */ new Map();
|
|
@@ -277,7 +317,7 @@ function scanFunctionDefinitions(targets, hatOpcode) {
|
|
|
277
317
|
if (block.opcode !== hatOpcode || block.topLevel === false) continue;
|
|
278
318
|
const where = `${targetName} (block ${block.id})`;
|
|
279
319
|
try {
|
|
280
|
-
const definition = readDefinition(block, blocks, targetName);
|
|
320
|
+
const definition = readDefinition(block, blocks, targetName, options);
|
|
281
321
|
const previous = seen.get(definition.name);
|
|
282
322
|
if (previous !== void 0) throw new Error(`function "${definition.name}" is already defined in ${previous}`);
|
|
283
323
|
seen.set(definition.name, where);
|
|
@@ -301,14 +341,14 @@ function toFunctionTools(functions) {
|
|
|
301
341
|
parameters: definition.parameters
|
|
302
342
|
}));
|
|
303
343
|
}
|
|
304
|
-
function readDefinition(block, blocks, targetName) {
|
|
344
|
+
function readDefinition(block, blocks, targetName, options) {
|
|
305
345
|
const name = readLiteralInput(block, blocks, "NAME").trim();
|
|
306
346
|
if (!FUNCTION_NAME_PATTERN.test(name)) throw new Error("function name must be 1-64 letters, digits, \"_\" or \"-\"");
|
|
307
347
|
const description = readLiteralInput(block, blocks, "DESCRIPTION").trim();
|
|
308
348
|
if (description.length > 1024) throw new Error(`description must be at most ${MAX_DESCRIPTION_LENGTH} characters`);
|
|
309
|
-
const parameters = parseSchema(readLiteralInput(block, blocks, "SCHEMA"), "args schema", true);
|
|
349
|
+
const parameters = parseSchema(readLiteralInput(block, blocks, "SCHEMA"), "args schema", true, options);
|
|
310
350
|
const returnsText = readOptionalLiteralInput(block, blocks, "RETURNS");
|
|
311
|
-
const returns = returnsText === void 0 || returnsText.trim() === "" ? void 0 : parseSchema(returnsText, "returns schema", false);
|
|
351
|
+
const returns = returnsText === void 0 || returnsText.trim() === "" ? void 0 : parseSchema(returnsText, "returns schema", false, options);
|
|
312
352
|
const exportAs = String(block.fields?.EXPORT?.value ?? "none") === "tool" ? "tool" : "none";
|
|
313
353
|
if (exportAs === "tool" && description.length === 0) throw new Error("a function exported as a tool needs a description");
|
|
314
354
|
return {
|
|
@@ -321,25 +361,17 @@ function readDefinition(block, blocks, targetName) {
|
|
|
321
361
|
blockId: block.id
|
|
322
362
|
};
|
|
323
363
|
}
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
if (!input) throw new Error(`${inputName} is missing`);
|
|
331
|
-
if (input.block && input.block !== input.shadow) throw new Error(`${inputName} must be literal text, not a reporter block`);
|
|
332
|
-
const shadowId = input.shadow ?? input.block;
|
|
333
|
-
const shadow = shadowId ? blocks[shadowId] : void 0;
|
|
334
|
-
const field = shadow?.fields ? Object.values(shadow.fields)[0] : void 0;
|
|
335
|
-
if (!field) throw new Error(`${inputName} is missing`);
|
|
336
|
-
return String(field.value ?? "");
|
|
337
|
-
}
|
|
338
|
-
function parseSchema(text, label, requireObjectType) {
|
|
364
|
+
/**
|
|
365
|
+
* SCHEMA and RETURNS hold either JSON Schema text or, when the consumer supports it, the name of a
|
|
366
|
+
* `define schema` hat. Text that is not JSON is treated as a name, so existing JSON projects are
|
|
367
|
+
* unaffected.
|
|
368
|
+
*/
|
|
369
|
+
function parseSchema(text, label, requireObjectType, options) {
|
|
339
370
|
let value;
|
|
340
371
|
try {
|
|
341
372
|
value = JSON.parse(text);
|
|
342
373
|
} catch {
|
|
374
|
+
if (options.resolveSchema) return resolveNamed(text, label, requireObjectType, options.resolveSchema);
|
|
343
375
|
throw new Error(`${label} must be valid JSON`);
|
|
344
376
|
}
|
|
345
377
|
if (typeof value !== "object" || value === null || Array.isArray(value)) throw new Error(`${label} must be a JSON object`);
|
|
@@ -348,6 +380,310 @@ function parseSchema(text, label, requireObjectType) {
|
|
|
348
380
|
validateSchemaDefinition(schema, label);
|
|
349
381
|
return schema;
|
|
350
382
|
}
|
|
383
|
+
function resolveNamed(text, label, requireObjectType, resolveSchema) {
|
|
384
|
+
const schema = resolveSchema(text, label);
|
|
385
|
+
if (requireObjectType && schema.type !== "object") throw new Error("args schema must have \"type\": \"object\"");
|
|
386
|
+
validateSchemaDefinition(schema, label);
|
|
387
|
+
return schema;
|
|
388
|
+
}
|
|
389
|
+
//#endregion
|
|
390
|
+
//#region src/schema-builder.ts
|
|
391
|
+
/**
|
|
392
|
+
* Lowers `define schema` block stacks into the canonical JSON Schema objects consumed by
|
|
393
|
+
* `validateAgainstSchema`.
|
|
394
|
+
*
|
|
395
|
+
* The builder is declarative: it reads literal block fields before any script runs and never
|
|
396
|
+
* executes the blocks it reads. Every schema it produces is checked with `validateSchemaDefinition`,
|
|
397
|
+
* so a block-built schema and a hand-written JSON one go through one validator and one diagnostic
|
|
398
|
+
* format.
|
|
399
|
+
*/
|
|
400
|
+
var SCHEMA_NAME_PATTERN = /^[A-Za-z0-9_.-]{1,128}$/u;
|
|
401
|
+
/** Complete SemVer, as the named-data schema identity contract requires. */
|
|
402
|
+
var SCHEMA_VERSION_PATTERN = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?(?:\+[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?$/u;
|
|
403
|
+
/** How deep a chain of schema references may nest before it is reported as too deep. */
|
|
404
|
+
var MAX_SCHEMA_REFERENCE_DEPTH = 16;
|
|
405
|
+
var PROPERTY_TYPES = {
|
|
406
|
+
string: () => ({ type: "string" }),
|
|
407
|
+
number: () => ({ type: "number" }),
|
|
408
|
+
integer: () => ({ type: "integer" }),
|
|
409
|
+
boolean: () => ({ type: "boolean" }),
|
|
410
|
+
null: () => ({ type: "null" }),
|
|
411
|
+
object: () => ({ type: "object" }),
|
|
412
|
+
array: () => ({ type: "array" }),
|
|
413
|
+
"array of string": () => ({
|
|
414
|
+
type: "array",
|
|
415
|
+
items: { type: "string" }
|
|
416
|
+
}),
|
|
417
|
+
"array of number": () => ({
|
|
418
|
+
type: "array",
|
|
419
|
+
items: { type: "number" }
|
|
420
|
+
}),
|
|
421
|
+
"array of integer": () => ({
|
|
422
|
+
type: "array",
|
|
423
|
+
items: { type: "integer" }
|
|
424
|
+
}),
|
|
425
|
+
"array of boolean": () => ({
|
|
426
|
+
type: "array",
|
|
427
|
+
items: { type: "boolean" }
|
|
428
|
+
}),
|
|
429
|
+
"array of object": () => ({
|
|
430
|
+
type: "array",
|
|
431
|
+
items: { type: "object" }
|
|
432
|
+
})
|
|
433
|
+
};
|
|
434
|
+
var CONSTRAINT_KEYWORDS = /* @__PURE__ */ new Set([
|
|
435
|
+
"minimum",
|
|
436
|
+
"maximum",
|
|
437
|
+
"minLength",
|
|
438
|
+
"maxLength",
|
|
439
|
+
"minItems",
|
|
440
|
+
"maxItems"
|
|
441
|
+
]);
|
|
442
|
+
/** Keywords that describe the array itself rather than each of its items. */
|
|
443
|
+
var ARRAY_LEVEL_KEYWORDS = /* @__PURE__ */ new Set(["minItems", "maxItems"]);
|
|
444
|
+
/**
|
|
445
|
+
* Reads every `define schema` hat in the project and lowers it to a canonical JSON Schema.
|
|
446
|
+
* A hat that cannot be read is reported in `errors` and left out of `schemas`.
|
|
447
|
+
*/
|
|
448
|
+
function scanSchemaDefinitions(targets, opcodes) {
|
|
449
|
+
const drafts = [];
|
|
450
|
+
const errors = [];
|
|
451
|
+
const seen = /* @__PURE__ */ new Map();
|
|
452
|
+
for (const target of targets) {
|
|
453
|
+
if (target.isOriginal === false) continue;
|
|
454
|
+
const targetName = target.getName?.() ?? (target.isStage ? "Stage" : "sprite");
|
|
455
|
+
const blocks = target.blocks._blocks;
|
|
456
|
+
for (const block of Object.values(blocks)) {
|
|
457
|
+
if (block.opcode !== opcodes.hat || block.topLevel === false) continue;
|
|
458
|
+
const where = `${targetName} (block ${block.id})`;
|
|
459
|
+
try {
|
|
460
|
+
const draft = readDraft(block, blocks, opcodes, targetName);
|
|
461
|
+
const key = `${draft.name}@${draft.version}`;
|
|
462
|
+
const previous = seen.get(key);
|
|
463
|
+
if (previous !== void 0) throw new Error(`schema "${key}" is already defined in ${previous}`);
|
|
464
|
+
seen.set(key, where);
|
|
465
|
+
drafts.push(draft);
|
|
466
|
+
} catch (error) {
|
|
467
|
+
errors.push(`${where}: ${messageOf$1(error)}`);
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
const byKey = new Map(drafts.map((draft) => [`${draft.name}@${draft.version}`, draft]));
|
|
472
|
+
const schemas = [];
|
|
473
|
+
for (const draft of drafts) try {
|
|
474
|
+
const schema = lowerDraft(draft, byKey, []);
|
|
475
|
+
validateSchemaDefinition(schema, `schema ${draft.name}`);
|
|
476
|
+
schemas.push({
|
|
477
|
+
name: draft.name,
|
|
478
|
+
version: draft.version,
|
|
479
|
+
schema,
|
|
480
|
+
targetName: draft.targetName,
|
|
481
|
+
blockId: draft.blockId
|
|
482
|
+
});
|
|
483
|
+
} catch (error) {
|
|
484
|
+
errors.push(`${draft.targetName} (block ${draft.blockId}): ${messageOf$1(error)}`);
|
|
485
|
+
}
|
|
486
|
+
schemas.sort((left, right) => compare(`${left.name}@${left.version}`, `${right.name}@${right.version}`));
|
|
487
|
+
return {
|
|
488
|
+
schemas,
|
|
489
|
+
errors
|
|
490
|
+
};
|
|
491
|
+
}
|
|
492
|
+
/** Parses `Name` or `Name@1.2.3`. */
|
|
493
|
+
function parseSchemaReference(text, label) {
|
|
494
|
+
const trimmed = text.trim();
|
|
495
|
+
const at = trimmed.lastIndexOf("@");
|
|
496
|
+
const name = at > 0 ? trimmed.slice(0, at) : trimmed;
|
|
497
|
+
const version = at > 0 ? trimmed.slice(at + 1) : void 0;
|
|
498
|
+
if (!SCHEMA_NAME_PATTERN.test(name)) throw new Error(`${label} must be 1-128 letters, digits, ".", "_" or "-"`);
|
|
499
|
+
if (version !== void 0 && !SCHEMA_VERSION_PATTERN.test(version)) throw new Error(`${label} version "${version}" must be a complete SemVer value such as 1.0.0`);
|
|
500
|
+
return version === void 0 ? { name } : {
|
|
501
|
+
name,
|
|
502
|
+
version
|
|
503
|
+
};
|
|
504
|
+
}
|
|
505
|
+
/**
|
|
506
|
+
* Resolves a `Name` or `Name@1.2.3` reference against scanned schemas. An unknown name or version
|
|
507
|
+
* fails; a name with several versions and no pinned version is ambiguous rather than guessed.
|
|
508
|
+
*/
|
|
509
|
+
function resolveSchemaReference(schemas, text, label) {
|
|
510
|
+
const reference = parseSchemaReference(text, label);
|
|
511
|
+
const matches = schemas.filter((candidate) => candidate.name === reference.name);
|
|
512
|
+
if (matches.length === 0) throw new Error(`${label} refers to unknown schema "${reference.name}"`);
|
|
513
|
+
if (reference.version !== void 0) {
|
|
514
|
+
const exact = matches.find((candidate) => candidate.version === reference.version);
|
|
515
|
+
if (!exact) {
|
|
516
|
+
const available = matches.map((candidate) => candidate.version).join(", ");
|
|
517
|
+
throw new Error(`${label} refers to unknown version "${reference.version}" of schema "${reference.name}" (defined: ${available})`);
|
|
518
|
+
}
|
|
519
|
+
return exact.schema;
|
|
520
|
+
}
|
|
521
|
+
if (matches.length > 1) {
|
|
522
|
+
const available = matches.map((candidate) => candidate.version).join(", ");
|
|
523
|
+
throw new Error(`${label} refers to schema "${reference.name}", which has several versions (${available}); pin one as "${reference.name}@${matches[0]?.version}"`);
|
|
524
|
+
}
|
|
525
|
+
return matches[0].schema;
|
|
526
|
+
}
|
|
527
|
+
function readDraft(hat, blocks, opcodes, targetName) {
|
|
528
|
+
const name = readLiteralInput(hat, blocks, "NAME").trim();
|
|
529
|
+
if (!SCHEMA_NAME_PATTERN.test(name)) throw new Error("schema name must be 1-128 letters, digits, \".\", \"_\" or \"-\"");
|
|
530
|
+
const version = readLiteralInput(hat, blocks, "VERSION").trim();
|
|
531
|
+
if (!SCHEMA_VERSION_PATTERN.test(version)) throw new Error(`schema version "${version}" must be a complete SemVer value such as 1.0.0`);
|
|
532
|
+
const draft = {
|
|
533
|
+
name,
|
|
534
|
+
version,
|
|
535
|
+
properties: [],
|
|
536
|
+
targetName,
|
|
537
|
+
blockId: hat.id
|
|
538
|
+
};
|
|
539
|
+
const declared = /* @__PURE__ */ new Set();
|
|
540
|
+
for (const block of stackBelow(hat, blocks)) if (block.opcode === opcodes.property) draft.properties.push(readProperty(block, blocks, declared));
|
|
541
|
+
else if (block.opcode === opcodes.propertySchema || block.opcode === opcodes.propertyArraySchema) draft.properties.push(readReferenceProperty(block, blocks, declared, block.opcode === opcodes.propertyArraySchema));
|
|
542
|
+
else if (block.opcode === opcodes.constraint) applyConstraint(block, blocks, draft);
|
|
543
|
+
else if (block.opcode === opcodes.allowedValues) applyAllowedValues(block, blocks, draft);
|
|
544
|
+
else if (block.opcode === opcodes.additionalProperties) {
|
|
545
|
+
if (draft.additionalProperties !== void 0) throw new Error("\"allow undeclared properties\" is used more than once in this schema");
|
|
546
|
+
draft.additionalProperties = readMenuField(block, "ALLOW") === "yes";
|
|
547
|
+
} else throw new Error(`${block.opcode} cannot be used inside a "define schema" stack`);
|
|
548
|
+
return draft;
|
|
549
|
+
}
|
|
550
|
+
function readProperty(block, blocks, declared) {
|
|
551
|
+
const name = readPropertyName(block, blocks, declared);
|
|
552
|
+
const typeName = readMenuField(block, "TYPE");
|
|
553
|
+
const build = PROPERTY_TYPES[typeName];
|
|
554
|
+
if (!build) throw new Error(`property "${name}" has unsupported type "${typeName}"`);
|
|
555
|
+
return {
|
|
556
|
+
name,
|
|
557
|
+
required: readPresence(block),
|
|
558
|
+
schema: build()
|
|
559
|
+
};
|
|
560
|
+
}
|
|
561
|
+
function readReferenceProperty(block, blocks, declared, asArray) {
|
|
562
|
+
const name = readPropertyName(block, blocks, declared);
|
|
563
|
+
const reference = parseSchemaReference(readLiteralInput(block, blocks, "SCHEMA"), `property "${name}" schema`);
|
|
564
|
+
return {
|
|
565
|
+
name,
|
|
566
|
+
required: readPresence(block),
|
|
567
|
+
reference,
|
|
568
|
+
referenceIsArray: asArray
|
|
569
|
+
};
|
|
570
|
+
}
|
|
571
|
+
function readPropertyName(block, blocks, declared) {
|
|
572
|
+
const name = readLiteralInput(block, blocks, "NAME").trim();
|
|
573
|
+
if (name.length === 0) throw new Error("a property needs a name");
|
|
574
|
+
if (declared.has(name)) throw new Error(`property "${name}" is declared more than once`);
|
|
575
|
+
declared.add(name);
|
|
576
|
+
return name;
|
|
577
|
+
}
|
|
578
|
+
function readPresence(block) {
|
|
579
|
+
return readMenuField(block, "PRESENCE") === "required";
|
|
580
|
+
}
|
|
581
|
+
function applyConstraint(block, blocks, draft) {
|
|
582
|
+
const keyword = readMenuField(block, "KEYWORD");
|
|
583
|
+
if (!CONSTRAINT_KEYWORDS.has(keyword)) throw new Error(`unsupported limit "${keyword}"`);
|
|
584
|
+
const target = constraintTarget(draft, `limit "${keyword}"`, keyword);
|
|
585
|
+
const raw = readLiteralInput(block, blocks, "VALUE").trim();
|
|
586
|
+
const value = Number(raw);
|
|
587
|
+
if (raw.length === 0 || !Number.isFinite(value)) throw new Error(`limit "${keyword}" needs a number, not "${raw}"`);
|
|
588
|
+
if (target[keyword] !== void 0) throw new Error(`limit "${keyword}" is set more than once on property "${lastProperty(draft).name}"`);
|
|
589
|
+
target[keyword] = value;
|
|
590
|
+
}
|
|
591
|
+
function applyAllowedValues(block, blocks, draft) {
|
|
592
|
+
const target = constraintTarget(draft, "\"allowed values\"", "enum");
|
|
593
|
+
const property = lastProperty(draft);
|
|
594
|
+
if (target.enum !== void 0) throw new Error(`"allowed values" is set more than once on property "${property.name}"`);
|
|
595
|
+
const values = parseAllowedValues(readLiteralInput(block, blocks, "VALUES"), property.name);
|
|
596
|
+
const type = target.type;
|
|
597
|
+
if (type !== void 0) {
|
|
598
|
+
for (const value of values) if (validateAgainstSchema(value, { type }).length > 0) throw new Error(`allowed value ${JSON.stringify(value)} does not match the declared type of property "${property.name}"`);
|
|
599
|
+
}
|
|
600
|
+
target.enum = values;
|
|
601
|
+
}
|
|
602
|
+
/**
|
|
603
|
+
* Where a limit or an allowed-values list belongs. For an array property the item keywords describe
|
|
604
|
+
* each item, while `minItems`/`maxItems` describe the array.
|
|
605
|
+
*/
|
|
606
|
+
function constraintTarget(draft, label, keyword) {
|
|
607
|
+
const property = lastProperty(draft, label);
|
|
608
|
+
if (!property.schema) throw new Error(`${label} cannot be used on property "${property.name}", which uses another schema`);
|
|
609
|
+
if (!(property.schema.type === "array")) {
|
|
610
|
+
if (ARRAY_LEVEL_KEYWORDS.has(keyword)) throw new Error(`${label} can only be used on an array property, not "${property.name}"`);
|
|
611
|
+
return property.schema;
|
|
612
|
+
}
|
|
613
|
+
if (ARRAY_LEVEL_KEYWORDS.has(keyword)) return property.schema;
|
|
614
|
+
const items = property.schema.items;
|
|
615
|
+
if (items === void 0 || typeof items !== "object" || items === null || Array.isArray(items)) throw new Error(`${label} needs property "${property.name}" to declare an item type, such as "array of number"`);
|
|
616
|
+
return items;
|
|
617
|
+
}
|
|
618
|
+
function lastProperty(draft, label = "this block") {
|
|
619
|
+
const property = draft.properties.at(-1);
|
|
620
|
+
if (!property) throw new Error(`${label} must come after a property block`);
|
|
621
|
+
return property;
|
|
622
|
+
}
|
|
623
|
+
/** JSON array text, or a comma-separated list of strings. */
|
|
624
|
+
function parseAllowedValues(text, propertyName) {
|
|
625
|
+
const trimmed = text.trim();
|
|
626
|
+
if (trimmed.length === 0) throw new Error(`"allowed values" for property "${propertyName}" is empty`);
|
|
627
|
+
if (trimmed.startsWith("[")) {
|
|
628
|
+
let parsed;
|
|
629
|
+
try {
|
|
630
|
+
parsed = JSON.parse(trimmed);
|
|
631
|
+
} catch {
|
|
632
|
+
throw new Error(`"allowed values" for property "${propertyName}" must be valid JSON`);
|
|
633
|
+
}
|
|
634
|
+
if (!Array.isArray(parsed) || parsed.length === 0) throw new Error(`"allowed values" for property "${propertyName}" must be a non-empty JSON array`);
|
|
635
|
+
return parsed;
|
|
636
|
+
}
|
|
637
|
+
const values = trimmed.split(",").map((value) => value.trim()).filter((value) => value.length > 0);
|
|
638
|
+
if (values.length === 0) throw new Error(`"allowed values" for property "${propertyName}" is empty`);
|
|
639
|
+
return values;
|
|
640
|
+
}
|
|
641
|
+
function lowerDraft(draft, byKey, stack) {
|
|
642
|
+
const key = `${draft.name}@${draft.version}`;
|
|
643
|
+
if (stack.includes(key)) throw new Error(`schema reference cycle: ${[...stack, key].join(" -> ")}`);
|
|
644
|
+
if (stack.length >= 16) throw new Error(`schema references nest deeper than 16 levels`);
|
|
645
|
+
const nextStack = [...stack, key];
|
|
646
|
+
const properties = {};
|
|
647
|
+
const required = [];
|
|
648
|
+
for (const property of draft.properties) {
|
|
649
|
+
properties[property.name] = property.reference ? lowerReference(property, draft, byKey, nextStack) : property.schema;
|
|
650
|
+
if (property.required) required.push(property.name);
|
|
651
|
+
}
|
|
652
|
+
return {
|
|
653
|
+
type: "object",
|
|
654
|
+
properties,
|
|
655
|
+
...required.length > 0 ? { required } : {},
|
|
656
|
+
...draft.additionalProperties === void 0 ? {} : { additionalProperties: draft.additionalProperties }
|
|
657
|
+
};
|
|
658
|
+
}
|
|
659
|
+
function lowerReference(property, draft, byKey, stack) {
|
|
660
|
+
const reference = property.reference;
|
|
661
|
+
const lowered = lowerDraft(findDraft(byKey, reference, `property "${property.name}" of schema ${draft.name}`), byKey, stack);
|
|
662
|
+
return property.referenceIsArray ? {
|
|
663
|
+
type: "array",
|
|
664
|
+
items: lowered
|
|
665
|
+
} : lowered;
|
|
666
|
+
}
|
|
667
|
+
function findDraft(byKey, reference, label) {
|
|
668
|
+
if (reference.version !== void 0) {
|
|
669
|
+
const exact = byKey.get(`${reference.name}@${reference.version}`);
|
|
670
|
+
if (!exact) throw new Error(`${label} refers to unknown schema "${reference.name}@${reference.version}"`);
|
|
671
|
+
return exact;
|
|
672
|
+
}
|
|
673
|
+
const matches = [...byKey.values()].filter((candidate) => candidate.name === reference.name);
|
|
674
|
+
if (matches.length === 0) throw new Error(`${label} refers to unknown schema "${reference.name}"`);
|
|
675
|
+
if (matches.length > 1) {
|
|
676
|
+
const available = matches.map((candidate) => candidate.version).join(", ");
|
|
677
|
+
throw new Error(`${label} refers to schema "${reference.name}", which has several versions (${available}); pin one as "${reference.name}@${matches[0]?.version}"`);
|
|
678
|
+
}
|
|
679
|
+
return matches[0];
|
|
680
|
+
}
|
|
681
|
+
function compare(left, right) {
|
|
682
|
+
return left < right ? -1 : left > right ? 1 : 0;
|
|
683
|
+
}
|
|
684
|
+
function messageOf$1(error) {
|
|
685
|
+
return error instanceof Error ? error.message : String(error);
|
|
686
|
+
}
|
|
351
687
|
//#endregion
|
|
352
688
|
//#region src/composition.ts
|
|
353
689
|
/**
|
|
@@ -380,12 +716,22 @@ var NamedFunctionsImpl = class {
|
|
|
380
716
|
options.runtime.on("PROJECT_STOP_ALL", () => this.promises.clear());
|
|
381
717
|
}
|
|
382
718
|
scan() {
|
|
383
|
-
return scanFunctionDefinitions(this.options.runtime.targets, this.options.functionHatOpcode);
|
|
719
|
+
if (!this.options.schemaBlockOpcodes) return scanFunctionDefinitions(this.options.runtime.targets, this.options.functionHatOpcode);
|
|
720
|
+
const schemas = this.schemas().schemas;
|
|
721
|
+
return scanFunctionDefinitions(this.options.runtime.targets, this.options.functionHatOpcode, { resolveSchema: (reference, label) => resolveSchemaReference(schemas, reference, label) });
|
|
722
|
+
}
|
|
723
|
+
schemas() {
|
|
724
|
+
const opcodes = this.options.schemaBlockOpcodes;
|
|
725
|
+
if (!opcodes) return {
|
|
726
|
+
schemas: [],
|
|
727
|
+
errors: []
|
|
728
|
+
};
|
|
729
|
+
return scanSchemaDefinitions(this.options.runtime.targets, opcodes);
|
|
384
730
|
}
|
|
385
731
|
tools() {
|
|
386
|
-
const
|
|
387
|
-
if (
|
|
388
|
-
return toFunctionTools(scan.functions);
|
|
732
|
+
const errors = [...this.schemas().errors, ...this.scan().errors];
|
|
733
|
+
if (errors.length > 0) throw new Error(`Invalid function definitions: ${errors.join("; ")}`);
|
|
734
|
+
return toFunctionTools(this.scan().functions);
|
|
389
735
|
}
|
|
390
736
|
isDefined(name) {
|
|
391
737
|
return this.scan().functions.some((definition) => definition.name === name.trim());
|
|
@@ -468,4 +814,4 @@ function messageOf(error) {
|
|
|
468
814
|
return error instanceof Error ? error.message : String(error);
|
|
469
815
|
}
|
|
470
816
|
//#endregion
|
|
471
|
-
export { FUNCTION_NAME_PATTERN, createNamedFunctions, parseJsonOrText, readArgumentPath, scanFunctionDefinitions, toFunctionTools, toScratchValue, validateAgainstSchema, validateSchemaDefinition };
|
|
817
|
+
export { FUNCTION_NAME_PATTERN, MAX_SCHEMA_REFERENCE_DEPTH, SCHEMA_NAME_PATTERN, SCHEMA_VERSION_PATTERN, createNamedFunctions, parseJsonOrText, parseSchemaReference, readArgumentPath, resolveSchemaReference, scanFunctionDefinitions, scanSchemaDefinitions, toFunctionTools, toScratchValue, validateAgainstSchema, validateSchemaDefinition };
|
|
@@ -63,6 +63,20 @@
|
|
|
63
63
|
}
|
|
64
64
|
]
|
|
65
65
|
},
|
|
66
|
+
{
|
|
67
|
+
"opcode": "defineSchema",
|
|
68
|
+
"blockType": "HAT",
|
|
69
|
+
"arguments": [
|
|
70
|
+
{
|
|
71
|
+
"id": "NAME",
|
|
72
|
+
"type": "STRING"
|
|
73
|
+
},
|
|
74
|
+
{
|
|
75
|
+
"id": "VERSION",
|
|
76
|
+
"type": "STRING"
|
|
77
|
+
}
|
|
78
|
+
]
|
|
79
|
+
},
|
|
66
80
|
{
|
|
67
81
|
"opcode": "functionArgument",
|
|
68
82
|
"blockType": "REPORTER",
|
|
@@ -88,6 +102,16 @@
|
|
|
88
102
|
}
|
|
89
103
|
]
|
|
90
104
|
},
|
|
105
|
+
{
|
|
106
|
+
"opcode": "isSchemaDefined",
|
|
107
|
+
"blockType": "BOOLEAN",
|
|
108
|
+
"arguments": [
|
|
109
|
+
{
|
|
110
|
+
"id": "NAME",
|
|
111
|
+
"type": "STRING"
|
|
112
|
+
}
|
|
113
|
+
]
|
|
114
|
+
},
|
|
91
115
|
{
|
|
92
116
|
"opcode": "isSettled",
|
|
93
117
|
"blockType": "BOOLEAN",
|
|
@@ -113,6 +137,110 @@
|
|
|
113
137
|
}
|
|
114
138
|
]
|
|
115
139
|
},
|
|
140
|
+
{
|
|
141
|
+
"opcode": "schemaAdditionalProperties",
|
|
142
|
+
"blockType": "COMMAND",
|
|
143
|
+
"arguments": [
|
|
144
|
+
{
|
|
145
|
+
"id": "ALLOW",
|
|
146
|
+
"type": "STRING",
|
|
147
|
+
"menu": "allowModes"
|
|
148
|
+
}
|
|
149
|
+
]
|
|
150
|
+
},
|
|
151
|
+
{
|
|
152
|
+
"opcode": "schemaAllowedValues",
|
|
153
|
+
"blockType": "COMMAND",
|
|
154
|
+
"arguments": [
|
|
155
|
+
{
|
|
156
|
+
"id": "VALUES",
|
|
157
|
+
"type": "STRING"
|
|
158
|
+
}
|
|
159
|
+
]
|
|
160
|
+
},
|
|
161
|
+
{
|
|
162
|
+
"opcode": "schemaConstraint",
|
|
163
|
+
"blockType": "COMMAND",
|
|
164
|
+
"arguments": [
|
|
165
|
+
{
|
|
166
|
+
"id": "KEYWORD",
|
|
167
|
+
"type": "STRING",
|
|
168
|
+
"menu": "constraintKeywords"
|
|
169
|
+
},
|
|
170
|
+
{
|
|
171
|
+
"id": "VALUE",
|
|
172
|
+
"type": "NUMBER"
|
|
173
|
+
}
|
|
174
|
+
]
|
|
175
|
+
},
|
|
176
|
+
{
|
|
177
|
+
"opcode": "schemaJson",
|
|
178
|
+
"blockType": "REPORTER",
|
|
179
|
+
"arguments": [
|
|
180
|
+
{
|
|
181
|
+
"id": "NAME",
|
|
182
|
+
"type": "STRING"
|
|
183
|
+
}
|
|
184
|
+
]
|
|
185
|
+
},
|
|
186
|
+
{
|
|
187
|
+
"opcode": "schemaProperty",
|
|
188
|
+
"blockType": "COMMAND",
|
|
189
|
+
"arguments": [
|
|
190
|
+
{
|
|
191
|
+
"id": "NAME",
|
|
192
|
+
"type": "STRING"
|
|
193
|
+
},
|
|
194
|
+
{
|
|
195
|
+
"id": "PRESENCE",
|
|
196
|
+
"type": "STRING",
|
|
197
|
+
"menu": "presenceModes"
|
|
198
|
+
},
|
|
199
|
+
{
|
|
200
|
+
"id": "TYPE",
|
|
201
|
+
"type": "STRING",
|
|
202
|
+
"menu": "propertyTypes"
|
|
203
|
+
}
|
|
204
|
+
]
|
|
205
|
+
},
|
|
206
|
+
{
|
|
207
|
+
"opcode": "schemaPropertyArraySchema",
|
|
208
|
+
"blockType": "COMMAND",
|
|
209
|
+
"arguments": [
|
|
210
|
+
{
|
|
211
|
+
"id": "NAME",
|
|
212
|
+
"type": "STRING"
|
|
213
|
+
},
|
|
214
|
+
{
|
|
215
|
+
"id": "PRESENCE",
|
|
216
|
+
"type": "STRING",
|
|
217
|
+
"menu": "presenceModes"
|
|
218
|
+
},
|
|
219
|
+
{
|
|
220
|
+
"id": "SCHEMA",
|
|
221
|
+
"type": "STRING"
|
|
222
|
+
}
|
|
223
|
+
]
|
|
224
|
+
},
|
|
225
|
+
{
|
|
226
|
+
"opcode": "schemaPropertySchema",
|
|
227
|
+
"blockType": "COMMAND",
|
|
228
|
+
"arguments": [
|
|
229
|
+
{
|
|
230
|
+
"id": "NAME",
|
|
231
|
+
"type": "STRING"
|
|
232
|
+
},
|
|
233
|
+
{
|
|
234
|
+
"id": "PRESENCE",
|
|
235
|
+
"type": "STRING",
|
|
236
|
+
"menu": "presenceModes"
|
|
237
|
+
},
|
|
238
|
+
{
|
|
239
|
+
"id": "SCHEMA",
|
|
240
|
+
"type": "STRING"
|
|
241
|
+
}
|
|
242
|
+
]
|
|
243
|
+
},
|
|
116
244
|
{
|
|
117
245
|
"opcode": "startFunction",
|
|
118
246
|
"blockType": "REPORTER",
|
|
@@ -129,9 +257,25 @@
|
|
|
129
257
|
}
|
|
130
258
|
],
|
|
131
259
|
"menus": [
|
|
260
|
+
{
|
|
261
|
+
"id": "allowModes",
|
|
262
|
+
"acceptReporters": false
|
|
263
|
+
},
|
|
264
|
+
{
|
|
265
|
+
"id": "constraintKeywords",
|
|
266
|
+
"acceptReporters": false
|
|
267
|
+
},
|
|
132
268
|
{
|
|
133
269
|
"id": "exportModes",
|
|
134
270
|
"acceptReporters": false
|
|
271
|
+
},
|
|
272
|
+
{
|
|
273
|
+
"id": "presenceModes",
|
|
274
|
+
"acceptReporters": false
|
|
275
|
+
},
|
|
276
|
+
{
|
|
277
|
+
"id": "propertyTypes",
|
|
278
|
+
"acceptReporters": false
|
|
135
279
|
}
|
|
136
280
|
]
|
|
137
281
|
}
|