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/src/proxy-manager.js
CHANGED
|
@@ -1,82 +1,85 @@
|
|
|
1
|
-
const axios = require('axios');
|
|
2
|
-
const fs = require('fs');
|
|
3
|
-
const path = require('path');
|
|
4
|
-
const { spawn } = require('child_process');
|
|
5
|
-
const { LABS_DIR } = require('./config');
|
|
6
|
-
const { loadProxyKey } = require('./local-auth');
|
|
7
|
-
const { version: VERSION } = require('../package.json');
|
|
8
|
-
|
|
9
|
-
async function health(port) {
|
|
10
|
-
try {
|
|
11
|
-
return (await axios.get(`http://127.0.0.1:${port}/health`, { timeout: 1000 })).data;
|
|
12
|
-
} catch {
|
|
13
|
-
return null;
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
// Reuses a running proxy of the same version; an older version (after `npm update -g`)
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
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
|
-
|
|
1
|
+
const axios = require('axios');
|
|
2
|
+
const fs = require('fs');
|
|
3
|
+
const path = require('path');
|
|
4
|
+
const { spawn } = require('child_process');
|
|
5
|
+
const { LABS_DIR, credentialFingerprint, loadConfig } = require('./config');
|
|
6
|
+
const { loadProxyKey } = require('./local-auth');
|
|
7
|
+
const { version: VERSION } = require('../package.json');
|
|
8
|
+
|
|
9
|
+
async function health(port) {
|
|
10
|
+
try {
|
|
11
|
+
return (await axios.get(`http://127.0.0.1:${port}/health`, { timeout: 1000 })).data;
|
|
12
|
+
} catch {
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// Reuses a running proxy of the same version and key; an older version (after `npm update -g`) or one still holding
|
|
18
|
+
// an old ElevenLabs key or bridge agent (after `labs setup` with a new account) is stopped and replaced.
|
|
19
|
+
async function ensureProxy(port) {
|
|
20
|
+
const wanted = credentialFingerprint(loadConfig());
|
|
21
|
+
const isCurrent = info => info?.name === 'labs-claude' && info.version === VERSION && info.credentials === wanted;
|
|
22
|
+
const running = await health(port);
|
|
23
|
+
if (isCurrent(running)) return true;
|
|
24
|
+
if (running?.name === 'labs-claude') {
|
|
25
|
+
await stopProxy(port);
|
|
26
|
+
await new Promise(resolve => setTimeout(resolve, 500));
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
fs.mkdirSync(LABS_DIR, { recursive: true });
|
|
30
|
+
const log = fs.openSync(path.join(LABS_DIR, 'proxy.log'), 'a');
|
|
31
|
+
const child = spawn(process.execPath, [path.join(__dirname, 'proxy.js')], {
|
|
32
|
+
detached: true,
|
|
33
|
+
stdio: ['ignore', log, log],
|
|
34
|
+
windowsHide: true
|
|
35
|
+
});
|
|
36
|
+
child.unref();
|
|
37
|
+
|
|
38
|
+
for (let attempt = 0; attempt < 120; attempt += 1) {
|
|
39
|
+
if (isCurrent(await health(port))) return true;
|
|
40
|
+
await new Promise(resolve => setTimeout(resolve, 250));
|
|
41
|
+
}
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// Claude Code only reaches the proxy, so a proxy that died (crash, `labs stop`, an upgrade) left every open window
|
|
46
|
+
// retrying ECONNREFUSED until it gave up. While Claude Code runs, the launcher restarts a dead proxy within seconds.
|
|
47
|
+
// It never replaces a running proxy of another version: windows of different versions would fight over the port.
|
|
48
|
+
const WATCHDOG_MS = 5000;
|
|
49
|
+
const BUSY_PROXY_TIMEOUT_MS = 3000;
|
|
50
|
+
|
|
51
|
+
// Only a refused connection means no proxy. A slow /health is a busy proxy: treating it as dead spawned duplicates.
|
|
52
|
+
async function isProxyDown(port) {
|
|
53
|
+
try {
|
|
54
|
+
await axios.get(`http://127.0.0.1:${port}/health`, { timeout: BUSY_PROXY_TIMEOUT_MS });
|
|
55
|
+
return false;
|
|
56
|
+
} catch (error) {
|
|
57
|
+
return error.code === 'ECONNREFUSED';
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function keepProxyAlive(port) {
|
|
62
|
+
let restarting = false;
|
|
63
|
+
let misses = 0;
|
|
64
|
+
const timer = setInterval(async () => {
|
|
65
|
+
if (restarting) return;
|
|
66
|
+
// Two misses in a row: a newer labs replacing the proxy leaves the port free for a moment, and restarting
|
|
67
|
+
// this window's older version in that gap would take the port from the upgrade.
|
|
68
|
+
misses = await isProxyDown(port) ? misses + 1 : 0;
|
|
69
|
+
if (misses < 2) return;
|
|
70
|
+
restarting = true;
|
|
71
|
+
// Never print here: this runs inside Claude Code's terminal, and a line written there lands in Boss's input box.
|
|
72
|
+
fs.appendFileSync(path.join(LABS_DIR, 'proxy.log'), `⚠️ ${new Date().toISOString()} watchdog: labs proxy was down; restarting it\n`);
|
|
73
|
+
await ensureProxy(port).catch(() => {});
|
|
74
|
+
restarting = false;
|
|
75
|
+
}, WATCHDOG_MS);
|
|
76
|
+
timer.unref();
|
|
77
|
+
return timer;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
async function stopProxy(port) {
|
|
81
|
+
return axios.post(`http://127.0.0.1:${port}/shutdown`, null, { headers: { 'x-api-key': loadProxyKey(LABS_DIR) } })
|
|
82
|
+
.then(() => true, () => false);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
module.exports = { ensureProxy, keepProxyAlive, stopProxy };
|