feat(models): add tool use support to AnthropicClient

This commit is contained in:
William Valentin
2026-02-05 17:44:00 -08:00
parent c96165fb2f
commit 36c1cfc768
2 changed files with 68 additions and 18 deletions
+14 -2
View File
@@ -1,4 +1,5 @@
import Anthropic from '@anthropic-ai/sdk';
import type { Message } from '@anthropic-ai/sdk/resources/messages/messages.js';
import type { ChatRequest, ChatResponse, ChatStreamEvent, ModelClient } from './types.js';
export interface AnthropicClientConfig {
@@ -23,7 +24,7 @@ export class AnthropicClient implements ModelClient {
}
async chat(request: ChatRequest): Promise<ChatResponse> {
const response = await this.client.messages.create({
const params: Record<string, unknown> = {
model: this.model,
max_tokens: request.maxTokens ?? this.defaultMaxTokens,
system: request.system,
@@ -31,11 +32,21 @@ export class AnthropicClient implements ModelClient {
role: m.role,
content: m.content,
})),
});
};
if (request.tools && request.tools.length > 0) {
params.tools = request.tools;
}
const response = await this.client.messages.create(params as unknown as Parameters<typeof this.client.messages.create>[0]) as Message;
const textContent = response.content.find((c) => c.type === 'text');
const content = textContent?.type === 'text' ? textContent.text : '';
const toolCalls = response.content
.filter((c): c is { type: 'tool_use'; id: string; name: string; input: unknown } => c.type === 'tool_use')
.map(c => ({ id: c.id, name: c.name, args: c.input }));
return {
content,
stopReason: response.stop_reason ?? 'end_turn',
@@ -43,6 +54,7 @@ export class AnthropicClient implements ModelClient {
inputTokens: response.usage.input_tokens,
outputTokens: response.usage.output_tokens,
},
...(toolCalls.length > 0 ? { toolCalls } : {}),
};
}