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/reference/node.md
CHANGED
|
@@ -2,18 +2,22 @@
|
|
|
2
2
|
|
|
3
3
|
This page lists every export of `llm-large-files/node`: `createDocumentHandlers`, its options, and
|
|
4
4
|
the three HTTP handlers it returns, with the request each one expects and every status and body it
|
|
5
|
-
can answer;
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
read
|
|
5
|
+
can answer; `createIsolatedExtractors`, which reads PDF and Office files in child processes with a
|
|
6
|
+
memory limit and a kill timer; and `writeSseEvents` and `sseMessageOf`, which send the events of a
|
|
7
|
+
question or an ingest to a browser as Server-Sent Events. It is for you if your server runs on Node
|
|
8
|
+
and you want uploads, look-ups and deletes of documents without writing the routes yourself, files
|
|
9
|
+
from users read apart from your server, or a streamed answer without writing the stream yourself.
|
|
10
|
+
How to mount them, what the browser sends, and how to read the stream in the browser are in the
|
|
11
|
+
guide [web app](../guides/web-app.md).
|
|
10
12
|
|
|
11
13
|
```ts
|
|
12
|
-
import { createDocumentHandlers, sseMessageOf, writeSseEvents } from "llm-large-files/node";
|
|
13
|
-
import type {
|
|
14
|
+
import { createDocumentHandlers, createIsolatedExtractors, sseMessageOf, writeSseEvents } from "llm-large-files/node";
|
|
15
|
+
import type {
|
|
16
|
+
DocumentHandlerOptions, DocumentHandlers, IsolatedExtractorOptions, IsolatedExtractors, SseMessage, SseWriteOptions,
|
|
17
|
+
} from "llm-large-files/node";
|
|
14
18
|
// What the signatures below use:
|
|
15
19
|
import type { IncomingMessage, ServerResponse } from "node:http";
|
|
16
|
-
import type { LargeFiles } from "llm-large-files";
|
|
20
|
+
import type { DocumentFormat, Extractor, LargeFiles, PdfExtractorOptions } from "llm-large-files";
|
|
17
21
|
```
|
|
18
22
|
|
|
19
23
|
## Contents
|
|
@@ -24,6 +28,8 @@ import type { LargeFiles } from "llm-large-files";
|
|
|
24
28
|
[its progress as events](#upload-with-progress-accept-textevent-stream),
|
|
25
29
|
[`get()`](#getrequest-response-id), [`remove()`](#removerequest-response-id)
|
|
26
30
|
- [How errors are answered](#how-errors-are-answered)
|
|
31
|
+
- [Isolated ingest](#isolated-ingest): [`createIsolatedExtractors()`](#createisolatedextractorsoptions),
|
|
32
|
+
[`IsolatedExtractorOptions`](#isolatedextractoroptions), [`IsolatedExtractors`](#isolatedextractors)
|
|
27
33
|
- [Server-Sent Events](#server-sent-events): [`writeSseEvents()`](#writesseeventsresponse-events-options),
|
|
28
34
|
[`sseMessageOf()`](#ssemessageofevent), [`SseMessage`](#ssemessage),
|
|
29
35
|
[`SseWriteOptions`](#ssewriteoptions)
|
|
@@ -135,7 +141,7 @@ promise resolves once the response is written; none rejects.
|
|
|
135
141
|
|
|
136
142
|
The summary is a [`DocumentSummary`](./library.md#documentsummary), the fields of a document that
|
|
137
143
|
are safe to send to a browser: `id`, `name`, `mimeType`, `format`, `bytes`, `lines`,
|
|
138
|
-
`estimatedTokens`, `pages` (for a PDF or
|
|
144
|
+
`estimatedTokens`, `pages` (for a PDF, a PPTX or an XLSX), `encoding` and `replacedCharacters` (for a text
|
|
139
145
|
file that was not valid UTF-8, since 0.6.0) and `expiresAt`. It holds no `owner`.
|
|
140
146
|
|
|
141
147
|
#### upload(request, response)
|
|
@@ -169,12 +175,19 @@ What it does, in order:
|
|
|
169
175
|
as the file, with the same limit;
|
|
170
176
|
- an object with fields in `request.body` (`express.json()` on a JSON request) is answered
|
|
171
177
|
`400`;
|
|
172
|
-
- otherwise it reads the request stream. Past the limit it stops
|
|
173
|
-
The rest of the body is
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
+
- otherwise it reads the request stream. Past the limit it stops keeping the bytes, and
|
|
179
|
+
answers `413` at once. The rest of the body is then read and dropped, up to 8 MB, and the
|
|
180
|
+
answer ends when the body does: a client that is still sending reads the `413`, and can send
|
|
181
|
+
its next request on the same keep-alive connection. A body that goes on more than 8 MB past
|
|
182
|
+
that is cut off: the connection is closed, and a client still sending may then get a reset
|
|
183
|
+
(`ECONNRESET`) instead of the answer. Closing the connection at once was not chosen: some
|
|
184
|
+
systems (Windows) reset a connection closed while data is still arriving, and the client
|
|
185
|
+
never reads the answer. The same holds for every answer sent before the body was read to its
|
|
186
|
+
end (no name, or a `Content-Length` over the limit, whose body Node would otherwise read
|
|
187
|
+
whole). Before 0.7.1 the rest was left unread, and the connection stayed open with the
|
|
188
|
+
client stuck sending into it. The answer to such a body has no `Content-Length`. A stream
|
|
189
|
+
that another middleware has already read gives no bytes, and the upload is answered `422` as
|
|
190
|
+
an empty file.
|
|
178
191
|
4. Ingests it: `files.ingest({ name, bytes, mimeType, owner, signal })`, `mimeType` and `owner`
|
|
179
192
|
only when there are any. The `signal` aborts when the client closes the connection before the
|
|
180
193
|
answer is sent: the ingest stops between two pages or pieces, and nothing is stored. The
|
|
@@ -190,7 +203,8 @@ The responses:
|
|
|
190
203
|
| `400` | `{ "error": "Name the file with an X-File-Name header or a ?name= query parameter." }` | No name. |
|
|
191
204
|
| `400` | `{ "error": "The upload body was already parsed by another middleware. Mount the upload route before body parsers, or send Content-Type: application/octet-stream." }` | `request.body` is an object with fields. |
|
|
192
205
|
| `413` | `{ "error": "The file is larger than the 100 MB upload limit." }`, with your `maxUploadBytes` in its place (`512 KB`, `1000 B`) | `Content-Length`, the bytes read, or the `Buffer` in `request.body` are over `maxUploadBytes`. |
|
|
193
|
-
| `422` | `{ "error": <the message of files.ingest> }` | The file cannot be read. The message says why and is meant for the user: "app.log is empty.", "app.log did not contain any text.", "This file looks binary, not text. Attach a text, log, PDF,
|
|
206
|
+
| `422` | `{ "error": <the message of files.ingest> }` | The file cannot be read: `files.ingest` threw an [`IngestError`](./library.md#ingesterror), whose `status` is 422. The message says why and is meant for the user: "app.log is empty.", "app.log did not contain any text.", "This file looks binary, not text. Attach a text, log, PDF, Word, PowerPoint, Excel or HTML file.", "This file is gzip-compressed. Decompress it, then attach the text file inside.", "This file is a ZIP archive. Extract it, then attach the text file you need.", an Office file of before 2007 or with a password, a PDF with a password or with no text layer, and a PDF, DOCX, PPTX or XLSX while the optional dependency that reads it is missing ("Reading PDF files needs the optional dependency "unpdf".") or, for a PDF, cannot run on this Node ("The unpdf installed cannot read PDF files on Node 20 (…). Install unpdf 1.7.0 (npm install unpdf@1.7.0), or run Node 22 or later.", see [defaultExtractors](./library.md#defaultextractors)). A text file is never refused for its encoding. A file past the limits of `createLargeFiles`: more text than `maxTextBytes`, more pages than `maxPdfPages`, longer to read than `ingestTimeoutMs` ("report.pdf took too long to read (more than 2 min). Split it, or save it as plain text, and attach that."), or built to exhaust the server ([every message](../guides/pdfs.md#what-is-refused)). With [`createIsolatedExtractors`](#createisolatedextractorsoptions), also a file whose reader ran out of memory or time. An extractor of your own that throws an `Error` gives its message here too. |
|
|
207
|
+
| `503` | `{ "error": "The server is reading other files right now. Try again in a moment." }`, with `Retry-After: 5` | The isolated readers of [`createIsolatedExtractors`](#createisolatedextractorsoptions) are all busy and their queue is full: an `IngestError` of code `"busy"`. The header is its `retryAfterMs` in seconds, rounded up, at least 1. While they shut down (`close()`): "The server is shutting down. Try again in a moment." New in 0.8.0. |
|
|
194
208
|
| 400 to 599 | `{ "error": <the error's message> }` | `getOwner`, an extractor or the store threw an error with that `status` or `statusCode`. |
|
|
195
209
|
| `500` | `{ "error": "Upload failed (empty owner)." }` | `getOwner` returned `""`, spaces only or `null`. The body is not read. New in 0.6.0. |
|
|
196
210
|
| `500` | `{ "error": "Upload failed (owner is not a string)." }` | `getOwner` returned, from JavaScript, something that is neither a string nor a finite number: an object such as `req.user`, `true`, `NaN`, an array. The body is not read. New in 0.6.0. |
|
|
@@ -249,7 +263,7 @@ of [`IngestEvent`](./events.md#ingestevent), stamped with `seq` (1, 2, 3… with
|
|
|
249
263
|
| `ingest.start` | `name` (cleaned), `format` (what the name and the type say: a text file can still become a log), `bytes` | First. |
|
|
250
264
|
| `ingest.progress` | `stage` (`"decode"`, `"extract"`, `"check"`, `"pictures"`, `"prepare"`, `"index"` or `"store"`), `done`, `total` (absent when not known), `unit` (`"pages"`, `"parts"`, `"bytes"` or `"characters"`) | At most about every 12 ms for a stage, and once more when the stage ends. A stage with nothing to report may send none ([stages and units](./events.md#stages-and-units)). |
|
|
251
265
|
| `ingest.finish` | `document`: the [`DocumentSummary`](./library.md#documentsummary), the same fields as the body of a `201` | Last, when the document is stored. |
|
|
252
|
-
| `ingest.error` | `error`: `{ "message": …, "status": … }`, the message and status the JSON answer would have | Last, when the ingest fails. |
|
|
266
|
+
| `ingest.error` | `error`: `{ "message": …, "status": … }`, the message and status the JSON answer would have, and for a file that could not be read `"code"`, the [`IngestError`](./library.md#ingesterror) code (new in 0.8.0) | Last, when the ingest fails. |
|
|
253
267
|
|
|
254
268
|
```text
|
|
255
269
|
id: 4
|
|
@@ -262,10 +276,13 @@ data: {"type":"ingest.finish","document":{"id":"doc_f6502d2f94694d709c7db2ec634c
|
|
|
262
276
|
of your own sends no events at all, the handler adds the last one: then that one event is the
|
|
263
277
|
whole stream, with `seq` 1.
|
|
264
278
|
- **`ingest.error` holds what the JSON answer would.** A file that cannot be read gives
|
|
265
|
-
`{ "message": "This file looks binary, not text. …", "status": 422
|
|
266
|
-
`{ "message": "
|
|
267
|
-
|
|
268
|
-
`
|
|
279
|
+
`{ "message": "This file looks binary, not text. …", "status": 422, "code": "not_text" }`, and
|
|
280
|
+
isolated readers that are all busy `{ "message": "The server is reading other files right now. Try
|
|
281
|
+
again in a moment.", "status": 503, "code": "busy" }`, with no `Retry-After` (the headers are
|
|
282
|
+
sent). A store that failed is `{ "message": "Upload failed.", "status": 500 }`, never the store's
|
|
283
|
+
own message, which may name a host. An error with a status keeps it. The error has no `kind` or
|
|
284
|
+
`name` here, unlike the `EventError` of `files.ingest`'s own events; `code` is new in 0.8.0, so
|
|
285
|
+
that a page can say why in its own words.
|
|
269
286
|
- **The status is `200` whatever happens next.** Once the stream has started, a failure can only be
|
|
270
287
|
an `ingest.error` event.
|
|
271
288
|
- **A client that goes away stops the ingest**, as in step 4: nothing is stored, and nothing more
|
|
@@ -309,7 +326,8 @@ The three handlers answer an error by one rule:
|
|
|
309
326
|
from 400 to 599 is answered with that status and `{ "error": <its message> }`. With an empty
|
|
310
327
|
message, the body says what failed instead ("Upload failed.", "The file could not be looked
|
|
311
328
|
up.", "The file could not be deleted."). A `413` is always answered with the size message of
|
|
312
|
-
`upload`.
|
|
329
|
+
`upload`. An [`IngestError`](./library.md#ingesterror) carries its `status` (422, 413 or 503),
|
|
330
|
+
and a `503` whose error has a `retryAfterMs` also gets a `Retry-After` header, in seconds.
|
|
313
331
|
- **Any other error** is the server's trouble: `500`, with what failed and, for a Node system error
|
|
314
332
|
(one with a `code` and a `syscall`), its code: "Upload failed (ENOSPC).". The error's own message
|
|
315
333
|
is never sent, since it can name paths and hosts of your server.
|
|
@@ -322,6 +340,166 @@ The three handlers answer an error by one rule:
|
|
|
322
340
|
Guide: [errors and reliability](../guides/errors-and-reliability.md), for the errors of a question
|
|
323
341
|
itself; [web app](../guides/web-app.md), for what to show in the browser.
|
|
324
342
|
|
|
343
|
+
## Isolated ingest
|
|
344
|
+
|
|
345
|
+
New in 0.8.0. The built-in extractors read a file in your server's process, in its heap: a file
|
|
346
|
+
built to exhaust a server (a zip bomb, a PDF whose objects take gigabytes) can run that heap out of
|
|
347
|
+
memory and stop every answer streaming on the server. `createIsolatedExtractors` reads PDF, DOCX,
|
|
348
|
+
PPTX and XLSX files in child processes instead, each with a heap limit and a kill timer, so that
|
|
349
|
+
such a file is refused and the server goes on. A worker thread does not reliably contain running
|
|
350
|
+
out of memory; a process does ([Security](../security.md#reading-pdfs-for-pages-and-pictures)).
|
|
351
|
+
|
|
352
|
+
### createIsolatedExtractors(options?)
|
|
353
|
+
|
|
354
|
+
```ts
|
|
355
|
+
declare function createIsolatedExtractors(options?: IsolatedExtractorOptions): IsolatedExtractors;
|
|
356
|
+
```
|
|
357
|
+
|
|
358
|
+
Starts nothing yet: a child process starts with the first file it is given. Pass its `extractors`
|
|
359
|
+
to `createLargeFiles`. Everything else stays as it is: `files.ingest`, the upload handler, events
|
|
360
|
+
and progress.
|
|
361
|
+
|
|
362
|
+
```ts
|
|
363
|
+
import { createServer } from "node:http";
|
|
364
|
+
import { createLargeFiles, FileDocumentStore } from "llm-large-files";
|
|
365
|
+
import { createDocumentHandlers, createIsolatedExtractors } from "llm-large-files/node";
|
|
366
|
+
|
|
367
|
+
const isolated = createIsolatedExtractors({ maxHeapMb: 512, concurrency: 2 });
|
|
368
|
+
const files = createLargeFiles({ store: new FileDocumentStore("./data"), extractors: isolated.extractors });
|
|
369
|
+
const documents = createDocumentHandlers(files);
|
|
370
|
+
const server = createServer(/* your routes, with documents.upload */).listen(3000);
|
|
371
|
+
|
|
372
|
+
// A SIGTERM listener replaces Node's own exit: stop the server there too, or it goes on serving.
|
|
373
|
+
// (On Windows no SIGTERM arrives: a kill ends the process.)
|
|
374
|
+
process.once("SIGTERM", () => {
|
|
375
|
+
server.close(); // no new connections; requests under way go on
|
|
376
|
+
void isolated.close(); // kills the children: the files they read are refused as "busy"
|
|
377
|
+
});
|
|
378
|
+
```
|
|
379
|
+
|
|
380
|
+
With nothing else open (a database pool, a timer of your own), the process then exits once the
|
|
381
|
+
requests under way end. To let the uploads under way finish before the children are killed, call
|
|
382
|
+
`close()` later, when the grace period of your platform runs out, as in
|
|
383
|
+
[Stopping a server](../guides/deployment.md#stopping-a-server).
|
|
384
|
+
|
|
385
|
+
What happens to a file of one of its formats:
|
|
386
|
+
|
|
387
|
+
1. `files.ingest` calls the isolated extractor, as it would the built-in one. When `concurrency`
|
|
388
|
+
files are being read, the file waits, up to `maxQueue` files; past that it is refused at once
|
|
389
|
+
as `"busy"`.
|
|
390
|
+
2. Its bytes are copied to a child process, with its name, MIME type, format and the limits of
|
|
391
|
+
`createLargeFiles` (`maxTextBytes`, `maxPdfPages`, `scannedPdfPages`). The child reads it with the
|
|
392
|
+
built-in extractor of its format, and sends its progress and then its text back.
|
|
393
|
+
3. The rest of the ingest runs in your server, as without it: the text is normalized, profiled,
|
|
394
|
+
indexed and stored. A file costs its bytes copied to the child and its text copied back.
|
|
395
|
+
4. A child reads one file at a time. It is replaced by a new one after `maxFilesPerChild` files,
|
|
396
|
+
since some parsers keep memory for the life of their process (`@cantoo/pdf-lib` keeps every name
|
|
397
|
+
it reads), and stopped after `idleMs` with nothing to read. An idle child does not keep your
|
|
398
|
+
process alive.
|
|
399
|
+
|
|
400
|
+
When the ingest's signal aborts (`ingestTimeoutMs`, or a client that went away), the child that
|
|
401
|
+
reads the file is killed, and a file that waits leaves the queue. The children load the same
|
|
402
|
+
optional dependencies (`unpdf`, `jszip`, `@cantoo/pdf-lib`) the server would, so install them on
|
|
403
|
+
the server. Text formats, HTML included, are read in the server, in slices, by default.
|
|
404
|
+
|
|
405
|
+
The refusals of the children. Each is an [`IngestError`](./library.md#ingesterror), with your
|
|
406
|
+
numbers in its message:
|
|
407
|
+
|
|
408
|
+
| `code` | Message | When |
|
|
409
|
+
|---|---|---|
|
|
410
|
+
| `"out_of_memory"` | `report.pdf needed more memory than is allowed for reading one file (512 MB). It may be built to exhaust the server, so it is not read. Split it, or save it as plain text, and attach that.` | The child ran out of its heap, `maxHeapMb`: its standard error says so, or it ended with `SIGABRT` or exit code 134. |
|
|
411
|
+
| `"timeout"` | `report.pdf took too long to read (more than 2 min). Split it, or save it as plain text, and attach that.` | The file took longer than `timeoutMs`. The child is killed, even in the middle of a PDF page that pdf.js reads in one block, which `ingestTimeoutMs` cannot cut short. |
|
|
412
|
+
| `"busy"` | `The server is reading other files right now. Try again in a moment.` | `concurrency` files are being read and `maxQueue` wait. `retryAfterMs` is 5,000: the upload handler answers `503` with `Retry-After: 5` ([upload](#uploadrequest-response)). |
|
|
413
|
+
| `"busy"` | `The server is shutting down. Try again in a moment.` | `close()` was called while the file waited or was read, or before it was sent. |
|
|
414
|
+
| `"unreadable"` | `report.pdf could not be read: its reader stopped (SIGSEGV).`, or `(exit code 1)` | The child ended another way. |
|
|
415
|
+
| `"unreadable"` | `report.pdf could not be read: its reader could not be reached.` | The file could not be sent to the child. |
|
|
416
|
+
|
|
417
|
+
A child that stops before it is ready to read, so before it was sent the file, is the server's
|
|
418
|
+
trouble, not the file's: a `childScript` that is not there, an `execArgv` that Node does not know.
|
|
419
|
+
Such a file is refused with an `Error` that is not an `IngestError`, with `status` 500:
|
|
420
|
+
`The server could not start a reader for report.pdf (exit code 1).` The upload handler answers it
|
|
421
|
+
`500` with that message. The end of the child's standard error, which names paths of the server, is
|
|
422
|
+
its `cause` only. The upload handler answers the error itself, so to log the cause there, wrap the
|
|
423
|
+
extractors before you hand them to `createLargeFiles`:
|
|
424
|
+
|
|
425
|
+
```ts
|
|
426
|
+
const isolated = createIsolatedExtractors();
|
|
427
|
+
const extractors = Object.fromEntries(Object.entries(isolated.extractors).map(([format, extract]) => [
|
|
428
|
+
format,
|
|
429
|
+
(bytes: Uint8Array, input: ExtractInput) => extract!(bytes, input).catch((error: unknown) => {
|
|
430
|
+
if ((error as { status?: unknown }).status === 500) console.error("reader did not start", (error as Error).cause);
|
|
431
|
+
throw error;
|
|
432
|
+
}),
|
|
433
|
+
]));
|
|
434
|
+
const files = createLargeFiles({ store, extractors });
|
|
435
|
+
```
|
|
436
|
+
|
|
437
|
+
A file the built-in extractor refuses in the child is refused with the same message as in the
|
|
438
|
+
server, and the code of its words: a zip bomb or a PDF that would hold the server is `"hostile"`, a
|
|
439
|
+
PDF without text `"no_text"`, and so on ([`IngestError`](./library.md#ingesterror)).
|
|
440
|
+
|
|
441
|
+
**The heap limit.** Each child runs with `--max-old-space-size` set to `maxHeapMb`, and Node adds
|
|
442
|
+
its young generation above it: with 512, a heap limit of 560 MB on Node 20 and 22, 704 MB on Node
|
|
443
|
+
24 and 608 MB on Node 26 ([Deployment](../guides/deployment.md#memory-on-each-server)). The
|
|
444
|
+
server's own `NODE_OPTIONS` are not passed to the children: a `--max-old-space-size` there would
|
|
445
|
+
replace this one. The rest of its environment is. The limit bounds the heap only: what a parser
|
|
446
|
+
unpacks outside it, such as a stream of a PDF, is not bounded by it. For uploads you do not trust,
|
|
447
|
+
give the server a memory limit of its own too, such as that of its container
|
|
448
|
+
([Security](../security.md#uploads)).
|
|
449
|
+
|
|
450
|
+
### `IsolatedExtractorOptions`
|
|
451
|
+
|
|
452
|
+
```ts
|
|
453
|
+
type IsolatedExtractorOptions = {
|
|
454
|
+
maxHeapMb?: number;
|
|
455
|
+
timeoutMs?: number;
|
|
456
|
+
concurrency?: number;
|
|
457
|
+
maxQueue?: number;
|
|
458
|
+
formats?: DocumentFormat[];
|
|
459
|
+
pdf?: PdfExtractorOptions;
|
|
460
|
+
maxFilesPerChild?: number;
|
|
461
|
+
idleMs?: number;
|
|
462
|
+
execArgv?: string[];
|
|
463
|
+
childScript?: string | URL;
|
|
464
|
+
};
|
|
465
|
+
```
|
|
466
|
+
|
|
467
|
+
| Option | Type | Default | What it does |
|
|
468
|
+
|---|---|---|---|
|
|
469
|
+
| `maxHeapMb` | `number` | 512 | The heap of each child process, in MB, given as `--max-old-space-size`. A file that needs more is refused (`"out_of_memory"`), and the server goes on. From 1 to 1,048,576, and finite; a fraction is cut to a whole number. Node reads `--max-old-space-size=0` as its default heap, so a value below 1 is refused rather than cut to 0. |
|
|
470
|
+
| `timeoutMs` | `number` | 120,000 | The most time one file may take, in milliseconds. Past it, its child is killed and the file refused (`"timeout"`). 0 sets none, and so does a value past what a timer can wait (2,147,483,647 ms, 24.8 days), `Infinity` included, as for `ingestTimeoutMs`. With both at their defaults, the `ingestTimeoutMs` of `createLargeFiles` usually ends the ingest first, and its signal kills the child too. |
|
|
471
|
+
| `concurrency` | `number` | 2 | How many files are read at once, each in a child process of its own. At least 1. |
|
|
472
|
+
| `maxQueue` | `number` | 8 | How many files may wait for a child. Past it, a file is refused at once as `"busy"`. 0: none waits. |
|
|
473
|
+
| `formats` | `DocumentFormat[]` | `["pdf", "docx", "pptx", "xlsx"]` | The formats read in child processes: by default those whose parsers a hostile file can exhaust. `extractors` has an entry for each. |
|
|
474
|
+
| `pdf` | [`PdfExtractorOptions`](./library.md#pdfextractoroptions) | `{}` | Options of the built-in PDF extractor in the children, as for [`createPdfExtractor`](./library.md#createpdfextractoroptions). |
|
|
475
|
+
| `maxFilesPerChild` | `number` | 50 | How many files one child reads before it is replaced by a new one. At least 1. |
|
|
476
|
+
| `idleMs` | `number` | 30,000 | A child with nothing to read is stopped after this many milliseconds. 0, or a value past what a timer can wait (`Infinity` included), keeps the children until `close()`. Either way an idle child does not keep your process alive. |
|
|
477
|
+
| `execArgv` | `string[]` | `[]` | Further Node options for the children, after the heap limit. |
|
|
478
|
+
| `childScript` | `string \| URL` | The `ingest-child.js` shipped next to the module | The script the children run: a path, a `file:` URL, or a string of one. For a bundler that moves files: `import.meta.resolve("llm-large-files/node/ingest-child.js")` gives the one in the installed package ([Bundlers](../guides/deployment.md#nodejs-esm-and-require)). |
|
|
479
|
+
|
|
480
|
+
A number option that is not a number, NaN, below 0, or 0 where it must be above 0, throws a
|
|
481
|
+
`RangeError` when `createIsolatedExtractors` is called: "maxHeapMb must be a number above 0, and is
|
|
482
|
+
NaN.", or "timeoutMs must be a number above or equal to 0, and is -1.". So does a `maxHeapMb` below
|
|
483
|
+
1, above 1,048,576 (1 TB, past which Node reads the flag wrong) or not finite: "maxHeapMb must be a
|
|
484
|
+
number from 1 to 1,048,576 (1 TB), and is 0.5.". A heap below about 32 MB cannot start a reader
|
|
485
|
+
that reads ordinary files: every file is then refused.
|
|
486
|
+
|
|
487
|
+
### `IsolatedExtractors`
|
|
488
|
+
|
|
489
|
+
```ts
|
|
490
|
+
type IsolatedExtractors = {
|
|
491
|
+
extractors: Partial<Record<DocumentFormat, Extractor>>;
|
|
492
|
+
stats(): { running: number; queued: number; children: number };
|
|
493
|
+
close(): Promise<void>;
|
|
494
|
+
};
|
|
495
|
+
```
|
|
496
|
+
|
|
497
|
+
| Member | What it is |
|
|
498
|
+
|---|---|
|
|
499
|
+
| `extractors` | One extractor for each of `formats`, to pass to `createLargeFiles({ extractors })`. To add one of your own for another format: `{ ...isolated.extractors, text: myText }`. |
|
|
500
|
+
| `stats()` | The files being read (`running`), the files waiting (`queued`) and the child processes alive (`children`): for a health check or a metric. |
|
|
501
|
+
| `close()` | Kills every child process, and refuses the files waiting, those being read and those sent while it runs as `"busy"` ("The server is shutting down. Try again in a moment."). Resolves once the children have exited, also when nothing else keeps your process open, so code after `await isolated.close()` runs. Files sent after that start new children. Call it when the server stops, next to the server's own `close()`: a SIGTERM listener replaces Node's own exit ([above](#createisolatedextractorsoptions)). |
|
|
502
|
+
|
|
325
503
|
## Server-Sent Events
|
|
326
504
|
|
|
327
505
|
New in 0.7.0. Two functions send the events of a question (`askStream`, or
|
package/docs/reference/openai.md
CHANGED
|
@@ -143,7 +143,7 @@ every later question on that model, in that process.
|
|
|
143
143
|
| Case | Error | `status` | `kind` |
|
|
144
144
|
|---|---|---|---|
|
|
145
145
|
| The server answered with an HTTP error | [`OpenAiRequestError`](#openairequesterror) with the body's `error.message` (or `error`, when it is a string; a body wrapped in a list, as the Gemini API's layer sends it, is read too); else, since 0.6.0, a `message` at the top of the body (vLLM before its OpenAI-shaped errors, SGLang, FastChat) or a FastAPI `detail` (a string, or the `msg` of each problem in a list, joined with "; "); else "The model returned HTTP {status}." (for example "The model returned HTTP 503."). With `param`, `code` and `retryAfterMs` when the server gave them, in `error` or at the top of the body. | The HTTP status | Told from the status, the message and `code` |
|
|
146
|
-
| An error object inside a stream that began with 200 | `OpenAiRequestError` with its `message`, else "The model reported an error.", with its `code` and `param` when they are strings (since 0.6.0). | Its `code` when that is a status from 400 to 599; else from its `type` or `code`: `server_error`, `api_error`, `internal_error`, `internal_server_error` 500; `service_unavailable` 503; `rate_limit_exceeded`, `rate_limit_error`, `tokens_exceeded`, `insufficient_quota` 429; `timeout` 408; `invalid_request_error`, `context_length_exceeded` 400; any other 500. In 0.5.0: its `code` when that was a number, else the response's 200, not sent again | Told from the status, the message and `code`: `context_length_exceeded` is `"context_overflow"`, and `insufficient_quota` `"quota"`, neither sent again |
|
|
146
|
+
| An error object inside a stream that began with 200, or in a body of 200 that is not streamed (`{ "error": { … } }` with no `choices`, as OpenRouter sends once generation has started; since 0.7.1, also for `generateText`) | `OpenAiRequestError` with its `message`, else "The model reported an error.", with its `code` and `param` when they are strings (since 0.6.0). Before 0.7.1 a body of 200 with an error was read as a reply with no text. | Its `code` when that is a status from 400 to 599; else from its `type` or `code`: `server_error`, `api_error`, `internal_error`, `internal_server_error` 500; `service_unavailable` 503; `rate_limit_exceeded`, `rate_limit_error`, `tokens_exceeded`, `insufficient_quota` 429; `timeout` 408; `invalid_request_error`, `context_length_exceeded` 400; any other 500. In 0.5.0: its `code` when that was a number, else the response's 200, not sent again | Told from the status, the message and `code`: `context_length_exceeded` is `"context_overflow"`, and `insufficient_quota` `"quota"`, neither sent again |
|
|
147
147
|
| The connection failed, before or while the reply arrived, or an event of the stream was not JSON | `OpenAiRequestError`: "The model could not be reached (fetch failed).", the reason being the HTTP client's message | 0 | `"unreachable"` |
|
|
148
148
|
| A reply of HTTP 200 whose body is empty | `OpenAiRequestError`: "The model could not be reached (the reply was empty).", new in 0.6.0. When the reply's type is `text/event-stream`, "The model's reply ended before it was complete." Either is sent again as a reply cut off on its way. | 0 | `"unreachable"` |
|
|
149
149
|
| Nothing arrived for a whole wait (`firstByteTimeoutMs`, `idleTimeoutMs`) | `OpenAiRequestError`: "The model sent nothing for {seconds} s.", the wait that ran out (600 s or 300 s by default) | 0 | `"timeout"` |
|