@toolproof-core/genesis 1.0.50 → 1.0.51
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/generated-src/implementations/tools.ts +214 -214
- package/generated-src/metadata/Core.json +78 -0
- package/package.json +10 -12
- package/src/declarations/booleans.json +3 -3
- package/src/declarations/naturals.json +12 -12
- package/src/declarations/resourceTypes/resourceTypeShells.json +52 -52
- package/src/declarations/resourceTypes/schemas.json +1838 -1838
- package/src/declarations/tools.json +704 -704
- package/src/implementations/tools.ts +214 -214
- package/src/index.ts +131 -131
- package/src/utils/constantsAndMappings.ts +194 -194
- package/src/utils/coreProjection.ts +52 -52
- package/src/utils/resourceTypes.ts +70 -70
- package/src/utils/schemaDependencies.ts +114 -114
- package/src/utils/schemaObjectNormalization.ts +70 -70
- package/src/utils/schemaRefNormalization.ts +82 -82
- package/src/utils/schemaShims.ts +16 -16
- package/src/utils/standaloneSchemas.ts +113 -113
- package/src/utils/standaloneTypes.ts +27 -27
- package/src/utils/standaloneZodSchemas.ts +71 -71
- package/src/utils/timestampedResources.ts +41 -41
- package/src/utils/typeGeneration.ts +30 -30
- package/src/utils/typeGenerationPostProcess.ts +245 -245
- package/src/utils/typeGenerationPreflight.ts +118 -118
- package/src/utils/zodCodegen.ts +548 -548
- package/toolproof.json +18 -18
package/src/index.ts
CHANGED
|
@@ -1,131 +1,131 @@
|
|
|
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 ResourceTypeTimestampedResources } from '../generated-src/timestampedResources/resourceTypes.js';
|
|
18
|
-
|
|
19
|
-
export {
|
|
20
|
-
Add,
|
|
21
|
-
BooleanIdentity,
|
|
22
|
-
createSpecializationKey,
|
|
23
|
-
Divide,
|
|
24
|
-
Double,
|
|
25
|
-
Identity,
|
|
26
|
-
LessThan,
|
|
27
|
-
Multiply,
|
|
28
|
-
NaturalAdd,
|
|
29
|
-
NaturalDivide,
|
|
30
|
-
NaturalDouble,
|
|
31
|
-
NaturalIdentity,
|
|
32
|
-
NaturalLessThan,
|
|
33
|
-
NaturalMultiply,
|
|
34
|
-
NaturalSubtract,
|
|
35
|
-
Subtract,
|
|
36
|
-
TOOL_IMPLEMENTATION_BY_HANDLE,
|
|
37
|
-
TOOL_IMPLEMENTATION_BY_NAME,
|
|
38
|
-
} from '../generated-src/implementations/tools.js';
|
|
39
|
-
|
|
40
|
-
export { default as CONSTANTS } from '../generated-src/lookups/constants.js';
|
|
41
|
-
export { default as MAPPINGS } from '../generated-src/lookups/mappings.js';
|
|
42
|
-
|
|
43
|
-
export type {
|
|
44
|
-
ResourceTypeResource,
|
|
45
|
-
} from '../generated-src/types/standalone/ResourceTypeResource.js';
|
|
46
|
-
export type {
|
|
47
|
-
ToolResource,
|
|
48
|
-
} from '../generated-src/types/standalone/ToolResource.js';
|
|
49
|
-
export type {
|
|
50
|
-
StrategyResource,
|
|
51
|
-
} from '../generated-src/types/standalone/StrategyResource.js';
|
|
52
|
-
export type {
|
|
53
|
-
StrategyTraceResource,
|
|
54
|
-
} from '../generated-src/types/standalone/StrategyTraceResource.js';
|
|
55
|
-
|
|
56
|
-
export type {
|
|
57
|
-
BaseResource,
|
|
58
|
-
BaseStrategy,
|
|
59
|
-
BaseToolStepPathSpec,
|
|
60
|
-
Boolean,
|
|
61
|
-
BranchStep,
|
|
62
|
-
Case,
|
|
63
|
-
ContainerKind,
|
|
64
|
-
DeferredStrategyProvenance,
|
|
65
|
-
Description,
|
|
66
|
-
DescriptionFacet,
|
|
67
|
-
DocumentationSpec,
|
|
68
|
-
ExternalInputPotential,
|
|
69
|
-
GenesisProvenance,
|
|
70
|
-
Goal,
|
|
71
|
-
InputPotential,
|
|
72
|
-
InputResource,
|
|
73
|
-
InternalInputPotential,
|
|
74
|
-
JsonSchemaObject,
|
|
75
|
-
JsonSchemaObject1,
|
|
76
|
-
JsonSchemaObject2,
|
|
77
|
-
Name,
|
|
78
|
-
NameFacet,
|
|
79
|
-
Natural,
|
|
80
|
-
OutputAddress,
|
|
81
|
-
Provenance,
|
|
82
|
-
ProvenanceFacet,
|
|
83
|
-
ProvenanceKind,
|
|
84
|
-
Resource,
|
|
85
|
-
ResourceId,
|
|
86
|
-
ResourcePointer,
|
|
87
|
-
ResourceType,
|
|
88
|
-
ResourceTypeHandle,
|
|
89
|
-
RoleBindingSpec,
|
|
90
|
-
RoleBindingSpecFacet,
|
|
91
|
-
RoleBindingSpecFacet1,
|
|
92
|
-
RoleName,
|
|
93
|
-
RoleNameArray,
|
|
94
|
-
RoleSpec,
|
|
95
|
-
RoleSpecFacet,
|
|
96
|
-
RoleValue,
|
|
97
|
-
RoleValueByName,
|
|
98
|
-
Step,
|
|
99
|
-
StepArray,
|
|
100
|
-
StepArrayArray,
|
|
101
|
-
StepKind,
|
|
102
|
-
StepKindFacet,
|
|
103
|
-
Strategy,
|
|
104
|
-
StrategyHandle,
|
|
105
|
-
StrategyKind,
|
|
106
|
-
StrategyProvenance,
|
|
107
|
-
StrategyStateInputEntry,
|
|
108
|
-
StrategyStateInputEntryByRoleName,
|
|
109
|
-
StrategyStateInputKind,
|
|
110
|
-
StrategyTrace,
|
|
111
|
-
StrategyTraceHandle,
|
|
112
|
-
ThreadedStrategy,
|
|
113
|
-
ThreadedStrategyState,
|
|
114
|
-
ThreadedStrategyStateFacet,
|
|
115
|
-
ThreadedToolStepPath,
|
|
116
|
-
ThreadedToolStepPathSpec,
|
|
117
|
-
Tool,
|
|
118
|
-
ToolHandle,
|
|
119
|
-
ToolStep,
|
|
120
|
-
ToolStepPath,
|
|
121
|
-
ToolStepPathSlot,
|
|
122
|
-
ToolStepPathSlotFacet,
|
|
123
|
-
TypeRef,
|
|
124
|
-
UnthreadedStrategy,
|
|
125
|
-
UnthreadedStrategyState,
|
|
126
|
-
UnthreadedStrategyStateFacet,
|
|
127
|
-
UnthreadedToolStepPath,
|
|
128
|
-
UnthreadedToolStepPathSpec,
|
|
129
|
-
WhileStep,
|
|
130
|
-
ForStep,
|
|
131
|
-
} from '../generated-src/types/types.js';
|
|
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 ResourceTypeTimestampedResources } from '../generated-src/timestampedResources/resourceTypes.js';
|
|
18
|
+
|
|
19
|
+
export {
|
|
20
|
+
Add,
|
|
21
|
+
BooleanIdentity,
|
|
22
|
+
createSpecializationKey,
|
|
23
|
+
Divide,
|
|
24
|
+
Double,
|
|
25
|
+
Identity,
|
|
26
|
+
LessThan,
|
|
27
|
+
Multiply,
|
|
28
|
+
NaturalAdd,
|
|
29
|
+
NaturalDivide,
|
|
30
|
+
NaturalDouble,
|
|
31
|
+
NaturalIdentity,
|
|
32
|
+
NaturalLessThan,
|
|
33
|
+
NaturalMultiply,
|
|
34
|
+
NaturalSubtract,
|
|
35
|
+
Subtract,
|
|
36
|
+
TOOL_IMPLEMENTATION_BY_HANDLE,
|
|
37
|
+
TOOL_IMPLEMENTATION_BY_NAME,
|
|
38
|
+
} from '../generated-src/implementations/tools.js';
|
|
39
|
+
|
|
40
|
+
export { default as CONSTANTS } from '../generated-src/lookups/constants.js';
|
|
41
|
+
export { default as MAPPINGS } from '../generated-src/lookups/mappings.js';
|
|
42
|
+
|
|
43
|
+
export type {
|
|
44
|
+
ResourceTypeResource,
|
|
45
|
+
} from '../generated-src/types/standalone/ResourceTypeResource.js';
|
|
46
|
+
export type {
|
|
47
|
+
ToolResource,
|
|
48
|
+
} from '../generated-src/types/standalone/ToolResource.js';
|
|
49
|
+
export type {
|
|
50
|
+
StrategyResource,
|
|
51
|
+
} from '../generated-src/types/standalone/StrategyResource.js';
|
|
52
|
+
export type {
|
|
53
|
+
StrategyTraceResource,
|
|
54
|
+
} from '../generated-src/types/standalone/StrategyTraceResource.js';
|
|
55
|
+
|
|
56
|
+
export type {
|
|
57
|
+
BaseResource,
|
|
58
|
+
BaseStrategy,
|
|
59
|
+
BaseToolStepPathSpec,
|
|
60
|
+
Boolean,
|
|
61
|
+
BranchStep,
|
|
62
|
+
Case,
|
|
63
|
+
ContainerKind,
|
|
64
|
+
DeferredStrategyProvenance,
|
|
65
|
+
Description,
|
|
66
|
+
DescriptionFacet,
|
|
67
|
+
DocumentationSpec,
|
|
68
|
+
ExternalInputPotential,
|
|
69
|
+
GenesisProvenance,
|
|
70
|
+
Goal,
|
|
71
|
+
InputPotential,
|
|
72
|
+
InputResource,
|
|
73
|
+
InternalInputPotential,
|
|
74
|
+
JsonSchemaObject,
|
|
75
|
+
JsonSchemaObject1,
|
|
76
|
+
JsonSchemaObject2,
|
|
77
|
+
Name,
|
|
78
|
+
NameFacet,
|
|
79
|
+
Natural,
|
|
80
|
+
OutputAddress,
|
|
81
|
+
Provenance,
|
|
82
|
+
ProvenanceFacet,
|
|
83
|
+
ProvenanceKind,
|
|
84
|
+
Resource,
|
|
85
|
+
ResourceId,
|
|
86
|
+
ResourcePointer,
|
|
87
|
+
ResourceType,
|
|
88
|
+
ResourceTypeHandle,
|
|
89
|
+
RoleBindingSpec,
|
|
90
|
+
RoleBindingSpecFacet,
|
|
91
|
+
RoleBindingSpecFacet1,
|
|
92
|
+
RoleName,
|
|
93
|
+
RoleNameArray,
|
|
94
|
+
RoleSpec,
|
|
95
|
+
RoleSpecFacet,
|
|
96
|
+
RoleValue,
|
|
97
|
+
RoleValueByName,
|
|
98
|
+
Step,
|
|
99
|
+
StepArray,
|
|
100
|
+
StepArrayArray,
|
|
101
|
+
StepKind,
|
|
102
|
+
StepKindFacet,
|
|
103
|
+
Strategy,
|
|
104
|
+
StrategyHandle,
|
|
105
|
+
StrategyKind,
|
|
106
|
+
StrategyProvenance,
|
|
107
|
+
StrategyStateInputEntry,
|
|
108
|
+
StrategyStateInputEntryByRoleName,
|
|
109
|
+
StrategyStateInputKind,
|
|
110
|
+
StrategyTrace,
|
|
111
|
+
StrategyTraceHandle,
|
|
112
|
+
ThreadedStrategy,
|
|
113
|
+
ThreadedStrategyState,
|
|
114
|
+
ThreadedStrategyStateFacet,
|
|
115
|
+
ThreadedToolStepPath,
|
|
116
|
+
ThreadedToolStepPathSpec,
|
|
117
|
+
Tool,
|
|
118
|
+
ToolHandle,
|
|
119
|
+
ToolStep,
|
|
120
|
+
ToolStepPath,
|
|
121
|
+
ToolStepPathSlot,
|
|
122
|
+
ToolStepPathSlotFacet,
|
|
123
|
+
TypeRef,
|
|
124
|
+
UnthreadedStrategy,
|
|
125
|
+
UnthreadedStrategyState,
|
|
126
|
+
UnthreadedStrategyStateFacet,
|
|
127
|
+
UnthreadedToolStepPath,
|
|
128
|
+
UnthreadedToolStepPathSpec,
|
|
129
|
+
WhileStep,
|
|
130
|
+
ForStep,
|
|
131
|
+
} from '../generated-src/types/types.js';
|
|
@@ -1,195 +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');
|
|
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
195
|
}
|