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
package/src/model.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  // The contract between the tool loop (`askWithFiles`) and a model provider. An adapter turns a
2
2
  // neutral request into the provider's own, sends it, and reads the reply back. Everything else
3
3
  // (planning, tools, limits, retries, usage, the forced answer) is the loop's, once, for every provider.
4
+ import { brand, branded } from "./errors.js";
4
5
  import type { GenerateText, ToolDeclaration, ToolFile, ToolResult } from "./types.js";
5
6
 
6
7
  /**
@@ -15,7 +16,8 @@ export type ToolAnswer = { call: ToolCall; result: ToolResult; files?: ToolFile[
15
16
 
16
17
  /**
17
18
  * One piece of a message: plain text, or something in the provider's own form (an image, an inline
18
- * PDF) that only its adapter reads. A native part may carry the text it holds, to be sized by.
19
+ * PDF) that only its adapter reads. A native part may carry the text it holds: its tokens are
20
+ * counted from that text, and its bytes from the larger of the text and the part's JSON.
19
21
  */
20
22
  export type ModelPart = { text: string; native?: undefined } | { native: unknown; text?: string };
21
23
 
@@ -40,8 +42,15 @@ export type ChatTurn = { role: "user" | "assistant" | "model"; text: string };
40
42
  * A round of tool use as an application stores it between a question and its answer
41
43
  * (`AskResult.toolTurns`): plain JSON. Passed back in `messages`, the model sees in a later
42
44
  * question what its tools returned in an earlier one, and does not have to search again.
45
+ *
46
+ * `version` is the version of this shape: `TOOL_TURN_VERSION` (1) on the rounds of 0.9.0 and later,
47
+ * absent on those of earlier versions. It is never sent to the model. `askWithFiles` refuses a
48
+ * round of a higher version, which a newer version of the package made, with a `TypeError`.
43
49
  */
44
- export type ToolTurn = { role: "tools"; calls: Array<ToolCall & { result: ToolResult }> };
50
+ export type ToolTurn = { role: "tools"; version?: number; calls: Array<ToolCall & { result: ToolResult }> };
51
+
52
+ /** The version of `ToolTurn` this package writes, and the highest it reads. New in 0.9.0. */
53
+ export const TOOL_TURN_VERSION = 1;
45
54
 
46
55
  /**
47
56
  * A result of a stored tool round, as it is sent again in a later question: without
@@ -94,12 +103,25 @@ export type ModelReply = {
94
103
  /**
95
104
  * How the reply ended, in terms every adapter maps its own to: "complete"; "cut" at the output
96
105
  * limit; "filtered" by the provider's safety filters; "malformed_call", a tool call the model
97
- * tried to make that could not be read (Gemini's MALFORMED_FUNCTION_CALL). Unset when the
98
- * adapter does not know the reason.
106
+ * tried to make that could not be read (Gemini's MALFORMED_FUNCTION_CALL); "other", a known
107
+ * reason that is none of these and that asking again does not mend (Gemini's OTHER or LANGUAGE;
108
+ * new in 0.10.0). The loop does not ask again after "filtered" or "other". Unset when the adapter
109
+ * does not know the reason.
99
110
  */
100
- finish?: "complete" | "cut" | "filtered" | "malformed_call";
111
+ finish?: "complete" | "cut" | "filtered" | "malformed_call" | "other";
101
112
  /** Why the provider refused the prompt, when it did. Nothing more is asked after it. */
102
113
  blockReason?: string;
114
+ /**
115
+ * The provider's ID of this response (Gemini's `responseId`, the `id` of OpenAI and Claude), when
116
+ * it gives one. `gemini()`, `openAiCompatible()` and `anthropic()` set it. New in 0.10.0.
117
+ */
118
+ responseId?: string;
119
+ /**
120
+ * The model that wrote this response, as the provider names it (Gemini's `modelVersion`, the
121
+ * `model` of OpenAI and Claude). `gemini()`, `openAiCompatible()` and `anthropic()` set it. New
122
+ * in 0.10.0.
123
+ */
124
+ responseModel?: string;
103
125
  usage?: Partial<ModelUsage>;
104
126
  /** The provider's own usage object, as it came. */
105
127
  providerUsage?: Record<string, unknown>;
