llm-large-files 0.5.0 → 0.7.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 (221) hide show
  1. package/CHANGELOG.md +1402 -467
  2. package/README.md +188 -60
  3. package/dist/analyze.d.ts +5 -0
  4. package/dist/analyze.d.ts.map +1 -1
  5. package/dist/analyze.js +96 -47
  6. package/dist/analyze.js.map +1 -1
  7. package/dist/anthropic/adapter.d.ts +14 -3
  8. package/dist/anthropic/adapter.d.ts.map +1 -1
  9. package/dist/anthropic/adapter.js +190 -39
  10. package/dist/anthropic/adapter.js.map +1 -1
  11. package/dist/ask.d.ts +60 -4
  12. package/dist/ask.d.ts.map +1 -1
  13. package/dist/ask.js +1068 -206
  14. package/dist/ask.js.map +1 -1
  15. package/dist/encoding.d.ts +17 -0
  16. package/dist/encoding.d.ts.map +1 -0
  17. package/dist/encoding.js +852 -0
  18. package/dist/encoding.js.map +1 -0
  19. package/dist/errors.d.ts +4 -0
  20. package/dist/errors.d.ts.map +1 -1
  21. package/dist/errors.js +15 -0
  22. package/dist/errors.js.map +1 -1
  23. package/dist/events.d.ts +333 -0
  24. package/dist/events.d.ts.map +1 -0
  25. package/dist/events.js +159 -0
  26. package/dist/events.js.map +1 -0
  27. package/dist/evidence.d.ts +2 -0
  28. package/dist/evidence.d.ts.map +1 -1
  29. package/dist/evidence.js +3 -2
  30. package/dist/evidence.js.map +1 -1
  31. package/dist/extract.d.ts +35 -3
  32. package/dist/extract.d.ts.map +1 -1
  33. package/dist/extract.js +626 -58
  34. package/dist/extract.js.map +1 -1
  35. package/dist/gemini/adapter.d.ts +17 -8
  36. package/dist/gemini/adapter.d.ts.map +1 -1
  37. package/dist/gemini/adapter.js +108 -61
  38. package/dist/gemini/adapter.js.map +1 -1
  39. package/dist/gemini/chat.d.ts +23 -3
  40. package/dist/gemini/chat.d.ts.map +1 -1
  41. package/dist/gemini/chat.js +6 -2
  42. package/dist/gemini/chat.js.map +1 -1
  43. package/dist/gemini/errors.d.ts +9 -4
  44. package/dist/gemini/errors.d.ts.map +1 -1
  45. package/dist/gemini/errors.js +32 -0
  46. package/dist/gemini/errors.js.map +1 -1
  47. package/dist/gemini/generate.d.ts +10 -2
  48. package/dist/gemini/generate.d.ts.map +1 -1
  49. package/dist/gemini/generate.js +13 -7
  50. package/dist/gemini/generate.js.map +1 -1
  51. package/dist/gemini/index.d.ts +3 -3
  52. package/dist/gemini/index.d.ts.map +1 -1
  53. package/dist/gemini/index.js +2 -2
  54. package/dist/gemini/index.js.map +1 -1
  55. package/dist/gemini/sse.d.ts +1 -1
  56. package/dist/gemini/sse.d.ts.map +1 -1
  57. package/dist/gemini/sse.js +1 -1
  58. package/dist/gemini/sse.js.map +1 -1
  59. package/dist/http.d.ts +116 -0
  60. package/dist/http.d.ts.map +1 -1
  61. package/dist/http.js +347 -1
  62. package/dist/http.js.map +1 -1
  63. package/dist/index.d.ts +11 -6
  64. package/dist/index.d.ts.map +1 -1
  65. package/dist/index.js +6 -4
  66. package/dist/index.js.map +1 -1
  67. package/dist/library.d.ts +67 -5
  68. package/dist/library.d.ts.map +1 -1
  69. package/dist/library.js +405 -85
  70. package/dist/library.js.map +1 -1
  71. package/dist/map-reduce.d.ts +28 -1
  72. package/dist/map-reduce.d.ts.map +1 -1
  73. package/dist/map-reduce.js +240 -52
  74. package/dist/map-reduce.js.map +1 -1
  75. package/dist/model.d.ts +84 -2
  76. package/dist/model.d.ts.map +1 -1
  77. package/dist/model.js +182 -1
  78. package/dist/model.js.map +1 -1
  79. package/dist/node/handlers.d.ts +20 -4
  80. package/dist/node/handlers.d.ts.map +1 -1
  81. package/dist/node/handlers.js +122 -20
  82. package/dist/node/handlers.js.map +1 -1
  83. package/dist/node/index.d.ts +2 -0
  84. package/dist/node/index.d.ts.map +1 -1
  85. package/dist/node/index.js +1 -0
  86. package/dist/node/index.js.map +1 -1
  87. package/dist/node/sse.d.ts +59 -0
  88. package/dist/node/sse.d.ts.map +1 -0
  89. package/dist/node/sse.js +174 -0
  90. package/dist/node/sse.js.map +1 -0
  91. package/dist/openai/adapter.d.ts +29 -5
  92. package/dist/openai/adapter.d.ts.map +1 -1
  93. package/dist/openai/adapter.js +311 -37
  94. package/dist/openai/adapter.js.map +1 -1
  95. package/dist/output-limit.d.ts +14 -2
  96. package/dist/output-limit.d.ts.map +1 -1
  97. package/dist/output-limit.js +39 -10
  98. package/dist/output-limit.js.map +1 -1
  99. package/dist/pause.d.ts +24 -0
  100. package/dist/pause.d.ts.map +1 -0
  101. package/dist/pause.js +38 -0
  102. package/dist/pause.js.map +1 -0
  103. package/dist/pdf-pages.d.ts +32 -2
  104. package/dist/pdf-pages.d.ts.map +1 -1
  105. package/dist/pdf-pages.js +275 -14
  106. package/dist/pdf-pages.js.map +1 -1
  107. package/dist/pdf-pictures.d.ts +30 -3
  108. package/dist/pdf-pictures.d.ts.map +1 -1
  109. package/dist/pdf-pictures.js +50 -8
  110. package/dist/pdf-pictures.js.map +1 -1
  111. package/dist/pdf-work.d.ts +40 -0
  112. package/dist/pdf-work.d.ts.map +1 -0
  113. package/dist/pdf-work.js +734 -0
  114. package/dist/pdf-work.js.map +1 -0
  115. package/dist/plan.d.ts +22 -4
  116. package/dist/plan.d.ts.map +1 -1
  117. package/dist/plan.js +61 -12
  118. package/dist/plan.js.map +1 -1
  119. package/dist/retry.d.ts +23 -1
  120. package/dist/retry.d.ts.map +1 -1
  121. package/dist/retry.js +33 -3
  122. package/dist/retry.js.map +1 -1
  123. package/dist/search.d.ts +8 -2
  124. package/dist/search.d.ts.map +1 -1
  125. package/dist/search.js +60 -10
  126. package/dist/search.js.map +1 -1
  127. package/dist/sse.d.ts +35 -2
  128. package/dist/sse.d.ts.map +1 -1
  129. package/dist/sse.js +145 -31
  130. package/dist/sse.js.map +1 -1
  131. package/dist/stores.d.ts.map +1 -1
  132. package/dist/stores.js +5 -2
  133. package/dist/stores.js.map +1 -1
  134. package/dist/text-index.d.ts +6 -0
  135. package/dist/text-index.d.ts.map +1 -1
  136. package/dist/text-index.js +25 -8
  137. package/dist/text-index.js.map +1 -1
  138. package/dist/text.d.ts +62 -2
  139. package/dist/text.d.ts.map +1 -1
  140. package/dist/text.js +314 -14
  141. package/dist/text.js.map +1 -1
  142. package/dist/tools.d.ts +61 -1
  143. package/dist/tools.d.ts.map +1 -1
  144. package/dist/tools.js +269 -42
  145. package/dist/tools.js.map +1 -1
  146. package/dist/types.d.ts +102 -3
  147. package/dist/types.d.ts.map +1 -1
  148. package/dist/zip-directory.d.ts +43 -0
  149. package/dist/zip-directory.d.ts.map +1 -0
  150. package/dist/zip-directory.js +130 -0
  151. package/dist/zip-directory.js.map +1 -0
  152. package/docs/README.md +288 -0
  153. package/docs/api.md +37 -381
  154. package/docs/configuration.md +28 -558
  155. package/docs/getting-started.md +407 -223
  156. package/docs/guides/anthropic.md +590 -0
  157. package/docs/guides/conversations.md +629 -0
  158. package/docs/guides/costs-and-limits.md +726 -0
  159. package/docs/guides/deployment.md +654 -0
  160. package/docs/guides/errors-and-reliability.md +735 -0
  161. package/docs/guides/explorer-model.md +399 -0
  162. package/docs/guides/openai-compatible.md +602 -0
  163. package/docs/guides/own-adapter-and-loop.md +1169 -0
  164. package/docs/guides/pdfs.md +971 -0
  165. package/docs/guides/storage.md +731 -0
  166. package/docs/guides/testing.md +1199 -0
  167. package/docs/guides/vertex-and-gemini.md +717 -0
  168. package/docs/guides/web-app.md +1589 -0
  169. package/docs/how-it-works.md +1012 -164
  170. package/docs/measurements.md +881 -0
  171. package/docs/publishing.md +274 -46
  172. package/docs/reference/anthropic.md +427 -0
  173. package/docs/reference/ask.md +892 -0
  174. package/docs/reference/errors-and-retries.md +758 -0
  175. package/docs/reference/events.md +993 -0
  176. package/docs/reference/gemini.md +823 -0
  177. package/docs/reference/library.md +1467 -0
  178. package/docs/reference/node.md +528 -0
  179. package/docs/reference/openai.md +385 -0
  180. package/docs/reference/tools.md +883 -0
  181. package/docs/security.md +897 -103
  182. package/docs/troubleshooting.md +1390 -0
  183. package/package.json +2 -2
  184. package/src/analyze.ts +93 -42
  185. package/src/anthropic/adapter.ts +196 -39
  186. package/src/ask.ts +1087 -202
  187. package/src/encoding.ts +850 -0
  188. package/src/errors.ts +17 -0
  189. package/src/events.ts +442 -0
  190. package/src/evidence.ts +5 -2
  191. package/src/extract.ts +602 -62
  192. package/src/gemini/adapter.ts +118 -65
  193. package/src/gemini/chat.ts +28 -4
  194. package/src/gemini/errors.ts +34 -1
  195. package/src/gemini/generate.ts +23 -10
  196. package/src/gemini/index.ts +3 -3
  197. package/src/gemini/sse.ts +1 -1
  198. package/src/http.ts +376 -1
  199. package/src/index.ts +27 -5
  200. package/src/library.ts +459 -85
  201. package/src/map-reduce.ts +252 -51
  202. package/src/model.ts +236 -3
  203. package/src/node/handlers.ts +137 -22
  204. package/src/node/index.ts +2 -0
  205. package/src/node/sse.ts +198 -0
  206. package/src/openai/adapter.ts +306 -38
  207. package/src/output-limit.ts +44 -8
  208. package/src/pause.ts +41 -0
  209. package/src/pdf-pages.ts +268 -15
  210. package/src/pdf-pictures.ts +56 -9
  211. package/src/pdf-work.ts +722 -0
  212. package/src/plan.ts +68 -12
  213. package/src/retry.ts +49 -5
  214. package/src/search.ts +63 -10
  215. package/src/sse.ts +153 -28
  216. package/src/stores.ts +5 -2
  217. package/src/text-index.ts +26 -7
  218. package/src/text.ts +323 -13
  219. package/src/tools.ts +261 -42
  220. package/src/types.ts +107 -4
  221. package/src/zip-directory.ts +136 -0
