diff --git a/memento-note/app/api/ai/tags/route.ts b/memento-note/app/api/ai/tags/route.ts index bf12adf..bfc47a0 100644 --- a/memento-note/app/api/ai/tags/route.ts +++ b/memento-note/app/api/ai/tags/route.ts @@ -31,6 +31,7 @@ export async function POST(req: NextRequest) { // If notebookId is provided, use contextual suggestions (IA2) if (notebookId) { try { + console.log('[/api/ai/tags] contextual request — notebookId:', notebookId, 'content length:', content.length) const suggestions = await contextualAutoTagService.suggestLabels( content, notebookId, @@ -38,6 +39,8 @@ export async function POST(req: NextRequest) { language ); + console.log('[/api/ai/tags] suggestions:', suggestions.length, suggestions.map(s => `${s.label}(${s.confidence})`)) + const convertedTags = suggestions.map(s => ({ tag: s.label, confidence: s.confidence, diff --git a/memento-note/lib/ai/services/contextual-auto-tag.service.ts b/memento-note/lib/ai/services/contextual-auto-tag.service.ts index dcf2713..d1d60b2 100644 --- a/memento-note/lib/ai/services/contextual-auto-tag.service.ts +++ b/memento-note/lib/ai/services/contextual-auto-tag.service.ts @@ -50,12 +50,16 @@ export class ContextualAutoTagService { }) if (!notebook) { + console.log('[ContextualAutoTag] notebook not found for id:', notebookId) return [] } + console.log('[ContextualAutoTag] notebook:', notebook.name, 'labels:', notebook.labels.map((l: any) => l.name)) + // CASE 1: Notebook has existing labels → suggest from them (IA2) if (notebook.labels.length > 0) { const existing = await this.suggestFromExistingLabels(noteContent, notebook, language) + console.log('[ContextualAutoTag] existing label suggestions:', existing.length) if (existing.length > 0) return existing // Fallback: no existing label matched → suggest new ones too } @@ -83,6 +87,7 @@ export class ContextualAutoTagService { // Use generateText with JSON response const response = await provider.generateText(prompt) + console.log('[ContextualAutoTag] AI raw response (existing):', response?.substring(0, 300)) // Improved JSON parsing with multiple fallback strategies let parsed: any @@ -111,7 +116,7 @@ export class ContextualAutoTagService { cleanedJson = cleanedJson.replace(/([{,]\s*)([a-zA-Z_][a-zA-Z0-9_]*)\s*:/g, '$1"$2":') parsed = JSON.parse(cleanedJson) } else { - console.error('❌ Could not extract JSON from response') + console.error('[ContextualAutoTag] Could not extract JSON from response') return [] } } @@ -124,7 +129,7 @@ export class ContextualAutoTagService { } else if (Array.isArray(parsed)) { suggestionsArray = parsed } else { - console.error('❌ Invalid response structure:', parsed) + console.error('[ContextualAutoTag] Invalid response structure:', parsed) return [] } @@ -146,6 +151,7 @@ export class ContextualAutoTagService { .sort((a: any, b: any) => b.confidence - a.confidence) .slice(0, 5) + console.log('[ContextualAutoTag] filtered existing suggestions:', suggestions.length, suggestions.map((s: any) => `${s.label}(${s.confidence})`)) return suggestions as LabelSuggestion[] } catch (error) { console.error('Failed to suggest labels:', error) @@ -170,6 +176,7 @@ export class ContextualAutoTagService { // Use generateText with JSON response const response = await provider.generateText(prompt) + console.log('[ContextualAutoTag] AI raw response (new):', response?.substring(0, 300)) // Improved JSON parsing with multiple fallback strategies let parsed: any @@ -198,7 +205,7 @@ export class ContextualAutoTagService { cleanedJson = cleanedJson.replace(/([{,]\s*)([a-zA-Z_][a-zA-Z0-9_]*)\s*:/g, '$1"$2":') parsed = JSON.parse(cleanedJson) } else { - console.error('❌ Could not extract JSON from response') + console.error('[ContextualAutoTag] Could not extract JSON from new-labels response') return [] } } @@ -211,7 +218,7 @@ export class ContextualAutoTagService { } else if (Array.isArray(parsed)) { suggestionsArray = parsed } else { - console.error('❌ Invalid response structure:', parsed) + console.error('[ContextualAutoTag] Invalid new-labels response structure:', parsed) return [] } @@ -229,6 +236,7 @@ export class ContextualAutoTagService { .sort((a: any, b: any) => b.confidence - a.confidence) .slice(0, 5) + console.log('[ContextualAutoTag] new label suggestions:', suggestions.length, suggestions.map((s: any) => `${s.label}(${s.confidence})`)) return suggestions as LabelSuggestion[] } catch (error) { console.error('❌ Failed to suggest new labels:', error)