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.
- package/CHANGELOG.md +319 -1
- package/README.md +10 -8
- package/dist/analyze.d.ts +3 -1
- package/dist/analyze.d.ts.map +1 -1
- package/dist/analyze.js +109 -14
- package/dist/analyze.js.map +1 -1
- package/dist/anthropic/adapter.d.ts.map +1 -1
- package/dist/anthropic/adapter.js +4 -1
- package/dist/anthropic/adapter.js.map +1 -1
- package/dist/ask.d.ts +3 -3
- package/dist/ask.d.ts.map +1 -1
- package/dist/ask.js +72 -17
- package/dist/ask.js.map +1 -1
- package/dist/decode.d.ts +19 -0
- package/dist/decode.d.ts.map +1 -0
- package/dist/decode.js +97 -0
- package/dist/decode.js.map +1 -0
- package/dist/errors.d.ts +55 -0
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +65 -0
- package/dist/errors.js.map +1 -1
- package/dist/events.d.ts +6 -0
- package/dist/events.d.ts.map +1 -1
- package/dist/events.js +5 -1
- package/dist/events.js.map +1 -1
- package/dist/evidence.d.ts.map +1 -1
- package/dist/evidence.js +6 -0
- package/dist/evidence.js.map +1 -1
- package/dist/extract.d.ts +7 -20
- package/dist/extract.d.ts.map +1 -1
- package/dist/extract.js +49 -751
- package/dist/extract.js.map +1 -1
- package/dist/gemini/adapter.d.ts.map +1 -1
- package/dist/gemini/adapter.js +9 -5
- package/dist/gemini/adapter.js.map +1 -1
- package/dist/gemini/endpoints.d.ts +5 -1
- package/dist/gemini/endpoints.d.ts.map +1 -1
- package/dist/gemini/endpoints.js +11 -2
- package/dist/gemini/endpoints.js.map +1 -1
- package/dist/gemini/generate.d.ts.map +1 -1
- package/dist/gemini/generate.js +20 -3
- package/dist/gemini/generate.js.map +1 -1
- package/dist/groups.d.ts +54 -0
- package/dist/groups.d.ts.map +1 -0
- package/dist/groups.js +136 -0
- package/dist/groups.js.map +1 -0
- package/dist/html.d.ts +11 -0
- package/dist/html.d.ts.map +1 -0
- package/dist/html.js +1112 -0
- package/dist/html.js.map +1 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/library.d.ts.map +1 -1
- package/dist/library.js +48 -12
- package/dist/library.js.map +1 -1
- package/dist/limiter.d.ts +2 -1
- package/dist/limiter.d.ts.map +1 -1
- package/dist/limiter.js +3 -2
- package/dist/limiter.js.map +1 -1
- package/dist/model.d.ts.map +1 -1
- package/dist/model.js +8 -1
- package/dist/model.js.map +1 -1
- package/dist/node/handlers.d.ts.map +1 -1
- package/dist/node/handlers.js +69 -15
- package/dist/node/handlers.js.map +1 -1
- package/dist/node/index.d.ts +2 -0
- package/dist/node/index.d.ts.map +1 -1
- package/dist/node/index.js +1 -0
- package/dist/node/index.js.map +1 -1
- package/dist/node/ingest-child.d.ts +38 -0
- package/dist/node/ingest-child.d.ts.map +1 -0
- package/dist/node/ingest-child.js +42 -0
- package/dist/node/ingest-child.js.map +1 -0
- package/dist/node/isolated.d.ts +88 -0
- package/dist/node/isolated.d.ts.map +1 -0
- package/dist/node/isolated.js +381 -0
- package/dist/node/isolated.js.map +1 -0
- package/dist/office.d.ts +105 -0
- package/dist/office.d.ts.map +1 -0
- package/dist/office.js +1092 -0
- package/dist/office.js.map +1 -0
- package/dist/openai/adapter.d.ts.map +1 -1
- package/dist/openai/adapter.js +15 -1
- package/dist/openai/adapter.js.map +1 -1
- package/dist/pdf-pages.d.ts.map +1 -1
- package/dist/pdf-pages.js +2 -6
- package/dist/pdf-pages.js.map +1 -1
- package/dist/peers.d.ts +8 -0
- package/dist/peers.d.ts.map +1 -0
- package/dist/peers.js +32 -0
- package/dist/peers.js.map +1 -0
- package/dist/plan.d.ts.map +1 -1
- package/dist/plan.js +1 -1
- package/dist/plan.js.map +1 -1
- package/dist/ranged.d.ts.map +1 -1
- package/dist/ranged.js +4 -2
- package/dist/ranged.js.map +1 -1
- package/dist/search.d.ts +99 -22
- package/dist/search.d.ts.map +1 -1
- package/dist/search.js +744 -140
- package/dist/search.js.map +1 -1
- package/dist/text-index.d.ts +3 -1
- package/dist/text-index.d.ts.map +1 -1
- package/dist/text-index.js +3 -1
- package/dist/text-index.js.map +1 -1
- package/dist/text.d.ts +27 -3
- package/dist/text.d.ts.map +1 -1
- package/dist/text.js +225 -23
- package/dist/text.js.map +1 -1
- package/dist/tools.d.ts.map +1 -1
- package/dist/tools.js +200 -12
- package/dist/tools.js.map +1 -1
- package/dist/types.d.ts +3 -3
- package/dist/types.d.ts.map +1 -1
- package/dist/word-styles.d.ts +102 -0
- package/dist/word-styles.d.ts.map +1 -0
- package/dist/word-styles.js +419 -0
- package/dist/word-styles.js.map +1 -0
- package/dist/xlsx.d.ts +23 -0
- package/dist/xlsx.d.ts.map +1 -0
- package/dist/xlsx.js +511 -0
- package/dist/xlsx.js.map +1 -0
- package/dist/xml-stream.d.ts +55 -0
- package/dist/xml-stream.d.ts.map +1 -0
- package/dist/xml-stream.js +235 -0
- package/dist/xml-stream.js.map +1 -0
- package/docs/README.md +8 -8
- package/docs/getting-started.md +1 -1
- package/docs/guides/costs-and-limits.md +7 -5
- package/docs/guides/deployment.md +95 -29
- package/docs/guides/errors-and-reliability.md +23 -10
- package/docs/guides/pdfs.md +127 -74
- package/docs/guides/storage.md +1 -1
- package/docs/guides/testing.md +5 -1
- package/docs/guides/web-app.md +10 -9
- package/docs/how-it-works.md +73 -50
- package/docs/reference/ask.md +26 -8
- package/docs/reference/errors-and-retries.md +5 -3
- package/docs/reference/events.md +14 -6
- package/docs/reference/gemini.md +6 -2
- package/docs/reference/library.md +205 -74
- package/docs/reference/node.md +200 -22
- package/docs/reference/openai.md +1 -1
- package/docs/reference/tools.md +170 -22
- package/docs/security.md +114 -69
- package/docs/troubleshooting.md +40 -20
- package/package.json +6 -2
- package/src/analyze.ts +117 -13
- package/src/anthropic/adapter.ts +4 -1
- package/src/ask.ts +73 -17
- package/src/decode.ts +89 -0
- package/src/errors.ts +108 -0
- package/src/events.ts +10 -1
- package/src/evidence.ts +6 -0
- package/src/extract.ts +52 -709
- package/src/gemini/adapter.ts +9 -5
- package/src/gemini/endpoints.ts +12 -2
- package/src/gemini/generate.ts +19 -3
- package/src/groups.ts +162 -0
- package/src/html.ts +1071 -0
- package/src/index.ts +2 -1
- package/src/library.ts +43 -12
- package/src/limiter.ts +6 -1
- package/src/model.ts +8 -1
- package/src/node/handlers.ts +66 -16
- package/src/node/index.ts +2 -0
- package/src/node/ingest-child.ts +61 -0
- package/src/node/isolated.ts +464 -0
- package/src/office.ts +1047 -0
- package/src/openai/adapter.ts +14 -1
- package/src/pdf-pages.ts +2 -5
- package/src/peers.ts +29 -0
- package/src/plan.ts +2 -0
- package/src/ranged.ts +4 -2
- package/src/search.ts +738 -146
- package/src/text-index.ts +4 -2
- package/src/text.ts +214 -23
- package/src/tools.ts +192 -12
- package/src/types.ts +3 -3
- package/src/word-styles.ts +420 -0
- package/src/xlsx.ts +478 -0
- package/src/xml-stream.ts +224 -0
package/src/openai/adapter.ts
CHANGED
|
@@ -209,7 +209,13 @@ function detailText(detail: unknown): string | undefined {
|
|
|
209
209
|
return detail.map((problem) => (typeof (problem as Json | null)?.msg === "string" ? (problem as Json).msg as string : "")).filter(Boolean).join("; ") || undefined;
|
|
210
210
|
}
|
|
211
211
|
|
|
212
|
-
/**
|
|
212
|
+
/** The error a reply that came with 200 holds instead of choices, when it holds one. */
|
|
213
|
+
function bodyError(data: unknown): unknown {
|
|
214
|
+
const body = (Array.isArray(data) ? data[0] : data) as Json | null | undefined;
|
|
215
|
+
return body && typeof body === "object" && body.error ? body.error : undefined;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/** An error event inside a stream, or a body, that began with 200, with the server's own word for it. */
|
|
213
219
|
function streamFailure(failed: unknown): OpenAiRequestError {
|
|
214
220
|
const named = failed && typeof failed === "object" ? failed as Json : {};
|
|
215
221
|
return new OpenAiRequestError(typeof failed === "string" ? failed : String(named.message ?? "The model reported an error."), streamErrorStatus(failed), {
|
|
@@ -404,6 +410,10 @@ export function openAiCompatible(options: OpenAiCompatibleOptions): ModelAdapter
|
|
|
404
410
|
response = await post(body(other), signal ? AbortSignal.any([signal, timeout]) : timeout, stopped);
|
|
405
411
|
}
|
|
406
412
|
const data = await readJson<Json>(response, stopped, unreachable, undefined, (error) => new OpenAiRequestError(notJsonMessage("The model's", response), response.status, { kind: "invalid", cause: error }));
|
|
413
|
+
// An error in a body that came with 200 (OpenRouter, once generation has started): thrown as
|
|
414
|
+
// a stream's error is, with its status, so that a 502 is sent again rather than read as no text.
|
|
415
|
+
const failed = bodyError(data);
|
|
416
|
+
if (failed) throw streamFailure(failed);
|
|
407
417
|
const used = usageOf(data.usage);
|
|
408
418
|
onUsage?.(used);
|
|
409
419
|
const choice = data.choices?.[0];
|
|
@@ -548,6 +558,9 @@ export function openAiCompatible(options: OpenAiCompatibleOptions): ModelAdapter
|
|
|
548
558
|
} else {
|
|
549
559
|
// In one piece: no streaming was asked for, or the server answered without it.
|
|
550
560
|
const data = await readJson<Json>(response, () => Boolean(hooks.signal?.aborted), lost, watch, notJson);
|
|
561
|
+
// An error in a body that came with 200, as a stream may send one: not an empty reply.
|
|
562
|
+
const failed = bodyError(data);
|
|
563
|
+
if (failed) throw streamFailure(failed);
|
|
551
564
|
const choice = data.choices?.[0] as Json | undefined;
|
|
552
565
|
message = (choice?.message ?? { role: "assistant", content: null }) as Json;
|
|
553
566
|
finish = choice?.finish_reason;
|
package/src/pdf-pages.ts
CHANGED
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
* - a wanted page gets its own resource dictionary, without the images it does not paint.
|
|
10
10
|
*/
|
|
11
11
|
import { getHeapStatistics } from "node:v8";
|
|
12
|
+
import { importPeer } from "./peers.js";
|
|
12
13
|
import type { PDFContext, PDFDict, PDFObject, PDFObjectCopier, PDFPage, PDFPageLeaf, PDFRef } from "@cantoo/pdf-lib";
|
|
13
14
|
import { TimeSlicer } from "./pause.js";
|
|
14
15
|
import { formatBytes } from "./plan.js";
|
|
@@ -208,11 +209,7 @@ export function latin1(bytes: Uint8Array): string {
|
|
|
208
209
|
}
|
|
209
210
|
|
|
210
211
|
export async function loadPdfLibrary(): Promise<PdfLibrary> {
|
|
211
|
-
|
|
212
|
-
return await import("@cantoo/pdf-lib");
|
|
213
|
-
} catch {
|
|
214
|
-
throw new Error("Showing PDF pages needs the optional dependency \"@cantoo/pdf-lib\".");
|
|
215
|
-
}
|
|
212
|
+
return importPeer("@cantoo/pdf-lib", () => import("@cantoo/pdf-lib"), "Showing PDF pages needs the optional dependency \"@cantoo/pdf-lib\".");
|
|
216
213
|
}
|
|
217
214
|
|
|
218
215
|
/**
|
package/src/peers.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Loads an optional dependency. When it is not installed, the error says so in `missing`, words for
|
|
3
|
+
* a user ("Reading PDF files needs the optional dependency \"unpdf\"."). When it is installed but
|
|
4
|
+
* fails to load (a version this Node cannot run, a broken install), the error says that, with the
|
|
5
|
+
* reason, and keeps the failure as its cause: it was reported as missing before 0.8.0.
|
|
6
|
+
*/
|
|
7
|
+
export async function importPeer<T>(name: string, load: () => Promise<T>, missing: string): Promise<T> {
|
|
8
|
+
try {
|
|
9
|
+
return await load();
|
|
10
|
+
} catch (error) {
|
|
11
|
+
if (notFound(error, name)) throw new Error(missing, { cause: error });
|
|
12
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
13
|
+
throw new Error(`The optional dependency "${name}" is installed but could not be loaded (${reason.split("\n")[0]}). Reinstall it, or install a version this Node can run.`, { cause: error });
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Whether an import failed because the package is not installed: Node's code, or the words of Node
|
|
19
|
+
* and of bundlers that give none, about this package and not one it needs. A loader that wraps the
|
|
20
|
+
* failure (a test runner's mock) keeps it as a cause, looked at too.
|
|
21
|
+
*/
|
|
22
|
+
function notFound(error: unknown, name: string): boolean {
|
|
23
|
+
for (let current = error, depth = 0; current && depth < 4; current = (current as { cause?: unknown }).cause, depth++) {
|
|
24
|
+
const { code, message } = current as { code?: unknown; message?: unknown };
|
|
25
|
+
if (typeof message === "string" && message.includes(name)
|
|
26
|
+
&& (code === "ERR_MODULE_NOT_FOUND" || code === "MODULE_NOT_FOUND" || /Cannot find (?:package|module)/.test(message))) return true;
|
|
27
|
+
}
|
|
28
|
+
return false;
|
|
29
|
+
}
|
package/src/plan.ts
CHANGED
|
@@ -197,6 +197,8 @@ export function documentToolsInstruction(searchable: DocumentMeta[], viewable: D
|
|
|
197
197
|
"You see only a summary card for each. Read them through the tools:",
|
|
198
198
|
"- search_document finds lines (exact or regex) or the most relevant passages (keywords).",
|
|
199
199
|
"- To count or list what can be matched as text (a word, an ID, an error code, a log level, a pattern), use search_document in exact or regex mode: its total_matching_lines is exact.",
|
|
200
|
+
"- To count per group (per hour or day, per error code or ID with a regex group, per message shape), pass group_by: one call gives every count. For more matches than a result shows, search again from its next_from_line, or with order \"last\" for the last ones.",
|
|
201
|
+
"- Search for the bare form of a word: a search matches inside longer words, so \"timeout\" also finds \"timeouts\", and a Hebrew word without its prefix letters (חוזה, not בחוזה) finds every form of it.",
|
|
200
202
|
"- read_document reads a line range. Read around search hits before you rely on them.",
|
|
201
203
|
"- get_document_overview returns the outline, log profile, and the start and end of a file.",
|
|
202
204
|
...(wholeReads ? ["- analyze_whole_document reads the entire file in parts. Use it only when the task needs all of the file and cannot be done by matching text: a full summary, a timeline, or items that must be judged by meaning. It is slow and costly, and it can miss items of a long list."] : []),
|
package/src/ranged.ts
CHANGED
|
@@ -44,7 +44,9 @@ export const pieceKey = (id: string, at: number) => `${id}#${at}`;
|
|
|
44
44
|
const BUILD_READ_BYTES = 4 * 1024 * 1024;
|
|
45
45
|
/** A pass shared by several scans lets other work run after this much time on the main thread. */
|
|
46
46
|
const YIELD_AFTER_MS = 15;
|
|
47
|
-
|
|
47
|
+
// `ignoreBOM`: every decode is a stream of its own, and would drop a U+FEFF that starts it. A range
|
|
48
|
+
// starts at a line, so a line that began with one lost it, and its piece no longer matched the index.
|
|
49
|
+
const decoder = new TextDecoder("utf-8", { ignoreBOM: true });
|
|
48
50
|
|
|
49
51
|
const gone = (id: string) => new Error(`${id} is no longer available on the server. Ask the user to attach the file again.`);
|
|
50
52
|
|
|
@@ -347,7 +349,7 @@ export async function openRangedDocument(
|
|
|
347
349
|
if (!index) return undefined;
|
|
348
350
|
// An index that disagrees with the metadata describes a text that is not what was ingested.
|
|
349
351
|
// It is used, since it is what the store holds, but not saved.
|
|
350
|
-
if (store.putIndex && canSerialize(index) && index.lineCount === meta.lines && index.characters === meta.characters) {
|
|
352
|
+
if (store.putIndex && canSerialize(index) && index.lineCount === meta.lines && index.characters === meta.characters && (meta.textBytes === undefined || index.textBytes === meta.textBytes)) {
|
|
351
353
|
await store.putIndex(meta.id, serializeTextIndex(index)).catch(() => undefined);
|
|
352
354
|
}
|
|
353
355
|
}
|