constructa-core 0.0.6 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -6,7 +6,7 @@ This package contains the core runtime responsible for registering, composing, a
6
6
 
7
7
  ## Responsibilities
8
8
 
9
- Planned areas include:
9
+ Current APIs define trusted generator implementations, portable typed definitions, and engine-provided generation context services. Planned areas include:
10
10
 
11
11
  * Generator registry
12
12
  * Generator execution
@@ -15,6 +15,12 @@ Planned areas include:
15
15
  * Generator composition and lifecycle
16
16
  * Structured errors
17
17
 
18
+ ## Generator implementation contract
19
+
20
+ Use `defineGenerator()` for developer-authored executable implementations. An implementation declares a stable lowercase `type`, positive integer `version`, definition validator, optional dependency analysis hook, and `generate({ definition, context })` function. Validation returns schema `ValidationIssue` objects without imposing a validation-library dependency.
21
+
22
+ `GeneratorDefinition<Output>` carries output information only at compile time; emitted definitions remain plain JSON data. Factories should use `createGeneratorDefinition()` so literal fields are preserved and the resulting definition is portable. Implementations receive randomness and child generation through `GenerationContext`; they must not use global randomness or built-in-specific execution switches.
23
+
18
24
  ## Dependency Boundary
19
25
 
20
26
  `constructa-schema` is the only Constructa runtime dependency that `constructa-core` may depend on.
