llm-large-files 0.7.0 → 0.8.1

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.
Files changed (196) hide show
  1. package/CHANGELOG.md +370 -1
  2. package/README.md +12 -8
  3. package/dist/aggregate.d.ts +103 -0
  4. package/dist/aggregate.d.ts.map +1 -0
  5. package/dist/aggregate.js +651 -0
  6. package/dist/aggregate.js.map +1 -0
  7. package/dist/analyze.d.ts +3 -1
  8. package/dist/analyze.d.ts.map +1 -1
  9. package/dist/analyze.js +109 -14
  10. package/dist/analyze.js.map +1 -1
  11. package/dist/anthropic/adapter.d.ts.map +1 -1
  12. package/dist/anthropic/adapter.js +4 -1
  13. package/dist/anthropic/adapter.js.map +1 -1
  14. package/dist/ask.d.ts +3 -3
  15. package/dist/ask.d.ts.map +1 -1
  16. package/dist/ask.js +72 -17
  17. package/dist/ask.js.map +1 -1
  18. package/dist/decode.d.ts +19 -0
  19. package/dist/decode.d.ts.map +1 -0
  20. package/dist/decode.js +97 -0
  21. package/dist/decode.js.map +1 -0
  22. package/dist/errors.d.ts +55 -0
  23. package/dist/errors.d.ts.map +1 -1
  24. package/dist/errors.js +65 -0
  25. package/dist/errors.js.map +1 -1
  26. package/dist/events.d.ts +6 -0
  27. package/dist/events.d.ts.map +1 -1
  28. package/dist/events.js +5 -1
  29. package/dist/events.js.map +1 -1
  30. package/dist/evidence.d.ts.map +1 -1
  31. package/dist/evidence.js +23 -0
  32. package/dist/evidence.js.map +1 -1
  33. package/dist/extract.d.ts +12 -18
  34. package/dist/extract.d.ts.map +1 -1
  35. package/dist/extract.js +82 -746
  36. package/dist/extract.js.map +1 -1
  37. package/dist/gemini/adapter.d.ts.map +1 -1
  38. package/dist/gemini/adapter.js +9 -5
  39. package/dist/gemini/adapter.js.map +1 -1
  40. package/dist/gemini/endpoints.d.ts +5 -1
  41. package/dist/gemini/endpoints.d.ts.map +1 -1
  42. package/dist/gemini/endpoints.js +11 -2
  43. package/dist/gemini/endpoints.js.map +1 -1
  44. package/dist/gemini/generate.d.ts.map +1 -1
  45. package/dist/gemini/generate.js +20 -3
  46. package/dist/gemini/generate.js.map +1 -1
  47. package/dist/groups.d.ts +54 -0
  48. package/dist/groups.d.ts.map +1 -0
  49. package/dist/groups.js +136 -0
  50. package/dist/groups.js.map +1 -0
  51. package/dist/html.d.ts +11 -0
  52. package/dist/html.d.ts.map +1 -0
  53. package/dist/html.js +1112 -0
  54. package/dist/html.js.map +1 -0
  55. package/dist/index.d.ts +2 -1
  56. package/dist/index.d.ts.map +1 -1
  57. package/dist/index.js +1 -1
  58. package/dist/index.js.map +1 -1
  59. package/dist/library.d.ts.map +1 -1
  60. package/dist/library.js +57 -14
  61. package/dist/library.js.map +1 -1
  62. package/dist/limiter.d.ts +2 -1
  63. package/dist/limiter.d.ts.map +1 -1
  64. package/dist/limiter.js +3 -2
  65. package/dist/limiter.js.map +1 -1
  66. package/dist/model.d.ts.map +1 -1
  67. package/dist/model.js +8 -1
  68. package/dist/model.js.map +1 -1
  69. package/dist/node/handlers.d.ts.map +1 -1
  70. package/dist/node/handlers.js +69 -15
  71. package/dist/node/handlers.js.map +1 -1
  72. package/dist/node/index.d.ts +2 -0
  73. package/dist/node/index.d.ts.map +1 -1
  74. package/dist/node/index.js +1 -0
  75. package/dist/node/index.js.map +1 -1
  76. package/dist/node/ingest-child.d.ts +38 -0
  77. package/dist/node/ingest-child.d.ts.map +1 -0
  78. package/dist/node/ingest-child.js +42 -0
  79. package/dist/node/ingest-child.js.map +1 -0
  80. package/dist/node/isolated.d.ts +88 -0
  81. package/dist/node/isolated.d.ts.map +1 -0
  82. package/dist/node/isolated.js +381 -0
  83. package/dist/node/isolated.js.map +1 -0
  84. package/dist/office.d.ts +105 -0
  85. package/dist/office.d.ts.map +1 -0
  86. package/dist/office.js +1092 -0
  87. package/dist/office.js.map +1 -0
  88. package/dist/openai/adapter.d.ts.map +1 -1
  89. package/dist/openai/adapter.js +15 -1
  90. package/dist/openai/adapter.js.map +1 -1
  91. package/dist/pdf-pages.d.ts.map +1 -1
  92. package/dist/pdf-pages.js +2 -6
  93. package/dist/pdf-pages.js.map +1 -1
  94. package/dist/peers.d.ts +8 -0
  95. package/dist/peers.d.ts.map +1 -0
  96. package/dist/peers.js +32 -0
  97. package/dist/peers.js.map +1 -0
  98. package/dist/plan.d.ts +3 -1
  99. package/dist/plan.d.ts.map +1 -1
  100. package/dist/plan.js +17 -1
  101. package/dist/plan.js.map +1 -1
  102. package/dist/quality.d.ts +16 -0
  103. package/dist/quality.d.ts.map +1 -0
  104. package/dist/quality.js +157 -0
  105. package/dist/quality.js.map +1 -0
  106. package/dist/ranged.d.ts.map +1 -1
  107. package/dist/ranged.js +4 -2
  108. package/dist/ranged.js.map +1 -1
  109. package/dist/search.d.ts +99 -22
  110. package/dist/search.d.ts.map +1 -1
  111. package/dist/search.js +744 -140
  112. package/dist/search.js.map +1 -1
  113. package/dist/text-index.d.ts +3 -1
  114. package/dist/text-index.d.ts.map +1 -1
  115. package/dist/text-index.js +3 -1
  116. package/dist/text-index.js.map +1 -1
  117. package/dist/text.d.ts +27 -3
  118. package/dist/text.d.ts.map +1 -1
  119. package/dist/text.js +225 -23
  120. package/dist/text.js.map +1 -1
  121. package/dist/tools.d.ts +9 -0
  122. package/dist/tools.d.ts.map +1 -1
  123. package/dist/tools.js +457 -16
  124. package/dist/tools.js.map +1 -1
  125. package/dist/types.d.ts +23 -3
  126. package/dist/types.d.ts.map +1 -1
  127. package/dist/word-styles.d.ts +102 -0
  128. package/dist/word-styles.d.ts.map +1 -0
  129. package/dist/word-styles.js +419 -0
  130. package/dist/word-styles.js.map +1 -0
  131. package/dist/xlsx.d.ts +23 -0
  132. package/dist/xlsx.d.ts.map +1 -0
  133. package/dist/xlsx.js +511 -0
  134. package/dist/xlsx.js.map +1 -0
  135. package/dist/xml-stream.d.ts +55 -0
  136. package/dist/xml-stream.d.ts.map +1 -0
  137. package/dist/xml-stream.js +235 -0
  138. package/dist/xml-stream.js.map +1 -0
  139. package/docs/README.md +8 -8
  140. package/docs/getting-started.md +1 -1
  141. package/docs/guides/costs-and-limits.md +7 -5
  142. package/docs/guides/deployment.md +95 -29
  143. package/docs/guides/errors-and-reliability.md +23 -10
  144. package/docs/guides/pdfs.md +127 -74
  145. package/docs/guides/storage.md +1 -1
  146. package/docs/guides/testing.md +5 -1
  147. package/docs/guides/web-app.md +10 -9
  148. package/docs/how-it-works.md +80 -50
  149. package/docs/reference/ask.md +26 -8
  150. package/docs/reference/errors-and-retries.md +5 -3
  151. package/docs/reference/events.md +14 -6
  152. package/docs/reference/gemini.md +6 -2
  153. package/docs/reference/library.md +221 -79
  154. package/docs/reference/node.md +201 -23
  155. package/docs/reference/openai.md +1 -1
  156. package/docs/reference/tools.md +269 -23
  157. package/docs/security.md +114 -69
  158. package/docs/troubleshooting.md +40 -20
  159. package/package.json +6 -2
  160. package/src/aggregate.ts +674 -0
  161. package/src/analyze.ts +117 -13
  162. package/src/anthropic/adapter.ts +4 -1
  163. package/src/ask.ts +73 -17
  164. package/src/decode.ts +89 -0
  165. package/src/errors.ts +108 -0
  166. package/src/events.ts +10 -1
  167. package/src/evidence.ts +23 -0
  168. package/src/extract.ts +79 -701
  169. package/src/gemini/adapter.ts +9 -5
  170. package/src/gemini/endpoints.ts +12 -2
  171. package/src/gemini/generate.ts +19 -3
  172. package/src/groups.ts +162 -0
  173. package/src/html.ts +1071 -0
  174. package/src/index.ts +2 -1
  175. package/src/library.ts +52 -14
  176. package/src/limiter.ts +6 -1
  177. package/src/model.ts +8 -1
  178. package/src/node/handlers.ts +66 -16
  179. package/src/node/index.ts +2 -0
  180. package/src/node/ingest-child.ts +61 -0
  181. package/src/node/isolated.ts +464 -0
  182. package/src/office.ts +1047 -0
  183. package/src/openai/adapter.ts +14 -1
  184. package/src/pdf-pages.ts +2 -5
  185. package/src/peers.ts +29 -0
  186. package/src/plan.ts +19 -1
  187. package/src/quality.ts +146 -0
  188. package/src/ranged.ts +4 -2
  189. package/src/search.ts +738 -146
  190. package/src/text-index.ts +4 -2
  191. package/src/text.ts +214 -23
  192. package/src/tools.ts +440 -16
  193. package/src/types.ts +20 -3
  194. package/src/word-styles.ts +420 -0
  195. package/src/xlsx.ts +478 -0
  196. 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 PowerPoint](#hidden-text-in-word-and-powerpoint)).
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 PowerPoint](#hidden-text-in-word-and-powerpoint),
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 PowerPoint
329
+ ### Hidden text in Word, PowerPoint and HTML
330
330
 
331
- A DOCX can hold text that Word does not show, and a PPTX slides that the slide show skips. Their
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="&#48;"`); 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 or slide.
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 not read at all. The marks count toward `maxTextBytes` and the limit of 32 million
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
- of the process: in the server that ingests, and in the one that answers `view_pages`. After the
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
- million new objects added about 140 MB each, and the fourth ingest ran a heap of 512 MB out of
486
- memory. On the server that answers `view_pages`, the seventh look at such files ran a heap of
487
- 1 GB out of memory, and that memory comes back only when the server restarts. A process of its
488
- own for each ingest, as [Deployment](./guides/deployment.md#uploads-and-ingest) shows, gives it
489
- all back when it ends. For the documents of users you do not trust, do not offer `view_pages`
490
- (`viewPages: false`), or keep no PDF originals.
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)): ingest in a process of its own.
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: run ingest there. If you keep their PDFs for `view_pages`, run the questions there too
529
- (the tool loop of each question, in a process you fork), or do not offer `view_pages` on their
530
- documents (`viewPages: false`). The parsers of
531
- PDF text and of DOCX and PPTX files then run there as well ([Uploads](#uploads)). The limits of the library bound
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 `--max-old-space-size`, stops alone, and your server goes
537
- on. That flag bounds the heap only. What a parser unpacks is held outside it: a PDF of 1.6 KB
538
- took 1.6 GB of memory at ingest with 20 MB of heap ([Objects of a PDF](#uploads)). Give that
539
- process a memory limit of its own too, such as a container or a cgroup of its own.
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 ingest in a process of its own: [Deployment](./guides/deployment.md) has measurements and
564
- recipes.
565
- - **Text.** A small file can hold far more text than its size: a PDF of 3 KB whose page tree
566
- listed one dense page 150 times drew 107 MB of text in 0.5.0. `maxTextBytes` (100 MB by
567
- default, or `maxUploadBytes` when that is larger) bounds the text kept from one file, in UTF-8 bytes. The PDF extractor
568
- counts it page by page, and DOCX and PPTX as they are unpacked; each refuses the file as soon as
569
- it passes. The text of any other extractor is checked when it returns. A PDF whose page tree
570
- lists one page more than once is refused before its text grows: "This PDF lists the same page
571
- more than once. A file built that way can hold far more text than its size, so it is not read.
572
- Print it to a new PDF, and attach that." A PDF whose pages paint forms inside forms draws the
573
- text of the innermost one each time it is painted: past the first 16 MB, a PDF that draws more
574
- than 100 times its size in text is refused as it is read ("This PDF draws more than 16 MB of
575
- text, over 100 times its size. …"), whatever `maxTextBytes` allows.
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 PPTX extractors stop between two pages or two pieces. At the
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. Ingest files you do not trust in a
621
- process of their own, with a memory limit of its own as well as a heap limit, such as a
622
- container of its own ([Deployment](./guides/deployment.md#uploads-and-ingest)). A look at their
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 PPTX packed hundreds of times tighter than documents are, about 0.2 s at a
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
- Ingest files you do not trust in a process of their own
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 `@cantoo/pdf-lib`,
655
- and DOCX and PPTX are unpacked with `jszip`. They are optional dependencies, and they run on
656
- untrusted input in your process. Keep them updated, and for high-risk deployments move extraction
657
- to a process of its own or a sandbox ([Reading PDFs for pages and pictures](#reading-pdfs-for-pages-and-pictures)
658
- says why a worker thread is not enough).
659
- - **Zip bombs.** A DOCX or PPTX is unpacked a piece at a time, and refused as it grows past 100
660
- times its size (after the first 16 MB) or past 128 MB of XML: "This DOCX unpacks to more than
661
- 16 MB, over 100 times its size. A file that grows that much is not read: it may be a zip bomb. Save
662
- it as text or PDF, and attach that." Only the parts that hold text are unpacked (in a DOCX the
663
- document, its headers, footers, footnotes and endnotes; in a PPTX the slides, not their speaker
664
- notes). Pictures and fonts stay packed. What is kept in memory is bounded by the text, not by the
665
- XML: each paragraph becomes one string when it closes, and an empty one is dropped. A file is
666
- refused with more than 32 million characters of text (or `maxTextBytes`, when that is smaller), more than
667
- 1,000,000 paragraphs with text, paragraphs nested more than 32 deep, or more than 20,000 parts in
668
- its ZIP directory. In 0.5.0, a DOCX of 1.5 MB that held 120 MB of empty paragraphs ran a heap of
669
- 512 MB out of memory and stopped the server; it is now refused with the process at 115 MB.
670
- - **The ZIP directory of a DOCX or PPTX** is read before JSZip reads it, the way JSZip 3.10 will
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. The nested-quantifier check is a quick
753
- filter, not a guarantee: `(a|a)+$` and `((a+))+$` pass it.
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 ([Reading PDFs for pages and pictures](#reading-pdfs-for-pages-and-pictures)), and keep
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)).
@@ -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 PPTX files needs the optional dependency "jszip".`
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 error written for your user, and the upload handler answers `422` with it.
310
- The common ones:
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, DOCX, or PPTX file.` | An image, a legacy `.doc` or `.xls`, any other binary file. |
315
- | `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 or PPTX: XLSX, ODT. |
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 XLSX, convert it to text or CSV before `files.ingest`, or give a `text` extractor
338
- of your own that reads it: every file of a format the library does not know goes to that one.
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.** Ingest PDFs you do not trust in a child process with its own `--max-old-space-size`
392
- ([Deployment](./guides/deployment.md#uploads-and-ingest)) and a memory limit of its own, such as a
393
- container of its own: the file then stops that process, the upload gets an error, and your server
394
- goes on. For their documents, do not offer `view_pages` (`viewPages: false`), or keep no PDF
395
- originals. A larger heap only moves the size of file that does it.
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.** Comments and speaker notes of DOCX and PPTX files are
533
- not read, and neither are their pictures.
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 PPTX take memory of their own while they read, in the
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, ingest in a process of its own with its own heap
1106
- limit: a worker thread does not keep it apart. A process for each ingest also gives back what
1107
- `@cantoo/pdf-lib` kept when it ends. For the documents of users you do not trust, do not offer
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`. A request still running when the question stopped may be billed by the provider,
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.7.0",
3
+ "version": "0.8.1",
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": "3.2.7"
117
+ "vitest": "4.1.12"
118
+ },
119
+ "overrides": {
120
+ "vite": "7.3.7"
117
121
  }
118
122
  }