/* plugin-api-promptlib.js —— 宿主 promptlib capability(M6:提示词库插件化) * * 给插件「提示词 + 提示词分组」的读写面: * list() → { prompts, groups } * save({prompts, groups}) → 落盘(宿主仍持有数据:iwb_web_prompts + 分组缓存) * insert(text, name) → 在画布视口中心插入提示词节点(插件不碰画布状态) * copy(text) → 走宿主剪贴板(失败有可见提示) * notify(text, type) → 宿主 toast / 提示条 * onChanged(fn) → 数据变化(收藏/其它标签页同步/插件自己保存) * * 与 gallery / grouplib 同一套「impl 桥」:capability 在 PluginHost.init 时创建(早于 Workbench 渲染), * 这里只持有稳定代理,真实实现由 Workbench 每次渲染时 setImpl 注入。 */ ;(function (global) { 'use strict' let impl = null const listeners = [] function setImpl (next) { impl = (next && typeof next === 'object') ? next : null } function call (name) { return function () { const fn = impl && impl[name] if (typeof fn !== 'function') return Promise.reject(new Error('提示词能力未就绪')) try { return Promise.resolve(fn.apply(null, arguments)) } catch (e) { return Promise.reject(e) } } } function emit (reason) { for (const fn of listeners.slice()) { try { fn(reason || 'changed') } catch (e) { /* 插件回调异常不影响宿主 */ } } } const API = { list: call('list'), save: call('save'), insert: call('insert'), copy: call('copy'), notify: call('notify'), onChanged: function (fn) { if (typeof fn !== 'function') return function () {} listeners.push(fn) return function () { const i = listeners.indexOf(fn); if (i >= 0) listeners.splice(i, 1) } }, } // 纯函数:旧数据/缺分组时从 prompts 推导分组(与宿主 promptGroupsOf 一致) function groupsOf (prompts, existing) { if (Array.isArray(existing) && existing.length) return existing const seen = [] for (const p of (Array.isArray(prompts) ? prompts : [])) { const g = String((p && p.group) || '').trim() if (g && !seen.some((x) => x.name === g)) seen.push({ id: g, name: g }) } return seen } global.IWBPluginPromptLib = { setImpl: setImpl, api: function () { return API }, emit: emit, groupsOf: groupsOf } })(typeof window !== 'undefined' ? window : globalThis)