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/types.ts CHANGED
@@ -27,10 +27,52 @@ export type LogProfile = {
27
27
  topIssues: LogIssue[];
28
28
  };
29
29
 
30
+ /**
31
+ * Where a document belongs, from the widest part to the narrowest: `[userId, chatId]`. 1 to 8
32
+ * segments, each 1 to 256 UTF-16 units, with no control characters, no "/" or "\", and none of them
33
+ * "." or "..". A scope that breaks these rules is a `DocumentScopeError`. New in 0.9.0.
34
+ */
35
+ export type DocumentScope = readonly string[];
36
+
37
+ /**
38
+ * What the library knows of the caller of a store method: the scope and the owner it was given. It
39
+ * is the last argument of every `DocumentStore` method, and both fields may be absent. A store may
40
+ * keep the documents of each scope apart (a folder, a key prefix, a collection filter), and pass
41
+ * the context on unchanged when it wraps another store. New in 0.9.0.
42
+ */
43
+ export type DocumentContext = { readonly scope?: DocumentScope; readonly owner?: string };
44
+
30
45
  export type DocumentMeta = {
31
46
  id: string;
47
+ /**
48
+ * The version of this metadata's shape: `DOCUMENT_META_VERSION` (1) on documents ingested by
49
+ * 0.9.0 and later, absent on older ones. The library refuses a higher one (a document written by a
50
+ * newer version of the package) with a `DocumentStoreError` of code "NEWER_VERSION". It is never
51
+ * sent to the model. New in 0.9.0.
52
+ */
53
+ metaVersion?: number;
32
54
  /** Who may use this document. Set it at ingest; access with another owner is refused. */
33
55
  owner?: string;
56
+ /**
57
+ * The scope the document was ingested in (`IngestInput.scope`), as a copy. Only a caller with the
58
+ * same scope may use it. It is never sent to the model, nor in a summary. An event holds it only
59
+ * with `AskOptions.eventResults: true`, in the full plan of `run.finish` and `run.error`. New in 0.9.0.
60
+ */
61
+ scope?: string[];
62
+ /**
63
+ * The SHA-256 of the uploaded file, as 64 lowercase hex digits. It is never sent to the model, nor
64
+ * in a summary. An event holds it only with `AskOptions.eventResults: true`, in the full plan of
65
+ * `run.finish` and `run.error`. New in 0.9.0.
66
+ */
67
+ sha256?: string;
68
+ /**
69
+ * The uploaded file, when the application keeps it and the library reads it from there
70
+ * (`IngestInput.original`): where it is, and its size and SHA-256 when it was read, to know that
71
+ * it has not changed since. `ref` is never sent to the model, nor in a summary. An event holds it
72
+ * only with `AskOptions.eventResults: true`, in the full plan of `run.finish` and `run.error`.
73
+ * New in 0.9.0.
74
+ */
75
+ original?: { ref: string; bytes: number; sha256: string };
34
76
  name: string;
35
77
  mimeType: string;
36
78
  format: DocumentFormat;
@@ -109,38 +151,47 @@ export type DocumentSummary = {
109
151
  expiresAt: string;
110
152
  };
111
153
 
112
- /** Where extracted text lives. Implement this for Redis, S3, a database, etc. */
154
+ /**
155
+ * Where extracted text lives. Implement this for Redis, S3, a database, etc.
156
+ *
157
+ * Every method takes a last, optional `context` (`DocumentContext`): the scope and the owner of
158
+ * the call. New in 0.9.0. A store written for 0.8, whose methods take no context, still implements
159
+ * this interface, and works as before. A store that wraps another one passes the context on.
160
+ */
113
161
  export interface DocumentStore {
114
- put(document: StoredDocument): Promise<void>;
162
+ put(document: StoredDocument, context?: DocumentContext): Promise<void>;
115
163
  /** Returns undefined for unknown or expired documents. */
116
- get(id: string): Promise<StoredDocument | undefined>;
117
- getMeta(id: string): Promise<DocumentMeta | undefined>;
164
+ get(id: string, context?: DocumentContext): Promise<StoredDocument | undefined>;
165
+ getMeta(id: string, context?: DocumentContext): Promise<DocumentMeta | undefined>;
118
166
  /** Removes the document, and its original file if one is kept. */
119
- delete(id: string): Promise<boolean>;
120
- /** Removes expired documents (and their original files) and returns how many were removed. */
121
- sweep?(now?: Date): Promise<number>;
167
+ delete(id: string, context?: DocumentContext): Promise<boolean>;
168
+ /**
169
+ * Removes expired documents (and their original files) and returns how many were removed. With
170
+ * a scope in `context`, only those of that scope; without one, those of every scope.
171
+ */
172
+ sweep?(now?: Date, context?: DocumentContext): Promise<number>;
122
173
  /**
123
174
  * Keeps the uploaded file of a document. It is called before `put` for the same ID. Optional: a
124
175
  * store without it works as before, and original files are then not kept.
125
176
  */
126
- putOriginal?(id: string, bytes: Uint8Array): Promise<void>;
177
+ putOriginal?(id: string, bytes: Uint8Array, context?: DocumentContext): Promise<void>;
127
178
  /** The uploaded file, or undefined when none was kept or the document is unknown or expired. */
128
- getOriginal?(id: string): Promise<Uint8Array | undefined>;
179
+ getOriginal?(id: string, context?: DocumentContext): Promise<Uint8Array | undefined>;
129
180
  /**
130
181
  * Bytes `start` (inclusive) to `end` (exclusive) of the stored text, as UTF-8. Optional: with it,
131
182
  * a tool call reads only the lines it needs, and a search reads the text a piece at a time,
132
183
  * instead of loading the whole text into memory. Return fewer bytes at the end of the text, and
133
184
  * undefined when the document is gone. It is called only after `getMeta` found the document.
134
185
  */
135
- readTextRange?(id: string, start: number, end: number): Promise<Uint8Array | undefined>;
186
+ readTextRange?(id: string, start: number, end: number, context?: DocumentContext): Promise<Uint8Array | undefined>;
136
187
  /**
137
188
  * Keeps the line index of a document (a few bytes a line), so that reading by range needs no
138
189
  * pass over the text first. Called at ingest, before `put`, and when an older document without an
139
190
  * index is first read. Optional, and only useful with `readTextRange`.
140
191
  */
141
- putIndex?(id: string, index: Uint8Array): Promise<void>;
192
+ putIndex?(id: string, index: Uint8Array, context?: DocumentContext): Promise<void>;
142
193
  /** The index kept by `putIndex`, or undefined. A damaged one is noticed and built again. */
143
- getIndex?(id: string): Promise<Uint8Array | undefined>;
194
+ getIndex?(id: string, context?: DocumentContext): Promise<Uint8Array | undefined>;
144
195
  }
145
196
 
146
197
  export type ExtractedText = {
@@ -153,6 +204,24 @@ export type ExtractedText = {
153
204
  encoding?: string;
154
205
  /** Characters that were not valid in the encoding the file was read in, each now U+FFFD. Kept as `DocumentMeta.replacedCharacters`. */
155
206
  replacedCharacters?: number;
207
+ /**
208
+ * What an extractor that reads paths (`Extractor.readsPath`) read from `ExtractInput.path`: how
209
+ * many bytes the file has, and their SHA-256, as 64 lowercase hex digits. Kept as
210
+ * `DocumentMeta.bytes` and `DocumentMeta.sha256`. Without it, `ingest` reads the file once more
211
+ * to hash it, a slice at a time. Other extractors leave it out. New in 0.9.0.
212
+ */
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 }>;
156
225
  };
157
226
 
158
227
  export type ExtractInput = {
@@ -187,6 +256,15 @@ export type ExtractInput = {
187
256
  * extractor of your own may ignore it. New in 0.7.0.
188
257
  */
189
258
  onProgress?: (progress: ExtractProgress) => void;
259
+ /**
260
+ * The path of the file, when it is ingested from a path (`IngestFromSource`) and the extractor
261
+ * reads the file itself (`Extractor.readsPath`): the bytes it is given are then empty. Read it
262
+ * once, at most `maxUploadBytes + 1` bytes, refuse it when it has more than `maxUploadBytes`, and
263
+ * return its size and SHA-256 in `ExtractedText.file`. Never write, move or delete it. New in 0.9.0.
264
+ */
265
+ path?: string;
266
+ /** With `path`: the most bytes the file may have (`maxUploadBytes`). New in 0.9.0. */
267
+ maxUploadBytes?: number;
190
268
  };
191
269
 
192
270
  /** How far an extraction is, for `ExtractInput.onProgress`. */
@@ -199,7 +277,14 @@ export type ExtractProgress = {
199
277
  unit: "pages" | "parts" | "bytes";
200
278
  };
201
279
 
202
- export type Extractor = (bytes: Uint8Array, input: ExtractInput) => Promise<ExtractedText>;
280
+ /**
281
+ * Reads the text of a file. A plain function is an extractor, as before. `readsPath`, new in 0.9.0:
282
+ * the extractor reads a file ingested from a path itself, from `ExtractInput.path`, so the server
283
+ * does not read it first (`createIsolatedExtractors` sets it: its child processes read the file).
284
+ * It is then given empty bytes, unless the file is kept in the store (`keepOriginals` without
285
+ * `IngestInput.original`), which needs its bytes in the server.
286
+ */
287
+ export type Extractor = ((bytes: Uint8Array, input: ExtractInput) => Promise<ExtractedText>) & { readsPath?: boolean };
203
288
 
204
289
  /** The limits of the model connection the documents must fit through. */
205
290
  export type Budget = {
@@ -267,9 +352,21 @@ export type ToolCallLimits = {
267
352
  /**
268
353
  * The most model requests a whole-file read (`analyze_whole_document`) may make: what the
269
354
  * question has left of `limits.requests`. A part the model refuses as too large is read as two
270
- * 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).
271
361
  */
272
362
  maxRequests?: number;
363
+ /**
364
+ * The most milliseconds a look at pages (`view_pages`) of this call waits for its turn, when that
365
+ * is less than the library's `pageLookWaitMs`. 0: it runs only when a turn is free now, and is
366
+ * told the server is busy if not. `askWithFiles` gives 0 to the looks of a turn after one of them
367
+ * came back busy, so that a wait that ends busy comes once in a turn. New in 0.9.0.
368
+ */
369
+ maxWaitMs?: number;
273
370
  };
274
371
  /**
275
372
  * Listeners for one tool call, next to its limits. New in 0.7.0: with them, the progress of two
@@ -283,6 +380,23 @@ export type ToolCallHooks = {
283
380
  * returns about each retry of that request, and when it ends.
284
381
  */
285
382
  onRequest?: (request: ToolRequestStart) => ToolRequestHandle;
383
+ /**
384
+ * The document this call names is no longer in the store: deleted or expired since it was
385
+ * planned, or not readable by the caller (which looks the same). The call answers the model with
386
+ * an error that says so. `askWithFiles` makes it a "document_gone" notice. New in 0.9.0.
387
+ */
388
+ onDocumentGone?: (documentId: string) => void;
389
+ /**
390
+ * A look at pages (`view_pages`) waited longer than `pageLookWaitMs` for its turn
391
+ * (`maxConcurrentPageLooks`) and was not run. The call answers the model with an error that says
392
+ * the server is busy. `askWithFiles` makes it a "page_looks_busy" notice. New in 0.9.0.
393
+ */
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;
286
400
  };
287
401
 
288
402
  /** One model request of a tool call (`ToolCallHooks.onRequest`). */
@@ -330,6 +444,13 @@ export type ToolSet = {
330
444
  * before it runs a call.
331
445
  */
332
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;
333
454
  };
334
455
 
335
456
  /** Tokens one model request used, as far as the provider said. */
@@ -373,6 +494,15 @@ export type DocumentPlan = {
373
494
  inlineBytes: number;
374
495
  };
375
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
+ */
376
506
  export type PreparedContext = {
377
507
  plan: DocumentPlan;
378
508
  /** Prompt text per document ID: the full text for inline documents, a summary card otherwise. */
@@ -386,4 +516,11 @@ export type PreparedContext = {
386
516
  * was called with `viewPages: true`. Pass it to `tools(..., { viewPages })`.
387
517
  */
388
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[];
389
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
+ }