berget 2.2.5 → 2.2.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (148) hide show
  1. package/.github/workflows/publish.yml +8 -8
  2. package/.github/workflows/test.yml +12 -6
  3. package/.husky/pre-commit +1 -0
  4. package/.prettierignore +15 -0
  5. package/.prettierrc +5 -3
  6. package/CONTRIBUTING.md +38 -0
  7. package/README.md +2 -148
  8. package/dist/index.js +21 -21
  9. package/dist/package.json +30 -2
  10. package/dist/src/agents/app.js +28 -0
  11. package/dist/src/agents/backend.js +25 -0
  12. package/dist/src/agents/devops.js +34 -0
  13. package/dist/src/agents/frontend.js +25 -0
  14. package/dist/src/agents/fullstack.js +25 -0
  15. package/dist/src/agents/index.js +61 -0
  16. package/dist/src/agents/quality.js +70 -0
  17. package/dist/src/agents/security.js +26 -0
  18. package/dist/src/agents/types.js +2 -0
  19. package/dist/src/client.js +54 -62
  20. package/dist/src/commands/api-keys.js +132 -140
  21. package/dist/src/commands/auth.js +9 -9
  22. package/dist/src/commands/autocomplete.js +9 -9
  23. package/dist/src/commands/billing.js +7 -9
  24. package/dist/src/commands/chat.js +90 -92
  25. package/dist/src/commands/clusters.js +12 -12
  26. package/dist/src/commands/code/__tests__/auth-sync.test.js +348 -0
  27. package/dist/src/commands/code/__tests__/fake-api-key-service.js +23 -0
  28. package/dist/src/commands/code/__tests__/fake-auth-service.js +55 -0
  29. package/dist/src/commands/code/__tests__/fake-command-runner.js +50 -0
  30. package/dist/src/commands/code/__tests__/fake-file-store.js +55 -0
  31. package/dist/src/commands/code/__tests__/fake-prompter.js +133 -0
  32. package/dist/src/commands/code/__tests__/setup-flow.test.js +505 -0
  33. package/dist/src/commands/code/adapters/clack-prompter.js +81 -0
  34. package/dist/src/commands/code/adapters/fs-file-store.js +80 -0
  35. package/dist/src/commands/code/adapters/spawn-command-runner.js +53 -0
  36. package/dist/src/commands/code/auth-sync.js +283 -0
  37. package/dist/src/commands/code/errors.js +27 -0
  38. package/dist/src/commands/code/ports/auth-services.js +2 -0
  39. package/dist/src/commands/code/ports/command-runner.js +2 -0
  40. package/dist/src/commands/code/ports/file-store.js +2 -0
  41. package/dist/src/commands/code/ports/prompter.js +2 -0
  42. package/dist/src/commands/code/setup.js +533 -0
  43. package/dist/src/commands/code.js +223 -779
  44. package/dist/src/commands/models.js +13 -15
  45. package/dist/src/commands/users.js +6 -8
  46. package/dist/src/constants/command-structure.js +116 -114
  47. package/dist/src/services/api-key-service.js +43 -48
  48. package/dist/src/services/auth-service.js +60 -299
  49. package/dist/src/services/browser-auth.js +278 -0
  50. package/dist/src/services/chat-service.js +78 -91
  51. package/dist/src/services/cluster-service.js +6 -6
  52. package/dist/src/services/collaborator-service.js +5 -8
  53. package/dist/src/services/flux-service.js +5 -8
  54. package/dist/src/services/helm-service.js +5 -8
  55. package/dist/src/services/kubectl-service.js +7 -10
  56. package/dist/src/utils/config-checker.js +5 -5
  57. package/dist/src/utils/config-loader.js +25 -25
  58. package/dist/src/utils/default-api-key.js +23 -23
  59. package/dist/src/utils/env-manager.js +7 -7
  60. package/dist/src/utils/error-handler.js +60 -61
  61. package/dist/src/utils/logger.js +7 -7
  62. package/dist/src/utils/markdown-renderer.js +2 -2
  63. package/dist/src/utils/opencode-validator.js +17 -20
  64. package/dist/src/utils/token-manager.js +38 -11
  65. package/dist/tests/commands/chat.test.js +24 -24
  66. package/dist/tests/commands/code.test.js +169 -138
  67. package/dist/tests/utils/config-loader.test.js +114 -114
  68. package/dist/tests/utils/env-manager.test.js +57 -57
  69. package/dist/tests/utils/opencode-validator.test.js +44 -43
  70. package/dist/vitest.config.js +1 -1
  71. package/eslint.config.mjs +47 -0
  72. package/index.ts +42 -48
  73. package/package.json +30 -2
  74. package/src/agents/app.ts +27 -0
  75. package/src/agents/backend.ts +24 -0
  76. package/src/agents/devops.ts +33 -0
  77. package/src/agents/frontend.ts +24 -0
  78. package/src/agents/fullstack.ts +24 -0
  79. package/src/agents/index.ts +71 -0
  80. package/src/agents/quality.ts +69 -0
  81. package/src/agents/security.ts +26 -0
  82. package/src/agents/types.ts +17 -0
  83. package/src/client.ts +125 -167
  84. package/src/commands/api-keys.ts +261 -358
  85. package/src/commands/auth.ts +24 -30
  86. package/src/commands/autocomplete.ts +12 -12
  87. package/src/commands/billing.ts +22 -27
  88. package/src/commands/chat.ts +230 -323
  89. package/src/commands/clusters.ts +33 -33
  90. package/src/commands/code/__tests__/auth-sync.test.ts +481 -0
  91. package/src/commands/code/__tests__/fake-api-key-service.ts +13 -0
  92. package/src/commands/code/__tests__/fake-auth-service.ts +50 -0
  93. package/src/commands/code/__tests__/fake-command-runner.ts +44 -0
  94. package/src/commands/code/__tests__/fake-file-store.ts +44 -0
  95. package/src/commands/code/__tests__/fake-prompter.ts +121 -0
  96. package/src/commands/code/__tests__/setup-flow.test.ts +628 -0
  97. package/src/commands/code/adapters/clack-prompter.ts +55 -0
  98. package/src/commands/code/adapters/fs-file-store.ts +37 -0
  99. package/src/commands/code/adapters/spawn-command-runner.ts +40 -0
  100. package/src/commands/code/auth-sync.ts +329 -0
  101. package/src/commands/code/errors.ts +23 -0
  102. package/src/commands/code/ports/auth-services.ts +14 -0
  103. package/src/commands/code/ports/command-runner.ts +10 -0
  104. package/src/commands/code/ports/file-store.ts +7 -0
  105. package/src/commands/code/ports/prompter.ts +29 -0
  106. package/src/commands/code/setup.ts +630 -0
  107. package/src/commands/code.ts +335 -1074
  108. package/src/commands/index.ts +19 -19
  109. package/src/commands/models.ts +32 -37
  110. package/src/commands/users.ts +15 -22
  111. package/src/constants/command-structure.ts +120 -140
  112. package/src/services/api-key-service.ts +96 -113
  113. package/src/services/auth-service.ts +92 -339
  114. package/src/services/browser-auth.ts +296 -0
  115. package/src/services/chat-service.ts +246 -279
  116. package/src/services/cluster-service.ts +29 -32
  117. package/src/services/collaborator-service.ts +13 -18
  118. package/src/services/flux-service.ts +16 -18
  119. package/src/services/helm-service.ts +16 -18
  120. package/src/services/kubectl-service.ts +12 -14
  121. package/src/types/api.d.ts +924 -926
  122. package/src/types/json.d.ts +3 -3
  123. package/src/utils/config-checker.ts +10 -10
  124. package/src/utils/config-loader.ts +110 -127
  125. package/src/utils/default-api-key.ts +81 -93
  126. package/src/utils/env-manager.ts +36 -40
  127. package/src/utils/error-handler.ts +83 -78
  128. package/src/utils/logger.ts +41 -41
  129. package/src/utils/markdown-renderer.ts +11 -11
  130. package/src/utils/opencode-validator.ts +51 -56
  131. package/src/utils/token-manager.ts +84 -64
  132. package/templates/agents/app.md +23 -0
  133. package/templates/agents/backend.md +23 -0
  134. package/templates/agents/devops.md +30 -0
  135. package/templates/agents/frontend.md +25 -0
  136. package/templates/agents/fullstack.md +23 -0
  137. package/templates/agents/quality.md +69 -0
  138. package/templates/agents/security.md +21 -0
  139. package/tests/commands/chat.test.ts +60 -70
  140. package/tests/commands/code.test.ts +346 -345
  141. package/tests/utils/config-loader.test.ts +260 -260
  142. package/tests/utils/env-manager.test.ts +127 -134
  143. package/tests/utils/opencode-validator.test.ts +65 -69
  144. package/tsconfig.json +2 -2
  145. package/vitest.config.ts +3 -3
  146. package/AGENTS.md +0 -374
  147. package/TODO.md +0 -19
  148. package/opencode.json +0 -146
