labs-claude 0.1.5 → 0.1.6

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "labs-claude",
3
- "version": "0.1.5",
3
+ "version": "0.1.6",
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"],
@@ -104,6 +104,16 @@ function createSessionKey(agentId, sessionId) {
104
104
  return `${encodeURIComponent(agentId)}:${sessionId}`;
105
105
  }
106
106
 
107
+ // Identifies an assistant reply as Claude Code echoes it back in the next request, so that request can find the one
108
+ // ElevenLabs conversation whose memory ends with exactly that reply. Tool-use ids are unique; text is normalised.
109
+ function replySignature(content) {
110
+ const blocks = Array.isArray(content) ? content : [{ type: 'text', text: String(content || '') }];
111
+ const toolIds = blocks.filter(block => block.type === 'tool_use').map(block => block.id);
112
+ if (toolIds.length) return `tools:${toolIds.join(',')}`;
113
+ const text = blocks.filter(block => block.type === 'text').map(block => block.text || '').join(' ').replace(/\s+/g, ' ').trim();
114
+ return `text:${createHash('sha1').update(text).digest('hex')}`;
115
+ }
116
+
107
117
  // The main thread and each subagent share one Claude Code session id but start from different first messages.
108
118
  function createThreadId(sessionId, messages) {
109
119
  const opening = contentToText(messages[0]?.content).slice(0, 4000);
@@ -213,6 +223,7 @@ module.exports = {
213
223
  createSessionKey,
214
224
  createThreadId,
215
225
  extractLatestUserContent,
226
+ replySignature,
216
227
  formatConversationHistory,
217
228
  isValidSessionId,
218
229
  resolveLlm,
package/src/proxy.js CHANGED
@@ -11,6 +11,7 @@ const {
11
11
  createAnthropicMessage,
12
12
  createThreadId,
13
13
  extractLatestUserContent,
14
+ replySignature,
14
15
  formatConversationHistory,
15
16
  isValidSessionId,
16
17
  resolveLlm,
@@ -418,6 +419,23 @@ function sendTurn(conn, { text, toolResults, history, images = [] }) {
418
419
  });
419
420
  }
420
421
 
422
+ // Forks and parallel subagents share the main thread's history, so a key built from that history put them all on one
423
+ // ElevenLabs conversation ("A request is already active"). A request now continues only the free conversation whose
424
+ // last reply is the one the request ends with; anything else gets its own conversation, seeded from its history.
425
+ let conversationCounter = 0;
426
+
427
+ function claimContinuingConversation(prefix, priorReply) {
428
+ if (!priorReply) return null;
429
+ for (const [key, candidate] of connections) {
430
+ if (key.startsWith(prefix) && candidate.lastReply === priorReply && !candidate.claimed && !candidate.pending
431
+ && candidate.ws.readyState === WebSocket.OPEN) {
432
+ candidate.claimed = true;
433
+ return candidate;
434
+ }
435
+ }
436
+ return null;
437
+ }
438
+
421
439
  function saveConversation(threadId, llm, conversationId, messages) {
422
440
  if (!fs.existsSync(CONVERSATIONS_DIR)) fs.mkdirSync(CONVERSATIONS_DIR, { recursive: true });
423
441
  fs.writeFileSync(path.join(CONVERSATIONS_DIR, `${threadId}.json`),
@@ -457,17 +475,21 @@ app.post('/v1/messages', async (req, res) => {
457
475
  const threadId = isValidSessionId(sessionId) ? createThreadId(sessionId, messages) : createThreadId('anon', messages);
458
476
  const toolIds = isSideRequest ? [] : await toolRegistry.ensure(tools.filter(isForwardedTool));
459
477
  const toolSetHash = createHash('sha1').update(toolIds.join(',')).digest('hex').slice(0, 8);
478
+ const prior = messages[messages.length - 2];
479
+ const priorReply = prior?.role === 'assistant' ? replySignature(prior.content) : null;
460
480
  let conn;
461
481
  let result;
462
482
  for (;;) {
463
483
  const settings = agentSettingsFor(llm, outputConfig?.effort || 'high', maxTokens);
464
- const key = isSideRequest
465
- ? `side_${Date.now()}_${Math.random().toString(36).slice(2)}`
466
- : `${llm}:${settings.reasoning_effort}:${threadId}:${toolSetHash}`;
467
- const existing = connections.get(key);
468
- conn = existing && existing.ws.readyState === WebSocket.OPEN
469
- ? existing
470
- : await withAgentSettings(settings, () => openConversation(key, { llm, prompt: `${systemText(system) || 'You are a helpful assistant.'}${BRIDGE_NOTE}`, toolIds }));
484
+ const prefix = `${llm}:${settings.reasoning_effort}:${toolSetHash}:`;
485
+ conn = isSideRequest ? null : claimContinuingConversation(prefix, priorReply);
486
+ if (!conn) {
487
+ const key = isSideRequest
488
+ ? `side_${Date.now()}_${Math.random().toString(36).slice(2)}`
489
+ : `${prefix}${threadId}:${++conversationCounter}`;
490
+ conn = await withAgentSettings(settings, () => openConversation(key, { llm, prompt: `${systemText(system) || 'You are a helpful assistant.'}${BRIDGE_NOTE}`, toolIds }));
491
+ conn.claimed = true;
492
+ }
471
493
  // Past the 10-file limit the main conversation stays put: a side conversation looks at the new images and
472
494
  // its description enters the same chat as text, so no context is lost to a fresh conversation.
473
495
  if (latest.images.length && conn.filesUploaded + latest.images.length > MAX_FILES_PER_CONVERSATION) {
@@ -477,8 +499,10 @@ app.post('/v1/messages', async (req, res) => {
477
499
  }
478
500
  try {
479
501
  result = await sendTurn(conn, { text: latest.text, toolResults: latest.toolResults, images: latest.images, history: messages.slice(0, -1) });
502
+ conn.claimed = false;
480
503
  break;
481
504
  } catch (error) {
505
+ conn.claimed = false;
482
506
  if (!/Cascade Error: TimeoutError|took too long/.test(error.message) || !lowerEffortCap(llm, settings.reasoning_effort)) throw error;
483
507
  console.log(`🎚️ ${llm} too slow at effort=${settings.reasoning_effort}; retrying at ${effortCaps.get(llm)}`);
484
508
  }
@@ -498,6 +522,7 @@ app.post('/v1/messages', async (req, res) => {
498
522
  inputTokens: conn.contextTokens || Math.ceil(requestChars / 4)
499
523
  });
500
524
 
525
+ conn.lastReply = replySignature(message.content);
501
526
  if (isSideRequest) conn.ws.close();
502
527
  else saveConversation(threadId, llm, conn.conversationId, [...messages, { role: 'assistant', content: message.content }]);
503
528