@metaobjectsdev/sdk 0.5.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.
Files changed (118) hide show
  1. package/LICENSE +189 -0
  2. package/README.md +32 -0
  3. package/dist/agent-docs/body.d.ts +2 -0
  4. package/dist/agent-docs/body.d.ts.map +1 -0
  5. package/dist/agent-docs/body.js +563 -0
  6. package/dist/agent-docs/body.js.map +1 -0
  7. package/dist/agent-docs/content-hash.d.ts +8 -0
  8. package/dist/agent-docs/content-hash.d.ts.map +1 -0
  9. package/dist/agent-docs/content-hash.js +23 -0
  10. package/dist/agent-docs/content-hash.js.map +1 -0
  11. package/dist/agent-docs/index.d.ts +3 -0
  12. package/dist/agent-docs/index.d.ts.map +1 -0
  13. package/dist/agent-docs/index.js +4 -0
  14. package/dist/agent-docs/index.js.map +1 -0
  15. package/dist/config.d.ts +113 -0
  16. package/dist/config.d.ts.map +1 -0
  17. package/dist/config.js +53 -0
  18. package/dist/config.js.map +1 -0
  19. package/dist/forge-types.d.ts +47 -0
  20. package/dist/forge-types.d.ts.map +1 -0
  21. package/dist/forge-types.js +133 -0
  22. package/dist/forge-types.js.map +1 -0
  23. package/dist/index.d.ts +20 -0
  24. package/dist/index.d.ts.map +1 -0
  25. package/dist/index.js +39 -0
  26. package/dist/index.js.map +1 -0
  27. package/dist/memory.d.ts +30 -0
  28. package/dist/memory.d.ts.map +1 -0
  29. package/dist/memory.js +105 -0
  30. package/dist/memory.js.map +1 -0
  31. package/dist/package.d.ts +65 -0
  32. package/dist/package.d.ts.map +1 -0
  33. package/dist/package.js +105 -0
  34. package/dist/package.js.map +1 -0
  35. package/dist/paths.d.ts +5 -0
  36. package/dist/paths.d.ts.map +1 -0
  37. package/dist/paths.js +26 -0
  38. package/dist/paths.js.map +1 -0
  39. package/dist/records/any.d.ts +467 -0
  40. package/dist/records/any.d.ts.map +1 -0
  41. package/dist/records/any.js +14 -0
  42. package/dist/records/any.js.map +1 -0
  43. package/dist/records/convention.d.ts +90 -0
  44. package/dist/records/convention.d.ts.map +1 -0
  45. package/dist/records/convention.js +9 -0
  46. package/dist/records/convention.js.map +1 -0
  47. package/dist/records/core.d.ts +84 -0
  48. package/dist/records/core.d.ts.map +1 -0
  49. package/dist/records/core.js +47 -0
  50. package/dist/records/core.js.map +1 -0
  51. package/dist/records/decision.d.ts +90 -0
  52. package/dist/records/decision.d.ts.map +1 -0
  53. package/dist/records/decision.js +9 -0
  54. package/dist/records/decision.js.map +1 -0
  55. package/dist/records/failure.d.ts +93 -0
  56. package/dist/records/failure.d.ts.map +1 -0
  57. package/dist/records/failure.js +10 -0
  58. package/dist/records/failure.js.map +1 -0
  59. package/dist/records/glossary.d.ts +111 -0
  60. package/dist/records/glossary.d.ts.map +1 -0
  61. package/dist/records/glossary.js +14 -0
  62. package/dist/records/glossary.js.map +1 -0
  63. package/dist/records/principle.d.ts +99 -0
  64. package/dist/records/principle.d.ts.map +1 -0
  65. package/dist/records/principle.js +12 -0
  66. package/dist/records/principle.js.map +1 -0
  67. package/dist/storage/errors.d.ts +14 -0
  68. package/dist/storage/errors.d.ts.map +1 -0
  69. package/dist/storage/errors.js +27 -0
  70. package/dist/storage/errors.js.map +1 -0
  71. package/dist/storage/index.d.ts +7 -0
  72. package/dist/storage/index.d.ts.map +1 -0
  73. package/dist/storage/index.js +6 -0
  74. package/dist/storage/index.js.map +1 -0
  75. package/dist/storage/lifecycle.d.ts +5 -0
  76. package/dist/storage/lifecycle.d.ts.map +1 -0
  77. package/dist/storage/lifecycle.js +27 -0
  78. package/dist/storage/lifecycle.js.map +1 -0
  79. package/dist/storage/list.d.ts +8 -0
  80. package/dist/storage/list.d.ts.map +1 -0
  81. package/dist/storage/list.js +42 -0
  82. package/dist/storage/list.js.map +1 -0
  83. package/dist/storage/read.d.ts +9 -0
  84. package/dist/storage/read.d.ts.map +1 -0
  85. package/dist/storage/read.js +43 -0
  86. package/dist/storage/read.js.map +1 -0
  87. package/dist/storage/write.d.ts +8 -0
  88. package/dist/storage/write.d.ts.map +1 -0
  89. package/dist/storage/write.js +20 -0
  90. package/dist/storage/write.js.map +1 -0
  91. package/dist/workspace.d.ts +49 -0
  92. package/dist/workspace.d.ts.map +1 -0
  93. package/dist/workspace.js +280 -0
  94. package/dist/workspace.js.map +1 -0
  95. package/package.json +48 -0
  96. package/src/agent-docs/body.ts +562 -0
  97. package/src/agent-docs/content-hash.ts +25 -0
  98. package/src/agent-docs/index.ts +8 -0
  99. package/src/config.ts +69 -0
  100. package/src/forge-types.ts +167 -0
  101. package/src/index.ts +98 -0
  102. package/src/memory.ts +116 -0
  103. package/src/package.ts +120 -0
  104. package/src/paths.ts +30 -0
  105. package/src/records/any.ts +15 -0
  106. package/src/records/convention.ts +10 -0
  107. package/src/records/core.ts +55 -0
  108. package/src/records/decision.ts +10 -0
  109. package/src/records/failure.ts +11 -0
  110. package/src/records/glossary.ts +15 -0
  111. package/src/records/principle.ts +13 -0
  112. package/src/storage/errors.ts +23 -0
  113. package/src/storage/index.ts +10 -0
  114. package/src/storage/lifecycle.ts +38 -0
  115. package/src/storage/list.ts +53 -0
  116. package/src/storage/read.ts +54 -0
  117. package/src/storage/write.ts +32 -0
  118. package/src/workspace.ts +342 -0
