llm-large-files 0.9.0 → 0.10.1

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 (222) hide show
  1. package/CHANGELOG.md +500 -0
  2. package/README.md +13 -7
  3. package/dist/access.d.ts +45 -3
  4. package/dist/access.d.ts.map +1 -1
  5. package/dist/access.js +106 -3
  6. package/dist/access.js.map +1 -1
  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 +115 -14
  24. package/dist/ask.d.ts.map +1 -1
  25. package/dist/ask.js +572 -103
  26. package/dist/ask.js.map +1 -1
  27. package/dist/citations.d.ts +36 -0
  28. package/dist/citations.d.ts.map +1 -0
  29. package/dist/citations.js +298 -0
  30. package/dist/citations.js.map +1 -0
  31. package/dist/error-info.d.ts +13 -0
  32. package/dist/error-info.d.ts.map +1 -1
  33. package/dist/error-info.js +37 -1
  34. package/dist/error-info.js.map +1 -1
  35. package/dist/errors.d.ts +3 -1
  36. package/dist/errors.d.ts.map +1 -1
  37. package/dist/errors.js +3 -1
  38. package/dist/errors.js.map +1 -1
  39. package/dist/events.d.ts +27 -8
  40. package/dist/events.d.ts.map +1 -1
  41. package/dist/events.js.map +1 -1
  42. package/dist/evidence.d.ts.map +1 -1
  43. package/dist/evidence.js +21 -2
  44. package/dist/evidence.js.map +1 -1
  45. package/dist/extract.d.ts.map +1 -1
  46. package/dist/extract.js +6 -1
  47. package/dist/extract.js.map +1 -1
  48. package/dist/gemini/adapter.d.ts +35 -4
  49. package/dist/gemini/adapter.d.ts.map +1 -1
  50. package/dist/gemini/adapter.js +68 -29
  51. package/dist/gemini/adapter.js.map +1 -1
  52. package/dist/gemini/chat.d.ts +28 -55
  53. package/dist/gemini/chat.d.ts.map +1 -1
  54. package/dist/gemini/chat.js +20 -27
  55. package/dist/gemini/chat.js.map +1 -1
  56. package/dist/gemini/endpoints.d.ts +59 -11
  57. package/dist/gemini/endpoints.d.ts.map +1 -1
  58. package/dist/gemini/endpoints.js +33 -7
  59. package/dist/gemini/endpoints.js.map +1 -1
  60. package/dist/gemini/errors.d.ts +39 -5
  61. package/dist/gemini/errors.d.ts.map +1 -1
  62. package/dist/gemini/errors.js +70 -9
  63. package/dist/gemini/errors.js.map +1 -1
  64. package/dist/gemini/generate.d.ts +53 -2
  65. package/dist/gemini/generate.d.ts.map +1 -1
  66. package/dist/gemini/generate.js +100 -50
  67. package/dist/gemini/generate.js.map +1 -1
  68. package/dist/gemini/index.d.ts +3 -3
  69. package/dist/gemini/index.d.ts.map +1 -1
  70. package/dist/http.d.ts +41 -3
  71. package/dist/http.d.ts.map +1 -1
  72. package/dist/http.js +40 -2
  73. package/dist/http.js.map +1 -1
  74. package/dist/index.d.ts +7 -5
  75. package/dist/index.d.ts.map +1 -1
  76. package/dist/index.js +4 -2
  77. package/dist/index.js.map +1 -1
  78. package/dist/ingest.d.ts +9 -1
  79. package/dist/ingest.d.ts.map +1 -1
  80. package/dist/ingest.js +50 -25
  81. package/dist/ingest.js.map +1 -1
  82. package/dist/library.d.ts +129 -2
  83. package/dist/library.d.ts.map +1 -1
  84. package/dist/library.js +224 -149
  85. package/dist/library.js.map +1 -1
  86. package/dist/map-reduce.d.ts +176 -10
  87. package/dist/map-reduce.d.ts.map +1 -1
  88. package/dist/map-reduce.js +804 -58
  89. package/dist/map-reduce.js.map +1 -1
  90. package/dist/model.d.ts +36 -4
  91. package/dist/model.d.ts.map +1 -1
  92. package/dist/model.js +17 -1
  93. package/dist/model.js.map +1 -1
  94. package/dist/node/handlers.d.ts +29 -8
  95. package/dist/node/handlers.d.ts.map +1 -1
  96. package/dist/node/handlers.js +148 -126
  97. package/dist/node/handlers.js.map +1 -1
  98. package/dist/node/index.d.ts +1 -1
  99. package/dist/node/index.d.ts.map +1 -1
  100. package/dist/node/index.js.map +1 -1
  101. package/dist/node/ingest-child.js +2 -2
  102. package/dist/node/ingest-child.js.map +1 -1
  103. package/dist/node/spool.d.ts +9 -5
  104. package/dist/node/spool.d.ts.map +1 -1
  105. package/dist/node/spool.js +28 -15
  106. package/dist/node/spool.js.map +1 -1
  107. package/dist/node/sse.d.ts +6 -5
  108. package/dist/node/sse.d.ts.map +1 -1
  109. package/dist/node/sse.js +8 -7
  110. package/dist/node/sse.js.map +1 -1
  111. package/dist/openai/adapter.d.ts +5 -1
  112. package/dist/openai/adapter.d.ts.map +1 -1
  113. package/dist/openai/adapter.js +53 -12
  114. package/dist/openai/adapter.js.map +1 -1
  115. package/dist/output-limit.d.ts +2 -1
  116. package/dist/output-limit.d.ts.map +1 -1
  117. package/dist/output-limit.js +5 -2
  118. package/dist/output-limit.js.map +1 -1
  119. package/dist/pdf-outline.d.ts +71 -0
  120. package/dist/pdf-outline.d.ts.map +1 -0
  121. package/dist/pdf-outline.js +414 -0
  122. package/dist/pdf-outline.js.map +1 -0
  123. package/dist/plan.d.ts +137 -7
  124. package/dist/plan.d.ts.map +1 -1
  125. package/dist/plan.js +248 -26
  126. package/dist/plan.js.map +1 -1
  127. package/dist/prepare.d.ts +50 -0
  128. package/dist/prepare.d.ts.map +1 -0
  129. package/dist/prepare.js +207 -0
  130. package/dist/prepare.js.map +1 -0
  131. package/dist/ranged.d.ts +2 -1
  132. package/dist/ranged.d.ts.map +1 -1
  133. package/dist/ranged.js +16 -3
  134. package/dist/ranged.js.map +1 -1
  135. package/dist/search.d.ts +7 -0
  136. package/dist/search.d.ts.map +1 -1
  137. package/dist/search.js +72 -10
  138. package/dist/search.js.map +1 -1
  139. package/dist/stores.d.ts +6 -0
  140. package/dist/stores.d.ts.map +1 -1
  141. package/dist/stores.js +82 -11
  142. package/dist/stores.js.map +1 -1
  143. package/dist/tools.d.ts +22 -2
  144. package/dist/tools.d.ts.map +1 -1
  145. package/dist/tools.js +323 -76
  146. package/dist/tools.js.map +1 -1
  147. package/dist/types.d.ts +49 -1
  148. package/dist/types.d.ts.map +1 -1
  149. package/dist/whole-cost.d.ts +37 -0
  150. package/dist/whole-cost.d.ts.map +1 -0
  151. package/dist/whole-cost.js +46 -0
  152. package/dist/whole-cost.js.map +1 -0
  153. package/docs/README.md +4 -0
  154. package/docs/guides/anthropic.md +5 -2
  155. package/docs/guides/conversations.md +1 -1
  156. package/docs/guides/costs-and-limits.md +133 -36
  157. package/docs/guides/deployment.md +106 -13
  158. package/docs/guides/errors-and-reliability.md +158 -45
  159. package/docs/guides/explorer-model.md +4 -1
  160. package/docs/guides/openai-compatible.md +47 -7
  161. package/docs/guides/own-adapter-and-loop.md +33 -6
  162. package/docs/guides/pdfs.md +96 -7
  163. package/docs/guides/storage.md +46 -3
  164. package/docs/guides/testing.md +71 -0
  165. package/docs/guides/vertex-and-gemini.md +159 -50
  166. package/docs/guides/web-app.md +10 -6
  167. package/docs/how-it-works.md +120 -22
  168. package/docs/measurements.md +172 -5
  169. package/docs/publishing.md +76 -4
  170. package/docs/reference/anthropic.md +12 -7
  171. package/docs/reference/ask.md +264 -35
  172. package/docs/reference/errors-and-retries.md +30 -2
  173. package/docs/reference/events.md +27 -10
  174. package/docs/reference/gemini.md +189 -68
  175. package/docs/reference/library.md +190 -23
  176. package/docs/reference/node.md +51 -17
  177. package/docs/reference/openai.md +21 -10
  178. package/docs/reference/tools.md +273 -26
  179. package/docs/security.md +101 -12
  180. package/docs/troubleshooting.md +14 -12
  181. package/docs/upgrading-0.10.md +484 -0
  182. package/package.json +5 -1
  183. package/src/access.ts +127 -4
  184. package/src/aggregate.ts +92 -14
  185. package/src/analyze.ts +2 -2
  186. package/src/answer-info.ts +45 -0
  187. package/src/anthropic/adapter.ts +40 -11
  188. package/src/ask.ts +646 -106
  189. package/src/citations.ts +301 -0
  190. package/src/error-info.ts +36 -1
  191. package/src/errors.ts +3 -1
  192. package/src/events.ts +31 -7
  193. package/src/evidence.ts +21 -2
  194. package/src/extract.ts +6 -1
  195. package/src/gemini/adapter.ts +99 -32
  196. package/src/gemini/chat.ts +53 -82
  197. package/src/gemini/endpoints.ts +89 -16
  198. package/src/gemini/errors.ts +74 -11
  199. package/src/gemini/generate.ts +147 -57
  200. package/src/gemini/index.ts +3 -3
  201. package/src/http.ts +65 -4
  202. package/src/index.ts +7 -5
  203. package/src/ingest.ts +58 -27
  204. package/src/library.ts +336 -142
  205. package/src/map-reduce.ts +855 -67
  206. package/src/model.ts +44 -4
  207. package/src/node/handlers.ts +159 -118
  208. package/src/node/index.ts +1 -0
  209. package/src/node/ingest-child.ts +2 -2
  210. package/src/node/spool.ts +28 -14
  211. package/src/node/sse.ts +8 -7
  212. package/src/openai/adapter.ts +56 -13
  213. package/src/output-limit.ts +5 -2
  214. package/src/pdf-outline.ts +400 -0
  215. package/src/plan.ts +287 -36
  216. package/src/prepare.ts +253 -0
  217. package/src/ranged.ts +16 -3
  218. package/src/search.ts +69 -13
  219. package/src/stores.ts +76 -11
  220. package/src/tools.ts +327 -75
  221. package/src/types.ts +45 -1
  222. package/src/whole-cost.ts +64 -0
