{"version":3,"file":"text.cjs","names":["BaseDocumentLoader","Document","getEnv"],"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;CAC9B;CAAnB,YAAY,gBAAsC;EAChD,MAAM;EADW,KAAA,iBAAA;CAEnB;;;;;;;;CASA,MAAgB,MAAM,KAAgC;EACpD,OAAO,CAAC,GAAG;CACb;;;;;;;;;;;;CAaA,MAAa,OAA4B;EACvC,IAAI;EACJ,IAAI;EACJ,IAAI,OAAO,KAAK,mBAAmB,UAAU;GAC3C,MAAM,EAAE,aAAa,MAAM,WAAW,QAAQ;GAC9C,OAAO,MAAM,SAAS,KAAK,gBAAgB,MAAM;GACjD,WAAW,EAAE,QAAQ,KAAK,eAAe;EAC3C,OAAO;GACL,OAAO,MAAM,KAAK,eAAe,KAAK;GACtC,WAAW;IAAE,QAAQ;IAAQ,UAAU,KAAK,eAAe;GAAK;EAClE;EACA,MAAM,SAAS,MAAM,KAAK,MAAM,IAAI;EACpC,OAAO,SAAS,aAAa,MAAM;GACjC,IAAI,OAAO,gBAAgB,UACzB,MAAM,IAAI,MACR,gCAAgC,EAAE,OAAO,OAAO,aAClD;EAEJ,CAAC;EACD,OAAO,OAAO,KACX,aAAa,MACZ,IAAIC,0BAAAA,SAAS;GACX;GACA,UACE,OAAO,WAAW,IACd,WACA;IACE,GAAG;IACH,MAAM,IAAI;GACZ;EACR,CAAC,CACL;CACF;;;;;;;;;CAUA,aAAa,UAEV;EACD,IAAI;GACF,MAAM,EAAE,aAAa,MAAM,OAAO;GAClC,OAAO,EAAE,SAAS;EACpB,SAAS,GAAG;GACV,QAAQ,MAAM,CAAC;GACf,MAAM,IAAI,MACR,yHAAA,GAAwHC,0BAAAA,OAAAA,CAAO,EAAE,gDACnI;EACF;CACF;AACF"}