@@ -347,6 +369,9 @@ export function classifyModelError(status: number, message: string, signals: { c
347
369
  return { kind: "invalid" };
348
370
  }
349
371
 
372
+ const MODEL_REQUEST_ERROR = Symbol.for("llm-large-files.ModelRequestError");
373
+ const DOCUMENTS_NOT_FOUND_ERROR = Symbol.for("llm-large-files.DocumentsNotFoundError");
374
+
350
375
  /**
351
376
  * A model request that failed: the provider answered with an HTTP error, or could not be reached
352
377
  * (`status` 0). `retryAfterMs` is the wait the provider asked for, when it named one. `kind` says
@@ -380,7 +405,23 @@ export class ModelRequestError extends Error {
380
405
  if (found.requestedTokens) this.requestedTokens = found.requestedTokens;
381
406
  }
382
407
  }
408
+
409
+ /**
410
+ * Whether `value` is a `ModelRequestError` (or one of its kinds, such as `GeminiRequestError`),
411
+ * made by this copy of the package or another one. New in 0.9.0.
412
+ */
413
+ static is(value: unknown): value is ModelRequestError {
414
+ return branded(value, MODEL_REQUEST_ERROR);
415
+ }
383
416
  }
417
+ brand(ModelRequestError, MODEL_REQUEST_ERROR);
418
+
419
+ /**
420
+ * A status of a model's error: 0 (no answer came) or an HTTP error, 400 to 599. `errorInfoOf`, the
421
+ * events, `modelFailureReason` and the retry policy tell the kind of an error without one of its
422
+ * own (an adapter of your own, `httpError`) from such a status.
423
+ */
424
+ export const isModelStatus = (status: unknown): status is number => typeof status === "number" && (status === 0 || (status >= 400 && status <= 599));
384
425
 
385
426
  /** The kind of a model error, when it has one: a `ModelRequestError`'s, or that of an error with the same field. */
