From a3701e220d6fa88b5b864e5a0429fd4e51bb51ed Mon Sep 17 00:00:00 2001 From: TrendpilotAI Date: Thu, 26 Feb 2026 02:03:52 -0500 Subject: [PATCH] feat: Add OpenClaw as conversion target MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add openclaw as the 8th conversion target, enabling: bunx @every-env/compound-plugin install compound-engineering --to openclaw Converts Claude Code plugins into OpenClaw's extension format: - Agents → skills/agent-*/SKILL.md - Commands → api.registerCommand() + skills/cmd-*/SKILL.md - Skills → copied verbatim with path rewriting (.claude/ → .openclaw/) - MCP servers → openclaw.json config - Generates openclaw.plugin.json manifest, package.json, and index.ts entry point Output installs to ~/.openclaw/extensions// Co-Authored-By: Claude Opus 4.6 --- src/commands/install.ts | 5 +- src/converters/claude-to-openclaw.ts | 239 +++++++++++++++++++++++++++ src/targets/index.ts | 9 + src/targets/openclaw.ts | 96 +++++++++++ src/types/openclaw.ts | 52 ++++++ 5 files changed, 400 insertions(+), 1 deletion(-) create mode 100644 src/converters/claude-to-openclaw.ts create mode 100644 src/targets/openclaw.ts create mode 100644 src/types/openclaw.ts diff --git a/src/commands/install.ts b/src/commands/install.ts index eeb5a85..b5522b8 100644 --- a/src/commands/install.ts +++ b/src/commands/install.ts @@ -25,7 +25,7 @@ export default defineCommand({ to: { type: "string", default: "opencode", - description: "Target format (opencode | codex | droid | cursor | pi | copilot | gemini | kiro)", + description: "Target format (opencode | codex | droid | cursor | pi | copilot | gemini | kiro | openclaw)", }, output: { type: "string", @@ -195,6 +195,9 @@ function resolveTargetOutputRoot( const base = hasExplicitOutput ? outputRoot : process.cwd() return path.join(base, ".kiro") } + if (targetName === "openclaw") { + return path.join(os.homedir(), ".openclaw", "extensions", "compound-engineering") + } return outputRoot } diff --git a/src/converters/claude-to-openclaw.ts b/src/converters/claude-to-openclaw.ts new file mode 100644 index 0000000..71fdce3 --- /dev/null +++ b/src/converters/claude-to-openclaw.ts @@ -0,0 +1,239 @@ +import { formatFrontmatter } from "../utils/frontmatter" +import type { + ClaudeAgent, + ClaudeCommand, + ClaudePlugin, + ClaudeMcpServer, +} from "../types/claude" +import type { + OpenClawBundle, + OpenClawCommandRegistration, + OpenClawPluginManifest, + OpenClawSkillFile, +} from "../types/openclaw" +import type { ClaudeToOpenCodeOptions } from "./claude-to-opencode" + +export function convertClaudeToOpenClaw( + plugin: ClaudePlugin, + _options: ClaudeToOpenCodeOptions, +): OpenClawBundle { + const manifest = buildManifest(plugin) + const packageJson = buildPackageJson(plugin) + + const agentSkills = plugin.agents.map(convertAgentToSkill) + const commandSkills = plugin.commands + .filter((cmd) => !cmd.disableModelInvocation) + .map(convertCommandToSkill) + + const commands = plugin.commands + .filter((cmd) => !cmd.disableModelInvocation) + .map(convertCommand) + + const skills: OpenClawSkillFile[] = [...agentSkills, ...commandSkills] + + const skillDirCopies = plugin.skills.map((skill) => ({ + sourceDir: skill.sourceDir, + name: skill.name, + })) + + // Add original skill names to manifest.skills + const allSkillDirs = [ + ...agentSkills.map((s) => s.dir), + ...commandSkills.map((s) => s.dir), + ...plugin.skills.map((s) => s.name), + ] + manifest.skills = allSkillDirs.map((dir) => `skills/${dir}`) + + const openclawConfig = plugin.mcpServers + ? buildOpenClawConfig(plugin.mcpServers) + : undefined + + const entryPoint = generateEntryPoint(commands) + + return { + manifest, + packageJson, + entryPoint, + skills, + skillDirCopies, + commands, + openclawConfig, + } +} + +function buildManifest(plugin: ClaudePlugin): OpenClawPluginManifest { + return { + id: plugin.manifest.name, + name: formatDisplayName(plugin.manifest.name), + kind: "tool", + } +} + +function buildPackageJson(plugin: ClaudePlugin): Record { + return { + name: `openclaw-${plugin.manifest.name}`, + version: plugin.manifest.version, + type: "module", + private: true, + description: plugin.manifest.description, + main: "index.ts", + openclaw: { + extensions: [ + { + id: plugin.manifest.name, + entry: "./index.ts", + }, + ], + }, + keywords: [ + "openclaw", + "openclaw-plugin", + ...(plugin.manifest.keywords ?? []), + ], + } +} + +function convertAgentToSkill(agent: ClaudeAgent): OpenClawSkillFile { + const frontmatter: Record = { + name: agent.name, + description: agent.description, + } + + if (agent.model && agent.model !== "inherit") { + frontmatter.model = agent.model + } + + const body = rewritePaths(agent.body) + const content = formatFrontmatter(frontmatter, body) + + return { + name: agent.name, + content, + dir: `agent-${agent.name}`, + } +} + +function convertCommandToSkill(command: ClaudeCommand): OpenClawSkillFile { + const frontmatter: Record = { + name: `cmd-${command.name}`, + description: command.description, + } + + if (command.model && command.model !== "inherit") { + frontmatter.model = command.model + } + + const body = rewritePaths(command.body) + const content = formatFrontmatter(frontmatter, body) + + return { + name: command.name, + content, + dir: `cmd-${command.name}`, + } +} + +function convertCommand(command: ClaudeCommand): OpenClawCommandRegistration { + return { + name: command.name.replace(/:/g, "-"), + description: command.description ?? `Run ${command.name}`, + acceptsArgs: Boolean(command.argumentHint), + body: rewritePaths(command.body), + } +} + +function buildOpenClawConfig( + servers: Record, +): Record { + const mcpServers: Record = {} + + for (const [name, server] of Object.entries(servers)) { + if (server.command) { + mcpServers[name] = { + type: "stdio", + command: server.command, + args: server.args ?? [], + env: server.env, + } + } else if (server.url) { + mcpServers[name] = { + type: "http", + url: server.url, + headers: server.headers, + } + } + } + + return { mcpServers } +} + +function generateEntryPoint(commands: OpenClawCommandRegistration[]): string { + const commandRegistrations = commands + .map((cmd) => { + const escapedName = cmd.name.replace(/"/g, '\\"') + const escapedDesc = (cmd.description ?? "").replace(/"/g, '\\"') + return ` api.registerCommand({ + name: "${escapedName}", + description: "${escapedDesc}", + acceptsArgs: ${cmd.acceptsArgs}, + requireAuth: false, + handler: (ctx) => ({ + text: skills["${escapedName}"] ?? "Command ${escapedName} not found. Check skills directory.", + }), + });` + }) + .join("\n\n") + + return `// Auto-generated OpenClaw plugin entry point +// Converted from Claude Code plugin format by compound-plugin CLI +import { promises as fs } from "fs"; +import path from "path"; +import { fileURLToPath } from "url"; + +const __dirname = path.dirname(fileURLToPath(import.meta.url)); + +// Pre-load skill bodies for command responses +const skills = {}; + +async function loadSkills() { + const skillsDir = path.join(__dirname, "skills"); + try { + const entries = await fs.readdir(skillsDir, { withFileTypes: true }); + for (const entry of entries) { + if (!entry.isDirectory()) continue; + const skillPath = path.join(skillsDir, entry.name, "SKILL.md"); + try { + const content = await fs.readFile(skillPath, "utf8"); + // Strip frontmatter + const body = content.replace(/^---[\\s\\S]*?---\\n*/, ""); + skills[entry.name.replace(/^cmd-/, "")] = body.trim(); + } catch { + // Skill file not found, skip + } + } + } catch { + // Skills directory not found + } +} + +export default async function register(api) { + await loadSkills(); + +${commandRegistrations} +} +` +} + +function rewritePaths(body: string): string { + return body + .replace(/~\/\.claude\//g, "~/.openclaw/") + .replace(/\.claude\//g, ".openclaw/") + .replace(/\.claude-plugin\//g, "openclaw-plugin/") +} + +function formatDisplayName(name: string): string { + return name + .split("-") + .map((word) => word.charAt(0).toUpperCase() + word.slice(1)) + .join(" ") +} diff --git a/src/targets/index.ts b/src/targets/index.ts index b7b3ea2..196a17e 100644 --- a/src/targets/index.ts +++ b/src/targets/index.ts @@ -6,6 +6,7 @@ import type { PiBundle } from "../types/pi" import type { CopilotBundle } from "../types/copilot" import type { GeminiBundle } from "../types/gemini" import type { KiroBundle } from "../types/kiro" +import type { OpenClawBundle } from "../types/openclaw" import { convertClaudeToOpenCode, type ClaudeToOpenCodeOptions } from "../converters/claude-to-opencode" import { convertClaudeToCodex } from "../converters/claude-to-codex" import { convertClaudeToDroid } from "../converters/claude-to-droid" @@ -13,6 +14,7 @@ import { convertClaudeToPi } from "../converters/claude-to-pi" import { convertClaudeToCopilot } from "../converters/claude-to-copilot" import { convertClaudeToGemini } from "../converters/claude-to-gemini" import { convertClaudeToKiro } from "../converters/claude-to-kiro" +import { convertClaudeToOpenClaw } from "../converters/claude-to-openclaw" import { writeOpenCodeBundle } from "./opencode" import { writeCodexBundle } from "./codex" import { writeDroidBundle } from "./droid" @@ -20,6 +22,7 @@ import { writePiBundle } from "./pi" import { writeCopilotBundle } from "./copilot" import { writeGeminiBundle } from "./gemini" import { writeKiroBundle } from "./kiro" +import { writeOpenClawBundle } from "./openclaw" export type TargetHandler = { name: string @@ -71,4 +74,10 @@ export const targets: Record = { convert: convertClaudeToKiro as TargetHandler["convert"], write: writeKiroBundle as TargetHandler["write"], }, + openclaw: { + name: "openclaw", + implemented: true, + convert: convertClaudeToOpenClaw as TargetHandler["convert"], + write: writeOpenClawBundle as TargetHandler["write"], + }, } diff --git a/src/targets/openclaw.ts b/src/targets/openclaw.ts new file mode 100644 index 0000000..d2ec688 --- /dev/null +++ b/src/targets/openclaw.ts @@ -0,0 +1,96 @@ +import path from "path" +import { promises as fs } from "fs" +import { backupFile, copyDir, ensureDir, pathExists, readJson, walkFiles, writeJson, writeText } from "../utils/files" +import type { OpenClawBundle } from "../types/openclaw" + +export async function writeOpenClawBundle(outputRoot: string, bundle: OpenClawBundle): Promise { + const paths = resolveOpenClawPaths(outputRoot) + await ensureDir(paths.root) + + // Write openclaw.plugin.json + await writeJson(paths.manifestPath, bundle.manifest) + + // Write package.json + await writeJson(paths.packageJsonPath, bundle.packageJson) + + // Write index.ts entry point + await writeText(paths.entryPointPath, bundle.entryPoint) + + // Write generated skills (agents + commands converted to SKILL.md) + for (const skill of bundle.skills) { + const skillDir = path.join(paths.skillsDir, skill.dir) + await ensureDir(skillDir) + await writeText(path.join(skillDir, "SKILL.md"), skill.content + "\n") + } + + // Copy original skill directories (preserving references/, assets/, scripts/) + // and rewrite .claude/ paths to .openclaw/ in markdown files + for (const skill of bundle.skillDirCopies) { + const destDir = path.join(paths.skillsDir, skill.name) + await copyDir(skill.sourceDir, destDir) + await rewritePathsInDir(destDir) + } + + // Write openclaw.json config fragment if MCP servers exist + if (bundle.openclawConfig) { + const configPath = path.join(paths.root, "openclaw.json") + const backupPath = await backupFile(configPath) + if (backupPath) { + console.log(`Backed up existing config to ${backupPath}`) + } + const merged = await mergeOpenClawConfig(configPath, bundle.openclawConfig) + await writeJson(configPath, merged) + } +} + +function resolveOpenClawPaths(outputRoot: string) { + return { + root: outputRoot, + manifestPath: path.join(outputRoot, "openclaw.plugin.json"), + packageJsonPath: path.join(outputRoot, "package.json"), + entryPointPath: path.join(outputRoot, "index.ts"), + skillsDir: path.join(outputRoot, "skills"), + } +} + +async function rewritePathsInDir(dir: string): Promise { + const files = await walkFiles(dir) + for (const file of files) { + if (!file.endsWith(".md")) continue + const content = await fs.readFile(file, "utf8") + const rewritten = content + .replace(/~\/\.claude\//g, "~/.openclaw/") + .replace(/\.claude\//g, ".openclaw/") + .replace(/\.claude-plugin\//g, "openclaw-plugin/") + if (rewritten !== content) { + await fs.writeFile(file, rewritten, "utf8") + } + } +} + +async function mergeOpenClawConfig( + configPath: string, + incoming: Record, +): Promise> { + if (!(await pathExists(configPath))) return incoming + + let existing: Record + try { + existing = await readJson>(configPath) + } catch { + console.warn( + `Warning: existing ${configPath} is not valid JSON. Writing plugin config without merging.`, + ) + return incoming + } + + // Merge MCP servers: existing takes precedence on conflict + const incomingMcp = (incoming.mcpServers ?? {}) as Record + const existingMcp = (existing.mcpServers ?? {}) as Record + const mergedMcp = { ...incomingMcp, ...existingMcp } + + return { + ...existing, + mcpServers: Object.keys(mergedMcp).length > 0 ? mergedMcp : undefined, + } +} diff --git a/src/types/openclaw.ts b/src/types/openclaw.ts new file mode 100644 index 0000000..5d68910 --- /dev/null +++ b/src/types/openclaw.ts @@ -0,0 +1,52 @@ +export type OpenClawPluginManifest = { + id: string + name: string + kind: "tool" + configSchema?: { + type: "object" + additionalProperties: boolean + properties: Record + required?: string[] + } + uiHints?: Record + skills?: string[] +} + +export type OpenClawConfigProperty = { + type: string + description?: string + default?: unknown +} + +export type OpenClawUiHint = { + label: string + sensitive?: boolean + placeholder?: string +} + +export type OpenClawSkillFile = { + name: string + content: string + /** Subdirectory path inside skills/ (e.g. "agent-native-reviewer") */ + dir: string +} + +export type OpenClawCommandRegistration = { + name: string + description: string + acceptsArgs: boolean + /** The prompt body that becomes the command handler response */ + body: string +} + +export type OpenClawBundle = { + manifest: OpenClawPluginManifest + packageJson: Record + entryPoint: string + skills: OpenClawSkillFile[] + /** Skill directories to copy verbatim (original Claude skills with references/) */ + skillDirCopies: { sourceDir: string; name: string }[] + commands: OpenClawCommandRegistration[] + /** openclaw.json fragment for MCP servers */ + openclawConfig?: Record +}