/** * Copyright 2026 aphrody-code * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /** * Projects text into a 512-dimensional unit-length term vector (Hashing Trick). * This FNV-1a inspired hash-projection algorithm runs natively in Bun without requiring * external network calls or LLM API keys. */ export function getLocalTextEmbedding(text: string, dimensions = 512): number[] { const clean = text.replace(/[^\w\s]/g, " ").toLowerCase(); const words = clean.split(/\s+/).filter(Boolean); const vector = Array.from({ length: dimensions }, () => 0); for (const word of words) { // FNV-1a 32-bit hash let hash = 2166136261; for (let i = 0; i < word.length; i++) { hash ^= word.charCodeAt(i); hash = Math.imul(hash, 16777619); } const index = Math.abs(hash) % dimensions; // Determine polarity to avoid bias accumulation const polarity = (hash & 1) === 0 ? 1 : -1; vector[index] += polarity; } // Normalize vector to L2 unit length const sumOfSquares = vector.reduce((sum, val) => sum + val * val, 0); const magnitude = Math.sqrt(sumOfSquares); if (magnitude === 0) return vector; return vector.map((val) => val / magnitude); } /** * Embed `text` with the process-wide default embedder (local model when * installed, then Gemini/OpenAI when a key is configured, then feature * hashing). Indexing and querying therefore always use the same model; see * `src/embed/embedder.ts`. */ export async function getEmbedding(text: string, kind: "query" | "passage" = "query"): Promise { const { defaultEmbedder } = await import("../embed/embedder.ts"); const embedder = await defaultEmbedder(); try { const [vector] = await embedder.embed([text.slice(0, 10_000)], kind); if (vector) return Array.from(vector); } catch (err) { console.error(`[vector-embedding] ${embedder.model} failed, using feature hashing:`, err); } return getLocalTextEmbedding(text.slice(0, 10_000)); } /** * Calculates the cosine similarity score between two float vectors. */ export function cosineSimilarity(a: number[], b: number[]): number { if (a.length !== b.length) return 0; let dotProduct = 0; let normA = 0; let normB = 0; for (let i = 0; i < a.length; i++) { const valA = a[i]; const valB = b[i]; dotProduct += valA * valB; normA += valA * valA; normB += valB * valB; } if (normA === 0 || normB === 0) return 0; return dotProduct / (Math.sqrt(normA) * Math.sqrt(normB)); }