stores.d.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import { Serializable } from "./load/serializable.js";
  2. /** @deprecated For backwards compatibility only. Remove on next minor version upgrade. */
  3. export interface BaseStoreInterface<K, V> {
  4. /**
  5. * Method to get multiple values for a set of keys.
  6. * @param {K[]} keys - An array of keys.
  7. * @returns {Promise<(V | undefined)[]>} - A Promise that resolves with array of values or undefined if key not found.
  8. */
  9. mget(keys: K[]): Promise<(V | undefined)[]>;
  10. /**
  11. * Method to set a value for multiple keys.
  12. * @param {[K, V][]} keyValuePairs - An array of key-value pairs.
  13. * @returns {Promise<void>} - A Promise that resolves when the operation is complete.
  14. */
  15. mset(keyValuePairs: [K, V][]): Promise<void>;
  16. /**
  17. * Method to delete multiple keys.
  18. * @param {K[]} keys - An array of keys to delete.
  19. * @returns {Promise<void>} - A Promise that resolves when the operation is complete.
  20. */
  21. mdelete(keys: K[]): Promise<void>;
  22. /**
  23. * Method to yield keys optionally based on a prefix.
  24. * @param {string} prefix - Optional prefix to filter keys.
  25. * @returns {AsyncGenerator<K | string>} - An asynchronous generator that yields keys on iteration.
  26. */
  27. yieldKeys(prefix?: string): AsyncGenerator<K | string>;
  28. }
  29. /**
  30. * Abstract interface for a key-value store.
  31. */
  32. export declare abstract class BaseStore<K, V> extends Serializable implements BaseStoreInterface<K, V> {
  33. /**
  34. * Abstract method to get multiple values for a set of keys.
  35. * @param {K[]} keys - An array of keys.
  36. * @returns {Promise<(V | undefined)[]>} - A Promise that resolves with array of values or undefined if key not found.
  37. */
  38. abstract mget(keys: K[]): Promise<(V | undefined)[]>;
  39. /**
  40. * Abstract method to set a value for multiple keys.
  41. * @param {[K, V][]} keyValuePairs - An array of key-value pairs.
  42. * @returns {Promise<void>} - A Promise that resolves when the operation is complete.
  43. */
  44. abstract mset(keyValuePairs: [K, V][]): Promise<void>;
  45. /**
  46. * Abstract method to delete multiple keys.
  47. * @param {K[]} keys - An array of keys to delete.
  48. * @returns {Promise<void>} - A Promise that resolves when the operation is complete.
  49. */
  50. abstract mdelete(keys: K[]): Promise<void>;
  51. /**
  52. * Abstract method to yield keys optionally based on a prefix.
  53. * @param {string} prefix - Optional prefix to filter keys.
  54. * @returns {AsyncGenerator<K | string>} - An asynchronous generator that yields keys on iteration.
  55. */
  56. abstract yieldKeys(prefix?: string): AsyncGenerator<K | string>;
  57. }
  58. /**
  59. * In-memory implementation of the BaseStore using a dictionary. Used for
  60. * storing key-value pairs in memory.
  61. * @example
  62. * ```typescript
  63. * const store = new InMemoryStore<BaseMessage>();
  64. * await store.mset(
  65. * Array.from({ length: 5 }).map((_, index) => [
  66. * `message:id:${index}`,
  67. * index % 2 === 0
  68. * ? new AIMessage("ai stuff...")
  69. * : new HumanMessage("human stuff..."),
  70. * ]),
  71. * );
  72. *
  73. * const retrievedMessages = await store.mget(["message:id:0", "message:id:1"]);
  74. * await store.mdelete(await store.yieldKeys("message:id:").toArray());
  75. * ```
  76. */
  77. export declare class InMemoryStore<T = any> extends BaseStore<string, T> {
  78. lc_namespace: string[];
  79. protected store: Record<string, T>;
  80. /**
  81. * Retrieves the values associated with the given keys from the store.
  82. * @param keys Keys to retrieve values for.
  83. * @returns Array of values associated with the given keys.
  84. */
  85. mget(keys: string[]): Promise<T[]>;
  86. /**
  87. * Sets the values for the given keys in the store.
  88. * @param keyValuePairs Array of key-value pairs to set in the store.
  89. * @returns Promise that resolves when all key-value pairs have been set.
  90. */
  91. mset(keyValuePairs: [string, T][]): Promise<void>;
  92. /**
  93. * Deletes the given keys and their associated values from the store.
  94. * @param keys Keys to delete from the store.
  95. * @returns Promise that resolves when all keys have been deleted.
  96. */
  97. mdelete(keys: string[]): Promise<void>;
  98. /**
  99. * Asynchronous generator that yields keys from the store. If a prefix is
  100. * provided, it only yields keys that start with the prefix.
  101. * @param prefix Optional prefix to filter keys.
  102. * @returns AsyncGenerator that yields keys from the store.
  103. */
  104. yieldKeys(prefix?: string | undefined): AsyncGenerator<string>;
  105. }