llm-large-files 0.8.2 → 0.9.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 +535 -0
- package/README.md +9 -10
- package/dist/access.d.ts +86 -0
- package/dist/access.d.ts.map +1 -0
- package/dist/access.js +131 -0
- package/dist/access.js.map +1 -0
- package/dist/ask.d.ts +45 -1
- package/dist/ask.d.ts.map +1 -1
- package/dist/ask.js +181 -20
- package/dist/ask.js.map +1 -1
- package/dist/cache.d.ts +12 -3
- package/dist/cache.d.ts.map +1 -1
- package/dist/cache.js +18 -5
- package/dist/cache.js.map +1 -1
- package/dist/encoding.js +5 -3
- package/dist/encoding.js.map +1 -1
- package/dist/error-info.d.ts +57 -0
- package/dist/error-info.d.ts.map +1 -0
- package/dist/error-info.js +217 -0
- package/dist/error-info.js.map +1 -0
- package/dist/errors.d.ts +72 -8
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +102 -15
- package/dist/errors.js.map +1 -1
- package/dist/events.d.ts +35 -9
- package/dist/events.d.ts.map +1 -1
- package/dist/events.js +42 -11
- package/dist/events.js.map +1 -1
- package/dist/evidence.d.ts +4 -2
- package/dist/evidence.d.ts.map +1 -1
- package/dist/evidence.js +14 -3
- package/dist/evidence.js.map +1 -1
- package/dist/extract.d.ts +32 -8
- package/dist/extract.d.ts.map +1 -1
- package/dist/extract.js +46 -35
- package/dist/extract.js.map +1 -1
- package/dist/gemini/adapter.d.ts +19 -3
- package/dist/gemini/adapter.d.ts.map +1 -1
- package/dist/gemini/adapter.js +84 -12
- package/dist/gemini/adapter.js.map +1 -1
- package/dist/gemini/chat.d.ts +18 -6
- package/dist/gemini/chat.d.ts.map +1 -1
- package/dist/gemini/chat.js +2 -0
- package/dist/gemini/chat.js.map +1 -1
- package/dist/gemini/index.d.ts +1 -1
- package/dist/gemini/index.d.ts.map +1 -1
- package/dist/index.d.ts +9 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -3
- package/dist/index.js.map +1 -1
- package/dist/ingest.d.ts +70 -0
- package/dist/ingest.d.ts.map +1 -0
- package/dist/ingest.js +868 -0
- package/dist/ingest.js.map +1 -0
- package/dist/library.d.ts +169 -34
- package/dist/library.d.ts.map +1 -1
- package/dist/library.js +324 -591
- package/dist/library.js.map +1 -1
- package/dist/map-reduce.js +3 -3
- package/dist/map-reduce.js.map +1 -1
- package/dist/model.d.ts +43 -2
- package/dist/model.d.ts.map +1 -1
- package/dist/model.js +63 -3
- package/dist/model.js.map +1 -1
- package/dist/node/handlers.d.ts +226 -14
- package/dist/node/handlers.d.ts.map +1 -1
- package/dist/node/handlers.js +423 -181
- package/dist/node/handlers.js.map +1 -1
- package/dist/node/index.d.ts +3 -2
- package/dist/node/index.d.ts.map +1 -1
- package/dist/node/index.js +2 -1
- package/dist/node/index.js.map +1 -1
- package/dist/node/ingest-child.d.ts +7 -1
- package/dist/node/ingest-child.d.ts.map +1 -1
- package/dist/node/ingest-child.js +18 -3
- package/dist/node/ingest-child.js.map +1 -1
- package/dist/node/isolated.d.ts +3 -1
- package/dist/node/isolated.d.ts.map +1 -1
- package/dist/node/isolated.js +11 -4
- package/dist/node/isolated.js.map +1 -1
- package/dist/node/originals.d.ts +19 -0
- package/dist/node/originals.d.ts.map +1 -0
- package/dist/node/originals.js +82 -0
- package/dist/node/originals.js.map +1 -0
- package/dist/node/spool.d.ts +102 -0
- package/dist/node/spool.d.ts.map +1 -0
- package/dist/node/spool.js +365 -0
- package/dist/node/spool.js.map +1 -0
- package/dist/node/sse.d.ts +4 -3
- package/dist/node/sse.d.ts.map +1 -1
- package/dist/node/sse.js +7 -6
- package/dist/node/sse.js.map +1 -1
- package/dist/pdf-layout.d.ts.map +1 -1
- package/dist/pdf-layout.js +71 -12
- package/dist/pdf-layout.js.map +1 -1
- package/dist/ranged.d.ts +15 -11
- package/dist/ranged.d.ts.map +1 -1
- package/dist/ranged.js +42 -11
- package/dist/ranged.js.map +1 -1
- package/dist/retry.d.ts +3 -1
- package/dist/retry.d.ts.map +1 -1
- package/dist/retry.js +6 -2
- package/dist/retry.js.map +1 -1
- package/dist/scope.d.ts +25 -0
- package/dist/scope.d.ts.map +1 -0
- package/dist/scope.js +60 -0
- package/dist/scope.js.map +1 -0
- package/dist/source.d.ts +60 -0
- package/dist/source.d.ts.map +1 -0
- package/dist/source.js +170 -0
- package/dist/source.js.map +1 -0
- package/dist/stores.d.ts +120 -31
- package/dist/stores.d.ts.map +1 -1
- package/dist/stores.js +582 -139
- package/dist/stores.js.map +1 -1
- package/dist/testing/fake-fetch.d.ts +31 -0
- package/dist/testing/fake-fetch.d.ts.map +1 -0
- package/dist/testing/fake-fetch.js +66 -0
- package/dist/testing/fake-fetch.js.map +1 -0
- package/dist/testing/fake-model.d.ts +38 -0
- package/dist/testing/fake-model.d.ts.map +1 -0
- package/dist/testing/fake-model.js +62 -0
- package/dist/testing/fake-model.js.map +1 -0
- package/dist/testing/index.d.ts +7 -0
- package/dist/testing/index.d.ts.map +1 -0
- package/dist/testing/index.js +9 -0
- package/dist/testing/index.js.map +1 -0
- package/dist/testing/store-checks.d.ts +72 -0
- package/dist/testing/store-checks.d.ts.map +1 -0
- package/dist/testing/store-checks.js +619 -0
- package/dist/testing/store-checks.js.map +1 -0
- package/dist/tools.d.ts +25 -0
- package/dist/tools.d.ts.map +1 -1
- package/dist/tools.js +135 -13
- package/dist/tools.js.map +1 -1
- package/dist/types.d.ts +116 -13
- package/dist/types.d.ts.map +1 -1
- package/docs/README.md +17 -8
- package/docs/getting-started.md +18 -13
- package/docs/guides/anthropic.md +12 -8
- package/docs/guides/conversations.md +112 -47
- package/docs/guides/costs-and-limits.md +16 -7
- package/docs/guides/deployment.md +284 -83
- package/docs/guides/errors-and-reliability.md +161 -23
- package/docs/guides/own-adapter-and-loop.md +27 -10
- package/docs/guides/pdfs.md +123 -29
- package/docs/guides/storage.md +633 -145
- package/docs/guides/testing.md +78 -129
- package/docs/guides/vertex-and-gemini.md +10 -7
- package/docs/guides/web-app.md +312 -78
- package/docs/how-it-works.md +35 -8
- package/docs/measurements.md +43 -8
- package/docs/publishing.md +112 -36
- package/docs/reference/ask.md +154 -13
- package/docs/reference/errors-and-retries.md +166 -14
- package/docs/reference/events.md +53 -20
- package/docs/reference/gemini.md +70 -21
- package/docs/reference/library.md +706 -165
- package/docs/reference/node.md +624 -110
- package/docs/reference/testing.md +311 -0
- package/docs/reference/tools.md +15 -5
- package/docs/security.md +304 -67
- package/docs/troubleshooting.md +144 -65
- package/docs/upgrading-0.9.md +890 -0
- package/package.json +12 -3
- package/src/access.ts +144 -0
- package/src/ask.ts +230 -19
- package/src/cache.ts +20 -5
- package/src/encoding.ts +6 -4
- package/src/error-info.ts +255 -0
- package/src/errors.ts +129 -15
- package/src/events.ts +86 -21
- package/src/evidence.ts +14 -3
- package/src/extract.ts +50 -33
- package/src/gemini/adapter.ts +97 -11
- package/src/gemini/chat.ts +20 -6
- package/src/gemini/index.ts +1 -1
- package/src/index.ts +9 -6
- package/src/ingest.ts +927 -0
- package/src/library.ts +467 -601
- package/src/map-reduce.ts +3 -3
- package/src/model.ts +69 -5
- package/src/node/handlers.ts +630 -183
- package/src/node/index.ts +13 -2
- package/src/node/ingest-child.ts +23 -5
- package/src/node/isolated.ts +11 -4
- package/src/node/originals.ts +78 -0
- package/src/node/spool.ts +365 -0
- package/src/node/sse.ts +7 -6
- package/src/pdf-layout.ts +69 -11
- package/src/ranged.ts +34 -13
- package/src/retry.ts +6 -2
- package/src/scope.ts +63 -0
- package/src/source.ts +178 -0
- package/src/stores.ts +635 -140
- package/src/testing/fake-fetch.ts +80 -0
- package/src/testing/fake-model.ts +78 -0
- package/src/testing/index.ts +11 -0
- package/src/testing/store-checks.ts +660 -0
- package/src/tools.ts +150 -14
- package/src/types.ts +106 -13
package/docs/security.md
CHANGED
|
@@ -20,24 +20,41 @@ import { askGemini } from "llm-large-files/gemini";
|
|
|
20
20
|
// your sign-in session, never from the request body. `bytes`, `documentIds` and `messages` (an
|
|
21
21
|
// array of `ChatTurn` and `ToolTurn`, which both calls take) are yours.
|
|
22
22
|
const owner = userId;
|
|
23
|
-
|
|
24
|
-
const
|
|
25
|
-
|
|
26
|
-
const
|
|
27
|
-
const
|
|
28
|
-
|
|
29
|
-
const
|
|
30
|
-
|
|
23
|
+
// New in 0.9.0, and optional: the chat the document belongs to. `chatId` comes from your session too.
|
|
24
|
+
const scope = [userId, chatId];
|
|
25
|
+
|
|
26
|
+
const meta = await files.ingest({ name: "app.log", bytes, owner, scope });
|
|
27
|
+
const current = await files.getMeta(meta.id, { owner, scope });
|
|
28
|
+
const original = await files.getOriginal(meta.id, { owner, scope });
|
|
29
|
+
const result = await askWithFiles({ files, model, documentIds, messages, owner, scope });
|
|
30
|
+
// askGemini passes `owner` and `scope` on to askWithFiles.
|
|
31
|
+
const geminiResult = await askGemini({ files, endpoint, documentIds, messages, owner, scope });
|
|
32
|
+
const deleted = await files.delete(meta.id, { owner, scope });
|
|
31
33
|
```
|
|
32
34
|
|
|
33
35
|
A document that has an owner is used only by calls that pass that owner. A call with another
|
|
34
36
|
owner, or with none, gets what it gets for a document that does not exist: `undefined`, `false`,
|
|
35
37
|
`missing`, `DocumentsNotFoundError` or a 404 (the table is in
|
|
36
|
-
[Storage](./guides/storage.md#how-ownership-works)).
|
|
38
|
+
[Storage](./guides/storage.md#how-ownership-works)). A document that has a scope (new in 0.9.0) is
|
|
39
|
+
used only by calls that pass the same scope, segment by segment, in the same way
|
|
40
|
+
([Scopes](./guides/storage.md#scopes)). Every refusal reads as missing, never as forbidden.
|
|
37
41
|
|
|
38
42
|
A caller cannot tell a document of another owner from one that never existed or has expired.
|
|
39
43
|
`toDocumentSummary`, which the upload handlers answer with, leaves the owner out.
|
|
40
44
|
|
|
45
|
+
**One exception: `ingest` with an application's ID says that the ID is taken.** New in 0.9.0. An
|
|
46
|
+
ingest with `id` answers `IngestError` `"exists"` (409) when a document of that ID is there,
|
|
47
|
+
whoever's it is. Over a store that ignores the context, every store written for 0.8 among them, that
|
|
48
|
+
spans the whole store: a caller in one scope learns that an ID is used in another. It learns nothing
|
|
49
|
+
else: not the owner, the name or the text. Make such IDs hard to guess: random ones, such as
|
|
50
|
+
`` `up-${randomUUID().replaceAll("-", "")}` ``, or ones that start with the tenant's own prefix
|
|
51
|
+
([Application IDs](./guides/storage.md#application-ids)).
|
|
52
|
+
|
|
53
|
+
**`ifExists: "replace"` never replaces another owner's document.** A document of another owner or
|
|
54
|
+
scope is refused with `"exists"`, even when its text seems to be gone, unless it has expired. A
|
|
55
|
+
store or a wrapper that loses the context of one read would make a document that is there look
|
|
56
|
+
gone: the library does not take that as leave to delete it.
|
|
57
|
+
|
|
41
58
|
**An owner must be a string that is not empty.** `files.ingest` throws a `TypeError` for an owner
|
|
42
59
|
of `""`, of spaces only, or `null`:
|
|
43
60
|
`owner must be a string that is not empty or only spaces, such as the signed-in user's ID. To let anyone with the document's ID use it, leave owner out.`
|
|
@@ -47,14 +64,66 @@ others whose IDs it has. From JavaScript, an owner that is neither a string nor
|
|
|
47
64
|
(the user object in place of its ID, `true`, `NaN`, an array) is refused with the same error. A
|
|
48
65
|
number is accepted, as before. Only `ingest` and the upload handler refuse: calls that read with
|
|
49
66
|
`owner: ""` work as before, so documents that 0.5.0 stored with that owner stay readable until
|
|
50
|
-
they expire.
|
|
67
|
+
they expire. Under `requireOwner` (below), every call refuses such an owner, and those documents
|
|
68
|
+
can no longer be read.
|
|
51
69
|
|
|
52
70
|
**A document ingested without an owner is open to every call that has its ID**, with an owner or
|
|
53
|
-
without. Its ID then works like a password. An ID is `doc_` and 32 hex digits, a
|
|
54
|
-
122 random bits: it cannot be guessed, but it can leak.
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
71
|
+
without. Its ID then works like a password. An ID the library makes is `doc_` and 32 hex digits, a
|
|
72
|
+
random UUID with 122 random bits: it cannot be guessed, but it can leak. An ID your application
|
|
73
|
+
gives (`IngestInput.id`, new in 0.9.0) may be guessed, if it counts up or follows a pattern, and
|
|
74
|
+
`ingest` tells whether it is taken (above): use random IDs, or IDs with a prefix per tenant. The ID
|
|
75
|
+
of a document the model reads through tools is in its card and in the system instruction your model
|
|
76
|
+
provider receives (so a model can repeat it in an answer), and IDs end up in your URLs, in browser
|
|
77
|
+
storage and in logs. So an ID must never hold personal data, and every document should have an
|
|
78
|
+
owner.
|
|
79
|
+
|
|
80
|
+
**Fail closed with `requireOwner` and `requireScope`.** New in 0.9.0, both off by default. With
|
|
81
|
+
`createLargeFiles({ ..., requireOwner: true })`, every call must name an owner: `ingest`,
|
|
82
|
+
`getMeta`, `getOriginal`, `delete`, `prepare`, `askWithFiles`, `askStream`, `askGemini`, and
|
|
83
|
+
`tools`, which then needs `access`. A call without one throws
|
|
84
|
+
`TypeError("owner is required: requireOwner is on")` before the store is asked anything, and a
|
|
85
|
+
document stored without an owner reads as missing. So a route that forgot the owner fails, instead
|
|
86
|
+
of reaching every document stored without one. `requireScope` does the same for scopes
|
|
87
|
+
(`"scope is required: requireScope is on"`); `files.sweep()` without a scope stays allowed. Turn on
|
|
88
|
+
both where every document belongs to a signed-in user and a chat. A document a store gives back
|
|
89
|
+
with the owner `null` (a MongoDB driver writes a field that was left out that way) counts as having
|
|
90
|
+
no owner: open without `requireOwner`, missing with it.
|
|
91
|
+
|
|
92
|
+
**A store that forgets the context fails closed, except under `unscoped: "root"`.** Every store
|
|
93
|
+
call carries the owner and the scope (new in 0.9.0). A wrapper or a subclass that passes on only the
|
|
94
|
+
ID (`super.getMeta(id)`) loses them. Over a store that keeps scopes apart, the documents then look
|
|
95
|
+
missing; over a `FileDocumentStore` with scoped folders, every call but `sweep` throws a
|
|
96
|
+
`DocumentScopeError`. With `scopes.unscoped: "root"`, which serves the documents of 0.8, nothing
|
|
97
|
+
throws, even with `requireScope` on: the text and the metadata go to the root folder, outside the
|
|
98
|
+
chat's folder, and removing the chat's folder does not erase them. Another chat still cannot read
|
|
99
|
+
them: the library checks `meta.scope`. And a `sweep` that drops the context sweeps every chat:
|
|
100
|
+
`files.sweep({ scope })` removes the expired documents of all of them.
|
|
101
|
+
|
|
102
|
+
What the library checks is the metadata, against the caller: the owner and `meta.scope`. It asks
|
|
103
|
+
`getMeta` before every use of a document. When `get` returns a document whose metadata is not the
|
|
104
|
+
one `getMeta` described (another ID, owner, scope or version: a wrapper that keeps what `get` gave
|
|
105
|
+
by ID, or a document replaced on another server in between), the document is missing for that call.
|
|
106
|
+
So is a text of another length or number of lines than its metadata says, read whole or by range
|
|
107
|
+
(new in 0.9.0): a store that writes the text and the metadata apart can give one document's
|
|
108
|
+
metadata with another's text, when two servers ingest one ID at the same moment. A text of the same
|
|
109
|
+
length and lines is not caught: give each upload an ID of its own.
|
|
110
|
+
Bytes read by range and the original file carry no metadata: after they are read, the library asks
|
|
111
|
+
`getMeta` again, and they are missing unless the store still describes the same document. That
|
|
112
|
+
catches a document replaced while it was read. It cannot catch a store that answers `readTextRange`,
|
|
113
|
+
`getIndex` or `getOriginal` with another document's bytes while its `getMeta` is right, such as a
|
|
114
|
+
wrapper that keeps those answers by ID alone: such a store serves one scope's text to another. Pass
|
|
115
|
+
the context on, and key any cache of those reads by it too, as
|
|
116
|
+
[Writing a store of your own](./guides/storage.md#writing-a-store-of-your-own) shows.
|
|
117
|
+
|
|
118
|
+
**One ID, one ingest at a time.** In one library, while an ingest stores an application's ID,
|
|
119
|
+
another ingest of that ID is refused with `"exists"`, so two ingests cannot both find the ID free
|
|
120
|
+
and mix one user's file with another's metadata. Across servers, the store is
|
|
121
|
+
asked just before the writes too, but two servers can still write one ID at the same moment: the
|
|
122
|
+
library takes them for one upload sent twice. Give each upload an ID of its own. When the writes,
|
|
123
|
+
or the clean-up after a failed write, do not end in time, the ID stays refused in that library until they have ended. The clean-up after
|
|
124
|
+
a failed write leaves a document whose metadata it finds under the ID. The parts of one that another
|
|
125
|
+
server is still writing at that moment may be removed: one more reason to give each upload an ID of
|
|
126
|
+
its own.
|
|
58
127
|
|
|
59
128
|
What the owner should be (a user, or a user and a chat session), and how to keep a list of a
|
|
60
129
|
session's documents, is in [Storage](./guides/storage.md). The owner stops one user from reaching
|
|
@@ -77,7 +146,10 @@ before its body is read: `{ "error": "Upload failed (empty owner)." }` when `get
|
|
|
77
146
|
when it returns something that is neither a string nor a finite number, such as the user object in
|
|
78
147
|
place of its ID. Nothing is logged. Where every upload needs a signed-in user, throw a 401 as
|
|
79
148
|
below. Return `undefined` only where anonymous uploads are meant (`user?.id || undefined`). GET and
|
|
80
|
-
DELETE pass such an owner on as before
|
|
149
|
+
DELETE pass such an owner on as before, except under `requireOwner`, where the library refuses it
|
|
150
|
+
on every call: they answer `""` or spaces only with a 500 then, "The file could not be looked up
|
|
151
|
+
(empty owner)." and "The file could not be deleted (empty owner).", without calling it, and `null`
|
|
152
|
+
with a 401, as `undefined` (new in 0.9.0).
|
|
81
153
|
|
|
82
154
|
```ts
|
|
83
155
|
import type { IncomingMessage } from "node:http";
|
|
@@ -96,6 +168,14 @@ export const documentHandlers = createDocumentHandlers(files, {
|
|
|
96
168
|
});
|
|
97
169
|
```
|
|
98
170
|
|
|
171
|
+
New in 0.9.0: with `requireOwner: true` in `createLargeFiles`, the library refuses every call
|
|
172
|
+
without an owner, and the handlers follow. `createDocumentHandlers` throws a `TypeError` without
|
|
173
|
+
`getOwner`, and answers an owner of `undefined` with a `401` `{ "error": "Sign in first." }`, on
|
|
174
|
+
every route, before the body is read. A mistake that would store an upload open to anyone fails
|
|
175
|
+
at once instead. `requireScope` works the same way with `getScope`, which gives the scope of a
|
|
176
|
+
request (such as `[userId, chatId]`): a scope that is not valid, or none, is a `500` before the
|
|
177
|
+
body is read.
|
|
178
|
+
|
|
99
179
|
The routes, statuses and the rest of the upload flow are in [Web app](./guides/web-app.md) and the
|
|
100
180
|
[node reference](./reference/node.md).
|
|
101
181
|
|
|
@@ -109,19 +189,26 @@ may use. `view_pages` has its own list: the PDFs whose original is kept (in a st
|
|
|
109
189
|
that only its text was kept, any other is an unknown `document_id`.
|
|
110
190
|
|
|
111
191
|
`askWithFiles` and `askGemini` make one tool set per question, from the plan that `prepare` made
|
|
112
|
-
with the question's owner
|
|
113
|
-
|
|
114
|
-
**`files.tools()` checks
|
|
115
|
-
every call that reads a document's text or the pages of its PDF, it
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
`doc_… is no longer available on the server. Ask the user to attach the
|
|
119
|
-
document that is gone, and `view_pages` shows no page.
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
`
|
|
192
|
+
with the question's owner and scope, and pass them to the tools as `access`. You have nothing to do.
|
|
193
|
+
|
|
194
|
+
**`files.tools(documents, { access })` checks the owner and scope the store holds.** New in 0.9.0.
|
|
195
|
+
On every call that reads a document's text or the pages of its PDF, it asks the store for the
|
|
196
|
+
document with the owner and scope of `access`, checks that it is still there and has not expired,
|
|
197
|
+
and compares its stored owner and scope with `access`, as `getMeta(id, access)` does. When they
|
|
198
|
+
differ, the call answers `doc_… is no longer available on the server. Ask the user to attach the
|
|
199
|
+
file again.`, as for a document that is gone, and `view_pages` shows no page. The metadata handed
|
|
200
|
+
in decides nothing: metadata read straight from the store for another user (such as
|
|
201
|
+
`await store.getMeta(id)`) reads nothing with `access: { owner: "bob" }`. Under `requireOwner` or
|
|
202
|
+
`requireScope`, `tools` throws without `access`.
|
|
203
|
+
|
|
204
|
+
**Without `access`, `files.tools()` checks owners through the metadata it is given**, as before
|
|
205
|
+
0.9.0. It compares the owner and scope stored with the document with those of the `DocumentMeta`
|
|
206
|
+
you passed for that ID. So metadata built from an ID a client sent, with no owner or another one,
|
|
207
|
+
reads nothing of a document that has an owner. But the check compares strings: metadata that
|
|
208
|
+
carries the stored owner reads the document, wherever it came from, and a document stored without
|
|
209
|
+
an owner is open to any metadata. (Before 0.6.0, `tools()` checked no owner.) In a tool loop of
|
|
210
|
+
your own, pass `access`, and give `tools` only metadata that `prepare(ids, base, { owner, scope })`
|
|
211
|
+
or `getMeta(id, { owner, scope })` returned, never objects built from IDs a client sent:
|
|
125
212
|
|
|
126
213
|
```ts
|
|
127
214
|
import { DocumentsNotFoundError } from "llm-large-files";
|
|
@@ -129,9 +216,9 @@ import { DocumentsNotFoundError } from "llm-large-files";
|
|
|
129
216
|
// Your own loop: `base` is the size of the rest of your request. The tools below have no
|
|
130
217
|
// `generate`: `wholeDocumentAnalysis: false` keeps the instruction from naming
|
|
131
218
|
// `analyze_whole_document`, and the cards from offering a whole-file read.
|
|
132
|
-
const prepared = await files.prepare(documentIds, base, { owner, wholeDocumentAnalysis: false });
|
|
219
|
+
const prepared = await files.prepare(documentIds, base, { owner, scope, wholeDocumentAnalysis: false });
|
|
133
220
|
if (prepared.missing.length > 0) throw new DocumentsNotFoundError(prepared.missing);
|
|
134
|
-
const tools = files.tools(prepared.plan.searchable);
|
|
221
|
+
const tools = files.tools(prepared.plan.searchable, { access: { owner, scope } });
|
|
135
222
|
```
|
|
136
223
|
|
|
137
224
|
Writing your own loop is covered in [Your own adapter and loop](./guides/own-adapter-and-loop.md),
|
|
@@ -401,6 +488,12 @@ U+FFFD. `meta.replacedCharacters` counts the U+FFFD, and the model is told of th
|
|
|
401
488
|
(one hour by default). `FileDocumentStore`'s `sweep` throws nothing: a directory it cannot read
|
|
402
489
|
gives 0, and a document it cannot remove is left for the next sweep. Watch the count it returns,
|
|
403
490
|
and the volume, to know that expired files really go.
|
|
491
|
+
- **A delete is done once the metadata is gone.** Since 0.9.0, `FileDocumentStore.delete` returns
|
|
492
|
+
true once it removed the metadata, and a text, index or original it could not remove then waits
|
|
493
|
+
for the sweep (before, it threw). Where the files must go for good (a user's erasure request),
|
|
494
|
+
remove the folder of the scope, or keep the sweep running. The sweep removes such leftovers in the
|
|
495
|
+
store's own folders (`scopes`), and in a folder it shares with your files only under the IDs the
|
|
496
|
+
library makes.
|
|
404
497
|
- **A delete takes effect on every server that shares the store, from the next tool call on.**
|
|
405
498
|
Every use of a document first asks the store whether it is still there. A copy kept in memory
|
|
406
499
|
(within `cacheBytes`, 256 MB by default) is used only after that, and dropped when the document
|
|
@@ -410,25 +503,49 @@ U+FFFD. `meta.replacedCharacters` counts the U+FFFD, and the model is told of th
|
|
|
410
503
|
later questions. Neither reaches the answers, steps and evidence you stored, your logs, or what
|
|
411
504
|
your model provider keeps. To forget a document, also drop the tool rounds that name it from the
|
|
412
505
|
stored conversation ([Conversations](./guides/conversations.md)).
|
|
413
|
-
- **`FileDocumentStore` writes plain files** in one directory
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
506
|
+
- **`FileDocumentStore` writes plain files** in one directory, or with `scopes` in a folder per
|
|
507
|
+
scope (`<directory>/<userId>/<chatId>/.llm-large-files/`): `<id>.txt`, the text; `<id>.json`,
|
|
508
|
+
the metadata (owner, scope, file name, MIME type, sizes, dates, outline headings and, for a log,
|
|
509
|
+
its message templates); `<id>.idx`, a line index; `<id>.bin`, the original with `keepOriginals`;
|
|
510
|
+
and `.tmp` files while it writes. Nothing is encrypted, and a delete removes the files without
|
|
417
511
|
overwriting them. Use an encrypted volume, or write a `DocumentStore` over storage with encryption
|
|
418
512
|
at rest ([Storage](./guides/storage.md)).
|
|
419
|
-
- **
|
|
420
|
-
|
|
513
|
+
- **File modes.** Since 0.9.0, `FileDocumentStore` writes its files `0600` and makes its folders
|
|
514
|
+
`0700` (`fileMode`, `directoryMode`), minus the umask: only the user the server runs as can read
|
|
515
|
+
them. Before, they were `0666` and `0777` minus the umask, which gave files every user of the
|
|
516
|
+
machine could read (`0664` under the usual umask `0002`). On a volume shared by servers that run
|
|
517
|
+
as different users of one group (a Kubernetes `fsGroup`), set `fileMode: 0o640` and
|
|
518
|
+
`directoryMode: 0o750`, never more. Files written before 0.9.0 keep their modes: change them once
|
|
519
|
+
with `chmod` to narrow them.
|
|
520
|
+
- **No path traversal.** File names are document IDs, and only IDs of the shape
|
|
521
|
+
`APP_DOCUMENT_ID_PATTERN` (lowercase letters, digits, `_` and `-`, and no name Windows keeps for
|
|
522
|
+
a device) reach the file system. With `scopes`, each segment of a scope names a folder, and must
|
|
523
|
+
pass `isSafeScopeSegment` (the same shape, no `.` or `..`), or the call is refused. A call checks
|
|
524
|
+
that the folder of its scope is where its path says inside the directory, so a link there, to
|
|
525
|
+
another chat or out of the volume, is refused, and `sweep` follows no link. Writes and deletes
|
|
526
|
+
check every time; a read takes a check made less than a second ago, so a link put in place of a
|
|
527
|
+
chat's folder is seen by reads within a second. `createScopeFolders` makes a scope's folders one
|
|
528
|
+
level at a time, and nothing where a link points. A read does not follow a link at a file name
|
|
529
|
+
either (`O_NOFOLLOW`), except on Windows, which has no such flag: there, only links in the
|
|
530
|
+
folders are refused. A store with `scopes` never makes the folder of a chat again once you
|
|
531
|
+
removed it, unless `createScopeFolders` is on
|
|
532
|
+
([Storage](./guides/storage.md#files-in-a-folder-per-user-or-per-chat)).
|
|
533
|
+
- **Your files beside the store's.** In the store's directory without `scopes`, or with
|
|
534
|
+
`scopes.folder` `""`, the names `<id>.json`, `.txt`, `.idx` and `.bin`, and their temporary
|
|
535
|
+
files, belong to the store for every ID it is given: an ingest writes over your file of such a
|
|
536
|
+
name. Keep your own files in a subfolder, or under other names.
|
|
421
537
|
- `MemoryDocumentStore` keeps everything in the memory of one process, until it restarts. It is for
|
|
422
538
|
tests and single-process demos. It copies the metadata it is given and the metadata it hands out,
|
|
423
539
|
so deleting `owner` from what `ingest` or `getMeta` returned, before you send it to a browser,
|
|
424
|
-
changes nothing stored. (In 0.5.0 it opened the document to every owner.)
|
|
425
|
-
|
|
540
|
+
changes nothing stored. (In 0.5.0 it opened the document to every owner.) Since 0.9.0, the
|
|
541
|
+
original it returns is a copy too.
|
|
426
542
|
|
|
427
543
|
## Original files
|
|
428
544
|
|
|
429
545
|
With `keepOriginals` (for example `["pdf"]`), the uploaded file itself is stored next to its text
|
|
430
|
-
(`<id>.bin` in `FileDocumentStore`)
|
|
431
|
-
keeps
|
|
546
|
+
(`<id>.bin` in `FileDocumentStore`), or, since 0.9.0, read back from where your application keeps it
|
|
547
|
+
(`IngestInput.original`, [below](#originals-your-application-keeps)). That is off by default, and a
|
|
548
|
+
store without `putOriginal` keeps no copy. When you turn it on:
|
|
432
549
|
|
|
433
550
|
- **More is kept than before.** The text of a PDF leaves out its pictures, signatures, stamps and
|
|
434
551
|
hidden layers; the file has them all, and its metadata (author, software, dates). Treat the store
|
|
@@ -449,6 +566,40 @@ keeps none. When you turn it on:
|
|
|
449
566
|
or copying is opened with an empty password, and the cut is not encrypted: the restriction does
|
|
450
567
|
not travel with it. A PDF that needs a password to open is not shown.
|
|
451
568
|
|
|
569
|
+
- **Its bytes are checked when they are read back.** Since 0.9.0 every document has `sha256`, the
|
|
570
|
+
SHA-256 of the upload: a copy the store gives back is used only when it has that size and
|
|
571
|
+
SHA-256, so a store that answers with another document's file of the same size is caught. The
|
|
572
|
+
metadata is asked for again once the bytes are read, so a document deleted and stored again
|
|
573
|
+
under its ID by another server meanwhile is not mixed up with it.
|
|
574
|
+
|
|
575
|
+
### Originals your application keeps
|
|
576
|
+
|
|
577
|
+
New in 0.9.0. With `IngestInput.original: { ref }` and `LargeFilesOptions.originals`, the store keeps
|
|
578
|
+
no copy: the library records the ref with the file's size and SHA-256, and reads the file back
|
|
579
|
+
through your reader.
|
|
580
|
+
|
|
581
|
+
- **The ref never reaches the model,** nor a summary. It is in the metadata
|
|
582
|
+
(`meta.original.ref`), which you should not send to a browser: send `toDocumentSummary(meta)`.
|
|
583
|
+
An event holds it only with `eventResults: true`, in the full plan of `run.finish` and
|
|
584
|
+
`run.error`: keep such events on your server
|
|
585
|
+
([events](./reference/events.md#documents-eventresults-and-eventresult)).
|
|
586
|
+
- **Only the ref the store holds is read,** never one from metadata handed to `files.tools`: a
|
|
587
|
+
forged `original.ref` in a meta made by hand reads nothing it names.
|
|
588
|
+
- **A changed file is not shown.** One of another size or SHA-256 than at ingest gives `undefined`
|
|
589
|
+
from `getOriginal`, and `view_pages` tells the model it changed and to attach it again: its pages
|
|
590
|
+
would not be those of the text the model has.
|
|
591
|
+
- **`fileOriginals(root)` keeps refs inside `root`.** The ref is resolved under `root`, and must
|
|
592
|
+
stay inside it (`root` and a path separator before it): an absolute path elsewhere, or `..` that
|
|
593
|
+
climbs out, is a `TypeError`. A link at any level below `root` is not followed: the file's real
|
|
594
|
+
path must be the path the ref names, or it reads as missing. The file is opened without following
|
|
595
|
+
a link at its name, except on Windows. A folder swapped for a link between that check and the
|
|
596
|
+
read is not seen: keep the folders of the uploads writable only by your server. It reads at most
|
|
597
|
+
one byte past the size recorded, and only reads.
|
|
598
|
+
- **It is still a file of the user.** `delete` and `sweep` never touch it: remove the upload with
|
|
599
|
+
its chat, and with the user's other data when the account is closed.
|
|
600
|
+
- **The upload handler can write it.** `prepareUpload` gives the path and the ref, and the handler
|
|
601
|
+
saves the body there as it arrives, made new: [Files the upload handler writes](#files-the-upload-handler-writes).
|
|
602
|
+
|
|
452
603
|
How `view_pages` works, and what to weigh before you keep originals, is in [PDFs](./guides/pdfs.md).
|
|
453
604
|
|
|
454
605
|
## Reading PDFs for pages and pictures
|
|
@@ -583,10 +734,11 @@ server that memory is the server's heap. Choose the isolation with care:
|
|
|
583
734
|
`express.raw()`) has already read the body, it buffered it under its own `limit`: set that one
|
|
584
735
|
too. `files.ingest` checks the bytes you give it, which are already in memory.
|
|
585
736
|
- **Memory.** The handlers buffer the whole upload, up to `maxUploadBytes`, before reading its
|
|
586
|
-
text,
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
737
|
+
text, unless it is written to a file (`uploadSpool`, or the `path` of `prepareUpload`, new in
|
|
738
|
+
0.9.0: [below](#files-the-upload-handler-writes)), and an ingest then holds several copies of the
|
|
739
|
+
text at once (several times the size of the text), in your server's heap. Since 0.8.2 the
|
|
740
|
+
handlers hold uploads of at most `maxPendingUploadBytes` at once in memory (twice
|
|
741
|
+
`maxUploadBytes`), and answer the others 503 before reading their body; each library lets `maxConcurrentIngests` (2) ingests work at once, and queues the
|
|
590
742
|
others. A route of your own must bound the bodies it reads itself. The room of an upload is
|
|
591
743
|
taken from its `Content-Length` before its body arrives, so a few clients that send headers and
|
|
592
744
|
then trickle their bodies can hold all of it: `uploadIdleTimeoutMs` (30 s) frees the room of a
|
|
@@ -661,12 +813,15 @@ server that memory is the server's heap. Choose the isolation with care:
|
|
|
661
813
|
on their documents (`viewPages: false`), or keep no PDF originals
|
|
662
814
|
([Reading PDFs for pages and pictures](#reading-pdfs-for-pages-and-pictures)).
|
|
663
815
|
- **Stalls.** Ingest pauses for other requests about every 12 ms of its work: between the pages of
|
|
664
|
-
a PDF, as a DOCX or PPTX is unpacked, as a text file that is not UTF-8 is decoded, and in
|
|
665
|
-
|
|
666
|
-
stall of the event loop was 31 to 34 ms for a 99 MB log and 0.16 s for
|
|
667
|
-
1.9 to 2.0 s and 4.4 to 4.5 s with the ingest of 0.5.0
|
|
816
|
+
a PDF, as a DOCX or PPTX is unpacked, as a text file that is not UTF-8 is decoded, and in the
|
|
817
|
+
steps after extraction up to the store, a line of megabytes included. In a measurement of 0.6.0
|
|
818
|
+
on one machine, the longest stall of the event loop was 31 to 34 ms for a 99 MB log and 0.16 s for
|
|
819
|
+
a 2,000-page PDF, against 1.9 to 2.0 s and 4.4 to 4.5 s with the ingest of 0.5.0; 0.9.0 measured
|
|
820
|
+
0.23 to 0.36 s for a 95 MB log on a slower, busy machine
|
|
668
821
|
([Measurements](./measurements.md#the-server)). Some steps still hold the server, and neither the
|
|
669
822
|
time limit nor a signal cuts them short:
|
|
823
|
+
- A text file in UTF-8, as it is decoded when its reading starts, and, over a
|
|
824
|
+
`FileDocumentStore`, as its text is written: about 2 to 4 ms for each MB.
|
|
670
825
|
- A PDF page, for as long as pdf.js takes to read it: up to about 1.5 s for a page that passes
|
|
671
826
|
the check in a small file, longer in a large file, and without limit when the page check does
|
|
672
827
|
not run (without `@cantoo/pdf-lib`, on a file it cannot read or that declares more than 150,000
|
|
@@ -751,7 +906,8 @@ server that memory is the server's heap. Choose the isolation with care:
|
|
|
751
906
|
count or log them apart, by user. `"out_of_memory"` and `"timeout"` from the child processes of
|
|
752
907
|
`createIsolatedExtractors` can be such files too.
|
|
753
908
|
- **Scanned PDFs** (no text at all) are refused, unless PDF originals are kept (`keepOriginals`
|
|
754
|
-
includes `"pdf"` and the store has `putOriginal
|
|
909
|
+
includes `"pdf"` and the store has `putOriginal`, or the ingest gives `original` and the library
|
|
910
|
+
has `originals`) and the file has at most `scannedPdfPages` pages
|
|
755
911
|
(30 by default): their pages can then be shown.
|
|
756
912
|
- **File names** are reduced at ingest to one printable line: the last path segment. Control
|
|
757
913
|
characters, line and paragraph separators and text-direction controls become a space, and the
|
|
@@ -761,9 +917,29 @@ server that memory is the server's heap. Choose the isolation with care:
|
|
|
761
917
|
characters, and is never cut in the middle of a character (`file` when nothing is left). An
|
|
762
918
|
emoji of several characters, such as a flag, can lose its end at the cut. A name that an
|
|
763
919
|
earlier version cut in the middle of a character is sent with U+FFFD in place of the half.
|
|
764
|
-
- **The MIME type is the client's word.** The handlers take it from an `X-File-Type` header,
|
|
765
|
-
from `Content-Type` unless that is `application/octet-stream`.
|
|
766
|
-
|
|
920
|
+
- **The MIME type is the client's word.** The handlers take it from an `X-File-Type` header, which
|
|
921
|
+
they URI-decode, or from `Content-Type` unless that is `application/octet-stream`. Since 0.9.0
|
|
922
|
+
`Content-Type` is taken as it came, not URI-decoded: before, `%0D%0A` in it became a line break
|
|
923
|
+
(S11). The format is picked from the file name first, then from it. It is stored, and returned in
|
|
924
|
+
summaries: do not trust it. Since 0.9.0 `ingest` keeps only its type and subtype, in lower case,
|
|
925
|
+
without parameters, for every caller, your own routes included: a value that is not a
|
|
926
|
+
type/subtype of RFC 9110 token characters of at most 127 characters, or that holds anything but
|
|
927
|
+
printable ASCII anywhere (a line break, U+202E or another text-direction control, a tag
|
|
928
|
+
character), is stored as `application/octet-stream`. Before, it was only trimmed, so a line
|
|
929
|
+
break, a direction override or 8 KB of it reached the metadata, the summaries, `ingest.finish`
|
|
930
|
+
and your logs. It is never sent to the model.
|
|
931
|
+
- **A path is read as it is given.** `ingest({ source: { path } })` (new in 0.9.0) reads the file
|
|
932
|
+
at that path, which can be any file the process may read: never build it from what a client sends.
|
|
933
|
+
A link at the path is refused, not followed, and so is a folder, a pipe or a device. The file is
|
|
934
|
+
opened without following a link at its name (not on Windows), and read once, at most
|
|
935
|
+
`maxUploadBytes + 1` bytes, so a file that grew after it was looked at is refused. A relative
|
|
936
|
+
path is made absolute once, when `ingest` is called: a change of the working folder before the
|
|
937
|
+
ingest's turn, or the working folder of an isolated reader's child process, does not change which
|
|
938
|
+
file is read. It is not normalized as text: a `..` after a link, and a `/` at the end, are the
|
|
939
|
+
system's to resolve, as they are in an absolute path. So `fs.promises.realpath` (or
|
|
940
|
+
`fs.realpathSync.native`) of the path, relative or absolute, names the file the library reads.
|
|
941
|
+
`path.resolve`, `fs.realpath` and `fs.realpathSync` remove them as text first, and can name
|
|
942
|
+
another file: do not check a path with them. The library never writes, moves or deletes it.
|
|
767
943
|
- **Text is cleaned.** Terminal escape sequences are removed, and so are control characters other
|
|
768
944
|
than tab and line breaks, C1 (U+0080-U+009F) included, and the other characters listed in
|
|
769
945
|
[Characters that show nothing](#characters-that-show-nothing). An `ESC ]` without its end on the
|
|
@@ -792,6 +968,51 @@ server that memory is the server's heap. Choose the isolation with care:
|
|
|
792
968
|
Formats, optional dependencies and the messages of refused files are in [PDFs](./guides/pdfs.md);
|
|
793
969
|
the upload routes in [Web app](./guides/web-app.md).
|
|
794
970
|
|
|
971
|
+
### Files the upload handler writes
|
|
972
|
+
|
|
973
|
+
New in 0.9.0. `createDocumentHandlers` writes no file unless it is asked to: by the `path` that
|
|
974
|
+
`prepareUpload` returns, or by `uploadSpool`.
|
|
975
|
+
|
|
976
|
+
- **Where.** The body of an upload goes to the absolute path `prepareUpload` returns, in your app's
|
|
977
|
+
folder, or to `<directory>/llf-upload-<random UUID>` of the spool (`os.tmpdir()` by default).
|
|
978
|
+
Nothing else is written by the handler. Build the path from IDs your server chose and checked (the
|
|
979
|
+
user, the chat, the upload), never from the file's name or another value the client sent.
|
|
980
|
+
- **Modes.** A file is made with mode `0o600`, minus the umask of the process: only the server's
|
|
981
|
+
user can read it. `uploadFileMode` changes it for the files of `prepareUpload` (`0o640` lets a
|
|
982
|
+
group read them); the spool's files are always `0o600`. The handler makes no folder: the folders,
|
|
983
|
+
and their modes, are yours.
|
|
984
|
+
- **Never through a link, never over a file.** Each file is opened with `"wx"` (`O_CREAT` with
|
|
985
|
+
`O_EXCL`): when anything is already at its name, a file or a link (even one that points nowhere),
|
|
986
|
+
the open fails and nothing is written. That upload is answered `409`, and what was there stays as
|
|
987
|
+
it is: it is never written through, truncated or removed. So the handler never follows a link at
|
|
988
|
+
the file's name. A link in a folder on the way is followed, except where `fileOriginals` would
|
|
989
|
+
not read through it: when the path of `prepareUpload` ends with the ref of its `original`
|
|
990
|
+
(`<root>/<ref>`), a link in a folder below that root is refused before anything is written
|
|
991
|
+
(`500` "Upload failed (ELOOP)."). Keep the folders of the uploads, and the spool, writable only
|
|
992
|
+
by your server.
|
|
993
|
+
- **Removed when they are not needed.** A file of the spool is removed when its ingest ends,
|
|
994
|
+
whatever the end. A file of `prepareUpload` is removed when its upload fails (too large, stopped,
|
|
995
|
+
a client that left, a full disk, a disk that does not answer, a file refused, an ID taken,
|
|
996
|
+
`onUploaded` throwing), and is yours once its document is stored: the library never removes it,
|
|
997
|
+
even when the document is deleted or expires.
|
|
998
|
+
A removal that fails (a file another process holds open, on Windows) is tried three times, then
|
|
999
|
+
left. A server that stops in the middle of an upload leaves its file. Empty the spool when the
|
|
1000
|
+
server starts: give each server process a spool folder of its own (an `emptyDir` or
|
|
1001
|
+
`os.hostname()` gives one a pod: [Deployment](./guides/deployment.md#uploads-and-ingest) says how
|
|
1002
|
+
to give one to each process), and let each empty only its own. Never empty a folder that several
|
|
1003
|
+
servers share: it holds the uploads the others are still ingesting. The partial file of `prepareUpload` stays until a job of yours removes it
|
|
1004
|
+
([Stopping a server](./guides/deployment.md#stopping-a-server)).
|
|
1005
|
+
- **Bounded.** A body written to a file has the same limits as one in memory: `maxUploadBytes` and
|
|
1006
|
+
`uploadIdleTimeoutMs`, which also bounds each call to the disk (a volume that stops answering is
|
|
1007
|
+
a `500` "Upload failed (ETIMEDOUT).", and gives the upload's room back). `maxSpooledUploadBytes`
|
|
1008
|
+
(ten times `maxUploadBytes`) bounds the spool's files at once. A full disk (`ENOSPC`, `EDQUOT`) is
|
|
1009
|
+
answered `503`, and the file removed. The files of `prepareUpload` are bounded by your disk only.
|
|
1010
|
+
- **Not sent.** The path of a file and its ref are never in an answer, an event, a summary, or a
|
|
1011
|
+
request to a model. A system error that names a path, such as Node's `ENOENT` for a folder that is
|
|
1012
|
+
not there, is answered with its code only: `"Upload failed (ENOENT)."`.
|
|
1013
|
+
- **Not on a memory-backed volume.** A spool on a tmpfs (a Kubernetes `emptyDir` with
|
|
1014
|
+
`medium: Memory`) counts against the pod's memory: [Deployment](./guides/deployment.md#uploads-and-ingest).
|
|
1015
|
+
|
|
795
1016
|
## Regex search
|
|
796
1017
|
|
|
797
1018
|
The model writes the regex, and the model reads untrusted file text, so a file can ask for a slow
|
|
@@ -900,19 +1121,31 @@ Waits, retries and deadlines are explained in
|
|
|
900
1121
|
## Storage errors
|
|
901
1122
|
|
|
902
1123
|
A store's own errors can name paths and hosts. Reads reach users and models only as
|
|
903
|
-
`DocumentStoreError`, whose message names neither, with the original error as its `cause`.
|
|
904
|
-
|
|
905
|
-
`
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
1124
|
+
`DocumentStoreError`, whose message names neither, with the original error as its `cause`. Since
|
|
1125
|
+
0.9.0 so do writes: a store that fails in `ingest`, `files.delete` or `files.sweep` is a
|
|
1126
|
+
`DocumentStoreError` of operation `"write"`, `"delete"` or `"sweep"`, with status 503, and the
|
|
1127
|
+
upload handler answers it 503 with that message. (Before, writes were passed on as they came, and a
|
|
1128
|
+
write error with a `status` of its own was answered with its own message, which could name a host.)
|
|
1129
|
+
The handler also adds `Retry-After: 5`.
|
|
1130
|
+
|
|
1131
|
+
S12, closed in 0.9.0: the upload handler answered any `Error` without a status `422` with its
|
|
1132
|
+
message, as a file that could not be read. A `LargeFiles` of your own whose `ingest` threw a
|
|
1133
|
+
database driver's error (`MongoServerSelectionError: connect ECONNREFUSED mongo-prod.internal…:27017`)
|
|
1134
|
+
sent that host to the browser. Now only an `IngestError` is a `422` with its message, and an
|
|
1135
|
+
`IngestError` of code `"unreadable"` is told in fixed words: its message is a parser's, or that of
|
|
1136
|
+
an extractor of your own, which may name a host (an OCR service's). Any other error without a
|
|
1137
|
+
status is a `500` `{ "error": "Upload failed." }`, a system error adds only its code, and a model's
|
|
1138
|
+
error is told by its kind, never in the provider's words. The rule is
|
|
1139
|
+
`uploadAnswerOf` from `llm-large-files/node`: answer with it in a route of your own around
|
|
1140
|
+
`files.ingest` ([Web app](./guides/web-app.md#filesingest-and-its-errors)). An `ingest` of your own
|
|
1141
|
+
that refuses a file throws `new IngestError(code, message)`, as `ingestInProcess` in
|
|
1142
|
+
[Deployment](./guides/deployment.md#uploads-and-ingest) does. An error with a `status` of its own
|
|
1143
|
+
still keeps its message: give such errors words for users. Pass the `DocumentStoreError` on as it
|
|
1144
|
+
is, and log its `cause` yourself, since the handlers log nothing: see
|
|
1145
|
+
[Storage](./guides/storage.md#errors) and the
|
|
1146
|
+
[node reference](./reference/node.md#how-errors-are-answered). What to show a user, in your own
|
|
1147
|
+
words and language, is `errorInfoOf`'s job: see
|
|
1148
|
+
[Errors and reliability](./guides/errors-and-reliability.md#your-users-words-errorinfoof).
|
|
916
1149
|
|
|
917
1150
|
## What the library logs
|
|
918
1151
|
|
|
@@ -960,7 +1193,8 @@ What you log is your choice, and some of what the library hands you holds user d
|
|
|
960
1193
|
browser ([Prompt injection](#prompt-injection)).
|
|
961
1194
|
- Set `ttlMs` to your data policy, run `sweep` on a schedule, and make your deletion rules cover
|
|
962
1195
|
stored `toolTurns` ([Storage and retention](#storage-and-retention)).
|
|
963
|
-
- Keep the store on encrypted storage.
|
|
1196
|
+
- Keep the store on encrypted storage. With `FileDocumentStore`, keep the default file modes, or
|
|
1197
|
+
`0o640` and `0o750` for one group ([Storage and retention](#storage-and-retention)).
|
|
964
1198
|
- Turn on `keepOriginals` only if you want `view_pages`, and knowing what it keeps
|
|
965
1199
|
([Original files](#original-files)).
|
|
966
1200
|
- Set `limits` and a deadline, and log `usage` for failed questions too
|
|
@@ -968,6 +1202,9 @@ What you log is your choice, and some of what the library hands you holds user d
|
|
|
968
1202
|
- Keep the bound on uploads held at once (`maxPendingUploadBytes` of the handlers, or your own in a
|
|
969
1203
|
route of your own), limit uploads per user, and set the limit of any body parser in front of the
|
|
970
1204
|
handlers ([Uploads](#uploads)).
|
|
1205
|
+
- With `uploadSpool` or the `path` of `prepareUpload`, keep those folders writable only by your
|
|
1206
|
+
server, on a disk rather than a tmpfs, and build each path from IDs your server chose
|
|
1207
|
+
([Files the upload handler writes](#files-the-upload-handler-writes)).
|
|
971
1208
|
- Keep the limits on one file (`maxTextBytes`, `maxPdfPages`, `ingestTimeoutMs`) as low as your
|
|
972
1209
|
users allow ([Uploads](#uploads)).
|
|
973
1210
|
- For untrusted uploads, parse them in a process of their own with a memory limit, not in a worker
|