fix: update claude version with response handling correction
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
import Anthropic from '@anthropic-ai/sdk';
|
import Anthropic from '@anthropic-ai/sdk';
|
||||||
import { BaseTool } from './tool';
|
import { BaseTool, ToolUseBlock } from './tool';
|
||||||
|
|
||||||
export async function getMessage<T>(text: string, tool: BaseTool) {
|
export async function getMessage<T>(text: string, tool: BaseTool) {
|
||||||
const anthropic = new Anthropic({
|
const anthropic = new Anthropic({
|
||||||
@@ -9,7 +9,7 @@ export async function getMessage<T>(text: string, tool: BaseTool) {
|
|||||||
console.info('Anthropic request with text: ', text);
|
console.info('Anthropic request with text: ', text);
|
||||||
|
|
||||||
const response = await anthropic.messages.create({
|
const response = await anthropic.messages.create({
|
||||||
model: 'claude-3-5-sonnet-20241022',
|
model: 'claude-sonnet-4-0',
|
||||||
max_tokens: 2048,
|
max_tokens: 2048,
|
||||||
messages: [{ role: 'user', content: text }],
|
messages: [{ role: 'user', content: text }],
|
||||||
tools: [tool]
|
tools: [tool]
|
||||||
@@ -18,12 +18,15 @@ export async function getMessage<T>(text: string, tool: BaseTool) {
|
|||||||
console.info('Anthropic response: ', response);
|
console.info('Anthropic response: ', response);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const data = response.content as [
|
const content = response.content;
|
||||||
{ type: string; text: string },
|
|
||||||
{ type: string; input: object }
|
|
||||||
];
|
|
||||||
|
|
||||||
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) {
|
} catch (error) {
|
||||||
throw Error(JSON.stringify(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 {
|
export interface NewsletterTool {
|
||||||
title: string;
|
title: string;
|
||||||
content: string;
|
content: string;
|
||||||
|
|||||||
Reference in New Issue
Block a user