{"version":3,"file":"gcs.cjs","names":["Docstore","Storage","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,mCAAAA,SAAS;CACvD;CAEA,SAAS;CAET;CAEA,YAAY,QAAiD;AAC3D,SAAO;AAEP,OAAK,SAAS,OAAO;AACrB,OAAK,SAAS,OAAO,UAAU,KAAK;AAEpC,OAAK,UAAU,IAAIC,sBAAAA,SAAS;;;;;;;;CAS9B,MAAM,OAAO,QAAmC;EAC9C,MAAM,OAAO,KAAK,QAAQ,OAAO;EAEjC,MAAM,CAAC,gBAAgB,MAAM,KAAK,aAAa;EAC/C,MAAM,WAAW,cAAc;EAE/B,MAAM,CAAC,cAAc,MAAM,KAAK,UAAU;AAQ1C,SALY,IAAIC,0BAAAA,SAAS;GACvB,aAHkB,WAAW,UAAU;GAIvC;GACD,CAAC;;;;;;;CAUJ,MAAM,IAAI,OAAgD;AACxD,QAAM,QAAQ,IACZ,OAAO,KAAK,MAAM,CAAC,KAAK,QAAQ,KAAK,YAAY,KAAK,MAAM,KAAK,CAAC,CACnE;;;;;;;;CASH,MAAM,YAAY,MAAc,UAAmC;EACjE,MAAM,OAAO,KAAK,QAAQ,KAAK;AAC/B,QAAM,KAAK,KAAK,SAAS,YAAY;AACrC,QAAM,KAAK,YAAY,EAAE,UAAU,SAAS,UAAU,CAAC;;;;;;;CAQzD,QAAgB,MAAoB;EAClC,MAAM,WAAW,KAAK,SAAS;AAE/B,SADa,KAAK,QAAQ,OAAO,KAAK,OAAO,CAAC,KAAK,SAAS"}