ESM conversion:
- package.json: add "type": "module"; drop stale "main": "index.ts"
- tsconfig.json: switch module/moduleResolution to "nodenext"
- plugin/index.ts: replace `module.exports = { register }` and
`module.exports.X = X` with `export default definePluginEntry({ ... })`
plus named ESM re-exports; replace `require('os')`/`require('path')`
with proper imports.
- plugin/tools/pcexec.ts: replace `require('child_process')` with import
from "node:child_process".
- plugin/commands/ego-mgr-slash.ts: replace `require('path')` with
proper path import.
- All relative imports/exports across plugin/ now carry .js extensions
as required by Node ESM (nodenext module resolution).
Plugin SDK convention update:
- Wrap default export with definePluginEntry({ id, name, description,
register }) per the current openclaw authoring contract.
- Type api parameter as OpenClawPluginApi (was `any`); the non-standard
api.registerSlashCommand call is preserved behind a guarded any-cast,
so the plugin remains a no-op for slash commands when the host doesn't
expose that hook (matches the previous defensive guard).
- Add openclaw as a devDependency (file:/usr/lib/node_modules/openclaw)
so tsc can resolve openclaw/plugin-sdk/* subpath types at build time.
- Modernize openclaw.plugin.json: drop entry/version, add
activation.onStartup so gateway_start fires for this plugin at boot,
declare contracts.tools listing pcexec/proxy-pcexec/safe_restart.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
239 lines
8.1 KiB
TypeScript
239 lines
8.1 KiB
TypeScript
// PaddedCell Plugin for OpenClaw
|
|
// Registers pcexec, proxy-pcexec, and safe_restart tools
|
|
|
|
import os from 'node:os';
|
|
import path from 'node:path';
|
|
import { definePluginEntry } from 'openclaw/plugin-sdk/plugin-entry';
|
|
import type { OpenClawPluginApi } from 'openclaw/plugin-sdk/core';
|
|
|
|
import { pcexec, pcexecSync } from './tools/pcexec.js';
|
|
import {
|
|
safeRestart,
|
|
createSafeRestartTool,
|
|
StatusManager,
|
|
createApiServer,
|
|
startApiServer,
|
|
} from './core/index.js';
|
|
import { SlashCommandHandler } from './commands/slash-commands.js';
|
|
import { EgoMgrSlashCommand } from './commands/ego-mgr-slash.js';
|
|
|
|
/** Sentinel value injected into every pcexec subprocess */
|
|
const AGENT_VERIFY = 'IF YOU ARE AN AGENT/MODEL, YOU SHOULD NEVER TOUCH THIS ENV VARIABLE';
|
|
|
|
/**
|
|
* Resolve the openclaw base path.
|
|
* Priority: explicit config → $OPENCLAW_PATH → ~/.openclaw
|
|
*/
|
|
function resolveOpenclawPath(config?: { openclawProfilePath?: string }): string {
|
|
if (config?.openclawProfilePath) return config.openclawProfilePath;
|
|
if (process.env.OPENCLAW_PATH) return process.env.OPENCLAW_PATH;
|
|
const home = process.env.HOME || os.homedir();
|
|
return path.join(home, '.openclaw');
|
|
}
|
|
|
|
function getPluginConfig(api: OpenClawPluginApi): Record<string, unknown> {
|
|
return ((api?.pluginConfig as Record<string, unknown> | undefined) || {});
|
|
}
|
|
|
|
function resolveProxyAllowlist(config?: { proxyAllowlist?: unknown; 'proxy-allowlist'?: unknown }): string[] {
|
|
const value = config?.proxyAllowlist ?? config?.['proxy-allowlist'];
|
|
if (!Array.isArray(value)) return [];
|
|
return value.filter((item): item is string => typeof item === 'string');
|
|
}
|
|
|
|
function register(api: OpenClawPluginApi): void {
|
|
const logger = api.logger || { info: console.log, error: console.error, warn: console.warn };
|
|
|
|
logger.info('PaddedCell plugin initializing...');
|
|
|
|
const pluginConfig = getPluginConfig(api);
|
|
const openclawPath = resolveOpenclawPath(pluginConfig as { openclawProfilePath?: string });
|
|
const proxyAllowlist = resolveProxyAllowlist(pluginConfig as { proxyAllowlist?: unknown; 'proxy-allowlist'?: unknown });
|
|
const binDir = path.join(openclawPath, 'bin');
|
|
|
|
// Register pcexec tool — pass a FACTORY function that receives context
|
|
api.registerTool((ctx) => {
|
|
const agentId = ctx.agentId;
|
|
const workspaceDir = ctx.workspaceDir;
|
|
|
|
return {
|
|
name: 'pcexec',
|
|
description: 'Safe exec with password sanitization',
|
|
parameters: {
|
|
type: 'object',
|
|
properties: {
|
|
command: { type: 'string', description: 'Command to execute' },
|
|
cwd: { type: 'string', description: 'Working directory' },
|
|
timeout: { type: 'number', description: 'Timeout in milliseconds' },
|
|
},
|
|
required: ['command'],
|
|
},
|
|
async execute(_id: string, params: any) {
|
|
const command = params.command;
|
|
if (!command) {
|
|
throw new Error('Missing required parameter: command');
|
|
}
|
|
|
|
// Build PATH with openclaw bin dir appended
|
|
const currentPath = process.env.PATH || '';
|
|
const newPath = currentPath.includes(binDir)
|
|
? currentPath
|
|
: `${currentPath}:${binDir}`;
|
|
|
|
const result = await pcexec(command, {
|
|
cwd: params.cwd || workspaceDir,
|
|
timeout: params.timeout,
|
|
env: {
|
|
AGENT_ID: agentId || '',
|
|
AGENT_WORKSPACE: workspaceDir || '',
|
|
AGENT_VERIFY,
|
|
PATH: newPath,
|
|
},
|
|
});
|
|
|
|
// Format output for OpenClaw tool response
|
|
let output = result.stdout;
|
|
if (result.stderr) {
|
|
output += result.stderr;
|
|
}
|
|
return { content: [{ type: 'text', text: output }] };
|
|
},
|
|
} as any;
|
|
});
|
|
|
|
api.registerTool((ctx) => {
|
|
const agentId = ctx.agentId;
|
|
const workspaceDir = ctx.workspaceDir;
|
|
|
|
return {
|
|
name: 'proxy-pcexec',
|
|
description: 'Safe exec with password sanitization using a proxied AGENT_ID',
|
|
parameters: {
|
|
type: 'object',
|
|
properties: {
|
|
command: { type: 'string', description: 'Command to execute' },
|
|
cwd: { type: 'string', description: 'Working directory' },
|
|
timeout: { type: 'number', description: 'Timeout in milliseconds' },
|
|
'proxy-for': { type: 'string', description: 'AGENT_ID value to inject for the subprocess' },
|
|
},
|
|
required: ['command', 'proxy-for'],
|
|
},
|
|
async execute(_id: string, params: any) {
|
|
const command = params.command;
|
|
const proxyFor = params['proxy-for'];
|
|
if (!command) {
|
|
throw new Error('Missing required parameter: command');
|
|
}
|
|
if (!proxyFor) {
|
|
throw new Error('Missing required parameter: proxy-for');
|
|
}
|
|
if (!agentId || !proxyAllowlist.includes(agentId)) {
|
|
throw new Error('Current agent is not allowed to call proxy-pcexec');
|
|
}
|
|
|
|
logger.info(`proxy-pcexec invoked executor=${agentId} proxyFor=${proxyFor} command=${command}`);
|
|
|
|
const currentPath = process.env.PATH || '';
|
|
const newPath = currentPath.includes(binDir)
|
|
? currentPath
|
|
: `${currentPath}:${binDir}`;
|
|
|
|
const result = await pcexec(command, {
|
|
cwd: params.cwd || workspaceDir,
|
|
timeout: params.timeout,
|
|
env: {
|
|
AGENT_ID: String(proxyFor),
|
|
AGENT_WORKSPACE: workspaceDir || '',
|
|
AGENT_VERIFY,
|
|
PROXY_PCEXEC_EXECUTOR: agentId || '',
|
|
PCEXEC_PROXIED: 'true',
|
|
PATH: newPath,
|
|
},
|
|
});
|
|
|
|
let output = result.stdout;
|
|
if (result.stderr) {
|
|
output += result.stderr;
|
|
}
|
|
return { content: [{ type: 'text', text: output }] };
|
|
},
|
|
} as any;
|
|
});
|
|
|
|
// Register safe_restart tool
|
|
api.registerTool((ctx) => {
|
|
const agentId = ctx.agentId;
|
|
const sessionKey = ctx.sessionKey;
|
|
|
|
return {
|
|
name: 'safe_restart',
|
|
description: 'Safe coordinated restart of OpenClaw gateway',
|
|
parameters: {
|
|
type: 'object',
|
|
properties: {
|
|
rollback: { type: 'string', description: 'Rollback script path' },
|
|
log: { type: 'string', description: 'Log file path' },
|
|
},
|
|
},
|
|
async execute(_id: string, params: any) {
|
|
return await safeRestart({
|
|
agentId: agentId ?? '',
|
|
sessionKey: sessionKey ?? '',
|
|
rollback: params.rollback,
|
|
log: params.log,
|
|
});
|
|
},
|
|
} as any;
|
|
});
|
|
|
|
// Register /ego-mgr slash command if the host exposes the (non-standard) hook.
|
|
// This API is not part of the current OpenClawPluginApi surface; the guard
|
|
// makes the plugin a no-op for slash commands when the host doesn't support
|
|
// them, instead of failing to load.
|
|
const apiAny = api as unknown as { registerSlashCommand?: (cmd: unknown) => void };
|
|
if (typeof apiAny.registerSlashCommand === 'function') {
|
|
apiAny.registerSlashCommand({
|
|
name: 'ego-mgr',
|
|
description: 'Manage agent identity/profile fields',
|
|
handler: async (ctx: any, command: string) => {
|
|
const egoMgrSlash = new EgoMgrSlashCommand({
|
|
openclawPath,
|
|
agentId: ctx.agentId || '',
|
|
workspaceDir: ctx.workspaceDir || '',
|
|
onReply: async (message: string) => {
|
|
if (ctx.reply) {
|
|
await ctx.reply(message);
|
|
}
|
|
},
|
|
});
|
|
await egoMgrSlash.handle(command);
|
|
},
|
|
});
|
|
logger.info('Registered /ego-mgr slash command');
|
|
}
|
|
|
|
logger.info('PaddedCell plugin initialized');
|
|
}
|
|
|
|
export default definePluginEntry({
|
|
id: 'padded-cell',
|
|
name: 'PaddedCell',
|
|
description: 'Secure secret management, agent identity management, safe execution, and coordinated agent restart',
|
|
register,
|
|
});
|
|
|
|
// Named ESM re-exports — equivalent to the previous `module.exports.X = X`
|
|
// surface, so consumers that reach into the plugin module directly keep working.
|
|
export { register };
|
|
export { pcexec, pcexecSync } from './tools/pcexec.js';
|
|
export {
|
|
safeRestart,
|
|
createSafeRestartTool,
|
|
StatusManager,
|
|
createApiServer,
|
|
startApiServer,
|
|
} from './core/index.js';
|
|
export { SlashCommandHandler } from './commands/slash-commands.js';
|
|
export { EgoMgrSlashCommand } from './commands/ego-mgr-slash.js';
|
|
export { AGENT_VERIFY };
|