fix: update claude version with response handling correction
This commit is contained in:
@@ -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));
|
||||
}
|
||||
|
||||
@@ -8,6 +8,13 @@ export interface BaseTool {
|
||||
};
|
||||
}
|
||||
|
||||
export type ToolUseBlock = {
|
||||
type: 'tool_use';
|
||||
id: string;
|
||||
name: string;
|
||||
input: Record<string, any>;
|
||||
};
|
||||
|
||||
export interface NewsletterTool {
|
||||
title: string;
|
||||
content: string;
|
||||
|
||||
Reference in New Issue
Block a user