From 99a04d2a3ed8ca654fb95a6ed27e1b054b2026e2 Mon Sep 17 00:00:00 2001 From: Riccardo Senica Date: Sun, 28 Sep 2025 17:16:29 +0800 Subject: [PATCH] fix: correct handling of claude response --- .gitignore | 1 + components/Content.tsx | 4 ++++ next-env.d.ts | 2 +- utils/anthropicClient.ts | 23 +++++++++++++++++------ 4 files changed, 23 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index 6a2c8f6..44999c1 100644 --- a/.gitignore +++ b/.gitignore @@ -133,3 +133,4 @@ dist .DS_Store .vercel +.env*.local diff --git a/components/Content.tsx b/components/Content.tsx index 9b46ccc..b82bd42 100644 --- a/components/Content.tsx +++ b/components/Content.tsx @@ -37,6 +37,7 @@ export const Content = () => { try { downloadJson(consumer, 'consumer.json'); } catch (err) { + console.error('Failed to download consumer data', err); showToast('Failed to download consumer data'); } }; @@ -46,6 +47,7 @@ export const Content = () => { try { downloadJson(purchasesResult, 'purchases.json'); } catch (err) { + console.error('Failed to download purchase history', err); showToast('Failed to download purchase history'); } }; @@ -69,6 +71,7 @@ export const Content = () => { setConsumer(data.consumer); setEditedConsumer(JSON.stringify(data.consumer, null, 2)); } catch (err) { + console.error('Something went wrong', err); if (axios.isAxiosError(err)) { const errorMessage = err.response?.data?.error || err.message; showToast(errorMessage); @@ -119,6 +122,7 @@ export const Content = () => { setPurchasesResult(data); } catch (err) { + console.error('Something went wrong', err); if (axios.isAxiosError(err)) { const errorMessage = err.response?.data?.error || err.message; showToast(errorMessage); diff --git a/next-env.d.ts b/next-env.d.ts index 40c3d68..1b3be08 100644 --- a/next-env.d.ts +++ b/next-env.d.ts @@ -2,4 +2,4 @@ /// // NOTE: This file should not be edited -// see https://nextjs.org/docs/app/building-your-application/configuring/typescript for more information. +// see https://nextjs.org/docs/app/api-reference/config/typescript for more information. diff --git a/utils/anthropicClient.ts b/utils/anthropicClient.ts index 0102bcd..eb7f505 100644 --- a/utils/anthropicClient.ts +++ b/utils/anthropicClient.ts @@ -11,6 +11,13 @@ export interface BaseTool { }; } +export type ToolUseBlock = { + type: 'tool_use'; + id: string; + name: string; + input: Record; +}; + export async function makeRequest(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(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(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.'); } }