File size: 6,027 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
/**
 * opencode-config-sync.js โ€” BADA์˜ MCP ํ”„๋กœํ•„ ์ƒํƒœ๋ฅผ opencode ์„ค์ •์— ๋ฏธ๋Ÿฌ๋ง
 *
 * ๋ฐฐ๊ฒฝ: BADA์˜ ์—์ด์ „ํŠธ ํ”„๋กœํ•„๋ณ„ MCP ์˜จ/์˜คํ”„(syncAgentMcpServers)๋Š” ์ง€๊ธˆ๊นŒ์ง€
 * ~/.claude/settings.json ์˜ mcpServers ๋งŒ ๊ฐฑ์‹ ํ–ˆ๋‹ค. opencode๋Š” ์ž์ฒด ์„ค์ •
 * (~/.config/opencode/opencode.json)์˜ `mcp` ํ‚ค๋ฅผ ์ฝ์œผ๋ฏ€๋กœ, ํ™œ์„ฑ CLI๊ฐ€ opencode์ผ ๋•Œ
 * bada MCP ์„œ๋ฒ„(์—์ด์ „ํŠธโ†”BADA ์—ฐ๋™ ๋ฐฑ๋ณธ)๊ฐ€ ์—์ด์ „ํŠธ์— ๋…ธ์ถœ๋˜์ง€ ์•Š๋Š” ๋ฝ์ธ์ด ์žˆ์—ˆ๋‹ค.
 * ์ด ๋ชจ๋“ˆ์ด Claude ํ˜•์‹ โ†’ opencode ํ˜•์‹ ๋ณ€ํ™˜ยท๋ฏธ๋Ÿฌ๋ง์„ ๋‹ด๋‹นํ•œ๋‹ค.
 *
 * ํ˜•์‹ ๋Œ€์‘ (opencode config ์Šคํ‚ค๋งˆ, 2026-07 ๊ธฐ์ค€):
 *  - stdio:  { command, args, env }        โ†’ { type:'local',  command:[command,...args], environment: env, enabled }
 *  - ์›๊ฒฉ:   { type:'sse'|'http', url }    โ†’ { type:'remote', url, enabled }
 *
 * ์•ˆ์ „ ์›์น™:
 *  1) ์‚ฌ์šฉ์ž๊ฐ€ opencode.json์— ์ง์ ‘ ์ •์˜ํ•œ(์šฐ๋ฆฌ ๊ด€๋ฆฌ ๋Œ€์ƒ์ด ์•„๋‹Œ) mcp ํ•ญ๋ชฉ์€ ์ ˆ๋Œ€ ๊ฑด๋“œ๋ฆฌ์ง€ ์•Š๋Š”๋‹ค.
 *  2) ์ด๋ฏธ ์กด์žฌํ•˜๋Š” ๋™๋ช… ํ•ญ๋ชฉ์€ ํ†ต์งธ๋กœ ๊ต์ฒดํ•˜์ง€ ์•Š๊ณ  `enabled` ํ”Œ๋ž˜๊ทธ๋งŒ ํ† ๊ธ€ํ•œ๋‹ค.
 *  3) ์„œ๋ฒ„๋ฅผ ๋Œ ๋•Œ ์‚ญ์ œํ•˜์ง€ ์•Š๊ณ  enabled:false ๋กœ ๋‘”๋‹ค (opencode๊ฐ€ ๊ณต์‹ ์ง€์›ํ•˜๋Š” ๋ฐฉ์‹).
 *  4) ์ตœ์ดˆ ์ˆ˜์ • ์ „ 1ํšŒ ์›๋ณธ ๋ฐฑ์—…(opencode.json.bada-backup)์„ ๋‚จ๊ธด๋‹ค.
 */
const fs = require('fs');
const path = require('path');
const cliPaths = require('./cli-paths');
const jsonc = require('jsonc-parser');

