From 7ed3ba6738a2301027254472fbb176cb9de81b17 Mon Sep 17 00:00:00 2001 From: qiqi Date: Fri, 20 Nov 2026 09:08:56 +0800 Subject: [PATCH] =?UTF-8?q?feat(plugin):=20=E6=8F=92=E4=BB=B6=E7=9B=AE?= =?UTF-8?q?=E5=BD=95=E5=8C=96=20M1=20=E2=80=94=E2=80=94=20=E7=9B=AE?= =?UTF-8?q?=E5=BD=95=E6=8F=92=E4=BB=B6=E5=8A=A0=E8=BD=BD=20+=20=E6=97=A7?= =?UTF-8?q?=E5=8D=95=E6=96=87=E4=BB=B6=E5=85=BC=E5=AE=B9=20v3.25.41?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - loader 新增 loadFolder():manifest → main.js(逐文件 sha256,内置信随包 index.json)→ blob 注入; 样式文本暂存,激活时注入、停用时移除(配合 host 新增 onActivate/onDeactivate 钩子) - storage-file 增加 plugins.listDirs/readFile/writeFile/writeBinary/mkdir/removeDir(按段校验,禁 ..) - app.js:三路合并支持目录条目(dir/main/style/files),失败隔离 + 按 id 排序 - 修复:无 iwb_plugins 记录时插件永不激活(首次安装面板显示「未加载」) - 备份模块兼容目录形态;市场暂时跳过目录插件(M3 zip 后恢复) - 试点:image-annotate 转为 plugins/image-annotate/{manifest.json,main.js} - 方案文档 docs/PLUGIN-ARCHITECTURE-PLAN.md 一并入库 --- CHANGELOG.md | 11 + app.js | 21 +- docs/PLUGIN-ARCHITECTURE-PLAN.md | 354 ++++++++++++++++++ package.json | 2 +- .../main.js} | 0 plugins/image-annotate/manifest.json | 17 + plugins/index.json | 7 +- scripts/copy-plugins.js | 15 +- scripts/gen-market.js | 5 +- scripts/sync-plugins-from-repo.js | 35 +- src-tauri/Cargo.lock | 2 +- src-tauri/Cargo.toml | 2 +- src-tauri/tauri.conf.json | 2 +- src/plugin-host.js | 7 + src/runtime-plugin-backup.js | 42 ++- src/runtime-plugin-loader.js | 109 +++++- src/storage-file.js | 64 +++- 17 files changed, 673 insertions(+), 22 deletions(-) create mode 100644 docs/PLUGIN-ARCHITECTURE-PLAN.md rename plugins/{runtime/plugin-image-annotate.7a865c7c353c.js => image-annotate/main.js} (100%) create mode 100644 plugins/image-annotate/manifest.json diff --git a/CHANGELOG.md b/CHANGELOG.md index e804070..7213cf6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,17 @@ > 3. 核验正式地址版本 → 之后才移动 `stable` / `stable-verified-<日期>` 标签、更新本 CHANGELOG、写 GitHub Release(`gh release create`)。 > 测试发现问题 → 改 → 再 `deploy:test`,正式环境不受影响。回滚点 `git reset --hard stable`。 +## v3.25.41(2026-11-20)插件目录化 M1:目录插件加载 + 兼容旧单文件 + +- 插件支持**目录形态**:`//{manifest.json,main.js[,styles.css]}`;内置 `image-annotate` 转为目录做试点,其余 7 个仍是单文件(双轨兼容)。 +- 加载器新增 `loadFolder()`:读 manifest → 逐文件 **sha256 校验**(内置以随包 index.json 为准)→ blob 注入 → 样式文本暂存,**激活时才注入**、停用时移除。 +- 目录插件失败隔离:单个插件 manifest 损坏/hash 不符只记 `error`,不中断其余插件加载;加载顺序按 id 稳定排序。 +- 新增磁盘能力:`plugins.listDirs/readFile/writeFile/writeBinary/mkdir/removeDir`(路径按段校验,禁 `..`)。 +- 修复历史 bug:**首次安装(`iwb_plugins` 无记录)时所有插件都不会激活**,表现为面板显示「xxx 未加载」;现在无记录即默认启用。 +- 插件备份(`runtime-plugin-backup`)兼容目录形态(收集 main+style,恢复写回目录)。 +- 市场索引暂时跳过目录插件(zip 打包在 M3 落地后恢复)。 +- 同步脚本 `sync-plugins-from-repo.js` / `copy-plugins.js` 支持产出与打包目录插件。 + ## v3.25.40(2026-11-20)图库设定行去掉文字标签,只留三个窄下拉 - 去掉「插入 / 显示 / 时间」三个文字标签(按用户选择),三个下拉直接并排:`0.5K | 特大 | 全部`。 diff --git a/app.js b/app.js index 3ffaee1..22eac91 100644 --- a/app.js +++ b/app.js @@ -8678,6 +8678,8 @@ items.push(el('button', { key: 'bgc', className: 'iwb-ctx-item', onClick: (e) => } } catch (e) { /* 开发者模式读取失败不影响启动 */ } for (const e of ext) { const id = String((e && e.id) || ''); if (id) byId.set(id, Object.assign({}, e, { source: 'external' })) } + // 目录插件(v3.25.41):扫描 <资料目录>/plugins/*/manifest.json;与单文件同 id 时目录优先 + try { const L0 = wPluginLoader(); if (L0 && L0.listFolderEntries) for (const e of await L0.listFolderEntries()) if (!byId.has(e.id)) byId.set(e.id, e) } catch (e2) { /* 目录扫描失败不影响内置加载 */ } // 关键:加载前先快照「用户关掉过哪些插件」,加载过程绝不能覆盖这个状态 let persistedState = {} try { @@ -8696,9 +8698,18 @@ items.push(el('button', { key: 'bgc', className: 'iwb-ctx-item', onClick: (e) => const file = String((entry && entry.file) || '') const isExt = (entry && entry.source) === 'external' const isLocalEntry = (entry && entry.source) === 'local' - const record = { id, file, version: (entry && entry.version) || '', status: 'loading', error: null, code: (entry && entry.code) || '', source: isExt ? 'external' : ((entry && entry.source === 'local') ? 'local' : 'builtin') } + const dir = String((entry && entry.dir) || '') + const mainName = String((entry && entry.main) || 'main.js') + const record = { id, dir, main: mainName, style: String((entry && entry.style) || ''), hashes: (entry && entry.files) || null, file: dir ? dir + '/' + mainName : file, version: (entry && entry.version) || '', status: 'loading', error: null, code: (entry && entry.code) || '', source: isExt ? 'external' : ((entry && entry.source === 'local') ? 'local' : 'builtin') } wRuntimePlugins.push(record) - if (!id || !/^[A-Za-z0-9._-]+$/.test(file)) { record.status = 'bad-entry'; continue } + if (!id) { record.status = 'bad-entry'; continue } + if (dir) { + if (!/^[A-Za-z0-9][A-Za-z0-9._-]{1,63}$/.test(dir) || !/^[A-Za-z0-9._-]+\.js$/.test(mainName)) { record.status = 'bad-entry'; record.error = '目录或入口名不合法'; continue } + if (!record.hashes || !record.hashes[mainName]) { record.status = 'bad-entry'; record.error = '缺少文件哈希(拒绝加载)'; continue } + const LF = wPluginLoader(); if (PluginHost && LF && LF.loadFolder) pending.push(LF.loadFolder(record)); else record.status = 'idle' + continue + } + if (!/^[A-Za-z0-9._-]+$/.test(file)) { record.status = 'bad-entry'; continue } const integrity = String((entry && entry.integrity) || '') // fail-closed:无 integrity 声明即拒绝加载(清单由 KV 发布脚本生成、必带 integrity;缺失视为不可信) if (!integrity && !isLocalEntry) { record.status = 'bad-entry'; record.error = 'integrity 缺失(拒绝加载)'; continue } @@ -8719,8 +8730,8 @@ items.push(el('button', { key: 'bgc', className: 'iwb-ctx-item', onClick: (e) => for (const r of (wRuntimePlugins || [])) { if (!r || r.status !== 'loaded') continue const rec = st[r.id] - if (!rec) continue - const want = rec.enabled !== false + // 没有持久化记录 ⇒ 默认启用(修复:首次安装/清空存储后插件永远不激活,面板显示"未加载") + const want = !rec || rec.enabled !== false if (!PluginHost || typeof PluginHost.setEnabled !== 'function') continue // 注意:插件是在 init() 之后才注册的,init 的"激活已启用项"覆盖不到它们。 // 所以这里对 want=true 一律再 setEnabled(true)(activate 幂等:已激活会返回 already), @@ -8736,6 +8747,8 @@ items.push(el('button', { key: 'bgc', className: 'iwb-ctx-item', onClick: (e) => if (PluginHost && typeof PluginHost.init === 'function') { try { PluginHost.init({ + onActivate: (id) => { const L = wPluginLoader(); if (L && L.applyStyleFor) L.applyStyleFor(id) }, + onDeactivate: (id) => { const L = wPluginLoader(); if (L && L.removeStyle) L.removeStyle(id) }, readState: () => { const v = lsGet('iwb_plugins', {}); return v && typeof v === 'object' ? v : {} }, writeState: (state) => { lsSet('iwb_plugins', state) }, log: () => { try { wLog.apply(null, arguments) } catch (e) { /* ignore */ } }, diff --git a/docs/PLUGIN-ARCHITECTURE-PLAN.md b/docs/PLUGIN-ARCHITECTURE-PLAN.md new file mode 100644 index 0000000..ebe933f --- /dev/null +++ b/docs/PLUGIN-ARCHITECTURE-PLAN.md @@ -0,0 +1,354 @@ +# 插件目录化 + 三个库插件化 · 完整方案 + +> v2(2026-11-20)· 已并入「桌面端定位」「展示页例外」「不做分类」三项裁定 +> 范围:插件从「单文件」改为「文件夹」;随后把 **图库 / 提示词库 / 节点组库** 改为插件 +> 非目标:分类插件(画布分类维持现状);插件安全沙箱;画布内核重构 + +--- + +## 0. 决策摘要(本方案按这 10 条执行) + +| # | 决策 | 状态 | +|---|---|---| +| 1 | 应用本体 = 独立桌面端(Tauri);产品代码不加浏览器降级分支 | 已定 | +| 2 | **例外**:插件展示页 / 市场页 / 下载页仍是静态网页,继续维护 | 已定 | +| 3 | 插件格式 = 目录(`manifest.json + main.js + styles.css + data.json + assets/`) | 本方案 | +| 4 | 插件数据落 `<资料目录>/plugin-data/.json`(与代码目录分离,卸载默认保留) | 建议默认 | +| 5 | 市场安装 = zip 整包 + 逐文件 sha256;安装仅桌面 | 建议默认 | +| 6 | 旧单文件插件保留一个版本周期的双轨兼容 | 本方案 | +| 7 | **不做**分类插件(`iwb_home_cats` + `workspace.category` 维持现状) | 已定 | +| 8 | 插件不得直接改画布:插入/实例化一律走宿主 capability | 本方案 | +| 9 | 市场包用 **ZIP STORE(不压缩)**,绕开旧 WebView 无 `DecompressionStream` 的兼容坑 | 复查新增 | +| 10 | 图库/提示词/节点组插件化后,**保留宿主面板一个版本周期**可一键退回(`iwb_panels_mode`) | 复查新增 | + +--- + +## 1. 现状(代码核对过的事实) + +| 环节 | 现状 | 位置 | +|---|---|---| +| 插件产物 | 单个 IIFE `..js`,内容寻址 | `plugins/runtime/` | +| 清单 | `index.json`:`{id,file,version,integrity}`;展示信息在 `meta.json` | `plugins/index.json` | +| 注册契约 | 插件自注册 `IWBPluginHost.register(manifest, factory)` | 各插件末尾 | +| 内置加载 | `