@mastra/core
Version:
179 lines (176 loc) • 6.45 kB
JavaScript
export { BaseFilterTranslator } from '../chunk-BWYU7D33.js';
import { createVectorErrorId } from '../chunk-PIFDJ2RZ.js';
import { MastraBase } from '../chunk-77VL4DNS.js';
export { embed as embedV2 } from '../chunk-KJTM46JT.js';
export { embed as embedV3 } from '../chunk-TOBPSKTN.js';
export { embed as embedV1 } from '../chunk-D23QKBCZ.js';
import { MastraError, ErrorCategory, ErrorDomain } from '../chunk-M7RBQNFP.js';
// src/vector/vector.ts
var supportedEmbeddingModelSpecifications = ["v2", "v3"];
var 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;
}
}
};
// src/vector/validation.ts
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
}
});
}
}
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
}
});
}
}
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)
}
});
}
}
}
}
function validateUpsert(storeName, vectors, metadata, ids, validateValues = false) {
validateUpsertInput(storeName, vectors, metadata, ids);
if (validateValues && vectors) {
validateVectorValues(storeName, vectors);
}
}
export { MastraVector, isSupportedEmbeddingModel, supportedEmbeddingModelSpecifications, validateTopK, validateUpsert, validateUpsertInput, validateVectorValues };
//# sourceMappingURL=index.js.map
//# sourceMappingURL=index.js.map