Files
openmotion/scripts/build-template-cards.mjs
MasonJJ 6b6862427c fix: align recommendation briefs with template library (OPEND-5392) (#536)
* fix: align recommendation briefs with template library (OPEND-5392)

* test: align installation routing contract with source capabilities

* docs: align MCP card contract with capability-based routing

---------

Co-authored-by: bone3deep1962-collab <bone3deep1962@gmail.com>
2026-11-17 18:47:52 +08:00

117 lines
5.8 KiB
JavaScript

import assert from 'node:assert/strict';
import { readFile, writeFile, lstat } from 'node:fs/promises';
import { createHash } from 'node:crypto';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
const applicationRoot = fileURLToPath(new URL('../', import.meta.url));
const directory = path.join(applicationRoot, 'mcp-ui/template-recommendations');
const sourceInputs = [
'mcp-ui/template-recommendations/app.ts',
'mcp-ui/template-recommendations/native.ts',
'mcp-ui/template-recommendations/i18n.ts',
'mcp-ui/template-recommendations/app.css',
'mcp-ui/template-recommendations/types.ts',
'mcp-ui/tsconfig.json',
'scripts/build-template-cards.mjs',
'shared/onboarding-templates.ts',
'shared/template-prompts.ts',
'shared/template-locale.ts',
'packages/design-system/inline-feedback.ts',
'packages/design-system/tokens.css',
'packages/design-system/studio/feedback.css',
'packages/design-system/studio/template-cards.css',
].sort();
const buildOutputs = ['mcp-ui/template-recommendations/build/app.js', 'mcp-ui/template-recommendations/build/inline.js', 'mcp-ui/template-recommendations/build/app.css'].sort();
const identityPath = 'mcp-ui/template-recommendations/build/identity.json';
const sha256 = bytes => createHash('sha256').update(bytes).digest('hex');
async function identityFiles(root, names) {
return Promise.all(names.map(async name => {
const filename = path.join(root, name);
const info = await lstat(filename);
assert(info.isFile() && !info.isSymbolicLink(), 'Build input/output must be a regular file: ' + name);
const bytes = await readFile(filename);
return { path: name, bytes: bytes.length, sha256: sha256(bytes) };
}));
}
async function buildIdentity(root) {
const info = async name => JSON.parse(await readFile(path.join(root, 'node_modules', name, 'package.json'), 'utf8'));
const [sdk, vite, pkg] = await Promise.all([info('@modelcontextprotocol/ext-apps'), info('vite'),
readFile(path.join(root, 'package.json'), 'utf8').then(JSON.parse)]);
assert.equal(sdk.version, '2.0.4', 'Build with the reviewed browser SDK version');
assert.equal(pkg.devDependencies?.['@modelcontextprotocol/ext-apps'], sdk.version, 'Browser SDK must remain an exact build dependency');
assert.equal(pkg.devDependencies?.vite, vite.version, 'Vite must match the pinned build dependency');
return {
schemaVersion: 1,
sdk: { name: '@modelcontextprotocol/ext-apps', version: sdk.version,
browserEntrySha256: sha256(await readFile(path.join(root, 'node_modules/@modelcontextprotocol/ext-apps/dist/src/app-with-deps.js'))) },
vite: vite.version,
inputs: await identityFiles(root, sourceInputs),
outputs: await identityFiles(root, buildOutputs),
};
}
/** Source packaging checks freshness without importing any development runtime. */
export async function checkTemplateCardsBuild(root = applicationRoot) {
try {
const saved = JSON.parse(await readFile(path.join(root, identityPath), 'utf8'));
assert.deepEqual(saved, await buildIdentity(root), 'Browser inputs or generated output changed after build');
return saved;
} catch (cause) {
throw new Error('TEMPLATE_CARDS_BUILD_OUTDATED: run npm run build:template-cards before packaging', { cause });
}
}
// Source builds produce a self-contained browser bundle. Installed runtimes
// read these frozen assets and never import Vite or the MCP Apps package.
export async function buildTemplateCards() {
const sdk = JSON.parse(await readFile(path.join(applicationRoot, 'node_modules/@modelcontextprotocol/ext-apps/package.json'), 'utf8'));
assert.equal(sdk.version, '2.0.4', 'Build with the reviewed browser SDK version');
const { build } = await import('vite');
await build({
configFile: false,
root: applicationRoot,
logLevel: 'warn',
build: {
lib: {
entry: path.join(directory, 'native.ts'),
name: 'OpenMotionTemplateCards',
formats: ['iife'],
fileName: () => 'app.js',
},
outDir: path.join(directory, 'build'),
emptyOutDir: true,
minify: 'esbuild',
sourcemap: false,
cssCodeSplit: false,
rollupOptions: { output: { inlineDynamicImports: true } },
},
});
await build({
configFile: false, root: applicationRoot, logLevel: 'warn',
build: {
lib: { entry: path.join(directory, 'app.ts'), name: 'OpenMotionTemplateCards', formats: ['iife'], fileName: () => 'inline.js' },
outDir: path.join(directory, 'build'), emptyOutDir: false,
minify: 'esbuild', sourcemap: false, cssCodeSplit: false,
},
});
const styles = await Promise.all(['packages/design-system/tokens.css', 'packages/design-system/studio/feedback.css',
'packages/design-system/studio/template-cards.css',
'mcp-ui/template-recommendations/app.css'].map(file => readFile(path.join(applicationRoot, file), 'utf8')));
await writeFile(path.join(directory, 'build/app.css'), styles.join('\n'));
await writeFile(path.join(applicationRoot, identityPath), JSON.stringify(await buildIdentity(applicationRoot), null, 2) + '\n');
await checkTemplateCardsBuild();
// Validate the same bytes and final JSON envelope that production will serve,
// including source fingerprints, media identities, metadata and the budget.
const { loadTemplatePresentation } = await import('../shared/mcp-template-recommendations.ts');
const presentation = loadTemplatePresentation({ directory, applicationRoot });
const bytes = Buffer.byteLength(JSON.stringify({ jsonrpc: '2.0', id: 1, result: { contents: [presentation.content] } }));
console.log(`Template cards: ${bytes} UTF-8 response bytes, resource ${presentation.resourceVersion}`);
return { bytes, resourceVersion: presentation.resourceVersion, uri: presentation.uri };
}
if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) await buildTemplateCards();