@manifesto-ai/codegen 0.1.4 → 0.2.1

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
@@ -1,6 +1,6 @@
1
1
  # @manifesto-ai/codegen
2
2
 
3
- > **Codegen** generates TypeScript types and Zod schemas from a Manifesto DomainSchema through a deterministic plugin pipeline.
3
+ > **Codegen** generates canonical domain facade types from a Manifesto DomainSchema through a deterministic plugin pipeline.
4
4
 
5
5
  ---
6
6
 
@@ -23,7 +23,8 @@ DomainSchema -> CODEGEN -> Generated Files
23
23
 
24
24
  | Responsibility | Description |
25
25
  |----------------|-------------|
26
- | Generate TypeScript types | DomainSchema types -> `export interface` / `export type` |
26
+ | Generate canonical domain facades | DomainSchema -> `<domain>.mel.ts` with `state` / `computed` / `actions` |
27
+ | Generate legacy TS/Zod artifacts | Optional low-level `types.ts` / `base.ts` output |
27
28
  | Generate Zod schemas | DomainSchema types -> Zod validators with type annotations |
28
29
  | Plugin pipeline | Run plugins sequentially with shared artifacts |
29
30
  | Path safety | Validate and normalize output file paths |
@@ -58,7 +59,7 @@ npm install @manifesto-ai/codegen
58
59
  ## Quick Example
59
60
 
60
61
  ```typescript
61
- import { generate, createTsPlugin, createZodPlugin } from "@manifesto-ai/codegen";
62
+ import { generate, createDomainPlugin } from "@manifesto-ai/codegen";
62
63
  import type { DomainSchema } from "@manifesto-ai/core";
63
64
 
64
65
  const schema: DomainSchema = { /* your domain schema */ };
@@ -66,36 +67,52 @@ const schema: DomainSchema = { /* your domain schema */ };
66
67
  const result = await generate({
67
68
  schema,
68
69
  outDir: "./generated",
69
- plugins: [createTsPlugin(), createZodPlugin()],
70
+ sourceId: "src/domain/hello.mel",
71
+ plugins: [createDomainPlugin()],
70
72
  });
71
73
 
72
- // result.files -> [{ path: "types.ts", content: "..." }, { path: "base.ts", content: "..." }]
74
+ // result.files -> [{ path: "src/domain/hello.mel.ts", content: "..." }]
73
75
  // result.diagnostics -> [] (empty = no warnings or errors)
74
76
  ```
75
77
 
76
- This produces two files:
78
+ For bundler-time emission, inject Codegen explicitly into the compiler plugin:
77
79
 
78
- **types.ts** -- TypeScript type definitions:
79
80
  ```typescript
80
- export interface Todo {
81
- completed: boolean;
82
- id: string;
83
- title: string;
84
- }
81
+ import { defineConfig } from "vite";
82
+ import { melPlugin } from "@manifesto-ai/compiler/vite";
83
+ import { createCompilerCodegen } from "@manifesto-ai/codegen";
84
+
85
+ export default defineConfig({
86
+ plugins: [
87
+ melPlugin({
88
+ codegen: createCompilerCodegen(),
89
+ }),
90
+ ],
91
+ });
85
92
  ```
86
93
 
87
- **base.ts** -- Zod schemas with type annotations:
88
- ```typescript
89
- import { z } from "zod";
90
- import type { Todo } from "./types";
94
+ This produces a canonical domain facade:
91
95
 
92
- export const TodoSchema: z.ZodType<Todo> = z.object({
93
- completed: z.boolean(),
94
- id: z.string(),
95
- title: z.string(),
96
- });
96
+ **src/domain/hello.mel.ts**
97
+ ```typescript
98
+ export interface HelloDomain {
99
+ readonly state: {
100
+ counter: number
101
+ hello: string
102
+ }
103
+ readonly computed: {
104
+ canDecrement: boolean
105
+ doubled: number
106
+ }
107
+ readonly actions: {
108
+ decrement: () => void
109
+ increment: () => void
110
+ }
111
+ }
97
112
  ```
98
113
 
114
+ Legacy `createTsPlugin()` and `createZodPlugin()` remain available, but are deprecated in favor of `createDomainPlugin()`.
115
+
99
116
  > See [GUIDE.md](docs/GUIDE.md) for the full tutorial.
100
117
 
101
118
  ---
@@ -107,9 +124,13 @@ export const TodoSchema: z.ZodType<Todo> = z.object({
107
124
  ```typescript
108
125
  // Entry point
109
126
  function generate(options: GenerateOptions): Promise<GenerateResult>;
