labs-claude 0.1.2 → 0.1.5
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/package.json +1 -1
- package/src/anthropic-protocol.js +15 -2
- package/src/proxy.js +91 -13
- package/src/setup.js +1 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "labs-claude",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.5",
|
|
4
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
5
|
"bin": { "labs": "bin/labs.js" },
|
|
6
6
|
"files": ["bin", "src", "README.md"],
|
|
@@ -26,6 +26,18 @@ function contentToText(content) {
|
|
|
26
26
|
}).filter(Boolean).join('\n\n');
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
+
// Base64 images and PDFs in the user turn, including those nested in tool results (screenshots, Read of a .png).
|
|
30
|
+
function collectImages(blocks) {
|
|
31
|
+
const found = [];
|
|
32
|
+
for (const block of blocks || []) {
|
|
33
|
+
if ((block.type === 'image' || block.type === 'document') && block.source?.type === 'base64') {
|
|
34
|
+
found.push({ mediaType: block.source.media_type, data: block.source.data });
|
|
35
|
+
}
|
|
36
|
+
if (block.type === 'tool_result' && Array.isArray(block.content)) found.push(...collectImages(block.content));
|
|
37
|
+
}
|
|
38
|
+
return found;
|
|
39
|
+
}
|
|
40
|
+
|
|
29
41
|
function extractLatestUserContent(messages) {
|
|
30
42
|
for (let index = messages.length - 1; index >= 0; index -= 1) {
|
|
31
43
|
const message = messages[index];
|
|
@@ -48,11 +60,12 @@ function extractLatestUserContent(messages) {
|
|
|
48
60
|
|
|
49
61
|
return {
|
|
50
62
|
text: text.trim(),
|
|
51
|
-
toolResults
|
|
63
|
+
toolResults,
|
|
64
|
+
images: collectImages(blocks)
|
|
52
65
|
};
|
|
53
66
|
}
|
|
54
67
|
|
|
55
|
-
return { text: '', toolResults: [] };
|
|
68
|
+
return { text: '', toolResults: [], images: [] };
|
|
56
69
|
}
|
|
57
70
|
|
|
58
71
|
function createAnthropicMessage({ model, text = '', toolCalls = [], inputTokens = 0 }) {
|
package/src/proxy.js
CHANGED
|
@@ -129,7 +129,7 @@ function withAgentSettings(settings, openFn) {
|
|
|
129
129
|
};
|
|
130
130
|
// Conversations default to 600 s on new agents; 7200 s is the API maximum.
|
|
131
131
|
await axios.patch(`${API_BASE}/agents/${config.bridgeAgent}`,
|
|
132
|
-
{ conversation_config: { agent: { prompt }, conversation: { max_duration_seconds: 7200 } } }, { headers: HEADERS });
|
|
132
|
+
{ conversation_config: { agent: { prompt }, conversation: { max_duration_seconds: 7200, file_input: { enabled: true } } } }, { headers: HEADERS });
|
|
133
133
|
appliedSettings = key;
|
|
134
134
|
console.log(`🎚️ Bridge set to ${settings.llm} effort=${settings.reasoning_effort || 'n/a'} max_tokens=${settings.max_tokens}`);
|
|
135
135
|
}
|
|
@@ -168,6 +168,7 @@ function openConversation(key, { llm, prompt, toolIds }) {
|
|
|
168
168
|
conversationId: null,
|
|
169
169
|
contextTokens: 0,
|
|
170
170
|
openToolCallIds: new Set(),
|
|
171
|
+
filesUploaded: 0,
|
|
171
172
|
closeReason: ''
|
|
172
173
|
};
|
|
173
174
|
connections.set(key, conn);
|
|
@@ -284,9 +285,20 @@ function withWindowsPaths(input) {
|
|
|
284
285
|
}
|
|
285
286
|
|
|
286
287
|
// The agent can emit text and several tool calls for one turn as separate events, so settle after a short quiet gap.
|
|
288
|
+
// ElevenLabs sends no end-of-turn event, and a model may send its "Now adding X" sentence long before the tool call
|
|
289
|
+
// that writes X. Settling 0.8 s after text alone ended turns early and the late tool call was thrown away.
|
|
290
|
+
const TEXT_SETTLE_MS = 6000;
|
|
291
|
+
const ANNOUNCEMENT_SETTLE_MS = 40000;
|
|
292
|
+
const ANNOUNCES_WORK = /(\bnow\b|\bnext\b|\bnext:|i'll|i will|let me|adding|writing|building|creating|updating|starting|:\s*$|…\s*$|\.\.\.\s*$)/i;
|
|
293
|
+
|
|
287
294
|
function settleSoon(conn) {
|
|
288
|
-
|
|
289
|
-
|
|
295
|
+
const pending = conn.pending;
|
|
296
|
+
clearTimeout(pending.settleTimer);
|
|
297
|
+
const lastLine = (pending.text || '').trim().split('\n').pop() || '';
|
|
298
|
+
// Announcements are short; a long text reply that merely mentions "next steps" is a finished answer.
|
|
299
|
+
const announces = (pending.text || '').length < 400 && ANNOUNCES_WORK.test(lastLine);
|
|
300
|
+
const wait = pending.toolCalls.length ? SETTLE_MS : announces ? ANNOUNCEMENT_SETTLE_MS : TEXT_SETTLE_MS;
|
|
301
|
+
pending.settleTimer = setTimeout(() => settle(conn, null), wait);
|
|
290
302
|
}
|
|
291
303
|
|
|
292
304
|
function settle(conn, failure) {
|
|
@@ -299,12 +311,72 @@ function settle(conn, failure) {
|
|
|
299
311
|
else pending.resolve({ text: pending.text, toolCalls: pending.toolCalls });
|
|
300
312
|
}
|
|
301
313
|
|
|
302
|
-
|
|
314
|
+
// Images reach ElevenLabs only as files uploaded to the conversation and referenced from a multimodal_message.
|
|
315
|
+
const MAX_FILES_PER_MESSAGE = 5;
|
|
316
|
+
|
|
317
|
+
// ElevenLabs refuses an 11th file in one conversation; the request loop answers this error with a fresh conversation.
|
|
318
|
+
const MAX_FILES_PER_CONVERSATION = 10;
|
|
319
|
+
const EYES_PROMPT = 'You are the eyes of a coding agent that cannot see these images. For each image, in order, describe everything '
|
|
320
|
+
+ 'it would need: all visible text verbatim (code, errors, labels, numbers), layout and positions, colours, UI state, and '
|
|
321
|
+
+ 'anything that looks wrong. Be exhaustive and factual; do not guess beyond what is visible.';
|
|
322
|
+
|
|
323
|
+
async function describeImages(llm, settings, images, question) {
|
|
324
|
+
const parts = [];
|
|
325
|
+
for (let start = 0; start < images.length; start += MAX_FILES_PER_CONVERSATION) {
|
|
326
|
+
const chunk = images.slice(start, start + MAX_FILES_PER_CONVERSATION);
|
|
327
|
+
const key = `eyes_${Date.now()}_${Math.random().toString(36).slice(2)}`;
|
|
328
|
+
const eyes = await withAgentSettings(settings, () => openConversation(key, { llm, prompt: EYES_PROMPT, toolIds: [] }));
|
|
329
|
+
try {
|
|
330
|
+
const answer = await sendTurn(eyes, {
|
|
331
|
+
text: `Describe these ${chunk.length} image(s). The agent's current task: ${question || '(not stated)'}`,
|
|
332
|
+
toolResults: [], images: chunk, history: []
|
|
333
|
+
});
|
|
334
|
+
parts.push(answer.text);
|
|
335
|
+
} finally {
|
|
336
|
+
eyes.ws.close();
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
console.log(`👁️ Described ${images.length} image(s) in a side conversation (main chat is at the 10-file limit)`);
|
|
340
|
+
return parts.join('\n\n');
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
async function uploadImages(conn, images) {
|
|
344
|
+
const fileIds = [];
|
|
345
|
+
for (const [index, image] of images.entries()) {
|
|
346
|
+
const extension = (image.mediaType.split('/')[1] || 'png').replace('jpeg', 'jpg');
|
|
347
|
+
const form = new FormData();
|
|
348
|
+
form.append('file', new Blob([Buffer.from(image.data, 'base64')], { type: image.mediaType }), `image-${index + 1}.${extension}`);
|
|
349
|
+
const response = await fetch(`${API_BASE}/conversations/${conn.conversationId}/files`, { method: 'POST', headers: HEADERS, body: form });
|
|
350
|
+
if (!response.ok) throw new Error(`Image upload failed (${response.status}): ${(await response.text()).slice(0, 200)}`);
|
|
351
|
+
fileIds.push((await response.json()).file_id);
|
|
352
|
+
conn.filesUploaded += 1;
|
|
353
|
+
}
|
|
354
|
+
return fileIds;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
function sendMultimodal(conn, text, fileIds) {
|
|
358
|
+
for (let start = 0; start < fileIds.length; start += MAX_FILES_PER_MESSAGE) {
|
|
359
|
+
conn.ws.send(JSON.stringify({
|
|
360
|
+
type: 'multimodal_message',
|
|
361
|
+
text: { type: 'user_message', text: start === 0 ? text : 'More images:', source_medium: 'image' },
|
|
362
|
+
files: fileIds.slice(start, start + MAX_FILES_PER_MESSAGE).map(fileId => ({ type: 'file_input', file_id: fileId }))
|
|
363
|
+
}));
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
function sendTurn(conn, { text, toolResults, history, images = [] }) {
|
|
303
368
|
if (conn.pending) throw new Error('A request is already active for this conversation');
|
|
304
369
|
const requestId = String(++conn.messageId);
|
|
305
370
|
|
|
306
|
-
return new Promise((resolve, reject) => {
|
|
371
|
+
return new Promise(async (resolve, reject) => {
|
|
307
372
|
conn.pending = { resolve, reject, text: '', toolCalls: [], settleTimer: null };
|
|
373
|
+
let fileIds = [];
|
|
374
|
+
try {
|
|
375
|
+
fileIds = images.length ? await uploadImages(conn, images) : [];
|
|
376
|
+
} catch (error) {
|
|
377
|
+
settle(conn, { error });
|
|
378
|
+
return;
|
|
379
|
+
}
|
|
308
380
|
conn.pending.timeout = setTimeout(
|
|
309
381
|
() => settle(conn, { error: new Error(`No reply from ${conn.llm} within ${REQUEST_TIMEOUT_MS / 1000}s`) }),
|
|
310
382
|
REQUEST_TIMEOUT_MS
|
|
@@ -326,11 +398,12 @@ function sendTurn(conn, { text, toolResults, history }) {
|
|
|
326
398
|
conn.ws.send(JSON.stringify({
|
|
327
399
|
type: 'client_tool_result',
|
|
328
400
|
tool_call_id: result.toolUseId,
|
|
329
|
-
result: result.content,
|
|
401
|
+
result: fileIds.length ? `${result.content}\n[The image output of this tool follows in the next message.]` : result.content,
|
|
330
402
|
is_error: result.isError
|
|
331
403
|
}));
|
|
332
404
|
}
|
|
333
405
|
if (text) conn.ws.send(JSON.stringify({ type: 'contextual_update', text }));
|
|
406
|
+
if (fileIds.length) sendMultimodal(conn, 'Image output of the tool call above.', fileIds);
|
|
334
407
|
return;
|
|
335
408
|
}
|
|
336
409
|
|
|
@@ -339,11 +412,9 @@ function sendTurn(conn, { text, toolResults, history }) {
|
|
|
339
412
|
const resultText = toolResults
|
|
340
413
|
.map(result => `${result.isError ? 'Tool error' : 'Tool result'} (${result.toolUseId}):\n${result.content}`)
|
|
341
414
|
.join('\n\n');
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
text: [resultText, text, resultText ? 'Continue the task from here.' : ''].filter(Boolean).join('\n\n')
|
|
346
|
-
}));
|
|
415
|
+
const turnText = [resultText, text, resultText ? 'Continue the task from here.' : ''].filter(Boolean).join('\n\n');
|
|
416
|
+
if (fileIds.length) sendMultimodal(conn, turnText || 'See the attached image.', fileIds);
|
|
417
|
+
else conn.ws.send(JSON.stringify({ type: 'user_message', id: requestId, text: turnText }));
|
|
347
418
|
});
|
|
348
419
|
}
|
|
349
420
|
|
|
@@ -376,7 +447,7 @@ app.post('/v1/messages', async (req, res) => {
|
|
|
376
447
|
|
|
377
448
|
try {
|
|
378
449
|
const latest = extractLatestUserContent(messages);
|
|
379
|
-
if (!latest.text && latest.toolResults.length === 0) {
|
|
450
|
+
if (!latest.text && latest.toolResults.length === 0 && latest.images.length === 0) {
|
|
380
451
|
return anthropicError(res, 400, 'invalid_request_error', 'No text or tool result in the latest user message');
|
|
381
452
|
}
|
|
382
453
|
|
|
@@ -397,8 +468,15 @@ app.post('/v1/messages', async (req, res) => {
|
|
|
397
468
|
conn = existing && existing.ws.readyState === WebSocket.OPEN
|
|
398
469
|
? existing
|
|
399
470
|
: await withAgentSettings(settings, () => openConversation(key, { llm, prompt: `${systemText(system) || 'You are a helpful assistant.'}${BRIDGE_NOTE}`, toolIds }));
|
|
471
|
+
// Past the 10-file limit the main conversation stays put: a side conversation looks at the new images and
|
|
472
|
+
// its description enters the same chat as text, so no context is lost to a fresh conversation.
|
|
473
|
+
if (latest.images.length && conn.filesUploaded + latest.images.length > MAX_FILES_PER_CONVERSATION) {
|
|
474
|
+
const description = await describeImages(llm, settings, latest.images, latest.text);
|
|
475
|
+
latest.text = `${latest.text}\n\n[Image descriptions. This chat already holds ElevenLabs' maximum of ${MAX_FILES_PER_CONVERSATION} images, so a vision pass on the same model described the new ones:]\n${description}`;
|
|
476
|
+
latest.images = [];
|
|
477
|
+
}
|
|
400
478
|
try {
|
|
401
|
-
result = await sendTurn(conn, { text: latest.text, toolResults: latest.toolResults, history: messages.slice(0, -1) });
|
|
479
|
+
result = await sendTurn(conn, { text: latest.text, toolResults: latest.toolResults, images: latest.images, history: messages.slice(0, -1) });
|
|
402
480
|
break;
|
|
403
481
|
} catch (error) {
|
|
404
482
|
if (!/Cascade Error: TimeoutError|took too long/.test(error.message) || !lowerEffortCap(llm, settings.reasoning_effort)) throw error;
|
package/src/setup.js
CHANGED
|
@@ -46,7 +46,7 @@ async function createBridgeAgent(headers) {
|
|
|
46
46
|
backup_llm_config: { preference: 'disabled' }
|
|
47
47
|
}
|
|
48
48
|
},
|
|
49
|
-
conversation: { text_only: true, max_duration_seconds: 7200 }
|
|
49
|
+
conversation: { text_only: true, max_duration_seconds: 7200, file_input: { enabled: true } }
|
|
50
50
|
},
|
|
51
51
|
platform_settings: {
|
|
52
52
|
// Post-conversation analysis adds nothing for a coding bridge.
|