{"version":3,"file":"text.cjs","names":["BaseDocumentLoader","Document"],"sources":["../../../src/document_loaders/fs/text.ts"],"sourcesContent":["import type { readFile as ReadFileT } from \"node:fs/promises\";\nimport { Document } from \"@langchain/core/documents\";\nimport { getEnv } from \"@langchain/core/utils/env\";\nimport { BaseDocumentLoader } from \"@langchain/core/document_loaders/base\";\n\n/**\n * A class that extends the `BaseDocumentLoader` class. It represents a\n * document loader that loads documents from a text file. The `load()`\n * method is implemented to read the text from the file or blob, parse it\n * using the `parse()` method, and create a `Document` instance for each\n * parsed page. The metadata includes the source of the text (file path or\n * blob) and, if there are multiple pages, the line number of each page.\n * @example\n * ```typescript\n * const loader = new TextLoader(\"src/document_loaders/example_data/example.txt\");\n * const docs = await loader.load();\n * ```\n */\nexport class TextLoader extends BaseDocumentLoader {\n  constructor(public filePathOrBlob: string | Blob) {\n    super();\n  }\n\n  /**\n   * A protected method that takes a `raw` string as a parameter and returns\n   * a promise that resolves to an array containing the raw text as a single\n   * element.\n   * @param raw The raw text to be parsed.\n   * @returns A promise that resolves to an array containing the raw text as a single element.\n   */\n  protected async parse(raw: string): Promise<string[]> {\n    return [raw];\n  }\n\n  /**\n   * A method that loads the text file or blob and returns a promise that\n   * resolves to an array of `Document` instances. It reads the text from\n   * the file or blob using the `readFile` function from the\n   * `node:fs/promises` module or the `text()` method of the blob. It then\n   * parses the text using the `parse()` method and creates a `Document`\n   * instance for each parsed page. The metadata includes the source of the\n   * text (file path or blob) and, if there are multiple pages, the line\n   * number of each page.\n   * @returns A promise that resolves to an array of `Document` instances.\n   */\n  public async load(): Promise<Document[]> {\n    let text: string;\n    let metadata: Record<string, string>;\n    if (typeof this.filePathOrBlob === \"string\") {\n      const { readFile } = await TextLoader.imports();\n      text = await readFile(this.filePathOrBlob, \"utf8\");\n      metadata = { source: this.filePathOrBlob };\n    } else {\n      text = await this.filePathOrBlob.text();\n      metadata = { source: \"blob\", blobType: this.filePathOrBlob.type };\n    }\n    const parsed = await this.parse(text);\n    parsed.forEach((pageContent, i) => {\n      if (typeof pageContent !== \"string\") {\n        throw new Error(\n          `Expected string, at position ${i} got ${typeof pageContent}`\n        );\n      }\n    });\n    return parsed.map(\n      (pageContent, i) =>\n        new Document({\n          pageContent,\n          metadata:\n            parsed.length === 1\n              ? metadata\n              : {\n                  ...metadata,\n                  line: i + 1,\n                },\n        })\n    );\n  }\n\n  /**\n   * A static method that imports the `readFile` function from the\n   * `node:fs/promises` module. It is used to dynamically import the\n   * function when needed. If the import fails, it throws an error\n   * indicating that the `fs/promises` module is not available in the\n   * current environment.\n   * @returns A promise that resolves to an object containing the `readFile` function from the `node:fs/promises` module.\n   */\n  static async imports(): Promise<{\n    readFile: typeof ReadFileT;\n  }> {\n    try {\n      const { readFile } = await import(\"node:fs/promises\");\n      return { readFile };\n    } catch (e) {\n      console.error(e);\n      throw new Error(\n        `Failed to load fs/promises. TextLoader available only on environment 'node'. It appears you are running environment '${getEnv()}'. See https://<link to docs> for alternatives.`\n      );\n    }\n  }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAkBA,IAAa,aAAb,MAAa,mBAAmBA,sCAAAA,mBAAmB;CACjD,YAAY,gBAAsC;AAChD,SAAO;AADU,OAAA,iBAAA;;;;;;;;;CAWnB,MAAgB,MAAM,KAAgC;AACpD,SAAO,CAAC,IAAI;;;;;;;;;;;;;CAcd,MAAa,OAA4B;EACvC,IAAI;EACJ,IAAI;AACJ,MAAI,OAAO,KAAK,mBAAmB,UAAU;GAC3C,MAAM,EAAE,aAAa,MAAM,WAAW,SAAS;AAC/C,UAAO,MAAM,SAAS,KAAK,gBAAgB,OAAO;AAClD,cAAW,EAAE,QAAQ,KAAK,gBAAgB;SACrC;AACL,UAAO,MAAM,KAAK,eAAe,MAAM;AACvC,cAAW;IAAE,QAAQ;IAAQ,UAAU,KAAK,eAAe;IAAM;;EAEnE,MAAM,SAAS,MAAM,KAAK,MAAM,KAAK;AACrC,SAAO,SAAS,aAAa,MAAM;AACjC,OAAI,OAAO,gBAAgB,SACzB,OAAM,IAAI,MACR,gCAAgC,EAAE,OAAO,OAAO,cACjD;IAEH;AACF,SAAO,OAAO,KACX,aAAa,MACZ,IAAIC,0BAAAA,SAAS;GACX;GACA,UACE,OAAO,WAAW,IACd,WACA;IACE,GAAG;IACH,MAAM,IAAI;IACX;GACR,CAAC,CACL;;;;;;;;;;CAWH,aAAa,UAEV;AACD,MAAI;GACF,MAAM,EAAE,aAAa,MAAM,OAAO;AAClC,UAAO,EAAE,UAAU;WACZ,GAAG;AACV,WAAQ,MAAM,EAAE;AAChB,SAAM,IAAI,MACR,yHAAA,GAAA,0BAAA,SAAgI,CAAC,iDAClI"}