labs-claude 0.1.6 → 0.1.7

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.
Files changed (2) hide show
  1. package/package.json +22 -6
  2. package/src/proxy.js +52 -44
package/package.json CHANGED
@@ -1,12 +1,28 @@
1
1
  {
2
2
  "name": "labs-claude",
3
- "version": "0.1.6",
3
+ "version": "0.1.7",
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
- "bin": { "labs": "bin/labs.js" },
6
- "files": ["bin", "src", "README.md"],
7
- "scripts": { "test": "node --test" },
8
- "engines": { "node": ">=18" },
9
- "keywords": ["claude-code", "elevenlabs", "proxy", "anthropic", "cli"],
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
+ ],
10
26
  "license": "MIT",
11
27
  "dependencies": {
12
28
  "axios": "^1.20.0",
package/src/proxy.js CHANGED
@@ -188,13 +188,16 @@ function openConversation(key, { llm, prompt, toolIds }) {
188
188
  ws.on('error', error => {
189
189
  console.error(`🚨 WS error for ${key}: ${error.message}`);
190
190
  reject(error);
191
+ if (conn.pending) settle(conn, { error });
192
+ if (connections.get(key) === conn) connections.delete(key);
193
+ ws.terminate();
191
194
  });
192
195
  ws.on('close', (code, reason) => {
193
196
  conn.closeReason = `code ${code}${reason?.length ? `: ${reason}` : ''}`;
194
197
  console.log(`🔌 Conversation closed for ${key} (${conn.closeReason})`);
195
198
  reject(new Error(`ElevenLabs closed the conversation (${conn.closeReason})`));
196
199
  if (conn.pending) settle(conn, { error: new Error(`ElevenLabs closed the conversation (${conn.closeReason})`) });
197
- connections.delete(key);
200
+ if (connections.get(key) === conn) connections.delete(key);
198
201
  });
199
202
  });
200
203
  conn.ready.catch(() => {});
