Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions builder/cli.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { basename, dirname, join, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import { buildApp, claimLines } from './build.mjs';
import { buildSet } from './set.mjs';
import { productUi } from './ui.mjs';
import { TOOL } from './tool.mjs';
import { SpecError } from './vendor/prism-code/src/journey/spec.mjs';

Expand Down Expand Up @@ -100,6 +101,8 @@ try {
// The newest commit of the set or any of its prompts dates the threat model.
const lastModified = flag('--last-modified') ?? [file, ...set.prompts.map((p) => join(dirname(file), p))].map(committed).sort().pop();
const built = await buildSet(set, prompts, { lastModified, current });
const product = current('prism/product.json');
if (product) Object.assign(built.files, productUi(set, prompts, JSON.parse(product)));
if (rest.includes('--check')) {
const stale = [...Object.entries(built.files).filter(([path, text]) => current(path) !== text).map(([path]) => path), ...built.remove.filter((path) => current(path) !== undefined)];
for (const path of stale) console.error(`stale: ${path}`);
Expand Down
55 changes: 55 additions & 0 deletions builder/ui.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* The product page of a prompt set: `site/product/index.html`, written from the
* set, its prompts and the product record (`prism/product.json`), and the lock
* that binds it, `prism/builder.lock`. The page decides nothing: every answer is
* the verified holo's, run in the reader's browser after its archive identities
* verify and its acceptance vectors replay. The page loads the holo and the
* prism-code runtime the Pages workflow publishes beside it.
*/
import { createHash } from 'node:crypto';
import { readFileSync } from 'node:fs';

const PAGE = readFileSync(new URL('./ui/product.html', import.meta.url), 'utf8');
const LOADER = `import { parseArchive, resolveArchive, asBytes } from "../vendor/prism-code/src/archive.mjs";
import { compileGuest, runGuest, runAcceptance } from "../vendor/prism-code/src/core.mjs";
import { blake3Hex } from "../vendor/prism-code/src/blake3.mjs";
async function openHolo() {
const resolved = resolveArchive(parseArchive(new Uint8Array(await (await fetch("../app.holo")).arrayBuffer())));
const a = resolved.modelDocument.application;
const module = await compileGuest(resolved.guest.content);
const limits = { request: a.request_maximum, response: a.response_maximum, guestAllocation: a.guest_allocation_maximum };
const vectors = (a.acceptance_vectors ?? []).map((v) => ({ request: Uint8Array.from(v.request), response: Uint8Array.from(v.response) }));
const dispatch = async (request) => runGuest(module, asBytes(request), limits);
return { identities: resolved.identities, vectors, dispatch, acceptance: () => runAcceptance(vectors, dispatch) };
}`;

/** The design system's tokens as CSS custom properties, light and dark, from `product.design`. */
function tokens(d) {
const kebab = (k) => k.replace(/[A-Z]/g, (c) => `-${c.toLowerCase()}`);
const colors = (c) => Object.entries(c).map(([k, v]) => `--${kebab(k)}: ${v};`).join(' ');
const fixed = [
`--f-display: "${d.type.display}", system-ui, sans-serif; --f-body: "${d.type.body}", system-ui, sans-serif; --f-mono: "${d.type.mono}", ui-monospace, monospace;`,
Object.entries(d.type.scale).map(([k, v]) => `--t-${k}: ${v};`).join(' '),
Object.entries(d.space).map(([k, v]) => `--s-${k}: ${v};`).join(' '),
Object.entries(d.radius).map(([k, v]) => `--r-${k}: ${v};`).join(' '),
].join('\n ');
return `/* Design system: ${d.accessibility.standard} ${d.accessibility.level}. Generated from prism/product.json. */
:root {\n ${fixed}\n ${colors(d.color.light)} color-scheme: light;\n}
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) { ${colors(d.color.dark)} color-scheme: dark; } }
:root[data-theme="dark"] { ${colors(d.color.dark)} color-scheme: dark; }`;
}

export function productUi(set, prompts, product) {
const model = { set, prompts: Object.fromEntries(prompts.map((p) => [p.app.name, p])), product };
const page = PAGE.replace('/*LOADER*/', () => LOADER)
.replace('/*TOKENS*/', () => tokens(product.design))
.replace('/*MODEL*/', () => JSON.stringify(model).replace(/</g, '\\u003c'))
.replaceAll('{{TITLE}}', product.brand.name);
const files = { 'site/product/index.html': page };
const lock = {
spec: 'prism/builder-lock/1',
source: { repository: set.builder.repository, commit: set.builder.commit },
files: Object.entries(files).map(([path, text]) => ({ path, sha256: createHash('sha256').update(text).digest('hex') })),
};
return { ...files, 'prism/builder.lock': `${JSON.stringify(lock, null, 2)}\n` };
}
Loading
Loading