llm-large-files 0.8.0 → 0.8.2

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 (63) hide show
  1. package/CHANGELOG.md +147 -0
  2. package/README.md +2 -0
  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/evidence.d.ts.map +1 -1
  8. package/dist/evidence.js +17 -0
  9. package/dist/evidence.js.map +1 -1
  10. package/dist/extract.d.ts +8 -1
  11. package/dist/extract.d.ts.map +1 -1
  12. package/dist/extract.js +40 -2
  13. package/dist/extract.js.map +1 -1
  14. package/dist/library.d.ts +7 -0
  15. package/dist/library.d.ts.map +1 -1
  16. package/dist/library.js +112 -18
  17. package/dist/library.js.map +1 -1
  18. package/dist/limiter.d.ts +2 -2
  19. package/dist/limiter.js +2 -2
  20. package/dist/node/handlers.d.ts +15 -0
  21. package/dist/node/handlers.d.ts.map +1 -1
  22. package/dist/node/handlers.js +155 -36
  23. package/dist/node/handlers.js.map +1 -1
  24. package/dist/plan.d.ts +3 -1
  25. package/dist/plan.d.ts.map +1 -1
  26. package/dist/plan.js +17 -1
  27. package/dist/plan.js.map +1 -1
  28. package/dist/quality.d.ts +16 -0
  29. package/dist/quality.d.ts.map +1 -0
  30. package/dist/quality.js +157 -0
  31. package/dist/quality.js.map +1 -0
  32. package/dist/ranged.d.ts +7 -0
  33. package/dist/ranged.d.ts.map +1 -1
  34. package/dist/ranged.js +8 -1
  35. package/dist/ranged.js.map +1 -1
  36. package/dist/tools.d.ts +9 -0
  37. package/dist/tools.d.ts.map +1 -1
  38. package/dist/tools.js +258 -5
  39. package/dist/tools.js.map +1 -1
  40. package/dist/types.d.ts +20 -0
  41. package/dist/types.d.ts.map +1 -1
  42. package/docs/guides/deployment.md +27 -6
  43. package/docs/guides/storage.md +5 -2
  44. package/docs/guides/web-app.md +14 -3
  45. package/docs/how-it-works.md +7 -0
  46. package/docs/measurements.md +20 -0
  47. package/docs/reference/library.md +22 -8
  48. package/docs/reference/node.md +30 -15
  49. package/docs/reference/tools.md +99 -1
  50. package/docs/security.md +15 -6
  51. package/docs/troubleshooting.md +28 -0
  52. package/package.json +1 -1
  53. package/src/aggregate.ts +674 -0
  54. package/src/evidence.ts +17 -0
  55. package/src/extract.ts +37 -2
  56. package/src/library.ts +116 -16
  57. package/src/limiter.ts +2 -2
  58. package/src/node/handlers.ts +159 -32
  59. package/src/plan.ts +17 -1
  60. package/src/quality.ts +146 -0
  61. package/src/ranged.ts +8 -1
  62. package/src/tools.ts +249 -5
  63. package/src/types.ts +17 -0
@@ -41,6 +41,7 @@ the [loop reference](./ask.md).
41
41
  | `search_document` | Finds lines that contain a text or match a regex, with an exact count, the first of them and the last one, the last ones, or a count for each group (hour, code, message shape); or the passages that match keywords best. In all of a file or in a range of its lines. |
42
42
  | `read_document` | Reads a range of lines, numbered, up to 600 a call. |
43
43
  | `get_document_overview` | The outline, the log profile, the first 30 and last 20 lines, and what a whole-file read would take. |
44
+ | `aggregate_table` | Counts, sums, averages, minimums and maximums over the rows of a table (a CSV file, a sheet of a workbook, a `\| cell \|` table), with filters and groups, reading every row. New in 0.8.1. |
44
45
  | `analyze_whole_document` | Reads the whole file in parts with other model calls (map-reduce) and answers a task that needs all of it. |
45
46
  | `view_pages` | Up to 5 pages of a PDF, cut out of the uploaded file, as one small PDF the model looks at. |
46
47
 
