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/tools.ts
CHANGED
|
@@ -1,264 +1,274 @@
|
|
|
1
|
-
import { analyzeWholeDocument, splitIntoParts } from "./map-reduce.js";
|
|
2
|
-
import { grep, rankPassages, type SearchableDocument } from "./search.js";
|
|
3
|
-
import { formatBytes, outlineLines } from "./plan.js";
|
|
4
|
-
import type { Budget, GenerateText, ProgressListener, ToolDeclaration, ToolResult, ToolSet } from "./types.js";
|
|
5
|
-
|
|
6
|
-
const DEFAULT_READ_LINES = 200;
|
|
7
|
-
const MAX_READ_LINES = 600;
|
|
8
|
-
|
|
9
|
-
export const TOOL_NAMES = {
|
|
10
|
-
search: "search_document",
|
|
11
|
-
read: "read_document",
|
|
12
|
-
overview: "get_document_overview",
|
|
13
|
-
analyze: "analyze_whole_document",
|
|
14
|
-
} as const;
|
|
15
|
-
|
|
16
|
-
const documentId = { type: "string" as const, description: "The document_id from the file's card." };
|
|
17
|
-
|
|
18
|
-
const DECLARATIONS: Record<keyof typeof TOOL_NAMES, ToolDeclaration> = {
|
|
19
|
-
search: {
|
|
20
|
-
name: TOOL_NAMES.search,
|
|
21
|
-
description: "Search a large attached file. mode \"keywords\" returns the most relevant passages for a question; \"exact\" returns every line containing the text; \"regex\" returns lines matching a JavaScript regular expression. Results include line numbers to read further with read_document.",
|
|
22
|
-
parameters: {
|
|
23
|
-
type: "object",
|
|
24
|
-
properties: {
|
|
25
|
-
document_id: documentId,
|
|
26
|
-
query: { type: "string", description: "Keywords, exact text, or a regex, depending on mode." },
|
|
27
|
-
mode: { type: "string", enum: ["keywords", "exact", "regex"], description: "Defaults to keywords." },
|
|
28
|
-
max_results: { type: "integer", description: "Up to 50 for exact/regex (default 20), up to 10 passages for keywords (default 5)." },
|
|
29
|
-
context_lines: { type: "integer", description: "exact/regex only: lines to show before and after each match, 0-5." },
|
|
30
|
-
case_sensitive: { type: "boolean", description: "exact/regex only. Defaults to false." },
|
|
31
|
-
},
|
|
32
|
-
required: ["document_id", "query"],
|
|
33
|
-
},
|
|
34
|
-
},
|
|
35
|
-
read: {
|
|
36
|
-
name: TOOL_NAMES.read,
|
|
37
|
-
description: `Read a range of lines from a large attached file, with line numbers. Up to ${MAX_READ_LINES} lines per call; long results are cut and tell you where to continue.`,
|
|
38
|
-
parameters: {
|
|
39
|
-
type: "object",
|
|
40
|
-
properties: {
|
|
41
|
-
document_id: documentId,
|
|
42
|
-
start_line: { type: "integer", description: "First line to read (1-based)." },
|
|
43
|
-
end_line: { type: "integer", description: `Last line to read. Defaults to start_line + ${DEFAULT_READ_LINES - 1}.` },
|
|
44
|
-
},
|
|
45
|
-
required: ["document_id", "start_line"],
|
|
46
|
-
},
|
|
47
|
-
},
|
|
48
|
-
overview: {
|
|
49
|
-
name: TOOL_NAMES.overview,
|
|
50
|
-
description: "Get the full outline (headings with line numbers), log profile (levels, time range, most frequent errors), and the first and last lines of a large attached file.",
|
|
51
|
-
parameters: { type: "object", properties: { document_id: documentId }, required: ["document_id"] },
|
|
52
|
-
},
|
|
53
|
-
analyze: {
|
|
54
|
-
name: TOOL_NAMES.analyze,
|
|
55
|
-
description: "Read an entire large file in parts and answer a task that needs all of it, such as a full summary, a timeline, or every occurrence of something. Slow and costly: prefer search_document and read_document for specific questions.",
|
|
56
|
-
parameters: {
|
|
57
|
-
type: "object",
|
|
58
|
-
properties: {
|
|
59
|
-
document_id: documentId,
|
|
60
|
-
task: { type: "string", description: "What to find or produce from the whole file, stated completely." },
|
|
61
|
-
},
|
|
62
|
-
required: ["document_id", "task"],
|
|
63
|
-
},
|
|
64
|
-
},
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
function integer(value: unknown, fallback: number, min: number, max: number): number {
|
|
68
|
-
const number = typeof value === "number" ? value : typeof value === "string" ? Number(value) : Number.NaN;
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
if (typeof result.
|
|
108
|
-
|
|
109
|
-
if (
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
if (name === TOOL_NAMES.
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
1
|
+
import { analyzeWholeDocument, splitIntoParts } from "./map-reduce.js";
|
|
2
|
+
import { grep, rankPassages, type SearchableDocument } from "./search.js";
|
|
3
|
+
import { formatBytes, outlineLines } from "./plan.js";
|
|
4
|
+
import type { Budget, GenerateText, ProgressListener, ToolDeclaration, ToolResult, ToolSet } from "./types.js";
|
|
5
|
+
|
|
6
|
+
const DEFAULT_READ_LINES = 200;
|
|
7
|
+
const MAX_READ_LINES = 600;
|
|
8
|
+
|
|
9
|
+
export const TOOL_NAMES = {
|
|
10
|
+
search: "search_document",
|
|
11
|
+
read: "read_document",
|
|
12
|
+
overview: "get_document_overview",
|
|
13
|
+
analyze: "analyze_whole_document",
|
|
14
|
+
} as const;
|
|
15
|
+
|
|
16
|
+
const documentId = { type: "string" as const, description: "The document_id from the file's card." };
|
|
17
|
+
|
|
18
|
+
const DECLARATIONS: Record<keyof typeof TOOL_NAMES, ToolDeclaration> = {
|
|
19
|
+
search: {
|
|
20
|
+
name: TOOL_NAMES.search,
|
|
21
|
+
description: "Search a large attached file. mode \"keywords\" returns the most relevant passages for a question; \"exact\" returns every line containing the text; \"regex\" returns lines matching a JavaScript regular expression. Results include line numbers to read further with read_document.",
|
|
22
|
+
parameters: {
|
|
23
|
+
type: "object",
|
|
24
|
+
properties: {
|
|
25
|
+
document_id: documentId,
|
|
26
|
+
query: { type: "string", description: "Keywords, exact text, or a regex, depending on mode." },
|
|
27
|
+
mode: { type: "string", enum: ["keywords", "exact", "regex"], description: "Defaults to keywords." },
|
|
28
|
+
max_results: { type: "integer", description: "Up to 50 for exact/regex (default 20), up to 10 passages for keywords (default 5)." },
|
|
29
|
+
context_lines: { type: "integer", description: "exact/regex only: lines to show before and after each match, 0-5." },
|
|
30
|
+
case_sensitive: { type: "boolean", description: "exact/regex only. Defaults to false." },
|
|
31
|
+
},
|
|
32
|
+
required: ["document_id", "query"],
|
|
33
|
+
},
|
|
34
|
+
},
|
|
35
|
+
read: {
|
|
36
|
+
name: TOOL_NAMES.read,
|
|
37
|
+
description: `Read a range of lines from a large attached file, with line numbers. Up to ${MAX_READ_LINES} lines per call; long results are cut and tell you where to continue.`,
|
|
38
|
+
parameters: {
|
|
39
|
+
type: "object",
|
|
40
|
+
properties: {
|
|
41
|
+
document_id: documentId,
|
|
42
|
+
start_line: { type: "integer", description: "First line to read (1-based)." },
|
|
43
|
+
end_line: { type: "integer", description: `Last line to read. Defaults to start_line + ${DEFAULT_READ_LINES - 1}.` },
|
|
44
|
+
},
|
|
45
|
+
required: ["document_id", "start_line"],
|
|
46
|
+
},
|
|
47
|
+
},
|
|
48
|
+
overview: {
|
|
49
|
+
name: TOOL_NAMES.overview,
|
|
50
|
+
description: "Get the full outline (headings with line numbers), log profile (levels, time range, most frequent errors), and the first and last lines of a large attached file.",
|
|
51
|
+
parameters: { type: "object", properties: { document_id: documentId }, required: ["document_id"] },
|
|
52
|
+
},
|
|
53
|
+
analyze: {
|
|
54
|
+
name: TOOL_NAMES.analyze,
|
|
55
|
+
description: "Read an entire large file in parts and answer a task that needs all of it, such as a full summary, a timeline, or every occurrence of something. Slow and costly: prefer search_document and read_document for specific questions.",
|
|
56
|
+
parameters: {
|
|
57
|
+
type: "object",
|
|
58
|
+
properties: {
|
|
59
|
+
document_id: documentId,
|
|
60
|
+
task: { type: "string", description: "What to find or produce from the whole file, stated completely." },
|
|
61
|
+
},
|
|
62
|
+
required: ["document_id", "task"],
|
|
63
|
+
},
|
|
64
|
+
},
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
function integer(value: unknown, fallback: number, min: number, max: number): number {
|
|
68
|
+
const number = typeof value === "number" ? value : typeof value === "string" ? Number(value) : Number.NaN;
|
|
69
|
+
// The fallback is clamped too: a default end line must not point past the end of the file.
|
|
70
|
+
return Math.min(max, Math.max(min, Math.trunc(Number.isFinite(number) ? number : fallback)));
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const INCOMPLETE_ANALYSIS = "Incomplete: the model's output limit was reached, so some relevant items are missing from this answer. Tell the user it is incomplete. For a complete list or an exact count, use search_document in exact or regex mode (it returns total_matching_lines), or ask about a narrower part of the file.";
|
|
74
|
+
|
|
75
|
+
function text(value: unknown, name: string): string {
|
|
76
|
+
if (typeof value !== "string" || !value.trim()) throw new Error(`${name} is required.`);
|
|
77
|
+
return value;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Trims a result to the character budget by dropping trailing list entries, then cutting text. */
|
|
81
|
+
function fit(result: ToolResult, listKey: string | null, limit: number): ToolResult {
|
|
82
|
+
if (JSON.stringify(result).length <= limit) return result;
|
|
83
|
+
const list = listKey ? result[listKey] : undefined;
|
|
84
|
+
if (Array.isArray(list)) {
|
|
85
|
+
const kept = [...list];
|
|
86
|
+
const note = () => `Showing ${kept.length} of ${list.length} results to stay within the size limit. Narrow the query or read specific lines.`;
|
|
87
|
+
const trimmed = () => ({ ...result, [listKey!]: kept, note: note() });
|
|
88
|
+
while (kept.length > 1 && JSON.stringify(trimmed()).length > limit) kept.pop();
|
|
89
|
+
if (JSON.stringify(trimmed()).length <= limit) return trimmed();
|
|
90
|
+
}
|
|
91
|
+
// Last resort: a raw prefix. Escaping grows the text, so shrink until the whole result fits.
|
|
92
|
+
const serialized = JSON.stringify(result);
|
|
93
|
+
let size = limit;
|
|
94
|
+
let cut: ToolResult;
|
|
95
|
+
do {
|
|
96
|
+
size = Math.floor(size * 0.85);
|
|
97
|
+
cut = { truncated: true, partial: serialized.slice(0, size), note: "Result cut to the size limit. Ask for a smaller range." };
|
|
98
|
+
} while (JSON.stringify(cut).length > limit && size > 100);
|
|
99
|
+
return cut;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* What a finished tool call found, as a suffix for its `describe()` label, for a steps list:
|
|
104
|
+
* "Search app.log (exact) for “timeout”" + " — 12 matching lines".
|
|
105
|
+
*/
|
|
106
|
+
export function toolResultSummary(label: string, result: ToolResult): string {
|
|
107
|
+
if (typeof result.error === "string") return ` — failed: ${result.error}`;
|
|
108
|
+
if (typeof result.total_matching_lines === "number") return ` — ${result.total_matching_lines.toLocaleString("en-US")} matching lines`;
|
|
109
|
+
if (Array.isArray(result.passages)) return ` — ${result.passages.length} passages`;
|
|
110
|
+
if (typeof result.parts_read === "number") {
|
|
111
|
+
const incomplete = result.notes_cut_short || result.answer_cut_short ? ", incomplete" : "";
|
|
112
|
+
return ` — ${result.parts_read} parts, ${String(result.parts_with_relevant_content ?? 0)} relevant${incomplete}`;
|
|
113
|
+
}
|
|
114
|
+
// A read reports its range only when it was cut short of what was asked.
|
|
115
|
+
if (typeof result.lines === "string" && typeof result.total_lines === "number") return label.endsWith(result.lines) ? "" : ` — got ${result.lines}`;
|
|
116
|
+
return "";
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export function createDocumentTools(options: {
|
|
120
|
+
/** Loads a document the tools may access. Return undefined for anything out of scope. */
|
|
121
|
+
load(documentId: string): Promise<SearchableDocument | undefined>;
|
|
122
|
+
documentIds: string[];
|
|
123
|
+
/** File names by document ID, so `describe` can name files before they are loaded. */
|
|
124
|
+
names?: Record<string, string>;
|
|
125
|
+
budget: Budget;
|
|
126
|
+
generate?: GenerateText;
|
|
127
|
+
onProgress?: ProgressListener;
|
|
128
|
+
signal?: AbortSignal;
|
|
129
|
+
}): ToolSet {
|
|
130
|
+
const allowed = new Set(options.documentIds);
|
|
131
|
+
const names = new Map<string, string>(Object.entries(options.names ?? {}));
|
|
132
|
+
const declarations = [DECLARATIONS.search, DECLARATIONS.read, DECLARATIONS.overview, ...(options.generate ? [DECLARATIONS.analyze] : [])];
|
|
133
|
+
const known = new Set(declarations.map((declaration) => declaration.name));
|
|
134
|
+
const limit = options.budget.toolResultCharacters;
|
|
135
|
+
|
|
136
|
+
async function documentFor(args: Record<string, unknown>): Promise<SearchableDocument> {
|
|
137
|
+
const id = text(args.document_id, "document_id");
|
|
138
|
+
// Scope check: a model can only reach files attached to this conversation.
|
|
139
|
+
if (!allowed.has(id)) throw new Error(`Unknown document_id ${id}. Use an ID from a file card.`);
|
|
140
|
+
const document = await options.load(id);
|
|
141
|
+
if (!document) throw new Error(`${id} is no longer available on the server. Ask the user to attach the file again.`);
|
|
142
|
+
names.set(id, document.meta.name);
|
|
143
|
+
return document;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
const handlers: Record<string, (args: Record<string, unknown>) => Promise<ToolResult>> = {
|
|
147
|
+
async [TOOL_NAMES.search](args) {
|
|
148
|
+
const document = await documentFor(args);
|
|
149
|
+
const query = text(args.query, "query");
|
|
150
|
+
const mode = args.mode === "exact" || args.mode === "regex" ? args.mode : "keywords";
|
|
151
|
+
if (mode === "keywords") {
|
|
152
|
+
const topK = integer(args.max_results, 5, 1, 10);
|
|
153
|
+
const ranked = rankPassages(document, query, topK);
|
|
154
|
+
return fit({
|
|
155
|
+
document: document.meta.name,
|
|
156
|
+
mode,
|
|
157
|
+
passages: ranked.map((passage) => ({
|
|
158
|
+
lines: `${passage.from}-${passage.to}`,
|
|
159
|
+
score: passage.score,
|
|
160
|
+
text: document.lines.numbered(passage.from, passage.to),
|
|
161
|
+
})),
|
|
162
|
+
...(ranked.length === 0 ? { note: "No passage contains these keywords. Try other words, synonyms, or exact mode." } : {}),
|
|
163
|
+
}, "passages", limit);
|
|
164
|
+
}
|
|
165
|
+
const result = grep(document, query, {
|
|
166
|
+
regex: mode === "regex",
|
|
167
|
+
caseSensitive: args.case_sensitive === true,
|
|
168
|
+
maxResults: integer(args.max_results, 20, 1, 50),
|
|
169
|
+
contextLines: integer(args.context_lines, 0, 0, 5),
|
|
170
|
+
});
|
|
171
|
+
return fit({
|
|
172
|
+
document: document.meta.name,
|
|
173
|
+
mode,
|
|
174
|
+
total_matching_lines: result.totalMatchingLines,
|
|
175
|
+
...(result.stoppedEarly ? { note: "The scan stopped early; the total is a lower bound." } : {}),
|
|
176
|
+
matches: result.matches,
|
|
177
|
+
}, "matches", limit);
|
|
178
|
+
},
|
|
179
|
+
|
|
180
|
+
async [TOOL_NAMES.read](args) {
|
|
181
|
+
const document = await documentFor(args);
|
|
182
|
+
const total = document.lines.lineCount;
|
|
183
|
+
const start = integer(args.start_line, 1, 1, total);
|
|
184
|
+
const end = integer(args.end_line, start + DEFAULT_READ_LINES - 1, start, Math.min(total, start + MAX_READ_LINES - 1));
|
|
185
|
+
let to = end;
|
|
186
|
+
let body = document.lines.numbered(start, to);
|
|
187
|
+
while (body.length > limit - 500 && to > start) {
|
|
188
|
+
to = start + Math.max(0, Math.floor((to - start) * 0.7));
|
|
189
|
+
body = document.lines.numbered(start, to);
|
|
190
|
+
}
|
|
191
|
+
return {
|
|
192
|
+
document: document.meta.name,
|
|
193
|
+
lines: `${start}-${to}`,
|
|
194
|
+
total_lines: total,
|
|
195
|
+
text: body.slice(0, limit - 500),
|
|
196
|
+
...(to < total ? { next_start_line: to + 1 } : { note: "End of file." }),
|
|
197
|
+
};
|
|
198
|
+
},
|
|
199
|
+
|
|
200
|
+
async [TOOL_NAMES.overview](args) {
|
|
201
|
+
const document = await documentFor(args);
|
|
202
|
+
const { meta, lines } = document;
|
|
203
|
+
const tail = Math.max(1, lines.lineCount - 19);
|
|
204
|
+
return fit({
|
|
205
|
+
document: meta.name,
|
|
206
|
+
format: meta.format,
|
|
207
|
+
size: formatBytes(meta.bytes),
|
|
208
|
+
lines: meta.lines,
|
|
209
|
+
estimated_tokens: meta.estimatedTokens,
|
|
210
|
+
...(meta.pages ? { pages: meta.pages } : {}),
|
|
211
|
+
parts_for_whole_document_analysis: splitIntoParts(document, options.budget).length,
|
|
212
|
+
...(meta.logProfile ? { log_profile: meta.logProfile } : {}),
|
|
213
|
+
first_lines: lines.numbered(1, Math.min(30, lines.lineCount)),
|
|
214
|
+
last_lines: lines.lineCount > 30 ? lines.numbered(tail, lines.lineCount) : "",
|
|
215
|
+
outline: meta.outline.length ? outlineLines(meta.outline) : [],
|
|
216
|
+
}, "outline", limit);
|
|
217
|
+
},
|
|
218
|
+
|
|
219
|
+
async [TOOL_NAMES.analyze](args) {
|
|
220
|
+
if (!options.generate) throw new Error("Whole-document analysis is not enabled.");
|
|
221
|
+
const document = await documentFor(args);
|
|
222
|
+
const task = text(args.task, "task");
|
|
223
|
+
const result = await analyzeWholeDocument({
|
|
224
|
+
document,
|
|
225
|
+
task,
|
|
226
|
+
generate: options.generate,
|
|
227
|
+
budget: options.budget,
|
|
228
|
+
onProgress: options.onProgress,
|
|
229
|
+
signal: options.signal,
|
|
230
|
+
});
|
|
231
|
+
const notesCutShort = result.truncatedParts + result.truncatedMerges;
|
|
232
|
+
return fit({
|
|
233
|
+
document: document.meta.name,
|
|
234
|
+
parts_read: result.parts,
|
|
235
|
+
parts_with_relevant_content: result.relevantParts,
|
|
236
|
+
...(result.failedParts ? { parts_that_failed: result.failedParts } : {}),
|
|
237
|
+
...(notesCutShort ? { notes_cut_short: notesCutShort } : {}),
|
|
238
|
+
...(result.answerTruncated ? { answer_cut_short: true } : {}),
|
|
239
|
+
...(notesCutShort || result.answerTruncated ? { note: INCOMPLETE_ANALYSIS } : {}),
|
|
240
|
+
answer: result.answer,
|
|
241
|
+
}, null, Math.max(limit, 40_000));
|
|
242
|
+
},
|
|
243
|
+
};
|
|
244
|
+
|
|
245
|
+
return {
|
|
246
|
+
declarations,
|
|
247
|
+
has: (name) => known.has(name),
|
|
248
|
+
async execute(name, args) {
|
|
249
|
+
if (!known.has(name)) return { error: `Unknown tool ${name}.` };
|
|
250
|
+
try {
|
|
251
|
+
return await handlers[name]!(args);
|
|
252
|
+
} catch (error) {
|
|
253
|
+
if (options.signal?.aborted) throw error;
|
|
254
|
+
return { error: error instanceof Error ? error.message : String(error) };
|
|
255
|
+
}
|
|
256
|
+
},
|
|
257
|
+
describe(name, args) {
|
|
258
|
+
const id = typeof args.document_id === "string" ? args.document_id : "";
|
|
259
|
+
const file = names.get(id) ?? "the file";
|
|
260
|
+
if (name === TOOL_NAMES.search) {
|
|
261
|
+
const mode = args.mode === "exact" || args.mode === "regex" ? args.mode : "keywords";
|
|
262
|
+
return `Search ${file} (${mode}) for “${String(args.query ?? "").slice(0, 80)}”`;
|
|
263
|
+
}
|
|
264
|
+
if (name === TOOL_NAMES.read) {
|
|
265
|
+
const start = integer(args.start_line, 1, 1, Number.MAX_SAFE_INTEGER);
|
|
266
|
+
const end = typeof args.end_line === "number" && args.end_line >= start ? `-${Math.trunc(args.end_line)}` : "";
|
|
267
|
+
return `Read ${file} lines ${start}${end}`;
|
|
268
|
+
}
|
|
269
|
+
if (name === TOOL_NAMES.overview) return `Overview of ${file}`;
|
|
270
|
+
if (name === TOOL_NAMES.analyze) return `Read all of ${file} for “${String(args.task ?? "").slice(0, 100)}”`;
|
|
271
|
+
return name;
|
|
272
|
+
},
|
|
273
|
+
};
|
|
274
|
+
}
|