diff --git a/scripts/build-ui-worker.mjs b/scripts/build-ui-worker.mjs new file mode 100644 index 0000000..1ce075f --- /dev/null +++ b/scripts/build-ui-worker.mjs @@ -0,0 +1,208 @@ +#!/usr/bin/env node +/** + * build-ui-worker.mjs — 把 `console-ui/public` 內嵌成一顆單檔 worker(`arcrun-rag-ui`)。 + * + * 🔴 2026-08-15(D91/Arcrun#125):這支是**從 arcrun-rag 搬過來的**。 + * leo 原話:「今天開始出貨一律不准在 arcrun rag 或任何別的地方 build, + * **這就是 arcrun 的專屬工作**。你告訴我要把 cypher 搬到 arcrun,我說好, + * 結果搞到現在還用違反的方式。」 + * + * 實況(2026-08-14 查):五顆 tier2 worker 早就改成「向 Arcrun 取用成品」了 + * (`build-worker-artifacts.mjs` → `.worker-builds/`),**只有這顆 UI 沒搬**—— + * arcrun-rag 的 `installer/scripts/build-ui-bundle.mjs` 仍然自己讀本 repo 的 + * `console-ui/public`、自己拼裝出 worker 原始碼。於是: + * · 用戶拿到的 portal 前端,其位元組沒有任何一個 Arcrun commit 說得出來源 + * · 「成品只有一個產地」這句話對六顆零件裡的五顆成立,對第六顆不成立 + * ⇒ 規則不是被誰違反,是**它從來沒有被機械驗證過**(同 D54/D64/D90 的形狀)。 + * + * 搬過來之後,UI 與其他零件走同一條路: + * 產地(本 repo)→ `.worker-builds/arcrun-rag-ui/worker.mjs` + manifest 指紋 + * → arcrun-rag 只做「複製 + 算版本」,不再有任何一行產生 worker 原始碼。 + * + * ⚠️ 內嵌邏輯與兩道閘(世代閘、前端 JS 語法閘)**原樣搬過來,一個字沒改**—— + * 它們驗的是本 repo 的 `console-ui/public`,本來就該長在來源這一側。 + * 唯一改動:產出物開頭那行註解改成指向本檔(不留「描述不存在機制的註解」)。 + * + * 對外只有一個 API:`buildUiWorker({ uiDir })` → `{ source, fingerprint, fileCount }`。 + * 寫檔與 manifest 由 `build-worker-artifacts.mjs` 統一處理(產物格式與其他零件一致)。 + */ +import { readdirSync, statSync, readFileSync, writeFileSync } from 'node:fs'; +import { join, relative, extname } from 'node:path'; +import { createHash } from 'node:crypto'; + +/** + * @param {{ uiDir: string }} opts uiDir=`console-ui/public` 的絕對路徑 + * @returns {Promise<{ source: string, fingerprint: string, fileCount: number }>} + */ +export async function buildUiWorker({ uiDir }) { + const UI = uiDir; + + const TYPES = { + '.html': 'text/html; charset=utf-8', + '.js': 'application/javascript; charset=utf-8', + '.mjs': 'application/javascript; charset=utf-8', + '.css': 'text/css; charset=utf-8', + '.json': 'application/json; charset=utf-8', + '.svg': 'image/svg+xml', + '.png': 'image/png', + '.ico': 'image/x-icon', + }; + const TEXT_EXT = new Set(['.html', '.js', '.mjs', '.css', '.json', '.svg']); + // config.js 由 worker 動態產生(apiBase 注入),不吃靜態版 + const SKIP = new Set(['/config.js']); + + // ⚠️ 一定要 sort():指紋是 JSON.stringify(files) 算的,鍵的順序=目錄列舉順序。 + // 不排序的話同樣的內容在不同機器會得到不同指紋(B 版就漏了這一步)。 + function walk(dir, base, out) { + for (const name of readdirSync(dir).sort()) { + const p = join(dir, name); + if (statSync(p).isDirectory()) { walk(p, base, out); continue; } + const key = '/' + relative(base, p).split('\\').join('/'); + if (SKIP.has(key)) continue; + const ext = extname(name); + const type = TYPES[ext]; + if (!type) continue; // 未知型別不進 bundle + if (TEXT_EXT.has(ext)) out[key] = { type, b64: false, data: readFileSync(p, 'utf8') }; + else out[key] = { type, b64: true, data: readFileSync(p).toString('base64') }; + } + } + + // ── 閘① t160 世代閘(B 版)───────────────────────────────────────────── + // 打包前驗 portal 頁指紋——舊世代(缺「不需要人工新增」文案/含人工建庫「登記新庫」表單) + // 直接拒打包。病史:t159 重打包吃了停在舊世代的分支 public/,把 leo 實例的 UI 打回 + // 被淘汰的人工登記典範。 + // 🔴 2026-08-02 修誤判:原本直接對整份原始碼 grep「登記新庫」,但**現行世代的 HTML + // 註解裡就寫著「07-27 leo:拿掉『登記新庫』…」**——那是「已經拿掉了」的紀錄, + // 是新世代的證據,卻被當成舊世代特徵 ⇒ 閘把對的東西擋下來。 + // 正解:**先剝掉註解再驗**,只看用戶真的會看到的 UI 內容。 + { + const portalRaw = readFileSync(join(UI, 'portal', 'index.html'), 'utf8'); + const portal = portalRaw.replace(//g, ''); + if (!portal.includes('不需要人工新增') || portal.includes('登記新庫')) { + console.error('✘ 世代閘:--ui 指向的 public 不是現行世代(缺「不需要人工新增」或含「登記新庫」)——拒絕打包舊 UI。'); + console.error(` 剝註解後:不需要人工新增=${portal.includes('不需要人工新增')}/登記新庫=${portal.includes('登記新庫')}`); + process.exit(1); + } + } + + const files = {}; + walk(UI, UI, files); + if (!files['/portal/index.html']) throw new Error(`/portal/index.html 不在 ${UI} — 路徑錯了?`); + + // ── 閘② t131 前端 JS 語法閘(A 版)──────────────────────────────────── + // 2026-07-29 事故:誤刪一個 `})();` ⇒ portal 白畫面,驗收沒抓到—— + // 因為測試只測後端、grep 只看字串,**沒有人驗過前端 JS 語法**。 + // 打包前用 node --check 驗每個 HTML 內嵌 script,語法錯就拒絕產出 bundle。 + { + const { execFileSync } = await import('node:child_process'); + const { mkdtempSync } = await import('node:fs'); + const { tmpdir } = await import('node:os'); + const tmp = mkdtempSync(join(tmpdir(), 'uicheck-')); + for (const [name, f] of Object.entries(files)) { + if (!name.endsWith('.html') || f.b64) continue; + const blocks = [...String(f.data).matchAll(/]*\bsrc=)[^>]*>([\s\S]*?)<\/script>/g)]; + blocks.forEach((m, i) => { + const jsPath = join(tmp, `${name.replace(/[^\w]/g, '_')}_${i}.js`); + writeFileSync(jsPath, m[1]); + try { + execFileSync(process.execPath, ['--check', jsPath], { stdio: 'pipe' }); + } catch (e) { + const msg = String(e.stderr || e.message).split('\n').slice(0, 6).join('\n'); + throw new Error(`❌ ${name} 的第 ${i + 1} 個