@@ -57,6 +58,8 @@ In [askWithFiles(options)](./ask.md#askwithfilesoptions) (and `askGemini`, which
57
58
  - `search_document`, `read_document` and `get_document_overview` are offered when at least one
58
59
  document is too large for the prompt (`plan.searchable`). They reach only those documents: a
59
60
  document whose text is in the prompt is not searched.
61
+ - `aggregate_table` is offered with them when at least one searchable document may hold a table:
62
+ its format is `csv`, `xlsx`, `docx`, `pptx`, `pdf`, `html` or `markdown`. New in 0.8.1.
60
63
  - `analyze_whole_document` is offered with them when all of these hold: the model that explores
61
64
  (the explorer, or else the model) has `generateText`, `wholeDocumentAnalysis` is not `false`, and
62
65
  at least one searchable document is estimated, from its metadata, to need no more than
@@ -76,6 +79,9 @@ Through `files.tools(documents, options)` (a loop of your own, see [`ToolSet`](#
76
79
  - The three text tools are declared unless `documents` is empty and there are PDFs to look at.
77
80
  With no documents and no PDFs they are still declared, and every call answers that no attached
78
81
  file is read with this tool.
82
+ - `aggregate_table` is declared when at least one of `documents` may hold a table (the formats
83
+ above); a call of it is answered for any document, with "This file has no table to aggregate"
84
+ when it has none.
79
85
  - `analyze_whole_document` is declared when `options.generate` is given and at least one of
80
86
  `documents` is estimated within `maxMapChunks` parts (5% leeway). With `generate` and no such
81
87
  document it is not declared, but a call of it is still answered, with the reason. Without
@@ -253,11 +259,13 @@ Results of `exact` and `regex`:
253
259
  | `searched_lines` | string, optional | With `from_line` or `to_line`: the lines searched, as `"1941-3000"`. The counts are of those lines. Keyword results have it too. New in 0.8.0. |
254
260
  | `total_matching_lines` | number | Every matching line in the file (or in `searched_lines`), not only those shown. This is the number to count with. |
255
261
  | `order` | `"last"`, optional | With `order: "last"` ([below](#the-last-matches-order-last)). |
262
+ | `table_headers` | array, optional | When matches are rows of a table: the first row of each such table, as `{ line, text }`, up to 5: line 1 of a CSV file, the first row of each sheet of a workbook a match is in, the first row of each `\| cell \|` table elsewhere (looked for up to 20,000 lines above the match). Absent when no match is a row of a table, or the match is that first row. New in 0.8.1. |
256
263
  | `note` | string, optional | When more lines match than are shown: "N of the M matching lines are shown: the first K and the last (line L). To see the ones in between, search again with from_line F, or count them with group_by." ("the first and the last" when K is 1). Until 0.8.0 it ended "narrow the search." When the scan stopped (1,000,000 lines, 3 seconds of a `whole_word` or `group_by` search, or a regex limit): "The scan stopped early; the total is a lower bound, and the last match in the file is not known." Or a size-limit note (below). Absent when every match is shown. |
257
264
  | `next_from_line` | number, optional | When matches between the first ones and the last one are not shown: the line after the last of the first ones (F above). A search with `from_line` set to it shows the ones after. Absent when every match is shown, when the scan stopped early, with `order: "last"` and with `group_by`. New in 0.8.0. |
258
265
  | `matches` | array | The first `max_results` matching lines, in file order, then the last matching line of the file when it is not among them. A scan that stopped early shows no last line, since it did not reach the end. With `order: "last"`, the last `max_results` matching lines, in file order. |
259
266
  | `matches[].line` | number | The line number. |
260
267
  | `matches[].page` | number, optional | The page of the line, for a PDF whose pages can be shown. |
268
+ | `matches[].section` | string, optional | The heading of the outline above the line, up to 80 characters: "2.1 Rent", "Sheet 2: Sales". Absent for logs, JSON and CSV files, and for a document with no outline. New in 0.8.1. |
261
269
  | `matches[].text` | string | The line. A line longer than 400 characters is cut to 400 around the match, with `…` where it was cut. |
262
270
  | `matches[].before`, `matches[].after` | string[], optional | With `context_lines` above 0: the lines around the match, as `"1202\| text"`, each cut to 400 characters. |
263
271
 
@@ -404,6 +412,92 @@ Results of `keywords`:
404
412
  | `passages[].text` | string | The passage's lines, numbered (`"412\| text"`), one a line. |
405
413
  | `note` | string, optional | The no-passage note, or the size-limit note. |
406
414
 
415
+ ## `aggregate_table`
416
+
417
+ New in 0.8.1. Counts, sums, averages, minimums and maximums over the rows of one table of a
418
+ document, with filters and groups, exactly: it reads every row once, and runs no code. A model that
419
+ adds up thousands of rows by reading them needs hundreds of calls and gets it wrong; this answers
420
+ in one. Its description, as the model sees it:
421
+
422
+ > Count, sum, average, or find the minimum or maximum over the rows of a table in an attached file:
423
+ > a CSV or TSV file, a sheet of an Excel workbook, or a table written as | cell | rows (in Word,
424
+ > PowerPoint, PDF or HTML files). It can keep only the rows that match filters, and group them by up
425
+ > to 3 columns (a date column by year, month or day). Its numbers are exact, as it reads every row:
426
+ > use it instead of reading rows to add them up or count them. Columns are named as in the table's
427
+ > header row; a letter (C) or a number (3) works too. Each group lists the lines of its first rows,
428
+ > to cite.
429
+
430
+ | Argument | Type | Required | Default | Meaning |
431
+ |---|---|---|---|---|
432
+ | `document_id` | string | yes | | The document. |
433
+ | `table` | string | no | the first | A sheet's name or number in a workbook; a table's number in other files (1 is the first `\| cell \|` table). |
434
+ | `metrics` | string[] | no | `["count"]` | `"count"`, or an operation and a column: `"sum:Revenue"`, `"avg:Price"`, `"min:Date"`, `"max:Amount"`, `"count_distinct:Customer"`. |
435
+ | `group_by` | string[] | no | none | Up to 3 columns. A column of ISO dates may end in `:year`, `:month` or `:day` (`"Date:month"` groups 2026-03-14 under 2026-03). |
436
+ | `filters` | `{ column, op, value }[]` | no | none | Rows kept: all must match. `op` is `=`, `!=`, `>`, `>=`, `<`, `<=`, `contains`, `not_contains`, `starts_with`, `empty` or `not_empty`. Two numbers compare as numbers, ISO dates and other text as text, ignoring case; a cell that is not a number is neither more nor less than a number. |
437
+ | `order_by` | `"metric"` \| `"key"` | no | `"metric"` | The largest value of the first metric first, or the groups by their values, in order (months in time order). |
438
+ | `limit` | integer | no | 50 | Groups shown, 1 to 200. |
439
+ | `header_rows` | integer | no | found | The rows of the header, 0 to 5, below a title row. Leave it out: the header is found (below). |
440
+
441
+ The tables it reads:
442
+
443
+ - **A CSV or TSV file:** the whole file. The separator is found from the first lines (comma,
444
+ semicolon, tab or `|`); quoted fields may hold separators, quotes and line breaks. Its first line
445
+ is the header, or its second when the first holds one value only (a title).
446
+ - **A sheet of a workbook**, as the XLSX extractor writes it (`Row 7: | a | b |`). Its header is
447
+ the rows above its first row with a number, up to 3, joined column by column with " / " ("Q1 /
448
+ January"), a header cell left empty by a merged cell taking the one to its left; a year (1900 to
449
+ 2100) in a header row is a name. A first row with one value over rows of more is a title, left
450
+ out. A first row of numbers means no header: the columns are named by their letters.
451
+ - **A `| cell |` table** of a DOCX, PPTX, PDF, HTML or Markdown file: a run of such lines, numbered
452
+ in the order of the file. Its header is found as for a sheet; a Markdown separator row is
453
+ skipped, and a row the same as the header, found again inside the table (a PDF repeats it on each
454
+ page), is not a row.
455
+
456
+ A column is named as in the header (case and spaces ignored), by its letter (`C`) or number (`3`),
457
+ or by the lower part of a joined header ("הכנסות" for "פברואר / הכנסות") when only one column has
458
+ it. Numbers are read as people write them: `1,234.5`, `-12`, `(1,200)` for a negative, `₪4,750`,
459
+ `$ 12.00`, `12%` (12), `1 234`, `12,5` (a decimal comma), `1.234,56`. A sum or average leaves out
460
+ cells that are not numbers, and says how many (`values_not_numbers`); `min` and `max` of ISO dates
461
+ compare them as text. Up to 10,000 groups are counted one by one; rows of further groups are
462
+ counted in `other_rows`.
463
+
464
+ Result:
465
+
466
+ | Field | Type | Meaning |
467
+ |---|---|---|
468
+ | `document` | string | The file name. |
469
+ | `table` | string | The table read: "the CSV file", "Sheet 2: Sales", "table 3 of 5". |
470
+ | `tables_in_file` | number, optional | Sheets or tables in the file, when more than one. |
471
+ | `header_line` | number | The line of the header's last row (0 with no header). |
472
+ | `header_rows`, `title` | optional | The header's rows, when not one, and a title row left out. |
473
+ | `columns` | string[] | The column names, up to 60. |
474
+ | `rows_read`, `rows_matched` | number | The table's rows, and those the filters kept. |
475
+ | `total_groups` | number, optional | Groups found, with `group_by`. |
476
+ | `groups` | array | One entry a group: `group` (the value of each group-by column, as its first row writes it), each metric by its name (`count`, `"sum:Revenue"`: null when no row of the group had a number there), `rows` when `count` was not asked, and `lines`, the lines of its first 3 rows. |
477
+ | `note` | string, optional | "N of the M groups are shown…", when `limit` cut them. |
478
+ | `values_not_numbers`, `other_rows`, `repeated_header_rows`, `irregular_rows` | optional | Cells left out of a metric, rows of groups past 10,000, header rows found again inside the table, and rows with values past the header's columns (a sign that the header was read wrong). |
479
+
480
+ ```json
481
+ {
482
+ "document": "orders-2026.csv",
483
+ "table": "the CSV file",
484
+ "header_line": 1,
485
+ "columns": ["OrderId", "Date", "Region", "Product", "Amount"],
486
+ "rows_read": 5000,
487
+ "rows_matched": 1250,
488
+ "total_groups": 5,
489
+ "groups": [
490
+ { "group": { "Region": "Center" }, "avg:Amount": 535.27, "rows": 250, "lines": [5, 10, 15] },
491
+ { "group": { "Region": "East" }, "avg:Amount": 530.85, "rows": 250, "lines": [3, 8, 13] }
492
+ ]
493
+ }
494
+ ```
495
+
496
+ Errors say what to change: "No column \"Profit\" in this table. Its columns are: …", "\"הכנסות\"
497
+ names 3 columns of this table: …. Name one of them in full.", "No table \"Budget\" in this file: it
498
+ has 3 sheets…", "This file has no table to aggregate…". `evidence` cites the header line and each
499
+ group's lines; a result dropped to make room keeps its largest 30 groups.
500
+
407
501
  ## `read_document`
408
502
 
409
503
  The description the model sees:
@@ -627,6 +721,9 @@ your own adds it to its system prompt). In short, it tells the model:
627
721
  - since 0.8.0, what to search for: "Search for the bare form of a word: a search matches inside
628
722
  longer words, so "timeout" also finds "timeouts", and a Hebrew word without its prefix letters
629
723
  (חוזה, not בחוזה) finds every form of it.";
724
+ - since 0.8.1, when a searchable file may hold a table: "To add up, average, count or group the
725
+ rows of a table (a CSV, an Excel sheet, a | cell | table), use aggregate_table: it reads every
726
+ row, and its numbers are exact. Do not add numbers up from rows you read.";
630
727
  - to read around search hits before relying on them;
631
728
  - to use `analyze_whole_document` only when the task needs all of the file and cannot be done by
632
729
  matching text (a full summary, a timeline, items judged by meaning), and that it is slow, costly,
@@ -738,7 +835,7 @@ has no option for it.
738
835
 
739
836
  ### `TOOL_NAMES`
740
837
 
741
- The names of the five tools, as constants.
838
+ The names of the six tools, as constants.
742
839
 
743
840
  ```ts
744
841
  const TOOL_NAMES = {
@@ -747,6 +844,7 @@ const TOOL_NAMES = {
747
844
  overview: "get_document_overview",
748
845
  analyze: "analyze_whole_document",
749
846
  view: "view_pages",
847
+ aggregate: "aggregate_table", // new in 0.8.1
750
848
  } as const;
751
849
  ```
752
850
 
package/docs/security.md CHANGED
@@ -584,10 +584,18 @@ server that memory is the server's heap. Choose the isolation with care:
584
584
  too. `files.ingest` checks the bytes you give it, which are already in memory.
585
585
  - **Memory.** The handlers buffer the whole upload, up to `maxUploadBytes`, before reading its
586
586
  text, and an ingest then holds several copies of the text at once (several times the size of the
587
- text), in your server's heap. Cap concurrent uploads in front (a reverse proxy, a rate limiter),
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.
587
+ text), in your server's heap. Since 0.8.2 the handlers hold uploads of at most
588
+ `maxPendingUploadBytes` at once (twice `maxUploadBytes`), and answer the others 503 before reading
589
+ their body; each library lets `maxConcurrentIngests` (2) ingests work at once, and queues the
590
+ others. A route of your own must bound the bodies it reads itself. The room of an upload is
591
+ taken from its `Content-Length` before its body arrives, so a few clients that send headers and
592
+ then trickle their bodies can hold all of it: `uploadIdleTimeoutMs` (30 s) frees the room of a
593
+ body that stops, and `server.requestTimeout` that of one that trickles. A reverse proxy that reads
594
+ whole bodies before passing them on, and a limit on the connections and uploads of each client,
595
+ keep that room for the others ([Deployment](./guides/deployment.md#uploads-and-ingest)). Read PDF and Office files with
596
+ `createIsolatedExtractors`, whose `concurrency` (2) and `maxQueue` (8) bound how many are read and
597
+ wait, or ingest in a process of its own: [Deployment](./guides/deployment.md) has measurements
598
+ and recipes.
591
599
  - **Text.** A small file can hold far more text than its size: a PDF of 3 KB whose page tree listed
592
600
  one dense page 150 times drew 107 MB of text in 0.5.0. `maxTextBytes` (100 MB by default, or
593
601
  `maxUploadBytes` when that is larger) bounds the text kept from one file, in UTF-8 bytes. The PDF
@@ -957,8 +965,9 @@ What you log is your choice, and some of what the library hands you holds user d
957
965
  ([Original files](#original-files)).
958
966
  - Set `limits` and a deadline, and log `usage` for failed questions too
959
967
  ([What a question can cost](#what-a-question-can-cost)).
960
- - Cap concurrent uploads, and set the limit of any body parser in front of the handlers
961
- ([Uploads](#uploads)).
968
+ - Keep the bound on uploads held at once (`maxPendingUploadBytes` of the handlers, or your own in a
969
+ route of your own), limit uploads per user, and set the limit of any body parser in front of the
970
+ handlers ([Uploads](#uploads)).
962
971
  - Keep the limits on one file (`maxTextBytes`, `maxPdfPages`, `ingestTimeoutMs`) as low as your
963
972
  users allow ([Uploads](#uploads)).
964
973
  - For untrusted uploads, parse them in a process of their own with a memory limit, not in a worker
@@ -258,6 +258,31 @@ is read and its text extracted, so a higher limit is more memory for each upload
258
258
 
259
259
  See [Web app](./guides/web-app.md) and [Deployment](./guides/deployment.md).
260
260
 
261
+ ### `503`: "The server is receiving other files right now. Try again in a moment."
262
+
263
+ New in 0.8.2. `createDocumentHandlers` answers `503` with `Retry-After: 5`, before reading the body.
264
+
265
+ **Cause.** The handlers already hold uploads, and this one would bring them past
266
+ `maxPendingUploadBytes` (twice `maxUploadBytes`, 200 MB by default). An upload is held from the
267
+ reading of its body to the end of its ingest, and its room is taken from its `Content-Length`
268
+ before its body arrives. Many users sending files at once, or a few clients that send their
269
+ headers and then their bodies slowly, fill it.
270
+
271
+ **Fix.** Send the file again after `Retry-After` seconds, as the browser code in
272
+ [Web app](./guides/web-app.md#upload-from-the-browser) does. To take more at once, raise
273
+ `maxPendingUploadBytes` and size the server for it (each upload under way takes about five to seven
274
+ times its size while it is ingested; ingests take turns, `maxConcurrentIngests`). Put a proxy that
275
+ reads whole bodies before passing them on in front, so that slow clients hold no room. "The server
276
+ is reading other files right now." is the other `503`: the readers of `createIsolatedExtractors`
277
+ are busy ([Node reference](./reference/node.md#uploadrequest-response)).
278
+
279
+ ### `408`: "The upload stopped arriving: nothing came for 30 s. Send it again."
280
+
281
+ New in 0.8.2. **Cause.** The body of an upload stopped arriving for `uploadIdleTimeoutMs` (30 s):
282
+ the client's connection stalled, or a proxy held the body. The connection is closed. **Fix.** Send
283
+ the file again. For users on slow or unsteady links, raise `uploadIdleTimeoutMs` of
284
+ `createDocumentHandlers`.
285
+
261
286
  ### `400`: "Name the file with an X-File-Name header or a ?name= query parameter."
262
287
 
263
288
  **Cause.** The upload request names no file. The name decides the format, and is shown to the
@@ -490,6 +515,9 @@ handler's `get` answers `404` with `This file is no longer available.` for the s
490
515
  - belongs to another `owner`, or has an owner while the question passes none. A document with an
491
516
  owner is reported as missing, never as forbidden;
492
517
  - was stored in a `MemoryDocumentStore` of another process, or of this one before it restarted;
518
+ - has lost its text in the store while its metadata stayed (removed outside the library). Since
519
+ 0.8.2 such a document is missing here too; before, a question about one read by range failed
520
+ with a plain `Error`;
493
521
  - is not a document ID at all (`isDocumentId` is false for it).
494
522
 
495
523
  **Fix.** Pass the same `owner` to the question as to the upload: the value `getOwner` returned for
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "llm-large-files",
3
- "version": "0.8.0",
3
+ "version": "0.8.2",
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",