127
+ function createCompilerCodegen(options?: CompilerCodegenOptions): CompilerCodegenEmitter;
110
128
 
111
129
  // Built-in plugins
130
+ function createDomainPlugin(options?: DomainPluginOptions): CodegenPlugin;
131
+ /** @deprecated */
112
132
  function createTsPlugin(options?: TsPluginOptions): CodegenPlugin;
133
+ /** @deprecated */
113
134
  function createZodPlugin(options?: ZodPluginOptions): CodegenPlugin;
114
135
 
115
136
  // Key types
@@ -127,6 +148,12 @@ type GenerateResult = {
127
148
  diagnostics: Diagnostic[];
128
149
  };
129
150
 
151
+ type CompilerCodegenOptions = {
152
+ outDir?: string; // Default: "."
153
+ plugins?: CodegenPlugin[]; // Default: [createDomainPlugin()]
154
+ stamp?: boolean;
155
+ };
156
+
130
157
  type CodegenPlugin = {
131
158
  name: string;
132
159
  generate(ctx: CodegenContext): CodegenOutput;
@@ -141,7 +168,7 @@ type CodegenPlugin = {
141
168
 
142
169
  ### Plugin Pipeline
143
170
 
144
- Plugins run in array order. Each plugin receives a context containing the schema and artifacts from all previous plugins. The TS plugin publishes type names; the Zod plugin reads them to generate type-annotated schemas.
171
+ Plugins run in array order. Each plugin receives a context containing the schema and artifacts from all previous plugins. The canonical domain plugin is self-contained; the legacy TS plugin publishes type names and the legacy Zod plugin reads them to generate type-annotated schemas.
145
172
 
146
173
  ### Artifacts
147
174
 
@@ -169,6 +196,7 @@ Same DomainSchema always produces byte-identical output files. Fields and types
169
196
  ## When to Use Codegen
170
197
 
171
198
  Use Codegen when:
199
+ - You want a canonical `<domain>.mel.ts` facade for `createManifesto<T>()`
172
200
  - You want type-safe TypeScript interfaces from your DomainSchema
173
201
  - You want Zod runtime validators that match your schema types
174
202
  - You need deterministic, reproducible code generation in CI
@@ -0,0 +1,15 @@
1
+ import type { DomainSchema } from "@manifesto-ai/core";
2
+ import type { CodegenPlugin, GenerateResult } from "./types.js";
3
+ export interface CompilerCodegenInput {
4
+ readonly schema: DomainSchema;
5
+ readonly sourceId: string;
6
+ }
7
+ export interface CompilerCodegenOptions {
8
+ readonly outDir?: string;
9
+ readonly plugins?: readonly CodegenPlugin[];
10
+ readonly stamp?: boolean;
11
+ }
12
+ export interface CompilerCodegenEmitter {
13
+ (input: CompilerCodegenInput): Promise<GenerateResult>;
14
+ }
15
+ export declare function createCompilerCodegen(options?: CompilerCodegenOptions): CompilerCodegenEmitter;
@@ -0,0 +1,12 @@
1
+ export interface HeaderOptions {
2
+ readonly sourceId?: string;
3
+ readonly schemaHash: string;
4
+ readonly stamp?: boolean;
5
+ }
6
+ /**
7
+ * Generate the @generated file header (DET-2, DET-3, DET-4).
8
+ *
9
+ * Default mode: no timestamp (deterministic).
10
+ * With stamp=true: appends ISO 8601 timestamp line.
11
+ */
12
+ export declare function generateHeader(options: HeaderOptions): string;
package/dist/index.d.ts CHANGED
@@ -1,114 +1,11 @@
1
- import { DomainSchema } from '@manifesto-ai/core';
2
-
3
- type Diagnostic = {
4
- readonly level: "warn" | "error";
5
- readonly plugin: string;
6
- readonly message: string;
7
- };
8
- type FilePatch = {
9
- readonly op: "set";
10
- readonly path: string;
11
- readonly content: string;
12
- } | {
13
- readonly op: "delete";
14
- readonly path: string;
15
- };
16
- interface CodegenHelpers {
17
- stableHash(input: unknown): string;
18
- }
19
- interface CodegenContext {
20
- readonly schema: DomainSchema;
21
- readonly sourceId?: string;
22
- readonly outDir: string;
23
- readonly artifacts: Readonly<Record<string, unknown>>;
24
- readonly helpers: CodegenHelpers;
25
- }
26
- interface CodegenOutput {
27
- readonly patches: readonly FilePatch[];
28
- readonly artifacts?: Readonly<Record<string, unknown>>;
29
- readonly diagnostics?: readonly Diagnostic[];
30
- }
31
- interface CodegenPlugin {
32
- readonly name: string;
33
- generate(ctx: CodegenContext): CodegenOutput | Promise<CodegenOutput>;
34
- }
35
- interface GenerateOptions {
36
- readonly schema: DomainSchema;
37
- readonly outDir: string;
38
- readonly plugins: readonly CodegenPlugin[];
39
- readonly sourceId?: string;
40
- readonly stamp?: boolean;
41
- }
42
- interface GenerateResult {
43
- readonly files: ReadonlyArray<{
44
- readonly path: string;
45
- readonly content: string;
46
- }>;
47
- readonly artifacts: Readonly<Record<string, unknown>>;
48
- readonly diagnostics: readonly Diagnostic[];
49
- }
50
-
51
- /**
52
- * Generate typed artifacts from a DomainSchema using plugins.
53
- *
54
- * This is the sole entry point for codegen. It orchestrates:
55
- * - Plugin name uniqueness validation (GEN-2)
56
- * - Sequential plugin execution (GEN-3, GEN-7)
57
- * - FilePatch composition with collision detection
58
- * - Error gating (GEN-5, GEN-8)
59
- * - outDir clean + file flush (GEN-1)
60
- */
61
- declare function generate(opts: GenerateOptions): Promise<GenerateResult>;
62
-
63
- interface TsPluginOptions {
64
- readonly typesFile?: string;
65
- readonly actionsFile?: string;
66
- }
67
- interface TsPluginArtifacts {
68
- readonly typeNames: string[];
69
- readonly typeImportPath: string;
70
- }
71
- declare function createTsPlugin(options?: TsPluginOptions): CodegenPlugin;
72
-
73
- interface ZodPluginOptions {
74
- readonly schemasFile?: string;
75
- }
76
- declare function createZodPlugin(options?: ZodPluginOptions): CodegenPlugin;
77
-
78
- type PathValidationResult = {
79
- valid: true;
80
- normalized: string;
81
- } | {
82
- valid: false;
83
- reason: string;
84
- };
85
- /**
86
- * Validate and normalize a file path per FP-1, FP-2, GEN-6.
87
- *
88
- * - MUST be a POSIX relative path
89
- * - MUST NOT contain `..`, absolute prefixes, drive letters, or null bytes
90
- * - Normalizes backslashes, multiple slashes, and leading `./`
91
- */
92
- declare function validatePath(path: string): PathValidationResult;
93
-
94
- /**
95
- * Deterministic hash function (DET-1).
96
- * Same input always produces the same output.
97
- * Uses Core's canonical form (sorted keys, no undefined) + SHA-256.
98
- */
99
- declare function stableHash(input: unknown): string;
100
-
101
- interface HeaderOptions {
102
- readonly sourceId?: string;
103
- readonly schemaHash: string;
104
- readonly stamp?: boolean;
105
- }
106
- /**
107
- * Generate the @generated file header (DET-2, DET-3, DET-4).
108
- *
109
- * Default mode: no timestamp (deterministic).
110
- * With stamp=true: appends ISO 8601 timestamp line.
111
- */
112
- declare function generateHeader(options: HeaderOptions): string;
113
-
114
- export { type CodegenContext, type CodegenHelpers, type CodegenOutput, type CodegenPlugin, type Diagnostic, type FilePatch, type GenerateOptions, type GenerateResult, type HeaderOptions, type PathValidationResult, type TsPluginArtifacts, type TsPluginOptions, type ZodPluginOptions, createTsPlugin, createZodPlugin, generate, generateHeader, stableHash, validatePath };
1
+ export type { Diagnostic, FilePatch, CodegenPlugin, CodegenContext, CodegenOutput, CodegenHelpers, GenerateOptions, GenerateResult, } from "./types.js";
2
+ export { generate } from "./runner.js";
3
+ export { createCompilerCodegen } from "./compiler-codegen.js";
4
+ export type { CompilerCodegenInput, CompilerCodegenOptions, CompilerCodegenEmitter, } from "./compiler-codegen.js";
5
+ export { createDomainPlugin, createTsPlugin, createZodPlugin, } from "./plugins/index.js";
6
+ export type { DomainPluginOptions, TsPluginOptions, TsPluginArtifacts, ZodPluginOptions, } from "./plugins/index.js";
7
+ export { validatePath } from "./path-safety.js";
8
+ export type { PathValidationResult } from "./path-safety.js";
9
+ export { stableHash } from "./stable-hash.js";
10
+ export { generateHeader } from "./header.js";
11
+ export type { HeaderOptions } from "./header.js";