package/dist/index.d.ts CHANGED
@@ -1 +1,44 @@
1
- export {}
1
+ import { GeneratorDefinition, GeneratorDefinition as GeneratorDefinition$1, Infer, ValidationIssue, ValidationIssue as ValidationIssue$1, ValidationPath, ValidationPath as ValidationPath$1 } from "constructa-schema";
2
+ //#region src/index.d.ts
3
+ type RandomSource = {
4
+ next(): number;
5
+ };
6
+ /** Services supplied by the engine. Implementations must not use global randomness. */
7
+ type GenerationContext = {
8
+ readonly random: RandomSource;
9
+ readonly generateChild: <Output>(definition: GeneratorDefinition$1<Output>) => Output;
10
+ };
11
+ type GeneratorDependency = {
12
+ readonly typeId: string;
13
+ readonly path: ValidationPath$1;
14
+ };
15
+ type GeneratorImplementation<Definition extends GeneratorDefinition$1<Output>, Output> = {
16
+ readonly type: string;
17
+ readonly version: number;
18
+ readonly validateDefinition: (definition: unknown) => readonly ValidationIssue$1[];
19
+ readonly analyzeDependencies?: (definition: Definition) => readonly GeneratorDependency[];
20
+ readonly generate: (input: {
21
+ readonly definition: Definition;
22
+ readonly context: GenerationContext;
23
+ }) => Output;
24
+ };
25
+ /**
26
+ * Defines a trusted, developer-authored generator implementation. This API has
27
+ * no dependency on a particular validation library.
28
+ */
29
+ declare function defineGenerator<const Definition extends GeneratorDefinition$1<Output>, Output>(implementation: GeneratorImplementation<Definition, Output>): GeneratorImplementation<Definition, Output>;
30
+ /** Builds a portable definition while preserving literal fields and output inference. */
31
+ declare function createGeneratorDefinition<Output, const Definition extends GeneratorDefinition$1<Output>>(definition: Definition): Definition;
32
+ /**
33
+ * Invokes one validated implementation. Registry lookup and dispatch are added
34
+ * later; this function keeps validation and execution failure normalization in
35
+ * the same shared contract today.
36
+ */
37
+ declare function invokeGeneratorImplementation<Definition extends GeneratorDefinition$1<Output>, Output>(implementation: GeneratorImplementation<Definition, Output>, input: {
38
+ readonly definition: Definition;
39
+ readonly context: GenerationContext;
40
+ readonly path?: ValidationPath$1;
41
+ }): Output;
42
+ //#endregion
43
+ export { GenerationContext, type GeneratorDefinition, GeneratorDependency, GeneratorImplementation, type Infer, RandomSource, type ValidationIssue, type ValidationPath, createGeneratorDefinition, defineGenerator, invokeGeneratorImplementation };
44
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","names":[],"sources":["../src/index.ts"],"mappings":";;KAgBY;EACV;;;KAIU;WACD,QAAQ;WACR,gBAAgB,QACvB,YAAY,sBAAoB,YAC7B;;KAGK;WACD;WACA,MAAM;;KAGL,wBACV,mBAAmB,sBAAoB,SACvC;WAES;WACA;WACA,qBACP,iCACY;WACL,uBACP,YAAY,wBACA;WACL,WAAW;aACT,YAAY;aACZ,SAAS;QACd;;;;;;iBAOQ,sBACR,mBAAmB,sBAAoB,SAC7C,QAEA,gBAAgB,wBAAwB,YAAY,UACnD,wBAAwB,YAAY;;iBAMvB,0BACd,cACM,mBAAmB,sBAAoB,SAC7C,YAAY,aAAa;;;;;;iBAUX,8BACd,mBAAmB,sBAAoB,SACvC,QAEA,gBAAgB,wBAAwB,YAAY,SACpD;WACW,YAAY;WACZ,SAAS;WACT,OAAO;IAEjB"}
package/dist/index.js CHANGED
@@ -0,0 +1,77 @@
1
+ import { ConstructaError, assertGeneratorDefinition, normalizeConstructaError } from "constructa-schema";
2
+ //#region src/index.ts
3
+ /**
4
+ * Defines a trusted, developer-authored generator implementation. This API has
5
+ * no dependency on a particular validation library.
6
+ */
7
+ function defineGenerator(implementation) {
8
+ assertGeneratorImplementation(implementation);
9
+ return implementation;
10
+ }
11
+ /** Builds a portable definition while preserving literal fields and output inference. */
12
+ function createGeneratorDefinition(definition) {
13
+ assertGeneratorDefinition(definition);
14
+ return definition;
15
+ }
16
+ /**
17
+ * Invokes one validated implementation. Registry lookup and dispatch are added
18
+ * later; this function keeps validation and execution failure normalization in
19
+ * the same shared contract today.
20
+ */
21
+ function invokeGeneratorImplementation(implementation, input) {
22
+ const path = input.path ?? [];
23
+ let issues;
24
+ try {
25
+ issues = implementation.validateDefinition(input.definition);
26
+ } catch (cause) {
27
+ throw normalizeConstructaError(cause, {
28
+ kind: "configuration",
29
+ code: "INVALID_CONFIGURATION",
30
+ path,
31
+ message: "Generator definition validation failed."
32
+ });
33
+ }
34
+ if (issues.length > 0) {
35
+ const [issue] = issues;
36
+ if (issue !== void 0) throw new ConstructaError({
37
+ kind: "configuration",
38
+ code: "INVALID_CONFIGURATION",
39
+ path: [...path, ...issue.path],
40
+ message: issue.message,
41
+ details: { issueCode: issue.code }
42
+ });
43
+ throw new ConstructaError({
44
+ kind: "system",
45
+ code: "EXECUTION_FAILED",
46
+ path,
47
+ message: "Generator validation returned an invalid result."
48
+ });
49
+ }
50
+ try {
51
+ return implementation.generate({
52
+ definition: input.definition,
53
+ context: input.context
54
+ });
55
+ } catch (cause) {
56
+ throw normalizeConstructaError(cause, {
57
+ kind: "execution",
58
+ code: "EXECUTION_FAILED",
59
+ path,
60
+ message: "Generator execution failed."
61
+ });
62
+ }
63
+ }
64
+ function assertGeneratorImplementation(implementation) {
65
+ if (!isStableTypeId(implementation.type)) throw new TypeError("generator type must be a stable lowercase identifier");
66
+ if (!Number.isSafeInteger(implementation.version) || implementation.version < 1) throw new TypeError("generator version must be a positive safe integer");
67
+ if (typeof implementation.validateDefinition !== "function") throw new TypeError("validateDefinition must be a function");
68
+ if (typeof implementation.generate !== "function") throw new TypeError("generate must be a function");
69
+ if (implementation.analyzeDependencies !== void 0 && typeof implementation.analyzeDependencies !== "function") throw new TypeError("analyzeDependencies must be a function when present");
70
+ }
71
+ function isStableTypeId(value) {
72
+ return /^[a-z][a-z0-9]*(?:[._-][a-z0-9]+)*$/u.test(value);
73
+ }
74
+ //#endregion
75
+ export { createGeneratorDefinition, defineGenerator, invokeGeneratorImplementation };
76
+
77
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","names":[],"sources":["../src/index.ts"],"sourcesContent":["import {\n assertGeneratorDefinition,\n ConstructaError,\n type GeneratorDefinition,\n normalizeConstructaError,\n type ValidationIssue,\n type ValidationPath,\n} from \"constructa-schema\";\n\nexport type {\n GeneratorDefinition,\n Infer,\n ValidationIssue,\n ValidationPath,\n} from \"constructa-schema\";\n\nexport type RandomSource = {\n next(): number;\n};\n\n/** Services supplied by the engine. Implementations must not use global randomness. */\nexport type GenerationContext = {\n readonly random: RandomSource;\n readonly generateChild: <Output>(\n definition: GeneratorDefinition<Output>,\n ) => Output;\n};\n\nexport type GeneratorDependency = {\n readonly typeId: string;\n readonly path: ValidationPath;\n};\n\nexport type GeneratorImplementation<\n Definition extends GeneratorDefinition<Output>,\n Output,\n> = {\n readonly type: string;\n readonly version: number;\n readonly validateDefinition: (\n definition: unknown,\n ) => readonly ValidationIssue[];\n readonly analyzeDependencies?: (\n definition: Definition,\n ) => readonly GeneratorDependency[];\n readonly generate: (input: {\n readonly definition: Definition;\n readonly context: GenerationContext;\n }) => Output;\n};\n\n/**\n * Defines a trusted, developer-authored generator implementation. This API has\n * no dependency on a particular validation library.\n */\nexport function defineGenerator<\n const Definition extends GeneratorDefinition<Output>,\n Output,\n>(\n implementation: GeneratorImplementation<Definition, Output>,\n): GeneratorImplementation<Definition, Output> {\n assertGeneratorImplementation(implementation);\n return implementation;\n}\n\n/** Builds a portable definition while preserving literal fields and output inference. */\nexport function createGeneratorDefinition<\n Output,\n const Definition extends GeneratorDefinition<Output>,\n>(definition: Definition): Definition {\n assertGeneratorDefinition(definition);\n return definition;\n}\n\n/**\n * Invokes one validated implementation. Registry lookup and dispatch are added\n * later; this function keeps validation and execution failure normalization in\n * the same shared contract today.\n */\nexport function invokeGeneratorImplementation<\n Definition extends GeneratorDefinition<Output>,\n Output,\n>(\n implementation: GeneratorImplementation<Definition, Output>,\n input: {\n readonly definition: Definition;\n readonly context: GenerationContext;\n readonly path?: ValidationPath;\n },\n): Output {\n const path = input.path ?? [];\n let issues: readonly ValidationIssue[];\n\n try {\n issues = implementation.validateDefinition(input.definition);\n } catch (cause) {\n throw normalizeConstructaError(cause, {\n kind: \"configuration\",\n code: \"INVALID_CONFIGURATION\",\n path,\n message: \"Generator definition validation failed.\",\n });\n }\n\n if (issues.length > 0) {\n const [issue] = issues;\n if (issue !== undefined) {\n throw new ConstructaError({\n kind: \"configuration\",\n code: \"INVALID_CONFIGURATION\",\n path: [...path, ...issue.path],\n message: issue.message,\n details: { issueCode: issue.code },\n });\n }\n\n throw new ConstructaError({\n kind: \"system\",\n code: \"EXECUTION_FAILED\",\n path,\n message: \"Generator validation returned an invalid result.\",\n });\n }\n\n try {\n return implementation.generate({\n definition: input.definition,\n context: input.context,\n });\n } catch (cause) {\n throw normalizeConstructaError(cause, {\n kind: \"execution\",\n code: \"EXECUTION_FAILED\",\n path,\n message: \"Generator execution failed.\",\n });\n }\n}\n\nfunction assertGeneratorImplementation(implementation: {\n readonly type: string;\n readonly version: number;\n readonly validateDefinition: unknown;\n readonly analyzeDependencies?: unknown;\n readonly generate: unknown;\n}): void {\n if (!isStableTypeId(implementation.type)) {\n throw new TypeError(\"generator type must be a stable lowercase identifier\");\n }\n if (\n !Number.isSafeInteger(implementation.version) ||\n implementation.version < 1\n ) {\n throw new TypeError(\"generator version must be a positive safe integer\");\n }\n if (typeof implementation.validateDefinition !== \"function\") {\n throw new TypeError(\"validateDefinition must be a function\");\n }\n if (typeof implementation.generate !== \"function\") {\n throw new TypeError(\"generate must be a function\");\n }\n if (\n implementation.analyzeDependencies !== undefined &&\n typeof implementation.analyzeDependencies !== \"function\"\n ) {\n throw new TypeError(\"analyzeDependencies must be a function when present\");\n }\n}\n\nfunction isStableTypeId(value: string): boolean {\n return /^[a-z][a-z0-9]*(?:[._-][a-z0-9]+)*$/u.test(value);\n}\n"],"mappings":";;;;;;AAuDA,SAAgB,gBAId,gBAC6C;CAC7C,8BAA8B,cAAc;CAC5C,OAAO;AACT;;AAGA,SAAgB,0BAGd,YAAoC;CACpC,0BAA0B,UAAU;CACpC,OAAO;AACT;;;;;;AAOA,SAAgB,8BAId,gBACA,OAKQ;CACR,MAAM,OAAO,MAAM,QAAQ,CAAC;CAC5B,IAAI;CAEJ,IAAI;EACF,SAAS,eAAe,mBAAmB,MAAM,UAAU;CAC7D,SAAS,OAAO;EACd,MAAM,yBAAyB,OAAO;GACpC,MAAM;GACN,MAAM;GACN;GACA,SAAS;EACX,CAAC;CACH;CAEA,IAAI,OAAO,SAAS,GAAG;EACrB,MAAM,CAAC,SAAS;EAChB,IAAI,UAAU,KAAA,GACZ,MAAM,IAAI,gBAAgB;GACxB,MAAM;GACN,MAAM;GACN,MAAM,CAAC,GAAG,MAAM,GAAG,MAAM,IAAI;GAC7B,SAAS,MAAM;GACf,SAAS,EAAE,WAAW,MAAM,KAAK;EACnC,CAAC;EAGH,MAAM,IAAI,gBAAgB;GACxB,MAAM;GACN,MAAM;GACN;GACA,SAAS;EACX,CAAC;CACH;CAEA,IAAI;EACF,OAAO,eAAe,SAAS;GAC7B,YAAY,MAAM;GAClB,SAAS,MAAM;EACjB,CAAC;CACH,SAAS,OAAO;EACd,MAAM,yBAAyB,OAAO;GACpC,MAAM;GACN,MAAM;GACN;GACA,SAAS;EACX,CAAC;CACH;AACF;AAEA,SAAS,8BAA8B,gBAM9B;CACP,IAAI,CAAC,eAAe,eAAe,IAAI,GACrC,MAAM,IAAI,UAAU,sDAAsD;CAE5E,IACE,CAAC,OAAO,cAAc,eAAe,OAAO,KAC5C,eAAe,UAAU,GAEzB,MAAM,IAAI,UAAU,mDAAmD;CAEzE,IAAI,OAAO,eAAe,uBAAuB,YAC/C,MAAM,IAAI,UAAU,uCAAuC;CAE7D,IAAI,OAAO,eAAe,aAAa,YACrC,MAAM,IAAI,UAAU,6BAA6B;CAEnD,IACE,eAAe,wBAAwB,KAAA,KACvC,OAAO,eAAe,wBAAwB,YAE9C,MAAM,IAAI,UAAU,qDAAqD;AAE7E;AAEA,SAAS,eAAe,OAAwB;CAC9C,OAAO,uCAAuC,KAAK,KAAK;AAC1D"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "constructa-core",
3
- "version": "0.0.6",
3
+ "version": "0.1.0",
4
4
  "description": "The UI-agnostic Constructa generator execution engine.",
5
5
  "private": false,
6
6
  "license": "MIT",
@@ -26,7 +26,7 @@
26
26
  "access": "public"
27
27
  },
28
28
  "dependencies": {
29
- "constructa-schema": "2.0.0"
29
+ "constructa-schema": "2.2.0"
30
30
  },
31
31
  "devDependencies": {
32
32
  "typescript": "^6.0.3",