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

1
.gitignore vendored
View File

@@ -133,3 +133,4 @@ dist
.DS_Store .DS_Store
.vercel .vercel
.env*.local

View File

@@ -37,6 +37,7 @@ export const Content = () => {
try { try {
downloadJson(consumer, 'consumer.json'); downloadJson(consumer, 'consumer.json');
} catch (err) { } catch (err) {
console.error('Failed to download consumer data', err);
showToast('Failed to download consumer data'); showToast('Failed to download consumer data');
} }
}; };
@@ -46,6 +47,7 @@ export const Content = () => {
try { try {
downloadJson(purchasesResult, 'purchases.json'); downloadJson(purchasesResult, 'purchases.json');
} catch (err) { } catch (err) {
console.error('Failed to download purchase history', err);
showToast('Failed to download purchase history'); showToast('Failed to download purchase history');
} }
}; };
@@ -69,6 +71,7 @@ export const Content = () => {
setConsumer(data.consumer); setConsumer(data.consumer);
setEditedConsumer(JSON.stringify(data.consumer, null, 2)); setEditedConsumer(JSON.stringify(data.consumer, null, 2));
} catch (err) { } catch (err) {
console.error('Something went wrong', err);
if (axios.isAxiosError(err)) { if (axios.isAxiosError(err)) {
const errorMessage = err.response?.data?.error || err.message; const errorMessage = err.response?.data?.error || err.message;
showToast(errorMessage); showToast(errorMessage);
@@ -119,6 +122,7 @@ export const Content = () => {
setPurchasesResult(data); setPurchasesResult(data);
} catch (err) { } catch (err) {
console.error('Something went wrong', err);
if (axios.isAxiosError(err)) { if (axios.isAxiosError(err)) {
const errorMessage = err.response?.data?.error || err.message; const errorMessage = err.response?.data?.error || err.message;
showToast(errorMessage); showToast(errorMessage);

2
next-env.d.ts vendored
View File

@@ -2,4 +2,4 @@
/// <reference types="next/image-types/global" /> /// <reference types="next/image-types/global" />
// NOTE: This file should not be edited // 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.

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