@@ -0,0 +1,136 @@
1
+ /**
2
+ * Reads the directory of a ZIP file the way JSZip 3.10 reads it, before JSZip does, and adds up
3
+ * what JSZip would keep from it. JSZip reads the whole directory into memory before it unpacks any
4
+ * part, and the directory is written by whoever made the file: its count of parts, where it
5
+ * starts, and how long each record is. So the count of parts is not taken from the end record. The
6
+ * directory is found where JSZip will find it, and its records are walked as JSZip will walk them,
7
+ * each by its own lengths, until JSZip would stop, or the file is past a limit.
8
+ */
9
+
10
+ /** What JSZip would keep in memory from the directory of a ZIP file. */
11
+ export type ZipDirectory = {
12
+ /** The parts JSZip would read, counted to one past the limit. */
13
+ parts: number;
14
+ /** Their extra fields, counted to one past the limit: JSZip keeps about 250 bytes for each. */
15
+ extraFields: number;
16
+ /**
17
+ * The bytes JSZip turns into text, or goes through one by one, counted until they pass the
18
+ * limit: the comment of the file, and for each part the name in the local header it points to
19
+ * (a header that many parts point to counts once for each), its comment, and the data of its
20
+ * ZIP64 (1), Unicode Path (0x7075) and Unicode Comment (0x6375) extra fields. That data may run
21
+ * past the record, over the records after it.
22
+ */
23
+ textBytes: number;
24
+ /**
25
+ * The ZIP64 end record says it is longer than its fields. JSZip reads what follows in a loop that
26
+ * does not move on: it stops when it runs out of data, or never.
27
+ */
28
+ endless: boolean;
29
+ };
30
+
31
+ const LOCAL = 0x04034b50;
32
+ const CENTRAL = 0x02014b50;
33
+ const END = 0x06054b50;
34
+ const LOCATOR = 0x07064b50;
35
+ const END64 = 0x06064b50;
36
+
37
+ /**
38
+ * What JSZip would keep from the directory of `bytes`, up to the limits. Undefined when JSZip
39
+ * would stop before it reads any part: the file is not a ZIP, or is damaged, and JSZip will say
40
+ * so.
41
+ *
42
+ * JSZip reads the whole directory, then the local header of each part, and stops at the first part
43
+ * it cannot read (a local header that is not there, an unknown way of packing). The names and
44
+ * comments of the parts after it are counted too: such a file is not read either way.
45
+ */
46
+ export function readZipDirectory(bytes: Uint8Array, limits: { parts: number; extraFields: number; textBytes: number }): ZipDirectory | undefined {
47
+ const length = bytes.length;
48
+ const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
49
+ // JSZip reads 4 bytes as a signed number, and of 8 bytes only the low 4, as a signed number.
50
+ const int = (at: number) => view.getInt32(at, true);
51
+ const short = (at: number) => view.getUint16(at, true);
52
+ const at4 = (at: number, signature: number) => at >= 0 && at + 4 <= length && view.getUint32(at, true) === signature;
53
+ // JSZip looks for a record from the end of the whole file, not only of its last 64 KB.
54
+ const whole = Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength);
55
+ const lastIndexOf = (signature: number) => {
56
+ const needle = Buffer.alloc(4);
57
+ needle.writeUInt32LE(signature);
58
+ return whole.lastIndexOf(needle);
59
+ };
60
+
61
+ // The end record: 22 bytes, then its comment.
62
+ const end = lastIndexOf(END);
63
+ if (end < 0 || end + 22 > length || end + 22 + short(end + 20) > length) return undefined;
64
+ let size = int(end + 12);
65
+ let offset = int(end + 16);
66
+ let zip64 = 0;
67
+ if ([4, 6, 8, 10].some((field) => short(end + field) === 0xffff) || size === -1 || offset === -1) {
68
+ // A field too small for its value: JSZip reads the ZIP64 end record where the last ZIP64
69
+ // locator of the file says it is, or else the last ZIP64 end record of the file.
70
+ const locator = lastIndexOf(LOCATOR);
71
+ if (locator < 0 || locator + 20 > length || int(locator + 16) > 1) return undefined;
72
+ let end64 = int(locator + 8);
73
+ if (end64 < 0 || end64 + 4 > length) return undefined;
74
+ if (!at4(end64, END64)) end64 = lastIndexOf(END64);
75
+ if (end64 < 0 || end64 + 56 > length) return undefined;
76
+ if (int(end64 + 4) > 44) return { parts: 0, extraFields: 0, textBytes: 0, endless: true };
77
+ zip64 = 20 + 12 + int(end64 + 4);
78
+ size = int(end64 + 40);
79
+ offset = int(end64 + 48);
80
+ }
81
+ // Bytes before the archive, such as a program that unpacks it: JSZip reads every offset after them.
82
+ const before = end - (offset + size + zip64);
83
+ if (before < 0 || offset < 0 || before + offset > length) return undefined;
84
+
85
+ const directory: ZipDirectory = { parts: 0, extraFields: 0, textBytes: short(end + 20), endless: false };
86
+ /** Adds bytes JSZip turns into text or goes through, and says whether they are past the limit. */
87
+ const text = (count: number) => (directory.textBytes += count) > limits.textBytes;
88
+ let at = before + offset;
89
+ // A record: 46 bytes, the name, the extra fields and the comment.
90
+ while (at4(at, CENTRAL) && at + 46 <= length) {
91
+ // JSZip stops at an encrypted part.
92
+ if (short(at + 8) & 1) break;
93
+ const extraStart = at + 46 + short(at + 28);
94
+ const extraEnd = extraStart + short(at + 30);
95
+ if (extraEnd > length) break;
96
+ // Each extra field is 4 bytes and its data, read while more than 4 bytes are left. JSZip keeps
97
+ // the last ZIP64 field (id 1) and goes on from the end of the extra fields.
98
+ let field = extraStart;
99
+ let zip64Data = -1;
100
+ let zip64End = -1;
101
+ while (field + 4 < extraEnd) {
102
+ if (++directory.extraFields > limits.extraFields) return directory;
103
+ const id = short(field);
104
+ const fieldEnd = field + 4 + short(field + 2);
105
+ // JSZip goes through the data of these fields byte by byte, and decodes the Unicode ones as
106
+ // text, even when the data runs past the extra fields of the record.
107
+ if ((id === 1 || id === 0x7075 || id === 0x6375) && text(fieldEnd - field - 4)) return directory;
108
+ if (id === 1) {
109
+ zip64Data = field + 4;
110
+ zip64End = fieldEnd;
111
+ }
112
+ field = fieldEnd;
113
+ }
114
+ if (field > length) break;
115
+ let local = int(at + 42);
116
+ if (zip64Data >= 0) {
117
+ // The ZIP64 field holds, 8 bytes each, the sizes and the offset the record has no room for,
118
+ // in that order.
119
+ const values = [int(at + 24), int(at + 20), local].filter((value) => value === -1).length;
120
+ if (zip64Data + 8 * values > zip64End) break;
121
+ if (local === -1) local = int(zip64Data + 8 * values - 8);
122
+ }
123
+ const comment = short(at + 32);
124
+ const next = extraEnd + comment;
125
+ if (next > length) break;
126
+ if (++directory.parts > limits.parts) return directory;
127
+ if (text(comment)) return directory;
128
+ // The name is decoded from the local header the part points to.
129
+ const header = before + local;
130
+ if (local >= 0 && at4(header, LOCAL) && header + 30 <= length && header + 30 + short(header + 26) <= length) {
131
+ if (text(short(header + 26))) return directory;
132
+ }
133
+ at = next;
134
+ }
135
+ return directory;
136
+ }