@upstash/vector 1.2.0 → 1.2.1

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/README.md CHANGED
@@ -258,6 +258,22 @@ await namespace.fetch(["id-1", "id-2"]);
258
258
 
259
259
  If you wanna learn more about filtering check: [Metadata Filtering](https://upstash.com/docs/vector/features/filtering)
260
260
 
261
+ ## Telemetry
262
+
263
+ This sdk sends anonymous telemetry data to help us improve your experience.
264
+ We collect the following:
265
+
266
+ - SDK version
267
+ - Platform (Cloudflare, AWS or Vercel)
268
+ - Runtime version (node@18.x)
269
+
270
+ You can opt out by setting the `UPSTASH_DISABLE_TELEMETRY` environment variable
271
+ to any truthy value.
272
+
273
+ ```sh
274
+ UPSTASH_DISABLE_TELEMETRY=1
275
+ ```
276
+
261
277
  ## Troubleshooting
262
278
 
263
279
  We have a [Discord](upstash.com/discord) for common problems. If you can't find a solution, please [open an issue](https://github.com/upstash/vector-js/issues/new).
@@ -148,18 +148,28 @@ var QueryCommand = class extends Command {
148
148
 
149
149
  // src/commands/client/delete/index.ts
150
150
  var DeleteCommand = class extends Command {
151
- constructor(id, options) {
151
+ constructor(payload, options) {
152
152
  let endpoint = "delete";
153
153
  if (options?.namespace) {
154
154
  endpoint = `${endpoint}/${options.namespace}`;
155
155
  }
156
- const finalArr = [];
157
- if (Array.isArray(id)) {
158
- finalArr.push(...id);
159
- } else {
160
- finalArr.push(id);
156
+ if (typeof payload === "string" || typeof payload === "number") {
157
+ super(
158
+ {
159
+ ids: [payload]
160
+ },
161
+ endpoint
162
+ );
163
+ } else if (Array.isArray(payload)) {
164
+ super(
165
+ {
166
+ ids: payload
167
+ },
168
+ endpoint
169
+ );
170
+ } else if (typeof payload === "object") {
171
+ super(payload, endpoint);
161
172
  }
162
- super(finalArr, endpoint);
163
173
  }
164
174
  };
165
175
 
@@ -185,13 +195,19 @@ var isVectorPayload = (payload) => {
185
195
 
186
196
  // src/commands/client/fetch/index.ts
187
197
  var FetchCommand = class extends Command {
188
- constructor([ids, opts]) {
198
+ constructor([payload, opts]) {
189
199
  let endpoint = "fetch";
190
200
  if (opts?.namespace) {
191
201
  endpoint = `${endpoint}/${opts.namespace}`;
192
202
  delete opts.namespace;
193
203
  }
194
- super({ ids, ...opts }, endpoint);
204
+ if (Array.isArray(payload)) {
205
+ super({ ids: payload, ...opts }, endpoint);
206
+ } else if (typeof payload === "object") {
207
+ super({ ...payload, ...opts }, endpoint);
208
+ } else {
209
+ throw new Error("Invalid payload");
210
+ }
195
211
  }
196
212
  };
197
213
 
@@ -227,6 +243,67 @@ var InfoCommand = class extends Command {
227
243
  }
228
244
  };
229
245
 
246
+ // src/commands/client/resumable-query/resume.ts
247
+ var ResumeQueryCommand = class extends Command {
248
+ constructor(payload) {
249
+ super(payload, "resumable-query-next");
250
+ }
251
+ };
252
+
253
+ // src/commands/client/resumable-query/start.ts
254
+ var StartResumableQueryCommand = class extends Command {
255
+ constructor(payload, namespace) {
256
+ let endpoint = "resumable-query";
257
+ if ("data" in payload) {
258
+ endpoint = "resumable-query-data";
259
+ }
260
+ if (namespace) {
261
+ endpoint = `${endpoint}/${namespace}`;
262
+ }
263
+ super(payload, endpoint);
264
+ }
265
+ };
266
+
267
+ // src/commands/client/resumable-query/stop.ts
268
+ var StopResumableQueryCommand = class extends Command {
269
+ constructor(payload) {
270
+ super(payload, "resumable-query-end");
271
+ }
272
+ };
273
+
274
+ // src/commands/client/resumable-query/index.ts
275
+ var ResumableQuery = class {
276
+ uuid;
277
+ start;
278
+ fetchNext;
279
+ stop;
280
+ constructor(payload, client, namespace) {
281
+ this.start = async () => {
282
+ const result = await new StartResumableQueryCommand(payload, namespace).exec(
283
+ client
284
+ );
285
+ this.uuid = result.uuid;
286
+ return result;
287
+ };
288
+ this.fetchNext = (additionalK) => {
289
+ if (!this.uuid) {
290
+ throw new Error(
291
+ "The resumable query has already been stopped. Please start another resumable query."
292
+ );
293
+ }
294
+ return new ResumeQueryCommand({ uuid: this.uuid, additionalK }).exec(client);
295
+ };
296
+ this.stop = async () => {
297
+ if (!this.uuid) {
298
+ throw new Error("Resumable query has not been started. Call start() first.");
299
+ }
300
+ const result = await new StopResumableQueryCommand({ uuid: this.uuid }).exec(client);
301
+ this.uuid = "";
302
+ return result;
303
+ };
304
+ }
305
+ };
306
+
230
307
  // src/commands/client/namespace/index.ts
231
308
  var Namespace = class {
232
309
  client;
@@ -294,19 +371,21 @@ var Namespace = class {
294
371
  */
295
372
  update = (args) => new UpdateCommand(args, { namespace: this.namespace }).exec(this.client);
296
373
  /**
297
- * It's used for retrieving specific items from the index namespace, optionally including
298
- * their metadata and feature vectors.
374
+ * Fetches specific items from the index by their IDs or by an id prefix.
375
+ *
376
+ * Note: While using id prefix, the paginated `range` command is recommended to prevent timeouts on large result sets.
299
377
  *
300
378
  * @example
301
379
  * ```js
302
- * const fetchIds = ['123', '456'];
303
- * const fetchOptions = { includeMetadata: true, includeVectors: false };
304
- * const fetchResults = await index.namespace("ns").fetch(fetchIds, fetchOptions);
305
- * console.log(fetchResults); // Outputs the fetched items
380
+ * // Using ids
381
+ * await index.namespace("ns").fetch(["test-1", "test-2"], { includeMetadata: true });
382
+ *
383
+ * // Using id prefix
384
+ * await index.namespace("ns").fetch({ prefix: "test-" });
306
385
  * ```
307
386
  *
308
387
  * @param {...CommandArgs<typeof FetchCommand>} args - The arguments for the fetch command.
309
- * @param {(number[]|string[])} args[0] - An array of IDs of the items to be fetched.
388
+ * @param {FetchPayload} args[0] - An array of IDs or the id prefix of the items to be fetched.
310
389
  * @param {FetchCommandOptions} args[1] - Options for the fetch operation.
311
390
  * @param {boolean} [args[1].includeMetadata=false] - Optionally include metadata of the fetched items.
312
391
  * @param {boolean} [args[1].includeVectors=false] - Optionally include feature vectors of the fetched items.
@@ -347,34 +426,82 @@ var Namespace = class {
347
426
  */
348
427
  query = (args) => new QueryCommand(args, { namespace: this.namespace }).exec(this.client);
349
428
  /**
350
- * Deletes a specific item or items from the index namespace by their ID(s). *
429
+ * Initializes a resumable query operation on the vector database.
430
+ * This method allows for querying large result sets in multiple chunks or implementing pagination.
351
431
  *
432
+ * @template TMetadata
433
+ * @param {ResumableQueryPayload} args - The arguments for the resumable query.
434
+ * @param {number} args.maxIdle - The maximum idle time in seconds before the query session expires.
435
+ * @param {number} args.topK - The number of top results to return in each fetch operation.
436
+ * @param {number[]} args.vector - The query vector used for similarity search.
437
+ * @param {boolean} [args.includeMetadata] - Whether to include metadata in the query results.
438
+ * @param {boolean} [args.includeVectors] - Whether to include vectors in the query results.
439
+ * @param {Object} [options] - Additional options for the query.
440
+ * @returns {Promise<ResumableQuery<TMetadata>>} A promise that resolves to a ResumableQuery object.
352
441
  * @example
353
- * ```js
354
- * await index.namespace("ns").delete('test-id')
355
- * // { deleted: 1 }
356
- * ```
442
+ * const { result, fetchNext, stop } = await index.namespace("ns").resumableQuery({
443
+ * maxIdle: 3600,
444
+ * topK: 50,
445
+ * vector: [0.1, 0.2, 0.3, ...],
446
+ * includeMetadata: true,
447
+ * includeVectors: true
448
+ * }, { namespace: 'my-namespace' });
357
449
  *
358
- * @param id - List of ids or single id
359
- * @returns Number of deleted vectors like `{ deleted: number }`. The number will be 0 if no vectors are deleted.
450
+ * const firstBatch = await fetchNext(10);
451
+ * const secondBatch = await fetchNext(10);
452
+ * await stop(); // End the query session
360
453
  */
454
+ resumableQuery = async (args) => {
455
+ const resumableQuery = new ResumableQuery(args, this.client, this.namespace);
456
+ const initialQuery = await resumableQuery.start();
457
+ const { fetchNext, stop } = resumableQuery;
458
+ return { fetchNext, stop, result: initialQuery.scores };
459
+ };
460
+ /**
461
+ * Deletes items from the index namespace by id, by id prefix, or by filter.
462
+ *
463
+ * @example
464
+ * ```js
465
+ * // Delete by id
466
+ * await index.namespace("ns").delete("test-id");
467
+
468
+ * // Delete by ids
469
+ * await index.namespace("ns").delete(["test-id1", "test-id2"]);
470
+
471
+ * // Delete by id prefix
472
+ * await index.namespace("ns").delete({ prefix: "test-" });
473
+
474
+ * // Delete by filter
475
+ * await index.namespace("ns").delete({ filter: "age >= 23" });
476
+ * ```
477
+ *
478
+ * @param args - A single id, an array of ids, a prefix, or a filter to delete items from the index.
479
+ * @returns Number of deleted vectors in the format `{ deleted: number }`.If no vectors are deleted, returns `{ deleted: 0 }`.
480
+ */
361
481
  delete = (args) => new DeleteCommand(args, { namespace: this.namespace }).exec(this.client);
362
482
  /**
363
- * Retrieves a range of items from the index.
483
+ * Retrieves a paginated range of items from the index. Optionally filter results by an id prefix.
484
+ * Returns items in batches with a cursor for pagination.
364
485
  *
365
486
  * @example
366
487
  * ```js
367
- * const rangeArgs = {
368
- * cursor: 0,
488
+ * const args = {
369
489
  * limit: 10,
370
490
  * includeVectors: true,
371
491
  * includeMetadata: false
372
492
  * };
373
- * const rangeResults = await index.namespace("ns").range(rangeArgs);
374
- * console.log(rangeResults); // Outputs the result of the range operation
493
+ * await index.namespace("ns").range(args);
494
+ *
495
+ * // Use the cursor to get the next page of results
496
+ * const nextPage = await index.namespace("ns").range({
497
+ * // You have to pass the arguments from the first call
498
+ * ...args,
499
+ * cursor: rangeResult.nextCursor,
500
+ * });
375
501
  * ```
376
502
  *
377
503
  * @param {CommandArgs<typeof RangeCommand>} args - The arguments for the range command.
504
+ * @param {string} [args.prefix] - The prefix of the items to be fetched.
378
505
  * @param {number|string} args.cursor - The starting point (cursor) for the range query.
379
506
  * @param {number} args.limit - The maximum number of items to return in this range.
380
507
  * @param {boolean} [args.includeVectors=false] - Optionally include the feature vectors of the items in the response.
@@ -408,67 +535,6 @@ var UpdateCommand = class extends Command {
408
535
  }
409
536
  };
410
537
 
411
- // src/commands/client/resumable-query/resume.ts
412
- var ResumeQueryCommand = class extends Command {
413
- constructor(payload) {
414
- super(payload, "resumable-query-next");
415
- }
416
- };
417
-
418
- // src/commands/client/resumable-query/start.ts
419
- var StartResumableQueryCommand = class extends Command {
420
- constructor(payload, namespace) {
421
- let endpoint = "resumable-query";
422
- if ("data" in payload) {
423
- endpoint = "resumable-query-data";
424
- }
425
- if (namespace) {
426
- endpoint = `${endpoint}/${namespace}`;
427
- }
428
- super(payload, endpoint);
429
- }
430
- };
431
-
432
- // src/commands/client/resumable-query/stop.ts
433
- var StopResumableQueryCommand = class extends Command {
434
- constructor(payload) {
435
- super(payload, "resumable-query-end");
436
- }
437
- };
438
-
439
- // src/commands/client/resumable-query/index.ts
440
- var ResumableQuery = class {
441
- uuid;
442
- start;
443
- fetchNext;
444
- stop;
445
- constructor(payload, client, namespace) {
446
- this.start = async () => {
447
- const result = await new StartResumableQueryCommand(payload, namespace).exec(
448
- client
449
- );
450
- this.uuid = result.uuid;
451
- return result;
452
- };
453
- this.fetchNext = (additionalK) => {
454
- if (!this.uuid) {
455
- throw new Error(
456
- "The resumable query has already been stopped. Please start another resumable query."
457
- );
458
- }
459
- return new ResumeQueryCommand({ uuid: this.uuid, additionalK }).exec(client);
460
- };
461
- this.stop = async () => {
462
- if (!this.uuid) {
463
- throw new Error("Resumable query has not been started. Call start() first.");
464
- }
465
- const result = await new StopResumableQueryCommand({ uuid: this.uuid }).exec(client);
466
- this.uuid = "";
467
- return result;
468
- };
469
- }
470
- };
471
-
472
538
  // src/commands/management/namespaces/list/index.ts
473
539
  var ListNamespacesCommand = class extends Command {
474
540
  constructor() {
@@ -504,17 +570,26 @@ var Index = class {
504
570
  }
505
571
  namespace = (namespace) => new Namespace(this.client, namespace);
506
572
  /**
507
- * Deletes a specific item or items from the index by their ID(s). *
508
- *
509
- * @example
510
- * ```js
511
- * const result = await index.delete('test-id');
512
- * // { deleted: 1 }
513
- * ```
514
- *
515
- * @param id - List of ids or single id
516
- * @returns Number of deleted vectors like `{ deleted: number }`. The number will be 0 if no vectors are deleted.
517
- */
573
+ * Deletes items from the index by id, by id prefix, or by filter.
574
+ *
575
+ * @example
576
+ * ```js
577
+ * // Delete by id
578
+ * await index.delete("test-id");
579
+
580
+ * // Delete by ids
581
+ * await index.delete(["test-id1", "test-id2"]);
582
+
583
+ * // Delete by id prefix
584
+ * await index.delete({ prefix: "test-" });
585
+
586
+ * // Delete by filter
587
+ * await index.delete({ filter: "age >= 23" });
588
+ * ```
589
+ *
590
+ * @param args - A single id, an array of ids, a prefix, or a filter to delete items from the index.
591
+ * @returns Number of deleted vectors in the format `{ deleted: number }`.If no vectors are deleted, returns `{ deleted: 0 }`.
592
+ */
518
593
  delete = (args, options) => new DeleteCommand(args, options).exec(this.client);
519
594
  /**
520
595
  * Queries an index with specified parameters.
@@ -656,23 +731,25 @@ var Index = class {
656
731
  */
657
732
  update = (args, options) => new UpdateCommand(args, options).exec(this.client);
658
733
  /**
659
- * It's used for retrieving specific items from the index, optionally including
660
- * their metadata and feature vectors.
734
+ * Fetches specific items from the index by their IDs or by an id prefix.
735
+ *
736
+ * Note: While using id prefix, the paginated `range` command is recommended to prevent timeouts on large result sets.
661
737
  *
662
738
  * @example
663
739
  * ```js
664
- * const fetchIds = ['123', '456'];
665
- * const fetchOptions = { includeMetadata: true, includeVectors: false };
666
- * const fetchResults = await index.fetch(fetchIds, fetchOptions);
667
- * console.log(fetchResults); // Outputs the fetched items
740
+ * // Using ids
741
+ * await index.fetch(["test-1", "test-2"], { includeMetadata: true });
742
+ *
743
+ * // Using id prefix
744
+ * await index.fetch({ prefix: "test-" });
668
745
  * ```
669
746
  *
670
747
  * @param {...CommandArgs<typeof FetchCommand>} args - The arguments for the fetch command.
671
- * @param {(number[]|string[])} args - An array of IDs of the items to be fetched.
672
- * @param {FetchCommandOptions} args - Options for the fetch operation.
673
- * @param {boolean} [args.includeMetadata=false] - Optionally include metadata of the fetched items.
674
- * @param {boolean} [args.includeVectors=false] - Optionally include feature vectors of the fetched items.
675
- * @param {boolean} [args.metadataUpdateMode="OVERWRITE"] - Specifies whether to overwrite or patch the metadata values.
748
+ * @param {FetchPayload} args[0] - An array of IDs or the id prefix of the items to be fetched.
749
+ * @param {FetchCommandOptions} args[1] - Options for the fetch operation.
750
+ * @param {boolean} [args[1].includeMetadata=false] - Optionally include metadata of the fetched items.
751
+ * @param {boolean} [args[1].includeVectors=false] - Optionally include feature vectors of the fetched items.
752
+ * @param {string} [args[1].namespace = ""] - The namespace of the index to fetch items from.
676
753
  *
677
754
  * @returns {Promise<FetchReturnResponse<TMetadata>[]>} A promise that resolves with an array of fetched items or null if not found, after the command is executed.
678
755
  */
@@ -708,27 +785,28 @@ var Index = class {
708
785
  */
709
786
  reset = (options) => new ResetCommand(options).exec(this.client);
710
787
  /**
711
- * Retrieves a range of items from the index.
788
+ * Retrieves a paginated range of items from the index. Optionally filter results by an id prefix.
789
+ * Returns items in batches with a cursor for pagination.
712
790
  *
713
791
  * @example
714
792
  * ```js
715
- * const rangeArgs = {
716
- * cursor: 0,
793
+ * const args = {
717
794
  * limit: 10,
718
795
  * includeVectors: true,
719
796
  * includeMetadata: false
720
797
  * };
721
- * const rangeResults = await index.range(rangeArgs);
722
- * console.log(rangeResults); // Outputs the result of the range operation
723
- * ```
724
- *
725
- * You can also pass a namespace like:
798
+ * await index.range(args);
726
799
  *
727
- * ```js
728
- * const rangeResults = await index.range(rangeArgs, { namespace: "ns" });
800
+ * // Use the cursor to get the next page of results
801
+ * const nextPage = await index.range({
802
+ * // You have to pass the arguments from the first call
803
+ * ...args,
804
+ * cursor: rangeResult.nextCursor,
805
+ * });
729
806
  * ```
730
807
  *
731
808
  * @param {CommandArgs<typeof RangeCommand>} args - The arguments for the range command.
809
+ * @param {string} [args.prefix] - The prefix of the items to be fetched.
732
810
  * @param {number|string} args.cursor - The starting point (cursor) for the range query.
733
811
  * @param {number} args.limit - The maximum number of items to return in this range.
734
812
  * @param {boolean} [args.includeVectors=false] - Optionally include the feature vectors of the items in the response.
@@ -776,10 +854,14 @@ var Index = class {
776
854
  deleteNamespace = (namespace) => new DeleteNamespaceCommand(namespace).exec(this.client);
777
855
  };
778
856
 
857
+ // version.ts
858
+ var VERSION = "v1.2.1";
859
+
779
860
  export {
780
861
  HttpClient,
781
862
  WeightingStrategy,
782
863
  FusionAlgorithm,
783
864
  QueryMode,
784
- Index
865
+ Index,
866
+ VERSION
785
867
  };
@@ -1,5 +1,5 @@
1
- import { R as RequesterConfig, D as Dict, I as Index$1 } from './vector-FeePts30.mjs';
2
- export { d as FetchResult, F as FusionAlgorithm, f as InfoResult, Q as QueryMode, e as QueryResult, c as RangeResult, a as Requester, S as SparseVector, U as UpstashRequest, b as UpstashResponse, V as Vector, W as WeightingStrategy } from './vector-FeePts30.mjs';
1
+ import { R as RequesterConfig, D as Dict, I as Index$1 } from './vector-1qEMEEQL.mjs';
2
+ export { d as FetchResult, F as FusionAlgorithm, f as InfoResult, Q as QueryMode, e as QueryResult, c as RangeResult, a as Requester, S as SparseVector, U as UpstashRequest, b as UpstashResponse, V as Vector, W as WeightingStrategy } from './vector-1qEMEEQL.mjs';
3
3
 
4
4
  /**
5
5
  * Connection credentials for upstash vector.
@@ -19,6 +19,13 @@ type IndexConfig = {
19
19
  * For more check: https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal
20
20
  */
21
21
  signal?: AbortSignal;
22
+ /**
23
+ * Enable telemetry to help us improve the SDK.
24
+ * The sdk will send the sdk version, platform and node version as telemetry headers.
25
+ *
26
+ * @default true
27
+ */
28
+ enableTelemetry?: boolean;
22
29
  } & RequesterConfig;
23
30
  /**
24
31
  * Serverless vector client for upstash.
@@ -1,5 +1,5 @@
1
- import { R as RequesterConfig, D as Dict, I as Index$1 } from './vector-FeePts30.js';
2
- export { d as FetchResult, F as FusionAlgorithm, f as InfoResult, Q as QueryMode, e as QueryResult, c as RangeResult, a as Requester, S as SparseVector, U as UpstashRequest, b as UpstashResponse, V as Vector, W as WeightingStrategy } from './vector-FeePts30.js';
1
+ import { R as RequesterConfig, D as Dict, I as Index$1 } from './vector-1qEMEEQL.js';
2
+ export { d as FetchResult, F as FusionAlgorithm, f as InfoResult, Q as QueryMode, e as QueryResult, c as RangeResult, a as Requester, S as SparseVector, U as UpstashRequest, b as UpstashResponse, V as Vector, W as WeightingStrategy } from './vector-1qEMEEQL.js';
3
3
 
4
4
  /**
5
5
  * Connection credentials for upstash vector.
@@ -19,6 +19,13 @@ type IndexConfig = {
19
19
  * For more check: https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal
20
20
  */
21
21
  signal?: AbortSignal;
22
+ /**
23
+ * Enable telemetry to help us improve the SDK.
24
+ * The sdk will send the sdk version, platform and node version as telemetry headers.
25
+ *
26
+ * @default true
27
+ */
28
+ enableTelemetry?: boolean;
22
29
  } & RequesterConfig;
23
30
  /**
24
31
  * Serverless vector client for upstash.