@langchain/community
Version:
Third-party integrations for LangChain.js
1 lines • 6.38 kB
Source Map (JSON)
{"version":3,"file":"vercel_kv.cjs","names":["BaseStore","kv"],"sources":["../../src/storage/vercel_kv.ts"],"sourcesContent":["import { kv, type VercelKV } from \"@vercel/kv\";\n\nimport { BaseStore } from \"@langchain/core/stores\";\n\n/**\n * Class that extends the BaseStore class to interact with a Vercel KV\n * database. It provides methods for getting, setting, and deleting data,\n * as well as yielding keys from the database.\n * @example\n * ```typescript\n * const store = new VercelKVStore({\n * client: getClient(),\n * });\n * await store.mset([\n * { key: \"message:id:0\", value: \"encoded message 0\" },\n * { key: \"message:id:1\", value: \"encoded message 1\" },\n * ]);\n * const retrievedMessages = await store.mget([\"message:id:0\", \"message:id:1\"]);\n * const yieldedKeys = [];\n * for await (const key of store.yieldKeys(\"message:id:\")) {\n * yieldedKeys.push(key);\n * }\n * await store.mdelete(yieldedKeys);\n * ```\n */\nexport class VercelKVStore extends BaseStore<string, Uint8Array> {\n lc_namespace = [\"langchain\", \"storage\"];\n\n protected client: VercelKV;\n\n protected ttl?: number;\n\n protected namespace?: string;\n\n protected yieldKeysScanBatchSize = 1000;\n\n constructor(fields?: {\n client?: VercelKV;\n ttl?: number;\n namespace?: string;\n yieldKeysScanBatchSize?: number;\n }) {\n super(fields);\n this.client = fields?.client ?? kv;\n this.ttl = fields?.ttl;\n this.namespace = fields?.namespace;\n this.yieldKeysScanBatchSize =\n fields?.yieldKeysScanBatchSize ?? this.yieldKeysScanBatchSize;\n }\n\n _getPrefixedKey(key: string) {\n if (this.namespace) {\n const delimiter = \"/\";\n return `${this.namespace}${delimiter}${key}`;\n }\n return key;\n }\n\n _getDeprefixedKey(key: string) {\n if (this.namespace) {\n const delimiter = \"/\";\n return key.slice(this.namespace.length + delimiter.length);\n }\n return key;\n }\n\n /**\n * Gets multiple keys from the Redis database.\n * @param keys Array of keys to be retrieved.\n * @returns An array of retrieved values.\n */\n async mget(keys: string[]) {\n const prefixedKeys = keys.map(this._getPrefixedKey.bind(this));\n const retrievedValues = await this.client.mget<(string | undefined)[]>(\n ...prefixedKeys\n );\n const encoder = new TextEncoder();\n return retrievedValues.map((value) => {\n if (value === undefined || value === null) {\n return undefined;\n } else if (typeof value === \"object\") {\n return encoder.encode(JSON.stringify(value));\n } else {\n return encoder.encode(value);\n }\n });\n }\n\n /**\n * Sets multiple keys in the Redis database.\n * @param keyValuePairs Array of key-value pairs to be set.\n * @returns Promise that resolves when all keys have been set.\n */\n async mset(keyValuePairs: [string, Uint8Array][]): Promise<void> {\n const decoder = new TextDecoder();\n const decodedKeyValuePairs = keyValuePairs.map(([key, value]) => [\n this._getPrefixedKey(key),\n decoder.decode(value),\n ]);\n const pipeline = this.client.pipeline();\n for (const [key, value] of decodedKeyValuePairs) {\n if (this.ttl) {\n pipeline.setex(key, this.ttl, value);\n } else {\n pipeline.set(key, value);\n }\n }\n await pipeline.exec();\n }\n\n /**\n * Deletes multiple keys from the Redis database.\n * @param keys Array of keys to be deleted.\n * @returns Promise that resolves when all keys have been deleted.\n */\n async mdelete(keys: string[]): Promise<void> {\n await this.client.del(...keys.map(this._getPrefixedKey.bind(this)));\n }\n\n /**\n * Yields keys from the Redis database.\n * @param prefix Optional prefix to filter the keys.\n * @returns An AsyncGenerator that yields keys from the Redis database.\n */\n async *yieldKeys(prefix?: string): AsyncGenerator<string> {\n let pattern;\n if (prefix) {\n const wildcardPrefix = prefix.endsWith(\"*\") ? prefix : `${prefix}*`;\n pattern = this._getPrefixedKey(wildcardPrefix);\n } else {\n pattern = this._getPrefixedKey(\"*\");\n }\n let [cursor, batch] = await this.client.scan(0, {\n match: pattern,\n count: this.yieldKeysScanBatchSize,\n });\n for (const key of batch) {\n yield this._getDeprefixedKey(key);\n }\n // Backwards compatibility for older @vercel/kv versions\n while (String(cursor) !== \"0\") {\n [cursor, batch] = await this.client.scan(cursor, {\n match: pattern,\n count: this.yieldKeysScanBatchSize,\n });\n for (const key of batch) {\n yield this._getDeprefixedKey(key);\n }\n }\n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