langchain 0.0.128 → 0.0.129

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.
Files changed (39) hide show
  1. package/dist/embeddings/cache_backed.cjs +109 -0
  2. package/dist/embeddings/cache_backed.d.ts +60 -0
  3. package/dist/embeddings/cache_backed.js +102 -0
  4. package/dist/load/import_constants.cjs +1 -0
  5. package/dist/load/import_constants.js +1 -0
  6. package/dist/load/import_map.cjs +5 -2
  7. package/dist/load/import_map.d.ts +3 -0
  8. package/dist/load/import_map.js +3 -0
  9. package/dist/schema/runnable.cjs +177 -22
  10. package/dist/schema/runnable.d.ts +89 -0
  11. package/dist/schema/runnable.js +175 -21
  12. package/dist/schema/storage.cjs +10 -0
  13. package/dist/schema/storage.d.ts +10 -0
  14. package/dist/schema/storage.js +6 -0
  15. package/dist/storage/encoder_backed.cjs +66 -0
  16. package/dist/storage/encoder_backed.d.ts +18 -0
  17. package/dist/storage/encoder_backed.js +62 -0
  18. package/dist/storage/in_memory.cjs +44 -0
  19. package/dist/storage/in_memory.d.ts +9 -0
  20. package/dist/storage/in_memory.js +40 -0
  21. package/dist/storage/ioredis.cjs +110 -0
  22. package/dist/storage/ioredis.d.ts +22 -0
  23. package/dist/storage/ioredis.js +106 -0
  24. package/dist/vectorstores/zep.cjs +101 -23
  25. package/dist/vectorstores/zep.d.ts +32 -2
  26. package/dist/vectorstores/zep.js +101 -23
  27. package/embeddings/cache_backed.cjs +1 -0
  28. package/embeddings/cache_backed.d.ts +1 -0
  29. package/embeddings/cache_backed.js +1 -0
  30. package/package.json +33 -1
  31. package/schema/storage.cjs +1 -0
  32. package/schema/storage.d.ts +1 -0
  33. package/schema/storage.js +1 -0
  34. package/storage/in_memory.cjs +1 -0
  35. package/storage/in_memory.d.ts +1 -0
  36. package/storage/in_memory.js +1 -0
  37. package/storage/ioredis.cjs +1 -0
  38. package/storage/ioredis.d.ts +1 -0
  39. package/storage/ioredis.js +1 -0
@@ -7,6 +7,10 @@ function _coerceToDict(value, defaultKey) {
7
7
  ? value
8
8
  : { [defaultKey]: value };
9
9
  }
10
+ /**
11
+ * A Runnable is a generic unit of work that can be invoked, batched, streamed, and/or
12
+ * transformed.
13
+ */
10
14
  export class Runnable extends Serializable {
11
15
  constructor() {
12
16
  super(...arguments);
@@ -17,10 +21,28 @@ export class Runnable extends Serializable {
17
21
  value: true
18
22
  });
19
23
  }
24
+ /**
25
+ * Bind arguments to a Runnable, returning a new Runnable.
26
+ * @param kwargs
27
+ * @returns A new RunnableBinding that, when invoked, will apply the bound args.
28
+ */
20
29
  bind(kwargs) {
21
30
  // eslint-disable-next-line @typescript-eslint/no-use-before-define
22
31
  return new RunnableBinding({ bound: this, kwargs });
23
32
  }
