labs-claude 0.1.16 ā 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 +82 -9
- package/bin/labs.js +245 -119
- package/package.json +34 -33
- package/src/anthropic-protocol.js +2 -1
- package/src/auto-fallback.js +95 -0
- package/src/autostart.js +43 -0
- package/src/bedrock.js +162 -0
- package/src/browser-setup.js +38 -38
- package/src/chat/index.html +534 -0
- package/src/chat-routes.js +149 -0
- package/src/config.js +71 -58
- package/src/gateway-client.js +124 -0
- package/src/local-auth.js +33 -32
- package/src/proxy-manager.js +85 -70
- package/src/proxy.js +801 -710
- package/src/read-shunt.js +26 -26
- package/src/setup.js +23 -4
- package/src/subscription-failure.js +36 -0
- package/src/usage.js +44 -44
package/src/read-shunt.js
CHANGED
|
@@ -1,26 +1,26 @@
|
|
|
1
|
-
// Spotify's "Shunt" (2026-09): a whole-file Read of a big file goes to a cheap worker model, and the main model gets
|
|
2
|
-
// a digest with line ranges instead of every line. Reads with offset/limit are exact on purpose and pass untouched,
|
|
3
|
-
// so the main model can still fetch the precise lines it needs before an Edit.
|
|
4
|
-
function oversizedWholeFileReads(toolResults, priorContent, maxLines) {
|
|
5
|
-
if (!Array.isArray(priorContent)) return [];
|
|
6
|
-
const calls = new Map(priorContent.filter(block => block.type === 'tool_use').map(block => [block.id, block]));
|
|
7
|
-
return toolResults.flatMap(result => {
|
|
8
|
-
const call = calls.get(result.toolUseId);
|
|
9
|
-
if (!call || call.name !== 'Read' || result.isError) return [];
|
|
10
|
-
if (call.input?.offset !== undefined || call.input?.limit !== undefined) return [];
|
|
11
|
-
const lineCount = result.content.split('\n').length;
|
|
12
|
-
return lineCount > maxLines ? [{ result, filePath: call.input?.file_path || '(unknown file)', lineCount }] : [];
|
|
13
|
-
});
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
const WORKER_PROMPT = 'You read files for a coding agent that will not see them. Given a file (with line numbers) and '
|
|
17
|
-
+ 'the agent\'s current task, write a digest: what the file is for; every function, class, section and export with '
|
|
18
|
-
+ 'its line range; important constants, config keys and identifiers; and, copied verbatim with line numbers, every '
|
|
19
|
-
+ 'part that matters for the task. Be factual and complete; never invent lines.';
|
|
20
|
-
|
|
21
|
-
function shuntNote(filePath, lineCount, model, digest) {
|
|
22
|
-
return `[labs: ${filePath} has ${lineCount} lines. To save context, ${model} read the whole file and wrote this `
|
|
23
|
-
+ 'digest. Read it again with offset and limit for the exact lines before you Edit.]\n\n' + digest;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
module.exports = { oversizedWholeFileReads, shuntNote, WORKER_PROMPT };
|
|
1
|
+
// Spotify's "Shunt" (2026-09): a whole-file Read of a big file goes to a cheap worker model, and the main model gets
|
|
2
|
+
// a digest with line ranges instead of every line. Reads with offset/limit are exact on purpose and pass untouched,
|
|
3
|
+
// so the main model can still fetch the precise lines it needs before an Edit.
|
|
4
|
+
function oversizedWholeFileReads(toolResults, priorContent, maxLines) {
|
|
5
|
+
if (!Array.isArray(priorContent)) return [];
|
|
6
|
+
const calls = new Map(priorContent.filter(block => block.type === 'tool_use').map(block => [block.id, block]));
|
|
7
|
+
return toolResults.flatMap(result => {
|
|
8
|
+
const call = calls.get(result.toolUseId);
|
|
9
|
+
if (!call || call.name !== 'Read' || result.isError) return [];
|
|
10
|
+
if (call.input?.offset !== undefined || call.input?.limit !== undefined) return [];
|
|
11
|
+
const lineCount = result.content.split('\n').length;
|
|
12
|
+
return lineCount > maxLines ? [{ result, filePath: call.input?.file_path || '(unknown file)', lineCount }] : [];
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const WORKER_PROMPT = 'You read files for a coding agent that will not see them. Given a file (with line numbers) and '
|
|
17
|
+
+ 'the agent\'s current task, write a digest: what the file is for; every function, class, section and export with '
|
|
18
|
+
+ 'its line range; important constants, config keys and identifiers; and, copied verbatim with line numbers, every '
|
|
19
|
+
+ 'part that matters for the task. Be factual and complete; never invent lines.';
|
|
20
|
+
|
|
21
|
+
function shuntNote(filePath, lineCount, model, digest) {
|
|
22
|
+
return `[labs: ${filePath} has ${lineCount} lines. To save context, ${model} read the whole file and wrote this `
|
|
23
|
+
+ 'digest. Read it again with offset and limit for the exact lines before you Edit.]\n\n' + digest;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
module.exports = { oversizedWholeFileReads, shuntNote, WORKER_PROMPT };
|
package/src/setup.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
const axios = require('axios');
|
|
2
2
|
const readline = require('readline');
|
|
3
3
|
const { execFileSync } = require('child_process');
|
|
4
|
-
const
|
|
4
|
+
const path = require('path');
|
|
5
|
+
const { API_BASE, CONFIG_PATH, LABS_DIR, loadConfig, saveConfig, saveLlmList } = require('./config');
|
|
6
|
+
const { saveBedrockKey } = require('./bedrock');
|
|
5
7
|
const { EXTENSION_URL, installBrowserServer } = require('./browser-setup');
|
|
6
8
|
|
|
7
9
|
const BRIDGE_AGENT_NAME = 'Labs Claude Code Bridge';
|
|
@@ -47,7 +49,7 @@ async function createBridgeAgent(headers) {
|
|
|
47
49
|
backup_llm_config: { preference: 'disabled' }
|
|
48
50
|
}
|
|
49
51
|
},
|
|
50
|
-
conversation: { text_only: true, max_duration_seconds: 7200, file_input: { enabled: true } }
|
|
52
|
+
conversation: { text_only: true, max_duration_seconds: 7200, max_duration_seconds_after_last_agent_message: 7200, file_input: { enabled: true } }
|
|
51
53
|
},
|
|
52
54
|
platform_settings: {
|
|
53
55
|
// Post-conversation analysis adds nothing for a coding bridge.
|
|
@@ -88,6 +90,10 @@ async function runSetup() {
|
|
|
88
90
|
} else {
|
|
89
91
|
console.log('š Paste your ElevenLabs API key (elevenlabs.io ā Developers ā API Keys; it needs ElevenAgents access).');
|
|
90
92
|
apiKey = await askHidden(' API key: ');
|
|
93
|
+
if (!apiKey) {
|
|
94
|
+
console.log('ā Nothing was pasted. Your terminal may block pasting into a hidden prompt. Run this instead:\n $env:ELEVENLABS_API_KEY = "sk_..."; labs setup');
|
|
95
|
+
process.exit(1);
|
|
96
|
+
}
|
|
91
97
|
}
|
|
92
98
|
const headers = { 'xi-api-key': apiKey };
|
|
93
99
|
|
|
@@ -95,7 +101,9 @@ async function runSetup() {
|
|
|
95
101
|
try {
|
|
96
102
|
llms = (await axios.get(`${API_BASE}/llm/list`, { headers })).data.llms;
|
|
97
103
|
} catch (error) {
|
|
98
|
-
|
|
104
|
+
const reason = error.response?.data?.detail?.message || error.response?.data?.detail?.status || error.message;
|
|
105
|
+
console.log(`ā ElevenLabs rejected the key (${error.response?.status || 'no response'}): ${reason}`);
|
|
106
|
+
console.log(` The key you gave is ${apiKey.length} characters long${apiKey.startsWith('sk_') ? '' : ' and does not start with sk_'}. Check it has ElevenAgents permission.`);
|
|
99
107
|
process.exit(1);
|
|
100
108
|
}
|
|
101
109
|
saveLlmList(llms);
|
|
@@ -145,4 +153,15 @@ function ask(question) {
|
|
|
145
153
|
});
|
|
146
154
|
}
|
|
147
155
|
|
|
148
|
-
|
|
156
|
+
// The key is typed hidden and goes straight to ~/.labs/bedrock-key (mode 600); it is never printed or logged.
|
|
157
|
+
async function setupBedrock() {
|
|
158
|
+
console.log('\nš§ Amazon Bedrock key for GLM 5.3 (labs --model glm)');
|
|
159
|
+
console.log(' Create one in the AWS console: Amazon Bedrock > API keys > Generate long-term API key.\n');
|
|
160
|
+
const key = await askHidden('Paste the Bedrock API key (hidden): ');
|
|
161
|
+
if (!key) return console.log('Nothing saved.');
|
|
162
|
+
if (!/^ABSK/.test(key)) console.log('ā ļø Bedrock API keys usually start with ABSK; saving it anyway.');
|
|
163
|
+
saveBedrockKey(LABS_DIR, key);
|
|
164
|
+
console.log(`ā
Saved to ${path.join(LABS_DIR, 'bedrock-key')}. Start with: labs --model glm`);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
module.exports = { runSetup, setupBedrock, ask, askHidden };
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// Decides from a Claude Code transcript record whether the subscription path has failed for good.
|
|
2
|
+
// Signals are the real formats Claude Code writes (checked in 2026-10 transcripts and the StopFailure hook docs):
|
|
3
|
+
// an assistant record with isApiErrorMessage=true whose `error` is one of the hook's error types.
|
|
4
|
+
const LONG_WAIT_MS = 5 * 60 * 1000;
|
|
5
|
+
// These end a subscription for this session no matter when they reset.
|
|
6
|
+
const HARD_ERRORS = new Set(['authentication_failed', 'oauth_org_not_allowed', 'account_on_hold', 'billing_error']);
|
|
7
|
+
|
|
8
|
+
// quotaLimits exists only on a real subscription rate limit; a plain 429 or an overload is transient and Claude Code retries it.
|
|
9
|
+
function detectSubscriptionFailure(record, now = Date.now()) {
|
|
10
|
+
if (!record || record.type !== 'assistant' || record.isApiErrorMessage !== true) return null;
|
|
11
|
+
if (HARD_ERRORS.has(record.error)) return { kind: record.error, text: errorText(record) };
|
|
12
|
+
if (record.error !== 'rate_limit') return null;
|
|
13
|
+
|
|
14
|
+
const quota = record.quotaLimits;
|
|
15
|
+
if (!quota || quota.status !== 'rejected') return null;
|
|
16
|
+
const waitMs = quota.resetsAt ? quota.resetsAt * 1000 - now : Infinity;
|
|
17
|
+
if (waitMs <= LONG_WAIT_MS) return null;
|
|
18
|
+
return { kind: `rate_limit:${quota.rateLimitType || 'unknown'}`, text: errorText(record) };
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function errorText(record) {
|
|
22
|
+
return (record.message?.content || []).map(block => block.text || '').join(' ').slice(0, 200);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function detectInText(text, now) {
|
|
26
|
+
for (const line of text.split('\n')) {
|
|
27
|
+
if (!line.includes('isApiErrorMessage')) continue;
|
|
28
|
+
try {
|
|
29
|
+
const found = detectSubscriptionFailure(JSON.parse(line), now);
|
|
30
|
+
if (found) return { ...found, sessionId: JSON.parse(line).sessionId };
|
|
31
|
+
} catch { /* a half-written line is read again on the next poll */ }
|
|
32
|
+
}
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
module.exports = { detectSubscriptionFailure, detectInText, LONG_WAIT_MS };
|
package/src/usage.js
CHANGED
|
@@ -1,44 +1,44 @@
|
|
|
1
|
-
const axios = require('axios');
|
|
2
|
-
const { API_BASE } = require('./config');
|
|
3
|
-
|
|
4
|
-
// Totals what the bridge agent's recent conversations cost, per LLM, from ElevenLabs' own billing metadata.
|
|
5
|
-
async function printUsage(config, limit = 50) {
|
|
6
|
-
const headers = { 'xi-api-key': config.apiKey };
|
|
7
|
-
const subscription = (await axios.get('https://api.elevenlabs.io/v1/user/subscription', { headers })).data;
|
|
8
|
-
const list = (await axios.get(`${API_BASE}/conversations`, { headers, params: { agent_id: config.bridgeAgent, page_size: limit } })).data.conversations;
|
|
9
|
-
|
|
10
|
-
const perModel = {};
|
|
11
|
-
let credits = 0;
|
|
12
|
-
for (const item of list) {
|
|
13
|
-
const metadata = (await axios.get(`${API_BASE}/conversations/${item.conversation_id}`, { headers })).data.metadata || {};
|
|
14
|
-
credits += metadata.cost || 0;
|
|
15
|
-
const llmUsage = metadata.charging?.llm_usage || {};
|
|
16
|
-
for (const phase of ['irreversible_generation', 'initiated_generation']) {
|
|
17
|
-
for (const [model, usage] of Object.entries(llmUsage[phase]?.model_usage || {})) {
|
|
18
|
-
const row = perModel[model] ||= { conversations: 0, input: 0, cached: 0, output: 0, usd: 0 };
|
|
19
|
-
row.conversations += 1;
|
|
20
|
-
row.input += (usage.input?.tokens || 0) + (usage.input_cache_write?.tokens || 0);
|
|
21
|
-
row.cached += usage.input_cache_read?.tokens || 0;
|
|
22
|
-
row.output += usage.output_total?.tokens || 0;
|
|
23
|
-
row.usd += Object.values(usage).reduce((sum, part) => sum + (part.price || 0), 0);
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
const used = subscription.character_count;
|
|
29
|
-
const total = subscription.character_limit;
|
|
30
|
-
const reset = subscription.next_character_count_reset_unix
|
|
31
|
-
? new Date(subscription.next_character_count_reset_unix * 1000).toISOString().slice(0, 10)
|
|
32
|
-
: 'unknown';
|
|
33
|
-
console.log(`\nš° ElevenLabs credits: ${used.toLocaleString()} used of ${total.toLocaleString()} (${(used / total * 100).toFixed(1)}%), resets ${reset}`);
|
|
34
|
-
console.log(`š Last ${list.length} labs conversations: ${credits.toLocaleString()} credits\n`);
|
|
35
|
-
const pad = (value, width) => String(value).padStart(width);
|
|
36
|
-
console.log(`${'model'.padEnd(26)}${pad('convs', 6)}${pad('fresh in', 12)}${pad('cached in', 12)}${pad('output', 10)}${pad('USD', 9)}`);
|
|
37
|
-
for (const [model, row] of Object.entries(perModel).sort((a, b) => b[1].usd - a[1].usd)) {
|
|
38
|
-
console.log(`${model.padEnd(26)}${pad(row.conversations, 6)}${pad(row.input.toLocaleString(), 12)}${pad(row.cached.toLocaleString(), 12)}${pad(row.output.toLocaleString(), 10)}${pad(`$${row.usd.toFixed(2)}`, 9)}`);
|
|
39
|
-
}
|
|
40
|
-
console.log('\nFresh input is full price, cached input is about 10x cheaper, output is the most expensive per token.');
|
|
41
|
-
console.log('Billing can lag a few minutes behind a conversation.\n');
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
module.exports = { printUsage };
|
|
1
|
+
const axios = require('axios');
|
|
2
|
+
const { API_BASE } = require('./config');
|
|
3
|
+
|
|
4
|
+
// Totals what the bridge agent's recent conversations cost, per LLM, from ElevenLabs' own billing metadata.
|
|
5
|
+
async function printUsage(config, limit = 50) {
|
|
6
|
+
const headers = { 'xi-api-key': config.apiKey };
|
|
7
|
+
const subscription = (await axios.get('https://api.elevenlabs.io/v1/user/subscription', { headers })).data;
|
|
8
|
+
const list = (await axios.get(`${API_BASE}/conversations`, { headers, params: { agent_id: config.bridgeAgent, page_size: limit } })).data.conversations;
|
|
9
|
+
|
|
10
|
+
const perModel = {};
|
|
11
|
+
let credits = 0;
|
|
12
|
+
for (const item of list) {
|
|
13
|
+
const metadata = (await axios.get(`${API_BASE}/conversations/${item.conversation_id}`, { headers })).data.metadata || {};
|
|
14
|
+
credits += metadata.cost || 0;
|
|
15
|
+
const llmUsage = metadata.charging?.llm_usage || {};
|
|
16
|
+
for (const phase of ['irreversible_generation', 'initiated_generation']) {
|
|
17
|
+
for (const [model, usage] of Object.entries(llmUsage[phase]?.model_usage || {})) {
|
|
18
|
+
const row = perModel[model] ||= { conversations: 0, input: 0, cached: 0, output: 0, usd: 0 };
|
|
19
|
+
row.conversations += 1;
|
|
20
|
+
row.input += (usage.input?.tokens || 0) + (usage.input_cache_write?.tokens || 0);
|
|
21
|
+
row.cached += usage.input_cache_read?.tokens || 0;
|
|
22
|
+
row.output += usage.output_total?.tokens || 0;
|
|
23
|
+
row.usd += Object.values(usage).reduce((sum, part) => sum + (part.price || 0), 0);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const used = subscription.character_count;
|
|
29
|
+
const total = subscription.character_limit;
|
|
30
|
+
const reset = subscription.next_character_count_reset_unix
|
|
31
|
+
? new Date(subscription.next_character_count_reset_unix * 1000).toISOString().slice(0, 10)
|
|
32
|
+
: 'unknown';
|
|
33
|
+
console.log(`\nš° ElevenLabs credits: ${used.toLocaleString()} used of ${total.toLocaleString()} (${(used / total * 100).toFixed(1)}%), resets ${reset}`);
|
|
34
|
+
console.log(`š Last ${list.length} labs conversations: ${credits.toLocaleString()} credits\n`);
|
|
35
|
+
const pad = (value, width) => String(value).padStart(width);
|
|
36
|
+
console.log(`${'model'.padEnd(26)}${pad('convs', 6)}${pad('fresh in', 12)}${pad('cached in', 12)}${pad('output', 10)}${pad('USD', 9)}`);
|
|
37
|
+
for (const [model, row] of Object.entries(perModel).sort((a, b) => b[1].usd - a[1].usd)) {
|
|
38
|
+
console.log(`${model.padEnd(26)}${pad(row.conversations, 6)}${pad(row.input.toLocaleString(), 12)}${pad(row.cached.toLocaleString(), 12)}${pad(row.output.toLocaleString(), 10)}${pad(`$${row.usd.toFixed(2)}`, 9)}`);
|
|
39
|
+
}
|
|
40
|
+
console.log('\nFresh input is full price, cached input is about 10x cheaper, output is the most expensive per token.');
|
|
41
|
+
console.log('Billing can lag a few minutes behind a conversation.\n');
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
module.exports = { printUsage };
|