Files
Momento/memento-note/lib/ai/services/embedding.service.ts
Antigravity bd495be965
All checks were successful
Deploy to Production / Build and Deploy (push) Successful in 12s
feat: design system overhaul — sidebar, AI chats, settings, brainstorm, color cleanup
- Sidebar: dynamic brand-accent colors, brainstorm section restyled
- AI chat general: popup panel with expand/collapse, hides when contextual AI open
- AI chat contextual: tabs reordered (Actions first), X close button, height fix
- Settings: all tabs restyled, 6 new color presets (sage, terracotta, iron, etc.)
- Global color cleanup: emerald/orange hardcoded → brand-accent dynamic
- Brainstorm page: orange → brand-accent throughout
- PageEntry animation component added to key pages
- Floating AI button: bg-brand-accent instead of hardcoded black
- i18n: all 15 locales updated with new AI/billing keys
- Billing: freemium quota tracking, BYOK, stripe subscription scaffolding
- Admin: integrated into new design
- AGENTS.md + CLAUDE.md project rules added
2026-05-16 12:59:30 +00:00

116 lines
3.3 KiB
TypeScript

/**
* Embedding Service
* Generates vector embeddings for semantic search and similarity analysis.
* Stores embeddings as native pgvector in PostgreSQL.
*/
import { withAiProviderFallback } from '../fallback'
import { getSystemConfig } from '@/lib/config'
export interface EmbeddingResult {
embedding: number[]
model: string
dimension: number
}
export class EmbeddingService {
private readonly EMBEDDING_DIMENSION = 1536
async generateEmbedding(text: string): Promise<EmbeddingResult> {
if (!text || text.trim().length === 0) {
throw new Error('Cannot generate embedding for empty text')
}
try {
const config = await getSystemConfig()
const embedding = await withAiProviderFallback('embedding', config, (provider) =>
provider.getEmbeddings(text)
)
return {
embedding,
model: 'text-embedding-3-small',
dimension: embedding.length
}
} catch (error) {
console.error('Error generating embedding:', error)
throw new Error(`Failed to generate embedding: ${error}`)
}
}
async generateBatchEmbeddings(texts: string[]): Promise<EmbeddingResult[]> {
if (!texts || texts.length === 0) return []
const validTexts = texts.filter(t => t && t.trim().length > 0)
if (validTexts.length === 0) return []
try {
const config = await getSystemConfig()
const embeddings = await withAiProviderFallback('embedding', config, (provider) =>
Promise.all(validTexts.map((text) => provider.getEmbeddings(text)))
)
return embeddings.map(embedding => ({
embedding,
model: 'text-embedding-3-small',
dimension: embedding.length
}))
} catch (error) {
console.error('Error generating batch embeddings:', error)
throw error
}
}
/**
* Format a number[] embedding as a pgvector-compatible string literal.
* e.g. [0.1, 0.2, 0.3] → '[0.1,0.2,0.3]'
*/
toVectorString(embedding: number[]): string {
return `[${embedding.join(',')}]`
}
/**
* Parse a pgvector string from the DB back into number[].
* e.g. '[0.1,0.2,0.3]' → [0.1, 0.2, 0.3]
*/
fromVectorString(vec: string): number[] {
if (Array.isArray(vec)) return vec
if (!vec || typeof vec !== 'string') return []
return vec.replace(/^\[/, '').replace(/\]$/, '').split(',').map(Number)
}
/**
* JS cosine similarity — still used by memory-echo pairwise comparisons.
*/
calculateCosineSimilarity(a: number[], b: number[]): number {
if (!a.length || !b.length) return 0
const minLen = Math.min(a.length, b.length)
let dot = 0, mA = 0, mB = 0
for (let i = 0; i < minLen; i++) {
dot += a[i] * b[i]
mA += a[i] * a[i]
mB += b[i] * b[i]
}
mA = Math.sqrt(mA)
mB = Math.sqrt(mB)
if (mA === 0 || mB === 0) return 0
return dot / (mA * mB)
}
/**
* Check if a note needs embedding regeneration.
* Uses a content-content comparison (not embedding-content).
*/
shouldRegenerateEmbedding(
noteContent: string,
_lastEmbeddingContent: string | null,
lastAnalysis: Date | null
): boolean {
if (!lastAnalysis) return true
const daysSinceAnalysis = (Date.now() - lastAnalysis.getTime()) / (1000 * 60 * 60 * 24)
return daysSinceAnalysis > 7
}
}
export const embeddingService = new EmbeddingService()