{"version":3,"file":"vercel_kv.cjs","names":["BaseStore","fields?: {\n    client?: VercelKV;\n    ttl?: number;\n    namespace?: string;\n    yieldKeysScanBatchSize?: number;\n  }","kv","key: string","keys: string[]","keyValuePairs: [string, Uint8Array][]","prefix?: string"],"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  }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAyBA,IAAa,gBAAb,cAAmCA,kCAA8B;CAC/D,eAAe,CAAC,aAAa,SAAU;CAEvC,AAAU;CAEV,AAAU;CAEV,AAAU;CAEV,AAAU,yBAAyB;CAEnC,YAAYC,QAKT;EACD,MAAM,OAAO;EACb,KAAK,SAAS,QAAQ,UAAUC;EAChC,KAAK,MAAM,QAAQ;EACnB,KAAK,YAAY,QAAQ;EACzB,KAAK,yBACH,QAAQ,0BAA0B,KAAK;CAC1C;CAED,gBAAgBC,KAAa;AAC3B,MAAI,KAAK,WAAW;GAClB,MAAM,YAAY;AAClB,UAAO,GAAG,KAAK,YAAY,YAAY,KAAK;EAC7C;AACD,SAAO;CACR;CAED,kBAAkBA,KAAa;AAC7B,MAAI,KAAK,WAAW;GAClB,MAAM,YAAY;AAClB,UAAO,IAAI,MAAM,KAAK,UAAU,SAAS,EAAiB;EAC3D;AACD,SAAO;CACR;;;;;;CAOD,MAAM,KAAKC,MAAgB;EACzB,MAAM,eAAe,KAAK,IAAI,KAAK,gBAAgB,KAAK,KAAK,CAAC;EAC9D,MAAM,kBAAkB,MAAM,KAAK,OAAO,KACxC,GAAG,aACJ;EACD,MAAM,UAAU,IAAI;AACpB,SAAO,gBAAgB,IAAI,CAAC,UAAU;AACpC,OAAI,UAAU,UAAa,UAAU,KACnC,QAAO;YACE,OAAO,UAAU,SAC1B,QAAO,QAAQ,OAAO,KAAK,UAAU,MAAM,CAAC;OAE5C,QAAO,QAAQ,OAAO,MAAM;EAE/B,EAAC;CACH;;;;;;CAOD,MAAM,KAAKC,eAAsD;EAC/D,MAAM,UAA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