llm-large-files 0.1.0 → 0.2.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 +76 -0
- package/README.md +1 -1
- package/dist/analyze.d.ts.map +1 -1
- package/dist/analyze.js +5 -1
- package/dist/analyze.js.map +1 -1
- package/dist/extract.d.ts +17 -1
- package/dist/extract.d.ts.map +1 -1
- package/dist/extract.js +73 -23
- package/dist/extract.js.map +1 -1
- package/dist/gemini/chat.d.ts +22 -2
- package/dist/gemini/chat.d.ts.map +1 -1
- package/dist/gemini/chat.js +41 -24
- package/dist/gemini/chat.js.map +1 -1
- package/dist/gemini/generate.d.ts.map +1 -1
- package/dist/gemini/generate.js +4 -1
- package/dist/gemini/generate.js.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/library.d.ts +6 -0
- package/dist/library.d.ts.map +1 -1
- package/dist/library.js +15 -1
- package/dist/library.js.map +1 -1
- package/dist/map-reduce.d.ts +7 -0
- package/dist/map-reduce.d.ts.map +1 -1
- package/dist/map-reduce.js +58 -16
- package/dist/map-reduce.js.map +1 -1
- package/dist/pdf-layout.d.ts +32 -0
- package/dist/pdf-layout.d.ts.map +1 -0
- package/dist/pdf-layout.js +790 -0
- package/dist/pdf-layout.js.map +1 -0
- package/dist/plan.d.ts.map +1 -1
- package/dist/plan.js +6 -2
- package/dist/plan.js.map +1 -1
- package/dist/search.d.ts +8 -4
- package/dist/search.d.ts.map +1 -1
- package/dist/search.js +49 -24
- package/dist/search.js.map +1 -1
- package/dist/text.d.ts +18 -4
- package/dist/text.d.ts.map +1 -1
- package/dist/text.js +155 -15
- package/dist/text.js.map +1 -1
- package/dist/tools.d.ts.map +1 -1
- package/dist/tools.js +11 -3
- package/dist/tools.js.map +1 -1
- package/dist/types.d.ts +6 -0
- package/dist/types.d.ts.map +1 -1
- package/docs/api.md +20 -3
- package/docs/configuration.md +73 -5
- package/docs/getting-started.md +7 -4
- package/docs/how-it-works.md +26 -8
- package/docs/security.md +18 -5
- package/package.json +91 -91
- package/src/analyze.ts +3 -1
- package/src/extract.ts +77 -22
- package/src/gemini/chat.ts +60 -25
- package/src/gemini/generate.ts +3 -1
- package/src/index.ts +2 -1
- package/src/library.ts +241 -226
- package/src/map-reduce.ts +69 -16
- package/src/node/handlers.ts +122 -122
- package/src/pdf-layout.ts +771 -0
- package/src/plan.ts +100 -98
- package/src/search.ts +48 -25
- package/src/stores.ts +132 -132
- package/src/text.ts +131 -14
- package/src/tools.ts +274 -264
- package/src/types.ts +174 -168
package/src/text.ts
CHANGED
|
@@ -1,13 +1,41 @@
|
|
|
1
1
|
/** Lines longer than this are split so line-based tools always return bounded output. */
|
|
2
2
|
export const MAX_LINE_CHARACTERS = 2_000;
|
|
3
3
|
|
|
4
|
-
/**
|
|
4
|
+
/**
|
|
5
|
+
* Terminal escape sequences: CSI ("ESC [ 31 m"), OSC ("ESC ] ... BEL"), character-set selection
|
|
6
|
+
* ("ESC ( B"), and two-character escapes. Coloured logs are full of them.
|
|
7
|
+
*/
|
|
8
|
+
// eslint-disable-next-line no-control-regex
|
|
9
|
+
const ANSI_ESCAPE = /\x1b(?:\[[0-?]*[ -\/]*[@-~]|\][^\x07\x1b]*(?:\x07|\x1b\\)?|[ -\/]+[0-~]|[@-Z\\-_])/g;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Strips a BOM, unifies line endings, and removes terminal escape sequences and control characters
|
|
13
|
+
* other than tab and newline.
|
|
14
|
+
*/
|
|
5
15
|
export function normalizeText(raw: string): string {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
16
|
+
let text = raw.replace(/^/, "").replace(/\r\n?/g, "\n");
|
|
17
|
+
// Remove whole sequences first. Dropping only the ESC byte would leave "[31mERROR[0m" behind.
|
|
18
|
+
if (text.includes("\x1b")) text = text.replace(ANSI_ESCAPE, "");
|
|
19
|
+
// eslint-disable-next-line no-control-regex
|
|
20
|
+
return text.replace(/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]/g, "");
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Lowercases for case-insensitive search without changing the string's length, so an offset in the
|
|
25
|
+
* result points at the same character in the original. "İ" (U+0130) is the one character whose
|
|
26
|
+
* lowercase form is longer; it is folded to "i".
|
|
27
|
+
*/
|
|
28
|
+
export function foldCase(text: string): string {
|
|
29
|
+
const lower = (text.includes("\u0130") ? text.replaceAll("\u0130", "i") : text).toLowerCase();
|
|
30
|
+
if (lower.length === text.length) return lower;
|
|
31
|
+
// Not expected with today's Unicode data. Fold unit by unit and keep any unit that would grow.
|
|
32
|
+
let out = "";
|
|
33
|
+
for (let index = 0; index < text.length; index++) {
|
|
34
|
+
const unit = text[index]!;
|
|
35
|
+
const folded = unit.toLowerCase();
|
|
36
|
+
out += folded.length === 1 ? folded : unit;
|
|
37
|
+
}
|
|
38
|
+
return out;
|
|
11
39
|
}
|
|
12
40
|
|
|
13
41
|
/**
|
|
@@ -37,19 +65,108 @@ export function wrapLongLines(text: string, limit = MAX_LINE_CHARACTERS): { text
|
|
|
37
65
|
return { text: out.join("\n"), wrapped };
|
|
38
66
|
}
|
|
39
67
|
|
|
68
|
+
// Token cost of each kind of character, fitted to Gemini's countTokens on 60 texts (prose in
|
|
69
|
+
// eleven languages, source code, JSON, CSV, and eight log formats; Gemini 2.5, 3.5, and 3.8 count
|
|
70
|
+
// identically). On those texts the estimate is between 0.98 and 1.7 times Gemini's count.
|
|
71
|
+
const WORD = 1.1; // A run of ASCII letters.
|
|
72
|
+
const CASE_CHANGE = 1.3; // "camelCase", "HTTPServer", base64: each change starts a new piece.
|
|
73
|
+
const LONG_LETTER = 0.18; // Each letter past the 6th of a piece (the 3rd for capitals).
|
|
74
|
+
const LONG_AFTER = 6;
|
|
75
|
+
const LONG_AFTER_CAPITALS = 3;
|
|
76
|
+
const VERY_LONG_LETTER = 0.5; // Each letter past the 20th: no longer a word, but a key or a sequence.
|
|
77
|
+
const VERY_LONG_AFTER = 20;
|
|
78
|
+
const DIGIT = 1.2; // Digits are about one token each, so IDs, timestamps, and numbers are dense.
|
|
79
|
+
const PUNCTUATION = 0.85; // The first of a run of ASCII punctuation; the rest of the run is free.
|
|
80
|
+
const NEWLINE = 2.2;
|
|
81
|
+
const TAB = 1;
|
|
82
|
+
const LOOSE_SPACE = 0.2; // A space before anything but a letter. A space before a word is free.
|
|
83
|
+
const LATIN_EXTENDED = 0.6; // é, ü, ß
|
|
84
|
+
const GREEK = 0.4;
|
|
85
|
+
const CYRILLIC = 0.3;
|
|
86
|
+
const HEBREW = 0.52;
|
|
87
|
+
const ARABIC = 0.41;
|
|
88
|
+
const OTHER_SCRIPT = 0.45; // Devanagari, Thai, Georgian, and other alphabets and syllabaries.
|
|
89
|
+
const HAN = 0.7;
|
|
90
|
+
const KANA = 0.56;
|
|
91
|
+
const HANGUL = 0.7;
|
|
92
|
+
const SURROGATE = 0.5; // Half of an emoji or another character outside the basic plane.
|
|
93
|
+
const SYMBOL = 1; // Dashes, quotes, arrows, box drawing, and anything unclassified.
|
|
94
|
+
|
|
40
95
|
/**
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
96
|
+
* Estimates how many tokens a model will count for `text`, without calling the model.
|
|
97
|
+
*
|
|
98
|
+
* Characters per token varies a lot with the kind of text: about 4.5 for English prose, 2 for
|
|
99
|
+
* Hebrew, and between 1 and 2 for logs, CSV, and hex, because digits cost about a token each. A
|
|
100
|
+
* flat "4 characters per token" is therefore right for a novel and three times too low for a log.
|
|
101
|
+
* This walks the text once and prices each kind of character. It leans high, which only moves a
|
|
102
|
+
* document to the searchable path sooner. The one kind of text it still counts low is a long run
|
|
103
|
+
* of random letters in a single case with no digits (about 70% of the real count).
|
|
44
104
|
*/
|
|
45
105
|
export function estimateTokens(text: string): number {
|
|
46
|
-
let
|
|
47
|
-
|
|
106
|
+
let tokens = 0;
|
|
107
|
+
// The current run of ASCII letters: its length since the last case change, and its casing.
|
|
108
|
+
let piece = 0;
|
|
109
|
+
let capitalsInARow = 0;
|
|
110
|
+
let previousWasLower = false;
|
|
111
|
+
let previous = 0; // 1 = space, 2 = ASCII punctuation, 0 = anything else
|
|
48
112
|
for (let index = 0; index < text.length; index++) {
|
|
49
|
-
|
|
50
|
-
|
|
113
|
+
const code = text.charCodeAt(index);
|
|
114
|
+
if ((code >= 97 && code <= 122) || (code >= 65 && code <= 90)) {
|
|
115
|
+
const capital = code <= 90;
|
|
116
|
+
if (piece === 0) tokens += WORD;
|
|
117
|
+
else if ((capital && previousWasLower) || (!capital && capitalsInARow >= 2)) {
|
|
118
|
+
tokens += CASE_CHANGE;
|
|
119
|
+
piece = 0;
|
|
120
|
+
}
|
|
121
|
+
piece++;
|
|
122
|
+
if (piece > VERY_LONG_AFTER) tokens += VERY_LONG_LETTER;
|
|
123
|
+
else if (piece > (capital ? LONG_AFTER_CAPITALS : LONG_AFTER)) tokens += LONG_LETTER;
|
|
124
|
+
capitalsInARow = capital ? capitalsInARow + 1 : 0;
|
|
125
|
+
previousWasLower = !capital;
|
|
126
|
+
previous = 0;
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
piece = 0;
|
|
130
|
+
capitalsInARow = 0;
|
|
131
|
+
previousWasLower = false;
|
|
132
|
+
if (code === 32) {
|
|
133
|
+
previous = 1;
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
if (previous === 1) tokens += LOOSE_SPACE;
|
|
137
|
+
if (code < 0x80) {
|
|
138
|
+
if (code >= 48 && code <= 57) tokens += DIGIT;
|
|
139
|
+
else if (code === 10) tokens += NEWLINE;
|
|
140
|
+
else if (code === 9) tokens += TAB;
|
|
141
|
+
else {
|
|
142
|
+
if (previous !== 2) tokens += PUNCTUATION;
|
|
143
|
+
previous = 2;
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
previous = 0;
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
149
|
+
previous = 0;
|
|
150
|
+
if (code < 0xc0) tokens += SYMBOL;
|
|
151
|
+
else if (code < 0x250) tokens += LATIN_EXTENDED;
|
|
152
|
+
else if (code < 0x370) tokens += OTHER_SCRIPT;
|
|
153
|
+
else if (code < 0x400) tokens += GREEK;
|
|
154
|
+
else if (code < 0x530) tokens += CYRILLIC;
|
|
155
|
+
else if (code < 0x590) tokens += OTHER_SCRIPT;
|
|
156
|
+
else if (code < 0x600) tokens += HEBREW;
|
|
157
|
+
else if (code < 0x700) tokens += ARABIC;
|
|
158
|
+
else if (code < 0x2000) tokens += OTHER_SCRIPT;
|
|
159
|
+
else if (code < 0x2e80) tokens += SYMBOL;
|
|
160
|
+
else if (code < 0x3400) tokens += KANA;
|
|
161
|
+
else if (code < 0xac00) tokens += HAN;
|
|
162
|
+
else if (code < 0xd800) tokens += HANGUL;
|
|
163
|
+
else if (code < 0xe000) tokens += SURROGATE;
|
|
164
|
+
else if (code < 0xf900) tokens += SYMBOL;
|
|
165
|
+
else if (code < 0xfb00) tokens += HAN;
|
|
166
|
+
else if (code < 0xff00) tokens += OTHER_SCRIPT;
|
|
167
|
+
else tokens += KANA;
|
|
51
168
|
}
|
|
52
|
-
return Math.ceil(
|
|
169
|
+
return Math.ceil(tokens);
|
|
53
170
|
}
|
|
54
171
|
|
|
55
172
|
/** UTF-8 byte length without allocating an encoded copy. */
|