openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
5 lines • 3.1 kB
JavaScript
import{definePluginEntry}from"openclaw/plugin-sdk/plugin-entry";import fs from"node:fs";import path from"node:path";import{extractErrorCode}from"openclaw/plugin-sdk/error-runtime";import{resolvePluginConfigObject}from"openclaw/plugin-sdk/plugin-config-runtime";import{resolveStateDir}from"openclaw/plugin-sdk/state-paths";import{asBoolean,asOptionalRecord,readStringValue}from"openclaw/plugin-sdk/string-coerce-runtime";import{resolveUserPath}from"openclaw/plugin-sdk/text-utility-runtime";
//!extensions/canvas/src/config-migration.ts
const RETIRED_HOST_KEYS=[`root`,`port`,`liveReload`];function readLegacyCanvasRoot(e){const t=asOptionalRecord(asOptionalRecord(e)?.canvasHost);const n=asOptionalRecord(resolvePluginConfigObject(e,`canvas`)?.host);return{...t,...n}.root}function resolveLegacyCanvasDocumentsDir(e){const t=readStringValue(readLegacyCanvasRoot(e.config))?.trim();if(!t)return null;const n=path.join(path.resolve(resolveUserPath(t,e.env)),`documents`);const r=path.resolve(e.stateDir,`canvas`,`documents`);if(n===r)return null;try{if(fs.realpathSync(n)===fs.realpathSync(r))return null}catch{}return n}function listLegacyCanvasDocumentIds(e){try{return fs.readdirSync(e,{withFileTypes:true}).filter(e=>e.isDirectory()).map(e=>e.name).toSorted()}catch(t){if(extractErrorCode(t)===`ENOENT`)return[];throw new Error(`Cannot read Canvas documents at ${e}: ${String(t)}. Keep plugins.entries.canvas.config.host.root, fix access, then rerun "openclaw doctor --fix".`,{cause:t})}}function migrateCanvasHostConfig(e){const t=asOptionalRecord(e.canvasHost);const n=resolvePluginConfigObject(e,`canvas`);const r=asOptionalRecord(n?.host);const i=readLegacyCanvasRoot(e);let a=readStringValue(i)?.includes("${")===true;if(!a)try{const t=resolveLegacyCanvasDocumentsDir({config:e,env:process.env,stateDir:resolveStateDir()});a=t!==null&&listLegacyCanvasDocumentIds(t).length>0}catch{a=true}const o=RETIRED_HOST_KEYS.filter(e=>Object.hasOwn(r??{},e)&&!(e===`root`&&a));if(!t&&o.length===0)return null;const s=structuredClone(e);delete s.canvasHost;const c=asBoolean(r?.enabled)??asBoolean(t?.enabled);const l=asOptionalRecord(s.plugins)??{};const u=asOptionalRecord(l.entries)??{};const d=asOptionalRecord(u.canvas)??{};const f=asOptionalRecord(d.config)??{};if(r||c!==void 0||a){if(c===void 0&&!a)delete f.host;else f.host={...c!==void 0?{enabled:c}:{},...a?{root:i}:{}};d.config=f;u.canvas=d;l.entries=u;s.plugins=l}const p=[];if(t)p.push(a?`Migrated canvasHost to plugins.entries.canvas.config.host; retained root for document migration retry.`:c===void 0?`Removed retired canvasHost configuration.`:`Migrated canvasHost.enabled to plugins.entries.canvas.config.host.enabled.`);if(o.length>0)p.push(`Removed retired Canvas host config: ${o.map(e=>`plugins.entries.canvas.config.host.${e}`).join(`, `)}.`);return{config:s,changes:p}}
//!extensions/canvas/setup-api.ts
var setup_api_default=definePluginEntry({id:`canvas`,name:`Canvas Setup`,description:`Lightweight Canvas setup hooks`,register(e){e.registerConfigMigration(migrateCanvasHostConfig)}});export{setup_api_default as default};