/** * `ccl setup openclaw` — install recommended OpenClaw config snippet. * * Pure function `computeOpenclawPatch` is exported for tests. * IO / diff / atomic write / backup is delegated to lib/host-config-patcher.ts. */ import { Command } from 'commander'; import { homedir } from 'os'; import { join } from 'path'; import { runHostConfigSetup, unionArrayField, type HostPatchResult, } from '../../lib/host-config-patcher.js'; export const PLUGIN_ID = 'clawclaw'; export const PLUGIN_TOOL_GLOB = 'clawclaw:*'; export interface ComputeOpenclawPatchOpts { skipPluginsAllow?: boolean; skipToolsAllow?: boolean; } export function resolveOpenclawConfigPath(env: NodeJS.ProcessEnv = process.env): string { const home = env.OPENCLAW_HOME?.trim(); if (home) return join(home, 'openclaw.json'); return join(homedir(), '.openclaw', 'openclaw.json'); } /** Pure: compute the minimal additive patch to make `clawclaw` work under `coding` profile. */ export function computeOpenclawPatch(current: unknown, opts: ComputeOpenclawPatchOpts): HostPatchResult { const changes: string[] = []; const warnings: string[] = []; const cfg: Record = current !== null && typeof current === 'object' && !Array.isArray(current) ? { ...(current as Record) } : {}; // ── plugins.allow + plugins.entries.clawclaw.enabled ────────────────── if (!opts.skipPluginsAllow) { const plugins: Record = cfg.plugins !== null && typeof cfg.plugins === 'object' && !Array.isArray(cfg.plugins) ? { ...(cfg.plugins as Record) } : {}; // Only append to plugins.allow if it already exists as a restrictive // allowlist. Never create it from scratch — that would inadvertently // restrict all other plugins (browser, etc.) that were previously // allowed by default. if (plugins.allow !== undefined) { const allowResult = unionArrayField(plugins.allow, PLUGIN_ID); if ('warning' in allowResult) { warnings.push(`plugins.allow ${allowResult.warning}`); } else { if (allowResult.added) { changes.push(`add "${PLUGIN_ID}" to plugins.allow`); } plugins.allow = allowResult.next; } } const entries: Record = plugins.entries !== null && typeof plugins.entries === 'object' && !Array.isArray(plugins.entries) ? { ...(plugins.entries as Record) } : {}; const existingEntry = entries[PLUGIN_ID] !== null && typeof entries[PLUGIN_ID] === 'object' && !Array.isArray(entries[PLUGIN_ID]) ? { ...(entries[PLUGIN_ID] as Record) } : {}; if (existingEntry.enabled === false) { warnings.push( `plugins.entries.${PLUGIN_ID}.enabled is explicitly false; not flipping it on. Remove the line manually if you want it enabled.`, ); } else if (existingEntry.enabled !== true) { existingEntry.enabled = true; entries[PLUGIN_ID] = existingEntry; plugins.entries = entries; changes.push(`set plugins.entries.${PLUGIN_ID}.enabled = true (equivalent to: openclaw plugins enable ${PLUGIN_ID})`); } cfg.plugins = plugins; } // ── tools.alsoAllow ──────────────────────────────────────────────────── if (!opts.skipToolsAllow) { const tools: Record = cfg.tools !== null && typeof cfg.tools === 'object' && !Array.isArray(cfg.tools) ? { ...(cfg.tools as Record) } : {}; const alsoAllowResult = unionArrayField(tools.alsoAllow, PLUGIN_ID); if ('warning' in alsoAllowResult) { warnings.push(`tools.alsoAllow ${alsoAllowResult.warning}`); } else { if (alsoAllowResult.added) { if (tools.alsoAllow === undefined) { changes.push(`create tools.alsoAllow with ["${PLUGIN_ID}"]`); } else { changes.push(`add "${PLUGIN_ID}" to tools.alsoAllow`); } } tools.alsoAllow = alsoAllowResult.next; } // Also add the namespace glob so host-side tool-ID matching works even // when plugin-group expansion hasn't run yet (e.g. freshly loaded plugin). const globResult = unionArrayField(tools.alsoAllow, PLUGIN_TOOL_GLOB); if ('warning' in globResult) { warnings.push(`tools.alsoAllow ${globResult.warning}`); } else { if (globResult.added) { changes.push(`add "${PLUGIN_TOOL_GLOB}" to tools.alsoAllow`); } tools.alsoAllow = globResult.next; } cfg.tools = tools; } return { next: cfg, changes, warnings }; } export function createSetupOpenclawSubcommand(): Command { return new Command('openclaw') .description('Install recommended OpenClaw config (plugins.allow + tools.alsoAllow) for the clawclaw plugin.') .option('-y, --yes', 'Apply changes (default is dry-run with diff preview)') .option('--print', 'Only print the recommended JSON patch; do not read or write files') .option('--config ', 'Override openclaw.json path (default: $OPENCLAW_HOME/openclaw.json or ~/.openclaw/openclaw.json)') .option('--skip-plugins-allow', "Do not touch plugins.allow / plugins.entries.clawclaw") .option('--skip-tools-allow', "Do not touch tools.alsoAllow") .option('--no-backup', 'Do not write a timestamped .bak.* before applying') .action((opts: { yes?: boolean; print?: boolean; config?: string; skipPluginsAllow?: boolean; skipToolsAllow?: boolean; backup?: boolean; }) => { const result = runHostConfigSetup( { hostName: 'OpenClaw', resolveConfigPath: () => resolveOpenclawConfigPath(), computePatch: (current, hostOpts) => computeOpenclawPatch(current, hostOpts), }, { skipPluginsAllow: opts.skipPluginsAllow, skipToolsAllow: opts.skipToolsAllow, }, { yes: opts.yes, print: opts.print, configPath: opts.config, backup: opts.backup, }, ); for (const line of result.output) console.log(line); if (result.exitCode !== 0) process.exit(result.exitCode); }); }