llm-large-files 0.7.0 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +319 -1
- package/README.md +10 -8
- package/dist/analyze.d.ts +3 -1
- package/dist/analyze.d.ts.map +1 -1
- package/dist/analyze.js +109 -14
- package/dist/analyze.js.map +1 -1
- package/dist/anthropic/adapter.d.ts.map +1 -1
- package/dist/anthropic/adapter.js +4 -1
- package/dist/anthropic/adapter.js.map +1 -1
- package/dist/ask.d.ts +3 -3
- package/dist/ask.d.ts.map +1 -1
- package/dist/ask.js +72 -17
- package/dist/ask.js.map +1 -1
- package/dist/decode.d.ts +19 -0
- package/dist/decode.d.ts.map +1 -0
- package/dist/decode.js +97 -0
- package/dist/decode.js.map +1 -0
- package/dist/errors.d.ts +55 -0
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +65 -0
- package/dist/errors.js.map +1 -1
- package/dist/events.d.ts +6 -0
- package/dist/events.d.ts.map +1 -1
- package/dist/events.js +5 -1
- package/dist/events.js.map +1 -1
- package/dist/evidence.d.ts.map +1 -1
- package/dist/evidence.js +6 -0
- package/dist/evidence.js.map +1 -1
- package/dist/extract.d.ts +7 -20
- package/dist/extract.d.ts.map +1 -1
- package/dist/extract.js +49 -751
- package/dist/extract.js.map +1 -1
- package/dist/gemini/adapter.d.ts.map +1 -1
- package/dist/gemini/adapter.js +9 -5
- package/dist/gemini/adapter.js.map +1 -1
- package/dist/gemini/endpoints.d.ts +5 -1
- package/dist/gemini/endpoints.d.ts.map +1 -1
- package/dist/gemini/endpoints.js +11 -2
- package/dist/gemini/endpoints.js.map +1 -1
- package/dist/gemini/generate.d.ts.map +1 -1
- package/dist/gemini/generate.js +20 -3
- package/dist/gemini/generate.js.map +1 -1
- package/dist/groups.d.ts +54 -0
- package/dist/groups.d.ts.map +1 -0
- package/dist/groups.js +136 -0
- package/dist/groups.js.map +1 -0
- package/dist/html.d.ts +11 -0
- package/dist/html.d.ts.map +1 -0
- package/dist/html.js +1112 -0
- package/dist/html.js.map +1 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/library.d.ts.map +1 -1
- package/dist/library.js +48 -12
- package/dist/library.js.map +1 -1
- package/dist/limiter.d.ts +2 -1
- package/dist/limiter.d.ts.map +1 -1
- package/dist/limiter.js +3 -2
- package/dist/limiter.js.map +1 -1
- package/dist/model.d.ts.map +1 -1
- package/dist/model.js +8 -1
- package/dist/model.js.map +1 -1
- package/dist/node/handlers.d.ts.map +1 -1
- package/dist/node/handlers.js +69 -15
- package/dist/node/handlers.js.map +1 -1
- package/dist/node/index.d.ts +2 -0
- package/dist/node/index.d.ts.map +1 -1
- package/dist/node/index.js +1 -0
- package/dist/node/index.js.map +1 -1
- package/dist/node/ingest-child.d.ts +38 -0
- package/dist/node/ingest-child.d.ts.map +1 -0
- package/dist/node/ingest-child.js +42 -0
- package/dist/node/ingest-child.js.map +1 -0
- package/dist/node/isolated.d.ts +88 -0
- package/dist/node/isolated.d.ts.map +1 -0
- package/dist/node/isolated.js +381 -0
- package/dist/node/isolated.js.map +1 -0
- package/dist/office.d.ts +105 -0
- package/dist/office.d.ts.map +1 -0
- package/dist/office.js +1092 -0
- package/dist/office.js.map +1 -0
- package/dist/openai/adapter.d.ts.map +1 -1
- package/dist/openai/adapter.js +15 -1
- package/dist/openai/adapter.js.map +1 -1
- package/dist/pdf-pages.d.ts.map +1 -1
- package/dist/pdf-pages.js +2 -6
- package/dist/pdf-pages.js.map +1 -1
- package/dist/peers.d.ts +8 -0
- package/dist/peers.d.ts.map +1 -0
- package/dist/peers.js +32 -0
- package/dist/peers.js.map +1 -0
- package/dist/plan.d.ts.map +1 -1
- package/dist/plan.js +1 -1
- package/dist/plan.js.map +1 -1
- package/dist/ranged.d.ts.map +1 -1
- package/dist/ranged.js +4 -2
- package/dist/ranged.js.map +1 -1
- package/dist/search.d.ts +99 -22
- package/dist/search.d.ts.map +1 -1
- package/dist/search.js +744 -140
- package/dist/search.js.map +1 -1
- package/dist/text-index.d.ts +3 -1
- package/dist/text-index.d.ts.map +1 -1
- package/dist/text-index.js +3 -1
- package/dist/text-index.js.map +1 -1
- package/dist/text.d.ts +27 -3
- package/dist/text.d.ts.map +1 -1
- package/dist/text.js +225 -23
- package/dist/text.js.map +1 -1
- package/dist/tools.d.ts.map +1 -1
- package/dist/tools.js +200 -12
- package/dist/tools.js.map +1 -1
- package/dist/types.d.ts +3 -3
- package/dist/types.d.ts.map +1 -1
- package/dist/word-styles.d.ts +102 -0
- package/dist/word-styles.d.ts.map +1 -0
- package/dist/word-styles.js +419 -0
- package/dist/word-styles.js.map +1 -0
- package/dist/xlsx.d.ts +23 -0
- package/dist/xlsx.d.ts.map +1 -0
- package/dist/xlsx.js +511 -0
- package/dist/xlsx.js.map +1 -0
- package/dist/xml-stream.d.ts +55 -0
- package/dist/xml-stream.d.ts.map +1 -0
- package/dist/xml-stream.js +235 -0
- package/dist/xml-stream.js.map +1 -0
- package/docs/README.md +8 -8
- package/docs/getting-started.md +1 -1
- package/docs/guides/costs-and-limits.md +7 -5
- package/docs/guides/deployment.md +95 -29
- package/docs/guides/errors-and-reliability.md +23 -10
- package/docs/guides/pdfs.md +127 -74
- package/docs/guides/storage.md +1 -1
- package/docs/guides/testing.md +5 -1
- package/docs/guides/web-app.md +10 -9
- package/docs/how-it-works.md +73 -50
- package/docs/reference/ask.md +26 -8
- package/docs/reference/errors-and-retries.md +5 -3
- package/docs/reference/events.md +14 -6
- package/docs/reference/gemini.md +6 -2
- package/docs/reference/library.md +205 -74
- package/docs/reference/node.md +200 -22
- package/docs/reference/openai.md +1 -1
- package/docs/reference/tools.md +170 -22
- package/docs/security.md +114 -69
- package/docs/troubleshooting.md +40 -20
- package/package.json +6 -2
- package/src/analyze.ts +117 -13
- package/src/anthropic/adapter.ts +4 -1
- package/src/ask.ts +73 -17
- package/src/decode.ts +89 -0
- package/src/errors.ts +108 -0
- package/src/events.ts +10 -1
- package/src/evidence.ts +6 -0
- package/src/extract.ts +52 -709
- package/src/gemini/adapter.ts +9 -5
- package/src/gemini/endpoints.ts +12 -2
- package/src/gemini/generate.ts +19 -3
- package/src/groups.ts +162 -0
- package/src/html.ts +1071 -0
- package/src/index.ts +2 -1
- package/src/library.ts +43 -12
- package/src/limiter.ts +6 -1
- package/src/model.ts +8 -1
- package/src/node/handlers.ts +66 -16
- package/src/node/index.ts +2 -0
- package/src/node/ingest-child.ts +61 -0
- package/src/node/isolated.ts +464 -0
- package/src/office.ts +1047 -0
- package/src/openai/adapter.ts +14 -1
- package/src/pdf-pages.ts +2 -5
- package/src/peers.ts +29 -0
- package/src/plan.ts +2 -0
- package/src/ranged.ts +4 -2
- package/src/search.ts +738 -146
- package/src/text-index.ts +4 -2
- package/src/text.ts +214 -23
- package/src/tools.ts +192 -12
- package/src/types.ts +3 -3
- package/src/word-styles.ts +420 -0
- package/src/xlsx.ts +478 -0
- package/src/xml-stream.ts +224 -0
package/docs/security.md
CHANGED
|
@@ -215,7 +215,7 @@ What limits the damage:
|
|
|
215
215
|
tag characters, zero-width spaces, the controls that reorder what a person sees
|
|
216
216
|
([Characters that show nothing](#characters-that-show-nothing)).
|
|
217
217
|
- **Text of hidden runs in Word, and slides the slide show skips, are marked** as such; text hidden
|
|
218
|
-
in other ways is not ([Hidden text in Word and
|
|
218
|
+
in other ways is not ([Hidden text in Word, PowerPoint and HTML](#hidden-text-in-word-powerpoint-and-html)).
|
|
219
219
|
- **A file name cannot add lines to the instruction.** At ingest it is reduced to one printable line
|
|
220
220
|
without most characters that show nothing (see [Uploads](#uploads)), and the system instruction
|
|
221
221
|
and the frame quote it as a JSON string. It is still text the model reads, like the file's
|
|
@@ -235,7 +235,7 @@ What a hostile file can still do:
|
|
|
235
235
|
- **Hide text from a person, but not from the model**, in what the cleaning and the marks leave:
|
|
236
236
|
characters that scripts and emoji need, Word text hidden by a style, a text file in an older
|
|
237
237
|
encoding that an editor shows as � ([Characters that show nothing](#characters-that-show-nothing),
|
|
238
|
-
[Hidden text in Word and
|
|
238
|
+
[Hidden text in Word, PowerPoint and HTML](#hidden-text-in-word-powerpoint-and-html),
|
|
239
239
|
[Text files that were not UTF-8](#text-files-that-were-not-utf-8)).
|
|
240
240
|
- **Reach every model of the question**, an explorer model included.
|
|
241
241
|
- **Stay in the conversation.** The `toolTurns` you store hold the text the tools returned,
|
|
@@ -326,10 +326,10 @@ into fewer pieces than 0.5.0 split it, which moves the lines after it. Documents
|
|
|
326
326
|
earlier version keep their text until they expire. Steps and activity lines drop the same
|
|
327
327
|
characters ([What the library logs](#what-the-library-logs)).
|
|
328
328
|
|
|
329
|
-
### Hidden text in Word and
|
|
329
|
+
### Hidden text in Word, PowerPoint and HTML
|
|
330
330
|
|
|
331
|
-
A DOCX can hold text that Word does not show,
|
|
332
|
-
text is kept, and marked:
|
|
331
|
+
A DOCX can hold text that Word does not show, a PPTX slides that the slide show skips, and an HTML
|
|
332
|
+
page text that a browser does not show. Their text is kept, and marked:
|
|
333
333
|
|
|
334
334
|
- **Text of a hidden run** (`<w:vanish/>` in the run's own properties) comes after the text of its
|
|
335
335
|
paragraph that shows, and each of its lines starts with
|
|
@@ -339,6 +339,12 @@ text is kept, and marked:
|
|
|
339
339
|
as the reading of a ruby, is marked too, and so is the rest of the hidden run after it.
|
|
340
340
|
- **A hidden slide** (`show="0"`) has the heading
|
|
341
341
|
`## Slide 2 (hidden, not shown in the slide show)`. Its text is kept.
|
|
342
|
+
- **Text an HTML page hides** (an element with the `hidden` attribute, or an inline style of
|
|
343
|
+
`display: none` or `visibility: hidden` or `collapse`) is marked the same way, line by line and
|
|
344
|
+
in pieces. Hidden text inside a line that shows comes after that line, not where it stood. A row
|
|
345
|
+
of a table that the page hides is written marked, in its place. The hiding ends where the element
|
|
346
|
+
does; an element the page never closes hides the rest of its parent. A `hidden` attribute or a
|
|
347
|
+
style is read in the first 4,096 characters of its value.
|
|
342
348
|
|
|
343
349
|
The attributes of `w:vanish` and of a slide's `p:sld` are read however long the tag is, and by
|
|
344
350
|
their exact names, as XML reads them: `W:VAL="0"` is another attribute, and the run stays hidden.
|
|
@@ -347,15 +353,20 @@ The mark is a hint, not a filter. Text without it may still be hidden, and a doc
|
|
|
347
353
|
can write the mark. Not told apart, and read as text that shows:
|
|
348
354
|
|
|
349
355
|
- text hidden through a character or paragraph style, and white or tiny text;
|
|
356
|
+
- in HTML, text hidden by a style sheet, a class, a script, `opacity`, a position off the page or
|
|
357
|
+
a `<template>` (dropped, like scripts), and an `aria-hidden` element, which a browser shows;
|
|
350
358
|
- hidden shapes on a slide that shows;
|
|
351
359
|
- the content of `mc:Choice`, which the library reads where Word may show `mc:Fallback` instead
|
|
352
360
|
(the Fallback is not read);
|
|
353
361
|
- tags written with a namespace prefix other than `w:` or `p:`, and a text box inside a hidden run;
|
|
354
362
|
- a `show` value written as a character reference (`show="0"`); the same in a `w:val` marks
|
|
355
363
|
text that shows as hidden;
|
|
356
|
-
- parts that no relationship references, such as an orphan header
|
|
364
|
+
- parts that no relationship references, such as an orphan header (a slide that
|
|
365
|
+
`ppt/presentation.xml` does not list is not read, unless the deck lists none).
|
|
357
366
|
|
|
358
|
-
Speaker notes are
|
|
367
|
+
Speaker notes are read since 0.8.0, after a line `[Speaker notes]` under their slide, and so are
|
|
368
|
+
the comments of a DOCX, under `# Comments`: they are text a reader of the slide show or the page
|
|
369
|
+
does not see either. The marks count toward `maxTextBytes` and the limit of 32 million
|
|
359
370
|
characters.
|
|
360
371
|
|
|
361
372
|
### Text files that were not UTF-8
|
|
@@ -476,18 +487,20 @@ What bounds them:
|
|
|
476
487
|
stream inside it that declares more. `@cantoo/pdf-lib` makes an entry for each object such a
|
|
477
488
|
stream declares, whatever it holds: in a file of 8.7 KB, a million of them took 0.7 s, and four
|
|
478
489
|
million ran a heap of 512 MB out of memory.
|
|
479
|
-
- **Memory kept.** `@cantoo/pdf-lib` keeps every name and every reference it reads, for the life
|
|
480
|
-
|
|
490
|
+
- **Memory kept.** `@cantoo/pdf-lib` keeps every name and every reference it reads, for the life of
|
|
491
|
+
the process: in the server that ingests, and in the one that answers `view_pages`. After the
|
|
481
492
|
document was dropped, 2 MB of different names still took 37 MB of heap, and the million entries
|
|
482
493
|
above about 140 MB. A file read again adds nothing, and real PDFs add little: names such as
|
|
483
494
|
`/Type`, and references to the same object numbers, are kept once for all files. But each file
|
|
484
|
-
with other names or other object numbers adds its own: files of 9 KB that each declared a
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
495
|
+
with other names or other object numbers adds its own: files of 9 KB that each declared a million
|
|
496
|
+
new objects added about 140 MB each, and the fourth ingest ran a heap of 512 MB out of memory. On
|
|
497
|
+
the server that answers `view_pages`, the seventh look at such files ran a heap of 1 GB out of
|
|
498
|
+
memory, and that memory comes back only when the server restarts. A process of its own for each
|
|
499
|
+
ingest, as [Deployment](./guides/deployment.md#uploads-and-ingest) shows, gives it all back when
|
|
500
|
+
it ends, and so do the child processes of
|
|
501
|
+
[`createIsolatedExtractors`](./reference/node.md#createisolatedextractorsoptions), each replaced
|
|
502
|
+
after 50 files and stopped when idle. For the documents of users you do not trust, do not offer
|
|
503
|
+
`view_pages` (`viewPages: false`), or keep no PDF originals.
|
|
491
504
|
- **Time.** Reading cannot be interrupted once it has started, not even by the question's `signal`.
|
|
492
505
|
It pauses so that other requests get a turn after every 12 ms of work, or up to 40 ms where a
|
|
493
506
|
pause itself takes long, as a timer does on Windows. (Until 0.6.0 it paused every 4,000 objects,
|
|
@@ -511,7 +524,8 @@ To turn them off: without `keepOriginals` for PDFs, `view_pages` is never offere
|
|
|
511
524
|
(`viewPages: false` turns it off for one question). Picture marks and the page check are turned off
|
|
512
525
|
in the PDF extractor, or by not installing `@cantoo/pdf-lib`; a `view_pages` call then fails too,
|
|
513
526
|
so keep no PDF originals either. Without the page check, a page that is slow to read holds the
|
|
514
|
-
server for as long as pdf.js takes ([Uploads](#uploads)):
|
|
527
|
+
server for as long as pdf.js takes ([Uploads](#uploads)): read PDFs in child processes with
|
|
528
|
+
`createIsolatedExtractors`, whose kill timer stops such a page, or ingest in a process of its own.
|
|
515
529
|
|
|
516
530
|
```ts
|
|
517
531
|
import { createLargeFiles, createPdfExtractor, FileDocumentStore } from "llm-large-files";
|
|
@@ -525,18 +539,29 @@ const files = createLargeFiles({
|
|
|
525
539
|
```
|
|
526
540
|
|
|
527
541
|
For uploads you do not trust, parse them in a process of their own, with a memory limit and a
|
|
528
|
-
timeout
|
|
529
|
-
(
|
|
530
|
-
|
|
531
|
-
|
|
542
|
+
timeout. Since 0.8.0 the library does it: the `extractors` of
|
|
543
|
+
[`createIsolatedExtractors`](./reference/node.md#createisolatedextractorsoptions), from
|
|
544
|
+
`llm-large-files/node`, read PDF, DOCX, PPTX and XLSX files in child processes, each with a heap
|
|
545
|
+
flag (`maxHeapMb`, 512 by default: a heap limit of 560 to 704 MB, depending on the version of Node,
|
|
546
|
+
[The heap limit](./reference/node.md#createisolatedextractorsoptions)) and a kill timer
|
|
547
|
+
(`timeoutMs`, 120 s). A file that runs its child out of memory is refused with the code
|
|
548
|
+
`"out_of_memory"`, one that takes too long with `"timeout"`, and your server goes on
|
|
549
|
+
([Uploads](#uploads)). To run the whole ingest apart, an
|
|
550
|
+
extractor of your own included, fork a process for it
|
|
551
|
+
([Deployment](./guides/deployment.md#uploads-and-ingest)). If you keep their PDFs for
|
|
552
|
+
`view_pages`, run the questions in a process of their own too (the tool loop of each question, in a
|
|
553
|
+
process you fork), or do not offer `view_pages` on their documents (`viewPages: false`): a look
|
|
554
|
+
parses the file again, in the server that answers. The limits of the library bound
|
|
532
555
|
what a file can make it keep, but not the memory a parser takes for its own work, and in your
|
|
533
556
|
server that memory is the server's heap. Choose the isolation with care:
|
|
534
557
|
|
|
535
558
|
- **A child process contains running out of memory.** A process started with `child_process.fork`,
|
|
536
|
-
or a service of its own, with its own
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
559
|
+
as `createIsolatedExtractors` starts its children, or a service of its own, with its own
|
|
560
|
+
`--max-old-space-size`, stops alone, and your server goes on. `createIsolatedExtractors` does not
|
|
561
|
+
pass the server's `NODE_OPTIONS` to its children, so a heap flag there does not replace theirs.
|
|
562
|
+
That flag bounds the heap only. What a parser unpacks is held outside it: a PDF of 1.6 KB took 1.6
|
|
563
|
+
GB of memory at ingest with 20 MB of heap ([Objects of a PDF](#uploads)). Give that process a
|
|
564
|
+
memory limit of its own too, such as a container or a cgroup of its own.
|
|
540
565
|
- **A worker thread does not reliably contain it.** In the 0.6 review, on the code before this
|
|
541
566
|
release ([Measurements](./measurements.md#how-to-read-this-page)), a worker given
|
|
542
567
|
`resourceLimits: { maxOldGenerationSizeMb: 512 }` ran out of memory on a hostile DOCX and took
|
|
@@ -560,29 +585,32 @@ server that memory is the server's heap. Choose the isolation with care:
|
|
|
560
585
|
- **Memory.** The handlers buffer the whole upload, up to `maxUploadBytes`, before reading its
|
|
561
586
|
text, and an ingest then holds several copies of the text at once (several times the size of the
|
|
562
587
|
text), in your server's heap. Cap concurrent uploads in front (a reverse proxy, a rate limiter),
|
|
563
|
-
or
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
588
|
+
or read PDF and Office files with `createIsolatedExtractors`, whose `concurrency` (2) and
|
|
589
|
+
`maxQueue` (8) bound how many are read and wait, or ingest in a process of its own:
|
|
590
|
+
[Deployment](./guides/deployment.md) has measurements and recipes.
|
|
591
|
+
- **Text.** A small file can hold far more text than its size: a PDF of 3 KB whose page tree listed
|
|
592
|
+
one dense page 150 times drew 107 MB of text in 0.5.0. `maxTextBytes` (100 MB by default, or
|
|
593
|
+
`maxUploadBytes` when that is larger) bounds the text kept from one file, in UTF-8 bytes. The PDF
|
|
594
|
+
extractor counts it page by page, and DOCX, PPTX and XLSX as they are unpacked; each refuses the
|
|
595
|
+
file as soon as it passes. The text of any other extractor is checked when it returns. A PDF whose
|
|
596
|
+
page tree lists one page more than once is refused before its text grows: "This PDF lists the same
|
|
597
|
+
page more than once. A file built that way can hold far more text than its size, so it is not
|
|
598
|
+
read. Print it to a new PDF, and attach that." A PDF whose pages paint forms inside forms draws
|
|
599
|
+
the text of the innermost one each time it is painted: past the first 16 MB, a PDF that draws more
|
|
600
|
+
than 100 times its size in text is refused as it is read ("This PDF draws more than 16 MB of text,
|
|
601
|
+
over 100 times its size. …"), whatever `maxTextBytes` allows.
|
|
576
602
|
- **Pages.** A PDF of more than `maxPdfPages` pages (5,000 by default) is refused before any page
|
|
577
603
|
is read.
|
|
578
604
|
- **Time.** One ingest may take `ingestTimeoutMs` (2 minutes by default) to extract, index and
|
|
579
605
|
profile the text. Past it, `ingest` throws "report.pdf took too long to read (more than 2 min).
|
|
580
606
|
…", and nothing is stored. `IngestInput.signal` stops it sooner, when your route knows that the
|
|
581
607
|
uploader left; the upload handler of `createDocumentHandlers` aborts it when the client closes
|
|
582
|
-
the connection. The PDF, DOCX and
|
|
583
|
-
limit, `ingest` stops waiting, but a step already under way (one page of pdf.js, JSZip reading
|
|
608
|
+
the connection. The PDF, DOCX, PPTX and XLSX extractors stop between two pages or two pieces. At
|
|
609
|
+
the limit, `ingest` stops waiting, but a step already under way (one page of pdf.js, JSZip reading
|
|
584
610
|
the directory of a ZIP, a parse of `@cantoo/pdf-lib`, an extractor of your own that does not
|
|
585
|
-
look at `ExtractInput.signal`) runs on to its end in the background.
|
|
611
|
+
look at `ExtractInput.signal`) runs on to its end in the background. With
|
|
612
|
+
`createIsolatedExtractors`, the child process that reads the file is killed then, step and all,
|
|
613
|
+
and also at its own `timeoutMs`.
|
|
586
614
|
- **Work of a PDF page.** pdf.js reads the text of a page in one block that neither the time limit
|
|
587
615
|
nor a signal cuts short, and a page can be built to make it run far more than its file holds: a
|
|
588
616
|
3 KB page that paints a form that paints another, 50 times at each of three levels, held the
|
|
@@ -617,9 +645,10 @@ server that memory is the server's heap. Choose the isolation with care:
|
|
|
617
645
|
check, in under 0.07 s. A check of each object before it is built is not in 0.6.0:
|
|
618
646
|
pdf.js reads an object where the cross-reference table points, also inside what reads as the
|
|
619
647
|
data of a stream, and the object streams of an encrypted file cannot be unpacked before they are
|
|
620
|
-
decrypted, so such a check would be easy to get around.
|
|
621
|
-
|
|
622
|
-
container of its own
|
|
648
|
+
decrypted, so such a check would be easy to get around. Read files you do not trust with
|
|
649
|
+
`createIsolatedExtractors`, whose children have a heap limit, and for what a stream unpacks
|
|
650
|
+
outside the heap, ingest in a container of its own with a memory limit of its own
|
|
651
|
+
([Deployment](./guides/deployment.md#uploads-and-ingest)). A look at their
|
|
623
652
|
pages parses the file again on the server that answers the question: do not offer `view_pages`
|
|
624
653
|
on their documents (`viewPages: false`), or keep no PDF originals
|
|
625
654
|
([Reading PDFs for pages and pictures](#reading-pdfs-for-pages-and-pictures)).
|
|
@@ -645,29 +674,32 @@ server that memory is the server's heap. Choose the isolation with care:
|
|
|
645
674
|
32 MB ([Objects of a PDF](#uploads)).
|
|
646
675
|
- A stream that pdf.js unpacks to reach an object: 6.1 to 6.4 s for an object stream that
|
|
647
676
|
unpacks to 512 MB, in a file of 1.6 KB. `@cantoo/pdf-lib` took 1.0 s for it at a look.
|
|
648
|
-
- A piece of a DOCX or
|
|
649
|
-
time while JSZip unpacks it.
|
|
677
|
+
- A piece of a DOCX, PPTX or XLSX packed hundreds of times tighter than documents are, about
|
|
678
|
+
0.2 s at a time while JSZip unpacks it.
|
|
650
679
|
|
|
651
|
-
|
|
652
|
-
([Deployment](./guides/deployment.md#uploads-and-ingest)): only that process waits, and it is
|
|
680
|
+
Read files you do not trust with `createIsolatedExtractors`, or ingest them in a process of their
|
|
681
|
+
own ([Deployment](./guides/deployment.md#uploads-and-ingest)): only that process waits, and it is
|
|
653
682
|
stopped at its time limit.
|
|
654
|
-
- **Parsers.** PDF text is read with `unpdf` (pdf.js), pages, pictures and the page check with
|
|
655
|
-
and DOCX and
|
|
656
|
-
untrusted input in your process
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
683
|
+
- **Parsers.** PDF text is read with `unpdf` (pdf.js), pages, pictures and the page check with
|
|
684
|
+
`@cantoo/pdf-lib`, and DOCX, PPTX and XLSX are unpacked with `jszip`. They are optional
|
|
685
|
+
dependencies, and they run on untrusted input in your process, or in the child processes of
|
|
686
|
+
`createIsolatedExtractors`. Keep them updated, and for high-risk deployments move extraction out
|
|
687
|
+
of the server: into those child processes, a container of its own or a sandbox ([Reading PDFs for
|
|
688
|
+
pages and pictures](#reading-pdfs-for-pages-and-pictures) says why a worker thread is not enough).
|
|
689
|
+
- **Zip bombs.** A DOCX, PPTX or XLSX is unpacked a piece at a time, and refused as it grows past
|
|
690
|
+
100 times its size (after the first 16 MB) or past 128 MB of XML: "This DOCX unpacks to more than
|
|
691
|
+
16 MB, over 100 times its size. A file that grows that much is not read: it may be a zip bomb.
|
|
692
|
+
Save it as text or PDF, and attach that." Only the parts that hold text are unpacked (in a DOCX
|
|
693
|
+
the document, its footnotes, endnotes, comments, headers and footers, and its styles and
|
|
694
|
+
numbering; in a PPTX the slides and their speaker notes; in an XLSX the workbook, its styles, its
|
|
695
|
+
shared strings and its sheets). Pictures and fonts stay packed. What is kept in memory is bounded
|
|
696
|
+
by the text, not by the XML: each paragraph becomes one string when it closes, and an empty one is
|
|
697
|
+
dropped. A file is refused with more than 32 million characters of text (or `maxTextBytes`, when
|
|
698
|
+
that is smaller), more than 1,000,000 paragraphs with text, paragraphs nested more than 32 deep,
|
|
699
|
+
or more than 20,000 parts in its ZIP directory. In 0.5.0, a DOCX of 1.5 MB that held 120 MB of
|
|
700
|
+
empty paragraphs ran a heap of 512 MB out of memory and stopped the server; it is now refused with
|
|
701
|
+
the process at 115 MB.
|
|
702
|
+
- **The ZIP directory of a DOCX, PPTX or XLSX** is read before JSZip reads it, the way JSZip 3.10 will
|
|
671
703
|
read it: from where JSZip finds it (the last end record of the file, and for a ZIP64 file the
|
|
672
704
|
last locator and the record it points to), record by record, each by its own lengths. JSZip reads
|
|
673
705
|
every record of the directory into memory before it unpacks any part, and the count of parts that
|
|
@@ -700,7 +732,16 @@ server that memory is the server's heap. Choose the isolation with care:
|
|
|
700
732
|
that is not at the end (0.2 s for 100 MB built of near signatures), before JSZip searches the
|
|
701
733
|
file again ([Measurements](./measurements.md#the-server)).
|
|
702
734
|
- **Binary files are refused**, with a message for the user: "This file looks binary, not text…".
|
|
703
|
-
Compressed ones (`.gz`, `.zip`) get a message that says to decompress first
|
|
735
|
+
Compressed ones (`.gz`, `.zip`) get a message that says to decompress first, and an Office file
|
|
736
|
+
of before 2007, an Outlook message or an Office file with a password one that says to save it
|
|
737
|
+
again (since 0.8.0).
|
|
738
|
+
- **Refusals carry a code.** Since 0.8.0 `ingest` throws every refusal as an
|
|
739
|
+
[`IngestError`](./reference/library.md#ingesterror), and `ingest.error` events carry its `code`.
|
|
740
|
+
The files built the way files that exhaust a server are, which the checks above recognize (a zip
|
|
741
|
+
bomb, a ZIP directory or a tag no Office program writes, a PDF that lists a page twice, draws far
|
|
742
|
+
more text than its size or has a page that would hold the server), have the code `"hostile"`:
|
|
743
|
+
count or log them apart, by user. `"out_of_memory"` and `"timeout"` from the child processes of
|
|
744
|
+
`createIsolatedExtractors` can be such files too.
|
|
704
745
|
- **Scanned PDFs** (no text at all) are refused, unless PDF originals are kept (`keepOriginals`
|
|
705
746
|
includes `"pdf"` and the store has `putOriginal`) and the file has at most `scannedPdfPages` pages
|
|
706
747
|
(30 by default): their pages can then be shown.
|
|
@@ -749,8 +790,11 @@ The model writes the regex, and the model reads untrusted file text, so a file c
|
|
|
749
790
|
pattern. These bound the cost:
|
|
750
791
|
|
|
751
792
|
- **Refused up front:** a pattern longer than 300 characters, one that is not valid, and one with a
|
|
752
|
-
nested quantifier such as `(a+)+`. The model reads why.
|
|
753
|
-
|
|
793
|
+
nested quantifier such as `(a+)+`. The model reads why. Since 0.8.0 a repeated group whose
|
|
794
|
+
repeats end at a literal character that none of its quantified parts can match, as in
|
|
795
|
+
`(\d{1,3}\.){3}\d{1,3}` or `(\w+\.)+com`, passes: each repeat ends at the next such character,
|
|
796
|
+
so it takes time in proportion to the line. The nested-quantifier check is a quick filter, not a
|
|
797
|
+
guarantee: `(a|a)+$` and `((a+))+$` pass it.
|
|
754
798
|
- **3 seconds for one search.** Node's `vm` timeout interrupts a match that is still backtracking.
|
|
755
799
|
The search returns what it found, with the note "The scan stopped early; the total is a lower
|
|
756
800
|
bound, and the last match in the file is not known."
|
|
@@ -918,7 +962,8 @@ What you log is your choice, and some of what the library hands you holds user d
|
|
|
918
962
|
- Keep the limits on one file (`maxTextBytes`, `maxPdfPages`, `ingestTimeoutMs`) as low as your
|
|
919
963
|
users allow ([Uploads](#uploads)).
|
|
920
964
|
- For untrusted uploads, parse them in a process of their own with a memory limit, not in a worker
|
|
921
|
-
thread
|
|
965
|
+
thread: pass the `extractors` of `createIsolatedExtractors` to `createLargeFiles`
|
|
966
|
+
([Reading PDFs for pages and pictures](#reading-pdfs-for-pages-and-pictures)), and keep
|
|
922
967
|
`unpdf`, `@cantoo/pdf-lib` and `jszip` updated.
|
|
923
968
|
- Render answers as untrusted text, and steps and activity lines as plain text
|
|
924
969
|
([What the library logs](#what-the-library-logs)).
|
package/docs/troubleshooting.md
CHANGED
|
@@ -107,7 +107,12 @@ See [Getting started](./getting-started.md).
|
|
|
107
107
|
`files.ingest` throws one of these, and the upload handler answers `422` with it:
|
|
108
108
|
|
|
109
109
|
- `Reading PDF files needs the optional dependency "unpdf".`
|
|
110
|
-
- `Reading DOCX and
|
|
110
|
+
- `Reading DOCX, PPTX and XLSX files needs the optional dependency "jszip".` (until 0.8.0:
|
|
111
|
+
`Reading DOCX and PPTX files …`)
|
|
112
|
+
|
|
113
|
+
A package that is installed but fails to load, such as a version this Node cannot run, says so
|
|
114
|
+
since 0.8.0: `The optional dependency "unpdf" is installed but could not be loaded (…). Reinstall
|
|
115
|
+
it, or install a version this Node can run.` Its code is `"missing_dependency"` all the same.
|
|
111
116
|
|
|
112
117
|
A look at the pages of a PDF fails in `steps`, while uploads work:
|
|
113
118
|
`Look at report.pdf pages 12-14 — failed: report.pdf: Showing PDF pages needs the optional dependency "@cantoo/pdf-lib".`
|
|
@@ -306,13 +311,16 @@ See [`DocumentHandlerOptions`](./reference/node.md#documenthandleroptions) and
|
|
|
306
311
|
|
|
307
312
|
### `422` for a file the library cannot read
|
|
308
313
|
|
|
309
|
-
`files.ingest` throws an
|
|
310
|
-
|
|
314
|
+
`files.ingest` throws an [`IngestError`](./reference/library.md#ingesterror) written for your
|
|
315
|
+
user, with a `code` for your app (`"not_text"`, `"compressed"`, `"no_text"`…), and the upload
|
|
316
|
+
handler answers `422` with its message. The common ones:
|
|
311
317
|
|
|
312
318
|
| Message | What the file is |
|
|
313
319
|
|---|---|
|
|
314
|
-
| `This file looks binary, not text. Attach a text, log, PDF,
|
|
315
|
-
| `This file
|
|
320
|
+
| `This file looks binary, not text. Attach a text, log, PDF, Word, PowerPoint, Excel or HTML file.` | An image, an audio file, any other binary file. Until 0.8.0 the message ended `Attach a text, log, PDF, DOCX, or PPTX file.` |
|
|
321
|
+
| `This is a Word, Excel or PowerPoint file of before 2007 (.doc, .xls, .ppt), an Outlook message (.msg), or an Office file protected by a password and stored that way. None of these can be read: save it as .docx, .xlsx, .pptx, PDF or text, without a password, and attach that.` | A legacy `.doc`, `.xls` or `.ppt`, a `.msg`, or a DOCX, PPTX or XLSX saved with a password. New in 0.8.0: the first were "binary", the last failed as damaged files. |
|
|
322
|
+
| `This PDF is protected by a password, so it cannot be read. Open it, print it to a new PDF without the password, and attach that.` | A PDF with a password. New in 0.8.0: pdf.js said "No password given". |
|
|
323
|
+
| `This file is a ZIP archive. Extract it, then attach the text file you need.` | A ZIP, or a format that is a ZIP inside and is not DOCX, PPTX or XLSX: ODT, ODS. Until 0.8.0 an XLSX got it too. |
|
|
316
324
|
| `This file is gzip-compressed. Decompress it, then attach the text file inside.` | A compressed log, such as `app.log.gz`. |
|
|
317
325
|
| `report.txt is empty.` | A file of 0 bytes. |
|
|
318
326
|
| `report.docx did not contain any text.` | Nothing left once read: a DOCX of pictures, a file of blank lines. |
|
|
@@ -334,8 +342,10 @@ The common ones:
|
|
|
334
342
|
**Fix.** Most messages say what the user can do. For a scanned PDF, keep PDFs
|
|
335
343
|
(`keepOriginals: ["pdf"]`): a scan of up to `scannedPdfPages` (30) pages is then accepted, and the
|
|
336
344
|
model reads it by looking at its pages. A longer scan needs OCR. To accept a format the library
|
|
337
|
-
refuses, such as
|
|
338
|
-
of your own that reads it: every file of a format the library does not
|
|
345
|
+
refuses, such as an OpenDocument file (ODT, ODS), convert it to text or CSV before `files.ingest`,
|
|
346
|
+
or give a `text` extractor of your own that reads it: every file of a format the library does not
|
|
347
|
+
know goes to that one. To tell users in your own words or language, map `error.code`
|
|
348
|
+
([`IngestError`](./reference/library.md#ingesterror)).
|
|
339
349
|
|
|
340
350
|
For a file past a limit, the user splits it and attaches the part the question is about. If your
|
|
341
351
|
users need larger files, raise `maxPdfPages` or `maxTextBytes` in `createLargeFiles`: the numbers
|
|
@@ -388,11 +398,15 @@ A stream that unpacks to hundreds of MB takes that memory outside the heap, and
|
|
|
388
398
|
printed: a PDF of 1.6 KB was read with 20 MB of heap, while the process took 1.6 GB of memory and
|
|
389
399
|
the server was held for 6 s. A container with less than that left runs out of memory.
|
|
390
400
|
|
|
391
|
-
**Fix.**
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
401
|
+
**Fix.** Read PDFs you do not trust in child processes with a heap limit of their own: pass the
|
|
402
|
+
`extractors` of [`createIsolatedExtractors`](./reference/node.md#createisolatedextractorsoptions) to
|
|
403
|
+
`createLargeFiles`. Such a file then runs out of the child's heap, the upload is answered `422`
|
|
404
|
+
"report.pdf needed more memory than is allowed for reading one file (512 MB). …" (code
|
|
405
|
+
`"out_of_memory"`), and your server goes on. For the memory a stream takes outside the heap, give
|
|
406
|
+
the server, or an ingest process of its own, a memory limit too, such as a container of its own
|
|
407
|
+
([Deployment](./guides/deployment.md#uploads-and-ingest)). For their documents, do not offer
|
|
408
|
+
`view_pages` (`viewPages: false`), or keep no PDF originals. A larger heap only moves the size of
|
|
409
|
+
file that does it.
|
|
396
410
|
|
|
397
411
|
See [Security](./security.md#uploads) and the [measurements](./measurements.md#the-server).
|
|
398
412
|
|
|
@@ -529,8 +543,9 @@ Log the result ([Start here](#start-here-what-to-log)) and look for one of these
|
|
|
529
543
|
`[No text on this page. It may be a scan, a photo, or a chart.]` or
|
|
530
544
|
`[Picture on this page. This text cannot show it.]`. Without `view_pages` the model cannot see it
|
|
531
545
|
(see [below](#the-model-never-looks-at-a-page-or-says-it-cannot-see-it)).
|
|
532
|
-
- **The library does not read that part.**
|
|
533
|
-
|
|
546
|
+
- **The library does not read that part.** The pictures of DOCX and PPTX files are not read, nor
|
|
547
|
+
the charts of a workbook. Comments of a DOCX and speaker notes of a PPTX are read since 0.8.0:
|
|
548
|
+
a document ingested before keeps the text it had until it is uploaded again.
|
|
534
549
|
- **The document was not in the question.** Only the `documentIds` of this question are planned
|
|
535
550
|
and searchable.
|
|
536
551
|
- **The server cut the prompt.** Some local servers shorten a prompt longer than the context they
|
|
@@ -1089,8 +1104,9 @@ See [Errors and reliability](./guides/errors-and-reliability.md) and [Web app](.
|
|
|
1089
1104
|
`readTextRange`, as `FileDocumentStore` does. With a store of your own without it, and with
|
|
1090
1105
|
`MemoryDocumentStore`, every document is loaded whole.
|
|
1091
1106
|
- An upload is held in memory whole while it is read and extracted, with several copies of its
|
|
1092
|
-
text, and the parsers of PDF, DOCX and
|
|
1093
|
-
same heap as everything else
|
|
1107
|
+
text, and the parsers of PDF, DOCX, PPTX and XLSX take memory of their own while they read, in
|
|
1108
|
+
the same heap as everything else, unless they run in the child processes of
|
|
1109
|
+
`createIsolatedExtractors`.
|
|
1094
1110
|
- `@cantoo/pdf-lib`, which reads PDFs for the page check, the picture marks and `view_pages`,
|
|
1095
1111
|
keeps every name and every reference it has read for the life of the process. Real PDFs add
|
|
1096
1112
|
little, as they share most of them, but each crafted one with other names or object numbers adds
|
|
@@ -1102,9 +1118,10 @@ See [Errors and reliability](./guides/errors-and-reliability.md) and [Web app](.
|
|
|
1102
1118
|
**Fix.** Give a store of your own `readTextRange` (and `putIndex` and `getIndex`, so that no pass
|
|
1103
1119
|
over the text is needed first). Lower `cacheBytes` and `maxConcurrentScans` to fit the memory of
|
|
1104
1120
|
the server, and `maxUploadBytes` and `maxTextBytes` to the files you expect. To keep a file that
|
|
1105
|
-
needs too much memory from stopping the server,
|
|
1106
|
-
limit: a worker thread does not keep it apart.
|
|
1107
|
-
`@cantoo/pdf-lib` kept
|
|
1121
|
+
needs too much memory from stopping the server, read it in a process of its own with its own heap
|
|
1122
|
+
limit, as `createIsolatedExtractors` does: a worker thread does not keep it apart. Its children,
|
|
1123
|
+
replaced after 50 files, also give back what `@cantoo/pdf-lib` kept, and so does a process for each
|
|
1124
|
+
ingest when it ends. For the documents of users you do not trust, do not offer
|
|
1108
1125
|
`view_pages` (`viewPages: false`), or keep no PDF originals.
|
|
1109
1126
|
|
|
1110
1127
|
See [Storage](./guides/storage.md) and [Deployment](./guides/deployment.md#uploads-and-ingest).
|
|
@@ -1384,7 +1401,10 @@ The question then throws the signal's reason, at once: since 0.6.0 also with an
|
|
|
1384
1401
|
that does not stop its request, and when the adapter fails with its own error as it stops. Nothing
|
|
1385
1402
|
is sent after that, and text that arrives later is not shown. What it used until then was paid for:
|
|
1386
1403
|
`partialResultOf(error)` gives its `usage` when the reason is an object, so abort with no reason or
|
|
1387
|
-
with an `Error`.
|
|
1404
|
+
with an `Error`. Questions stopped by one shared signal all throw its one reason, and since 0.7.1
|
|
1405
|
+
`partialResultOf` gives none of them a result for it: give each question a controller of its own,
|
|
1406
|
+
or read `partial` from each question's `run.error` event (see
|
|
1407
|
+
[`partialResultOf`](./reference/ask.md#partialresultoferror)). A request still running when the question stopped may be billed by the provider,
|
|
1388
1408
|
and is not counted there: pass the signal on to the request, as the built-in adapters do.
|
|
1389
1409
|
|
|
1390
1410
|
See [Web app](./guides/web-app.md).
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "llm-large-files",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"description": "Let an LLM answer questions about books, logs, and other files larger than its request limit or context window: upload, plan, search and read tools, map-reduce, a look at the pages of a PDF, and one tool loop for any model. Adapters for Gemini and Vertex AI, OpenAI-compatible endpoints, and Claude.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"llm",
|
|
@@ -59,6 +59,7 @@
|
|
|
59
59
|
"types": "./dist/node/index.d.ts",
|
|
60
60
|
"default": "./dist/node/index.js"
|
|
61
61
|
},
|
|
62
|
+
"./node/ingest-child.js": "./dist/node/ingest-child.js",
|
|
62
63
|
"./package.json": "./package.json"
|
|
63
64
|
},
|
|
64
65
|
"typesVersions": {
|
|
@@ -113,6 +114,9 @@
|
|
|
113
114
|
"jszip": "3.10.1",
|
|
114
115
|
"typescript": "5.9.3",
|
|
115
116
|
"unpdf": "1.4.0",
|
|
116
|
-
"vitest": "
|
|
117
|
+
"vitest": "4.1.12"
|
|
118
|
+
},
|
|
119
|
+
"overrides": {
|
|
120
|
+
"vite": "7.3.7"
|
|
117
121
|
}
|
|
118
122
|
}
|