llm-large-files 0.2.0 → 0.3.5
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 +101 -0
- package/README.md +52 -6
- package/dist/extract.d.ts +13 -6
- package/dist/extract.d.ts.map +1 -1
- package/dist/extract.js +27 -6
- package/dist/extract.js.map +1 -1
- package/dist/gemini/chat.d.ts +48 -4
- package/dist/gemini/chat.d.ts.map +1 -1
- package/dist/gemini/chat.js +108 -24
- package/dist/gemini/chat.js.map +1 -1
- package/dist/gemini/endpoints.d.ts +17 -0
- package/dist/gemini/endpoints.d.ts.map +1 -1
- package/dist/gemini/endpoints.js +16 -2
- package/dist/gemini/endpoints.js.map +1 -1
- package/dist/gemini/generate.d.ts +34 -4
- package/dist/gemini/generate.d.ts.map +1 -1
- package/dist/gemini/generate.js +47 -7
- package/dist/gemini/generate.js.map +1 -1
- package/dist/gemini/index.d.ts +3 -3
- package/dist/gemini/index.d.ts.map +1 -1
- package/dist/gemini/index.js +2 -2
- package/dist/gemini/index.js.map +1 -1
- package/dist/index.d.ts +2 -2
- 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 +31 -1
- package/dist/library.d.ts.map +1 -1
- package/dist/library.js +44 -6
- package/dist/library.js.map +1 -1
- package/dist/pdf-layout.d.ts +7 -0
- package/dist/pdf-layout.d.ts.map +1 -1
- package/dist/pdf-layout.js +32 -2
- package/dist/pdf-layout.js.map +1 -1
- package/dist/pdf-pages.d.ts +45 -0
- package/dist/pdf-pages.d.ts.map +1 -0
- package/dist/pdf-pages.js +336 -0
- package/dist/pdf-pages.js.map +1 -0
- package/dist/pdf-pictures.d.ts +20 -0
- package/dist/pdf-pictures.d.ts.map +1 -0
- package/dist/pdf-pictures.js +336 -0
- package/dist/pdf-pictures.js.map +1 -0
- package/dist/plan.d.ts +13 -3
- package/dist/plan.d.ts.map +1 -1
- package/dist/plan.js +22 -17
- package/dist/plan.js.map +1 -1
- package/dist/search.d.ts +9 -0
- package/dist/search.d.ts.map +1 -1
- package/dist/search.js +41 -0
- package/dist/search.js.map +1 -1
- package/dist/stores.d.ts +7 -1
- package/dist/stores.d.ts.map +1 -1
- package/dist/stores.js +37 -5
- package/dist/stores.js.map +1 -1
- package/dist/tools.d.ts +11 -0
- package/dist/tools.d.ts.map +1 -1
- package/dist/tools.js +145 -13
- package/dist/tools.js.map +1 -1
- package/dist/types.d.ts +57 -3
- package/dist/types.d.ts.map +1 -1
- package/docs/api.md +59 -17
- package/docs/configuration.md +351 -5
- package/docs/getting-started.md +50 -3
- package/docs/how-it-works.md +73 -4
- package/docs/security.md +39 -4
- package/package.json +7 -2
- package/src/extract.ts +34 -8
- package/src/gemini/chat.ts +145 -30
- package/src/gemini/endpoints.ts +34 -2
- package/src/gemini/generate.ts +63 -9
- package/src/gemini/index.ts +3 -3
- package/src/index.ts +2 -2
- package/src/library.ts +74 -7
- package/src/pdf-layout.ts +37 -1
- package/src/pdf-pages.ts +315 -0
- package/src/pdf-pictures.ts +324 -0
- package/src/plan.ts +36 -16
- package/src/search.ts +37 -0
- package/src/stores.ts +39 -6
- package/src/tools.ts +147 -13
- package/src/types.ts +57 -3
package/src/tools.ts
CHANGED
|
@@ -1,16 +1,22 @@
|
|
|
1
1
|
import { analyzeWholeDocument, splitIntoParts } from "./map-reduce.js";
|
|
2
|
+
import { cutPdfPages } from "./pdf-pages.js";
|
|
2
3
|
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";
|
|
4
|
+
import { formatBytes, outlineLines, REQUEST_BYTES_MARGIN } from "./plan.js";
|
|
5
|
+
import type { Budget, GenerateText, ProgressListener, ToolDeclaration, ToolFileLimits, ToolOutput, ToolResult, ToolSet } from "./types.js";
|
|
5
6
|
|
|
6
7
|
const DEFAULT_READ_LINES = 200;
|
|
7
8
|
const MAX_READ_LINES = 600;
|
|
9
|
+
/** The most pages one `view_pages` call shows. A page costs a model about 530 tokens. */
|
|
10
|
+
export const MAX_VIEW_PAGES = 5;
|
|
11
|
+
/** Below this, no page fits: a cut of one page of plain text is larger. */
|
|
12
|
+
const MIN_FILE_BYTES = 1_024;
|
|
8
13
|
|
|
9
14
|
export const TOOL_NAMES = {
|
|
10
15
|
search: "search_document",
|
|
11
16
|
read: "read_document",
|
|
12
17
|
overview: "get_document_overview",
|
|
13
18
|
analyze: "analyze_whole_document",
|
|
19
|
+
view: "view_pages",
|
|
14
20
|
} as const;
|
|
15
21
|
|
|
16
22
|
const documentId = { type: "string" as const, description: "The document_id from the file's card." };
|
|
@@ -62,8 +68,24 @@ const DECLARATIONS: Record<keyof typeof TOOL_NAMES, ToolDeclaration> = {
|
|
|
62
68
|
required: ["document_id", "task"],
|
|
63
69
|
},
|
|
64
70
|
},
|
|
71
|
+
view: {
|
|
72
|
+
name: TOOL_NAMES.view,
|
|
73
|
+
description: `Look at pages of an attached PDF as they are printed. Use it when the answer depends on something the text of the file cannot show: a picture, photo, chart, diagram, map, handwriting, a stamp or signature, or how a page is laid out. It is not for reading text. Up to ${MAX_VIEW_PAGES} pages per call.`,
|
|
74
|
+
parameters: {
|
|
75
|
+
type: "object",
|
|
76
|
+
properties: {
|
|
77
|
+
document_id: { type: "string", description: "The document_id of the PDF." },
|
|
78
|
+
from_page: { type: "integer", description: "First page to look at (1-based, the N of the \"--- Page N ---\" lines in the text)." },
|
|
79
|
+
to_page: { type: "integer", description: "Last page to look at. Defaults to from_page." },
|
|
80
|
+
},
|
|
81
|
+
required: ["document_id", "from_page"],
|
|
82
|
+
},
|
|
83
|
+
},
|
|
65
84
|
};
|
|
66
85
|
|
|
86
|
+
/** Says so when a tool that returns files is run through `execute`, which has nowhere to put them. */
|
|
87
|
+
const FILES_NOT_DELIVERED = "The pages could not be shown: this application does not pass files from tools to the model. Answer from the text.";
|
|
88
|
+
|
|
67
89
|
function integer(value: unknown, fallback: number, min: number, max: number): number {
|
|
68
90
|
const number = typeof value === "number" ? value : typeof value === "string" ? Number(value) : Number.NaN;
|
|
69
91
|
// The fallback is clamped too: a default end line must not point past the end of the file.
|
|
@@ -77,6 +99,15 @@ function text(value: unknown, name: string): string {
|
|
|
77
99
|
return value;
|
|
78
100
|
}
|
|
79
101
|
|
|
102
|
+
/** A page number given by the model. Unlike a line number it is not clamped: a wrong page is a wrong picture. */
|
|
103
|
+
function pageNumber(value: unknown, name: string): number {
|
|
104
|
+
const number = typeof value === "number" ? value : typeof value === "string" && value.trim() ? Number(value) : Number.NaN;
|
|
105
|
+
if (!Number.isFinite(number) || Math.trunc(number) < 1) throw new Error(`${name} must be a page number: 1 or higher.`);
|
|
106
|
+
return Math.trunc(number);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const pageRange = (from: number, to: number): string => (to > from ? `${from}-${to}` : String(from));
|
|
110
|
+
|
|
80
111
|
/** Trims a result to the character budget by dropping trailing list entries, then cutting text. */
|
|
81
112
|
function fit(result: ToolResult, listKey: string | null, limit: number): ToolResult {
|
|
82
113
|
if (JSON.stringify(result).length <= limit) return result;
|
|
@@ -113,6 +144,10 @@ export function toolResultSummary(label: string, result: ToolResult): string {
|
|
|
113
144
|
}
|
|
114
145
|
// A read reports its range only when it was cut short of what was asked.
|
|
115
146
|
if (typeof result.lines === "string" && typeof result.total_lines === "number") return label.endsWith(result.lines) ? "" : ` — got ${result.lines}`;
|
|
147
|
+
// So does a look at pages.
|
|
148
|
+
if (typeof result.pages === "string" && typeof result.total_pages === "number") {
|
|
149
|
+
return label.endsWith(` ${result.pages}`) ? "" : ` — got ${result.pages.includes("-") ? "pages" : "page"} ${result.pages}`;
|
|
150
|
+
}
|
|
116
151
|
return "";
|
|
117
152
|
}
|
|
118
153
|
|
|
@@ -126,12 +161,28 @@ export function createDocumentTools(options: {
|
|
|
126
161
|
generate?: GenerateText;
|
|
127
162
|
onProgress?: ProgressListener;
|
|
128
163
|
signal?: AbortSignal;
|
|
164
|
+
/**
|
|
165
|
+
* PDFs whose pages `view_pages` may show. The tool is declared only when there is at least one
|
|
166
|
+
* and `loadOriginal` is given. They need not be among `documentIds`: a PDF whose text is in the
|
|
167
|
+
* prompt has nothing to search, but its pages can still be looked at.
|
|
168
|
+
*/
|
|
169
|
+
viewableIds?: string[];
|
|
170
|
+
/** Loads the uploaded file of a document, for `view_pages`. */
|
|
171
|
+
loadOriginal?(documentId: string): Promise<Uint8Array | undefined>;
|
|
129
172
|
}): ToolSet {
|
|
130
173
|
const allowed = new Set(options.documentIds);
|
|
174
|
+
const viewable = new Set(options.loadOriginal ? options.viewableIds ?? [] : []);
|
|
131
175
|
const names = new Map<string, string>(Object.entries(options.names ?? {}));
|
|
132
|
-
|
|
176
|
+
// The text tools are left out only when there are pages to look at and no document to search.
|
|
177
|
+
const textTools = allowed.size > 0 || viewable.size === 0;
|
|
178
|
+
const declarations = [
|
|
179
|
+
...(textTools ? [DECLARATIONS.search, DECLARATIONS.read, DECLARATIONS.overview, ...(options.generate ? [DECLARATIONS.analyze] : [])] : []),
|
|
180
|
+
...(viewable.size > 0 ? [DECLARATIONS.view] : []),
|
|
181
|
+
];
|
|
133
182
|
const known = new Set(declarations.map((declaration) => declaration.name));
|
|
134
183
|
const limit = options.budget.toolResultCharacters;
|
|
184
|
+
// When no limit is given with a call: three eighths of a request, so that two looks still fit.
|
|
185
|
+
const defaultFileBytes = Math.floor((options.budget.maxRequestBytes - REQUEST_BYTES_MARGIN) * 3 / 8);
|
|
135
186
|
|
|
136
187
|
async function documentFor(args: Record<string, unknown>): Promise<SearchableDocument> {
|
|
137
188
|
const id = text(args.document_id, "document_id");
|
|
@@ -148,17 +199,24 @@ export function createDocumentTools(options: {
|
|
|
148
199
|
const document = await documentFor(args);
|
|
149
200
|
const query = text(args.query, "query");
|
|
150
201
|
const mode = args.mode === "exact" || args.mode === "regex" ? args.mode : "keywords";
|
|
202
|
+
// Page numbers are added only for a PDF whose pages can be looked at: they are what
|
|
203
|
+
// view_pages takes. Everything else gets the results of 0.2.0 unchanged.
|
|
204
|
+
const paged = viewable.has(document.meta.id);
|
|
151
205
|
if (mode === "keywords") {
|
|
152
206
|
const topK = integer(args.max_results, 5, 1, 10);
|
|
153
207
|
const ranked = rankPassages(document, query, topK);
|
|
154
208
|
return fit({
|
|
155
209
|
document: document.meta.name,
|
|
156
210
|
mode,
|
|
157
|
-
passages: ranked.map((passage) =>
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
211
|
+
passages: ranked.map((passage) => {
|
|
212
|
+
const pages = paged ? document.pagesOfLines(passage.from, passage.to) : undefined;
|
|
213
|
+
return {
|
|
214
|
+
lines: `${passage.from}-${passage.to}`,
|
|
215
|
+
...(pages ? { pages } : {}),
|
|
216
|
+
score: passage.score,
|
|
217
|
+
text: document.lines.numbered(passage.from, passage.to),
|
|
218
|
+
};
|
|
219
|
+
}),
|
|
162
220
|
...(ranked.length === 0 ? { note: "No passage contains these keywords. Try other words, synonyms, or exact mode." } : {}),
|
|
163
221
|
}, "passages", limit);
|
|
164
222
|
}
|
|
@@ -173,7 +231,10 @@ export function createDocumentTools(options: {
|
|
|
173
231
|
mode,
|
|
174
232
|
total_matching_lines: result.totalMatchingLines,
|
|
175
233
|
...(result.stoppedEarly ? { note: "The scan stopped early; the total is a lower bound." } : {}),
|
|
176
|
-
matches: result.matches,
|
|
234
|
+
matches: paged ? result.matches.map(({ line, ...rest }) => {
|
|
235
|
+
const page = document.pageOfLine(line);
|
|
236
|
+
return { line, ...(page ? { page } : {}), ...rest };
|
|
237
|
+
}) : result.matches,
|
|
177
238
|
}, "matches", limit);
|
|
178
239
|
},
|
|
179
240
|
|
|
@@ -188,9 +249,11 @@ export function createDocumentTools(options: {
|
|
|
188
249
|
to = start + Math.max(0, Math.floor((to - start) * 0.7));
|
|
189
250
|
body = document.lines.numbered(start, to);
|
|
190
251
|
}
|
|
252
|
+
const pages = viewable.has(document.meta.id) ? document.pagesOfLines(start, to) : undefined;
|
|
191
253
|
return {
|
|
192
254
|
document: document.meta.name,
|
|
193
255
|
lines: `${start}-${to}`,
|
|
256
|
+
...(pages ? { pages } : {}),
|
|
194
257
|
total_lines: total,
|
|
195
258
|
text: body.slice(0, limit - 500),
|
|
196
259
|
...(to < total ? { next_start_line: to + 1 } : { note: "End of file." }),
|
|
@@ -242,21 +305,92 @@ export function createDocumentTools(options: {
|
|
|
242
305
|
},
|
|
243
306
|
};
|
|
244
307
|
|
|
308
|
+
/** `view_pages`: the pages as one small PDF, next to a short JSON result. */
|
|
309
|
+
async function viewPages(args: Record<string, unknown>, limits: ToolFileLimits): Promise<ToolOutput> {
|
|
310
|
+
const id = text(args.document_id, "document_id");
|
|
311
|
+
// The same scope check as the text tools, on its own list: a PDF can be looked at without
|
|
312
|
+
// being searchable, and the other way round.
|
|
313
|
+
if (!viewable.has(id)) {
|
|
314
|
+
throw new Error(allowed.has(id)
|
|
315
|
+
? `The pages of ${names.get(id) ?? id} cannot be shown: only its text was kept. Answer from the text.`
|
|
316
|
+
: `Unknown document_id ${id}. Use the document_id of an attached PDF.`);
|
|
317
|
+
}
|
|
318
|
+
const document = await options.load(id);
|
|
319
|
+
if (!document) throw new Error(`${id} is no longer available on the server. Ask the user to attach the file again.`);
|
|
320
|
+
const file = document.meta.name;
|
|
321
|
+
names.set(id, file);
|
|
322
|
+
const from = pageNumber(args.from_page, "from_page");
|
|
323
|
+
const asked = args.to_page === undefined || args.to_page === null ? from : pageNumber(args.to_page, "to_page");
|
|
324
|
+
if (asked < from) throw new Error(`to_page (${asked}) is before from_page (${from}).`);
|
|
325
|
+
const total = document.meta.pages;
|
|
326
|
+
if (total && from > total) throw new Error(`${file} has ${total} ${total === 1 ? "page" : "pages"}. There is no page ${from}.`);
|
|
327
|
+
const whole = (value: number | undefined, fallback: number) => (typeof value === "number" && Number.isFinite(value) ? Math.trunc(value) : fallback);
|
|
328
|
+
const mostPages = Math.min(MAX_VIEW_PAGES, whole(limits.maxPages, MAX_VIEW_PAGES));
|
|
329
|
+
if (mostPages < 1) throw new Error("No more pages can be looked at for this question. Answer from the pages and the text you already have.");
|
|
330
|
+
const maxBytes = whole(limits.maxFileBytes, defaultFileBytes);
|
|
331
|
+
if (maxBytes < MIN_FILE_BYTES) throw new Error("There is no room left in this request to show more pages. Answer from the pages and the text you already have.");
|
|
332
|
+
const original = await options.loadOriginal!(id);
|
|
333
|
+
if (!original) throw new Error(`The original file of ${file} is no longer on the server, so its pages cannot be shown. Answer from the text.`);
|
|
334
|
+
let cut;
|
|
335
|
+
try {
|
|
336
|
+
cut = await cutPdfPages(original, from, Math.min(asked, from + mostPages - 1), maxBytes, total);
|
|
337
|
+
} catch (error) {
|
|
338
|
+
throw new Error(`${file}: ${error instanceof Error ? error.message : String(error)}`);
|
|
339
|
+
}
|
|
340
|
+
const pages = pageRange(cut.from, cut.to);
|
|
341
|
+
const shown = cut.to - cut.from + 1;
|
|
342
|
+
// What was asked for, as far as the file goes.
|
|
343
|
+
const wanted = Math.min(asked, cut.pageCount);
|
|
344
|
+
const showing = `Showing ${shown} of the ${wanted - cut.from + 1} pages asked for`;
|
|
345
|
+
const again = `Call view_pages from page ${cut.to + 1} if you need them.`;
|
|
346
|
+
const rest: ToolResult = cut.to >= wanted ? {}
|
|
347
|
+
: shown < mostPages ? { next_page: cut.to + 1, note: `${showing}: the others did not fit in this request. ${again}` }
|
|
348
|
+
: mostPages < MAX_VIEW_PAGES ? { note: `${showing}: no more pages can be looked at for this question.` }
|
|
349
|
+
: { next_page: cut.to + 1, note: `${showing}: one call shows at most ${MAX_VIEW_PAGES}. ${again}` };
|
|
350
|
+
const name = `pages-${pages}.pdf`;
|
|
351
|
+
return {
|
|
352
|
+
result: { document: file, pages, total_pages: cut.pageCount, file: name, ...rest },
|
|
353
|
+
files: [{ name, mimeType: "application/pdf", bytes: cut.bytes }],
|
|
354
|
+
};
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
const failure = (error: unknown): ToolResult => ({ error: error instanceof Error ? error.message : String(error) });
|
|
358
|
+
|
|
359
|
+
async function execute(name: string, args: Record<string, unknown>): Promise<ToolResult> {
|
|
360
|
+
if (!known.has(name)) return { error: `Unknown tool ${name}.` };
|
|
361
|
+
// The pages are a file, and this method returns only JSON. Saying that pages are attached
|
|
362
|
+
// with nothing attached would be worse than saying that they could not be shown.
|
|
363
|
+
if (name === TOOL_NAMES.view) return { error: FILES_NOT_DELIVERED };
|
|
364
|
+
try {
|
|
365
|
+
return await handlers[name]!(args);
|
|
366
|
+
} catch (error) {
|
|
367
|
+
if (options.signal?.aborted) throw error;
|
|
368
|
+
return failure(error);
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
|
|
245
372
|
return {
|
|
246
373
|
declarations,
|
|
247
374
|
has: (name) => known.has(name),
|
|
248
|
-
|
|
249
|
-
|
|
375
|
+
execute,
|
|
376
|
+
async executeWithFiles(name, args, limits = {}) {
|
|
377
|
+
if (name !== TOOL_NAMES.view || !known.has(name)) return { result: await execute(name, args), files: [] };
|
|
250
378
|
try {
|
|
251
|
-
return await
|
|
379
|
+
return await viewPages(args, limits);
|
|
252
380
|
} catch (error) {
|
|
253
381
|
if (options.signal?.aborted) throw error;
|
|
254
|
-
return {
|
|
382
|
+
return { result: failure(error), files: [] };
|
|
255
383
|
}
|
|
256
384
|
},
|
|
257
385
|
describe(name, args) {
|
|
258
386
|
const id = typeof args.document_id === "string" ? args.document_id : "";
|
|
259
387
|
const file = names.get(id) ?? "the file";
|
|
388
|
+
if (name === TOOL_NAMES.view) {
|
|
389
|
+
// Read the numbers the way the tool will: a model sometimes sends them as text.
|
|
390
|
+
const from = integer(args.from_page, 1, 1, Number.MAX_SAFE_INTEGER);
|
|
391
|
+
const to = integer(args.to_page, from, from, Number.MAX_SAFE_INTEGER);
|
|
392
|
+
return `Look at ${file} ${to > from ? "pages" : "page"} ${pageRange(from, to)}`;
|
|
393
|
+
}
|
|
260
394
|
if (name === TOOL_NAMES.search) {
|
|
261
395
|
const mode = args.mode === "exact" || args.mode === "regex" ? args.mode : "keywords";
|
|
262
396
|
return `Search ${file} (${mode}) for “${String(args.query ?? "").slice(0, 80)}”`;
|
package/src/types.ts
CHANGED
|
@@ -44,6 +44,11 @@ export type DocumentMeta = {
|
|
|
44
44
|
/** Lines longer than the wrap limit are split so every line stays readable by line number. */
|
|
45
45
|
wrappedLines: number;
|
|
46
46
|
pages?: number;
|
|
47
|
+
/**
|
|
48
|
+
* True when the uploaded file itself is kept in the store next to its text (see `keepOriginals`).
|
|
49
|
+
* Recorded here so that planning a request does not have to load the file to know.
|
|
50
|
+
*/
|
|
51
|
+
originalKept?: boolean;
|
|
47
52
|
outline: OutlineEntry[];
|
|
48
53
|
logProfile?: LogProfile;
|
|
49
54
|
createdAt: string;
|
|
@@ -71,9 +76,17 @@ export interface DocumentStore {
|
|
|
71
76
|
/** Returns undefined for unknown or expired documents. */
|
|
72
77
|
get(id: string): Promise<StoredDocument | undefined>;
|
|
73
78
|
getMeta(id: string): Promise<DocumentMeta | undefined>;
|
|
79
|
+
/** Removes the document, and its original file if one is kept. */
|
|
74
80
|
delete(id: string): Promise<boolean>;
|
|
75
|
-
/** Removes expired documents and returns how many were removed. */
|
|
81
|
+
/** Removes expired documents (and their original files) and returns how many were removed. */
|
|
76
82
|
sweep?(now?: Date): Promise<number>;
|
|
83
|
+
/**
|
|
84
|
+
* Keeps the uploaded file of a document. It is called before `put` for the same ID. Optional: a
|
|
85
|
+
* store without it works as before, and original files are then not kept.
|
|
86
|
+
*/
|
|
87
|
+
putOriginal?(id: string, bytes: Uint8Array): Promise<void>;
|
|
88
|
+
/** The uploaded file, or undefined when none was kept or the document is unknown or expired. */
|
|
89
|
+
getOriginal?(id: string): Promise<Uint8Array | undefined>;
|
|
77
90
|
}
|
|
78
91
|
|
|
79
92
|
export type ExtractedText = { text: string; pages?: number };
|
|
@@ -82,6 +95,12 @@ export type ExtractInput = {
|
|
|
82
95
|
name: string;
|
|
83
96
|
mimeType: string;
|
|
84
97
|
format: DocumentFormat;
|
|
98
|
+
/**
|
|
99
|
+
* Set when a PDF without any text (a scan) is worth storing, because its pages can be shown to
|
|
100
|
+
* the model: the most pages such a PDF may have. Unset, a PDF without text is refused, as it
|
|
101
|
+
* always was. An extractor of your own may ignore it.
|
|
102
|
+
*/
|
|
103
|
+
scannedPages?: number;
|
|
85
104
|
};
|
|
86
105
|
|
|
87
106
|
export type Extractor = (bytes: Uint8Array, input: ExtractInput) => Promise<ExtractedText>;
|
|
@@ -122,11 +141,41 @@ export type ToolDeclaration = {
|
|
|
122
141
|
|
|
123
142
|
export type ToolResult = Record<string, unknown>;
|
|
124
143
|
|
|
144
|
+
/** A file a tool hands back next to its JSON result, for example pages cut out of a PDF. */
|
|
145
|
+
export type ToolFile = {
|
|
146
|
+
/** A short ASCII name, unique among the files of one result: "pages-12-14.pdf". */
|
|
147
|
+
name: string;
|
|
148
|
+
mimeType: string;
|
|
149
|
+
bytes: Uint8Array;
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
/** A tool's JSON result, and the files that belong to it. Most tools return no files. */
|
|
153
|
+
export type ToolOutput = { result: ToolResult; files: ToolFile[] };
|
|
154
|
+
|
|
155
|
+
/** What a call that can return files is allowed to use of the request it will be sent in. */
|
|
156
|
+
export type ToolFileLimits = {
|
|
157
|
+
/**
|
|
158
|
+
* The most bytes the files of this call may have together. Give the room left in your request,
|
|
159
|
+
* times 3/4 when files are sent as base64. Defaults to about a third of `maxRequestBytes`.
|
|
160
|
+
*/
|
|
161
|
+
maxFileBytes?: number;
|
|
162
|
+
/** The most pages this call may show. Lower it as the pages already shown use up your token budget. */
|
|
163
|
+
maxPages?: number;
|
|
164
|
+
};
|
|
165
|
+
|
|
125
166
|
export type ToolSet = {
|
|
126
167
|
declarations: ToolDeclaration[];
|
|
127
168
|
has(name: string): boolean;
|
|
128
|
-
/**
|
|
169
|
+
/**
|
|
170
|
+
* Runs a tool call. Bad arguments come back as `{ error }` so the model can correct itself. A
|
|
171
|
+
* tool that returns files (`view_pages`) answers with an error here: use `executeWithFiles`.
|
|
172
|
+
*/
|
|
129
173
|
execute(name: string, args: Record<string, unknown>): Promise<ToolResult>;
|
|
174
|
+
/**
|
|
175
|
+
* Runs a tool call and also returns the files it produced, to be sent to the model with the
|
|
176
|
+
* result. Use it in place of `execute` in a loop that can send files.
|
|
177
|
+
*/
|
|
178
|
+
executeWithFiles(name: string, args: Record<string, unknown>, limits?: ToolFileLimits): Promise<ToolOutput>;
|
|
130
179
|
/** A short, human-readable line describing a call, for progress UIs. */
|
|
131
180
|
describe(name: string, args: Record<string, unknown>): string;
|
|
132
181
|
};
|
|
@@ -167,8 +216,13 @@ export type PreparedContext = {
|
|
|
167
216
|
plan: DocumentPlan;
|
|
168
217
|
/** Prompt text per document ID: the full text for inline documents, a summary card otherwise. */
|
|
169
218
|
blocks: Map<string, string>;
|
|
170
|
-
/** System instruction to add when any document is searchable, else an empty string. */
|
|
219
|
+
/** System instruction to add when any document is searchable or viewable, else an empty string. */
|
|
171
220
|
instruction: string;
|
|
172
221
|
/** IDs that were not found (never uploaded, deleted, or expired). */
|
|
173
222
|
missing: string[];
|
|
223
|
+
/**
|
|
224
|
+
* PDFs whose pages `view_pages` can show: their original file is kept. Empty unless `prepare`
|
|
225
|
+
* was called with `viewPages: true`. Pass it to `tools(..., { viewPages })`.
|
|
226
|
+
*/
|
|
227
|
+
viewable: DocumentMeta[];
|
|
174
228
|
};
|