From 3aa4684923398308224acb7a4a15b906733e7c30 Mon Sep 17 00:00:00 2001 From: ztimson Date: Tue, 16 Dec 2025 13:07:03 -0500 Subject: [PATCH] Fixing message combination on anthropic --- package.json | 2 +- src/antrhopic.ts | 17 ++++++----------- 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/package.json b/package.json index b2180a0..1aa5821 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@ztimson/ai-utils", - "version": "0.1.19", + "version": "0.1.20", "description": "AI Utility library", "author": "Zak Timson", "license": "MIT", diff --git a/src/antrhopic.ts b/src/antrhopic.ts index c0ec2ec..cb70719 100644 --- a/src/antrhopic.ts +++ b/src/antrhopic.ts @@ -73,11 +73,11 @@ export class Anthropic extends LLMProvider { }; let resp: any; - const loopMessages: any[] = []; + const assistantMessages: string[] = []; do { resp = await this.client.messages.create(requestParams); if(options.stream) { - if(loopMessages.length) options.stream({text: '\n\n'}); + if(assistantMessages.length) options.stream({text: '\n\n'}); resp.content = []; for await (const chunk of resp) { if(controller.signal.aborted) break; @@ -104,7 +104,8 @@ export class Anthropic extends LLMProvider { } } - loopMessages.push({role: 'assistant', content: resp.content, timestamp: Date.now()}); + const textContent = resp.content.filter((c: any) => c.type == 'text').map((c: any) => c.text).join('\n\n'); + if(textContent) assistantMessages.push(textContent); const toolCalls = resp.content.filter((c: any) => c.type === 'tool_use'); if(toolCalls.length && !controller.signal.aborted) { history.push({role: 'assistant', content: resp.content}); @@ -118,18 +119,12 @@ export class Anthropic extends LLMProvider { return {type: 'tool_result', tool_use_id: toolCall.id, is_error: true, content: err?.message || err?.toString() || 'Unknown'}; } })); - const userMsg = {role: 'user', content: results}; - history.push(userMsg); - loopMessages.push({...userMsg, timestamp: Date.now()}); + history.push({role: 'user', content: results}); requestParams.messages = history; } } while (!controller.signal.aborted && resp.content.some((c: any) => c.type === 'tool_use')); - - const combinedContent = loopMessages.filter(m => m.role === 'assistant') - .map(m => m.content.filter((c: any) => c.type == 'text').map((c: any) => c.text).join('\n\n')) - .filter(c => c).join('\n\n'); if(options.stream) options.stream({done: true}); - res(this.toStandard([...history, {role: 'assistant', content: combinedContent, timestamp: Date.now()}])); + res(this.toStandard([...history, {role: 'assistant', content: assistantMessages.join('\n\n'), timestamp: Date.now()}])); }); return Object.assign(response, {abort: () => controller.abort()});