ocr-ai 1.0.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.
@@ -0,0 +1,355 @@
1
+ /**
2
+ * Supported AI providers
3
+ */
4
+ type AIProvider = 'gemini' | 'openai' | 'claude' | 'grok' | 'vertex';
5
+ /**
6
+ * Output format for extraction
7
+ */
8
+ type OutputFormat = 'text' | 'json';
9
+ /**
10
+ * Supported file types
11
+ */
12
+ type SupportedFileType = 'pdf' | 'image' | 'text';
13
+ /**
14
+ * Configuration for a specific AI provider
15
+ */
16
+ interface ProviderConfig {
17
+ apiKey: string;
18
+ model?: string;
19
+ }
20
+ /**
21
+ * Vertex AI specific configuration
22
+ */
23
+ interface VertexConfig$1 {
24
+ project: string;
25
+ location: string;
26
+ }
27
+ /**
28
+ * Main configuration for ExtractaAI
29
+ */
30
+ interface ExtractaConfig {
31
+ provider: AIProvider;
32
+ apiKey?: string;
33
+ model?: string;
34
+ /**
35
+ * Vertex AI configuration (required when provider is 'vertex')
36
+ */
37
+ vertexConfig?: VertexConfig$1;
38
+ }
39
+ /**
40
+ * Model-specific configuration parameters
41
+ */
42
+ interface ModelConfig {
43
+ /**
44
+ * Controls randomness (0.0 = deterministic, 1.0+ = more random)
45
+ */
46
+ temperature?: number;
47
+ /**
48
+ * Maximum tokens to generate in the response
49
+ */
50
+ maxTokens?: number;
51
+ /**
52
+ * Top-p (nucleus) sampling
53
+ */
54
+ topP?: number;
55
+ /**
56
+ * Top-k sampling (Gemini/Claude only)
57
+ */
58
+ topK?: number;
59
+ /**
60
+ * Stop sequences to end generation
61
+ */
62
+ stopSequences?: string[];
63
+ }
64
+ /**
65
+ * Options for extraction
66
+ */
67
+ interface ExtractionOptions {
68
+ /**
69
+ * Output format: 'text' for plain text, 'json' for structured JSON
70
+ */
71
+ format?: OutputFormat;
72
+ /**
73
+ * JSON schema to validate/structure the output (only for format: 'json')
74
+ * Can be a JSON Schema object or a simple object describing the structure
75
+ */
76
+ schema?: Record<string, unknown>;
77
+ /**
78
+ * Custom prompt to guide the extraction
79
+ */
80
+ prompt?: string;
81
+ /**
82
+ * Language for extraction (default: 'auto')
83
+ */
84
+ language?: string;
85
+ /**
86
+ * Output file path (if you want to save to disk)
87
+ */
88
+ outputPath?: string;
89
+ /**
90
+ * Model-specific configuration (temperature, maxTokens, etc.)
91
+ */
92
+ modelConfig?: ModelConfig;
93
+ }
94
+ /**
95
+ * Result of text extraction
96
+ */
97
+ interface TextExtractionResult {
98
+ success: true;
99
+ format: 'text';
100
+ content: string;
101
+ metadata: ExtractionMetadata;
102
+ }
103
+ /**
104
+ * Result of JSON extraction
105
+ */
106
+ interface JsonExtractionResult<T = Record<string, unknown>> {
107
+ success: true;
108
+ format: 'json';
109
+ data: T;
110
+ metadata: ExtractionMetadata;
111
+ }
112
+ /**
113
+ * Error result
114
+ */
115
+ interface ExtractionError {
116
+ success: false;
117
+ error: string;
118
+ code: string;
119
+ }
120
+ /**
121
+ * Combined extraction result type
122
+ */
123
+ type ExtractionResult<T = Record<string, unknown>> = TextExtractionResult | JsonExtractionResult<T> | ExtractionError;
124
+ /**
125
+ * Token usage information
126
+ */
127
+ interface TokenUsage {
128
+ inputTokens: number;
129
+ outputTokens: number;
130
+ totalTokens: number;
131
+ }
132
+ /**
133
+ * Metadata about the extraction
134
+ */
135
+ interface ExtractionMetadata {
136
+ provider: AIProvider;
137
+ model: string;
138
+ fileType: SupportedFileType;
139
+ fileName: string;
140
+ processingTimeMs: number;
141
+ tokens?: TokenUsage;
142
+ }
143
+ /**
144
+ * File information after loading
145
+ */
146
+ interface FileInfo {
147
+ path: string;
148
+ name: string;
149
+ type: SupportedFileType;
150
+ mimeType: string;
151
+ size: number;
152
+ content: Buffer;
153
+ base64?: string;
154
+ }
155
+ /**
156
+ * Result from provider extraction including tokens
157
+ */
158
+ interface ProviderResult<T = string> {
159
+ content: T;
160
+ tokens?: TokenUsage;
161
+ }
162
+ /**
163
+ * Interface that all AI providers must implement
164
+ */
165
+ interface IAIProvider {
166
+ readonly name: AIProvider;
167
+ readonly model: string;
168
+ /**
169
+ * Extract text from a file
170
+ */
171
+ extractText(file: FileInfo, options?: ExtractionOptions): Promise<ProviderResult<string>>;
172
+ /**
173
+ * Extract structured JSON from a file
174
+ */
175
+ extractJson<T = Record<string, unknown>>(file: FileInfo, schema: Record<string, unknown>, options?: ExtractionOptions): Promise<ProviderResult<T>>;
176
+ /**
177
+ * Check if the provider supports the given file type
178
+ */
179
+ supportsFileType(type: SupportedFileType): boolean;
180
+ }
181
+
182
+ /**
183
+ * Main class for document extraction using AI
184
+ */
185
+ declare class ExtractaAI {
186
+ private provider;
187
+ private config;
188
+ constructor(config: ExtractaConfig);
189
+ /**
190
+ * Create a provider instance based on configuration
191
+ */
192
+ private createProvider;
193
+ /**
194
+ * Extract content from a file path or URL
195
+ */
196
+ extract(source: string, options?: ExtractionOptions): Promise<ExtractionResult>;
197
+ /**
198
+ * Extract content from a Buffer
199
+ */
200
+ extractFromBuffer(buffer: Buffer, fileName: string, options?: ExtractionOptions): Promise<ExtractionResult>;
201
+ /**
202
+ * Extract content from a base64 string
203
+ */
204
+ extractFromBase64(base64: string, fileName: string, options?: ExtractionOptions): Promise<ExtractionResult>;
205
+ /**
206
+ * Process the extraction based on format
207
+ */
208
+ private processExtraction;
209
+ /**
210
+ * Create an error result
211
+ */
212
+ private createErrorResult;
213
+ /**
214
+ * Get current provider name
215
+ */
216
+ getProvider(): AIProvider;
217
+ /**
218
+ * Get current model
219
+ */
220
+ getModel(): string;
221
+ /**
222
+ * Change the AI provider
223
+ */
224
+ setProvider(provider: AIProvider, apiKey: string, model?: string): void;
225
+ }
226
+ /**
227
+ * Factory function to create ExtractaAI instance
228
+ */
229
+ declare function createExtractaAI(config: ExtractaConfig): ExtractaAI;
230
+
231
+ /**
232
+ * Load a file from disk and prepare it for AI processing
233
+ */
234
+ declare function loadFile(filePath: string): Promise<FileInfo>;
235
+ /**
236
+ * Load a file from a Buffer
237
+ */
238
+ declare function loadFileFromBuffer(buffer: Buffer, fileName: string, mimeType?: string): FileInfo;
239
+ /**
240
+ * Load a file from base64 string
241
+ */
242
+ declare function loadFileFromBase64(base64: string, fileName: string, mimeType?: string): FileInfo;
243
+ /**
244
+ * Save content to a file
245
+ */
246
+ declare function saveToFile(filePath: string, content: string | Buffer): Promise<void>;
247
+ /**
248
+ * Get supported file extensions
249
+ */
250
+ declare function getSupportedExtensions(): string[];
251
+ /**
252
+ * Check if a file extension is supported
253
+ */
254
+ declare function isExtensionSupported(ext: string): boolean;
255
+ /**
256
+ * Check if a string is a URL
257
+ */
258
+ declare function isUrl(str: string): boolean;
259
+ /**
260
+ * Load a file from a URL
261
+ */
262
+ declare function loadFileFromUrl(url: string): Promise<FileInfo>;
263
+
264
+ /**
265
+ * Base class for AI providers with common functionality
266
+ */
267
+ declare abstract class BaseProvider implements IAIProvider {
268
+ abstract readonly name: AIProvider;
269
+ abstract readonly model: string;
270
+ protected apiKey: string;
271
+ constructor(apiKey: string);
272
+ abstract extractText(file: FileInfo, options?: ExtractionOptions): Promise<ProviderResult<string>>;
273
+ abstract extractJson<T = Record<string, unknown>>(file: FileInfo, schema: Record<string, unknown>, options?: ExtractionOptions): Promise<ProviderResult<T>>;
274
+ supportsFileType(type: SupportedFileType): boolean;
275
+ /**
276
+ * Build the text extraction prompt
277
+ */
278
+ protected buildTextPrompt(options?: ExtractionOptions): string;
279
+ /**
280
+ * Build the JSON extraction prompt
281
+ */
282
+ protected buildJsonPrompt(schema: Record<string, unknown>, options?: ExtractionOptions): string;
283
+ /**
284
+ * Parse JSON response from AI, handling potential formatting issues
285
+ */
286
+ protected parseJsonResponse<T>(response: string): T;
287
+ }
288
+
289
+ declare class GeminiProvider extends BaseProvider {
290
+ readonly name: AIProvider;
291
+ readonly model: string;
292
+ private client;
293
+ constructor(apiKey: string, model?: string);
294
+ extractText(file: FileInfo, options?: ExtractionOptions): Promise<ProviderResult<string>>;
295
+ extractJson<T = Record<string, unknown>>(file: FileInfo, schema: Record<string, unknown>, options?: ExtractionOptions): Promise<ProviderResult<T>>;
296
+ private buildGenerationConfig;
297
+ private extractTokenUsage;
298
+ private buildContent;
299
+ }
300
+
301
+ declare class OpenAIProvider extends BaseProvider {
302
+ readonly name: AIProvider;
303
+ readonly model: string;
304
+ private client;
305
+ constructor(apiKey: string, model?: string);
306
+ extractText(file: FileInfo, options?: ExtractionOptions): Promise<ProviderResult<string>>;
307
+ extractJson<T = Record<string, unknown>>(file: FileInfo, schema: Record<string, unknown>, options?: ExtractionOptions): Promise<ProviderResult<T>>;
308
+ private buildCompletionOptions;
309
+ private extractTokenUsage;
310
+ private buildMessages;
311
+ }
312
+
313
+ declare class ClaudeProvider extends BaseProvider {
314
+ readonly name: AIProvider;
315
+ readonly model: string;
316
+ private client;
317
+ constructor(apiKey: string, model?: string);
318
+ extractText(file: FileInfo, options?: ExtractionOptions): Promise<ProviderResult<string>>;
319
+ extractJson<T = Record<string, unknown>>(file: FileInfo, schema: Record<string, unknown>, options?: ExtractionOptions): Promise<ProviderResult<T>>;
320
+ private buildMessageOptions;
321
+ supportsFileType(type: SupportedFileType): boolean;
322
+ private extractTokenUsage;
323
+ private buildContent;
324
+ private getMediaType;
325
+ }
326
+
327
+ declare class GrokProvider extends BaseProvider {
328
+ readonly name: AIProvider;
329
+ readonly model: string;
330
+ private client;
331
+ constructor(apiKey: string, model?: string);
332
+ extractText(file: FileInfo, options?: ExtractionOptions): Promise<ProviderResult<string>>;
333
+ extractJson<T = Record<string, unknown>>(file: FileInfo, schema: Record<string, unknown>, options?: ExtractionOptions): Promise<ProviderResult<T>>;
334
+ private buildCompletionOptions;
335
+ private extractTokenUsage;
336
+ private buildMessages;
337
+ }
338
+
339
+ interface VertexConfig {
340
+ project: string;
341
+ location: string;
342
+ }
343
+ declare class VertexProvider extends BaseProvider {
344
+ readonly name: AIProvider;
345
+ readonly model: string;
346
+ private client;
347
+ constructor(config: VertexConfig, model?: string);
348
+ extractText(file: FileInfo, options?: ExtractionOptions): Promise<ProviderResult<string>>;
349
+ extractJson<T = Record<string, unknown>>(file: FileInfo, schema: Record<string, unknown>, options?: ExtractionOptions): Promise<ProviderResult<T>>;
350
+ private buildGenerationConfig;
351
+ private extractTokenUsage;
352
+ private buildContents;
353
+ }
354
+
355
+ export { type AIProvider, BaseProvider, ClaudeProvider, ExtractaAI, type ExtractaConfig, type ExtractionError, type ExtractionMetadata, type ExtractionOptions, type ExtractionResult, type FileInfo, GeminiProvider, GrokProvider, type IAIProvider, type JsonExtractionResult, type ModelConfig, OpenAIProvider, type OutputFormat, type ProviderConfig, type SupportedFileType, type TextExtractionResult, type TokenUsage, type VertexConfig$1 as VertexConfig, VertexProvider, createExtractaAI, getSupportedExtensions, isExtensionSupported, isUrl, loadFile, loadFileFromBase64, loadFileFromBuffer, loadFileFromUrl, saveToFile };