@mastra/qdrant 0.0.0-error-handler-fix-20251020202607 → 0.0.0-execa-dynamic-import-20260304221256

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -1,6 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  var error = require('@mastra/core/error');
4
+ var storage = require('@mastra/core/storage');
4
5
  var vector = require('@mastra/core/vector');
5
6
  var jsClientRest = require('@qdrant/js-client-rest');
6
7
  var filter = require('@mastra/core/vector/filter');
@@ -14,11 +15,15 @@ var QdrantFilterTranslator = class extends filter.BaseFilterTranslator {
14
15
  return {
15
16
  ...filter.BaseFilterTranslator.DEFAULT_OPERATORS,
16
17
  logical: ["$and", "$or", "$not"],
17
- array: ["$in", "$nin"],
18
+ array: ["$in", "$nin", "$all"],
18
19
  regex: ["$regex"],
20
+ element: ["$exists"],
19
21
  custom: ["$count", "$geo", "$nested", "$datetime", "$null", "$empty", "$hasId", "$hasVector"]
20
22
  };
21
23
  }
24
+ isOperator(key) {
25
+ return super.isOperator(key) || key === "$not";
26
+ }
22
27
  translate(filter) {
23
28
  if (this.isEmpty(filter)) return filter;
24
29
  this.validateFilter(filter);
@@ -45,7 +50,7 @@ var QdrantFilterTranslator = class extends filter.BaseFilterTranslator {
45
50
  return node.length === 0 ? { is_empty: { key: fieldKey } } : this.createCondition("match", { any: this.normalizeArrayValues(node) }, fieldKey);
46
51
  }
47
52
  const entries = Object.entries(node);
48
- const logicalResult = this.handleLogicalOperators(entries, isNested);
53
+ const logicalResult = this.handleLogicalOperators(entries, isNested, fieldKey);
49
54
  if (logicalResult) {
50
55
  return logicalResult;
51
56
  }
@@ -67,8 +72,11 @@ var QdrantFilterTranslator = class extends filter.BaseFilterTranslator {
67
72
  return { must: conditions };
68
73
  }
69
74
  }
