Fixed tool calls
All checks were successful
Publish Library / Build NPM Project (push) Successful in 39s
Publish Library / Tag Version (push) Successful in 8s

This commit is contained in:
2025-12-27 17:27:53 -05:00
parent 98dd0bb323
commit 6dce0e8954
6 changed files with 29 additions and 15 deletions

View File

@@ -54,12 +54,14 @@ export class Anthropic extends LLMProvider {
let history = this.fromStandard([...options.history || [], {role: 'user', content: message, timestamp: Date.now()}]);
const original = deepCopy(history);
if(options.compress) history = await this.ai.language.compressHistory(<any>history, options.compress.max, options.compress.min, options);
const tools = options.tools || this.ai.options.tools || [];
const requestParams: any = {
model: options.model || this.model,
max_tokens: options.max_tokens || this.ai.options.max_tokens || 4096,
system: options.system || this.ai.options.system || '',
temperature: options.temperature || this.ai.options.temperature || 0.7,
tools: (options.tools || this.ai.options.tools || []).map(t => ({
tools: tools.map(t => ({
name: t.name,
description: t.description,
input_schema: {
@@ -76,7 +78,10 @@ export class Anthropic extends LLMProvider {
let resp: any, isFirstMessage = true;
const assistantMessages: string[] = [];
do {
resp = await this.client.messages.create(requestParams);
resp = await this.client.messages.create(requestParams).catch(err => {
err.message += `\n\nMessages:\n${JSON.stringify(history, null, 2)}`;
throw err;
});
// Streaming mode
if(options.stream) {
@@ -114,7 +119,7 @@ export class Anthropic extends LLMProvider {
history.push({role: 'assistant', content: resp.content});
original.push({role: 'assistant', content: resp.content});
const results = await Promise.all(toolCalls.map(async (toolCall: any) => {
const tool = options.tools?.find(findByProp('name', toolCall.name));
const tool = tools.find(findByProp('name', toolCall.name));
if(!tool) return {tool_use_id: toolCall.id, is_error: true, content: 'Tool not found'};
try {
const result = await tool.fn(toolCall.input, this.ai);