'use strict';

var chunkLZ32IIH2_cjs = require('./chunk-LZ32IIH2.cjs');
var chunkZ7LCIYK7_cjs = require('./chunk-Z7LCIYK7.cjs');
var chunkN54Q3AQR_cjs = require('./chunk-N54Q3AQR.cjs');
var chunk64ITUOXI_cjs = require('./chunk-64ITUOXI.cjs');
var chunkO7I5CWRX_cjs = require('./chunk-O7I5CWRX.cjs');
var llm = require('@mastra/core/llm');

// src/server/handlers/vector.ts
var vector_exports = {};
chunkO7I5CWRX_cjs.__export(vector_exports, {
  CREATE_INDEX_ROUTE: () => CREATE_INDEX_ROUTE,
  DELETE_INDEX_ROUTE: () => DELETE_INDEX_ROUTE,
  DESCRIBE_INDEX_ROUTE: () => DESCRIBE_INDEX_ROUTE,
  LIST_EMBEDDERS_ROUTE: () => LIST_EMBEDDERS_ROUTE,
  LIST_INDEXES_ROUTE: () => LIST_INDEXES_ROUTE,
  LIST_VECTORS_ROUTE: () => LIST_VECTORS_ROUTE,
  QUERY_VECTORS_ROUTE: () => QUERY_VECTORS_ROUTE,
  UPSERT_VECTORS_ROUTE: () => UPSERT_VECTORS_ROUTE,
  createIndex: () => createIndex,
  deleteIndex: () => deleteIndex,
  describeIndex: () => describeIndex,
  listIndexes: () => listIndexes,
  listVectorStores: () => listVectorStores,
  queryVectors: () => queryVectors,
  upsertVectors: () => upsertVectors
});
function getVector(mastra, vectorName) {
  if (!vectorName) {
    throw new chunk64ITUOXI_cjs.HTTPException(400, { message: "Vector name is required" });
  }
  const vector = mastra.getVector(vectorName);
  if (!vector) {
    throw new chunk64ITUOXI_cjs.HTTPException(404, { message: `Vector store ${vectorName} not found` });
  }
  return vector;
}
async function upsertVectors({
  mastra,
  vectorName,
  indexName,
  vectors,
  metadata,
  ids
}) {
  try {
    if (!indexName || !vectors || !Array.isArray(vectors)) {
      throw new chunk64ITUOXI_cjs.HTTPException(400, { message: "Invalid request index. indexName and vectors array are required." });
    }
    const vector = getVector(mastra, vectorName);
    const result = await vector.upsert({ indexName, vectors, metadata, ids });
    return { ids: result };
  } catch (error) {
    return chunkZ7LCIYK7_cjs.handleError(error, "Error upserting vectors");
  }
}
async function createIndex({
  mastra,
  vectorName,
  indexName,
  dimension,
  metric
}) {
  try {
    if (!indexName || typeof dimension !== "number" || dimension <= 0) {
      throw new chunk64ITUOXI_cjs.HTTPException(400, {
        message: "Invalid request index, indexName and positive dimension number are required."
      });
    }
    if (metric && !["cosine", "euclidean", "dotproduct"].includes(metric)) {
      throw new chunk64ITUOXI_cjs.HTTPException(400, { message: "Invalid metric. Must be one of: cosine, euclidean, dotproduct" });
    }
    const vector = getVector(mastra, vectorName);
    await vector.createIndex({ indexName, dimension, metric });
    return { success: true };
  } catch (error) {
    return chunkZ7LCIYK7_cjs.handleError(error, "Error creating index");
  }
}
async function queryVectors({
  mastra,
  vectorName,
  indexName,
  queryVector,
  topK,
  filter,
  includeVector
}) {
  try {
    if (!indexName || !queryVector || !Array.isArray(queryVector)) {
      throw new chunk64ITUOXI_cjs.HTTPException(400, { message: "Invalid request query. indexName and queryVector array are required." });
    }
    const vector = getVector(mastra, vectorName);
    const results = await vector.query({ indexName, queryVector, topK, filter, includeVector });
    return results;
  } catch (error) {
    return chunkZ7LCIYK7_cjs.handleError(error, "Error querying vectors");
  }
}
async function listIndexes({ mastra, vectorName }) {
  try {
    const vector = getVector(mastra, vectorName);
    const indexes = await vector.listIndexes();
    return indexes.filter(Boolean);
  } catch (error) {
    return chunkZ7LCIYK7_cjs.handleError(error, "Error listing indexes");
  }
}
async function describeIndex({
  mastra,
  vectorName,
  indexName
}) {
  try {
    if (!indexName) {
      throw new chunk64ITUOXI_cjs.HTTPException(400, { message: "Index name is required" });
    }
    const vector = getVector(mastra, vectorName);
    const stats = await vector.describeIndex({ indexName });
    return {
      dimension: stats.dimension,
      count: stats.count,
      metric: stats.metric?.toLowerCase()
    };
  } catch (error) {
    return chunkZ7LCIYK7_cjs.handleError(error, "Error describing index");
  }
}
async function deleteIndex({
  mastra,
  vectorName,
  indexName
}) {
  try {
    if (!indexName) {
      throw new chunk64ITUOXI_cjs.HTTPException(400, { message: "Index name is required" });
    }
    const vector = getVector(mastra, vectorName);
    await vector.deleteIndex({ indexName });
    return { success: true };
  } catch (error) {
    return chunkZ7LCIYK7_cjs.handleError(error, "Error deleting index");
  }
}
async function listVectorStores({ mastra }) {
  try {
    const vectors = mastra.listVectors();
    if (!vectors) {
      return { vectors: [] };
    }
    const vectorList = Object.entries(vectors).map(([name, vector]) => ({
      name,
      id: vector.id || name,
      // Use the key as fallback when vector has no id property
      type: vector.constructor.name
      // Add any other metadata that might be useful
    }));
    return { vectors: vectorList };
  } catch (error) {
    return chunkZ7LCIYK7_cjs.handleError(error, "Error listing vector stores");
  }
}
var UPSERT_VECTORS_ROUTE = chunkN54Q3AQR_cjs.createRoute({
  method: "POST",
  path: "/vector/:vectorName/upsert",
  responseType: "json",
  pathParamSchema: chunkLZ32IIH2_cjs.vectorNamePathParams,
  bodySchema: chunkLZ32IIH2_cjs.upsertVectorsBodySchema,
  responseSchema: chunkLZ32IIH2_cjs.upsertVectorsResponseSchema,
  summary: "Upsert vectors",
  description: "Inserts or updates vectors in the specified index",
  tags: ["Vectors"],
  requiresAuth: true,
  handler: async ({ mastra, vectorName, ...params }) => {
    try {
      const { indexName, vectors, metadata, ids } = params;
      if (!indexName || !vectors || !Array.isArray(vectors)) {
        throw new chunk64ITUOXI_cjs.HTTPException(400, { message: "Invalid request index. indexName and vectors array are required." });
      }
      const vector = getVector(mastra, vectorName);
      const result = await vector.upsert({ indexName, vectors, metadata, ids });
      return { ids: result };
    } catch (error) {
      return chunkZ7LCIYK7_cjs.handleError(error, "Error upserting vectors");
    }
  }
});
var CREATE_INDEX_ROUTE = chunkN54Q3AQR_cjs.createRoute({
  method: "POST",
  path: "/vector/:vectorName/create-index",
  responseType: "json",
  pathParamSchema: chunkLZ32IIH2_cjs.vectorNamePathParams,
  bodySchema: chunkLZ32IIH2_cjs.createIndexBodySchema,
  responseSchema: chunkLZ32IIH2_cjs.createIndexResponseSchema,
  summary: "Create index",
  description: "Creates a new vector index with the specified dimension and metric",
  tags: ["Vectors"],
  requiresAuth: true,
  handler: async ({ mastra, vectorName, ...params }) => {
    try {
      const { indexName, dimension, metric } = params;
      if (!indexName || typeof dimension !== "number" || dimension <= 0) {
        throw new chunk64ITUOXI_cjs.HTTPException(400, {
          message: "Invalid request index, indexName and positive dimension number are required."
        });
      }
      if (metric && !["cosine", "euclidean", "dotproduct"].includes(metric)) {
        throw new chunk64ITUOXI_cjs.HTTPException(400, { message: "Invalid metric. Must be one of: cosine, euclidean, dotproduct" });
      }
      const vector = getVector(mastra, vectorName);
      await vector.createIndex({ indexName, dimension, metric });
      return { success: true };
    } catch (error) {
      return chunkZ7LCIYK7_cjs.handleError(error, "Error creating index");
    }
  }
});
var QUERY_VECTORS_ROUTE = chunkN54Q3AQR_cjs.createRoute({
  method: "POST",
  path: "/vector/:vectorName/query",
  responseType: "json",
  pathParamSchema: chunkLZ32IIH2_cjs.vectorNamePathParams,
  bodySchema: chunkLZ32IIH2_cjs.queryVectorsBodySchema,
  responseSchema: chunkLZ32IIH2_cjs.queryVectorsResponseSchema,
  summary: "Query vectors",
  description: "Performs a similarity search on the vector index",
  tags: ["Vectors"],
  requiresAuth: true,
  handler: async ({ mastra, vectorName, ...params }) => {
    try {
      const { indexName, queryVector, topK, filter, includeVector } = params;
      if (!indexName || !queryVector || !Array.isArray(queryVector)) {
        throw new chunk64ITUOXI_cjs.HTTPException(400, {
          message: "Invalid request query. indexName and queryVector array are required."
        });
      }
      const vector = getVector(mastra, vectorName);
      const results = await vector.query({ indexName, queryVector, topK, filter, includeVector });
      return results;
    } catch (error) {
      return chunkZ7LCIYK7_cjs.handleError(error, "Error querying vectors");
    }
  }
});
var LIST_INDEXES_ROUTE = chunkN54Q3AQR_cjs.createRoute({
  method: "GET",
  path: "/vector/:vectorName/indexes",
  responseType: "json",
  pathParamSchema: chunkLZ32IIH2_cjs.vectorNamePathParams,
  responseSchema: chunkLZ32IIH2_cjs.listIndexesResponseSchema,
  summary: "List indexes",
  description: "Returns a list of all indexes in the vector store",
  tags: ["Vectors"],
  requiresAuth: true,
  handler: async ({ mastra, vectorName }) => {
    try {
      const vector = getVector(mastra, vectorName);
      const indexes = await vector.listIndexes();
      return indexes.filter(Boolean);
    } catch (error) {
      return chunkZ7LCIYK7_cjs.handleError(error, "Error listing indexes");
    }
  }
});
var DESCRIBE_INDEX_ROUTE = chunkN54Q3AQR_cjs.createRoute({
  method: "GET",
  path: "/vector/:vectorName/indexes/:indexName",
  responseType: "json",
  pathParamSchema: chunkLZ32IIH2_cjs.vectorIndexPathParams,
  responseSchema: chunkLZ32IIH2_cjs.describeIndexResponseSchema,
  summary: "Describe index",
  description: "Returns statistics and metadata for a specific index",
  tags: ["Vectors"],
  requiresAuth: true,
  handler: async ({ mastra, vectorName, indexName }) => {
    try {
      if (!indexName) {
        throw new chunk64ITUOXI_cjs.HTTPException(400, { message: "Index name is required" });
      }
      const vector = getVector(mastra, vectorName);
      const stats = await vector.describeIndex({ indexName });
      return {
        dimension: stats.dimension,
        count: stats.count,
        metric: stats.metric?.toLowerCase()
      };
    } catch (error) {
      return chunkZ7LCIYK7_cjs.handleError(error, "Error describing index");
    }
  }
});
var DELETE_INDEX_ROUTE = chunkN54Q3AQR_cjs.createRoute({
  method: "DELETE",
  path: "/vector/:vectorName/indexes/:indexName",
  responseType: "json",
  pathParamSchema: chunkLZ32IIH2_cjs.vectorIndexPathParams,
  responseSchema: chunkLZ32IIH2_cjs.deleteIndexResponseSchema,
  summary: "Delete index",
  description: "Deletes a vector index and all its data",
  tags: ["Vectors"],
  requiresAuth: true,
  handler: async ({ mastra, vectorName, indexName }) => {
    try {
      if (!indexName) {
        throw new chunk64ITUOXI_cjs.HTTPException(400, { message: "Index name is required" });
      }
      const vector = getVector(mastra, vectorName);
      await vector.deleteIndex({ indexName });
      return { success: true };
    } catch (error) {
      return chunkZ7LCIYK7_cjs.handleError(error, "Error deleting index");
    }
  }
});
var LIST_VECTORS_ROUTE = chunkN54Q3AQR_cjs.createRoute({
  method: "GET",
  path: "/vectors",
  responseType: "json",
  responseSchema: chunkLZ32IIH2_cjs.listVectorsResponseSchema,
  summary: "List vector stores",
  description: "Returns a list of all configured vector stores",
  tags: ["Vectors"],
  requiresAuth: true,
  handler: async ({ mastra }) => {
    try {
      const vectors = mastra.listVectors();
      if (!vectors) {
        return { vectors: [] };
      }
      const vectorList = Object.entries(vectors).map(([name, vector]) => ({
        id: vector.id || name,
        // Use the key as the ID since vectors might not have their own id property
        name,
        type: vector.constructor.name
      }));
      return { vectors: vectorList };
    } catch (error) {
      return chunkZ7LCIYK7_cjs.handleError(error, "Error listing vector stores");
    }
  }
});
var LIST_EMBEDDERS_ROUTE = chunkN54Q3AQR_cjs.createRoute({
  method: "GET",
  path: "/embedders",
  responseType: "json",
  responseSchema: chunkLZ32IIH2_cjs.listEmbeddersResponseSchema,
  summary: "List available embedder models",
  description: "Returns a list of all available embedding models",
  tags: ["Vectors"],
  requiresAuth: true,
  handler: async () => {
    try {
      const embeddersList = llm.EMBEDDING_MODELS.map((model) => ({
        id: `${model.provider}/${model.id}`,
        provider: model.provider,
        name: model.id,
        description: model.description || "",
        dimensions: model.dimensions,
        maxInputTokens: model.maxInputTokens
      }));
      return { embedders: embeddersList };
    } catch (error) {
      return chunkZ7LCIYK7_cjs.handleError(error, "Error listing embedders");
    }
  }
});

exports.CREATE_INDEX_ROUTE = CREATE_INDEX_ROUTE;
exports.DELETE_INDEX_ROUTE = DELETE_INDEX_ROUTE;
exports.DESCRIBE_INDEX_ROUTE = DESCRIBE_INDEX_ROUTE;
exports.LIST_EMBEDDERS_ROUTE = LIST_EMBEDDERS_ROUTE;
exports.LIST_INDEXES_ROUTE = LIST_INDEXES_ROUTE;
exports.LIST_VECTORS_ROUTE = LIST_VECTORS_ROUTE;
exports.QUERY_VECTORS_ROUTE = QUERY_VECTORS_ROUTE;
exports.UPSERT_VECTORS_ROUTE = UPSERT_VECTORS_ROUTE;
exports.createIndex = createIndex;
exports.deleteIndex = deleteIndex;
exports.describeIndex = describeIndex;
exports.listIndexes = listIndexes;
exports.listVectorStores = listVectorStores;
exports.queryVectors = queryVectors;
exports.upsertVectors = upsertVectors;
exports.vector_exports = vector_exports;
//# sourceMappingURL=chunk-BQ2JQTKA.cjs.map
//# sourceMappingURL=chunk-BQ2JQTKA.cjs.map