33
+ /**
34
+ * Create a new runnable from the current one that will try invoking
35
+ * other passed fallback runnables if the initial invocation fails.
36
+ * @param fields.fallbacks Other runnables to call if the runnable errors.
37
+ * @returns A new RunnableWithFallbacks.
38
+ */
39
+ withFallbacks(fields) {
40
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
41
+ return new RunnableWithFallbacks({
42
+ runnable: this,
43
+ fallbacks: fields.fallbacks,
44
+ });
45
+ }
24
46
  _getOptionsList(options, length = 0) {
25
47
  if (Array.isArray(options)) {
26
48
  if (options.length !== length) {
@@ -30,6 +52,14 @@ export class Runnable extends Serializable {
30
52
  }
31
53
  return Array.from({ length }, () => options);
32
54
  }
55
+ /**
56
+ * Default implementation of batch, which calls invoke N times.
57
+ * Subclasses should override this method if they can batch more efficiently.
58
+ * @param inputs Array of inputs to each batch call.
59
+ * @param options Either a single call options object to apply to each batch call or an array for each call.
60
+ * @param batchOptions.maxConcurrency Maximum number of calls to run at once.
61
+ * @returns An array of RunOutputs
62
+ */
33
63
  async batch(inputs, options, batchOptions) {
34
64
  const configList = this._getOptionsList(options ?? {}, inputs.length);
35
65
  const batchSize = batchOptions?.maxConcurrency && batchOptions.maxConcurrency > 0
@@ -39,15 +69,27 @@ export class Runnable extends Serializable {
39
69
  for (let i = 0; i < inputs.length; i += batchSize) {
40
70
  const batchPromises = inputs
41
71
  .slice(i, i + batchSize)
42
- .map((input, i) => this.invoke(input, configList[i]));
72
+ .map((input, j) => this.invoke(input, configList[j]));
43
73
  const batchResult = await Promise.all(batchPromises);
44
74
  batchResults.push(batchResult);
45
75
  }
46
76
  return batchResults.flat();
47
77
  }
78
+ /**
79
+ * Default streaming implementation.
80
+ * Subclasses should override this method if they support streaming output.
81
+ * @param input
82
+ * @param options
83
+ */
48
84
  async *_streamIterator(input, options) {
49
85
  yield this.invoke(input, options);
50
86
  }
87
+ /**
88
+ * Stream output in chunks.
89
+ * @param input
90
+ * @param options
91
+ * @returns A readable stream that is also an iterable.
92
+ */
51
93
  async stream(input, options) {
52
94
  return IterableReadableStream.fromAsyncGenerator(this._streamIterator(input, options));
53
95
  }
@@ -112,6 +154,12 @@ export class Runnable extends Serializable {
112
154
  _patchConfig(config = {}, callbackManager = undefined) {
113
155
  return { ...config, callbacks: callbackManager };
114
156
  }
157
+ /**
158
+ * Create a new runnable sequence that runs each individual runnable in series,
159
+ * piping the output of one runnable into another runnable or runnable-like.
160
+ * @param coerceable A runnable, function, or object whose values are functions or runnables.
161
+ * @returns A new runnable sequence.
162
+ */
115
163
  pipe(coerceable) {
116
164
  // eslint-disable-next-line @typescript-eslint/no-use-before-define
117
165
  return new RunnableSequence({
@@ -124,6 +172,9 @@ export class Runnable extends Serializable {
124
172
  return thing.lc_runnable;
125
173
  }
126
174
  }
175
+ /**
176
+ * A sequence of runnables, where the output of each is the input of the next.
177
+ */
127
178
  export class RunnableSequence extends Runnable {
128
179
  constructor(fields) {
129
180
  super(fields);
@@ -194,9 +245,9 @@ export class RunnableSequence extends Runnable {
194
245
  try {
195
246
  for (let i = 0; i < [this.first, ...this.middle].length; i += 1) {
196
247
  const step = this.steps[i];
197
- nextStepInputs = await step.batch(nextStepInputs, runManagers.map((runManager) => this._patchConfig(configList[i], runManager?.getChild()), batchOptions));
248
+ nextStepInputs = await step.batch(nextStepInputs, runManagers.map((runManager, j) => this._patchConfig(configList[j], runManager?.getChild())), batchOptions);
198
249
  }
199
- finalOutputs = await this.last.batch(nextStepInputs, runManagers.map((runManager) => this._patchConfig(configList[this.steps.length - 1], runManager?.getChild()), batchOptions));
250
+ finalOutputs = await this.last.batch(nextStepInputs, runManagers.map((runManager) => this._patchConfig(configList[this.steps.length - 1], runManager?.getChild())), batchOptions);
200
251
  }
201
252
  catch (e) {
202
253
  await Promise.all(runManagers.map((runManager) => runManager?.handleChainError(e)));
@@ -291,6 +342,10 @@ export class RunnableSequence extends Runnable {
291
342
  });
292
343
  }
293
344
  }
345
+ /**
346
+ * A runnable that runs a mapping of runnables in parallel,
347
+ * and returns a mapping of their outputs.
348
+ */
294
349
  export class RunnableMap extends Runnable {
295
350
  constructor(fields) {
296
351
  super(fields);
@@ -340,6 +395,9 @@ export class RunnableMap extends Runnable {
340
395
  return output;
341
396
  }
342
397
  }
398
+ /**
399
+ * A runnable that runs a callable.
400
+ */
343
401
  export class RunnableLambda extends Runnable {
344
402
  constructor(fields) {
345
403
  super(fields);
@@ -361,6 +419,9 @@ export class RunnableLambda extends Runnable {
361
419
  return this._callWithConfig(async (input) => this.func(input), input, options);
362
420
  }
363
421
  }
422
+ /**
423
+ * A runnable that passes through the input.
424
+ */
364
425
  export class RunnablePassthrough extends Runnable {
365
426
  constructor() {
366
427
  super(...arguments);
@@ -381,24 +442,9 @@ export class RunnablePassthrough extends Runnable {
381
442
  return this._callWithConfig((input) => Promise.resolve(input), input, options);
382
443
  }
383
444
  }
384
- function _coerceToRunnable(coerceable) {
385
- if (typeof coerceable === "function") {
386
- return new RunnableLambda({ func: coerceable });
387
- }
388
- else if (Runnable.isRunnable(coerceable)) {
389
- return coerceable;
390
- }
391
- else if (!Array.isArray(coerceable) && typeof coerceable === "object") {
392
- const runnables = {};
393
- for (const [key, value] of Object.entries(coerceable)) {
394
- runnables[key] = _coerceToRunnable(value);
395
- }
396
- return new RunnableMap({ steps: runnables });
397
- }
398
- else {
399
- throw new Error(`Expected a Runnable, function or object.\nInstead got an unsupported type.`);
400
- }
401
- }
445
+ /**
446
+ * A runnable that delegates calls to another runnable with a set of kwargs.
447
+ */
402
448
  export class RunnableBinding extends Runnable {
403
449
  constructor(fields) {
404
450
  super(fields);
@@ -451,6 +497,10 @@ export class RunnableBinding extends Runnable {
451
497
  return this.bound.stream(input, { ...options, ...this.kwargs });
452
498
  }
453
499
  }
500
+ /**
501
+ * A runnable that routes to a set of runnables based on Input['key'].
502
+ * Returns the output of the selected runnable.
503
+ */
454
504
  export class RouterRunnable extends Runnable {
455
505
  constructor(fields) {
456
506
  super(fields);
@@ -513,3 +563,107 @@ export class RouterRunnable extends Runnable {
513
563
  return runnable.stream(actualInput, options);
514
564
  }
515
565
  }
566
+ /**
567
+ * A Runnable that can fallback to other Runnables if it fails.
568
+ */
569
+ export class RunnableWithFallbacks extends Runnable {
570
+ constructor(fields) {
571
+ super(fields);
572
+ Object.defineProperty(this, "lc_namespace", {
573
+ enumerable: true,
574
+ configurable: true,
575
+ writable: true,
576
+ value: ["schema", "langchain"]
577
+ });
578
+ Object.defineProperty(this, "lc_serializable", {
579
+ enumerable: true,
580
+ configurable: true,
581
+ writable: true,
582
+ value: true
583
+ });
584
+ Object.defineProperty(this, "runnable", {
585
+ enumerable: true,
586
+ configurable: true,
587
+ writable: true,
588
+ value: void 0
589
+ });
590
+ Object.defineProperty(this, "fallbacks", {
591
+ enumerable: true,
592
+ configurable: true,
593
+ writable: true,
594
+ value: void 0
595
+ });
596
+ this.runnable = fields.runnable;
597
+ this.fallbacks = fields.fallbacks;
598
+ }
599
+ *runnables() {
600
+ yield this.runnable;
601
+ for (const fallback of this.fallbacks) {
602
+ yield fallback;
603
+ }
604
+ }
605
+ async invoke(input, options) {
606
+ const callbackManager_ = await CallbackManager.configure(options?.callbacks, undefined, options?.tags, undefined, options?.metadata);
607
+ const runManager = await callbackManager_?.handleChainStart(this.toJSON(), _coerceToDict(input, "input"));
608
+ let firstError;
609
+ for (const runnable of this.runnables()) {
610
+ try {
611
+ const output = await runnable.invoke(input, this._patchConfig(options, runManager?.getChild()));
612
+ await runManager?.handleChainEnd(_coerceToDict(output, "output"));
613
+ return output;
614
+ }
615
+ catch (e) {
616
+ if (firstError === undefined) {
617
+ firstError = e;
618
+ }
619
+ }
620
+ }
621
+ if (firstError === undefined) {
622
+ throw new Error("No error stored at end of fallback.");
623
+ }
624
+ await runManager?.handleChainError(firstError);
625
+ throw firstError;
626
+ }
627
+ async batch(inputs, options, batchOptions) {
628
+ const configList = this._getOptionsList(options ?? {}, inputs.length);
629
+ const callbackManagers = await Promise.all(configList.map((config) => CallbackManager.configure(config?.callbacks, undefined, config?.tags, undefined, config?.metadata)));
630
+ const runManagers = await Promise.all(callbackManagers.map((callbackManager, i) => callbackManager?.handleChainStart(this.toJSON(), _coerceToDict(inputs[i], "input"))));
631
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
632
+ let firstError;
633
+ for (const runnable of this.runnables()) {
634
+ try {
635
+ const outputs = await runnable.batch(inputs, runManagers.map((runManager, j) => this._patchConfig(configList[j], runManager?.getChild())), batchOptions);
636
+ await Promise.all(runManagers.map((runManager, i) => runManager?.handleChainEnd(_coerceToDict(outputs[i], "output"))));
637
+ return outputs;
638
+ }
639
+ catch (e) {
640
+ if (firstError === undefined) {
641
+ firstError = e;
642
+ }
643
+ }
644
+ }
645
+ if (!firstError) {
646
+ throw new Error("No error stored at end of fallbacks.");
647
+ }
648
+ await Promise.all(runManagers.map((runManager) => runManager?.handleChainError(firstError)));
649
+ throw firstError;
650
+ }
651
+ }
652
+ function _coerceToRunnable(coerceable) {
653
+ if (typeof coerceable === "function") {
654
+ return new RunnableLambda({ func: coerceable });
655
+ }
656
+ else if (Runnable.isRunnable(coerceable)) {
657
+ return coerceable;
658
+ }
659
+ else if (!Array.isArray(coerceable) && typeof coerceable === "object") {
660
+ const runnables = {};
661
+ for (const [key, value] of Object.entries(coerceable)) {
662
+ runnables[key] = _coerceToRunnable(value);
663
+ }
664
+ return new RunnableMap({ steps: runnables });
665
+ }
666
+ else {
667
+ throw new Error(`Expected a Runnable, function or object.\nInstead got an unsupported type.`);
668
+ }
669
+ }
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BaseStore = void 0;
4
+ const serializable_js_1 = require("../load/serializable.cjs");
5
+ /**
6
+ * Abstract interface for a key-value store.
7
+ */
8
+ class BaseStore extends serializable_js_1.Serializable {
9
+ }
10
+ exports.BaseStore = BaseStore;
@@ -0,0 +1,10 @@
1
+ import { Serializable } from "../load/serializable.js";
2
+ /**
3
+ * Abstract interface for a key-value store.
4
+ */
5
+ export declare abstract class BaseStore<K, V> extends Serializable {
6
+ abstract mget(keys: K[]): Promise<(V | undefined)[]>;
7
+ abstract mset(keyValuePairs: [K, V][]): Promise<void>;
8
+ abstract mdelete(keys: K[]): Promise<void>;
9
+ abstract yieldKeys(prefix?: string): AsyncGenerator<K | string>;
10
+ }
@@ -0,0 +1,6 @@
1
+ import { Serializable } from "../load/serializable.js";
2
+ /**
3
+ * Abstract interface for a key-value store.
4
+ */
5
+ export class BaseStore extends Serializable {
6
+ }
@@ -0,0 +1,66 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.EncoderBackedStore = void 0;
4
+ const storage_js_1 = require("../schema/storage.cjs");
5
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
6
+ class EncoderBackedStore extends storage_js_1.BaseStore {
7
+ constructor(fields) {
8
+ super(fields);
9
+ Object.defineProperty(this, "lc_namespace", {
10
+ enumerable: true,
11
+ configurable: true,
12
+ writable: true,
13
+ value: ["langchain", "storage", "encoder_backed"]
14
+ });
15
+ Object.defineProperty(this, "store", {
16
+ enumerable: true,
17
+ configurable: true,
18
+ writable: true,
19
+ value: void 0
20
+ });
21
+ Object.defineProperty(this, "keyEncoder", {
22
+ enumerable: true,
23
+ configurable: true,
24
+ writable: true,
25
+ value: void 0
26
+ });
27
+ Object.defineProperty(this, "valueSerializer", {
28
+ enumerable: true,
29
+ configurable: true,
30
+ writable: true,
31
+ value: void 0
32
+ });
33
+ Object.defineProperty(this, "valueDeserializer", {
34
+ enumerable: true,
35
+ configurable: true,
36
+ writable: true,
37
+ value: void 0
38
+ });
39
+ this.store = fields.store;
40
+ this.keyEncoder = fields.keyEncoder;
41
+ this.valueSerializer = fields.valueSerializer;
42
+ this.valueDeserializer = fields.valueDeserializer;
43
+ }
44
+ async mget(keys) {
45
+ const encodedKeys = keys.map(this.keyEncoder);
46
+ const values = await this.store.mget(encodedKeys);
47
+ return values.map((value) => {
48
+ if (value === undefined) {
49
+ return undefined;
50
+ }
51
+ return this.valueDeserializer(value);
52
+ });
53
+ }
54
+ async mset(keyValuePairs) {
55
+ const encodedPairs = keyValuePairs.map(([key, value]) => [this.keyEncoder(key), this.valueSerializer(value)]);
56
+ return this.store.mset(encodedPairs);
57
+ }
58
+ async mdelete(keys) {
59
+ const encodedKeys = keys.map(this.keyEncoder);
60
+ return this.store.mdelete(encodedKeys);
61
+ }
62
+ async *yieldKeys(prefix) {
63
+ yield* this.store.yieldKeys(prefix);
64
+ }
65
+ }
66
+ exports.EncoderBackedStore = EncoderBackedStore;
@@ -0,0 +1,18 @@
1
+ import { BaseStore } from "../schema/storage.js";
2
+ export declare class EncoderBackedStore<K, V, SerializedType = any> extends BaseStore<K, V> {
3
+ lc_namespace: string[];
4
+ store: BaseStore<string, SerializedType>;
5
+ keyEncoder: (key: K) => string;
6
+ valueSerializer: (value: V) => SerializedType;
7
+ valueDeserializer: (value: SerializedType) => V;
8
+ constructor(fields: {
9
+ store: BaseStore<string, SerializedType>;
10
+ keyEncoder: (key: K) => string;
11
+ valueSerializer: (value: V) => SerializedType;
12
+ valueDeserializer: (value: SerializedType) => V;
13
+ });
14
+ mget(keys: K[]): Promise<(V | undefined)[]>;
15
+ mset(keyValuePairs: [K, V][]): Promise<void>;
16
+ mdelete(keys: K[]): Promise<void>;
17
+ yieldKeys(prefix?: string | undefined): AsyncGenerator<string | K>;
18
+ }
@@ -0,0 +1,62 @@
1
+ import { BaseStore } from "../schema/storage.js";
2
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
3
+ export class EncoderBackedStore extends BaseStore {
4
+ constructor(fields) {
5
+ super(fields);
6
+ Object.defineProperty(this, "lc_namespace", {
7
+ enumerable: true,
8
+ configurable: true,
9
+ writable: true,
10
+ value: ["langchain", "storage", "encoder_backed"]
11
+ });
12
+ Object.defineProperty(this, "store", {
13
+ enumerable: true,
14
+ configurable: true,
15
+ writable: true,
16
+ value: void 0
17
+ });
18
+ Object.defineProperty(this, "keyEncoder", {
19
+ enumerable: true,
20
+ configurable: true,
21
+ writable: true,
22
+ value: void 0
23
+ });
24
+ Object.defineProperty(this, "valueSerializer", {
25
+ enumerable: true,
26
+ configurable: true,
27
+ writable: true,
28
+ value: void 0
29
+ });
30
+ Object.defineProperty(this, "valueDeserializer", {
31
+ enumerable: true,
32
+ configurable: true,
33
+ writable: true,
34
+ value: void 0
35
+ });
36
+ this.store = fields.store;
37
+ this.keyEncoder = fields.keyEncoder;
38
+ this.valueSerializer = fields.valueSerializer;
39
+ this.valueDeserializer = fields.valueDeserializer;
40
+ }
41
+ async mget(keys) {
42
+ const encodedKeys = keys.map(this.keyEncoder);
43
+ const values = await this.store.mget(encodedKeys);
44
+ return values.map((value) => {
45
+ if (value === undefined) {
46
+ return undefined;
47
+ }
48
+ return this.valueDeserializer(value);
49
+ });
50
+ }
51
+ async mset(keyValuePairs) {
52
+ const encodedPairs = keyValuePairs.map(([key, value]) => [this.keyEncoder(key), this.valueSerializer(value)]);
53
+ return this.store.mset(encodedPairs);
54
+ }
55
+ async mdelete(keys) {
56
+ const encodedKeys = keys.map(this.keyEncoder);
57
+ return this.store.mdelete(encodedKeys);
58
+ }
59
+ async *yieldKeys(prefix) {
60
+ yield* this.store.yieldKeys(prefix);
61
+ }
62
+ }
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.InMemoryStore = void 0;
4
+ const storage_js_1 = require("../schema/storage.cjs");
5
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
6
+ class InMemoryStore extends storage_js_1.BaseStore {
7
+ constructor() {
8
+ super(...arguments);
9
+ Object.defineProperty(this, "lc_namespace", {
10
+ enumerable: true,
11
+ configurable: true,
12
+ writable: true,
13
+ value: ["langchain", "storage", "in_memory"]
14
+ });
15
+ Object.defineProperty(this, "store", {
16
+ enumerable: true,
17
+ configurable: true,
18
+ writable: true,
19
+ value: {}
20
+ });
21
+ }
22
+ async mget(keys) {
23
+ return keys.map((key) => this.store[key]);
24
+ }
25
+ async mset(keyValuePairs) {
26
+ for (const [key, value] of keyValuePairs) {
27
+ this.store[key] = value;
28
+ }
29
+ }
30
+ async mdelete(keys) {
31
+ for (const key of keys) {
32
+ delete this.store[key];
33
+ }
34
+ }
35
+ async *yieldKeys(prefix) {
36
+ const keys = Object.keys(this.store);
37
+ for (const key of keys) {
38
+ if (prefix === undefined || key.startsWith(prefix)) {
39
+ yield key;
40
+ }
41
+ }
42
+ }
43
+ }
44
+ exports.InMemoryStore = InMemoryStore;
@@ -0,0 +1,9 @@
1
+ import { BaseStore } from "../schema/storage.js";
2
+ export declare class InMemoryStore<T = any> extends BaseStore<string, T> {
3
+ lc_namespace: string[];
4
+ protected store: Record<string, T>;
5
+ mget(keys: string[]): Promise<T[]>;
6
+ mset(keyValuePairs: [string, T][]): Promise<void>;
7
+ mdelete(keys: string[]): Promise<void>;
8
+ yieldKeys(prefix?: string | undefined): AsyncGenerator<string>;
9
+ }
@@ -0,0 +1,40 @@
1
+ import { BaseStore } from "../schema/storage.js";
2
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
3
+ export class InMemoryStore extends BaseStore {
4
+ constructor() {
5
+ super(...arguments);
6
+ Object.defineProperty(this, "lc_namespace", {
7
+ enumerable: true,
8
+ configurable: true,
9
+ writable: true,
10
+ value: ["langchain", "storage", "in_memory"]
11
+ });
12
+ Object.defineProperty(this, "store", {
13
+ enumerable: true,
14
+ configurable: true,
15
+ writable: true,
16
+ value: {}
17
+ });
18
+ }
19
+ async mget(keys) {
20
+ return keys.map((key) => this.store[key]);
21
+ }
22
+ async mset(keyValuePairs) {
23
+ for (const [key, value] of keyValuePairs) {
24
+ this.store[key] = value;
25
+ }
26
+ }
27
+ async mdelete(keys) {
28
+ for (const key of keys) {
29
+ delete this.store[key];
30
+ }
31
+ }
32
+ async *yieldKeys(prefix) {
33
+ const keys = Object.keys(this.store);
34
+ for (const key of keys) {
35
+ if (prefix === undefined || key.startsWith(prefix)) {
36
+ yield key;
37
+ }
38
+ }
39
+ }
40
+ }
@@ -0,0 +1,110 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RedisByteStore = void 0;
4
+ const storage_js_1 = require("../schema/storage.cjs");
5
+ class RedisByteStore extends storage_js_1.BaseStore {
6
+ constructor(fields) {
7
+ super(fields);
8
+ Object.defineProperty(this, "lc_namespace", {
9
+ enumerable: true,
10
+ configurable: true,
11
+ writable: true,
12
+ value: ["langchain", "storage", "ioredis"]
13
+ });
14
+ Object.defineProperty(this, "client", {
15
+ enumerable: true,
16
+ configurable: true,
17
+ writable: true,
18
+ value: void 0
19
+ });
20
+ Object.defineProperty(this, "ttl", {
21
+ enumerable: true,
22
+ configurable: true,
23
+ writable: true,
24
+ value: void 0
25
+ });
26
+ Object.defineProperty(this, "namespace", {
27
+ enumerable: true,
28
+ configurable: true,
29
+ writable: true,
30
+ value: void 0
31
+ });
32
+ Object.defineProperty(this, "yieldKeysScanBatchSize", {
33
+ enumerable: true,
34
+ configurable: true,
35
+ writable: true,
36
+ value: 1000
37
+ });
38
+ this.client = fields.client;
39
+ this.ttl = fields.ttl;
40
+ this.namespace = fields.namespace;
41
+ this.yieldKeysScanBatchSize =
42
+ fields.yieldKeysScanBatchSize ?? this.yieldKeysScanBatchSize;
43
+ }
44
+ _getPrefixedKey(key) {
45
+ if (this.namespace) {
46
+ const delimiter = "/";
47
+ return `${this.namespace}${delimiter}${key}`;
48
+ }
49
+ return key;
50
+ }
51
+ _getDeprefixedKey(key) {
52
+ if (this.namespace) {
53
+ const delimiter = "/";
54
+ return key.slice(this.namespace.length + delimiter.length);
55
+ }
56
+ return key;
57
+ }
58
+ async mget(keys) {
59
+ const prefixedKeys = keys.map(this._getPrefixedKey.bind(this));
60
+ const retrievedValues = await this.client.mgetBuffer(prefixedKeys);
61
+ return retrievedValues.map((key) => {
62
+ if (!key) {
63
+ return undefined;
64
+ }
65
+ else {
66
+ return key;
67
+ }
68
+ });
69
+ }
70
+ async mset(keyValuePairs) {
71
+ const decoder = new TextDecoder();
72
+ const encodedKeyValuePairs = keyValuePairs.map(([key, value]) => [
73
+ this._getPrefixedKey(key),
74
+ decoder.decode(value),
75
+ ]);
76
+ const pipeline = this.client.pipeline();
77
+ for (const [key, value] of encodedKeyValuePairs) {
78
+ if (this.ttl) {
79
+ pipeline.set(key, value, "EX", this.ttl);
80
+ }
81
+ else {
82
+ pipeline.set(key, value);
83
+ }
84
+ }
85
+ await pipeline.exec();
86
+ }
87
+ async mdelete(keys) {
88
+ await this.client.del(...keys.map(this._getPrefixedKey.bind(this)));
89
+ }
90
+ async *yieldKeys(prefix) {
91
+ let pattern;
92
+ if (prefix) {
93
+ pattern = this._getPrefixedKey(prefix);
94
+ }
95
+ else {
96
+ pattern = this._getPrefixedKey("*");
97
+ }
98
+ let [cursor, batch] = await this.client.scan(0, "MATCH", pattern, "COUNT", this.yieldKeysScanBatchSize);
99
+ for (const key of batch) {
100
+ yield this._getDeprefixedKey(key);
101
+ }
102
+ while (cursor !== "0") {
103
+ [cursor, batch] = await this.client.scan(cursor, "MATCH", pattern, "COUNT", this.yieldKeysScanBatchSize);
104
+ for (const key of batch) {
105
+ yield this._getDeprefixedKey(key);
106
+ }
107
+ }
108
+ }
109
+ }
110
+ exports.RedisByteStore = RedisByteStore;