@metaobjectsdev/codegen-ts 0.7.0-rc.9 → 0.8.0-rc.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/dist/generator.d.ts +9 -0
- package/dist/generator.d.ts.map +1 -1
- package/dist/generator.js.map +1 -1
- package/dist/generators/docs-data-builder.d.ts +16 -0
- package/dist/generators/docs-data-builder.d.ts.map +1 -0
- package/dist/generators/docs-data-builder.js +381 -0
- package/dist/generators/docs-data-builder.js.map +1 -0
- package/dist/generators/docs-data.d.ts +98 -0
- package/dist/generators/docs-data.d.ts.map +1 -0
- package/dist/generators/docs-data.js +43 -0
- package/dist/generators/docs-data.js.map +1 -0
- package/dist/generators/docs-file.d.ts +8 -0
- package/dist/generators/docs-file.d.ts.map +1 -0
- package/dist/generators/docs-file.js +77 -0
- package/dist/generators/docs-file.js.map +1 -0
- package/dist/generators/entity-file.d.ts.map +1 -1
- package/dist/generators/entity-file.js +7 -0
- package/dist/generators/entity-file.js.map +1 -1
- package/dist/generators/index.d.ts +5 -0
- package/dist/generators/index.d.ts.map +1 -1
- package/dist/generators/index.js +4 -0
- package/dist/generators/index.js.map +1 -1
- package/dist/generators/output-prompt-file.d.ts +9 -0
- package/dist/generators/output-prompt-file.d.ts.map +1 -0
- package/dist/generators/output-prompt-file.js +51 -0
- package/dist/generators/output-prompt-file.js.map +1 -0
- package/dist/generators/template-generator.d.ts +41 -0
- package/dist/generators/template-generator.d.ts.map +1 -0
- package/dist/generators/template-generator.js +62 -0
- package/dist/generators/template-generator.js.map +1 -0
- package/dist/index.d.ts +7 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -1
- package/dist/index.js.map +1 -1
- package/dist/instance-artifacts.d.ts +29 -0
- package/dist/instance-artifacts.d.ts.map +1 -0
- package/dist/instance-artifacts.js +57 -0
- package/dist/instance-artifacts.js.map +1 -0
- package/dist/metaobjects-config.d.ts +10 -0
- package/dist/metaobjects-config.d.ts.map +1 -1
- package/dist/metaobjects-config.js +1 -0
- package/dist/metaobjects-config.js.map +1 -1
- package/dist/overwrite-policy.d.ts +39 -2
- package/dist/overwrite-policy.d.ts.map +1 -1
- package/dist/overwrite-policy.js +233 -13
- package/dist/overwrite-policy.js.map +1 -1
- package/dist/render-context.d.ts +4 -1
- package/dist/render-context.d.ts.map +1 -1
- package/dist/render-context.js +1 -0
- package/dist/render-context.js.map +1 -1
- package/dist/render-engine/framework-provider.d.ts +28 -0
- package/dist/render-engine/framework-provider.d.ts.map +1 -0
- package/dist/render-engine/framework-provider.js +104 -0
- package/dist/render-engine/framework-provider.js.map +1 -0
- package/dist/runner.d.ts +15 -1
- package/dist/runner.d.ts.map +1 -1
- package/dist/runner.js +45 -6
- package/dist/runner.js.map +1 -1
- package/dist/templates/docs-file.d.ts +17 -0
- package/dist/templates/docs-file.d.ts.map +1 -0
- package/dist/templates/docs-file.js +37 -0
- package/dist/templates/docs-file.js.map +1 -0
- package/dist/templates/entity-file.d.ts.map +1 -1
- package/dist/templates/entity-file.js +12 -0
- package/dist/templates/entity-file.js.map +1 -1
- package/dist/templates/fr010-field-mapping.d.ts +28 -0
- package/dist/templates/fr010-field-mapping.d.ts.map +1 -0
- package/dist/templates/fr010-field-mapping.js +170 -0
- package/dist/templates/fr010-field-mapping.js.map +1 -0
- package/dist/templates/output-format-spec-emitter.d.ts +4 -0
- package/dist/templates/output-format-spec-emitter.d.ts.map +1 -0
- package/dist/templates/output-format-spec-emitter.js +60 -0
- package/dist/templates/output-format-spec-emitter.js.map +1 -0
- package/dist/templates/output-parser.d.ts.map +1 -1
- package/dist/templates/output-parser.js +69 -4
- package/dist/templates/output-parser.js.map +1 -1
- package/dist/templates/output-prompt.d.ts +10 -0
- package/dist/templates/output-prompt.d.ts.map +1 -0
- package/dist/templates/output-prompt.js +75 -0
- package/dist/templates/output-prompt.js.map +1 -0
- package/dist/templates/recover-schema-emitter.d.ts +8 -0
- package/dist/templates/recover-schema-emitter.d.ts.map +1 -0
- package/dist/templates/recover-schema-emitter.js +64 -0
- package/dist/templates/recover-schema-emitter.js.map +1 -0
- package/package.json +5 -5
- package/src/generator.ts +9 -0
- package/src/generators/docs-data-builder.ts +470 -0
- package/src/generators/docs-data.ts +154 -0
- package/src/generators/docs-file.ts +87 -0
- package/src/generators/entity-file.ts +7 -0
- package/src/generators/index.ts +17 -0
- package/src/generators/output-prompt-file.ts +66 -0
- package/src/generators/template-generator.ts +106 -0
- package/src/index.ts +34 -2
- package/src/instance-artifacts.ts +61 -0
- package/src/metaobjects-config.ts +11 -0
- package/src/overwrite-policy.ts +325 -14
- package/src/render-context.ts +5 -1
- package/src/render-engine/framework-provider.ts +107 -0
- package/src/runner.ts +66 -6
- package/src/templates/docs-file.ts +51 -0
- package/src/templates/entity-file.ts +13 -0
- package/src/templates/fr010-field-mapping.ts +191 -0
- package/src/templates/output-format-spec-emitter.ts +97 -0
- package/src/templates/output-parser.ts +77 -2
- package/src/templates/output-prompt.ts +88 -0
- package/src/templates/recover-schema-emitter.ts +91 -0
- package/templates/docs/entity-page.md.mustache +54 -0
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
// Backward-compatible facade over the rc.12+ template-driven docsFile()
|
|
2
|
+
// implementation. The Markdown structure now lives in
|
|
3
|
+
// `templates/docs/entity-page.md.mustache`; this module preserves the
|
|
4
|
+
// `renderDocsFile()` + `DocsRenderOpts` exports so the per-section unit
|
|
5
|
+
// tests in `test/templates/docs-file.test.ts` keep working without
|
|
6
|
+
// modification.
|
|
7
|
+
//
|
|
8
|
+
// Adopters writing custom docs templates should import the public surface
|
|
9
|
+
// from `@metaobjectsdev/codegen-ts/generators` instead:
|
|
10
|
+
// - `buildEntityDocData(entity, opts)` for the typed data dict
|
|
11
|
+
// - `templateGenerator({ template: "docs/entity-page.md", ... })` for the
|
|
12
|
+
// standard end-to-end generator
|
|
13
|
+
|
|
14
|
+
import type { MetaObject, MetaRoot } from "@metaobjectsdev/metadata";
|
|
15
|
+
import type { Dialect } from "../column-mapper.js";
|
|
16
|
+
import type { ColumnNamingStrategy } from "../metaobjects-config.js";
|
|
17
|
+
import { render } from "@metaobjectsdev/render";
|
|
18
|
+
import { buildEntityDocData } from "../generators/docs-data-builder.js";
|
|
19
|
+
import { frameworkTemplatesProvider } from "../render-engine/framework-provider.js";
|
|
20
|
+
|
|
21
|
+
export interface DocsRenderOpts {
|
|
22
|
+
dialect: Dialect;
|
|
23
|
+
columnNamingStrategy?: ColumnNamingStrategy;
|
|
24
|
+
loadedRoot: MetaRoot;
|
|
25
|
+
/** Names of generators present in the pipeline — drives the "Generated code"
|
|
26
|
+
* section. Always includes "entity-file" implicitly. Recognized names:
|
|
27
|
+
* "queries-file", "routes-file", "routes-file-hono". */
|
|
28
|
+
generatorNames?: ReadonlySet<string>;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Backward-compatible entry point: builds the EntityDocData payload and
|
|
32
|
+
* renders it via the framework template. Byte-identical to the hand-coded
|
|
33
|
+
* rc.11 output (gated by `docs-file-conformance.test.ts`). */
|
|
34
|
+
export function renderDocsFile(entity: MetaObject, opts: DocsRenderOpts): string {
|
|
35
|
+
const data = buildEntityDocData(entity, {
|
|
36
|
+
dialect: opts.dialect,
|
|
37
|
+
...(opts.columnNamingStrategy !== undefined
|
|
38
|
+
? { columnNamingStrategy: opts.columnNamingStrategy }
|
|
39
|
+
: {}),
|
|
40
|
+
loadedRoot: opts.loadedRoot,
|
|
41
|
+
...(opts.generatorNames !== undefined
|
|
42
|
+
? { generatorNames: opts.generatorNames }
|
|
43
|
+
: {}),
|
|
44
|
+
});
|
|
45
|
+
return render({
|
|
46
|
+
ref: "docs/entity-page.md",
|
|
47
|
+
payload: data,
|
|
48
|
+
provider: frameworkTemplatesProvider,
|
|
49
|
+
format: "markdown",
|
|
50
|
+
});
|
|
51
|
+
}
|
|
@@ -20,6 +20,7 @@ import { isProjection } from "../projection/projection-detector.js";
|
|
|
20
20
|
import { renderProjectionDecl } from "./projection-decl.js";
|
|
21
21
|
import { hasWritableRdbSource } from "../source-detect.js";
|
|
22
22
|
import { renderValueObjectFile } from "./value-object-file.js";
|
|
23
|
+
import { isAbstract } from "../instance-artifacts.js";
|
|
23
24
|
|
|
24
25
|
/**
|
|
25
26
|
* Render-time options for the entity-file composer.
|
|
@@ -43,6 +44,18 @@ export function renderEntityFile(
|
|
|
43
44
|
): string {
|
|
44
45
|
const allowlists = opts?.allowlists ?? true;
|
|
45
46
|
|
|
47
|
+
// --- Abstract path (shape only) ---
|
|
48
|
+
// An abstract entity contributes shape via inheritance only — it must NEVER
|
|
49
|
+
// produce a Drizzle table / migration footprint / filter allowlist, even when
|
|
50
|
+
// it carries a source.rdb child. This is the cross-port invariant (abstract →
|
|
51
|
+
// no instance/write artifacts, including CREATE TABLE). It still emits its
|
|
52
|
+
// value-object shape (interface + Zod) so subclasses/consumers can reference
|
|
53
|
+
// it. The entity-file generator suppresses this entirely when
|
|
54
|
+
// emitAbstractShapes is off; here we only guarantee "shape, never table".
|
|
55
|
+
if (isAbstract(entity)) {
|
|
56
|
+
return renderValueObjectFile(entity);
|
|
57
|
+
}
|
|
58
|
+
|
|
46
59
|
// --- Projection path (read-only: view-backed entity with no table source) ---
|
|
47
60
|
// Projections intentionally get the z.enum() validator but NOT a named enum
|
|
48
61
|
// type alias — emitting aliases here is a deliberate v1 scope decision.
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
// server/typescript/packages/codegen-ts/src/templates/fr010-field-mapping.ts
|
|
2
|
+
//
|
|
3
|
+
// Shared field-kind mapping for the FR-010 codegen emitters (recover-schema-emitter +
|
|
4
|
+
// output-format-spec-emitter). Maps a metadata field subtype onto the render engine's
|
|
5
|
+
// FieldKind member, the idiomatic nullable TS type used by the recover mirror interface,
|
|
6
|
+
// and the RecoverMap accessor that reads it from the forgiving outcome map.
|
|
7
|
+
//
|
|
8
|
+
// Mirrors the C# Fr010FieldMapping (adapted to TS syntax + the `| null` nullable mirror).
|
|
9
|
+
// Bounded scope (parity with Java/Kotlin/C#): scalar / enum / scalar-array. Nested object +
|
|
10
|
+
// array-of-enum are deferred.
|
|
11
|
+
|
|
12
|
+
import {
|
|
13
|
+
type MetaData,
|
|
14
|
+
TYPE_FIELD,
|
|
15
|
+
FIELD_SUBTYPE_STRING,
|
|
16
|
+
FIELD_SUBTYPE_CLASS,
|
|
17
|
+
FIELD_SUBTYPE_DATE,
|
|
18
|
+
FIELD_SUBTYPE_TIME,
|
|
19
|
+
FIELD_SUBTYPE_TIMESTAMP,
|
|
20
|
+
FIELD_SUBTYPE_INT,
|
|
21
|
+
FIELD_SUBTYPE_SHORT,
|
|
22
|
+
FIELD_SUBTYPE_BYTE,
|
|
23
|
+
FIELD_SUBTYPE_LONG,
|
|
24
|
+
FIELD_SUBTYPE_CURRENCY,
|
|
25
|
+
FIELD_SUBTYPE_DOUBLE,
|
|
26
|
+
FIELD_SUBTYPE_FLOAT,
|
|
27
|
+
FIELD_SUBTYPE_DECIMAL,
|
|
28
|
+
FIELD_SUBTYPE_BOOLEAN,
|
|
29
|
+
FIELD_SUBTYPE_ENUM,
|
|
30
|
+
FIELD_SUBTYPE_OBJECT,
|
|
31
|
+
FIELD_ATTR_REQUIRED,
|
|
32
|
+
FIELD_ATTR_VALUES,
|
|
33
|
+
} from "@metaobjectsdev/metadata";
|
|
34
|
+
|
|
35
|
+
/** The render-engine FieldKind member name for a scalar field subtype, or null if non-scalar. */
|
|
36
|
+
export function scalarKind(subType: string): string | null {
|
|
37
|
+
switch (subType) {
|
|
38
|
+
case FIELD_SUBTYPE_STRING:
|
|
39
|
+
case FIELD_SUBTYPE_CLASS:
|
|
40
|
+
case FIELD_SUBTYPE_DATE:
|
|
41
|
+
case FIELD_SUBTYPE_TIME:
|
|
42
|
+
case FIELD_SUBTYPE_TIMESTAMP:
|
|
43
|
+
return "STRING";
|
|
44
|
+
case FIELD_SUBTYPE_INT:
|
|
45
|
+
case FIELD_SUBTYPE_SHORT:
|
|
46
|
+
case FIELD_SUBTYPE_BYTE:
|
|
47
|
+
return "INT";
|
|
48
|
+
case FIELD_SUBTYPE_LONG:
|
|
49
|
+
case FIELD_SUBTYPE_CURRENCY:
|
|
50
|
+
return "LONG";
|
|
51
|
+
case FIELD_SUBTYPE_DOUBLE:
|
|
52
|
+
case FIELD_SUBTYPE_FLOAT:
|
|
53
|
+
case FIELD_SUBTYPE_DECIMAL:
|
|
54
|
+
return "DOUBLE";
|
|
55
|
+
case FIELD_SUBTYPE_BOOLEAN:
|
|
56
|
+
return "BOOLEAN";
|
|
57
|
+
default:
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** The field children of a payload value-object, in declaration order. */
|
|
63
|
+
export function fields(vo: MetaData): MetaData[] {
|
|
64
|
+
return vo.children().filter((c) => c.type === TYPE_FIELD);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** isArray is a native (reserved) property on MetaData, not an attr. */
|
|
68
|
+
export function isArray(field: MetaData): boolean {
|
|
69
|
+
return field.isArray === true;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** True iff the field's @required is explicitly true (or the string "true"). */
|
|
73
|
+
export function isRequired(field: MetaData): boolean {
|
|
74
|
+
const v = field.ownAttr(FIELD_ATTR_REQUIRED);
|
|
75
|
+
if (v === true) return true;
|
|
76
|
+
return typeof v === "string" && v.toLowerCase() === "true";
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** The string members of an enum field's @values attr (empty when absent). */
|
|
80
|
+
export function enumValues(field: MetaData): string[] {
|
|
81
|
+
const v = field.ownAttr(FIELD_ATTR_VALUES);
|
|
82
|
+
if (Array.isArray(v)) return v.map((e) => String(e));
|
|
83
|
+
return [];
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** The nullable TS type for a field in the recover mirror interface. */
|
|
87
|
+
export function mirrorType(field: MetaData): string {
|
|
88
|
+
// Matches asStringList's `(string | null)[] | null` return — a recovered array
|
|
89
|
+
// can contain null elements where individual items were lost.
|
|
90
|
+
if (isArray(field)) return "(string | null)[] | null";
|
|
91
|
+
if (field.subType === FIELD_SUBTYPE_OBJECT) return "unknown"; // nested deferred
|
|
92
|
+
if (field.subType === FIELD_SUBTYPE_ENUM) return "string | null"; // enum is string-backed
|
|
93
|
+
switch (scalarKind(field.subType)) {
|
|
94
|
+
case "INT":
|
|
95
|
+
case "LONG":
|
|
96
|
+
case "DOUBLE":
|
|
97
|
+
return "number | null";
|
|
98
|
+
case "BOOLEAN":
|
|
99
|
+
return "boolean | null";
|
|
100
|
+
default:
|
|
101
|
+
return "string | null";
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* The RecoverMap.as* helper name that reads this field from the forgiving map, or null
|
|
107
|
+
* for a nested object (which emits a null literal, not a helper call). Single source of
|
|
108
|
+
* truth for the per-field dispatch — both recoverMapCall and recoverMapHelpersUsed use it.
|
|
109
|
+
*/
|
|
110
|
+
function recoverMapHelper(field: MetaData): string | null {
|
|
111
|
+
if (isArray(field)) return "asStringList";
|
|
112
|
+
if (field.subType === FIELD_SUBTYPE_OBJECT) return null; // null literal, no helper
|
|
113
|
+
if (field.subType === FIELD_SUBTYPE_ENUM) return "asString";
|
|
114
|
+
switch (scalarKind(field.subType)) {
|
|
115
|
+
case "INT":
|
|
116
|
+
return "asInt";
|
|
117
|
+
case "LONG":
|
|
118
|
+
return "asLong";
|
|
119
|
+
case "DOUBLE":
|
|
120
|
+
return "asDouble";
|
|
121
|
+
case "BOOLEAN":
|
|
122
|
+
return "asBool";
|
|
123
|
+
default:
|
|
124
|
+
return "asString";
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** The RecoverMap.as* helper name + call that reads this field from the forgiving map `d`. */
|
|
129
|
+
export function recoverMapCall(field: MetaData): string {
|
|
130
|
+
const helper = recoverMapHelper(field);
|
|
131
|
+
if (helper === null) return "null /* FR-010: nested recover deferred */";
|
|
132
|
+
return `${helper}(d, ${jsonStringLiteral(field.name)})`;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** Distinct RecoverMap helper names used across a value-object's fields (for the import). */
|
|
136
|
+
export function recoverMapHelpersUsed(vo: MetaData): string[] {
|
|
137
|
+
const used = new Set<string>();
|
|
138
|
+
for (const f of fields(vo)) {
|
|
139
|
+
const helper = recoverMapHelper(f);
|
|
140
|
+
if (helper !== null) used.add(helper);
|
|
141
|
+
}
|
|
142
|
+
return [...used].sort();
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** A TS double-quoted string literal for `value`, with the load-bearing chars escaped. */
|
|
146
|
+
export function jsonStringLiteral(value: string): string {
|
|
147
|
+
let out = '"';
|
|
148
|
+
for (const ch of value) {
|
|
149
|
+
switch (ch) {
|
|
150
|
+
case "\\":
|
|
151
|
+
out += "\\\\";
|
|
152
|
+
break;
|
|
153
|
+
case '"':
|
|
154
|
+
out += '\\"';
|
|
155
|
+
break;
|
|
156
|
+
case "\t":
|
|
157
|
+
out += "\\t";
|
|
158
|
+
break;
|
|
159
|
+
case "\n":
|
|
160
|
+
out += "\\n";
|
|
161
|
+
break;
|
|
162
|
+
case "\r":
|
|
163
|
+
out += "\\r";
|
|
164
|
+
break;
|
|
165
|
+
default:
|
|
166
|
+
out += ch;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
return out + '"';
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** A TS array literal `["a", "b"]` for the given members. */
|
|
173
|
+
export function stringArrayLiteral(values: readonly string[]): string {
|
|
174
|
+
return "[" + values.map((v) => jsonStringLiteral(v)).join(", ") + "]";
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* A TS object literal `{ "k": "v", … }` for a properties-shaped attr (e.g. @enumAlias /
|
|
179
|
+
* @enumDoc), or "null" when absent/empty. Null values are dropped; keys are sorted
|
|
180
|
+
* ordinally for deterministic output (matches the canonical-serializer properties sort).
|
|
181
|
+
*/
|
|
182
|
+
export function propertiesMapLiteral(attr: unknown): string {
|
|
183
|
+
if (attr == null || typeof attr !== "object" || Array.isArray(attr)) return "null";
|
|
184
|
+
const d = attr as Record<string, unknown>;
|
|
185
|
+
const entries = Object.keys(d)
|
|
186
|
+
.filter((k) => d[k] != null)
|
|
187
|
+
.sort()
|
|
188
|
+
.map((k) => `${jsonStringLiteral(k)}: ${jsonStringLiteral(String(d[k]))}`);
|
|
189
|
+
if (entries.length === 0) return "null";
|
|
190
|
+
return "{ " + entries.join(", ") + " }";
|
|
191
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
// server/typescript/packages/codegen-ts/src/templates/output-format-spec-emitter.ts
|
|
2
|
+
//
|
|
3
|
+
// Turns a payload value-object + its template.output node into a TS source literal for an
|
|
4
|
+
// OutputFormatSpec — the artifact-1 prompt descriptor used by the FR-010 output-prompt codegen.
|
|
5
|
+
//
|
|
6
|
+
// Emits an `{ format, rootName, style, fields: [ … ] } satisfies OutputFormatSpec` literal.
|
|
7
|
+
// Mirrors the C# OutputFormatSpecEmitter (adapted to TS syntax). Field-kind mapping is shared
|
|
8
|
+
// via fr010-field-mapping. Bounded scope: scalar / enum. Nested object → FieldKind.OBJECT placeholder.
|
|
9
|
+
|
|
10
|
+
import {
|
|
11
|
+
type MetaData,
|
|
12
|
+
FIELD_SUBTYPE_ENUM,
|
|
13
|
+
FIELD_SUBTYPE_OBJECT,
|
|
14
|
+
FIELD_ATTR_EXAMPLE,
|
|
15
|
+
FIELD_ATTR_INSTRUCTION,
|
|
16
|
+
FIELD_ATTR_ENUM_DOC,
|
|
17
|
+
TEMPLATE_ATTR_FORMAT,
|
|
18
|
+
TEMPLATE_ATTR_PROMPT_STYLE,
|
|
19
|
+
PROMPT_STYLE_INLINE,
|
|
20
|
+
PROMPT_STYLE_EXAMPLE_ONLY,
|
|
21
|
+
} from "@metaobjectsdev/metadata";
|
|
22
|
+
import {
|
|
23
|
+
fields,
|
|
24
|
+
isRequired,
|
|
25
|
+
isArray,
|
|
26
|
+
scalarKind,
|
|
27
|
+
enumValues,
|
|
28
|
+
jsonStringLiteral,
|
|
29
|
+
stringArrayLiteral,
|
|
30
|
+
propertiesMapLiteral,
|
|
31
|
+
} from "./fr010-field-mapping.js";
|
|
32
|
+
|
|
33
|
+
/** Emit `{ format, rootName, style, fields: [ … ] } satisfies OutputFormatSpec`. */
|
|
34
|
+
export function specLiteral(vo: MetaData, template: MetaData, rootName: string): string {
|
|
35
|
+
const formatEnum = resolveFormat(template);
|
|
36
|
+
const styleEnum = resolvePromptStyle(template);
|
|
37
|
+
const fieldLits = fields(vo).map(promptFieldLiteral);
|
|
38
|
+
return (
|
|
39
|
+
`{ format: ${formatEnum}, rootName: ${jsonStringLiteral(rootName)}, ` +
|
|
40
|
+
`style: ${styleEnum}, fields: [${fieldLits.join(", ")}] } satisfies OutputFormatSpec`
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function resolveFormat(template: MetaData): string {
|
|
45
|
+
const f = template.ownAttr(TEMPLATE_ATTR_FORMAT);
|
|
46
|
+
return typeof f === "string" && f.toLowerCase() === "xml" ? "Format.XML" : "Format.JSON";
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function resolvePromptStyle(template: MetaData): string {
|
|
50
|
+
switch (template.ownAttr(TEMPLATE_ATTR_PROMPT_STYLE)) {
|
|
51
|
+
case PROMPT_STYLE_INLINE:
|
|
52
|
+
return "PromptStyle.INLINE";
|
|
53
|
+
case PROMPT_STYLE_EXAMPLE_ONLY:
|
|
54
|
+
return "PromptStyle.EXAMPLE_ONLY";
|
|
55
|
+
default:
|
|
56
|
+
return "PromptStyle.GUIDE";
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** A PromptField object literal. Field order matches the PromptField interface. */
|
|
61
|
+
function promptFieldLiteral(field: MetaData): string {
|
|
62
|
+
const name = jsonStringLiteral(field.name);
|
|
63
|
+
const required = isRequired(field);
|
|
64
|
+
const array = isArray(field);
|
|
65
|
+
|
|
66
|
+
if (field.subType === FIELD_SUBTYPE_OBJECT) {
|
|
67
|
+
return (
|
|
68
|
+
`{ name: ${name}, kind: FieldKind.OBJECT, required: ${required}, array: ${array}, ` +
|
|
69
|
+
`enumValues: null, enumDoc: null, example: null, instruction: null, nested: null }` +
|
|
70
|
+
` /* FR-010: nested prompt deferred */`
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const example = optStringAttr(field, FIELD_ATTR_EXAMPLE);
|
|
75
|
+
const instruction = optStringAttr(field, FIELD_ATTR_INSTRUCTION);
|
|
76
|
+
|
|
77
|
+
if (field.subType === FIELD_SUBTYPE_ENUM) {
|
|
78
|
+
const valuesLit = stringArrayLiteral(enumValues(field));
|
|
79
|
+
const enumDocLit = propertiesMapLiteral(field.ownAttr(FIELD_ATTR_ENUM_DOC));
|
|
80
|
+
return (
|
|
81
|
+
`{ name: ${name}, kind: FieldKind.ENUM, required: ${required}, array: ${array}, ` +
|
|
82
|
+
`enumValues: ${valuesLit}, enumDoc: ${enumDocLit}, example: ${example}, ` +
|
|
83
|
+
`instruction: ${instruction}, nested: null }`
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const kind = scalarKind(field.subType) ?? "STRING";
|
|
88
|
+
return (
|
|
89
|
+
`{ name: ${name}, kind: FieldKind.${kind}, required: ${required}, array: ${array}, ` +
|
|
90
|
+
`enumValues: null, enumDoc: null, example: ${example}, instruction: ${instruction}, nested: null }`
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function optStringAttr(field: MetaData, attrName: string): string {
|
|
95
|
+
const v = field.ownAttr(attrName);
|
|
96
|
+
return typeof v === "string" ? jsonStringLiteral(v) : "null";
|
|
97
|
+
}
|
|
@@ -17,7 +17,14 @@ import {
|
|
|
17
17
|
FIELD_SUBTYPE_OBJECT,
|
|
18
18
|
FIELD_ATTR_OBJECT_REF,
|
|
19
19
|
TEMPLATE_ATTR_PAYLOAD_REF,
|
|
20
|
+
TEMPLATE_ATTR_FORMAT,
|
|
20
21
|
} from "@metaobjectsdev/metadata";
|
|
22
|
+
import {
|
|
23
|
+
schemaLiteral,
|
|
24
|
+
mirrorInterface,
|
|
25
|
+
mirrorInitializer,
|
|
26
|
+
} from "./recover-schema-emitter.js";
|
|
27
|
+
import { recoverMapHelpersUsed } from "./fr010-field-mapping.js";
|
|
21
28
|
|
|
22
29
|
const SCALAR_ZOD: Record<string, string> = {
|
|
23
30
|
string: "z.string()",
|
|
@@ -103,9 +110,14 @@ export function renderOutputParser(root: MetaData, templateName: string): string
|
|
|
103
110
|
const parseName = `parse${templateName}`;
|
|
104
111
|
const safeParseName = `safeParse${templateName}`;
|
|
105
112
|
|
|
106
|
-
|
|
113
|
+
// FR-010: emit the tolerant recover() API alongside the strict Zod parser when the
|
|
114
|
+
// template targets json/xml. The @payloadRef already resolved to a value-object above,
|
|
115
|
+
// so a RecoverSchema can always be baked. text-format outputs get no recover.
|
|
116
|
+
const format = (tmpl.ownAttr(TEMPLATE_ATTR_FORMAT) as string | undefined) ?? "text";
|
|
117
|
+
const lc = format.toLowerCase();
|
|
118
|
+
const emitRecover = lc === "json" || lc === "xml";
|
|
107
119
|
|
|
108
|
-
const ${schemaName} = ${schema};
|
|
120
|
+
const strictBody = `const ${schemaName} = ${schema};
|
|
109
121
|
|
|
110
122
|
export type ${dataName} = z.infer<typeof ${schemaName}>;
|
|
111
123
|
export type ${errorName} = z.ZodError;
|
|
@@ -140,4 +152,67 @@ export function ${safeParseName}(
|
|
|
140
152
|
: { success: false, error: result.error };
|
|
141
153
|
}
|
|
142
154
|
`;
|
|
155
|
+
|
|
156
|
+
if (!emitRecover) {
|
|
157
|
+
return `import { z } from "zod";\n\n${strictBody}`;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// ---- FR-010 tolerant recover block (json/xml only) ----
|
|
161
|
+
const recoveredName = `${templateName}Recovered`;
|
|
162
|
+
const recoverFnName = `recover${templateName}`;
|
|
163
|
+
const tryRecoverName = `tryRecover${templateName}`;
|
|
164
|
+
const schemaConstName = `${templateName}RecoverSchema`;
|
|
165
|
+
const schemaLit = schemaLiteral(vo, format, payloadRef);
|
|
166
|
+
const mirrorDecl = mirrorInterface(vo, recoveredName);
|
|
167
|
+
const initializer = mirrorInitializer(vo);
|
|
168
|
+
const mapHelpers = recoverMapHelpersUsed(vo);
|
|
169
|
+
|
|
170
|
+
// Render-package imports the recover block needs. Only pull in the names the emitted
|
|
171
|
+
// source actually references, so the file has no unused imports (tsc noUnusedLocals-safe).
|
|
172
|
+
const renderImports = ["recover", "recoverSchema", "Format"];
|
|
173
|
+
if (schemaLit.includes("scalar(")) renderImports.push("scalar");
|
|
174
|
+
if (schemaLit.includes("enumField(")) renderImports.push("enumField");
|
|
175
|
+
if (schemaLit.includes("FieldKind.")) renderImports.push("FieldKind");
|
|
176
|
+
renderImports.push("type RecoverSchema", "type RecoverOptions", "type RecoveryResult");
|
|
177
|
+
renderImports.push(...mapHelpers);
|
|
178
|
+
|
|
179
|
+
const recoverBody = `/** Baked recover descriptor for the ${templateName} output. */
|
|
180
|
+
const ${schemaConstName}: RecoverSchema = ${schemaLit};
|
|
181
|
+
|
|
182
|
+
${mirrorDecl}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Tolerant best-effort recovery of a dirty LLM response; never throws. Returns a
|
|
186
|
+
* nullable mirror (\`${recoveredName}\`) with fields null where lost/malformed,
|
|
187
|
+
* plus the per-field recovery report.
|
|
188
|
+
*/
|
|
189
|
+
export function ${recoverFnName}(
|
|
190
|
+
text: string,
|
|
191
|
+
opts?: RecoverOptions,
|
|
192
|
+
): RecoveryResult<${recoveredName}> {
|
|
193
|
+
const outcome = recover(text, ${schemaConstName}, opts);
|
|
194
|
+
const d = outcome.data;
|
|
195
|
+
const data: ${recoveredName} = ${initializer};
|
|
196
|
+
return { data, report: outcome.report };
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Recovery as a bool gate: \`true\` when the response was non-empty and no required
|
|
201
|
+
* field was lost. On success, \`result\` carries the recovered mirror + report.
|
|
202
|
+
*/
|
|
203
|
+
export function ${tryRecoverName}(
|
|
204
|
+
text: string,
|
|
205
|
+
): { ok: boolean; result: RecoveryResult<${recoveredName}> } {
|
|
206
|
+
const result = ${recoverFnName}(text);
|
|
207
|
+
const ok = !result.report.isEmpty() && !result.report.hasLostRequired();
|
|
208
|
+
return { ok, result };
|
|
209
|
+
}
|
|
210
|
+
`;
|
|
211
|
+
|
|
212
|
+
return (
|
|
213
|
+
`import { z } from "zod";\n` +
|
|
214
|
+
`import {\n ${renderImports.join(",\n ")},\n} from "@metaobjectsdev/render";\n\n` +
|
|
215
|
+
`${strictBody}\n` +
|
|
216
|
+
`${recoverBody}`
|
|
217
|
+
);
|
|
143
218
|
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
// server/typescript/packages/codegen-ts/src/templates/output-prompt.ts
|
|
2
|
+
//
|
|
3
|
+
// Per-template renderer for the FR-010 artifact-1 output-format prompt fragment
|
|
4
|
+
// ("produce your answer like this"). For each json/xml `template.output` whose
|
|
5
|
+
// @payloadRef resolves to a value-object, emits a `<TemplateName>.prompt.ts` file
|
|
6
|
+
// exporting `render<TemplateName>Format(overrides?)` backed by the render engine's
|
|
7
|
+
// renderOutputFormat(). The baked OutputFormatSpec's rootName is the payload name,
|
|
8
|
+
// so the prompt fragment and the recover() codegen agree on the root name.
|
|
9
|
+
//
|
|
10
|
+
// Mirrors the C# OutputPromptGenerator + OutputFormatSpecEmitter (split into a
|
|
11
|
+
// generator factory + this pure renderer, matching the TS output-parser shape).
|
|
12
|
+
|
|
13
|
+
import {
|
|
14
|
+
type MetaData,
|
|
15
|
+
TYPE_OBJECT,
|
|
16
|
+
TYPE_TEMPLATE,
|
|
17
|
+
TEMPLATE_SUBTYPE_OUTPUT,
|
|
18
|
+
TEMPLATE_ATTR_PAYLOAD_REF,
|
|
19
|
+
TEMPLATE_ATTR_FORMAT,
|
|
20
|
+
} from "@metaobjectsdev/metadata";
|
|
21
|
+
import { specLiteral } from "./output-format-spec-emitter.js";
|
|
22
|
+
|
|
23
|
+
function findObject(root: MetaData, name: string): MetaData | undefined {
|
|
24
|
+
return root.ownChildren().find((c) => c.type === TYPE_OBJECT && c.name === name);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function findTemplate(root: MetaData, name: string): MetaData | undefined {
|
|
28
|
+
return root.ownChildren().find((c) => c.type === TYPE_TEMPLATE && c.name === name);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** True iff the template.output's @format is json or xml (the renderable structured formats). */
|
|
32
|
+
export function templateSupportsPrompt(tmpl: MetaData): boolean {
|
|
33
|
+
const f = ((tmpl.ownAttr(TEMPLATE_ATTR_FORMAT) as string | undefined) ?? "text").toLowerCase();
|
|
34
|
+
return f === "json" || f === "xml";
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Render the full output-prompt file for one json/xml `template.output` node.
|
|
39
|
+
* Throws if the template isn't found, isn't a template.output, isn't json/xml,
|
|
40
|
+
* or its @payloadRef doesn't resolve to an object.value.
|
|
41
|
+
*/
|
|
42
|
+
export function renderOutputPrompt(root: MetaData, templateName: string): string {
|
|
43
|
+
const tmpl = findTemplate(root, templateName);
|
|
44
|
+
if (!tmpl) {
|
|
45
|
+
throw new Error(`template "${templateName}" not found in metadata root`);
|
|
46
|
+
}
|
|
47
|
+
if (tmpl.subType !== TEMPLATE_SUBTYPE_OUTPUT) {
|
|
48
|
+
throw new Error(`template "${templateName}" is not a template.output (got subtype "${tmpl.subType}")`);
|
|
49
|
+
}
|
|
50
|
+
if (!templateSupportsPrompt(tmpl)) {
|
|
51
|
+
throw new Error(`template "${templateName}" @format is not json/xml — no prompt fragment`);
|
|
52
|
+
}
|
|
53
|
+
const payloadRef = tmpl.ownAttr(TEMPLATE_ATTR_PAYLOAD_REF);
|
|
54
|
+
if (typeof payloadRef !== "string") {
|
|
55
|
+
throw new Error(`template "${templateName}" missing @payloadRef`);
|
|
56
|
+
}
|
|
57
|
+
const vo = findObject(root, payloadRef);
|
|
58
|
+
if (!vo) {
|
|
59
|
+
throw new Error(`template "${templateName}" @payloadRef "${payloadRef}" not found in metadata root`);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// rootName == payload name so the prompt fragment and recover() agree.
|
|
63
|
+
const spec = specLiteral(vo, tmpl, payloadRef);
|
|
64
|
+
const specName = `${templateName}FormatSpec`;
|
|
65
|
+
const fnName = `render${templateName}Format`;
|
|
66
|
+
|
|
67
|
+
return `import {
|
|
68
|
+
renderOutputFormat,
|
|
69
|
+
Format,
|
|
70
|
+
FieldKind,
|
|
71
|
+
PromptStyle,
|
|
72
|
+
type OutputFormatSpec,
|
|
73
|
+
type PromptOverrides,
|
|
74
|
+
} from "@metaobjectsdev/render";
|
|
75
|
+
|
|
76
|
+
const ${specName}: OutputFormatSpec = ${spec};
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* The output-format instruction fragment for the ${templateName} template.output
|
|
80
|
+
* ("produce your answer like this"). Comment-free — guidance lives in prose /
|
|
81
|
+
* inline placeholders / a filled skeleton. Pass \`overrides\` to override the style
|
|
82
|
+
* or per-field example/instruction at render time.
|
|
83
|
+
*/
|
|
84
|
+
export function ${fnName}(overrides?: PromptOverrides): string {
|
|
85
|
+
return renderOutputFormat(${specName}, overrides ?? {});
|
|
86
|
+
}
|
|
87
|
+
`;
|
|
88
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
// server/typescript/packages/codegen-ts/src/templates/recover-schema-emitter.ts
|
|
2
|
+
//
|
|
3
|
+
// Turns a payload value-object into TS source fragments for the FR-010 recover codegen:
|
|
4
|
+
// • schemaLiteral — a `recoverSchema(Format.JSON, "root", [ … ])` baked descriptor
|
|
5
|
+
// built from FieldSpec factories (scalar / enumField).
|
|
6
|
+
// • mirrorInterface — an all-nullable mirror interface `<Payload>Recovered` (each
|
|
7
|
+
// component `T | null`); recover returns this nullable twin rather
|
|
8
|
+
// than the strict payload (same reasoning as the Java/C#/Kotlin ports).
|
|
9
|
+
// • mirrorInitializer — `{ prop: asString(d, "prop"), … }` building the mirror from the
|
|
10
|
+
// forgiving outcome map `d`.
|
|
11
|
+
//
|
|
12
|
+
// Mirrors the C# RecoverSchemaEmitter (adapted to TS syntax + the `| null` nullable mirror).
|
|
13
|
+
// Bounded scope: scalar / enum / scalar-array. Nested object + array-of-enum deferred.
|
|
14
|
+
|
|
15
|
+
import {
|
|
16
|
+
type MetaData,
|
|
17
|
+
FIELD_SUBTYPE_ENUM,
|
|
18
|
+
FIELD_SUBTYPE_OBJECT,
|
|
19
|
+
FIELD_ATTR_ENUM_ALIAS,
|
|
20
|
+
} from "@metaobjectsdev/metadata";
|
|
21
|
+
import {
|
|
22
|
+
fields,
|
|
23
|
+
isRequired,
|
|
24
|
+
isArray,
|
|
25
|
+
scalarKind,
|
|
26
|
+
mirrorType,
|
|
27
|
+
recoverMapCall,
|
|
28
|
+
enumValues,
|
|
29
|
+
jsonStringLiteral,
|
|
30
|
+
stringArrayLiteral,
|
|
31
|
+
propertiesMapLiteral,
|
|
32
|
+
} from "./fr010-field-mapping.js";
|
|
33
|
+
|
|
34
|
+
/** Emit `recoverSchema(Format.X, "rootName", [ … ])`. */
|
|
35
|
+
export function schemaLiteral(vo: MetaData, format: string, rootName: string): string {
|
|
36
|
+
const formatEnum = format.toLowerCase() === "xml" ? "Format.XML" : "Format.JSON";
|
|
37
|
+
const specs = fields(vo).map(fieldSpecLiteral);
|
|
38
|
+
return `recoverSchema(${formatEnum}, ${jsonStringLiteral(rootName)}, [${specs.join(", ")}])`;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function fieldSpecLiteral(field: MetaData): string {
|
|
42
|
+
const name = jsonStringLiteral(field.name);
|
|
43
|
+
const required = isRequired(field);
|
|
44
|
+
|
|
45
|
+
if (field.subType === FIELD_SUBTYPE_ENUM) {
|
|
46
|
+
const valuesLit = stringArrayLiteral(enumValues(field));
|
|
47
|
+
const aliasLit = propertiesMapLiteral(field.ownAttr(FIELD_ATTR_ENUM_ALIAS));
|
|
48
|
+
// enumField() sets array:false; enum-array is a bounded deferral (parity with Java/C#).
|
|
49
|
+
return `enumField(${name}, ${required}, ${valuesLit}, ${aliasLit})`;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
if (field.subType === FIELD_SUBTYPE_OBJECT) {
|
|
53
|
+
// FR-010: nested recover deferred — treat as an opaque required/optional string slot.
|
|
54
|
+
return `scalar(${name}, FieldKind.STRING, ${required}) /* FR-010: nested recover deferred */`;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const kind = scalarKind(field.subType) ?? "STRING";
|
|
58
|
+
// Scalar-array: the scalar() factory only builds singular specs (array:false), so emit a
|
|
59
|
+
// FieldSpec object literal with array:true. Tier-2 win over the Roslyn proof: the emitted
|
|
60
|
+
// recover() actually populates the array at runtime (RecoverMap.asStringList).
|
|
61
|
+
if (isArray(field)) {
|
|
62
|
+
return (
|
|
63
|
+
`{ name: ${name}, kind: FieldKind.${kind}, required: ${required}, array: true, ` +
|
|
64
|
+
`enumValues: null, enumAlias: null, min: null, max: null, nested: null }`
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
return `scalar(${name}, FieldKind.${kind}, ${required})`;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** Emit the all-nullable mirror interface declaration. */
|
|
71
|
+
export function mirrorInterface(vo: MetaData, interfaceName: string): string {
|
|
72
|
+
const base = interfaceName.endsWith("Recovered")
|
|
73
|
+
? interfaceName.slice(0, -"Recovered".length)
|
|
74
|
+
: interfaceName;
|
|
75
|
+
const lines: string[] = [];
|
|
76
|
+
lines.push(
|
|
77
|
+
`/** Best-effort recovered twin of \`${base}\` — every field nullable (null where lost/malformed). */`,
|
|
78
|
+
);
|
|
79
|
+
lines.push(`export interface ${interfaceName} {`);
|
|
80
|
+
for (const f of fields(vo)) {
|
|
81
|
+
lines.push(` ${f.name}: ${mirrorType(f)};`);
|
|
82
|
+
}
|
|
83
|
+
lines.push("}");
|
|
84
|
+
return lines.join("\n");
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** Emit `{ prop: asString(d, "prop"), … }` building the mirror from the forgiving map `d`. */
|
|
88
|
+
export function mirrorInitializer(vo: MetaData): string {
|
|
89
|
+
const assigns = fields(vo).map((f) => `${f.name}: ${recoverMapCall(f)}`);
|
|
90
|
+
return `{ ${assigns.join(", ")} }`;
|
|
91
|
+
}
|