fix: add validation for empty vectors in brain.similar()

Prevents using metadata-only entities with brain.similar() when vectors
are not loaded. Provides helpful error message guiding users to either:
1. Pass entity ID: brain.similar({ to: entityId })
2. Load with vectors: brain.similar({ to: await brain.get(id, { includeVectors: true }) })

This ensures brain.similar() always has valid vectors to compute similarity.
This commit is contained in:
David Snelling 2025-11-18 15:52:41 -08:00
parent a6e680d792
commit 0426027765

View file

@ -2006,7 +2006,16 @@ export class Brainy<T = any> implements BrainyInterface<T> {
} else if (Array.isArray(params.to)) {
targetVector = params.to as Vector
} else {
targetVector = (params.to as Entity<T>).vector
// v5.11.1: Entity object passed - check if vectors are loaded
const entityVector = (params.to as Entity<T>).vector
if (!entityVector || entityVector.length === 0) {
throw new Error(
'Entity passed to brain.similar() has no vector embeddings loaded. ' +
'Please retrieve the entity with { includeVectors: true } or pass the entity ID instead.\n\n' +
'Example: brain.similar({ to: entityId }) OR brain.similar({ to: await brain.get(entityId, { includeVectors: true }) })'
)
}
targetVector = entityVector
}
// Use find with vector