返回 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, deriveFeatures, REPO_ROOT } from "./facts-lib.mjs";
15 import { writeAgentDocs } from "./docs-sources-lib.mjs";
16 import { refreshReleaseBaseline } from "./release-baseline-lib.mjs";
17
18 const __dirname = dirname(fileURLToPath(import.meta.url));
19 const target = resolve(__dirname, "..", "lib", "facts.generated.ts");
20
21 const out = buildFacts();
22
23 // Preserve the committed `generatedAt` when every *checked* fact is unchanged,
24 // so a clean rebuild doesn't dirty the tracked file on every run. The drift
25 // gate (check-facts.mjs) ignores generatedAt + exact-build provenance; we
26 // mirror that volatile set here. Only when a checked fact changes do we stamp
27 // a fresh time.
28 const VOLATILE = new Set(["generatedAt", "sourceRevision", "sourceCommittedAt"]);
29 function readCommittedFacts() {
30 if (!existsSync(target)) return null;
31 const src = readFileSync(target, "utf-8");
32 const m = src.match(/export const FACTS\s*:\s*\w+\s*=\s*([\s\S]*?);?\s*$/);
33 if (!m) return null;
34 try {
35 return JSON.parse(m[1]);
36 } catch {
37 return null;
38 }
39 }
40 const committed = readCommittedFacts();
41 if (committed && typeof committed.generatedAt === "string") {
42 const sameChecked = Object.keys(out).every(
43 (k) => VOLATILE.has(k) || JSON.stringify(out[k]) === JSON.stringify(committed[k]),
44 );
45 if (sameChecked) out.generatedAt = committed.generatedAt;
46 }
47
48 const missing = Object.entries(out).filter(
49 ([k, v]) =>
50 k !== "generatedAt" &&
51 k !== "sourceRevision" &&
52 k !== "sourceCommittedAt" &&
53 (v == null || (Array.isArray(v) && v.length === 0)),
54 );
55 if (missing.length > 0) {
56 console.warn("[derive-facts] missing values:", missing.map(([k]) => k).join(", "));
57 }
58
59
60 const ts = `// AUTO-GENERATED by web/scripts/derive-facts.mjs at prebuild.
61 // DO NOT EDIT — re-run \`npm run prebuild\` (or just \`npm run build\`) after changing the parent repo.
62 // Runtime KV snapshots must use this shape and include exact source provenance.
63
64 export interface ProviderFact { id: string; label: string; env: string }
65
66 export interface ModelFact {
67 id: string;
68 provider: string | null;
69 contextWindow: number | null;
70 maxOutput: number | null;
71 reasoning: boolean;
72 addedAt: string | null;
73 }
74
75 export interface ReleaseAssetFact { name: string; size: number; sha256: string | null }
76
77 export interface PublishedReleaseFact {
78 tag: string;
79 version: string;
80 publishedAt: string;
81 url: string;
82 readAt?: string;
83 assets?: ReleaseAssetFact[];
84 }
85
86 export interface RepoFacts {
87 generatedAt: string;
88 sourceRevision: string | null;
89 sourceCommittedAt: string | null;
90 version: string | null;
91 crates: string[];
92 sandboxBackends: string[];
93 providers: ProviderFact[];
94 models: ModelFact[];
95 defaultModel: string | null;
96 nodeEngines: string | null;
97 toolCount: number | null;
98 contributorCount: number;
99 license: string | null;
100 latestPublishedRelease: PublishedReleaseFact | null;
101 }
102
103 export const FACTS: RepoFacts = ${JSON.stringify(out, null, 2)};
104 `;
105
106 writeFileSync(target, ts);
107 writeFileSync(resolve(__dirname, "..", "data", "features.generated.json"), `${JSON.stringify(deriveFeatures(), null, 2)}\n`);
108 refreshReleaseBaseline(out.latestPublishedRelease);
109 writeAgentDocs(resolve(__dirname, ".."), REPO_ROOT);
110 console.log(`[derive-facts] wrote ${target}`);
111 console.log(
112 `[derive-facts] version=${out.version} crates=${out.crates.length} ` +
113 `providers=${out.providers.length} models=${out.models.length} sandboxes=${out.sandboxBackends.length} ` +
114 `default-model=${out.defaultModel} node=${out.nodeEngines} ` +
115 `tools=${out.toolCount} contributors=${out.contributorCount} license=${out.license}`,
116 );
117
117 lines Plain Text