@intelligo-dev/cli 1.0.0-beta.1 → 1.0.0-beta.14

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 (223) hide show
  1. package/NOTICE +6 -0
  2. package/README.md +102 -0
  3. package/dist/bin.js +122 -37
  4. package/dist/bin.js.map +1 -1
  5. package/dist/commands/add.d.ts +26 -1
  6. package/dist/commands/add.d.ts.map +1 -1
  7. package/dist/commands/add.js +70 -4
  8. package/dist/commands/add.js.map +1 -1
  9. package/dist/commands/create-flow.d.ts +37 -0
  10. package/dist/commands/create-flow.d.ts.map +1 -0
  11. package/dist/commands/create-flow.js +212 -0
  12. package/dist/commands/create-flow.js.map +1 -0
  13. package/dist/commands/create.d.ts +27 -6
  14. package/dist/commands/create.d.ts.map +1 -1
  15. package/dist/commands/create.js +53 -13
  16. package/dist/commands/create.js.map +1 -1
  17. package/dist/commands/doctor.d.ts +14 -3
  18. package/dist/commands/doctor.d.ts.map +1 -1
  19. package/dist/commands/doctor.js +295 -37
  20. package/dist/commands/doctor.js.map +1 -1
  21. package/dist/commands/migrate-check.d.ts +73 -1
  22. package/dist/commands/migrate-check.d.ts.map +1 -1
  23. package/dist/commands/migrate-check.js +130 -5
  24. package/dist/commands/migrate-check.js.map +1 -1
  25. package/dist/commands/migrate.d.ts +97 -0
  26. package/dist/commands/migrate.d.ts.map +1 -0
  27. package/dist/commands/migrate.js +141 -0
  28. package/dist/commands/migrate.js.map +1 -0
  29. package/dist/commands/sync-messages.d.ts +37 -0
  30. package/dist/commands/sync-messages.d.ts.map +1 -0
  31. package/dist/commands/sync-messages.js +88 -0
  32. package/dist/commands/sync-messages.js.map +1 -0
  33. package/dist/commands/sync-scaffold.d.ts +36 -0
  34. package/dist/commands/sync-scaffold.d.ts.map +1 -0
  35. package/dist/commands/sync-scaffold.js +53 -0
  36. package/dist/commands/sync-scaffold.js.map +1 -0
  37. package/dist/commands/sync.d.ts +103 -0
  38. package/dist/commands/sync.d.ts.map +1 -0
  39. package/dist/commands/sync.js +361 -0
  40. package/dist/commands/sync.js.map +1 -0
  41. package/dist/commands/upgrade-check.d.ts +5 -5
  42. package/dist/commands/upgrade-check.d.ts.map +1 -1
  43. package/dist/commands/upgrade-check.js +22 -6
  44. package/dist/commands/upgrade-check.js.map +1 -1
  45. package/dist/env-files.d.ts +12 -0
  46. package/dist/env-files.d.ts.map +1 -0
  47. package/dist/env-files.js +29 -0
  48. package/dist/env-files.js.map +1 -0
  49. package/dist/index.d.ts +9 -0
  50. package/dist/index.d.ts.map +1 -1
  51. package/dist/index.js +9 -0
  52. package/dist/index.js.map +1 -1
  53. package/dist/manifest.d.ts +36 -15
  54. package/dist/manifest.d.ts.map +1 -1
  55. package/dist/manifest.js +32 -16
  56. package/dist/manifest.js.map +1 -1
  57. package/dist/migrations.d.ts +3 -6
  58. package/dist/migrations.d.ts.map +1 -1
  59. package/dist/migrations.js +3 -6
  60. package/dist/migrations.js.map +1 -1
  61. package/dist/model-catalogue.d.ts +14 -0
  62. package/dist/model-catalogue.d.ts.map +1 -0
  63. package/dist/model-catalogue.js +34 -0
  64. package/dist/model-catalogue.js.map +1 -0
  65. package/dist/module-exports.d.ts +63 -0
  66. package/dist/module-exports.d.ts.map +1 -0
  67. package/dist/module-exports.js +231 -0
  68. package/dist/module-exports.js.map +1 -0
  69. package/dist/registry-bundle.d.ts +43 -0
  70. package/dist/registry-bundle.d.ts.map +1 -0
  71. package/dist/registry-bundle.js +74 -0
  72. package/dist/registry-bundle.js.map +1 -0
  73. package/dist/registry-items.d.ts +84 -0
  74. package/dist/registry-items.d.ts.map +1 -0
  75. package/dist/registry-items.js +187 -0
  76. package/dist/registry-items.js.map +1 -0
  77. package/package.json +36 -10
  78. package/src/bin.ts +316 -0
  79. package/src/commands/add.ts +255 -0
  80. package/src/commands/create-flow.ts +284 -0
  81. package/src/commands/create.ts +149 -0
  82. package/src/commands/doctor.ts +487 -0
  83. package/src/commands/migrate-check.ts +298 -0
  84. package/src/commands/migrate.ts +228 -0
  85. package/src/commands/sync-messages.ts +114 -0
  86. package/src/commands/sync-scaffold.ts +83 -0
  87. package/src/commands/sync.ts +534 -0
  88. package/src/commands/upgrade-check.ts +160 -0
  89. package/src/env-files.ts +31 -0
  90. package/src/index.ts +39 -0
  91. package/src/manifest.ts +147 -0
  92. package/src/migrations-dir.ts +25 -0
  93. package/src/migrations.ts +133 -0
  94. package/src/model-catalogue.ts +35 -0
  95. package/src/module-exports.ts +266 -0
  96. package/src/registry-bundle.ts +103 -0
  97. package/src/registry-items.ts +237 -0
  98. package/templates/admin-page/admin-page.tsx.tpl +2 -3
  99. package/templates/app-scaffold/assistant-route.ts.tpl +2 -2
  100. package/templates/app-scaffold/auth-route.ts.tpl +10 -0
  101. package/templates/app-scaffold/components.json.tpl +6 -2
  102. package/templates/app-scaffold/db-schema.ts.tpl +29 -0
  103. package/templates/app-scaffold/drizzle-journal.json.tpl +5 -0
  104. package/templates/app-scaffold/drizzle.config.ts.tpl +45 -0
  105. package/templates/app-scaffold/env.example.tpl +65 -1
  106. package/templates/app-scaffold/globals.css.tpl +172 -281
  107. package/templates/app-scaffold/i18n-request.ts.tpl +25 -0
  108. package/templates/app-scaffold/instrumentation.ts.tpl +21 -0
  109. package/templates/app-scaffold/intelligo.ts.tpl +95 -41
  110. package/templates/app-scaffold/layout.tsx.tpl +18 -1
  111. package/templates/app-scaffold/lib-utils.ts.tpl +1 -6
  112. package/templates/app-scaffold/next.config.mjs.tpl +2 -0
  113. package/templates/app-scaffold/package.json.tpl +31 -17
  114. package/templates/app-scaffold/page.tsx.tpl +16 -0
  115. package/templates/app-scaffold/plans.ts.tpl +32 -13
  116. package/templates/app-scaffold/{middleware.ts.tpl → proxy.ts.tpl} +6 -0
  117. package/templates/app-scaffold/sonner.tsx.tpl +42 -20
  118. package/templates/app-scaffold/stripe-webhook-route.ts.tpl +24 -0
  119. package/templates/app-scaffold/theme-provider.tsx.tpl +6 -4
  120. package/templates/app-scaffold/tsconfig.json.tpl +20 -4
  121. package/templates/app-scaffold/use-mobile.ts.tpl +19 -0
  122. package/templates/app-scaffold/workspace-bootstrap.ts.tpl +30 -0
  123. package/templates/maintenance/maintenance-route.ts.tpl +119 -0
  124. package/templates/manifest.json +75 -17
  125. package/templates/registry/ai-agent-activity.json +23 -0
  126. package/templates/registry/ai-agent-progress.json +21 -0
  127. package/templates/registry/ai-approval-card.json +27 -0
  128. package/templates/registry/ai-artifact.json +22 -0
  129. package/templates/registry/ai-branch.json +21 -0
  130. package/templates/registry/ai-citations.json +24 -0
  131. package/templates/registry/ai-code-block.json +23 -0
  132. package/templates/registry/ai-composer-menu.json +19 -0
  133. package/templates/registry/ai-file-diff.json +25 -0
  134. package/templates/registry/ai-image-generation.json +23 -0
  135. package/templates/registry/ai-markdown.json +23 -0
  136. package/templates/registry/ai-message-bubble.json +23 -0
  137. package/templates/registry/ai-message-scroller.json +22 -0
  138. package/templates/registry/ai-message.json +21 -0
  139. package/templates/registry/ai-motion.json +19 -0
  140. package/templates/registry/ai-prompt-input.json +28 -0
  141. package/templates/registry/ai-reasoning-text.json +22 -0
  142. package/templates/registry/ai-reasoning.json +22 -0
  143. package/templates/registry/ai-shimmer-text.json +19 -0
  144. package/templates/registry/ai-sidebar.json +25 -0
  145. package/templates/registry/ai-speech-input.json +21 -0
  146. package/templates/registry/ai-streaming-response.json +24 -0
  147. package/templates/registry/ai-suggestion.json +19 -0
  148. package/templates/registry/ai-todo-list.json +22 -0
  149. package/templates/registry/ai-tool-approval.json +26 -0
  150. package/templates/registry/ai-tool-result.json +25 -0
  151. package/templates/registry/alert-dialog.json +23 -0
  152. package/templates/registry/animated-list.json +21 -0
  153. package/templates/registry/app-shell.json +93 -0
  154. package/templates/registry/artifacts.json +82 -0
  155. package/templates/registry/attachment.json +22 -0
  156. package/templates/registry/auth-email-verification.json +39 -0
  157. package/templates/registry/auth-login.json +72 -0
  158. package/templates/registry/auth-password-reset.json +53 -0
  159. package/templates/registry/auth-signup.json +41 -0
  160. package/templates/registry/billing-settings.json +60 -0
  161. package/templates/registry/button.json +22 -0
  162. package/templates/registry/chat-eve.json +19 -0
  163. package/templates/registry/chat-panel.json +30 -0
  164. package/templates/registry/chat-share.json +42 -0
  165. package/templates/registry/chat-widget.json +34 -0
  166. package/templates/registry/chat.json +326 -0
  167. package/templates/registry/checkbox.json +22 -0
  168. package/templates/registry/checkout.json +40 -0
  169. package/templates/registry/collapsible.json +19 -0
  170. package/templates/registry/command.json +23 -0
  171. package/templates/registry/copy-button.json +21 -0
  172. package/templates/registry/dashboard.json +69 -0
  173. package/templates/registry/dialog.json +24 -0
  174. package/templates/registry/document-viewer.json +22 -0
  175. package/templates/registry/dropdown-menu.json +23 -0
  176. package/templates/registry/expandable-tabs.json +22 -0
  177. package/templates/registry/feature-gating.json +73 -0
  178. package/templates/registry/file-upload.json +24 -0
  179. package/templates/registry/hold-action-button.json +22 -0
  180. package/templates/registry/input-group.json +23 -0
  181. package/templates/registry/input.json +19 -0
  182. package/templates/registry/intelligo.json +187 -0
  183. package/templates/registry/invitation-accept.json +64 -0
  184. package/templates/registry/language-switcher.json +29 -0
  185. package/templates/registry/morphing-modal.json +24 -0
  186. package/templates/registry/notification-stack.json +24 -0
  187. package/templates/registry/notifications.json +87 -0
  188. package/templates/registry/onboarding.json +83 -0
  189. package/templates/registry/otp-input.json +22 -0
  190. package/templates/registry/page-header.json +15 -0
  191. package/templates/registry/payment-poll.json +52 -0
  192. package/templates/registry/popover-morph.json +22 -0
  193. package/templates/registry/popover.json +22 -0
  194. package/templates/registry/pricing.json +111 -0
  195. package/templates/registry/privacy-settings.json +65 -0
  196. package/templates/registry/profile-settings.json +68 -0
  197. package/templates/registry/progress.json +19 -0
  198. package/templates/registry/radio-group.json +22 -0
  199. package/templates/registry/registry.json +2945 -0
  200. package/templates/registry/route-error.json +65 -0
  201. package/templates/registry/select-morph.json +23 -0
  202. package/templates/registry/select.json +23 -0
  203. package/templates/registry/settings-shell.json +47 -0
  204. package/templates/registry/sheet.json +24 -0
  205. package/templates/registry/sidebar.json +28 -0
  206. package/templates/registry/smoke.json +34 -0
  207. package/templates/registry/spinner.json +16 -0
  208. package/templates/registry/stat-card.json +18 -0
  209. package/templates/registry/status-badge.json +18 -0
  210. package/templates/registry/switch.json +20 -0
  211. package/templates/registry/tabs.json +23 -0
  212. package/templates/registry/team-settings.json +97 -0
  213. package/templates/registry/textarea.json +16 -0
  214. package/templates/registry/tooltip.json +22 -0
  215. package/templates/registry/trial-banner.json +43 -0
  216. package/templates/registry/usage.json +84 -0
  217. package/templates/registry/workspace-settings.json +71 -0
  218. package/templates/registry-items.json +116 -0
  219. package/templates/registry-requires.json +180 -0
  220. package/templates/usage-page/usage-page.tsx.tpl +23 -8
  221. package/templates/vitest/server-only.ts.tpl +7 -0
  222. package/templates/vitest/vitest.config.ts.tpl +37 -0
  223. package/templates/billing-page/billing-page.tsx.tpl +0 -72