package/src/types.ts CHANGED
@@ -211,6 +211,17 @@ export type ExtractedText = {
211
211
  * to hash it, a slice at a time. Other extractors leave it out. New in 0.9.0.
212
212
  */
213
213
  file?: { bytes: number; sha256: string };
214
+ /**
215
+ * A PDF's headings, found from the size of its type: each line set larger than the body, with
216
+ * its page (from 1), its level (1 is the top) and its text, in the order of the text; and `skip`,
217
+ * when lines above it on its page read the same (a contents list, a running header), how many.
218
+ * The built-in PDF extractor returns them; `ingest` finds each among the lines of its page (the
219
+ * lines after `--- Page N ---`), with spaces left out of the comparison, past the first `skip`
220
+ * that match, and makes them the outline when the text's own rules find fewer than 3 headings and
221
+ * it finds 3 or more. Read for a PDF only. An extractor that wraps the built-in one passes them on
222
+ * to keep that outline. New in 0.10.0.
223
+ */
224
+ headings?: Array<{ page: number; level: number; title: string; skip?: number }>;
214
225
  };
215
226
 
216
227
  export type ExtractInput = {
@@ -341,7 +352,12 @@ export type ToolCallLimits = {
341
352
  /**
342
353
  * The most model requests a whole-file read (`analyze_whole_document`) may make: what the
343
354
  * question has left of `limits.requests`. A part the model refuses as too large is read as two
344
- * halves only while that and `maxMapChunks` allow it.
355
+ * halves only while that and `maxMapChunks` allow it. Given to a `read_document` call (since
356
+ * 0.10.0), it is what a whole read of its file may make on the model's next turn, the first it
357
+ * can call one on: a read cut short of its range points at one (`whole_file`) only when the
358
+ * file's estimated parts are within it. `askWithFiles` gives a read 0 when the question would not
359
+ * start a whole read of its file on that turn (a ceiling it would pass, with that turn's request
360
+ * counted, or its whole reads made).
345
361
  */
346
362
  maxRequests?: number;
347
363
  /**
@@ -376,6 +392,11 @@ export type ToolCallHooks = {
376
392
  * the server is busy. `askWithFiles` makes it a "page_looks_busy" notice. New in 0.9.0.
377
393
  */
378
394
  onPagesBusy?: (documentId: string) => void;
395
+ /**
396
+ * Ends this call alone: its scans, regex searches, whole-file read, look at pages and loads. It is
397
+ * joined with the tool set's own signal (`ToolOptions.signal`). New in 0.10.0.
398
+ */
399
+ signal?: AbortSignal;
379
400
  };
380
401
 
381
402
  /** One model request of a tool call (`ToolCallHooks.onRequest`). */
@@ -423,6 +444,13 @@ export type ToolSet = {
423
444
  * before it runs a call.
424
445
  */
425
446
  resultCharacters(name: string): number;
447
+ /**
448
+ * How many more times this tool set may read the document `documentId` whole
449
+ * (`analyze_whole_document`): 0 once a read of it found no part it could read, or at the cap.
450
+ * Optional, so that a tool set of your own still compiles. The tool set of `files.tools()` has
451
+ * it. New in 0.10.0.
452
+ */
453
+ wholeReadsLeft?(documentId: string): number;
426
454
  };
427
455
 
428
456
  /** Tokens one model request used, as far as the provider said. */
@@ -466,6 +494,15 @@ export type DocumentPlan = {
466
494
  inlineBytes: number;
467
495
  };
468
496
 
497
+ /**
498
+ * The name of a document tool, as the model calls it (`TOOL_NAMES`). New in 0.10.0.
499
+ */
500
+ export type ToolName = "search_document" | "read_document" | "get_document_overview" | "analyze_whole_document" | "view_pages" | "aggregate_table";
501
+
502
+ /**
503
+ * What `prepare` gives for one request. The fields new in 0.10.0 are optional, so that a
504
+ * `LargeFiles` of your own still compiles: without them, `askWithFiles` works as in 0.9.
505
+ */
469
506
  export type PreparedContext = {
470
507
  plan: DocumentPlan;
471
508
  /** Prompt text per document ID: the full text for inline documents, a summary card otherwise. */
@@ -479,4 +516,11 @@ export type PreparedContext = {
479
516
  * was called with `viewPages: true`. Pass it to `tools(..., { viewPages })`.
480
517
  */
481
518
  viewable: DocumentMeta[];
519
+ /**
520
+ * Documents sent whole whose tables the tools can still count and search: every CSV and XLSX
521
+ * file, and a file of another format that `aggregate_table` reads whose text has a `| cell |`
522
+ * table of at least 3 rows. Set when `prepare` was called with `inlineTables: "tools"`, else
523
+ * absent. `askWithFiles` makes the tools for them with the searched documents. New in 0.10.0.
524
+ */
525
+ toolable?: DocumentMeta[];
482
526
  };
@@ -0,0 +1,64 @@
1
+ // What a whole-file read (`analyze_whole_document`) costs, before it starts: the input tokens of
2
+ // all its requests, and how many requests it makes, from the document's metadata alone. New in
3
+ // 0.10.0, for the ceiling of a question, which prices a read before it lets it run. Internal: not
4
+ // exported from the package.
5
+ import { estimateMerges, ITEMS_FRAME_TOKENS, PART_HEADING_TOKENS, PART_OUTPUT_TOKENS, partsHaveHeadings, REQUEST_FRAME_TOKENS } from "./map-reduce.js";
6
+ import { estimateParts } from "./plan.js";
7
+ import type { Budget, DocumentMeta } from "./types.js";
8
+
9
+ /** The merges of notes a read makes, worked out from the rounds of `analyzeWholeDocument` (src/map-reduce.ts). */
10
+ export { estimateMerges };
11
+
12
+ /**
13
+ * The digits of the line numbers 1 to `lines`, written out: what the numbers take in the prompts of
14
+ * the parts. `estimateParts` (src/plan.ts) counts them the same way.
15
+ */
16
+ export function lineNumberDigits(lines: number): number {
17
+ let digits = 0;
18
+ for (let width = 1, first = 1; first <= lines; width++, first *= 10) digits += (Math.min(lines, first * 10 - 1) - first + 1) * width;
19
+ return digits;
20
+ }
21
+
22
+ /** What a whole-file read costs. */
23
+ export type WholeReadCost = {
24
+ /** The input tokens of all its requests, in the model's own tokens (the library's estimate times `scale`). */
25
+ inputTokens: number;
26
+ /** All its requests: the parts, the merges of their notes, and the answer of the read. */
27
+ requests: number;
28
+ /** The part reads among them. */
29
+ parts: number;
30
+ /** The merges of notes among them. */
31
+ merges: number;
32
+ };
33
+
34
+ /**
35
+ * The input tokens and requests of a whole-file read of `meta`, with the `budget` of the question
36
+ * and the `scale` of the model that reads it (`capabilities.tokenScale`). The parts are
37
+ * `estimateParts`'s; each sends its lines numbered (the document's tokens, 1.2 tokens a digit of
38
+ * the line numbers and 1.5 a line) with its system text, task and rules, and each after the first
39
+ * of a file with an outline the heading it starts under (`PART_HEADING_TOKENS`).
40
+ * The merges are `estimateMerges`'s, for notes as long as the output limit allows, and the answer
41
+ * of the read is one request more. It is a price before the read: what a read sends is usually
42
+ * less, as notes are shorter than the limit, and not every part starts under a heading. NaN for
43
+ * metadata that does not give its size.
44
+ *
45
+ * `output` "items" (a read that lists items, new in 0.10.0) is the parts alone, each with the
46
+ * longer rules of a list: no merge and no answer. A part read again in halves, when its list does
47
+ * not fit the output limit, is not in the price, as a part halved by an answer read is not.
48
+ */
49
+ export function wholeReadCost(
50
+ meta: Pick<DocumentMeta, "estimatedTokens" | "lines" | "textBytes" | "jsonBytes"> & Partial<Pick<DocumentMeta, "format" | "outline">>,
51
+ budget: Budget,
52
+ scale: number,
53
+ output: "answer" | "items" = "answer",
54
+ ): WholeReadCost {
55
+ const parts = estimateParts(meta, budget);
56
+ const lines = Math.max(1, meta.lines);
57
+ // The first part starts at line 1, under no heading.
58
+ const headings = partsHaveHeadings(meta) ? (parts - 1) * PART_HEADING_TOKENS : 0;
59
+ const numbered = meta.estimatedTokens + 1.2 * lineNumberDigits(lines) + 1.5 * lines + headings;
60
+ if (output === "items") return { inputTokens: Math.ceil((numbered + parts * ITEMS_FRAME_TOKENS) * scale), requests: parts, parts, merges: 0 };
61
+ const merges = estimateMerges(parts, PART_OUTPUT_TOKENS, budget.mapChunkTokens);
62
+ const tokens = numbered + parts * REQUEST_FRAME_TOKENS + merges.inputTokens + merges.finalTokens + REQUEST_FRAME_TOKENS;
63
+ return { inputTokens: Math.ceil(tokens * scale), requests: parts + merges.requests + 1, parts, merges: merges.requests };
64
+ }