llm-large-files 0.8.2 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (201) hide show
  1. package/CHANGELOG.md +535 -0
  2. package/README.md +9 -10
  3. package/dist/access.d.ts +86 -0
  4. package/dist/access.d.ts.map +1 -0
  5. package/dist/access.js +131 -0
  6. package/dist/access.js.map +1 -0
  7. package/dist/ask.d.ts +45 -1
  8. package/dist/ask.d.ts.map +1 -1
  9. package/dist/ask.js +181 -20
  10. package/dist/ask.js.map +1 -1
  11. package/dist/cache.d.ts +12 -3
  12. package/dist/cache.d.ts.map +1 -1
  13. package/dist/cache.js +18 -5
  14. package/dist/cache.js.map +1 -1
  15. package/dist/encoding.js +5 -3
  16. package/dist/encoding.js.map +1 -1
  17. package/dist/error-info.d.ts +57 -0
  18. package/dist/error-info.d.ts.map +1 -0
  19. package/dist/error-info.js +217 -0
  20. package/dist/error-info.js.map +1 -0
  21. package/dist/errors.d.ts +72 -8
  22. package/dist/errors.d.ts.map +1 -1
  23. package/dist/errors.js +102 -15
  24. package/dist/errors.js.map +1 -1
  25. package/dist/events.d.ts +35 -9
  26. package/dist/events.d.ts.map +1 -1
  27. package/dist/events.js +42 -11
  28. package/dist/events.js.map +1 -1
  29. package/dist/evidence.d.ts +4 -2
  30. package/dist/evidence.d.ts.map +1 -1
  31. package/dist/evidence.js +14 -3
  32. package/dist/evidence.js.map +1 -1
  33. package/dist/extract.d.ts +32 -8
  34. package/dist/extract.d.ts.map +1 -1
  35. package/dist/extract.js +46 -35
  36. package/dist/extract.js.map +1 -1
  37. package/dist/gemini/adapter.d.ts +19 -3
  38. package/dist/gemini/adapter.d.ts.map +1 -1
  39. package/dist/gemini/adapter.js +84 -12
  40. package/dist/gemini/adapter.js.map +1 -1
  41. package/dist/gemini/chat.d.ts +18 -6
  42. package/dist/gemini/chat.d.ts.map +1 -1
  43. package/dist/gemini/chat.js +2 -0
  44. package/dist/gemini/chat.js.map +1 -1
  45. package/dist/gemini/index.d.ts +1 -1
  46. package/dist/gemini/index.d.ts.map +1 -1
  47. package/dist/index.d.ts +9 -6
  48. package/dist/index.d.ts.map +1 -1
  49. package/dist/index.js +4 -3
  50. package/dist/index.js.map +1 -1
  51. package/dist/ingest.d.ts +70 -0
  52. package/dist/ingest.d.ts.map +1 -0
  53. package/dist/ingest.js +868 -0
  54. package/dist/ingest.js.map +1 -0
  55. package/dist/library.d.ts +169 -34
  56. package/dist/library.d.ts.map +1 -1
  57. package/dist/library.js +324 -591
  58. package/dist/library.js.map +1 -1
  59. package/dist/map-reduce.js +3 -3
  60. package/dist/map-reduce.js.map +1 -1
  61. package/dist/model.d.ts +43 -2
  62. package/dist/model.d.ts.map +1 -1
  63. package/dist/model.js +63 -3
  64. package/dist/model.js.map +1 -1
  65. package/dist/node/handlers.d.ts +226 -14
  66. package/dist/node/handlers.d.ts.map +1 -1
  67. package/dist/node/handlers.js +423 -181
  68. package/dist/node/handlers.js.map +1 -1
  69. package/dist/node/index.d.ts +3 -2
  70. package/dist/node/index.d.ts.map +1 -1
  71. package/dist/node/index.js +2 -1
  72. package/dist/node/index.js.map +1 -1
  73. package/dist/node/ingest-child.d.ts +7 -1
  74. package/dist/node/ingest-child.d.ts.map +1 -1
  75. package/dist/node/ingest-child.js +18 -3
  76. package/dist/node/ingest-child.js.map +1 -1
  77. package/dist/node/isolated.d.ts +3 -1
  78. package/dist/node/isolated.d.ts.map +1 -1
  79. package/dist/node/isolated.js +11 -4
  80. package/dist/node/isolated.js.map +1 -1
  81. package/dist/node/originals.d.ts +19 -0
  82. package/dist/node/originals.d.ts.map +1 -0
  83. package/dist/node/originals.js +82 -0
  84. package/dist/node/originals.js.map +1 -0
  85. package/dist/node/spool.d.ts +102 -0
  86. package/dist/node/spool.d.ts.map +1 -0
  87. package/dist/node/spool.js +365 -0
  88. package/dist/node/spool.js.map +1 -0
  89. package/dist/node/sse.d.ts +4 -3
  90. package/dist/node/sse.d.ts.map +1 -1
  91. package/dist/node/sse.js +7 -6
  92. package/dist/node/sse.js.map +1 -1
  93. package/dist/pdf-layout.d.ts.map +1 -1
  94. package/dist/pdf-layout.js +71 -12
  95. package/dist/pdf-layout.js.map +1 -1
  96. package/dist/ranged.d.ts +15 -11
  97. package/dist/ranged.d.ts.map +1 -1
  98. package/dist/ranged.js +42 -11
  99. package/dist/ranged.js.map +1 -1
  100. package/dist/retry.d.ts +3 -1
  101. package/dist/retry.d.ts.map +1 -1
  102. package/dist/retry.js +6 -2
  103. package/dist/retry.js.map +1 -1
  104. package/dist/scope.d.ts +25 -0
  105. package/dist/scope.d.ts.map +1 -0
  106. package/dist/scope.js +60 -0
  107. package/dist/scope.js.map +1 -0
  108. package/dist/source.d.ts +60 -0
  109. package/dist/source.d.ts.map +1 -0
  110. package/dist/source.js +170 -0
  111. package/dist/source.js.map +1 -0
  112. package/dist/stores.d.ts +120 -31
  113. package/dist/stores.d.ts.map +1 -1
  114. package/dist/stores.js +582 -139
  115. package/dist/stores.js.map +1 -1
  116. package/dist/testing/fake-fetch.d.ts +31 -0
  117. package/dist/testing/fake-fetch.d.ts.map +1 -0
  118. package/dist/testing/fake-fetch.js +66 -0
  119. package/dist/testing/fake-fetch.js.map +1 -0
  120. package/dist/testing/fake-model.d.ts +38 -0
  121. package/dist/testing/fake-model.d.ts.map +1 -0
  122. package/dist/testing/fake-model.js +62 -0
  123. package/dist/testing/fake-model.js.map +1 -0
  124. package/dist/testing/index.d.ts +7 -0
  125. package/dist/testing/index.d.ts.map +1 -0
  126. package/dist/testing/index.js +9 -0
  127. package/dist/testing/index.js.map +1 -0
  128. package/dist/testing/store-checks.d.ts +72 -0
  129. package/dist/testing/store-checks.d.ts.map +1 -0
  130. package/dist/testing/store-checks.js +619 -0
  131. package/dist/testing/store-checks.js.map +1 -0
  132. package/dist/tools.d.ts +25 -0
  133. package/dist/tools.d.ts.map +1 -1
  134. package/dist/tools.js +135 -13
  135. package/dist/tools.js.map +1 -1
  136. package/dist/types.d.ts +116 -13
  137. package/dist/types.d.ts.map +1 -1
  138. package/docs/README.md +17 -8
  139. package/docs/getting-started.md +18 -13
  140. package/docs/guides/anthropic.md +12 -8
  141. package/docs/guides/conversations.md +112 -47
  142. package/docs/guides/costs-and-limits.md +16 -7
  143. package/docs/guides/deployment.md +284 -83
  144. package/docs/guides/errors-and-reliability.md +161 -23
  145. package/docs/guides/own-adapter-and-loop.md +27 -10
  146. package/docs/guides/pdfs.md +123 -29
  147. package/docs/guides/storage.md +633 -145
  148. package/docs/guides/testing.md +78 -129
  149. package/docs/guides/vertex-and-gemini.md +10 -7
  150. package/docs/guides/web-app.md +312 -78
  151. package/docs/how-it-works.md +35 -8
  152. package/docs/measurements.md +43 -8
  153. package/docs/publishing.md +112 -36
  154. package/docs/reference/ask.md +154 -13
  155. package/docs/reference/errors-and-retries.md +166 -14
  156. package/docs/reference/events.md +53 -20
  157. package/docs/reference/gemini.md +70 -21
  158. package/docs/reference/library.md +706 -165
  159. package/docs/reference/node.md +624 -110
  160. package/docs/reference/testing.md +311 -0
  161. package/docs/reference/tools.md +15 -5
  162. package/docs/security.md +304 -67
  163. package/docs/troubleshooting.md +144 -65
  164. package/docs/upgrading-0.9.md +890 -0
  165. package/package.json +12 -3
  166. package/src/access.ts +144 -0
  167. package/src/ask.ts +230 -19
  168. package/src/cache.ts +20 -5
  169. package/src/encoding.ts +6 -4
  170. package/src/error-info.ts +255 -0
  171. package/src/errors.ts +129 -15
  172. package/src/events.ts +86 -21
  173. package/src/evidence.ts +14 -3
  174. package/src/extract.ts +50 -33
  175. package/src/gemini/adapter.ts +97 -11
  176. package/src/gemini/chat.ts +20 -6
  177. package/src/gemini/index.ts +1 -1
  178. package/src/index.ts +9 -6
  179. package/src/ingest.ts +927 -0
  180. package/src/library.ts +467 -601
  181. package/src/map-reduce.ts +3 -3
  182. package/src/model.ts +69 -5
  183. package/src/node/handlers.ts +630 -183
  184. package/src/node/index.ts +13 -2
  185. package/src/node/ingest-child.ts +23 -5
  186. package/src/node/isolated.ts +11 -4
  187. package/src/node/originals.ts +78 -0
  188. package/src/node/spool.ts +365 -0
  189. package/src/node/sse.ts +7 -6
  190. package/src/pdf-layout.ts +69 -11
  191. package/src/ranged.ts +34 -13
  192. package/src/retry.ts +6 -2
  193. package/src/scope.ts +63 -0
  194. package/src/source.ts +178 -0
  195. package/src/stores.ts +635 -140
  196. package/src/testing/fake-fetch.ts +80 -0
  197. package/src/testing/fake-model.ts +78 -0
  198. package/src/testing/index.ts +11 -0
  199. package/src/testing/store-checks.ts +660 -0
  200. package/src/tools.ts +150 -14
  201. package/src/types.ts +106 -13
