llm-large-files 0.8.2 → 0.9.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 +535 -0
- package/README.md +9 -10
- package/dist/access.d.ts +86 -0
- package/dist/access.d.ts.map +1 -0
- package/dist/access.js +131 -0
- package/dist/access.js.map +1 -0
- package/dist/ask.d.ts +45 -1
- package/dist/ask.d.ts.map +1 -1
- package/dist/ask.js +181 -20
- package/dist/ask.js.map +1 -1
- package/dist/cache.d.ts +12 -3
- package/dist/cache.d.ts.map +1 -1
- package/dist/cache.js +18 -5
- package/dist/cache.js.map +1 -1
- package/dist/encoding.js +5 -3
- package/dist/encoding.js.map +1 -1
- package/dist/error-info.d.ts +57 -0
- package/dist/error-info.d.ts.map +1 -0
- package/dist/error-info.js +217 -0
- package/dist/error-info.js.map +1 -0
- package/dist/errors.d.ts +72 -8
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +102 -15
- package/dist/errors.js.map +1 -1
- package/dist/events.d.ts +35 -9
- package/dist/events.d.ts.map +1 -1
- package/dist/events.js +42 -11
- package/dist/events.js.map +1 -1
- package/dist/evidence.d.ts +4 -2
- package/dist/evidence.d.ts.map +1 -1
- package/dist/evidence.js +14 -3
- package/dist/evidence.js.map +1 -1
- package/dist/extract.d.ts +32 -8
- package/dist/extract.d.ts.map +1 -1
- package/dist/extract.js +46 -35
- package/dist/extract.js.map +1 -1
- package/dist/gemini/adapter.d.ts +19 -3
- package/dist/gemini/adapter.d.ts.map +1 -1
- package/dist/gemini/adapter.js +84 -12
- package/dist/gemini/adapter.js.map +1 -1
- package/dist/gemini/chat.d.ts +18 -6
- package/dist/gemini/chat.d.ts.map +1 -1
- package/dist/gemini/chat.js +2 -0
- package/dist/gemini/chat.js.map +1 -1
- package/dist/gemini/index.d.ts +1 -1
- package/dist/gemini/index.d.ts.map +1 -1
- package/dist/index.d.ts +9 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -3
- package/dist/index.js.map +1 -1
- package/dist/ingest.d.ts +70 -0
- package/dist/ingest.d.ts.map +1 -0
- package/dist/ingest.js +868 -0
- package/dist/ingest.js.map +1 -0
- package/dist/library.d.ts +169 -34
- package/dist/library.d.ts.map +1 -1
- package/dist/library.js +324 -591
- package/dist/library.js.map +1 -1
- package/dist/map-reduce.js +3 -3
- package/dist/map-reduce.js.map +1 -1
- package/dist/model.d.ts +43 -2
- package/dist/model.d.ts.map +1 -1
- package/dist/model.js +63 -3
- package/dist/model.js.map +1 -1
- package/dist/node/handlers.d.ts +226 -14
- package/dist/node/handlers.d.ts.map +1 -1
- package/dist/node/handlers.js +423 -181
- package/dist/node/handlers.js.map +1 -1
- package/dist/node/index.d.ts +3 -2
- package/dist/node/index.d.ts.map +1 -1
- package/dist/node/index.js +2 -1
- package/dist/node/index.js.map +1 -1
- package/dist/node/ingest-child.d.ts +7 -1
- package/dist/node/ingest-child.d.ts.map +1 -1
- package/dist/node/ingest-child.js +18 -3
- package/dist/node/ingest-child.js.map +1 -1
- package/dist/node/isolated.d.ts +3 -1
- package/dist/node/isolated.d.ts.map +1 -1
- package/dist/node/isolated.js +11 -4
- package/dist/node/isolated.js.map +1 -1
- package/dist/node/originals.d.ts +19 -0
- package/dist/node/originals.d.ts.map +1 -0
- package/dist/node/originals.js +82 -0
- package/dist/node/originals.js.map +1 -0
- package/dist/node/spool.d.ts +102 -0
- package/dist/node/spool.d.ts.map +1 -0
- package/dist/node/spool.js +365 -0
- package/dist/node/spool.js.map +1 -0
- package/dist/node/sse.d.ts +4 -3
- package/dist/node/sse.d.ts.map +1 -1
- package/dist/node/sse.js +7 -6
- package/dist/node/sse.js.map +1 -1
- package/dist/pdf-layout.d.ts.map +1 -1
- package/dist/pdf-layout.js +71 -12
- package/dist/pdf-layout.js.map +1 -1
- package/dist/ranged.d.ts +15 -11
- package/dist/ranged.d.ts.map +1 -1
- package/dist/ranged.js +42 -11
- package/dist/ranged.js.map +1 -1
- package/dist/retry.d.ts +3 -1
- package/dist/retry.d.ts.map +1 -1
- package/dist/retry.js +6 -2
- package/dist/retry.js.map +1 -1
- package/dist/scope.d.ts +25 -0
- package/dist/scope.d.ts.map +1 -0
- package/dist/scope.js +60 -0
- package/dist/scope.js.map +1 -0
- package/dist/source.d.ts +60 -0
- package/dist/source.d.ts.map +1 -0
- package/dist/source.js +170 -0
- package/dist/source.js.map +1 -0
- package/dist/stores.d.ts +120 -31
- package/dist/stores.d.ts.map +1 -1
- package/dist/stores.js +582 -139
- package/dist/stores.js.map +1 -1
- package/dist/testing/fake-fetch.d.ts +31 -0
- package/dist/testing/fake-fetch.d.ts.map +1 -0
- package/dist/testing/fake-fetch.js +66 -0
- package/dist/testing/fake-fetch.js.map +1 -0
- package/dist/testing/fake-model.d.ts +38 -0
- package/dist/testing/fake-model.d.ts.map +1 -0
- package/dist/testing/fake-model.js +62 -0
- package/dist/testing/fake-model.js.map +1 -0
- package/dist/testing/index.d.ts +7 -0
- package/dist/testing/index.d.ts.map +1 -0
- package/dist/testing/index.js +9 -0
- package/dist/testing/index.js.map +1 -0
- package/dist/testing/store-checks.d.ts +72 -0
- package/dist/testing/store-checks.d.ts.map +1 -0
- package/dist/testing/store-checks.js +619 -0
- package/dist/testing/store-checks.js.map +1 -0
- package/dist/tools.d.ts +25 -0
- package/dist/tools.d.ts.map +1 -1
- package/dist/tools.js +135 -13
- package/dist/tools.js.map +1 -1
- package/dist/types.d.ts +116 -13
- package/dist/types.d.ts.map +1 -1
- package/docs/README.md +17 -8
- package/docs/getting-started.md +18 -13
- package/docs/guides/anthropic.md +12 -8
- package/docs/guides/conversations.md +112 -47
- package/docs/guides/costs-and-limits.md +16 -7
- package/docs/guides/deployment.md +284 -83
- package/docs/guides/errors-and-reliability.md +161 -23
- package/docs/guides/own-adapter-and-loop.md +27 -10
- package/docs/guides/pdfs.md +123 -29
- package/docs/guides/storage.md +633 -145
- package/docs/guides/testing.md +78 -129
- package/docs/guides/vertex-and-gemini.md +10 -7
- package/docs/guides/web-app.md +312 -78
- package/docs/how-it-works.md +35 -8
- package/docs/measurements.md +43 -8
- package/docs/publishing.md +112 -36
- package/docs/reference/ask.md +154 -13
- package/docs/reference/errors-and-retries.md +166 -14
- package/docs/reference/events.md +53 -20
- package/docs/reference/gemini.md +70 -21
- package/docs/reference/library.md +706 -165
- package/docs/reference/node.md +624 -110
- package/docs/reference/testing.md +311 -0
- package/docs/reference/tools.md +15 -5
- package/docs/security.md +304 -67
- package/docs/troubleshooting.md +144 -65
- package/docs/upgrading-0.9.md +890 -0
- package/package.json +12 -3
- package/src/access.ts +144 -0
- package/src/ask.ts +230 -19
- package/src/cache.ts +20 -5
- package/src/encoding.ts +6 -4
- package/src/error-info.ts +255 -0
- package/src/errors.ts +129 -15
- package/src/events.ts +86 -21
- package/src/evidence.ts +14 -3
- package/src/extract.ts +50 -33
- package/src/gemini/adapter.ts +97 -11
- package/src/gemini/chat.ts +20 -6
- package/src/gemini/index.ts +1 -1
- package/src/index.ts +9 -6
- package/src/ingest.ts +927 -0
- package/src/library.ts +467 -601
- package/src/map-reduce.ts +3 -3
- package/src/model.ts +69 -5
- package/src/node/handlers.ts +630 -183
- package/src/node/index.ts +13 -2
- package/src/node/ingest-child.ts +23 -5
- package/src/node/isolated.ts +11 -4
- package/src/node/originals.ts +78 -0
- package/src/node/spool.ts +365 -0
- package/src/node/sse.ts +7 -6
- package/src/pdf-layout.ts +69 -11
- package/src/ranged.ts +34 -13
- package/src/retry.ts +6 -2
- package/src/scope.ts +63 -0
- package/src/source.ts +178 -0
- package/src/stores.ts +635 -140
- package/src/testing/fake-fetch.ts +80 -0
- package/src/testing/fake-model.ts +78 -0
- package/src/testing/index.ts +11 -0
- package/src/testing/store-checks.ts +660 -0
- package/src/tools.ts +150 -14
- package/src/types.ts +106 -13
package/src/map-reduce.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { safeErrorMessage } from "./errors.js";
|
|
2
2
|
import { until } from "./http.js";
|
|
3
|
-
import { classifyModelError, errorKindOf, type ModelErrorKind } from "./model.js";
|
|
3
|
+
import { classifyModelError, errorKindOf, isModelStatus, modelErrorOf, type ModelErrorKind } from "./model.js";
|
|
4
4
|
import { noTextReason } from "./output-limit.js";
|
|
5
5
|
import { statusOf, withRetry, type RetryOptions } from "./retry.js";
|
|
6
6
|
import { estimateTokens, jsonBytes } from "./text.js";
|
|
@@ -117,7 +117,7 @@ export function modelFailureReason(error: unknown): string {
|
|
|
117
117
|
if (noText !== undefined) return `The model returned no text${PLAIN_REASON.test(noText) ? ` (${noText})` : ""}.`;
|
|
118
118
|
const status = httpStatusOf(error);
|
|
119
119
|
let kind: unknown = errorKindOf(error);
|
|
120
|
-
if (!knownKind(kind) &&
|
|
120
|
+
if (!knownKind(kind) && isModelStatus(status)) {
|
|
121
121
|
const code = (error as { code?: unknown } | null)?.code;
|
|
122
122
|
kind = classifyModelError(status, error instanceof Error ? error.message : "", { code: typeof code === "string" ? code : undefined }).kind;
|
|
123
123
|
}
|
|
@@ -384,7 +384,7 @@ export async function analyzeWholeDocument(options: {
|
|
|
384
384
|
return [{ from, to, text, read: true }];
|
|
385
385
|
} catch (error) {
|
|
386
386
|
if (signal?.aborted) throw error;
|
|
387
|
-
if (!halved && to > from &&
|
|
387
|
+
if (!halved && to > from && modelErrorOf(error)?.kind === "context_overflow" && halvings > 0) {
|
|
388
388
|
halvings--;
|
|
389
389
|
const middle = Math.floor((from + to) / 2);
|
|
390
390
|
return [
|
package/src/model.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// The contract between the tool loop (`askWithFiles`) and a model provider. An adapter turns a
|
|
2
2
|
// neutral request into the provider's own, sends it, and reads the reply back. Everything else
|
|
3
3
|
// (planning, tools, limits, retries, usage, the forced answer) is the loop's, once, for every provider.
|
|
4
|
+
import { brand, branded } from "./errors.js";
|
|
4
5
|
import type { GenerateText, ToolDeclaration, ToolFile, ToolResult } from "./types.js";
|
|
5
6
|
|
|
6
7
|
/**
|
|
@@ -15,7 +16,8 @@ export type ToolAnswer = { call: ToolCall; result: ToolResult; files?: ToolFile[
|
|
|
15
16
|
|
|
16
17
|
/**
|
|
17
18
|
* One piece of a message: plain text, or something in the provider's own form (an image, an inline
|
|
18
|
-
* PDF) that only its adapter reads. A native part may carry the text it holds
|
|
19
|
+
* PDF) that only its adapter reads. A native part may carry the text it holds: its tokens are
|
|
20
|
+
* counted from that text, and its bytes from the larger of the text and the part's JSON.
|
|
19
21
|
*/
|
|
20
22
|
export type ModelPart = { text: string; native?: undefined } | { native: unknown; text?: string };
|
|
21
23
|
|
|
@@ -40,8 +42,15 @@ export type ChatTurn = { role: "user" | "assistant" | "model"; text: string };
|
|
|
40
42
|
* A round of tool use as an application stores it between a question and its answer
|
|
41
43
|
* (`AskResult.toolTurns`): plain JSON. Passed back in `messages`, the model sees in a later
|
|
42
44
|
* question what its tools returned in an earlier one, and does not have to search again.
|
|
45
|
+
*
|
|
46
|
+
* `version` is the version of this shape: `TOOL_TURN_VERSION` (1) on the rounds of 0.9.0 and later,
|
|
47
|
+
* absent on those of earlier versions. It is never sent to the model. `askWithFiles` refuses a
|
|
48
|
+
* round of a higher version, which a newer version of the package made, with a `TypeError`.
|
|
43
49
|
*/
|
|
44
|
-
export type ToolTurn = { role: "tools"; calls: Array<ToolCall & { result: ToolResult }> };
|
|
50
|
+
export type ToolTurn = { role: "tools"; version?: number; calls: Array<ToolCall & { result: ToolResult }> };
|
|
51
|
+
|
|
52
|
+
/** The version of `ToolTurn` this package writes, and the highest it reads. New in 0.9.0. */
|
|
53
|
+
export const TOOL_TURN_VERSION = 1;
|
|
45
54
|
|
|
46
55
|
/**
|
|
47
56
|
* A result of a stored tool round, as it is sent again in a later question: without
|
|
@@ -347,6 +356,9 @@ export function classifyModelError(status: number, message: string, signals: { c
|
|
|
347
356
|
return { kind: "invalid" };
|
|
348
357
|
}
|
|
349
358
|
|
|
359
|
+
const MODEL_REQUEST_ERROR = Symbol.for("llm-large-files.ModelRequestError");
|
|
360
|
+
const DOCUMENTS_NOT_FOUND_ERROR = Symbol.for("llm-large-files.DocumentsNotFoundError");
|
|
361
|
+
|
|
350
362
|
/**
|
|
351
363
|
* A model request that failed: the provider answered with an HTTP error, or could not be reached
|
|
352
364
|
* (`status` 0). `retryAfterMs` is the wait the provider asked for, when it named one. `kind` says
|
|
@@ -380,7 +392,23 @@ export class ModelRequestError extends Error {
|
|
|
380
392
|
if (found.requestedTokens) this.requestedTokens = found.requestedTokens;
|
|
381
393
|
}
|
|
382
394
|
}
|
|
395
|
+
|
|
396
|
+
/**
|
|
397
|
+
* Whether `value` is a `ModelRequestError` (or one of its kinds, such as `GeminiRequestError`),
|
|
398
|
+
* made by this copy of the package or another one. New in 0.9.0.
|
|
399
|
+
*/
|
|
400
|
+
static is(value: unknown): value is ModelRequestError {
|
|
401
|
+
return branded(value, MODEL_REQUEST_ERROR);
|
|
402
|
+
}
|
|
383
403
|
}
|
|
404
|
+
brand(ModelRequestError, MODEL_REQUEST_ERROR);
|
|
405
|
+
|
|
406
|
+
/**
|
|
407
|
+
* A status of a model's error: 0 (no answer came) or an HTTP error, 400 to 599. `errorInfoOf`, the
|
|
408
|
+
* events, `modelFailureReason` and the retry policy tell the kind of an error without one of its
|
|
409
|
+
* own (an adapter of your own, `httpError`) from such a status.
|
|
410
|
+
*/
|
|
411
|
+
export const isModelStatus = (status: unknown): status is number => typeof status === "number" && (status === 0 || (status >= 400 && status <= 599));
|
|
384
412
|
|
|
385
413
|
/** The kind of a model error, when it has one: a `ModelRequestError`'s, or that of an error with the same field. */
|
|
386
414
|
export function errorKindOf(error: unknown): ModelErrorKind | undefined {
|
|
@@ -388,24 +416,60 @@ export function errorKindOf(error: unknown): ModelErrorKind | undefined {
|
|
|
388
416
|
return typeof kind === "string" ? kind as ModelErrorKind : undefined;
|
|
389
417
|
}
|
|
390
418
|
|
|
419
|
+
/**
|
|
420
|
+
* What a model's error is, as its events and `errorInfoOf` tell it: its own `kind`, with its own
|
|
421
|
+
* `limitTokens` and `requestedTokens`; or, for an error without a kind that has a model's status
|
|
422
|
+
* (an adapter of your own, `httpError`), what `classifyModelError` gives its status, message and
|
|
423
|
+
* `code`, with the numbers of an overflow its message gives. Undefined for an error with neither,
|
|
424
|
+
* and for thrown plain data, which is not an adapter's error. The retry policy and the loop act on
|
|
425
|
+
* this kind: the loop makes a request smaller, plans the documents again, or reads a part of a
|
|
426
|
+
* whole-file read as two halves, for a "context_overflow".
|
|
427
|
+
*/
|
|
428
|
+
export function modelErrorOf(error: unknown): { kind: unknown; limitTokens?: number; requestedTokens?: number } | undefined {
|
|
429
|
+
if (typeof error !== "object" || error === null) return undefined;
|
|
430
|
+
const { kind, limitTokens, requestedTokens } = error as { kind?: unknown; limitTokens?: number; requestedTokens?: number };
|
|
431
|
+
if (kind !== undefined) return { kind, limitTokens, requestedTokens };
|
|
432
|
+
const { status, message, code } = error as { status?: unknown; message?: unknown; code?: unknown };
|
|
433
|
+
if (!isModelStatus(status)) return undefined;
|
|
434
|
+
const proto: unknown = Object.getPrototypeOf(error);
|
|
435
|
+
if (proto === Object.prototype || proto === null) return undefined;
|
|
436
|
+
return classifyModelError(status, typeof message === "string" ? message : "", { code: typeof code === "string" ? code : undefined });
|
|
437
|
+
}
|
|
438
|
+
|
|
391
439
|
/**
|
|
392
440
|
* For a request refused as larger than the tokens a minute (or a day) of the provider allows, not
|
|
393
441
|
* than the model's window: that period. Undefined for any other error.
|
|
394
442
|
*/
|
|
395
443
|
export function rateAllowanceOf(error: unknown): "minute" | "day" | undefined {
|
|
396
|
-
if (
|
|
397
|
-
|
|
444
|
+
if (modelErrorOf(error)?.kind !== "context_overflow") return undefined;
|
|
445
|
+
// modelErrorOf found an object: its message is read whatever its class, as modelErrorOf reads it.
|
|
446
|
+
const { message } = error as { message?: unknown };
|
|
447
|
+
if (typeof message !== "string") return undefined;
|
|
448
|
+
const period = RATE_TOO_SMALL.exec(message)?.[1];
|
|
398
449
|
if (period === undefined) return undefined;
|
|
399
450
|
return period.toLowerCase() === "day" ? "day" : "minute";
|
|
400
451
|
}
|
|
401
452
|
|
|
402
|
-
/**
|
|
453
|
+
/**
|
|
454
|
+
* Some document IDs were never uploaded, have expired, or belong to another owner. `code` and
|
|
455
|
+
* `status` are new in 0.9.0.
|
|
456
|
+
*/
|
|
403
457
|
export class DocumentsNotFoundError extends Error {
|
|
458
|
+
readonly code: "documents_missing" = "documents_missing";
|
|
459
|
+
/** The HTTP status to answer with. */
|
|
460
|
+
readonly status: 404 = 404;
|
|
461
|
+
|
|
404
462
|
constructor(readonly ids: string[]) {
|
|
405
463
|
super(`${ids.length === 1 ? "A document is" : `${ids.length} documents are`} no longer available: ${ids.join(", ")}. Upload ${ids.length === 1 ? "it" : "them"} again.`);
|
|
406
464
|
this.name = "DocumentsNotFoundError";
|
|
407
465
|
}
|
|
466
|
+
|
|
467
|
+
/** Whether `value` is a `DocumentsNotFoundError`, made by this copy of the package or another one. New in 0.9.0. */
|
|
468
|
+
static is(value: unknown): value is DocumentsNotFoundError {
|
|
469
|
+
return branded(value, DOCUMENTS_NOT_FOUND_ERROR);
|
|
470
|
+
}
|
|
408
471
|
}
|
|
472
|
+
brand(DocumentsNotFoundError, DOCUMENTS_NOT_FOUND_ERROR);
|
|
409
473
|
|
|
410
474
|
const EMPTY_USAGE: ModelUsage = { inputTokens: 0, cachedInputTokens: 0, outputTokens: 0, thoughtTokens: 0 };
|
|
411
475
|
|