@@ -0,0 +1,562 @@
1
+ // Agent reference docs body. Scaffolded into .metaobjects/AGENTS.md and CLAUDE.md by `meta init`.
2
+ export const AGENT_DOCS_BODY = `# Meta Forge — agent reference
3
+
4
+ This file is scaffolded by \`meta init\` and lives alongside your \`metaobjects/\` records. It teaches AI coding assistants (Claude Code, Codex, etc.) how to read and modify MetaObjects metadata correctly. Refresh after CLI updates with \`meta init --refresh-docs\`.
5
+
6
+ ## Five working principles (read first)
7
+
8
+ These shape every interaction with a metaobjects-driven project. Follow them when you author metadata, write hand-coded business logic, or review someone else's work.
9
+
10
+ ### 1. If it's pattern-derivable from metadata, generate it. Never hand-write boilerplate.
11
+
12
+ The metaobjects raison d'être is that anything the metadata fully describes — schemas, FK references, basic CRUD, query helpers, Zod validators, route handlers, RHF rules, form fields — should be produced by codegen, not hand-typed. If you find yourself hand-writing something the metadata already knows about, stop and use the generated artifact.
13
+
14
+ The first version of the trainer website's database layer had hand-written Drizzle schemas, Zod schemas, and CRUD endpoints. Every one of those is now generated. The hand-written code that remains is real business logic (Stripe webhooks, Loops integration, custom auth flows) — things the generator genuinely cannot derive.
15
+
16
+ When you're about to add a new field or entity: edit \`metaobjects/*.json\` and re-run \`meta gen\`. Don't reach for the generated file directly.
17
+
18
+ ### 2. Use the generated constants. Never use magic strings that touch metadata.
19
+
20
+ After \`meta gen\`, each entity file exports a rich metadata-constants block. Each non-dollar-prefixed key is a per-field object that carries everything a consumer might need (name, label, view, html input type, placeholder, RHF validation rules):
21
+
22
+ \`\`\`ts
23
+ export const Subscriber = {
24
+ $entity: "Subscriber", // entity name string
25
+ $table: "subscribers", // SQL table name
26
+ $path: "/subscribers", // REST resource path
27
+
28
+ email: {
29
+ name: "email", // field name string (use for filters, register())
30
+ label: "Email Address", // humanized fallback or @label override
31
+ view: "text", // MetaView subtype
32
+ htmlType: "email", // optional; maps view → HTML <input type=>
33
+ placeholder: "you@example.com", // optional; only when @placeholder is set on the view
34
+ helpText: "We never share this.", // optional; only when @helpText is set
35
+ rules: { // optional; derived from validator children
36
+ required: "Email is required",
37
+ maxLength: { value: 255, message: "Too long" },
38
+ pattern: { value: /.../, message: "Invalid email" },
39
+ },
40
+ },
41
+ firstName: { name: "firstName", label: "First Name", view: "text", htmlType: "text", rules: { required: "First Name is required" } },
42
+ // ...
43
+ } as const;
44
+ \`\`\`
45
+
46
+ **Use them everywhere — in both generated AND hand-written code:**
47
+
48
+ \`\`\`tsx
49
+ // ✗ Don't:
50
+ <input name="email" type="email" placeholder="Email" />
51
+
52
+ // ✓ Do:
53
+ <input
54
+ type={Subscriber.email.htmlType}
55
+ name={Subscriber.email.name}
56
+ placeholder={Subscriber.email.placeholder}
57
+ aria-label={Subscriber.email.label}
58
+ />
59
+ \`\`\`
60
+
61
+ Rename a field in \`metaobjects/\` and re-gen — TypeScript catches every stale reference.
62
+
63
+ **Special case — Drizzle column access:** when you're already inside Drizzle's typed builder, just use the column properties directly. Drizzle's table-const types are themselves derived from metadata, so \`weeks.programId\` is already TS-safe:
64
+
65
+ \`\`\`ts
66
+ // ✓ Use Drizzle's typed accessor directly — no constants needed here:
67
+ db.select().from(weeks).where(eq(weeks.programId, X))
68
+
69
+ // ✗ Don't do this — it's redundant indirection:
70
+ db.select().from(weeks).where(eq(weeks[Week.programId.name], X))
71
+ \`\`\`
72
+
73
+ Use the constants when you need a STRING (filter object keys, registration arguments, REST paths, labels). Use Drizzle properties directly when the type system already does the work.
74
+
75
+ ### 3. Forms: spread \`form.input.<field>\` from useEntityForm. One line per input.
76
+
77
+ For React forms, use \`useEntityForm\` from \`@metaobjectsdev/react\`. It returns the standard React Hook Form surface plus a pre-bound \`.input\` accessor — one entry per field, ready to spread onto an \`<input>\`:
78
+
79
+ \`\`\`tsx
80
+ import { useEntityForm } from '@metaobjectsdev/react';
81
+ import { Subscriber, SubscriberInsertSchema } from './generated/Subscriber';
82
+
83
+ const form = useEntityForm(Subscriber, SubscriberInsertSchema);
84
+
85
+ <label>{Subscriber.email.label}</label>
86
+ <input {...form.input.email} /> // ← type, placeholder, name, rules, aria-label all spread automatically
87
+ \`\`\`
88
+
89
+ For non-\`<input>\` controls (textarea, select), the \`type\` attr is omitted from \`form.input.X\` — pick the right element yourself.
90
+
91
+ The same Zod schema (\`SubscriberInsertSchema\`) validates on the server (in Fastify routes) and on the client (via the resolver). One schema, two surfaces, zero drift.
92
+
93
+ ### 4. Routes: use the generated \`<Entity>.routes.ts\` for stock CRUD. Hand-write only what's custom.
94
+
95
+ \`meta gen\` emits a per-entity routes file that mounts the 5 standard verbs via \`mountCrudRoutes\` from \`@metaobjectsdev/runtime-ts/drizzle-fastify\`. The runtime is plain Drizzle + Zod — no extra ORM.
96
+
97
+ For custom flows (Stripe webhooks, side effects, auth-gated actions), hand-write the route — but import the entity constants + generated Zod schema. The boilerplate (CRUD, validation, 404 mapping, pagination) lives in the helper; your hand-written code is just the business logic.
98
+
99
+ **Auth pattern:** install a plugin-level Fastify \`preHandler\` hook at the top of your route plugin. The hook applies to every route registered after it — both hand-written handlers AND metaobjects-generated routes via the \`routeOptions\` field. Beats sprinkling \`if (!auth(...)) return;\` at the top of every handler.
100
+
101
+ ### 5. Hand-coded code is always available, but coexists with generated code.
102
+
103
+ Generated code does the boilerplate. Hand-coded code does the business logic. They live in the same project, the same package, sometimes the same file. The hand-coded code consumes the generated constants and generated Zod schemas — it never duplicates schema, never hard-codes paths, never declares its own validators that metadata could declare.
104
+
105
+ Concrete pattern from the trainer website:
106
+ - Generated \`Subscriber.routes.ts\` registers GET / GET-by-id / POST / PATCH / DELETE on \`/api/subscribers\`.
107
+ - Hand-written \`apps/api/src/routes/subscribers.ts\` keeps \`POST /subscribe\` — the custom endpoint with the Loops side-effect.
108
+ - Both registered with \`fastify.register()\`. Both validate via \`SubscriberInsertSchema\`. Both use \`Subscriber.email.name\` / etc. Neither knows the other exists.
109
+
110
+ ## Metaobjects metamodel — quick rules
111
+
112
+ The format used by \`metaobjects/*.json\` is **metaobjects metadata**, a cross-language standard. Eight base types:
113
+
114
+ | Type | Purpose |
115
+ |---|---|
116
+ | \`metadata\` | Root document wrapper |
117
+ | \`object\` | An entity (table/record) |
118
+ | \`field\` | A property on an object |
119
+ | \`attr\` | Named scalar/array decoration on any parent |
120
+ | \`validator\` | A validation rule |
121
+ | \`view\` | A UI control kind |
122
+ | \`identity\` | A primary/secondary key |
123
+ | \`relationship\` | An association between objects |
124
+
125
+ ### Two most-violated rules
126
+
127
+ 1. **Attribute uniqueness.** Within a single parent metadata node, all attribute names must be unique. You cannot have two \`attr\` children both named \`alternative\`. For multi-value, use a single \`stringarray\` attr: \`"@alternatives": ["a", "b", "c"]\`.
128
+
129
+ 2. **Inline \`@<name>\` and \`attr\` child are the same thing.** \`"@maxLength": 50\` is shorthand for \`{"attr": {"name": "maxLength", "subType": "int", "value": "50"}}\`. The parser converts inline form into attr children. Don't use both forms for the same attribute name on the same parent.
130
+
131
+ ### Object subtypes (v0.3)
132
+
133
+ - \`base\` — abstract template (no runtime semantics)
134
+ - \`entity\` — persistent record; should have a primary identity
135
+ - \`value\` — value-object; equality by content; must NOT have a primary identity
136
+
137
+ Java-runtime strategies (pojo / map / proxy) belong on \`@javaRuntime\`, not in \`subType\`.
138
+
139
+ ### Reserved structural keys (NOT attributes)
140
+
141
+ \`name\`, \`subType\`, \`package\`, \`extends\`, \`isAbstract\`, \`children\`, \`merge\`, \`value\`.
142
+
143
+ The v0.2 keys (\`super\`, \`overlay\`, \`override\`, \`isInterface\`, \`implements\`) are **gone**. The current parser will reject them. Use:
144
+ - \`extends:\` instead of \`super:\` for the supertype reference
145
+ - \`merge: true\` instead of \`overlay: true\` / \`override: true\` for in-place modification
146
+ - \`@isAbstract: true\` instead of \`isInterface: true\` (multiple inheritance is not supported)
147
+
148
+ ### Package paths and inheritance
149
+
150
+ - Package segments separated by \`::\` — \`acme::common::id\`
151
+ - Relative references in \`extends:\` — \`..::common::id\` means "go up to parent package, descend into \`common::id\`"
152
+ - Cross-file resolution works as long as all files are passed to Loader (or live in the same \`metaobjects/\` directory)
153
+
154
+ ### Two special intercepted attrs (parser-routed)
155
+
156
+ - \`@isArray\` → marks a field as a collection
157
+ - \`@isAbstract\` → marks a node as abstract (inheritable but not instantiable)
158
+
159
+ ## Validators — two layers
160
+
161
+ Validators can attach in two places, and they compose:
162
+
163
+ **Field-level validators** describe what makes the *stored value* valid. They survive across UI, API, batch import, manual SQL — anywhere data enters the system. The generated Zod \`<Entity>InsertSchema\` encodes these.
164
+
165
+ \`\`\`json
166
+ {"field": {"name": "email", "subType": "string",
167
+ "children": [
168
+ {"validator": {"subType": "required"}},
169
+ {"validator": {"subType": "regex", "@pattern": "^[^@]+@[^@]+\\\\.[^@]+$"}}
170
+ ]
171
+ }}
172
+ \`\`\`
173
+
174
+ **View-level validators** describe what makes user *input* valid in a specific UI surface — possibly stricter, possibly with different messages, possibly format-specific. They run client-side in generated forms. They do NOT necessarily reject the stored value if it's already in the DB.
175
+
176
+ \`\`\`json
177
+ {"field": {"name": "phone", "subType": "string",
178
+ "children": [
179
+ {"validator": {"subType": "regex", "@pattern": "^\\\\+?[0-9]+$"}},
180
+ {"view": {"subType": "text-input", "@label": "Phone",
181
+ "children": [
182
+ {"validator": {"subType": "length", "@min": 7, "@max": 20,
183
+ "@message": "Phone must be 7-20 digits"}}
184
+ ]
185
+ }}
186
+ ]
187
+ }}
188
+ \`\`\`
189
+
190
+ Rule of thumb: rules that protect data integrity → field. Rules that improve input UX → view.
191
+
192
+ ## metaobjects.config.ts — generator wiring (project root)
193
+
194
+ \`meta gen\` reads \`metaobjects.config.ts\` at the project root. This is where you declare which generators run and their options. It is TypeScript, type-checked, and imported via \`jiti\` at run time.
195
+
196
+ \`\`\`ts
197
+ import { defineConfig } from "@metaobjectsdev/cli";
198
+ import {
199
+ entityFile, queriesFile, routesFile, /* formFile, */ barrel,
200
+ } from "@metaobjectsdev/codegen-ts/generators";
201
+
202
+ export default defineConfig({
203
+ outDir: "packages/database/src/generated",
204
+ extStyle: "none",
205
+ dbImport: "../index",
206
+ dialect: "sqlite",
207
+ generators: [
208
+ entityFile(),
209
+ queriesFile(),
210
+ routesFile(),
211
+ // formFile(), // opt-in: emits stock React forms per entity
212
+ barrel(),
213
+ ],
214
+ });
215
+ \`\`\`
216
+
217
+ 3rd-party generator example: \`import { tanstackQuery } from "@metaobjectsdev/codegen-ts-tanstack"; // then add tanstackQuery({ ... }) to the generators array\`
218
+
219
+ Filters live on the generator entry: \`routesFile({ filter: e => e.name !== "AuditLog" })\`
220
+
221
+ \`.metaobjects/config.json\` is unchanged — it still holds static project state (schema_version, pending_in_git, confidence_thresholds). Generator wiring belongs in \`metaobjects.config.ts\` so TypeScript can type-check the imports.
222
+
223
+ ## Generated hooks + grids (TanStack)
224
+
225
+ When \`tanstackQuery()\` is in your \`metaobjects.config.ts\`, every entity gets \`<Entity>.hooks.ts\` with a query-key factory + \`useEntity\`, \`useEntities\`, \`useCreate\`, \`useUpdate\`, \`useDelete\` hooks. When \`tanstackGrid()\` is in the config, entities with a \`layout[dataGrid]\` child also get \`<Entity>.columns.tsx\`.
226
+
227
+ \`\`\`tsx
228
+ import { usePrograms, useCreateProgram } from "@your-pkg/database/generated/Program.hooks";
229
+ import { programDefaultColumns, programDefaultGrid } from "@your-pkg/database/generated/Program.columns";
230
+ import { EntityGrid } from "@metaobjectsdev/tanstack";
231
+
232
+ const { data, isLoading } = usePrograms();
233
+ const create = useCreateProgram({ onSuccess: () => navigate("/programs") });
234
+
235
+ <EntityGrid
236
+ columns={programDefaultColumns}
237
+ grid={programDefaultGrid}
238
+ data={data ?? []}
239
+ isLoading={isLoading}
240
+ onRowClick={(row) => navigate(\`/admin/programs/\${row.id}\`)}
241
+ />
242
+ \`\`\`
243
+
244
+ **Provider setup.** Wrap your app with \`<EntityFetcherProvider value={fetcher}>\` (supplies the HTTP fetcher to all generated hooks). For an admin subtree with different auth, wrap a second time inside: \`<EntityFetcherProvider value={adminFetch}>...</EntityFetcherProvider>\` overrides the outer one.
245
+
246
+ **Metadata layer — grid definition:**
247
+
248
+ \`\`\`jsonc
249
+ { "layout": {
250
+ "subType": "dataGrid",
251
+ "name": "default",
252
+ "@pageSize": 25,
253
+ "@defaultSortField": "createdAt",
254
+ "@defaultSortOrder": "desc",
255
+ "@filterable": true,
256
+ "@columns": ["email", "firstName", "subscribed", "createdAt"]
257
+ }}
258
+ \`\`\`
259
+
260
+ The \`@columns\` attr is a flat string array listing fields to display. Per-column rendering comes from each field's own \`view\` subtype (the same one that drives forms); sortability comes from the field's \`@sortable\` attr; width belongs in app CSS. There are no nested per-column children — just \`@columns\`.
261
+
262
+ **Cell renderers.** Field rendering inside grids comes from each field's own \`view\` subtype (the same one that drives forms). Override defaults app-wide with \`<CellRendererProvider value={{ currency: ({ getValue }) => <Money value={getValue()} /> }}>\`.
263
+
264
+ **Per-entity opt-out.** \`@emitTanstack: false\` on an entity skips both hooks and columns.
265
+
266
+ ## Filtering generated lists
267
+
268
+ Mark filterable fields in metadata with \`@filterable: true\`:
269
+
270
+ \`\`\`jsonc
271
+ { "field": { "name": "email", "subType": "string", "@filterable": true } }
272
+ \`\`\`
273
+
274
+ The generated \`useSubscribers(filter)\` hook accepts a typed filter:
275
+
276
+ \`\`\`tsx
277
+ const { data } = useSubscribers({
278
+ email: { like: "%@example.com" },
279
+ subscribed: true,
280
+ sort: "createdAt:desc",
281
+ limit: 25,
282
+ });
283
+ \`\`\`
284
+
285
+ URL sent: \`/subscribers?filter[email][like]=%25@example.com&filter[subscribed]=true&sort=createdAt:desc&limit=25\`
286
+
287
+ **Operators by field subtype:**
288
+ - String: \`eq, ne, in, like, isNull\`
289
+ - Number/date: \`eq, ne, gt, gte, lt, lte, in, isNull\`
290
+ - Boolean: \`eq, isNull\`
291
+
292
+ Illegal combinations like \`useSubscribers({ subscribed: { gte: true } })\` fail to compile (booleans don't support \`gte\`).
293
+
294
+ **Per-grid preset filter** via layout \`@filter\`:
295
+
296
+ \`\`\`jsonc
297
+ { "layout": { "subType": "dataGrid", "name": "active",
298
+ "@filter": { "subscribed": true },
299
+ "@columns": ["email", "firstName", "subscribed"] }}
300
+ \`\`\`
301
+
302
+ Generates \`subscriberActiveFilter\` const consumable in pages. Compose with ad-hoc filters via object spread.
303
+
304
+ ## Projections (read models with joined/aggregated columns)
305
+
306
+ When a list needs computed columns (counts, sums, joined fields), create a **projection** — an entity that extends a base entity but reads from a SQL view:
307
+
308
+ \`\`\`json
309
+ // metaobjects/meta.commerce.json (inline with Program)
310
+ {
311
+ "object": {
312
+ "name": "ProgramSummary",
313
+ "subType": "entity",
314
+ "extends": "Program",
315
+ "children": [
316
+ { "source": { "subType": "dbView", "@name": "v_program_summary" } },
317
+ { "field": { "name": "weekCount", "subType": "int", "children": [
318
+ { "origin": { "subType": "aggregate",
319
+ "@agg": "count", "@of": "Week.id", "@via": "Program.weeks" }}
320
+ ]}},
321
+ { "identity": { "subType": "primary", "@fields": "id" } }
322
+ ]
323
+ }
324
+ }
325
+ \`\`\`
326
+
327
+ \`meta gen\` produces a read-only \`useProgramSummaries(filter)\` hook, a SQL view DDL in the migration, and a read-only GET-only route.
328
+
329
+ **Aggregate vocabulary**: \`count\`, \`sum\`, \`avg\`, \`min\`, \`max\`.
330
+
331
+ **Multi-level via paths** are supported: \`@via: "Program.weeks.workouts"\` builds a 2-level JOIN tree.
332
+
333
+ **For pages that need a full nested tree** (e.g., Program → Weeks → Workouts → Exercises), use 4 entity hooks with Project D's filter syntax for batched lookups (no projection needed — flat hooks + client-side stitching is enough):
334
+
335
+ \`\`\`tsx
336
+ const { data: weeks } = useWeeks({ programId, sort: "weekNumber:asc" });
337
+ const weekIds = weeks?.map((w) => w.id) ?? [];
338
+ const { data: workouts } = useWorkouts(
339
+ weekIds.length ? { weekId: { in: weekIds } } : undefined,
340
+ );
341
+ \`\`\`
342
+
343
+ ## Currency fields
344
+
345
+ Declare a money field with \`subType: "currency"\`:
346
+
347
+ \`\`\`json
348
+ { "field": { "name": "priceCents", "subType": "currency", "@currency": "USD" } }
349
+ \`\`\`
350
+
351
+ Storage stays as integer minor units (cents for USD). The generated \`<Entity>\` constants block carries \`view\`, \`currency\`, \`locale\` so admin grids auto-format prices.
352
+
353
+ **Imports — use sub-paths in browser code:**
354
+
355
+ \`\`\`tsx
356
+ import { formatCurrency } from "@metaobjectsdev/runtime-web";
357
+ import { CurrencyInput } from "@metaobjectsdev/react";
358
+ \`\`\`
359
+
360
+ **Display:**
361
+
362
+ \`\`\`tsx
363
+ <span>{formatCurrency(program.priceCents)}</span> // $15.00
364
+ <span>{formatCurrency(p.amountCents, "EUR", "de-DE")}</span> // 15,00 €
365
+ \`\`\`
366
+
367
+ **Form input:**
368
+
369
+ \`\`\`tsx
370
+ <CurrencyInput value={priceCents} onChange={setPriceCents} currency="USD" />
371
+ \`\`\`
372
+
373
+ User types \`15.99\` → component emits \`1599\` to \`onChange\` on blur. Wire format is always integer cents.
374
+
375
+ **Locale override** via a \`view[currency]\` child:
376
+
377
+ \`\`\`json
378
+ { "field": { "name": "priceCents", "subType": "currency", "@currency": "EUR",
379
+ "children": [{ "view": { "subType": "currency", "@locale": "de-DE" } }]
380
+ }}
381
+ \`\`\`
382
+
383
+ Currency code lives on the field; locale lives on the view.
384
+
385
+ ## Generated artifacts — what \`meta gen\` produces
386
+
387
+ After \`meta gen\`, you get one barrel + per-entity files in your configured \`outDir\` (default \`packages/database/src/generated/\`):
388
+
389
+ | File | What's in it | When to touch by hand |
390
+ |---|---|---|
391
+ | \`<Entity>.ts\` | Drizzle table, relations(), inferred types, Zod insert/update schemas, and the rich \`<Entity>\` constants block (per-field objects with name, label, view, htmlType, rules, etc.) | Never. Regenerate. |
392
+ | \`<Entity>.queries.ts\` | Typed query helpers (\`findUserById\`, \`listUsers\`, \`createUser\`, ...) using prepared statements | Never. Regenerate. |
393
+ | \`<Entity>.routes.ts\` | Fastify CRUD plugin delegating to \`mountCrudRoutes\` from \`@metaobjectsdev/runtime-ts/drizzle-fastify\` (5 verbs, Zod validation, 404/204 mapping, Drizzle-direct under the hood) | Never. Regenerate. |
394
+ | \`<Entity>.form.tsx\` | React form using \`useEntityForm\` + the entity constants. **OPT-IN at project level:** add \`formFile()\` to \`generators\` in \`metaobjects.config.ts\`. Opt out per-entity via \`@emitForm: false\`. | Never. Regenerate. |
395
+ | \`index.ts\` | Barrel re-exporting every entity file | Never. Regenerate. |
396
+
397
+ For business logic the generator doesn't cover, create a SIBLING file: \`<Entity>.extra.ts\` for query/route helpers, or any file you like in your apps directory. Import the constants from the generated \`<Entity>.ts\`.
398
+
399
+ ### Stock route mounting
400
+
401
+ \`\`\`ts
402
+ import { subscriberRoutes } from "@your-pkg/database/generated/Subscriber.routes";
403
+ fastify.register(subscriberRoutes, { prefix: "/api" });
404
+ \`\`\`
405
+
406
+ That mounts: \`GET /api/subscribers\`, \`GET /api/subscribers/:id\`, \`POST /api/subscribers\`, \`PATCH /api/subscribers/:id\`, \`DELETE /api/subscribers/:id\`. Pagination via \`?limit=\` & \`?offset=\`. Validation via the generated Zod schemas. Drizzle calls under the hood.
407
+
408
+ ### Mixing custom routes alongside generated
409
+
410
+ \`\`\`ts
411
+ import { db, subscribers } from "@your-pkg/database";
412
+ import { Subscriber, SubscriberInsertSchema } from "@your-pkg/database/generated/Subscriber";
413
+ import { eq } from "drizzle-orm";
414
+
415
+ fastify.post("/subscribe", async (req, reply) => {
416
+ // Generated Zod schema validates the body — same schema the API route uses.
417
+ const parsed = SubscriberInsertSchema.safeParse(req.body);
418
+ if (!parsed.success) return reply.code(400).send({ issues: parsed.error.issues });
419
+
420
+ // Drizzle's typed accessors are already TS-safe; no need for indirection.
421
+ const existing = await db.select().from(subscribers).where(eq(subscribers.email, parsed.data.email)).get();
422
+ if (existing) return reply.code(409).send({ error: "Already subscribed" });
423
+
424
+ const [row] = await db.insert(subscribers).values(parsed.data).returning();
425
+ // ... your business logic (analytics, Loops/Mailchimp, navigation, etc.)
426
+ return reply.code(201).send(row);
427
+ });
428
+ \`\`\`
429
+
430
+ The fact that every metadata-derived value flows from \`Subscriber\` / \`SubscriberInsertSchema\` / the typed \`subscribers\` table is what makes rename-the-field-in-metadata-and-regen safe.
431
+
432
+ ### Hand-written form using \`useEntityForm\`
433
+
434
+ \`\`\`tsx
435
+ import { useEntityForm } from '@metaobjectsdev/react';
436
+ import { Subscriber, SubscriberInsertSchema, type Subscriber as Row } from './generated/Subscriber';
437
+
438
+ export function SubscribeForm() {
439
+ const form = useEntityForm(Subscriber, SubscriberInsertSchema);
440
+ const { handleSubmit, formState: { errors } } = form;
441
+
442
+ return (
443
+ <form onSubmit={handleSubmit(/* your onSubmit */)} className="your-design-system">
444
+ <label>{Subscriber.email.label}</label>
445
+ <input {...form.input.email} />
446
+ {errors.email && <span>{errors.email.message}</span>}
447
+
448
+ <label>{Subscriber.firstName.label}</label>
449
+ <input {...form.input.firstName} />
450
+ {errors.firstName && <span>{errors.firstName.message}</span>}
451
+
452
+ <button type="submit">Subscribe</button>
453
+ </form>
454
+ );
455
+ }
456
+ \`\`\`
457
+
458
+ Spread \`form.input.<field>\` — it carries name, type, placeholder, rules, aria-label automatically. No magic strings.
459
+
460
+ ## Meta Forge additions
461
+
462
+ ### \`@forge*\` attribute namespace
463
+
464
+ Provenance and confidence concerns expressed as inline attributes on any metadata child. Names use camelCase (no separator).
465
+
466
+ Most common:
467
+ - \`@forgeConfidence\` (double 0..1) — confidence the record is correct
468
+ - \`@forgeSource\` (string) — \`human\` | \`claude\` | \`ts-ast\` | \`drizzle\` | ...
469
+ - \`@forgePrimaryLocation\` (string) — file path for an entity
470
+ - \`@forgeRationale\` (string, decision only) — why this decision
471
+ - \`@forgeAlternatives\` (stringarray, decision only) — alternatives considered
472
+
473
+ Full inventory in \`packages/sdk/FORGE-METADATA.md\`.
474
+
475
+ ### New top-level types
476
+
477
+ Registered by \`@metaobjectsdev/sdk\` into the TypeRegistry:
478
+
479
+ | Type | Purpose |
480
+ |---|---|
481
+ | \`decision\` | Architectural or design decision |
482
+ | \`principle\` | Design principle (advisory/enforced) |
483
+ | \`convention\` | Coding/structural convention |
484
+ | \`glossary\` | Domain-term definition |
485
+ | \`failure\` | Recorded failure mode |
486
+
487
+ These coexist with \`object\` children in the same package files. \`meta gen\` and \`meta migrate\` only consume \`object\`; the descriptive types are context for AI tooling and don't drive codegen.
488
+
489
+ ## File layout
490
+
491
+ \`\`\`
492
+ metaobjects/
493
+ ├── meta.common.json shared base fields/validators (optional)
494
+ ├── meta.<domain>.json your entity packages(s)
495
+ └── _pending/<pkg>.json proposed packages awaiting review
496
+
497
+ .metaobjects/
498
+ ├── config.json static project state
499
+ ├── migrations/ written by meta migrate
500
+ └── .gen-state/ codegen merge base (gitignored)
501
+
502
+ metaobjects.config.ts generator wiring (committed)
503
+ \`\`\`
504
+
505
+ ## Worked example
506
+
507
+ \`\`\`json
508
+ {
509
+ "metadata": {
510
+ "package": "myapp",
511
+ "children": [
512
+ {
513
+ "object": {
514
+ "name": "User",
515
+ "subType": "entity",
516
+ "@forgeConfidence": 0.95,
517
+ "@forgeSource": "human",
518
+ "@forgePrimaryLocation": "src/db/users.schema.ts",
519
+ "children": [
520
+ {"field": {"name": "id", "extends": "..::common::id"}},
521
+ {"field": {"name": "email", "subType": "string",
522
+ "@dbColumn": "email_address",
523
+ "children": [{"validator": {"subType": "required"}}]
524
+ }},
525
+ {"identity": {"name": "pk", "subType": "primary", "@fields": ["id"], "@generation": "increment"}}
526
+ ]
527
+ }
528
+ },
529
+ {
530
+ "decision": {
531
+ "name": "useTanstackQuery",
532
+ "subType": "global",
533
+ "@forgeConfidence": 0.9,
534
+ "@forgeSource": "human",
535
+ "@forgeRationale": "Real-time invalidation matters for live game state.",
536
+ "@forgeAlternatives": ["swr", "redux-toolkit-query"]
537
+ }
538
+ }
539
+ ]
540
+ }
541
+ }
542
+ \`\`\`
543
+
544
+ ## Authoring guidance
545
+
546
+ | Situation | Action |
547
+ |---|---|
548
+ | Adding a field to an existing entity | Edit the \`object\`'s \`children\`; append a \`field\` node, then \`meta gen\` |
549
+ | New entity in an existing domain | Append an \`object\` to the appropriate package file, then \`meta gen\` |
550
+ | Renaming an entity or field | Edit the metadata, regenerate; TS will surface every stale consumer of the constants |
551
+ | New REST resource | Already done — \`meta gen\` produced \`<Entity>.routes.ts\`. Just \`fastify.register(...)\` it |
552
+ | Custom business logic (Stripe webhook, side-effects, auth flows) | Hand-write a route/handler that imports the generated constants + \`om()\` |
553
+ | Architectural choice affecting how entities are built | Add a \`decision\` with \`@forgeRationale\` + \`@forgeAlternatives\` |
554
+ | Coding convention | Add a \`convention\` with \`@forgePatternDescription\` + \`@forgeAppliesTo\` |
555
+ | Domain term | Add a \`glossary\` entry with \`@forgeTerm\` + \`@forgeDefinition\` |
556
+
557
+ ## Deeper references
558
+
559
+ - \`packages/metadata/METAMODEL.md\` — full metamodel reference
560
+ - \`packages/sdk/FORGE-METADATA.md\` — full \`@forge*\` inventory + MetaObjects layout details
561
+ - \`docs/strategy/2026-05-12-v0.3-ai-first-metadata-loading.md\` — current strategy (v0.3 vocab, packages, AI-first loading)
562
+ `;
@@ -0,0 +1,25 @@
1
+ import { createHash } from "node:crypto";
2
+
3
+ export function computeContentHash(body: string): string {
4
+ return createHash("sha256").update(body, "utf8").digest("hex");
5
+ }
6
+
7
+ /** Returns the body with a content-hash HTML comment prepended. */
8
+ export function withContentHash(body: string): string {
9
+ const hash = computeContentHash(body);
10
+ return `<!-- metaobjects-content-hash: ${hash} -->\n${body}`;
11
+ }
12
+
13
+ /** Extract the embedded hash, or undefined if not present. */
14
+ export function extractContentHash(fileBody: string): string | undefined {
15
+ const match = /<!-- metaobjects-content-hash: ([a-f0-9]{64}) -->/.exec(fileBody);
16
+ return match?.[1];
17
+ }
18
+
19
+ /** True iff the file body's hash matches its own content (i.e. unmodified). */
20
+ export function isUnmodified(fileBody: string): boolean {
21
+ const embedded = extractContentHash(fileBody);
22
+ if (embedded === undefined) return false;
23
+ const withoutHash = fileBody.replace(/^<!-- metaobjects-content-hash: [a-f0-9]{64} -->\n/, "");
24
+ return computeContentHash(withoutHash) === embedded;
25
+ }
@@ -0,0 +1,8 @@
1
+ // Public surface for @metaobjectsdev/sdk/agent-docs.
2
+ export { AGENT_DOCS_BODY } from "./body.js";
3
+ export {
4
+ computeContentHash,
5
+ withContentHash,
6
+ extractContentHash,
7
+ isUnmodified,
8
+ } from "./content-hash.js";
package/src/config.ts ADDED
@@ -0,0 +1,69 @@
1
+ import { z } from "zod";
2
+ import { readFile, writeFile } from "node:fs/promises";
3
+ import { join } from "node:path";
4
+
5
+ const DialectEnum = z.enum(["sqlite", "postgres"]);
6
+
7
+ const OnAmbiguousEnum = z.enum(["abort", "rename", "drop-add"]);
8
+
9
+ const AllowTokenEnum = z.enum([
10
+ "drop-column",
11
+ "drop-table",
12
+ "type-change",
13
+ "drop-index",
14
+ "drop-fk",
15
+ "nullable-to-not-null",
16
+ ]);
17
+
18
+ const MigrateBlock = z.object({
19
+ outDir: z.string(),
20
+ databaseUrl: z.string(),
21
+ dialect: DialectEnum,
22
+ onAmbiguous: OnAmbiguousEnum,
23
+ allow: z.array(AllowTokenEnum),
24
+ }).partial();
25
+
26
+ export const ConfigSchema = z.object({
27
+ schema_version: z.literal(1),
28
+ pending_in_git: z.boolean().default(true),
29
+ confidence_thresholds: z
30
+ .object({
31
+ pending_promote: z.number().min(0).max(1).default(0.8),
32
+ drift_warn: z.number().min(0).max(1).default(0.7),
33
+ })
34
+ .default({}),
35
+ sources: z
36
+ .array(
37
+ z.union([
38
+ z.object({ kind: z.literal("path"), path: z.string() }),
39
+ z.object({ kind: z.literal("package"), package: z.string() }),
40
+ ]),
41
+ )
42
+ .default([]),
43
+ extract: z
44
+ .object({
45
+ metaignore: z.string().optional(),
46
+ })
47
+ .default({}),
48
+ migrate: MigrateBlock.optional(),
49
+ });
50
+
51
+ export type Config = z.infer<typeof ConfigSchema>;
52
+
53
+ export const DEFAULT_CONFIG: Config = ConfigSchema.parse({ schema_version: 1 });
54
+
55
+ const CONFIG_FILE = "config.json";
56
+
57
+ export async function loadConfig(metaRoot: string): Promise<Config> {
58
+ const raw = await readFile(join(metaRoot, CONFIG_FILE), "utf8");
59
+ return ConfigSchema.parse(JSON.parse(raw));
60
+ }
61
+
62
+ export async function saveConfig(metaRoot: string, config: Config): Promise<void> {
63
+ ConfigSchema.parse(config); // validate before writing
64
+ await writeFile(
65
+ join(metaRoot, CONFIG_FILE),
66
+ JSON.stringify(config, null, 2) + "\n",
67
+ "utf8",
68
+ );
69
+ }