diff --git a/utils/anthropic/client.ts b/utils/anthropic/client.ts index e88e9f1..af4c623 100644 --- a/utils/anthropic/client.ts +++ b/utils/anthropic/client.ts @@ -1,5 +1,5 @@ import Anthropic from '@anthropic-ai/sdk'; -import { BaseTool } from './tool'; +import { BaseTool, ToolUseBlock } from './tool'; export async function getMessage(text: string, tool: BaseTool) { const anthropic = new Anthropic({ @@ -9,7 +9,7 @@ export async function getMessage(text: string, tool: BaseTool) { console.info('Anthropic request with text: ', text); const response = await anthropic.messages.create({ - model: 'claude-3-5-sonnet-20241022', + model: 'claude-sonnet-4-0', max_tokens: 2048, messages: [{ role: 'user', content: text }], tools: [tool] @@ -18,12 +18,15 @@ export async function getMessage(text: string, tool: BaseTool) { console.info('Anthropic response: ', response); try { - const data = response.content as [ - { type: string; text: string }, - { type: string; input: object } - ]; + const content = response.content; - return data[1].input as T; + const toolUse = content.find((block): block is ToolUseBlock => block.type === 'tool_use'); + + if (!toolUse) { + throw new Error('No tool_use block found in response'); + } + + return toolUse.input as T; } catch (error) { throw Error(JSON.stringify(error)); } diff --git a/utils/anthropic/tool.ts b/utils/anthropic/tool.ts index e47ed6d..77cff02 100644 --- a/utils/anthropic/tool.ts +++ b/utils/anthropic/tool.ts @@ -8,6 +8,13 @@ export interface BaseTool { }; } +export type ToolUseBlock = { + type: 'tool_use'; + id: string; + name: string; + input: Record; +}; + export interface NewsletterTool { title: string; content: string;