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 };