opencode.ts 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /**
  2. * opencode target.
  3. *
  4. * - MCP server entry to `~/.config/opencode/opencode.jsonc` (global,
  5. * XDG-style; `%APPDATA%/opencode/opencode.jsonc` on Windows) or
  6. * `./opencode.jsonc` (local). Falls back to `opencode.json` when a
  7. * `.json` file already exists; defaults new installs to `.jsonc`
  8. * because that's what opencode itself creates on first run.
  9. * - Instructions to `~/.config/opencode/AGENTS.md` (global) or
  10. * `./AGENTS.md` (local). opencode reads AGENTS.md for agent
  11. * instructions — same convention Codex CLI uses.
  12. * - No permissions concept.
  13. *
  14. * Config shape uses opencode's wrapper:
  15. * {
  16. * "$schema": "https://opencode.ai/config.json",
  17. * "mcp": { "codegraph": { "type": "local", "command": [...], "enabled": true } }
  18. * }
  19. *
  20. * The shape differs from Claude/Cursor — opencode uses `mcp.<name>`
  21. * (not `mcpServers`), takes `command` as a string array combining
  22. * binary + args, and includes an explicit `enabled` flag.
  23. *
  24. * Reads + writes go through `jsonc-parser` so any `//` and `/* *\/`
  25. * comments the user has added to their `.jsonc` survive idempotent
  26. * re-runs.
  27. */
  28. import * as fs from 'fs';
  29. import * as path from 'path';
  30. import * as os from 'os';
  31. import { parse as parseJsonc, modify, applyEdits } from 'jsonc-parser';
  32. import {
  33. AgentTarget,
  34. DetectionResult,
  35. InstallOptions,
  36. Location,
  37. WriteResult,
  38. } from './types';
  39. import {
  40. atomicWriteFileSync,
  41. jsonDeepEqual,
  42. removeMarkedSection,
  43. replaceOrAppendMarkedSection,
  44. } from './shared';
  45. import {
  46. CODEGRAPH_SECTION_END,
  47. CODEGRAPH_SECTION_START,
  48. INSTRUCTIONS_TEMPLATE,
  49. } from '../instructions-template';
  50. function globalConfigDir(): string {
  51. if (process.platform === 'win32') {
  52. const appData = process.env.APPDATA ?? path.join(os.homedir(), 'AppData', 'Roaming');
  53. return path.join(appData, 'opencode');
  54. }
  55. // XDG_CONFIG_HOME if set, else ~/.config — matches opencode's docs.
  56. const xdg = process.env.XDG_CONFIG_HOME && process.env.XDG_CONFIG_HOME.trim().length > 0
  57. ? process.env.XDG_CONFIG_HOME
  58. : path.join(os.homedir(), '.config');
  59. return path.join(xdg, 'opencode');
  60. }
  61. function configBaseDir(loc: Location): string {
  62. return loc === 'global' ? globalConfigDir() : process.cwd();
  63. }
  64. // Pick existing .jsonc, then .json, default to .jsonc for new files.
  65. // opencode auto-creates .jsonc on first run, so that's the dominant
  66. // real-world case and the sensible default for greenfield installs.
  67. function configPath(loc: Location): string {
  68. const dir = configBaseDir(loc);
  69. const jsonc = path.join(dir, 'opencode.jsonc');
  70. const json = path.join(dir, 'opencode.json');
  71. if (fs.existsSync(jsonc)) return jsonc;
  72. if (fs.existsSync(json)) return json;
  73. return jsonc;
  74. }
  75. function instructionsPath(loc: Location): string {
  76. return path.join(configBaseDir(loc), 'AGENTS.md');
  77. }
  78. function readConfigText(file: string): string {
  79. if (!fs.existsSync(file)) return '';
  80. return fs.readFileSync(file, 'utf-8');
  81. }
  82. function parseConfig(text: string): Record<string, any> {
  83. if (!text.trim()) return {};
  84. const errors: any[] = [];
  85. const result = parseJsonc(text, errors, { allowTrailingComma: true });
  86. if (result == null || typeof result !== 'object' || Array.isArray(result)) {
  87. return {};
  88. }
  89. return result as Record<string, any>;
  90. }
  91. function getOpencodeServerEntry(): { type: string; command: string[]; enabled: boolean } {
  92. return {
  93. type: 'local',
  94. command: ['codegraph', 'serve', '--mcp'],
  95. enabled: true,
  96. };
  97. }
  98. const FORMATTING = { tabSize: 2, insertSpaces: true, eol: '\n' };
  99. class OpencodeTarget implements AgentTarget {
  100. readonly id = 'opencode' as const;
  101. readonly displayName = 'opencode';
  102. readonly docsUrl = 'https://opencode.ai/docs/config';
  103. supportsLocation(_loc: Location): boolean {
  104. return true;
  105. }
  106. detect(loc: Location): DetectionResult {
  107. const file = configPath(loc);
  108. const config = parseConfig(readConfigText(file));
  109. const alreadyConfigured = !!config.mcp?.codegraph;
  110. const installed = loc === 'global'
  111. ? fs.existsSync(globalConfigDir())
  112. : fs.existsSync(file);
  113. return { installed, alreadyConfigured, configPath: file };
  114. }
  115. install(loc: Location, _opts: InstallOptions): WriteResult {
  116. const files: WriteResult['files'] = [];
  117. files.push(writeMcpEntry(loc));
  118. files.push(writeInstructionsEntry(loc));
  119. return { files };
  120. }
  121. uninstall(loc: Location): WriteResult {
  122. const files: WriteResult['files'] = [];
  123. const file = configPath(loc);
  124. if (!fs.existsSync(file)) {
  125. files.push({ path: file, action: 'not-found' });
  126. } else {
  127. const text = readConfigText(file);
  128. const config = parseConfig(text);
  129. if (!config.mcp?.codegraph) {
  130. files.push({ path: file, action: 'not-found' });
  131. } else {
  132. // Drop our key surgically. Leaves siblings + comments untouched.
  133. let edits = modify(text, ['mcp', 'codegraph'], undefined, {
  134. formattingOptions: FORMATTING,
  135. });
  136. let updated = applyEdits(text, edits);
  137. // If `mcp` is now an empty object, drop the wrapper too.
  138. const afterParsed = parseConfig(updated);
  139. if (afterParsed.mcp && typeof afterParsed.mcp === 'object' &&
  140. Object.keys(afterParsed.mcp).length === 0) {
  141. edits = modify(updated, ['mcp'], undefined, { formattingOptions: FORMATTING });
  142. updated = applyEdits(updated, edits);
  143. }
  144. atomicWriteFileSync(file, updated);
  145. files.push({ path: file, action: 'removed' });
  146. }
  147. }
  148. const instr = instructionsPath(loc);
  149. const instrAction = removeMarkedSection(instr, CODEGRAPH_SECTION_START, CODEGRAPH_SECTION_END);
  150. files.push({ path: instr, action: instrAction });
  151. return { files };
  152. }
  153. printConfig(loc: Location): string {
  154. const target = configPath(loc);
  155. const snippet = JSON.stringify({
  156. $schema: 'https://opencode.ai/config.json',
  157. mcp: { codegraph: getOpencodeServerEntry() },
  158. }, null, 2);
  159. return `# Add to ${target}\n\n${snippet}\n`;
  160. }
  161. describePaths(loc: Location): string[] {
  162. return [configPath(loc), instructionsPath(loc)];
  163. }
  164. }
  165. function writeMcpEntry(loc: Location): WriteResult['files'][number] {
  166. const file = configPath(loc);
  167. const existed = fs.existsSync(file);
  168. let text = readConfigText(file);
  169. // Seed a minimal opencode config when the file is brand-new so
  170. // the result is a complete, schema-tagged file (not just a bare
  171. // `{ "mcp": {...} }`).
  172. if (!text.trim()) {
  173. text = '{\n "$schema": "https://opencode.ai/config.json"\n}\n';
  174. }
  175. const config = parseConfig(text);
  176. const before = config.mcp?.codegraph;
  177. const after = getOpencodeServerEntry();
  178. if (jsonDeepEqual(before, after)) {
  179. return { path: file, action: 'unchanged' };
  180. }
  181. // Add $schema if the user's existing file is missing it.
  182. if (!config.$schema) {
  183. const schemaEdits = modify(text, ['$schema'], 'https://opencode.ai/config.json', {
  184. formattingOptions: FORMATTING,
  185. });
  186. text = applyEdits(text, schemaEdits);
  187. }
  188. // Surgical edit — preserves comments, formatting, and order of
  189. // every key we don't touch.
  190. const edits = modify(text, ['mcp', 'codegraph'], after, {
  191. formattingOptions: FORMATTING,
  192. });
  193. const updated = applyEdits(text, edits);
  194. atomicWriteFileSync(file, updated);
  195. return { path: file, action: existed ? 'updated' : 'created' };
  196. }
  197. function writeInstructionsEntry(loc: Location): WriteResult['files'][number] {
  198. const file = instructionsPath(loc);
  199. const dir = path.dirname(file);
  200. if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
  201. const action = replaceOrAppendMarkedSection(
  202. file,
  203. INSTRUCTIONS_TEMPLATE,
  204. CODEGRAPH_SECTION_START,
  205. CODEGRAPH_SECTION_END,
  206. );
  207. const mapped: 'created' | 'updated' | 'unchanged' =
  208. action === 'created' ? 'created'
  209. : action === 'unchanged' ? 'unchanged'
  210. : 'updated';
  211. return { path: file, action: mapped };
  212. }
  213. export const opencodeTarget: AgentTarget = new OpencodeTarget();