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.
- package/CHANGELOG.md +981 -0
- package/README.md +22 -17
- package/dist/access.d.ts +128 -0
- package/dist/access.d.ts.map +1 -0
- package/dist/access.js +234 -0
- package/dist/access.js.map +1 -0
- package/dist/aggregate.d.ts +6 -0
- package/dist/aggregate.d.ts.map +1 -1
- package/dist/aggregate.js +86 -13
- package/dist/aggregate.js.map +1 -1
- package/dist/analyze.d.ts +4 -0
- package/dist/analyze.d.ts.map +1 -1
- package/dist/analyze.js +2 -2
- package/dist/analyze.js.map +1 -1
- package/dist/answer-info.d.ts +26 -0
- package/dist/answer-info.d.ts.map +1 -0
- package/dist/answer-info.js +32 -0
- package/dist/answer-info.js.map +1 -0
- package/dist/anthropic/adapter.d.ts +5 -1
- package/dist/anthropic/adapter.d.ts.map +1 -1
- package/dist/anthropic/adapter.js +35 -10
- package/dist/anthropic/adapter.js.map +1 -1
- package/dist/ask.d.ts +159 -14
- package/dist/ask.d.ts.map +1 -1
- package/dist/ask.js +743 -113
- 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/citations.d.ts +36 -0
- package/dist/citations.d.ts.map +1 -0
- package/dist/citations.js +291 -0
- package/dist/citations.js.map +1 -0
- package/dist/encoding.js +5 -3
- package/dist/encoding.js.map +1 -1
- package/dist/error-info.d.ts +70 -0
- package/dist/error-info.d.ts.map +1 -0
- package/dist/error-info.js +253 -0
- package/dist/error-info.js.map +1 -0
- package/dist/errors.d.ts +74 -8
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +104 -15
- package/dist/errors.js.map +1 -1
- package/dist/events.d.ts +57 -13
- 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 +35 -5
- 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 +52 -36
- package/dist/extract.js.map +1 -1
- package/dist/gemini/adapter.d.ts +54 -7
- package/dist/gemini/adapter.d.ts.map +1 -1
- package/dist/gemini/adapter.js +152 -41
- package/dist/gemini/adapter.js.map +1 -1
- package/dist/gemini/chat.d.ts +38 -53
- package/dist/gemini/chat.d.ts.map +1 -1
- package/dist/gemini/chat.js +20 -25
- package/dist/gemini/chat.js.map +1 -1
- package/dist/gemini/endpoints.d.ts +59 -11
- package/dist/gemini/endpoints.d.ts.map +1 -1
- package/dist/gemini/endpoints.js +33 -7
- package/dist/gemini/endpoints.js.map +1 -1
- package/dist/gemini/errors.d.ts +39 -5
- package/dist/gemini/errors.d.ts.map +1 -1
- package/dist/gemini/errors.js +70 -9
- package/dist/gemini/errors.js.map +1 -1
- package/dist/gemini/generate.d.ts +53 -2
- package/dist/gemini/generate.d.ts.map +1 -1
- package/dist/gemini/generate.js +100 -50
- package/dist/gemini/generate.js.map +1 -1
- package/dist/gemini/index.d.ts +4 -4
- package/dist/gemini/index.d.ts.map +1 -1
- package/dist/http.d.ts +41 -3
- package/dist/http.d.ts.map +1 -1
- package/dist/http.js +40 -2
- package/dist/http.js.map +1 -1
- package/dist/index.d.ts +13 -8
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -5
- package/dist/index.js.map +1 -1
- package/dist/ingest.d.ts +78 -0
- package/dist/ingest.d.ts.map +1 -0
- package/dist/ingest.js +893 -0
- package/dist/ingest.js.map +1 -0
- package/dist/library.d.ts +295 -33
- package/dist/library.d.ts.map +1 -1
- package/dist/library.js +458 -650
- package/dist/library.js.map +1 -1
- package/dist/map-reduce.d.ts +176 -10
- package/dist/map-reduce.d.ts.map +1 -1
- package/dist/map-reduce.js +807 -61
- package/dist/map-reduce.js.map +1 -1
- package/dist/model.d.ts +79 -6
- package/dist/model.d.ts.map +1 -1
- package/dist/model.js +80 -4
- package/dist/model.js.map +1 -1
- package/dist/node/handlers.d.ts +247 -14
- package/dist/node/handlers.d.ts.map +1 -1
- package/dist/node/handlers.js +491 -227
- 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 +19 -4
- 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 +106 -0
- package/dist/node/spool.d.ts.map +1 -0
- package/dist/node/spool.js +378 -0
- package/dist/node/spool.js.map +1 -0
- package/dist/node/sse.d.ts +6 -4
- package/dist/node/sse.d.ts.map +1 -1
- package/dist/node/sse.js +9 -7
- package/dist/node/sse.js.map +1 -1
- package/dist/openai/adapter.d.ts +5 -1
- package/dist/openai/adapter.d.ts.map +1 -1
- package/dist/openai/adapter.js +53 -12
- package/dist/openai/adapter.js.map +1 -1
- package/dist/output-limit.d.ts +2 -1
- package/dist/output-limit.d.ts.map +1 -1
- package/dist/output-limit.js +5 -2
- package/dist/output-limit.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/pdf-outline.d.ts +71 -0
- package/dist/pdf-outline.d.ts.map +1 -0
- package/dist/pdf-outline.js +414 -0
- package/dist/pdf-outline.js.map +1 -0
- package/dist/plan.d.ts +137 -7
- package/dist/plan.d.ts.map +1 -1
- package/dist/plan.js +248 -26
- package/dist/plan.js.map +1 -1
- package/dist/prepare.d.ts +50 -0
- package/dist/prepare.d.ts.map +1 -0
- package/dist/prepare.js +207 -0
- package/dist/prepare.js.map +1 -0
- package/dist/ranged.d.ts +16 -11
- package/dist/ranged.d.ts.map +1 -1
- package/dist/ranged.js +56 -12
- 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/search.d.ts +7 -0
- package/dist/search.d.ts.map +1 -1
- package/dist/search.js +72 -10
- package/dist/search.js.map +1 -1
- 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 +126 -31
- package/dist/stores.d.ts.map +1 -1
- package/dist/stores.js +655 -141
- 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 +47 -2
- package/dist/tools.d.ts.map +1 -1
- package/dist/tools.js +440 -71
- package/dist/tools.js.map +1 -1
- package/dist/types.d.ts +165 -14
- package/dist/types.d.ts.map +1 -1
- package/dist/whole-cost.d.ts +37 -0
- package/dist/whole-cost.d.ts.map +1 -0
- package/dist/whole-cost.js +46 -0
- package/dist/whole-cost.js.map +1 -0
- package/docs/README.md +21 -8
- package/docs/getting-started.md +18 -13
- package/docs/guides/anthropic.md +17 -10
- package/docs/guides/conversations.md +112 -47
- package/docs/guides/costs-and-limits.md +147 -42
- package/docs/guides/deployment.md +380 -86
- package/docs/guides/errors-and-reliability.md +317 -67
- package/docs/guides/explorer-model.md +4 -1
- package/docs/guides/openai-compatible.md +47 -7
- package/docs/guides/own-adapter-and-loop.md +60 -16
- package/docs/guides/pdfs.md +219 -36
- package/docs/guides/storage.md +676 -145
- package/docs/guides/testing.md +149 -129
- package/docs/guides/vertex-and-gemini.md +169 -57
- package/docs/guides/web-app.md +318 -80
- package/docs/how-it-works.md +155 -30
- package/docs/measurements.md +215 -13
- package/docs/publishing.md +185 -37
- package/docs/reference/anthropic.md +12 -7
- package/docs/reference/ask.md +415 -47
- package/docs/reference/errors-and-retries.md +194 -14
- package/docs/reference/events.md +77 -27
- package/docs/reference/gemini.md +252 -87
- package/docs/reference/library.md +886 -178
- package/docs/reference/node.md +659 -111
- package/docs/reference/openai.md +21 -10
- package/docs/reference/testing.md +311 -0
- package/docs/reference/tools.md +285 -30
- package/docs/security.md +404 -78
- package/docs/troubleshooting.md +151 -70
- package/docs/upgrading-0.10.md +472 -0
- package/docs/upgrading-0.9.md +890 -0
- package/package.json +16 -3
- package/src/access.ts +267 -0
- package/src/aggregate.ts +92 -14
- package/src/analyze.ts +2 -2
- package/src/answer-info.ts +45 -0
- package/src/anthropic/adapter.ts +40 -11
- package/src/ask.ts +866 -115
- package/src/cache.ts +20 -5
- package/src/citations.ts +294 -0
- package/src/encoding.ts +6 -4
- package/src/error-info.ts +290 -0
- package/src/errors.ts +131 -15
- package/src/events.ts +112 -24
- package/src/evidence.ts +35 -5
- package/src/extract.ts +56 -34
- package/src/gemini/adapter.ts +196 -43
- package/src/gemini/chat.ts +63 -78
- package/src/gemini/endpoints.ts +89 -16
- package/src/gemini/errors.ts +74 -11
- package/src/gemini/generate.ts +147 -57
- package/src/gemini/index.ts +4 -4
- package/src/http.ts +65 -4
- package/src/index.ts +13 -8
- package/src/ingest.ts +958 -0
- package/src/library.ts +711 -651
- package/src/map-reduce.ts +858 -70
- package/src/model.ts +113 -9
- package/src/node/handlers.ts +709 -221
- package/src/node/index.ts +14 -2
- package/src/node/ingest-child.ts +24 -6
- package/src/node/isolated.ts +11 -4
- package/src/node/originals.ts +78 -0
- package/src/node/spool.ts +379 -0
- package/src/node/sse.ts +9 -7
- package/src/openai/adapter.ts +56 -13
- package/src/output-limit.ts +5 -2
- package/src/pdf-layout.ts +69 -11
- package/src/pdf-outline.ts +400 -0
- package/src/plan.ts +287 -36
- package/src/prepare.ts +253 -0
- package/src/ranged.ts +48 -14
- package/src/retry.ts +6 -2
- package/src/scope.ts +63 -0
- package/src/search.ts +69 -13
- package/src/source.ts +178 -0
- package/src/stores.ts +702 -142
- 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 +461 -73
- package/src/types.ts +151 -14
- package/src/whole-cost.ts +64 -0
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
|
|
@@ -94,12 +103,25 @@ export type ModelReply = {
|
|
|
94
103
|
/**
|
|
95
104
|
* How the reply ended, in terms every adapter maps its own to: "complete"; "cut" at the output
|
|
96
105
|
* limit; "filtered" by the provider's safety filters; "malformed_call", a tool call the model
|
|
97
|
-
* tried to make that could not be read (Gemini's MALFORMED_FUNCTION_CALL)
|
|
98
|
-
*
|
|
106
|
+
* tried to make that could not be read (Gemini's MALFORMED_FUNCTION_CALL); "other", a known
|
|
107
|
+
* reason that is none of these and that asking again does not mend (Gemini's OTHER or LANGUAGE;
|
|
108
|
+
* new in 0.10.0). The loop does not ask again after "filtered" or "other". Unset when the adapter
|
|
109
|
+
* does not know the reason.
|
|
99
110
|
*/
|
|
100
|
-
finish?: "complete" | "cut" | "filtered" | "malformed_call";
|
|
111
|
+
finish?: "complete" | "cut" | "filtered" | "malformed_call" | "other";
|
|
101
112
|
/** Why the provider refused the prompt, when it did. Nothing more is asked after it. */
|
|
102
113
|
blockReason?: string;
|
|
114
|
+
/**
|
|
115
|
+
* The provider's ID of this response (Gemini's `responseId`, the `id` of OpenAI and Claude), when
|
|
116
|
+
* it gives one. `gemini()`, `openAiCompatible()` and `anthropic()` set it. New in 0.10.0.
|
|
117
|
+
*/
|
|
118
|
+
responseId?: string;
|
|
119
|
+
/**
|
|
120
|
+
* The model that wrote this response, as the provider names it (Gemini's `modelVersion`, the
|
|
121
|
+
* `model` of OpenAI and Claude). `gemini()`, `openAiCompatible()` and `anthropic()` set it. New
|
|
122
|
+
* in 0.10.0.
|
|
123
|
+
*/
|
|
124
|
+
responseModel?: string;
|
|
103
125
|
usage?: Partial<ModelUsage>;
|
|
104
126
|
/** The provider's own usage object, as it came. */
|
|
105
127
|
providerUsage?: Record<string, unknown>;
|
|
@@ -347,6 +369,9 @@ export function classifyModelError(status: number, message: string, signals: { c
|
|
|
347
369
|
return { kind: "invalid" };
|
|
348
370
|
}
|
|
349
371
|
|
|
372
|
+
const MODEL_REQUEST_ERROR = Symbol.for("llm-large-files.ModelRequestError");
|
|
373
|
+
const DOCUMENTS_NOT_FOUND_ERROR = Symbol.for("llm-large-files.DocumentsNotFoundError");
|
|
374
|
+
|
|
350
375
|
/**
|
|
351
376
|
* A model request that failed: the provider answered with an HTTP error, or could not be reached
|
|
352
377
|
* (`status` 0). `retryAfterMs` is the wait the provider asked for, when it named one. `kind` says
|
|
@@ -380,7 +405,23 @@ export class ModelRequestError extends Error {
|
|
|
380
405
|
if (found.requestedTokens) this.requestedTokens = found.requestedTokens;
|
|
381
406
|
}
|
|
382
407
|
}
|
|
408
|
+
|
|
409
|
+
/**
|
|
410
|
+
* Whether `value` is a `ModelRequestError` (or one of its kinds, such as `GeminiRequestError`),
|
|
411
|
+
* made by this copy of the package or another one. New in 0.9.0.
|
|
412
|
+
*/
|
|
413
|
+
static is(value: unknown): value is ModelRequestError {
|
|
414
|
+
return branded(value, MODEL_REQUEST_ERROR);
|
|
415
|
+
}
|
|
383
416
|
}
|
|
417
|
+
brand(ModelRequestError, MODEL_REQUEST_ERROR);
|
|
418
|
+
|
|
419
|
+
/**
|
|
420
|
+
* A status of a model's error: 0 (no answer came) or an HTTP error, 400 to 599. `errorInfoOf`, the
|
|
421
|
+
* events, `modelFailureReason` and the retry policy tell the kind of an error without one of its
|
|
422
|
+
* own (an adapter of your own, `httpError`) from such a status.
|
|
423
|
+
*/
|
|
424
|
+
export const isModelStatus = (status: unknown): status is number => typeof status === "number" && (status === 0 || (status >= 400 && status <= 599));
|
|
384
425
|
|
|
385
426
|
/** The kind of a model error, when it has one: a `ModelRequestError`'s, or that of an error with the same field. */
|
|
386
427
|
export function errorKindOf(error: unknown): ModelErrorKind | undefined {
|
|
@@ -388,24 +429,60 @@ export function errorKindOf(error: unknown): ModelErrorKind | undefined {
|
|
|
388
429
|
return typeof kind === "string" ? kind as ModelErrorKind : undefined;
|
|
389
430
|
}
|
|
390
431
|
|
|
432
|
+
/**
|
|
433
|
+
* What a model's error is, as its events and `errorInfoOf` tell it: its own `kind`, with its own
|
|
434
|
+
* `limitTokens` and `requestedTokens`; or, for an error without a kind that has a model's status
|
|
435
|
+
* (an adapter of your own, `httpError`), what `classifyModelError` gives its status, message and
|
|
436
|
+
* `code`, with the numbers of an overflow its message gives. Undefined for an error with neither,
|
|
437
|
+
* and for thrown plain data, which is not an adapter's error. The retry policy and the loop act on
|
|
438
|
+
* this kind: the loop makes a request smaller, plans the documents again, or reads a part of a
|
|
439
|
+
* whole-file read as two halves, for a "context_overflow".
|
|
440
|
+
*/
|
|
441
|
+
export function modelErrorOf(error: unknown): { kind: unknown; limitTokens?: number; requestedTokens?: number } | undefined {
|
|
442
|
+
if (typeof error !== "object" || error === null) return undefined;
|
|
443
|
+
const { kind, limitTokens, requestedTokens } = error as { kind?: unknown; limitTokens?: number; requestedTokens?: number };
|
|
444
|
+
if (kind !== undefined) return { kind, limitTokens, requestedTokens };
|
|
445
|
+
const { status, message, code } = error as { status?: unknown; message?: unknown; code?: unknown };
|
|
446
|
+
if (!isModelStatus(status)) return undefined;
|
|
447
|
+
const proto: unknown = Object.getPrototypeOf(error);
|
|
448
|
+
if (proto === Object.prototype || proto === null) return undefined;
|
|
449
|
+
return classifyModelError(status, typeof message === "string" ? message : "", { code: typeof code === "string" ? code : undefined });
|
|
450
|
+
}
|
|
451
|
+
|
|
391
452
|
/**
|
|
392
453
|
* For a request refused as larger than the tokens a minute (or a day) of the provider allows, not
|
|
393
454
|
* than the model's window: that period. Undefined for any other error.
|
|
394
455
|
*/
|
|
395
456
|
export function rateAllowanceOf(error: unknown): "minute" | "day" | undefined {
|
|
396
|
-
if (
|
|
397
|
-
|
|
457
|
+
if (modelErrorOf(error)?.kind !== "context_overflow") return undefined;
|
|
458
|
+
// modelErrorOf found an object: its message is read whatever its class, as modelErrorOf reads it.
|
|
459
|
+
const { message } = error as { message?: unknown };
|
|
460
|
+
if (typeof message !== "string") return undefined;
|
|
461
|
+
const period = RATE_TOO_SMALL.exec(message)?.[1];
|
|
398
462
|
if (period === undefined) return undefined;
|
|
399
463
|
return period.toLowerCase() === "day" ? "day" : "minute";
|
|
400
464
|
}
|
|
401
465
|
|
|
402
|
-
/**
|
|
466
|
+
/**
|
|
467
|
+
* Some document IDs were never uploaded, have expired, or belong to another owner. `code` and
|
|
468
|
+
* `status` are new in 0.9.0.
|
|
469
|
+
*/
|
|
403
470
|
export class DocumentsNotFoundError extends Error {
|
|
471
|
+
readonly code: "documents_missing" = "documents_missing";
|
|
472
|
+
/** The HTTP status to answer with. */
|
|
473
|
+
readonly status: 404 = 404;
|
|
474
|
+
|
|
404
475
|
constructor(readonly ids: string[]) {
|
|
405
476
|
super(`${ids.length === 1 ? "A document is" : `${ids.length} documents are`} no longer available: ${ids.join(", ")}. Upload ${ids.length === 1 ? "it" : "them"} again.`);
|
|
406
477
|
this.name = "DocumentsNotFoundError";
|
|
407
478
|
}
|
|
479
|
+
|
|
480
|
+
/** Whether `value` is a `DocumentsNotFoundError`, made by this copy of the package or another one. New in 0.9.0. */
|
|
481
|
+
static is(value: unknown): value is DocumentsNotFoundError {
|
|
482
|
+
return branded(value, DOCUMENTS_NOT_FOUND_ERROR);
|
|
483
|
+
}
|
|
408
484
|
}
|
|
485
|
+
brand(DocumentsNotFoundError, DOCUMENTS_NOT_FOUND_ERROR);
|
|
409
486
|
|
|
410
487
|
const EMPTY_USAGE: ModelUsage = { inputTokens: 0, cachedInputTokens: 0, outputTokens: 0, thoughtTokens: 0 };
|
|
411
488
|
|
|
@@ -414,13 +491,36 @@ export type UsageTotals = ModelUsage & {
|
|
|
414
491
|
/** Requests that were answered. One that failed and was sent again counts once. */
|
|
415
492
|
requests: number;
|
|
416
493
|
byModel: Record<string, ModelUsage & { requests: number }>;
|
|
494
|
+
/**
|
|
495
|
+
* The input tokens of the requests whose provider said nothing of their input, by the library's
|
|
496
|
+
* estimate, in each model's own tokens: the ceiling of the question (`limits.inputTokens`) counts
|
|
497
|
+
* them with the reported ones. Absent when every request reported its input. New in 0.10.0.
|
|
498
|
+
*/
|
|
499
|
+
estimatedInputTokens?: number;
|
|
500
|
+
/**
|
|
501
|
+
* Requests that were answered without their input tokens from the provider: no usage, or an input
|
|
502
|
+
* of 0 (see `estimatedInputTokens`). Absent when there was none. New in 0.10.0.
|
|
503
|
+
*/
|
|
504
|
+
unmeasuredRequests?: number;
|
|
505
|
+
};
|
|
506
|
+
|
|
507
|
+
/** True when `usage` gives the input tokens of its request: a number above 0. */
|
|
508
|
+
export const reportsInput = (usage: Partial<ModelUsage> | undefined): boolean => {
|
|
509
|
+
const input = usage?.inputTokens;
|
|
510
|
+
return typeof input === "number" && Number.isFinite(input) && input > 0;
|
|
417
511
|
};
|
|
418
512
|
|
|
419
513
|
/** Sums the usage of the requests of one question. */
|
|
420
514
|
export class UsageMeter {
|
|
421
515
|
readonly totals: UsageTotals = { requests: 0, ...EMPTY_USAGE, byModel: {} };
|
|
422
516
|
|
|
423
|
-
|
|
517
|
+
/**
|
|
518
|
+
* Counts one answered request of `model`. `estimatedInputTokens` is the library's estimate of its
|
|
519
|
+
* input, in the model's own tokens: when `usage` does not give the input (no usage, or 0), the
|
|
520
|
+
* estimate goes into `estimatedInputTokens` and the request into `unmeasuredRequests`. Without
|
|
521
|
+
* it, such a request is counted as before 0.10.0.
|
|
522
|
+
*/
|
|
523
|
+
add(model: string, usage: Partial<ModelUsage> | undefined, estimatedInputTokens?: number): void {
|
|
424
524
|
const { byModel } = this.totals;
|
|
425
525
|
// An own entry only: an id such as "__proto__" or "constructor" found what every object
|
|
426
526
|
// inherits, and "__proto__" then counted the usage into Object.prototype.
|
|
@@ -436,5 +536,9 @@ export class UsageMeter {
|
|
|
436
536
|
if (typeof value === "number" && Number.isFinite(value) && value > 0) sum[key] += value;
|
|
437
537
|
}
|
|
438
538
|
}
|
|
539
|
+
if (estimatedInputTokens === undefined || reportsInput(usage)) return;
|
|
540
|
+
this.totals.unmeasuredRequests = (this.totals.unmeasuredRequests ?? 0) + 1;
|
|
541
|
+
const estimate = Number.isFinite(estimatedInputTokens) && estimatedInputTokens > 0 ? Math.ceil(estimatedInputTokens) : 0;
|
|
542
|
+
this.totals.estimatedInputTokens = (this.totals.estimatedInputTokens ?? 0) + estimate;
|
|
439
543
|
}
|
|
440
544
|
}
|