@@ -296,6 +296,7 @@ outcome that says so.
296
296
  | `code` | [`NoticeCode`](#noticecode-and-noticedisplay) | What happened. |
297
297
  | `message` | `string` | One line of plain text, word for word what the callback got when there was one. |
298
298
  | `display` | [`NoticeDisplay`](#noticecode-and-noticedisplay) | Where it went in the callbacks: `"activity"`, `"step"` or `"none"`. |
299
+ | `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. |
299
300
 
300
301
  #### `run.finish`
301
302
 
@@ -407,9 +408,17 @@ to `onStep` and `AskResult.steps`, `"none"` to neither. The `"none"` notices are
407
408
  | `"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." |
408
409
  | `"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
410
  | `"blocked"` | none | The provider refused the prompt (`AskResult.blockReason`). | "The provider refused the prompt: SAFETY." |
411
+ | `"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." |
412
+ | `"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." |
413
+ | `"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." |
410
414
 
411
415
  The messages are written for people and may change between versions. Test `code`, not `message`.
412
416
 
417
+ New codes come with new versions: 0.9.0 added `"documents_missing"`, `"document_gone"` and
418
+ `"page_looks_busy"`. In TypeScript, a `switch` over `NoticeCode` that must handle every code (one
419
+ with a `never` check in its `default`) no longer compiles until it handles them. Give such a switch
420
+ a `default` that shows nothing, or the message.
421
+
413
422
  ### How the callbacks map to events
414
423
 
415
424
  The callbacks of earlier versions get nothing the events do not also say:
@@ -434,20 +443,25 @@ An error in an event is plain data, safe to send to a browser:
434
443
  | Field | Type | What it holds |
435
444
  |---|---|---|
436
445
  | `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. |
446
+ | `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. |
447
+ | `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`. |
448
+ | `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. |
449
+ | `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
450
 
442
451
  How `message` is written:
443
452
 
444
- - **A model error** (one with a `kind` or a numeric `status`) is told by its kind and status, in
453
+ - **A model error** (one with a `kind`, or an error with a `status` of 0 or 400 to 599, other than
454
+ the library's own errors and plain data thrown) is told by its kind and status, in
445
455
  the library's words: "The model was busy (HTTP 429).", "The model failed on its side (HTTP
446
456
  503).", "The model refused the key or token it was sent (HTTP 401).". The provider's own words
447
457
  can name projects, hosts or keys: they stay on the error that is thrown, for your logs.
448
458
  - **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).".
459
+ 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
460
+ be read from storage (ENOENT).". Since 0.9.0 its code is told only when it reads as one
461
+ (capitals, digits and `_`): a driver may put a host in `code`.
462
+ - **Another `status`** (not 0 or 400 to 599), and plain data thrown with a status
463
+ (`{ status: 429, message }`), is told as "Something went wrong.", as `errorInfoOf` reads it.
464
+ New in 0.9.0: such an error was told in a model's words ("The model was busy (HTTP 429).").
451
465
  - **Any other `Error`** is told by its message as it is written: the library's own refusals and
452
466
  messages ("app.log is empty."), and the reason of an abort. That includes errors your own code
453
467
  throws: an adapter, an extractor or a listener of your own. Keep secrets out of their messages,
@@ -455,16 +469,19 @@ How `message` is written:
455
469
  - **Anything that is not an `Error`** is told as "Something went wrong.".
456
470
 
457
471
  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
472
+ its `code`, and with its `status` only for `"too_large"` (413), `"exists"` (409, new in 0.9.0) and
473
+ `"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
474
  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`.
475
+ `"IngestError"`. A [`DocumentScopeError`](./library.md#documentscopeerror) is told by its message, with
476
+ `name: "DocumentScopeError"` and its `code` (new in 0.9.0). A store that failed, an abort and a
477
+ listener that threw have no `code`.
462
478
 
463
479
  Where errors appear: `retry.error`, `request.end.error`, `run.error.error` and `ingest.error.error`
464
480
  are `EventError`s. `tool.end.error` is a string: the error the model was sent, or, for a call
465
481
  ended because its turn failed, the `message` of the turn's error. An ingest whose store failed to
466
482
  write the document says only "The document could not be stored. Try again later.", whatever the
467
- store threw.
483
+ store threw. Since 0.9.0 that error also has `name: "DocumentStoreError"` and `status: 503`, so it
484
+ reads as the error `ingest` throws.
468
485
 
469
486
  To show your own words, map the error by `kind` and `name`, as `examples/server.ts` does in its
470
487
  `userMessage`, and the error of an ingest by `code`. [Errors and reliability](../guides/errors-and-reliability.md) says what each kind
@@ -478,8 +495,11 @@ text of the documents:
478
495
  - `tool.end` has no `result`;
479
496
  - the result in `run.finish` and `run.error` has no `toolTurns`, and its `plan` names each
480
497
  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.
498
+ full `DocumentMeta`, which holds the `owner`, the `scope` (such as `[userId, chatId]`), the
499
+ `sha256` of the file, the `original` with its `ref` (where your application keeps the file), the
500
+ headings of the outline and, for a log, the templates of its most frequent lines.
501
+
502
+ `missing` (new in 0.9.0) is there as in `AskResult`: the IDs of the documents left out.
483
503
 
484
504
  ```ts
485
505
  type EventResult = Omit<AskResult, "toolTurns" | "plan"> & {
@@ -766,9 +786,9 @@ The events of `files.ingest`, through `IngestInput.onEvent`:
766
786
 
767
787
  | `type` | Fields | When |
768
788
  |---|---|---|
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. |
789
+ | `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
790
  | `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`. |
791
+ | `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
792
  | `ingest.error` | `error: EventError` | Always last when the ingest throws: a refusal, an abort, a store that failed, or a listener that threw. |
773
793
 
774
794
  Each event also has `seq`, `runId` and `time`. Nothing comes after `ingest.finish` or
@@ -825,16 +845,23 @@ events when the request's `Accept` header names `text/event-stream` itself: not
825
845
  not only through a wildcard such as `*/*` or `text/*`.
826
846
 
827
847
  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
848
+ (`400`), an owner that `getOwner` refused or that is not allowed (its status, `401` or `500`), a
849
+ scope that `getScope` refused or that is not valid (its status, or `500`; new in 0.9.0), a body
829
850
  another middleware already parsed (`400`), and a file larger than `maxUploadBytes` (`413`).
830
851
  2. Then it answers `200` with Server-Sent Events, written by `writeSseEvents`: `ingest.start`,
831
852
  `ingest.progress`…, and last one of:
832
- - `ingest.finish`, whose `document` is the summary a `201` would hold;
853
+ - `ingest.finish`, whose `document` is the summary a `201` would hold, and whose `app` is the
854
+ `answer` of `onUploaded` (new in 0.9.0). It is sent once `onUploaded` is done;
833
855
  - `ingest.error`, whose `error` is `{ message, status }`, the message and status a JSON answer
834
856
  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
857
+ `503` "The document could not be stored. Try again." for a store that failed (since 0.9.0),
858
+ `500` "Upload failed (ENOSPC)." for a Node system error, with its code when it reads as one,
859
+ `500` "Upload failed." for any other error, what `onUploaded` threw, and so on, as in the
836
860
  [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).
861
+ has `code`, the `IngestError` code (`"empty"`, `"busy"`…; new in 0.8.0). Since 0.9.0, a store
862
+ that failed also has `name` `"DocumentStoreError"`, and a scope error `name`
863
+ `"DocumentScopeError"` and its `code` (`"scope_missing"`, 404, for a deleted chat), so that
864
+ [`errorInfoOf`](./errors-and-retries.md#errorinfooferror) reads it.
838
865
  3. When the client goes away, the ingest stops and nothing is stored.
839
866
 
840
867
  It always ends with `ingest.finish` or `ingest.error`, even with a `LargeFiles` of your own whose
@@ -941,6 +968,8 @@ executeWithFiles(name: string, args: Record<string, unknown>, limits?: ToolCallL
941
968
  type ToolCallHooks = {
942
969
  onProgress?: ProgressListener;
943
970
  onRequest?: (request: ToolRequestStart) => ToolRequestHandle;
971
+ onDocumentGone?: (documentId: string) => void; // new in 0.9.0
972
+ onPagesBusy?: (documentId: string) => void; // new in 0.9.0
944
973
  };
945
974
  type ToolRequestStart = { purpose: "part" | "merge" | "final"; detail: string };
946
975
  type ToolRequestHandle = {
@@ -954,8 +983,12 @@ type ToolRequestEnd = { outcome: "ok" | "error"; usage?: GenerateUsage; error?:
954
983
  |---|---|---|
955
984
  | `onProgress` | `ProgressListener` | Progress of this call, in place of `ToolOptions.onProgress` for it: `{ kind: "map" \| "reduce", message, done, total }`. |
956
985
  | `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. |
986
+ | `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. |
987
+ | `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. |
957
988
 
958
- Only `analyze_whole_document` uses them; the other tools ignore them, and `view_pages` too.
989
+ `onProgress` and `onRequest` are used only by `analyze_whole_document`; the other tools ignore
990
+ them, and `view_pages` too. `onDocumentGone` is called by every tool, and `onPagesBusy` by
991
+ `view_pages`.
959
992
  `execute()` takes no hooks. A `ToolSet` of your own may take the fourth argument and ignore it;
960
993
  code that implements `ToolSet` for 0.6.0 still compiles. Do not throw from the hooks: a throw
961
994
  becomes a failed part, or the tool's error result, as a throw of `ToolOptions.onProgress` does.
@@ -21,7 +21,8 @@ The entry point needs no Google SDK: it posts to the Generate Content REST API w
21
21
  [`AskGeminiOptions`](#askgeminioptions-1), [`AskGeminiResult`](#askgeminiresult)
22
22
  - [The model adapter](#the-model-adapter): [`gemini()`](#geminiendpoint-options),
23
23
  [`GeminiModelOptions`](#geminimodeloptions), [`GEMINI_PAGE_TOKENS`](#gemini_page_tokens)
24
- - [Gemini contents](#gemini-contents): [`fromGeminiContents()`](#fromgeminicontentscontents),
24
+ - [Gemini contents](#gemini-contents): [`fromGeminiContents()`](#fromgeminicontentscontents-options),
25
+ [`FromGeminiContentsOptions`](#fromgeminicontentsoptions),
25
26
  [`geminiToolTurns()`](#geminitoolturnsturns), [`GeminiContent`](#geminicontent),
26
27
  [`GeminiPart`](#geminipart)
27
28
  - [Helpers for your own loop](#helpers-for-your-own-loop):
@@ -231,7 +232,7 @@ another endpoint.
231
232
  **Throws** what `askWithFiles` throws ([ask reference](./ask.md)). Among them:
232
233
 
233
234
  - `DocumentsNotFoundError`, before any model request, when a document ID was never uploaded, has
234
- expired, or belongs to another `owner`.
235
+ expired, or belongs to another `owner` or `scope`.
235
236
  - `Error("The last message must be the user's.")`, when the last turn of `messages` or `contents`
236
237
  is not the user's, or when both are left out.
237
238
  - `DocumentStoreError`, when the store could not be read.
@@ -270,12 +271,15 @@ takes their place.
270
271
  Shared with [`AskOptions`](./ask.md#askoptions), with the same meaning and defaults: `files`,
271
272
  `documentIds`, `documentPlacement`, `system`, `owner`, `limits`, `retry`, `budget`,
272
273
  `wholeDocumentAnalysis`, `viewPages`, `onText`, `onActivity`, `onStep`, `signal`, and, new in 0.7.0,
273
- `streamText`, `onEvent`, `runId` and `eventResults` (see [events](./events.md)). With Gemini:
274
-
275
- - `messages` is optional and typed `Array<ChatTurn | ToolTurn>`: plain turns and the `toolTurns`
276
- of earlier questions. Turns with native parts (images, inline PDFs, thought signatures) go in
277
- `contents` instead. Give `messages` or `contents`: with neither, the question throws "The last
278
- message must be the user's."
274
+ `streamText`, `onEvent`, `runId` and `eventResults` (see [events](./events.md)), and, new in 0.9.0,
275
+ `scope` and `onMissingDocuments` ([missing documents](./ask.md#missing-documents)). With Gemini:
276
+
277
+ - `messages` is optional and typed [`ChatMessage[]`](./ask.md#chatmessage), as in `askWithFiles`:
278
+ plain turns, the `toolTurns` of earlier questions, and turns with parts, such as
279
+ `fromGeminiContents(contents, { rounds: false })` gives. Before 0.9.0 it was typed
280
+ `Array<ChatTurn | ToolTurn>`. Turns with native parts (images, inline PDFs, thought signatures)
281
+ usually go in `contents`. Give `messages` or `contents`: with neither, the question throws "The
282
+ last message must be the user's."
279
283
  - `onText` streams only when the endpoint has a `streamUrl`. Without one, it gets the text of each
280
284
  reply once the reply has arrived. The same holds for `streamText` (on by default with `onText`):
281
285
  it asks for the answer as it is written only from an endpoint with a `streamUrl`.
@@ -287,7 +291,7 @@ Fields `askGemini` adds:
287
291
  | Field | Type | Default | What it does |
288
292
  |---|---|---|---|
289
293
  | `endpoint` | [`GeminiEndpoint`](#geminiendpoint) | required | The model that answers, and the explorer when `explorerUrl` is set and differs from `url`. |
290
- | `contents` | [`GeminiContent[]`](#geminicontent) | none | The conversation as Gemini `contents`, with images, inline PDFs or function calls. Converted with [`fromGeminiContents()`](#fromgeminicontentscontents). It wins over `messages` when both are given. The array is not changed. The last turn must be the user's. |
294
+ | `contents` | [`GeminiContent[]`](#geminicontent) | none | The conversation as Gemini `contents`, with images, inline PDFs or function calls. Converted with [`fromGeminiContents()`](#fromgeminicontentscontents-options), whose tool rounds come back as stored rounds (new in 0.9.0). For the conversion of 0.8, pass `messages: fromGeminiContents(contents, { rounds: false })` instead. It wins over `messages` when both are given. The array is not changed. The last turn must be the user's. |
291
295
  | `generationConfig` | `Record<string, unknown>` | none | Sent as `generationConfig` with every chat request of the answering model, and of the explorer unless `explorerGenerationConfig` is set: `temperature`, `maxOutputTokens`, `thinkingConfig` and so on. Not used for part reads. |
292
296
  | `explorerGenerationConfig` | `Record<string, unknown>` | `generationConfig` | The same, for the explorer's chat requests, when the two models do not take the same settings. Not used without an explorer. |
293
297
  | `toolFilePlacement` | [`GeminiToolFilePlacement`](#geminitoolfileplacement) | `"inside"` | Where the pages of `view_pages` go, for both models. `"beside"` for Gemini 2.5. |
@@ -469,20 +473,63 @@ of `pageTokens` in `gemini()`, and the value `askGemini` always uses. Guide: [PD
469
473
 
470
474
  For an application that keeps its conversations as Gemini `contents`.
471
475
 
472
- ### fromGeminiContents(contents)
476
+ ### fromGeminiContents(contents, options?)
473
477
 
474
478
  ```ts
475
- declare function fromGeminiContents(contents: GeminiContent[]): ChatMessage[];
479
+ declare function fromGeminiContents(contents: GeminiContent[], options?: FromGeminiContentsOptions): ChatMessage[];
476
480
  ```
477
481
 
478
482
  Gemini `contents` as the `messages` of `askWithFiles` ([`ChatMessage`](./ask.md#chatmessage)).
479
- The role `"user"` stays `"user"`, and `"model"` becomes `"assistant"`. A part that holds only
480
- `text` becomes `{ text }`. A part with `text` and other fields (a thought signature) becomes
481
- `{ native: part, text }`. Every other part (an image, an inline PDF, a function call) is kept as
482
- `{ native: part }`, which `gemini()` sends back as it is. The array and its parts are not changed.
483
- `askGemini` uses it for `contents`.
483
+ `askGemini` uses it for `contents`. The array and its parts are not changed.
484
+
485
+ - **The tool rounds of earlier questions become stored rounds** ([`ToolTurn`](./ask.md#toolturn)),
486
+ as [`geminiToolTurns()`](#geminitoolturnsturns) wrote them: a `model` turn made only of
487
+ `functionCall` parts that call the library's tools ([`TOOL_NAMES`](./tools.md#tool_names)),
488
+ followed by the `user` turn of their `functionResponse` parts, one for each call, in the same
489
+ order, with the same names and `id`s. New in 0.9.0, and on by default (`rounds`).
490
+ - The role `"user"` stays `"user"`, and `"model"` becomes `"assistant"`.
491
+ - A part that holds only `text` becomes `{ text }`. A part with `text` and other fields (a thought
492
+ signature) becomes `{ native: part, text }`. The loop counts its tokens from the text and its
493
+ bytes from the whole part (the bytes since 0.9.0).
494
+ - Every other part is kept as `{ native: part }`, which `gemini()` sends back as it is: an image,
495
+ an inline PDF, and a function call or response that is not part of such a round.
496
+ - A `functionCall` or `functionResponse` part kept this way carries, as its `text`, the JSON of its
497
+ call or response. The loop counts its tokens from that text, as it counts a stored round, and
498
+ its bytes from the whole part, a thought signature next to the call included. One with files in
499
+ it (`parts` in a response) is sized as media. New in 0.9.0: before, every function part was
500
+ sized as media, at about a token for each 256 characters, and a follow-up whose rounds were in
501
+ `contents` was counted about 15,000 tokens short in a test of two calls.
502
+
503
+ With its rounds as stored rounds, a follow-up asked with `contents` works as one asked with
504
+ `messages` and `toolTurns`: a repeated call is answered with a pointer to its earlier result, the
505
+ results can be shortened to make room, and they are sized alike. For a round that
506
+ `geminiToolTurns` wrote, the request is the same, byte for byte, as before 0.9.0: the round is
507
+ sent as it was written. When the loop shortens such a round, the later requests of the question
508
+ hold its stand-ins. A round that 0.5.0 converted has `tool_turns_left` in its responses: it is
509
+ lifted too, and sent without it, as stored rounds are, and a call without `args` is sent with
510
+ `args: {}`. A conversation that holds such a round misses the prompt cache once after the
511
+ upgrade, from that round on.
512
+
513
+ A turn is kept as it is, part by part, when anything in it is not of that form: a function of
514
+ your own, a call next to one of yours, a thought signature, a response with a file in it, or
515
+ responses that do not match the calls. So is a round that ends `contents`, with no turn after it:
516
+ its responses are the turn the model answers (an application that ran the tools in a loop of its
517
+ own hands the conversation over). `askWithFiles` throws "The last message must be the user's." for
518
+ a conversation that ends with a stored round.
519
+
520
+ Guide: [conversations](../guides/conversations.md#gemini-contents).
521
+
522
+ ### `FromGeminiContentsOptions`
523
+
524
+ ```ts
525
+ type FromGeminiContentsOptions = { rounds?: boolean };
526
+ ```
527
+
528
+ New in 0.9.0.
484
529
 
485
- Guide: [conversations](../guides/conversations.md).
530
+ | Field | Type | Default | What it does |
531
+ |---|---|---|---|
532
+ | `rounds` | `boolean` | `true` | Turn the tool rounds of earlier questions into stored rounds (above). `false` keeps every turn as its parts, as 0.8 did. Function parts are sized by their JSON either way. With `askGemini`: `messages: fromGeminiContents(contents, { rounds: false })`. |
486
533
 
487
534
  ### geminiToolTurns(turns)
488
535
 
@@ -496,7 +543,9 @@ of `functionResponse` parts, in call order. Put them between the question and it
496
543
  model sees in a later question what its tools returned in this one. The results leave out
497
544
  `tool_turns_left`, which counted the turns of their own question, as `askWithFiles` does with
498
545
  stored rounds (since 0.6.0): a history kept as `contents` gives the same request as one kept as
499
- `toolTurns`. Contents that 0.5.0 converted keep the field.
546
+ `toolTurns`. Contents that 0.5.0 converted keep the field. Since 0.9.0 `fromGeminiContents` turns
547
+ these turns back into stored rounds, so the loop treats them alike too, and the rounds of 0.5.0
548
+ are then sent without the field (above).
500
549
 
501
550
  ```ts
502
551
  import { askGemini, geminiToolTurns, type GeminiContent } from "llm-large-files/gemini";
@@ -530,8 +579,8 @@ type GeminiPart = Record<string, unknown>;
530
579
  ```
531
580
 
532
581
  One part of a turn, in Gemini's own form: `{ text }`, `{ inlineData }`, `{ functionCall }`,
533
- `{ functionResponse }` and so on. The library reads `text`, `thought` and `functionCall`, and
534
- passes every part on as it is.
582
+ `{ functionResponse }` and so on. The library reads `text`, `thought` and `functionCall` (and,
583
+ since 0.9.0, the function parts of `fromGeminiContents`), and passes every part on as it is.
535
584
 
536
585
  ## Helpers for your own loop
537
586
 
@@ -790,7 +839,7 @@ passed in as `code` and `details` to tell the `kind`, and are not kept on the er
790
839
  import { askGemini, DocumentsNotFoundError, GeminiRequestError, partialResultOf } from "llm-large-files/gemini";
791
840
 
792
841
  // files and endpoint as above; documentIds: string[];
793
- // messages: Array<ChatTurn | ToolTurn> (plain turns and stored toolTurns).
842
+ // messages: ChatMessage[] (plain turns and stored toolTurns).
794
843
  try {
795
844
  const result = await askGemini({ files, endpoint, documentIds, messages });
796
845
  console.log(result.text);