@toolproof-core/schema 1.0.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.
Files changed (74) hide show
  1. package/dist/generated/types/Resource_Genesis.d.ts +3 -0
  2. package/dist/generated/types/Resource_Genesis.js +1 -0
  3. package/dist/generated/types/Resource_Job.d.ts +3 -0
  4. package/dist/generated/types/Resource_Job.js +1 -0
  5. package/dist/generated/types/Resource_RawStrategy.d.ts +3 -0
  6. package/dist/generated/types/Resource_RawStrategy.js +1 -0
  7. package/dist/generated/types/Resource_ResourceType.d.ts +3 -0
  8. package/dist/generated/types/Resource_ResourceType.js +1 -0
  9. package/dist/generated/types/Resource_RunnableStrategy.d.ts +3 -0
  10. package/dist/generated/types/Resource_RunnableStrategy.js +1 -0
  11. package/dist/generated/types/types.d.ts +1784 -0
  12. package/dist/generated/types/types.js +1 -0
  13. package/dist/scripts/_lib/config.d.ts +53 -0
  14. package/dist/scripts/_lib/config.js +138 -0
  15. package/dist/scripts/extractSchemas.d.ts +1 -0
  16. package/dist/scripts/extractSchemas.js +210 -0
  17. package/dist/scripts/extractSubSchemaWithDefs.d.ts +1 -0
  18. package/dist/scripts/extractSubSchemaWithDefs.js +187 -0
  19. package/dist/scripts/generateDependencies.d.ts +1 -0
  20. package/dist/scripts/generateDependencies.js +106 -0
  21. package/dist/scripts/generateResourceShells.d.ts +1 -0
  22. package/dist/scripts/generateResourceShells.js +91 -0
  23. package/dist/scripts/generateResourceTypeType.d.ts +1 -0
  24. package/dist/scripts/generateResourceTypeType.js +93 -0
  25. package/dist/scripts/generateSchemaShims.d.ts +1 -0
  26. package/dist/scripts/generateSchemaShims.js +105 -0
  27. package/dist/scripts/generateTypes.d.ts +1 -0
  28. package/dist/scripts/generateTypes.js +550 -0
  29. package/dist/scripts/rewriteAnchors.d.ts +1 -0
  30. package/dist/scripts/rewriteAnchors.js +96 -0
  31. package/package.json +45 -0
  32. package/src/Genesis.json +2043 -0
  33. package/src/Roadmap.json +102 -0
  34. package/src/generated/dependencies.json +299 -0
  35. package/src/generated/resourceTypes/Genesis.json +2043 -0
  36. package/src/generated/resourceTypes/Genesis.ts +2 -0
  37. package/src/generated/resources/Genesis.json +2962 -0
  38. package/src/generated/resources/Genesis.ts +2 -0
  39. package/src/generated/schemas/Genesis.json +1489 -0
  40. package/src/generated/schemas/Genesis.ts +2 -0
  41. package/src/generated/schemas/Goal.json +86 -0
  42. package/src/generated/schemas/Goal.ts +2 -0
  43. package/src/generated/schemas/Job.json +236 -0
  44. package/src/generated/schemas/Job.ts +2 -0
  45. package/src/generated/schemas/RawStrategy.json +667 -0
  46. package/src/generated/schemas/RawStrategy.ts +2 -0
  47. package/src/generated/schemas/ResourceType.json +140 -0
  48. package/src/generated/schemas/ResourceType.ts +2 -0
  49. package/src/generated/schemas/RunnableStrategy.json +737 -0
  50. package/src/generated/schemas/RunnableStrategy.ts +2 -0
  51. package/src/generated/schemas/StrategyRun.json +1025 -0
  52. package/src/generated/schemas/StrategyRun.ts +2 -0
  53. package/src/generated/types/Resource_Genesis.d.ts +3 -0
  54. package/src/generated/types/Resource_Genesis.js +1 -0
  55. package/src/generated/types/Resource_Job.d.ts +3 -0
  56. package/src/generated/types/Resource_Job.js +1 -0
  57. package/src/generated/types/Resource_RawStrategy.d.ts +3 -0
  58. package/src/generated/types/Resource_RawStrategy.js +1 -0
  59. package/src/generated/types/Resource_ResourceType.d.ts +3 -0
  60. package/src/generated/types/Resource_ResourceType.js +1 -0
  61. package/src/generated/types/Resource_RunnableStrategy.d.ts +3 -0
  62. package/src/generated/types/Resource_RunnableStrategy.js +1 -0
  63. package/src/generated/types/types.d.ts +1784 -0
  64. package/src/generated/types/types.js +1 -0
  65. package/src/index.ts +1 -0
  66. package/src/scripts/_lib/config.ts +181 -0
  67. package/src/scripts/extractSchemas.ts +229 -0
  68. package/src/scripts/extractSubSchemaWithDefs.ts +196 -0
  69. package/src/scripts/generateDependencies.ts +120 -0
  70. package/src/scripts/generateResourceShells.ts +105 -0
  71. package/src/scripts/generateResourceTypeType.ts +110 -0
  72. package/src/scripts/generateSchemaShims.ts +115 -0
  73. package/src/scripts/generateTypes.ts +615 -0
  74. package/src/scripts/rewriteAnchors.ts +123 -0
