@getmegabrain/cli 0.1.0 → 0.1.1
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.
- package/dist/constants.js +2 -2
- package/dist/index.js +15 -8
- package/dist/models.js +25 -0
- package/dist/opencode-config.js +14 -3
- package/package.json +1 -1
package/dist/constants.js
CHANGED
|
@@ -5,5 +5,5 @@
|
|
|
5
5
|
export const APP_BASE_URL = (process.env.MEGABRAIN_APP_URL ?? 'https://getmegabrain.com').replace(/\/+$/, '');
|
|
6
6
|
/** OpenAI-compatible gateway root; OpenCode's provider appends `/chat/completions`. */
|
|
7
7
|
export const GATEWAY_BASE_URL = `${APP_BASE_URL}/api/gateway/`;
|
|
8
|
-
/** Gateway model id used when the user hasn't picked one. */
|
|
9
|
-
export const DEFAULT_MODEL_ID = '
|
|
8
|
+
/** Gateway model id used when the user hasn't picked one (MegaBrain auto-router). */
|
|
9
|
+
export const DEFAULT_MODEL_ID = 'mb-auto/balanced';
|
package/dist/index.js
CHANGED
|
@@ -3,20 +3,27 @@ import { spawn } from 'node:child_process';
|
|
|
3
3
|
import { login } from './device-auth.js';
|
|
4
4
|
import { readCredentials, writeCredentials, clearCredentials } from './credentials.js';
|
|
5
5
|
import { writeOpenCodeConfig } from './opencode-config.js';
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
return;
|
|
6
|
+
import { fetchGatewayModelIds } from './models.js';
|
|
7
|
+
async function signIn() {
|
|
9
8
|
console.log('Signing in to MegaBrain…');
|
|
10
9
|
const { token, userId, userEmail } = await login();
|
|
11
10
|
writeCredentials({ token, userId, userEmail });
|
|
12
|
-
writeOpenCodeConfig(token);
|
|
11
|
+
writeOpenCodeConfig(token, await fetchGatewayModelIds());
|
|
12
|
+
return userEmail;
|
|
13
|
+
}
|
|
14
|
+
async function ensureSignedIn() {
|
|
15
|
+
const credentials = readCredentials();
|
|
16
|
+
if (credentials) {
|
|
17
|
+
// Refresh OpenCode's config on every launch so CLI upgrades (new default
|
|
18
|
+
// model, catalog changes) take effect without an explicit re-login.
|
|
19
|
+
writeOpenCodeConfig(credentials.token, await fetchGatewayModelIds());
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
const userEmail = await signIn();
|
|
13
23
|
console.log(`\nSigned in as ${userEmail}.\n`);
|
|
14
24
|
}
|
|
15
25
|
async function runLogin() {
|
|
16
|
-
|
|
17
|
-
const { token, userId, userEmail } = await login();
|
|
18
|
-
writeCredentials({ token, userId, userEmail });
|
|
19
|
-
writeOpenCodeConfig(token);
|
|
26
|
+
const userEmail = await signIn();
|
|
20
27
|
console.log(`\nSigned in as ${userEmail}. Run \`megabrain\` to start coding.\n`);
|
|
21
28
|
}
|
|
22
29
|
function runLogout() {
|
package/dist/models.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { APP_BASE_URL } from './constants.js';
|
|
2
|
+
/**
|
|
3
|
+
* Fetches the MegaBrain Gateway's public model catalog so OpenCode's model
|
|
4
|
+
* picker can list every model the gateway serves (including BYOK-eligible
|
|
5
|
+
* ones — BYOK routing is transparent gateway-side, the ids are the same).
|
|
6
|
+
*
|
|
7
|
+
* Best-effort: returns [] on any failure so login never blocks on this —
|
|
8
|
+
* the caller falls back to registering just the default auto-router model.
|
|
9
|
+
*/
|
|
10
|
+
export async function fetchGatewayModelIds() {
|
|
11
|
+
try {
|
|
12
|
+
const res = await fetch(`${APP_BASE_URL}/api/gateway/models`);
|
|
13
|
+
if (!res.ok)
|
|
14
|
+
return [];
|
|
15
|
+
const body = (await res.json());
|
|
16
|
+
if (!Array.isArray(body.data))
|
|
17
|
+
return [];
|
|
18
|
+
return body.data
|
|
19
|
+
.map(model => model.id)
|
|
20
|
+
.filter((id) => typeof id === 'string' && id.length > 0);
|
|
21
|
+
}
|
|
22
|
+
catch {
|
|
23
|
+
return [];
|
|
24
|
+
}
|
|
25
|
+
}
|
package/dist/opencode-config.js
CHANGED
|
@@ -16,7 +16,18 @@ const PROVIDER_ID = 'megabrain';
|
|
|
16
16
|
function toOpenCodeBaseUrl(gatewayBaseUrl) {
|
|
17
17
|
return gatewayBaseUrl.replace(/\/+$/, '') + '/v1';
|
|
18
18
|
}
|
|
19
|
-
|
|
19
|
+
/**
|
|
20
|
+
* @param token Gateway token from `megabrain login`.
|
|
21
|
+
* @param modelIds Full catalog to register in OpenCode's model picker; the
|
|
22
|
+
* default auto-router model is always included and stays the default.
|
|
23
|
+
*/
|
|
24
|
+
export function writeOpenCodeConfig(token, modelIds = []) {
|
|
25
|
+
const models = {
|
|
26
|
+
[DEFAULT_MODEL_ID]: { name: DEFAULT_MODEL_ID },
|
|
27
|
+
};
|
|
28
|
+
for (const id of modelIds) {
|
|
29
|
+
models[id] = { name: id };
|
|
30
|
+
}
|
|
20
31
|
const config = {
|
|
21
32
|
$schema: 'https://opencode.ai/config.json',
|
|
22
33
|
provider: {
|
|
@@ -24,10 +35,10 @@ export function writeOpenCodeConfig(token, modelId = DEFAULT_MODEL_ID) {
|
|
|
24
35
|
npm: '@ai-sdk/openai-compatible',
|
|
25
36
|
name: 'MegaBrain Gateway',
|
|
26
37
|
options: { baseURL: toOpenCodeBaseUrl(GATEWAY_BASE_URL), apiKey: token },
|
|
27
|
-
models
|
|
38
|
+
models,
|
|
28
39
|
},
|
|
29
40
|
},
|
|
30
|
-
model: `${PROVIDER_ID}/${
|
|
41
|
+
model: `${PROVIDER_ID}/${DEFAULT_MODEL_ID}`,
|
|
31
42
|
};
|
|
32
43
|
fs.mkdirSync(OPENCODE_CONFIG_DIR, { recursive: true });
|
|
33
44
|
fs.writeFileSync(OPENCODE_CONFIG_FILE, JSON.stringify(config, null, 2), { mode: 0o600 });
|
package/package.json
CHANGED