@@ -0,0 +1,487 @@
1
+ /**
2
+ * `intelligo doctor` — report the problems that are invisible until
3
+ * they are an incident.
4
+ */
5
+
6
+ import path from "node:path";
7
+ import { fileURLToPath } from "node:url";
8
+ import { existsSync, readFileSync } from "node:fs";
9
+ import { parseEnv } from "node:util";
10
+
11
+ import { inspectMigrationChain, readMigrationChain } from "../migrations.js";
12
+ import { readManifest } from "../manifest.js";
13
+ import {
14
+ checkExports,
15
+ hasObjectKey,
16
+ reexportedSource,
17
+ stripComments,
18
+ } from "../module-exports.js";
19
+ import {
20
+ MODEL_CATALOGUE_LOCATIONS,
21
+ readCatalogueModelIds,
22
+ } from "../model-catalogue.js";
23
+ import {
24
+ MIGRATION_LOCATIONS,
25
+ resolveMigrationsDir,
26
+ } from "../migrations-dir.js";
27
+
28
+ export type CheckResult = {
29
+ name: string;
30
+ status: "ok" | "warn" | "error";
31
+ detail: string;
32
+ };
33
+
34
+ type ItemRequires = {
35
+ marker: string;
36
+ items?: string[];
37
+ files?: string[];
38
+ exports?: Record<string, string[]>;
39
+ features?: string[];
40
+ };
41
+
42
+ export type RegistryRequires = {
43
+ scaffold: string[];
44
+ items: Record<string, ItemRequires>;
45
+ };
46
+
47
+ /**
48
+ * templates/registry-requires.json is a build-time copy of
49
+ * packages/registry/requires.json (scripts/sync-registry-requires.mjs),
50
+ * committed so that running from source works too, and shipped so
51
+ * doctor can check an installed app without a registry checkout.
52
+ * `../../templates` resolves from src/ and from dist/commands/ alike.
53
+ */
54
+ function bundledRequires(): RegistryRequires | null {
55
+ const file = path.resolve(
56
+ // fileURLToPath, not `.pathname` — see the note in bin.ts.
57
+ path.dirname(fileURLToPath(import.meta.url)),
58
+ "..",
59
+ "..",
60
+ "templates",
61
+ "registry-requires.json"
62
+ );
63
+ if (!existsSync(file)) return null;
64
+ return JSON.parse(readFileSync(file, "utf8")) as RegistryRequires;
65
+ }
66
+
67
+ export type DoctorOptions = {
68
+ /** Workspace root; defaults to the current working directory. */
69
+ root?: string;
70
+ /** Registry requirements; defaults to the copy bundled with the CLI. */
71
+ requires?: RegistryRequires | null;
72
+ /** Environment to validate; defaults to process.env. */
73
+ env?: NodeJS.ProcessEnv;
74
+ };
75
+
76
+ /** Variables the app cannot boot without. */
77
+ const REQUIRED_ENV = [
78
+ "DATABASE_URL",
79
+ "BETTER_AUTH_SECRET",
80
+ "NEXT_PUBLIC_APP_URL",
81
+ ];
82
+
83
+ /** The app's `assertEnv` refuses a shorter BETTER_AUTH_SECRET. */
84
+ const MIN_AUTH_SECRET_LENGTH = 32;
85
+
86
+ /** Env files Next.js reads only for a production build or server. */
87
+ const PRODUCTION_ENV_FILES = [".env.production.local", ".env.production"];
88
+
89
+ const LOCAL_URL = /^https?:\/\/(?:localhost|127\.0\.0\.1|\[::1\])(?::|\/|$)/;
90
+
91
+ /**
92
+ * Where a production deployment would get a NEXT_PUBLIC_APP_URL that
93
+ * points at this machine: the environment itself when it says
94
+ * production, or a production env file in the app.
95
+ */
96
+ function localAppUrlInProduction(
97
+ root: string,
98
+ env: NodeJS.ProcessEnv
99
+ ): string | null {
100
+ if (
101
+ env.NODE_ENV === "production" &&
102
+ LOCAL_URL.test(env.NEXT_PUBLIC_APP_URL ?? "")
103
+ ) {
104
+ return "NODE_ENV=production";
105
+ }
106
+ for (const name of PRODUCTION_ENV_FILES) {
107
+ const file = path.join(root, name);
108
+ if (!existsSync(file)) continue;
109
+ const url = parseEnv(readFileSync(file, "utf8")).NEXT_PUBLIC_APP_URL;
110
+ if (url && LOCAL_URL.test(url)) return name;
111
+ }
112
+ return null;
113
+ }
114
+
115
+ /** Files of the chat items that name the models a deployment runs on. */
116
+ const MODEL_ID_FILES = [
117
+ "lib/chat-model.ts",
118
+ "lib/chat-models.ts",
119
+ "lib/chat-server-config.ts",
120
+ ];
121
+
122
+ /** A provider-prefixed model id literal, in any of the three quote styles. */
123
+ const MODEL_ID =
124
+ /["'`]((?:openai|anthropic|google|xai|mistral|meta)\/[a-z0-9._-]+)["'`]/g;
125
+
126
+ /** `id: "provider/model"` — a model registered by a literal. */
127
+ const REGISTERED_ID = /\bid:\s*["'`]([a-z0-9-]+\/[a-z0-9._-]+)["'`]/g;
128
+
129
+ /**
130
+ * Required environment: present, and strong enough that the app's own
131
+ * startup validation will accept it.
132
+ */
133
+ function checkEnvironment(
134
+ root: string,
135
+ env: NodeJS.ProcessEnv | Record<string, string | undefined>
136
+ ): CheckResult[] {
137
+ const results: CheckResult[] = [];
138
+ const missing = REQUIRED_ENV.filter((k) => !env[k]);
139
+ const authSecret = env.BETTER_AUTH_SECRET ?? "";
140
+ const weakSecret =
141
+ authSecret.length > 0 && authSecret.length < MIN_AUTH_SECRET_LENGTH;
142
+ if (missing.length > 0) {
143
+ results.push({
144
+ name: "env",
145
+ status: "error",
146
+ detail: `Missing: ${missing.join(", ")}`,
147
+ });
148
+ }
149
+ if (weakSecret) {
150
+ results.push({
151
+ name: "env",
152
+ status: "error",
153
+ detail:
154
+ `BETTER_AUTH_SECRET is ${authSecret.length} chars — the app refuses to ` +
155
+ `boot with fewer than ${MIN_AUTH_SECRET_LENGTH}. Generate one with: ` +
156
+ "openssl rand -base64 32",
157
+ });
158
+ }
159
+ if (missing.length === 0 && !weakSecret) {
160
+ results.push({
161
+ name: "env",
162
+ status: "ok",
163
+ detail: `${REQUIRED_ENV.length} required variables present`,
164
+ });
165
+ }
166
+ const localUrlSource = localAppUrlInProduction(root, env);
167
+ if (localUrlSource) {
168
+ results.push({
169
+ name: "env",
170
+ status: "warn",
171
+ detail:
172
+ `NEXT_PUBLIC_APP_URL points at localhost (${localUrlSource}) — auth ` +
173
+ "callbacks and email links are built from it; production needs the " +
174
+ "public https URL",
175
+ });
176
+ }
177
+ return results;
178
+ }
179
+
180
+ /**
181
+ * Model ids. A registered catalogue still refuses an id it does not
182
+ * hold, so a typo in the chat seam is a refused first turn. Every
183
+ * provider-prefixed literal there must be in DEFAULT_MODELS (when the
184
+ * root registers it) or registered by a literal `id` in the root.
185
+ */
186
+ function checkModelIds(root: string, source: string): CheckResult[] {
187
+ const used = new Map<string, string>();
188
+ for (const rel of MODEL_ID_FILES) {
189
+ const file = path.join(root, rel);
190
+ if (!existsSync(file)) continue;
191
+ const text = stripComments(readFileSync(file, "utf8"));
192
+ for (const match of text.matchAll(MODEL_ID)) {
193
+ if (!used.has(match[1]!)) used.set(match[1]!, rel);
194
+ }
195
+ }
196
+
197
+ if (used.size === 0) return [];
198
+
199
+ const code = stripComments(source);
200
+ const known = new Set([...code.matchAll(REGISTERED_ID)].map((m) => m[1]!));
201
+ const usesCatalogue = /\bDEFAULT_MODELS\b/.test(code);
202
+ const catalogue = usesCatalogue ? readCatalogueModelIds(root) : null;
203
+ for (const id of catalogue ?? []) known.add(id);
204
+
205
+ const unknown = [...used].filter(([id]) => !known.has(id));
206
+ const listed = unknown.map(([id, rel]) => `${id} (${rel})`).join(", ");
207
+
208
+ if (unknown.length === 0) {
209
+ return [
210
+ {
211
+ name: "model-ids",
212
+ status: "ok",
213
+ detail: `${used.size} model id(s) in the chat seam, all registered`,
214
+ },
215
+ ];
216
+ } else if (usesCatalogue && !catalogue) {
217
+ return [
218
+ {
219
+ name: "model-ids",
220
+ status: "warn",
221
+ detail:
222
+ `cannot read DEFAULT_MODELS (looked in ${MODEL_CATALOGUE_LOCATIONS.join(", ")}) ` +
223
+ `to check ${listed} — is @intelligo-dev/executions installed?`,
224
+ },
225
+ ];
226
+ } else if (OPAQUE_REGISTRATION.test(code)) {
227
+ return [
228
+ {
229
+ name: "model-ids",
230
+ status: "warn",
231
+ detail:
232
+ `${listed} not found in DEFAULT_MODELS or a literal registerModel({ id }) — ` +
233
+ "the composition root registers a catalogue defined elsewhere, " +
234
+ "so confirm it holds them",
235
+ },
236
+ ];
237
+ } else {
238
+ return [
239
+ {
240
+ name: "model-ids",
241
+ status: "error",
242
+ detail:
243
+ `${listed} not registered — admission refuses an unpriced model ` +
244
+ "with `unknown_model`. Fix the id, or add " +
245
+ "`registerModel({ id, … })` to the composition root",
246
+ },
247
+ ];
248
+ }
249
+ }
250
+
251
+ /**
252
+ * A `registerModel(s)` argument that is neither a literal nor
253
+ * `DEFAULT_MODELS`: a catalogue defined in another module, whose ids
254
+ * cannot be read from the composition root.
255
+ */
256
+ const OPAQUE_REGISTRATION =
257
+ /\bregisterModels?\s*\(\s*(?![\s[{]|DEFAULT_MODELS\s*[,)])/;
258
+
259
+ export function runChecks(options: DoctorOptions = {}): CheckResult[] {
260
+ const root = options.root ?? process.cwd();
261
+ const env = options.env ?? process.env;
262
+ const results: CheckResult[] = [];
263
+
264
+ // 1. Migration chain: drift between the journal and the .sql files.
265
+ const migrationsDir = resolveMigrationsDir(root);
266
+ if (!migrationsDir) {
267
+ results.push({
268
+ name: "migrations",
269
+ status: "warn",
270
+ detail: `No migrations directory (looked in ${MIGRATION_LOCATIONS.join(", ")}) — run from the workspace root, with @intelligo-dev/core installed?`,
271
+ });
272
+ } else {
273
+ const chain = readMigrationChain(migrationsDir);
274
+ const problems = inspectMigrationChain(chain);
275
+ if (problems.length === 0) {
276
+ results.push({
277
+ name: "migrations",
278
+ status: "ok",
279
+ detail: `${chain.files.length} migrations, all registered in the journal`,
280
+ });
281
+ } else {
282
+ for (const p of problems) {
283
+ results.push({
284
+ name: "migrations",
285
+ status: p.level === "error" ? "error" : "warn",
286
+ detail: p.message,
287
+ });
288
+ }
289
+ }
290
+ }
291
+
292
+ // 2. Required environment.
293
+ results.push(...checkEnvironment(root, env));
294
+
295
+ // 3. Billing product. The engine has no built-in default catalogue;
296
+ // an unset product means every plan lookup returns nothing and
297
+ // quotas silently read as zero.
298
+ results.push(
299
+ env.INTELLIGO_BILLING_PRODUCT
300
+ ? {
301
+ name: "billing",
302
+ status: "ok",
303
+ detail: `Product: ${env.INTELLIGO_BILLING_PRODUCT}`,
304
+ }
305
+ : {
306
+ name: "billing",
307
+ status: "warn",
308
+ detail:
309
+ "INTELLIGO_BILLING_PRODUCT unset — the composition root must call " +
310
+ "setDefaultProductSlug(), or plan lookups resolve to nothing",
311
+ }
312
+ );
313
+
314
+ // 4. Installed registry items against registry/requires.json: the
315
+ // sibling items they import from, the scaffold files they import,
316
+ // the exports the composition root must provide, and the feature
317
+ // keys they gate on — each a `next build` or a 403 that only shows
318
+ // up later. An item counts as installed when its marker file is.
319
+ const requires =
320
+ options.requires === undefined ? bundledRequires() : options.requires;
321
+ if (requires) {
322
+ const exists = (rel: string) =>
323
+ [".ts", ".tsx", ".js", ".jsx", ".json", ""].some((ext) =>
324
+ existsSync(path.join(root, rel + ext))
325
+ );
326
+ // lib/plans.ts and whatever it re-exports — a catalogue defined in
327
+ // a workspace package counts — comments stripped.
328
+ const plansFile = ["lib/plans.ts", "lib/plans.tsx"]
329
+ .map((rel) => path.join(root, rel))
330
+ .find((file) => existsSync(file));
331
+ const plans = plansFile ? reexportedSource(plansFile, root) : null;
332
+
333
+ for (const [name, item] of Object.entries(requires.items)) {
334
+ if (!existsSync(path.join(root, item.marker))) continue;
335
+ const problems: string[] = [];
336
+ const notes: string[] = [];
337
+
338
+ for (const dep of item.items ?? []) {
339
+ const marker = requires.items[dep]?.marker;
340
+ if (marker && !existsSync(path.join(root, marker))) {
341
+ problems.push(`install the ${dep} item first`);
342
+ }
343
+ }
344
+ for (const file of item.files ?? []) {
345
+ if (!exists(file)) problems.push(`create ${file} (scaffold-provided)`);
346
+ }
347
+ for (const [file, names] of Object.entries(item.exports ?? {})) {
348
+ const abs = [".ts", ".tsx"]
349
+ .map((ext) => path.join(root, file + ext))
350
+ .find((p) => existsSync(p));
351
+ if (!abs) continue; // reported above as a missing file
352
+ const { missing, unresolved } = checkExports(abs, names, root);
353
+ if (missing.length) {
354
+ problems.push(`${file} must export ${missing.join(", ")}`);
355
+ } else if (unresolved.length) {
356
+ notes.push(
357
+ `${file}: could not read ${unresolved.map((u) => `\`export * from "${u}"\``).join(", ")} — assumed to provide ${names.join(", ")}`
358
+ );
359
+ }
360
+ }
361
+ for (const feature of item.features ?? []) {
362
+ if (!plans || hasObjectKey(plans.text, feature)) continue;
363
+ if (plans.unresolved.length) {
364
+ notes.push(
365
+ `the "${feature}" feature key is not in lib/plans.ts, and ${plans.unresolved.join(", ")} could not be read to confirm it`
366
+ );
367
+ } else {
368
+ problems.push(
369
+ `register the "${feature}" feature key in lib/plans.ts — an unregistered key is denied (403)`
370
+ );
371
+ }
372
+ }
373
+
374
+ results.push(
375
+ problems.length === 0
376
+ ? notes.length === 0
377
+ ? { name: `item:${name}`, status: "ok", detail: "requirements met" }
378
+ : { name: `item:${name}`, status: "warn", detail: notes.join("; ") }
379
+ : {
380
+ name: `item:${name}`,
381
+ status: "error",
382
+ detail: problems.join("; "),
383
+ }
384
+ );
385
+ }
386
+ }
387
+
388
+ // 4b. Maintenance route. It refuses to serve without a strong
389
+ // CRON_SECRET, and a scheduler hitting a 403 every five minutes
390
+ // is easy to miss.
391
+ if (existsSync(path.join(root, "app/api/cron/maintenance/route.ts"))) {
392
+ const cronSecret = env.CRON_SECRET ?? "";
393
+ results.push(
394
+ cronSecret.length >= 32
395
+ ? { name: "maintenance", status: "ok", detail: "CRON_SECRET set" }
396
+ : {
397
+ name: "maintenance",
398
+ status: "error",
399
+ detail:
400
+ "app/api/cron/maintenance/route.ts exists but CRON_SECRET is " +
401
+ (cronSecret ? "shorter than 32 chars" : "unset") +
402
+ " — the route answers 403 until it is",
403
+ }
404
+ );
405
+ }
406
+
407
+ // 4c. Better-Auth's HTTP mount. Every auth item's form posts to
408
+ // `/api/auth/*` on this origin; without the catch-all route
409
+ // those requests 404 and the pages look broken for no visible
410
+ // reason.
411
+ if (existsSync(path.join(root, "app/[locale]/(auth)/layout.tsx"))) {
412
+ results.push(
413
+ existsSync(path.join(root, "app/api/auth/[...all]/route.ts"))
414
+ ? { name: "auth-mount", status: "ok", detail: "/api/auth mounted" }
415
+ : {
416
+ name: "auth-mount",
417
+ status: "error",
418
+ detail:
419
+ "auth pages are installed but app/api/auth/[...all]/route.ts " +
420
+ "is missing — add `export { GET, POST } from " +
421
+ '"@intelligo-dev/next/auth";` there or every sign-in answers 404',
422
+ }
423
+ );
424
+ }
425
+
426
+ // 4d. Model prices. The registry is open and nothing self-registers,
427
+ // so a composition root that never calls `registerModels` leaves
428
+ // admission with no price to estimate against: every request is
429
+ // refused with `unknown_model`, at runtime, on a deployment whose
430
+ // only mistake was omitting one line.
431
+ const compositionRoot = ["lib/intelligo.ts", "lib/intelligo.tsx"]
432
+ .map((rel) => path.join(root, rel))
433
+ .find((file) => existsSync(file));
434
+
435
+ if (compositionRoot) {
436
+ const source = readFileSync(compositionRoot, "utf8");
437
+ const registers = /\bregisterModels?\s*\(/.test(stripComments(source));
438
+ results.push(
439
+ registers
440
+ ? {
441
+ name: "models",
442
+ status: "ok",
443
+ detail: "the composition root registers model prices",
444
+ }
445
+ : {
446
+ name: "models",
447
+ status: "error",
448
+ detail:
449
+ "no registerModels() in the composition root — admission cannot " +
450
+ "price any model and refuses every request with `unknown_model`. " +
451
+ "Add `registerModels(DEFAULT_MODELS)` from @intelligo-dev/executions, " +
452
+ "or your own catalogue",
453
+ }
454
+ );
455
+
456
+ if (registers) results.push(...checkModelIds(root, source));
457
+ }
458
+
459
+ // 5. Generated source. A conflict — template and consumer both
460
+ // moved — is the one state an upgrade cannot resolve on its own.
461
+ const manifest = readManifest(root);
462
+ if (manifest) {
463
+ const features = Object.keys(manifest.features);
464
+ results.push({
465
+ name: "generated",
466
+ status: "ok",
467
+ detail:
468
+ features.length === 0
469
+ ? "manifest present, no features generated"
470
+ : `${features.length} feature(s): ${features.join(", ")} — run \`intelligo upgrade --check\` for detail`,
471
+ });
472
+ }
473
+
474
+ return results;
475
+ }
476
+
477
+ export function formatResults(results: CheckResult[]): string {
478
+ const icon = { ok: "✓", warn: "!", error: "✗" } as const;
479
+ return results
480
+ .map((r) => `${icon[r.status]} ${r.name.padEnd(12)} ${r.detail}`)
481
+ .join("\n");
482
+ }
483
+
484
+ /** Process exit code: non-zero when any check errored. */
485
+ export function exitCodeFor(results: CheckResult[]): number {
486
+ return results.some((r) => r.status === "error") ? 1 : 0;
487
+ }