llm-large-files 0.8.2 → 0.10.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.
Files changed (288) hide show
  1. package/CHANGELOG.md +981 -0
  2. package/README.md +22 -17
  3. package/dist/access.d.ts +128 -0
  4. package/dist/access.d.ts.map +1 -0
  5. package/dist/access.js +234 -0
  6. package/dist/access.js.map +1 -0
  7. package/dist/aggregate.d.ts +6 -0
  8. package/dist/aggregate.d.ts.map +1 -1
  9. package/dist/aggregate.js +86 -13
  10. package/dist/aggregate.js.map +1 -1
  11. package/dist/analyze.d.ts +4 -0
  12. package/dist/analyze.d.ts.map +1 -1
  13. package/dist/analyze.js +2 -2
  14. package/dist/analyze.js.map +1 -1
  15. package/dist/answer-info.d.ts +26 -0
  16. package/dist/answer-info.d.ts.map +1 -0
  17. package/dist/answer-info.js +32 -0
  18. package/dist/answer-info.js.map +1 -0
  19. package/dist/anthropic/adapter.d.ts +5 -1
  20. package/dist/anthropic/adapter.d.ts.map +1 -1
  21. package/dist/anthropic/adapter.js +35 -10
  22. package/dist/anthropic/adapter.js.map +1 -1
  23. package/dist/ask.d.ts +159 -14
  24. package/dist/ask.d.ts.map +1 -1
  25. package/dist/ask.js +743 -113
  26. package/dist/ask.js.map +1 -1
  27. package/dist/cache.d.ts +12 -3
  28. package/dist/cache.d.ts.map +1 -1
  29. package/dist/cache.js +18 -5
  30. package/dist/cache.js.map +1 -1
  31. package/dist/citations.d.ts +36 -0
  32. package/dist/citations.d.ts.map +1 -0
  33. package/dist/citations.js +291 -0
  34. package/dist/citations.js.map +1 -0
  35. package/dist/encoding.js +5 -3
  36. package/dist/encoding.js.map +1 -1
  37. package/dist/error-info.d.ts +70 -0
  38. package/dist/error-info.d.ts.map +1 -0
  39. package/dist/error-info.js +253 -0
  40. package/dist/error-info.js.map +1 -0
  41. package/dist/errors.d.ts +74 -8
  42. package/dist/errors.d.ts.map +1 -1
  43. package/dist/errors.js +104 -15
  44. package/dist/errors.js.map +1 -1
  45. package/dist/events.d.ts +57 -13
  46. package/dist/events.d.ts.map +1 -1
  47. package/dist/events.js +42 -11
  48. package/dist/events.js.map +1 -1
  49. package/dist/evidence.d.ts +4 -2
  50. package/dist/evidence.d.ts.map +1 -1
  51. package/dist/evidence.js +35 -5
  52. package/dist/evidence.js.map +1 -1
  53. package/dist/extract.d.ts +32 -8
  54. package/dist/extract.d.ts.map +1 -1
  55. package/dist/extract.js +52 -36
  56. package/dist/extract.js.map +1 -1
  57. package/dist/gemini/adapter.d.ts +54 -7
  58. package/dist/gemini/adapter.d.ts.map +1 -1
  59. package/dist/gemini/adapter.js +152 -41
  60. package/dist/gemini/adapter.js.map +1 -1
  61. package/dist/gemini/chat.d.ts +38 -53
  62. package/dist/gemini/chat.d.ts.map +1 -1
  63. package/dist/gemini/chat.js +20 -25
  64. package/dist/gemini/chat.js.map +1 -1
  65. package/dist/gemini/endpoints.d.ts +59 -11
  66. package/dist/gemini/endpoints.d.ts.map +1 -1
  67. package/dist/gemini/endpoints.js +33 -7
  68. package/dist/gemini/endpoints.js.map +1 -1
  69. package/dist/gemini/errors.d.ts +39 -5
  70. package/dist/gemini/errors.d.ts.map +1 -1
  71. package/dist/gemini/errors.js +70 -9
  72. package/dist/gemini/errors.js.map +1 -1
  73. package/dist/gemini/generate.d.ts +53 -2
  74. package/dist/gemini/generate.d.ts.map +1 -1
  75. package/dist/gemini/generate.js +100 -50
  76. package/dist/gemini/generate.js.map +1 -1
  77. package/dist/gemini/index.d.ts +4 -4
  78. package/dist/gemini/index.d.ts.map +1 -1
  79. package/dist/http.d.ts +41 -3
  80. package/dist/http.d.ts.map +1 -1
  81. package/dist/http.js +40 -2
  82. package/dist/http.js.map +1 -1
  83. package/dist/index.d.ts +13 -8
  84. package/dist/index.d.ts.map +1 -1
  85. package/dist/index.js +8 -5
  86. package/dist/index.js.map +1 -1
  87. package/dist/ingest.d.ts +78 -0
  88. package/dist/ingest.d.ts.map +1 -0
  89. package/dist/ingest.js +893 -0
  90. package/dist/ingest.js.map +1 -0
  91. package/dist/library.d.ts +295 -33
  92. package/dist/library.d.ts.map +1 -1
  93. package/dist/library.js +458 -650
  94. package/dist/library.js.map +1 -1
  95. package/dist/map-reduce.d.ts +176 -10
  96. package/dist/map-reduce.d.ts.map +1 -1
  97. package/dist/map-reduce.js +807 -61
  98. package/dist/map-reduce.js.map +1 -1
  99. package/dist/model.d.ts +79 -6
  100. package/dist/model.d.ts.map +1 -1
  101. package/dist/model.js +80 -4
  102. package/dist/model.js.map +1 -1
  103. package/dist/node/handlers.d.ts +247 -14
  104. package/dist/node/handlers.d.ts.map +1 -1
  105. package/dist/node/handlers.js +491 -227
  106. package/dist/node/handlers.js.map +1 -1
  107. package/dist/node/index.d.ts +3 -2
  108. package/dist/node/index.d.ts.map +1 -1
  109. package/dist/node/index.js +2 -1
  110. package/dist/node/index.js.map +1 -1
  111. package/dist/node/ingest-child.d.ts +7 -1
  112. package/dist/node/ingest-child.d.ts.map +1 -1
  113. package/dist/node/ingest-child.js +19 -4
  114. package/dist/node/ingest-child.js.map +1 -1
  115. package/dist/node/isolated.d.ts +3 -1
  116. package/dist/node/isolated.d.ts.map +1 -1
  117. package/dist/node/isolated.js +11 -4
  118. package/dist/node/isolated.js.map +1 -1
  119. package/dist/node/originals.d.ts +19 -0
  120. package/dist/node/originals.d.ts.map +1 -0
  121. package/dist/node/originals.js +82 -0
  122. package/dist/node/originals.js.map +1 -0
  123. package/dist/node/spool.d.ts +106 -0
  124. package/dist/node/spool.d.ts.map +1 -0
  125. package/dist/node/spool.js +378 -0
  126. package/dist/node/spool.js.map +1 -0
  127. package/dist/node/sse.d.ts +6 -4
  128. package/dist/node/sse.d.ts.map +1 -1
  129. package/dist/node/sse.js +9 -7
  130. package/dist/node/sse.js.map +1 -1
  131. package/dist/openai/adapter.d.ts +5 -1
  132. package/dist/openai/adapter.d.ts.map +1 -1
  133. package/dist/openai/adapter.js +53 -12
  134. package/dist/openai/adapter.js.map +1 -1
  135. package/dist/output-limit.d.ts +2 -1
  136. package/dist/output-limit.d.ts.map +1 -1
  137. package/dist/output-limit.js +5 -2
  138. package/dist/output-limit.js.map +1 -1
  139. package/dist/pdf-layout.d.ts.map +1 -1
  140. package/dist/pdf-layout.js +71 -12
  141. package/dist/pdf-layout.js.map +1 -1
  142. package/dist/pdf-outline.d.ts +71 -0
  143. package/dist/pdf-outline.d.ts.map +1 -0
  144. package/dist/pdf-outline.js +414 -0
  145. package/dist/pdf-outline.js.map +1 -0
  146. package/dist/plan.d.ts +137 -7
  147. package/dist/plan.d.ts.map +1 -1
  148. package/dist/plan.js +248 -26
  149. package/dist/plan.js.map +1 -1
  150. package/dist/prepare.d.ts +50 -0
  151. package/dist/prepare.d.ts.map +1 -0
  152. package/dist/prepare.js +207 -0
  153. package/dist/prepare.js.map +1 -0
  154. package/dist/ranged.d.ts +16 -11
  155. package/dist/ranged.d.ts.map +1 -1
  156. package/dist/ranged.js +56 -12
  157. package/dist/ranged.js.map +1 -1
  158. package/dist/retry.d.ts +3 -1
  159. package/dist/retry.d.ts.map +1 -1
  160. package/dist/retry.js +6 -2
  161. package/dist/retry.js.map +1 -1
  162. package/dist/scope.d.ts +25 -0
  163. package/dist/scope.d.ts.map +1 -0
  164. package/dist/scope.js +60 -0
  165. package/dist/scope.js.map +1 -0
  166. package/dist/search.d.ts +7 -0
  167. package/dist/search.d.ts.map +1 -1
  168. package/dist/search.js +72 -10
  169. package/dist/search.js.map +1 -1
  170. package/dist/source.d.ts +60 -0
  171. package/dist/source.d.ts.map +1 -0
  172. package/dist/source.js +170 -0
  173. package/dist/source.js.map +1 -0
  174. package/dist/stores.d.ts +126 -31
  175. package/dist/stores.d.ts.map +1 -1
  176. package/dist/stores.js +655 -141
  177. package/dist/stores.js.map +1 -1
  178. package/dist/testing/fake-fetch.d.ts +31 -0
  179. package/dist/testing/fake-fetch.d.ts.map +1 -0
  180. package/dist/testing/fake-fetch.js +66 -0
  181. package/dist/testing/fake-fetch.js.map +1 -0
  182. package/dist/testing/fake-model.d.ts +38 -0
  183. package/dist/testing/fake-model.d.ts.map +1 -0
  184. package/dist/testing/fake-model.js +62 -0
  185. package/dist/testing/fake-model.js.map +1 -0
  186. package/dist/testing/index.d.ts +7 -0
  187. package/dist/testing/index.d.ts.map +1 -0
  188. package/dist/testing/index.js +9 -0
  189. package/dist/testing/index.js.map +1 -0
  190. package/dist/testing/store-checks.d.ts +72 -0
  191. package/dist/testing/store-checks.d.ts.map +1 -0
  192. package/dist/testing/store-checks.js +619 -0
  193. package/dist/testing/store-checks.js.map +1 -0
  194. package/dist/tools.d.ts +47 -2
  195. package/dist/tools.d.ts.map +1 -1
  196. package/dist/tools.js +440 -71
  197. package/dist/tools.js.map +1 -1
  198. package/dist/types.d.ts +165 -14
  199. package/dist/types.d.ts.map +1 -1
  200. package/dist/whole-cost.d.ts +37 -0
  201. package/dist/whole-cost.d.ts.map +1 -0
  202. package/dist/whole-cost.js +46 -0
  203. package/dist/whole-cost.js.map +1 -0
  204. package/docs/README.md +21 -8
  205. package/docs/getting-started.md +18 -13
  206. package/docs/guides/anthropic.md +17 -10
  207. package/docs/guides/conversations.md +112 -47
  208. package/docs/guides/costs-and-limits.md +147 -42
  209. package/docs/guides/deployment.md +380 -86
  210. package/docs/guides/errors-and-reliability.md +317 -67
  211. package/docs/guides/explorer-model.md +4 -1
  212. package/docs/guides/openai-compatible.md +47 -7
  213. package/docs/guides/own-adapter-and-loop.md +60 -16
  214. package/docs/guides/pdfs.md +219 -36
  215. package/docs/guides/storage.md +676 -145
  216. package/docs/guides/testing.md +149 -129
  217. package/docs/guides/vertex-and-gemini.md +169 -57
  218. package/docs/guides/web-app.md +318 -80
  219. package/docs/how-it-works.md +155 -30
  220. package/docs/measurements.md +215 -13
  221. package/docs/publishing.md +185 -37
  222. package/docs/reference/anthropic.md +12 -7
  223. package/docs/reference/ask.md +415 -47
  224. package/docs/reference/errors-and-retries.md +194 -14
  225. package/docs/reference/events.md +77 -27
  226. package/docs/reference/gemini.md +252 -87
  227. package/docs/reference/library.md +886 -178
  228. package/docs/reference/node.md +659 -111
  229. package/docs/reference/openai.md +21 -10
  230. package/docs/reference/testing.md +311 -0
  231. package/docs/reference/tools.md +285 -30
  232. package/docs/security.md +404 -78
  233. package/docs/troubleshooting.md +151 -70
  234. package/docs/upgrading-0.10.md +472 -0
  235. package/docs/upgrading-0.9.md +890 -0
  236. package/package.json +16 -3
  237. package/src/access.ts +267 -0
  238. package/src/aggregate.ts +92 -14
  239. package/src/analyze.ts +2 -2
  240. package/src/answer-info.ts +45 -0
  241. package/src/anthropic/adapter.ts +40 -11
  242. package/src/ask.ts +866 -115
  243. package/src/cache.ts +20 -5
  244. package/src/citations.ts +294 -0
  245. package/src/encoding.ts +6 -4
  246. package/src/error-info.ts +290 -0
  247. package/src/errors.ts +131 -15
  248. package/src/events.ts +112 -24
  249. package/src/evidence.ts +35 -5
  250. package/src/extract.ts +56 -34
  251. package/src/gemini/adapter.ts +196 -43
  252. package/src/gemini/chat.ts +63 -78
  253. package/src/gemini/endpoints.ts +89 -16
  254. package/src/gemini/errors.ts +74 -11
  255. package/src/gemini/generate.ts +147 -57
  256. package/src/gemini/index.ts +4 -4
  257. package/src/http.ts +65 -4
  258. package/src/index.ts +13 -8
  259. package/src/ingest.ts +958 -0
  260. package/src/library.ts +711 -651
  261. package/src/map-reduce.ts +858 -70
  262. package/src/model.ts +113 -9
  263. package/src/node/handlers.ts +709 -221
  264. package/src/node/index.ts +14 -2
  265. package/src/node/ingest-child.ts +24 -6
  266. package/src/node/isolated.ts +11 -4
  267. package/src/node/originals.ts +78 -0
  268. package/src/node/spool.ts +379 -0
  269. package/src/node/sse.ts +9 -7
  270. package/src/openai/adapter.ts +56 -13
  271. package/src/output-limit.ts +5 -2
  272. package/src/pdf-layout.ts +69 -11
  273. package/src/pdf-outline.ts +400 -0
  274. package/src/plan.ts +287 -36
  275. package/src/prepare.ts +253 -0
  276. package/src/ranged.ts +48 -14
  277. package/src/retry.ts +6 -2
  278. package/src/scope.ts +63 -0
  279. package/src/search.ts +69 -13
  280. package/src/source.ts +178 -0
  281. package/src/stores.ts +702 -142
  282. package/src/testing/fake-fetch.ts +80 -0
  283. package/src/testing/fake-model.ts +78 -0
  284. package/src/testing/index.ts +11 -0
  285. package/src/testing/store-checks.ts +660 -0
  286. package/src/tools.ts +461 -73
  287. package/src/types.ts +151 -14
  288. package/src/whole-cost.ts +64 -0
