llm-large-files 0.1.0 → 0.3.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.
Files changed (91) hide show
  1. package/CHANGELOG.md +147 -0
  2. package/README.md +32 -6
  3. package/dist/analyze.d.ts.map +1 -1
  4. package/dist/analyze.js +5 -1
  5. package/dist/analyze.js.map +1 -1
  6. package/dist/extract.d.ts +30 -7
  7. package/dist/extract.d.ts.map +1 -1
  8. package/dist/extract.js +94 -23
  9. package/dist/extract.js.map +1 -1
  10. package/dist/gemini/chat.d.ts +38 -2
  11. package/dist/gemini/chat.d.ts.map +1 -1
  12. package/dist/gemini/chat.js +83 -32
  13. package/dist/gemini/chat.js.map +1 -1
  14. package/dist/gemini/generate.d.ts +34 -4
  15. package/dist/gemini/generate.d.ts.map +1 -1
  16. package/dist/gemini/generate.js +51 -8
  17. package/dist/gemini/generate.js.map +1 -1
  18. package/dist/gemini/index.d.ts +3 -3
  19. package/dist/gemini/index.d.ts.map +1 -1
  20. package/dist/gemini/index.js +2 -2
  21. package/dist/gemini/index.js.map +1 -1
  22. package/dist/index.d.ts +4 -3
  23. package/dist/index.d.ts.map +1 -1
  24. package/dist/index.js +2 -2
  25. package/dist/index.js.map +1 -1
  26. package/dist/library.d.ts +37 -1
  27. package/dist/library.d.ts.map +1 -1
  28. package/dist/library.js +59 -7
  29. package/dist/library.js.map +1 -1
  30. package/dist/map-reduce.d.ts +7 -0
  31. package/dist/map-reduce.d.ts.map +1 -1
  32. package/dist/map-reduce.js +58 -16
  33. package/dist/map-reduce.js.map +1 -1
  34. package/dist/pdf-layout.d.ts +39 -0
  35. package/dist/pdf-layout.d.ts.map +1 -0
  36. package/dist/pdf-layout.js +820 -0
  37. package/dist/pdf-layout.js.map +1 -0
  38. package/dist/pdf-pages.d.ts +45 -0
  39. package/dist/pdf-pages.d.ts.map +1 -0
  40. package/dist/pdf-pages.js +336 -0
  41. package/dist/pdf-pages.js.map +1 -0
  42. package/dist/pdf-pictures.d.ts +20 -0
  43. package/dist/pdf-pictures.d.ts.map +1 -0
  44. package/dist/pdf-pictures.js +336 -0
  45. package/dist/pdf-pictures.js.map +1 -0
  46. package/dist/plan.d.ts +13 -3
  47. package/dist/plan.d.ts.map +1 -1
  48. package/dist/plan.js +27 -18
  49. package/dist/plan.js.map +1 -1
  50. package/dist/search.d.ts +17 -4
  51. package/dist/search.d.ts.map +1 -1
  52. package/dist/search.js +90 -24
  53. package/dist/search.js.map +1 -1
  54. package/dist/stores.d.ts +7 -1
  55. package/dist/stores.d.ts.map +1 -1
  56. package/dist/stores.js +37 -5
  57. package/dist/stores.js.map +1 -1
  58. package/dist/text.d.ts +18 -4
  59. package/dist/text.d.ts.map +1 -1
  60. package/dist/text.js +155 -15
  61. package/dist/text.js.map +1 -1
  62. package/dist/tools.d.ts +11 -0
  63. package/dist/tools.d.ts.map +1 -1
  64. package/dist/tools.js +156 -16
  65. package/dist/tools.js.map +1 -1
  66. package/dist/types.d.ts +63 -3
  67. package/dist/types.d.ts.map +1 -1
  68. package/docs/api.md +56 -11
  69. package/docs/configuration.md +298 -6
  70. package/docs/getting-started.md +41 -7
  71. package/docs/how-it-works.md +77 -11
  72. package/docs/security.md +54 -6
  73. package/package.json +96 -91
  74. package/src/analyze.ts +3 -1
  75. package/src/extract.ts +105 -24
  76. package/src/gemini/chat.ts +116 -34
  77. package/src/gemini/generate.ts +66 -10
  78. package/src/gemini/index.ts +3 -3
  79. package/src/index.ts +4 -3
  80. package/src/library.ts +308 -226
  81. package/src/map-reduce.ts +69 -16
  82. package/src/node/handlers.ts +122 -122
  83. package/src/pdf-layout.ts +807 -0
  84. package/src/pdf-pages.ts +315 -0
  85. package/src/pdf-pictures.ts +324 -0
  86. package/src/plan.ts +120 -98
  87. package/src/search.ts +85 -25
  88. package/src/stores.ts +165 -132
  89. package/src/text.ts +131 -14
  90. package/src/tools.ts +408 -264
  91. package/src/types.ts +228 -168
