forked from colbymchenry/codegraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopencode.ts
More file actions
244 lines (213 loc) · 7.87 KB
/
opencode.ts
File metadata and controls
244 lines (213 loc) · 7.87 KB
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/**
* opencode target.
*
* - MCP server entry to `~/.config/opencode/opencode.jsonc` (global,
* XDG-style; `%APPDATA%/opencode/opencode.jsonc` on Windows) or
* `./opencode.jsonc` (local). Falls back to `opencode.json` when a
* `.json` file already exists; defaults new installs to `.jsonc`
* because that's what opencode itself creates on first run.
* - Instructions to `~/.config/opencode/AGENTS.md` (global) or
* `./AGENTS.md` (local). opencode reads AGENTS.md for agent
* instructions — same convention Codex CLI uses.
* - No permissions concept.
*
* Config shape uses opencode's wrapper:
* {
* "$schema": "https://opencode.ai/config.json",
* "mcp": { "codegraph": { "type": "local", "command": [...], "enabled": true } }
* }
*
* The shape differs from Claude/Cursor — opencode uses `mcp.<name>`
* (not `mcpServers`), takes `command` as a string array combining
* binary + args, and includes an explicit `enabled` flag.
*
* Reads + writes go through `jsonc-parser` so any `//` and `/* *\/`
* comments the user has added to their `.jsonc` survive idempotent
* re-runs.
*/
import * as fs from 'fs';
import * as path from 'path';
import * as os from 'os';
import { parse as parseJsonc, modify, applyEdits } from 'jsonc-parser';
import {
AgentTarget,
DetectionResult,
InstallOptions,
Location,
WriteResult,
} from './types';
import {
atomicWriteFileSync,
jsonDeepEqual,
removeMarkedSection,
replaceOrAppendMarkedSection,
} from './shared';
import {
CODEGRAPH_SECTION_END,
CODEGRAPH_SECTION_START,
INSTRUCTIONS_TEMPLATE,
} from '../instructions-template';
function globalConfigDir(): string {
if (process.platform === 'win32') {
const appData = process.env.APPDATA ?? path.join(os.homedir(), 'AppData', 'Roaming');
return path.join(appData, 'opencode');
}
// XDG_CONFIG_HOME if set, else ~/.config — matches opencode's docs.
const xdg = process.env.XDG_CONFIG_HOME && process.env.XDG_CONFIG_HOME.trim().length > 0
? process.env.XDG_CONFIG_HOME
: path.join(os.homedir(), '.config');
return path.join(xdg, 'opencode');
}
function configBaseDir(loc: Location): string {
return loc === 'global' ? globalConfigDir() : process.cwd();
}
// Pick existing .jsonc, then .json, default to .jsonc for new files.
// opencode auto-creates .jsonc on first run, so that's the dominant
// real-world case and the sensible default for greenfield installs.
function configPath(loc: Location): string {
const dir = configBaseDir(loc);
const jsonc = path.join(dir, 'opencode.jsonc');
const json = path.join(dir, 'opencode.json');
if (fs.existsSync(jsonc)) return jsonc;
if (fs.existsSync(json)) return json;
return jsonc;
}
function instructionsPath(loc: Location): string {
return path.join(configBaseDir(loc), 'AGENTS.md');
}
function readConfigText(file: string): string {
if (!fs.existsSync(file)) return '';
return fs.readFileSync(file, 'utf-8');
}
function parseConfig(text: string): Record<string, any> {
if (!text.trim()) return {};
const errors: any[] = [];
const result = parseJsonc(text, errors, { allowTrailingComma: true });
if (result == null || typeof result !== 'object' || Array.isArray(result)) {
return {};
}
return result as Record<string, any>;
}
function getOpencodeServerEntry(): { type: string; command: string[]; enabled: boolean } {
return {
type: 'local',
command: ['codegraph', 'serve', '--mcp'],
enabled: true,
};
}
const FORMATTING = { tabSize: 2, insertSpaces: true, eol: '\n' };
class OpencodeTarget implements AgentTarget {
readonly id = 'opencode' as const;
readonly displayName = 'opencode';
readonly docsUrl = 'https://opencode.ai/docs/config';
supportsLocation(_loc: Location): boolean {
return true;
}
detect(loc: Location): DetectionResult {
const file = configPath(loc);
const config = parseConfig(readConfigText(file));
const alreadyConfigured = !!config.mcp?.codegraph;
const installed = loc === 'global'
? fs.existsSync(globalConfigDir())
: fs.existsSync(file);
return { installed, alreadyConfigured, configPath: file };
}
install(loc: Location, _opts: InstallOptions): WriteResult {
const files: WriteResult['files'] = [];
files.push(writeMcpEntry(loc));
files.push(writeInstructionsEntry(loc));
return { files };
}
uninstall(loc: Location): WriteResult {
const files: WriteResult['files'] = [];
const file = configPath(loc);
if (!fs.existsSync(file)) {
files.push({ path: file, action: 'not-found' });
} else {
const text = readConfigText(file);
const config = parseConfig(text);
if (!config.mcp?.codegraph) {
files.push({ path: file, action: 'not-found' });
} else {
// Drop our key surgically. Leaves siblings + comments untouched.
let edits = modify(text, ['mcp', 'codegraph'], undefined, {
formattingOptions: FORMATTING,
});
let updated = applyEdits(text, edits);
// If `mcp` is now an empty object, drop the wrapper too.
const afterParsed = parseConfig(updated);
if (afterParsed.mcp && typeof afterParsed.mcp === 'object' &&
Object.keys(afterParsed.mcp).length === 0) {
edits = modify(updated, ['mcp'], undefined, { formattingOptions: FORMATTING });
updated = applyEdits(updated, edits);
}
atomicWriteFileSync(file, updated);
files.push({ path: file, action: 'removed' });
}
}
const instr = instructionsPath(loc);
const instrAction = removeMarkedSection(instr, CODEGRAPH_SECTION_START, CODEGRAPH_SECTION_END);
files.push({ path: instr, action: instrAction });
return { files };
}
printConfig(loc: Location): string {
const target = configPath(loc);
const snippet = JSON.stringify({
$schema: 'https://opencode.ai/config.json',
mcp: { codegraph: getOpencodeServerEntry() },
}, null, 2);
return `# Add to ${target}\n\n${snippet}\n`;
}
describePaths(loc: Location): string[] {
return [configPath(loc), instructionsPath(loc)];
}
}
function writeMcpEntry(loc: Location): WriteResult['files'][number] {
const file = configPath(loc);
const existed = fs.existsSync(file);
let text = readConfigText(file);
// Seed a minimal opencode config when the file is brand-new so
// the result is a complete, schema-tagged file (not just a bare
// `{ "mcp": {...} }`).
if (!text.trim()) {
text = '{\n "$schema": "https://opencode.ai/config.json"\n}\n';
}
const config = parseConfig(text);
const before = config.mcp?.codegraph;
const after = getOpencodeServerEntry();
if (jsonDeepEqual(before, after)) {
return { path: file, action: 'unchanged' };
}
// Add $schema if the user's existing file is missing it.
if (!config.$schema) {
const schemaEdits = modify(text, ['$schema'], 'https://opencode.ai/config.json', {
formattingOptions: FORMATTING,
});
text = applyEdits(text, schemaEdits);
}
// Surgical edit — preserves comments, formatting, and order of
// every key we don't touch.
const edits = modify(text, ['mcp', 'codegraph'], after, {
formattingOptions: FORMATTING,
});
const updated = applyEdits(text, edits);
atomicWriteFileSync(file, updated);
return { path: file, action: existed ? 'updated' : 'created' };
}
function writeInstructionsEntry(loc: Location): WriteResult['files'][number] {
const file = instructionsPath(loc);
const dir = path.dirname(file);
if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
const action = replaceOrAppendMarkedSection(
file,
INSTRUCTIONS_TEMPLATE,
CODEGRAPH_SECTION_START,
CODEGRAPH_SECTION_END,
);
const mapped: 'created' | 'updated' | 'unchanged' =
action === 'created' ? 'created'
: action === 'unchanged' ? 'unchanged'
: 'updated';
return { path: file, action: mapped };
}
export const opencodeTarget: AgentTarget = new OpencodeTarget();