File size: 7,959 Bytes
d739b1b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 | /**
* opencode-agent-sync.js โ Claude Code ์๋ธ์์ด์ ํธ๋ฅผ opencode ์์ด์ ํธ๋ก ๋ฏธ๋ฌ๋ง
*
* ๋ฐฐ๊ฒฝ: ~/.claude/agents/*.md ์ ์๋ธ์์ด์ ํธ(analyst, architect, code-reviewer ๋ฑ)๋
* Claude Code ์ ์ฉ ๋ฐ๊ฒฌ ๋ฉ์ปค๋์ฆ์ด๋ผ opencode์์๋ ์ ํ ์ธ์๋์ง ์๋ ๋ฝ์ธ์ด ์์๋ค.
* opencode๋ ~/.config/opencode/agent/*.md (frontmatter=AgentConfig, ๋ณธ๋ฌธ=ํ๋กฌํํธ)๋ฅผ
* ์ฝ์ผ๋ฏ๋ก, ํ์์ ๋ณํํด ๋ฏธ๋ฌ๋งํ๋ค. (๊ท์ฝ ์ค์ธก ๊ฒ์ฆ: 2026-07-11, `opencode agent list`๊ฐ
* ๊ธ๋ก๋ฒ agent/ ๋๋ ํ ๋ฆฌ์ .md๋ฅผ subagent๋ก ์ธ์ํจ โ TR-027 ์ฐ์ฅ)
*
* ํ์ ๋์:
* - ํ์ผ๋ช
โ opencode ์์ด์ ํธ ์ด๋ฆ (๋์ผ ์ ์ง)
* - description โ description (์์ผ๋ฉด ๋ฏธ๋ฌ๋ง ์ ์ธ โ opencode ์คํค๋ง ํ์๊ฐ)
* - (mode) โ 'subagent' ๊ณ ์
* - model ๋ณ์นญ(opus ๋ฑ) โ ์๋ต (opencode๋ provider/model ํ์ โ ์ธ์
๋ชจ๋ธ ์์์ด ์์ )
* - tools/disallowedTools โ opencode tools ๋งต(boolean)์ผ๋ก ๋ณํ (์๋ ค์ง ํด๋ช
๋ง)
* - ๋ณธ๋ฌธ โ ํ๋กฌํํธ ๊ทธ๋๋ก
*
* ์์ ์์น:
* 1) ์์ฑ ํ์ผ์๋ BADA ๋ฏธ๋ฌ ๋ง์ปค๋ฅผ ์ฌ๊ณ , ๋ง์ปค ์๋ ํ์ผ๋ง ๊ฐฑ์ /์ญ์ ํ๋ค
* (์ฌ์ฉ์๊ฐ ์ง์ ๋ง๋ opencode ์์ด์ ํธ๋ ์ ๋ ๊ฑด๋๋ฆฌ์ง ์์).
* 2) ์๋ณธ์ด ์ฌ๋ผ์ง ๋ฏธ๋ฌ ํ์ผ(๊ณ ์)์ ์ ๊ฑฐํด ๋ชฉ๋ก ์ค์ผ์ ๋ง๋๋ค.
* 3) ์๋ณธ ํด์๊ฐ ๊ฐ์ผ๋ฉด ์ฌ๊ธฐ๋กํ์ง ์๋๋ค (๋ฉฑ๋ฑ).
*/
const fs = require('fs');
const path = require('path');
const os = require('os');
const crypto = require('crypto');
const cliPaths = require('./cli-paths');
/** ์์ฑ ํ์ผ ์๋ณ์ฉ ๋ง์ปค (๋ณธ๋ฌธ ์ต์๋จ HTML ์ฃผ์) */
const MIRROR_MARKER = '<!-- BADA-MIRROR: claude-agent';
/** Claude Code ํด๋ช
โ opencode ํด๋ช
๋์ํ (์๋ ค์ง ๊ฒ๋ง ๋ณํ, ๋ฏธ์ง์ ํด์ ์๋ต) */
const TOOL_NAME_MAP = {
bash: 'bash', read: 'read', write: 'write', edit: 'edit',
grep: 'grep', glob: 'glob', webfetch: 'webfetch', websearch: 'websearch',
agent: 'task', task: 'task', todowrite: 'todowrite', todoread: 'todoread',
list: 'list', patch: 'patch'
};
/**
* ๋งํฌ๋ค์ด frontmatter๋ฅผ ๋จ์ ํ์ฑํ๋ค (์ธ๋ถ ์์กด์ฑ ์์ด key: value ํ ์ค ํ์๋ง).
* Claude ์์ด์ ํธ ์ ์๋ ์ค์ฒฉ YAML์ ์ฐ์ง ์์ผ๋ฏ๋ก ์ด ์์ค์ด๋ฉด ์ถฉ๋ถํ๋ค.
* @returns {{ meta: Object, body: string }|null} frontmatter ๋ถ์ฌ ์ null
*/
function parseFrontmatter(content) {
const m = content.match(/^---\r?\n([\s\S]*?)\r?\n---\r?\n?([\s\S]*)$/);
if (!m) return null;
const meta = {};
for (const line of m[1].split(/\r?\n/)) {
const kv = line.match(/^([A-Za-z_][\w-]*)\s*:\s*(.*)$/);
if (kv) meta[kv[1]] = kv[2].trim();
}
return { meta, body: m[2] || '' };
}
/** ์ผํ ๊ตฌ๋ถ ํด ๋ชฉ๋ก ๋ฌธ์์ด โ opencode tools ๋งต ํญ๋ชฉ๋ค */
function mapToolList(listStr, enabled, toolsOut) {
if (!listStr) return;
for (const raw of String(listStr).split(',')) {
const key = raw.trim().toLowerCase();
if (!key) continue;
const mapped = TOOL_NAME_MAP[key];
if (mapped) toolsOut[mapped] = enabled;
// ๋์ํ์ ์๋ ํด(claude ์ ์ฉ MCP ํด ๋ฑ)์ ์๋ต โ opencode๊ฐ ๋ชจ๋ฅด๋ ํค ์ฃผ์
๋ฐฉ์ง
}
}
/**
* Claude ์์ด์ ํธ .md ํ ๊ฑด์ opencode ์์ด์ ํธ .md ๋ก ๋ณํํ๋ค.
* @param {string} name ์์ด์ ํธ ์ด๋ฆ(ํ์ผ๋ช
stem)
* @param {string} content ์๋ณธ ํ์ผ ์ ์ฒด ๋ด์ฉ
* @returns {string|null} ๋ณํ ๊ฒฐ๊ณผ (๋ณํ ๋ถ๊ฐ ์ null)
*/
function convertAgentMd(name, content) {
const parsed = parseFrontmatter(content);
if (!parsed) return null;
const { meta, body } = parsed;
if (!meta.description) return null; // opencode ์คํค๋ง์์ description์ ์ฌ์ค์ ํ์
const tools = {};
mapToolList(meta.tools, true, tools);
mapToolList(meta.disallowedTools, false, tools);
const srcHash = crypto.createHash('sha256').update(content).digest('hex').slice(0, 16);
const fmLines = [
'---',
// YAML ๋ฌธ์์ด ์์ ํ: ์๋ฐ์ดํ ์ด์ค์ผ์ดํ ํ ์ธ์ฉ
`description: "${meta.description.replace(/\\/g, '\\\\').replace(/"/g, '\\"')}"`,
'mode: subagent'
];
if (Object.keys(tools).length > 0) {
fmLines.push('tools:');
for (const [k, v] of Object.entries(tools)) fmLines.push(` ${k}: ${v}`);
}
fmLines.push('---');
return `${fmLines.join('\n')}\n${MIRROR_MARKER} src=${name}.md hash=${srcHash} โ BADA๊ฐ ~/.claude/agents ์์ ์๋ ๋ฏธ๋ฌ๋ง. ์ง์ ์์ ๊ธ์ง -->\n\n${body.trim()}\n`;
}
/**
* ~/.claude/agents/*.md ์ ์ฒด๋ฅผ opencode ๊ธ๋ก๋ฒ agent ๋๋ ํ ๋ฆฌ๋ก ๋ฏธ๋ฌ๋งํ๋ค.
* ํ์ฑ CLI๊ฐ opencode๊ฐ ์๋๊ฑฐ๋ opencode ๋ฏธ์ค์น๋ฉด ์๋ฌด๊ฒ๋ ํ์ง ์๋๋ค.
* @returns {{ synced: number, removed: number, skipped: number, reason?: string }}
*/
function syncClaudeAgentsToOpencode() {
const result = { synced: 0, removed: 0, skipped: 0 };
const ctx = require('../context');
let activeCli;
try {
activeCli = ctx.getActiveCli();
} catch (e) {
return { ...result, reason: 'ํ์ฑ CLI ํ์ธ ์คํจ: ' + e.message };
}
if (activeCli !== 'opencode' && activeCli !== 'codeagent') {
return { ...result, reason: 'ํ์ฑ CLI๊ฐ opencode/codeagent ์๋' };
}
try {
const ocHome = cliPaths.getCliPaths(activeCli).homeDir;
if (!fs.existsSync(ocHome)) {
return { ...result, reason: `${activeCli} ์ค์ ํ ๋ถ์ฌ: ${ocHome}` };
}
const srcDir = path.join(os.homedir(), '.claude', 'agents');
const dstDir = path.join(ocHome, 'agent');
const srcFiles = fs.existsSync(srcDir)
? fs.readdirSync(srcDir).filter(f => f.endsWith('.md'))
: [];
fs.mkdirSync(dstDir, { recursive: true });
// 1) ์์ฑ/๊ฐฑ์ โ ์๋ณธ ํด์๊ฐ ๊ฐ์ผ๋ฉด ์คํต (๋ฉฑ๋ฑ)
const validTargets = new Set();
for (const file of srcFiles) {
try {
const content = fs.readFileSync(path.join(srcDir, file), 'utf-8');
const converted = convertAgentMd(path.basename(file, '.md'), content);
if (!converted) { result.skipped++; continue; }
const dstPath = path.join(dstDir, file);
validTargets.add(file);
if (fs.existsSync(dstPath)) {
const existing = fs.readFileSync(dstPath, 'utf-8');
if (!existing.includes(MIRROR_MARKER)) {
// ๋๋ช
์ ์ฌ์ฉ์ ์ ์ ์์ด์ ํธ ์กด์ฌ โ ๋ฎ์ด์ฐ์ง ์๋๋ค (์์ ์์น 1)
result.skipped++;
continue;
}
if (existing === converted) continue; // ๋ณ๊ฒฝ ์์
}
fs.writeFileSync(dstPath, converted, 'utf-8');
result.synced++;
} catch (fileErr) {
console.error(`[OpencodeAgentSync] ๋ณํ ์คํจ(๊ฑด๋๋): ${file} โ ${fileErr.message}`);
result.skipped++;
}
}
// 2) ๊ณ ์ ๋ฏธ๋ฌ ์ ๊ฑฐ โ ๋ง์ปค ์๋ ํ์ผ ์ค ์๋ณธ์ด ์ฌ๋ผ์ง ๊ฒ๋ง
for (const file of fs.readdirSync(dstDir).filter(f => f.endsWith('.md'))) {
try {
if (validTargets.has(file)) continue;
const content = fs.readFileSync(path.join(dstDir, file), 'utf-8');
if (content.includes(MIRROR_MARKER) && !fs.existsSync(path.join(srcDir, file))) {
fs.unlinkSync(path.join(dstDir, file));
result.removed++;
}
} catch (orphanErr) {
console.error(`[OpencodeAgentSync] ๊ณ ์ ์ ๋ฆฌ ์คํจ(๋ฌด์): ${file} โ ${orphanErr.message}`);
}
}
if (result.synced > 0 || result.removed > 0) {
console.log(`[OpencodeAgentSync] ${activeCli} ์๋ธ์์ด์ ํธ ๋ฏธ๋ฌ๋ง ์๋ฃ โ (๊ฐฑ์ ${result.synced}, ์ ๊ฑฐ ${result.removed}, ์คํต ${result.skipped})`);
}
return result;
} catch (e) {
console.error(`[OpencodeAgentSync] ${activeCli} ๋ฏธ๋ฌ๋ง ์คํจ(๋ฌด์ํ๊ณ ๊ณ์):`, e.message);
return { ...result, reason: e.message };
}
}
module.exports = { syncClaudeAgentsToOpencode, convertAgentMd, parseFrontmatter };
|