Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 | import { eq } from 'drizzle-orm' import { db, schema } from '@/db' import type { FlowchartMeta } from './definitions' import { bufferToEmbedding, generateEmbedding, EMBEDDING_VERSION } from './embedding' /** * In-memory cache for flowchart embeddings. * Maps flowchart ID to { embedding, promptEmbedding, timestamp } * Cache is invalidated after 5 minutes. */ interface CachedEmbedding { /** Full content embedding (title + description + topic + difficulty) */ embedding: Float32Array /** Prompt-only embedding (just the original topic description) - better for short queries */ promptEmbedding: Float32Array | null timestamp: number source: 'database' meta: FlowchartMeta } const CACHE_TTL_MS = 5 * 60 * 1000 // 5 minutes let embeddingCache: Map<string, CachedEmbedding> | null = null let cacheTimestamp = 0 /** * Calculate cosine similarity between two embedding vectors. * Returns a value between -1 and 1, where 1 means identical. */ export function cosineSimilarity(a: Float32Array, b: Float32Array): number { if (a.length !== b.length) { throw new Error(`Embedding dimension mismatch: ${a.length} vs ${b.length}`) } let dotProduct = 0 let normA = 0 let normB = 0 for (let i = 0; i < a.length; i++) { dotProduct += a[i] * b[i] normA += a[i] * a[i] normB += b[i] * b[i] } const denominator = Math.sqrt(normA) * Math.sqrt(normB) if (denominator === 0) { return 0 } return dotProduct / denominator } /** * Get all published flowchart embeddings from the database. * Uses in-memory caching with 5 minute TTL. * * NOTE: All flowcharts (including built-in ones) must be seeded to * the database to appear in search results. */ export async function getAllFlowchartEmbeddings(): Promise<Map<string, CachedEmbedding>> { const now = Date.now() // Return cached embeddings if still valid if (embeddingCache && now - cacheTimestamp < CACHE_TTL_MS) { return embeddingCache } const cache = new Map<string, CachedEmbedding>() // Get all published flowcharts with embeddings const flowcharts = await db.query.teacherFlowcharts.findMany({ where: eq(schema.teacherFlowcharts.status, 'published'), columns: { id: true, title: true, description: true, emoji: true, difficulty: true, embedding: true, promptEmbedding: true, embeddingVersion: true, }, }) for (const fc of flowcharts) { // Only include if has valid embedding if (fc.embedding && fc.embeddingVersion === EMBEDDING_VERSION) { cache.set(fc.id, { embedding: bufferToEmbedding(fc.embedding), promptEmbedding: fc.promptEmbedding ? bufferToEmbedding(fc.promptEmbedding) : null, timestamp: now, source: 'database', meta: { id: fc.id, title: fc.title, description: fc.description || '', emoji: fc.emoji || '📊', difficulty: (fc.difficulty as FlowchartMeta['difficulty']) || 'Beginner', }, }) } } embeddingCache = cache cacheTimestamp = now return cache } /** * Search result from similarity search */ export interface FlowchartSearchResult { id: string title: string description: string emoji: string difficulty: string similarity: number source: 'database' } /** * Search options */ export interface SearchOptions { /** Maximum number of results to return (default: 5) */ limit?: number /** Minimum similarity threshold (default: 0.5) */ minSimilarity?: number /** Flowchart ID to exclude from results (for finding related flowcharts) */ excludeId?: string } /** * Search for similar flowcharts using a text query. * * Compares the query against both the full content embedding and the prompt * embedding (if available), returning the higher similarity score. This improves * matching for short queries that resemble user prompts. * * @param query - The search query text * @param options - Search options * @returns Array of matching flowcharts sorted by similarity */ export async function searchSimilarFlowcharts( query: string, options: SearchOptions = {} ): Promise<FlowchartSearchResult[]> { const { limit = 5, minSimilarity = 0.5, excludeId } = options // Generate embedding for the query const queryEmbedding = await generateEmbedding(query) // Get all flowchart embeddings const embeddings = await getAllFlowchartEmbeddings() // Calculate similarity for each flowchart const results: FlowchartSearchResult[] = [] for (const [id, cached] of embeddings) { if (excludeId && id === excludeId) { continue } // Compare against full content embedding const contentSimilarity = cosineSimilarity(queryEmbedding, cached.embedding) // Compare against prompt embedding if available (better for short queries) const promptSimilarity = cached.promptEmbedding ? cosineSimilarity(queryEmbedding, cached.promptEmbedding) : 0 // Take the higher similarity score const similarity = Math.max(contentSimilarity, promptSimilarity) if (similarity >= minSimilarity) { results.push({ id, title: cached.meta.title, description: cached.meta.description, emoji: cached.meta.emoji, difficulty: cached.meta.difficulty, similarity, source: cached.source, }) } } // Sort by similarity (highest first) and limit results results.sort((a, b) => b.similarity - a.similarity) return results.slice(0, limit) } /** * Search for flowcharts related to a specific flowchart. * * @param flowchartId - The ID of the flowchart to find related items for * @param options - Search options * @returns Array of related flowcharts sorted by similarity */ export async function findRelatedFlowcharts( flowchartId: string, options: SearchOptions = {} ): Promise<FlowchartSearchResult[]> { const embeddings = await getAllFlowchartEmbeddings() const targetCached = embeddings.get(flowchartId) if (!targetCached) { return [] } const { limit = 5, minSimilarity = 0.4 } = options const results: FlowchartSearchResult[] = [] for (const [id, cached] of embeddings) { if (id === flowchartId) { continue } const similarity = cosineSimilarity(targetCached.embedding, cached.embedding) if (similarity >= minSimilarity) { results.push({ id, title: cached.meta.title, description: cached.meta.description, emoji: cached.meta.emoji, difficulty: cached.meta.difficulty, similarity, source: cached.source, }) } } // Sort by similarity (highest first) and limit results results.sort((a, b) => b.similarity - a.similarity) return results.slice(0, limit) } /** * Invalidate the embedding cache. * Call this after generating new embeddings to force a refresh. */ export function invalidateEmbeddingCache(): void { embeddingCache = null cacheTimestamp = 0 } |