llm-large-files 0.7.0 → 0.8.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 (184) hide show
  1. package/CHANGELOG.md +319 -1
  2. package/README.md +10 -8
  3. package/dist/analyze.d.ts +3 -1
  4. package/dist/analyze.d.ts.map +1 -1
  5. package/dist/analyze.js +109 -14
  6. package/dist/analyze.js.map +1 -1
  7. package/dist/anthropic/adapter.d.ts.map +1 -1
  8. package/dist/anthropic/adapter.js +4 -1
  9. package/dist/anthropic/adapter.js.map +1 -1
  10. package/dist/ask.d.ts +3 -3
  11. package/dist/ask.d.ts.map +1 -1
  12. package/dist/ask.js +72 -17
  13. package/dist/ask.js.map +1 -1
  14. package/dist/decode.d.ts +19 -0
  15. package/dist/decode.d.ts.map +1 -0
  16. package/dist/decode.js +97 -0
  17. package/dist/decode.js.map +1 -0
  18. package/dist/errors.d.ts +55 -0
  19. package/dist/errors.d.ts.map +1 -1
  20. package/dist/errors.js +65 -0
  21. package/dist/errors.js.map +1 -1
  22. package/dist/events.d.ts +6 -0
  23. package/dist/events.d.ts.map +1 -1
  24. package/dist/events.js +5 -1
  25. package/dist/events.js.map +1 -1
  26. package/dist/evidence.d.ts.map +1 -1
  27. package/dist/evidence.js +6 -0
  28. package/dist/evidence.js.map +1 -1
  29. package/dist/extract.d.ts +7 -20
  30. package/dist/extract.d.ts.map +1 -1
  31. package/dist/extract.js +49 -751
  32. package/dist/extract.js.map +1 -1
  33. package/dist/gemini/adapter.d.ts.map +1 -1
  34. package/dist/gemini/adapter.js +9 -5
  35. package/dist/gemini/adapter.js.map +1 -1
  36. package/dist/gemini/endpoints.d.ts +5 -1
  37. package/dist/gemini/endpoints.d.ts.map +1 -1
  38. package/dist/gemini/endpoints.js +11 -2
  39. package/dist/gemini/endpoints.js.map +1 -1
  40. package/dist/gemini/generate.d.ts.map +1 -1
  41. package/dist/gemini/generate.js +20 -3
  42. package/dist/gemini/generate.js.map +1 -1
  43. package/dist/groups.d.ts +54 -0
  44. package/dist/groups.d.ts.map +1 -0
  45. package/dist/groups.js +136 -0
  46. package/dist/groups.js.map +1 -0
  47. package/dist/html.d.ts +11 -0
  48. package/dist/html.d.ts.map +1 -0
  49. package/dist/html.js +1112 -0
  50. package/dist/html.js.map +1 -0
  51. package/dist/index.d.ts +2 -1
  52. package/dist/index.d.ts.map +1 -1
  53. package/dist/index.js +1 -1
  54. package/dist/index.js.map +1 -1
  55. package/dist/library.d.ts.map +1 -1
  56. package/dist/library.js +48 -12
  57. package/dist/library.js.map +1 -1
  58. package/dist/limiter.d.ts +2 -1
  59. package/dist/limiter.d.ts.map +1 -1
  60. package/dist/limiter.js +3 -2
  61. package/dist/limiter.js.map +1 -1
  62. package/dist/model.d.ts.map +1 -1
  63. package/dist/model.js +8 -1
  64. package/dist/model.js.map +1 -1
  65. package/dist/node/handlers.d.ts.map +1 -1
  66. package/dist/node/handlers.js +69 -15
  67. package/dist/node/handlers.js.map +1 -1
  68. package/dist/node/index.d.ts +2 -0
  69. package/dist/node/index.d.ts.map +1 -1
  70. package/dist/node/index.js +1 -0
  71. package/dist/node/index.js.map +1 -1
  72. package/dist/node/ingest-child.d.ts +38 -0
  73. package/dist/node/ingest-child.d.ts.map +1 -0
  74. package/dist/node/ingest-child.js +42 -0
  75. package/dist/node/ingest-child.js.map +1 -0
  76. package/dist/node/isolated.d.ts +88 -0
  77. package/dist/node/isolated.d.ts.map +1 -0
  78. package/dist/node/isolated.js +381 -0
  79. package/dist/node/isolated.js.map +1 -0
  80. package/dist/office.d.ts +105 -0
  81. package/dist/office.d.ts.map +1 -0
  82. package/dist/office.js +1092 -0
  83. package/dist/office.js.map +1 -0
  84. package/dist/openai/adapter.d.ts.map +1 -1
  85. package/dist/openai/adapter.js +15 -1
  86. package/dist/openai/adapter.js.map +1 -1
  87. package/dist/pdf-pages.d.ts.map +1 -1
  88. package/dist/pdf-pages.js +2 -6
  89. package/dist/pdf-pages.js.map +1 -1
  90. package/dist/peers.d.ts +8 -0
  91. package/dist/peers.d.ts.map +1 -0
  92. package/dist/peers.js +32 -0
  93. package/dist/peers.js.map +1 -0
  94. package/dist/plan.d.ts.map +1 -1
  95. package/dist/plan.js +1 -1
  96. package/dist/plan.js.map +1 -1
  97. package/dist/ranged.d.ts.map +1 -1
  98. package/dist/ranged.js +4 -2
  99. package/dist/ranged.js.map +1 -1
  100. package/dist/search.d.ts +99 -22
  101. package/dist/search.d.ts.map +1 -1
  102. package/dist/search.js +744 -140
  103. package/dist/search.js.map +1 -1
  104. package/dist/text-index.d.ts +3 -1
  105. package/dist/text-index.d.ts.map +1 -1
  106. package/dist/text-index.js +3 -1
  107. package/dist/text-index.js.map +1 -1
  108. package/dist/text.d.ts +27 -3
  109. package/dist/text.d.ts.map +1 -1
  110. package/dist/text.js +225 -23
  111. package/dist/text.js.map +1 -1
  112. package/dist/tools.d.ts.map +1 -1
  113. package/dist/tools.js +200 -12
  114. package/dist/tools.js.map +1 -1
  115. package/dist/types.d.ts +3 -3
  116. package/dist/types.d.ts.map +1 -1
  117. package/dist/word-styles.d.ts +102 -0
  118. package/dist/word-styles.d.ts.map +1 -0
  119. package/dist/word-styles.js +419 -0
  120. package/dist/word-styles.js.map +1 -0
  121. package/dist/xlsx.d.ts +23 -0
  122. package/dist/xlsx.d.ts.map +1 -0
  123. package/dist/xlsx.js +511 -0
  124. package/dist/xlsx.js.map +1 -0
  125. package/dist/xml-stream.d.ts +55 -0
  126. package/dist/xml-stream.d.ts.map +1 -0
  127. package/dist/xml-stream.js +235 -0
  128. package/dist/xml-stream.js.map +1 -0
  129. package/docs/README.md +8 -8
  130. package/docs/getting-started.md +1 -1
  131. package/docs/guides/costs-and-limits.md +7 -5
  132. package/docs/guides/deployment.md +95 -29
  133. package/docs/guides/errors-and-reliability.md +23 -10
  134. package/docs/guides/pdfs.md +127 -74
  135. package/docs/guides/storage.md +1 -1
  136. package/docs/guides/testing.md +5 -1
  137. package/docs/guides/web-app.md +10 -9
  138. package/docs/how-it-works.md +73 -50
  139. package/docs/reference/ask.md +26 -8
  140. package/docs/reference/errors-and-retries.md +5 -3
  141. package/docs/reference/events.md +14 -6
  142. package/docs/reference/gemini.md +6 -2
  143. package/docs/reference/library.md +205 -74
  144. package/docs/reference/node.md +200 -22
  145. package/docs/reference/openai.md +1 -1
  146. package/docs/reference/tools.md +170 -22
  147. package/docs/security.md +114 -69
  148. package/docs/troubleshooting.md +40 -20
  149. package/package.json +6 -2
  150. package/src/analyze.ts +117 -13
  151. package/src/anthropic/adapter.ts +4 -1
  152. package/src/ask.ts +73 -17
  153. package/src/decode.ts +89 -0
  154. package/src/errors.ts +108 -0
  155. package/src/events.ts +10 -1
  156. package/src/evidence.ts +6 -0
  157. package/src/extract.ts +52 -709
  158. package/src/gemini/adapter.ts +9 -5
  159. package/src/gemini/endpoints.ts +12 -2
  160. package/src/gemini/generate.ts +19 -3
  161. package/src/groups.ts +162 -0
  162. package/src/html.ts +1071 -0
  163. package/src/index.ts +2 -1
  164. package/src/library.ts +43 -12
  165. package/src/limiter.ts +6 -1
  166. package/src/model.ts +8 -1
  167. package/src/node/handlers.ts +66 -16
  168. package/src/node/index.ts +2 -0
  169. package/src/node/ingest-child.ts +61 -0
  170. package/src/node/isolated.ts +464 -0
  171. package/src/office.ts +1047 -0
  172. package/src/openai/adapter.ts +14 -1
  173. package/src/pdf-pages.ts +2 -5
  174. package/src/peers.ts +29 -0
  175. package/src/plan.ts +2 -0
  176. package/src/ranged.ts +4 -2
  177. package/src/search.ts +738 -146
  178. package/src/text-index.ts +4 -2
  179. package/src/text.ts +214 -23
  180. package/src/tools.ts +192 -12
  181. package/src/types.ts +3 -3
  182. package/src/word-styles.ts +420 -0
  183. package/src/xlsx.ts +478 -0
  184. package/src/xml-stream.ts +224 -0