@@ -0,0 +1,106 @@
1
+ import fs from "fs";
2
+ import path from "path";
3
+ import { getConfig } from "./_lib/config.js";
4
+ function decodeJsonPointerSegment(segment) {
5
+ // JSON Pointer decoding: ~1 => / and ~0 => ~
6
+ return segment.replace(/~1/g, "/").replace(/~0/g, "~");
7
+ }
8
+ function collectRefs(node, out) {
9
+ if (Array.isArray(node)) {
10
+ for (const item of node)
11
+ collectRefs(item, out);
12
+ return;
13
+ }
14
+ if (!node || typeof node !== "object")
15
+ return;
16
+ const obj = node;
17
+ const ref = obj["$ref"];
18
+ if (typeof ref === "string")
19
+ out.add(ref);
20
+ for (const value of Object.values(obj)) {
21
+ collectRefs(value, out);
22
+ }
23
+ }
24
+ function resolveInternalRefToDefKey(ref, defKeys, anchorToDef) {
25
+ if (!ref.startsWith("#"))
26
+ return null;
27
+ // JSON Pointer: #/$defs/<Name>(/...)
28
+ const defsPrefix = "#/$defs/";
29
+ if (ref.startsWith(defsPrefix)) {
30
+ const rest = ref.slice(defsPrefix.length);
31
+ const firstSegment = rest.split("/")[0] ?? "";
32
+ const defKey = decodeJsonPointerSegment(firstSegment);
33
+ return defKeys.has(defKey) ? defKey : null;
34
+ }
35
+ // Anchor ref: #AnchorName
36
+ if (!ref.startsWith("#/")) {
37
+ const anchor = ref.slice(1);
38
+ const mapped = anchorToDef[anchor];
39
+ if (mapped && defKeys.has(mapped))
40
+ return mapped;
41
+ if (defKeys.has(anchor))
42
+ return anchor;
43
+ }
44
+ return null;
45
+ }
46
+ /**
47
+ * Pure function that generates a dependency map from a JSON Schema document.
48
+ *
49
+ * @param doc The source JSON Schema document
50
+ * @returns A record mapping definition names to their dependency lists
51
+ */
52
+ function generateDependencyMapLogic(doc) {
53
+ const defs = doc?.$defs && typeof doc.$defs === "object" ? doc.$defs : {};
54
+ const defKeys = new Set(Object.keys(defs));
55
+ // Map anchors to $defs keys (useful if any anchor-style refs remain)
56
+ const anchorToDef = {};
57
+ for (const [defKey, defSchema] of Object.entries(defs)) {
58
+ if (!defSchema || typeof defSchema !== "object" || Array.isArray(defSchema))
59
+ continue;
60
+ const anchor = defSchema.$anchor;
61
+ if (typeof anchor === "string" && !(anchor in anchorToDef)) {
62
+ anchorToDef[anchor] = defKey;
63
+ }
64
+ }
65
+ const dependencyMap = {};
66
+ for (const [defKey, defSchema] of Object.entries(defs)) {
67
+ const refs = new Set();
68
+ collectRefs(defSchema, refs);
69
+ const deps = new Set();
70
+ for (const ref of refs) {
71
+ const depKey = resolveInternalRefToDefKey(ref, defKeys, anchorToDef);
72
+ if (!depKey)
73
+ continue;
74
+ if (depKey === defKey)
75
+ continue;
76
+ deps.add(depKey);
77
+ }
78
+ dependencyMap[defKey] = Array.from(deps);
79
+ }
80
+ return dependencyMap;
81
+ }
82
+ async function main() {
83
+ const config = getConfig();
84
+ const inPath = config.getOutputPath("Genesis.json");
85
+ const outPath = config.getDependencyMapPath();
86
+ if (!fs.existsSync(inPath)) {
87
+ console.error(`Genesis schema not found at ${inPath}. Run extractSchemas first.`);
88
+ process.exit(1);
89
+ }
90
+ try {
91
+ const raw = fs.readFileSync(inPath, "utf8");
92
+ const doc = JSON.parse(raw);
93
+ const dependencyMap = generateDependencyMapLogic(doc);
94
+ fs.mkdirSync(path.dirname(outPath), { recursive: true });
95
+ fs.writeFileSync(outPath, JSON.stringify(dependencyMap, null, 4), "utf8");
96
+ console.log(`Wrote dependency map to ${outPath}`);
97
+ }
98
+ catch (error) {
99
+ console.error(`Error generating dependency map: ${error.message}`);
100
+ process.exit(1);
101
+ }
102
+ }
103
+ main().catch((e) => {
104
+ console.error(e);
105
+ process.exit(1);
106
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,91 @@
1
+ import fs from 'fs';
2
+ import path from 'path';
3
+ import { getConfig } from './_lib/config.js';
4
+ /**
5
+ * Generate Resource envelopes for all ResourceTypes defined in Genesis.json
6
+ *
7
+ * This script wraps each ResourceType definition (from $defs) with a Resource envelope
8
+ * that conforms to the Resource.nucleusSchema pattern defined in Genesis.json.
9
+ *
10
+ * For the top-level Genesis ResourceType, extractedData is set to {} to avoid
11
+ * duplicating the entire $defs object.
12
+ *
13
+ * Resource identities follow the pattern RESOURCE-{Name} where Name is the key
14
+ * from the $defs object. Genesis itself uses RESOURCE-Genesis.
15
+ *
16
+ * Usage: node ./dist/scripts/generateResourceEnvelopes.js
17
+ */
18
+ /**
19
+ * Pure function to generate resource envelopes from a Genesis schema.
20
+ *
21
+ * @param genesis The Genesis schema object
22
+ * @returns A record mapping resource names to their envelopes
23
+ */
24
+ function generateResourceEnvelopesLogic(genesis) {
25
+ if (!genesis.nucleusSchema || !genesis.nucleusSchema.$defs) {
26
+ throw new Error('Genesis.json must have nucleusSchema.$defs');
27
+ }
28
+ const defs = genesis.nucleusSchema.$defs;
29
+ const defKeys = Object.keys(defs);
30
+ // Generate Resource envelopes
31
+ const resources = {};
32
+ // Genesis timestamp: 2025-11-30T00:00:00.000Z marks the genesis of ToolProof
33
+ const genesisTimestamp = '2025-11-30T00:00:00.000Z';
34
+ // First entry is Genesis itself with empty extractedData
35
+ resources['Genesis'] = {
36
+ identity: 'RESOURCE-Genesis',
37
+ resourceTypeRef: 'TYPE-ResourceType',
38
+ creationContext: {
39
+ resourceRoleRef: 'ROLE-Genesis',
40
+ executionRef: 'EXECUTION-Genesis'
41
+ },
42
+ kind: 'materialized',
43
+ timestamp: genesisTimestamp,
44
+ extractedData: {}
45
+ };
46
+ // Generate resources for all other $defs
47
+ defKeys.forEach((defName) => {
48
+ const defValue = defs[defName];
49
+ resources[defName] = {
50
+ identity: `RESOURCE-${defName}`,
51
+ resourceTypeRef: 'TYPE-ResourceType',
52
+ creationContext: {
53
+ resourceRoleRef: 'ROLE-Genesis',
54
+ executionRef: `EXECUTION-${defName}`
55
+ },
56
+ kind: 'materialized',
57
+ timestamp: genesisTimestamp,
58
+ extractedData: defValue
59
+ };
60
+ });
61
+ return resources;
62
+ }
63
+ async function main() {
64
+ const config = getConfig();
65
+ // Use normalized version with anchor refs rewritten to pointers
66
+ const genesisSourcePath = config.getNormalizedSourcePath();
67
+ const outputPath = path.join(config.getGeneratedResourcesDir(), 'Genesis.json');
68
+ if (!fs.existsSync(genesisSourcePath)) {
69
+ console.error(`Genesis source file not found at ${genesisSourcePath}`);
70
+ process.exit(1);
71
+ }
72
+ const raw = fs.readFileSync(genesisSourcePath, 'utf-8');
73
+ const genesis = JSON.parse(raw);
74
+ try {
75
+ const resources = generateResourceEnvelopesLogic(genesis);
76
+ // Ensure output directory exists
77
+ const outputDir = path.dirname(outputPath);
78
+ fs.mkdirSync(outputDir, { recursive: true });
79
+ // Write the generated resources file
80
+ fs.writeFileSync(outputPath, JSON.stringify(resources, null, 4) + '\n', 'utf-8');
81
+ console.log(`Generated ${Object.keys(resources).length} Resource envelopes -> ${outputPath}`);
82
+ }
83
+ catch (error) {
84
+ console.error(error.message);
85
+ process.exit(1);
86
+ }
87
+ }
88
+ main().catch((e) => {
89
+ console.error(e);
90
+ process.exit(1);
91
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,93 @@
1
+ import fs from 'fs';
2
+ import { getConfig } from './_lib/config.js';
3
+ /**
4
+ * Generate a typed Resource variant where `extractedData` is typed to a specific schema
5
+ * extracted under the configured output directory.
6
+ *
7
+ * Usage: node ./dist/scripts/generateResourceTypeType.js --name Job
8
+ */
9
+ /**
10
+ * Pure function to generate the typed Resource D.TS content.
11
+ *
12
+ * @param name The name of the schema
13
+ * @returns The TypeScript D.TS file content
14
+ */
15
+ function generateResourceTypeTypeLogic(name) {
16
+ const header = '// Auto-generated strict composite type. Do not edit.\n';
17
+ const ts = `import type { ResourceMetaBase, ${name} as ExtractedData } from './types.js';\n` +
18
+ `export type Resource_${name} = ResourceMetaBase & { extractedData: ExtractedData };\n`;
19
+ return header + ts;
20
+ }
21
+ async function main() {
22
+ const config = getConfig();
23
+ const { name } = parseArgs(process.argv.slice(2));
24
+ if (!name) {
25
+ console.error('Missing --name <SchemaBasename> argument');
26
+ process.exit(1);
27
+ }
28
+ const inPath = config.getOutputPath(`${name}.json`);
29
+ if (!fs.existsSync(inPath)) {
30
+ console.error(`Schema file not found: ${inPath}`);
31
+ process.exit(1);
32
+ }
33
+ // Basic validation against the expected shape of nucleusSchema.
34
+ const raw = fs.readFileSync(inPath, 'utf8');
35
+ let parsed = null;
36
+ try {
37
+ parsed = JSON.parse(raw);
38
+ }
39
+ catch (e) {
40
+ console.error(`Failed to parse JSON schema ${inPath}:`, e);
41
+ process.exit(1);
42
+ }
43
+ // Minimal checks that roughly match the nucleusSchema constraints used elsewhere.
44
+ if (parsed.$schema && parsed.$schema !== 'https://json-schema.org/draft/2020-12/schema') {
45
+ console.warn(`Warning: schema $schema is '${parsed.$schema}', expected draft 2020-12. Proceeding anyway.`);
46
+ }
47
+ if (parsed.type && parsed.type !== 'object') {
48
+ console.warn(`Warning: nucleusSchema usually has type: 'object' but this schema has type: '${parsed.type}'. Proceeding.`);
49
+ }
50
+ const tsContent = generateResourceTypeTypeLogic(name);
51
+ const jsContent = 'export {}\n';
52
+ // Output setup
53
+ const outName = `Resource_${name}.d.ts`;
54
+ const outJsName = `Resource_${name}.js`;
55
+ // Process src output
56
+ const outDir = config.getTypesSrcDir();
57
+ fs.mkdirSync(outDir, { recursive: true });
58
+ const outPath = config.getTypesSrcPath(outName);
59
+ const outJsPath = config.getTypesSrcPath(outJsName);
60
+ fs.writeFileSync(outPath, tsContent, 'utf8');
61
+ console.log(`Wrote ${outPath}`);
62
+ if (!fs.existsSync(outJsPath)) {
63
+ fs.writeFileSync(outJsPath, jsContent, 'utf8');
64
+ console.log(`Wrote ${outJsPath}`);
65
+ }
66
+ // Process dist output
67
+ const distLibDir = config.getTypesDistDir();
68
+ fs.mkdirSync(distLibDir, { recursive: true });
69
+ const distDtsPath = config.getTypesDistPath(outName);
70
+ const distJsPath = config.getTypesDistPath(outJsName);
71
+ fs.writeFileSync(distDtsPath, tsContent, 'utf8');
72
+ fs.writeFileSync(distJsPath, jsContent, 'utf8');
73
+ console.log(`Wrote ${distDtsPath}`);
74
+ console.log(`Wrote ${distJsPath}`);
75
+ }
76
+ function parseArgs(args) {
77
+ let name;
78
+ for (let i = 0; i < args.length; i++) {
79
+ const a = args[i];
80
+ if (a === '--name') {
81
+ name = args[i + 1];
82
+ i++;
83
+ }
84
+ else if (a.startsWith('--name=')) {
85
+ name = a.split('=')[1];
86
+ }
87
+ }
88
+ return { name };
89
+ }
90
+ main().catch((e) => {
91
+ console.error(e);
92
+ process.exit(1);
93
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,105 @@
1
+ import fs from 'fs';
2
+ import path from 'path';
3
+ import { getConfig } from './_lib/config.js';
4
+ /**
5
+ * Generate TypeScript shim files for all JSON schemas and resources
6
+ *
7
+ * Creates .ts files that import and re-export .json files using
8
+ * the JSON import assertion syntax. This enables proper dependency resolution
9
+ * and type inference when importing schemas in TypeScript.
10
+ *
11
+ * Generates shims for:
12
+ * - src/genesis/generated/schemas/*.json (schema files)
13
+ * - src/genesis/generated/resources/*.json (resource envelope files)
14
+ *
15
+ * Usage: node ./dist/scripts/generateSchemaShims.js
16
+ */
17
+ /**
18
+ * Pure function to generate the content of a TypeScript shim file.
19
+ *
20
+ * @param jsonFile The name of the JSON file to import
21
+ * @param variableName The name of the variable to use for the import
22
+ * @returns The TypeScript file content
23
+ */
24
+ function generateShimContent(jsonFile, variableName) {
25
+ return `import ${variableName} from './${jsonFile}' with { type: 'json' };\nexport default ${variableName};\n`;
26
+ }
27
+ /**
28
+ * Pure function to map a list of JSON files to their corresponding TypeScript shim files and contents.
29
+ *
30
+ * @param jsonFiles List of JSON filenames
31
+ * @param variableName The variable name to use in the shim
32
+ * @returns A record mapping TS filenames to their contents
33
+ */
34
+ function getShimsForFiles(jsonFiles, variableName) {
35
+ const shims = {};
36
+ for (const jsonFile of jsonFiles) {
37
+ const baseName = path.basename(jsonFile, '.json');
38
+ const tsFile = `${baseName}.ts`;
39
+ shims[tsFile] = generateShimContent(jsonFile, variableName);
40
+ }
41
+ return shims;
42
+ }
43
+ async function main() {
44
+ const config = getConfig();
45
+ const schemasDir = config.getOutputDir();
46
+ const resourcesDir = config.getGeneratedResourcesDir();
47
+ const generatedResourceTypesDir = config.getNormalizedDir();
48
+ let totalCount = 0;
49
+ // Process schemas directory
50
+ if (fs.existsSync(schemasDir)) {
51
+ const files = fs.readdirSync(schemasDir);
52
+ const jsonFiles = files.filter(f => f.endsWith('.json') && !f.startsWith('.'));
53
+ const shims = getShimsForFiles(jsonFiles, 'schema');
54
+ for (const [tsFile, content] of Object.entries(shims)) {
55
+ const tsPath = path.join(schemasDir, tsFile);
56
+ fs.writeFileSync(tsPath, content, 'utf-8');
57
+ console.log(`Generated ${tsFile} in ${schemasDir}`);
58
+ totalCount++;
59
+ }
60
+ console.log(`Generated ${jsonFiles.length} TypeScript schema shims in ${schemasDir}`);
61
+ }
62
+ else {
63
+ console.warn(`Schemas directory not found at ${schemasDir}`);
64
+ }
65
+ // Process resources directory
66
+ if (fs.existsSync(resourcesDir)) {
67
+ const files = fs.readdirSync(resourcesDir);
68
+ const jsonFiles = files.filter(f => f.endsWith('.json') && !f.startsWith('.'));
69
+ const shims = getShimsForFiles(jsonFiles, 'resources');
70
+ for (const [tsFile, content] of Object.entries(shims)) {
71
+ const tsPath = path.join(resourcesDir, tsFile);
72
+ fs.writeFileSync(tsPath, content, 'utf-8');
73
+ console.log(`Generated ${tsFile} in ${resourcesDir}`);
74
+ totalCount++;
75
+ }
76
+ console.log(`Generated ${jsonFiles.length} TypeScript resource shims in ${resourcesDir}`);
77
+ }
78
+ else {
79
+ console.warn(`Resources directory not found at ${resourcesDir}`);
80
+ }
81
+ // Genesis (normalized) shim
82
+ try {
83
+ const genesisJsonPath = config.getNormalizedSourcePath();
84
+ if (fs.existsSync(genesisJsonPath)) {
85
+ fs.mkdirSync(generatedResourceTypesDir, { recursive: true });
86
+ const tsFile = 'Genesis.ts';
87
+ const content = generateShimContent('Genesis.json', 'schema');
88
+ const tsPath = path.join(generatedResourceTypesDir, tsFile);
89
+ fs.writeFileSync(tsPath, content, 'utf-8');
90
+ console.log(`Generated ${tsFile} in ${generatedResourceTypesDir}`);
91
+ totalCount++;
92
+ }
93
+ else {
94
+ console.warn(`Genesis source JSON not found at ${genesisJsonPath}; skipping Genesis.ts shim`);
95
+ }
96
+ }
97
+ catch (e) {
98
+ console.warn('Failed to generate Genesis.ts shim:', e);
99
+ }
100
+ console.log(`Generated ${totalCount} total TypeScript shims`);
101
+ }
102
+ main().catch((e) => {
103
+ console.error(e);
104
+ process.exit(1);
105
+ });
@@ -0,0 +1 @@
1
+ export {};