diff --git a/docs/standards/architecture-current-state.md b/docs/standards/architecture-current-state.md index fd33fa1..6537226 100644 --- a/docs/standards/architecture-current-state.md +++ b/docs/standards/architecture-current-state.md @@ -22,7 +22,7 @@ - 图片**新写入主通道 = IndexedDB Blob `{__blob}` / key `imgblob__`** (A) `app.js:2143`;`{__cache}`(Cache Storage)为兜底;\`ref_\`(`app.js` 内定义的 `IDB_REF_PREFIX = 'ref_'`)为 legacy 形态 (A)。 - 大图判定:`isBigDataUrl` = `data:` 且 `length > 3000` (A) `app.js:2018`;**画布保存保留阈值** `< 16384` 才原样保留 (A) `app.js:2182`。**两处阈值并存,含义不同**。 - 单测是**源码契约测试**:`tests/` 目录共 **493** 处源码文本钉点(`source.includes` / `source.indexOf` / `/re/.test(source)`),分布在 **20** 个文件(其中 `iwb-v32-web-test.js` 一个就占 **407**)→ **结构性重构会先打红测试**。**该数字已由 `tests/source-text-ratchet-test.js` 的「只减不增」棘轮守门**(2026-10-20 起守卫范围从 1 个文件扩到全目录:新增任何一处源码文本钉点都会打红)。**2026-11-12 复算**(`npm run audit:pins`,v3.24.76):二分 STRUCT **417** / NAME **76**;形态 **POS 403 / NEG 87 / COUNT 3**;难度 **易 62 / 中 316 / 难 115**。钉点减法第一批(v3.24.72)把 **537 → 505**:8 条探针钉点改为「门禁内 spec 是否真的读该计数器」、5 条改为运行时断言(生产态 `window` 上无探针)、10 条改为 DOM 断言(质量档位禁用 / 传输页无三模式按钮 / 模型项 title)、4 条改为运行时对象断言(健康检查两域、策略对象冻结)、其余 9 条有同文件 DOM 等价断言或仅剩 NEG。第二批(v3.24.76)把 **505 → 493**:94-5~7 三条「多标签释放语义」(10 处正则)换成 `tests/iwb-tabs-release.spec.js` 的 3 条真 e2e,剩 1 条「消费方契约」(断言该 spec 在门禁内且 spec 源码真的读两个探针);另 2 条 `wPurgeIntent.add` 钉点改为读 `src/workspace-purge.js`。**这一批一换就查出 3 个真缺陷**(objectURL 从未释放 / 彻底删除留孤儿标签 · session / 刷新后误回收打开标签的图)—— 见 CHANGELOG v3.24.76。 -- `app.js` = 单 IIFE **8,508 行**(**双层预算**:绝对上限 8,700 / 占总源码 ≤ 40%,现 **36.8%**,2026-11-12 起;此前为单一上限 8,509、零余量);`Workbench()` = **2,998 行**(上限 **3,030**,余量 32;2026-11-16 把生成取消族 `cancelRunsFor`/`cancelRun`/`cancelTaskJobs` 与工作区列表写回族 `persistWs`/`duplicateWs` 搬进 `src/runtime-workbench-logic.js`(`createCancelActions` / `createWorkspaceListActions`),并删掉全仓零调用的 `runAll`,3090 → 2,998);大量逻辑已抽到 `src/*.js`(现 **107** 个模块 / 14,607 行,`sourceModules` 107 项;v3.24.68 新增 `clipboard-graph.js`,v3.24.74 新增 `image-admit.js`,v3.24.75 新增 `image-dedupe.js`,v3.24.76 新增 `workspace-purge.js`,v3.24.79 新增 `generation-provider-ping.js`,v3.24.80 删除无调用的 `createWBFlushViewport` 17 行)。 +- `app.js` = 单 IIFE **8,508 行**(**双层预算**:绝对上限 8,700 / 占总源码 ≤ 40%,现 **36.8%**,2026-11-12 起;此前为单一上限 8,509、零余量);`Workbench()` = **2,998 行**(上限 **3,030**,余量 32;2026-11-16 把生成取消族 `cancelRunsFor`/`cancelRun`/`cancelTaskJobs` 与工作区列表写回族 `persistWs`/`duplicateWs` 搬进 `src/runtime-workbench-logic.js`(`createCancelActions` / `createWorkspaceListActions`),并删掉全仓零调用的 `runAll`,3090 → 2,998);大量逻辑已抽到 `src/*.js`(现 **108** 个模块 / 14,607 行,`sourceModules` 108 项;v3.24.68 新增 `clipboard-graph.js`,v3.24.74 新增 `image-admit.js`,v3.24.75 新增 `image-dedupe.js`,v3.24.76 新增 `workspace-purge.js`,v3.24.79 新增 `generation-provider-ping.js`,v3.24.80 删除无调用的 `createWBFlushViewport` 17 行)。 - 门禁:`npm run verify` = `verify:ci`(`build` → `node --check app.js` → `node --check dist/app.js` → `check-dist.js` → `lint`(ESLint 正确性规则,`src/` + `app.js`,P4-16a)→ `test:unit` → `test` → `test:facts`)+ `test:e2e` (A) `package.json`。 ## 2. 门禁与环境现状 @@ -167,7 +167,7 @@ - 同一张图被 3 个节点引用、hydrate 成 3 个不同 objectURL 时,preview/display 缓存各自 miss → 重复解码(与 F2 相邻)。 - **F10|`refs.genScheduler` 是桩** (A) → 未接线,暂无运行时影响(P3)。 - **F11|已建但未接线的模块层(2026-10-17 接线审计;2026-10-18 复核更正)** (A) - - **判定法**:`global.X =` / `root.X =` 判「导出」;跨文件裸引用判「读」;排除自合并(`src/runtime-constants.js:9`)。`app.js` 是**唯一生产消费者**(无 HTML 入口;`sw.js` 不引用模块全局)。**107 个模块中 59 个导出**被 `app.js` 读取;其余为未接线 / 仅测试 / 零引用(见下)。 + - **判定法**:`global.X =` / `root.X =` 判「导出」;跨文件裸引用判「读」;排除自合并(`src/runtime-constants.js:9`)。`app.js` 是**唯一生产消费者**(无 HTML 入口;`sw.js` 不引用模块全局)。**108 个模块中 59 个导出**被 `app.js` 读取;其余为未接线 / 仅测试 / 零引用(见下)。 - **仅被测试引用(9 模块 + 2 导出)**:`canvas-domain`、`generation-retry`、`generation-service`、`runtime-recovery-runner`、`runtime-state-bridge`、`runtime-store`、`storage-reconcile`、`storage-result`、`workspace-service`;导出 `IWBStorageHealthFactory`、`IWBPluginHostFactory`。(`storage-result` 是**存储层共享结果类型**,被 6 个存储模块读取,**非死代码**;`runtime-store` 为状态模型 epic 地基。) - **app.js 零引用(1 模块 + 3 导出)**:模块 `generation-controller`;导出 `IWBRuntimeMetricsFactory`、`IWBStorageRuntimeFactory`、`IWBUIComponents`。`canvas-relations` 已随 `06cf211` 删除(本文档早前把它列为"唯一可删者",实际已删)。零引用证据 = `grep -rn "<名字>" .`(排除 `node_modules|dist|vendor|test-results`)**只命中其自身赋值行**。 - **半接线 / 未接线**:`IWBRuntimeStateBridge` **无 app.js 引用(仅测试)**;`IWBRuntimeRunContext` **已接线**(`runNode` 读取运行上下文);`IWBGenerationController` 是完整但**从未被调用**的工厂;`ModularWorkspaceStore`(`app.js` 内)是 `storage-workspace` 的**预留接线 seam**(当前无下游使用,保留);`window.IWBImageFolder` 自注册后读回(装载路径)。 @@ -183,7 +183,7 @@ | `storage-reconcile` | 存储对账(`createReconciler`/`check`) | 内联对账 | 存储层收口 | | `runtime-recovery-runner` | 执行恢复候选(`candidate`/`valid`/`run`) | 内联恢复 | 断点恢复 | | `canvas-domain` | 画布域模型(`commit`/`updateNode`/`deleteNodes`…) | `CanvasStore` 那一套 | Workbench 解耦(P2-9) | - - **耦合面已冻结**:`tests/architecture-boundary-guard-test.js`(进 `npm run test:unit` 末环)冻结 app.js↔src 耦合面(**86** 项)与 `sourceModules` 顺序(**107** 项);命名例外由第 5 组冻结,新增非 `IWB` 前缀导出即打红。 + - **耦合面已冻结**:`tests/architecture-boundary-guard-test.js`(进 `npm run test:unit` 末环)冻结 app.js↔src 耦合面(**86** 项)与 `sourceModules` 顺序(**108** 项);命名例外由第 5 组冻结,新增非 `IWB` 前缀导出即打红。 ## 12. 易踩的坑 @@ -254,7 +254,7 @@ **信任模型(必须明示)**:形态 A 的插件是**可信代码**(与核心同权限、无沙箱);启停与设置是 UX 与可维护性手段,**不是安全边界**(同 §13.27 教训:前端开关一定可被绕过)。 -**守门与快照**:`tests/architecture-boundary-guard-test.js` 冻结「app.js ↔ src 耦合面」(**86** 项)与 `sourceModules` 顺序(**107** 项);新增模块、新增耦合、新增 dev 探针都**必须有意重生成对应快照**(`IWB_WRITE_BOUNDARY_SNAPSHOT=1`)。契约与行为的可执行验证在 `tests/plugin-host-test.js`(宿主单测)与 `tests/_plugin-host-probe.spec.js`(真实浏览器探针,`_` 前缀 + `iwb-dev-probe`,不进 34 门禁)。 +**守门与快照**:`tests/architecture-boundary-guard-test.js` 冻结「app.js ↔ src 耦合面」(**86** 项)与 `sourceModules` 顺序(**108** 项);新增模块、新增耦合、新增 dev 探针都**必须有意重生成对应快照**(`IWB_WRITE_BOUNDARY_SNAPSHOT=1`)。契约与行为的可执行验证在 `tests/plugin-host-test.js`(宿主单测)与 `tests/_plugin-host-probe.spec.js`(真实浏览器探针,`_` 前缀 + `iwb-dev-probe`,不进 34 门禁)。 **关键陷阱**:`normalizeNodes`(现位于 `src/canvas-normalize.js`)会**静默丢弃**不在 `NODE_H` 里的节点类型 ⇒ 新增节点类型必须先登记 `NODE_H`,否则节点一刷新就消失(§13.27 规则三的同一失败模式)。 diff --git a/plugins/index.json b/plugins/index.json index 7829a1a..d068bc7 100644 --- a/plugins/index.json +++ b/plugins/index.json @@ -46,7 +46,7 @@ "style": "styles.css", "version": "1.5.0", "files": { - "main.js": "4b2381ed6c3418ed6e70073b96f1ad54f51c0a422224862c6d48292fcdf51343", + "main.js": "26ff14eea709e6c129c20ed6fee8546576892dfd65416c6308a9722ecc8576c2", "styles.css": "28a267866d81708fc51aa61a40f922decd917c9ea907c52c8c916fe6398cad68" } }, diff --git a/plugins/promptlib/main.js b/plugins/promptlib/main.js index 4e423c4..aac1391 100644 --- a/plugins/promptlib/main.js +++ b/plugins/promptlib/main.js @@ -648,6 +648,106 @@ return { node: box, sync: sync, list: function () { return list }, values: values } } + // ===== v1.5.9:数据备份(导出/导入 JSON,含封面)===== + function backupFileName () { + var d = new Date() + var mm = String(d.getMonth() + 1).padStart(2, '0') + var dd = String(d.getDate()).padStart(2, '0') + return 'promptlib-backup-' + d.getFullYear() + '-' + mm + '-' + dd + '.json' + } + function coverToDataUrl (p) { + var c = p && p.cover + if (!c) return Promise.resolve('') + if (typeof c === 'string') return Promise.resolve(c.indexOf('data:') === 0 ? c : '') + if (c.img && FILEAPI) return Promise.resolve(FILEAPI.get(String(c.img))).then(function (r) { return (r && r.ok && r.url) || '' }).catch(function () { return '' }) + return Promise.resolve('') + } + function openBackup () { + var exportBtn = el('button', { class: 'iwb-btn iwb-btn-sm iwb-btn-primary', text: '导出备份…' }) + var importBtn = el('button', { class: 'iwb-btn iwb-btn-sm', text: '导入备份…' }) + var overwrite = el('input', { type: 'checkbox' }) + var status = el('div', { class: 'iwb-hint', text: '' }) + openDialog({ + key: 'backup', + title: '提示词库 · 数据备份', + width: 620, height: 420, + actions: [el('span', { class: 'iwb-drawer-spacer' })], + build: function (bodyEl, footEl) { + var box = el('div', { class: 'iwb-plp-backup' }) + box.appendChild(el('div', { class: 'iwb-hint', text: '导出为 JSON(含封面图片),可发给他人或换机导入。' })) + box.appendChild(el('div', { class: 'iwb-hint', text: '导入默认合并:按 id 去重,已存在的不覆盖;勾选下方选项则用备份覆盖同名条目。' })) + var line = el('label', { class: 'iwb-plp-bkopt' }) + line.appendChild(overwrite) + line.appendChild(el('span', { text: '导入时覆盖同名条目' })) + box.appendChild(line) + box.appendChild(status) + bodyEl.appendChild(box) + footEl.appendChild(exportBtn) + footEl.appendChild(importBtn) + exportBtn.addEventListener('click', function () { + exportBtn.disabled = true + status.textContent = '正在导出…' + Promise.all(prompts.map(function (p) { + return coverToDataUrl(p).then(function (url) { + var item = Object.assign({}, p) + if (url) item.cover = url + else delete item.cover + return item + }) + })).then(function (list) { + var payload = { app: 'image-workbench', kind: 'promptlib-backup', version: 1, exportedAt: Date.now(), prompts: list, groups: groupsOf() } + return Promise.resolve(api.exportText(backupFileName(), JSON.stringify(payload, null, 2))) + }).then(function (r) { + exportBtn.disabled = false + if (r && r.ok) { status.textContent = '已导出:' + (r.path || backupFileName()); notify('已导出提示词备份') } + else if (r && r.canceled) status.textContent = '已取消' + else { status.textContent = '导出失败:' + ((r && r.error) || '未知错误'); notify('导出失败', 'err') } + }).catch(function (e) { exportBtn.disabled = false; status.textContent = '导出失败:' + ((e && e.message) || e); notify('导出失败', 'err') }) + }) + importBtn.addEventListener('click', function () { + importBtn.disabled = true + status.textContent = '等待选择文件…' + Promise.resolve(api.importText()).then(function (r) { + if (!r || !r.ok) { importBtn.disabled = false; status.textContent = (r && r.canceled) ? '已取消' : ('导入失败:' + ((r && r.error) || '')); return } + var payload = null + try { payload = JSON.parse(r.text) } catch (e) { payload = null } + if (!payload || !Array.isArray(payload.prompts)) { importBtn.disabled = false; status.textContent = '不是有效的提示词备份文件'; notify('导入失败:文件格式不对', 'err'); return } + return Promise.all(payload.prompts.map(function (p) { + var c = p && p.cover + if (typeof c !== 'string' || c.indexOf('data:') !== 0 || !FILEAPI) return Promise.resolve(p) + var h = 5381 + for (var i2 = 0; i2 < c.length; i2++) h = ((h << 5) + h + c.charCodeAt(i2)) >>> 0 + var name = 'c' + h.toString(36) + '.jpg' + return Promise.resolve(FILEAPI.put(name, c)).then(function (res) { + return res && res.ok ? Object.assign({}, p, { cover: { img: name } }) : p + }).catch(function () { return p }) + })).then(function (incoming) { + var byId = {} + prompts.forEach(function (p) { byId[p.id] = p }) + var added = 0 + incoming.forEach(function (p) { + if (!p || !p.id) return + if (byId[p.id] && !overwrite.checked) return + if (!byId[p.id]) added++ + byId[p.id] = normalize(p) + }) + prompts = Object.keys(byId).map(function (k) { return byId[k] }) + var gs = groupsOf().slice() + ;(payload.groups || []).forEach(function (g) { + var name = typeof g === 'string' ? g : (g && g.name) + if (name && !gs.some(function (x) { return x.name === name })) gs.push({ id: name, name: name }) + }) + groups = gs + importBtn.disabled = false + status.textContent = '已导入 ' + incoming.length + ' 条(新增 ' + added + ' 条)' + return persist().then(function () { render(); notify('已导入 ' + incoming.length + ' 条提示词') }) + }) + }).catch(function (e) { importBtn.disabled = false; status.textContent = '导入失败:' + ((e && e.message) || e); notify('导入失败', 'err') }) + }) + }, + }) + } + function paintCoverInto (node, p) { node.textContent = '' var url = p && p._coverUrl ? p._coverUrl : coverOf(p) @@ -919,6 +1019,9 @@ chips.appendChild(addChip) var mgChip = el('button', { class: 'iwb-chip iwb-plp-add iwb-chip-add', title: '管理分组(重命名 / 排序 / 删除)', text: '⚙' }) mgChip.addEventListener('click', openManager) + var bkChip = el('button', { class: 'iwb-chip iwb-plp-add iwb-chip-add', title: '数据备份(导出/导入)', text: '⇅' }) + bkChip.addEventListener('click', openBackup) + chips.appendChild(bkChip) chips.appendChild(mgChip) fixed.appendChild(chips) diff --git a/plugins/promptlib/manifest.json b/plugins/promptlib/manifest.json index 95fe00c..a12eba3 100644 --- a/plugins/promptlib/manifest.json +++ b/plugins/promptlib/manifest.json @@ -13,7 +13,7 @@ "提示词" ], "files": { - "main.js": "4b2381ed6c3418ed6e70073b96f1ad54f51c0a422224862c6d48292fcdf51343", + "main.js": "26ff14eea709e6c129c20ed6fee8546576892dfd65416c6308a9722ecc8576c2", "styles.css": "28a267866d81708fc51aa61a40f922decd917c9ea907c52c8c916fe6398cad68" } } diff --git a/scripts/build.js b/scripts/build.js index 44dc948..01b9e31 100644 --- a/scripts/build.js +++ b/scripts/build.js @@ -70,6 +70,7 @@ const sourceModules = [ 'storage-backup.js', 'storage-zip.js', 'plugin-archive.js', + 'plugin-promptlib-backup.js', 'runtime-constants.js', 'canvas-store.js', 'generation-scheduler.js', diff --git a/src/plugin-api-promptlib.js b/src/plugin-api-promptlib.js index 754f2c7..94afdda 100644 --- a/src/plugin-api-promptlib.js +++ b/src/plugin-api-promptlib.js @@ -64,6 +64,9 @@ fileList: fileOp('list'), fileRemove: fileOp('remove'), fileStat: fileOp('stat'), + // 备份文件读写(宿主的另存为/打开对话框) + exportText: function (name, text) { const B = (typeof global !== 'undefined') ? global.IWBPromptlibBackup : null; return (B && typeof B.saveText === 'function') ? B.saveText(text, name) : Promise.reject(new Error('备份能力未就绪')) }, + importText: function () { const B = (typeof global !== 'undefined') ? global.IWBPromptlibBackup : null; return (B && typeof B.pickText === 'function') ? B.pickText() : Promise.reject(new Error('备份能力未就绪')) }, onChanged: function (fn) { if (typeof fn !== 'function') return function () {} listeners.push(fn) diff --git a/src/plugin-promptlib-backup.js b/src/plugin-promptlib-backup.js new file mode 100644 index 0000000..249daeb --- /dev/null +++ b/src/plugin-promptlib-backup.js @@ -0,0 +1,41 @@ +/* plugin-promptlib-backup.js —— 提示词插件备份文件的读写(v3.26.37) + * + * 插件跑在 WebView 里没有文件权限:导出/导入必须经宿主的「另存为/打开」对话框。 + * 这里只做文件 IO,内容格式(哪些条目、如何合并)由插件决定。 + */ +;(function (global) { + 'use strict' + + function tauri () { return (typeof global.__TAURI__ !== 'undefined') ? global.__TAURI__ : null } + + async function saveText (text, name) { + const T = tauri() + if (!T || !T.dialog || !T.fs) return { ok: false, error: '桌面版文件对话框不可用' } + try { + const path = await T.dialog.save({ + defaultPath: String(name || 'promptlib-backup.json'), + filters: [{ name: '提示词备份', extensions: ['json'] }], + }) + if (!path) return { ok: false, canceled: true } + await T.fs.writeTextFile(path, String(text || '')) + return { ok: true, path: path } + } catch (e) { return { ok: false, error: String((e && e.message) || e) } } + } + + async function pickText () { + const T = tauri() + if (!T || !T.dialog || !T.fs) return { ok: false, error: '桌面版文件对话框不可用' } + try { + const picked = await T.dialog.open({ + multiple: false, + filters: [{ name: '提示词备份', extensions: ['json'] }], + }) + const path = Array.isArray(picked) ? picked[0] : picked + if (!path) return { ok: false, canceled: true } + const text = await T.fs.readTextFile(path) + return { ok: true, text: text, path: path } + } catch (e) { return { ok: false, error: String((e && e.message) || e) } } + } + + global.IWBPromptlibBackup = { saveText: saveText, pickText: pickText } +})(typeof window !== 'undefined' ? window : globalThis) diff --git a/tests/fixtures/iwb-source-modules-order.json b/tests/fixtures/iwb-source-modules-order.json index 9fe9c41..c600c93 100644 --- a/tests/fixtures/iwb-source-modules-order.json +++ b/tests/fixtures/iwb-source-modules-order.json @@ -42,6 +42,7 @@ "storage-backup.js", "storage-zip.js", "plugin-archive.js", + "plugin-promptlib-backup.js", "runtime-constants.js", "canvas-store.js", "generation-scheduler.js",