@mastra/core
Version:
203 lines (199 loc) • 7.84 kB
JavaScript
'use strict';
var chunk6XCINXZ7_cjs = require('../chunk-6XCINXZ7.cjs');
var chunkOAN2CNR7_cjs = require('../chunk-OAN2CNR7.cjs');
var chunkWSD4JNMB_cjs = require('../chunk-WSD4JNMB.cjs');
var chunkZQADKFIL_cjs = require('../chunk-ZQADKFIL.cjs');
var chunk4SCYX53T_cjs = require('../chunk-4SCYX53T.cjs');
var chunkI4YELKDU_cjs = require('../chunk-I4YELKDU.cjs');
var chunkXSOONORA_cjs = require('../chunk-XSOONORA.cjs');
// src/vector/vector.ts
var supportedEmbeddingModelSpecifications = ["v2", "v3"];
var isSupportedEmbeddingModel = (model) => {
return supportedEmbeddingModelSpecifications.includes(
model.specificationVersion
);
};
var MastraVector = class extends chunkWSD4JNMB_cjs.MastraBase {
id;
disableInit = false;
constructor({ id, disableInit }) {
if (!id || typeof id !== "string" || id.trim() === "") {
throw new chunkXSOONORA_cjs.MastraError({
id: "VECTOR_INVALID_ID",
text: "Vector id must be provided and cannot be empty",
domain: chunkXSOONORA_cjs.ErrorDomain.MASTRA_VECTOR,
category: chunkXSOONORA_cjs.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 chunkXSOONORA_cjs.MastraError(
{
id: "VECTOR_VALIDATE_INDEX_FETCH_FAILED",
text: `Index "${indexName}" already exists, but failed to fetch index info for dimension check.`,
domain: chunkXSOONORA_cjs.ErrorDomain.MASTRA_VECTOR,
category: chunkXSOONORA_cjs.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 chunkXSOONORA_cjs.MastraError({
id: "VECTOR_VALIDATE_INDEX_DIMENSION_MISMATCH",
text: `Index "${indexName}" already exists with ${existingDim} dimensions, but ${dimension} dimensions were requested`,
domain: chunkXSOONORA_cjs.ErrorDomain.MASTRA_VECTOR,
category: chunkXSOONORA_cjs.ErrorCategory.USER,
details: { indexName, existingDim, requestedDim: dimension }
});
this.logger?.trackException(mastraError);
throw mastraError;
} else {
const mastraError = new chunkXSOONORA_cjs.MastraError({
id: "VECTOR_VALIDATE_INDEX_NO_DIMENSION",
text: `Index "${indexName}" already exists, but could not retrieve its dimensions for validation.`,
domain: chunkXSOONORA_cjs.ErrorDomain.MASTRA_VECTOR,
category: chunkXSOONORA_cjs.ErrorCategory.SYSTEM,
details: { indexName }
});
this.logger?.trackException(mastraError);
throw mastraError;
}
}
};
// src/vector/validation.ts
function validateUpsertInput(storeName, vectors, metadata, ids) {
if (!vectors || vectors.length === 0) {
throw new chunkXSOONORA_cjs.MastraError({
id: chunkOAN2CNR7_cjs.createVectorErrorId(storeName, "UPSERT", "EMPTY_VECTORS"),
domain: chunkXSOONORA_cjs.ErrorDomain.MASTRA_VECTOR,
category: chunkXSOONORA_cjs.ErrorCategory.USER,
details: {
message: "Vectors array cannot be empty"
}
});
}
if (metadata && metadata.length > 0 && metadata.length !== vectors.length) {
throw new chunkXSOONORA_cjs.MastraError({
id: chunkOAN2CNR7_cjs.createVectorErrorId(storeName, "UPSERT", "METADATA_LENGTH_MISMATCH"),
domain: chunkXSOONORA_cjs.ErrorDomain.MASTRA_VECTOR,
category: chunkXSOONORA_cjs.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 chunkXSOONORA_cjs.MastraError({
id: chunkOAN2CNR7_cjs.createVectorErrorId(storeName, "UPSERT", "IDS_LENGTH_MISMATCH"),
domain: chunkXSOONORA_cjs.ErrorDomain.MASTRA_VECTOR,
category: chunkXSOONORA_cjs.ErrorCategory.USER,
details: {
message: "IDs array length must match vectors array length",
vectorsLength: vectors.length,
idsLength: ids.length
}
});
}
}
function validateTopK(storeName, topK) {
if (!Number.isInteger(topK) || topK <= 0) {
throw new chunkXSOONORA_cjs.MastraError({
id: chunkOAN2CNR7_cjs.createVectorErrorId(storeName, "QUERY", "INVALID_TOP_K"),
domain: chunkXSOONORA_cjs.ErrorDomain.MASTRA_VECTOR,
category: chunkXSOONORA_cjs.ErrorCategory.USER,
details: {
message: "topK must be a positive integer",
topK
}
});
}
}
function validateVectorValues(storeName, vectors) {
for (let i = 0; i < vectors.length; i++) {
const vector = vectors[i];
if (!vector) {
throw new chunkXSOONORA_cjs.MastraError({
id: chunkOAN2CNR7_cjs.createVectorErrorId(storeName, "UPSERT", "INVALID_VECTOR"),
domain: chunkXSOONORA_cjs.ErrorDomain.MASTRA_VECTOR,
category: chunkXSOONORA_cjs.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 chunkXSOONORA_cjs.MastraError({
id: chunkOAN2CNR7_cjs.createVectorErrorId(storeName, "UPSERT", "INVALID_VECTOR_VALUE"),
domain: chunkXSOONORA_cjs.ErrorDomain.MASTRA_VECTOR,
category: chunkXSOONORA_cjs.ErrorCategory.USER,
details: {
message: `Vector contains invalid value (null, undefined, NaN, or Infinity) at position [${i}][${j}]`,
vectorIndex: i,
componentIndex: j,
value: String(value)
}
});
}
}
}
}
function validateUpsert(storeName, vectors, metadata, ids, validateValues = false) {
validateUpsertInput(storeName, vectors, metadata, ids);
if (validateValues && vectors) {
validateVectorValues(storeName, vectors);
}
}
Object.defineProperty(exports, "BaseFilterTranslator", {
enumerable: true,
get: function () { return chunk6XCINXZ7_cjs.BaseFilterTranslator; }
});
Object.defineProperty(exports, "embedV2", {
enumerable: true,
get: function () { return chunkZQADKFIL_cjs.embed; }
});
Object.defineProperty(exports, "embedV3", {
enumerable: true,
get: function () { return chunk4SCYX53T_cjs.embed; }
});
Object.defineProperty(exports, "embedV1", {
enumerable: true,
get: function () { return chunkI4YELKDU_cjs.embed; }
});
exports.MastraVector = MastraVector;
exports.isSupportedEmbeddingModel = isSupportedEmbeddingModel;
exports.supportedEmbeddingModelSpecifications = supportedEmbeddingModelSpecifications;
exports.validateTopK = validateTopK;
exports.validateUpsert = validateUpsert;
exports.validateUpsertInput = validateUpsertInput;
exports.validateVectorValues = validateVectorValues;
//# sourceMappingURL=index.cjs.map
//# sourceMappingURL=index.cjs.map