Download src/config/opencode-config-sync.js from VanyaJ/bada-terminal: direct link, hf CLI and curl.
- Browser
- Download file 6.03 kB
-
https://huggingface.co/VanyaJ/bada-terminal/resolve/main/src/config/opencode-config-sync.js
- Command line
-
hf download hf://VanyaJ/bada-terminal/src/config/opencode-config-sync.js
-
curl -L -o opencode-config-sync.js https://huggingface.co/VanyaJ/bada-terminal/resolve/main/src/config/opencode-config-sync.js
6.03 kB
| /** | |
| * 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 }; | |