@@ -0,0 +1,630 @@
1
+ import type { Prompter } from "./ports/prompter";
2
+ import type { FileStore } from "./ports/file-store";
3
+ import type { CommandRunner } from "./ports/command-runner";
4
+ import type { AuthServicePort, ApiKeyServicePort } from "./ports/auth-services";
5
+ import { CancelledError, CommandFailedError, PrerequisiteError } from "./errors";
6
+ import { modify, parse, applyEdits } from "jsonc-parser";
7
+ import { configureAuth } from "./auth-sync.js";
8
+ import { ClackPrompter } from "./adapters/clack-prompter.js";
9
+ import { FsFileStore } from "./adapters/fs-file-store.js";
10
+ import { SpawnCommandRunner } from "./adapters/spawn-command-runner.js";
11
+ import { AuthService } from "../../services/auth-service.js";
12
+ import { ApiKeyService } from "../../services/api-key-service.js";
13
+ import { getAllAgents, toMarkdown, toPiPrompt } from "../../agents/index.js";
14
+ import * as os from "os";
15
+
16
+ const OPENCODE_PLUGIN = "@bergetai/opencode-auth@1.0.16";
17
+ const PI_PROVIDER = "npm:@bergetai/pi-provider";
18
+ const OPENCODE_PLUGIN_NAME = "@bergetai/opencode-auth";
19
+ const PI_PROVIDER_NAME = "@bergetai/pi-provider";
20
+
21
+ export interface WizardDeps {
22
+ prompter: Prompter;
23
+ files: FileStore;
24
+ commands: CommandRunner;
25
+ authService: AuthServicePort;
26
+ apiKeyService: ApiKeyServicePort;
27
+ homeDir: string;
28
+ cwd: string;
29
+ }
30
+
31
+ export async function runSetup(deps: WizardDeps): Promise<void> {
32
+ const { prompter, files, commands, authService, apiKeyService, homeDir, cwd } = deps;
33
+
34
+ prompter.intro("\uD83D\uDD27 Berget Code Setup");
35
+
36
+ const ocState = await getOpencodeState(files, homeDir, cwd);
37
+ const piState = await getPiState(files, homeDir, cwd);
38
+
39
+ const tool = await prompter.select<"opencode" | "pi">({
40
+ message: "How do you want to use Berget AI?",
41
+ options: [
42
+ {
43
+ value: "opencode",
44
+ label: `OpenCode${getOpencodeLabel(ocState)}`,
45
+ hint: "Open source AI coding agent",
46
+ },
47
+ {
48
+ value: "pi",
49
+ label: `Pi${getPiLabel(piState)}`,
50
+ hint: "Minimal terminal coding harness",
51
+ },
52
+ ],
53
+ });
54
+
55
+ const scope = await prompter.select<"project" | "global">({
56
+ message: "Where should the configuration apply?",
57
+ options: [
58
+ {
59
+ value: "project",
60
+ label: "This project only",
61
+ hint:
62
+ tool === "opencode"
63
+ ? ocState.project
64
+ ? "Already configured"
65
+ : "opencode.json in current directory"
66
+ : piState.project
67
+ ? "Already configured"
68
+ : ".pi/settings.json in current directory",
69
+ },
70
+ {
71
+ value: "global",
72
+ label: "Globally for all projects",
73
+ hint:
74
+ tool === "opencode"
75
+ ? ocState.global
76
+ ? "Already configured"
77
+ : "~/.config/opencode/opencode.json"
78
+ : piState.global
79
+ ? "Already configured"
80
+ : "~/.pi/agent/settings.json",
81
+ },
82
+ ],
83
+ });
84
+
85
+ const authResult = await configureAuth(
86
+ { prompter, files, authService, apiKeyService, homeDir },
87
+ tool
88
+ );
89
+
90
+ if (tool === "opencode") {
91
+ await setupOpenCode({ prompter, files, commands, homeDir, cwd, scope });
92
+ await setupOpenCodeAgents({ prompter, files, homeDir, cwd, scope });
93
+
94
+ if (authResult.authenticated) {
95
+ prompter.note(
96
+ `You're all set!\n\n1. Run: opencode\n2. Select model: /models\n\nFor more information, see official docs:\n\nhttps://github.com/berget-ai/opencode-berget-auth`,
97
+ "Successfully configured Berget AI for OpenCode"
98
+ );
99
+ } else {
100
+ prompter.note(
101
+ `Next steps:\n\n1. Run: opencode\n2. Type: /connect\n3. Choose your auth method:\n • "Login with Berget" — Berget Code plan\n • "Enter Berget API Key manually"\n • (or set BERGET_API_KEY env var)\n4. Select model: /models\n\nFor more information, see official docs:\n\nhttps://github.com/berget-ai/opencode-berget-auth`,
102
+ "Successfully configured Berget AI for OpenCode"
103
+ );
104
+ }
105
+ } else {
106
+ await setupPi({ prompter, files, commands, homeDir, cwd, scope });
107
+ await setupPiAgent({ prompter, files, homeDir, cwd, scope });
108
+
109
+ if (authResult.authenticated) {
110
+ prompter.note(
111
+ `You're all set!\n\n1. Restart Pi or run /reload\n2. Select model: /model\n\nFor more information, see official docs:\n\nhttps://github.com/berget-ai/pi-provider`,
112
+ "Successfully configured Berget AI for Pi"
113
+ );
114
+ } else {
115
+ prompter.note(
116
+ `Next steps:\n\n1. Restart Pi or run /reload\n2. Type: /login\n3. Choose your auth method:\n • "Use a subscription" → Berget AI\n • (or set BERGET_API_KEY env var)\n4. Select model: /model\n\nFor more information, see official docs:\n\nhttps://github.com/berget-ai/pi-provider`,
117
+ "Successfully configured Berget AI for Pi"
118
+ );
119
+ }
120
+ }
121
+
122
+ prompter.outro("Setup complete!");
123
+ }
124
+
125
+ // ─── OpenCode ────────────────────────────────────────────────────────────────
126
+
127
+ async function setupOpenCode(deps: {
128
+ prompter: Prompter;
129
+ files: FileStore;
130
+ commands: CommandRunner;
131
+ homeDir: string;
132
+ cwd: string;
133
+ scope: "project" | "global";
134
+ }): Promise<void> {
135
+ const { prompter, files, commands, homeDir, cwd, scope } = deps;
136
+
137
+ const installed = await commands.checkInstalled("opencode");
138
+ if (!installed) {
139
+ throw new PrerequisiteError("opencode");
140
+ }
141
+
142
+ const configPath = await resolveOpencodeConfigPath(files, homeDir, cwd, scope);
143
+ const existingContent = await files.readFile(configPath);
144
+ const newContent = generateModifiedContent(existingContent, configPath);
145
+
146
+ if (existingContent && existingContent === newContent) {
147
+ return;
148
+ }
149
+
150
+ if (existingContent) {
151
+ prompter.note(generateDiff(existingContent, newContent, configPath), "Changes to be written");
152
+ } else {
153
+ prompter.note(`New config at ${configPath}:\n\n${newContent}`, "Config preview");
154
+ }
155
+
156
+ const shouldWrite = await prompter.confirm({
157
+ message: existingContent ? `Write these changes to ${configPath}?` : `Create ${configPath}?`,
158
+ initialValue: true,
159
+ });
160
+ if (!shouldWrite) throw new CancelledError();
161
+
162
+ const s = prompter.spinner();
163
+ s.start("Writing OpenCode configuration...");
164
+ await files.writeFile(configPath, newContent);
165
+ s.stop(`Wrote configuration to ${configPath}.`);
166
+ }
167
+
168
+ // ─── Pi ────────────────────────────────────────────────────────────────────────
169
+
170
+ async function setupPi(deps: {
171
+ prompter: Prompter;
172
+ files: FileStore;
173
+ commands: CommandRunner;
174
+ homeDir: string;
175
+ cwd: string;
176
+ scope: "project" | "global";
177
+ }): Promise<void> {
178
+ const { prompter, files, commands, homeDir, cwd, scope } = deps;
179
+ const s = prompter.spinner();
180
+
181
+ const installed = await commands.checkInstalled("pi");
182
+ if (!installed) {
183
+ throw new PrerequisiteError("pi");
184
+ }
185
+
186
+ const installArgs =
187
+ scope === "project" ? ["install", "-l", PI_PROVIDER] : ["install", PI_PROVIDER];
188
+
189
+ s.start(`Installing Berget AI provider for Pi...`);
190
+ try {
191
+ await commands.run("pi", installArgs);
192
+ s.stop("Installed Pi provider.");
193
+ } catch {
194
+ s.stop("Pi provider installation failed. Please try again or install manually.");
195
+ throw new CommandFailedError(`pi ${installArgs.join(" ")}`, 1);
196
+ }
197
+
198
+ const settingsPath =
199
+ scope === "project"
200
+ ? pathJoin(cwd, ".pi", "settings.json")
201
+ : pathJoin(homeDir, ".pi", "agent", "settings.json");
202
+
203
+ let settings = (await readJsonMaybe(files, settingsPath)) || {};
204
+
205
+ if (settings.defaultProvider === "berget") {
206
+ prompter.note(
207
+ "Berget AI is already set as your default provider.",
208
+ "Default provider already set"
209
+ );
210
+ } else {
211
+ if (settings.defaultProvider) {
212
+ const makeDefault = await prompter.confirm({
213
+ message: `Your default provider is ${settings.defaultProvider}. Switch to Berget AI instead?`,
214
+ initialValue: false,
215
+ });
216
+ if (makeDefault) {
217
+ settings.defaultProvider = "berget";
218
+ await writeJsonFile(files, settingsPath, settings);
219
+ prompter.note("Berget AI is now your default provider.", "Updated default provider");
220
+ }
221
+ } else {
222
+ settings.defaultProvider = "berget";
223
+ await writeJsonFile(files, settingsPath, settings);
224
+ prompter.note("Berget AI is now your default provider.", "Updated default provider");
225
+ }
226
+ }
227
+ }
228
+
229
+ async function setupOpenCodeAgents(deps: {
230
+ prompter: Prompter;
231
+ files: FileStore;
232
+ homeDir: string;
233
+ cwd: string;
234
+ scope: "project" | "global";
235
+ }): Promise<void> {
236
+ const { prompter, files, homeDir, cwd, scope } = deps;
237
+
238
+ const agents = getAllAgents().filter(a => a.config.mode === "primary");
239
+
240
+ if (agents.length === 0) {
241
+ return;
242
+ }
243
+
244
+ const selectedAgents = await prompter.multiselect({
245
+ message: "Select agents to set up (optional - press enter to skip):",
246
+ options: agents.map(agent => ({
247
+ value: agent.config.name,
248
+ label: agent.config.name,
249
+ hint: agent.config.description,
250
+ })),
251
+ });
252
+
253
+ if (selectedAgents.length === 0) {
254
+ return;
255
+ }
256
+
257
+ const agentsDir =
258
+ scope === "project"
259
+ ? pathJoin(cwd, ".opencode", "agents")
260
+ : pathJoin(homeDir, ".config", "opencode", "agents");
261
+
262
+ await files.mkdir(agentsDir);
263
+
264
+ const hasChanges = await Promise.all(
265
+ selectedAgents.map(async agentName => {
266
+ const agent = agents.find(a => a.config.name === agentName);
267
+ if (!agent) return false;
268
+
269
+ const agentPath = pathJoin(agentsDir, `${agentName}.md`);
270
+ const existing = await files.readFile(agentPath);
271
+ const newContent = toMarkdown(agent);
272
+
273
+ if (existing === newContent) {
274
+ return false;
275
+ }
276
+
277
+ if (existing) {
278
+ prompter.note(
279
+ generateDiff(existing, newContent, agentPath),
280
+ `Changes to ${agentName} agent`
281
+ );
282
+ }
283
+
284
+ return true;
285
+ })
286
+ );
287
+
288
+ if (!hasChanges.some(Boolean)) {
289
+ prompter.note("Agent files are already up to date.", "No changes needed");
290
+ return;
291
+ }
292
+
293
+ const shouldWrite = await prompter.confirm({
294
+ message: "Write agent configuration files?",
295
+ initialValue: true,
296
+ });
297
+
298
+ if (!shouldWrite) {
299
+ throw new CancelledError();
300
+ }
301
+
302
+ const s = prompter.spinner();
303
+ s.start("Writing agent configurations...");
304
+
305
+ for (const agentName of selectedAgents) {
306
+ const agent = agents.find(a => a.config.name === agentName);
307
+ if (!agent) continue;
308
+
309
+ const agentPath = pathJoin(agentsDir, `${agentName}.md`);
310
+ const content = toMarkdown(agent);
311
+ await files.writeFile(agentPath, content);
312
+ }
313
+
314
+ s.stop(`Wrote ${selectedAgents.length} agent(s) to ${agentsDir}`);
315
+ }
316
+
317
+ async function setupPiAgent(deps: {
318
+ prompter: Prompter;
319
+ files: FileStore;
320
+ homeDir: string;
321
+ cwd: string;
322
+ scope: "project" | "global";
323
+ }): Promise<void> {
324
+ const { prompter, files, homeDir, cwd, scope } = deps;
325
+
326
+ const agents = getAllAgents().filter(a => a.config.mode === "primary");
327
+
328
+ if (agents.length === 0) {
329
+ return;
330
+ }
331
+
332
+ const selectedAgentName = await prompter.select({
333
+ message: "Select an agent (optional - press enter to skip):",
334
+ options: [
335
+ { value: "__skip__", label: "Skip agent setup" },
336
+ ...agents.map(agent => ({
337
+ value: agent.config.name,
338
+ label: agent.config.name,
339
+ hint: agent.config.description,
340
+ })),
341
+ ],
342
+ });
343
+
344
+ if (selectedAgentName === "__skip__") {
345
+ return;
346
+ }
347
+
348
+ const agent = agents.find(a => a.config.name === selectedAgentName);
349
+ if (!agent) return;
350
+
351
+ const systemPath =
352
+ scope === "project"
353
+ ? pathJoin(cwd, ".pi", "SYSTEM.md")
354
+ : pathJoin(homeDir, ".pi", "agent", "SYSTEM.md");
355
+
356
+ const existing = await files.readFile(systemPath);
357
+ const newContent = toPiPrompt(agent);
358
+
359
+ if (existing === newContent) {
360
+ prompter.note("Agent configuration is already up to date.", "No changes needed");
361
+ return;
362
+ }
363
+
364
+ if (existing) {
365
+ prompter.note(generateDiff(existing, newContent, systemPath), "Changes to agent configuration");
366
+ } else {
367
+ prompter.note(newContent, "New agent configuration");
368
+ }
369
+
370
+ const shouldWrite = await prompter.confirm({
371
+ message: existing ? "Overwrite existing agent configuration?" : "Create agent configuration?",
372
+ initialValue: true,
373
+ });
374
+
375
+ if (!shouldWrite) {
376
+ throw new CancelledError();
377
+ }
378
+
379
+ const s = prompter.spinner();
380
+ s.start("Writing agent configuration...");
381
+
382
+ const systemDir = scope === "project" ? pathJoin(cwd, ".pi") : pathJoin(homeDir, ".pi", "agent");
383
+ await files.mkdir(systemDir);
384
+ await files.writeFile(systemPath, newContent);
385
+
386
+ s.stop(`Wrote agent configuration to ${systemPath}`);
387
+ }
388
+
389
+ // ─── Helpers ─────────────────────────────────────────────────────────────────
390
+
391
+ function pathJoin(...parts: string[]): string {
392
+ // Simple path join that avoids importing 'path' module
393
+ // This is good enough for cross-platform testing since tests control the path format
394
+ return parts.join("/");
395
+ }
396
+
397
+ function stripJsoncComments(content: string): string {
398
+ content = content.replace(/\/\/.*$/gm, "");
399
+ content = content.replace(/\/\*[\s\S]*?\*\//g, "");
400
+ return content;
401
+ }
402
+
403
+ function generateDiff(oldText: string, newText: string, filePath: string): string {
404
+ const oldLines = oldText.split("\n");
405
+ const newLines = newText.split("\n");
406
+ let result = `--- ${filePath}\n+++ ${filePath}\n`;
407
+
408
+ const maxLen = Math.max(oldLines.length, newLines.length);
409
+ for (let i = 0; i < maxLen; i++) {
410
+ const oldLine = oldLines[i];
411
+ const newLine = newLines[i];
412
+ if (oldLine !== newLine) {
413
+ if (oldLine !== undefined) result += `- ${oldLine}\n`;
414
+ if (newLine !== undefined) result += `+ ${newLine}\n`;
415
+ }
416
+ }
417
+ return result.trimEnd();
418
+ }
419
+
420
+ async function readJsonMaybe(files: FileStore, filePath: string): Promise<any | null> {
421
+ const content = await files.readFile(filePath);
422
+ if (!content) return null;
423
+ try {
424
+ return JSON.parse(content);
425
+ } catch {
426
+ try {
427
+ return JSON.parse(stripJsoncComments(content));
428
+ } catch {
429
+ return null;
430
+ }
431
+ }
432
+ }
433
+
434
+ async function writeJsonFile(
435
+ files: FileStore,
436
+ filePath: string,
437
+ data: Record<string, unknown>
438
+ ): Promise<void> {
439
+ await files.writeFile(filePath, JSON.stringify(data, null, 2) + "\n");
440
+ }
441
+
442
+ async function hasPluginInConfig(config: any): Promise<boolean> {
443
+ if (!config) return false;
444
+ const plugins = config.plugin || config.plugins || [];
445
+ return plugins.some((p: string) => p.includes(OPENCODE_PLUGIN_NAME));
446
+ }
447
+
448
+ async function hasPiProviderInSettings(settings: any): Promise<boolean> {
449
+ if (!settings) return false;
450
+ const packages = settings.packages || [];
451
+ return packages.some((p: any) => {
452
+ if (typeof p === "string") return p.includes(PI_PROVIDER_NAME);
453
+ if (typeof p === "object" && p.source) return p.source.includes(PI_PROVIDER_NAME);
454
+ return false;
455
+ });
456
+ }
457
+
458
+ async function getOpencodeState(
459
+ files: FileStore,
460
+ homeDir: string,
461
+ cwd: string
462
+ ): Promise<{ project: boolean; global: boolean }> {
463
+ const projectJsonc = await readJsonMaybe(files, pathJoin(cwd, "opencode.jsonc"));
464
+ const projectJson = await readJsonMaybe(files, pathJoin(cwd, "opencode.json"));
465
+ const globalJsonc = await readJsonMaybe(
466
+ files,
467
+ pathJoin(homeDir, ".config", "opencode", "opencode.jsonc")
468
+ );
469
+ const globalJson = await readJsonMaybe(
470
+ files,
471
+ pathJoin(homeDir, ".config", "opencode", "opencode.json")
472
+ );
473
+
474
+ return {
475
+ project: (await hasPluginInConfig(projectJsonc)) || (await hasPluginInConfig(projectJson)),
476
+ global: (await hasPluginInConfig(globalJsonc)) || (await hasPluginInConfig(globalJson)),
477
+ };
478
+ }
479
+
480
+ async function getPiState(
481
+ files: FileStore,
482
+ homeDir: string,
483
+ cwd: string
484
+ ): Promise<{ project: boolean; global: boolean }> {
485
+ const projectSettings = await readJsonMaybe(files, pathJoin(cwd, ".pi", "settings.json"));
486
+ const globalSettings = await readJsonMaybe(
487
+ files,
488
+ pathJoin(homeDir, ".pi", "agent", "settings.json")
489
+ );
490
+
491
+ return {
492
+ project: await hasPiProviderInSettings(projectSettings),
493
+ global: await hasPiProviderInSettings(globalSettings),
494
+ };
495
+ }
496
+
497
+ function getOpencodeLabel(state: { project: boolean; global: boolean }): string {
498
+ if (state.project || state.global) return " (already configured)";
499
+ return "";
500
+ }
501
+
502
+ function getPiLabel(state: { project: boolean; global: boolean }): string {
503
+ if (state.project || state.global) return " (already configured)";
504
+ return "";
505
+ }
506
+
507
+ async function resolveOpencodeConfigPath(
508
+ files: FileStore,
509
+ homeDir: string,
510
+ cwd: string,
511
+ scope: "project" | "global"
512
+ ): Promise<string> {
513
+ if (scope === "project") {
514
+ const jsoncPath = pathJoin(cwd, "opencode.jsonc");
515
+ const jsonPath = pathJoin(cwd, "opencode.json");
516
+ if (await files.exists(jsoncPath)) return jsoncPath;
517
+ if (await files.exists(jsonPath)) return jsonPath;
518
+ return jsonPath;
519
+ } else {
520
+ const globalDir = pathJoin(homeDir, ".config", "opencode");
521
+ const jsoncPath = pathJoin(globalDir, "opencode.jsonc");
522
+ const jsonPath = pathJoin(globalDir, "opencode.json");
523
+ if (await files.exists(jsoncPath)) return jsoncPath;
524
+ if (await files.exists(jsonPath)) return jsonPath;
525
+ return jsonPath;
526
+ }
527
+ }
528
+
529
+ function generateModifiedContent(existingContent: string | null, configPath: string): string {
530
+ if (configPath.endsWith(".jsonc")) {
531
+ const content = existingContent || "{}";
532
+ const parseErrors: any[] = [];
533
+ const parsed = parse(content, parseErrors, {
534
+ allowTrailingComma: true,
535
+ disallowComments: false,
536
+ });
537
+
538
+ let jsConfig: Record<string, any> = {};
539
+ const canModifyText =
540
+ parsed !== undefined &&
541
+ typeof parsed === "object" &&
542
+ parsed !== null &&
543
+ !Array.isArray(parsed);
544
+
545
+ if (canModifyText) {
546
+ jsConfig = parsed as Record<string, any>;
547
+ }
548
+
549
+ const pluginsKey = jsConfig.plugins !== undefined ? "plugins" : "plugin";
550
+ const existing: string[] = jsConfig[pluginsKey] || [];
551
+ const filtered = existing.filter((p: string) => !p.includes(OPENCODE_PLUGIN_NAME));
552
+ filtered.push(OPENCODE_PLUGIN);
553
+
554
+ if (canModifyText) {
555
+ let modifiedContent = content;
556
+ const pluginEdits = modify(modifiedContent, [pluginsKey], filtered, {
557
+ formattingOptions: { insertSpaces: true, tabSize: 2 },
558
+ });
559
+ modifiedContent = applyEdits(modifiedContent, pluginEdits);
560
+
561
+ if (!jsConfig.$schema) {
562
+ const schemaEdits = modify(
563
+ modifiedContent,
564
+ ["$schema"],
565
+ "https://opencode.ai/config.json",
566
+ {
567
+ formattingOptions: { insertSpaces: true, tabSize: 2 },
568
+ }
569
+ );
570
+ modifiedContent = applyEdits(modifiedContent, schemaEdits);
571
+ }
572
+
573
+ return modifiedContent;
574
+ }
575
+
576
+ // Malformed, empty, or non-object JSONC — write a clean config
577
+ const config: Record<string, any> = {
578
+ [pluginsKey]: filtered,
579
+ $schema: "https://opencode.ai/config.json",
580
+ };
581
+ return JSON.stringify(config, null, 2) + "\n";
582
+ }
583
+
584
+ // Plain JSON
585
+ let config: Record<string, any> = {};
586
+ if (existingContent) {
587
+ try {
588
+ config = JSON.parse(existingContent);
589
+ } catch {
590
+ // ignore malformed, overwrite
591
+ }
592
+ }
593
+
594
+ const pluginsKey = config.plugins !== undefined ? "plugins" : "plugin";
595
+ const existing: string[] = config[pluginsKey] || [];
596
+ const filtered = existing.filter((p: string) => !p.includes(OPENCODE_PLUGIN_NAME));
597
+ filtered.push(OPENCODE_PLUGIN);
598
+ config[pluginsKey] = filtered;
599
+ config.$schema = config.$schema || "https://opencode.ai/config.json";
600
+
601
+ return JSON.stringify(config, null, 2) + "\n";
602
+ }
603
+
604
+ export async function runSetupCommand(): Promise<void> {
605
+ try {
606
+ await runSetup({
607
+ prompter: new ClackPrompter(),
608
+ files: new FsFileStore(),
609
+ commands: new SpawnCommandRunner(),
610
+ authService: AuthService.getInstance(),
611
+ apiKeyService: ApiKeyService.getInstance(),
612
+ homeDir: os.homedir(),
613
+ cwd: process.cwd(),
614
+ });
615
+ process.exit(0);
616
+ } catch (err) {
617
+ if (err instanceof CancelledError) {
618
+ process.exit(130);
619
+ }
620
+ if (err instanceof PrerequisiteError) {
621
+ console.error(`Missing required binary: ${err.binary}`);
622
+ process.exit(2);
623
+ }
624
+ if (err instanceof CommandFailedError) {
625
+ console.error(err.message);
626
+ process.exit(5);
627
+ }
628
+ throw err;
629
+ }
630
+ }