70
- handleLogicalOperators(entries, isNested) {
75
+ handleLogicalOperators(entries, isNested, fieldKey) {
71
76
  const firstKey = entries[0]?.[0];
77
+ if (firstKey === "$not" && fieldKey) {
78
+ return null;
79
+ }
72
80
  if (firstKey && this.isLogicalOperator(firstKey) && !this.isCustomOperator(firstKey)) {
73
81
  const [key, value] = entries[0];
74
82
  const qdrantOp = this.getQdrantLogicalOp(key);
@@ -93,7 +101,34 @@ var QdrantFilterTranslator = class extends filter.BaseFilterTranslator {
93
101
  conditions.push(customOp);
94
102
  } else if (this.isOperator(key)) {
95
103
  const opResult = this.translateOperatorValue(key, value);
96
- if (opResult.range) {
104
+ if (opResult._specialNull) {
105
+ conditions.push({ is_null: { key: fieldKey } });
106
+ } else if (opResult._specialNotNull) {
107
+ conditions.push({ must_not: [{ is_null: { key: fieldKey } }] });
108
+ } else if (opResult._specialNe) {
109
+ conditions.push({
110
+ must_not: [{ key: fieldKey, match: { value: opResult._specialNe } }]
111
+ });
112
+ } else if (opResult._specialNin) {
113
+ conditions.push({
114
+ must_not: [{ key: fieldKey, match: { any: opResult._specialNin } }]
115
+ });
116
+ } else if (opResult._specialAll) {
117
+ for (const val of opResult._specialAll) {
118
+ conditions.push({ key: fieldKey, match: { value: val } });
119
+ }
120
+ } else if (opResult._specialExists) {
121
+ conditions.push({
122
+ must_not: [{ is_null: { key: fieldKey } }, { is_empty: { key: fieldKey } }]
123
+ });
124
+ } else if (opResult._specialNotExists) {
125
+ conditions.push({
126
+ should: [{ is_null: { key: fieldKey } }, { is_empty: { key: fieldKey } }]
127
+ });
128
+ } else if (opResult._specialNot) {
129
+ const innerResult = this.translateNode(opResult._specialNot, true, fieldKey);
130
+ conditions.push({ must_not: [innerResult] });
131
+ } else if (opResult.range) {
97
132
  Object.assign(range, opResult.range);
98
133
  } else {
99
134
  matchCondition = opResult;
@@ -164,9 +199,15 @@ var QdrantFilterTranslator = class extends filter.BaseFilterTranslator {
164
199
  const normalizedValue = this.normalizeComparisonValue(value);
165
200
  switch (operator) {
166
201
  case "$eq":
202
+ if (value === null) {
203
+ return { _specialNull: true };
204
+ }
167
205
  return { value: normalizedValue };
168
206
  case "$ne":
169
- return { except: [normalizedValue] };
207
+ if (value === null) {
208
+ return { _specialNotNull: true };
209
+ }
210
+ return { _specialNe: normalizedValue };
170
211
  case "$gt":
171
212
  return { range: { gt: normalizedValue } };
172
213
  case "$gte":
@@ -178,15 +219,15 @@ var QdrantFilterTranslator = class extends filter.BaseFilterTranslator {
178
219
  case "$in":
179
220
  return { any: this.normalizeArrayValues(value) };
180
221
  case "$nin":
181
- return { except: this.normalizeArrayValues(value) };
222
+ return { _specialNin: this.normalizeArrayValues(value) };
182
223
  case "$regex":
183
224
  return { text: value };
184
- case "exists":
185
- return value ? {
186
- must_not: [{ is_null: { key: value } }, { is_empty: { key: value } }]
187
- } : {
188
- is_empty: { key: value }
189
- };
225
+ case "$all":
226
+ return { _specialAll: this.normalizeArrayValues(value) };
227
+ case "$exists":
228
+ return value ? { _specialExists: true } : { _specialNotExists: true };
229
+ case "$not":
230
+ return { _specialNot: value };
190
231
  default:
191
232
  throw new Error(`Unsupported operator: ${operator}`);
192
233
  }
@@ -242,38 +283,64 @@ var QdrantVector = class extends vector.MastraVector {
242
283
  client;
243
284
  /**
244
285
  * Creates a new QdrantVector client.
286
+ * @param id - The unique identifier for this vector store instance.
245
287
  * @param url - The URL of the Qdrant server.
246
288
  * @param apiKey - The API key for Qdrant.
247
289
  * @param https - Whether to use HTTPS.
248
290
  */
249
- constructor({ url, apiKey, https, ...rest }) {
250
- super();
251
- const baseClient = new jsClientRest.QdrantClient({
252
- url,
253
- apiKey,
254
- https,
255
- ...rest
256
- });
257
- const telemetry = this.__getTelemetry();
258
- this.client = telemetry?.traceClass(baseClient, {
259
- spanNamePrefix: "qdrant-vector",
260
- attributes: {
261
- "vector.type": "qdrant"
262
- }
263
- }) ?? baseClient;
291
+ constructor({ id, ...qdrantParams }) {
292
+ super({ id });
293
+ this.client = new jsClientRest.QdrantClient(qdrantParams);
294
+ }
295
+ /**
296
+ * Validates that a named vector exists in the collection.
297
+ * @param indexName - The name of the collection to check.
298
+ * @param vectorName - The name of the vector space to validate.
299
+ * @throws Error if the vector name doesn't exist in the collection.
300
+ */
301
+ async validateVectorName(indexName, vectorName) {
302
+ const { config } = await this.client.getCollection(indexName);
303
+ const vectorsConfig = config.params.vectors;
304
+ const isNamedVectors = vectorsConfig && typeof vectorsConfig === "object" && !("size" in vectorsConfig);
305
+ if (!isNamedVectors || !(vectorName in vectorsConfig)) {
306
+ throw new Error(`Vector name "${vectorName}" does not exist in collection "${indexName}"`);
307
+ }
264
308
  }
265
- async upsert({ indexName, vectors, metadata, ids }) {
309
+ /**
310
+ * Upserts vectors into the index.
311
+ * @param indexName - The name of the index to upsert into.
312
+ * @param vectors - Array of embedding vectors.
313
+ * @param metadata - Optional metadata for each vector.
314
+ * @param ids - Optional vector IDs (auto-generated if not provided).
315
+ * @param vectorName - Optional name of the vector space when using named vectors.
316
+ */
317
+ async upsert({ indexName, vectors, metadata, ids, vectorName }) {
318
+ vector.validateUpsertInput("QDRANT", vectors, metadata, ids);
266
319
  const pointIds = ids || vectors.map(() => crypto.randomUUID());
320
+ if (vectorName) {
321
+ try {
322
+ await this.validateVectorName(indexName, vectorName);
323
+ } catch (validationError) {
324
+ throw new error.MastraError(
325
+ {
326
+ id: storage.createVectorErrorId("QDRANT", "UPSERT", "INVALID_VECTOR_NAME"),
327
+ domain: error.ErrorDomain.STORAGE,
328
+ category: error.ErrorCategory.USER,
329
+ details: { indexName, vectorName }
330
+ },
331
+ validationError
332
+ );
333
+ }
334
+ }
267
335
  const records = vectors.map((vector, i) => ({
268
336
  id: pointIds[i],
269
- vector,
337
+ vector: vectorName ? { [vectorName]: vector } : vector,
270
338
  payload: metadata?.[i] || {}
271
339
  }));
272
340
  try {
273
341
  for (let i = 0; i < records.length; i += BATCH_SIZE) {
274
342
  const batch = records.slice(i, i + BATCH_SIZE);
275
343
  await this.client.upsert(indexName, {
276
- // @ts-expect-error
277
344
  points: batch,
278
345
  wait: true
279
346
  });
@@ -282,50 +349,118 @@ var QdrantVector = class extends vector.MastraVector {
282
349
  } catch (error$1) {
283
350
  throw new error.MastraError(
284
351
  {
285
- id: "STORAGE_QDRANT_VECTOR_UPSERT_FAILED",
352
+ id: storage.createVectorErrorId("QDRANT", "UPSERT", "FAILED"),
286
353
  domain: error.ErrorDomain.STORAGE,
287
354
  category: error.ErrorCategory.THIRD_PARTY,
288
- details: { indexName, vectorCount: vectors.length }
355
+ details: { indexName, vectorCount: vectors.length, ...vectorName && { vectorName } }
289
356
  },
290
357
  error$1
291
358
  );
292
359
  }
293
360
  }
294
- async createIndex({ indexName, dimension, metric = "cosine" }) {
361
+ /**
362
+ * Creates a new index (collection) in Qdrant.
363
+ * Supports both single vector and named vector configurations.
364
+ *
365
+ * @param indexName - The name of the collection to create.
366
+ * @param dimension - Vector dimension (required for single vector mode).
367
+ * @param metric - Distance metric (default: 'cosine').
368
+ * @param namedVectors - Optional named vector configurations for multi-vector collections.
369
+ *
370
+ * @example
371
+ * ```ts
372
+ * // Single vector collection
373
+ * await qdrant.createIndex({ indexName: 'docs', dimension: 768, metric: 'cosine' });
374
+ *
375
+ * // Named vectors collection
376
+ * await qdrant.createIndex({
377
+ * indexName: 'multi-modal',
378
+ * dimension: 768, // Used as fallback, can be omitted with namedVectors
379
+ * namedVectors: {
380
+ * text: { size: 768, distance: 'cosine' },
381
+ * image: { size: 512, distance: 'euclidean' },
382
+ * },
383
+ * });
384
+ * ```
385
+ */
386
+ async createIndex({ indexName, dimension, metric = "cosine", namedVectors }) {
295
387
  try {
296
- if (!Number.isInteger(dimension) || dimension <= 0) {
297
- throw new Error("Dimension must be a positive integer");
298
- }
299
- if (!DISTANCE_MAPPING[metric]) {
300
- throw new Error(`Invalid metric: "${metric}". Must be one of: cosine, euclidean, dotproduct`);
388
+ if (namedVectors) {
389
+ if (Object.keys(namedVectors).length === 0) {
390
+ throw new Error("namedVectors must contain at least one named vector configuration");
391
+ }
392
+ for (const [name, config] of Object.entries(namedVectors)) {
393
+ if (!Number.isInteger(config.size) || config.size <= 0) {
394
+ throw new Error(`Named vector "${name}": size must be a positive integer`);
395
+ }
396
+ if (!DISTANCE_MAPPING[config.distance]) {
397
+ throw new Error(
398
+ `Named vector "${name}": invalid distance "${config.distance}". Must be one of: cosine, euclidean, dotproduct`
399
+ );
400
+ }
401
+ }
402
+ } else {
403
+ if (!Number.isInteger(dimension) || dimension <= 0) {
404
+ throw new Error("Dimension must be a positive integer");
405
+ }
406
+ if (!DISTANCE_MAPPING[metric]) {
407
+ throw new Error(`Invalid metric: "${metric}". Must be one of: cosine, euclidean, dotproduct`);
408
+ }
301
409
  }
302
410
  } catch (validationError) {
303
411
  throw new error.MastraError(
304
412
  {
305
- id: "STORAGE_QDRANT_VECTOR_CREATE_INDEX_INVALID_ARGS",
413
+ id: storage.createVectorErrorId("QDRANT", "CREATE_INDEX", "INVALID_ARGS"),
306
414
  domain: error.ErrorDomain.STORAGE,
307
415
  category: error.ErrorCategory.USER,
308
- details: { indexName, dimension, metric }
416
+ details: {
417
+ indexName,
418
+ dimension,
419
+ metric,
420
+ ...namedVectors && { namedVectorNames: Object.keys(namedVectors).join(", ") }
421
+ }
309
422
  },
310
423
  validationError
311
424
  );
312
425
  }
313
426
  try {
314
- await this.client.createCollection(indexName, {
315
- vectors: {
316
- size: dimension,
317
- distance: DISTANCE_MAPPING[metric]
318
- }
319
- });
427
+ if (namedVectors) {
428
+ const namedVectorsConfig = Object.entries(namedVectors).reduce(
429
+ (acc, [name, config]) => {
430
+ acc[name] = {
431
+ size: config.size,
432
+ distance: DISTANCE_MAPPING[config.distance]
433
+ };
434
+ return acc;
435
+ },
436
+ {}
437
+ );
438
+ await this.client.createCollection(indexName, {
439
+ vectors: namedVectorsConfig
440
+ });
441
+ } else {
442
+ await this.client.createCollection(indexName, {
443
+ vectors: {
444
+ size: dimension,
445
+ distance: DISTANCE_MAPPING[metric]
446
+ }
447
+ });
448
+ }
320
449
  } catch (error$1) {
321
450
  const message = error$1?.message || error$1?.toString();
322
451
  if (error$1?.status === 409 || typeof message === "string" && message.toLowerCase().includes("exists")) {
323
- await this.validateExistingIndex(indexName, dimension, metric);
452
+ if (!namedVectors) {
453
+ await this.validateExistingIndex(indexName, dimension, metric);
454
+ } else {
455
+ this.logger.info(
456
+ `Collection "${indexName}" already exists. Skipping validation for named vectors configuration.`
457
+ );
458
+ }
324
459
  return;
325
460
  }
326
461
  throw new error.MastraError(
327
462
  {
328
- id: "STORAGE_QDRANT_VECTOR_CREATE_INDEX_FAILED",
463
+ id: storage.createVectorErrorId("QDRANT", "CREATE_INDEX", "FAILED"),
329
464
  domain: error.ErrorDomain.STORAGE,
330
465
  category: error.ErrorCategory.THIRD_PARTY,
331
466
  details: { indexName, dimension, metric }
@@ -338,13 +473,33 @@ var QdrantVector = class extends vector.MastraVector {
338
473
  const translator = new QdrantFilterTranslator();
339
474
  return translator.translate(filter);
340
475
  }
476
+ /**
477
+ * Queries the index for similar vectors.
478
+ *
479
+ * @param indexName - The name of the index to query.
480
+ * @param queryVector - The query vector to find similar vectors for.
481
+ * @param topK - Number of results to return (default: 10).
482
+ * @param filter - Optional metadata filter.
483
+ * @param includeVector - Whether to include vectors in results (default: false).
484
+ * @param using - Name of the vector space to query when using named vectors.
485
+ */
341
486
  async query({
342
487
  indexName,
343
488
  queryVector,
344
489
  topK = 10,
345
490
  filter,
346
- includeVector = false
491
+ includeVector = false,
492
+ using
347
493
  }) {
494
+ if (!queryVector) {
495
+ throw new error.MastraError({
496
+ id: storage.createVectorErrorId("QDRANT", "QUERY", "MISSING_VECTOR"),
497
+ text: "queryVector is required for Qdrant queries. Metadata-only queries are not supported by this vector store.",
498
+ domain: error.ErrorDomain.STORAGE,
499
+ category: error.ErrorCategory.USER,
500
+ details: { indexName }
501
+ });
502
+ }
348
503
  const translatedFilter = this.transformFilter(filter) ?? {};
349
504
  try {
350
505
  const results = (await this.client.query(indexName, {
@@ -352,15 +507,20 @@ var QdrantVector = class extends vector.MastraVector {
352
507
  limit: topK,
353
508
  filter: translatedFilter,
354
509
  with_payload: true,
355
- with_vector: includeVector
510
+ with_vector: includeVector,
511
+ ...using ? { using } : {}
356
512
  })).points;
357
513
  return results.map((match) => {
358
514
  let vector = [];
359
- if (includeVector) {
515
+ if (includeVector && match.vector != null) {
360
516
  if (Array.isArray(match.vector)) {
361
517
  vector = match.vector;
362
518
  } else if (typeof match.vector === "object" && match.vector !== null) {
363
- vector = Object.values(match.vector).filter((v) => typeof v === "number");
519
+ const namedVectors = match.vector;
520
+ const sourceArray = using && Array.isArray(namedVectors[using]) ? namedVectors[using] : Object.values(namedVectors).find((v) => Array.isArray(v));
521
+ if (sourceArray) {
522
+ vector = sourceArray.filter((v) => typeof v === "number");
523
+ }
364
524
  }
365
525
  }
366
526
  return {
@@ -373,10 +533,10 @@ var QdrantVector = class extends vector.MastraVector {
373
533
  } catch (error$1) {
374
534
  throw new error.MastraError(
375
535
  {
376
- id: "STORAGE_QDRANT_VECTOR_QUERY_FAILED",
536
+ id: storage.createVectorErrorId("QDRANT", "QUERY", "FAILED"),
377
537
  domain: error.ErrorDomain.STORAGE,
378
538
  category: error.ErrorCategory.THIRD_PARTY,
379
- details: { indexName, topK }
539
+ details: { indexName, topK, ...using && { using } }
380
540
  },
381
541
  error$1
382
542
  );
@@ -389,7 +549,7 @@ var QdrantVector = class extends vector.MastraVector {
389
549
  } catch (error$1) {
390
550
  throw new error.MastraError(
391
551
  {
392
- id: "STORAGE_QDRANT_VECTOR_LIST_INDEXES_FAILED",
552
+ id: storage.createVectorErrorId("QDRANT", "LIST_INDEXES", "FAILED"),
393
553
  domain: error.ErrorDomain.STORAGE,
394
554
  category: error.ErrorCategory.THIRD_PARTY
395
555
  },
@@ -410,13 +570,13 @@ var QdrantVector = class extends vector.MastraVector {
410
570
  return {
411
571
  dimension: config.params.vectors?.size,
412
572
  count: points_count || 0,
413
- // @ts-expect-error
573
+ // @ts-expect-error - Object.keys returns string[] but DISTANCE_MAPPING keys are typed
414
574
  metric: Object.keys(DISTANCE_MAPPING).find((key) => DISTANCE_MAPPING[key] === distance)
415
575
  };
416
576
  } catch (error$1) {
417
577
  throw new error.MastraError(
418
578
  {
419
- id: "STORAGE_QDRANT_VECTOR_DESCRIBE_INDEX_FAILED",
579
+ id: storage.createVectorErrorId("QDRANT", "DESCRIBE_INDEX", "FAILED"),
420
580
  domain: error.ErrorDomain.STORAGE,
421
581
  category: error.ErrorCategory.THIRD_PARTY,
422
582
  details: { indexName }
@@ -429,9 +589,14 @@ var QdrantVector = class extends vector.MastraVector {
429
589
  try {
430
590
  await this.client.deleteCollection(indexName);
431
591
  } catch (error$1) {
592
+ const errorMessage = error$1?.message || error$1?.toString() || "";
593
+ if (error$1?.status === 404 || errorMessage.toLowerCase().includes("not found") || errorMessage.toLowerCase().includes("not exist")) {
594
+ this.logger.info(`Collection ${indexName} does not exist, treating as already deleted`);
595
+ return;
596
+ }
432
597
  throw new error.MastraError(
433
598
  {
434
- id: "STORAGE_QDRANT_VECTOR_DELETE_INDEX_FAILED",
599
+ id: storage.createVectorErrorId("QDRANT", "DELETE_INDEX", "FAILED"),
435
600
  domain: error.ErrorDomain.STORAGE,
436
601
  category: error.ErrorCategory.THIRD_PARTY,
437
602
  details: { indexName }
@@ -441,66 +606,143 @@ var QdrantVector = class extends vector.MastraVector {
441
606
  }
442
607
  }
443
608
  /**
444
- * Updates a vector by its ID with the provided vector and/or metadata.
445
- * @param indexName - The name of the index containing the vector.
446
- * @param id - The ID of the vector to update.
609
+ * Updates a vector by its ID or multiple vectors matching a filter.
610
+ * @param indexName - The name of the index containing the vector(s).
611
+ * @param id - The ID of the vector to update (mutually exclusive with filter).
612
+ * @param filter - Filter to match multiple vectors to update (mutually exclusive with id).
447
613
  * @param update - An object containing the vector and/or metadata to update.
448
614
  * @param update.vector - An optional array of numbers representing the new vector.
449
615
  * @param update.metadata - An optional record containing the new metadata.
450
616
  * @returns A promise that resolves when the update is complete.
451
617
  * @throws Will throw an error if no updates are provided or if the update operation fails.
452
618
  */
453
- async updateVector({ indexName, id, update }) {
454
- try {
455
- if (!update.vector && !update.metadata) {
456
- throw new Error("No updates provided");
457
- }
458
- } catch (validationError) {
459
- throw new error.MastraError(
460
- {
461
- id: "STORAGE_QDRANT_VECTOR_UPDATE_VECTOR_INVALID_ARGS",
462
- domain: error.ErrorDomain.STORAGE,
463
- category: error.ErrorCategory.USER,
464
- details: { indexName, id }
465
- },
466
- validationError
467
- );
619
+ async updateVector({ indexName, id, filter, update }) {
620
+ if (id && filter) {
621
+ throw new error.MastraError({
622
+ id: storage.createVectorErrorId("QDRANT", "UPDATE_VECTOR", "MUTUALLY_EXCLUSIVE"),
623
+ text: "Cannot specify both id and filter - they are mutually exclusive",
624
+ domain: error.ErrorDomain.STORAGE,
625
+ category: error.ErrorCategory.USER,
626
+ details: { indexName }
627
+ });
628
+ }
629
+ if (!id && !filter) {
630
+ throw new error.MastraError({
631
+ id: storage.createVectorErrorId("QDRANT", "UPDATE_VECTOR", "NO_TARGET"),
632
+ text: "Either id or filter must be provided",
633
+ domain: error.ErrorDomain.STORAGE,
634
+ category: error.ErrorCategory.USER,
635
+ details: { indexName }
636
+ });
637
+ }
638
+ if (!update.vector && !update.metadata) {
639
+ throw new error.MastraError({
640
+ id: storage.createVectorErrorId("QDRANT", "UPDATE_VECTOR", "NO_PAYLOAD"),
641
+ text: "No updates provided",
642
+ domain: error.ErrorDomain.STORAGE,
643
+ category: error.ErrorCategory.USER,
644
+ details: {
645
+ indexName,
646
+ ...id && { id }
647
+ }
648
+ });
649
+ }
650
+ if (filter && Object.keys(filter).length === 0) {
651
+ throw new error.MastraError({
652
+ id: storage.createVectorErrorId("QDRANT", "UPDATE_VECTOR", "EMPTY_FILTER"),
653
+ text: "Filter cannot be an empty filter object",
654
+ domain: error.ErrorDomain.STORAGE,
655
+ category: error.ErrorCategory.USER,
656
+ details: { indexName }
657
+ });
468
658
  }
469
- const pointId = this.parsePointId(id);
470
659
  try {
471
- if (update.metadata && !update.vector) {
472
- await this.client.setPayload(indexName, { payload: update.metadata, points: [pointId] });
473
- return;
474
- }
475
- if (update.vector && !update.metadata) {
476
- await this.client.updateVectors(indexName, {
477
- points: [
478
- {
479
- id: pointId,
480
- vector: update.vector
481
- }
482
- ]
483
- });
484
- return;
485
- }
486
- if (update.vector && update.metadata) {
487
- const point = {
488
- id: pointId,
489
- vector: update.vector,
490
- payload: update.metadata
491
- };
492
- await this.client.upsert(indexName, {
493
- points: [point]
494
- });
495
- return;
660
+ if (id) {
661
+ const pointId = this.parsePointId(id);
662
+ if (update.metadata && !update.vector) {
663
+ await this.client.setPayload(indexName, { payload: update.metadata, points: [pointId] });
664
+ return;
665
+ }
666
+ if (update.vector && !update.metadata) {
667
+ await this.client.updateVectors(indexName, {
668
+ points: [
669
+ {
670
+ id: pointId,
671
+ vector: update.vector
672
+ }
673
+ ]
674
+ });
675
+ return;
676
+ }
677
+ if (update.vector && update.metadata) {
678
+ const point = {
679
+ id: pointId,
680
+ vector: update.vector,
681
+ payload: update.metadata
682
+ };
683
+ await this.client.upsert(indexName, {
684
+ points: [point]
685
+ });
686
+ return;
687
+ }
688
+ } else if (filter) {
689
+ const translatedFilter = this.transformFilter(filter);
690
+ const matchingPoints = [];
691
+ let offset = void 0;
692
+ do {
693
+ const scrollResult = await this.client.scroll(indexName, {
694
+ filter: translatedFilter,
695
+ limit: 100,
696
+ offset,
697
+ with_payload: false,
698
+ with_vector: update.vector ? false : true
699
+ // Only fetch vectors if not updating them
700
+ });
701
+ matchingPoints.push(
702
+ ...scrollResult.points.map((point) => ({
703
+ id: point.id,
704
+ vector: Array.isArray(point.vector) ? point.vector : void 0
705
+ }))
706
+ );
707
+ const nextOffset = scrollResult.next_page_offset;
708
+ offset = typeof nextOffset === "string" || typeof nextOffset === "number" ? nextOffset : void 0;
709
+ } while (offset !== void 0);
710
+ if (matchingPoints.length === 0) {
711
+ return;
712
+ }
713
+ const pointIds = matchingPoints.map((p) => p.id);
714
+ if (update.metadata && !update.vector) {
715
+ await this.client.setPayload(indexName, { payload: update.metadata, points: pointIds });
716
+ return;
717
+ }
718
+ if (update.vector) {
719
+ const points = matchingPoints.map((p) => ({
720
+ id: p.id,
721
+ vector: update.vector,
722
+ payload: update.metadata || {}
723
+ }));
724
+ for (let i = 0; i < points.length; i += BATCH_SIZE) {
725
+ const batch = points.slice(i, i + BATCH_SIZE);
726
+ await this.client.upsert(indexName, {
727
+ points: batch,
728
+ wait: true
729
+ });
730
+ }
731
+ return;
732
+ }
496
733
  }
497
734
  } catch (error$1) {
735
+ if (error$1 instanceof error.MastraError) throw error$1;
498
736
  throw new error.MastraError(
499
737
  {
500
- id: "STORAGE_QDRANT_VECTOR_UPDATE_VECTOR_FAILED",
738
+ id: storage.createVectorErrorId("QDRANT", "UPDATE_VECTOR", "FAILED"),
501
739
  domain: error.ErrorDomain.STORAGE,
502
740
  category: error.ErrorCategory.THIRD_PARTY,
503
- details: { indexName, id }
741
+ details: {
742
+ indexName,
743
+ ...id && { id },
744
+ ...filter && { filter: JSON.stringify(filter) }
745
+ }
504
746
  },
505
747
  error$1
506
748
  );
@@ -522,10 +764,13 @@ var QdrantVector = class extends vector.MastraVector {
522
764
  } catch (error$1) {
523
765
  throw new error.MastraError(
524
766
  {
525
- id: "STORAGE_QDRANT_VECTOR_DELETE_VECTOR_FAILED",
767
+ id: storage.createVectorErrorId("QDRANT", "DELETE_VECTOR", "FAILED"),
526
768
  domain: error.ErrorDomain.STORAGE,
527
769
  category: error.ErrorCategory.THIRD_PARTY,
528
- details: { indexName, id }
770
+ details: {
771
+ indexName,
772
+ ...id && { id }
773
+ }
529
774
  },
530
775
  error$1
531
776
  );
@@ -566,6 +811,245 @@ var QdrantVector = class extends vector.MastraVector {
566
811
  }
567
812
  return id;
568
813
  }
814
+ /**
815
+ * Deletes multiple vectors by IDs or filter.
816
+ * @param indexName - The name of the index containing the vectors.
817
+ * @param ids - Array of vector IDs to delete (mutually exclusive with filter).
818
+ * @param filter - Filter to match vectors to delete (mutually exclusive with ids).
819
+ * @returns A promise that resolves when the deletion is complete.
820
+ * @throws Will throw an error if both ids and filter are provided, or if neither is provided.
821
+ */
822
+ async deleteVectors({ indexName, filter, ids }) {
823
+ if (ids && filter) {
824
+ throw new error.MastraError({
825
+ id: storage.createVectorErrorId("QDRANT", "DELETE_VECTORS", "MUTUALLY_EXCLUSIVE"),
826
+ text: "Cannot specify both ids and filter - they are mutually exclusive",
827
+ domain: error.ErrorDomain.STORAGE,
828
+ category: error.ErrorCategory.USER,
829
+ details: { indexName }
830
+ });
831
+ }
832
+ if (!ids && !filter) {
833
+ throw new error.MastraError({
834
+ id: storage.createVectorErrorId("QDRANT", "DELETE_VECTORS", "NO_TARGET"),
835
+ text: "Either filter or ids must be provided",
836
+ domain: error.ErrorDomain.STORAGE,
837
+ category: error.ErrorCategory.USER,
838
+ details: { indexName }
839
+ });
840
+ }
841
+ if (ids && ids.length === 0) {
842
+ throw new error.MastraError({
843
+ id: storage.createVectorErrorId("QDRANT", "DELETE_VECTORS", "EMPTY_IDS"),
844
+ text: "Cannot delete with empty ids array",
845
+ domain: error.ErrorDomain.STORAGE,
846
+ category: error.ErrorCategory.USER,
847
+ details: { indexName }
848
+ });
849
+ }
850
+ if (filter && Object.keys(filter).length === 0) {
851
+ throw new error.MastraError({
852
+ id: storage.createVectorErrorId("QDRANT", "DELETE_VECTORS", "EMPTY_FILTER"),
853
+ text: "Cannot delete with empty filter object",
854
+ domain: error.ErrorDomain.STORAGE,
855
+ category: error.ErrorCategory.USER,
856
+ details: { indexName }
857
+ });
858
+ }
859
+ try {
860
+ if (ids) {
861
+ const pointIds = ids.map((id) => this.parsePointId(id));
862
+ try {
863
+ await this.client.delete(indexName, {
864
+ points: pointIds,
865
+ wait: true
866
+ });
867
+ } catch (error) {
868
+ const message = error?.message || error?.toString() || "";
869
+ if (message.toLowerCase().includes("bad request")) {
870
+ return;
871
+ }
872
+ throw error;
873
+ }
874
+ } else if (filter) {
875
+ const translatedFilter = this.transformFilter(filter) ?? {};
876
+ await this.client.delete(indexName, {
877
+ filter: translatedFilter,
878
+ wait: true
879
+ });
880
+ }
881
+ } catch (error$1) {
882
+ if (error$1 instanceof error.MastraError) throw error$1;
883
+ throw new error.MastraError(
884
+ {
885
+ id: storage.createVectorErrorId("QDRANT", "DELETE_VECTORS", "FAILED"),
886
+ domain: error.ErrorDomain.STORAGE,
887
+ category: error.ErrorCategory.THIRD_PARTY,
888
+ details: {
889
+ indexName,
890
+ ...filter && { filter: JSON.stringify(filter) },
891
+ ...ids && { idsCount: ids.length }
892
+ }
893
+ },
894
+ error$1
895
+ );
896
+ }
897
+ }
898
+ /**
899
+ * Creates a payload index on a Qdrant collection to enable efficient filtering on metadata fields.
900
+ *
901
+ * This is required for Qdrant Cloud and any Qdrant instance with `strict_mode_config = true`,
902
+ * where metadata (payload) fields must be explicitly indexed before they can be used for filtering.
903
+ *
904
+ * @param params - The parameters for creating the payload index.
905
+ * @param params.indexName - The name of the collection (index) to create the payload index on.
906
+ * @param params.fieldName - The name of the payload field to index.
907
+ * @param params.fieldSchema - The schema type for the field (e.g., 'keyword', 'integer', 'text').
908
+ * @param params.wait - Whether to wait for the operation to complete. Defaults to true.
909
+ * @returns A promise that resolves when the index is created (idempotent if index already exists).
910
+ * @throws Will throw a MastraError if arguments are invalid or if the operation fails.
911
+ *
912
+ * @example
913
+ * ```ts
914
+ * // Create a keyword index for filtering by source
915
+ * await qdrant.createPayloadIndex({
916
+ * indexName: 'my-collection',
917
+ * fieldName: 'source',
918
+ * fieldSchema: 'keyword',
919
+ * });
920
+ *
921
+ * // Create an integer index for numeric filtering
922
+ * await qdrant.createPayloadIndex({
923
+ * indexName: 'my-collection',
924
+ * fieldName: 'price',
925
+ * fieldSchema: 'integer',
926
+ * });
927
+ * ```
928
+ *
929
+ * @see https://qdrant.tech/documentation/concepts/indexing/#payload-index
930
+ */
931
+ async createPayloadIndex({
932
+ indexName,
933
+ fieldName,
934
+ fieldSchema,
935
+ wait = true
936
+ }) {
937
+ const validSchemas = [
938
+ "keyword",
939
+ "integer",
940
+ "float",
941
+ "geo",
942
+ "text",
943
+ "bool",
944
+ "datetime",
945
+ "uuid"
946
+ ];
947
+ if (!indexName || typeof indexName !== "string" || indexName.trim() === "") {
948
+ throw new error.MastraError({
949
+ id: storage.createVectorErrorId("QDRANT", "CREATE_PAYLOAD_INDEX", "INVALID_ARGS"),
950
+ text: "indexName must be a non-empty string",
951
+ domain: error.ErrorDomain.STORAGE,
952
+ category: error.ErrorCategory.USER,
953
+ details: { indexName, fieldName, fieldSchema }
954
+ });
955
+ }
956
+ if (!fieldName || typeof fieldName !== "string" || fieldName.trim() === "") {
957
+ throw new error.MastraError({
958
+ id: storage.createVectorErrorId("QDRANT", "CREATE_PAYLOAD_INDEX", "INVALID_ARGS"),
959
+ text: "fieldName must be a non-empty string",
960
+ domain: error.ErrorDomain.STORAGE,
961
+ category: error.ErrorCategory.USER,
962
+ details: { indexName, fieldName, fieldSchema }
963
+ });
964
+ }
965
+ if (!validSchemas.includes(fieldSchema)) {
966
+ throw new error.MastraError({
967
+ id: storage.createVectorErrorId("QDRANT", "CREATE_PAYLOAD_INDEX", "INVALID_ARGS"),
968
+ text: `fieldSchema must be one of: ${validSchemas.join(", ")}`,
969
+ domain: error.ErrorDomain.STORAGE,
970
+ category: error.ErrorCategory.USER,
971
+ details: { indexName, fieldName, fieldSchema }
972
+ });
973
+ }
974
+ try {
975
+ await this.client.createPayloadIndex(indexName, {
976
+ field_name: fieldName,
977
+ field_schema: fieldSchema,
978
+ wait
979
+ });
980
+ } catch (error$1) {
981
+ const message = error$1?.message || error$1?.toString() || "";
982
+ if (error$1?.status === 409 || message.toLowerCase().includes("exists")) {
983
+ this.logger.info(`Payload index for field "${fieldName}" already exists on collection "${indexName}"`);
984
+ return;
985
+ }
986
+ throw new error.MastraError(
987
+ {
988
+ id: storage.createVectorErrorId("QDRANT", "CREATE_PAYLOAD_INDEX", "FAILED"),
989
+ domain: error.ErrorDomain.STORAGE,
990
+ category: error.ErrorCategory.THIRD_PARTY,
991
+ details: { indexName, fieldName, fieldSchema }
992
+ },
993
+ error$1
994
+ );
995
+ }
996
+ }
997
+ /**
998
+ * Deletes a payload index from a Qdrant collection.
999
+ *
1000
+ * @param params - The parameters for deleting the payload index.
1001
+ * @param params.indexName - The name of the collection (index) to delete the payload index from.
1002
+ * @param params.fieldName - The name of the payload field index to delete.
1003
+ * @param params.wait - Whether to wait for the operation to complete. Defaults to true.
1004
+ * @returns A promise that resolves when the index is deleted (idempotent if index doesn't exist).
1005
+ * @throws Will throw a MastraError if the operation fails.
1006
+ *
1007
+ * @example
1008
+ * ```ts
1009
+ * await qdrant.deletePayloadIndex({
1010
+ * indexName: 'my-collection',
1011
+ * fieldName: 'source',
1012
+ * });
1013
+ * ```
1014
+ */
1015
+ async deletePayloadIndex({ indexName, fieldName, wait = true }) {
1016
+ if (!indexName || typeof indexName !== "string" || indexName.trim() === "") {
1017
+ throw new error.MastraError({
1018
+ id: storage.createVectorErrorId("QDRANT", "DELETE_PAYLOAD_INDEX", "INVALID_ARGS"),
1019
+ text: "indexName must be a non-empty string",
1020
+ domain: error.ErrorDomain.STORAGE,
1021
+ category: error.ErrorCategory.USER,
1022
+ details: { indexName, fieldName }
1023
+ });
1024
+ }
1025
+ if (!fieldName || typeof fieldName !== "string" || fieldName.trim() === "") {
1026
+ throw new error.MastraError({
1027
+ id: storage.createVectorErrorId("QDRANT", "DELETE_PAYLOAD_INDEX", "INVALID_ARGS"),
1028
+ text: "fieldName must be a non-empty string",
1029
+ domain: error.ErrorDomain.STORAGE,
1030
+ category: error.ErrorCategory.USER,
1031
+ details: { indexName, fieldName }
1032
+ });
1033
+ }
1034
+ try {
1035
+ await this.client.deletePayloadIndex(indexName, fieldName, { wait });
1036
+ } catch (error$1) {
1037
+ const message = error$1?.message || error$1?.toString() || "";
1038
+ if (error$1?.status === 404 || message.toLowerCase().includes("not found") || message.toLowerCase().includes("not exist")) {
1039
+ this.logger.info(`Payload index for field "${fieldName}" does not exist on collection "${indexName}"`);
1040
+ return;
1041
+ }
1042
+ throw new error.MastraError(
1043
+ {
1044
+ id: storage.createVectorErrorId("QDRANT", "DELETE_PAYLOAD_INDEX", "FAILED"),
1045
+ domain: error.ErrorDomain.STORAGE,
1046
+ category: error.ErrorCategory.THIRD_PARTY,
1047
+ details: { indexName, fieldName }
1048
+ },
1049
+ error$1
1050
+ );
1051
+ }
1052
+ }
569
1053
  };
570
1054
 
571
1055
  // src/vector/prompt.ts