llm-large-files 0.4.0 → 0.5.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 +205 -0
- package/README.md +48 -8
- package/dist/anthropic/adapter.d.ts +106 -0
- package/dist/anthropic/adapter.d.ts.map +1 -0
- package/dist/anthropic/adapter.js +391 -0
- package/dist/anthropic/adapter.js.map +1 -0
- package/dist/anthropic/index.d.ts +3 -0
- package/dist/anthropic/index.d.ts.map +1 -0
- package/dist/anthropic/index.js +2 -0
- package/dist/anthropic/index.js.map +1 -0
- package/dist/ask.d.ts +144 -0
- package/dist/ask.d.ts.map +1 -0
- package/dist/ask.js +540 -0
- package/dist/ask.js.map +1 -0
- package/dist/budget.d.ts +23 -0
- package/dist/budget.d.ts.map +1 -0
- package/dist/budget.js +64 -0
- package/dist/budget.js.map +1 -0
- package/dist/cache.d.ts +11 -2
- package/dist/cache.d.ts.map +1 -1
- package/dist/cache.js +36 -7
- package/dist/cache.js.map +1 -1
- package/dist/errors.d.ts +21 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +44 -0
- package/dist/errors.js.map +1 -0
- package/dist/evidence.d.ts +40 -0
- package/dist/evidence.d.ts.map +1 -0
- package/dist/evidence.js +137 -0
- package/dist/evidence.js.map +1 -0
- package/dist/gemini/adapter.d.ts +50 -0
- package/dist/gemini/adapter.d.ts.map +1 -0
- package/dist/gemini/adapter.js +179 -0
- package/dist/gemini/adapter.js.map +1 -0
- package/dist/gemini/chat.d.ts +32 -71
- package/dist/gemini/chat.d.ts.map +1 -1
- package/dist/gemini/chat.js +38 -284
- package/dist/gemini/chat.js.map +1 -1
- package/dist/gemini/errors.d.ts +11 -0
- package/dist/gemini/errors.d.ts.map +1 -0
- package/dist/gemini/errors.js +14 -0
- package/dist/gemini/errors.js.map +1 -0
- package/dist/gemini/generate.d.ts +12 -13
- package/dist/gemini/generate.d.ts.map +1 -1
- package/dist/gemini/generate.js +62 -26
- package/dist/gemini/generate.js.map +1 -1
- package/dist/gemini/index.d.ts +3 -1
- package/dist/gemini/index.d.ts.map +1 -1
- package/dist/gemini/index.js +2 -1
- package/dist/gemini/index.js.map +1 -1
- package/dist/gemini/sse.d.ts +1 -2
- package/dist/gemini/sse.d.ts.map +1 -1
- package/dist/gemini/sse.js +1 -38
- package/dist/gemini/sse.js.map +1 -1
- package/dist/http.d.ts +20 -0
- package/dist/http.d.ts.map +1 -0
- package/dist/http.js +55 -0
- package/dist/http.js.map +1 -0
- package/dist/index.d.ts +14 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +12 -4
- package/dist/index.js.map +1 -1
- package/dist/library.d.ts +50 -5
- package/dist/library.d.ts.map +1 -1
- package/dist/library.js +56 -26
- package/dist/library.js.map +1 -1
- package/dist/map-reduce.d.ts +15 -0
- package/dist/map-reduce.d.ts.map +1 -1
- package/dist/map-reduce.js +58 -27
- package/dist/map-reduce.js.map +1 -1
- package/dist/model.d.ts +196 -0
- package/dist/model.d.ts.map +1 -0
- package/dist/model.js +41 -0
- package/dist/model.js.map +1 -0
- package/dist/openai/adapter.d.ts +85 -0
- package/dist/openai/adapter.d.ts.map +1 -0
- package/dist/openai/adapter.js +293 -0
- package/dist/openai/adapter.js.map +1 -0
- package/dist/openai/index.d.ts +3 -0
- package/dist/openai/index.d.ts.map +1 -0
- package/dist/openai/index.js +2 -0
- package/dist/openai/index.js.map +1 -0
- package/dist/output-limit.d.ts +30 -0
- package/dist/output-limit.d.ts.map +1 -0
- package/dist/output-limit.js +92 -0
- package/dist/output-limit.js.map +1 -0
- package/dist/plan.d.ts +20 -4
- package/dist/plan.d.ts.map +1 -1
- package/dist/plan.js +44 -7
- package/dist/plan.js.map +1 -1
- package/dist/ranged.d.ts +2 -2
- package/dist/ranged.d.ts.map +1 -1
- package/dist/ranged.js +11 -4
- package/dist/ranged.js.map +1 -1
- package/dist/retry.d.ts +54 -0
- package/dist/retry.d.ts.map +1 -0
- package/dist/retry.js +111 -0
- package/dist/retry.js.map +1 -0
- package/dist/search.d.ts +16 -2
- package/dist/search.d.ts.map +1 -1
- package/dist/search.js +109 -26
- package/dist/search.js.map +1 -1
- package/dist/sse.d.ts +3 -0
- package/dist/sse.d.ts.map +1 -0
- package/dist/sse.js +39 -0
- package/dist/sse.js.map +1 -0
- package/dist/stores.d.ts +5 -1
- package/dist/stores.d.ts.map +1 -1
- package/dist/stores.js +58 -15
- package/dist/stores.js.map +1 -1
- package/dist/text.d.ts +12 -1
- package/dist/text.d.ts.map +1 -1
- package/dist/text.js +43 -15
- package/dist/text.js.map +1 -1
- package/dist/tools.d.ts +23 -0
- package/dist/tools.d.ts.map +1 -1
- package/dist/tools.js +115 -35
- package/dist/tools.js.map +1 -1
- package/dist/types.d.ts +33 -3
- package/dist/types.d.ts.map +1 -1
- package/docs/api.md +253 -28
- package/docs/configuration.md +89 -10
- package/docs/getting-started.md +69 -16
- package/docs/how-it-works.md +46 -4
- package/docs/security.md +36 -3
- package/package.json +21 -3
- package/src/anthropic/adapter.ts +454 -0
- package/src/anthropic/index.ts +2 -0
- package/src/ask.ts +675 -0
- package/src/budget.ts +71 -0
- package/src/cache.ts +32 -8
- package/src/errors.ts +42 -0
- package/src/evidence.ts +162 -0
- package/src/gemini/adapter.ts +218 -0
- package/src/gemini/chat.ts +62 -333
- package/src/gemini/errors.ts +15 -0
- package/src/gemini/generate.ts +72 -30
- package/src/gemini/index.ts +3 -1
- package/src/gemini/sse.ts +1 -34
- package/src/http.ts +59 -0
- package/src/index.ts +34 -4
- package/src/library.ts +102 -29
- package/src/map-reduce.ts +52 -23
- package/src/model.ts +200 -0
- package/src/openai/adapter.ts +345 -0
- package/src/openai/index.ts +2 -0
- package/src/output-limit.ts +99 -0
- package/src/plan.ts +46 -7
- package/src/ranged.ts +13 -6
- package/src/retry.ts +132 -0
- package/src/search.ts +116 -25
- package/src/sse.ts +34 -0
- package/src/stores.ts +51 -15
- package/src/text.ts +39 -12
- package/src/tools.ts +127 -38
- package/src/types.ts +29 -3
package/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,211 @@ All notable changes to this package are recorded here. The format follows
|
|
|
4
4
|
[Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and versions follow
|
|
5
5
|
[semantic versioning](https://semver.org).
|
|
6
6
|
|
|
7
|
+
## [0.5.0] - 2026-11-13
|
|
8
|
+
|
|
9
|
+
One tool loop for every model. What `askGemini` did (plan the documents, offer the tools, run the
|
|
10
|
+
calls, end with an answer) is now `askWithFiles` in the core, and a model provider is an adapter.
|
|
11
|
+
Three come with the package: Gemini and Vertex AI, any OpenAI-compatible endpoint, and Claude. The
|
|
12
|
+
loop also learned to send a failed request again, to count what a question costs, and to keep a
|
|
13
|
+
growing request inside the model's window.
|
|
14
|
+
|
|
15
|
+
`askGemini` is still there and takes the options it took. Read **Changed** before you upgrade:
|
|
16
|
+
six of its points can need a change in your code.
|
|
17
|
+
|
|
18
|
+
### Added
|
|
19
|
+
|
|
20
|
+
- **`askWithFiles({ files, model, explorer?, documentIds, messages, … })`** in `llm-large-files`.
|
|
21
|
+
`model` and `explorer` are adapters, and need not be of the same provider: a cheap model behind
|
|
22
|
+
an OpenAI-compatible endpoint can explore while Gemini or Claude writes the answer.
|
|
23
|
+
- **Adapters.** `gemini(endpoint, options)` in `llm-large-files/gemini`,
|
|
24
|
+
`openAiCompatible({ baseUrl, apiKey, model })` in `llm-large-files/openai` (OpenAI, Azure OpenAI,
|
|
25
|
+
DeepSeek, LiteLLM, vLLM, Ollama, OpenRouter, Gemini's OpenAI layer), and
|
|
26
|
+
`anthropic({ apiKey, model })` in `llm-large-files/anthropic`. None needs an SDK. An adapter of
|
|
27
|
+
your own is an object with `id`, `capabilities`, `send` and, for whole-file reads,
|
|
28
|
+
`generateText`: see `ModelAdapter` in the API reference.
|
|
29
|
+
- **A request that failed is sent again.** HTTP 408, 409, 429, any 5xx, and a connection that
|
|
30
|
+
failed are retried up to three times, after a wait that doubles (at most 0.5, 1 and 2 seconds,
|
|
31
|
+
with jitter), or after the wait the provider asks for (`Retry-After`, `retry-after-ms`, and the
|
|
32
|
+
`retryDelay` of Gemini's `RetryInfo`). A request the model refused (400, 401, 403, 404) is not retried, and nothing is
|
|
33
|
+
retried once text of the reply was shown, or after the caller stopped the question. A wait the
|
|
34
|
+
provider asks for that is longer than `retry.maxDelayMs` (one minute) is not waited for. The
|
|
35
|
+
part reads of a whole-file read follow the same rule: a 429 was retried 0.2 ms later before.
|
|
36
|
+
Set it with `retry: { retries, baseDelayMs, maxDelayMs }`.
|
|
37
|
+
- **`usage` is the whole question.** `{ requests, inputTokens, cachedInputTokens, outputTokens,
|
|
38
|
+
thoughtTokens, byModel }`, summed over every request that was answered, the part reads of a
|
|
39
|
+
whole-file read included. In a live run of 0.4.0, 13,687 prompt tokens were billed and 6,398
|
|
40
|
+
reported.
|
|
41
|
+
- **Ceilings for one question**, in `limits`: `wholeDocumentReads` (1 for each document: the
|
|
42
|
+
model asked for the same file to be read whole six times in one probe), `inputTokens` and
|
|
43
|
+
`requests` (no more tools once they are reached, and a whole-file read that would pass them is
|
|
44
|
+
refused with the numbers), next to `toolTurns` and `callsPerTurn`.
|
|
45
|
+
- **The request is kept inside the window.** The loop counts the request as it grows. Before it
|
|
46
|
+
runs the calls of a turn it gives each one the room that is left: a result is cut to it and says
|
|
47
|
+
where to go on, and a call that finds no room is answered "no room left, answer now", after
|
|
48
|
+
which the next request is the answer. When the room runs short, the results of all but the last
|
|
49
|
+
three tool rounds are replaced by short stand-ins that keep the document and the lines or pages
|
|
50
|
+
they showed. A turn of sixty reads built a request of 491,000 tokens against a window of 128,000
|
|
51
|
+
before; the same run now peaks at about 100,000.
|
|
52
|
+
- **The tool calls of one turn run together**, four at a time, and are answered in the order they
|
|
53
|
+
were made. Looks at pages still run one after another, because each is given the room the one
|
|
54
|
+
before it left, and so do regex searches, which share their time. Six whole-file reads, two in
|
|
55
|
+
each of three turns, took 841 ms one after another against a fake model, and take about 540 ms.
|
|
56
|
+
- **Headers are asked for with every request**, the part reads included, so a token that
|
|
57
|
+
`vertexAi({ accessToken })` renews is used. They were asked for once a question.
|
|
58
|
+
- **The budget follows the model.** `reservedTokens`, `inlineTokens`, `mapChunkTokens` and
|
|
59
|
+
`toolResultCharacters` are worked out from `contextTokens` when they are left out
|
|
60
|
+
(`resolveBudget`): setting only `contextTokens: 64_000` made a nine-token note a card with tools
|
|
61
|
+
before. An adapter can say what its model takes (`contextTokens`, `maxRequestBytes`,
|
|
62
|
+
`outputTokens`, `pageTokens`, and `tokenScale` for a tokenizer that counts more than the
|
|
63
|
+
library's estimate), and the budget of a question is made to fit it. `budget` in `askWithFiles`,
|
|
64
|
+
`prepare` and `tools` changes fields for one question, and `files.budgetFor()` shows the result.
|
|
65
|
+
- **`evidence`** in the result: for each tool call, the document and the lines or pages the model
|
|
66
|
+
was shown.
|
|
67
|
+
- **`toolTurns`** in the result: the tool rounds of the question as plain JSON. Kept with the
|
|
68
|
+
conversation and passed back in `messages` between the question and its answer, they let a
|
|
69
|
+
follow-up question be answered from what was already read. In a live check the follow-up took
|
|
70
|
+
one request and no search. `geminiToolTurns`, `openAiToolTurns` and `anthropicToolTurns` give
|
|
71
|
+
them in a provider's own form, and `fromGeminiContents`, `fromOpenAiMessages` and
|
|
72
|
+
`fromAnthropicMessages` turn a history kept in that form into `messages`.
|
|
73
|
+
- **A tool call that did not arrive whole is not run.** Arguments that are not JSON, or a call cut
|
|
74
|
+
off at the output limit, are answered with what was wrong, and the model can send the call again.
|
|
75
|
+
- **The output limit of a part read is raised for any model that thinks**, not only Gemini: the
|
|
76
|
+
rule of 0.4.1 is shared by the three adapters.
|
|
77
|
+
- For Claude: the model's turn goes back exactly as it came, thinking blocks included; the tools
|
|
78
|
+
stay listed in the request that must be answered in text (`tool_choice: none`); the conversation
|
|
79
|
+
is never rewritten, so old results are not shortened for it; pages go into the tool result as a
|
|
80
|
+
document block; prompt caching is on; a refusal is reported as `blockReason`; and `fallbacks`
|
|
81
|
+
turns on the API's own fallback to another model.
|
|
82
|
+
|
|
83
|
+
### Changed
|
|
84
|
+
|
|
85
|
+
What can need a change in your code:
|
|
86
|
+
|
|
87
|
+
- **`result.usage` is no longer Gemini's `usageMetadata` of the last response.** That object is
|
|
88
|
+
now `result.lastUsage`. If you forward `usage` to a browser or a log, read `lastUsage` for the
|
|
89
|
+
old shape, or use the totals.
|
|
90
|
+
- **A failure can take longer to reach you**, because the request is tried again first: up to
|
|
91
|
+
three and a half seconds of waiting by default for a model that stays down, and up to a minute
|
|
92
|
+
when the provider names a wait. `retry: { retries: 0 }` gives the old behaviour.
|
|
93
|
+
- **A connection that failed throws `GeminiRequestError` with `status` 0**, not the `TypeError` of
|
|
94
|
+
`fetch`. The original error is its `cause`.
|
|
95
|
+
- **A second whole-file read of the same document in one question is refused.** Set
|
|
96
|
+
`limits.wholeDocumentReads` (or `maxWholeDocumentReads` in `files.tools()`) to allow more. Make
|
|
97
|
+
one tool set for each question: the count is the tool set's.
|
|
98
|
+
- **Budget values that cannot work together throw at `createLargeFiles`**: `reservedTokens` or
|
|
99
|
+
`mapChunkTokens` at or above `contextTokens`. And with `contextTokens` below 400,000, fields you
|
|
100
|
+
left out are now smaller than the fixed defaults they had.
|
|
101
|
+
- **`onActivity` and `onStep` can interleave differently.** Calls of one turn start together, so
|
|
102
|
+
the "Searching…" lines of several calls can come before the first finished step. Steps still
|
|
103
|
+
arrive in call order.
|
|
104
|
+
|
|
105
|
+
What needs none, unless you built on it:
|
|
106
|
+
|
|
107
|
+
- Old tool results can be replaced by stand-ins (`{ dropped: true, lines, note }`) late in a long
|
|
108
|
+
question. `steps` then holds "Older tool results were shortened to make room in the request".
|
|
109
|
+
- `ToolSet` has a new member, `resultCharacters(name)`, and `LargeFiles` has `budgetFor()`: a mock
|
|
110
|
+
of either needs them to type-check.
|
|
111
|
+
- `createGeminiGenerateText` throws `GeminiRequestError` (it threw an `Error` with a `status`),
|
|
112
|
+
and takes `headers` as a function too.
|
|
113
|
+
- `GeminiFunctionCall` and `GeminiToolAnswer` are now the core's `ToolCall` and `ToolAnswer`, and
|
|
114
|
+
`ToolFileLimits` is `ToolCallLimits` (the old name is kept).
|
|
115
|
+
- `maxToolTurns` and `maxToolCallsPerTurn` of `askGemini` still work. Their new home is
|
|
116
|
+
`limits.toolTurns` and `limits.callsPerTurn`.
|
|
117
|
+
- Tool results are those of 0.4.1, compared call by call with 0.3.6 over 40 documents, except
|
|
118
|
+
that a repeated whole-file read is refused.
|
|
119
|
+
|
|
120
|
+
### Checked
|
|
121
|
+
|
|
122
|
+
- 331 tests, 65 of them new. The requests `askGemini` sends are the ones 0.4.1 sent: its 44 tests
|
|
123
|
+
pass unchanged but for one that now turns the retry off.
|
|
124
|
+
- Live, on the Gemini API: `askWithFiles` with `gemini()` and with `openAiCompatible()` on Gemini's
|
|
125
|
+
OpenAI layer (streamed, a 5.2 MB log), an explorer behind the OpenAI layer with a thinking Gemini
|
|
126
|
+
model answering, `askGemini` on a thinking model alone and with an explorer, a follow-up
|
|
127
|
+
question from stored tool rounds, and a part read through the OpenAI adapter. In each, the usage
|
|
128
|
+
the library returned was what the provider reported, request by request.
|
|
129
|
+
- **Not run against a real server:** the Claude adapter (there was no key: it is tested against
|
|
130
|
+
the request and response shapes of the Messages API documentation), and `pdfInput` of the OpenAI
|
|
131
|
+
adapter, which sends the pages of a PDF as a `file` part. Gemini's OpenAI layer refuses that part
|
|
132
|
+
(HTTP 400), so it is off by default and follows OpenAI's documented shape untested.
|
|
133
|
+
|
|
134
|
+
## [0.4.1] - 2026-11-13
|
|
135
|
+
|
|
136
|
+
Fixes from a review of 0.4.0. Nothing to change in your code. Tool results are those of 0.4.0
|
|
137
|
+
except for the three points under Changed (compared call by call with 0.3.6 over 40 documents).
|
|
138
|
+
|
|
139
|
+
### Fixed
|
|
140
|
+
|
|
141
|
+
- **A whole-file read returned almost nothing on a model that thinks.** Such a model counts its
|
|
142
|
+
thoughts in `maxOutputTokens`. Asked for 4,096 tokens of notes on a part of a log, Gemini 3.8
|
|
143
|
+
Flash spent 3,931 of them thinking and wrote 317 characters, and Gemini 4 Hydrogen wrote none.
|
|
144
|
+
`createGeminiGenerateText` now sends such a request once more with 32,768
|
|
145
|
+
(`THINKING_OUTPUT_TOKENS`), and starts there for every later request to that model: one request
|
|
146
|
+
is wasted per model and process, not one per part. A model or a gateway that refuses the larger
|
|
147
|
+
limit is not asked for it again. An `analysisConfig` of your own still decides what it sets.
|
|
148
|
+
Checked live: both models then returned whole notes, and an answer that cited all 70 matching
|
|
149
|
+
lines of a log read in three parts.
|
|
150
|
+
- **`sweep` deleted a healthy document when one read of its metadata failed.** Every error counted
|
|
151
|
+
as "cannot be read", and a file older than an hour was then removed, with its text and its
|
|
152
|
+
original. Now only metadata that is not valid is that. A read that failed (a busy volume, too
|
|
153
|
+
many open files) leaves the document alone, and a document that cannot be removed no longer
|
|
154
|
+
stops the sweep.
|
|
155
|
+
- **A store that could not be read looked like a deleted document.** `FileDocumentStore` answered
|
|
156
|
+
every failed read with "not there", and `askGemini` then told the user to upload the file
|
|
157
|
+
again. A failed read is now a `DocumentStoreError`: retry the call. Its message names no path
|
|
158
|
+
and no host, and the error that was caught is its `cause`.
|
|
159
|
+
- **Storage errors reached the model, and a user's screen, with the server's path.** A tool call
|
|
160
|
+
that met `EACCES: permission denied, open '/data/large-files/doc_….txt'` passed that on in its
|
|
161
|
+
result and in `steps`. It now answers `The document store could not be read (EACCES). Try again.`
|
|
162
|
+
- **One model turn could hold the server.** A regex that the quick check lets through, such as
|
|
163
|
+
`(a|a)+$`, ran for its full 3 seconds on the main thread, and a turn can hold any number of
|
|
164
|
+
calls: five such searches in one turn answered no other request for 15 seconds. A file can ask
|
|
165
|
+
a model for exactly that. Now:
|
|
166
|
+
- a regex scan runs in slices of 250 ms with other work in between, and stops at a line that
|
|
167
|
+
takes two whole slices: 0.8 s for such a search, not 3;
|
|
168
|
+
- the regex searches of one tool set (one question, in `askGemini`) share 6 seconds
|
|
169
|
+
(`regexBudgetMs`). Past that, a regex search answers with an error that tells the model to
|
|
170
|
+
search another way;
|
|
171
|
+
- `askGemini` runs at most 8 tool calls of one turn (`maxToolCallsPerTurn`). The others are
|
|
172
|
+
answered "not run, send it again if you still need it". Sixty reads in one turn built a
|
|
173
|
+
request of 339,000 tokens before.
|
|
174
|
+
- **`ttlMs: Infinity` made every ingest throw** (`RangeError: Invalid time value`). It now keeps
|
|
175
|
+
documents until they are deleted: their `expiresAt` is `NEVER_EXPIRES`.
|
|
176
|
+
- **A long line of emoji lost characters when it was wrapped.** The cut at 2,000 could fall
|
|
177
|
+
between the two halves of a character outside the basic plane, and each half was stored as a
|
|
178
|
+
replacement character.
|
|
179
|
+
- **`jsonBytes` and the planner under-counted** text with quotes, backslashes, tabs or many line
|
|
180
|
+
breaks: by 23% for a JSON-lines export and 31% for TSV. They now count what JSON adds exactly,
|
|
181
|
+
`DocumentMeta.jsonBytes` keeps it for planning, and `read_document` trims its result to the
|
|
182
|
+
limit as it is sent: a read of quoted text came out at 42,514 characters against 24,000.
|
|
183
|
+
- **`**NOTHING RELEVANT**` counted as a relevant part** of a whole-file read. The reply of a part
|
|
184
|
+
that found nothing is recognised whatever Markdown is around it.
|
|
185
|
+
- **A merge of notes that failed twice lost the notes of every part.** They now go on unmerged,
|
|
186
|
+
and the answer is written from them.
|
|
187
|
+
- **`cacheSize` counted the pieces kept of a document that is read by range**, so with a small
|
|
188
|
+
value a document pushed itself out of memory on every search. It counts documents again.
|
|
189
|
+
- **`delete` could leave a document listed without its text.** The metadata is removed first, and
|
|
190
|
+
if that fails nothing else is removed.
|
|
191
|
+
- **`\p{L}` matched nothing in a regex search.** Patterns are compiled with the Unicode flag when
|
|
192
|
+
they allow it, and without it otherwise.
|
|
193
|
+
|
|
194
|
+
### Changed
|
|
195
|
+
|
|
196
|
+
- **A file too large for `analyze_whole_document` is no longer offered it.** Its card says "It is
|
|
197
|
+
too large to be analyzed whole", the tool guide and the declared tools leave the tool out when
|
|
198
|
+
no file can use it, and `get_document_overview` says so in `whole_document_analysis`. A call of
|
|
199
|
+
the tool still answers with the reason.
|
|
200
|
+
- **`get_document_overview` works out the number of parts from the metadata**, not by reading
|
|
201
|
+
the whole file: that took 60 MB of reads and 0.7 s for a 60 MB log. The number is the count, or
|
|
202
|
+
within a few percent of it.
|
|
203
|
+
- In a regex, `.` and a quantifier take a character outside the basic plane (an emoji) as one
|
|
204
|
+
character.
|
|
205
|
+
|
|
206
|
+
### Added
|
|
207
|
+
|
|
208
|
+
- `DocumentStoreError`, `NEVER_EXPIRES`, `REGEX_BUDGET_MS` and `THINKING_OUTPUT_TOKENS`.
|
|
209
|
+
- Options `maxToolCallsPerTurn` (`askGemini`) and `regexBudgetMs` (`tools`).
|
|
210
|
+
- `DocumentMeta.jsonBytes`. Documents ingested before keep the planner's 8% rule.
|
|
211
|
+
|
|
7
212
|
## [0.4.0] - 2026-11-12
|
|
8
213
|
|
|
9
214
|
Nothing to change in your code: the API, the tools and their results are those of 0.3.6 (compared
|
package/README.md
CHANGED
|
@@ -12,8 +12,9 @@ and 1M tokens of context. This package bridges the gap:
|
|
|
12
12
|
- **Pictures in a PDF** are not in its text. The model can **look at the real pages** when it
|
|
13
13
|
needs to: a chart, a photo, a scan.
|
|
14
14
|
|
|
15
|
-
It's a small library, not a framework: no model SDK, no server, and no UI are required.
|
|
16
|
-
|
|
15
|
+
It's a small library, not a framework: no model SDK, no server, and no UI are required. One tool
|
|
16
|
+
loop, `askWithFiles`, works with any model through an adapter. Adapters for **Gemini and Vertex
|
|
17
|
+
AI**, **any OpenAI-compatible endpoint** and **Claude** are included, and so are Node HTTP handlers.
|
|
17
18
|
|
|
18
19
|
```bash
|
|
19
20
|
npm install llm-large-files
|
|
@@ -53,6 +54,43 @@ const endpoint = vertexAi({ project: "my-project", model: "gemini-3.8-flash", ac
|
|
|
53
54
|
const endpoint = { url: "https://llm-gw.corp/v1/gemini:generateContent", headers: { Authorization: `Bearer ${key}` } };
|
|
54
55
|
```
|
|
55
56
|
|
|
57
|
+
## Any model
|
|
58
|
+
|
|
59
|
+
`askGemini` is `askWithFiles` with a Gemini model. With another provider, name the model yourself:
|
|
60
|
+
|
|
61
|
+
```ts
|
|
62
|
+
import { askWithFiles } from "llm-large-files";
|
|
63
|
+
import { openAiCompatible } from "llm-large-files/openai";
|
|
64
|
+
import { anthropic } from "llm-large-files/anthropic";
|
|
65
|
+
|
|
66
|
+
// OpenAI, Azure OpenAI, DeepSeek, LiteLLM, vLLM, Ollama, OpenRouter: anything that speaks Chat Completions.
|
|
67
|
+
const deepseek = openAiCompatible({ baseUrl: "https://api.deepseek.com/v1", apiKey: process.env.DEEPSEEK_API_KEY!, model: "deepseek-v4-pro" });
|
|
68
|
+
// Claude. `fallbacks` lets another model answer when a safety classifier declines the request.
|
|
69
|
+
const claude = anthropic({ apiKey: process.env.ANTHROPIC_API_KEY!, model: "claude-opus-5-5", fallbacks: "default" });
|
|
70
|
+
|
|
71
|
+
const result = await askWithFiles({
|
|
72
|
+
files,
|
|
73
|
+
model: claude, // writes the answer
|
|
74
|
+
explorer: deepseek, // optional: a second model, of any provider, that searches and reads
|
|
75
|
+
documentIds: [doc.id],
|
|
76
|
+
messages: [{ role: "user", text: "What caused the outage, and when? Cite lines." }],
|
|
77
|
+
onText: (delta) => process.stdout.write(delta),
|
|
78
|
+
});
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
Every model gets the same loop: a request that failed is sent again, the request is kept inside
|
|
82
|
+
the model's window, and one question cannot read a file whole twice. The result says what it cost
|
|
83
|
+
and what it read:
|
|
84
|
+
|
|
85
|
+
```ts
|
|
86
|
+
result.usage; // { requests: 5, inputTokens: 32662, cachedInputTokens: 6086, outputTokens: 750, byModel: { … } }
|
|
87
|
+
result.evidence; // [{ tool: "read_document", documentId, document: "prod.log", lines: [[34100, 34110]] }, …]
|
|
88
|
+
result.toolTurns; // keep these with the conversation: a follow-up question then needs no new search
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
A model that has no adapter needs one object with a `send` method:
|
|
92
|
+
[your own adapter](docs/api.md#your-own-adapter).
|
|
93
|
+
|
|
56
94
|
## Add it to a web app
|
|
57
95
|
|
|
58
96
|
```ts
|
|
@@ -126,7 +164,9 @@ What each model gets, and what it costs: [An explorer model](docs/configuration.
|
|
|
126
164
|
| **Formats** | Text, Markdown, logs (UTF-8 and UTF-16), JSON/JSONL, CSV/TSV, DOCX, PPTX, PDF (text layer, with tables rebuilt so empty cells stay empty; pages with a picture are marked, and the model can look at the pages themselves; scans of up to 30 pages) |
|
|
127
165
|
| **Logs** | Detected automatically; the card shows level counts, the time range, and the most frequent errors grouped by shape (`timeout after <n>ms to <ip>`) |
|
|
128
166
|
| **Books and documents** | An outline of headings with line numbers, including Markdown, "Chapter 12", numbered sections, and Hebrew chapter titles |
|
|
129
|
-
| **Limits** | One budget covers tokens *and* request bytes, so files fit both your context window and your connector's payload limit |
|
|
167
|
+
| **Limits** | One budget covers tokens *and* request bytes, so files fit both your context window and your connector's payload limit. It follows the model: set `contextTokens`, or let the adapter say it |
|
|
168
|
+
| **Models** | Gemini and Vertex AI, any OpenAI-compatible endpoint, Claude, or an adapter of your own. Two models of different providers can share a question |
|
|
169
|
+
| **Cost** | Usage summed over every request of a question, for each model, with ceilings on whole-file reads, requests and tokens |
|
|
130
170
|
| **Privacy** | Documents can belong to an `owner`; other users can't plan, read, or delete them |
|
|
131
171
|
| **Storage** | In memory, on disk with expiry, or your own store (Redis, S3, a database) |
|
|
132
172
|
|
|
@@ -143,11 +183,11 @@ With `gemini-3.8-flash` on the Gemini API:
|
|
|
143
183
|
|
|
144
184
|
## Documentation
|
|
145
185
|
|
|
146
|
-
- [Integration guide](docs/getting-started.md): Express, plain Node, browser upload, streaming, Vertex AI, gateways, existing chat history
|
|
147
|
-
- [API reference](docs/api.md)
|
|
148
|
-
- [Configuration](docs/configuration.md): budget, an explorer model, stores, formats, extractors, pictures in PDFs
|
|
149
|
-
- [How it works](docs/how-it-works.md): planner, cards, tools, map-reduce, looking at pages, and the
|
|
150
|
-
- [Security](docs/security.md): owners, tool scoping, retention, original files, regex safety
|
|
186
|
+
- [Integration guide](docs/getting-started.md): Express, plain Node, browser upload, streaming, Vertex AI, gateways, other models, existing chat history
|
|
187
|
+
- [API reference](docs/api.md): `askWithFiles`, the three adapters, and what an adapter of your own must do
|
|
188
|
+
- [Configuration](docs/configuration.md): budget, limits and retries, an explorer model, stores, formats, extractors, pictures in PDFs
|
|
189
|
+
- [How it works](docs/how-it-works.md): planner, cards, tools, map-reduce, looking at pages, the loop, and the provider quirks it handles
|
|
190
|
+
- [Security](docs/security.md): owners, tool scoping, retention, original files, regex safety, what a question can cost
|
|
151
191
|
|
|
152
192
|
## Requirements
|
|
153
193
|
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { type HeaderSource, type SecretSource } from "../http.js";
|
|
2
|
+
import { ModelRequestError, type ChatMessage, type ModelAdapter, type ToolTurn } from "../model.js";
|
|
3
|
+
/** The API answered with an HTTP error, or could not be reached (`status` 0). `type` is the API's own word: "rate_limit_error". */
|
|
4
|
+
export declare class AnthropicRequestError extends ModelRequestError {
|
|
5
|
+
readonly type?: string;
|
|
6
|
+
constructor(message: string, status: number, options?: {
|
|
7
|
+
retryAfterMs?: number;
|
|
8
|
+
cause?: unknown;
|
|
9
|
+
type?: string;
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
/** A message of the Messages API, as it takes and returns it. */
|
|
13
|
+
export type AnthropicMessage = {
|
|
14
|
+
role: "user" | "assistant";
|
|
15
|
+
content: string | Array<Record<string, unknown>>;
|
|
16
|
+
};
|
|
17
|
+
/** The API version the requests are written for. */
|
|
18
|
+
export declare const ANTHROPIC_VERSION = "2026-10-21";
|
|
19
|
+
export type AnthropicOptions = {
|
|
20
|
+
/** The model ID: "claude-opus-5-5". */
|
|
21
|
+
model: string;
|
|
22
|
+
/** Sent as `x-api-key`. A function is called for each request. */
|
|
23
|
+
apiKey?: SecretSource | undefined;
|
|
24
|
+
/** Sent as `Authorization: Bearer …`, for an OAuth token or a gateway. A function is called for each request. */
|
|
25
|
+
authToken?: SecretSource | undefined;
|
|
26
|
+
/** The base of the API: `/v1/messages` is added to it. Defaults to https://api.anthropic.com. */
|
|
27
|
+
baseUrl?: string | undefined;
|
|
28
|
+
/** The whole Messages URL, when it is not `baseUrl` + `/v1/messages`. */
|
|
29
|
+
url?: string | undefined;
|
|
30
|
+
/** More headers. A function is called for each request. */
|
|
31
|
+
headers?: HeaderSource | undefined;
|
|
32
|
+
/** The `anthropic-version` header. Defaults to `ANTHROPIC_VERSION`. */
|
|
33
|
+
version?: string | undefined;
|
|
34
|
+
/** Values for the `anthropic-beta` header. */
|
|
35
|
+
betas?: string[] | undefined;
|
|
36
|
+
/**
|
|
37
|
+
* `max_tokens` of a chat request: the most the model may write, thoughts included. Defaults to
|
|
38
|
+
* 64,000. It comes out of the context window, so the budget of a question keeps it free.
|
|
39
|
+
*/
|
|
40
|
+
maxTokens?: number | undefined;
|
|
41
|
+
/** More fields for the body of every chat request: `thinking`, `output_config`, `context_management`, `metadata`, and so on. */
|
|
42
|
+
body?: Record<string, unknown> | undefined;
|
|
43
|
+
/** More fields for the body of a whole-document part read, given the `max_tokens` it is sent with. */
|
|
44
|
+
analysisBody?: ((maxOutputTokens: number) => Record<string, unknown>) | undefined;
|
|
45
|
+
/**
|
|
46
|
+
* Have another model answer when this one's safety classifiers decline a request: "default", or
|
|
47
|
+
* a list of `{ model }`. It adds the `fallbacks` field and its beta header. Off unless set: it
|
|
48
|
+
* is offered on the Claude API, not on Amazon Bedrock, Vertex AI or Microsoft Foundry, and not
|
|
49
|
+
* for every model.
|
|
50
|
+
*/
|
|
51
|
+
fallbacks?: "default" | Array<Record<string, unknown> & {
|
|
52
|
+
model: string;
|
|
53
|
+
}> | undefined;
|
|
54
|
+
/**
|
|
55
|
+
* Prompt caching: a top-level `cache_control`, which caches the request up to its last block, so
|
|
56
|
+
* each request of a tool loop, and a follow-up question, reads what the one before wrote.
|
|
57
|
+
* `true` (the default) keeps it five minutes, "1h" an hour, `false` sends none.
|
|
58
|
+
*/
|
|
59
|
+
cache?: boolean | "5m" | "1h" | undefined;
|
|
60
|
+
/**
|
|
61
|
+
* Where the pages of `view_pages` go: "inside" the tool result as a document block (the
|
|
62
|
+
* default), "beside" it as a document block after the tool results, or "none" to not offer
|
|
63
|
+
* `view_pages`.
|
|
64
|
+
*/
|
|
65
|
+
toolFilePlacement?: "inside" | "beside" | "none" | undefined;
|
|
66
|
+
/** Chat requests are streamed, which keeps a long reply from timing out. False for a gateway without streaming. */
|
|
67
|
+
stream?: boolean | undefined;
|
|
68
|
+
/**
|
|
69
|
+
* Set `eager_input_streaming` on the tools of a streamed request, so tool input arrives as it
|
|
70
|
+
* is written. Defaults to true for api.anthropic.com and false for any other URL, which may
|
|
71
|
+
* refuse the field.
|
|
72
|
+
*/
|
|
73
|
+
eagerInputStreaming?: boolean | undefined;
|
|
74
|
+
/** The model's context window in tokens, when the budget of a question should follow it. */
|
|
75
|
+
contextTokens?: number | undefined;
|
|
76
|
+
/** The largest request body in bytes. Defaults to 32 MB, the limit of the Claude API. */
|
|
77
|
+
maxRequestBytes?: number | undefined;
|
|
78
|
+
/** Tokens billed for one PDF page: its text and its picture. Defaults to 3,500. */
|
|
79
|
+
pageTokens?: number | undefined;
|
|
80
|
+
/**
|
|
81
|
+
* This model's tokens for one token of the library's estimate, which is fitted to Gemini.
|
|
82
|
+
* Defaults to 1.35, a cautious guess for the tokenizer of the current models: the count the API
|
|
83
|
+
* reports with each reply corrects it during a question.
|
|
84
|
+
*/
|
|
85
|
+
tokenScale?: number | undefined;
|
|
86
|
+
/** Milliseconds a part read may take. Defaults to 180,000. */
|
|
87
|
+
timeoutMs?: number | undefined;
|
|
88
|
+
/** Names the model in usage totals. Defaults to `model`. */
|
|
89
|
+
id?: string | undefined;
|
|
90
|
+
fetch?: typeof fetch | undefined;
|
|
91
|
+
};
|
|
92
|
+
/**
|
|
93
|
+
* Messages of the Messages API as the conversation of `askWithFiles`: text blocks as text, a turn
|
|
94
|
+
* of `tool_use` blocks and the `tool_result` blocks that answer it as one stored tool round, and
|
|
95
|
+
* every other block (an image, a document) as it is. Thinking blocks are left out: they belong to
|
|
96
|
+
* the conversation they were written in.
|
|
97
|
+
*/
|
|
98
|
+
export declare function fromAnthropicMessages(history: AnthropicMessage[]): ChatMessage[];
|
|
99
|
+
/**
|
|
100
|
+
* The tool rounds a question returned (`AskResult.toolTurns`) as messages of the Messages API, for
|
|
101
|
+
* an application that keeps its history in that form: put them between the question and its answer.
|
|
102
|
+
*/
|
|
103
|
+
export declare function anthropicToolTurns(turns: ToolTurn[]): AnthropicMessage[];
|
|
104
|
+
/** A Claude model for `askWithFiles`. */
|
|
105
|
+
export declare function anthropic(options: AnthropicOptions): ModelAdapter;
|
|
106
|
+
//# sourceMappingURL=adapter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"adapter.d.ts","sourceRoot":"","sources":["../../src/anthropic/adapter.ts"],"names":[],"mappings":"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"}
|