{"version":3,"file":"gcs.cjs","names":["Docstore","config: GoogleCloudStorageDocstoreConfiguration","Storage","search: string","Document","texts: Record<string, Document>","name: string","document: Document"],"sources":["../../../src/stores/doc/gcs.ts"],"sourcesContent":["import { Storage, File } from \"@google-cloud/storage\";\n\nimport { Document } from \"@langchain/core/documents\";\nimport { Docstore } from \"@langchain/classic/stores/doc/base\";\n\n/**\n * Interface that defines the configuration for the\n * GoogleCloudStorageDocstore. It includes the bucket name and an optional\n * prefix.\n */\nexport interface GoogleCloudStorageDocstoreConfiguration {\n  /** The identifier for the GCS bucket */\n  bucket: string;\n\n  /**\n   * An optional prefix to prepend to each object name.\n   * Often used to create a pseudo-hierarchy.\n   */\n  prefix?: string;\n}\n\n/**\n * Class that provides an interface for interacting with Google Cloud\n * Storage (GCS) as a document store. It extends the Docstore class and\n * implements methods to search, add, and add a document to the GCS\n * bucket.\n */\nexport class GoogleCloudStorageDocstore extends Docstore {\n  bucket: string;\n\n  prefix = \"\";\n\n  storage: Storage;\n\n  constructor(config: GoogleCloudStorageDocstoreConfiguration) {\n    super();\n\n    this.bucket = config.bucket;\n    this.prefix = config.prefix ?? this.prefix;\n\n    this.storage = new Storage();\n  }\n\n  /**\n   * Searches for a document in the GCS bucket and returns it as a Document\n   * instance.\n   * @param search The name of the document to search for in the GCS bucket\n   * @returns A Promise that resolves to a Document instance representing the found document\n   */\n  async search(search: string): Promise<Document> {\n    const file = this.getFile(search);\n\n    const [fileMetadata] = await file.getMetadata();\n    const metadata = fileMetadata?.metadata;\n\n    const [dataBuffer] = await file.download();\n    const pageContent = dataBuffer.toString();\n\n    const ret = new Document({\n      pageContent,\n      metadata,\n    });\n\n    return ret;\n  }\n\n  /**\n   * Adds multiple documents to the GCS bucket.\n   * @param texts An object where each key is the name of a document and the value is the Document instance to be added\n   * @returns A Promise that resolves when all documents have been added\n   */\n  async add(texts: Record<string, Document>): Promise<void> {\n    await Promise.all(\n      Object.keys(texts).map((key) => this.addDocument(key, texts[key]))\n    );\n  }\n\n  /**\n   * Adds a single document to the GCS bucket.\n   * @param name The name of the document to be added\n   * @param document The Document instance to be added\n   * @returns A Promise that resolves when the document has been added\n   */\n  async addDocument(name: string, document: Document): Promise<void> {\n    const file = this.getFile(name);\n    await file.save(document.pageContent);\n    await file.setMetadata({ metadata: document.metadata });\n  }\n\n  /**\n   * Gets a file from the GCS bucket.\n   * @param name The name of the file to get from the GCS bucket\n   * @returns A File instance representing the fetched file\n   */\n  private getFile(name: string): File {\n    const filename = this.prefix + name;\n    const file = this.storage.bucket(this.bucket).file(filename);\n    return file;\n  }\n}\n"],"mappings":";;;;;;;;;;;;;;AA2BA,IAAa,6BAAb,cAAgDA,6CAAS;CACvD;CAEA,SAAS;CAET;CAEA,YAAYC,QAAiD;EAC3D,OAAO;EAEP,KAAK,SAAS,OAAO;EACrB,KAAK,SAAS,OAAO,UAAU,KAAK;EAEpC,KAAK,UAAU,IAAIC;CACpB;;;;;;;CAQD,MAAM,OAAOC,QAAmC;EAC9C,MAAM,OAAO,KAAK,QAAQ,OAAO;EAEjC,MAAM,CAAC,aAAa,GAAG,MAAM,KAAK,aAAa;EAC/C,MAAM,WAAW,cAAc;EAE/B,MAAM,CAAC,WAAW,GAAG,MAAM,KAAK,UAAU;EAC1C,MAAM,cAAc,WAAW,UAAU;EAEzC,MAAM,MAAM,IAAIC,oCAAS;GACvB;GACA;EACD;AAED,SAAO;CACR;;;;;;CAOD,MAAM,IAAIC,OAAgD;EACxD,MAAM,QAAQ,IACZ,OAAO,KAAK,MAAM,CAAC,IAAI,CAAC,QAAQ,KAAK,YAAY,KAAK,MAAM,KAAK,CAAC,CACnE;CACF;;;;;;;CAQD,MAAM,YAAYC,MAAcC,UAAmC;EACjE,MAAM,OAAO,KAAK,QAAQ,KAAK;EAC/B,MAAM,KAAK,KAAK,SAAS,YAAY;EACrC,MAAM,KAAK,YAAY,EAAE,UAAU,SAAS,SAAU,EAAC;CACxD;;;;;;CAOD,AAAQ,QAAQD,MAAoB;EAClC,MAAM,WAAW,KAAK,SAAS;EAC/B,MAAM,OAAO,KAAK,QAAQ,OAAO,KAAK,OAAO,CAAC,KAAK,SAAS;AAC5D,SAAO;CACR;AACF"}