@kubohiroya/turbowarp-named-functions 0.1.2 → 0.2.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 +148 -6
- package/README.md +190 -15
- 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 +6 -6
- 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
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Readers for the literal parts of a serialized block.
|
|
3
|
+
*
|
|
4
|
+
* Definitions are read before any script runs, so every field they use must be literal text typed
|
|
5
|
+
* into the block. A reporter plugged into such an input is rejected instead of evaluated.
|
|
6
|
+
*/
|
|
7
|
+
import type { SerializedBlock } from './runtime-types.js';
|
|
8
|
+
export declare function readLiteralInput(block: SerializedBlock, blocks: Record<string, SerializedBlock>, inputName: string): string;
|
|
9
|
+
export declare function readOptionalLiteralInput(block: SerializedBlock, blocks: Record<string, SerializedBlock>, inputName: string): string | undefined;
|
|
10
|
+
/** Reads a dropdown that does not accept reporters; the VM stores it as a field on the block. */
|
|
11
|
+
export declare function readMenuField(block: SerializedBlock, fieldName: string): string;
|
|
12
|
+
/**
|
|
13
|
+
* The blocks stacked under `block`, in order. Shadow blocks are skipped, and a `next` chain that
|
|
14
|
+
* loops or leaves the container stops the walk instead of hanging.
|
|
15
|
+
*/
|
|
16
|
+
export declare function stackBelow(block: SerializedBlock, blocks: Record<string, SerializedBlock>): SerializedBlock[];
|
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
import { type FunctionScan, type FunctionTool } from './function-registry.js';
|
|
2
|
+
import { type SchemaBlockOpcodes, type SchemaScan } from './schema-builder.js';
|
|
2
3
|
import type { RuntimeLike, RuntimeThread } from './runtime-types.js';
|
|
3
|
-
export type { FunctionDefinition, FunctionScan, FunctionTool } from './function-registry.js';
|
|
4
|
+
export type { FunctionDefinition, FunctionScan, FunctionScanOptions, FunctionTool } from './function-registry.js';
|
|
4
5
|
export { FUNCTION_NAME_PATTERN, scanFunctionDefinitions, toFunctionTools } from './function-registry.js';
|
|
6
|
+
export type { SchemaBlockOpcodes, SchemaDefinition, SchemaReference, SchemaScan } from './schema-builder.js';
|
|
7
|
+
export { MAX_SCHEMA_REFERENCE_DEPTH, SCHEMA_NAME_PATTERN, SCHEMA_VERSION_PATTERN, parseSchemaReference, resolveSchemaReference, scanSchemaDefinitions } from './schema-builder.js';
|
|
5
8
|
export type { RuntimeLike, RuntimeThread, BlockUtilityLike } from './runtime-types.js';
|
|
6
9
|
export { parseJsonOrText, readArgumentPath, toScratchValue } from './function-dispatcher.js';
|
|
7
10
|
export { validateAgainstSchema, validateSchemaDefinition } from './schema-validator.js';
|
|
@@ -18,6 +21,11 @@ export interface NamedFunctionsOptions {
|
|
|
18
21
|
runtime: RuntimeLike;
|
|
19
22
|
/** Opcode of the consumer extension's `define function` hat, e.g. `myextension_defineFunction`. */
|
|
20
23
|
functionHatOpcode: string;
|
|
24
|
+
/**
|
|
25
|
+
* Opcodes of the consumer extension's visual schema-builder blocks. When omitted, SCHEMA and
|
|
26
|
+
* RETURNS accept JSON text only, exactly as before.
|
|
27
|
+
*/
|
|
28
|
+
schemaBlockOpcodes?: SchemaBlockOpcodes;
|
|
21
29
|
timeoutMs?: number;
|
|
22
30
|
/** Maximum concurrently running `start`ed calls; further starts wait for a slot. Default 8. */
|
|
23
31
|
maxInFlight?: number;
|
|
@@ -28,6 +36,8 @@ export interface NamedFunctionsOptions {
|
|
|
28
36
|
}
|
|
29
37
|
export interface NamedFunctions {
|
|
30
38
|
scan(): FunctionScan;
|
|
39
|
+
/** `define schema` hats lowered to canonical JSON Schema. Empty without `schemaBlockOpcodes`. */
|
|
40
|
+
schemas(): SchemaScan;
|
|
31
41
|
/** Functions exported as tools, in OpenAI function-calling shape. Throws if any definition is invalid. */
|
|
32
42
|
tools(): FunctionTool[];
|
|
33
43
|
isDefined(name: string): boolean;
|
|
@@ -1,6 +1,9 @@
|
|
|
1
|
-
import { type NamedFunctionsOptions } from './composition.js';
|
|
1
|
+
import { type NamedFunctionsOptions, type SchemaBlockOpcodes } from './composition.js';
|
|
2
2
|
import type { BlockUtilityLike } from './runtime-types.js';
|
|
3
3
|
export declare const DEFINE_FUNCTION_OPCODE: string;
|
|
4
|
+
export declare const DEFINE_SCHEMA_OPCODE: string;
|
|
5
|
+
/** Opcodes of the blocks that make up a `define schema` stack. */
|
|
6
|
+
export declare const SCHEMA_BLOCK_OPCODES: SchemaBlockOpcodes;
|
|
4
7
|
export type NamedFunctionsExtensionDependencies = Omit<NamedFunctionsOptions, 'functionHatOpcode'>;
|
|
5
8
|
type Args = Record<string, unknown>;
|
|
6
9
|
type Util = BlockUtilityLike & {
|
|
@@ -15,6 +18,19 @@ export declare class NamedFunctionsExtension implements TurboWarpExtension {
|
|
|
15
18
|
dispose(): void;
|
|
16
19
|
getInfo(): Record<string, unknown>;
|
|
17
20
|
defineFunction(args: Args, util?: Util): boolean;
|
|
21
|
+
/**
|
|
22
|
+
* A `define schema` stack is a declaration, not a script: the hat never activates, and the blocks
|
|
23
|
+
* under it do nothing if a user clicks the stack.
|
|
24
|
+
*/
|
|
25
|
+
defineSchema(): boolean;
|
|
26
|
+
schemaProperty(): void;
|
|
27
|
+
schemaPropertySchema(): void;
|
|
28
|
+
schemaPropertyArraySchema(): void;
|
|
29
|
+
schemaConstraint(): void;
|
|
30
|
+
schemaAllowedValues(): void;
|
|
31
|
+
schemaAdditionalProperties(): void;
|
|
32
|
+
schemaJson(args: Args): string;
|
|
33
|
+
isSchemaDefined(args: Args): boolean;
|
|
18
34
|
functionArgument(args: Args, util?: Util): string | number | boolean;
|
|
19
35
|
functionArgumentsJson(_args: Args, util?: Util): string;
|
|
20
36
|
returnValue(args: Args, util?: Util): void;
|
|
@@ -22,9 +22,16 @@ export interface FunctionScan {
|
|
|
22
22
|
functions: FunctionDefinition[];
|
|
23
23
|
errors: string[];
|
|
24
24
|
}
|
|
25
|
+
export interface FunctionScanOptions {
|
|
26
|
+
/**
|
|
27
|
+
* Resolves a schema name typed into SCHEMA or RETURNS instead of JSON text, such as a
|
|
28
|
+
* `define schema` hat's name. Without it, those inputs must be JSON.
|
|
29
|
+
*/
|
|
30
|
+
resolveSchema?: (reference: string, label: string) => Record<string, unknown>;
|
|
31
|
+
}
|
|
25
32
|
/**
|
|
26
33
|
* Reads every `define function` hat in the project. NAME, DESCRIPTION, and SCHEMA are part of the
|
|
27
34
|
* tool contract sent to the model before any script runs, so they must be literal text.
|
|
28
35
|
*/
|
|
29
|
-
export declare function scanFunctionDefinitions(targets: readonly RuntimeTarget[], hatOpcode: string): FunctionScan;
|
|
36
|
+
export declare function scanFunctionDefinitions(targets: readonly RuntimeTarget[], hatOpcode: string, options?: FunctionScanOptions): FunctionScan;
|
|
30
37
|
export declare function toFunctionTools(functions: readonly FunctionDefinition[]): FunctionTool[];
|
|
@@ -11,6 +11,8 @@ export interface SerializedBlock {
|
|
|
11
11
|
opcode: string;
|
|
12
12
|
topLevel?: boolean;
|
|
13
13
|
shadow?: boolean;
|
|
14
|
+
/** Id of the next block in the same stack, as the VM serializes a script. */
|
|
15
|
+
next?: string | null;
|
|
14
16
|
inputs?: Record<string, SerializedInput>;
|
|
15
17
|
fields?: Record<string, SerializedField>;
|
|
16
18
|
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type { RuntimeTarget } from './runtime-types.js';
|
|
2
|
+
export declare const SCHEMA_NAME_PATTERN: RegExp;
|
|
3
|
+
/** Complete SemVer, as the named-data schema identity contract requires. */
|
|
4
|
+
export declare const SCHEMA_VERSION_PATTERN: RegExp;
|
|
5
|
+
/** How deep a chain of schema references may nest before it is reported as too deep. */
|
|
6
|
+
export declare const MAX_SCHEMA_REFERENCE_DEPTH = 16;
|
|
7
|
+
/** Opcodes of the consumer extension's schema-builder blocks. */
|
|
8
|
+
export interface SchemaBlockOpcodes {
|
|
9
|
+
/** `define schema [NAME] version [VERSION]` */
|
|
10
|
+
hat: string;
|
|
11
|
+
/** `property [NAME] type [TYPE] [PRESENCE]` */
|
|
12
|
+
property: string;
|
|
13
|
+
/** `property [NAME] schema [SCHEMA] [PRESENCE]` */
|
|
14
|
+
propertySchema: string;
|
|
15
|
+
/** `property [NAME] array of schema [SCHEMA] [PRESENCE]` */
|
|
16
|
+
propertyArraySchema: string;
|
|
17
|
+
/** `limit [KEYWORD] to (VALUE)` */
|
|
18
|
+
constraint: string;
|
|
19
|
+
/** `allowed values [VALUES]` */
|
|
20
|
+
allowedValues: string;
|
|
21
|
+
/** `allow undeclared properties [ALLOW]` */
|
|
22
|
+
additionalProperties: string;
|
|
23
|
+
}
|
|
24
|
+
export interface SchemaDefinition {
|
|
25
|
+
name: string;
|
|
26
|
+
version: string;
|
|
27
|
+
/** Canonical JSON Schema with every schema reference already inlined. */
|
|
28
|
+
schema: Record<string, unknown>;
|
|
29
|
+
targetName: string;
|
|
30
|
+
blockId: string;
|
|
31
|
+
}
|
|
32
|
+
export interface SchemaScan {
|
|
33
|
+
schemas: SchemaDefinition[];
|
|
34
|
+
errors: string[];
|
|
35
|
+
}
|
|
36
|
+
/** A `Name` or `Name@1.2.3` reference typed into a block field. */
|
|
37
|
+
export interface SchemaReference {
|
|
38
|
+
name: string;
|
|
39
|
+
version?: string;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Reads every `define schema` hat in the project and lowers it to a canonical JSON Schema.
|
|
43
|
+
* A hat that cannot be read is reported in `errors` and left out of `schemas`.
|
|
44
|
+
*/
|
|
45
|
+
export declare function scanSchemaDefinitions(targets: readonly RuntimeTarget[], opcodes: SchemaBlockOpcodes): SchemaScan;
|
|
46
|
+
/** Parses `Name` or `Name@1.2.3`. */
|
|
47
|
+
export declare function parseSchemaReference(text: string, label: string): SchemaReference;
|
|
48
|
+
/**
|
|
49
|
+
* Resolves a `Name` or `Name@1.2.3` reference against scanned schemas. An unknown name or version
|
|
50
|
+
* fails; a name with several versions and no pinned version is ambiguous rather than guessed.
|
|
51
|
+
*/
|
|
52
|
+
export declare function resolveSchemaReference(schemas: readonly SchemaDefinition[], text: string, label: string): Record<string, unknown>;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kubohiroya/turbowarp-named-functions",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Named, JSON-in/JSON-out functions for TurboWarp that can be called by name, synchronously or asynchronously, and exported as tools.",
|
|
5
5
|
"author": "Hiroya Kubo <hiroya@cuc.ac.jp>",
|
|
6
6
|
"license": "MPL-2.0",
|
|
@@ -56,8 +56,8 @@
|
|
|
56
56
|
},
|
|
57
57
|
"devDependencies": {
|
|
58
58
|
"@eslint/js": "^9.39.5",
|
|
59
|
-
"@kubohiroya/turbowarp-extension-manifest": "0.3.2",
|
|
60
|
-
"@kubohiroya/vite-plugin-turbowarp-extension": "0.
|
|
59
|
+
"@kubohiroya/turbowarp-extension-manifest": "^0.3.2",
|
|
60
|
+
"@kubohiroya/vite-plugin-turbowarp-extension": "^0.8.0",
|
|
61
61
|
"@types/node": "^24.13.3",
|
|
62
62
|
"eslint": "^9.39.5",
|
|
63
63
|
"jiti": "^2.7.0",
|
|
@@ -75,11 +75,11 @@
|
|
|
75
75
|
"./package.json": "./package.json"
|
|
76
76
|
},
|
|
77
77
|
"dependencies": {
|
|
78
|
-
"@kubohiroya/turbowarp-extension-data-editor": "0.
|
|
78
|
+
"@kubohiroya/turbowarp-extension-data-editor": "^0.3.4"
|
|
79
79
|
},
|
|
80
|
-
"gitHead": "
|
|
80
|
+
"gitHead": "37fba5b6cfb1d0a862ab4341df951d4685d0f20e",
|
|
81
81
|
"x-turbowarp-source": {
|
|
82
|
-
"revision": "
|
|
82
|
+
"revision": "37fba5b6cfb1d0a862ab4341df951d4685d0f20e",
|
|
83
83
|
"revisionRole": "base-commit",
|
|
84
84
|
"sourceState": "local-release-overlay"
|
|
85
85
|
}
|
|
@@ -5,8 +5,8 @@
|
|
|
5
5
|
"opcode": "defineFunction",
|
|
6
6
|
"blockType": "HAT",
|
|
7
7
|
"text": "define function [NAME] description [DESCRIPTION] args schema [SCHEMA] returns schema [RETURNS] export as [EXPORT]",
|
|
8
|
-
"description": "Defines a named function. NAME, DESCRIPTION, and SCHEMA must be literal text. RETURNS
|
|
9
|
-
"descriptionJa": "名前を持つ関数を定義します。NAME、DESCRIPTION、SCHEMAには文字列を直接書く必要があります。RETURNS
|
|
8
|
+
"description": "Defines a named function. NAME, DESCRIPTION, and SCHEMA must be literal text. SCHEMA and RETURNS each take JSON Schema text or the name of a `define schema` hat, optionally pinned as Name@1.0.0. RETURNS is optional; leave it blank to disable return validation.",
|
|
9
|
+
"descriptionJa": "名前を持つ関数を定義します。NAME、DESCRIPTION、SCHEMAには文字列を直接書く必要があります。SCHEMAとRETURNSには、JSON Schema文字列、または`define schema`ハットの名前(Name@1.0.0の形でバージョン固定も可)を書けます。RETURNSは任意で、空欄なら戻り値を検証しません。",
|
|
10
10
|
"arguments": {
|
|
11
11
|
"NAME": {
|
|
12
12
|
"type": "STRING",
|
|
@@ -31,6 +31,161 @@
|
|
|
31
31
|
}
|
|
32
32
|
}
|
|
33
33
|
},
|
|
34
|
+
{
|
|
35
|
+
"opcode": "defineSchema",
|
|
36
|
+
"blockType": "HAT",
|
|
37
|
+
"text": "define schema [NAME] version [VERSION]",
|
|
38
|
+
"description": "Declares a named JSON Schema from the property blocks stacked under it. NAME and VERSION must be literal text, and VERSION must be a complete SemVer value. The stack is read as a declaration and never run; the result is the same canonical JSON Schema that schema text produces.",
|
|
39
|
+
"descriptionJa": "下に積まれたpropertyブロックから、名前付きJSON Schemaを宣言します。NAMEとVERSIONには文字列を直接書く必要があり、VERSIONは完全なSemVer値でなければなりません。この積み木は宣言として読まれるだけで実行されず、結果はSchema文字列と同じcanonical JSON Schemaになります。",
|
|
40
|
+
"arguments": {
|
|
41
|
+
"NAME": {
|
|
42
|
+
"type": "STRING",
|
|
43
|
+
"defaultValue": "Player"
|
|
44
|
+
},
|
|
45
|
+
"VERSION": {
|
|
46
|
+
"type": "STRING",
|
|
47
|
+
"defaultValue": "1.0.0"
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
"opcode": "schemaProperty",
|
|
53
|
+
"blockType": "COMMAND",
|
|
54
|
+
"text": "property [NAME] type [TYPE] [PRESENCE]",
|
|
55
|
+
"description": "Inside a `define schema` stack, declares one property of the schema.",
|
|
56
|
+
"descriptionJa": "`define schema`の積み木の中で、Schemaのプロパティを1つ宣言します。",
|
|
57
|
+
"arguments": {
|
|
58
|
+
"NAME": {
|
|
59
|
+
"type": "STRING",
|
|
60
|
+
"defaultValue": "name"
|
|
61
|
+
},
|
|
62
|
+
"TYPE": {
|
|
63
|
+
"type": "STRING",
|
|
64
|
+
"defaultValue": "string",
|
|
65
|
+
"menu": "propertyTypes"
|
|
66
|
+
},
|
|
67
|
+
"PRESENCE": {
|
|
68
|
+
"type": "STRING",
|
|
69
|
+
"defaultValue": "required",
|
|
70
|
+
"menu": "presenceModes"
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
},
|
|
74
|
+
{
|
|
75
|
+
"opcode": "schemaPropertySchema",
|
|
76
|
+
"blockType": "COMMAND",
|
|
77
|
+
"text": "property [NAME] schema [SCHEMA] [PRESENCE]",
|
|
78
|
+
"description": "Declares a property whose value follows another named schema. SCHEMA is a `define schema` name, optionally pinned as Name@1.0.0.",
|
|
79
|
+
"descriptionJa": "別の名前付きSchemaに従う値を持つプロパティを宣言します。SCHEMAには`define schema`の名前を書き、Name@1.0.0の形でバージョンを固定できます。",
|
|
80
|
+
"arguments": {
|
|
81
|
+
"NAME": {
|
|
82
|
+
"type": "STRING",
|
|
83
|
+
"defaultValue": "profile"
|
|
84
|
+
},
|
|
85
|
+
"SCHEMA": {
|
|
86
|
+
"type": "STRING",
|
|
87
|
+
"defaultValue": "Profile"
|
|
88
|
+
},
|
|
89
|
+
"PRESENCE": {
|
|
90
|
+
"type": "STRING",
|
|
91
|
+
"defaultValue": "required",
|
|
92
|
+
"menu": "presenceModes"
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
},
|
|
96
|
+
{
|
|
97
|
+
"opcode": "schemaPropertyArraySchema",
|
|
98
|
+
"blockType": "COMMAND",
|
|
99
|
+
"text": "property [NAME] array of schema [SCHEMA] [PRESENCE]",
|
|
100
|
+
"description": "Declares a property holding an array whose items follow another named schema.",
|
|
101
|
+
"descriptionJa": "項目が別の名前付きSchemaに従う配列を持つプロパティを宣言します。",
|
|
102
|
+
"arguments": {
|
|
103
|
+
"NAME": {
|
|
104
|
+
"type": "STRING",
|
|
105
|
+
"defaultValue": "items"
|
|
106
|
+
},
|
|
107
|
+
"SCHEMA": {
|
|
108
|
+
"type": "STRING",
|
|
109
|
+
"defaultValue": "Item"
|
|
110
|
+
},
|
|
111
|
+
"PRESENCE": {
|
|
112
|
+
"type": "STRING",
|
|
113
|
+
"defaultValue": "optional",
|
|
114
|
+
"menu": "presenceModes"
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
},
|
|
118
|
+
{
|
|
119
|
+
"opcode": "schemaConstraint",
|
|
120
|
+
"blockType": "COMMAND",
|
|
121
|
+
"text": "limit [KEYWORD] to [VALUE]",
|
|
122
|
+
"description": "Adds a limit to the property declared just above. `minItems` and `maxItems` apply to an array property itself; the other limits apply to each of its items.",
|
|
123
|
+
"descriptionJa": "すぐ上で宣言したプロパティに制約を加えます。`minItems`と`maxItems`は配列そのものに、その他の制約は配列の各項目に適用されます。",
|
|
124
|
+
"arguments": {
|
|
125
|
+
"KEYWORD": {
|
|
126
|
+
"type": "STRING",
|
|
127
|
+
"defaultValue": "minimum",
|
|
128
|
+
"menu": "constraintKeywords"
|
|
129
|
+
},
|
|
130
|
+
"VALUE": {
|
|
131
|
+
"type": "NUMBER",
|
|
132
|
+
"defaultValue": "0"
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
},
|
|
136
|
+
{
|
|
137
|
+
"opcode": "schemaAllowedValues",
|
|
138
|
+
"blockType": "COMMAND",
|
|
139
|
+
"text": "allowed values [VALUES]",
|
|
140
|
+
"description": "Restricts the property declared just above to an enum. VALUES is a JSON array, or a comma-separated list of strings.",
|
|
141
|
+
"descriptionJa": "すぐ上で宣言したプロパティの値をenumに制限します。VALUESにはJSON配列、またはカンマ区切りの文字列を書きます。",
|
|
142
|
+
"arguments": {
|
|
143
|
+
"VALUES": {
|
|
144
|
+
"type": "STRING",
|
|
145
|
+
"defaultValue": "red, green, blue"
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
},
|
|
149
|
+
{
|
|
150
|
+
"opcode": "schemaAdditionalProperties",
|
|
151
|
+
"blockType": "COMMAND",
|
|
152
|
+
"text": "allow undeclared properties [ALLOW]",
|
|
153
|
+
"description": "Sets whether the schema accepts properties it does not declare. Left out, undeclared properties are accepted, as in JSON Schema.",
|
|
154
|
+
"descriptionJa": "宣言していないプロパティをSchemaが受け入れるかどうかを設定します。省略した場合は、JSON Schemaと同じく受け入れます。",
|
|
155
|
+
"arguments": {
|
|
156
|
+
"ALLOW": {
|
|
157
|
+
"type": "STRING",
|
|
158
|
+
"defaultValue": "no",
|
|
159
|
+
"menu": "allowModes"
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
},
|
|
163
|
+
{
|
|
164
|
+
"opcode": "schemaJson",
|
|
165
|
+
"blockType": "REPORTER",
|
|
166
|
+
"text": "schema [NAME] JSON",
|
|
167
|
+
"description": "Reports the canonical JSON Schema a `define schema` hat lowers to, or an empty string if it is unknown or invalid.",
|
|
168
|
+
"descriptionJa": "`define schema`ハットが変換されたcanonical JSON Schemaを返します。未定義または不正な場合は空文字列を返します。",
|
|
169
|
+
"arguments": {
|
|
170
|
+
"NAME": {
|
|
171
|
+
"type": "STRING",
|
|
172
|
+
"defaultValue": "Player"
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
},
|
|
176
|
+
{
|
|
177
|
+
"opcode": "isSchemaDefined",
|
|
178
|
+
"blockType": "BOOLEAN",
|
|
179
|
+
"text": "schema [NAME] defined?",
|
|
180
|
+
"description": "True when a valid `define schema` hat has that name. A name may be pinned as Name@1.0.0.",
|
|
181
|
+
"descriptionJa": "その名前の正しい`define schema`ハットがあればtrueを返します。Name@1.0.0の形でバージョンを固定できます。",
|
|
182
|
+
"arguments": {
|
|
183
|
+
"NAME": {
|
|
184
|
+
"type": "STRING",
|
|
185
|
+
"defaultValue": "Player"
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
},
|
|
34
189
|
{
|
|
35
190
|
"opcode": "functionArgument",
|
|
36
191
|
"blockType": "REPORTER",
|
|
@@ -167,6 +322,48 @@
|
|
|
167
322
|
"none",
|
|
168
323
|
"tool"
|
|
169
324
|
]
|
|
325
|
+
},
|
|
326
|
+
"propertyTypes": {
|
|
327
|
+
"acceptReporters": false,
|
|
328
|
+
"items": [
|
|
329
|
+
"string",
|
|
330
|
+
"number",
|
|
331
|
+
"integer",
|
|
332
|
+
"boolean",
|
|
333
|
+
"null",
|
|
334
|
+
"object",
|
|
335
|
+
"array",
|
|
336
|
+
"array of string",
|
|
337
|
+
"array of number",
|
|
338
|
+
"array of integer",
|
|
339
|
+
"array of boolean",
|
|
340
|
+
"array of object"
|
|
341
|
+
]
|
|
342
|
+
},
|
|
343
|
+
"presenceModes": {
|
|
344
|
+
"acceptReporters": false,
|
|
345
|
+
"items": [
|
|
346
|
+
"required",
|
|
347
|
+
"optional"
|
|
348
|
+
]
|
|
349
|
+
},
|
|
350
|
+
"constraintKeywords": {
|
|
351
|
+
"acceptReporters": false,
|
|
352
|
+
"items": [
|
|
353
|
+
"minimum",
|
|
354
|
+
"maximum",
|
|
355
|
+
"minLength",
|
|
356
|
+
"maxLength",
|
|
357
|
+
"minItems",
|
|
358
|
+
"maxItems"
|
|
359
|
+
]
|
|
360
|
+
},
|
|
361
|
+
"allowModes": {
|
|
362
|
+
"acceptReporters": false,
|
|
363
|
+
"items": [
|
|
364
|
+
"no",
|
|
365
|
+
"yes"
|
|
366
|
+
]
|
|
170
367
|
}
|
|
171
368
|
}
|
|
172
369
|
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Readers for the literal parts of a serialized block.
|
|
3
|
+
*
|
|
4
|
+
* Definitions are read before any script runs, so every field they use must be literal text typed
|
|
5
|
+
* into the block. A reporter plugged into such an input is rejected instead of evaluated.
|
|
6
|
+
*/
|
|
7
|
+
import type {SerializedBlock} from './runtime-types.js';
|
|
8
|
+
|
|
9
|
+
export function readLiteralInput(
|
|
10
|
+
block: SerializedBlock,
|
|
11
|
+
blocks: Record<string, SerializedBlock>,
|
|
12
|
+
inputName: string
|
|
13
|
+
): string {
|
|
14
|
+
const input = block.inputs?.[inputName];
|
|
15
|
+
if (!input) throw new Error(`${inputName} is missing`);
|
|
16
|
+
if (input.block && input.block !== input.shadow) {
|
|
17
|
+
throw new Error(`${inputName} must be literal text, not a reporter block`);
|
|
18
|
+
}
|
|
19
|
+
const shadowId = input.shadow ?? input.block;
|
|
20
|
+
const shadow = shadowId ? blocks[shadowId] : undefined;
|
|
21
|
+
const field = shadow?.fields ? Object.values(shadow.fields)[0] : undefined;
|
|
22
|
+
if (!field) throw new Error(`${inputName} is missing`);
|
|
23
|
+
return String(field.value ?? '');
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function readOptionalLiteralInput(
|
|
27
|
+
block: SerializedBlock,
|
|
28
|
+
blocks: Record<string, SerializedBlock>,
|
|
29
|
+
inputName: string
|
|
30
|
+
): string | undefined {
|
|
31
|
+
if (!block.inputs?.[inputName]) return undefined;
|
|
32
|
+
return readLiteralInput(block, blocks, inputName);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Reads a dropdown that does not accept reporters; the VM stores it as a field on the block. */
|
|
36
|
+
export function readMenuField(block: SerializedBlock, fieldName: string): string {
|
|
37
|
+
const field = block.fields?.[fieldName];
|
|
38
|
+
if (!field) throw new Error(`${fieldName} is missing`);
|
|
39
|
+
return String(field.value ?? '');
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* The blocks stacked under `block`, in order. Shadow blocks are skipped, and a `next` chain that
|
|
44
|
+
* loops or leaves the container stops the walk instead of hanging.
|
|
45
|
+
*/
|
|
46
|
+
export function stackBelow(
|
|
47
|
+
block: SerializedBlock,
|
|
48
|
+
blocks: Record<string, SerializedBlock>
|
|
49
|
+
): SerializedBlock[] {
|
|
50
|
+
const stack: SerializedBlock[] = [];
|
|
51
|
+
const seen = new Set<string>([block.id]);
|
|
52
|
+
let nextId = block.next ?? null;
|
|
53
|
+
while (nextId) {
|
|
54
|
+
if (seen.has(nextId)) break;
|
|
55
|
+
seen.add(nextId);
|
|
56
|
+
const next = blocks[nextId];
|
|
57
|
+
if (!next) break;
|
|
58
|
+
if (next.shadow !== true) stack.push(next);
|
|
59
|
+
nextId = next.next ?? null;
|
|
60
|
+
}
|
|
61
|
+
return stack;
|
|
62
|
+
}
|
package/src/composition.ts
CHANGED
|
@@ -12,11 +12,26 @@ import {
|
|
|
12
12
|
type FunctionScan,
|
|
13
13
|
type FunctionTool
|
|
14
14
|
} from './function-registry.js';
|
|
15
|
+
import {
|
|
16
|
+
resolveSchemaReference,
|
|
17
|
+
scanSchemaDefinitions,
|
|
18
|
+
type SchemaBlockOpcodes,
|
|
19
|
+
type SchemaScan
|
|
20
|
+
} from './schema-builder.js';
|
|
15
21
|
import type {RuntimeLike, RuntimeThread} from './runtime-types.js';
|
|
16
22
|
import {validateAgainstSchema} from './schema-validator.js';
|
|
17
23
|
|
|
18
|
-
export type {FunctionDefinition, FunctionScan, FunctionTool} from './function-registry.js';
|
|
24
|
+
export type {FunctionDefinition, FunctionScan, FunctionScanOptions, FunctionTool} from './function-registry.js';
|
|
19
25
|
export {FUNCTION_NAME_PATTERN, scanFunctionDefinitions, toFunctionTools} from './function-registry.js';
|
|
26
|
+
export type {SchemaBlockOpcodes, SchemaDefinition, SchemaReference, SchemaScan} from './schema-builder.js';
|
|
27
|
+
export {
|
|
28
|
+
MAX_SCHEMA_REFERENCE_DEPTH,
|
|
29
|
+
SCHEMA_NAME_PATTERN,
|
|
30
|
+
SCHEMA_VERSION_PATTERN,
|
|
31
|
+
parseSchemaReference,
|
|
32
|
+
resolveSchemaReference,
|
|
33
|
+
scanSchemaDefinitions
|
|
34
|
+
} from './schema-builder.js';
|
|
20
35
|
export type {RuntimeLike, RuntimeThread, BlockUtilityLike} from './runtime-types.js';
|
|
21
36
|
export {parseJsonOrText, readArgumentPath, toScratchValue} from './function-dispatcher.js';
|
|
22
37
|
export {validateAgainstSchema, validateSchemaDefinition} from './schema-validator.js';
|
|
@@ -36,6 +51,11 @@ export interface NamedFunctionsOptions {
|
|
|
36
51
|
runtime: RuntimeLike;
|
|
37
52
|
/** Opcode of the consumer extension's `define function` hat, e.g. `myextension_defineFunction`. */
|
|
38
53
|
functionHatOpcode: string;
|
|
54
|
+
/**
|
|
55
|
+
* Opcodes of the consumer extension's visual schema-builder blocks. When omitted, SCHEMA and
|
|
56
|
+
* RETURNS accept JSON text only, exactly as before.
|
|
57
|
+
*/
|
|
58
|
+
schemaBlockOpcodes?: SchemaBlockOpcodes;
|
|
39
59
|
timeoutMs?: number;
|
|
40
60
|
/** Maximum concurrently running `start`ed calls; further starts wait for a slot. Default 8. */
|
|
41
61
|
maxInFlight?: number;
|
|
@@ -47,6 +67,8 @@ export interface NamedFunctionsOptions {
|
|
|
47
67
|
|
|
48
68
|
export interface NamedFunctions {
|
|
49
69
|
scan(): FunctionScan;
|
|
70
|
+
/** `define schema` hats lowered to canonical JSON Schema. Empty without `schemaBlockOpcodes`. */
|
|
71
|
+
schemas(): SchemaScan;
|
|
50
72
|
/** Functions exported as tools, in OpenAI function-calling shape. Throws if any definition is invalid. */
|
|
51
73
|
tools(): FunctionTool[];
|
|
52
74
|
isDefined(name: string): boolean;
|
|
@@ -102,13 +124,26 @@ class NamedFunctionsImpl implements NamedFunctions {
|
|
|
102
124
|
}
|
|
103
125
|
|
|
104
126
|
public scan(): FunctionScan {
|
|
105
|
-
|
|
127
|
+
const opcodes = this.options.schemaBlockOpcodes;
|
|
128
|
+
if (!opcodes) {
|
|
129
|
+
return scanFunctionDefinitions(this.options.runtime.targets, this.options.functionHatOpcode);
|
|
130
|
+
}
|
|
131
|
+
const schemas = this.schemas().schemas;
|
|
132
|
+
return scanFunctionDefinitions(this.options.runtime.targets, this.options.functionHatOpcode, {
|
|
133
|
+
resolveSchema: (reference, label) => resolveSchemaReference(schemas, reference, label)
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
public schemas(): SchemaScan {
|
|
138
|
+
const opcodes = this.options.schemaBlockOpcodes;
|
|
139
|
+
if (!opcodes) return {schemas: [], errors: []};
|
|
140
|
+
return scanSchemaDefinitions(this.options.runtime.targets, opcodes);
|
|
106
141
|
}
|
|
107
142
|
|
|
108
143
|
public tools(): FunctionTool[] {
|
|
109
|
-
const
|
|
110
|
-
if (
|
|
111
|
-
return toFunctionTools(scan.functions);
|
|
144
|
+
const errors = [...this.schemas().errors, ...this.scan().errors];
|
|
145
|
+
if (errors.length > 0) throw new Error(`Invalid function definitions: ${errors.join('; ')}`);
|
|
146
|
+
return toFunctionTools(this.scan().functions);
|
|
112
147
|
}
|
|
113
148
|
|
|
114
149
|
public isDefined(name: string): boolean {
|
package/src/extension.ts
CHANGED
|
@@ -5,9 +5,11 @@ import {
|
|
|
5
5
|
createNamedFunctions,
|
|
6
6
|
parseJsonOrText,
|
|
7
7
|
readArgumentPath,
|
|
8
|
+
resolveSchemaReference,
|
|
8
9
|
toScratchValue,
|
|
9
10
|
type NamedFunctions,
|
|
10
|
-
type NamedFunctionsOptions
|
|
11
|
+
type NamedFunctionsOptions,
|
|
12
|
+
type SchemaBlockOpcodes
|
|
11
13
|
} from './composition.js';
|
|
12
14
|
import type {BlockUtilityLike} from './runtime-types.js';
|
|
13
15
|
|
|
@@ -36,6 +38,18 @@ const blockDefinitions = definitions.blocks as readonly BlockDefinition[];
|
|
|
36
38
|
const menuDefinitions = definitions.menus as Record<string, MenuDefinition>;
|
|
37
39
|
|
|
38
40
|
export const DEFINE_FUNCTION_OPCODE = `${extensionConfig.id}_defineFunction`;
|
|
41
|
+
export const DEFINE_SCHEMA_OPCODE = `${extensionConfig.id}_defineSchema`;
|
|
42
|
+
|
|
43
|
+
/** Opcodes of the blocks that make up a `define schema` stack. */
|
|
44
|
+
export const SCHEMA_BLOCK_OPCODES: SchemaBlockOpcodes = {
|
|
45
|
+
hat: DEFINE_SCHEMA_OPCODE,
|
|
46
|
+
property: `${extensionConfig.id}_schemaProperty`,
|
|
47
|
+
propertySchema: `${extensionConfig.id}_schemaPropertySchema`,
|
|
48
|
+
propertyArraySchema: `${extensionConfig.id}_schemaPropertyArraySchema`,
|
|
49
|
+
constraint: `${extensionConfig.id}_schemaConstraint`,
|
|
50
|
+
allowedValues: `${extensionConfig.id}_schemaAllowedValues`,
|
|
51
|
+
additionalProperties: `${extensionConfig.id}_schemaAdditionalProperties`
|
|
52
|
+
};
|
|
39
53
|
|
|
40
54
|
export type NamedFunctionsExtensionDependencies = Omit<NamedFunctionsOptions, 'functionHatOpcode'>;
|
|
41
55
|
|
|
@@ -53,7 +67,11 @@ export class NamedFunctionsExtension implements TurboWarpExtension {
|
|
|
53
67
|
typeof Scratch === 'undefined' ? {} : Scratch, fixedInputEditorDefinitions, deps.runtime
|
|
54
68
|
);
|
|
55
69
|
try {
|
|
56
|
-
this.functions = createNamedFunctions({
|
|
70
|
+
this.functions = createNamedFunctions({
|
|
71
|
+
...deps,
|
|
72
|
+
functionHatOpcode: DEFINE_FUNCTION_OPCODE,
|
|
73
|
+
schemaBlockOpcodes: SCHEMA_BLOCK_OPCODES
|
|
74
|
+
});
|
|
57
75
|
} catch (error) {
|
|
58
76
|
this.unregisterFixedInputEditors();
|
|
59
77
|
throw error;
|
|
@@ -86,6 +104,50 @@ export class NamedFunctionsExtension implements TurboWarpExtension {
|
|
|
86
104
|
return this.functions.matchHat(Scratch.Cast.toString(args.NAME), util?.thread);
|
|
87
105
|
}
|
|
88
106
|
|
|
107
|
+
/**
|
|
108
|
+
* A `define schema` stack is a declaration, not a script: the hat never activates, and the blocks
|
|
109
|
+
* under it do nothing if a user clicks the stack.
|
|
110
|
+
*/
|
|
111
|
+
public defineSchema(): boolean {
|
|
112
|
+
return false;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
public schemaProperty(): void {}
|
|
116
|
+
|
|
117
|
+
public schemaPropertySchema(): void {}
|
|
118
|
+
|
|
119
|
+
public schemaPropertyArraySchema(): void {}
|
|
120
|
+
|
|
121
|
+
public schemaConstraint(): void {}
|
|
122
|
+
|
|
123
|
+
public schemaAllowedValues(): void {}
|
|
124
|
+
|
|
125
|
+
public schemaAdditionalProperties(): void {}
|
|
126
|
+
|
|
127
|
+
public schemaJson(args: Args): string {
|
|
128
|
+
return this.guard('', () => {
|
|
129
|
+
const scan = this.functions.schemas();
|
|
130
|
+
try {
|
|
131
|
+
return JSON.stringify(
|
|
132
|
+
resolveSchemaReference(scan.schemas, Scratch.Cast.toString(args.NAME), 'schema name')
|
|
133
|
+
);
|
|
134
|
+
} catch (error) {
|
|
135
|
+
// A schema that failed to load is simply absent, so name the definition errors as well.
|
|
136
|
+
if (scan.errors.length === 0) throw error;
|
|
137
|
+
throw new Error(`${messageOf(error)} (invalid schema definitions: ${scan.errors.join('; ')})`);
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
public isSchemaDefined(args: Args): boolean {
|
|
143
|
+
try {
|
|
144
|
+
resolveSchemaReference(this.functions.schemas().schemas, Scratch.Cast.toString(args.NAME), 'schema name');
|
|
145
|
+
return true;
|
|
146
|
+
} catch {
|
|
147
|
+
return false;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
89
151
|
public functionArgument(args: Args, util?: Util): string | number | boolean {
|
|
90
152
|
return this.guard('', () =>
|
|
91
153
|
toScratchValue(readArgumentPath(this.functions.argumentsFor(util?.thread), Scratch.Cast.toString(args.PATH)))
|