@@ -369,53 +372,58 @@ function sendTurn(conn, { text, toolResults, history, images = [] }) {
369
372
  if (conn.pending) throw new Error('A request is already active for this conversation');
370
373
  const requestId = String(++conn.messageId);
371
374
 
372
- return new Promise(async (resolve, reject) => {
373
- conn.pending = { resolve, reject, text: '', toolCalls: [], settleTimer: null };
374
- let fileIds = [];
375
- try {
376
- fileIds = images.length ? await uploadImages(conn, images) : [];
377
- } catch (error) {
378
- settle(conn, { error });
379
- return;
380
- }
381
- conn.pending.timeout = setTimeout(
382
- () => settle(conn, { error: new Error(`No reply from ${conn.llm} within ${REQUEST_TIMEOUT_MS / 1000}s`) }),
383
- REQUEST_TIMEOUT_MS
384
- );
385
-
386
- // Claude Code resends the whole thread every turn, so a fresh conversation (reconnect, /model switch,
387
- // --resume, compaction) is seeded from what Claude Code sent.
388
- if (conn.messageId === 1 && history.length) {
389
- conn.ws.send(JSON.stringify({
390
- type: 'contextual_update',
391
- text: `Conversation so far:\n${formatConversationHistory(history).slice(-MAX_REPLAY_CHARS)}`
392
- }));
393
- }
394
-
395
- const known = toolResults.filter(result => conn.openToolCallIds.has(result.toolUseId));
396
- if (toolResults.length && known.length === toolResults.length) {
397
- for (const result of toolResults) {
398
- conn.openToolCallIds.delete(result.toolUseId);
375
+ return new Promise((resolve, reject) => {
376
+ const pending = { resolve, reject, text: '', toolCalls: [], settleTimer: null };
377
+ conn.pending = pending;
378
+ pending.timeout = setTimeout(() => {
379
+ if (conn.pending !== pending) return;
380
+ settle(conn, { error: new Error(`No reply from ${conn.llm} within ${REQUEST_TIMEOUT_MS / 1000}s`) });
381
+ conn.ws.terminate();
382
+ }, REQUEST_TIMEOUT_MS);
383
+ const send = async () => {
384
+ if (conn.ws.readyState !== WebSocket.OPEN) throw new Error('Conversation socket is not open');
385
+ const fileIds = images.length ? await uploadImages(conn, images) : [];
386
+ // A close or timeout can settle this turn while the upload is in flight.
387
+ if (conn.pending !== pending) return;
388
+ if (conn.ws.readyState !== WebSocket.OPEN) throw new Error('Conversation socket is not open');
389
+
390
+ // Claude Code resends the whole thread every turn, so a fresh conversation (reconnect, /model switch,
391
+ // --resume, compaction) is seeded from what Claude Code sent.
392
+ if (conn.messageId === 1 && history.length) {
399
393
  conn.ws.send(JSON.stringify({
400
- type: 'client_tool_result',
401
- tool_call_id: result.toolUseId,
402
- result: fileIds.length ? `${result.content}\n[The image output of this tool follows in the next message.]` : result.content,
403
- is_error: result.isError
394
+ type: 'contextual_update',
395
+ text: `Conversation so far:\n${formatConversationHistory(history).slice(-MAX_REPLAY_CHARS)}`
404
396
  }));
405
397
  }
406
- if (text) conn.ws.send(JSON.stringify({ type: 'contextual_update', text }));
407
- if (fileIds.length) sendMultimodal(conn, 'Image output of the tool call above.', fileIds);
408
- return;
409
- }
410
398
 
411
- // Results for calls this conversation never made (it was reopened) go in as text, otherwise the agent
412
- // waits on tool_call_ids it does not know and never answers.
413
- const resultText = toolResults
414
- .map(result => `${result.isError ? 'Tool error' : 'Tool result'} (${result.toolUseId}):\n${result.content}`)
415
- .join('\n\n');
416
- const turnText = [resultText, text, resultText ? 'Continue the task from here.' : ''].filter(Boolean).join('\n\n');
417
- if (fileIds.length) sendMultimodal(conn, turnText || 'See the attached image.', fileIds);
418
- else conn.ws.send(JSON.stringify({ type: 'user_message', id: requestId, text: turnText }));
399
+ const known = toolResults.filter(result => conn.openToolCallIds.has(result.toolUseId));
400
+ if (toolResults.length && known.length === toolResults.length) {
401
+ for (const result of toolResults) {
402
+ conn.openToolCallIds.delete(result.toolUseId);
403
+ conn.ws.send(JSON.stringify({
404
+ type: 'client_tool_result',
405
+ tool_call_id: result.toolUseId,
406
+ result: fileIds.length ? `${result.content}\n[The image output of this tool follows in the next message.]` : result.content,
407
+ is_error: result.isError
408
+ }));
409
+ }
410
+ if (text) conn.ws.send(JSON.stringify({ type: 'contextual_update', text }));
411
+ if (fileIds.length) sendMultimodal(conn, 'Image output of the tool call above.', fileIds);
412
+ return;
413
+ }
414
+
415
+ // Results for calls this conversation never made (it was reopened) go in as text, otherwise the agent
416
+ // waits on tool_call_ids it does not know and never answers.
417
+ const resultText = toolResults
418
+ .map(result => `${result.isError ? 'Tool error' : 'Tool result'} (${result.toolUseId}):\n${result.content}`)
419
+ .join('\n\n');
420
+ const turnText = [resultText, text, resultText ? 'Continue the task from here.' : ''].filter(Boolean).join('\n\n');
421
+ if (fileIds.length) sendMultimodal(conn, turnText || 'See the attached image.', fileIds);
422
+ else conn.ws.send(JSON.stringify({ type: 'user_message', id: requestId, text: turnText }));
423
+ };
424
+ send().catch(error => {
425
+ if (conn.pending === pending) settle(conn, { error });
426
+ });
419
427
  });
420
428
  }
421
429