import { createHash } from 'node:crypto'; import { readFile } from 'node:fs/promises'; import { dirname, resolve } from 'node:path'; import { fileURLToPath } from 'node:url'; const root = resolve(dirname(fileURLToPath(import.meta.url)), '..'); const source = JSON.parse(await readFile(resolve(root, 'vendor/wllama-bonsai/SOURCE.json'), 'utf8')); const readme = await readFile(resolve(root, 'README.md'), 'utf8'); if (source.wllamaRevision !== '912c18b75d4358c1405a64646b8dbe43a205943b') { throw new Error(`Unexpected wllama revision: ${String(source.wllamaRevision)}`); } if (source.llamaCppRevision !== '00fa7cb284cbf133fc426733bd64238a3588a33e') { throw new Error(`Unexpected llama.cpp revision: ${String(source.llamaCppRevision)}`); } if (source.schemaVersion !== 2 || !source.patchSet?.sha256 || !source.build?.dawnArchiveSha256) { throw new Error('Vendored Bonsai wllama provenance is incomplete'); } const patchBytes = await readFile(resolve(root, source.patchSet.path)); const patchDigest = createHash('sha256').update(patchBytes).digest('hex'); if (patchBytes.byteLength !== source.patchSet.bytes || patchDigest !== source.patchSet.sha256) { throw new Error(`Bonsai wllama patch-set mismatch: ${patchDigest}`); } const readmeProvenance = [ ['custom patch set', source.patchSet.sha256], ['custom ESM', source.files.find((file) => file.path === 'vendor/wllama-bonsai/esm/index.js')?.sha256], ['JSPI WASM', source.files.find((file) => file.path === 'public/wasm/wllama.wasm')?.sha256], ['compat WASM', source.files.find((file) => file.path === 'public/wasm/wllama-compat.wasm')?.sha256], ['compat worker', source.files.find((file) => file.path === 'public/wasm/wllama-compat.js')?.sha256], ]; for (const [label, digest] of readmeProvenance) { if (!digest || !readme.includes(`- ${label}: \`${digest}\``)) { throw new Error(`README provenance is stale or incomplete for ${label}`); } } for (const file of source.files) { const bytes = await readFile(resolve(root, file.path)); if (bytes.byteLength !== file.bytes) { throw new Error(`Byte-size mismatch for ${file.path}: ${bytes.byteLength}`); } const digest = createHash('sha256').update(bytes).digest('hex'); if (digest !== file.sha256) { throw new Error(`SHA-256 mismatch for ${file.path}: ${digest}`); } if (file.path.startsWith('public/')) { const distPath = `dist/${file.path.slice('public/'.length)}`; const distBytes = await readFile(resolve(root, distPath)); const distDigest = createHash('sha256').update(distBytes).digest('hex'); if (distBytes.byteLength !== file.bytes || distDigest !== file.sha256) { throw new Error(`Checked-in production artifact mismatch for ${distPath}: ${distDigest}`); } } console.log(`${file.path} ${digest}`); } const declarations = await readFile(resolve(root, 'vendor/wllama-bonsai/esm/types/types.d.ts'), 'utf8'); if (!declarations.includes('offload_token_embedding?: boolean')) { throw new Error('Vendored Bonsai wllama declarations lack offload_token_embedding'); } if (!declarations.includes('n_ubatch?: number')) { throw new Error('Vendored Bonsai wllama declarations lack n_ubatch'); } const wllamaDeclarations = await readFile(resolve(root, 'vendor/wllama-bonsai/esm/wllama.d.ts'), 'utf8'); if (!wllamaDeclarations.includes('forceCompat?: boolean')) { throw new Error('Vendored Bonsai wllama declarations lack forceCompat'); } const oaiDeclarations = await readFile(resolve(root, 'vendor/wllama-bonsai/esm/types/oai-compat.d.ts'), 'utf8'); if (!oaiDeclarations.includes('export interface ChatCompletionLogprob {\n id: number;')) { throw new Error('Vendored Bonsai wllama declarations lack sampled token ids'); } if (!oaiDeclarations.includes('return_progress?: boolean;')) { throw new Error('Vendored Bonsai wllama declarations lack prompt-progress opt-in'); } if (!oaiDeclarations.includes('export interface PromptProgress')) { throw new Error('Vendored Bonsai wllama declarations lack prompt-progress payloads'); } if (!oaiDeclarations.includes('prompt_progress?: PromptProgress;')) { throw new Error('Vendored Bonsai wllama declarations do not expose streamed prompt progress'); } const moduleSource = await readFile(resolve(root, 'vendor/wllama-bonsai/esm/index.js'), 'utf8'); if (!moduleSource.includes('n_ubatch: params.n_ubatch')) { throw new Error('Vendored Bonsai wllama module does not forward n_ubatch'); } if (!moduleSource.includes('const forceCompat = this.config.forceCompat === true')) { throw new Error('Vendored Bonsai wllama module does not implement forceCompat'); }