From 5fbe98630b89463acf012b07e00f694cbb1cc276 Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Fri, 29 Aug 2025 11:00:56 +0200 Subject: [PATCH] feat(Index.tsx): enhance tag generation and search functionality --- src/pages/Index.tsx | 101 +++++++++++++++++++++++++++++--------------- 1 file changed, 67 insertions(+), 34 deletions(-) diff --git a/src/pages/Index.tsx b/src/pages/Index.tsx index 34e9aaf..bd15b26 100644 --- a/src/pages/Index.tsx +++ b/src/pages/Index.tsx @@ -97,6 +97,7 @@ const Index = () => { const [showDebugPanel, setShowDebugPanel] = useState(false); const [autoGenerateTags, setAutoGenerateTags] = useState(true); const [ollamaStatus, setOllamaStatus] = useState<'unknown' | 'online' | 'offline'>('unknown'); + const [includeTagsInSearch, setIncludeTagsInSearch] = useState(true); const { resolvedTheme, setTheme } = useTheme(); @@ -240,7 +241,7 @@ const Index = () => { }; // Generate tags using Ollama - const generateTags = async (content: string): Promise => { + const generateTags = async (content: string, noteIndex?: number): Promise => { try { const systemPrompt = `You are a helpful assistant that generates searchable tags for journal entries. @@ -256,9 +257,29 @@ Return ONLY a comma-separated list of tags, no other text. Example: golang, test Keep tags concise, use lowercase, and separate words with hyphens if needed.`; + // Get context from surrounding notes + let context = ''; + if (noteIndex !== undefined && noteCache.length > 0) { + const contextNotes = []; + const start = Math.max(0, noteIndex - 2); + const end = Math.min(noteCache.length, noteIndex + 3); + + for (let i = start; i < end; i++) { + if (i !== noteIndex) { + const note = noteCache[i]; + const date = new Date(note.epochTime).toLocaleDateString(); + contextNotes.push(`[${date}] ${note.content.substring(0, 200)}${note.content.length > 200 ? '...' : ''}`); + } + } + + if (contextNotes.length > 0) { + context = `\n\nContext from surrounding notes:\n${contextNotes.join('\n\n')}`; + } + } + const userPrompt = `Generate tags for this journal entry: -${content}`; +${content}${context}`; const response = await fetchWithTiming(`${OLLAMA_ENDPOINT}/api/generate`, { method: 'POST', @@ -448,7 +469,7 @@ ${content}`; let tags: string[] = currentNoteTags; if (autoGenerateTags && tags.length === 0) { addDebugInfo('Generating tags for new note...'); - tags = await generateTags(trimmedContent); + tags = await generateTags(trimmedContent, 0); // New note will be at index 0 } const document = { @@ -519,7 +540,8 @@ ${content}`; let tags = note.tags || []; if (autoGenerateTags && trimmedContent !== note.content) { addDebugInfo('Content changed, regenerating tags...'); - tags = await generateTags(trimmedContent); + const noteIndex = noteCache.findIndex(n => n.id === note.id); + tags = await generateTags(trimmedContent, noteIndex); } const document = { @@ -767,7 +789,7 @@ ${content}`; const searchStartTime = Date.now(); console.log(`Starting search for: "${query}"`); - addDebugInfo(`Searching for: "${query}"`); + addDebugInfo(`Searching for: "${query}"${includeTagsInSearch ? ' (including tags)' : ' (content only)'}`); try { const response = await fetchWithTiming(`${MEILISEARCH_ENDPOINT}/indexes/${NOTE_INDEX}/search`, { @@ -780,7 +802,7 @@ ${content}`; q: query, matchingStrategy: 'all', limit: 50, - attributesToHighlight: ['content', 'tags'], + attributesToHighlight: includeTagsInSearch ? ['content', 'tags'] : ['content'], showRankingScore: true, highlightPreTag: '', highlightPostTag: '', @@ -1034,9 +1056,10 @@ ${content}`; const notes: Note[] = data.hits.map((hit: any) => mapHitToNote(hit)); let updatedCount = 0; - for (const note of notes) { + for (let i = 0; i < notes.length; i++) { + const note = notes[i]; try { - const newTags = await generateTags(note.content); + const newTags = await generateTags(note.content, i); if (JSON.stringify(newTags) !== JSON.stringify(note.tags || [])) { const updatedNote = { ...note, tags: newTags }; await updateNote(updatedNote); @@ -1727,16 +1750,16 @@ ${content}`; className={`flex-1 border-0 bg-transparent ${getTextClass('base')} p-0 outline-none`} /> {!autoGenerateTags && ( -