labs-claude 0.2.6 ā 0.3.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 +141 -122
- package/bin/labs.js +245 -178
- package/package.json +34 -34
- package/src/auto-fallback.js +95 -0
- package/src/autostart.js +43 -43
- package/src/bedrock.js +162 -162
- package/src/browser-setup.js +38 -38
- package/src/chat/index.html +534 -534
- package/src/chat-routes.js +149 -149
- package/src/config.js +71 -63
- package/src/gateway-client.js +124 -0
- package/src/local-auth.js +33 -33
- package/src/proxy-manager.js +85 -82
- package/src/proxy.js +801 -801
- package/src/read-shunt.js +26 -26
- package/src/setup.js +1 -1
- package/src/subscription-failure.js +36 -0
- package/src/usage.js +44 -44
package/bin/labs.js
CHANGED
|
@@ -1,178 +1,245 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
const axios = require('axios');
|
|
3
|
-
const { spawn } = require('child_process');
|
|
4
|
-
const { randomUUID } = require('crypto');
|
|
5
|
-
const { CONFIG_PATH, LABS_DIR, loadConfig, loadLlmList } = require('../src/config');
|
|
6
|
-
const { loadProxyKey } = require('../src/local-auth');
|
|
7
|
-
const { ensureProxy, keepProxyAlive, stopProxy } = require('../src/proxy-manager');
|
|
8
|
-
const { parseLaunchArgs, withLabsHeaders, chromeCompatibilityWarning } = require('../src/launch-args');
|
|
9
|
-
const { runSetup, setupBedrock } = require('../src/setup');
|
|
10
|
-
const {
|
|
11
|
-
const {
|
|
12
|
-
const {
|
|
13
|
-
const {
|
|
14
|
-
|
|
15
|
-
const
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
labs
|
|
20
|
-
labs
|
|
21
|
-
labs
|
|
22
|
-
labs
|
|
23
|
-
labs
|
|
24
|
-
labs
|
|
25
|
-
labs
|
|
26
|
-
labs
|
|
27
|
-
labs
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
console.log(
|
|
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
|
-
const
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
}
|
|
94
|
-
child.on('
|
|
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
|
-
if (
|
|
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
|
-
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
const axios = require('axios');
|
|
3
|
+
const { spawn } = require('child_process');
|
|
4
|
+
const { randomUUID } = require('crypto');
|
|
5
|
+
const { CONFIG_PATH, LABS_DIR, loadConfig, loadLlmList } = require('../src/config');
|
|
6
|
+
const { loadProxyKey } = require('../src/local-auth');
|
|
7
|
+
const { ensureProxy, keepProxyAlive, stopProxy } = require('../src/proxy-manager');
|
|
8
|
+
const { parseLaunchArgs, withLabsHeaders, chromeCompatibilityWarning } = require('../src/launch-args');
|
|
9
|
+
const { runSetup, setupBedrock, ask, askHidden } = require('../src/setup');
|
|
10
|
+
const { gatewayModelFrom, prepareGatewayLaunch, saveGatewayKey, makeAdminToken, openConsole, storedConfig } = require('../src/gateway-client');
|
|
11
|
+
const { bedrockModelId, readBedrockKey } = require('../src/bedrock');
|
|
12
|
+
const { printUsage } = require('../src/usage');
|
|
13
|
+
const { setAutostart } = require('../src/autostart');
|
|
14
|
+
const { planSubscriptionLeg, planLabsLegArgs, createWatcher } = require('../src/auto-fallback');
|
|
15
|
+
const { version } = require('../package.json');
|
|
16
|
+
|
|
17
|
+
const HELP = `labs ${version} ā Claude Code running on ElevenLabs models
|
|
18
|
+
|
|
19
|
+
labs open Claude Code on the default model (astra)
|
|
20
|
+
labs --model opus pick a model by name, number (labs --model 5) or raw ElevenLabs id
|
|
21
|
+
labs -p "question" any Claude Code flag works, e.g. -p, --resume, --permission-mode
|
|
22
|
+
labs auto [claude args] real Claude Code on your subscription; on a limit or expired login, same session continues on labs Opus 5.5
|
|
23
|
+
labs models [--all] list models
|
|
24
|
+
labs usage credits left and what recent sessions cost per model
|
|
25
|
+
labs setup set your ElevenLabs key, the bridge agent and real-Chrome control
|
|
26
|
+
labs bedrock save your Amazon Bedrock key, then: labs --model glm (GLM 5.3)
|
|
27
|
+
labs --model qwen Claude Code on our GPU box (qwen | qwen-uncensored), through the labs gateway
|
|
28
|
+
labs key save the labs key you were given for the box
|
|
29
|
+
labs console open the gateway console (keys, usage, $); admin only
|
|
30
|
+
labs admin-token <ssh> make a new console admin token on the gateway server
|
|
31
|
+
labs chat open the private chat page (http://127.0.0.1:8081/chat)
|
|
32
|
+
labs serve start the background proxy if it is not running, then exit
|
|
33
|
+
labs autostart on|off keep the proxy (and the chat page) running from login (Windows)
|
|
34
|
+
labs stop stop the background proxy
|
|
35
|
+
|
|
36
|
+
Inside Claude Code: /model <name> switches model, e.g. /model gpt-5.6-terra
|
|
37
|
+
Browser: "open gmail.com and read my top email" uses your real Chrome (set up with labs setup)
|
|
38
|
+
Update: npm install -g labs-claude@latest --prefer-online, then close and reopen every labs window`;
|
|
39
|
+
|
|
40
|
+
function printModels(config, showAll) {
|
|
41
|
+
const llms = loadLlmList();
|
|
42
|
+
const byId = Object.fromEntries(llms.map(item => [item.llm, item]));
|
|
43
|
+
const size = (n) => n >= 1000000 ? `${Math.round(n / 100000) / 10}M` : `${Math.round(n / 1000)}k`;
|
|
44
|
+
console.log('\nš Labs models (labs --model <name or number>)\n');
|
|
45
|
+
Object.entries(config.models).forEach(([name, llm], index) => {
|
|
46
|
+
const info = byId[llm];
|
|
47
|
+
const detail = bedrockModelId(llm)
|
|
48
|
+
? `Amazon Bedrock, ${size(config.bedrockContextLimit).padStart(5)} context 128k reply`
|
|
49
|
+
: info ? `${size(info.max_context_limit).padStart(5)} context ${size(info.max_tokens_limit).padStart(5)} reply` : 'not offered on your account';
|
|
50
|
+
const marker = name === config.defaultModel ? ' ā default' : '';
|
|
51
|
+
console.log(`${String(index + 1).padStart(3)}. ${name.padEnd(11)} ${llm.padEnd(24)} ${detail}${marker}`);
|
|
52
|
+
});
|
|
53
|
+
const current = llms.filter(item => !item.deprecation_info && !item.is_checkpoint);
|
|
54
|
+
if (showAll) {
|
|
55
|
+
console.log(`\nAll ${current.length} current ElevenLabs LLMs (use the id with --model):\n`);
|
|
56
|
+
current.forEach(item => console.log(` ${item.llm.padEnd(32)} ${size(item.max_context_limit).padStart(5)} context ${size(item.max_tokens_limit).padStart(5)} reply`));
|
|
57
|
+
} else {
|
|
58
|
+
console.log(`\n${current.length} current ElevenLabs LLMs in total: labs models --all`);
|
|
59
|
+
}
|
|
60
|
+
console.log('Inside Claude Code: /model <name> switches model.\n');
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// The browser opens a one-time link (single use, 60 s), so the key is never pasted or shown. If that fails, the key
|
|
64
|
+
// goes to the clipboard, never to the screen, so it does not end up in terminal logs or screenshots.
|
|
65
|
+
async function openChat(config) {
|
|
66
|
+
if (!(await ensureProxy(config.port))) {
|
|
67
|
+
console.error('ā The labs proxy did not start. See ~/.labs/proxy.log');
|
|
68
|
+
process.exit(1);
|
|
69
|
+
}
|
|
70
|
+
const base = `http://127.0.0.1:${config.port}`;
|
|
71
|
+
let url = `${base}/chat`;
|
|
72
|
+
try {
|
|
73
|
+
const { data } = await axios.post(`${base}/chat/api/login-link`, null, { headers: { 'x-api-key': loadProxyKey(LABS_DIR) }, timeout: 3000 });
|
|
74
|
+
url = base + data.path;
|
|
75
|
+
console.log('š Opening the chat page, already logged in.');
|
|
76
|
+
} catch {
|
|
77
|
+
const clipboard = { win32: ['clip', []], darwin: ['pbcopy', []] }[process.platform];
|
|
78
|
+
if (clipboard) {
|
|
79
|
+
const copy = spawn(clipboard[0], clipboard[1]);
|
|
80
|
+
copy.stdin.end(loadProxyKey(LABS_DIR));
|
|
81
|
+
console.log('š Your labs key is on the clipboard: paste it once on the page.');
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
console.log(`š¬ ${base}/chat`);
|
|
85
|
+
const opener = process.platform === 'win32' ? ['cmd', ['/c', 'start', '', url]] : [process.platform === 'darwin' ? 'open' : 'xdg-open', [url]];
|
|
86
|
+
spawn(opener[0], opener[1], { stdio: 'ignore', detached: true }).unref();
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function launchClaude(claudeArgs, env, onExit = code => process.exit(code ?? 0)) {
|
|
90
|
+
// The native installer ships claude.exe; an npm-installed Claude Code on Windows is a .cmd shim that needs a shell.
|
|
91
|
+
const command = process.platform === 'win32' ? 'claude.exe' : 'claude';
|
|
92
|
+
const child = spawn(command, claudeArgs, { stdio: 'inherit', env });
|
|
93
|
+
const handle = { child, kill: () => handle.child.kill() };
|
|
94
|
+
child.on('error', error => {
|
|
95
|
+
if (error.code !== 'ENOENT' || process.platform !== 'win32') {
|
|
96
|
+
console.error(`ā Could not start Claude Code: ${error.message}`);
|
|
97
|
+
process.exit(1);
|
|
98
|
+
}
|
|
99
|
+
const quoted = claudeArgs.map(arg => `"${arg.replace(/"/g, '\\"')}"`).join(' ');
|
|
100
|
+
handle.child = spawn(`claude ${quoted}`, { stdio: 'inherit', env, shell: true });
|
|
101
|
+
handle.child.on('exit', code => onExit(code));
|
|
102
|
+
});
|
|
103
|
+
child.on('exit', code => onExit(code));
|
|
104
|
+
return handle;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// Everything needed to start Claude Code against the labs proxy; shared by `labs` and the labs leg of `labs auto`.
|
|
108
|
+
async function prepareLabsLaunch(config, args) {
|
|
109
|
+
const { modelName, claudeArgs } = parseLaunchArgs(args, {
|
|
110
|
+
aliases: config.models,
|
|
111
|
+
llmIds: new Set(loadLlmList().map(item => item.llm)),
|
|
112
|
+
defaultModel: config.defaultModel
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
const chromeWarning = chromeCompatibilityWarning(claudeArgs);
|
|
116
|
+
if (chromeWarning) console.error(`ā ļø ${chromeWarning}`);
|
|
117
|
+
|
|
118
|
+
if (!(await ensureProxy(config.port))) {
|
|
119
|
+
console.error('ā The labs proxy did not start. See ~/.labs/proxy.log');
|
|
120
|
+
process.exit(1);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const llm = config.models[modelName] || modelName;
|
|
124
|
+
const onBedrock = Boolean(bedrockModelId(llm));
|
|
125
|
+
// Without a key every call gets 401, and Claude Code retries those silently: the window would look frozen.
|
|
126
|
+
if (onBedrock && !readBedrockKey(LABS_DIR)) {
|
|
127
|
+
console.error(`ā ${modelName} runs on Amazon Bedrock and no Bedrock key is saved. Run: labs bedrock`);
|
|
128
|
+
process.exit(1);
|
|
129
|
+
}
|
|
130
|
+
const llmInfo = onBedrock
|
|
131
|
+
? { max_context_limit: config.bedrockContextLimit }
|
|
132
|
+
: loadLlmList().find(item => item.llm === llm);
|
|
133
|
+
if (!claudeArgs.some(arg => arg === '--effort' || arg.startsWith('--effort='))) {
|
|
134
|
+
claudeArgs.push('--effort', config.defaultEffort || 'high');
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const env = {
|
|
138
|
+
...process.env,
|
|
139
|
+
// Claude Code only knows Anthropic windows; without this it assumes 200k and compacts at the wrong point.
|
|
140
|
+
...(llmInfo ? { CLAUDE_CODE_MAX_CONTEXT_TOKENS: String(Math.min(llmInfo.max_context_limit, config.contextLimit)) } : {}),
|
|
141
|
+
ANTHROPIC_BASE_URL: `http://127.0.0.1:${config.port}`,
|
|
142
|
+
ANTHROPIC_AUTH_TOKEN: loadProxyKey(LABS_DIR),
|
|
143
|
+
ANTHROPIC_CUSTOM_HEADERS: withLabsHeaders(process.env.ANTHROPIC_CUSTOM_HEADERS, randomUUID(), modelName),
|
|
144
|
+
// Subagents run on the chosen model too; the proxy answers overload with 529 so Claude Code retries, never switches.
|
|
145
|
+
CLAUDE_CODE_SUBAGENT_MODEL: modelName
|
|
146
|
+
};
|
|
147
|
+
// A leftover key (e.g. from an Ollama setup) would take precedence over the proxy's auth token.
|
|
148
|
+
delete env.ANTHROPIC_API_KEY;
|
|
149
|
+
|
|
150
|
+
const window = llmInfo ? `, ${Math.round(llmInfo.max_context_limit / 1000)}k context` : '';
|
|
151
|
+
const banner = `š Claude Code on ${onBedrock ? 'Amazon Bedrock' : 'ElevenLabs'} ${modelName} (${llm}${window})`;
|
|
152
|
+
return { modelName, claudeArgs, env, banner };
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// `labs auto`: real Claude Code on the subscription first; when the subscription fails for good in this session
|
|
156
|
+
// (see src/subscription-failure.js), the same session is resumed on labs Opus 5.5. The next start tries the subscription again.
|
|
157
|
+
async function runAuto(args) {
|
|
158
|
+
const force = process.env.LABS_AUTO_FORCE_FALLBACK === '1';
|
|
159
|
+
const leg = planSubscriptionLeg(args);
|
|
160
|
+
const env = { ...process.env };
|
|
161
|
+
// The subscription leg must talk to Anthropic directly, even when this shell was started with labs variables.
|
|
162
|
+
for (const name of ['ANTHROPIC_BASE_URL', 'ANTHROPIC_AUTH_TOKEN', 'ANTHROPIC_CUSTOM_HEADERS', 'CLAUDE_CODE_MAX_CONTEXT_TOKENS', 'CLAUDE_CODE_SUBAGENT_MODEL']) delete env[name];
|
|
163
|
+
console.log('š¢ Opus Ā· subscription');
|
|
164
|
+
process.stdout.write(']0;Opus Ā· subscription');
|
|
165
|
+
|
|
166
|
+
let switched = false;
|
|
167
|
+
const toLabs = async failure => {
|
|
168
|
+
if (switched) return;
|
|
169
|
+
switched = true;
|
|
170
|
+
watcher.stop();
|
|
171
|
+
const sessionId = leg.sessionId || failure.sessionId;
|
|
172
|
+
handle.kill();
|
|
173
|
+
let config = loadConfig();
|
|
174
|
+
if (!config?.apiKey || !config.bridgeAgent) { await runSetup(); config = loadConfig(); }
|
|
175
|
+
const launch = await prepareLabsLaunch(config, planLabsLegArgs(args, sessionId));
|
|
176
|
+
console.log(`
|
|
177
|
+
š Subscription limit hit (${failure.kind}), continuing on labs Opus 5.5`);
|
|
178
|
+
process.stdout.write(']0;Opus 5.5 Ā· labs');
|
|
179
|
+
keepProxyAlive(config.port);
|
|
180
|
+
launchClaude(launch.claudeArgs, launch.env);
|
|
181
|
+
};
|
|
182
|
+
const watcher = createWatcher({ sessionId: leg.sessionId, fromStart: leg.fromStart, startedAt: Date.now(), forceFallback: force, onFailure: toLabs });
|
|
183
|
+
const handle = launchClaude(leg.args, env, async code => {
|
|
184
|
+
if (switched) return;
|
|
185
|
+
// A -p run ends right after the error, before the next poll: read what is left before deciding to exit.
|
|
186
|
+
const found = watcher.pollNow();
|
|
187
|
+
if (found) return toLabs(found);
|
|
188
|
+
watcher.stop();
|
|
189
|
+
process.exit(code ?? 0);
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
async function main() {
|
|
194
|
+
const args = process.argv.slice(2);
|
|
195
|
+
if (args[0] === 'setup') return runSetup();
|
|
196
|
+
if (args[0] === 'bedrock') return setupBedrock();
|
|
197
|
+
if (args[0] === 'auto') return runAuto(args.slice(1));
|
|
198
|
+
if (args[0] === '--help' || args[0] === 'help') return console.log(HELP);
|
|
199
|
+
if (args[0] === '--version') return console.log(version);
|
|
200
|
+
if (args[0] === 'autostart') {
|
|
201
|
+
if (!['on', 'off'].includes(args[1])) return console.log('Use: labs autostart on | labs autostart off');
|
|
202
|
+
const { ok, message } = setAutostart(args[1] === 'on');
|
|
203
|
+
console[ok ? 'log' : 'error'](message);
|
|
204
|
+
return process.exit(ok ? 0 : 1);
|
|
205
|
+
}
|
|
206
|
+
// The autostart task runs this with no window, so it must never fall into the interactive setup.
|
|
207
|
+
if (args[0] === 'serve') {
|
|
208
|
+
const ready = loadConfig()?.apiKey && await ensureProxy(loadConfig().port);
|
|
209
|
+
console.log(ready ? 'ā
labs proxy running' : 'ā labs proxy not started (run labs setup, see ~/.labs/proxy.log)');
|
|
210
|
+
return process.exit(ready ? 0 : 1);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// Box users (friends) may have no ElevenLabs account at all, so these never fall into the ElevenLabs setup.
|
|
214
|
+
if (args[0] === 'key') return saveGatewayKey(askHidden, ask);
|
|
215
|
+
if (args[0] === 'admin-token') return makeAdminToken(args[1]);
|
|
216
|
+
if (args[0] === 'console') return openConsole(storedConfig());
|
|
217
|
+
const boxModel = gatewayModelFrom(args);
|
|
218
|
+
if (boxModel) {
|
|
219
|
+
const launch = await prepareGatewayLaunch(storedConfig(), args, boxModel);
|
|
220
|
+
console.log(launch.banner);
|
|
221
|
+
return launchClaude(launch.claudeArgs, launch.env);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
let config = loadConfig();
|
|
225
|
+
if (!config?.apiKey || !config.bridgeAgent) {
|
|
226
|
+
console.log(`No setup found at ${CONFIG_PATH}, running setup first.\n`);
|
|
227
|
+
await runSetup();
|
|
228
|
+
config = loadConfig();
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
if (args[0] === 'models') return printModels(config, args.includes('--all'));
|
|
232
|
+
if (args[0] === 'usage') return printUsage(config);
|
|
233
|
+
if (args[0] === 'chat') return openChat(config);
|
|
234
|
+
if (args[0] === 'stop') return console.log(await stopProxy(config.port) ? 'š Proxy stopped' : 'Proxy was not running');
|
|
235
|
+
|
|
236
|
+
const launch = await prepareLabsLaunch(config, args);
|
|
237
|
+
console.log(launch.banner);
|
|
238
|
+
keepProxyAlive(config.port);
|
|
239
|
+
launchClaude(launch.claudeArgs, launch.env);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
main().catch(error => {
|
|
243
|
+
console.error(`ā ${error.message}`);
|
|
244
|
+
process.exit(1);
|
|
245
|
+
});
|
package/package.json
CHANGED
|
@@ -1,34 +1,34 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "labs-claude",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Run the real Claude Code CLI on ElevenLabs models (GPT-6 Astra, Opus 5.5, Gemini, GLM...) with your own ElevenLabs key ā like Ollama, but for ElevenLabs.",
|
|
5
|
-
"bin": {
|
|
6
|
-
"labs": "bin/labs.js"
|
|
7
|
-
},
|
|
8
|
-
"files": [
|
|
9
|
-
"bin",
|
|
10
|
-
"src",
|
|
11
|
-
"README.md"
|
|
12
|
-
],
|
|
13
|
-
"scripts": {
|
|
14
|
-
"test": "node --test"
|
|
15
|
-
},
|
|
16
|
-
"engines": {
|
|
17
|
-
"node": ">=18"
|
|
18
|
-
},
|
|
19
|
-
"keywords": [
|
|
20
|
-
"claude-code",
|
|
21
|
-
"elevenlabs",
|
|
22
|
-
"proxy",
|
|
23
|
-
"anthropic",
|
|
24
|
-
"cli"
|
|
25
|
-
],
|
|
26
|
-
"license": "MIT",
|
|
27
|
-
"dependencies": {
|
|
28
|
-
"@aws-sdk/client-bedrock-runtime": "^3.1156.0",
|
|
29
|
-
"axios": "^1.20.0",
|
|
30
|
-
"body-parser": "^2.3.0",
|
|
31
|
-
"express": "^5.2.1",
|
|
32
|
-
"ws": "^8.22.0"
|
|
33
|
-
}
|
|
34
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "labs-claude",
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"description": "Run the real Claude Code CLI on ElevenLabs models (GPT-6 Astra, Opus 5.5, Gemini, GLM...) with your own ElevenLabs key ā like Ollama, but for ElevenLabs.",
|
|
5
|
+
"bin": {
|
|
6
|
+
"labs": "bin/labs.js"
|
|
7
|
+
},
|
|
8
|
+
"files": [
|
|
9
|
+
"bin",
|
|
10
|
+
"src",
|
|
11
|
+
"README.md"
|
|
12
|
+
],
|
|
13
|
+
"scripts": {
|
|
14
|
+
"test": "node --test"
|
|
15
|
+
},
|
|
16
|
+
"engines": {
|
|
17
|
+
"node": ">=18"
|
|
18
|
+
},
|
|
19
|
+
"keywords": [
|
|
20
|
+
"claude-code",
|
|
21
|
+
"elevenlabs",
|
|
22
|
+
"proxy",
|
|
23
|
+
"anthropic",
|
|
24
|
+
"cli"
|
|
25
|
+
],
|
|
26
|
+
"license": "MIT",
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"@aws-sdk/client-bedrock-runtime": "^3.1156.0",
|
|
29
|
+
"axios": "^1.20.0",
|
|
30
|
+
"body-parser": "^2.3.0",
|
|
31
|
+
"express": "^5.2.1",
|
|
32
|
+
"ws": "^8.22.0"
|
|
33
|
+
}
|
|
34
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
const fs = require('fs');
|
|
2
|
+
const os = require('os');
|
|
3
|
+
const path = require('path');
|
|
4
|
+
const { randomUUID } = require('crypto');
|
|
5
|
+
const { detectInText } = require('./subscription-failure');
|
|
6
|
+
|
|
7
|
+
const PROJECTS_DIR = path.join(os.homedir(), '.claude', 'projects');
|
|
8
|
+
|
|
9
|
+
// Flags that name the session or the model are rewritten per leg; everything else passes through untouched.
|
|
10
|
+
function stripSessionAndModelArgs(args) {
|
|
11
|
+
const out = [];
|
|
12
|
+
for (let i = 0; i < args.length; i += 1) {
|
|
13
|
+
const arg = args[i];
|
|
14
|
+
if (['--session-id', '--resume', '-r', '--model'].includes(arg)) { i += 1; continue; }
|
|
15
|
+
if (/^--(session-id|resume|model)=/.test(arg) || arg === '--continue' || arg === '-c') continue;
|
|
16
|
+
out.push(arg);
|
|
17
|
+
}
|
|
18
|
+
return out;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function findResumeId(args) {
|
|
22
|
+
const i = args.findIndex(arg => arg === '--resume' || arg === '-r' || arg === '--session-id');
|
|
23
|
+
if (i >= 0 && args[i + 1] && !args[i + 1].startsWith('-')) return args[i + 1];
|
|
24
|
+
const inline = args.find(arg => /^--(resume|session-id)=/.test(arg));
|
|
25
|
+
return inline ? inline.split('=')[1] : null;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// First leg: real Claude Code. A fresh session gets a known id so the transcript can be watched.
|
|
29
|
+
function planSubscriptionLeg(args) {
|
|
30
|
+
const sessionId = findResumeId(args);
|
|
31
|
+
const continuing = args.includes('--continue') || args.includes('-c');
|
|
32
|
+
if (sessionId || continuing) return { sessionId, fromStart: false, args };
|
|
33
|
+
const fresh = randomUUID();
|
|
34
|
+
return { sessionId: fresh, fromStart: true, args: [...args, '--session-id', fresh] };
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// Second leg: the same session resumed through labs. The prompt/-p arguments stay, so `-p "task"` re-asks the task.
|
|
38
|
+
function planLabsLegArgs(args, sessionId) {
|
|
39
|
+
return [...stripSessionAndModelArgs(args), '--resume', sessionId, '--model', 'opus'];
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function transcriptPath(sessionId) {
|
|
43
|
+
if (!fs.existsSync(PROJECTS_DIR)) return null;
|
|
44
|
+
for (const dir of fs.readdirSync(PROJECTS_DIR)) {
|
|
45
|
+
const file = path.join(PROJECTS_DIR, dir, `${sessionId}.jsonl`);
|
|
46
|
+
if (fs.existsSync(file)) return file;
|
|
47
|
+
}
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function newestTranscriptSince(startedAt) {
|
|
52
|
+
if (!fs.existsSync(PROJECTS_DIR)) return null;
|
|
53
|
+
let best = null;
|
|
54
|
+
for (const dir of fs.readdirSync(PROJECTS_DIR)) {
|
|
55
|
+
const full = path.join(PROJECTS_DIR, dir);
|
|
56
|
+
if (!fs.statSync(full).isDirectory()) continue;
|
|
57
|
+
for (const name of fs.readdirSync(full).filter(n => n.endsWith('.jsonl'))) {
|
|
58
|
+
const file = path.join(full, name);
|
|
59
|
+
const mtime = fs.statSync(file).mtimeMs;
|
|
60
|
+
if (mtime >= startedAt && (!best || mtime > best.mtime)) best = { file, mtime };
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return best?.file || null;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// Reads only bytes appended after the leg started, so an old limit message in a resumed session never triggers.
|
|
67
|
+
function createWatcher({ sessionId, startedAt, forceFallback, fromStart, onFailure, intervalMs = 1000 }) {
|
|
68
|
+
let file = sessionId ? transcriptPath(sessionId) : null;
|
|
69
|
+
let offset = fromStart ? 0 : (file ? fs.statSync(file).size : null);
|
|
70
|
+
let sawReply = false;
|
|
71
|
+
const poll = () => {
|
|
72
|
+
file = file || (sessionId ? transcriptPath(sessionId) : newestTranscriptSince(startedAt));
|
|
73
|
+
if (!file) return null;
|
|
74
|
+
const size = fs.statSync(file).size;
|
|
75
|
+
if (offset === null) offset = size;
|
|
76
|
+
if (size <= offset) return null;
|
|
77
|
+
const fd = fs.openSync(file, 'r');
|
|
78
|
+
const buffer = Buffer.alloc(size - offset);
|
|
79
|
+
fs.readSync(fd, buffer, 0, buffer.length, offset);
|
|
80
|
+
fs.closeSync(fd);
|
|
81
|
+
const text = buffer.toString('utf8');
|
|
82
|
+
const lastNewline = text.lastIndexOf('\n');
|
|
83
|
+
if (lastNewline < 0) return null;
|
|
84
|
+
offset += Buffer.byteLength(text.slice(0, lastNewline + 1));
|
|
85
|
+
const chunk = text.slice(0, lastNewline + 1);
|
|
86
|
+
const found = detectInText(chunk);
|
|
87
|
+
if (found) return found;
|
|
88
|
+
if (forceFallback && /"type":"assistant"/.test(chunk)) sawReply = true;
|
|
89
|
+
return forceFallback && sawReply ? { kind: 'forced', text: 'LABS_AUTO_FORCE_FALLBACK', sessionId } : null;
|
|
90
|
+
};
|
|
91
|
+
const timer = setInterval(() => { const found = poll(); if (found) { clearInterval(timer); onFailure(found); } }, intervalMs);
|
|
92
|
+
return { stop: () => clearInterval(timer), pollNow: poll };
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
module.exports = { stripSessionAndModelArgs, findResumeId, planSubscriptionLeg, planLabsLegArgs, createWatcher, transcriptPath, newestTranscriptSince };
|