package/src/types.ts CHANGED
@@ -1,168 +1,228 @@
1
- // Public types. Everything a consumer touches is defined here, so the package can be
2
- // published without exposing internal module layout.
3
-
4
- export type DocumentFormat = "text" | "markdown" | "log" | "json" | "csv" | "pdf" | "docx" | "pptx";
5
-
6
- export type OutlineEntry = {
7
- /** 1-based line number in the extracted text. */
8
- line: number;
9
- /** 1 is the top level. */
10
- level: number;
11
- title: string;
12
- };
13
-
14
- export type LogIssue = {
15
- /** The message with numbers, IDs, addresses, and quoted values replaced by placeholders. */
16
- template: string;
17
- level: string;
18
- count: number;
19
- firstLine: number;
20
- };
21
-
22
- export type LogProfile = {
23
- levels: Record<string, number>;
24
- firstTimestamp?: string;
25
- lastTimestamp?: string;
26
- /** The most frequent ERROR/FATAL/WARN message shapes. */
27
- topIssues: LogIssue[];
28
- };
29
-
30
- export type DocumentMeta = {
31
- id: string;
32
- /** Who may use this document. Set it at ingest; access with another owner is refused. */
33
- owner?: string;
34
- name: string;
35
- mimeType: string;
36
- format: DocumentFormat;
37
- /** Size of the uploaded file. */
38
- bytes: number;
39
- /** UTF-8 size of the extracted text. */
40
- textBytes: number;
41
- characters: number;
42
- lines: number;
43
- estimatedTokens: number;
44
- /** Lines longer than the wrap limit are split so every line stays readable by line number. */
45
- wrappedLines: number;
46
- pages?: number;
47
- outline: OutlineEntry[];
48
- logProfile?: LogProfile;
49
- createdAt: string;
50
- expiresAt: string;
51
- };
52
-
53
- export type StoredDocument = { meta: DocumentMeta; text: string };
54
-
55
- /** A JSON-safe summary for API responses and browser state. */
56
- export type DocumentSummary = {
57
- id: string;
58
- name: string;
59
- mimeType: string;
60
- format: DocumentFormat;
61
- bytes: number;
62
- lines: number;
63
- estimatedTokens: number;
64
- pages?: number;
65
- expiresAt: string;
66
- };
67
-
68
- /** Where extracted text lives. Implement this for Redis, S3, a database, etc. */
69
- export interface DocumentStore {
70
- put(document: StoredDocument): Promise<void>;
71
- /** Returns undefined for unknown or expired documents. */
72
- get(id: string): Promise<StoredDocument | undefined>;
73
- getMeta(id: string): Promise<DocumentMeta | undefined>;
74
- delete(id: string): Promise<boolean>;
75
- /** Removes expired documents and returns how many were removed. */
76
- sweep?(now?: Date): Promise<number>;
77
- }
78
-
79
- export type ExtractedText = { text: string; pages?: number };
80
-
81
- export type ExtractInput = {
82
- name: string;
83
- mimeType: string;
84
- format: DocumentFormat;
85
- };
86
-
87
- export type Extractor = (bytes: Uint8Array, input: ExtractInput) => Promise<ExtractedText>;
88
-
89
- /** The limits of the model connection the documents must fit through. */
90
- export type Budget = {
91
- /** The model's context window, in tokens. */
92
- contextTokens: number;
93
- /** The largest request body the model connector accepts, in bytes. */
94
- maxRequestBytes: number;
95
- /** The most document text to include in full in one request, in tokens. */
96
- inlineTokens: number;
97
- /** Tokens kept free for the answer, tool results, and conversation growth. */
98
- reservedTokens: number;
99
- /** Size of each part when a whole document is read with map-reduce. */
100
- mapChunkTokens: number;
101
- /** Refuse whole-document reads that would need more parts than this. */
102
- maxMapChunks: number;
103
- /** Cap on the characters of one tool result sent back to the model. */
104
- toolResultCharacters: number;
105
- };
106
-
107
- /** A JSON Schema subset accepted by Gemini, OpenAI, and Anthropic function declarations. */
108
- export type JsonSchema = {
109
- type: "string" | "integer" | "number" | "boolean" | "object" | "array";
110
- description?: string;
111
- enum?: string[];
112
- properties?: Record<string, JsonSchema>;
113
- required?: string[];
114
- items?: JsonSchema;
115
- };
116
-
117
- export type ToolDeclaration = {
118
- name: string;
119
- description: string;
120
- parameters: JsonSchema & { type: "object" };
121
- };
122
-
123
- export type ToolResult = Record<string, unknown>;
124
-
125
- export type ToolSet = {
126
- declarations: ToolDeclaration[];
127
- has(name: string): boolean;
128
- /** Runs a tool call. Bad arguments come back as `{ error }` so the model can correct itself. */
129
- execute(name: string, args: Record<string, unknown>): Promise<ToolResult>;
130
- /** A short, human-readable line describing a call, for progress UIs. */
131
- describe(name: string, args: Record<string, unknown>): string;
132
- };
133
-
134
- /** A plain text completion. The package stays provider-neutral by taking this as a function. */
135
- export type GenerateText = (request: {
136
- system: string;
137
- prompt: string;
138
- maxOutputTokens: number;
139
- signal?: AbortSignal;
140
- }) => Promise<string>;
141
-
142
- export type ProgressEvent = {
143
- kind: "map" | "reduce";
144
- message: string;
145
- done: number;
146
- total: number;
147
- };
148
-
149
- export type ProgressListener = (event: ProgressEvent) => void;
150
-
151
- export type DocumentPlan = {
152
- /** Documents whose full text goes into the prompt. */
153
- inline: DocumentMeta[];
154
- /** Documents the model reaches only through tools. */
155
- searchable: DocumentMeta[];
156
- inlineTokens: number;
157
- inlineBytes: number;
158
- };
159
-
160
- export type PreparedContext = {
161
- plan: DocumentPlan;
162
- /** Prompt text per document ID: the full text for inline documents, a summary card otherwise. */
163
- blocks: Map<string, string>;
164
- /** System instruction to add when any document is searchable, else an empty string. */
165
- instruction: string;
166
- /** IDs that were not found (never uploaded, deleted, or expired). */
167
- missing: string[];
168
- };
1
+ // Public types. Everything a consumer touches is defined here, so the package can be
2
+ // published without exposing internal module layout.
3
+
4
+ export type DocumentFormat = "text" | "markdown" | "log" | "json" | "csv" | "pdf" | "docx" | "pptx";
5
+
6
+ export type OutlineEntry = {
7
+ /** 1-based line number in the extracted text. */
8
+ line: number;
9
+ /** 1 is the top level. */
10
+ level: number;
11
+ title: string;
12
+ };
13
+
14
+ export type LogIssue = {
15
+ /** The message with numbers, IDs, addresses, and quoted values replaced by placeholders. */
16
+ template: string;
17
+ level: string;
18
+ count: number;
19
+ firstLine: number;
20
+ };
21
+
22
+ export type LogProfile = {
23
+ levels: Record<string, number>;
24
+ firstTimestamp?: string;
25
+ lastTimestamp?: string;
26
+ /** The most frequent ERROR/FATAL/WARN message shapes. */
27
+ topIssues: LogIssue[];
28
+ };
29
+
30
+ export type DocumentMeta = {
31
+ id: string;
32
+ /** Who may use this document. Set it at ingest; access with another owner is refused. */
33
+ owner?: string;
34
+ name: string;
35
+ mimeType: string;
36
+ format: DocumentFormat;
37
+ /** Size of the uploaded file. */
38
+ bytes: number;
39
+ /** UTF-8 size of the extracted text. */
40
+ textBytes: number;
41
+ characters: number;
42
+ lines: number;
43
+ estimatedTokens: number;
44
+ /** Lines longer than the wrap limit are split so every line stays readable by line number. */
45
+ wrappedLines: number;
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;
52
+ outline: OutlineEntry[];
53
+ logProfile?: LogProfile;
54
+ createdAt: string;
55
+ expiresAt: string;
56
+ };
57
+
58
+ export type StoredDocument = { meta: DocumentMeta; text: string };
59
+
60
+ /** A JSON-safe summary for API responses and browser state. */
61
+ export type DocumentSummary = {
62
+ id: string;
63
+ name: string;
64
+ mimeType: string;
65
+ format: DocumentFormat;
66
+ bytes: number;
67
+ lines: number;
68
+ estimatedTokens: number;
69
+ pages?: number;
70
+ expiresAt: string;
71
+ };
72
+
73
+ /** Where extracted text lives. Implement this for Redis, S3, a database, etc. */
74
+ export interface DocumentStore {
75
+ put(document: StoredDocument): Promise<void>;
76
+ /** Returns undefined for unknown or expired documents. */
77
+ get(id: string): Promise<StoredDocument | undefined>;
78
+ getMeta(id: string): Promise<DocumentMeta | undefined>;
79
+ /** Removes the document, and its original file if one is kept. */
80
+ delete(id: string): Promise<boolean>;
81
+ /** Removes expired documents (and their original files) and returns how many were removed. */
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>;
90
+ }
91
+
92
+ export type ExtractedText = { text: string; pages?: number };
93
+
94
+ export type ExtractInput = {
95
+ name: string;
96
+ mimeType: string;
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;
104
+ };
105
+
106
+ export type Extractor = (bytes: Uint8Array, input: ExtractInput) => Promise<ExtractedText>;
107
+
108
+ /** The limits of the model connection the documents must fit through. */
109
+ export type Budget = {
110
+ /** The model's context window, in tokens. */
111
+ contextTokens: number;
112
+ /** The largest request body the model connector accepts, in bytes. */
113
+ maxRequestBytes: number;
114
+ /** The most document text to include in full in one request, in tokens. */
115
+ inlineTokens: number;
116
+ /** Tokens kept free for the answer, tool results, and conversation growth. */
117
+ reservedTokens: number;
118
+ /** Size of each part when a whole document is read with map-reduce. */
119
+ mapChunkTokens: number;
120
+ /** Refuse whole-document reads that would need more parts than this. */
121
+ maxMapChunks: number;
122
+ /** Cap on the characters of one tool result sent back to the model. */
123
+ toolResultCharacters: number;
124
+ };
125
+
126
+ /** A JSON Schema subset accepted by Gemini, OpenAI, and Anthropic function declarations. */
127
+ export type JsonSchema = {
128
+ type: "string" | "integer" | "number" | "boolean" | "object" | "array";
129
+ description?: string;
130
+ enum?: string[];
131
+ properties?: Record<string, JsonSchema>;
132
+ required?: string[];
133
+ items?: JsonSchema;
134
+ };
135
+
136
+ export type ToolDeclaration = {
137
+ name: string;
138
+ description: string;
139
+ parameters: JsonSchema & { type: "object" };
140
+ };
141
+
142
+ export type ToolResult = Record<string, unknown>;
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
+
166
+ export type ToolSet = {
167
+ declarations: ToolDeclaration[];
168
+ has(name: string): boolean;
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
+ */
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>;
179
+ /** A short, human-readable line describing a call, for progress UIs. */
180
+ describe(name: string, args: Record<string, unknown>): string;
181
+ };
182
+
183
+ /** A plain text completion. The package stays provider-neutral by taking this as a function. */
184
+ export type GenerateText = (request: {
185
+ system: string;
186
+ prompt: string;
187
+ maxOutputTokens: number;
188
+ signal?: AbortSignal;
189
+ /**
190
+ * Call this when the model stopped because it reached `maxOutputTokens`, so a whole-document
191
+ * read can say its result is incomplete. Optional: an implementation that never calls it works
192
+ * as before, without that warning.
193
+ */
194
+ onTruncated?: () => void;
195
+ }) => Promise<string>;
196
+
197
+ export type ProgressEvent = {
198
+ kind: "map" | "reduce";
199
+ message: string;
200
+ done: number;
201
+ total: number;
202
+ };
203
+
204
+ export type ProgressListener = (event: ProgressEvent) => void;
205
+
206
+ export type DocumentPlan = {
207
+ /** Documents whose full text goes into the prompt. */
208
+ inline: DocumentMeta[];
209
+ /** Documents the model reaches only through tools. */
210
+ searchable: DocumentMeta[];
211
+ inlineTokens: number;
212
+ inlineBytes: number;
213
+ };
214
+
215
+ export type PreparedContext = {
216
+ plan: DocumentPlan;
217
+ /** Prompt text per document ID: the full text for inline documents, a summary card otherwise. */
218
+ blocks: Map<string, string>;
219
+ /** System instruction to add when any document is searchable or viewable, else an empty string. */
220
+ instruction: string;
221
+ /** IDs that were not found (never uploaded, deleted, or expired). */
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[];
228
+ };