diff --git a/promptlib/package.json b/promptlib/package.json index 066658a..2a3d182 100644 --- a/promptlib/package.json +++ b/promptlib/package.json @@ -1,7 +1,7 @@ { "name": "@iwb-plugin/promptlib", "private": true, - "version": "1.9.1", + "version": "1.9.2", "description": "提示词库:右栏提示词列表(搜索/分组/收藏/新建编辑/插入画布/复制/删除),数据由宿主 promptlib 能力提供", "ibwPlugin": { "id": "promptlib", diff --git a/promptlib/src/index.js b/promptlib/src/index.js index 11e8302..75e72a3 100644 --- a/promptlib/src/index.js +++ b/promptlib/src/index.js @@ -162,9 +162,16 @@ if (!item || typeof item.text !== 'string' || !item.text.trim()) return null return item } + // 分享是否附带封面图:后台设置页开关,默认带图(兼容老行为);关掉后分享文本只有文字,短很多。 + // 从 ctx.settings 读,manifest 里声明了 default:true —— 老客户端/未设置时也按“带图”走。 + function shareWithCover () { + var S = (PLP_CTX || {}).settings + return !S || typeof S.get !== 'function' || S.get('shareWithCover') !== false + } function plpSharePayload (p) { + var body = { app: 'image-workbench', kind: 'prompt-share', version: 1, sharing: { name: p.name || '', text: p.text || '', vars: detectVars(p.text || ''), group: p.group || '', tags: p.tags || [] } } + if (!shareWithCover()) return Promise.resolve(SHARE_TAG + '\n' + JSON.stringify(body)) return plpCoverUrl(p).then(function (cv) { - var body = { app: 'image-workbench', kind: 'prompt-share', version: 1, sharing: { name: p.name || '', text: p.text || '', vars: detectVars(p.text || ''), group: p.group || '', tags: p.tags || [] } } if (cv && cv.length < 300000) body.sharing.cover = cv return SHARE_TAG + '\n' + JSON.stringify(body) }) @@ -1304,8 +1311,10 @@ // 分享格式刻意做成「可读的 JSON 文本」,直接粘到微信/QQ 也能看懂;导入时两种都认: // ① 带 IWB-PROMPT 头的整段文本 ② 直接是 {kind:'prompt-share'} 的 JSON function sharePayload (p) { + var body = { app: 'image-workbench', kind: 'prompt-share', version: 1, sharing: { name: p.name || '', text: p.text || '', vars: varsOf(p), group: p.group || '', tags: p.tags || [] } } + // v1.9.2:后台「分享时附带封面图片」关掉后,连封面读取都跳过——分享文本只留文字,内容最少。 + if (!shareWithCover()) return Promise.resolve(SHARE_TAG + '\n' + JSON.stringify(body)) return coverToDataUrl(p).then(function (cv) { - var body = { app: 'image-workbench', kind: 'prompt-share', version: 1, sharing: { name: p.name || '', text: p.text || '', vars: varsOf(p), group: p.group || '', tags: p.tags || [] } } if (cv && cv.length < 300000) body.sharing.cover = cv return SHARE_TAG + '\n' + JSON.stringify(body) }) @@ -1870,11 +1879,14 @@ global.IWBPluginHost.register({ id: 'promptlib', name: '提示词库', - version: '1.9.1', + version: '1.9.2', apiVersion: 1, capabilities: ['panels', 'promptlib', 'nodeTypes', 'canvas', 'canvasWrite'], data: { schemaVersion: 3, maxBytes: 4 * 1024 * 1024 }, - settings: { panel: { type: 'custom', label: '分组与备份', render: renderPluginSettings } }, + settings: { + shareWithCover: { type: 'boolean', label: '分享时附带封面图片(关闭后分享内容更短)', default: true }, + panel: { type: 'custom', label: '分组与备份', render: renderPluginSettings }, + }, }, function (ctx) { PLP_CTX = ctx // v1.5.0:插件激活时就把旧数据迁到插件目录(不等面板打开), diff --git a/promptlib/tools/harness.mjs b/promptlib/tools/harness.mjs new file mode 100644 index 0000000..068ff3f --- /dev/null +++ b/promptlib/tools/harness.mjs @@ -0,0 +1,59 @@ +// promptlib 本地契约测试(纯 node):分享「带封面 / 不带封面」开关。 +// 运行:node promptlib/tools/harness.mjs (退出码 0 = 全过) +// 做法:静态断言 manifest 开关声明 + 从真实 src 抠出 sharePayload 函数体, +// 在受控沙箱里跑两条分支,验证关掉开关时连封面都不读、分享文本不带 cover。 +import fs from 'node:fs' +import path from 'node:path' +import { fileURLToPath } from 'node:url' +import vm from 'node:vm' + +const HERE = path.dirname(fileURLToPath(import.meta.url)) +const ROOT = path.resolve(HERE, '..') +const src = fs.readFileSync(path.join(ROOT, 'src/index.js'), 'utf8') +let failed = 0 +const ok = (name, cond) => { + if (!cond) failed++ + console.log((cond ? '✓ ' : '✗ ') + name) +} + +// 1) manifest 声明了布尔开关,默认带图(兼容老行为) +ok("manifest 有 shareWithCover 布尔开关(默认 true)", + /shareWithCover:\s*\{\s*type:\s*'boolean'[^}]*default:\s*true/.test(src)) +ok('开关 label 提示关闭后分享内容更短', /关闭后分享内容更短/.test(src)) +ok("manifest.version = 1.9.2", /version:\s*'1\.9\.2'/.test(src)) +ok('package.json version = 1.9.2', + JSON.parse(fs.readFileSync(path.join(ROOT, 'package.json'), 'utf8')).version === '1.9.2') +ok('shareWithCover() 从 ctx.settings 读、缺省按带图', + /function shareWithCover \(\)[\s\S]{0,200}S\.get\('shareWithCover'\) !== false/.test(src)) + +// 2) 两条分支:关掉开关时 coverToDataUrl 抛错也不能被调用 +const m = src.match(/ function sharePayload \(p\) \{[\s\S]*?\n \}\n function doShare/) +ok('从 src 找到 sharePayload 函数体', !!m) +const fnSrc = m ? m[0].replace(/\n function doShare$/, '') : '' +const sandbox = { + SHARE_TAG: 'IWB-PROMPT-SHARE', + varsOf: () => [], + coverToDataUrl: () => { throw new Error('关掉开关后不该读封面') }, + shareWithCover: () => false, + Promise, JSON, String, Number, Array, Object, +} +vm.createContext(sandbox) +vm.runInContext(fnSrc, sandbox) +const p = { name: '测试词', text: '一只猫', group: '分组A', tags: ['x'], cover: { img: 'a.jpg' } } + +const text = await sandbox.sharePayload(p) +const body = JSON.parse(text.split('\n')[1]) +ok('关掉开关:分享文本无 cover 字段', !('cover' in body.sharing)) +ok('关掉开关:不读取封面(stub 抛错也未触发)', true) +ok('关掉开关:名字/正文/分组仍在', + body.sharing.name === '测试词' && body.sharing.text === '一只猫' && body.sharing.group === '分组A') + +sandbox.shareWithCover = () => true +sandbox.coverToDataUrl = () => Promise.resolve('data:image/jpeg;base64,AAAA') +const text2 = await sandbox.sharePayload(p) +const body2 = JSON.parse(text2.split('\n')[1]) +ok('打开开关:分享文本仍带 cover(默认行为不变)', + body2.sharing.cover === 'data:image/jpeg;base64,AAAA') + +console.log(failed ? ('✗ ' + failed + ' 项失败') : '✓ 全部通过') +if (failed) process.exitCode = 1