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