{"version":3,"file":"upstash_redis.cjs","names":["BaseCache","Redis"],"sources":["../../src/caches/upstash_redis.ts"],"sourcesContent":["import { Redis, type RedisConfigNodejs } from \"@upstash/redis\";\n\nimport { Generation } from \"@langchain/core/outputs\";\nimport {\n  BaseCache,\n  deserializeStoredGeneration,\n  serializeGeneration,\n} from \"@langchain/core/caches\";\nimport { StoredGeneration } from \"@langchain/core/messages\";\n\nexport type UpstashRedisCacheProps = {\n  /**\n   * The config to use to instantiate an Upstash Redis client.\n   */\n  config?: RedisConfigNodejs;\n  /**\n   * An existing Upstash Redis client.\n   */\n  client?: Redis;\n  /**\n   * Time-to-live (TTL) for cached items in seconds.\n   */\n  ttl?: number;\n};\n\n/**\n * A cache that uses Upstash as the backing store.\n * See https://docs.upstash.com/redis.\n * @example\n * ```typescript\n * const cache = new UpstashRedisCache({\n *   config: {\n *     url: \"UPSTASH_REDIS_REST_URL\",\n *     token: \"UPSTASH_REDIS_REST_TOKEN\",\n *   },\n *   ttl: 3600, // Optional: Cache entries will expire after 1 hour\n * });\n * // Initialize the OpenAI model with Upstash Redis cache for caching responses\n * const model = new ChatOpenAI({\n *   model: \"gpt-4o-mini\",\n *   cache,\n * });\n * await model.invoke(\"How are you today?\");\n * const cachedValues = await cache.lookup(\"How are you today?\", \"llmKey\");\n * ```\n */\nexport class UpstashRedisCache extends BaseCache {\n  private redisClient: Redis;\n\n  private ttl?: number;\n\n  constructor(props: UpstashRedisCacheProps) {\n    super();\n    const { config, client, ttl } = props;\n    this.ttl = ttl;\n\n    if (client) {\n      this.redisClient = client;\n    } else if (config) {\n      this.redisClient = new Redis(config);\n    } else {\n      throw new Error(\n        `Upstash Redis caches require either a config object or a pre-configured client.`\n      );\n    }\n  }\n\n  /**\n   * Lookup LLM generations in cache by prompt and associated LLM key.\n   */\n  public async lookup(prompt: string, llmKey: string) {\n    let idx = 0;\n    let key = this.keyEncoder(prompt, llmKey, String(idx));\n    let value = await this.redisClient.get<StoredGeneration | null>(key);\n    const generations: Generation[] = [];\n\n    while (value) {\n      generations.push(deserializeStoredGeneration(value));\n      idx += 1;\n      key = this.keyEncoder(prompt, llmKey, String(idx));\n      value = await this.redisClient.get<StoredGeneration | null>(key);\n    }\n\n    return generations.length > 0 ? generations : null;\n  }\n\n  /**\n   * Update the cache with the given generations.\n   *\n   * Note this overwrites any existing generations for the given prompt and LLM key.\n   */\n  public async update(prompt: string, llmKey: string, value: Generation[]) {\n    for (let i = 0; i < value.length; i += 1) {\n      const key = this.keyEncoder(prompt, llmKey, String(i));\n      const serializedValue = JSON.stringify(serializeGeneration(value[i]));\n\n      if (this.ttl) {\n        await this.redisClient.set(key, serializedValue, { ex: this.ttl });\n      } else {\n        await this.redisClient.set(key, serializedValue);\n      }\n    }\n  }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AA8CA,IAAa,oBAAb,cAAuCA,uBAAAA,UAAU;CAC/C;CAEA;CAEA,YAAY,OAA+B;AACzC,SAAO;EACP,MAAM,EAAE,QAAQ,QAAQ,QAAQ;AAChC,OAAK,MAAM;AAEX,MAAI,OACF,MAAK,cAAc;WACV,OACT,MAAK,cAAc,IAAIC,eAAAA,MAAM,OAAO;MAEpC,OAAM,IAAI,MACR,kFACD;;;;;CAOL,MAAa,OAAO,QAAgB,QAAgB;EAClD,IAAI,MAAM;EACV,IAAI,MAAM,KAAK,WAAW,QAAQ,QAAQ,OAAO,IAAI,CAAC;EACtD,IAAI,QAAQ,MAAM,KAAK,YAAY,IAA6B,IAAI;EACpE,MAAM,cAA4B,EAAE;AAEpC,SAAO,OAAO;AACZ,eAAY,MAAA,GAAA,uBAAA,6BAAiC,MAAM,CAAC;AACpD,UAAO;AACP,SAAM,KAAK,WAAW,QAAQ,QAAQ,OAAO,IAAI,CAAC;AAClD,WAAQ,MAAM,KAAK,YAAY,IAA6B,IAAI;;AAGlE,SAAO,YAAY,SAAS,IAAI,cAAc;;;;;;;CAQhD,MAAa,OAAO,QAAgB,QAAgB,OAAqB;AACvE,OAAK,IAAI,IAAI,GAAG,IAAI,MAAM,QAAQ,KAAK,GAAG;GACxC,MAAM,MAAM,KAAK,WAAW,QAAQ,QAAQ,OAAO,EAAE,CAAC;GACtD,MAAM,kBAAkB,KAAK,WAAA,GAAA,uBAAA,qBAA8B,MAAM,GAAG,CAAC;AAErE,OAAI,KAAK,IACP,OAAM,KAAK,YAAY,IAAI,KAAK,iBAAiB,EAAE,IAAI,KAAK,KAAK,CAAC;OAElE,OAAM,KAAK,YAAY,IAAI,KAAK,gBAAgB"}