{"version":3,"file":"caches.cjs","names":["BaseCache"],"sources":["../src/caches.ts"],"sourcesContent":["import type { KVNamespace } from \"@cloudflare/workers-types\";\n\nimport {\n  BaseCache,\n  serializeGeneration,\n  deserializeStoredGeneration,\n} from \"@langchain/core/caches\";\nimport { Generation } from \"@langchain/core/outputs\";\n\n/**\n * Represents a specific implementation of a caching mechanism using Cloudflare KV\n * as the underlying storage system. It extends the `BaseCache` class and\n * overrides its methods to provide the Cloudflare KV-specific logic.\n * @example\n * ```typescript\n * // Example of using OpenAI with Cloudflare KV as cache in a Cloudflare Worker\n * const cache = new CloudflareKVCache(env.KV_NAMESPACE);\n * const model = new ChatAnthropic({\n *   cache,\n * });\n * const response = await model.invoke(\"How are you today?\");\n * return new Response(JSON.stringify(response), {\n *   headers: { \"content-type\": \"application/json\" },\n * });\n *\n * ```\n */\nexport class CloudflareKVCache extends BaseCache {\n  private binding: KVNamespace;\n\n  constructor(binding: KVNamespace) {\n    super();\n    this.binding = binding;\n  }\n\n  /**\n   * Retrieves data from the cache. It constructs a cache key from the given\n   * `prompt` and `llmKey`, and retrieves the corresponding value from the\n   * Cloudflare KV namespace.\n   * @param prompt The prompt used to construct the cache key.\n   * @param llmKey The LLM key used to construct the cache key.\n   * @returns An array of Generations if found, null otherwise.\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.binding.get(key);\n    const generations: Generation[] = [];\n\n    while (value) {\n      generations.push(deserializeStoredGeneration(JSON.parse(value)));\n      idx += 1;\n      key = this.keyEncoder(prompt, llmKey, String(idx));\n      value = await this.binding.get(key);\n    }\n\n    return generations.length > 0 ? generations : null;\n  }\n\n  /**\n   * Updates the cache with new data. It constructs a cache key from the\n   * given `prompt` and `llmKey`, and stores the `value` in the Cloudflare KV\n   * namespace.\n   * @param prompt The prompt used to construct the cache key.\n   * @param llmKey The LLM key used to construct the cache key.\n   * @param value The value to be stored in the cache.\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      await this.binding.put(\n        key,\n        JSON.stringify(serializeGeneration(value[i]))\n      );\n    }\n  }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AA2BA,IAAa,oBAAb,cAAuCA,uBAAAA,UAAU;CAC/C;CAEA,YAAY,SAAsB;AAChC,SAAO;AACP,OAAK,UAAU;;;;;;;;;;CAWjB,MAAa,OAAO,QAAgB,QAAgB;EAClD,IAAI,MAAM;EACV,IAAI,MAAM,KAAK,WAAW,QAAQ,QAAQ,OAAO,IAAI,CAAC;EACtD,IAAI,QAAQ,MAAM,KAAK,QAAQ,IAAI,IAAI;EACvC,MAAM,cAA4B,EAAE;AAEpC,SAAO,OAAO;AACZ,eAAY,MAAA,GAAA,uBAAA,6BAAiC,KAAK,MAAM,MAAM,CAAC,CAAC;AAChE,UAAO;AACP,SAAM,KAAK,WAAW,QAAQ,QAAQ,OAAO,IAAI,CAAC;AAClD,WAAQ,MAAM,KAAK,QAAQ,IAAI,IAAI;;AAGrC,SAAO,YAAY,SAAS,IAAI,cAAc;;;;;;;;;;CAWhD,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;AACtD,SAAM,KAAK,QAAQ,IACjB,KACA,KAAK,WAAA,GAAA,uBAAA,qBAA8B,MAAM,GAAG,CAAC,CAC9C"}