@toolproof-core/schema 1.0.1 → 1.0.2

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.
@@ -1,123 +1,123 @@
1
- import fs from 'fs';
2
- import path from 'path';
3
- import { getConfig } from './_lib/config.js';
4
-
5
- /**
6
- * Rewrite anchor-style references to JSON Pointer references in Genesis.json
7
- *
8
- * Converts #AnchorName to #/$defs/AnchorName for compatibility with strict
9
- * JSON Schema validators and downstream tooling. This normalization step runs
10
- * before both schema extraction and resource envelope generation.
11
- *
12
- * The rewritten file is saved under src/genesis/generated/resourceTypes/Genesis.json
13
- * for use by other scripts.
14
- *
15
- * Usage: node ./dist/scripts/normalizeAnchorsToPointers.js
16
- */
17
-
18
- type JSONValue = null | boolean | number | string | JSONValue[] | { [k: string]: JSONValue };
19
-
20
- /**
21
- * Pure function to rewrite anchor-style references to JSON Pointer references.
22
- * Converts #AnchorName to #/$defs/AnchorName for compatibility with strict JSON Schema validators.
23
- *
24
- * This function works on a schema object that has $defs at its top level.
25
- * @param input The schema object to process
26
- * @returns A new schema object with rewritten references
27
- */
28
- function normalizeAnchorsToPointersToPointers(input: any): any {
29
- if (!input || typeof input !== "object") return input;
30
-
31
- // Deep clone the input to ensure the function is pure (no side effects on input)
32
- const root = JSON.parse(JSON.stringify(input));
33
-
34
- // Build a map of anchors to their definition names
35
- const defs: Record<string, any> = root.$defs && typeof root.$defs === "object" ? root.$defs : {};
36
- const anchorToDef: Record<string, string> = {};
37
-
38
- // For Genesis structure: each def is a Type envelope with nucleusSchema.$anchor
39
- for (const [defName, defValue] of Object.entries(defs)) {
40
- if (!defValue || typeof defValue !== "object") continue;
41
-
42
- // Check if this is a Type envelope (has nucleusSchema property)
43
- const nucleusSchema = (defValue as any).nucleusSchema;
44
- if (nucleusSchema && typeof nucleusSchema === "object") {
45
- // Look for $anchor inside the nucleusSchema
46
- const anchor = nucleusSchema.$anchor;
47
- if (typeof anchor === "string" && !anchorToDef[anchor]) {
48
- anchorToDef[anchor] = defName;
49
- }
50
- } else {
51
- // Fallback: check for $anchor at the def level (non-envelope case)
52
- const anchor = (defValue as any).$anchor;
53
- if (typeof anchor === "string" && !anchorToDef[anchor]) {
54
- anchorToDef[anchor] = defName;
55
- }
56
- }
57
- }
58
-
59
- // Walk the entire tree and rewrite anchor refs to pointer refs
60
- function walk(node: any): void {
61
- if (Array.isArray(node)) {
62
- for (const item of node) walk(item);
63
- return;
64
- }
65
- if (!node || typeof node !== "object") return;
66
-
67
- // Rewrite $ref if it's an anchor-style reference
68
- if (typeof node.$ref === "string") {
69
- const ref: string = node.$ref;
70
- // Match anchor refs: starts with # but not #/ (JSON Pointer syntax)
71
- if (ref.startsWith("#") && !ref.startsWith("#/")) {
72
- const anchor = ref.slice(1);
73
- const defName = anchorToDef[anchor];
74
- if (defName) {
75
- node.$ref = `#/$defs/${defName}`;
76
- }
77
- }
78
- }
79
-
80
- // Recursively walk all properties
81
- for (const val of Object.values(node)) {
82
- walk(val);
83
- }
84
- }
85
-
86
- walk(root);
87
- return root;
88
- }
89
-
90
- async function main() {
91
- const config = getConfig();
92
- const genesisSourcePath = config.getSourcePath();
93
-
94
- // Create a generated/normalized version (anchor refs rewritten to pointers)
95
- const normalizedPath = config.getNormalizedSourcePath();
96
-
97
- if (!fs.existsSync(genesisSourcePath)) {
98
- console.error(`Genesis source file not found at ${genesisSourcePath}`);
99
- process.exit(1);
100
- }
101
-
102
- const raw = fs.readFileSync(genesisSourcePath, 'utf-8');
103
- const genesis = JSON.parse(raw);
104
-
105
- // Validate structure
106
- if (!genesis.nucleusSchema || !genesis.nucleusSchema.$defs) {
107
- console.error('Genesis.json must have nucleusSchema.$defs');
108
- process.exit(1);
109
- }
110
-
111
- // Rewrite anchors in the nucleusSchema (pure function call)
112
- genesis.nucleusSchema = normalizeAnchorsToPointersToPointers(genesis.nucleusSchema);
113
-
114
- // Write normalized version
115
- fs.mkdirSync(path.dirname(normalizedPath), { recursive: true });
116
- fs.writeFileSync(normalizedPath, JSON.stringify(genesis, null, 4), 'utf-8');
117
- console.log(`Wrote normalized Genesis with pointer refs to ${normalizedPath}`);
118
- }
119
-
120
- main().catch((e) => {
121
- console.error(e);
122
- process.exit(1);
123
- });
1
+ import fs from 'fs';
2
+ import path from 'path';
3
+ import { getConfig } from './_lib/config.js';
4
+
5
+ /**
6
+ * Rewrite anchor-style references to JSON Pointer references in Genesis.json
7
+ *
8
+ * Converts #AnchorName to #/$defs/AnchorName for compatibility with strict
9
+ * JSON Schema validators and downstream tooling. This normalization step runs
10
+ * before both schema extraction and resource envelope generation.
11
+ *
12
+ * The rewritten file is saved under src/genesis/generated/resourceTypes/Genesis.json
13
+ * for use by other scripts.
14
+ *
15
+ * Usage: node ./dist/scripts/normalizeAnchorsToPointers.js
16
+ */
17
+
18
+ type JSONValue = null | boolean | number | string | JSONValue[] | { [k: string]: JSONValue };
19
+
20
+ /**
21
+ * Pure function to rewrite anchor-style references to JSON Pointer references.
22
+ * Converts #AnchorName to #/$defs/AnchorName for compatibility with strict JSON Schema validators.
23
+ *
24
+ * This function works on a schema object that has $defs at its top level.
25
+ * @param input The schema object to process
26
+ * @returns A new schema object with rewritten references
27
+ */
28
+ function normalizeAnchorsToPointersToPointers(input: any): any {
29
+ if (!input || typeof input !== "object") return input;
30
+
31
+ // Deep clone the input to ensure the function is pure (no side effects on input)
32
+ const root = JSON.parse(JSON.stringify(input));
33
+
34
+ // Build a map of anchors to their definition names
35
+ const defs: Record<string, any> = root.$defs && typeof root.$defs === "object" ? root.$defs : {};
36
+ const anchorToDef: Record<string, string> = {};
37
+
38
+ // For Genesis structure: each def is a Type envelope with nucleusSchema.$anchor
39
+ for (const [defName, defValue] of Object.entries(defs)) {
40
+ if (!defValue || typeof defValue !== "object") continue;
41
+
42
+ // Check if this is a Type envelope (has nucleusSchema property)
43
+ const nucleusSchema = (defValue as any).nucleusSchema;
44
+ if (nucleusSchema && typeof nucleusSchema === "object") {
45
+ // Look for $anchor inside the nucleusSchema
46
+ const anchor = nucleusSchema.$anchor;
47
+ if (typeof anchor === "string" && !anchorToDef[anchor]) {
48
+ anchorToDef[anchor] = defName;
49
+ }
50
+ } else {
51
+ // Fallback: check for $anchor at the def level (non-envelope case)
52
+ const anchor = (defValue as any).$anchor;
53
+ if (typeof anchor === "string" && !anchorToDef[anchor]) {
54
+ anchorToDef[anchor] = defName;
55
+ }
56
+ }
57
+ }
58
+
59
+ // Walk the entire tree and rewrite anchor refs to pointer refs
60
+ function walk(node: any): void {
61
+ if (Array.isArray(node)) {
62
+ for (const item of node) walk(item);
63
+ return;
64
+ }
65
+ if (!node || typeof node !== "object") return;
66
+
67
+ // Rewrite $ref if it's an anchor-style reference
68
+ if (typeof node.$ref === "string") {
69
+ const ref: string = node.$ref;
70
+ // Match anchor refs: starts with # but not #/ (JSON Pointer syntax)
71
+ if (ref.startsWith("#") && !ref.startsWith("#/")) {
72
+ const anchor = ref.slice(1);
73
+ const defName = anchorToDef[anchor];
74
+ if (defName) {
75
+ node.$ref = `#/$defs/${defName}`;
76
+ }
77
+ }
78
+ }
79
+
80
+ // Recursively walk all properties
81
+ for (const val of Object.values(node)) {
82
+ walk(val);
83
+ }
84
+ }
85
+
86
+ walk(root);
87
+ return root;
88
+ }
89
+
90
+ async function main() {
91
+ const config = getConfig();
92
+ const genesisSourcePath = config.getSourcePath();
93
+
94
+ // Create a generated/normalized version (anchor refs rewritten to pointers)
95
+ const normalizedPath = config.getNormalizedSourcePath();
96
+
97
+ if (!fs.existsSync(genesisSourcePath)) {
98
+ console.error(`Genesis source file not found at ${genesisSourcePath}`);
99
+ process.exit(1);
100
+ }
101
+
102
+ const raw = fs.readFileSync(genesisSourcePath, 'utf-8');
103
+ const genesis = JSON.parse(raw);
104
+
105
+ // Validate structure
106
+ if (!genesis.nucleusSchema || !genesis.nucleusSchema.$defs) {
107
+ console.error('Genesis.json must have nucleusSchema.$defs');
108
+ process.exit(1);
109
+ }
110
+
111
+ // Rewrite anchors in the nucleusSchema (pure function call)
112
+ genesis.nucleusSchema = normalizeAnchorsToPointersToPointers(genesis.nucleusSchema);
113
+
114
+ // Write normalized version
115
+ fs.mkdirSync(path.dirname(normalizedPath), { recursive: true });
116
+ fs.writeFileSync(normalizedPath, JSON.stringify(genesis, null, 4), 'utf-8');
117
+ console.log(`Wrote normalized Genesis with pointer refs to ${normalizedPath}`);
118
+ }
119
+
120
+ main().catch((e) => {
121
+ console.error(e);
122
+ process.exit(1);
123
+ });
@@ -1,84 +1,84 @@
1
- import fs from 'fs';
2
- import path from 'path';
3
- import { getConfig } from './_lib/config.js';
4
-
5
- /**
6
- * Wrap each ResourceType definition (from Genesis.nucleusSchema.$defs) with a Resource shell
7
- * that conforms to the Resource.nucleusSchema pattern defined in Genesis.json.
8
- */
9
-
10
- function generateResourceShellLogic(genesis: any): Record<string, any> {
11
- if (!genesis.nucleusSchema || !genesis.nucleusSchema.$defs) {
12
- throw new Error('Genesis.json must have nucleusSchema.$defs');
13
- }
14
-
15
- const defs = genesis.nucleusSchema.$defs;
16
- const defKeys = Object.keys(defs);
17
-
18
- const resources: Record<string, any> = {};
19
-
20
- // Genesis timestamp: 2025-11-30T00:00:00.000Z marks the genesis of ToolProof
21
- const genesisTimestamp = '2025-11-30T00:00:00.000Z';
22
-
23
- resources['Genesis'] = {
24
- identity: 'RESOURCE-Genesis',
25
- resourceTypeHandle: 'TYPE-ResourceType',
26
- creationContext: {
27
- resourceRoleHandle: 'ROLE-Genesis',
28
- jobStepHandle: 'JOB_STEP-Genesis'
29
- },
30
- kind: 'materialized',
31
- timestamp: genesisTimestamp,
32
- nucleus: {}
33
- };
34
-
35
- defKeys.forEach((defName) => {
36
- const defValue = defs[defName];
37
-
38
- resources[defName] = {
39
- identity: `RESOURCE-${defName}`,
40
- resourceTypeHandle: 'TYPE-ResourceType',
41
- creationContext: {
42
- resourceRoleHandle: 'ROLE-Genesis',
43
- jobStepHandle: `JOB_STEP-${defName}`
44
- },
45
- kind: 'materialized',
46
- timestamp: genesisTimestamp,
47
- nucleus: defValue
48
- };
49
- });
50
-
51
- return resources;
52
- }
53
-
54
- async function main() {
55
- const config = getConfig();
56
- const genesisSourcePath = config.getNormalizedSourcePath();
57
- const outputPath = path.join(config.getResourcesDir(), 'Genesis.json');
58
-
59
- if (!fs.existsSync(genesisSourcePath)) {
60
- console.error(`Genesis source file not found at ${genesisSourcePath}`);
61
- process.exit(1);
62
- }
63
-
64
- const raw = fs.readFileSync(genesisSourcePath, 'utf-8');
65
- const genesis = JSON.parse(raw);
66
-
67
- try {
68
- const resources = generateResourceShellLogic(genesis);
69
-
70
- const outputDir = path.dirname(outputPath);
71
- fs.mkdirSync(outputDir, { recursive: true });
72
-
73
- fs.writeFileSync(outputPath, JSON.stringify(resources, null, 4) + '\n', 'utf-8');
74
- console.log(`Generated ${Object.keys(resources).length} Resource envelopes -> ${outputPath}`);
75
- } catch (error: any) {
76
- console.error(error.message);
77
- process.exit(1);
78
- }
79
- }
80
-
81
- main().catch((e) => {
82
- console.error(e);
83
- process.exit(1);
84
- });
1
+ import fs from 'fs';
2
+ import path from 'path';
3
+ import { getConfig } from './_lib/config.js';
4
+
5
+ /**
6
+ * Wrap each ResourceType definition (from Genesis.nucleusSchema.$defs) with a Resource shell
7
+ * that conforms to the Resource.nucleusSchema pattern defined in Genesis.json.
8
+ */
9
+
10
+ function generateResourceShellLogic(genesis: any): Record<string, any> {
11
+ if (!genesis.nucleusSchema || !genesis.nucleusSchema.$defs) {
12
+ throw new Error('Genesis.json must have nucleusSchema.$defs');
13
+ }
14
+
15
+ const defs = genesis.nucleusSchema.$defs;
16
+ const defKeys = Object.keys(defs);
17
+
18
+ const resources: Record<string, any> = {};
19
+
20
+ // Genesis timestamp: 2025-11-30T00:00:00.000Z marks the genesis of ToolProof
21
+ const genesisTimestamp = '2025-11-30T00:00:00.000Z';
22
+
23
+ resources['Genesis'] = {
24
+ identity: 'RESOURCE-Genesis',
25
+ resourceTypeHandle: 'TYPE-ResourceType',
26
+ creationContext: {
27
+ resourceRoleHandle: 'ROLE-Genesis',
28
+ jobStepHandle: 'JOB_STEP-Genesis'
29
+ },
30
+ kind: 'materialized',
31
+ timestamp: genesisTimestamp,
32
+ nucleus: {}
33
+ };
34
+
35
+ defKeys.forEach((defName) => {
36
+ const defValue = defs[defName];
37
+
38
+ resources[defName] = {
39
+ identity: `RESOURCE-${defName}`,
40
+ resourceTypeHandle: 'TYPE-ResourceType',
41
+ creationContext: {
42
+ resourceRoleHandle: 'ROLE-Genesis',
43
+ jobStepHandle: `JOB_STEP-${defName}`
44
+ },
45
+ kind: 'materialized',
46
+ timestamp: genesisTimestamp,
47
+ nucleus: defValue
48
+ };
49
+ });
50
+
51
+ return resources;
52
+ }
53
+
54
+ async function main() {
55
+ const config = getConfig();
56
+ const genesisSourcePath = config.getNormalizedSourcePath();
57
+ const outputPath = path.join(config.getResourcesDir(), 'Genesis.json');
58
+
59
+ if (!fs.existsSync(genesisSourcePath)) {
60
+ console.error(`Genesis source file not found at ${genesisSourcePath}`);
61
+ process.exit(1);
62
+ }
63
+
64
+ const raw = fs.readFileSync(genesisSourcePath, 'utf-8');
65
+ const genesis = JSON.parse(raw);
66
+
67
+ try {
68
+ const resources = generateResourceShellLogic(genesis);
69
+
70
+ const outputDir = path.dirname(outputPath);
71
+ fs.mkdirSync(outputDir, { recursive: true });
72
+
73
+ fs.writeFileSync(outputPath, JSON.stringify(resources, null, 4) + '\n', 'utf-8');
74
+ console.log(`Generated ${Object.keys(resources).length} Resource envelopes -> ${outputPath}`);
75
+ } catch (error: any) {
76
+ console.error(error.message);
77
+ process.exit(1);
78
+ }
79
+ }
80
+
81
+ main().catch((e) => {
82
+ console.error(e);
83
+ process.exit(1);
84
+ });