@yanzc/claude-pro 0.9.8 → 0.11.0
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/README.md +69 -21
- package/package.json +5 -2
- package/src/agent.js +12 -3
- package/src/api.js +85 -43
- package/src/browser/session.js +11 -1
- package/src/commands.js +390 -84
- package/src/config.js +139 -17
- package/src/gateway.js +27 -71
- package/src/main.js +10 -10
- package/src/openai-chat.js +379 -0
- package/src/peers.js +23 -6
- package/src/providers.js +331 -0
- package/src/remote.js +53 -12
- package/src/tools/bash.js +2 -8
- package/src/tools/peers.js +6 -5
- package/src/ui/dialogs.js +77 -1
- package/src/ui/repl.js +122 -38
- package/src/utils/shell.js +4 -5
package/src/config.js
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
// 5. process environment (advanced keys only)
|
|
8
8
|
// 6. command line flags (--model, --effort ...)
|
|
9
9
|
// The model and effort chosen with /model and /effort are remembered apart from these (see
|
|
10
|
-
// rememberChoice) and win over 1-5. The gateway key comes from /
|
|
10
|
+
// rememberChoice) and win over 1-5. The gateway key comes from /connect alone (gateway.json, see
|
|
11
11
|
// gatewayLogin): there is no variable for it, and no .env file is read.
|
|
12
12
|
|
|
13
13
|
import fs from 'node:fs';
|
|
@@ -15,6 +15,7 @@ import path from 'node:path';
|
|
|
15
15
|
import os from 'node:os';
|
|
16
16
|
import { fileURLToPath } from 'node:url';
|
|
17
17
|
import { normalizeTtl } from './cache.js';
|
|
18
|
+
import { GATEWAY_PROVIDER, splitModelRef, providerModel, providerInfo, normalizeBaseUrl, connectedProviders, readConnections, knownModel } from './providers.js';
|
|
18
19
|
|
|
19
20
|
export const INSTALL_DIR = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
|
|
20
21
|
// A source checkout (a git clone, also when npm links to one), as opposed to a copy installed from npm: the agent
|
|
@@ -22,13 +23,37 @@ export const INSTALL_DIR = path.resolve(path.dirname(fileURLToPath(import.meta.u
|
|
|
22
23
|
export const SOURCE_CHECKOUT = fs.existsSync(path.join(INSTALL_DIR, '.git'));
|
|
23
24
|
export const CONFIG_DIR = process.env.CLAUDE_PRO_CONFIG_DIR || path.join(os.homedir(), '.claude-pro');
|
|
24
25
|
export const APP_NAME = 'claude-pro';
|
|
25
|
-
export const GATEWAY_DEFAULT_URL = 'https://
|
|
26
|
+
export const GATEWAY_DEFAULT_URL = 'https://7bit.fun';
|
|
27
|
+
// The gateway's earlier address (until 2026-11-19): a login or a setting saved with it means the same gateway, which
|
|
28
|
+
// answers there too, so it is read as the new one and no computer has to log in again.
|
|
29
|
+
const OLD_GATEWAY_URLS = new Set(['https://gw.predix.icu', 'http://gw.predix.icu']);
|
|
30
|
+
export function normalizeGatewayUrl(url) {
|
|
31
|
+
const base = String(url || GATEWAY_DEFAULT_URL).trim().replace(/\/+$/, '');
|
|
32
|
+
return OLD_GATEWAY_URLS.has(base.toLowerCase()) ? GATEWAY_DEFAULT_URL : base;
|
|
33
|
+
}
|
|
26
34
|
export const VERSION = JSON.parse(fs.readFileSync(path.join(INSTALL_DIR, 'package.json'), 'utf8')).version;
|
|
27
35
|
|
|
36
|
+
// The gateway key /connect saved (gateway.json): {url, apiKey, account, savedAt} for this gateway, or null. Read again
|
|
37
|
+
// at most once a second (the model is resolved at every frame of the footer), and at once after this process changed
|
|
38
|
+
// the file (forgetGatewayLogin). gateway.js's gatewayLogin is this.
|
|
39
|
+
let gatewayMemo = null; // {at, data}
|
|
40
|
+
export function readGatewayLogin(url) {
|
|
41
|
+
if (!gatewayMemo || Date.now() - gatewayMemo.at >= 1000) {
|
|
42
|
+
let data = null;
|
|
43
|
+
try { data = JSON.parse(fs.readFileSync(path.join(process.env.CLAUDE_PRO_CONFIG_DIR || CONFIG_DIR, 'gateway.json'), 'utf8')); } catch { /* none */ }
|
|
44
|
+
gatewayMemo = { at: Date.now(), data };
|
|
45
|
+
}
|
|
46
|
+
const data = gatewayMemo.data;
|
|
47
|
+
if (!data?.apiKey || (url && normalizeGatewayUrl(data.url) !== normalizeGatewayUrl(url))) return null;
|
|
48
|
+
return { ...data, url: normalizeGatewayUrl(data.url) };
|
|
49
|
+
}
|
|
50
|
+
export function forgetGatewayLogin() { gatewayMemo = null; }
|
|
51
|
+
|
|
28
52
|
// The ordinary settings /config shows. `legacy`: the old spelling accepted in settings.json.env and by config
|
|
29
53
|
// commands, but no longer read from the process environment. Writes use only the ordinary spelling.
|
|
30
54
|
export const BASIC_SETTINGS = {
|
|
31
|
-
|
|
55
|
+
// (unset: see Config.defaultModelSpec, which follows the providers connected)
|
|
56
|
+
model: { default: 'opus', legacy: 'CLAUDE_PRO_GATEWAY_MODEL', description: 'Default model, provider/model (unset: opus where Claude is connected, else a model of the first connected provider); /model and --model override it' },
|
|
32
57
|
effort: { default: 'auto', legacy: 'CLAUDE_CODE_EFFORT_LEVEL', description: 'Reasoning effort; /effort and --effort override it' },
|
|
33
58
|
promptCacheTtl: { default: '1h', legacy: 'CLAUDE_CODE_PROMPT_CACHE_TTL', description: 'Conversation prompt cache: 5m or 1h' },
|
|
34
59
|
subagentPromptCacheTtl: { default: '5m', legacy: 'CLAUDE_CODE_SUBAGENT_PROMPT_CACHE_TTL', description: 'Sub-agent, compaction and side-query prompt cache: 5m or 1h' },
|
|
@@ -40,7 +65,7 @@ export const BASIC_SETTINGS = {
|
|
|
40
65
|
// Advanced keys, shown only by /config advanced (or config list --advanced). Settings files may also carry
|
|
41
66
|
// unrelated env entries; those are left alone, but are not advertised as configuration of this program.
|
|
42
67
|
export const KNOWN_KEYS = {
|
|
43
|
-
CLAUDE_PRO_GATEWAY_URL: `
|
|
68
|
+
CLAUDE_PRO_GATEWAY_URL: `Gateway address (default ${GATEWAY_DEFAULT_URL})`,
|
|
44
69
|
CLAUDE_PRO_ONLINE: 'Set to 0 to keep interactive sessions off the gateway. Otherwise a session is online on the gateway\'s web page whenever this computer is logged in, and sessions on your other computers can send it tasks (@computer/name)',
|
|
45
70
|
CLAUDE_PRO_REMOTE_COMMANDS: 'Set to 0 to make this computer\'s sessions read-only on the gateway\'s web page: it can watch them, but not send them messages, answer their questions, stop them, switch model or effort, or change their directory (default: on; read when a session goes online)',
|
|
46
71
|
CLAUDE_PRO_MACHINE_NAME: 'This computer\'s name for sessions on your other computers (@公司 ...; default: the host name)',
|
|
@@ -194,14 +219,44 @@ const int = (v, d) => {
|
|
|
194
219
|
const isOn = (v) => /^(1|true|yes)$/i.test(String(v ?? '').trim());
|
|
195
220
|
|
|
196
221
|
// The input window of each model a gateway serves (max_input_tokens in its GET /v1/models, which fetchGatewayModels
|
|
197
|
-
// passes on here), by gateway and model id: what resolveModel takes
|
|
222
|
+
// passes on here), by gateway and model id: what resolveModel takes. Kept on disk too (cache/gateway-models.json, the
|
|
223
|
+
// last list of each gateway), so that a new process has them before its first list arrives, and while the gateway
|
|
224
|
+
// does not answer: until then a model the gateway serves with 1M showed 200k (2026-11-20).
|
|
198
225
|
const servedWindows = new Map();
|
|
226
|
+
const servedFile = () => path.join(CONFIG_DIR, 'cache', 'gateway-models.json');
|
|
227
|
+
let servedOnDisk = null; // {url: {id: tokens}}, read once
|
|
228
|
+
function readServed() {
|
|
229
|
+
if (servedOnDisk) return servedOnDisk;
|
|
230
|
+
try { servedOnDisk = JSON.parse(fs.readFileSync(servedFile(), 'utf8')); } catch { /* none kept yet */ }
|
|
231
|
+
if (!servedOnDisk || typeof servedOnDisk !== 'object') servedOnDisk = {};
|
|
232
|
+
for (const [url, models] of Object.entries(servedOnDisk)) {
|
|
233
|
+
for (const [id, tokens] of Object.entries(models || {})) {
|
|
234
|
+
if (Number.isInteger(tokens) && tokens > 0 && !servedWindows.has(`${url}\n${id}`)) servedWindows.set(`${url}\n${id}`, tokens);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
return servedOnDisk;
|
|
238
|
+
}
|
|
199
239
|
export function noteServedModels(url, models) {
|
|
240
|
+
const kept = readServed();
|
|
241
|
+
const windows = {};
|
|
200
242
|
for (const m of models || []) {
|
|
201
243
|
const tokens = Number(m?.max_input_tokens);
|
|
202
|
-
if (typeof m?.id === 'string' && Number.isInteger(tokens) && tokens > 0)
|
|
244
|
+
if (typeof m?.id === 'string' && Number.isInteger(tokens) && tokens > 0) {
|
|
245
|
+
servedWindows.set(`${url}\n${m.id}`, tokens);
|
|
246
|
+
windows[m.id] = tokens;
|
|
247
|
+
}
|
|
203
248
|
}
|
|
249
|
+
// (written only when the list changed: it comes again every 10 minutes)
|
|
250
|
+
if (!Object.keys(windows).length || JSON.stringify(kept[url]) === JSON.stringify(windows)) return;
|
|
251
|
+
kept[url] = windows;
|
|
252
|
+
try {
|
|
253
|
+
fs.mkdirSync(path.dirname(servedFile()), { recursive: true });
|
|
254
|
+
const tmp = `${servedFile()}.${process.pid}.tmp`;
|
|
255
|
+
fs.writeFileSync(tmp, JSON.stringify(kept));
|
|
256
|
+
fs.renameSync(tmp, servedFile());
|
|
257
|
+
} catch { /* kept in memory only */ }
|
|
204
258
|
}
|
|
259
|
+
const servedWindow = (url, id) => { readServed(); return servedWindows.get(`${url}\n${id}`); };
|
|
205
260
|
|
|
206
261
|
export class Config {
|
|
207
262
|
constructor(env, sources, settings, cwd) {
|
|
@@ -216,30 +271,96 @@ export class Config {
|
|
|
216
271
|
this.verifyOverride = null; // set by /verify or --verify during a session
|
|
217
272
|
}
|
|
218
273
|
|
|
219
|
-
get gatewayUrl() { return (this.env.CLAUDE_PRO_GATEWAY_URL
|
|
220
|
-
|
|
274
|
+
get gatewayUrl() { return normalizeGatewayUrl(this.env.CLAUDE_PRO_GATEWAY_URL); }
|
|
275
|
+
// The gateway is one provider among the others (7bit in model names), connected when its key is saved.
|
|
276
|
+
get gatewayConnected() { return Boolean(readGatewayLogin(this.gatewayUrl)); }
|
|
277
|
+
providerConnected(id) { return id === GATEWAY_PROVIDER ? this.gatewayConnected : connectedProviders().some((p) => p.id === id); }
|
|
278
|
+
providerName(id) { return id === GATEWAY_PROVIDER ? '7bit.fun' : providerInfo(id)?.name || id; }
|
|
279
|
+
// Whether any provider is connected at all.
|
|
280
|
+
get anyProviderConnected() { return this.gatewayConnected || connectedProviders().length > 0; }
|
|
281
|
+
|
|
282
|
+
// Where the current model's requests go, for /status and the welcome box.
|
|
283
|
+
get endpointLabel() {
|
|
284
|
+
const provider = this.model.provider;
|
|
285
|
+
const connected = this.providerConnected(provider) ? '' : ', not connected: /connect';
|
|
286
|
+
if (provider === GATEWAY_PROVIDER) return `${this.gatewayUrl} (7bit.fun${connected})`;
|
|
287
|
+
const info = providerInfo(provider);
|
|
288
|
+
if (!info) return `${provider} (no such provider: /connect lists them)`;
|
|
289
|
+
return `${normalizeBaseUrl(info.connection?.baseUrl || info.baseUrl, info.protocol)} (${info.name}${connected})`;
|
|
290
|
+
}
|
|
221
291
|
|
|
222
|
-
|
|
223
|
-
|
|
292
|
+
// The connected provider the Claude aliases (opus, sonnet, haiku) go to: the first of 7bit.fun and Anthropic that is
|
|
293
|
+
// connected, in /connect's order; null with neither.
|
|
294
|
+
claudeProvider() {
|
|
295
|
+
if (this.gatewayConnected) return GATEWAY_PROVIDER;
|
|
296
|
+
if (connectedProviders().some((p) => p.id === 'anthropic')) return 'anthropic';
|
|
297
|
+
return null;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
// What an alias names: the model of the provider that serves Claude here (Anthropic's newest of the family in the
|
|
301
|
+
// list it gave, else the same id); 7bit.fun's when it is that provider, when neither is connected (the models
|
|
302
|
+
// then cannot be reached: /connect is said), and with `gateway` (7bit/opus).
|
|
303
|
+
aliasTarget(alias, { gateway = false } = {}) {
|
|
304
|
+
const id = { opus: 'claude-opus-5-5', sonnet: 'claude-sonnet-5-5', haiku: 'claude-haiku-5-5' }[alias];
|
|
305
|
+
if (!id) return null;
|
|
306
|
+
if (!gateway && this.claudeProvider() === 'anthropic') {
|
|
307
|
+
const listed = readConnections().anthropic?.models?.find((m) => m.id.includes(alias));
|
|
308
|
+
return `anthropic/${listed?.id || id}`;
|
|
309
|
+
}
|
|
310
|
+
return `${GATEWAY_PROVIDER}/${id}`;
|
|
224
311
|
}
|
|
225
312
|
|
|
226
|
-
//
|
|
227
|
-
//
|
|
313
|
+
// The model when the settings name none: opus where a provider of Claude is connected, else the model chosen when the
|
|
314
|
+
// first connected provider was connected (or the first it listed); opus when nothing is connected.
|
|
315
|
+
defaultModelSpec() {
|
|
316
|
+
if (this.claudeProvider()) return 'opus';
|
|
317
|
+
for (const p of connectedProviders()) {
|
|
318
|
+
const model = p.connection.model || p.connection.models?.[0]?.id;
|
|
319
|
+
if (model) return `${p.id}/${model}`;
|
|
320
|
+
}
|
|
321
|
+
return 'opus';
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
// Resolve a model spec (alias or name, maybe with [1m]) to {id, provider, contextWindow, label}. The window is the
|
|
325
|
+
// model's max_input_tokens from the gateway's model list, once loaded, else what is known of it here; [1m] asks for
|
|
326
|
+
// 1M. A provider's model (deepseek/deepseek-v4-pro, see providers.js) is sent as its own id to its provider, with
|
|
327
|
+
// the window, output limit and price it was listed with at /connect.
|
|
228
328
|
resolveModel(spec) {
|
|
229
329
|
let name = (spec || '').trim() || 'sonnet';
|
|
230
330
|
let oneM = false;
|
|
231
331
|
if (/\[1m\]$/i.test(name)) { oneM = true; name = name.replace(/\[1m\]$/i, ''); }
|
|
232
|
-
const
|
|
332
|
+
const ref = splitModelRef(name);
|
|
333
|
+
if (ref.provider !== GATEWAY_PROVIDER) {
|
|
334
|
+
const listed = providerModel(ref.provider, ref.model);
|
|
335
|
+
// (a list without windows, as DeepSeek's: what is known of the model here)
|
|
336
|
+
const known = { ...knownModel(ref.model), ...Object.fromEntries(Object.entries(listed || {}).filter(([, v]) => v != null)) };
|
|
337
|
+
const claudeOneM = /^claude-(?:(?:sonnet|haiku)-5(?:-|$)|opus-(?:5(?:-|$)|4-[78](?:-|$)))/i.test(ref.model);
|
|
338
|
+
return {
|
|
339
|
+
id: ref.model,
|
|
340
|
+
provider: ref.provider,
|
|
341
|
+
contextWindow: oneM ? 1_000_000 : known.window || (claudeOneM ? 1_000_000 : 200_000),
|
|
342
|
+
label: `${ref.provider}/${ref.model}${oneM ? '[1m]' : ''}`,
|
|
343
|
+
...(known.maxOutput ? { maxOutput: known.maxOutput } : {}),
|
|
344
|
+
...(known.price ? { price: known.price } : {}),
|
|
345
|
+
};
|
|
346
|
+
}
|
|
347
|
+
// A name without a provider is 7bit.fun's (as before there were others), and an alias goes where Claude is (see
|
|
348
|
+
// aliasTarget); 7bit/<name> is 7bit.fun's in any case.
|
|
349
|
+
const explicit = /^7bit\//i.test(name);
|
|
350
|
+
name = ref.model;
|
|
351
|
+
const aliased = this.aliasTarget(name.toLowerCase(), { gateway: explicit });
|
|
233
352
|
if (aliased) {
|
|
234
353
|
const inner = this.resolveModel(aliased);
|
|
235
|
-
return { ...inner, contextWindow: oneM ? 1_000_000 : inner.contextWindow, label: `${name}${oneM ? '[1m]' : ''} (${inner.label})` };
|
|
354
|
+
return { ...inner, contextWindow: oneM ? 1_000_000 : inner.contextWindow, label: `${explicit ? '7bit/' : ''}${name}${oneM ? '[1m]' : ''} (${inner.label})` };
|
|
236
355
|
}
|
|
237
|
-
|
|
356
|
+
// (the gateway's list, else the last one kept, else what is known of the model here)
|
|
357
|
+
const served = servedWindow(this.gatewayUrl, name) || knownModel(name)?.window;
|
|
358
|
+
const label = `${GATEWAY_PROVIDER}/${name}${oneM ? '[1m]' : ''}`;
|
|
238
359
|
// GPT models are served by the gateway's Codex backend, whose standard window is 272k. A request above 272k costs
|
|
239
360
|
// twice as much (see pricing.js), but the user wants the gateway's window here too (2026-10-27).
|
|
240
|
-
if (/^gpt-/i.test(name)) return { id: name, contextWindow: served || 272_000, label
|
|
361
|
+
if (/^gpt-/i.test(name)) return { id: name, provider: GATEWAY_PROVIDER, contextWindow: served || 272_000, label };
|
|
241
362
|
const nativeOneM = /^claude-(?:(?:sonnet|haiku)-5(?:-|$)|opus-(?:5(?:-|$)|4-[78](?:-|$)))/i.test(name);
|
|
242
|
-
return { id: name, contextWindow: oneM ? 1_000_000 : served || (nativeOneM ? 1_000_000 : 200_000), label
|
|
363
|
+
return { id: name, provider: GATEWAY_PROVIDER, contextWindow: oneM ? 1_000_000 : served || (nativeOneM ? 1_000_000 : 200_000), label };
|
|
243
364
|
}
|
|
244
365
|
|
|
245
366
|
// The selected model: the configured default unless /model has overridden it for this session.
|
|
@@ -248,6 +369,7 @@ export class Config {
|
|
|
248
369
|
|
|
249
370
|
setting(key) {
|
|
250
371
|
const spec = BASIC_SETTINGS[key];
|
|
372
|
+
if (key === 'model' && this.settings.model == null) return this.defaultModelSpec();
|
|
251
373
|
return spec ? this.settings[key] ?? spec.default : this.env[key];
|
|
252
374
|
}
|
|
253
375
|
get modelSpec() { return this.modelOverride || this.configuredModelSpec; }
|
package/src/gateway.js
CHANGED
|
@@ -1,60 +1,40 @@
|
|
|
1
|
-
// The
|
|
2
|
-
// API key of the user's gateway account, which pays for them from its
|
|
3
|
-
//
|
|
4
|
-
// (
|
|
1
|
+
// The 7bit.fun provider (the gateway, 7bit in model names): one of the providers, whose model requests go to a shared
|
|
2
|
+
// gateway (https://7bit.fun by default) with an API key of the user's gateway account, which pays for them from its
|
|
3
|
+
// balance. The user makes the key on the gateway's web page
|
|
4
|
+
// (its user page, "API 密钥") and pastes it into /connect (7bit.fun), which checks it (GET /v1/account) and saves it.
|
|
5
|
+
// (Until 2026-11-19 /login did, and before that it opened the gateway's page in the browser and took a key back
|
|
6
|
+
// through a local callback server.)
|
|
7
|
+
// The same key puts the sessions online at the gateway (remote.js), whichever provider the model requests go to.
|
|
5
8
|
|
|
6
|
-
import crypto from 'node:crypto';
|
|
7
9
|
import fs from 'node:fs';
|
|
8
|
-
import http from 'node:http';
|
|
9
|
-
import os from 'node:os';
|
|
10
10
|
import path from 'node:path';
|
|
11
|
-
import {
|
|
12
|
-
import { CONFIG_DIR, GATEWAY_DEFAULT_URL, noteServedModels } from './config.js';
|
|
11
|
+
import { CONFIG_DIR, GATEWAY_DEFAULT_URL, normalizeGatewayUrl, noteServedModels, readGatewayLogin, forgetGatewayLogin } from './config.js';
|
|
13
12
|
|
|
14
|
-
export { GATEWAY_DEFAULT_URL };
|
|
13
|
+
export { GATEWAY_DEFAULT_URL, normalizeGatewayUrl };
|
|
15
14
|
export const gatewayLoginFile = () => path.join(process.env.CLAUDE_PRO_CONFIG_DIR || CONFIG_DIR, 'gateway.json');
|
|
16
|
-
const LOGIN_TIMEOUT_MS = 10 * 60_000;
|
|
17
15
|
|
|
18
|
-
|
|
16
|
+
// The login /connect saved for this gateway, {url, apiKey, account, savedAt}, or null when there is none. It is where
|
|
17
|
+
// the key of every gateway request comes from: there is no environment variable or .env file for it. A login saved
|
|
18
|
+
// with the gateway's old address is this gateway's (see normalizeGatewayUrl), and comes back with the new one.
|
|
19
|
+
export const gatewayLogin = (url) => readGatewayLogin(url);
|
|
19
20
|
|
|
20
|
-
//
|
|
21
|
-
|
|
22
|
-
export function gatewayLogin(url) {
|
|
23
|
-
try {
|
|
24
|
-
const data = JSON.parse(fs.readFileSync(gatewayLoginFile(), 'utf8'));
|
|
25
|
-
if (!data?.apiKey || (url && normalizeGatewayUrl(data.url) !== normalizeGatewayUrl(url))) return null;
|
|
26
|
-
return data;
|
|
27
|
-
} catch {
|
|
28
|
-
return null;
|
|
29
|
-
}
|
|
30
|
-
}
|
|
21
|
+
// Where the gateway's users make their keys.
|
|
22
|
+
export const gatewayKeyPage = (url) => `${normalizeGatewayUrl(url)}/user#keys`;
|
|
31
23
|
|
|
32
24
|
export function saveGatewayLogin(login) {
|
|
33
25
|
const file = gatewayLoginFile();
|
|
34
26
|
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
35
27
|
const tmp = `${file}.${process.pid}.tmp`;
|
|
28
|
+
forgetGatewayLogin();
|
|
36
29
|
fs.writeFileSync(tmp, JSON.stringify({ ...login, url: normalizeGatewayUrl(login.url), savedAt: new Date().toISOString() }, null, 2), { mode: 0o600 });
|
|
37
30
|
fs.renameSync(tmp, file);
|
|
38
31
|
}
|
|
39
32
|
|
|
40
33
|
export function clearGatewayLogin() {
|
|
34
|
+
forgetGatewayLogin();
|
|
41
35
|
try { fs.unlinkSync(gatewayLoginFile()); return true; } catch { return false; }
|
|
42
36
|
}
|
|
43
37
|
|
|
44
|
-
export function openBrowser(url) {
|
|
45
|
-
try {
|
|
46
|
-
const [cmd, args] = process.platform === 'win32'
|
|
47
|
-
? ['rundll32', ['url.dll,FileProtocolHandler', url]]
|
|
48
|
-
: process.platform === 'darwin' ? ['open', [url]] : ['xdg-open', [url]];
|
|
49
|
-
const child = spawn(cmd, args, { stdio: 'ignore', detached: true, windowsHide: true });
|
|
50
|
-
child.on('error', () => {});
|
|
51
|
-
child.unref();
|
|
52
|
-
return true;
|
|
53
|
-
} catch {
|
|
54
|
-
return false;
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
38
|
async function gatewayJson(url, init = {}, fetchImpl = globalThis.fetch) {
|
|
59
39
|
const res = await fetchImpl(url, { ...init, headers: { 'content-type': 'application/json', ...(init.headers || {}) } });
|
|
60
40
|
const body = await res.json().catch(() => ({}));
|
|
@@ -165,41 +145,17 @@ export function nearestEffort(effort, supported) {
|
|
|
165
145
|
return supported.reduce((best, item) => (Math.abs(EFFORT_ORDER.indexOf(item) - want) < Math.abs(EFFORT_ORDER.indexOf(best) - want) ? item : best), supported[0]);
|
|
166
146
|
}
|
|
167
147
|
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
//
|
|
171
|
-
|
|
172
|
-
export async function loginWithBrowser({ url = GATEWAY_DEFAULT_URL, onUrl = () => {}, open = openBrowser, signal, fetchImpl = globalThis.fetch, timeoutMs = LOGIN_TIMEOUT_MS } = {}) {
|
|
148
|
+
// Log in with a key the user made on the gateway's page and pasted into /connect: checked with the account it belongs to
|
|
149
|
+
// (GET /v1/account) before it is saved. Resolves with the saved login and the account (its balance); throws {status}
|
|
150
|
+
// when the gateway refuses the key (401/403), and the gateway's error when it cannot be reached.
|
|
151
|
+
export async function loginWithKey({ url = GATEWAY_DEFAULT_URL, apiKey, fetchImpl = globalThis.fetch, signal } = {}) {
|
|
173
152
|
const base = normalizeGatewayUrl(url);
|
|
174
|
-
const
|
|
175
|
-
|
|
176
|
-
const
|
|
177
|
-
const
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
const ok = u.searchParams.get('state') === state && u.searchParams.get('code');
|
|
181
|
-
res.writeHead(ok ? 200 : 400, { 'content-type': 'text/html; charset=utf-8' });
|
|
182
|
-
res.end(ok ? PAGE('登录成功', '可以关掉这个页面,回到终端继续使用 claude-pro。') : PAGE('登录失败', '链接无效或已过期。请回到终端重新执行 /login。'));
|
|
183
|
-
if (ok) settle.resolve(u.searchParams.get('code'));
|
|
184
|
-
});
|
|
185
|
-
await new Promise((resolve, reject) => { server.once('error', reject); server.listen(0, '127.0.0.1', resolve); });
|
|
186
|
-
const timer = setTimeout(() => settle.reject(new Error('Login timed out: the browser did not come back within 10 minutes.')), timeoutMs);
|
|
187
|
-
const onAbort = () => settle.reject(Object.assign(new Error('Login cancelled'), { name: 'AbortError' }));
|
|
188
|
-
signal?.addEventListener('abort', onAbort, { once: true });
|
|
189
|
-
try {
|
|
190
|
-
const page = `${base}/cli/login?${new URLSearchParams({ port: String(server.address().port), state, client: 'claude-pro', device: os.hostname().slice(0, 60) })}`;
|
|
191
|
-
onUrl(page);
|
|
192
|
-
Promise.resolve().then(() => open(page)).catch((e) => settle.reject(e));
|
|
193
|
-
const code = await result;
|
|
194
|
-
const token = await gatewayJson(`${base}/cli/token`, { method: 'POST', body: JSON.stringify({ code, state }) }, fetchImpl);
|
|
195
|
-
const login = { url: base, apiKey: token.api_key, account: { id: token.account?.id, name: token.account?.name, email: token.account?.email } };
|
|
196
|
-
saveGatewayLogin(login);
|
|
197
|
-
return { ...login, balance: token.account };
|
|
198
|
-
} finally {
|
|
199
|
-
clearTimeout(timer);
|
|
200
|
-
signal?.removeEventListener('abort', onAbort);
|
|
201
|
-
server.close();
|
|
202
|
-
}
|
|
153
|
+
const key = String(apiKey || '').replace(/\s+/g, '');
|
|
154
|
+
if (!key) throw new Error('no key was given');
|
|
155
|
+
const account = await fetchGatewayAccount({ url: base, apiKey: key }, { fetchImpl, signal });
|
|
156
|
+
const login = { url: base, apiKey: key, account: { id: account?.id, name: account?.name, email: account?.email } };
|
|
157
|
+
saveGatewayLogin(login);
|
|
158
|
+
return { ...login, balance: account };
|
|
203
159
|
}
|
|
204
160
|
|
|
205
161
|
export const formatUsd = (value) => {
|
package/src/main.js
CHANGED
|
@@ -7,14 +7,15 @@ import { takeRestartState } from './restart.js';
|
|
|
7
7
|
|
|
8
8
|
const HELP = `Usage: claude-pro [options] [prompt]
|
|
9
9
|
|
|
10
|
-
A command line coding agent
|
|
10
|
+
A command line coding agent for many model providers: /connect connects them (Anthropic, OpenAI, DeepSeek,
|
|
11
|
+
Kimi, GLM, Qwen, OpenRouter, the 7bit.fun gateway, your own endpoint...), several at once.
|
|
11
12
|
Starts an interactive session by default; use -p for non-interactive output.
|
|
12
13
|
|
|
13
14
|
Options:
|
|
14
15
|
-p, --print Print the response and exit (useful for pipes)
|
|
15
16
|
-c, --continue Continue the most recent conversation in this directory
|
|
16
17
|
-r, --resume [sessionId] Resume a conversation (picker if no id given)
|
|
17
|
-
--model <model> Model for this session (
|
|
18
|
+
--model <model> Model for this session: provider/model (deepseek/deepseek-v4-pro), or opus, sonnet, haiku
|
|
18
19
|
--effort <level> Reasoning effort: none|minimal|low|medium|high|xhigh|max
|
|
19
20
|
--output-format <format> text | json | stream-json (with --print)
|
|
20
21
|
--verify <command> After the agent edits files, run this (e.g. "npm test") and send failures back to fix
|
|
@@ -24,23 +25,22 @@ Options:
|
|
|
24
25
|
--verbose Show detailed tool output
|
|
25
26
|
--no-mcp Do not start MCP servers
|
|
26
27
|
--name <name> Name this session, as /rename does (other sessions reach it with @name)
|
|
28
|
+
--rc, --remote-control Turn remote control on at the start, as /rc does (the gateway's web page shows the session)
|
|
27
29
|
-v, --version Print version
|
|
28
30
|
-h, --help Show help
|
|
29
31
|
|
|
30
32
|
There are no permission prompts: every tool runs immediately (full access). File edits are
|
|
31
33
|
checkpointed per prompt, and /rewind in a session undoes them. The old flags
|
|
32
34
|
--dangerously-skip-permissions, --permission-mode, --allowedTools and --disallowedTools
|
|
33
|
-
are accepted and ignored, and so
|
|
34
|
-
--remote-control (--rc): a session is online at the gateway whenever this computer is logged in to it.
|
|
35
|
+
are accepted and ignored, and so is --plan (there is no plan mode: ask for a plan in the message).
|
|
35
36
|
|
|
36
37
|
Commands:
|
|
37
38
|
claude-pro config list [--advanced] Show ordinary settings (add --advanced for optional controls)
|
|
38
39
|
claude-pro config set <KEY> <VALUE> [--project|--local] Save a setting (default: ~/.claude-pro/settings.json)
|
|
39
40
|
claude-pro config unset <KEY> [--project|--local]
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
/promt, /promits Accepted spelling-compatible aliases
|
|
41
|
+
|
|
42
|
+
In a session, /connect connects a model provider with its API key, /model picks a model of any connected one,
|
|
43
|
+
/disconnect forgets one.
|
|
44
44
|
`;
|
|
45
45
|
|
|
46
46
|
function parseArgs(argv) {
|
|
@@ -74,7 +74,7 @@ function parseArgs(argv) {
|
|
|
74
74
|
case '--system-prompt-file': flags.systemPromptFile = next(); break;
|
|
75
75
|
case '--verbose': flags.verbose = true; break;
|
|
76
76
|
case '--no-mcp': flags.noMcp = true; break;
|
|
77
|
-
case '--remote-control': case '--rc': break; //
|
|
77
|
+
case '--remote-control': case '--rc': flags.remoteControl = true; break; // as /rc at the start
|
|
78
78
|
case '--name': flags.name = next(); break;
|
|
79
79
|
case '-v': case '--version': flags.version = true; break;
|
|
80
80
|
case '-h': case '--help': flags.help = true; break;
|
|
@@ -108,7 +108,7 @@ async function configCommand(args, cwd) {
|
|
|
108
108
|
}
|
|
109
109
|
if (sub !== 'get') {
|
|
110
110
|
await loadGatewayModels(cfg);
|
|
111
|
-
console.log(`\
|
|
111
|
+
console.log(`\nbackend: ${cfg.endpointLabel}`);
|
|
112
112
|
console.log(`model: ${cfg.model.label} (context ${cfg.model.contextWindow.toLocaleString()})${cfg.effort ? `, effort ${cfg.effort}` : ''}`);
|
|
113
113
|
if (remembered.model || remembered.effort) {
|
|
114
114
|
const what = Object.entries(remembered).map(([k, v]) => `${k} ${v}`).join(', ');
|