Class: OfficeLoader
Defined in: packages/agentos/src/cognition/memory/io/ingestion/OfficeLoader.ts:292
Document loader for PowerPoint (.pptx), Excel (.xlsx), OpenDocument
(.odt, .ods, .odp), RTF (.rtf) and EPUB (.epub) files, read in
memory through officeparser, which is imported on the first read.
The loader reads bytes under the format its caller names: it reads no file from a path and does not guess a format from the bytes. The text is the document's as officeparser writes it, without its comments and without its pictures.
With OfficeLoaderOptions.ocrPages above 0, load(bytes, { format: 'pdf' })
reads a scanned PDF in a worker thread of its own (loadIsolated's kind
'scan'): officeparser reads the first ocrPages pages with pdf.js, in a
process of its own capped at pdfProcessMemoryMb, and tesseract.js
recognizes the text of each picture on them. A picture OCR could not read is
left out of the text, and the read goes on. A failure tesseract.js throws
outside officeparser's promise ends the worker thread, and the read rejects
with it.
Implements
Examples
const loader = new OfficeLoader({ maxInflatedBytes: 32 * 1024 * 1024, maxXmlElements: 400_000 });
try {
const doc = await loader.load(uploadedBuffer, { format: 'pptx', signal: AbortSignal.timeout(30_000) });
console.log(doc.metadata.wordCount);
} catch (error) {
if (error instanceof DocumentTooLargeError) {
console.log(error.cause); // names the bound the document reached
}
}
const loader = new OfficeLoader({ ocrPages: 3 });
const doc = await loader.load(scanBuffer, { format: 'pdf', signal: AbortSignal.timeout(120_000) });
Implements
Constructors
Constructor
new OfficeLoader(
options?):OfficeLoader
Defined in: packages/agentos/src/cognition/memory/io/ingestion/OfficeLoader.ts:301
Parameters
options?
OfficeLoaderOptions = {}
The loader's options; with none, an archive is bounded at 64 MiB inflated and a PDF is refused.
Returns
OfficeLoader
Throws
When a bound is not a positive, finite number, ocrPages is not a whole number of 0 or
more, or pdfProcessMemoryMb is under 64.
Properties
supportedExtensions
readonlysupportedExtensions:string[]
Defined in: packages/agentos/src/cognition/memory/io/ingestion/OfficeLoader.ts:294
File extensions this loader handles, each with a leading dot.
Used by LoaderRegistry to route file paths to the correct loader.
Example
['.md', '.mdx']
Implementation of
IDocumentLoader.supportedExtensions
Methods
canLoad()
canLoad(
source):boolean
Defined in: packages/agentos/src/cognition/memory/io/ingestion/OfficeLoader.ts:337
True for a file name with one of the loader's extensions. A buffer does
not say its format, so the answer for one is false; load reads it under
the format its caller names.
Parameters
source
A file name or path, or a document's bytes.
string | Buffer
Returns
boolean
Implementation of
load()
load(
source,options?):Promise<LoadedDocument>
Defined in: packages/agentos/src/cognition/memory/io/ingestion/OfficeLoader.ts:367
Reads a document's bytes under the format its caller names.
Parameters
source
The file's bytes. A path is refused: the loader reads no file.
string | Buffer
options?
format names the document's format: a value of OFFICE_TYPES, with or without a
leading dot, or pdf for a scanned PDF. signal cancels this read.
Returns
Promise<LoadedDocument>
The document's text, with metadata.wordCount, and metadata.pageCount when officeparser reports
a page count.
Throws
When the document reaches a bound, one officeparser only warns of included.
The error's limit is the loader's maxInflatedBytes whichever bound was reached, and its cause names
the bound by officeparser's code. So does its message, but past maxInflatedBytes, where the message
gives that bound in bytes.
Throws
For a PDF while ocrPages is 0, and for a scan whose pages gave no text. When
officeparser reported a picture OCR could not read, the first such failure is the error's cause.
Throws
The reason of options.signal when the caller aborts it.
Throws
When source is a path, when options.format names none of the formats, when officeparser
is not installed, and when a scan's pdf.js would run in the reading thread; the error a scan's worker
thread ended on, and loadIsolated's refusals of a scan's worker; and officeparser's own error for a file
it cannot read.