返回 CodeWhale
derive-facts.mjs
根目录 / web / scripts / derive-facts.mjs
1 #!/usr/bin/env node
2 /**
3 * derive-facts.mjs — extract mechanical facts from the parent repo and write
4 * them as a typed TS module. Run as `prebuild`. The same logic also runs in
5 * the content-drift cron against raw.githubusercontent.com so the deployed
6 * worker can detect repo→site drift between deploys.
7 *
8 * This script delegates all derivation logic to facts-lib.mjs so that
9 * check-facts.mjs can reuse the same code for the CI drift gate.
10 */
11 import { writeFileSync, readFileSync, existsSync } from "node:fs";
12 import { dirname, resolve } from "node:path";
13 import { fileURLToPath } from "node:url";
14 import { buildFacts } from "./facts-lib.mjs";
15
16 const __dirname = dirname(fileURLToPath(import.meta.url));
17 const target = resolve(__dirname, "..", "lib", "facts.generated.ts");
18
19 const out = buildFacts();
20
21 // Preserve the committed `generatedAt` when every *checked* fact is unchanged,
22 // so a clean rebuild doesn't dirty the tracked file on every run. The drift
23 // gate (check-facts.mjs) ignores generatedAt + exact-build provenance; we
24 // mirror that volatile set here. Only when a checked fact changes do we stamp
25 // a fresh time.
26 const VOLATILE = new Set(["generatedAt", "sourceRevision", "sourceCommittedAt"]);
27 function readCommittedFacts() {
28 if (!existsSync(target)) return null;
29 const src = readFileSync(target, "utf-8");
30 const m = src.match(/export const FACTS\s*:\s*\w+\s*=\s*([\s\S]*?);?\s*$/);
31 if (!m) return null;
32 try {
33 return JSON.parse(m[1]);
34 } catch {
35 return null;
36 }
37 }
38 const committed = readCommittedFacts();
39 if (committed && typeof committed.generatedAt === "string") {
40 const sameChecked = Object.keys(out).every(
41 (k) => VOLATILE.has(k) || JSON.stringify(out[k]) === JSON.stringify(committed[k]),
42 );
43 if (sameChecked) out.generatedAt = committed.generatedAt;
44 }
45
46 const missing = Object.entries(out).filter(
47 ([k, v]) =>
48 k !== "generatedAt" &&
49 k !== "sourceRevision" &&
50 k !== "sourceCommittedAt" &&
51 (v == null || (Array.isArray(v) && v.length === 0)),
52 );
53 if (missing.length > 0) {
54 console.warn("[derive-facts] missing values:", missing.map(([k]) => k).join(", "));
55 }
56
57
58 const ts = `// AUTO-GENERATED by web/scripts/derive-facts.mjs at prebuild.
59 // DO NOT EDIT — re-run \`npm run prebuild\` (or just \`npm run build\`) after changing the parent repo.
60 // Runtime KV snapshots must use this shape and include exact source provenance.
61
62 export interface ProviderFact { id: string; label: string; env: string }
63
64 export interface ModelFact {
65 id: string;
66 provider: string | null;
67 contextWindow: number | null;
68 maxOutput: number | null;
69 reasoning: boolean;
70 addedAt: string | null;
71 }
72
73 export interface PublishedReleaseFact {
74 tag: string;
75 version: string;
76 publishedAt: string;
77 url: string;
78 }
79
80 export interface RepoFacts {
81 generatedAt: string;
82 sourceRevision: string | null;
83 sourceCommittedAt: string | null;
84 version: string | null;
85 crates: string[];
86 sandboxBackends: string[];
87 providers: ProviderFact[];
88 models: ModelFact[];
89 defaultModel: string | null;
90 nodeEngines: string | null;
91 toolCount: number | null;
92 license: string | null;
93 latestPublishedRelease: PublishedReleaseFact | null;
94 }
95
96 export const FACTS: RepoFacts = ${JSON.stringify(out, null, 2)};
97 `;
98
99 writeFileSync(target, ts);
100 console.log(`[derive-facts] wrote ${target}`);
101 console.log(
102 `[derive-facts] version=${out.version} crates=${out.crates.length} ` +
103 `providers=${out.providers.length} models=${out.models.length} sandboxes=${out.sandboxBackends.length} ` +
104 `default-model=${out.defaultModel} node=${out.nodeEngines} ` +
105 `tools=${out.toolCount} license=${out.license}`,
106 );
107
107 lines Plain Text