Files
Willie cb2bcec948 feat(agents): convert agents with a target extension
Tools disagree on agent frontmatter and some need another format entirely,
so an agent written for Claude is misread elsewhere. An `extension` on a
target's agents block reuses the extras transform mechanism to convert each
agent during sync.

Converted targets hold transformed output, so collect never pulls them back
into the source. Leftover symlinks from merge or symlink mode are removed
rather than written through, and a failed conversion drops the old output
so an unconverted agent never stays active.

The bundled opencode-agents extension keeps only fields OpenCode documents
and refuses agents that restrict their tools, since dropping those fields
would widen the agent's access.

Refs #267
Refs #241
2026-09-21 13:31:38 +08:00

64 lines
2.6 KiB
JavaScript

#!/usr/bin/env node
// Markdown subagent (Claude style) → OpenCode agent (https://opencode.ai/docs/agents/).
//
// Only conversions that need no guessing:
// - keys OpenCode does not document are dropped (name, Claude-only fields, targets)
// - `mode` defaults to subagent; OpenCode's own default (all) would list a
// helper agent as a primary agent
// - a `model` that is not provider/model-id is dropped, so OpenCode uses its default
// - `tools`, `disallowedTools` and `permissionMode` fail the conversion: they
// restrict a Claude agent, and dropping them would let the OpenCode agent use
// every tool
// Frontmatter keys passed through unchanged. Add OpenCode keys here as needed.
const KEEP = ["description", "mode", "model", "temperature", "top_p", "steps", "permission", "hidden", "color", "prompt"];
let input = "";
process.stdin.setEncoding("utf8");
process.stdin.on("data", (chunk) => (input += chunk));
process.stdin.on("end", () => {
try {
process.stdout.write(convert(input));
} catch (err) {
process.stderr.write(`opencode-agents: ${err.message}\n`);
process.exit(1);
}
});
function convert(text) {
const match = text.match(/^---\r?\n([\s\S]*?)\r?\n---[ \t]*\r?\n?/);
const body = match ? text.slice(match[0].length) : text;
const entries = parseEntries(match ? match[1] : "");
const has = (key) => entries.some((e) => e.key === key);
const restriction = ["tools", "disallowedTools", "permissionMode"].find(has);
if (restriction) {
throw new Error(
`'${restriction}' limits which tools a Claude agent may use and has no safe OpenCode equivalent. ` +
"Keep a separate OpenCode variant with `targets: [opencode]` and `permission:`, " +
"and add `targets:` to this agent so it skips opencode."
);
}
if (!has("description")) {
throw new Error("missing required frontmatter 'description'");
}
const lines = entries
.filter((e) => KEEP.includes(e.key) && !(e.key === "model" && !e.value.includes("/")))
.flatMap((e) => e.lines);
if (!has("mode")) lines.push("mode: subagent");
return `---\n${lines.join("\n")}\n---\n${body}`;
}
// parseEntries groups frontmatter lines by top-level key; indented lines
// (nested maps such as permission, or lists) stay with the key above them.
function parseEntries(text) {
const entries = [];
for (const line of text.split(/\r?\n/)) {
const m = line.match(/^([A-Za-z0-9_-]+)\s*:\s*(.*)$/);
if (m) entries.push({ key: m[1], value: m[2].trim(), lines: [line] });
else if (entries.length) entries[entries.length - 1].lines.push(line);
}
return entries;
}