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/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
|
@@ -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 };
|