@toolproof-core/genesis 1.0.47
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/dist/src/utils/constantsAndMappings.d.ts +20 -0
- package/dist/src/utils/constantsAndMappings.js +155 -0
- package/dist/src/utils/coreProjection.d.ts +16 -0
- package/dist/src/utils/coreProjection.js +29 -0
- package/dist/src/utils/resourceTypes.d.ts +12 -0
- package/dist/src/utils/resourceTypes.js +17 -0
- package/dist/src/utils/resources.d.ts +5 -0
- package/dist/src/utils/resources.js +17 -0
- package/dist/src/utils/schemaDependencies.d.ts +13 -0
- package/dist/src/utils/schemaDependencies.js +94 -0
- package/dist/src/utils/schemaObjectNormalization.d.ts +1 -0
- package/dist/src/utils/schemaObjectNormalization.js +66 -0
- package/dist/src/utils/schemaRefNormalization.d.ts +2 -0
- package/dist/src/utils/schemaRefNormalization.js +76 -0
- package/dist/src/utils/schemaShims.d.ts +2 -0
- package/dist/src/utils/schemaShims.js +13 -0
- package/dist/src/utils/standaloneSchemas.d.ts +16 -0
- package/dist/src/utils/standaloneSchemas.js +101 -0
- package/dist/src/utils/standaloneTypes.d.ts +8 -0
- package/dist/src/utils/standaloneTypes.js +22 -0
- package/dist/src/utils/standaloneZodSchemas.d.ts +8 -0
- package/dist/src/utils/standaloneZodSchemas.js +53 -0
- package/dist/src/utils/typeGeneration.d.ts +1 -0
- package/dist/src/utils/typeGeneration.js +25 -0
- package/dist/src/utils/typeGenerationPostProcess.d.ts +2 -0
- package/dist/src/utils/typeGenerationPostProcess.js +166 -0
- package/dist/src/utils/typeGenerationPreflight.d.ts +2 -0
- package/dist/src/utils/typeGenerationPreflight.js +93 -0
- package/dist/src/utils/zodCodegen.d.ts +10 -0
- package/dist/src/utils/zodCodegen.js +521 -0
- package/generated-src/derived/constants.ts +130 -0
- package/generated-src/derived/mappings.ts +12 -0
- package/generated-src/metadata/Core.json +1919 -0
- package/generated-src/metadata/dependencyMap.json +283 -0
- package/generated-src/metadata/terminals.json +17 -0
- package/generated-src/resourceTypes/resourceTypes.json +375 -0
- package/generated-src/resourceTypes/resourceTypes.ts +2 -0
- package/generated-src/resources/resourceTypes.json +456 -0
- package/generated-src/resources/resourceTypes.ts +2 -0
- package/generated-src/schemas/schemas.json +1670 -0
- package/generated-src/schemas/schemas.ts +2 -0
- package/generated-src/schemas/standalone/Boolean.json +5 -0
- package/generated-src/schemas/standalone/Boolean.ts +2 -0
- package/generated-src/schemas/standalone/Error.json +78 -0
- package/generated-src/schemas/standalone/Error.ts +2 -0
- package/generated-src/schemas/standalone/Goal.json +53 -0
- package/generated-src/schemas/standalone/Goal.ts +2 -0
- package/generated-src/schemas/standalone/Natural.json +6 -0
- package/generated-src/schemas/standalone/Natural.ts +2 -0
- package/generated-src/schemas/standalone/Resource.json +170 -0
- package/generated-src/schemas/standalone/Resource.ts +2 -0
- package/generated-src/schemas/standalone/ResourceType.json +131 -0
- package/generated-src/schemas/standalone/ResourceType.ts +2 -0
- package/generated-src/schemas/standalone/Strategy.json +641 -0
- package/generated-src/schemas/standalone/Strategy.ts +2 -0
- package/generated-src/schemas/standalone/StrategyTrace.json +820 -0
- package/generated-src/schemas/standalone/StrategyTrace.ts +2 -0
- package/generated-src/schemas/standalone/Tool.json +508 -0
- package/generated-src/schemas/standalone/Tool.ts +2 -0
- package/generated-src/schemas/zod/Boolean.ts +6 -0
- package/generated-src/schemas/zod/Error.ts +11 -0
- package/generated-src/schemas/zod/Goal.ts +8 -0
- package/generated-src/schemas/zod/Natural.ts +6 -0
- package/generated-src/schemas/zod/Resource.ts +21 -0
- package/generated-src/schemas/zod/ResourceType.ts +14 -0
- package/generated-src/schemas/zod/Strategy.ts +68 -0
- package/generated-src/schemas/zod/StrategyTrace.ts +75 -0
- package/generated-src/schemas/zod/Tool.ts +29 -0
- package/generated-src/schemas/zod/index.ts +10 -0
- package/generated-src/types/standalone/BooleanResource.d.ts +3 -0
- package/generated-src/types/standalone/BooleanResource.js +1 -0
- package/generated-src/types/standalone/ErrorResource.d.ts +3 -0
- package/generated-src/types/standalone/ErrorResource.js +1 -0
- package/generated-src/types/standalone/GoalResource.d.ts +3 -0
- package/generated-src/types/standalone/GoalResource.js +1 -0
- package/generated-src/types/standalone/NaturalResource.d.ts +3 -0
- package/generated-src/types/standalone/NaturalResource.js +1 -0
- package/generated-src/types/standalone/ResourceResource.d.ts +3 -0
- package/generated-src/types/standalone/ResourceResource.js +1 -0
- package/generated-src/types/standalone/ResourceTypeResource.d.ts +3 -0
- package/generated-src/types/standalone/ResourceTypeResource.js +1 -0
- package/generated-src/types/standalone/StrategyResource.d.ts +3 -0
- package/generated-src/types/standalone/StrategyResource.js +1 -0
- package/generated-src/types/standalone/StrategyTraceResource.d.ts +3 -0
- package/generated-src/types/standalone/StrategyTraceResource.js +1 -0
- package/generated-src/types/standalone/ToolResource.d.ts +3 -0
- package/generated-src/types/standalone/ToolResource.js +1 -0
- package/generated-src/types/types.d.ts +701 -0
- package/generated-src/types/types.js +1 -0
- package/package.json +52 -0
- package/src/genesis/resourceTypeShells.json +47 -0
- package/src/genesis/resources/booleans.json +24 -0
- package/src/genesis/resources/implementations/foo.ts +182 -0
- package/src/genesis/resources/naturals.json +112 -0
- package/src/genesis/resources/tools.json +840 -0
- package/src/genesis/schemas.json +1670 -0
- package/src/index.ts +27 -0
- package/src/utils/constantsAndMappings.ts +195 -0
- package/src/utils/coreProjection.ts +53 -0
- package/src/utils/resourceTypes.ts +39 -0
- package/src/utils/resources.ts +26 -0
- package/src/utils/schemaDependencies.ts +115 -0
- package/src/utils/schemaObjectNormalization.ts +71 -0
- package/src/utils/schemaRefNormalization.ts +83 -0
- package/src/utils/schemaShims.ts +17 -0
- package/src/utils/standaloneSchemas.ts +114 -0
- package/src/utils/standaloneTypes.ts +28 -0
- package/src/utils/standaloneZodSchemas.ts +72 -0
- package/src/utils/typeGeneration.ts +31 -0
- package/src/utils/typeGenerationPostProcess.ts +246 -0
- package/src/utils/typeGenerationPreflight.ts +119 -0
- package/src/utils/zodCodegen.ts +549 -0
package/src/index.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// Re-export JSON schemas via .ts shims to avoid .json re-exports in declarations
|
|
2
|
+
export { default as Schemas } from '../generated-src/schemas/schemas.js';
|
|
3
|
+
|
|
4
|
+
export { default as ResourceTypeSchema } from '../generated-src/schemas/standalone/ResourceType.js';
|
|
5
|
+
export { default as ToolSchema } from '../generated-src/schemas/standalone/Tool.js';
|
|
6
|
+
export { default as StrategySchema } from '../generated-src/schemas/standalone/Strategy.js';
|
|
7
|
+
export { default as StrategyTraceSchema } from '../generated-src/schemas/standalone/StrategyTrace.js';
|
|
8
|
+
|
|
9
|
+
// Re-export generated Zod validators
|
|
10
|
+
export { ToolSchema as ZodToolSchema } from '../generated-src/schemas/zod/Tool.js';
|
|
11
|
+
export { ResourceTypeSchema as ZodResourceTypeSchema } from '../generated-src/schemas/zod/ResourceType.js';
|
|
12
|
+
export { StrategySchema as ZodStrategySchema } from '../generated-src/schemas/zod/Strategy.js';
|
|
13
|
+
export { StrategyTraceSchema as ZodStrategyTraceSchema } from '../generated-src/schemas/zod/StrategyTrace.js';
|
|
14
|
+
export { GoalSchema as ZodGoalSchema } from '../generated-src/schemas/zod/Goal.js';
|
|
15
|
+
export * as ZodSchemas from '../generated-src/schemas/zod/index.js';
|
|
16
|
+
|
|
17
|
+
export { default as ResourceTypes } from '../generated-src/resources/resourceTypes.js';
|
|
18
|
+
|
|
19
|
+
export { default as CONSTANTS } from '../generated-src/derived/constants.js';
|
|
20
|
+
export { default as MAPPINGS } from '../generated-src/derived/mappings.js';
|
|
21
|
+
|
|
22
|
+
export type * from '../generated-src/types/types.js';
|
|
23
|
+
|
|
24
|
+
export type * from '../generated-src/types/standalone/ResourceTypeResource.js';
|
|
25
|
+
export type * from '../generated-src/types/standalone/ToolResource.js';
|
|
26
|
+
export type * from '../generated-src/types/standalone/StrategyResource.js';
|
|
27
|
+
export type * from '../generated-src/types/standalone/StrategyTraceResource.js';
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
type JSONValue = null | boolean | number | string | JSONValue[] | { [k: string]: JSONValue };
|
|
2
|
+
|
|
3
|
+
export type GeneratedConstants = {
|
|
4
|
+
Names: Record<string, string>;
|
|
5
|
+
Enums: Record<string, Record<string, string>>;
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
export type GeneratedMappings = {
|
|
9
|
+
HandleNameToPrefix: Record<string, string>;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
function deriveIdPrefixFromPattern(pattern: string): string | undefined {
|
|
13
|
+
const match = /^\^([^$]+)\.\+\$$/.exec(pattern);
|
|
14
|
+
if (!match) return undefined;
|
|
15
|
+
|
|
16
|
+
const prefix = match[1];
|
|
17
|
+
if (!prefix || /[`\n\r]/.test(prefix)) return undefined;
|
|
18
|
+
|
|
19
|
+
return prefix;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function extractHandlePrefixes(schema: unknown): Record<string, string> {
|
|
23
|
+
if (!schema || typeof schema !== 'object') return {};
|
|
24
|
+
|
|
25
|
+
const defs = (schema as { $defs?: unknown }).$defs;
|
|
26
|
+
if (!defs || typeof defs !== 'object') return {};
|
|
27
|
+
|
|
28
|
+
const defEntries = Object.entries(defs as Record<string, unknown>);
|
|
29
|
+
defEntries.sort(([a], [b]) => a.localeCompare(b));
|
|
30
|
+
|
|
31
|
+
const out: Record<string, string> = {};
|
|
32
|
+
|
|
33
|
+
for (const [defName, defVal] of defEntries) {
|
|
34
|
+
if (!/(Id|Handle)$/.test(defName)) continue;
|
|
35
|
+
if (!defVal || typeof defVal !== 'object' || Array.isArray(defVal)) continue;
|
|
36
|
+
|
|
37
|
+
const value = defVal as { type?: unknown; pattern?: unknown };
|
|
38
|
+
if (value.type !== 'string') continue;
|
|
39
|
+
if (typeof value.pattern !== 'string') continue;
|
|
40
|
+
|
|
41
|
+
const prefix = deriveIdPrefixFromPattern(value.pattern);
|
|
42
|
+
if (!prefix) continue;
|
|
43
|
+
|
|
44
|
+
out[defName] = prefix;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return out;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function extractSubschemaNames(schema: unknown): Record<string, string> {
|
|
51
|
+
if (!schema || typeof schema !== 'object') return {};
|
|
52
|
+
|
|
53
|
+
const defs = (schema as { $defs?: unknown }).$defs;
|
|
54
|
+
if (!defs || typeof defs !== 'object') return {};
|
|
55
|
+
|
|
56
|
+
const defEntries = Object.entries(defs as Record<string, unknown>);
|
|
57
|
+
defEntries.sort(([a], [b]) => a.localeCompare(b));
|
|
58
|
+
|
|
59
|
+
const out: Record<string, string> = {};
|
|
60
|
+
for (const [defName] of defEntries) {
|
|
61
|
+
if (!defName || /[\n\r`]/.test(defName)) continue;
|
|
62
|
+
out[defName] = defName;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return out;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function extractEnums(schema: unknown): Record<string, Record<string, string>> {
|
|
69
|
+
if (!schema || typeof schema !== 'object') return {};
|
|
70
|
+
|
|
71
|
+
const defs = (schema as { $defs?: unknown }).$defs;
|
|
72
|
+
if (!defs || typeof defs !== 'object') return {};
|
|
73
|
+
|
|
74
|
+
const defEntries = Object.entries(defs as Record<string, unknown>);
|
|
75
|
+
defEntries.sort(([a], [b]) => a.localeCompare(b));
|
|
76
|
+
|
|
77
|
+
const out: Record<string, Record<string, string>> = {};
|
|
78
|
+
|
|
79
|
+
for (const [defName, defVal] of defEntries) {
|
|
80
|
+
if (!/(Kind|Status)$/.test(defName)) continue;
|
|
81
|
+
if (!defVal || typeof defVal !== 'object' || Array.isArray(defVal)) continue;
|
|
82
|
+
|
|
83
|
+
const value = defVal as { type?: unknown; enum?: unknown };
|
|
84
|
+
if (value.type !== 'string') continue;
|
|
85
|
+
if (!Array.isArray(value.enum) || value.enum.length === 0) continue;
|
|
86
|
+
if (value.enum.some((item) => typeof item !== 'string')) continue;
|
|
87
|
+
|
|
88
|
+
const members: Record<string, string> = {};
|
|
89
|
+
for (const member of value.enum as readonly string[]) {
|
|
90
|
+
members[member] = member;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
out[defName] = members;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return out;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export function extractGeneratedConstantsAndMappings(schema: JSONValue): {
|
|
100
|
+
CONSTANTS: GeneratedConstants;
|
|
101
|
+
MAPPINGS: GeneratedMappings;
|
|
102
|
+
} {
|
|
103
|
+
const names = extractSubschemaNames(schema);
|
|
104
|
+
const enums = extractEnums(schema);
|
|
105
|
+
const handlePrefixes = extractHandlePrefixes(schema);
|
|
106
|
+
|
|
107
|
+
return {
|
|
108
|
+
CONSTANTS: {
|
|
109
|
+
Names: names,
|
|
110
|
+
Enums: enums,
|
|
111
|
+
},
|
|
112
|
+
MAPPINGS: {
|
|
113
|
+
HandleNameToPrefix: handlePrefixes,
|
|
114
|
+
},
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function escapeTsString(value: string): string {
|
|
119
|
+
return value
|
|
120
|
+
.replace(/\\/g, '\\\\')
|
|
121
|
+
.replace(/\r/g, '\\r')
|
|
122
|
+
.replace(/\n/g, '\\n')
|
|
123
|
+
.replace(/\t/g, '\\t')
|
|
124
|
+
.replace(/'/g, "\\'");
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function renderTsStringLiteral(value: string): string {
|
|
128
|
+
return `'${escapeTsString(value)}'`;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function isValidTsIdentifier(key: string): boolean {
|
|
132
|
+
return /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(key);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function renderTsKey(key: string): string {
|
|
136
|
+
return isValidTsIdentifier(key) ? key : renderTsStringLiteral(key);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export function renderConstantsTs(constants: GeneratedConstants): string {
|
|
140
|
+
const nameKeys = Object.keys(constants.Names).sort((a, b) => a.localeCompare(b));
|
|
141
|
+
const enumKeys = Object.keys(constants.Enums).sort((a, b) => a.localeCompare(b));
|
|
142
|
+
|
|
143
|
+
const lines: string[] = [];
|
|
144
|
+
lines.push('const CONSTANTS = {');
|
|
145
|
+
lines.push(' Names: {');
|
|
146
|
+
|
|
147
|
+
for (const key of nameKeys) {
|
|
148
|
+
const value = constants.Names[key] ?? '';
|
|
149
|
+
lines.push(` ${renderTsKey(key)}: ${renderTsStringLiteral(value)},`);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
lines.push(' },');
|
|
153
|
+
lines.push(' Enums: {');
|
|
154
|
+
|
|
155
|
+
for (const key of enumKeys) {
|
|
156
|
+
const members = constants.Enums[key] ?? {};
|
|
157
|
+
lines.push(` ${renderTsKey(key)}: {`);
|
|
158
|
+
|
|
159
|
+
for (const memberKey of Object.keys(members)) {
|
|
160
|
+
const value = members[memberKey] ?? '';
|
|
161
|
+
lines.push(` ${renderTsKey(memberKey)}: ${renderTsStringLiteral(value)},`);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
lines.push(' },');
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
lines.push(' }');
|
|
168
|
+
lines.push('} as const;');
|
|
169
|
+
lines.push('');
|
|
170
|
+
lines.push('export default CONSTANTS;');
|
|
171
|
+
lines.push('');
|
|
172
|
+
|
|
173
|
+
return lines.join('\n');
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
export function renderMappingsTs(mappings: GeneratedMappings): string {
|
|
177
|
+
const handleKeys = Object.keys(mappings.HandleNameToPrefix).sort((a, b) => a.localeCompare(b));
|
|
178
|
+
|
|
179
|
+
const lines: string[] = [];
|
|
180
|
+
lines.push('const MAPPINGS = {');
|
|
181
|
+
lines.push(' HandleNameToPrefix: {');
|
|
182
|
+
|
|
183
|
+
for (const key of handleKeys) {
|
|
184
|
+
const value = mappings.HandleNameToPrefix[key] ?? '';
|
|
185
|
+
lines.push(` ${renderTsKey(key)}: ${renderTsStringLiteral(value)},`);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
lines.push(' },');
|
|
189
|
+
lines.push('} as const;');
|
|
190
|
+
lines.push('');
|
|
191
|
+
lines.push('export default MAPPINGS;');
|
|
192
|
+
lines.push('');
|
|
193
|
+
|
|
194
|
+
return lines.join('\n');
|
|
195
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import * as path from 'node:path';
|
|
2
|
+
|
|
3
|
+
export type CoreProjectionNode = CoreProjectionDirectoryNode | CoreProjectionFileNode;
|
|
4
|
+
|
|
5
|
+
export interface CoreProjectionBaseNode {
|
|
6
|
+
kind: 'directory' | 'file';
|
|
7
|
+
name: string;
|
|
8
|
+
path: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface CoreProjectionDirectoryNode extends CoreProjectionBaseNode {
|
|
12
|
+
kind: 'directory';
|
|
13
|
+
children: CoreProjectionNode[];
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface CoreProjectionFileNode extends CoreProjectionBaseNode {
|
|
17
|
+
kind: 'file';
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const EXCLUDED_DIRECTORY_NAMES = new Set([
|
|
21
|
+
'.git',
|
|
22
|
+
'.next',
|
|
23
|
+
'dist',
|
|
24
|
+
'node_modules',
|
|
25
|
+
]);
|
|
26
|
+
|
|
27
|
+
export function toPosixPath(value: string): string {
|
|
28
|
+
return value.split(path.sep).join('/');
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function shouldExclude(relativePath: string, isDirectory: boolean, excludedRelativePathPrefixes: string[]): boolean {
|
|
32
|
+
if (!relativePath || relativePath === '.') {
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const normalizedPath = toPosixPath(relativePath);
|
|
37
|
+
const baseName = path.posix.basename(normalizedPath);
|
|
38
|
+
|
|
39
|
+
if (isDirectory && EXCLUDED_DIRECTORY_NAMES.has(baseName)) {
|
|
40
|
+
return true;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return excludedRelativePathPrefixes.some((prefix) => {
|
|
44
|
+
return normalizedPath === prefix || normalizedPath.startsWith(`${prefix}/`);
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function compareNodes(a: CoreProjectionNode, b: CoreProjectionNode): number {
|
|
49
|
+
if (a.kind !== b.kind) {
|
|
50
|
+
return a.kind === 'directory' ? -1 : 1;
|
|
51
|
+
}
|
|
52
|
+
return a.name.localeCompare(b.name);
|
|
53
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
type JsonObject = Record<string, unknown>;
|
|
2
|
+
|
|
3
|
+
type SchemasDocument = {
|
|
4
|
+
$defs?: Record<string, JsonObject>;
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
type ResourceTypeShell = JsonObject & {
|
|
8
|
+
handle?: unknown;
|
|
9
|
+
name?: unknown;
|
|
10
|
+
description?: unknown;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
type ResourceTypeShellByName = Record<string, ResourceTypeShell>;
|
|
14
|
+
|
|
15
|
+
export function generateResourceTypes(
|
|
16
|
+
schemasDocument: SchemasDocument,
|
|
17
|
+
resourceTypeShells: ResourceTypeShellByName,
|
|
18
|
+
): ResourceTypeShellByName {
|
|
19
|
+
if (!schemasDocument.$defs || typeof schemasDocument.$defs !== 'object') {
|
|
20
|
+
throw new Error('Schema document must have root $defs');
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const resourceTypes: ResourceTypeShellByName = {};
|
|
24
|
+
|
|
25
|
+
for (const [resourceTypeName, resourceTypeShell] of Object.entries(resourceTypeShells)) {
|
|
26
|
+
const valueSchema = schemasDocument.$defs[resourceTypeName];
|
|
27
|
+
|
|
28
|
+
if (!valueSchema || typeof valueSchema !== 'object' || Array.isArray(valueSchema)) {
|
|
29
|
+
throw new Error(`Missing matching schema for resource type record ${resourceTypeName}`);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
resourceTypes[resourceTypeName] = {
|
|
33
|
+
...resourceTypeShell,
|
|
34
|
+
valueSchema,
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return resourceTypes;
|
|
39
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
type JsonObject = Record<string, unknown>;
|
|
2
|
+
|
|
3
|
+
type ResourceTypeByName = Record<string, JsonObject>;
|
|
4
|
+
|
|
5
|
+
type ResourceByName = Record<string, JsonObject>;
|
|
6
|
+
|
|
7
|
+
const GENESIS_TIMESTAMP = '2025-11-30T00:00:00.000Z';
|
|
8
|
+
|
|
9
|
+
export function generateResources(resourceTypes: ResourceTypeByName): ResourceByName {
|
|
10
|
+
const resources: ResourceByName = {};
|
|
11
|
+
|
|
12
|
+
for (const [name, resourceType] of Object.entries(resourceTypes)) {
|
|
13
|
+
resources[name] = {
|
|
14
|
+
id: `RESOURCE-${name}`,
|
|
15
|
+
resourceTypeHandle: 'TYPE-ResourceType',
|
|
16
|
+
provenance: {
|
|
17
|
+
resourceProvenanceKind: 'genesis',
|
|
18
|
+
},
|
|
19
|
+
version: 1,
|
|
20
|
+
timestamp: GENESIS_TIMESTAMP,
|
|
21
|
+
value: resourceType,
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return resources;
|
|
26
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
type JSONValue = null | boolean | number | string | JSONValue[] | { [k: string]: JSONValue };
|
|
2
|
+
|
|
3
|
+
export type DependencyMap = Record<string, string[]>;
|
|
4
|
+
|
|
5
|
+
export function decodeJsonPointerSegment(segment: string): string {
|
|
6
|
+
return segment.replace(/~1/g, '/').replace(/~0/g, '~');
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function collectRefs(node: unknown, out: Set<string>): void {
|
|
10
|
+
if (Array.isArray(node)) {
|
|
11
|
+
for (const item of node) collectRefs(item, out);
|
|
12
|
+
return;
|
|
13
|
+
}
|
|
14
|
+
if (!node || typeof node !== 'object') return;
|
|
15
|
+
|
|
16
|
+
const obj = node as Record<string, unknown>;
|
|
17
|
+
const ref = obj.$ref;
|
|
18
|
+
if (typeof ref === 'string') out.add(ref);
|
|
19
|
+
|
|
20
|
+
for (const value of Object.values(obj)) {
|
|
21
|
+
collectRefs(value, out);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function resolveInternalRefToDefKey(
|
|
26
|
+
ref: string,
|
|
27
|
+
defKeys: Set<string>,
|
|
28
|
+
anchorToDef: Record<string, string>,
|
|
29
|
+
): string | null {
|
|
30
|
+
if (!ref.startsWith('#')) return null;
|
|
31
|
+
|
|
32
|
+
const defsPrefix = '#/$defs/';
|
|
33
|
+
if (ref.startsWith(defsPrefix)) {
|
|
34
|
+
const rest = ref.slice(defsPrefix.length);
|
|
35
|
+
const firstSegment = rest.split('/')[0] ?? '';
|
|
36
|
+
const defKey = decodeJsonPointerSegment(firstSegment);
|
|
37
|
+
return defKeys.has(defKey) ? defKey : null;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (!ref.startsWith('#/')) {
|
|
41
|
+
const anchor = ref.slice(1);
|
|
42
|
+
const mapped = anchorToDef[anchor];
|
|
43
|
+
if (mapped && defKeys.has(mapped)) return mapped;
|
|
44
|
+
if (defKeys.has(anchor)) return anchor;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function generateDependencyMap(doc: { $defs?: Record<string, JSONValue> } | null | undefined): DependencyMap {
|
|
51
|
+
const defs: Record<string, JSONValue> = doc?.$defs && typeof doc.$defs === 'object' ? doc.$defs : {};
|
|
52
|
+
const defKeys = new Set(Object.keys(defs));
|
|
53
|
+
|
|
54
|
+
const anchorToDef: Record<string, string> = {};
|
|
55
|
+
for (const [defKey, defSchema] of Object.entries(defs)) {
|
|
56
|
+
if (!defSchema || typeof defSchema !== 'object' || Array.isArray(defSchema)) continue;
|
|
57
|
+
const anchor = (defSchema as { $anchor?: unknown }).$anchor;
|
|
58
|
+
if (typeof anchor === 'string' && !(anchor in anchorToDef)) {
|
|
59
|
+
anchorToDef[anchor] = defKey;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const dependencyMap: DependencyMap = {};
|
|
64
|
+
|
|
65
|
+
for (const [defKey, defSchema] of Object.entries(defs)) {
|
|
66
|
+
const refs = new Set<string>();
|
|
67
|
+
collectRefs(defSchema, refs);
|
|
68
|
+
|
|
69
|
+
const deps = new Set<string>();
|
|
70
|
+
for (const ref of refs) {
|
|
71
|
+
const depKey = resolveInternalRefToDefKey(ref, defKeys, anchorToDef);
|
|
72
|
+
if (!depKey || depKey === defKey) continue;
|
|
73
|
+
deps.add(depKey);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
dependencyMap[defKey] = Array.from(deps);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return dependencyMap;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export function normalizeDependencyMap(value: unknown): DependencyMap {
|
|
83
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
|
84
|
+
throw new Error('Invalid dependencyMap.json: expected an object');
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const input = value as Record<string, unknown>;
|
|
88
|
+
const out: DependencyMap = {};
|
|
89
|
+
|
|
90
|
+
for (const [key, rawDeps] of Object.entries(input)) {
|
|
91
|
+
if (rawDeps == null) {
|
|
92
|
+
out[key] = [];
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
if (!Array.isArray(rawDeps)) {
|
|
96
|
+
throw new Error(`Invalid dependencyMap.json: value for ${key} must be an array`);
|
|
97
|
+
}
|
|
98
|
+
out[key] = rawDeps.filter((dep): dep is string => typeof dep === 'string');
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return out;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export function computeTerminalsInKeyOrder(dependencyMap: DependencyMap): string[] {
|
|
105
|
+
const keys = Object.keys(dependencyMap);
|
|
106
|
+
const dependedUpon = new Set<string>();
|
|
107
|
+
|
|
108
|
+
for (const key of keys) {
|
|
109
|
+
for (const dep of dependencyMap[key] ?? []) {
|
|
110
|
+
dependedUpon.add(dep);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return keys.filter((key) => !dependedUpon.has(key));
|
|
115
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
export function normalizeAllOfSiblingObjectKeywords(node: any): any {
|
|
2
|
+
if (Array.isArray(node)) {
|
|
3
|
+
let changed = false;
|
|
4
|
+
const out = node.map((item) => {
|
|
5
|
+
const next = normalizeAllOfSiblingObjectKeywords(item);
|
|
6
|
+
if (next !== item) changed = true;
|
|
7
|
+
return next;
|
|
8
|
+
});
|
|
9
|
+
return changed ? out : node;
|
|
10
|
+
}
|
|
11
|
+
if (!node || typeof node !== 'object') return node;
|
|
12
|
+
|
|
13
|
+
const hasAllOf = Array.isArray((node as any).allOf) && (node as any).allOf.length > 0;
|
|
14
|
+
const looksLikeObjectSchema =
|
|
15
|
+
(node as any).type === 'object' ||
|
|
16
|
+
(node as any).properties !== undefined ||
|
|
17
|
+
(node as any).required !== undefined ||
|
|
18
|
+
(node as any).unevaluatedProperties !== undefined ||
|
|
19
|
+
(node as any).additionalProperties !== undefined ||
|
|
20
|
+
(node as any).propertyNames !== undefined;
|
|
21
|
+
|
|
22
|
+
const siblingKeys = [
|
|
23
|
+
'type',
|
|
24
|
+
'properties',
|
|
25
|
+
'required',
|
|
26
|
+
'additionalProperties',
|
|
27
|
+
'unevaluatedProperties',
|
|
28
|
+
'propertyNames',
|
|
29
|
+
'patternProperties',
|
|
30
|
+
'dependentRequired',
|
|
31
|
+
'dependentSchemas',
|
|
32
|
+
'minProperties',
|
|
33
|
+
'maxProperties',
|
|
34
|
+
] as const;
|
|
35
|
+
|
|
36
|
+
let localNode: any = node;
|
|
37
|
+
if (hasAllOf && looksLikeObjectSchema) {
|
|
38
|
+
const hasSiblingObjectKeywords = siblingKeys.some((key) => key in localNode);
|
|
39
|
+
if (hasSiblingObjectKeywords) {
|
|
40
|
+
const overlay: any = {};
|
|
41
|
+
const nextNode: any = { ...localNode };
|
|
42
|
+
|
|
43
|
+
for (const key of siblingKeys) {
|
|
44
|
+
if (key in nextNode) {
|
|
45
|
+
overlay[key] = nextNode[key];
|
|
46
|
+
delete nextNode[key];
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
nextNode.allOf = [overlay, ...(nextNode.allOf as any[])];
|
|
51
|
+
localNode = nextNode;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
let changed = localNode !== node;
|
|
56
|
+
const out: Record<string, any> = localNode === node ? ({} as any) : { ...localNode };
|
|
57
|
+
for (const [key, value] of Object.entries(localNode)) {
|
|
58
|
+
const next = normalizeAllOfSiblingObjectKeywords(value);
|
|
59
|
+
if (next !== value) {
|
|
60
|
+
if (!changed) {
|
|
61
|
+
changed = true;
|
|
62
|
+
Object.assign(out, localNode);
|
|
63
|
+
}
|
|
64
|
+
out[key] = next;
|
|
65
|
+
} else if (changed) {
|
|
66
|
+
out[key] = value;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return changed ? out : node;
|
|
71
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
export function stripAnchors<T>(schema: T): T {
|
|
2
|
+
const seen = new WeakMap<object, unknown>();
|
|
3
|
+
|
|
4
|
+
function walk(node: unknown): unknown {
|
|
5
|
+
if (!node || typeof node !== 'object') return node;
|
|
6
|
+
|
|
7
|
+
const existing = seen.get(node as object);
|
|
8
|
+
if (existing) return existing;
|
|
9
|
+
|
|
10
|
+
if (Array.isArray(node)) {
|
|
11
|
+
const out: unknown[] = [];
|
|
12
|
+
seen.set(node, out);
|
|
13
|
+
for (const item of node) out.push(walk(item));
|
|
14
|
+
return out;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const out: Record<string, unknown> = {};
|
|
18
|
+
seen.set(node as object, out);
|
|
19
|
+
|
|
20
|
+
for (const [key, value] of Object.entries(node as Record<string, unknown>)) {
|
|
21
|
+
if (key === '$anchor') continue;
|
|
22
|
+
out[key] = walk(value);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return out;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return walk(schema) as T;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function normalizeAnchorsToPointers<T>(input: T): T {
|
|
32
|
+
if (!input || typeof input !== 'object') return input;
|
|
33
|
+
|
|
34
|
+
const root = JSON.parse(JSON.stringify(input)) as { $defs?: Record<string, unknown> };
|
|
35
|
+
const defs: Record<string, unknown> = root.$defs && typeof root.$defs === 'object' ? root.$defs : {};
|
|
36
|
+
const anchorToDef: Record<string, string> = {};
|
|
37
|
+
|
|
38
|
+
for (const [defName, defValue] of Object.entries(defs)) {
|
|
39
|
+
if (!anchorToDef[defName]) {
|
|
40
|
+
anchorToDef[defName] = defName;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (!defValue || typeof defValue !== 'object') continue;
|
|
44
|
+
|
|
45
|
+
const envelope = defValue as { valueSchema?: { $anchor?: unknown }; $anchor?: unknown };
|
|
46
|
+
const valueSchema = envelope.valueSchema;
|
|
47
|
+
if (valueSchema && typeof valueSchema === 'object') {
|
|
48
|
+
const anchor = valueSchema.$anchor;
|
|
49
|
+
if (typeof anchor === 'string' && !anchorToDef[anchor]) {
|
|
50
|
+
anchorToDef[anchor] = defName;
|
|
51
|
+
}
|
|
52
|
+
} else {
|
|
53
|
+
const anchor = envelope.$anchor;
|
|
54
|
+
if (typeof anchor === 'string' && !anchorToDef[anchor]) {
|
|
55
|
+
anchorToDef[anchor] = defName;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function walk(node: unknown): 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
|
+
const obj = node as { $ref?: unknown } & Record<string, unknown>;
|
|
68
|
+
if (typeof obj.$ref === 'string' && obj.$ref.startsWith('#') && !obj.$ref.startsWith('#/')) {
|
|
69
|
+
const anchor = obj.$ref.slice(1);
|
|
70
|
+
const defName = anchorToDef[anchor];
|
|
71
|
+
if (defName) {
|
|
72
|
+
obj.$ref = `#/$defs/${defName}`;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
for (const value of Object.values(obj)) {
|
|
77
|
+
walk(value);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
walk(root);
|
|
82
|
+
return root as T;
|
|
83
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import * as path from 'node:path';
|
|
2
|
+
|
|
3
|
+
export function generateShimContent(jsonFile: string, variableName: string): string {
|
|
4
|
+
return `import ${variableName} from './${jsonFile}' with { type: 'json' };\nexport default ${variableName};\n`;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export function getShimsForFiles(jsonFiles: string[], variableName: string): Record<string, string> {
|
|
8
|
+
const shims: Record<string, string> = {};
|
|
9
|
+
|
|
10
|
+
for (const jsonFile of jsonFiles) {
|
|
11
|
+
const baseName = path.basename(jsonFile, '.json');
|
|
12
|
+
const tsFile = `${baseName}.ts`;
|
|
13
|
+
shims[tsFile] = generateShimContent(jsonFile, variableName);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
return shims;
|
|
17
|
+
}
|