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
|
@@ -0,0 +1,311 @@
|
|
|
1
|
+
# `llm-large-files/testing`
|
|
2
|
+
|
|
3
|
+
New in 0.9.0. This page lists every export of `llm-large-files/testing`: the fakes that the
|
|
4
|
+
library's own tests use, for the tests of your application. A scripted model, a `fetch` for the
|
|
5
|
+
real adapters, test documents, and the checks of a store of your own. How to write the tests is in
|
|
6
|
+
the guide [Testing an integration](../guides/testing.md).
|
|
7
|
+
|
|
8
|
+
```ts
|
|
9
|
+
import { calls, documentStoreChecks, fakeFetch, fakeModel, httpError, says, serviceLog, sse, utf8 } from "llm-large-files/testing";
|
|
10
|
+
import type {
|
|
11
|
+
DocumentStoreCheck, DocumentStoreChecksOptions, FakeFetch, FakeModel, FakeReply, Scripted, Sent, StoreCheckEnvironment, StoreCheckPart,
|
|
12
|
+
} from "llm-large-files/testing";
|
|
13
|
+
// What the signatures below use from the main entry:
|
|
14
|
+
import type { DocumentScope, DocumentStore, GenerateText, ModelAdapter, ModelCapabilities, ModelReply, ModelRequest, SendHooks, ToolCall } from "llm-large-files";
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
The entry point depends on no test framework: it works with Vitest, Jest or `node:test`. It is
|
|
18
|
+
code to import in tests, not in your server, but it does nothing when it is loaded.
|
|
19
|
+
|
|
20
|
+
## Contents
|
|
21
|
+
|
|
22
|
+
- [A scripted model](#a-scripted-model): [`fakeModel()`](#fakemodelscript-options),
|
|
23
|
+
[`says()`](#saystext-more), [`calls()`](#callslist-more), [`httpError()`](#httperrorstatus-message-retryafterms),
|
|
24
|
+
[`Scripted`](#scripted), [`FakeModel`](#fakemodel)
|
|
25
|
+
- [A fetch for the real adapters](#a-fetch-for-the-real-adapters): [`fakeFetch()`](#fakefetchreplies),
|
|
26
|
+
[`sse()`](#sseevents), [`FakeFetch`](#fakefetch), [`FakeReply`](#fakereply), [`Sent`](#sent)
|
|
27
|
+
- [Test documents](#test-documents): [`utf8()`](#utf8text), [`serviceLog()`](#serviceloglines)
|
|
28
|
+
- [Checking a store](#checking-a-store): [`documentStoreChecks()`](#documentstorechecksoptions),
|
|
29
|
+
[`DocumentStoreChecksOptions`](#documentstorechecksoptions-1), [`StoreCheckEnvironment`](#storecheckenvironment),
|
|
30
|
+
[`DocumentStoreCheck`](#documentstorecheck)
|
|
31
|
+
|
|
32
|
+
## A scripted model
|
|
33
|
+
|
|
34
|
+
### fakeModel(script, options?)
|
|
35
|
+
|
|
36
|
+
```ts
|
|
37
|
+
declare function fakeModel(
|
|
38
|
+
script: Scripted[],
|
|
39
|
+
options?: Partial<ModelCapabilities> & { id?: string; generateText?: GenerateText },
|
|
40
|
+
): FakeModel;
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
A [`ModelAdapter`](./ask.md#modeladapter) that answers each request with the next entry of
|
|
44
|
+
`script`. The last entry repeats, so a script of one entry answers every request the same way.
|
|
45
|
+
|
|
46
|
+
| Entry | What `send` does |
|
|
47
|
+
|---|---|
|
|
48
|
+
| A reply (`Partial<ModelReply>`) | Returns it, with `text` `""` and `calls` `[]` where it has none. |
|
|
49
|
+
| An `Error` | Throws it, as an adapter throws a request that failed. |
|
|
50
|
+
| A function `(request, call, hooks)` | Calls it with the request, its number (1 for the first) and the hooks, and uses what it returns (or resolves to) as above. |
|
|
51
|
+
|
|
52
|
+
When the loop passes `hooks.onText` (your code set `onText` or `streamText`, or uses `askStream`),
|
|
53
|
+
the text of a reply is passed to it in two pieces, as a stream would arrive.
|
|
54
|
+
|
|
55
|
+
| Option | Default | What it does |
|
|
56
|
+
|---|---|---|
|
|
57
|
+
| `id` | `"fake-model"` | The model's `id`: its name in `usage.byModel`. Give two fakes different ones. |
|
|
58
|
+
| `generateText` | None | The part reader of whole-file reads. Without it, `analyze_whole_document` is not offered. |
|
|
59
|
+
| Any field of [`ModelCapabilities`](./ask.md#modelcapabilities) | `toolFiles: "inside"` (as Gemini's), `pageTokens: 500` | The capabilities: `toolFiles: "none"` for a model that takes no files (no `view_pages`), `appendOnly: true` for a model the loop treats as Claude, `contextTokens` and `maxRequestBytes` to make the budget of a question fit a model. |
|
|
60
|
+
|
|
61
|
+
### says(text, more?)
|
|
62
|
+
|
|
63
|
+
```ts
|
|
64
|
+
declare function says(text: string, more?: Partial<ModelReply>): Partial<ModelReply>;
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
A reply of text. `more` adds fields: `usage`, `finishReason`, `blockReason`.
|
|
68
|
+
|
|
69
|
+
### calls(list, more?)
|
|
70
|
+
|
|
71
|
+
```ts
|
|
72
|
+
declare function calls(list: ToolCall[], more?: Partial<ModelReply>): Partial<ModelReply>;
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
A reply that calls tools. Its `raw` is `{ own: [the names of the tools] }`: the turn in a
|
|
76
|
+
"provider's own form", so that a test can tell where the loop gives that form back to the model.
|
|
77
|
+
|
|
78
|
+
### httpError(status, message?, retryAfterMs?)
|
|
79
|
+
|
|
80
|
+
```ts
|
|
81
|
+
declare function httpError(status: number, message?: string, retryAfterMs?: number): Error;
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
An `Error` with a `status` (and a `retryAfterMs`, when given), as an adapter of your own may throw
|
|
85
|
+
for an HTTP status. The message is `HTTP <status>` by default. The loop sends a request again
|
|
86
|
+
after a status worth trying again (408, 409, 429 or 5xx), and waits `retryAfterMs` when it is
|
|
87
|
+
there. It has no `kind`: its status, message and a `code` you add give one, as
|
|
88
|
+
[`classifyModelError`](./errors-and-retries.md#classifymodelerrorstatus-message-signals) does, and
|
|
89
|
+
a 429 of a quota (`code: "insufficient_quota"`) is not sent again (since 0.9.0). A 413, or a 429
|
|
90
|
+
"Request too large … tokens per min", is a `"context_overflow"`: the loop makes the request smaller
|
|
91
|
+
and sends it again, or plans the documents again, as for a `ModelRequestError` of that kind (since
|
|
92
|
+
0.9.0). For the error classes of the library, throw
|
|
93
|
+
[`ModelRequestError`](./errors-and-retries.md#modelrequesterror).
|
|
94
|
+
|
|
95
|
+
### Scripted
|
|
96
|
+
|
|
97
|
+
```ts
|
|
98
|
+
type Scripted =
|
|
99
|
+
| Partial<ModelReply>
|
|
100
|
+
| Error
|
|
101
|
+
| ((request: ModelRequest, call: number, hooks: SendHooks) => Partial<ModelReply> | Error | Promise<Partial<ModelReply> | Error>);
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
One entry of a script.
|
|
105
|
+
|
|
106
|
+
### FakeModel
|
|
107
|
+
|
|
108
|
+
```ts
|
|
109
|
+
type FakeModel = ModelAdapter & { readonly requests: ModelRequest[] };
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
`requests` holds every request `send` was called with, in order, failed attempts and requests
|
|
113
|
+
sent again included. Each is a copy of the request as it was sent, with a copy of its list of
|
|
114
|
+
messages, so that what the loop does later does not change it.
|
|
115
|
+
|
|
116
|
+
## A fetch for the real adapters
|
|
117
|
+
|
|
118
|
+
### fakeFetch(replies)
|
|
119
|
+
|
|
120
|
+
```ts
|
|
121
|
+
declare function fakeFetch(replies: FakeReply[]): FakeFetch;
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
A `fetch` that answers from a script, without the network. Give its `fetch` to an adapter: the
|
|
125
|
+
`fetch` option of `gemini()`, `askGemini()`, `openAiCompatible()` or `anthropic()`. Each request
|
|
126
|
+
gets the next reply, and the last one repeats. A `Response` is answered with a copy (its body can
|
|
127
|
+
be read only once), and a function is called with the request and its number (1 for the first).
|
|
128
|
+
Every request is recorded in `sent`. It throws a `TypeError` when `replies` is empty.
|
|
129
|
+
|
|
130
|
+
### sse(events)
|
|
131
|
+
|
|
132
|
+
```ts
|
|
133
|
+
declare function sse(events: unknown[]): Response;
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
A streamed reply: Server-Sent Events, one `data:` line for each event, each object written as JSON.
|
|
137
|
+
A string is sent as it is (`"[DONE]"`). The `Content-Type` is `text/event-stream`.
|
|
138
|
+
|
|
139
|
+
### FakeFetch
|
|
140
|
+
|
|
141
|
+
```ts
|
|
142
|
+
type FakeFetch = { fetch: typeof globalThis.fetch; readonly sent: Sent[] };
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
### FakeReply
|
|
146
|
+
|
|
147
|
+
```ts
|
|
148
|
+
type FakeReply = Response | ((sent: Sent, call: number) => Response | Promise<Response>);
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
### Sent
|
|
152
|
+
|
|
153
|
+
```ts
|
|
154
|
+
type Sent = { url: string; method: string; headers: Record<string, string>; body: Record<string, any> };
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
A request as the adapter sent it. `headers` keep the names the adapter wrote (`Authorization`,
|
|
158
|
+
`x-api-key`); headers given as a `Headers` object have their names in lower case. `body` is the
|
|
159
|
+
body parsed as JSON, typed loosely so that a test reads into it without a cast, and `{}` when
|
|
160
|
+
there is no body or it is not JSON.
|
|
161
|
+
|
|
162
|
+
## Test documents
|
|
163
|
+
|
|
164
|
+
### utf8(text)
|
|
165
|
+
|
|
166
|
+
```ts
|
|
167
|
+
declare function utf8(text: string): Uint8Array;
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
The bytes of `text` in UTF-8, as `ingest` takes them.
|
|
171
|
+
|
|
172
|
+
### serviceLog(lines)
|
|
173
|
+
|
|
174
|
+
```ts
|
|
175
|
+
declare function serviceLog(lines: number): string;
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
A service log of `lines` lines of about 90 characters, the same on every call: an `ERROR` line
|
|
179
|
+
every 97 lines (97, 194, …), a `WARN` line every 13, and `INFO` lines in between. 400 lines hold 4
|
|
180
|
+
errors; 5,000 lines, about 440 KB, hold 51.
|
|
181
|
+
|
|
182
|
+
## Checking a store
|
|
183
|
+
|
|
184
|
+
### documentStoreChecks(options)
|
|
185
|
+
|
|
186
|
+
```ts
|
|
187
|
+
declare function documentStoreChecks(options: DocumentStoreChecksOptions): DocumentStoreCheck[];
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
New in 0.9.0. The checks a [`DocumentStore`](./library.md#documentstore) must pass, as a list: each
|
|
191
|
+
has a name, a time limit, and a `run` that rejects with an `Error` saying what the store did wrong.
|
|
192
|
+
Give each to your test framework:
|
|
193
|
+
|
|
194
|
+
```ts
|
|
195
|
+
// blob-document-store.test.ts (Vitest; Jest takes the same line with `test`)
|
|
196
|
+
import { it } from "vitest";
|
|
197
|
+
import { documentStoreChecks } from "llm-large-files/testing";
|
|
198
|
+
import { BlobDocumentStore } from "./blob-document-store.js";
|
|
199
|
+
import { blobsFor, recordsFor, dropStorage } from "./test-storage.js"; // your own: a bucket prefix and a collection per name
|
|
200
|
+
|
|
201
|
+
const checks = documentStoreChecks({
|
|
202
|
+
create: ({ storage }) => new BlobDocumentStore(blobsFor(storage), recordsFor(storage)),
|
|
203
|
+
dispose: (_store, { storage }) => dropStorage(storage),
|
|
204
|
+
});
|
|
205
|
+
for (const check of checks) it(check.name, check.run, check.timeoutMs);
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
With `node:test`: `for (const check of checks) test(check.name, { timeout: check.timeoutMs }, check.run);`.
|
|
209
|
+
`run` takes no argument, so Jest does not wait for a `done` callback.
|
|
210
|
+
|
|
211
|
+
What the checks cover:
|
|
212
|
+
|
|
213
|
+
- `put` and `get` keep the text exactly (CRLF, Hebrew, an emoji, a byte-order mark inside), and
|
|
214
|
+
copy the metadata on the way in and out.
|
|
215
|
+
- An unknown ID gives `undefined`, and `delete` gives `false`.
|
|
216
|
+
- Fields of the metadata the store does not know are kept: those a later version adds.
|
|
217
|
+
- A field left out comes back absent, never `null`: `owner`, `scope` and others.
|
|
218
|
+
- An expired document is read without an error: as `undefined`, or as it was, for the library to
|
|
219
|
+
refuse. After `sweep`, it is gone.
|
|
220
|
+
- `delete` removes every part, a second one gives `false`, and two at once do not fail. The original
|
|
221
|
+
and the index of a document written only in part are removed too.
|
|
222
|
+
- `readTextRange` gives UTF-8 bytes across characters, fewer or none past the end, and `undefined`
|
|
223
|
+
after a delete ("ranges").
|
|
224
|
+
- The line index is kept byte for byte, as a copy, the last one written ("index").
|
|
225
|
+
- The original is kept byte for byte, as a copy ("originals").
|
|
226
|
+
- `sweep(now)` removes the documents expired at `now`, not at the time of day, every part of them
|
|
227
|
+
(the text, the original and the index), and no other ("sweep"). An original or an index the sweep
|
|
228
|
+
left shows when a document is put again under the ID. With `isolatesScopes`, a sweep with a scope
|
|
229
|
+
removes only that scope's.
|
|
230
|
+
- The IDs an application may choose ([`APP_DOCUMENT_ID_PATTERN`](./library.md#app_document_id_pattern)).
|
|
231
|
+
- Every method takes a context. Without `requiresScope`, also no context, `{}`, and an owner alone.
|
|
232
|
+
- With `requiresScope`: a call without a scope throws a
|
|
233
|
+
[`DocumentScopeError`](./library.md#documentscopeerror) of code `"scope_required"`, and `sweep`
|
|
234
|
+
without one sweeps every scope.
|
|
235
|
+
- With `isolatesScopes`: one ID in two scopes is two documents on every method, so the context
|
|
236
|
+
reaches every call, and a document of one scope is missing from another scope and from no scope.
|
|
237
|
+
A wrapper that forwards only the ID fails here.
|
|
238
|
+
- Two stores over one storage, as two servers, see each other's writes and deletes ("shared").
|
|
239
|
+
The first store reads the document, the second deletes it, then puts a new one under the ID: the
|
|
240
|
+
first gives `undefined`, then the new text and metadata. A store that keeps what it read, on each
|
|
241
|
+
server, fails here.
|
|
242
|
+
- The library over the store, with `wholeDocumentBytes: 0` (every document read by range), gives
|
|
243
|
+
the same metadata, prepared blocks, search and read results, original and delete as over
|
|
244
|
+
`MemoryDocumentStore`.
|
|
245
|
+
|
|
246
|
+
Each check makes its own store with `create`, over storage of its own (`environment.storage`), and
|
|
247
|
+
gives every store it made to `dispose` at the end. The checks pass a scope (and an owner) on most
|
|
248
|
+
calls. New checks are added only in minor versions, so a patch release never fails a store that
|
|
249
|
+
passed.
|
|
250
|
+
|
|
251
|
+
### `DocumentStoreChecksOptions`
|
|
252
|
+
|
|
253
|
+
```ts
|
|
254
|
+
type StoreCheckPart = "originals" | "ranges" | "index" | "sweep" | "shared";
|
|
255
|
+
|
|
256
|
+
type DocumentStoreChecksOptions = {
|
|
257
|
+
create(environment: StoreCheckEnvironment): DocumentStore | Promise<DocumentStore>;
|
|
258
|
+
dispose?(store: DocumentStore, environment: StoreCheckEnvironment): void | Promise<void>;
|
|
259
|
+
isolatesScopes?: boolean;
|
|
260
|
+
requiresScope?: boolean;
|
|
261
|
+
scopeDepth?: number;
|
|
262
|
+
skip?: readonly StoreCheckPart[];
|
|
263
|
+
};
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
| Field | Default | What it does |
|
|
267
|
+
|---|---|---|
|
|
268
|
+
| `create` | Required | Makes a new store over new, empty storage named by `environment.storage`. Called again with the same name, it gives a second store over the same storage, as a second server opens it. Give the store `environment.now` as its clock, if it has one. |
|
|
269
|
+
| `dispose` | None | Called at the end of each check for each store it made: close it, and remove its storage. |
|
|
270
|
+
| `isolatesScopes` | `false` | The store keeps the documents of each scope apart, as `MemoryDocumentStore` and a scoped `FileDocumentStore` do. Adds the checks of scopes. Leave it false for a store that ignores the context: the library then keeps scopes apart by `meta.scope`. |
|
|
271
|
+
| `requiresScope` | `false` | The store refuses a call without a scope, as a `FileDocumentStore` with `scopes` does. The checks then always pass one, and one more checks the refusal. |
|
|
272
|
+
| `scopeDepth` | `2` | How many segments the scopes of the checks have, from 1 to 8: `["user-1", "chat-a"]` with 2. |
|
|
273
|
+
| `skip` | `[]` | The parts the store does not have: `"originals"` (`putOriginal` and `getOriginal`), `"ranges"` (`readTextRange`), `"index"` (`putIndex` and `getIndex`), `"sweep"`, and `"shared"` for storage two stores cannot share (`MemoryDocumentStore`). A check of a part the store lacks, not skipped, fails and says to skip it. |
|
|
274
|
+
|
|
275
|
+
`documentStoreChecks` throws a `TypeError` without `create`, or for a `scopeDepth` outside 1 to 8.
|
|
276
|
+
|
|
277
|
+
### `StoreCheckEnvironment`
|
|
278
|
+
|
|
279
|
+
```ts
|
|
280
|
+
type StoreCheckEnvironment = {
|
|
281
|
+
readonly now: () => Date;
|
|
282
|
+
readonly storage: string;
|
|
283
|
+
readonly scopes: readonly DocumentScope[];
|
|
284
|
+
};
|
|
285
|
+
```
|
|
286
|
+
|
|
287
|
+
| Field | What it holds |
|
|
288
|
+
|---|---|
|
|
289
|
+
| `now` | The clock of the check: the time it started, rounded down to a second. A check may move it forward. |
|
|
290
|
+
| `storage` | A name for the storage of the store, of lowercase letters, digits and `-`, new for each check: a folder, a key prefix, a collection. |
|
|
291
|
+
| `scopes` | The two scopes the checks use, `scopeDepth` lowercase segments each, which pass `isSafeScopeSegment`. Make their place first when your store needs it, as for the folders of a `FileDocumentStore` without `createScopeFolders`. |
|
|
292
|
+
|
|
293
|
+
### `DocumentStoreCheck`
|
|
294
|
+
|
|
295
|
+
```ts
|
|
296
|
+
type DocumentStoreCheck = {
|
|
297
|
+
readonly name: string;
|
|
298
|
+
readonly timeoutMs: number;
|
|
299
|
+
readonly run: () => Promise<void>;
|
|
300
|
+
};
|
|
301
|
+
```
|
|
302
|
+
|
|
303
|
+
| Field | What it holds |
|
|
304
|
+
|---|---|
|
|
305
|
+
| `name` | What the store must do, as a sentence: the name of the test. |
|
|
306
|
+
| `timeoutMs` | The time the check may take: 10 s, and 30 s for the check through the library. |
|
|
307
|
+
| `run` | Runs the check. It resolves when the store passes, and rejects with an `Error` whose message starts "The store failed the check:" and says what was expected and what came back. When the store throws, the message names the call and its arguments, such as `readTextRange("check-delete", 0, 4, {…})`, and the error the store threw is its `cause`. An error that `create` or `dispose` throws is named after them, and any other after the check. |
|
|
308
|
+
|
|
309
|
+
How the library's own tests run them on `MemoryDocumentStore` and `FileDocumentStore`, and what to
|
|
310
|
+
check by hand besides (a database that is down, writes that fail), is in
|
|
311
|
+
[Storage](../guides/storage.md#checking-your-store).
|
package/docs/reference/tools.md
CHANGED
|
@@ -123,7 +123,7 @@ storage (EIO). Try the call again.", and a store that could not be read gives th
|
|
|
123
123
|
| `document_id is required.` | `document_id` is missing or blank. |
|
|
124
124
|
| `Unknown document_id ID. Use one of: doc_… ("app.log"), ….` | `search_document`, `read_document`, `get_document_overview`, `analyze_whole_document`: the ID is not one of the tool set's documents. Up to 20 usable IDs are listed, each with its file name, and `…` when there are more. |
|
|
125
125
|
| `Unknown document_id ID. No attached file is read with this tool in this conversation.` | `search_document`, `read_document`, `get_document_overview`, `analyze_whole_document`: the tool set has no document for these tools. |
|
|
126
|
-
| `ID is no longer available on the server. Ask the user to attach the file again.` | The document was deleted or expired after the request was planned. |
|
|
126
|
+
| `ID is no longer available on the server. Ask the user to attach the file again.` | The document was deleted or expired after the request was planned, or its text went while a search or read went through it. Since 0.9.0 the call's hooks are told (`ToolCallHooks.onDocumentGone`), and `askWithFiles` gives a `"document_gone"` notice. |
|
|
127
127
|
| `Unknown tool “NAME”.` | The name is not a tool of this tool set. The name is cut at 64 characters, never in the middle of a character. Until 0.6.0: `Unknown tool NAME.`, with the whole name. |
|
|
128
128
|
|
|
129
129
|
**Size.** One result is at most `toolResultCharacters` characters of JSON (a field of
|
|
@@ -658,7 +658,8 @@ The description the model sees:
|
|
|
658
658
|
|
|
659
659
|
The pages are cut out of the kept original into one small PDF, and returned as a file next to a
|
|
660
660
|
short JSON result. Only [`ToolSet.executeWithFiles`](#toolset) returns the file; `execute` answers
|
|
661
|
-
with an error.
|
|
661
|
+
with an error. Since 0.9.0 the looks of a library take their turns (`maxConcurrentPageLooks`, 1 by
|
|
662
|
+
default): a look waits for its turn before the original is loaded. Pages are left off the end until the file fits the call's `maxFileBytes`. Cutting
|
|
662
663
|
needs the optional dependency `@cantoo/pdf-lib`. How pages are cut and counted, and what they cost,
|
|
663
664
|
is in [PDFs](../guides/pdfs.md).
|
|
664
665
|
|
|
@@ -698,7 +699,10 @@ Errors, besides those of [every tool](#rules-for-every-tool):
|
|
|
698
699
|
| `NAME has N pages. There is no page F.` | `from_page` is past the last page. |
|
|
699
700
|
| `No more pages can be looked at for this question. Answer from the pages and the text you already have.` | The call's `maxPages` is below 1. |
|
|
700
701
|
| `There is no room left in this request to show more pages. Answer from the pages and the text you already have.` | The call's `maxFileBytes` is below 1,024. |
|
|
701
|
-
| `The original file of NAME is no longer on the server, so its pages cannot be shown. Answer from the text.` | The store no longer has the original. |
|
|
702
|
+
| `The original file of NAME is no longer on the server, so its pages cannot be shown. Answer from the text.` | The store no longer has the original, or (0.9.0) what it gave back is not the file that was ingested: of another size or SHA-256; or a file your application keeps (`IngestInput.original`) is not there. When the document itself is gone too (deleted while the look waited for its turn), the call gives `ID is no longer available on the server. Ask the user to attach the file again.` instead, and its hooks are told (`ToolCallHooks.onDocumentGone`; since 0.9.0). |
|
|
703
|
+
| `NAME changed on the server since it was read. Attach it again to look at its pages.` | The file your application keeps for the document (`IngestInput.original`, read through `LargeFilesOptions.originals`) is no longer the one that was ingested: its size or SHA-256 differs. Its pages would not match the text. New in 0.9.0. |
|
|
704
|
+
| `The original file of NAME could not be read in time, so its pages cannot be shown. Answer from the text.` | The store's `getOriginal` did not answer within `ingestTimeoutMs` (2 minutes when that is 0). The look gave its turn back, so the next look runs. The read goes on in the background: give your store's `getOriginal` a time limit of its own. New in 0.9.0. |
|
|
705
|
+
| `NAME: The pages cannot be looked at right now; the server is busy. Answer from the text, or look again later.` | The look waited longer than `pageLookWaitMs` for its turn, or the call's `maxWaitMs` when that is less: other looks of the library held them all (`maxConcurrentPageLooks`, [library](./library.md#largefilesoptions)). It was not run, and its PDF was not loaded. The call's hooks are told (`ToolCallHooks.onPagesBusy`), and `askWithFiles` gives a `"page_looks_busy"` notice. New in 0.9.0. |
|
|
702
706
|
| `NAME: Page F is 2.1 MB as a file, more than the 1.5 MB that fit in this request.` | The first page alone does not fit `maxFileBytes`. |
|
|
703
707
|
| `NAME: ` and the reason | The file cannot be cut: it needs a password, is damaged, declares too many objects to cut in memory, has another page count than its text, has no page `from_page` (when its page count was not recorded), or `@cantoo/pdf-lib` is not installed. |
|
|
704
708
|
| `The pages could not be shown: this application does not pass files from tools to the model. Answer from the text.` | The call was run with `execute`, which has no place for a file. |
|
|
@@ -787,7 +791,9 @@ of this unless you write it.
|
|
|
787
791
|
- **Stand-ins.** When the request runs short of room, the results of older tool rounds are replaced
|
|
788
792
|
by the short stand-ins of [`stubResult`](./ask.md#stubresultcall-result-hadfiles), with
|
|
789
793
|
`"dropped": true`; when and how is in [How it works](../how-it-works.md), and nothing is replaced
|
|
790
|
-
when any model of the question is append-only (Claude).
|
|
794
|
+
during a question when any model of the question is append-only (Claude). Since 0.9.0 the
|
|
795
|
+
results of stored rounds are replaced before the first request when they make it too large in tokens,
|
|
796
|
+
for every model, and a result is never replaced by a stand-in as large as itself. A pointer whose result was replaced
|
|
791
797
|
becomes
|
|
792
798
|
`{ "document": "app.log", "dropped": true, "note": "Not run then: it repeated an earlier call whose result was later dropped from the conversation to make room. Call it again if you need it." }`.
|
|
793
799
|
So does every pointer of a stored round when a model of the question declares
|
|
@@ -987,6 +993,7 @@ type ToolCallLimits = {
|
|
|
987
993
|
maxPages?: number;
|
|
988
994
|
maxResultCharacters?: number;
|
|
989
995
|
maxRequests?: number;
|
|
996
|
+
maxWaitMs?: number; // new in 0.9.0
|
|
990
997
|
};
|
|
991
998
|
```
|
|
992
999
|
|
|
@@ -996,12 +1003,15 @@ type ToolCallLimits = {
|
|
|
996
1003
|
| `maxPages` | number | [`MAX_VIEW_PAGES`](#max_view_pages) (5) | `view_pages`: the most pages this call may show, never more than 5. Below 1 the call fails. Lower it as the pages already shown use up your token budget. |
|
|
997
1004
|
| `maxResultCharacters` | number | The tool's own cap ([`resultCharacters`](#toolset)) | The most characters of the JSON result, when that is less than the tool's cap: the room left in the request. The result is cut to it and says where to go on. Never below 600. `view_pages` ignores it. |
|
|
998
1005
|
| `maxRequests` | number | No limit | `analyze_whole_document`: the most model requests the read may make. It does not stop the read: a part the model refuses as too large is read as two halves only while this and `maxMapChunks` allow it (the parts, a merge and the answer must fit). New in 0.6.0. |
|
|
1006
|
+
| `maxWaitMs` | number | The library's `pageLookWaitMs` (30,000) | `view_pages`: the most milliseconds the look waits for its turn, when that is less than `pageLookWaitMs`. 0: it runs only when a turn is free now, and is told the server is busy if not. New in 0.9.0. |
|
|
999
1007
|
|
|
1000
1008
|
In `askWithFiles`, `maxFileBytes` is what is left of `maxRequestBytes` after the request so far
|
|
1001
1009
|
and a tenth kept free (256 KiB at least), times 3/4; `maxPages` is what is left of
|
|
1002
1010
|
`reservedTokens / 2 / pageTokens` pages for the question (60 with a `reservedTokens` of 64,000 and
|
|
1003
1011
|
Gemini's 530 tokens a page); `maxRequests`, when `limits.requests` is set, is the read's own
|
|
1004
|
-
requests and a share of what the question has left of it
|
|
1012
|
+
requests and a share of what the question has left of it; `maxWaitMs` is 0 for the looks of a
|
|
1013
|
+
tool turn after one of them was told the server is busy, so that a wait that ends busy comes once
|
|
1014
|
+
in a turn. See
|
|
1005
1015
|
[Your own adapter and loop](../guides/own-adapter-and-loop.md).
|
|
1006
1016
|
|
|
1007
1017
|
### `ToolFileLimits`
|