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
package/src/map-reduce.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { safeErrorMessage } from "./errors.js";
2
2
  import { until } from "./http.js";
3
- import { classifyModelError, errorKindOf, type ModelErrorKind } from "./model.js";
3
+ import { classifyModelError, errorKindOf, isModelStatus, modelErrorOf, type ModelErrorKind } from "./model.js";
4
4
  import { noTextReason } from "./output-limit.js";
5
5
  import { statusOf, withRetry, type RetryOptions } from "./retry.js";
6
6
  import { estimateTokens, jsonBytes } from "./text.js";
@@ -117,7 +117,7 @@ export function modelFailureReason(error: unknown): string {
117
117
  if (noText !== undefined) return `The model returned no text${PLAIN_REASON.test(noText) ? ` (${noText})` : ""}.`;
118
118
  const status = httpStatusOf(error);
119
119
  let kind: unknown = errorKindOf(error);
120
- if (!knownKind(kind) && status !== undefined && (status === 0 || status >= 400)) {
120
+ if (!knownKind(kind) && isModelStatus(status)) {
121
121
  const code = (error as { code?: unknown } | null)?.code;
122
122
  kind = classifyModelError(status, error instanceof Error ? error.message : "", { code: typeof code === "string" ? code : undefined }).kind;
123
123
  }
@@ -384,7 +384,7 @@ export async function analyzeWholeDocument(options: {
384
384
  return [{ from, to, text, read: true }];
385
385
  } catch (error) {
386
386
  if (signal?.aborted) throw error;
387
- if (!halved && to > from && errorKindOf(error) === "context_overflow" && halvings > 0) {
387
+ if (!halved && to > from && modelErrorOf(error)?.kind === "context_overflow" && halvings > 0) {
388
388
  halvings--;
389
389
  const middle = Math.floor((from + to) / 2);
390
390
  return [
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
@@ -347,6 +356,9 @@ export function classifyModelError(status: number, message: string, signals: { c
347
356
  return { kind: "invalid" };
348
357
  }
349
358
 
359
+ const MODEL_REQUEST_ERROR = Symbol.for("llm-large-files.ModelRequestError");
360
+ const DOCUMENTS_NOT_FOUND_ERROR = Symbol.for("llm-large-files.DocumentsNotFoundError");
361
+
350
362
  /**
351
363
  * A model request that failed: the provider answered with an HTTP error, or could not be reached
352
364
  * (`status` 0). `retryAfterMs` is the wait the provider asked for, when it named one. `kind` says
@@ -380,7 +392,23 @@ export class ModelRequestError extends Error {
380
392
  if (found.requestedTokens) this.requestedTokens = found.requestedTokens;
381
393
  }
382
394
  }
395
+
396
+ /**
397
+ * Whether `value` is a `ModelRequestError` (or one of its kinds, such as `GeminiRequestError`),
398
+ * made by this copy of the package or another one. New in 0.9.0.
399
+ */
400
+ static is(value: unknown): value is ModelRequestError {
401
+ return branded(value, MODEL_REQUEST_ERROR);
402
+ }
383
403
  }
404
+ brand(ModelRequestError, MODEL_REQUEST_ERROR);
405
+
406
+ /**
407
+ * A status of a model's error: 0 (no answer came) or an HTTP error, 400 to 599. `errorInfoOf`, the
408
+ * events, `modelFailureReason` and the retry policy tell the kind of an error without one of its
409
+ * own (an adapter of your own, `httpError`) from such a status.
410
+ */
411
+ export const isModelStatus = (status: unknown): status is number => typeof status === "number" && (status === 0 || (status >= 400 && status <= 599));
384
412
 
385
413
  /** The kind of a model error, when it has one: a `ModelRequestError`'s, or that of an error with the same field. */
386
414
  export function errorKindOf(error: unknown): ModelErrorKind | undefined {
@@ -388,24 +416,60 @@ export function errorKindOf(error: unknown): ModelErrorKind | undefined {
388
416
  return typeof kind === "string" ? kind as ModelErrorKind : undefined;
389
417
  }
390
418
 
419
+ /**
420
+ * What a model's error is, as its events and `errorInfoOf` tell it: its own `kind`, with its own
421
+ * `limitTokens` and `requestedTokens`; or, for an error without a kind that has a model's status
422
+ * (an adapter of your own, `httpError`), what `classifyModelError` gives its status, message and
423
+ * `code`, with the numbers of an overflow its message gives. Undefined for an error with neither,
424
+ * and for thrown plain data, which is not an adapter's error. The retry policy and the loop act on
425
+ * this kind: the loop makes a request smaller, plans the documents again, or reads a part of a
426
+ * whole-file read as two halves, for a "context_overflow".
427
+ */
428
+ export function modelErrorOf(error: unknown): { kind: unknown; limitTokens?: number; requestedTokens?: number } | undefined {
429
+ if (typeof error !== "object" || error === null) return undefined;
430
+ const { kind, limitTokens, requestedTokens } = error as { kind?: unknown; limitTokens?: number; requestedTokens?: number };
431
+ if (kind !== undefined) return { kind, limitTokens, requestedTokens };
432
+ const { status, message, code } = error as { status?: unknown; message?: unknown; code?: unknown };
433
+ if (!isModelStatus(status)) return undefined;
434
+ const proto: unknown = Object.getPrototypeOf(error);
435
+ if (proto === Object.prototype || proto === null) return undefined;
436
+ return classifyModelError(status, typeof message === "string" ? message : "", { code: typeof code === "string" ? code : undefined });
437
+ }
438
+
391
439
  /**
392
440
  * For a request refused as larger than the tokens a minute (or a day) of the provider allows, not
393
441
  * than the model's window: that period. Undefined for any other error.
394
442
  */
395
443
  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];
444
+ if (modelErrorOf(error)?.kind !== "context_overflow") return undefined;
445
+ // modelErrorOf found an object: its message is read whatever its class, as modelErrorOf reads it.
446
+ const { message } = error as { message?: unknown };
447
+ if (typeof message !== "string") return undefined;
448
+ const period = RATE_TOO_SMALL.exec(message)?.[1];
398
449
  if (period === undefined) return undefined;
399
450
  return period.toLowerCase() === "day" ? "day" : "minute";
400
451
  }
401
452
 
402
- /** Some document IDs were never uploaded, have expired, or belong to another owner. */
453
+ /**
454
+ * Some document IDs were never uploaded, have expired, or belong to another owner. `code` and
455
+ * `status` are new in 0.9.0.
456
+ */
403
457
  export class DocumentsNotFoundError extends Error {
458
+ readonly code: "documents_missing" = "documents_missing";
459
+ /** The HTTP status to answer with. */
460
+ readonly status: 404 = 404;
461
+
404
462
  constructor(readonly ids: string[]) {
405
463
  super(`${ids.length === 1 ? "A document is" : `${ids.length} documents are`} no longer available: ${ids.join(", ")}. Upload ${ids.length === 1 ? "it" : "them"} again.`);
406
464
  this.name = "DocumentsNotFoundError";
407
465
  }
466
+
467
+ /** Whether `value` is a `DocumentsNotFoundError`, made by this copy of the package or another one. New in 0.9.0. */
468
+ static is(value: unknown): value is DocumentsNotFoundError {
469
+ return branded(value, DOCUMENTS_NOT_FOUND_ERROR);
470
+ }
408
471
  }
472
+ brand(DocumentsNotFoundError, DOCUMENTS_NOT_FOUND_ERROR);
409
473
 
410
474
  const EMPTY_USAGE: ModelUsage = { inputTokens: 0, cachedInputTokens: 0, outputTokens: 0, thoughtTokens: 0 };
411
475