fix: update claude version with response handling correction

This commit is contained in:
2025-09-28 17:02:33 +08:00
parent 271fa45937
commit 905054e3b5
2 changed files with 17 additions and 7 deletions

View File

@@ -1,5 +1,5 @@
import Anthropic from '@anthropic-ai/sdk';
import { BaseTool } from './tool';
import { BaseTool, ToolUseBlock } from './tool';
export async function getMessage<T>(text: string, tool: BaseTool) {
const anthropic = new Anthropic({
@@ -9,7 +9,7 @@ export async function getMessage<T>(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<T>(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));
}