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/node/handlers.ts
CHANGED
|
@@ -1,122 +1,122 @@
|
|
|
1
|
-
import type { IncomingMessage, ServerResponse } from "node:http";
|
|
2
|
-
import { toDocumentSummary, type LargeFiles } from "../library.js";
|
|
3
|
-
import { formatBytes } from "../plan.js";
|
|
4
|
-
|
|
5
|
-
export type DocumentHandlerOptions = {
|
|
6
|
-
/**
|
|
7
|
-
* The signed-in user for this request. When set, uploads are owned by that user and other
|
|
8
|
-
* users cannot read or delete them. Return undefined to allow anonymous access.
|
|
9
|
-
*/
|
|
10
|
-
getOwner?: (request: IncomingMessage) => string | undefined | Promise<string | undefined>;
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
export type DocumentHandlers = {
|
|
14
|
-
/** POST: the body is the raw file. Name it with an `X-File-Name` header (URI-encoded) or `?name=`. */
|
|
15
|
-
upload(request: IncomingMessage, response: ServerResponse): Promise<void>;
|
|
16
|
-
/** GET: the document's summary, or 404. */
|
|
17
|
-
get(request: IncomingMessage, response: ServerResponse, id: string): Promise<void>;
|
|
18
|
-
/** DELETE: removes the document. Responds `{ deleted: boolean }`. */
|
|
19
|
-
remove(request: IncomingMessage, response: ServerResponse, id: string): Promise<void>;
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
class HttpError extends Error {
|
|
23
|
-
constructor(readonly status: number, message: string) {
|
|
24
|
-
super(message);
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
function send(response: ServerResponse, status: number, value: unknown): void {
|
|
29
|
-
if (response.headersSent) return;
|
|
30
|
-
response.statusCode = status;
|
|
31
|
-
response.setHeader("Content-Type", "application/json; charset=utf-8");
|
|
32
|
-
response.end(JSON.stringify(value));
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
function header(request: IncomingMessage, name: string): string {
|
|
36
|
-
const raw = request.headers[name];
|
|
37
|
-
const value = Array.isArray(raw) ? raw[0] : raw;
|
|
38
|
-
if (!value) return "";
|
|
39
|
-
try {
|
|
40
|
-
return decodeURIComponent(value);
|
|
41
|
-
} catch {
|
|
42
|
-
return value;
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/** Reads the raw body, or uses one a framework already parsed into a Buffer (e.g. express.raw()). */
|
|
47
|
-
async function readBody(request: IncomingMessage, limit: number): Promise<Uint8Array> {
|
|
48
|
-
const parsed = (request as IncomingMessage & { body?: unknown }).body;
|
|
49
|
-
if (parsed instanceof Uint8Array) {
|
|
50
|
-
if (parsed.byteLength > limit) throw new HttpError(413, "");
|
|
51
|
-
return parsed;
|
|
52
|
-
}
|
|
53
|
-
if (parsed !== undefined && typeof parsed === "object" && parsed !== null && Object.keys(parsed).length) {
|
|
54
|
-
throw new HttpError(400, "The upload body was already parsed by another middleware. Mount the upload route before body parsers, or send Content-Type: application/octet-stream.");
|
|
55
|
-
}
|
|
56
|
-
const declared = Number(request.headers["content-length"]);
|
|
57
|
-
if (Number.isFinite(declared) && declared > limit) throw new HttpError(413, "");
|
|
58
|
-
const chunks: Buffer[] = [];
|
|
59
|
-
let size = 0;
|
|
60
|
-
for await (const chunk of request) {
|
|
61
|
-
const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk as string);
|
|
62
|
-
size += buffer.byteLength;
|
|
63
|
-
if (size > limit) {
|
|
64
|
-
// Stop buffering; drain the rest so the client can read the response.
|
|
65
|
-
request.resume();
|
|
66
|
-
throw new HttpError(413, "");
|
|
67
|
-
}
|
|
68
|
-
chunks.push(buffer);
|
|
69
|
-
}
|
|
70
|
-
const body = Buffer.concat(chunks);
|
|
71
|
-
return new Uint8Array(body.buffer, body.byteOffset, body.byteLength);
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* Upload, get, and delete handlers for any Node HTTP server (node:http, Express, Fastify via
|
|
76
|
-
* `request.raw`/`reply.raw`, Next.js API routes on the Node runtime). Responses are JSON.
|
|
77
|
-
*/
|
|
78
|
-
export function createDocumentHandlers(files: LargeFiles, options: DocumentHandlerOptions = {}): DocumentHandlers {
|
|
79
|
-
const tooLarge = `The file is larger than the ${formatBytes(files.maxUploadBytes)} upload limit.`;
|
|
80
|
-
const owner = async (request: IncomingMessage) => options.getOwner?.(request);
|
|
81
|
-
|
|
82
|
-
return {
|
|
83
|
-
async upload(request, response) {
|
|
84
|
-
try {
|
|
85
|
-
const url = new URL(request.url ?? "/", "http://localhost");
|
|
86
|
-
const name = header(request, "x-file-name").trim() || url.searchParams.get("name")?.trim() || "";
|
|
87
|
-
if (!name) throw new HttpError(400, "Name the file with an X-File-Name header or a ?name= query parameter.");
|
|
88
|
-
const bytes = await readBody(request, files.maxUploadBytes);
|
|
89
|
-
const contentType = header(request, "content-type");
|
|
90
|
-
const mimeType = header(request, "x-file-type") || (contentType && !contentType.startsWith("application/octet-stream") ? contentType : "");
|
|
91
|
-
const currentOwner = await owner(request);
|
|
92
|
-
let meta;
|
|
93
|
-
try {
|
|
94
|
-
meta = await files.ingest({
|
|
95
|
-
name,
|
|
96
|
-
bytes,
|
|
97
|
-
...(mimeType ? { mimeType } : {}),
|
|
98
|
-
...(currentOwner !== undefined ? { owner: currentOwner } : {}),
|
|
99
|
-
});
|
|
100
|
-
} catch (error) {
|
|
101
|
-
throw new HttpError(422, error instanceof Error ? error.message : String(error));
|
|
102
|
-
}
|
|
103
|
-
send(response, 201, toDocumentSummary(meta));
|
|
104
|
-
} catch (error) {
|
|
105
|
-
if (error instanceof HttpError) send(response, error.status, { error: error.status === 413 ? tooLarge : error.message });
|
|
106
|
-
else send(response, 500, { error: error instanceof Error ? error.message : "Upload failed." });
|
|
107
|
-
}
|
|
108
|
-
},
|
|
109
|
-
|
|
110
|
-
async get(request, response, id) {
|
|
111
|
-
const currentOwner = await owner(request);
|
|
112
|
-
const meta = await files.getMeta(id, currentOwner !== undefined ? { owner: currentOwner } : {});
|
|
113
|
-
if (meta) send(response, 200, toDocumentSummary(meta));
|
|
114
|
-
else send(response, 404, { error: "This file is no longer available." });
|
|
115
|
-
},
|
|
116
|
-
|
|
117
|
-
async remove(request, response, id) {
|
|
118
|
-
const currentOwner = await owner(request);
|
|
119
|
-
send(response, 200, { deleted: await files.delete(id, currentOwner !== undefined ? { owner: currentOwner } : {}) });
|
|
120
|
-
},
|
|
121
|
-
};
|
|
122
|
-
}
|
|
1
|
+
import type { IncomingMessage, ServerResponse } from "node:http";
|
|
2
|
+
import { toDocumentSummary, type LargeFiles } from "../library.js";
|
|
3
|
+
import { formatBytes } from "../plan.js";
|
|
4
|
+
|
|
5
|
+
export type DocumentHandlerOptions = {
|
|
6
|
+
/**
|
|
7
|
+
* The signed-in user for this request. When set, uploads are owned by that user and other
|
|
8
|
+
* users cannot read or delete them. Return undefined to allow anonymous access.
|
|
9
|
+
*/
|
|
10
|
+
getOwner?: (request: IncomingMessage) => string | undefined | Promise<string | undefined>;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export type DocumentHandlers = {
|
|
14
|
+
/** POST: the body is the raw file. Name it with an `X-File-Name` header (URI-encoded) or `?name=`. */
|
|
15
|
+
upload(request: IncomingMessage, response: ServerResponse): Promise<void>;
|
|
16
|
+
/** GET: the document's summary, or 404. */
|
|
17
|
+
get(request: IncomingMessage, response: ServerResponse, id: string): Promise<void>;
|
|
18
|
+
/** DELETE: removes the document. Responds `{ deleted: boolean }`. */
|
|
19
|
+
remove(request: IncomingMessage, response: ServerResponse, id: string): Promise<void>;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
class HttpError extends Error {
|
|
23
|
+
constructor(readonly status: number, message: string) {
|
|
24
|
+
super(message);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function send(response: ServerResponse, status: number, value: unknown): void {
|
|
29
|
+
if (response.headersSent) return;
|
|
30
|
+
response.statusCode = status;
|
|
31
|
+
response.setHeader("Content-Type", "application/json; charset=utf-8");
|
|
32
|
+
response.end(JSON.stringify(value));
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function header(request: IncomingMessage, name: string): string {
|
|
36
|
+
const raw = request.headers[name];
|
|
37
|
+
const value = Array.isArray(raw) ? raw[0] : raw;
|
|
38
|
+
if (!value) return "";
|
|
39
|
+
try {
|
|
40
|
+
return decodeURIComponent(value);
|
|
41
|
+
} catch {
|
|
42
|
+
return value;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Reads the raw body, or uses one a framework already parsed into a Buffer (e.g. express.raw()). */
|
|
47
|
+
async function readBody(request: IncomingMessage, limit: number): Promise<Uint8Array> {
|
|
48
|
+
const parsed = (request as IncomingMessage & { body?: unknown }).body;
|
|
49
|
+
if (parsed instanceof Uint8Array) {
|
|
50
|
+
if (parsed.byteLength > limit) throw new HttpError(413, "");
|
|
51
|
+
return parsed;
|
|
52
|
+
}
|
|
53
|
+
if (parsed !== undefined && typeof parsed === "object" && parsed !== null && Object.keys(parsed).length) {
|
|
54
|
+
throw new HttpError(400, "The upload body was already parsed by another middleware. Mount the upload route before body parsers, or send Content-Type: application/octet-stream.");
|
|
55
|
+
}
|
|
56
|
+
const declared = Number(request.headers["content-length"]);
|
|
57
|
+
if (Number.isFinite(declared) && declared > limit) throw new HttpError(413, "");
|
|
58
|
+
const chunks: Buffer[] = [];
|
|
59
|
+
let size = 0;
|
|
60
|
+
for await (const chunk of request) {
|
|
61
|
+
const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk as string);
|
|
62
|
+
size += buffer.byteLength;
|
|
63
|
+
if (size > limit) {
|
|
64
|
+
// Stop buffering; drain the rest so the client can read the response.
|
|
65
|
+
request.resume();
|
|
66
|
+
throw new HttpError(413, "");
|
|
67
|
+
}
|
|
68
|
+
chunks.push(buffer);
|
|
69
|
+
}
|
|
70
|
+
const body = Buffer.concat(chunks);
|
|
71
|
+
return new Uint8Array(body.buffer, body.byteOffset, body.byteLength);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Upload, get, and delete handlers for any Node HTTP server (node:http, Express, Fastify via
|
|
76
|
+
* `request.raw`/`reply.raw`, Next.js API routes on the Node runtime). Responses are JSON.
|
|
77
|
+
*/
|
|
78
|
+
export function createDocumentHandlers(files: LargeFiles, options: DocumentHandlerOptions = {}): DocumentHandlers {
|
|
79
|
+
const tooLarge = `The file is larger than the ${formatBytes(files.maxUploadBytes)} upload limit.`;
|
|
80
|
+
const owner = async (request: IncomingMessage) => options.getOwner?.(request);
|
|
81
|
+
|
|
82
|
+
return {
|
|
83
|
+
async upload(request, response) {
|
|
84
|
+
try {
|
|
85
|
+
const url = new URL(request.url ?? "/", "http://localhost");
|
|
86
|
+
const name = header(request, "x-file-name").trim() || url.searchParams.get("name")?.trim() || "";
|
|
87
|
+
if (!name) throw new HttpError(400, "Name the file with an X-File-Name header or a ?name= query parameter.");
|
|
88
|
+
const bytes = await readBody(request, files.maxUploadBytes);
|
|
89
|
+
const contentType = header(request, "content-type");
|
|
90
|
+
const mimeType = header(request, "x-file-type") || (contentType && !contentType.startsWith("application/octet-stream") ? contentType : "");
|
|
91
|
+
const currentOwner = await owner(request);
|
|
92
|
+
let meta;
|
|
93
|
+
try {
|
|
94
|
+
meta = await files.ingest({
|
|
95
|
+
name,
|
|
96
|
+
bytes,
|
|
97
|
+
...(mimeType ? { mimeType } : {}),
|
|
98
|
+
...(currentOwner !== undefined ? { owner: currentOwner } : {}),
|
|
99
|
+
});
|
|
100
|
+
} catch (error) {
|
|
101
|
+
throw new HttpError(422, error instanceof Error ? error.message : String(error));
|
|
102
|
+
}
|
|
103
|
+
send(response, 201, toDocumentSummary(meta));
|
|
104
|
+
} catch (error) {
|
|
105
|
+
if (error instanceof HttpError) send(response, error.status, { error: error.status === 413 ? tooLarge : error.message });
|
|
106
|
+
else send(response, 500, { error: error instanceof Error ? error.message : "Upload failed." });
|
|
107
|
+
}
|
|
108
|
+
},
|
|
109
|
+
|
|
110
|
+
async get(request, response, id) {
|
|
111
|
+
const currentOwner = await owner(request);
|
|
112
|
+
const meta = await files.getMeta(id, currentOwner !== undefined ? { owner: currentOwner } : {});
|
|
113
|
+
if (meta) send(response, 200, toDocumentSummary(meta));
|
|
114
|
+
else send(response, 404, { error: "This file is no longer available." });
|
|
115
|
+
},
|
|
116
|
+
|
|
117
|
+
async remove(request, response, id) {
|
|
118
|
+
const currentOwner = await owner(request);
|
|
119
|
+
send(response, 200, { deleted: await files.delete(id, currentOwner !== undefined ? { owner: currentOwner } : {}) });
|
|
120
|
+
},
|
|
121
|
+
};
|
|
122
|
+
}
|