@langchain/core
Version:
Core LangChain.js abstractions and schemas
1 lines • 4.44 kB
Source Map (JSON)
{"version":3,"file":"stores.cjs","names":["Serializable","keys: string[]","keyValuePairs: [string, T][]","prefix?: string | undefined"],"sources":["../src/stores.ts"],"sourcesContent":["import { Serializable } from \"./load/serializable.js\";\n\n/**\n * Abstract interface for a key-value store.\n */\nexport abstract class BaseStore<K, V> extends Serializable {\n /**\n * Abstract method to get multiple values for a set of keys.\n * @param {K[]} keys - An array of keys.\n * @returns {Promise<(V | undefined)[]>} - A Promise that resolves with array of values or undefined if key not found.\n */\n abstract mget(keys: K[]): Promise<(V | undefined)[]>;\n\n /**\n * Abstract method to set a value for multiple keys.\n * @param {[K, V][]} keyValuePairs - An array of key-value pairs.\n * @returns {Promise<void>} - A Promise that resolves when the operation is complete.\n */\n abstract mset(keyValuePairs: [K, V][]): Promise<void>;\n\n /**\n * Abstract method to delete multiple keys.\n * @param {K[]} keys - An array of keys to delete.\n * @returns {Promise<void>} - A Promise that resolves when the operation is complete.\n */\n abstract mdelete(keys: K[]): Promise<void>;\n\n /**\n * Abstract method to yield keys optionally based on a prefix.\n * @param {string} prefix - Optional prefix to filter keys.\n * @returns {AsyncGenerator<K | string>} - An asynchronous generator that yields keys on iteration.\n */\n abstract yieldKeys(prefix?: string): AsyncGenerator<K | string>;\n}\n\n/**\n * In-memory implementation of the BaseStore using a dictionary. Used for\n * storing key-value pairs in memory.\n * @example\n * ```typescript\n * const store = new InMemoryStore<BaseMessage>();\n * await store.mset(\n * Array.from({ length: 5 }).map((_, index) => [\n * `message:id:${index}`,\n * index % 2 === 0\n * ? new AIMessage(\"ai stuff...\")\n * : new HumanMessage(\"human stuff...\"),\n * ]),\n * );\n *\n * const retrievedMessages = await store.mget([\"message:id:0\", \"message:id:1\"]);\n * await store.mdelete(await store.yieldKeys(\"message:id:\").toArray());\n * ```\n */\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport class InMemoryStore<T = any> extends BaseStore<string, T> {\n lc_namespace = [\"langchain\", \"storage\"];\n\n protected store: Record<string, T> = {};\n\n /**\n * Retrieves the values associated with the given keys from the store.\n * @param keys Keys to retrieve values for.\n * @returns Array of values associated with the given keys.\n */\n async mget(keys: string[]) {\n return keys.map((key) => this.store[key]);\n }\n\n /**\n * Sets the values for the given keys in the store.\n * @param keyValuePairs Array of key-value pairs to set in the store.\n * @returns Promise that resolves when all key-value pairs have been set.\n */\n async mset(keyValuePairs: [string, T][]): Promise<void> {\n for (const [key, value] of keyValuePairs) {\n this.store[key] = value;\n }\n }\n\n /**\n * Deletes the given keys and their associated values from the store.\n * @param keys Keys to delete from the store.\n * @returns Promise that resolves when all keys have been deleted.\n */\n async mdelete(keys: string[]): Promise<void> {\n for (const key of keys) {\n delete this.store[key];\n }\n }\n\n /**\n * Asynchronous generator that yields keys from the store. If a prefix is\n * provided, it only yields keys that start with the prefix.\n * @param prefix Optional prefix to filter keys.\n * @returns AsyncGenerator that yields keys from the store.\n */\n async *yieldKeys(prefix?: string | undefined): AsyncGenerator<string> {\n const keys = Object.keys(this.store);\n for (const key of keys) {\n if (prefix === undefined || key.startsWith(prefix)) {\n yield key;\n }\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;AAKA,IAAsB,YAAtB,cAA8CA,uCAAa,CA4B1D;;;;;;;;;;;;;;;;;;;;AAsBD,IAAa,gBAAb,cAA4C,UAAqB;CAC/D,eAAe,CAAC,aAAa,SAAU;CAEvC,AAAU,QAA2B,CAAE;;;;;;CAOvC,MAAM,KAAKC,MAAgB;AACzB,SAAO,KAAK,IAAI,CAAC,QAAQ,KAAK,MAAM,KAAK;CAC1C;;;;;;CAOD,MAAM,KAAKC,eAA6C;AACtD,OAAK,MAAM,CAAC,KAAK,MAAM,IAAI,eACzB,KAAK,MAAM,OAAO;CAErB;;;;;;CAOD,MAAM,QAAQD,MAA+B;AAC3C,OAAK,MAAM,OAAO,MAChB,OAAO,KAAK,MAAM;CAErB;;;;;;;CAQD,OAAO,UAAUE,QAAqD;EACpE,MAAM,OAAO,OAAO,KAAK,KAAK,MAAM;AACpC,OAAK,MAAM,OAAO,KAChB,KAAI,WAAW,UAAa,IAAI,WAAW,OAAO,EAChD,MAAM;CAGX;AACF"}