Function loadIsolated

  • Reads a document's bytes with the loader of kind in a worker thread and answers what that loader answers.

    The bytes are copied into a buffer of the function's own, which is moved to the worker, so the caller's buffer stays usable. The worker's old generation is capped at maxHeapMb, the read ends after timeoutMs, with maxExternalMb it ends once the worker's memory outside its heap passes that bound, and with signal it ends when the caller aborts. On every path, an answer included, the worker is ended before the returned promise settles.

    Parameters

    • kind: IsolatedLoadKind

      The loader to read with.

    • bytes: Uint8Array<ArrayBufferLike>

      The document, as a Buffer or any Uint8Array.

    • options: IsolatedLoadOptions = {}

      The read's bounds.

    Returns Promise<LoadedDocument>

    What the loader answers: the document's content, metadata and format.

    Throws

    For a kind outside the twelve, bytes that are not a Uint8Array, an office loader's option given to a kind that does not take it, or maxExternalMb where worker.getHeapStatistics() is missing.

    Throws

    For a bound that is not a positive, finite number, a timeoutMs past 2,147,483,647, 'scan' without ocrPages of 1 or more, or an office loader's option that loader refuses.

    Throws

    When the worker reaches its heap cap, or its external memory passes maxExternalMb.

    Throws

    When the read passes timeoutMs.

    Throws

    When a Word or office file reaches its loader's bound, with the loader's message; from the office loader, its message and cause name the bound.

    Throws

    When a scan's pages gave no text.

    Throws

    The reason of options.signal when the caller aborts it.

    Throws

    The loader's own error, with its name and message, such as DocxLoader: not a Word archive; and the error the worker ended on, such as one tesseract.js threw outside officeparser's promise.

    Example: A Word file from someone you do not know

    import { loadIsolated, DocumentTooComplexError, DocumentReadTimeoutError } from '@framers/agentos/cognition/memory';

    try {
    const doc = await loadIsolated('docx', upload, { maxHeapMb: 256, timeoutMs: 30_000, maxExternalMb: 128 });
    console.log(doc.content.length);
    } catch (error) {
    if (error instanceof DocumentTooComplexError) console.log(`more than ${error.limitMb} MiB of ${error.resource}`);
    else if (error instanceof DocumentReadTimeoutError) console.log(`not read within ${error.timeoutMs} ms`);
    else throw error;
    }

    Example: A spreadsheet, and a scan read by OCR over its first two pages

    const sheet = await loadIsolated('xlsx', upload, { maxTableCells: 200_000, signal: AbortSignal.timeout(30_000) });
    const scan = await loadIsolated('scan', scanned, { ocrPages: 2, timeoutMs: 120_000 });