Skip to content

Latest commit

 

History

History
141 lines (109 loc) · 4.85 KB

File metadata and controls

141 lines (109 loc) · 4.85 KB

AI Search Integration - Enhanced Context-Aware Chat

Overview

The AI chat system has been enhanced to automatically use the new lexical search engine for context retrieval. This provides more accurate, relevant responses by leveraging your existing notes.

Key Enhancements

1. Automatic Context Retrieval

  • sendMessage(): Now automatically uses aiChatWithContext() for all regular chat messages
  • sendCommand(): Intelligently routes questions to search-powered AI vs. tool-based processing
  • No user intervention required: Context retrieval happens transparently

2. Smart Command Routing

Commands are automatically categorized:

Search-Powered (Context-aware):

  • Questions: "What did I write about...?", "How do I...?", "Explain..."
  • Information requests: "Tell me about...", "Summarize...", "Find..."
  • Research queries: "Show me...", "Where did I mention...?"

Tool-Based (Action-oriented):

  • Creation: "Create a note...", "Add a tag...", "Generate..."
  • Updates: "Update...", "Modify...", "Change..."
  • Management: "Delete...", "Remove...", "Extract todos..."

3. Enhanced User Experience

Visual Indicators

  • Context-aware badge: Shows when search was used
  • Token usage tracking: Displays estimated token consumption
  • Provider identification: Shows which AI model was used
  • Timestamp support: Optional message timing

Performance Optimizations

  • Cached results: 5-minute TTL for repeated queries
  • Debounced updates: Smooth streaming with minimal re-renders
  • Token estimation: Tracks usage even for search-powered responses

Technical Implementation

Backend Integration

// New command endpoints
ai_chat_with_context(query, search_query?, tags?) -> String
get_relevant_context(query, tags?, max_chunks?) -> QueryContext

Frontend Integration

// Enhanced AI store methods
sendMessage(content: string, explicitContext?: string)
sendCommand(command: string, explicitContext?: string)

// Search API integration
import { aiChatWithContext, getRelevantContext } from '@/api/search'

Automatic Indexing

  • Notes are automatically indexed on create/update/delete
  • Search service initializes with all existing notes
  • Incremental updates maintain index consistency

Usage Examples

Context-Aware Chat

// User asks: "What are my thoughts on React performance?"
// System automatically:
// 1. Searches notes for relevant content about React performance
// 2. Retrieves top-ranked chunks using BM25
// 3. Provides AI response with context from your notes

Smart Command Routing

// Question → Search-powered
await sendCommand("How do I implement authentication?")
// → Uses aiChatWithContext() for contextual answer

// Action → Tool-based
await sendCommand("Create a note about authentication")
// → Uses original tool system for note creation

Manual Context Control

// Override automatic behavior with explicit context
await sendMessage("Explain this code", explicitCodeContext)
await sendCommand("Analyze this", explicitAnalysisContext)

Configuration

Search Service Settings

  • Max context tokens: 4000 (configurable)
  • Cache TTL: 5 minutes
  • Index chunk size: 1000 tokens with 100 token overlap
  • BM25 parameters: k1=1.2, b=0.75

AI Integration Settings

  • Automatic context: Enabled by default
  • Token estimation: 4 characters ≈ 1 token
  • Fallback behavior: Original API for explicit context
  • Error handling: Graceful degradation to non-context mode

Components

New React Components

  • AISearchChat: Full-featured search + AI interface
  • SearchIntegrationStatus: Shows search system status
  • EnhancedAIChatMessage: Message component with context indicators

Enhanced Stores

  • aiStore.ts: Updated with search integration
  • notesStore.ts: Automatic indexing on CRUD operations

Benefits

  1. Contextual Accuracy: Responses based on your actual notes and knowledge
  2. Transparent Operation: Works automatically without user configuration
  3. Performance: Fast lexical search with BM25 ranking
  4. Caching: Repeated queries served from cache
  5. Flexibility: Maintains original functionality for explicit contexts
  6. Visual Feedback: Clear indicators when search is used

Migration Path

The integration is backward compatible:

  • Existing code continues to work unchanged
  • New search functionality is opt-in via automatic detection
  • Manual context provision still supported
  • All original AI features remain functional

Future Enhancements

  • Tag-based filtering: Automatic tag detection in queries
  • Date range filtering: Time-aware context retrieval
  • Conversation memory: Multi-turn context awareness
  • Performance analytics: Search timing and relevance metrics
  • User feedback loop: Learn from response quality ratings