llm-large-files 0.4.0 → 0.5.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 +205 -0
- package/README.md +48 -8
- package/dist/anthropic/adapter.d.ts +106 -0
- package/dist/anthropic/adapter.d.ts.map +1 -0
- package/dist/anthropic/adapter.js +391 -0
- package/dist/anthropic/adapter.js.map +1 -0
- package/dist/anthropic/index.d.ts +3 -0
- package/dist/anthropic/index.d.ts.map +1 -0
- package/dist/anthropic/index.js +2 -0
- package/dist/anthropic/index.js.map +1 -0
- package/dist/ask.d.ts +144 -0
- package/dist/ask.d.ts.map +1 -0
- package/dist/ask.js +540 -0
- package/dist/ask.js.map +1 -0
- package/dist/budget.d.ts +23 -0
- package/dist/budget.d.ts.map +1 -0
- package/dist/budget.js +64 -0
- package/dist/budget.js.map +1 -0
- package/dist/cache.d.ts +11 -2
- package/dist/cache.d.ts.map +1 -1
- package/dist/cache.js +36 -7
- package/dist/cache.js.map +1 -1
- package/dist/errors.d.ts +21 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +44 -0
- package/dist/errors.js.map +1 -0
- package/dist/evidence.d.ts +40 -0
- package/dist/evidence.d.ts.map +1 -0
- package/dist/evidence.js +137 -0
- package/dist/evidence.js.map +1 -0
- package/dist/gemini/adapter.d.ts +50 -0
- package/dist/gemini/adapter.d.ts.map +1 -0
- package/dist/gemini/adapter.js +179 -0
- package/dist/gemini/adapter.js.map +1 -0
- package/dist/gemini/chat.d.ts +32 -71
- package/dist/gemini/chat.d.ts.map +1 -1
- package/dist/gemini/chat.js +38 -284
- package/dist/gemini/chat.js.map +1 -1
- package/dist/gemini/errors.d.ts +11 -0
- package/dist/gemini/errors.d.ts.map +1 -0
- package/dist/gemini/errors.js +14 -0
- package/dist/gemini/errors.js.map +1 -0
- package/dist/gemini/generate.d.ts +12 -13
- package/dist/gemini/generate.d.ts.map +1 -1
- package/dist/gemini/generate.js +62 -26
- package/dist/gemini/generate.js.map +1 -1
- package/dist/gemini/index.d.ts +3 -1
- package/dist/gemini/index.d.ts.map +1 -1
- package/dist/gemini/index.js +2 -1
- package/dist/gemini/index.js.map +1 -1
- package/dist/gemini/sse.d.ts +1 -2
- package/dist/gemini/sse.d.ts.map +1 -1
- package/dist/gemini/sse.js +1 -38
- package/dist/gemini/sse.js.map +1 -1
- package/dist/http.d.ts +20 -0
- package/dist/http.d.ts.map +1 -0
- package/dist/http.js +55 -0
- package/dist/http.js.map +1 -0
- package/dist/index.d.ts +14 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +12 -4
- package/dist/index.js.map +1 -1
- package/dist/library.d.ts +50 -5
- package/dist/library.d.ts.map +1 -1
- package/dist/library.js +56 -26
- package/dist/library.js.map +1 -1
- package/dist/map-reduce.d.ts +15 -0
- package/dist/map-reduce.d.ts.map +1 -1
- package/dist/map-reduce.js +58 -27
- package/dist/map-reduce.js.map +1 -1
- package/dist/model.d.ts +196 -0
- package/dist/model.d.ts.map +1 -0
- package/dist/model.js +41 -0
- package/dist/model.js.map +1 -0
- package/dist/openai/adapter.d.ts +85 -0
- package/dist/openai/adapter.d.ts.map +1 -0
- package/dist/openai/adapter.js +293 -0
- package/dist/openai/adapter.js.map +1 -0
- package/dist/openai/index.d.ts +3 -0
- package/dist/openai/index.d.ts.map +1 -0
- package/dist/openai/index.js +2 -0
- package/dist/openai/index.js.map +1 -0
- package/dist/output-limit.d.ts +30 -0
- package/dist/output-limit.d.ts.map +1 -0
- package/dist/output-limit.js +92 -0
- package/dist/output-limit.js.map +1 -0
- package/dist/plan.d.ts +20 -4
- package/dist/plan.d.ts.map +1 -1
- package/dist/plan.js +44 -7
- package/dist/plan.js.map +1 -1
- package/dist/ranged.d.ts +2 -2
- package/dist/ranged.d.ts.map +1 -1
- package/dist/ranged.js +11 -4
- package/dist/ranged.js.map +1 -1
- package/dist/retry.d.ts +54 -0
- package/dist/retry.d.ts.map +1 -0
- package/dist/retry.js +111 -0
- package/dist/retry.js.map +1 -0
- package/dist/search.d.ts +16 -2
- package/dist/search.d.ts.map +1 -1
- package/dist/search.js +109 -26
- package/dist/search.js.map +1 -1
- package/dist/sse.d.ts +3 -0
- package/dist/sse.d.ts.map +1 -0
- package/dist/sse.js +39 -0
- package/dist/sse.js.map +1 -0
- package/dist/stores.d.ts +5 -1
- package/dist/stores.d.ts.map +1 -1
- package/dist/stores.js +58 -15
- package/dist/stores.js.map +1 -1
- package/dist/text.d.ts +12 -1
- package/dist/text.d.ts.map +1 -1
- package/dist/text.js +43 -15
- package/dist/text.js.map +1 -1
- package/dist/tools.d.ts +23 -0
- package/dist/tools.d.ts.map +1 -1
- package/dist/tools.js +115 -35
- package/dist/tools.js.map +1 -1
- package/dist/types.d.ts +33 -3
- package/dist/types.d.ts.map +1 -1
- package/docs/api.md +253 -28
- package/docs/configuration.md +89 -10
- package/docs/getting-started.md +69 -16
- package/docs/how-it-works.md +46 -4
- package/docs/security.md +36 -3
- package/package.json +21 -3
- package/src/anthropic/adapter.ts +454 -0
- package/src/anthropic/index.ts +2 -0
- package/src/ask.ts +675 -0
- package/src/budget.ts +71 -0
- package/src/cache.ts +32 -8
- package/src/errors.ts +42 -0
- package/src/evidence.ts +162 -0
- package/src/gemini/adapter.ts +218 -0
- package/src/gemini/chat.ts +62 -333
- package/src/gemini/errors.ts +15 -0
- package/src/gemini/generate.ts +72 -30
- package/src/gemini/index.ts +3 -1
- package/src/gemini/sse.ts +1 -34
- package/src/http.ts +59 -0
- package/src/index.ts +34 -4
- package/src/library.ts +102 -29
- package/src/map-reduce.ts +52 -23
- package/src/model.ts +200 -0
- package/src/openai/adapter.ts +345 -0
- package/src/openai/index.ts +2 -0
- package/src/output-limit.ts +99 -0
- package/src/plan.ts +46 -7
- package/src/ranged.ts +13 -6
- package/src/retry.ts +132 -0
- package/src/search.ts +116 -25
- package/src/sse.ts +34 -0
- package/src/stores.ts +51 -15
- package/src/text.ts +39 -12
- package/src/tools.ts +127 -38
- package/src/types.ts +29 -3
package/dist/budget.js
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/** Defaults sized for a 1M-token model behind a 10 MB request limit. */
|
|
2
|
+
export const DEFAULT_BUDGET = {
|
|
3
|
+
contextTokens: 1_000_000,
|
|
4
|
+
maxRequestBytes: 10_000_000,
|
|
5
|
+
inlineTokens: 200_000,
|
|
6
|
+
reservedTokens: 64_000,
|
|
7
|
+
mapChunkTokens: 100_000,
|
|
8
|
+
maxMapChunks: 30,
|
|
9
|
+
toolResultCharacters: 24_000,
|
|
10
|
+
};
|
|
11
|
+
const FIELDS = Object.keys(DEFAULT_BUDGET);
|
|
12
|
+
/** The fields of a partial budget that were given: a key set to undefined is one that was not. */
|
|
13
|
+
export function givenBudget(budget) {
|
|
14
|
+
const given = {};
|
|
15
|
+
for (const field of FIELDS) {
|
|
16
|
+
const value = budget?.[field];
|
|
17
|
+
if (value === undefined)
|
|
18
|
+
continue;
|
|
19
|
+
if (typeof value !== "number" || !Number.isFinite(value) || value <= 0) {
|
|
20
|
+
throw new Error(`budget.${field} must be a number above 0, and is ${String(value)}.`);
|
|
21
|
+
}
|
|
22
|
+
given[field] = value;
|
|
23
|
+
}
|
|
24
|
+
return given;
|
|
25
|
+
}
|
|
26
|
+
const count = (value) => value.toLocaleString("en-US");
|
|
27
|
+
/**
|
|
28
|
+
* A whole budget from the fields that were given. What was left out follows the context window:
|
|
29
|
+
* a quarter of it is kept free (64,000 tokens at most), half of it may be document text (200,000
|
|
30
|
+
* at most), a part of a whole-file read is half of it (100,000 at most), and one tool result is
|
|
31
|
+
* about an eighth of it (24,000 characters at most). At the default window of 1M tokens these are
|
|
32
|
+
* the defaults the library always had.
|
|
33
|
+
*
|
|
34
|
+
* Given values that cannot work together are an error. With `limits`, a model's own context window
|
|
35
|
+
* and request size, the budget is made to fit them instead: the smaller window holds, and given
|
|
36
|
+
* values too large for it are lowered.
|
|
37
|
+
*/
|
|
38
|
+
export function resolveBudget(budget, limits) {
|
|
39
|
+
const given = givenBudget(budget);
|
|
40
|
+
const lowest = (own, limit) => (typeof limit === "number" && Number.isFinite(limit) && limit > 0 ? Math.min(own, limit) : own);
|
|
41
|
+
const contextTokens = lowest(given.contextTokens ?? DEFAULT_BUDGET.contextTokens, limits?.contextTokens);
|
|
42
|
+
const maxRequestBytes = lowest(given.maxRequestBytes ?? DEFAULT_BUDGET.maxRequestBytes, limits?.maxRequestBytes);
|
|
43
|
+
// A window that a model made smaller than the one the values were given for: they follow it down.
|
|
44
|
+
const fitted = contextTokens < (given.contextTokens ?? DEFAULT_BUDGET.contextTokens);
|
|
45
|
+
const half = Math.max(1, Math.floor(contextTokens / 2));
|
|
46
|
+
const fit = (value, most) => (value === undefined ? undefined : fitted ? Math.min(value, most) : value);
|
|
47
|
+
const resolved = {
|
|
48
|
+
contextTokens,
|
|
49
|
+
maxRequestBytes,
|
|
50
|
+
inlineTokens: fit(given.inlineTokens, contextTokens) ?? Math.min(DEFAULT_BUDGET.inlineTokens, half),
|
|
51
|
+
reservedTokens: fit(given.reservedTokens, half) ?? Math.min(DEFAULT_BUDGET.reservedTokens, Math.max(1, Math.floor(contextTokens / 4))),
|
|
52
|
+
mapChunkTokens: fit(given.mapChunkTokens, half) ?? Math.min(DEFAULT_BUDGET.mapChunkTokens, half),
|
|
53
|
+
maxMapChunks: given.maxMapChunks ?? DEFAULT_BUDGET.maxMapChunks,
|
|
54
|
+
toolResultCharacters: fit(given.toolResultCharacters, Math.max(1_000, contextTokens)) ?? Math.min(DEFAULT_BUDGET.toolResultCharacters, Math.max(1_000, Math.round(contextTokens * 0.12))),
|
|
55
|
+
};
|
|
56
|
+
if (resolved.reservedTokens >= resolved.contextTokens) {
|
|
57
|
+
throw new Error(`budget.reservedTokens (${count(resolved.reservedTokens)}) must be smaller than budget.contextTokens (${count(resolved.contextTokens)}): nothing would be left for the conversation.`);
|
|
58
|
+
}
|
|
59
|
+
if (resolved.mapChunkTokens >= resolved.contextTokens) {
|
|
60
|
+
throw new Error(`budget.mapChunkTokens (${count(resolved.mapChunkTokens)}) must be smaller than budget.contextTokens (${count(resolved.contextTokens)}): a part of a whole-file read would not fit the model.`);
|
|
61
|
+
}
|
|
62
|
+
return resolved;
|
|
63
|
+
}
|
|
64
|
+
//# sourceMappingURL=budget.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"budget.js","sourceRoot":"","sources":["../src/budget.ts"],"names":[],"mappings":"AAEA,wEAAwE;AACxE,MAAM,CAAC,MAAM,cAAc,GAAW;IACpC,aAAa,EAAE,SAAS;IACxB,eAAe,EAAE,UAAU;IAC3B,YAAY,EAAE,OAAO;IACrB,cAAc,EAAE,MAAM;IACtB,cAAc,EAAE,OAAO;IACvB,YAAY,EAAE,EAAE;IAChB,oBAAoB,EAAE,MAAM;CAC7B,CAAC;AAKF,MAAM,MAAM,GAAG,MAAM,CAAC,IAAI,CAAC,cAAc,CAAwB,CAAC;AAElE,kGAAkG;AAClG,MAAM,UAAU,WAAW,CAAC,MAAmC;IAC7D,MAAM,KAAK,GAAoB,EAAE,CAAC;IAClC,KAAK,MAAM,KAAK,IAAI,MAAM,EAAE,CAAC;QAC3B,MAAM,KAAK,GAAG,MAAM,EAAE,CAAC,KAAK,CAAC,CAAC;QAC9B,IAAI,KAAK,KAAK,SAAS;YAAE,SAAS;QAClC,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,KAAK,IAAI,CAAC,EAAE,CAAC;YACvE,MAAM,IAAI,KAAK,CAAC,UAAU,KAAK,qCAAqC,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;QACxF,CAAC;QACD,KAAK,CAAC,KAAK,CAAC,GAAG,KAAK,CAAC;IACvB,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC;AAED,MAAM,KAAK,GAAG,CAAC,KAAa,EAAE,EAAE,CAAC,KAAK,CAAC,cAAc,CAAC,OAAO,CAAC,CAA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d.ts
CHANGED
|
@@ -14,6 +14,10 @@ export declare function documentVersion(meta: DocumentMeta): string;
|
|
|
14
14
|
* first. Bounded by the memory the documents take, not by their number: six small files and
|
|
15
15
|
* six 50 MB files are not the same load.
|
|
16
16
|
*
|
|
17
|
+
* An entry can belong to another one, as a decoded piece of a document's text belongs to that
|
|
18
|
+
* document: it counts toward the memory, not toward the number of documents, and it is dropped
|
|
19
|
+
* when the entry it belongs to is dropped for being one document too many.
|
|
20
|
+
*
|
|
17
21
|
* The cache never decides whether a document still exists. The caller asks the store first,
|
|
18
22
|
* every time (see `createLargeFiles`), so a document deleted or expired on another server is
|
|
19
23
|
* not served from here.
|
|
@@ -23,6 +27,8 @@ export declare class DocumentCache<T extends Sized> {
|
|
|
23
27
|
/** An optional cap on the number of documents too (the `cacheSize` of 0.3.x). */
|
|
24
28
|
private readonly maxEntries;
|
|
25
29
|
private readonly entries;
|
|
30
|
+
/** Entries that belong to no other: the documents. */
|
|
31
|
+
private documents;
|
|
26
32
|
private total;
|
|
27
33
|
constructor(maxBytes: number,
|
|
28
34
|
/** An optional cap on the number of documents too (the `cacheSize` of 0.3.x). */
|
|
@@ -31,8 +37,11 @@ export declare class DocumentCache<T extends Sized> {
|
|
|
31
37
|
get bytes(): number;
|
|
32
38
|
get size(): number;
|
|
33
39
|
get(id: string, version: string): T | undefined;
|
|
34
|
-
/**
|
|
35
|
-
|
|
40
|
+
/**
|
|
41
|
+
* Keeps a document. One larger than the whole cache is not kept: it is used for this call only.
|
|
42
|
+
* `owner` is the key of the entry this one belongs to (a piece of that document's text).
|
|
43
|
+
*/
|
|
44
|
+
set(id: string, version: string, value: T, owner?: string): void;
|
|
36
45
|
delete(id: string): void;
|
|
37
46
|
/** Drops every entry whose key starts with `prefix`: all the pieces of one document. */
|
|
38
47
|
deletePrefix(prefix: string): void;
|
package/dist/cache.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cache.d.ts","sourceRoot":"","sources":["../src/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,CAAC,EAAE,EAAE,MAAM,GAAG,IAAI;IAQxB,wFAAwF;IACxF,YAAY,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI;CAGnC"}
|
package/dist/cache.js
CHANGED
|
@@ -11,6 +11,10 @@ export function documentVersion(meta) {
|
|
|
11
11
|
* first. Bounded by the memory the documents take, not by their number: six small files and
|
|
12
12
|
* six 50 MB files are not the same load.
|
|
13
13
|
*
|
|
14
|
+
* An entry can belong to another one, as a decoded piece of a document's text belongs to that
|
|
15
|
+
* document: it counts toward the memory, not toward the number of documents, and it is dropped
|
|
16
|
+
* when the entry it belongs to is dropped for being one document too many.
|
|
17
|
+
*
|
|
14
18
|
* The cache never decides whether a document still exists. The caller asks the store first,
|
|
15
19
|
* every time (see `createLargeFiles`), so a document deleted or expired on another server is
|
|
16
20
|
* not served from here.
|
|
@@ -19,6 +23,8 @@ export class DocumentCache {
|
|
|
19
23
|
maxBytes;
|
|
20
24
|
maxEntries;
|
|
21
25
|
entries = new Map();
|
|
26
|
+
/** Entries that belong to no other: the documents. */
|
|
27
|
+
documents = 0;
|
|
22
28
|
total = 0;
|
|
23
29
|
constructor(maxBytes,
|
|
24
30
|
/** An optional cap on the number of documents too (the `cacheSize` of 0.3.x). */
|
|
@@ -46,8 +52,11 @@ export class DocumentCache {
|
|
|
46
52
|
this.entries.set(id, entry);
|
|
47
53
|
return entry.value;
|
|
48
54
|
}
|
|
49
|
-
/**
|
|
50
|
-
|
|
55
|
+
/**
|
|
56
|
+
* Keeps a document. One larger than the whole cache is not kept: it is used for this call only.
|
|
57
|
+
* `owner` is the key of the entry this one belongs to (a piece of that document's text).
|
|
58
|
+
*/
|
|
59
|
+
set(id, version, value, owner) {
|
|
51
60
|
const existing = this.entries.get(id);
|
|
52
61
|
if (existing?.value === value && existing.version === version)
|
|
53
62
|
return;
|
|
@@ -55,13 +64,31 @@ export class DocumentCache {
|
|
|
55
64
|
const bytes = Math.max(0, value.memoryBytes);
|
|
56
65
|
if (bytes > this.maxBytes || this.maxEntries < 1)
|
|
57
66
|
return;
|
|
58
|
-
this.entries.set(id, { version, value, bytes });
|
|
67
|
+
this.entries.set(id, { version, value, bytes, ...(owner !== undefined ? { owner } : {}) });
|
|
59
68
|
this.total += bytes;
|
|
60
|
-
|
|
61
|
-
|
|
69
|
+
if (owner === undefined)
|
|
70
|
+
this.documents++;
|
|
71
|
+
// One document too many: the one used longest ago goes, with what belongs to it.
|
|
72
|
+
while (this.documents > this.maxEntries) {
|
|
73
|
+
let oldest;
|
|
74
|
+
for (const [key, entry] of this.entries) {
|
|
75
|
+
if (entry.owner === undefined) {
|
|
76
|
+
oldest = key;
|
|
77
|
+
break;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
if (oldest === undefined)
|
|
81
|
+
break;
|
|
82
|
+
this.delete(oldest);
|
|
83
|
+
for (const [key, entry] of [...this.entries])
|
|
84
|
+
if (entry.owner === oldest)
|
|
85
|
+
this.delete(key);
|
|
86
|
+
}
|
|
87
|
+
// Too much memory: the entries used longest ago go, whatever they are.
|
|
88
|
+
for (const [oldest] of this.entries) {
|
|
89
|
+
if (this.total <= this.maxBytes)
|
|
62
90
|
break;
|
|
63
|
-
this.
|
|
64
|
-
this.total -= entry.bytes;
|
|
91
|
+
this.delete(oldest);
|
|
65
92
|
}
|
|
66
93
|
}
|
|
67
94
|
delete(id) {
|
|
@@ -70,6 +97,8 @@ export class DocumentCache {
|
|
|
70
97
|
return;
|
|
71
98
|
this.entries.delete(id);
|
|
72
99
|
this.total -= entry.bytes;
|
|
100
|
+
if (entry.owner === undefined)
|
|
101
|
+
this.documents--;
|
|
73
102
|
}
|
|
74
103
|
/** Drops every entry whose key starts with `prefix`: all the pieces of one document. */
|
|
75
104
|
deletePrefix(prefix) {
|
package/dist/cache.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cache.js","sourceRoot":"","sources":["../src/cache.ts"],"names":[],"mappings":"AAKA;;;;GAIG;AACH,MAAM,UAAU,e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files, a
|
|
3
|
+
* database that is down. It does not mean that the document is gone, and the same call may work a
|
|
4
|
+
* moment later, so retry it rather than ask the user to upload the file again.
|
|
5
|
+
*
|
|
6
|
+
* The message names no path and no host. The error that was caught is `cause`: log that one on
|
|
7
|
+
* the server, and show this one to a user or a model.
|
|
8
|
+
*/
|
|
9
|
+
export declare class DocumentStoreError extends Error {
|
|
10
|
+
/** The code of the system error behind it (`EIO`, `EMFILE`, `ETIMEDOUT`), when there is one. */
|
|
11
|
+
readonly code?: string;
|
|
12
|
+
/** The system call that failed (`open`, `read`), when there is one. */
|
|
13
|
+
readonly syscall?: string;
|
|
14
|
+
constructor(cause: unknown);
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* What to tell a model, or a user, about an error: its message, unless it is a system error, whose
|
|
18
|
+
* message names a path or a host of the server. Of that one only the code is passed on.
|
|
19
|
+
*/
|
|
20
|
+
export declare function safeErrorMessage(error: unknown): string;
|
|
21
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AASA;;;;;;;GAOG;AACH,qBAAa,kBAAmB,SAAQ,KAAK;IAC3C,gGAAgG;IAChG,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;IACvB,uEAAuE;IACvE,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,CAAC;gBAEd,KAAK,EAAE,OAAO;CAO3B;AAED;;;GAGG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CAKvD"}
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/** The code and system call of a Node system error (`EIO`, `open`), when the value is one. */
|
|
2
|
+
function systemError(value) {
|
|
3
|
+
const { code, syscall } = (value ?? {});
|
|
4
|
+
return {
|
|
5
|
+
...(typeof code === "string" ? { code } : {}),
|
|
6
|
+
...(typeof syscall === "string" ? { syscall } : {}),
|
|
7
|
+
};
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* The store could not be read just now: a volume that did not answer, too many open files, a
|
|
11
|
+
* database that is down. It does not mean that the document is gone, and the same call may work a
|
|
12
|
+
* moment later, so retry it rather than ask the user to upload the file again.
|
|
13
|
+
*
|
|
14
|
+
* The message names no path and no host. The error that was caught is `cause`: log that one on
|
|
15
|
+
* the server, and show this one to a user or a model.
|
|
16
|
+
*/
|
|
17
|
+
export class DocumentStoreError extends Error {
|
|
18
|
+
/** The code of the system error behind it (`EIO`, `EMFILE`, `ETIMEDOUT`), when there is one. */
|
|
19
|
+
code;
|
|
20
|
+
/** The system call that failed (`open`, `read`), when there is one. */
|
|
21
|
+
syscall;
|
|
22
|
+
constructor(cause) {
|
|
23
|
+
const { code, syscall } = systemError(cause);
|
|
24
|
+
super(`The document store could not be read${code ? ` (${code})` : ""}. Try again.`, { cause });
|
|
25
|
+
this.name = "DocumentStoreError";
|
|
26
|
+
if (code)
|
|
27
|
+
this.code = code;
|
|
28
|
+
if (syscall)
|
|
29
|
+
this.syscall = syscall;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* What to tell a model, or a user, about an error: its message, unless it is a system error, whose
|
|
34
|
+
* message names a path or a host of the server. Of that one only the code is passed on.
|
|
35
|
+
*/
|
|
36
|
+
export function safeErrorMessage(error) {
|
|
37
|
+
if (error instanceof DocumentStoreError)
|
|
38
|
+
return error.message;
|
|
39
|
+
const { code, syscall } = systemError(error);
|
|
40
|
+
if (code && syscall)
|
|
41
|
+
return `The file could not be read from storage (${code}). Try the call again.`;
|
|
42
|
+
return error instanceof Error ? error.message : String(error);
|
|
43
|
+
}
|
|
44
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,8FAA8F;AAC9F,SA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C,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;AAChE,CAAC"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { ToolCall } from "./model.js";
|
|
2
|
+
import type { ToolResult } from "./types.js";
|
|
3
|
+
/** A range of lines or pages, 1-based, both ends included. */
|
|
4
|
+
export type NumberRange = [from: number, to: number];
|
|
5
|
+
/** What one tool call showed the model of one document. */
|
|
6
|
+
export type Evidence = {
|
|
7
|
+
/** The tool that was called: "search_document", "read_document", and so on. */
|
|
8
|
+
tool: string;
|
|
9
|
+
documentId: string;
|
|
10
|
+
/** The file's name. */
|
|
11
|
+
document?: string;
|
|
12
|
+
/** The lines the model was shown, as ranges in order. */
|
|
13
|
+
lines?: NumberRange[];
|
|
14
|
+
/** The pages it looked at (`view_pages`), or the pages those lines are on, where the tool said. */
|
|
15
|
+
pages?: NumberRange[];
|
|
16
|
+
/** What was searched for, or the task of a whole-file read. */
|
|
17
|
+
query?: string;
|
|
18
|
+
/** "keywords", "exact" or "regex", for a search. */
|
|
19
|
+
mode?: string;
|
|
20
|
+
/** For an exact or regex search: how many lines match in the whole file. More than were shown, often. */
|
|
21
|
+
totalMatchingLines?: number;
|
|
22
|
+
/** True for a whole-file read: other model calls read every line, and the model saw their answer. */
|
|
23
|
+
whole?: boolean;
|
|
24
|
+
};
|
|
25
|
+
/** Ranges in order, with those that overlap or touch made one. */
|
|
26
|
+
export declare function mergeRanges(ranges: NumberRange[]): NumberRange[];
|
|
27
|
+
/**
|
|
28
|
+
* What a tool call showed of a document, read from the call and its result. Undefined for a call
|
|
29
|
+
* that failed, named no document, or was answered with a stand-in for a dropped result.
|
|
30
|
+
*/
|
|
31
|
+
export declare function evidenceOf(call: ToolCall, result: ToolResult): Evidence | undefined;
|
|
32
|
+
/**
|
|
33
|
+
* A short stand-in for a tool result that is dropped from the request to make room: it keeps
|
|
34
|
+
* which document it was about and which lines or pages it showed, so the model can read them
|
|
35
|
+
* again, and says that it is a stand-in. Undefined when there is nothing to gain: the result is an
|
|
36
|
+
* error, is small already, or is such a stand-in. `hadFiles` is true for a result whose files
|
|
37
|
+
* (pages) are dropped with it, however small its JSON is.
|
|
38
|
+
*/
|
|
39
|
+
export declare function stubResult(call: ToolCall, result: ToolResult, hadFiles?: boolean): ToolResult | undefined;
|
|
40
|
+
//# sourceMappingURL=evidence.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"evidence.d.ts","sourceRoot":"","sources":["../src/evidence.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAE3C,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAE7C,8DAA8D;AAC9D,MAAM,MAAM,WAAW,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,CAAC,CAAC;AAErD,2DAA2D;AAC3D,MAAM,MAAM,QAAQ,GAAG;IACrB,+EAA+E;IAC/E,IAAI,EAAE,MAAM,CAAC;IACb,UAAU,EAAE,MAAM,CAAC;IACnB,uBAAuB;IACvB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,yDAAyD;IACzD,KAAK,CAAC,EAAE,WAAW,EAAE,CAAC;IACtB,mGAAmG;IACnG,KAAK,CAAC,EAAE,WAAW,EAAE,CAAC;IACtB,+DAA+D;IAC/D,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,oDAAoD;IACpD,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,yGAAyG;IACzG,kBAAkB,CAAC,EAAE,MAAM,CAAC;IAC5B,qGAAqG;IACrG,KAAK,CAAC,EAAE,OAAO,CAAC;CACjB,CAAC;AAaF,kEAAkE;AAClE,wBAAgB,WAAW,CAAC,MAAM,EAAE,WAAW,EAAE,GAAG,WAAW,EAAE,CAShE;AAID;;;GAGG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,QAAQ,EAAE,MAAM,EAAE,UAAU,GAAG,QAAQ,GAAG,SAAS,CAyCnF;AAUD;;;;;;GAMG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,QAAQ,EAAE,MAAM,EAAE,UAAU,EAAE,QAAQ,UAAQ,GAAG,UAAU,GAAG,SAAS,CA4CvG"}
|
package/dist/evidence.js
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { TOOL_NAMES } from "./tools.js";
|
|
2
|
+
/** "12-14", "12" or 12 as a range. */
|
|
3
|
+
function rangeOf(value) {
|
|
4
|
+
if (typeof value === "number" && Number.isFinite(value))
|
|
5
|
+
return [value, value];
|
|
6
|
+
if (typeof value !== "string")
|
|
7
|
+
return undefined;
|
|
8
|
+
const match = /^(\d+)(?:-(\d+))?$/.exec(value.trim());
|
|
9
|
+
if (!match)
|
|
10
|
+
return undefined;
|
|
11
|
+
const from = Number(match[1]);
|
|
12
|
+
const to = match[2] === undefined ? from : Number(match[2]);
|
|
13
|
+
return to >= from ? [from, to] : undefined;
|
|
14
|
+
}
|
|
15
|
+
/** Ranges in order, with those that overlap or touch made one. */
|
|
16
|
+
export function mergeRanges(ranges) {
|
|
17
|
+
const sorted = [...ranges].sort((left, right) => left[0] - right[0] || left[1] - right[1]);
|
|
18
|
+
const merged = [];
|
|
19
|
+
for (const [from, to] of sorted) {
|
|
20
|
+
const last = merged.at(-1);
|
|
21
|
+
if (last && from <= last[1] + 1)
|
|
22
|
+
last[1] = Math.max(last[1], to);
|
|
23
|
+
else
|
|
24
|
+
merged.push([from, to]);
|
|
25
|
+
}
|
|
26
|
+
return merged;
|
|
27
|
+
}
|
|
28
|
+
const defined = (values) => values.filter((value) => value !== undefined);
|
|
29
|
+
/**
|
|
30
|
+
* What a tool call showed of a document, read from the call and its result. Undefined for a call
|
|
31
|
+
* that failed, named no document, or was answered with a stand-in for a dropped result.
|
|
32
|
+
*/
|
|
33
|
+
export function evidenceOf(call, result) {
|
|
34
|
+
const documentId = call.args.document_id;
|
|
35
|
+
if (typeof documentId !== "string" || !documentId || typeof result.error === "string" || result.dropped === true)
|
|
36
|
+
return undefined;
|
|
37
|
+
const entry = { tool: call.name, documentId, ...(typeof result.document === "string" ? { document: result.document } : {}) };
|
|
38
|
+
const withRanges = (lines, pages) => ({
|
|
39
|
+
...entry,
|
|
40
|
+
...(lines.length > 0 ? { lines: mergeRanges(lines) } : {}),
|
|
41
|
+
...(pages.length > 0 ? { pages: mergeRanges(pages) } : {}),
|
|
42
|
+
});
|
|
43
|
+
switch (call.name) {
|
|
44
|
+
case TOOL_NAMES.read:
|
|
45
|
+
return withRanges(defined([rangeOf(result.lines)]), defined([rangeOf(result.pages)]));
|
|
46
|
+
case TOOL_NAMES.search: {
|
|
47
|
+
const query = typeof call.args.query === "string" ? { query: call.args.query } : {};
|
|
48
|
+
const mode = typeof result.mode === "string" ? { mode: result.mode } : {};
|
|
49
|
+
if (Array.isArray(result.passages)) {
|
|
50
|
+
const passages = result.passages;
|
|
51
|
+
return { ...withRanges(defined(passages.map((passage) => rangeOf(passage.lines))), defined(passages.map((passage) => rangeOf(passage.pages)))), ...query, ...mode };
|
|
52
|
+
}
|
|
53
|
+
const matches = (Array.isArray(result.matches) ? result.matches : []);
|
|
54
|
+
const count = (value) => (Array.isArray(value) ? value.length : 0);
|
|
55
|
+
const lines = defined(matches.map((match) => (typeof match.line === "number" ? [Math.max(1, match.line - count(match.before)), match.line + count(match.after)] : undefined)));
|
|
56
|
+
return {
|
|
57
|
+
...withRanges(lines, defined(matches.map((match) => rangeOf(match.page)))),
|
|
58
|
+
...query,
|
|
59
|
+
...mode,
|
|
60
|
+
...(typeof result.total_matching_lines === "number" ? { totalMatchingLines: result.total_matching_lines } : {}),
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
case TOOL_NAMES.overview: {
|
|
64
|
+
const total = typeof result.lines === "number" ? result.lines : 0;
|
|
65
|
+
if (total < 1)
|
|
66
|
+
return entry;
|
|
67
|
+
return withRanges(total > 30 ? [[1, 30], [Math.max(1, total - 19), total]] : [[1, total]], []);
|
|
68
|
+
}
|
|
69
|
+
case TOOL_NAMES.analyze:
|
|
70
|
+
return { ...entry, whole: true, ...(typeof call.args.task === "string" ? { query: call.args.task } : {}) };
|
|
71
|
+
case TOOL_NAMES.view:
|
|
72
|
+
return withRanges([], defined([rangeOf(result.pages)]));
|
|
73
|
+
default:
|
|
74
|
+
return entry;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
/** Below this a result is left as it is: a stand-in would save next to nothing. */
|
|
78
|
+
const SMALL_RESULT_CHARACTERS = 700;
|
|
79
|
+
/** What is kept of the answer of a whole-file read, which cannot be asked for again. */
|
|
80
|
+
const KEPT_ANSWER_CHARACTERS = 6_000;
|
|
81
|
+
const MOST_RANGES = 60;
|
|
82
|
+
const rangeText = ([from, to]) => (to > from ? `${from}-${to}` : String(from));
|
|
83
|
+
/**
|
|
84
|
+
* A short stand-in for a tool result that is dropped from the request to make room: it keeps
|
|
85
|
+
* which document it was about and which lines or pages it showed, so the model can read them
|
|
86
|
+
* again, and says that it is a stand-in. Undefined when there is nothing to gain: the result is an
|
|
87
|
+
* error, is small already, or is such a stand-in. `hadFiles` is true for a result whose files
|
|
88
|
+
* (pages) are dropped with it, however small its JSON is.
|
|
89
|
+
*/
|
|
90
|
+
export function stubResult(call, result, hadFiles = false) {
|
|
91
|
+
if (typeof result.error === "string" || result.dropped === true)
|
|
92
|
+
return undefined;
|
|
93
|
+
if (!hadFiles && JSON.stringify(result).length < SMALL_RESULT_CHARACTERS)
|
|
94
|
+
return undefined;
|
|
95
|
+
const evidence = evidenceOf(call, result);
|
|
96
|
+
const base = { ...(typeof result.document === "string" ? { document: result.document } : {}), dropped: true };
|
|
97
|
+
const lines = evidence?.lines?.slice(0, MOST_RANGES).map(rangeText);
|
|
98
|
+
switch (call.name) {
|
|
99
|
+
case TOOL_NAMES.read:
|
|
100
|
+
return {
|
|
101
|
+
...base,
|
|
102
|
+
...(typeof result.lines === "string" ? { lines: result.lines } : {}),
|
|
103
|
+
...(typeof result.total_lines === "number" ? { total_lines: result.total_lines } : {}),
|
|
104
|
+
note: "These lines were shown to you earlier and were dropped from the conversation to make room. Read them again with read_document if you need them.",
|
|
105
|
+
};
|
|
106
|
+
case TOOL_NAMES.search:
|
|
107
|
+
return {
|
|
108
|
+
...base,
|
|
109
|
+
...(typeof result.mode === "string" ? { mode: result.mode } : {}),
|
|
110
|
+
...(typeof call.args.query === "string" ? { query: call.args.query.slice(0, 200) } : {}),
|
|
111
|
+
...(typeof result.total_matching_lines === "number" ? { total_matching_lines: result.total_matching_lines } : {}),
|
|
112
|
+
...(lines && lines.length > 0 ? { lines_shown: lines } : {}),
|
|
113
|
+
note: "The results of this search were shown to you earlier and were dropped from the conversation to make room. Read the lines listed here with read_document, or search again, if you need them.",
|
|
114
|
+
};
|
|
115
|
+
case TOOL_NAMES.analyze: {
|
|
116
|
+
const answer = typeof result.answer === "string" ? result.answer : "";
|
|
117
|
+
return {
|
|
118
|
+
...base,
|
|
119
|
+
...(typeof result.parts_read === "number" ? { parts_read: result.parts_read } : {}),
|
|
120
|
+
answer: answer.length > KEPT_ANSWER_CHARACTERS ? `${answer.slice(0, KEPT_ANSWER_CHARACTERS)}…` : answer,
|
|
121
|
+
note: "This is the start of the answer of the whole-file read. The rest was dropped from the conversation to make room.",
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
case TOOL_NAMES.view:
|
|
125
|
+
return {
|
|
126
|
+
...base,
|
|
127
|
+
...(typeof result.pages === "string" ? { pages: result.pages } : {}),
|
|
128
|
+
...(typeof result.total_pages === "number" ? { total_pages: result.total_pages } : {}),
|
|
129
|
+
note: "These pages were shown to you earlier and are no longer attached. Look at them again with view_pages if you need them.",
|
|
130
|
+
};
|
|
131
|
+
case TOOL_NAMES.overview:
|
|
132
|
+
return { ...base, note: "The overview was shown to you earlier and was dropped from the conversation to make room. Call get_document_overview again if you need it." };
|
|
133
|
+
default:
|
|
134
|
+
return { ...base, note: "This result was shown to you earlier and was dropped from the con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,CAAC,KAAK,CAAC,CAAC,EAAE,sBAAsB,CAAC,GAAG,CAAC,CAAC,CAAC,MAAM;gBACvG,IAAI,EAAE,kHAAkH;aACzH,CAAC;QACJ,CAAC;QACD,KAAK,UAAU,CAAC,IAAI;YAClB,OAAO;gBACL,GAAG,IAAI;gBACP,GAAG,CAAC,OAAO,MAAM,CAAC,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,MAAM,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;gBACpE,GAAG,CAAC,OAAO,MAAM,CAAC,WAAW,KAAK,QAAQ,CAAC,CAAC,CAAC,EAAE,WAAW,EAAE,MAAM,CAAC,WAAW,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;gBACtF,IAAI,EAAE,wHAAwH;aAC/H,CAAC;QACJ,KAAK,UAAU,CAAC,QAAQ;YACtB,OAAO,EAAE,GAAG,IAAI,EAAE,IAAI,EAAE,4IAA4I,EAAE,CAAC;QACzK;YACE,OAAO,EAAE,GAAG,IAAI,EAAE,IAAI,EAAE,0FAA0F,EAAE,CAAC;IACzH,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { ChatMessage, ModelAdapter, ToolTurn } from "../model.js";
|
|
2
|
+
import { type GeminiEndpoint } from "./endpoints.js";
|
|
3
|
+
import { type GeminiToolFilePlacement } from "./generate.js";
|
|
4
|
+
export type GeminiPart = Record<string, unknown>;
|
|
5
|
+
export type GeminiContent = {
|
|
6
|
+
role: "user" | "model";
|
|
7
|
+
parts: GeminiPart[];
|
|
8
|
+
};
|
|
9
|
+
/** What Gemini bills for one PDF page it is shown (measured: 520 to 532). */
|
|
10
|
+
export declare const GEMINI_PAGE_TOKENS = 530;
|
|
11
|
+
export type GeminiModelOptions = {
|
|
12
|
+
/** Sent with every chat request: temperature, maxOutputTokens, thinkingConfig, and so on. */
|
|
13
|
+
generationConfig?: Record<string, unknown> | undefined;
|
|
14
|
+
/**
|
|
15
|
+
* Where the pages of `view_pages` go in the request. "inside" (the default) puts the PDF in the
|
|
16
|
+
* function response itself, which Gemini 3 and 4 models read. "beside" sends it in a user turn
|
|
17
|
+
* of its own, after the function responses: use it for Gemini 2.5, which does not see a file
|
|
18
|
+
* inside a function response.
|
|
19
|
+
*/
|
|
20
|
+
toolFilePlacement?: GeminiToolFilePlacement | undefined;
|
|
21
|
+
/** generationConfig for whole-document part reads. Defaults to `{ temperature: 0.2, maxOutputTokens }`. */
|
|
22
|
+
analysisConfig?: ((maxOutputTokens: number) => Record<string, unknown>) | undefined;
|
|
23
|
+
/** The model's context window in tokens, when the budget of a question should follow it. */
|
|
24
|
+
contextTokens?: number | undefined;
|
|
25
|
+
/** The largest request body the model or your gateway takes, in bytes. */
|
|
26
|
+
maxRequestBytes?: number | undefined;
|
|
27
|
+
/** Tokens billed for one PDF page. Defaults to 530. */
|
|
28
|
+
pageTokens?: number | undefined;
|
|
29
|
+
/** Names the model in usage totals. Defaults to the model in the URL. */
|
|
30
|
+
id?: string | undefined;
|
|
31
|
+
fetch?: typeof fetch | undefined;
|
|
32
|
+
};
|
|
33
|
+
/**
|
|
34
|
+
* Gemini `contents` as messages of `askWithFiles`: text parts as text, every other part (an image,
|
|
35
|
+
* an inline PDF, a function call of an earlier question) as it is.
|
|
36
|
+
*/
|
|
37
|
+
export declare function fromGeminiContents(contents: GeminiContent[]): ChatMessage[];
|
|
38
|
+
/**
|
|
39
|
+
* The tool rounds a question returned (`AskResult.toolTurns`) as Gemini `contents`: a model turn
|
|
40
|
+
* of function calls and a user turn of function responses for each. For an application that
|
|
41
|
+
* keeps its history as `contents`: put them between the question and its answer.
|
|
42
|
+
*/
|
|
43
|
+
export declare function geminiToolTurns(turns: ToolTurn[]): GeminiContent[];
|
|
44
|
+
/**
|
|
45
|
+
* A Gemini model for `askWithFiles`. `endpoint` says where it is: `geminiApi()`, `vertexAi()`, or
|
|
46
|
+
* your own URLs and headers for a gateway. Its `explorerUrl` is not used here: an explorer is a
|
|
47
|
+
* model of its own, `gemini(geminiApi({ apiKey, model: "the-explorer" }))`.
|
|
48
|
+
*/
|
|
49
|
+
export declare function gemini(endpoint: Pick<GeminiEndpoint, "url" | "streamUrl" | "headers">, options?: GeminiModelOptions): ModelAdapter;
|
|
50
|
+
//# sourceMappingURL=adapter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"adapter.d.ts","sourceRoot":"","sources":["../../src/gemini/adapter.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,YAAY,EAAiG,QAAQ,EAAE,MAAM,aAAa,CAAC;AAGtK,OAAO,EAAkB,KAAK,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAErE,OAAO,EAAwE,KAAK,uBAAuB,EAAE,MAAM,eAAe,CAAC;AAEnI,MAAM,MAAM,UAAU,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AACjD,MAAM,MAAM,aAAa,GAAG;IAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC;IAAC,KAAK,EAAE,UAAU,EAAE,CAAA;CAAE,CAAC;AAE5E,6EAA6E;AAC7E,eAAO,MAAM,kBAAkB,MAAM,CAAC;AAEtC,MAAM,MAAM,kBAAkB,GAAG;IAC/B,6FAA6F;IAC7F,gBAAgB,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,SAAS,CAAC;IACvD;;;;;OAKG;IACH,iBAAiB,CAAC,EAAE,uBAAuB,GAAG,SAAS,CAAC;IACxD,2GAA2G;IAC3G,cAAc,CAAC,EAAE,CAAC,CAAC,eAAe,EAAE,MAAM,KAAK,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,GAAG,SAAS,CAAC;IACpF,4FAA4F;IAC5F,aAAa,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IACnC,0EAA0E;IAC1E,eAAe,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;I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