diff --git a/memento-note/app/api/ai/suggest-charts/route.ts b/memento-note/app/api/ai/suggest-charts/route.ts index 21874bf..b501331 100644 --- a/memento-note/app/api/ai/suggest-charts/route.ts +++ b/memento-note/app/api/ai/suggest-charts/route.ts @@ -32,21 +32,28 @@ interface SuggestChartsResponse { } export async function POST(req: Request) { + console.log('[suggest-charts] ===== REQUEST START =====') + // 1. Auth check const session = await auth() if (!session?.user?.id) { + console.error('[suggest-charts] NO SESSION') return new Response('Unauthorized', { status: 401 }) } const userId = session.user.id + console.log('[suggest-charts] userId:', userId) // 1.5 Quota check try { const sysConfigEarly = await getSystemConfig() const { usedByok: willUseByok } = await willUseByokForLane('suggest-charts', sysConfigEarly, userId) + console.log('[suggest-charts] BYOK:', willUseByok) if (!willUseByok) { await checkEntitlementOrThrow(userId, 'suggest_charts') + console.log('[suggest-charts] Quota OK') } } catch (err) { + console.error('[suggest-charts] QUOTA ERROR:', err) if (err instanceof QuotaExceededError) { return Response.json(err.toJSON(), { status: 402 }) } @@ -54,10 +61,24 @@ export async function POST(req: Request) { } // 2. Parse request body - const body = await req.json() as SuggestChartsRequest + let body: SuggestChartsRequest + try { + body = await req.json() + } catch (e) { + console.error('[suggest-charts] INVALID JSON:', e) + return Response.json({ + error: 'Invalid request', + suggestions: [], + analyzedText: '', + detectedData: '', + hasData: false, + } satisfies SuggestChartsResponse, { status: 400 }) + } const { content, selection, noteId } = body + console.log('[suggest-charts] contentLen:', content?.length, 'selectionLen:', selection?.length) if (!content || content.trim().length === 0) { + console.error('[suggest-charts] EMPTY CONTENT') return Response.json({ error: 'Content is required', suggestions: [], @@ -68,10 +89,26 @@ export async function POST(req: Request) { } const textToAnalyze = selection && selection.trim() ? selection.trim() : content.trim() + console.log('[suggest-charts] analyzeLen:', textToAnalyze.length, 'preview:', textToAnalyze.substring(0, 100)) // 3. Build AI context - const sysConfig = await getSystemConfig() - const { provider, model, timingInfo } = await resolveAiRouteWithTiming('suggest-charts', sysConfig) + let sysConfig, provider, model + try { + sysConfig = await getSystemConfig() + const result = await resolveAiRouteWithTiming('suggest-charts', sysConfig) + provider = result.provider + model = result.model + console.log('[suggest-charts] AI model:', model) + } catch (e) { + console.error('[suggest-charts] AI ROUTE ERROR:', e) + return Response.json({ + error: 'AI service unavailable: ' + (e instanceof Error ? e.message : String(e)), + suggestions: [], + analyzedText: textToAnalyze.substring(0, 100), + detectedData: '', + hasData: false, + } satisfies SuggestChartsResponse, { status: 500 }) + } try { // 4. Call AI to analyze and suggest - direct JSON response (no tool) @@ -206,12 +243,15 @@ Response format (COPY this structure): return Response.json(parsed satisfies SuggestChartsResponse) } catch (error) { - console.error('[suggest-charts] Error:', error) + console.error('[suggest-charts] ===== MAIN ERROR =====') + console.error('[suggest-charts] Error name:', error instanceof Error ? error.name : typeof error) + console.error('[suggest-charts] Error message:', error instanceof Error ? error.message : String(error)) + console.error('[suggest-charts] Error stack:', error instanceof Error ? error.stack : 'no stack') return Response.json({ - error: 'Failed to generate chart suggestions', + error: 'Failed: ' + (error instanceof Error ? error.message : String(error)), suggestions: [], - analyzedText: textToAnalyze.substring(0, 100), - detectedData: 'Error occurred during analysis', + analyzedText: textToAnalyze?.substring(0, 100) || '', + detectedData: 'Error occurred', hasData: false, } satisfies SuggestChartsResponse, { status: 500 }) } diff --git a/memento-note/components/chart-suggestions-dialog.tsx b/memento-note/components/chart-suggestions-dialog.tsx index 6bbad4e..52978f6 100644 --- a/memento-note/components/chart-suggestions-dialog.tsx +++ b/memento-note/components/chart-suggestions-dialog.tsx @@ -176,7 +176,14 @@ export function ChartSuggestionsDialog({
{response.error}
+{response.error}
+
+ analyzedText: {response.analyzedText}
+ {'\n'}detectedData: {response.detectedData}
+
+