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
@@ -11,6 +11,7 @@ import {
11
11
  classifyModelError,
12
12
  DEFAULT_FIRST_BYTE_TIMEOUT_MS,
13
13
  DEFAULT_IDLE_TIMEOUT_MS,
14
+ errorInfoOf,
14
15
  errorKindOf,
15
16
  isRetryableStatus,
16
17
  ModelRequestError,
@@ -18,7 +19,7 @@ import {
18
19
  retryAfterMs,
19
20
  withRetry,
20
21
  } from "llm-large-files";
21
- import type { EventError, IdleWaits, ModelErrorClass, ModelErrorKind, RetryNotice, RetryOptions, SseHooks } from "llm-large-files";
22
+ import type { ErrorCategory, ErrorInfo, EventError, IdleWaits, ModelErrorClass, ModelErrorKind, RetryNotice, RetryOptions, SseHooks } from "llm-large-files";
22
23
  ```
23
24
 
24
25
  Contents:
@@ -27,6 +28,7 @@ Contents:
27
28
  [`ModelErrorClass`](#modelerrorclass),
28
29
  [`classifyModelError`](#classifymodelerrorstatus-message-signals),
29
30
  [`errorKindOf`](#errorkindoferror), [other error classes](#other-error-classes),
31
+ [`errorInfoOf`](#errorinfooferror) and [`ErrorInfo`](#errorinfo),
30
32
  [errors in events: `EventError`](#errors-in-events-eventerror)
31
33
  - Retries: [`withRetry`](#withretryrun-options), [`RetryOptions`](#retryoptions),
32
34
  [`RetryNotice`](#retrynotice), [retries in events](#retries-in-events),
@@ -53,6 +55,7 @@ declare class ModelRequestError extends Error {
53
55
  readonly retryAfterMs?: number;
54
56
  readonly limitTokens?: number;
55
57
  readonly requestedTokens?: number;
58
+ static is(value: unknown): value is ModelRequestError; // new in 0.9.0
56
59
  }
57
60
  ```
58
61
 
@@ -308,21 +311,192 @@ errorKindOf({ kind: "quota" }); // "quota": any
308
311
  ### Other error classes
309
312
 
310
313
  The adapters' errors are `ModelRequestError`s, so `instanceof ModelRequestError` and
311
- `errorKindOf` work for all three. The three errors of the documents are not, and have no `kind`.
314
+ `errorKindOf` work for all three. The four errors of the documents are not, and have no `kind`.
312
315
 
313
316
  | Class | Exported from | Extends | Fields of its own | Reference |
314
317
  |---|---|---|---|---|
315
318
  | `GeminiRequestError` | `llm-large-files/gemini` | `ModelRequestError` | none | [gemini.md](./gemini.md) |
316
319
  | `OpenAiRequestError` | `llm-large-files/openai` | `ModelRequestError` | `param`, `code` | [openai.md](./openai.md) |
317
320
  | `AnthropicRequestError` | `llm-large-files/anthropic` | `ModelRequestError` | `type` | [anthropic.md](./anthropic.md) |
318
- | `DocumentsNotFoundError` | `llm-large-files`, `llm-large-files/gemini` | `Error` | `ids` | [ask.md](./ask.md#documentsnotfounderror) |
319
- | `DocumentStoreError` | `llm-large-files` | `Error` | `code`, `syscall`, and the caught error as `cause` | [library.md](./library.md#documentstoreerror) |
320
- | `IngestError` | `llm-large-files` | `Error` | `code`, `status`, `retryAfterMs`, and the extractor's error as `cause`. New in 0.8.0 | [library.md](./library.md#ingesterror) |
321
+ | `DocumentsNotFoundError` | `llm-large-files`, `llm-large-files/gemini` | `Error` | `ids`; `code` `"documents_missing"` and `status` 404, new in 0.9.0 | [ask.md](./ask.md#documentsnotfounderror) |
322
+ | `DocumentStoreError` | `llm-large-files` | `Error` | `code`, `syscall`, and the caught error as `cause`; `operation` (`"read"`, `"write"`, `"delete"`, `"sweep"`) and `status` 503, new in 0.9.0 | [library.md](./library.md#documentstoreerror) |
323
+ | `DocumentScopeError` | `llm-large-files` | `TypeError` | `code` (`"scope_required"`, `"scope_invalid"`, `"scope_missing"`). New in 0.9.0. The upload handlers answer `"scope_missing"` `404` "This chat no longer exists.", and the other two `500` | [library.md](./library.md#documentscopeerror) |
324
+ | `IngestError` | `llm-large-files` | `Error` | `code`, `status`, `retryAfterMs`, and the extractor's error as `cause`. New in 0.8.0. Code `"exists"` (status 409) is new in 0.9.0 | [library.md](./library.md#ingesterror) |
325
+
326
+ `DocumentStoreError` is the store failing: retry the call. `DocumentScopeError` is a scope the call
327
+ should not have given, or should have: the same call fails again. The message of
328
+ `DocumentStoreError` names its operation, never a path or a host:
329
+
330
+ | `operation` | Message |
331
+ |---|---|
332
+ | `"read"` | `The document store could not be read (EIO). Try again.` |
333
+ | `"write"` | `The document could not be stored (EIO). Try again.` |
334
+ | `"delete"` | `The document could not be deleted (EIO). Try again.` |
335
+ | `"sweep"` | `The document store could not be cleaned up (EIO). Try again.` |
336
+ | code `"NEWER_VERSION"` | `The document was stored by a newer version of llm-large-files. Try again.` |
337
+
338
+ The library throws `"read"` from every look-up, `"write"` from `files.ingest` (code `"ETIMEDOUT"`
339
+ for a write that did not end within `ingestTimeoutMs`), `"delete"` from `files.delete` and
340
+ `"sweep"` from `files.sweep`. Until 0.9.0 the errors of writes came out as the store threw them. A
341
+ `DocumentScopeError` the store throws is never wrapped. Since 0.10.0 a store call that does not end
342
+ within `storeTimeoutMs` (30 s by default) is one of code `"ETIMEDOUT"`, and one refused at once
343
+ because `maxStuckStoreCalls` calls of the process are stuck is one of code `"STORE_STUCK"`
344
+ ([`LargeFilesOptions`](./library.md#largefilesoptions)).
345
+
346
+ #### Telling errors apart across copies of the package
347
+
348
+ New in 0.9.0. Each of `ModelRequestError`, `DocumentsNotFoundError`, `DocumentStoreError`,
349
+ `DocumentScopeError` and `IngestError` has a static `is(value)`. It is `true` for an error of that
350
+ class, or of a class that extends it (`GeminiRequestError` for `ModelRequestError.is`), also when
351
+ another copy of the package made it: two versions installed side by side, or one bundled into a
352
+ dependency. `instanceof` is `false` then. `is` reads a mark that every copy puts on its errors
353
+ (`Symbol.for`), so it never throws, and is `false` for an object that only has the same `name`.
354
+
355
+ ```ts
356
+ import { DocumentsNotFoundError, DocumentStoreError, IngestError } from "llm-large-files";
357
+
358
+ function statusOf(error: unknown): number {
359
+ if (IngestError.is(error) || DocumentStoreError.is(error) || DocumentsNotFoundError.is(error)) return error.status;
360
+ return 500;
361
+ }
362
+ ```
363
+
364
+ ### errorInfoOf(error)
365
+
366
+ ```ts
367
+ declare function errorInfoOf(error: unknown): ErrorInfo;
368
+ ```
369
+
370
+ New in 0.9.0. What any error of the library means, in one record: its category and code, the HTTP
371
+ status to answer with, whether to retry it, and an English message that is safe to show. Switch on
372
+ `category` and `code` to show your own words, in your users' language. It is built on the rules of
373
+ [`EventError`](#errors-in-events-eventerror): a model error is told by its kind, never in the
374
+ provider's words.
375
+
376
+ - It reads the errors of every copy of the package, by their brand (`IngestError.is` and the
377
+ others), like the [`is` methods](#telling-errors-apart-across-copies-of-the-package).
378
+ - It also reads an [`EventError`](#errors-in-events-eventerror), the plain data of `run.error` and
379
+ `ingest.error`, so that a `map` of `writeSseEvents` can word a failed question: an `IngestError`'s
380
+ by its `code`, a `DocumentStoreError`'s and a `DocumentsNotFoundError`'s by their `name` (keeping
381
+ their message, which the library wrote), a `DocumentScopeError`'s by its `name` and `code`, a
382
+ model's by its `kind`, an `AbortError`'s, a `TypeError`'s and a `RangeError`'s by their name (the
383
+ `ingest.error` of a call `ingest` refused is `"input"`, as the error), and a Node system error's by the code in
384
+ the library's words for it ("The file could not be read from storage (ELOOP).", "Upload failed
385
+ (ELOOP)."). The `ingest.error` of the
386
+ [upload handlers](./node.md#upload-with-progress-accept-textevent-stream) is read the same way.
387
+ - Plain data with none of these is `"unknown"`, never a model's error: a model's `EventError`
388
+ always has its `kind`, the kind its status, message and `code` give when the error had none of
389
+ its own (an error of an adapter of yours, or `httpError` of the testing helpers). Plain data
390
+ thrown, such as `{ status: 429, message }`, is not an adapter's error: it reads as
391
+ `"unknown"`, and so does its `EventError`, which has no `kind`. Its `status` is kept when it is
392
+ 400 to 599 (the `ingest.error` of a hook of yours that threw a `409`), else 500, and it is
393
+ `retryable` for 429, 502, 503 and 504. Before, such data with a status was read as a model's
394
+ error: a store that failed (503) and a deleted chat (404) were "The model failed on its side" and
395
+ "The model refused the request".
396
+ - The `message` of a model's `EventError` is kept when it is the library's own words for its kind
397
+ ("The model failed on its side (HTTP 503)."), whatever status they name: the upload handlers'
398
+ `ingest.error` names the provider's status in its message and the answer's (502) in `status`.
399
+ Any other words are replaced by the library's, from its kind and `status`.
400
+ - It never throws. A value it cannot read is `"unknown"`.
401
+ - `message` never holds a path, a host, a stack or a provider's words. Log the error itself, with
402
+ its `cause`, on the server.
403
+
404
+ | Error | `category` | `code` | `status` | `retryable` | `message` |
405
+ |---|---|---|---|---|---|
406
+ | [`IngestError`](./library.md#ingesterror) | `"ingest"` | its code: `"too_large"`, `"busy"`… | its status: 422, 413, 409, 503 | `true` for `"busy"` only, with its `retryAfterMs` | its message; for `"unreadable"`, "This file could not be read. It may be damaged, or not of the type its name says.", since its own is a parser's |
407
+ | [`DocumentStoreError`](./library.md#documentstoreerror) | `"store"` | its code, when it has one that reads as a code (capital letters, digits and `_`, as its message shows it): `"EIO"`, `"ETIMEDOUT"`, `"NEWER_VERSION"`. A driver may put a host in `code`: such a code is left out | 503 | `true`, `retryAfterMs` 5,000 | its message: "The document could not be stored (EIO). Try again." |
408
+ | [`DocumentsNotFoundError`](./ask.md#documentsnotfounderror) | `"missing"` | `"documents_missing"` | 404 | `false` | its message, which names the IDs |
409
+ | [`DocumentScopeError`](./library.md#documentscopeerror) `"scope_missing"` | `"missing"` | `"scope_missing"` | 404 | `false` | "This chat no longer exists." |
410
+ | [`ModelRequestError`](#modelrequesterror), or an error with a `kind`, or an `Error` with a `status` of 0 or 400 to 599 and no `kind` (an adapter of your own) | `"model"` | its [kind](#modelerrorkind), from its status, message and `code` (`"insufficient_quota"` is `quota`) when it has none of its own | by kind: `context_overflow` 413, `rate_limit` 429, `quota` 503, `timeout` 504, `unreachable` and `server` 502, `auth` and `invalid` 500 | `true` for `rate_limit`, `timeout`, `unreachable` and `server`, with the provider's `retryAfterMs` when it named one | by kind and status: "The model was busy (HTTP 429)." |
411
+ | An `AbortError` (a signal that aborted: the user left, the server stopped the question) | `"aborted"` | `"aborted"` | 503 | `true` | "It was stopped before it finished." |
412
+ | A `TimeoutError` (`AbortSignal.timeout`, a deadline) | `"aborted"` | `"timeout"` | 504 | `true` | "It took too long, and was stopped. Try again." |
413
+ | `DocumentScopeError` `"scope_invalid"` or `"scope_required"`, a `TypeError`, a `RangeError`, and their `EventError` | `"input"` | the scope error's code | 500 | `false` | "Something went wrong." It is a bug of the server, not something the user did. |
414
+ | A Node system error (a `code` and a `syscall`), such as a path `ingest` does not read (`ELOOP`, `EISDIR`, `EINVAL`, `ENOENT`), and its `EventError` | `"unknown"` | its code: `"EACCES"`, when it reads as one (capitals, digits and `_`). A code that does not, such as a host, is left out | 500 | `false` | "Something went wrong (EACCES).", or "Something went wrong." without a code |
415
+ | A Node system error whose code says the server, or a service it needs, could not answer just now ([transient](#transient-system-errors)), and its `EventError`. New in 0.10.0 | `"unknown"` | its code: `"ETIMEDOUT"`, `"ECONNRESET"`… | 503 | `true`, `retryAfterMs` 5,000; for a full disk (`ENOSPC`, `EDQUOT`) 30,000 | "The server could not do this just now (ETIMEDOUT). Try again in a moment."; for a full disk "The server has no room right now (ENOSPC). Try again later." |
416
+ | Plain data (an `EventError`) without a `kind`, and without a `code` or `name` of the rows above | `"unknown"` | none | its `status` when it is 400 to 599, else 500 | `true` for 429, 502, 503 and 504 | "Something went wrong." |
417
+ | Anything else | `"unknown"` | none | 500 | `false` | "Something went wrong." |
418
+
419
+ The statuses of model errors are those a server that asks a model for its user answers: the same as
420
+ `toUserError` in [Errors and reliability](../guides/errors-and-reliability.md#putting-it-together-a-chat-route).
421
+ The upload handlers answer with [`uploadAnswerOf`](./node.md#uploadansweroferror), which follows
422
+ the same rules for the library's errors and keeps the message of an error with a status.
423
+
424
+ #### Transient system errors
425
+
426
+ New in 0.10.0. A Node system error (one with a `code` and a `syscall`) whose code says that the
427
+ server, or a service it needs, could not answer just now is read as one to retry: `errorInfoOf`
428
+ gives status 503, `retryable: true` and `retryAfterMs` 5,000, and the upload handlers answer it
429
+ `503` with `Retry-After: 5`. A full disk is retryable too, later: 30,000 and `Retry-After: 30`.
430
+ Before 0.10.0 they were `"unknown"`, 500, and not retryable.
431
+
432
+ | Code | What it says |
433
+ |---|---|
434
+ | `ETIMEDOUT` | A call that did not answer in time: a volume, a connection, a path upload whose read did not answer within `ingestTimeoutMs` (new in 0.10.0: it was the `IngestError` `"timeout"`, 422) |
435
+ | `ECONNRESET`, `EPIPE` | A connection cut by the other end |
436
+ | `ECONNREFUSED`, `EHOSTUNREACH`, `ENETUNREACH` | A service, a host or a network that could not be reached |
437
+ | `EAI_AGAIN` | A name that could not be looked up just now |
438
+ | `EAGAIN`, `EBUSY` | A resource busy for a moment |
439
+ | `EMFILE`, `ENFILE` | Too many files open, in the process or the system |
440
+ | `ESTALE` | A handle to a file of a network volume that the server no longer knows |
441
+ | `ENOSPC`, `EDQUOT` | A full disk, or a quota reached: 30 s to wait |
442
+
443
+ The code is read from the error itself, and from the library's words for it in an `EventError`
444
+ ("Upload failed (ETIMEDOUT).", "The file could not be read from storage (ETIMEDOUT)."), whatever
445
+ status that data holds: the `ingest.error` of a 0.9 upload handler said 500. Other codes keep the
446
+ row above: 500, not retryable. A store's error is a `DocumentStoreError`, 503 whatever its code.
447
+
448
+ #### Your own words, in one switch
449
+
450
+ Switch on `category`, and on `code` where you want more detail. This one answers a Hebrew-speaking
451
+ user; [Errors and reliability](../guides/errors-and-reliability.md#your-users-words-errorinfoof) has
452
+ one for every code:
453
+
454
+ ```ts
455
+ import { errorInfoOf } from "llm-large-files";
456
+
457
+ export function hebrewMessage(error: unknown): string {
458
+ const info = errorInfoOf(error);
459
+ switch (info.category) {
460
+ case "ingest": return info.code === "too_large" ? "הקובץ גדול מדי." : "לא ניתן לקרוא את הקובץ.";
461
+ case "store": return "לא ניתן לגשת לקבצים כרגע. נסו שוב בעוד רגע.";
462
+ case "missing": return info.code === "scope_missing" ? "השיחה הזו כבר לא קיימת." : "אחד הקבצים כבר לא זמין. צרפו אותו שוב.";
463
+ case "model": return info.retryable ? "העוזר עמוס כרגע. נסו שוב בעוד דקה." : "העוזר אינו זמין כרגע.";
464
+ case "aborted": return "השאלה נעצרה.";
465
+ default: return "משהו השתבש. נסו שוב מאוחר יותר."; // "input", "unknown", and later categories
466
+ }
467
+ }
468
+ ```
469
+
470
+ ### `ErrorInfo`
471
+
472
+ ```ts
473
+ type ErrorCategory = "ingest" | "store" | "missing" | "model" | "aborted" | "input" | "unknown";
474
+
475
+ type ErrorInfo = {
476
+ category: ErrorCategory;
477
+ code?: string;
478
+ status: number;
479
+ retryable: boolean;
480
+ retryAfterMs?: number;
481
+ message: string;
482
+ };
483
+ ```
484
+
485
+ New in 0.9.0. What [`errorInfoOf()`](#errorinfooferror) returns.
486
+
487
+ | Field | Type | What it holds |
488
+ |---|---|---|
489
+ | `category` | `ErrorCategory` | What kind of failure: a refused file, the store, documents or a chat that are gone, a model request, a stop, a mistake in the call, or anything else. A switch over every category must handle those a later version adds. |
490
+ | `code` | `string`, optional | More precise than the category: an `IngestErrorCode`, a `ModelErrorKind`, `"documents_missing"`, a `DocumentScopeErrorCode`, `"aborted"` or `"timeout"`, or a system error's code. |
491
+ | `status` | `number` | The HTTP status to answer with: 400 to 599. |
492
+ | `retryable` | `boolean` | Whether the same call may work when it is made again, later. |
493
+ | `retryAfterMs` | `number`, optional | How long to wait first, in milliseconds, when that is known. |
494
+ | `message` | `string` | English words a user may see. Never a path, a host, a stack or a provider's words. |
321
495
 
322
496
  ### Errors in events: `EventError`
323
497
 
324
498
  ```ts
325
- type EventError = { message: string; kind?: ModelErrorKind; status?: number; name?: string; code?: IngestErrorCode };
499
+ type EventError = { message: string; kind?: ModelErrorKind; status?: number; name?: string; code?: IngestErrorCode | DocumentScopeErrorCode };
326
500
  ```
327
501
 
328
502
  New in 0.7.0. An error as the events of a question or an ingest carry it ([events](./events.md)):
@@ -332,11 +506,11 @@ included: log that one.
332
506
 
333
507
  | Field | Type | What it holds |
334
508
  |---|---|---|
335
- | `message` | `string` | Words your users may see. A model error is told by its kind and status, never in the provider's words, which can name projects, hosts or keys: "The model was busy (HTTP 429).", "The model's quota has run out (HTTP 429).", "The model could not be reached.", "The model refused the key or token it was sent (HTTP 401)." A Node system error is told by its code ("The file could not be read from storage (ENOENT)."), a `DocumentStoreError` by its own message, which names only its code, and a store that failed to write in `ingest.error` as "The document could not be stored. Try again later.". The library's own errors (a refused file, `DocumentsNotFoundError`, an abort, the error a listener threw) keep their message, and so does any other `Error` without a `kind` or a numeric `status`, such as one an adapter or a token function of your own throws: give such errors a status, or words of your own. A value that is not an `Error` is "Something went wrong.". |
336
- | `kind` | [`ModelErrorKind`](#modelerrorkind), optional | For a model error: its kind, as [`errorKindOf`](#errorkindoferror) reads it. |
337
- | `status` | `number`, optional | For an HTTP error: its status. 0 when no answer came. |
338
- | `name` | `string`, optional | The error's class name: `"ModelRequestError"`, `"GeminiRequestError"`, `"AbortError"`, `"DocumentsNotFoundError"`, `"IngestError"`… |
339
- | `code` | [`IngestErrorCode`](./library.md#ingesterror), optional | For a file `ingest` could not read: why (`"too_large"`, `"no_text"`, `"busy"`…). New in 0.8.0. |
509
+ | `message` | `string` | Words your users may see. A model error is told by its kind and status, never in the provider's words, which can name projects, hosts or keys: "The model was busy (HTTP 429).", "The model's quota has run out (HTTP 429).", "The model could not be reached.", "The model refused the key or token it was sent (HTTP 401)." A Node system error is told by its code ("The file could not be read from storage (ENOENT)."), when the code reads as one (capitals, digits and `_`; since 0.9.0: a driver may put a host in `code`), a `DocumentStoreError` by its own message, which names only its code, and a store that failed to write in `ingest.error` as "The document could not be stored. Try again later." (with `name` `"DocumentStoreError"` and `status` 503 since 0.9.0). The library's own errors (a refused file, `DocumentsNotFoundError`, an abort, the error a listener threw) keep their message, and so does any other `Error` without a `kind` or a numeric `status`, such as one an adapter or a token function of your own throws: give such errors a status, or words of your own. A status that is not a model's (not 0 or 400 to 599), and plain data thrown with a status (`{ status: 429, message }`), are "Something went wrong.", as `errorInfoOf` reads them (since 0.9.0; before, a model's words). A value that is not an `Error` is "Something went wrong.". |
510
+ | `kind` | [`ModelErrorKind`](#modelerrorkind), optional | For a model error: its kind, as [`errorKindOf`](#errorkindoferror) reads it. Since 0.9.0 also for an `Error` with a `status` of 0 or 400 to 599 and no `kind` of its own (an adapter of yours, `httpError` of the testing helpers): the kind its status, message and `code` give ([`classifyModelError`](#classifymodelerrorstatus-message-signals)), as its message is told and as `errorInfoOf` reads the error. Not for plain data thrown (an object of no class), and never for the library's own errors (`DocumentStoreError`, `DocumentsNotFoundError`, `IngestError`, `DocumentScopeError`). |
511
+ | `status` | `number`, optional | For an HTTP error: its status. 0 when no answer came. Since 0.9.0 also the status of the library's own errors: 503 for `DocumentStoreError`, 404 for `DocumentsNotFoundError`, and for an `IngestError` the status of every code but those of 422 (413, 409, 503). |
512
+ | `name` | `string`, optional | The error's class name: `"ModelRequestError"`, `"GeminiRequestError"`, `"AbortError"`, `"DocumentsNotFoundError"`, `"DocumentStoreError"`, `"DocumentScopeError"`, `"IngestError"`, `"TypeError"`… Since 0.9.0 a store's own subclass of `DocumentStoreError` (one named `"BucketStoreError"`) is named `"DocumentStoreError"` here, and an extractor's own subclass of `IngestError` (one named `"OcrRefusal"`) `"IngestError"`, so that `errorInfoOf` reads them as a store's failure and a refused file. |
513
+ | `code` | [`IngestErrorCode`](./library.md#ingesterror) or [`DocumentScopeErrorCode`](./library.md#documentscopeerror), optional | For a file `ingest` could not read: why (`"too_large"`, `"no_text"`, `"busy"`, `"exists"`…). New in 0.8.0. Since 0.9.0 also, with `name` `"DocumentScopeError"`, why a scope could not be used: `"scope_missing"` (the chat was deleted), `"scope_invalid"` or `"scope_required"`. A table typed `Record<IngestErrorCode, string>` must check the code before it looks it up. |
340
514
 
341
515
  To show your own words, map the error by `kind`, `status` or `name`, as you would the thrown one
342
516
  (see [Errors and reliability](../guides/errors-and-reliability.md)).
@@ -358,8 +532,9 @@ declare function withRetry<T>(run: () => Promise<T>, options?: {
358
532
  Runs `run`, and runs it again when it failed in a way that another try can mend. It is the rule
359
533
  `askWithFiles` and `askGemini` use for each chat request, and the whole-file part reads use for
360
534
  each part. It reads three fields of what `run` throws, so any error with them works, not only a
361
- `ModelRequestError`: `status` (a finite number), `kind` and `retryAfterMs`. `run` is called again
362
- as it is: build the request inside it.
535
+ `ModelRequestError`: `status` (a finite number), `kind` and `retryAfterMs`. An error without a
536
+ `kind` also has its `message` and `code` read, for the kind they give. `run` is called again as it
537
+ is: build the request inside it.
363
538
 
364
539
  | Option | Type | Default | What it does |
365
540
  |---|---|---|---|
@@ -375,7 +550,12 @@ What happens to an error, checked in this order:
375
550
  2. Its `kind` is `"quota"` or `"context_overflow"`: it is thrown. A wait does not mend a daily cap
376
551
  or an empty balance, and only a smaller request passes a limit of size, whatever its status
377
552
  (OpenAI's request larger than the tokens a minute allows comes as 429). 0.5.0, which had no
378
- kinds, sent both again when their status was 0, 408, 409, 429 or 500 and more.
553
+ kinds, sent both again when their status was 0, 408, 409, 429 or 500 and more. An error without
554
+ a `kind` of its own that has a `status` of 0 or 400 to 599 (an adapter of yours, `httpError`)
555
+ has the kind [`classifyModelError`](#classifymodelerrorstatus-message-signals) gives its status,
556
+ message and `code`, as its events have it: a 429 of code `"insufficient_quota"` is a quota, and
557
+ is thrown. Plain data thrown (an object of no class) has no kind. New in 0.9.0: such an error
558
+ was sent again by its status, while its `retry` event and `errorInfoOf` called it not retryable.
379
559
  3. The provider said not to send it again: the adapter took note of Claude's
380
560
  `x-should-retry: false`. It is thrown, whatever its status. The note is internal: an error of
381
561
  your own cannot carry it.
@@ -242,9 +242,11 @@ for `onText`.
242
242
  | `ms` | `number` | From `request.start` to now, retries and their waits included. |
243
243
  | `outcome` | `"ok" \| "error"` | How it ended. |
244
244
  | `usage` | `Partial<ModelUsage>`, optional | What the provider said this request used. For a `"reader"` request, the sum of all its sends. |
245
- | `finish` | `"complete" \| "cut" \| "filtered" \| "malformed_call"`, optional | How the reply ended, in the library's words. Chat requests only. |
245
+ | `finish` | `"complete" \| "cut" \| "filtered" \| "malformed_call" \| "other"`, optional | How the reply ended, in the library's words. Chat requests only. `"other"` is new in 0.10.0: `gemini()` gives it for Gemini's `OTHER`, `LANGUAGE` and four more reasons. |
246
246
  | `finishReason` | `string`, optional | How the reply ended, in the provider's words. Chat requests only. |
247
247
  | `toolCalls` | `number`, optional | How many tool calls the reply asked for. Chat requests with outcome `"ok"`. |
248
+ | `responseId` | `string`, optional | The provider's ID of the response (`ModelReply.responseId`), when the adapter gives one: to find the request in the provider's logs. Chat requests only. New in 0.10.0. |
249
+ | `responseModel` | `string`, optional | The model that wrote the response, as the provider names it (`ModelReply.responseModel`), when the adapter gives one: the exact version behind an alias. Chat requests only. New in 0.10.0. |
248
250
  | `error` | [`EventError`](#errors-eventerror), optional | With outcome `"error"`: why. |
249
251
 
250
252
  A request that failed does not always end the question. An overflow is made smaller and sent
@@ -284,7 +286,7 @@ outcome that says so.
284
286
  | `outcome` | [`ToolOutcome`](#tooloutcome) | How it ended. |
285
287
  | `ms` | `number` | From `tool.start` to now. Ends come in call order, so this includes the wait for the calls before it in the turn. |
286
288
  | `step` | `string`, optional | The finished step, as `onStep` got it. Absent for `"not_run"` (one `calls_not_run` notice says it for all of them), and for a call ended because its turn failed. |
287
- | `evidence` | [`Evidence`](./ask.md#evidence), optional | What the call showed the model, with this `callId`. Each entry of `AskResult.evidence` has its `callId` too, so you can find the call it came from. |
289
+ | `evidence` | [`Evidence`](./ask.md#evidence), optional | What the call showed the model, with this `callId`. Each entry of `AskResult.evidence` that a call made has its `callId` too, so you can find the call it came from. The entries of the files sent whole (`tool: "inline"`, new in 0.10.0) have none: no call made them. |
288
290
  | `resultCharacters` | `number`, optional | The characters of the JSON result the model was sent. 0 for a call ended because its turn failed. |
289
291
  | `error` | `string`, optional | When the result the model was sent is an error (outcomes `"error"`, `"refused"`, `"not_run"`): its message. For a call ended because its turn failed: why, as in `EventError.message`. |
290
292
  | `result` | [`ToolResult`](./tools.md#toolresult), optional | The result the model was sent. Only with `eventResults: true`: it holds document text. |
@@ -296,6 +298,8 @@ outcome that says so.
296
298
  | `code` | [`NoticeCode`](#noticecode-and-noticedisplay) | What happened. |
297
299
  | `message` | `string` | One line of plain text, word for word what the callback got when there was one. |
298
300
  | `display` | [`NoticeDisplay`](#noticecode-and-noticedisplay) | Where it went in the callbacks: `"activity"`, `"step"` or `"none"`. |
301
+ | `documentIds` | `string[]`, optional | The documents the notice is about: those left out (`"documents_missing"`), the one found gone (`"document_gone"`), the one whose pages were not looked at (`"page_looks_busy"`). New in 0.9.0. |
302
+ | `discardText` | `true`, optional | With `"answer_filtered"` under `AskOptions.filteredAnswer: "drop"`: text of the question was already sent (`text` events, `onText`), and the answer is dropped. The page takes away the text it showed for the question, and shows its own message. Absent when no text was sent. See [filtered and cut answers](../guides/errors-and-reliability.md#filtered-and-cut-answers). New in 0.10.0. |
299
303
 
300
304
  #### `run.finish`
301
305
 
@@ -351,7 +355,9 @@ These hold for every run.
351
355
  from being asked for again. `no_room` and `results_shortened` come while a turn is planned,
352
356
  before its `tool.start` events. `room_made`, `nudged` and `answer_again` come after the
353
357
  `request.end` of the request they are about, before the next `request.start`. `blocked` comes
354
- right after the `request.end` of the reply the provider refused.
358
+ right after the `request.end` of the reply the provider refused. `answer_filtered` and
359
+ `answer_cut` come last, right before `run.finish`, after every `text` event of the question
360
+ (new in 0.10.0).
355
361
 
356
362
  ### `RequestRole` and `RequestPurpose`
357
363
 
@@ -385,9 +391,10 @@ How a tool call ended (`tool.end.outcome`):
385
391
  |---|---|
386
392
  | `"ok"` | Ran and returned a result. |
387
393
  | `"error"` | Ran, or was checked, and returned `{ error }`: bad arguments, a document it cannot read, a regex that took too long, a call that did not arrive whole. Or it was ended because its turn failed (the question was stopped, or a listener threw): then the model was sent nothing. |
388
- | `"refused"` | Was not run, for a limit of the question: its cost, the room left in the conversation, the reads of a whole file. |
394
+ | `"refused"` | Was not run, for a limit of the question: its cost, the room left in the conversation, the reads of a whole file. Since 0.10.0 also a call of a tool the question does not have, and a result that `AskOptions.onToolResult` rejected (`{ reject }`): the model was sent the error. |
389
395
  | `"already_shown"` | Repeated a call whose result is still in the conversation, and was answered with a pointer to it. |
390
396
  | `"not_run"` | Was over `limits.callsPerTurn` for its turn. |
397
+ | `"redacted"` | Ran, and `AskOptions.onToolResult` replaced its result: the model was sent the replacement, with `redacted: true`. `evidence`, `step`, `resultCharacters` and `result` tell the replacement. New in 0.10.0. |
391
398
 
392
399
  ### `NoticeCode` and `NoticeDisplay`
393
400
 
@@ -404,12 +411,22 @@ to `onStep` and `AskResult.steps`, `"none"` to neither. The `"none"` notices are
404
411
  | `"results_shortened"` | step | Older tool results were shortened to make room. | "Older tool results were shortened to make room in the request" |
405
412
  | `"answer_again"` | step | The answer came back without text and was asked for again. | "The model's answer was empty: it was asked again", "The model called a tool instead of answering: it was asked again for the answer" |
406
413
  | `"calls_not_run"` | step | A turn had more calls than `limits.callsPerTurn`. | "2 more tool calls in this turn were not run (at most 8 a turn)" |
407
- | `"budget_reached"` | none | `limits.inputTokens` or `limits.requests` was reached, so no more tools run, or the answer is not asked for again. Once a question. | "No more tools: the question reached its limit of input tokens or requests." |
414
+ | `"budget_reached"` | none | The ceiling of input tokens (`limits.inputTokens`, or the default since 0.10.0: the next tool request and the answer would pass it) or `limits.requests` was reached, so no more tools run, or the answer is not asked for again. Once a question. | "No more tools: the question reached its limit of input tokens or requests." |
408
415
  | `"no_room"` | none | There is no room left in the conversation for another tool result. Once a question. | "There is no room left in this conversation for another tool result." |
409
416
  | `"blocked"` | none | The provider refused the prompt (`AskResult.blockReason`). | "The provider refused the prompt: SAFETY." |
417
+ | `"documents_missing"` | none | Documents of `documentIds` were missing, and the question is answered without them (`onMissingDocuments: "drop"`, see [missing documents](./ask.md#missing-documents)). Before the `plan` event, once a question. `documentIds` names them; `AskResult.missing` lists them too. New in 0.9.0. | "1 of the attached files is no longer available: the question is answered without it." |
418
+ | `"document_gone"` | none | A tool call found a document gone during the question: deleted or expired since it was planned. The model was told so in the call's result ("‹id› is no longer available on the server. Ask the user to attach the file again."). Once for each document. `documentIds` names it. New in 0.9.0. | "app.log is no longer available on the server." |
419
+ | `"page_looks_busy"` | none | A look at pages waited longer than `pageLookWaitMs` for its turn (`maxConcurrentPageLooks`, [library](./library.md#largefilesoptions)) and was not run. The model was told the server is busy. `documentIds` names the PDF. New in 0.9.0. | "The pages of report.pdf were not looked at: the server was busy with other looks at pages." |
420
+ | `"answer_filtered"` | none | A filter of the provider stopped the answer, with or without text (`AskResult.finish` `"filtered"`). Right before `run.finish`. With `filteredAnswer: "drop"`, `discardText` says when text was already sent. New in 0.10.0. | "A filter of the provider stopped the answer before it was finished.", "… before any text came.", or under `"drop"` "… before it was finished: its text was dropped." |
421
+ | `"answer_cut"` | none | The answer stopped at the output limit (`AskResult.finish` `"cut"`). Right before `run.finish`. The answer is kept. New in 0.10.0. | "The answer stopped at the model's output limit, so it is incomplete.", or "… before any text came." |
410
422
 
411
423
  The messages are written for people and may change between versions. Test `code`, not `message`.
412
424
 
425
+ New codes come with new versions: 0.9.0 added `"documents_missing"`, `"document_gone"` and
426
+ `"page_looks_busy"`, and 0.10.0 `"answer_filtered"` and `"answer_cut"`. In TypeScript, a `switch`
427
+ over `NoticeCode` that must handle every code (one with a `never` check in its `default`) no longer
428
+ compiles until it handles them. Give such a switch a `default` that shows nothing, or the message.
429
+
413
430
  ### How the callbacks map to events
414
431
 
415
432
  The callbacks of earlier versions get nothing the events do not also say:
@@ -434,20 +451,25 @@ An error in an event is plain data, safe to send to a browser:
434
451
  | Field | Type | What it holds |
435
452
  |---|---|---|
436
453
  | `message` | `string` | A message your users may see. |
437
- | `kind` | `ModelErrorKind`, optional | For a model error: what kind it is (`errorKindOf`): `"rate_limit"`, `"timeout"`, `"auth"`… |
438
- | `status` | `number`, optional | For an HTTP error: its status. 0 when no answer came. |
439
- | `name` | `string`, optional | The error's class name: `"ModelRequestError"`, `"AbortError"`, `"DocumentsNotFoundError"`, `"IngestError"`… |
440
- | `code` | `IngestErrorCode`, optional | For a file `ingest` could not read: why, as [`IngestError.code`](./library.md#ingesterror) (`"too_large"`, `"no_text"`, `"hostile"`, `"busy"`…), so that a page can say it in its own words. New in 0.8.0. |
454
+ | `kind` | `ModelErrorKind`, optional | For a model error: what kind it is (`errorKindOf`): `"rate_limit"`, `"timeout"`, `"auth"`… Since 0.9.0 also for an error with a `status` (0, or 400 to 599) and no `kind` of its own, such as one an adapter of yours throws: the kind its status gives (`classifyModelError`). So a model's error always has its kind. Plain data thrown (`{ status: 429, message }`) gets none. |
455
+ | `status` | `number`, optional | For an HTTP error: its status. 0 when no answer came. Since 0.9.0 also 503 for a `DocumentStoreError` and 404 for a `DocumentsNotFoundError`. |
456
+ | `name` | `string`, optional | The error's class name: `"ModelRequestError"`, `"AbortError"`, `"DocumentsNotFoundError"`, `"DocumentScopeError"`, `"IngestError"`… Since 0.9.0 a subclass of `DocumentStoreError` or of `IngestError` with a name of its own is named `"DocumentStoreError"` or `"IngestError"` here, so that `errorInfoOf` reads it as its class. |
457
+ | `code` | `IngestErrorCode` or `DocumentScopeErrorCode`, optional | For a file `ingest` could not read: why, as [`IngestError.code`](./library.md#ingesterror) (`"too_large"`, `"no_text"`, `"hostile"`, `"busy"`…), so that a page can say it in its own words. New in 0.8.0. Since 0.9.0 also the code of a [`DocumentScopeError`](./library.md#documentscopeerror), with `name` `"DocumentScopeError"`: `"scope_missing"` when the chat was deleted. |
441
458
 
442
459
  How `message` is written:
443
460
 
444
- - **A model error** (one with a `kind` or a numeric `status`) is told by its kind and status, in
461
+ - **A model error** (one with a `kind`, or an error with a `status` of 0 or 400 to 599, other than
462
+ the library's own errors and plain data thrown) is told by its kind and status, in
445
463
  the library's words: "The model was busy (HTTP 429).", "The model failed on its side (HTTP
446
464
  503).", "The model refused the key or token it was sent (HTTP 401).". The provider's own words
447
465
  can name projects, hosts or keys: they stay on the error that is thrown, for your logs.
448
466
  - **A storage error** (`DocumentStoreError`) is told by its own message, which names only the
449
- code. **A Node system error** (one with a `code` and a `syscall`) is told as "The file could not
450
- be read from storage (ENOENT).".
467
+ code, and so is a `DocumentsNotFoundError`, which names the IDs. **A Node system error** (one with a `code` and a `syscall`) is told as "The file could not
468
+ be read from storage (ENOENT).". Since 0.9.0 its code is told only when it reads as one
469
+ (capitals, digits and `_`): a driver may put a host in `code`.
470
+ - **Another `status`** (not 0 or 400 to 599), and plain data thrown with a status
471
+ (`{ status: 429, message }`), is told as "Something went wrong.", as `errorInfoOf` reads it.
472
+ New in 0.9.0: such an error was told in a model's words ("The model was busy (HTTP 429).").
451
473
  - **Any other `Error`** is told by its message as it is written: the library's own refusals and
452
474
  messages ("app.log is empty."), and the reason of an abort. That includes errors your own code
453
475
  throws: an adapter, an extractor or a listener of your own. Keep secrets out of their messages,
@@ -455,16 +477,19 @@ How `message` is written:
455
477
  - **Anything that is not an `Error`** is told as "Something went wrong.".
456
478
 
457
479
  An [`IngestError`](./library.md#ingesterror) is told by its message, with `name: "IngestError"` and
458
- its `code`, and with its `status` only for `"too_large"` (413) and `"busy"` (503): that status is the
459
- answer of an upload, not of a model, and the others are all 422. Until 0.8.0 the same refusals were
480
+ its `code`, and with its `status` only for `"too_large"` (413), `"exists"` (409, new in 0.9.0) and
481
+ `"busy"` (503): that status is the answer of an upload, not of a model, and the others are all 422. Until 0.8.0 the same refusals were
460
482
  plain `Error`s, and their events said `name: "Error"`; a test that expects that needs
461
- `"IngestError"`. A store that failed, an abort and a listener that threw have no `code`.
483
+ `"IngestError"`. A [`DocumentScopeError`](./library.md#documentscopeerror) is told by its message, with
484
+ `name: "DocumentScopeError"` and its `code` (new in 0.9.0). A store that failed, an abort and a
485
+ listener that threw have no `code`.
462
486
 
463
487
  Where errors appear: `retry.error`, `request.end.error`, `run.error.error` and `ingest.error.error`
464
488
  are `EventError`s. `tool.end.error` is a string: the error the model was sent, or, for a call
465
489
  ended because its turn failed, the `message` of the turn's error. An ingest whose store failed to
466
490
  write the document says only "The document could not be stored. Try again later.", whatever the
467
- store threw.
491
+ store threw. Since 0.9.0 that error also has `name: "DocumentStoreError"` and `status: 503`, so it
492
+ reads as the error `ingest` throws.
468
493
 
469
494
  To show your own words, map the error by `kind` and `name`, as `examples/server.ts` does in its
470
495
  `userMessage`, and the error of an ingest by `code`. [Errors and reliability](../guides/errors-and-reliability.md) says what each kind
@@ -478,19 +503,29 @@ text of the documents:
478
503
  - `tool.end` has no `result`;
479
504
  - the result in `run.finish` and `run.error` has no `toolTurns`, and its `plan` names each
480
505
  document as a [`DocumentRef`](#plan) (id, name, format, estimated tokens, pages) instead of its
481
- full `DocumentMeta`, which holds the `owner`, the headings of the outline and, for a log, the
482
- templates of its most frequent lines.
506
+ full `DocumentMeta`, which holds the `owner`, the `scope` (such as `[userId, chatId]`), the
507
+ `sha256` of the file, the `original` with its `ref` (where your application keeps the file), the
508
+ headings of the outline and, for a log, the templates of its most frequent lines.
509
+
510
+ `missing` (new in 0.9.0) is there as in `AskResult`: the IDs of the documents left out.
483
511
 
484
512
  ```ts
485
513
  type EventResult = Omit<AskResult, "toolTurns" | "plan"> & {
486
514
  toolTurns?: ToolTurn[];
487
- plan: { inline: Array<DocumentRef | DocumentMeta>; searchable: Array<DocumentRef | DocumentMeta> };
515
+ // Since 0.10.0 the full metadata here has no owner, scope, sha256, original or metaVersion, in its
516
+ // type (DocumentMeta before) and at run time.
517
+ plan: {
518
+ inline: Array<DocumentRef | Omit<DocumentMeta, "owner" | "scope" | "sha256" | "original" | "metaVersion">>;
519
+ searchable: Array<DocumentRef | Omit<DocumentMeta, "owner" | "scope" | "sha256" | "original" | "metaVersion">>;
520
+ };
488
521
  };
489
522
  ```
490
523
 
491
524
  With `eventResults: true`, `tool.end.result` is the result the model was sent, and
492
525
  `run.finish.result` and `run.error.partial` have `toolTurns` and the full plan. Use it for a log or an
493
- audit trail on your server, not for a browser. The result `askWithFiles` returns, `askStream`'s
526
+ audit trail on your server, not for a browser. Since 0.10.0 even that plan has no `owner`, `scope`,
527
+ `sha256`, `original` or `metaVersion`: they stay on the server. Read them from `AskResult.plan`,
528
+ which keeps the full metadata. The result `askWithFiles` returns, `askStream`'s
494
529
  `result`, and `partialResultOf(error)` always have `toolTurns`: keep them from there, with the
495
530
  conversation.
496
531
 
@@ -766,9 +801,9 @@ The events of `files.ingest`, through `IngestInput.onEvent`:
766
801
 
767
802
  | `type` | Fields | When |
768
803
  |---|---|---|
769
- | `ingest.start` | `name: string`, `format: DocumentFormat`, `bytes: number` | Always first, before the input is checked. `name` is the file name as it will be stored; `format` is what the name and type say (a text file can still become a log); `bytes` is the size of the file. |
804
+ | `ingest.start` | `name: string`, `format: DocumentFormat`, `bytes: number` | Always first, before the input is checked. `name` is the file name as it will be stored; `format` is what the name and type say (a text file can still become a log); `bytes` is the size of the file. For a file ingested from a path ([`IngestFromSource`](./library.md#ingestfromsource), new in 0.9.0), the size `lstat` gives, before any of the file is read; 0 when the path cannot be looked at. |
770
805
  | `ingest.progress` | `stage: IngestStage`, `done: number`, `total?: number`, `unit: "pages" \| "parts" \| "bytes" \| "characters"` | How far a stage is. `total` is absent when it is not known. See [Stages and units](#stages-and-units). |
771
- | `ingest.finish` | `document: DocumentSummary` | Always last when the document is stored. `document` is [`toDocumentSummary(meta)`](./library.md#documentsummary): the summary an upload's `201` holds, without `owner`. |
806
+ | `ingest.finish` | `document: DocumentSummary`, `app?: Record<string, unknown>` | Always last when the document is stored. `document` is [`toDocumentSummary(meta)`](./library.md#documentsummary): the summary an upload's `201` holds, without `owner`. `app` is set only by the upload handler of `llm-large-files/node`: the `answer` its `onUploaded` returned (new in 0.9.0). `files.ingest` never sets it. |
772
807
  | `ingest.error` | `error: EventError` | Always last when the ingest throws: a refusal, an abort, a store that failed, or a listener that threw. |
773
808
 
774
809
  Each event also has `seq`, `runId` and `time`. Nothing comes after `ingest.finish` or
@@ -825,16 +860,23 @@ events when the request's `Accept` header names `text/event-stream` itself: not
825
860
  not only through a wildcard such as `*/*` or `text/*`.
826
861
 
827
862
  1. The checks that answer with an HTTP error come first, as before, in JSON: a missing name
828
- (`400`), an owner that `getOwner` refused or that is not allowed (its status, or `500`), a body
863
+ (`400`), an owner that `getOwner` refused or that is not allowed (its status, `401` or `500`), a
864
+ scope that `getScope` refused or that is not valid (its status, or `500`; new in 0.9.0), a body
829
865
  another middleware already parsed (`400`), and a file larger than `maxUploadBytes` (`413`).
830
866
  2. Then it answers `200` with Server-Sent Events, written by `writeSseEvents`: `ingest.start`,
831
867
  `ingest.progress`…, and last one of:
832
- - `ingest.finish`, whose `document` is the summary a `201` would hold;
868
+ - `ingest.finish`, whose `document` is the summary a `201` would hold, and whose `app` is the
869
+ `answer` of `onUploaded` (new in 0.9.0). It is sent once `onUploaded` is done;
833
870
  - `ingest.error`, whose `error` is `{ message, status }`, the message and status a JSON answer
834
871
  would have: `422` with the reason for a file that cannot be read ("empty.txt is empty."),
835
- `500` "Upload failed." for a store that failed, and so on, as in the
872
+ `503` "The document could not be stored. Try again." for a store that failed (since 0.9.0),
873
+ `500` "Upload failed (ENOSPC)." for a Node system error, with its code when it reads as one,
874
+ `500` "Upload failed." for any other error, what `onUploaded` threw, and so on, as in the
836
875
  [node reference](./node.md#uploadrequest-response). For a file that could not be read, it also
837
- has `code`, the `IngestError` code (`"empty"`, `"busy"`…; new in 0.8.0).
876
+ has `code`, the `IngestError` code (`"empty"`, `"busy"`…; new in 0.8.0). Since 0.9.0, a store
877
+ that failed also has `name` `"DocumentStoreError"`, and a scope error `name`
878
+ `"DocumentScopeError"` and its `code` (`"scope_missing"`, 404, for a deleted chat), so that
879
+ [`errorInfoOf`](./errors-and-retries.md#errorinfooferror) reads it.
838
880
  3. When the client goes away, the ingest stops and nothing is stored.
839
881
 
840
882
  It always ends with `ingest.finish` or `ingest.error`, even with a `LargeFiles` of your own whose
@@ -941,6 +983,9 @@ executeWithFiles(name: string, args: Record<string, unknown>, limits?: ToolCallL
941
983
  type ToolCallHooks = {
942
984
  onProgress?: ProgressListener;
943
985
  onRequest?: (request: ToolRequestStart) => ToolRequestHandle;
986
+ onDocumentGone?: (documentId: string) => void; // new in 0.9.0
987
+ onPagesBusy?: (documentId: string) => void; // new in 0.9.0
988
+ signal?: AbortSignal; // new in 0.10.0
944
989
  };
945
990
  type ToolRequestStart = { purpose: "part" | "merge" | "final"; detail: string };
946
991
  type ToolRequestHandle = {
@@ -954,8 +999,13 @@ type ToolRequestEnd = { outcome: "ok" | "error"; usage?: GenerateUsage; error?:
954
999
  |---|---|---|
955
1000
  | `onProgress` | `ProgressListener` | Progress of this call, in place of `ToolOptions.onProgress` for it: `{ kind: "map" \| "reduce", message, done, total }`. |
956
1001
  | `onRequest` | `(request: ToolRequestStart) => ToolRequestHandle` | Called as each model request of this call starts, with its `purpose` and `detail` (`"part 3 of 12"`, `"first half of part 3 of 12"`, `"round 2: 1 of 4"`, `"the answer"`). The handle you return is told of each retry of that request, and once of its end, with the usage its part reader reported (summed over its sends) and, on an error, the error. |
1002
+ | `onDocumentGone` | `(documentId: string) => void` | Called when the document the call names is no longer in the store: deleted or expired since it was planned, or not the caller's. The call's result is the error the model is told. `askWithFiles` makes it a `"document_gone"` notice. New in 0.9.0. |
1003
+ | `onPagesBusy` | `(documentId: string) => void` | Called when a look at pages (`view_pages`) waited longer than `pageLookWaitMs` for its turn and was not run. The call's result is the error the model is told. `askWithFiles` makes it a `"page_looks_busy"` notice. New in 0.9.0. |
1004
+ | `signal` | `AbortSignal` | Ends this call alone: its scans, regex searches, whole-file read, look at pages and loads. Joined with `ToolOptions.signal`. New in 0.10.0. |
957
1005
 
958
- Only `analyze_whole_document` uses them; the other tools ignore them, and `view_pages` too.
1006
+ `onProgress` and `onRequest` are used only by `analyze_whole_document`; the other tools ignore
1007
+ them, and `view_pages` too. `onDocumentGone` is called by every tool, and `onPagesBusy` by
1008
+ `view_pages`.
959
1009
  `execute()` takes no hooks. A `ToolSet` of your own may take the fourth argument and ignore it;
960
1010
  code that implements `ToolSet` for 0.6.0 still compiles. Do not throw from the hooks: a throw
961
1011
  becomes a failed part, or the tool's error result, as a throw of `ToolOptions.onProgress` does.