fix: correct handling of claude response

This commit is contained in:
2025-09-28 17:16:29 +08:00
parent 3f64b70e18
commit 99a04d2a3e
4 changed files with 23 additions and 7 deletions

View File

@@ -11,6 +11,13 @@ export interface BaseTool {
};
}
export type ToolUseBlock = {
type: 'tool_use';
id: string;
name: string;
input: Record<string, any>;
};
export async function makeRequest<T extends BaseTool>(prompt: string, tool: T) {
if (!process.env.ANTHROPIC_API_KEY) {
throw Error('No Anthropic API key found.');
@@ -20,7 +27,7 @@ export async function makeRequest<T extends BaseTool>(prompt: string, tool: T) {
try {
const response = await client.messages.create({
model: 'claude-sonnet-4-20250514',
model: 'claude-sonnet-4-0',
max_tokens: 8192,
temperature: 1,
tools: [tool],
@@ -35,13 +42,17 @@ export async function makeRequest<T extends BaseTool>(prompt: string, tool: T) {
throw Error(JSON.stringify(response));
}
const content = response.content as [
{ type: string; text: string },
{ type: string; input: object }
];
const content = response.content;
return content[1].input;
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;
} catch (error) {
console.error('Error making request:', error);
throw Error('Anthropic client error.');
}
}