/** Claude(settings.json) ํ˜•์‹์˜ MCP ํ•ญ๋ชฉ ํ•˜๋‚˜๋ฅผ opencode ํ˜•์‹์œผ๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค. */
function convertMcpEntry(claudeEntry, enabled) {
  if (!claudeEntry || typeof claudeEntry !== 'object' || Array.isArray(claudeEntry)) return null;
  const stringMap = value => value && typeof value === 'object' && !Array.isArray(value) && Object.values(value).every(item => typeof item === 'string');

  // ์›๊ฒฉ ์„œ๋ฒ„ (sse/http/url ์ง€์ •ํ˜•)
  if (claudeEntry.url) {
    try { if (typeof claudeEntry.url !== 'string' || !['http:', 'https:'].includes(new URL(claudeEntry.url).protocol)) return null; } catch { return null; }
    if (claudeEntry.headers !== undefined && !stringMap(claudeEntry.headers)) return null;
    const converted = { type: 'remote', url: claudeEntry.url, enabled: !!enabled };
    if (claudeEntry.headers && typeof claudeEntry.headers === 'object') converted.headers = { ...claudeEntry.headers };
    return converted;
  }

  // ๋กœ์ปฌ(stdio) ์„œ๋ฒ„
  if (claudeEntry.command) {
    if (typeof claudeEntry.command !== 'string' || !claudeEntry.command.trim()) return null;
    if (claudeEntry.args !== undefined && (!Array.isArray(claudeEntry.args) || !claudeEntry.args.every(arg => typeof arg === 'string'))) return null;
    if (claudeEntry.env !== undefined && !stringMap(claudeEntry.env)) return null;
    const cmdArr = [claudeEntry.command, ...(Array.isArray(claudeEntry.args) ? claudeEntry.args : [])];
    const converted = { type: 'local', command: cmdArr, enabled: !!enabled };
    if (claudeEntry.env && typeof claudeEntry.env === 'object' && Object.keys(claudeEntry.env).length > 0) {
      converted.environment = claudeEntry.env;
    }
    return converted;
  }

  return null; // ์•Œ ์ˆ˜ ์—†๋Š” ํ˜•์‹ โ€” ๋ฏธ๋Ÿฌ๋ง ์ƒ๋žต
}

/**
 * BADA๊ฐ€ ๊ด€๋ฆฌํ•˜๋Š” MCP ์„œ๋ฒ„ ๋ชฉ๋ก(claude ํ˜•์‹)์„ opencode.json ์˜ `mcp` ํ‚ค์— ๋ฐ˜์˜ํ•œ๋‹ค.
 *
 * @param {Object} masterMcp   ์ „์ฒด ๋งˆ์Šคํ„ฐ MCP ์ •์˜ (์ด๋ฆ„ โ†’ claude ํ˜•์‹ ํ•ญ๋ชฉ)
 * @param {Set<string>|string[]} enabledNames ์ด๋ฒˆ ํ”„๋กœํ•„์—์„œ ์ผœ์•ผ ํ•  ์„œ๋ฒ„ ์ด๋ฆ„๋“ค
 * @returns {{ synced: boolean, reason?: string }}
 */
function prepareMcpToOpencode(masterMcp, enabledNames, targetCli = 'opencode') {
  const application = require('./profile-application');
  const candidates = cliPaths.getCliPaths(targetCli).settingsFiles;
  const configPath = candidates.find(file => path.basename(file) === targetCli + '.jsonc' && fs.existsSync(file)) ||
    candidates.find(file => path.basename(file) === targetCli + '.json' && fs.existsSync(file)) ||
    candidates.find(file => path.basename(file) === targetCli + '.json');
  if (!configPath) throw new Error('OpenCode configuration path is unavailable');
  const original = application.read(configPath);
  const raw = original?.toString('utf8').replace(/^\uFEFF/, '') || '{}';
  const errors = [], config = jsonc.parse(raw, errors, { allowTrailingComma: true });
  if (errors.length || !config || typeof config !== 'object' || Array.isArray(config)) throw new Error('Invalid OpenCode configuration');
  if (config.mcp !== undefined && (!config.mcp || typeof config.mcp !== 'object' || Array.isArray(config.mcp))) throw new Error('Invalid OpenCode MCP configuration');
  const mcp = Object.assign(Object.create(null), config.mcp), enabled = new Set(enabledNames);
  for (const [name, entry] of Object.entries(masterMcp)) {
    if (mcp[name] && typeof mcp[name] === 'object') mcp[name] = { ...mcp[name], enabled: enabled.has(name) };
    else if (enabled.has(name)) {
      const converted = convertMcpEntry(entry, true);
      if (!converted) throw new Error('Unsupported MCP server: ' + name);
      mcp[name] = converted;
    }
  }
  const output = jsonc.applyEdits(raw, jsonc.modify(raw, ['mcp'], mcp, { formattingOptions: { insertSpaces: true, tabSize: 2 } }));
  const plans = [];
  if (original !== null && !fs.existsSync(configPath + '.bada-backup') && output !== raw) plans.push(application.plan(configPath + '.bada-backup', original));
  plans.push(application.plan(configPath, output, original));
  return plans;
}
function syncMcpToOpencode(masterMcp, enabledNames, targetCli = null) {
  try {
    const current = targetCli || require('../context').getActiveCli();
    const cli = ['opencode', 'codeagent'].includes(current) ? current : 'opencode';
    require('./profile-application').commit(prepareMcpToOpencode(masterMcp, enabledNames, cli));
    return { synced: true };
  } catch (error) { return { synced: false, reason: error.message }; }
}

module.exports = { syncMcpToOpencode, prepareMcpToOpencode, convertMcpEntry };