@@ -99,12 +99,18 @@ requests are never streamed. Each piece of text goes to `onText` as it arrives.
99
99
  request whose connection broke after `[DONE]` with status 0, with the whole answer already shown.
100
100
  - A server that sends the name of a tool call again with each piece of the call does not make it
101
101
  longer (since 0.6.0; it gave `search_documentsearch_document`). A name sent in pieces is joined.
102
+ - A piece of a tool call without `index` starts a new call only when its ID is there and is not the
103
+ last call's; otherwise it adds to the last call (since 0.10.0). A server that sends the ID with
104
+ every piece made a call of each piece before.
102
105
 
103
106
  **The reply** ([`ModelReply`](./ask.md#modelreply)):
104
107
 
105
108
  - `text` is the message's content. When there is none and the model refused, it is the `refusal`,
106
109
  which goes to `onText` too.
107
- - `calls` are the `tool_calls` that have a name. Arguments that are not a JSON object give the call
110
+ - `calls` are the `tool_calls` that have a name. A call that came without an ID, or with an empty
111
+ one, is given `call_` and 24 hexadecimal characters, once: in the call and in `raw`, so the tool
112
+ message that answers it names the same ID (since 0.10.0; before, `raw` went back without it and
113
+ the tool message had a numbered ID). Arguments that are not a JSON object give the call
108
114
  a `problem`: the loop answers it with that message instead of running it, and the model can send
109
115
  it again. In the turn that goes back, those arguments, and empty ones, become `"{}"`, and
110
116
  arguments that came as an object become their JSON text. When
@@ -121,11 +127,14 @@ requests are never streamed. Each piece of text goes to `onText` as it arrives.
121
127
  `completion_tokens`; `thoughtTokens` is `completion_tokens_details.reasoning_tokens`. A missing
122
128
  number counts 0. `providerUsage` is the `usage` object as it came. A stream reports usage only
123
129
  when the server sends it.
130
+ - `responseId` and `responseModel` are the reply's `id` and `model`, when the server gives them
131
+ (new in 0.10.0).
124
132
 
125
133
  **Part reads** (`generateText`, for `analyze_whole_document`) go to the same URL with the same
126
134
  headers and are never streamed. The body is `model`, a message of role `systemRole` and a user
127
135
  message, the output limit under the name `maxTokensField`, then the fields of
128
- `analysisBody(limit)`. `body` is not used. Each request may take `timeoutMs` (180 s). A reply cut
136
+ `analysisBody(limit)`. `body` is not used. Each request may take `timeoutMs` (180 s; 0 for none,
137
+ since 0.10.0). A reply cut
129
138
  off with nothing written, or with half its limit or more spent thinking, while the limit was below
130
139
  32,768, is asked again with 32,768 output tokens
131
140
  ([`THINKING_OUTPUT_TOKENS`](./gemini.md#thinking_output_tokens)), and later part reads to the same
@@ -155,13 +164,15 @@ every later question on that model, in that process.
155
164
 
156
165
  A part read throws as `send` does for an HTTP error, a connection that failed, an empty body,
157
166
  `insufficient_system_resource`, an abort and a body that is not JSON. It has no waits of
158
- `firstByteTimeoutMs` and `idleTimeoutMs`; instead, a request that ran past `timeoutMs` throws the
159
- error of its abort (a `TimeoutError`). A reply with no text throws
160
- `Error("The model returned no text (length).")`, the reason in brackets being `finish_reason`.
161
- These two have no `status`. In a whole-file read, the model is told why a part failed by the
162
- error's kind and status, never by the server's message: "The model took too long to answer." for
163
- the `TimeoutError`, "The model returned no text (length)." for a reply with no text, the reason
164
- only when it is a plain word (see [`analyze_whole_document`](./tools.md#analyze_whole_document)).
167
+ `firstByteTimeoutMs` and `idleTimeoutMs`; instead, a request that ran past `timeoutMs` throws an
168
+ `OpenAiRequestError` "The model took longer than 180 s to answer.", status 0, kind `"timeout"`
169
+ (since 0.10.0; before, the error of its abort, a `TimeoutError` with no status). A whole-file read
170
+ sends it again once, as the retry rule does for a chat request that got nothing back. A reply with
171
+ no text throws `Error("The model returned no text (length).")`, the reason in brackets being
172
+ `finish_reason`; it has no `status`. In a whole-file read, the model is told why a part failed by
173
+ the error's kind and status, never by the server's message: "The model took too long to answer."
174
+ for a timeout, "The model returned no text (length)." for a reply with no text, the reason only
175
+ when it is a plain word (see [`analyze_whole_document`](./tools.md#analyze_whole_document)).
165
176
 
166
177
  ```ts
167
178
  import { askWithFiles } from "llm-large-files";
@@ -237,7 +248,7 @@ The options of [`openAiCompatible()`](#openaicompatibleoptions).
237
248
  | `maxRequestBytes` | `number` | none | The largest request body the endpoint takes, in bytes. Set as `capabilities.maxRequestBytes`, it keeps the budget within it. Without it the library's budget holds: 10,000,000 bytes unless you set another. |
238
249
  | `pageTokens` | `number` | `1_000` | Tokens one PDF page shown by `view_pages` costs. The loop sizes the pages a question may look at from it. It matters only with `pdfInput`. |
239
250
  | `tokenScale` | `number` | none, counted as 1 | This model's tokens for one token of the library's estimate, which is fitted to Gemini. A model whose tokenizer counts a third more for the same text takes 1.33, and its documents are planned for a window that much smaller. |
240
- | `timeoutMs` | `number` | `180_000` | The longest a part read's request may take, in milliseconds, each try counted apart, except that a request sent again with the other name of the output limit (see `maxTokensField`) shares the time of the first. A request that runs longer is aborted. Not used for chat requests. |
251
+ | `timeoutMs` | `number` | `180_000` | The longest a part read's request may take, in milliseconds, each try counted apart, except that a request sent again with the other name of the output limit (see `maxTokensField`) shares the time of the first. A request that runs longer is aborted, and is an `OpenAiRequestError` of kind `"timeout"` and status 0 (since 0.10.0). 0 sets no limit (since 0.10.0; before, it aborted every part read at once). Not used for chat requests. |
241
252
  | `firstByteTimeoutMs` | `number` | `600_000` | The longest wait, in milliseconds, for the first piece of a chat reply. Headers do not count: a model that thinks sends nothing until it has thought, and a reply that is not streamed comes whole at the end. A request that gets nothing for that long is aborted with an `OpenAiRequestError` (`status` 0, `kind` `"timeout"`), which `askWithFiles` sends again once. 0, a negative or a non-finite number waits without end. The key and headers are resolved before the wait starts. Not used for part reads. |
242
253
  | `idleTimeoutMs` | `number` | `300_000` | The longest wait, in milliseconds, between two pieces of a chat reply once it has started to arrive. Any bytes count, keep-alives included. The same error; `askWithFiles` sends the request again once, unless text of it was shown. 0 waits without end. Not used for part reads. |
243
254
  | `id` | `string` | `model` | The name of the model in `usage.byModel` and in messages. |
@@ -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).