scaling-swarms / backend /scripts /gen-embed-kit.mjs
tfrere's picture
tfrere HF Staff
Cursor
feat(embed-studio): tweak panel, inspect, automatic check, launcher, inline edit
4953bfd
Raw History Blame Contribute Delete
3.92 kB
#!/usr/bin/env node
// Compile backend/embed-kit/ (the embed runtime, the Embed Studio guides and
// the checker's in-page inspector) into TypeScript modules: the runtime is
// inlined in every embed iframe by buildEmbedSrcdoc, the guides are appended
// to the Embed Studio prompt, the inspector runs in the embed checker.
//
// node scripts/gen-embed-kit.mjs # write the generated files
// node scripts/gen-embed-kit.mjs --check # fail if they are stale
import { readFile, writeFile } from 'node:fs/promises';
import { existsSync } from 'node:fs';
import { dirname, join, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
const backendDir = resolve(dirname(fileURLToPath(import.meta.url)), '..');
const kitDir = join(backendDir, 'embed-kit');
const checkOnly = process.argv.includes('--check');
const read = (name) => readFile(join(kitDir, name), 'utf8');
const runtime = await read('embed-runtime.js');
const version = (runtime.match(/const VERSION = '([^']+)'/) || [])[1];
if (!version) {
console.error('Could not read VERSION from embed-kit/embed-runtime.js.');
process.exit(1);
}
const header = (source) => [
'// GENERATED FILE - do not edit by hand.',
`// Source: backend/embed-kit/${source}`,
'// Regenerate with `npm run gen:embed-kit` in backend/.',
'',
].join('\n');
// Gallery charts offered as starting points in the Embed Studio launcher.
const STARTERS = ['chinchilla-scaling-law', 'smollm3-benchmarks', 'arena-win-rate-matrix', 'moe-banner'];
const starters = await Promise.all(STARTERS.map(async (slug) => {
const readme = await read(`gallery/${slug}/README.md`);
const lines = readme.split('\n');
const title = (lines.find((l) => l.startsWith('# ')) || slug).replace(/^# /, '').trim();
const start = lines.findIndex((l) => l.startsWith('# ')) + 1;
const para = [];
for (const line of lines.slice(start)) {
if (!line.trim()) { if (para.length) break; continue; }
if (line.startsWith('#')) break;
para.push(line.trim());
}
return {
slug,
title,
summary: para.join(' '),
kind: slug.endsWith('-banner') ? 'banner' : 'chart',
html: (await read(`gallery/${slug}/embed.html`)).trim(),
};
}));
const outputs = {
'src/shared/embed-runtime.generated.ts': [
header('embed-runtime.js'),
`export const EMBED_RUNTIME_VERSION = ${JSON.stringify(version)};`,
'',
`export const EMBED_RUNTIME_JS = ${JSON.stringify(runtime)};`,
'',
].join('\n'),
'src/agent/embed-skill.generated.ts': [
header('{contract,banners,dataviz}.md'),
`export const EMBED_CONTRACT_MD = ${JSON.stringify((await read('contract.md')).trim())};`,
'',
`export const EMBED_BANNERS_MD = ${JSON.stringify((await read('banners.md')).trim())};`,
'',
`export const DATAVIZ_DESIGN_MD = ${JSON.stringify((await read('dataviz.md')).trim())};`,
'',
].join('\n'),
'src/agent/embed-starters.generated.ts': [
header('gallery/{' + STARTERS.join(',') + '}'),
'export interface EmbedStarter { slug: string; title: string; summary: string; kind: "chart" | "banner"; html: string }',
'',
`export const EMBED_STARTERS: EmbedStarter[] = ${JSON.stringify(starters, null, 2)};`,
'',
].join('\n'),
'src/embed-check/inspect.generated.ts': [
header('inspect.js'),
`export const INSPECT_JS = ${JSON.stringify(await read('inspect.js'))};`,
'',
].join('\n'),
};
let stale = 0;
for (const [rel, content] of Object.entries(outputs)) {
const target = join(backendDir, rel);
const current = existsSync(target) ? await readFile(target, 'utf8') : null;
if (current === content) continue;
stale++;
if (checkOnly) console.error(`stale: ${rel} (run npm run gen:embed-kit)`);
else {
await writeFile(target, content);
console.log(`wrote ${rel}`);
}
}
if (checkOnly && stale) process.exit(1);
if (!stale) console.log(`embed kit ${version} already up to date.`);