UNPKG

@mastra/core

Version:
201 lines (200 loc) • 7.61 kB
import { t as MastraBase } from "../base-BeUQ6mLP.js"; import { i as MastraError, n as ErrorDomain, t as ErrorCategory } from "../error-MjDSls8S.js"; import { h as embed$2 } from "../message-list-mC29laJJ.js"; import { r as embed$1 } from "../dist-DIIEuFGB.js"; import { n as embed } from "../dist-_kmO6lmF.js"; import { rt as createVectorErrorId } from "../storage-BS3ic0Sd.js"; import { BaseFilterTranslator } from "./filter/index.js"; //#region src/vector/vector.ts /** Specification versions for supported (modern) embedding models */ const supportedEmbeddingModelSpecifications = ["v2", "v3"]; /** * Type guard to check if an embedding model is a supported modern version (V2 or V3). * Use embedV2 for V2 models, embedV3 for V3 models, and embedV1 for legacy V1 models. */ const isSupportedEmbeddingModel = (model) => { return supportedEmbeddingModelSpecifications.includes(model.specificationVersion); }; var MastraVector = class extends MastraBase { id; disableInit = false; constructor({ id, disableInit }) { if (!id || typeof id !== "string" || id.trim() === "") throw new MastraError({ id: "VECTOR_INVALID_ID", text: "Vector id must be provided and cannot be empty", domain: ErrorDomain.MASTRA_VECTOR, category: ErrorCategory.USER }); super({ name: "MastraVector", component: "VECTOR" }); this.id = id; this.disableInit = disableInit ?? false; } get indexSeparator() { return "_"; } async validateExistingIndex(indexName, dimension, metric) { let info; try { info = await this.describeIndex({ indexName }); } catch (infoError) { const mastraError = new MastraError({ id: "VECTOR_VALIDATE_INDEX_FETCH_FAILED", text: `Index "${indexName}" already exists, but failed to fetch index info for dimension check.`, domain: ErrorDomain.MASTRA_VECTOR, category: ErrorCategory.SYSTEM, details: { indexName } }, infoError); this.logger?.trackException(mastraError); throw mastraError; } const existingDim = info?.dimension; const existingMetric = info?.metric; if (existingDim === dimension) { this.logger?.info(`Index "${indexName}" already exists with ${existingDim} dimensions and metric ${existingMetric}, skipping creation.`); if (existingMetric !== metric) this.logger?.warn(`Attempted to create index with metric "${metric}", but index already exists with metric "${existingMetric}". To use a different metric, delete and recreate the index.`); } else if (info) { const mastraError = new MastraError({ id: "VECTOR_VALIDATE_INDEX_DIMENSION_MISMATCH", text: `Index "${indexName}" already exists with ${existingDim} dimensions, but ${dimension} dimensions were requested`, domain: ErrorDomain.MASTRA_VECTOR, category: ErrorCategory.USER, details: { indexName, existingDim, requestedDim: dimension } }); this.logger?.trackException(mastraError); throw mastraError; } else { const mastraError = new MastraError({ id: "VECTOR_VALIDATE_INDEX_NO_DIMENSION", text: `Index "${indexName}" already exists, but could not retrieve its dimensions for validation.`, domain: ErrorDomain.MASTRA_VECTOR, category: ErrorCategory.SYSTEM, details: { indexName } }); this.logger?.trackException(mastraError); throw mastraError; } } }; //#endregion //#region src/vector/validation.ts /** * Shared validation helpers for vector store implementations * * These helpers provide consistent validation across all vector stores, * reducing code duplication and ensuring uniform error handling. */ /** * Validates upsert input parameters * * @param storeName - Name of the vector store (e.g., 'PG', 'CHROMA') * @param vectors - Array of vectors to upsert * @param metadata - Optional metadata array * @param ids - Optional ids array * @throws MastraError if validation fails */ function validateUpsertInput(storeName, vectors, metadata, ids) { if (!vectors || vectors.length === 0) throw new MastraError({ id: createVectorErrorId(storeName, "UPSERT", "EMPTY_VECTORS"), domain: ErrorDomain.MASTRA_VECTOR, category: ErrorCategory.USER, details: { message: "Vectors array cannot be empty" } }); if (metadata && metadata.length > 0 && metadata.length !== vectors.length) throw new MastraError({ id: createVectorErrorId(storeName, "UPSERT", "METADATA_LENGTH_MISMATCH"), domain: ErrorDomain.MASTRA_VECTOR, category: ErrorCategory.USER, details: { message: "Metadata array length must match vectors array length", vectorsLength: vectors.length, metadataLength: metadata.length } }); if (ids && ids.length !== vectors.length) throw new MastraError({ id: createVectorErrorId(storeName, "UPSERT", "IDS_LENGTH_MISMATCH"), domain: ErrorDomain.MASTRA_VECTOR, category: ErrorCategory.USER, details: { message: "IDs array length must match vectors array length", vectorsLength: vectors.length, idsLength: ids.length } }); } /** * Validates topK parameter for queries * * @param storeName - Name of the vector store (e.g., 'PG', 'CHROMA') * @param topK - Number of results to return * @throws MastraError if topK is not a positive integer */ function validateTopK(storeName, topK) { if (!Number.isInteger(topK) || topK <= 0) throw new MastraError({ id: createVectorErrorId(storeName, "QUERY", "INVALID_TOP_K"), domain: ErrorDomain.MASTRA_VECTOR, category: ErrorCategory.USER, details: { message: "topK must be a positive integer", topK } }); } /** * Validates vector components for NaN/Infinity values * * @param storeName - Name of the vector store (e.g., 'PG', 'CHROMA') * @param vectors - Array of vectors to validate * @throws MastraError if any vector contains NaN, Infinity, null, or undefined */ function validateVectorValues(storeName, vectors) { for (let i = 0; i < vectors.length; i++) { const vector = vectors[i]; if (!vector) throw new MastraError({ id: createVectorErrorId(storeName, "UPSERT", "INVALID_VECTOR"), domain: ErrorDomain.MASTRA_VECTOR, category: ErrorCategory.USER, details: { message: `Vector at index ${i} is null or undefined`, vectorIndex: i } }); for (let j = 0; j < vector.length; j++) { const value = vector[j]; if (value === null || value === void 0 || !Number.isFinite(value)) throw new MastraError({ id: createVectorErrorId(storeName, "UPSERT", "INVALID_VECTOR_VALUE"), domain: ErrorDomain.MASTRA_VECTOR, category: ErrorCategory.USER, details: { message: `Vector contains invalid value (null, undefined, NaN, or Infinity) at position [${i}][${j}]`, vectorIndex: i, componentIndex: j, value: String(value) } }); } } } /** * Validates all upsert inputs including vector values * Combines validateUpsertInput and validateVectorValues * * @param storeName - Name of the vector store (e.g., 'PG', 'CHROMA') * @param vectors - Array of vectors to upsert * @param metadata - Optional metadata array * @param ids - Optional ids array * @param validateValues - Whether to validate vector values for NaN/Infinity (default: false) * @throws MastraError if validation fails */ function validateUpsert(storeName, vectors, metadata, ids, validateValues = false) { validateUpsertInput(storeName, vectors, metadata, ids); if (validateValues && vectors) validateVectorValues(storeName, vectors); } //#endregion export { BaseFilterTranslator, MastraVector, embed as embedV1, embed$1 as embedV2, embed$2 as embedV3, isSupportedEmbeddingModel, supportedEmbeddingModelSpecifications, validateTopK, validateUpsert, validateUpsertInput, validateVectorValues }; //# sourceMappingURL=index.js.map