llm-large-files 0.3.6 → 0.4.1
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 +115 -0
- package/dist/cache.d.ts +49 -0
- package/dist/cache.d.ts.map +1 -0
- package/dist/cache.js +110 -0
- package/dist/cache.js.map +1 -0
- 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/gemini/chat.d.ts +5 -0
- package/dist/gemini/chat.d.ts.map +1 -1
- package/dist/gemini/chat.js +17 -1
- package/dist/gemini/chat.js.map +1 -1
- package/dist/gemini/generate.d.ts +10 -0
- package/dist/gemini/generate.d.ts.map +1 -1
- package/dist/gemini/generate.js +110 -25
- package/dist/gemini/generate.js.map +1 -1
- package/dist/gemini/index.d.ts +1 -1
- package/dist/gemini/index.d.ts.map +1 -1
- package/dist/gemini/index.js +1 -1
- package/dist/gemini/index.js.map +1 -1
- package/dist/index.d.ts +3 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -2
- package/dist/index.js.map +1 -1
- package/dist/library.d.ts +48 -2
- package/dist/library.d.ts.map +1 -1
- package/dist/library.js +192 -52
- package/dist/library.js.map +1 -1
- package/dist/limiter.d.ts +20 -0
- package/dist/limiter.d.ts.map +1 -0
- package/dist/limiter.js +55 -0
- package/dist/limiter.js.map +1 -0
- package/dist/map-reduce.d.ts +14 -4
- package/dist/map-reduce.d.ts.map +1 -1
- package/dist/map-reduce.js +99 -33
- package/dist/map-reduce.js.map +1 -1
- package/dist/plan.d.ts +25 -7
- package/dist/plan.d.ts.map +1 -1
- package/dist/plan.js +51 -13
- package/dist/plan.js.map +1 -1
- package/dist/ranged.d.ts +102 -0
- package/dist/ranged.d.ts.map +1 -0
- package/dist/ranged.js +321 -0
- package/dist/ranged.js.map +1 -0
- package/dist/search.d.ts +93 -15
- package/dist/search.d.ts.map +1 -1
- package/dist/search.js +472 -131
- package/dist/search.js.map +1 -1
- package/dist/stores.d.ts +26 -2
- package/dist/stores.d.ts.map +1 -1
- package/dist/stores.js +181 -21
- package/dist/stores.js.map +1 -1
- package/dist/text-index.d.ts +64 -0
- package/dist/text-index.d.ts.map +1 -0
- package/dist/text-index.js +211 -0
- package/dist/text-index.js.map +1 -0
- package/dist/text.d.ts +14 -2
- package/dist/text.d.ts.map +1 -1
- package/dist/text.js +50 -16
- package/dist/text.js.map +1 -1
- package/dist/tools.d.ts +14 -2
- package/dist/tools.d.ts.map +1 -1
- package/dist/tools.js +53 -21
- package/dist/tools.js.map +1 -1
- package/dist/types.d.ts +20 -0
- package/dist/types.d.ts.map +1 -1
- package/docs/api.md +24 -10
- package/docs/configuration.md +50 -3
- package/docs/getting-started.md +2 -0
- package/docs/how-it-works.md +11 -3
- package/docs/security.md +21 -3
- package/package.json +7 -3
- package/src/cache.ts +107 -0
- package/src/errors.ts +42 -0
- package/src/gemini/chat.ts +22 -1
- package/src/gemini/generate.ts +107 -25
- package/src/gemini/index.ts +1 -1
- package/src/index.ts +3 -2
- package/src/library.ts +229 -50
- package/src/limiter.ts +51 -0
- package/src/map-reduce.ts +101 -33
- package/src/plan.ts +54 -14
- package/src/ranged.ts +355 -0
- package/src/search.ts +524 -121
- package/src/stores.ts +178 -23
- package/src/text-index.ts +228 -0
- package/src/text.ts +46 -13
- package/src/tools.ts +61 -23
- package/src/types.ts +20 -0
package/dist/map-reduce.js
CHANGED
|
@@ -1,31 +1,80 @@
|
|
|
1
1
|
import { estimateTokens, jsonBytes } from "./text.js";
|
|
2
|
+
import { numberLines, scanSource, SearchableDocument } from "./search.js";
|
|
2
3
|
const NOTHING = "NOTHING RELEVANT";
|
|
3
4
|
const PART_OUTPUT_TOKENS = 4_096;
|
|
4
5
|
const FINAL_OUTPUT_TOKENS = 8_192;
|
|
5
|
-
/**
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
6
|
+
/**
|
|
7
|
+
* Whether a part's reply says that the part holds nothing for the task: the sentinel, whatever
|
|
8
|
+
* Markdown or punctuation a model puts around it (`**NOTHING RELEVANT**`). A reply that starts
|
|
9
|
+
* that way and then cites a line did find something, and is kept.
|
|
10
|
+
*/
|
|
11
|
+
export function saysNothingRelevant(reply) {
|
|
12
|
+
const text = reply.replace(/^[^\p{L}\p{N}]+/u, "");
|
|
13
|
+
return text.toUpperCase().startsWith(NOTHING) && !/\(L\d+/.test(text.slice(NOTHING.length));
|
|
14
|
+
}
|
|
15
|
+
/** Splits a text, fed a piece at a time, into line ranges that each fit one model request. */
|
|
16
|
+
class PartSplitter {
|
|
17
|
+
maxTokens;
|
|
18
|
+
maxBytes;
|
|
19
|
+
parts = [];
|
|
20
|
+
from = 1;
|
|
21
|
+
tokens = 0;
|
|
22
|
+
bytes = 0;
|
|
23
|
+
constructor(budget) {
|
|
24
|
+
this.maxTokens = budget.mapChunkTokens;
|
|
25
|
+
this.maxBytes = Math.floor(budget.maxRequestBytes * 0.8);
|
|
26
|
+
}
|
|
27
|
+
add(chunk) {
|
|
28
|
+
for (let local = 1; local <= chunk.lines.lineCount; local++) {
|
|
29
|
+
const line = chunk.firstLine + local - 1;
|
|
30
|
+
// Count the "123| " prefix each line carries in the prompt.
|
|
31
|
+
const text = `${line}| ${chunk.lines.line(local)}\n`;
|
|
32
|
+
const lineTokens = estimateTokens(text);
|
|
33
|
+
const lineBytes = jsonBytes(text);
|
|
34
|
+
if (line > this.from && (this.tokens + lineTokens > this.maxTokens || this.bytes + lineBytes > this.maxBytes)) {
|
|
35
|
+
this.parts.push({ from: this.from, to: line - 1 });
|
|
36
|
+
this.from = line;
|
|
37
|
+
this.tokens = 0;
|
|
38
|
+
this.bytes = 0;
|
|
39
|
+
}
|
|
40
|
+
this.tokens += lineTokens;
|
|
41
|
+
this.bytes += lineBytes;
|
|
23
42
|
}
|
|
24
|
-
tokens += lineTokens;
|
|
25
|
-
bytes += lineBytes;
|
|
26
43
|
}
|
|
27
|
-
|
|
28
|
-
|
|
44
|
+
finish(lineCount) {
|
|
45
|
+
if (lineCount >= this.from)
|
|
46
|
+
this.parts.push({ from: this.from, to: lineCount });
|
|
47
|
+
return this.parts;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
/** Splits a document held in memory into line ranges that each fit one model request. */
|
|
51
|
+
export function splitIntoParts(document, budget) {
|
|
52
|
+
const splitter = new PartSplitter(budget);
|
|
53
|
+
splitter.add(document.wholeChunk());
|
|
54
|
+
return splitter.finish(document.lines.lineCount);
|
|
55
|
+
}
|
|
56
|
+
/** Parts already worked out, by document and budget: counting them reads the whole text. */
|
|
57
|
+
const knownParts = new WeakMap();
|
|
58
|
+
/** `splitIntoParts` for any document, worked out once per document object and budget. */
|
|
59
|
+
export async function partsOf(source, budget, signal) {
|
|
60
|
+
const key = `${budget.mapChunkTokens}|${budget.maxRequestBytes}`;
|
|
61
|
+
const known = knownParts.get(source)?.get(key);
|
|
62
|
+
if (known)
|
|
63
|
+
return known;
|
|
64
|
+
let parts;
|
|
65
|
+
if (source instanceof SearchableDocument) {
|
|
66
|
+
parts = splitIntoParts(source, budget);
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
const splitter = new PartSplitter(budget);
|
|
70
|
+
await scanSource(source, (chunk) => {
|
|
71
|
+
splitter.add(chunk);
|
|
72
|
+
}, signal);
|
|
73
|
+
parts = splitter.finish(source.lineCount);
|
|
74
|
+
}
|
|
75
|
+
if (!knownParts.has(source))
|
|
76
|
+
knownParts.set(source, new Map());
|
|
77
|
+
knownParts.get(source).set(key, parts);
|
|
29
78
|
return parts;
|
|
30
79
|
}
|
|
31
80
|
async function pool(count, concurrency, run, signal) {
|
|
@@ -67,7 +116,7 @@ async function ask(generate, request, signal) {
|
|
|
67
116
|
export async function analyzeWholeDocument(options) {
|
|
68
117
|
const { document, task, generate, budget, onProgress, signal } = options;
|
|
69
118
|
const name = document.meta.name;
|
|
70
|
-
const parts =
|
|
119
|
+
const parts = await partsOf(document, budget, signal);
|
|
71
120
|
if (parts.length > budget.maxMapChunks) {
|
|
72
121
|
throw new Error(`Reading all of ${name} needs ${parts.length} model calls, over the limit of ${budget.maxMapChunks}. Use search_document to narrow the question first.`);
|
|
73
122
|
}
|
|
@@ -80,6 +129,7 @@ export async function analyzeWholeDocument(options) {
|
|
|
80
129
|
let text;
|
|
81
130
|
let read = true;
|
|
82
131
|
try {
|
|
132
|
+
const part = numberLines(await document.readLines(from, to), from);
|
|
83
133
|
const reply = await ask(generate, {
|
|
84
134
|
system: "You read one part of a larger document and take notes for a later step that combines the notes from every part.",
|
|
85
135
|
prompt: [
|
|
@@ -89,7 +139,7 @@ export async function analyzeWholeDocument(options) {
|
|
|
89
139
|
`Write concise notes on everything in this part that is relevant to the task. Keep exact names, numbers, and quotes that matter. Cite line numbers like (L123). If nothing in this part is relevant, reply exactly: ${NOTHING}`,
|
|
90
140
|
"",
|
|
91
141
|
"<part>",
|
|
92
|
-
|
|
142
|
+
part,
|
|
93
143
|
"</part>",
|
|
94
144
|
].join("\n"),
|
|
95
145
|
maxOutputTokens: PART_OUTPUT_TOKENS,
|
|
@@ -116,7 +166,7 @@ export async function analyzeWholeDocument(options) {
|
|
|
116
166
|
// Nothing was read at all: an answer written from here would be invented.
|
|
117
167
|
if (failed === parts.length)
|
|
118
168
|
throw new Error(`Could not read any part of ${name}: ${firstFailure}`);
|
|
119
|
-
const relevant = notes.filter((note) => !note.text
|
|
169
|
+
const relevant = notes.filter((note) => !saysNothingRelevant(note.text));
|
|
120
170
|
const relevantParts = relevant.filter((note) => note.read).length;
|
|
121
171
|
if (relevantParts === 0) {
|
|
122
172
|
return {
|
|
@@ -158,18 +208,34 @@ export async function analyzeWholeDocument(options) {
|
|
|
158
208
|
batches.push(layer.slice(index, index + 2));
|
|
159
209
|
}
|
|
160
210
|
let combined = 0;
|
|
211
|
+
let unmerged = 0;
|
|
161
212
|
layer = await pool(batches.length, options.concurrency ?? 4, async (index) => {
|
|
162
|
-
const
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
213
|
+
const batch = batches[index].join("\n\n");
|
|
214
|
+
let merged;
|
|
215
|
+
try {
|
|
216
|
+
const reply = await ask(generate, {
|
|
217
|
+
system: "You merge partial notes about a document into shorter notes, keeping every relevant fact and line citation.",
|
|
218
|
+
prompt: `Task: ${task}\n\nMerge these notes from ${name} into one set of notes. Keep line citations.\n\n${batch}`,
|
|
219
|
+
maxOutputTokens: PART_OUTPUT_TOKENS,
|
|
220
|
+
}, signal);
|
|
221
|
+
if (reply.truncated)
|
|
222
|
+
truncatedMerges++;
|
|
223
|
+
merged = reply.truncated ? `${reply.text}\n[These merged notes were cut off at the output limit.]` : reply.text;
|
|
224
|
+
}
|
|
225
|
+
catch (error) {
|
|
226
|
+
if (signal?.aborted)
|
|
227
|
+
throw error;
|
|
228
|
+
// A merge that cannot be made loses nothing: the notes of every part were paid for, and go on as they are.
|
|
229
|
+
unmerged++;
|
|
230
|
+
merged = batch;
|
|
231
|
+
}
|
|
169
232
|
combined++;
|
|
170
233
|
onProgress?.({ kind: "reduce", message: `Combining notes on ${name} (round ${round}): ${combined} of ${batches.length}`, done: combined, total: batches.length });
|
|
171
|
-
return
|
|
234
|
+
return merged;
|
|
172
235
|
}, signal);
|
|
236
|
+
// Notes that would not merge will not merge in another round either: the answer is written from what there is.
|
|
237
|
+
if (unmerged > 0)
|
|
238
|
+
break;
|
|
173
239
|
}
|
|
174
240
|
onProgress?.({ kind: "reduce", message: `Writing the answer from ${relevantParts} relevant parts of ${name}`, done: 0, total: 1 });
|
|
175
241
|
const cutShort = truncatedParts + truncatedMerges;
|
package/dist/map-reduce.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"map-reduce.js","sourceRoot":"","sources":["../src/map-reduce.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;
|
|
1
|
+
{"version":3,"file":"map-reduce.js","sourceRoot":"","sources":["../src/map-reduce.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AACtD,OAAO,EAAE,WAAW,EAAE,UAAU,EAAE,kBAAkB,EAAuC,MAAM,aAAa,CAAC;AAG/G,MAAM,OAAO,GAAG,kBAAkB,CAAC;AACnC,MAAM,kBAAkB,GAAG,KAAK,CAAC;AACjC,MAAM,mBAAmB,GAAG,KAAK,CAAC;AAkBlC;;;;GAIG;AACH,MAAM,UAAU,mBAAmB,CAAC,KAAa;IAC/C,MAAM,IAAI,GAAG,KAAK,CAAC,OAAO,CAAC,kBAAkB,EAAE,EAAE,CAAC,CAAC;IACnD,OAAO,IAAI,CAAC,WAAW,EAAE,CAAC,UAAU,CAAC,OAAO,CAAC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC;AAC9F,CAAC;AAED,8FAA8F;AAC9F,MAAM,YAAY;IACC,SAAS,CAAS;IAClB,QAAQ,CAAS;IACjB,KAAK,GAAU,EAAE,CAAC;IAC3B,IAAI,GAAG,CAAC,CAAC;IACT,MAAM,GAAG,CAAC,CAAC;IACX,KAAK,GAAG,CAAC,CAAC;IAElB,YAAY,MAAc;QACxB,IAAI,CAAC,SAAS,GAAG,MAAM,CAAC,cAAc,CAAC;QACvC,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,eAAe,GAAG,GAAG,CAAC,CAAC;IAC3D,CAAC;IAED,GAAG,CAAC,KAAgB;QAClB,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,IA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,EAAE;QAChC,MAAM,EAAE,+EAA+E;QACvF,MAAM,EAAE;YACN,SAAS,IAAI,EAAE;YACf,EAAE;YACF,yBAAyB,KAAK,CAAC,MAAM,aAAa,IAAI,GAAG,MAAM,CAAC,CAAC,CAAC,KAAK,MAAM,6BAA6B,CAAC,CAAC,CAAC,EAAE,4CAA4C;YAC3J,GAAG,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,kIAAkI,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;YACzJ,yGAAyG;YACzG,EAAE;YACF,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC;SACnB,CAAC,IAAI,CAAC,IAAI,CAAC;QACZ,eAAe,EAAE,mBAAmB;KACrC,EAAE,MAAM,CAAC,CAAC;IACX,OAAO;QACL,MAAM,EAAE,KAAK,CAAC,IAAI;QAClB,KAAK,EAAE,KAAK,CAAC,MAAM;QACnB,aAAa;QACb,WAAW,EAAE,MAAM;QACnB,cAAc;QACd,eAAe;QACf,eAAe,EAAE,KAAK,CAAC,SAAS;KACjC,CAAC;AACJ,CAAC"}
|
package/dist/plan.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import type { Budget, DocumentMeta, DocumentPlan, OutlineEntry } from "./types.js";
|
|
2
|
-
import type { SearchableDocument } from "./search.js";
|
|
3
2
|
/** Bytes kept free for document cards, the tool declarations, and JSON framing. */
|
|
4
3
|
export declare const REQUEST_BYTES_MARGIN: number;
|
|
5
4
|
/**
|
|
@@ -10,19 +9,38 @@ export declare function planDocuments(documents: DocumentMeta[], budget: Budget,
|
|
|
10
9
|
tokens: number;
|
|
11
10
|
bytes: number;
|
|
12
11
|
}): DocumentPlan;
|
|
12
|
+
/**
|
|
13
|
+
* About how many parts a whole-document read of a document takes, from its metadata alone:
|
|
14
|
+
* counting them exactly means reading the whole text. In the prompt every line carries its number
|
|
15
|
+
* (`12345| `), at about 1.2 tokens a digit and one for the bar, and each line's estimate is
|
|
16
|
+
* rounded up. A part ends at the end of a line, so it is short of full by half a line on average.
|
|
17
|
+
* On logs, prose, tables and blank-heavy text this lands within a few percent of the count.
|
|
18
|
+
*/
|
|
19
|
+
export declare function estimateParts(meta: Pick<DocumentMeta, "estimatedTokens" | "lines" | "textBytes" | "jsonBytes">, budget: Budget): number;
|
|
20
|
+
/**
|
|
21
|
+
* Whether a whole-document read of a document is within `maxMapChunks`, as far as its metadata
|
|
22
|
+
* tells. The estimate runs a few percent high, so a file just over the limit by it is let through:
|
|
23
|
+
* the read counts its parts, and refuses with the reason if they are too many after all.
|
|
24
|
+
*/
|
|
25
|
+
export declare function canBeReadWhole(meta: Pick<DocumentMeta, "estimatedTokens" | "lines" | "textBytes" | "jsonBytes">, budget: Budget): boolean;
|
|
13
26
|
export declare function formatBytes(bytes: number): string;
|
|
14
27
|
/** "L12 Title" lines, indented relative to the shallowest heading. */
|
|
15
28
|
export declare function outlineLines(outline: OutlineEntry[]): string[];
|
|
16
|
-
export declare function renderInline(
|
|
29
|
+
export declare function renderInline(meta: DocumentMeta, text: string): string;
|
|
30
|
+
/** How many lines of a file its card shows. */
|
|
31
|
+
export declare const CARD_LINES = 25;
|
|
17
32
|
/**
|
|
18
|
-
* A compact stand-in for a document that is too large for the prompt. `
|
|
19
|
-
* saying that its pages can be looked at
|
|
33
|
+
* A compact stand-in for a document that is too large for the prompt. `head` is its first
|
|
34
|
+
* `CARD_LINES` lines, numbered. `viewable` adds a line saying that its pages can be looked at
|
|
35
|
+
* with `view_pages`. `readableWhole` is false for a file that a whole-document read would refuse:
|
|
36
|
+
* the card then does not offer one.
|
|
20
37
|
*/
|
|
21
|
-
export declare function renderCard(
|
|
38
|
+
export declare function renderCard(meta: DocumentMeta, head: string, viewable?: boolean, readableWhole?: boolean): string;
|
|
22
39
|
/**
|
|
23
40
|
* The tool guide for the system prompt. `searchable` are the files the model reads through the
|
|
24
41
|
* text tools. `viewable` are the PDFs whose pages it can look at with `view_pages`: some of them
|
|
25
|
-
* may be searchable, and some may have their whole text in the prompt.
|
|
42
|
+
* may be searchable, and some may have their whole text in the prompt. `wholeReads` is false when
|
|
43
|
+
* every searchable file is too large for `analyze_whole_document`: the guide then leaves it out.
|
|
26
44
|
*/
|
|
27
|
-
export declare function documentToolsInstruction(searchable: DocumentMeta[], viewable?: DocumentMeta[]): string;
|
|
45
|
+
export declare function documentToolsInstruction(searchable: DocumentMeta[], viewable?: DocumentMeta[], wholeReads?: boolean): string;
|
|
28
46
|
//# sourceMappingURL=plan.d.ts.map
|
package/dist/plan.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plan.d.ts","sourceRoot":"","sources":["../src/plan.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,YAAY,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;
|
|
1
|
+
{"version":3,"file":"plan.d.ts","sourceRoot":"","sources":["../src/plan.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,YAAY,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAEnF,mFAAmF;AACnF,eAAO,MAAM,oBAAoB,QAAa,CAAC;AAI/C;;;GAGG;AACH,wBAAgB,aAAa,CAC3B,SAAS,EAAE,YAAY,EAAE,EACzB,MAAM,EAAE,MAAM,EACd,IAAI,EAAE;IAAE,MAAM,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,GACtC,YAAY,CAed;AASD;;;;;;GAMG;AACH,wBAAgB,aAAa,CAAC,IAAI,EAAE,IAAI,CAAC,YAAY,EAAE,iBAAiB,GAAG,OAAO,GAAG,WAAW,GAAG,WAAW,CAAC,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CAQvI;AAED;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,IAAI,CAAC,YAAY,EAAE,iBAAiB,GAAG,OAAO,GAAG,WAAW,GAAG,WAAW,CAAC,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAIzI;AAQD,wBAAgB,WAAW,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAIjD;AAED,sEAAsE;AACtE,wBAAgB,YAAY,CAAC,OAAO,EAAE,YAAY,EAAE,GAAG,MAAM,EAAE,CAI9D;AAED,wBAAgB,YAAY,CAAC,IAAI,EAAE,YAAY,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,CAErE;AAED,+CAA+C;AAC/C,eAAO,MAAM,UAAU,KAAkB,CAAC;AAE1C;;;;;GAKG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,YAAY,EAAE,IAAI,EAAE,MAAM,EAAE,QAAQ,UAAQ,EAAE,aAAa,UAAO,GAAG,MAAM,CA2B3G;AAED;;;;;GAKG;AACH,wBAAgB,wBAAwB,CAAC,UAAU,EAAE,YAAY,EAAE,EAAE,QAAQ,GAAE,YAAY,EAAO,EAAE,UAAU,UAAO,GAAG,MAAM,CAyB7H"}
|
package/dist/plan.js
CHANGED
|
@@ -11,7 +11,7 @@ export function planDocuments(documents, budget, base) {
|
|
|
11
11
|
const byteRoom = budget.maxRequestBytes - REQUEST_BYTES_MARGIN - base.bytes;
|
|
12
12
|
const plan = { inline: [], searchable: [], inlineTokens: 0, inlineBytes: 0 };
|
|
13
13
|
for (const meta of [...documents].sort((left, right) => left.estimatedTokens - right.estimatedTokens)) {
|
|
14
|
-
const bytes = Math.ceil(meta.textBytes * 1.08) + 200;
|
|
14
|
+
const bytes = (meta.jsonBytes ?? Math.ceil(meta.textBytes * 1.08)) + 200;
|
|
15
15
|
if (plan.inlineTokens + meta.estimatedTokens <= tokenRoom && plan.inlineBytes + bytes <= byteRoom) {
|
|
16
16
|
plan.inline.push(meta);
|
|
17
17
|
plan.inlineTokens += meta.estimatedTokens;
|
|
@@ -23,6 +23,39 @@ export function planDocuments(documents, budget, base) {
|
|
|
23
23
|
}
|
|
24
24
|
return plan;
|
|
25
25
|
}
|
|
26
|
+
/** The digits of the numbers 1 to `count`, written out: what the line numbers of a text take. */
|
|
27
|
+
function digitsUpTo(count) {
|
|
28
|
+
let digits = 0;
|
|
29
|
+
for (let width = 1, first = 1; first <= count; width++, first *= 10)
|
|
30
|
+
digits += (Math.min(count, first * 10 - 1) - first + 1) * width;
|
|
31
|
+
return digits;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* About how many parts a whole-document read of a document takes, from its metadata alone:
|
|
35
|
+
* counting them exactly means reading the whole text. In the prompt every line carries its number
|
|
36
|
+
* (`12345| `), at about 1.2 tokens a digit and one for the bar, and each line's estimate is
|
|
37
|
+
* rounded up. A part ends at the end of a line, so it is short of full by half a line on average.
|
|
38
|
+
* On logs, prose, tables and blank-heavy text this lands within a few percent of the count.
|
|
39
|
+
*/
|
|
40
|
+
export function estimateParts(meta, budget) {
|
|
41
|
+
const lines = Math.max(1, meta.lines);
|
|
42
|
+
const digits = digitsUpTo(lines);
|
|
43
|
+
const tokens = meta.estimatedTokens + 1.2 * digits + 1.5 * lines;
|
|
44
|
+
const bytes = (meta.jsonBytes ?? meta.textBytes * 1.08) + digits + 4 * lines;
|
|
45
|
+
const room = (limit, size) => Math.max(1, limit - size / lines / 2);
|
|
46
|
+
const maxBytes = Math.floor(budget.maxRequestBytes * 0.8);
|
|
47
|
+
return Math.max(1, Math.ceil(Math.max(tokens / room(budget.mapChunkTokens, tokens), bytes / room(maxBytes, bytes))));
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Whether a whole-document read of a document is within `maxMapChunks`, as far as its metadata
|
|
51
|
+
* tells. The estimate runs a few percent high, so a file just over the limit by it is let through:
|
|
52
|
+
* the read counts its parts, and refuses with the reason if they are too many after all.
|
|
53
|
+
*/
|
|
54
|
+
export function canBeReadWhole(meta, budget) {
|
|
55
|
+
const parts = estimateParts(meta, budget);
|
|
56
|
+
// Metadata that does not say (a record of a document that an application kept in part): not refused here.
|
|
57
|
+
return !Number.isFinite(parts) || parts <= budget.maxMapChunks * 1.05;
|
|
58
|
+
}
|
|
26
59
|
function formatCount(value) {
|
|
27
60
|
if (value >= 1_000_000)
|
|
28
61
|
return `${(value / 1_000_000).toFixed(1)}M`;
|
|
@@ -45,21 +78,25 @@ export function outlineLines(outline) {
|
|
|
45
78
|
top = entry.level;
|
|
46
79
|
return outline.map((entry) => `L${entry.line} ${" ".repeat(entry.level - top)}${entry.title}`);
|
|
47
80
|
}
|
|
48
|
-
export function renderInline(
|
|
49
|
-
|
|
50
|
-
return `--- Attached file: ${meta.name} (${meta.format}, ${meta.lines.toLocaleString("en-US")} lines) ---\n${document.text}\n--- End attached file: ${meta.name} ---`;
|
|
81
|
+
export function renderInline(meta, text) {
|
|
82
|
+
return `--- Attached file: ${meta.name} (${meta.format}, ${meta.lines.toLocaleString("en-US")} lines) ---\n${text}\n--- End attached file: ${meta.name} ---`;
|
|
51
83
|
}
|
|
84
|
+
/** How many lines of a file its card shows. */
|
|
85
|
+
export const CARD_LINES = CARD_HEAD_LINES;
|
|
52
86
|
/**
|
|
53
|
-
* A compact stand-in for a document that is too large for the prompt. `
|
|
54
|
-
* saying that its pages can be looked at
|
|
87
|
+
* A compact stand-in for a document that is too large for the prompt. `head` is its first
|
|
88
|
+
* `CARD_LINES` lines, numbered. `viewable` adds a line saying that its pages can be looked at
|
|
89
|
+
* with `view_pages`. `readableWhole` is false for a file that a whole-document read would refuse:
|
|
90
|
+
* the card then does not offer one.
|
|
55
91
|
*/
|
|
56
|
-
export function renderCard(
|
|
57
|
-
const { meta } = document;
|
|
92
|
+
export function renderCard(meta, head, viewable = false, readableWhole = true) {
|
|
58
93
|
const out = [
|
|
59
94
|
`--- Large attached file: ${meta.name} ---`,
|
|
60
95
|
`document_id: ${meta.id}`,
|
|
61
96
|
`${meta.format} · ${formatBytes(meta.bytes)} · ${meta.lines.toLocaleString("en-US")} lines${meta.pages ? ` · ${meta.pages} pages` : ""} · about ${formatCount(meta.estimatedTokens)} tokens`,
|
|
62
|
-
|
|
97
|
+
readableWhole
|
|
98
|
+
? "This file is NOT in your context. Use the document tools to search it, read line ranges, or analyze all of it."
|
|
99
|
+
: "This file is NOT in your context. Use the document tools to search it and to read line ranges. It is too large to be analyzed whole.",
|
|
63
100
|
...(viewable ? ["Its text cannot show pictures. To see a picture, a chart or the layout of a page, look at the page with view_pages."] : []),
|
|
64
101
|
];
|
|
65
102
|
const profile = meta.logProfile;
|
|
@@ -79,21 +116,22 @@ export function renderCard(document, viewable = false) {
|
|
|
79
116
|
out.push(`Outline (${Math.min(meta.outline.length, CARD_OUTLINE_ENTRIES)} of ${meta.outline.length} headings):`);
|
|
80
117
|
out.push(...outlineLines(meta.outline.slice(0, CARD_OUTLINE_ENTRIES)).map((line) => ` ${line}`));
|
|
81
118
|
}
|
|
82
|
-
out.push(`First lines:`,
|
|
119
|
+
out.push(`First lines:`, head.split("\n").map((line) => ` ${line.slice(0, 300)}`).join("\n"));
|
|
83
120
|
out.push(`--- End large attached file: ${meta.name} ---`);
|
|
84
121
|
return out.join("\n");
|
|
85
122
|
}
|
|
86
123
|
/**
|
|
87
124
|
* The tool guide for the system prompt. `searchable` are the files the model reads through the
|
|
88
125
|
* text tools. `viewable` are the PDFs whose pages it can look at with `view_pages`: some of them
|
|
89
|
-
* may be searchable, and some may have their whole text in the prompt.
|
|
126
|
+
* may be searchable, and some may have their whole text in the prompt. `wholeReads` is false when
|
|
127
|
+
* every searchable file is too large for `analyze_whole_document`: the guide then leaves it out.
|
|
90
128
|
*/
|
|
91
|
-
export function documentToolsInstruction(searchable, viewable = []) {
|
|
129
|
+
export function documentToolsInstruction(searchable, viewable = [], wholeReads = true) {
|
|
92
130
|
// File names come from uploads. Quoting keeps a name that reads like an instruction a name.
|
|
93
131
|
const named = (meta, detail = "") => `${JSON.stringify(meta.name)} (document_id ${meta.id}${detail})`;
|
|
94
132
|
const guide = [];
|
|
95
133
|
if (searchable.length > 0) {
|
|
96
|
-
guide.push(`Some attached files are too large for your context: ${searchable.map((meta) => named(meta)).join("; ")}.`, "You see only a summary card for each. Read them through the tools:", "- search_document finds lines (exact or regex) or the most relevant passages (keywords).", "- read_document reads a line range. Read around search hits before you rely on them.", "- get_document_overview returns the outline, log profile, and the start and end of a file.", "- analyze_whole_document reads the entire file in parts. Use it only when the task needs all of it (a full summary, every occurrence, a timeline). It is slow and costly.", "You have a limited number of tool turns. Call several tools in the same turn when the searches do not depend on each other, and answer as soon as you have enough evidence.", "Base your answer on text you actually read. Cite line numbers like (app.log L1203). If the tools did not find something, say so rather than guessing.");
|
|
134
|
+
guide.push(`Some attached files are too large for your context: ${searchable.map((meta) => named(meta)).join("; ")}.`, "You see only a summary card for each. Read them through the tools:", "- search_document finds lines (exact or regex) or the most relevant passages (keywords).", "- read_document reads a line range. Read around search hits before you rely on them.", "- get_document_overview returns the outline, log profile, and the start and end of a file.", ...(wholeReads ? ["- analyze_whole_document reads the entire file in parts. Use it only when the task needs all of it (a full summary, every occurrence, a timeline). It is slow and costly."] : []), "You have a limited number of tool turns. Call several tools in the same turn when the searches do not depend on each other, and answer as soon as you have enough evidence.", "Base your answer on text you actually read. Cite line numbers like (app.log L1203). If the tools did not find something, say so rather than guessing.");
|
|
97
135
|
}
|
|
98
136
|
if (viewable.length > 0) {
|
|
99
137
|
guide.push(`The text of a PDF does not show its pictures, charts or diagrams. You can look at the real pages of these PDFs with view_pages: ${viewable.map((meta) => named(meta, meta.pages ? `, ${meta.pages} ${meta.pages === 1 ? "page" : "pages"}` : "")).join("; ")}.`, "- Use view_pages when the answer depends on a picture, photo, chart, diagram, map, handwriting, a stamp or signature, or on how a page is laid out. A page is worth a look when its text says it has a picture or has no text, or mentions a figure.", "- Page numbers are the N of the \"--- Page N ---\" lines in the text. Ask only for the pages you need: you have a limited number of tool turns.", "- Do not use view_pages to read text, and do not describe a picture you have not looked at. If you could not look at a page, say so.");
|
package/dist/plan.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plan.js","sourceRoot":"","sources":["../src/plan.ts"],"names":[],"m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AC1F,sFAAsF,EACtF,4FAA4F,EAC5F,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,2KAA2K,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,EACpM,6KAA6K,EAC7K,uJAAuJ,CACxJ,CAAC;IACJ,CAAC;IACD,IAAI,QAAQ,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QACxB,KAAK,CAAC,IAAI,CACR,mIAAmI,QAAQ,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,KAAK,CAAC,IAAI,EAAE,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,KAAK,IAAI,CAAC,KAAK,IAAI,IAAI,CAAC,KAAK,KAAK,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,EAChQ,sPAAsP,EACtP,iJAAiJ,EACjJ,sIAAsI,CACvI,CAAC;IACJ,CAAC;IACD,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AAC1B,CAAC"}
|
package/dist/ranged.d.ts
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A document read from the store a range at a time, for stores that can (`readTextRange`): a tool
|
|
3
|
+
* call reads the lines it needs, and a search reads the text in pieces, so no call holds a whole
|
|
4
|
+
* large file in memory. The line index comes from the store (`getIndex`), or is built from the
|
|
5
|
+
* text the first time an older document without one is opened, and saved for next time.
|
|
6
|
+
*/
|
|
7
|
+
import type { Limiter } from "./limiter.js";
|
|
8
|
+
import { type DocumentSource, type ScanFeed, type TextChunk } from "./search.js";
|
|
9
|
+
import { type TextIndex } from "./text-index.js";
|
|
10
|
+
import type { DocumentMeta, DocumentStore } from "./types.js";
|
|
11
|
+
export type RangeStore = Pick<DocumentStore, "getIndex" | "putIndex"> & Required<Pick<DocumentStore, "readTextRange">>;
|
|
12
|
+
export type ScanSettings = {
|
|
13
|
+
/** About this many bytes of text are read and searched at a time. */
|
|
14
|
+
chunkBytes: number;
|
|
15
|
+
/** Bounds how many scans of whole documents run at once. */
|
|
16
|
+
limiter: Limiter;
|
|
17
|
+
/** Told when the stored text of a document did not agree with its index. */
|
|
18
|
+
onBroken?: (id: string) => void;
|
|
19
|
+
/**
|
|
20
|
+
* Where decoded pieces of frequently searched documents are kept, within the cache's memory
|
|
21
|
+
* limit, so that the next search of them reads and decodes nothing. A document whose decoded
|
|
22
|
+
* text would take more than `maxDocumentBytes` is never kept: one scan of it would push out
|
|
23
|
+
* everything else.
|
|
24
|
+
*/
|
|
25
|
+
pieces?: PieceCache;
|
|
26
|
+
};
|
|
27
|
+
/** A piece of text kept in memory, and what it takes. */
|
|
28
|
+
export type CachedPiece = TextChunk & {
|
|
29
|
+
readonly memoryBytes: number;
|
|
30
|
+
};
|
|
31
|
+
export type PieceCache = {
|
|
32
|
+
get(key: string, version: string): CachedPiece | undefined;
|
|
33
|
+
set(key: string, version: string, piece: CachedPiece): void;
|
|
34
|
+
maxDocumentBytes: number;
|
|
35
|
+
};
|
|
36
|
+
/** The cache key of piece `at` of a document. All of them start with `${id}#`. */
|
|
37
|
+
export declare const pieceKey: (id: string, at: number) => string;
|
|
38
|
+
export declare class RangedDocument implements DocumentSource {
|
|
39
|
+
private readonly store;
|
|
40
|
+
readonly meta: DocumentMeta;
|
|
41
|
+
readonly index: TextIndex;
|
|
42
|
+
private readonly settings;
|
|
43
|
+
/**
|
|
44
|
+
* Set when the stored text did not agree with the index. The library then drops this object and
|
|
45
|
+
* opens the document again, building its index from the text.
|
|
46
|
+
*/
|
|
47
|
+
broken: boolean;
|
|
48
|
+
/** The version its pieces are kept under, and whether they are kept at all. */
|
|
49
|
+
private readonly pieceVersion;
|
|
50
|
+
private readonly keepPieces;
|
|
51
|
+
private ranges?;
|
|
52
|
+
constructor(store: RangeStore, meta: DocumentMeta, index: TextIndex, settings: ScanSettings);
|
|
53
|
+
get lineCount(): number;
|
|
54
|
+
get characters(): number;
|
|
55
|
+
get passageStarts(): ArrayLike<number>;
|
|
56
|
+
get memoryBytes(): number;
|
|
57
|
+
pageOfLine(line: number): number | undefined;
|
|
58
|
+
pagesOfLines(from: number, to: number): string | undefined;
|
|
59
|
+
readLines(from: number, to: number): Promise<string[]>;
|
|
60
|
+
readText(): Promise<string>;
|
|
61
|
+
/**
|
|
62
|
+
* The text in pieces of whole passages, about `chunkBytes` each. A scan waits for a slot of the
|
|
63
|
+
* limiter first, and holds it until it ends or is abandoned. `scan` does the same for several
|
|
64
|
+
* scans at once.
|
|
65
|
+
*/
|
|
66
|
+
chunks(signal?: AbortSignal): AsyncGenerator<TextChunk>;
|
|
67
|
+
/**
|
|
68
|
+
* Feeds the text, a piece at a time and in order, to `feed` until it returns true or the text
|
|
69
|
+
* ends. Scans asked for while others of this document wait for a slot of the limiter join them:
|
|
70
|
+
* the pieces are then read, decoded and lowercased once, for all of them. A scan asked for once
|
|
71
|
+
* the pass has started waits for the next one, since every scan must see the text from the start.
|
|
72
|
+
*/
|
|
73
|
+
scan(feed: ScanFeed, signal?: AbortSignal): Promise<void>;
|
|
74
|
+
/** Scans waiting for a slot, open to more of them. */
|
|
75
|
+
private gathering;
|
|
76
|
+
private pass;
|
|
77
|
+
/**
|
|
78
|
+
* The pieces, in order: kept ones from memory, the others read and decoded, the next one read
|
|
79
|
+
* while the current one is used. Line breaks are where the index says, which each piece read is
|
|
80
|
+
* checked against.
|
|
81
|
+
*/
|
|
82
|
+
private pieces;
|
|
83
|
+
/** Piece `at`, from memory when it is kept there. */
|
|
84
|
+
private piece;
|
|
85
|
+
/** The bytes of lines `first` to `last`, which must exist. */
|
|
86
|
+
private bytesOf;
|
|
87
|
+
/** Line ranges of whole passages, each about `chunkBytes` (one passage at least). */
|
|
88
|
+
private chunkRanges;
|
|
89
|
+
private read;
|
|
90
|
+
private mismatch;
|
|
91
|
+
}
|
|
92
|
+
/** Builds the index of a stored text by reading all of it once. Undefined when the text is gone. */
|
|
93
|
+
export declare function buildIndexFromStore(store: RangeStore, id: string, limiter: Limiter): Promise<TextIndex | undefined>;
|
|
94
|
+
/**
|
|
95
|
+
* Opens a document to be read by range. Uses the saved index when there is one that fits the
|
|
96
|
+
* document; otherwise builds it from the text and saves it (best effort: a store that cannot
|
|
97
|
+
* write, or another server writing the same index, changes nothing). Undefined when the text is gone.
|
|
98
|
+
*/
|
|
99
|
+
export declare function openRangedDocument(store: RangeStore, meta: DocumentMeta, settings: ScanSettings, options?: {
|
|
100
|
+
ignoreSavedIndex?: boolean;
|
|
101
|
+
}): Promise<RangedDocument | undefined>;
|
|
102
|
+
//# sourceMappingURL=ranged.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ranged.d.ts","sourceRoot":"","sources":["../src/ranged.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAC5C,OAAO,EAAuB,KAAK,cAAc,EAAE,KAAK,QAAQ,EAAE,KAAK,SAAS,EAAE,MAAM,aAAa,CAAC;AACtG,OAAO,EAAkF,KAAK,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAEjI,OAAO,KAAK,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAE9D,MAAM,MAAM,UAAU,GAAG,IAAI,CAAC,aAAa,EAAE,UAAU,GAAG,UAAU,CAAC,GAAG,QAAQ,CAAC,IAAI,CAAC,aAAa,EAAE,eAAe,CAAC,CAAC,CAAC;AAEvH,MAAM,MAAM,YAAY,GAAG;IACzB,qEAAqE;IACrE,UAAU,EAAE,MAAM,CAAC;IACnB,4DAA4D;IAC5D,OAAO,EAAE,OAAO,CAAC;IACjB,4EAA4E;IAC5E,QAAQ,CAAC,EAAE,CAAC,EAAE,EAAE,MAAM,KAAK,IAAI,CAAC;IAChC;;;;;OAKG;IACH,MAAM,CAAC,EAAE,UAAU,CAAC;CACr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