@@ -0,0 +1,224 @@
1
+ /**
2
+ * A small XML reader for the parts of Office files: tags with their attributes, and the text
3
+ * between them, from XML that may come in pieces as it is unpacked. It keeps one tag at a time, so
4
+ * the memory it holds does not grow with the XML. It is not a validating parser: Office files are
5
+ * machine-written, and what matters here is that a hostile one cannot make it slow or large.
6
+ */
7
+
8
+ /** A tag is read up to this many characters; a longer one is refused: no tag of an Office file comes near. */
9
+ const MAX_TAG_CHARACTERS = 64 * 1024;
10
+
11
+ /** Attributes of a tag, by their full name (`w:val`, `r:id`). */
12
+ export type XmlAttributes = Record<string, string>;
13
+
14
+ export type XmlHandlers = {
15
+ /** A start tag, or an empty one (`<w:br/>`), which gets no `close`. */
16
+ open?(name: string, attributes: XmlAttributes, empty: boolean): void;
17
+ close?(name: string): void;
18
+ /** Text between tags, with its entities decoded. It may come in more than one piece. */
19
+ text?(text: string): void;
20
+ };
21
+
22
+ /** Decodes the five XML entities and character references. */
23
+ export function decodeEntities(value: string): string {
24
+ if (!value.includes("&")) return value;
25
+ return value.replace(/&(#x[0-9a-f]+|#\d+|amp|lt|gt|quot|apos);/gi, (match, entity: string) => {
26
+ const lower = entity.toLowerCase();
27
+ if (lower === "amp") return "&";
28
+ if (lower === "lt") return "<";
29
+ if (lower === "gt") return ">";
30
+ if (lower === "quot") return "\"";
31
+ if (lower === "apos") return "'";
32
+ const code = lower.startsWith("#x") ? Number.parseInt(lower.slice(2), 16) : Number.parseInt(lower.slice(1), 10);
33
+ return Number.isFinite(code) && code > 0 && code <= 0x10ffff ? String.fromCodePoint(code) : match;
34
+ });
35
+ }
36
+
37
+ /** White space, as XML and HTML count it between attributes. */
38
+ const isSpace = (code: number) => code === 32 || code === 9 || code === 10 || code === 13 || code === 12;
39
+
40
+ /**
41
+ * The attributes in the inside of a tag, after its name: `name="value"` or `name='value'`, in one
42
+ * pass. A regular expression backtracked from every position of a tag with no "=": a 64 K tag
43
+ * took 6.5 s. A name without a value, or with an unquoted one (HTML's `href=x`), is read too.
44
+ */
45
+ export function parseAttributes(inside: string): XmlAttributes {
46
+ const attributes: XmlAttributes = Object.create(null) as XmlAttributes;
47
+ const length = inside.length;
48
+ let at = 0;
49
+ while (at < length) {
50
+ while (at < length && (isSpace(inside.charCodeAt(at)) || inside[at] === "/")) at++;
51
+ const start = at;
52
+ while (at < length && !isSpace(inside.charCodeAt(at)) && inside[at] !== "=" && inside[at] !== "/" && inside[at] !== ">") at++;
53
+ const name = inside.slice(start, at);
54
+ while (at < length && isSpace(inside.charCodeAt(at))) at++;
55
+ if (inside[at] !== "=") {
56
+ if (name) attributes[name] = "";
57
+ if (at === start) at++;
58
+ continue;
59
+ }
60
+ at++;
61
+ while (at < length && isSpace(inside.charCodeAt(at))) at++;
62
+ const quote = inside[at];
63
+ let value: string;
64
+ if (quote === "\"" || quote === "'") {
65
+ const end = inside.indexOf(quote, at + 1);
66
+ value = inside.slice(at + 1, end === -1 ? length : end);
67
+ at = end === -1 ? length : end + 1;
68
+ } else {
69
+ const begin = at;
70
+ while (at < length && !isSpace(inside.charCodeAt(at)) && inside[at] !== ">") at++;
71
+ value = inside.slice(begin, at);
72
+ }
73
+ if (name) attributes[name] = decodeEntities(value);
74
+ }
75
+ return attributes;
76
+ }
77
+
78
+ /**
79
+ * Reads XML given in pieces with `write`, and calls the handlers as tags and text go past.
80
+ * Comments, processing instructions and declarations are skipped; the text of a CDATA section is
81
+ * given as text. It reads in one loop, with no recursion, whatever the number of tags in a piece.
82
+ */
83
+ export class XmlStream {
84
+ private state: "text" | "tag" | "comment" | "cdata" = "text";
85
+ /**
86
+ * What is kept from the last piece, to be read again in front of the next one: the inside of a
87
+ * tag read so far, the start of a "<!" that cannot be told yet, or the last two characters of a
88
+ * comment or CDATA section, where its end mark may start.
89
+ */
90
+ private kept = "";
91
+ /** In a tag: how much of `kept` has been scanned, and the quote of the attribute value it is in. */
92
+ private scanned = 0;
93
+ private quote = 0;
94
+
95
+ constructor(private readonly handlers: XmlHandlers) {}
96
+
97
+ write(piece: string): void {
98
+ const xml = this.kept + piece;
99
+ const length = xml.length;
100
+ // In a tag, the inside starts at 0 and has been scanned up to `scanned`.
101
+ let tagStart = 0;
102
+ let at = this.state === "tag" ? this.scanned : 0;
103
+ this.kept = "";
104
+ this.scanned = 0;
105
+ while (at < length) {
106
+ if (this.state === "text") {
107
+ const start = xml.indexOf("<", at);
108
+ if (start === -1) {
109
+ // An entity cut at the end of the piece ("&am" + "p;") is read with the next piece.
110
+ const amp = xml.lastIndexOf("&");
111
+ const cut = amp >= at && length - amp <= MAX_ENTITY && !xml.includes(";", amp) ? amp : length;
112
+ if (cut > at) this.handlers.text?.(decodeEntities(xml.slice(at, cut)));
113
+ this.kept = xml.slice(cut);
114
+ return;
115
+ }
116
+ if (start > at) this.handlers.text?.(decodeEntities(xml.slice(at, start)));
117
+ const rest = xml.slice(start + 1, start + 9);
118
+ if (rest.startsWith("!--")) {
119
+ this.state = "comment";
120
+ at = start + 4;
121
+ } else if (rest.startsWith("![CDATA[")) {
122
+ this.state = "cdata";
123
+ at = start + 9;
124
+ } else if (rest.length < 8 && (CDATA_START.startsWith(rest) || COMMENT_START.startsWith(rest))) {
125
+ // Too little is known to tell a comment or CDATA from a tag: wait for the next piece.
126
+ this.kept = xml.slice(start);
127
+ return;
128
+ } else {
129
+ this.state = "tag";
130
+ this.quote = 0;
131
+ tagStart = start + 1;
132
+ at = tagStart;
133
+ }
134
+ continue;
135
+ }
136
+ if (this.state === "comment" || this.state === "cdata") {
137
+ const mark = this.state === "comment" ? "-->" : "]]>";
138
+ const end = xml.indexOf(mark, at);
139
+ const stop = end === -1 ? Math.max(at, length - 2) : end;
140
+ if (this.state === "cdata" && stop > at) this.handlers.text?.(xml.slice(at, stop));
141
+ if (end === -1) {
142
+ this.kept = xml.slice(stop);
143
+ return;
144
+ }
145
+ this.state = "text";
146
+ at = end + 3;
147
+ continue;
148
+ }
149
+ // In a tag: on to its ">", outside quotes.
150
+ let end = at;
151
+ for (; end < length; end++) {
152
+ const code = xml.charCodeAt(end);
153
+ if (this.quote !== 0) {
154
+ if (code === this.quote) this.quote = 0;
155
+ } else if (code === 34 || code === 39) {
156
+ this.quote = code;
157
+ } else if (code === 62) {
158
+ break;
159
+ }
160
+ }
161
+ if (end - tagStart > MAX_TAG_CHARACTERS) {
162
+ throw new Error("The XML has a tag far longer than any Office file writes. It may be built to exhaust the server, so it is not read.");
163
+ }
164
+ if (end === length) {
165
+ this.kept = xml.slice(tagStart);
166
+ this.scanned = this.kept.length;
167
+ return;
168
+ }
169
+ this.state = "text";
170
+ at = end + 1;
171
+ this.tagRead(xml.slice(tagStart, end));
172
+ }
173
+ }
174
+
175
+ private tagRead(inside: string): void {
176
+ const first = inside.charCodeAt(0);
177
+ // <?xml ... ?> and <!DOCTYPE ...>: nothing to read.
178
+ if (first === 63 || first === 33) return;
179
+ if (first === 47) {
180
+ this.handlers.close?.(inside.slice(1).trim());
181
+ return;
182
+ }
183
+ const empty = inside.charCodeAt(inside.length - 1) === 47;
184
+ const body = empty ? inside.slice(0, -1) : inside;
185
+ const space = body.search(/\s/);
186
+ const name = space === -1 ? body : body.slice(0, space);
187
+ this.handlers.open?.(name, space === -1 ? (Object.create(null) as XmlAttributes) : parseAttributes(body.slice(space)), empty);
188
+ }
189
+ }
190
+
191
+ /** The longest entity read: "&#x10FFFF;". */
192
+ const MAX_ENTITY = 10;
193
+ const COMMENT_START = "!--";
194
+ const CDATA_START = "![CDATA[";
195
+
196
+ /** Reads a whole piece of XML with `XmlStream`. */
197
+ export function readXml(xml: string, handlers: XmlHandlers): void {
198
+ new XmlStream(handlers).write(xml);
199
+ }
200
+
201
+ /** The relationships of an Office part (`_rels/….rels`): target by ID, made absolute from `base`, the folder of the part. */
202
+ export function readRelationships(xml: string | undefined, base: string): Map<string, { target: string; type: string; external: boolean }> {
203
+ const relationships = new Map<string, { target: string; type: string; external: boolean }>();
204
+ if (!xml) return relationships;
205
+ readXml(xml, {
206
+ open(name, attributes) {
207
+ if (name !== "Relationship" || !attributes.Id || !attributes.Target) return;
208
+ const external = attributes.TargetMode === "External";
209
+ relationships.set(attributes.Id, { target: external ? attributes.Target : resolvePart(base, attributes.Target), type: attributes.Type ?? "", external });
210
+ },
211
+ });
212
+ return relationships;
213
+ }
214
+
215
+ /** A target of a relationship as a part name: `../notesSlides/notesSlide1.xml` from `ppt/slides` is `ppt/notesSlides/notesSlide1.xml`. */
216
+ export function resolvePart(base: string, target: string): string {
217
+ if (target.startsWith("/")) return target.slice(1);
218
+ const parts = base ? base.split("/") : [];
219
+ for (const piece of target.split("/")) {
220
+ if (piece === "..") parts.pop();
221
+ else if (piece !== "." && piece !== "") parts.push(piece);
222
+ }
223
+ return parts.join("/");
224
+ }