386
427
  export function errorKindOf(error: unknown): ModelErrorKind | undefined {
@@ -388,24 +429,60 @@ export function errorKindOf(error: unknown): ModelErrorKind | undefined {
388
429
  return typeof kind === "string" ? kind as ModelErrorKind : undefined;
389
430
  }
390
431
 
432
+ /**
433
+ * What a model's error is, as its events and `errorInfoOf` tell it: its own `kind`, with its own
434
+ * `limitTokens` and `requestedTokens`; or, for an error without a kind that has a model's status
435
+ * (an adapter of your own, `httpError`), what `classifyModelError` gives its status, message and
436
+ * `code`, with the numbers of an overflow its message gives. Undefined for an error with neither,
437
+ * and for thrown plain data, which is not an adapter's error. The retry policy and the loop act on
438
+ * this kind: the loop makes a request smaller, plans the documents again, or reads a part of a
439
+ * whole-file read as two halves, for a "context_overflow".
440
+ */
441
+ export function modelErrorOf(error: unknown): { kind: unknown; limitTokens?: number; requestedTokens?: number } | undefined {
442
+ if (typeof error !== "object" || error === null) return undefined;
443
+ const { kind, limitTokens, requestedTokens } = error as { kind?: unknown; limitTokens?: number; requestedTokens?: number };
444
+ if (kind !== undefined) return { kind, limitTokens, requestedTokens };
445
+ const { status, message, code } = error as { status?: unknown; message?: unknown; code?: unknown };
446
+ if (!isModelStatus(status)) return undefined;
447
+ const proto: unknown = Object.getPrototypeOf(error);
448
+ if (proto === Object.prototype || proto === null) return undefined;
449
+ return classifyModelError(status, typeof message === "string" ? message : "", { code: typeof code === "string" ? code : undefined });
450
+ }
451
+
391
452
  /**
392
453
  * For a request refused as larger than the tokens a minute (or a day) of the provider allows, not
393
454
  * than the model's window: that period. Undefined for any other error.
394
455
  */
395
456
  export function rateAllowanceOf(error: unknown): "minute" | "day" | undefined {
396
- if (errorKindOf(error) !== "context_overflow" || !(error instanceof Error)) return undefined;
397
- const period = RATE_TOO_SMALL.exec(error.message)?.[1];
457
+ if (modelErrorOf(error)?.kind !== "context_overflow") return undefined;
458
+ // modelErrorOf found an object: its message is read whatever its class, as modelErrorOf reads it.
459
+ const { message } = error as { message?: unknown };
460
+ if (typeof message !== "string") return undefined;
461
+ const period = RATE_TOO_SMALL.exec(message)?.[1];
398
462
  if (period === undefined) return undefined;
399
463
  return period.toLowerCase() === "day" ? "day" : "minute";
400
464
  }
401
465
 
402
- /** Some document IDs were never uploaded, have expired, or belong to another owner. */
466
+ /**
467
+ * Some document IDs were never uploaded, have expired, or belong to another owner. `code` and
468
+ * `status` are new in 0.9.0.
469
+ */
403
470
  export class DocumentsNotFoundError extends Error {
471
+ readonly code: "documents_missing" = "documents_missing";
472
+ /** The HTTP status to answer with. */
473
+ readonly status: 404 = 404;
474
+
404
475
  constructor(readonly ids: string[]) {
405
476
  super(`${ids.length === 1 ? "A document is" : `${ids.length} documents are`} no longer available: ${ids.join(", ")}. Upload ${ids.length === 1 ? "it" : "them"} again.`);
406
477
  this.name = "DocumentsNotFoundError";
407
478
  }
479
+
480
+ /** Whether `value` is a `DocumentsNotFoundError`, made by this copy of the package or another one. New in 0.9.0. */
481
+ static is(value: unknown): value is DocumentsNotFoundError {
482
+ return branded(value, DOCUMENTS_NOT_FOUND_ERROR);
483
+ }
408
484
  }
485
+ brand(DocumentsNotFoundError, DOCUMENTS_NOT_FOUND_ERROR);
409
486
 
410
487
  const EMPTY_USAGE: ModelUsage = { inputTokens: 0, cachedInputTokens: 0, outputTokens: 0, thoughtTokens: 0 };
411
488
 
@@ -414,13 +491,36 @@ export type UsageTotals = ModelUsage & {
414
491
  /** Requests that were answered. One that failed and was sent again counts once. */
415
492
  requests: number;
416
493
  byModel: Record<string, ModelUsage & { requests: number }>;
494
+ /**
495
+ * The input tokens of the requests whose provider said nothing of their input, by the library's
496
+ * estimate, in each model's own tokens: the ceiling of the question (`limits.inputTokens`) counts
497
+ * them with the reported ones. Absent when every request reported its input. New in 0.10.0.
498
+ */
499
+ estimatedInputTokens?: number;
500
+ /**
501
+ * Requests that were answered without their input tokens from the provider: no usage, or an input
502
+ * of 0 (see `estimatedInputTokens`). Absent when there was none. New in 0.10.0.
503
+ */
504
+ unmeasuredRequests?: number;
505
+ };
506
+
507
+ /** True when `usage` gives the input tokens of its request: a number above 0. */
508
+ export const reportsInput = (usage: Partial<ModelUsage> | undefined): boolean => {
509
+ const input = usage?.inputTokens;
510
+ return typeof input === "number" && Number.isFinite(input) && input > 0;
417
511
  };
418
512
 
419
513
  /** Sums the usage of the requests of one question. */
420
514
  export class UsageMeter {
421
515
  readonly totals: UsageTotals = { requests: 0, ...EMPTY_USAGE, byModel: {} };
422
516
 
423
- add(model: string, usage: Partial<ModelUsage> | undefined): void {
517
+ /**
518
+ * Counts one answered request of `model`. `estimatedInputTokens` is the library's estimate of its
519
+ * input, in the model's own tokens: when `usage` does not give the input (no usage, or 0), the
520
+ * estimate goes into `estimatedInputTokens` and the request into `unmeasuredRequests`. Without
521
+ * it, such a request is counted as before 0.10.0.
522
+ */
523
+ add(model: string, usage: Partial<ModelUsage> | undefined, estimatedInputTokens?: number): void {
424
524
  const { byModel } = this.totals;
425
525
  // An own entry only: an id such as "__proto__" or "constructor" found what every object
426
526
  // inherits, and "__proto__" then counted the usage into Object.prototype.
@@ -436,5 +536,9 @@ export class UsageMeter {
436
536
  if (typeof value === "number" && Number.isFinite(value) && value > 0) sum[key] += value;
437
537
  }
438
538
  }
539
+ if (estimatedInputTokens === undefined || reportsInput(usage)) return;
540
+ this.totals.unmeasuredRequests = (this.totals.unmeasuredRequests ?? 0) + 1;
541
+ const estimate = Number.isFinite(estimatedInputTokens) && estimatedInputTokens > 0 ? Math.ceil(estimatedInputTokens) : 0;
542
+ this.totals.estimatedInputTokens = (this.totals.estimatedInputTokens ?? 0) + estimate;
439
543
  }
440
544
  }