@getmegabrain/cli 0.1.1 → 0.1.3

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.
@@ -7,15 +7,35 @@
7
7
  * same thing server-side for BrainClaw sandboxes) — same config shape, same
8
8
  * gateway, adapted to write to the local user's OpenCode config directory
9
9
  * instead of a pinned in-container path.
10
+ *
11
+ * The CLI rewrites this file on every launch (to pick up catalog and default
12
+ * changes), so it must MERGE into the existing config rather than replace it:
13
+ * only the keys the CLI owns (`provider.megabrain`, `model`) are overwritten,
14
+ * everything the user added themselves (lsp, theme, mcp, other providers…)
15
+ * is preserved.
10
16
  */
11
17
  import fs from 'node:fs';
12
18
  import { GATEWAY_BASE_URL, DEFAULT_MODEL_ID } from './constants.js';
13
19
  import { OPENCODE_CONFIG_DIR, OPENCODE_CONFIG_FILE } from './paths.js';
14
20
  const PROVIDER_ID = 'megabrain';
21
+ /** Legacy auto-router default written by CLI ≤0.1.0; migrated to mb-auto/*. */
22
+ const LEGACY_MODEL_PREFIX = `${PROVIDER_ID}/kilo-auto/`;
15
23
  /** OpenCode's `@ai-sdk/openai-compatible` provider appends `/chat/completions` to `baseURL`. */
16
24
  function toOpenCodeBaseUrl(gatewayBaseUrl) {
17
25
  return gatewayBaseUrl.replace(/\/+$/, '') + '/v1';
18
26
  }
27
+ function readExistingConfig() {
28
+ try {
29
+ const parsed = JSON.parse(fs.readFileSync(OPENCODE_CONFIG_FILE, 'utf8'));
30
+ if (parsed !== null && typeof parsed === 'object' && !Array.isArray(parsed)) {
31
+ return parsed;
32
+ }
33
+ }
34
+ catch {
35
+ // Missing or unparsable — start from scratch.
36
+ }
37
+ return {};
38
+ }
19
39
  /**
20
40
  * @param token Gateway token from `megabrain login`.
21
41
  * @param modelIds Full catalog to register in OpenCode's model picker; the
@@ -28,9 +48,21 @@ export function writeOpenCodeConfig(token, modelIds = []) {
28
48
  for (const id of modelIds) {
29
49
  models[id] = { name: id };
30
50
  }
51
+ const existing = readExistingConfig();
52
+ const existingProvider = existing.provider !== null && typeof existing.provider === 'object'
53
+ ? existing.provider
54
+ : {};
55
+ // Keep a user-chosen default model, but migrate the stale kilo-auto default
56
+ // that older CLI versions wrote.
57
+ const existingModel = typeof existing.model === 'string' ? existing.model : undefined;
58
+ const model = existingModel === undefined || existingModel.startsWith(LEGACY_MODEL_PREFIX)
59
+ ? `${PROVIDER_ID}/${DEFAULT_MODEL_ID}`
60
+ : existingModel;
31
61
  const config = {
62
+ ...existing,
32
63
  $schema: 'https://opencode.ai/config.json',
33
64
  provider: {
65
+ ...existingProvider,
34
66
  [PROVIDER_ID]: {
35
67
  npm: '@ai-sdk/openai-compatible',
36
68
  name: 'MegaBrain Gateway',
@@ -38,7 +70,15 @@ export function writeOpenCodeConfig(token, modelIds = []) {
38
70
  models,
39
71
  },
40
72
  },
41
- model: `${PROVIDER_ID}/${DEFAULT_MODEL_ID}`,
73
+ model,
74
+ // LSP diagnostics make the agent's edits far more reliable, but OpenCode
75
+ // ships with LSP off. Default it on; a user's explicit choice survives.
76
+ lsp: existing.lsp ?? true,
77
+ // OpenCode auto-connects a direct OpenRouter provider when it finds
78
+ // OPENROUTER_API_KEY or stored credentials, routing traffic (and billing)
79
+ // around the MegaBrain Gateway. Hide it by default — BYOK goes through
80
+ // the gateway. A user who wants it back can set disabled_providers: [].
81
+ disabled_providers: existing.disabled_providers ?? ['openrouter'],
42
82
  };
43
83
  fs.mkdirSync(OPENCODE_CONFIG_DIR, { recursive: true });
44
84
  fs.writeFileSync(OPENCODE_CONFIG_FILE, JSON.stringify(config, null, 2), { mode: 0o600 });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@getmegabrain/cli",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "description": "MegaBrain CLI — sign in once, then code from your terminal via OpenCode wired to the MegaBrain Gateway.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -10,6 +10,9 @@
10
10
  "files": [
11
11
  "dist"
12
12
  ],
13
+ "publishConfig": {
14
+ "access": "public"
15
+ },
13
16
  "engines": {
14
17
  "node": ">=18"
15
18
  },