ocr-ai 1.0.3 → 1.0.4

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/README.md CHANGED
@@ -453,6 +453,126 @@ const ocr = new OcrAI({
453
453
  | Real-time applications | `gpt-4.1-nano` |
454
454
  | Budget-conscious projects | `gpt-4o-mini` |
455
455
 
456
+ ## Promise API
457
+
458
+ For users who prefer callbacks or need more control over async operations, `ocr-ai` provides an alternative `OcrAIPromise` class with additional features.
459
+
460
+ ### Basic Usage with Callbacks
461
+
462
+ ```typescript
463
+ import { OcrAIPromise } from 'ocr-ai';
464
+
465
+ const ocr = new OcrAIPromise({
466
+ provider: 'gemini',
467
+ apiKey: 'YOUR_API_KEY',
468
+ });
469
+
470
+ // Using callback style
471
+ ocr.extract('./invoice.png', {}, (error, result) => {
472
+ if (error) {
473
+ console.error('Extraction failed:', error.message);
474
+ return;
475
+ }
476
+ console.log('Extracted:', result.content);
477
+ });
478
+ ```
479
+
480
+ ### Using .then()/.catch()
481
+
482
+ ```typescript
483
+ import { OcrAIPromise } from 'ocr-ai';
484
+
485
+ const ocr = new OcrAIPromise({
486
+ provider: 'gemini',
487
+ apiKey: 'YOUR_API_KEY',
488
+ });
489
+
490
+ // Promise chain style
491
+ ocr.extract('./invoice.png')
492
+ .then((result) => {
493
+ if (result.success) {
494
+ console.log('Content:', result.content);
495
+ }
496
+ })
497
+ .catch((error) => {
498
+ console.error('Error:', error);
499
+ });
500
+ ```
501
+
502
+ ### Extract Multiple Files in Parallel
503
+
504
+ ```typescript
505
+ const ocr = new OcrAIPromise({
506
+ provider: 'gemini',
507
+ apiKey: 'YOUR_API_KEY',
508
+ });
509
+
510
+ // Extract many files at once
511
+ const results = await ocr.extractMany([
512
+ './invoice1.png',
513
+ './invoice2.png',
514
+ './invoice3.png',
515
+ ]);
516
+
517
+ results.forEach((result, index) => {
518
+ if (result.success) {
519
+ console.log(`File ${index + 1}:`, result.content);
520
+ }
521
+ });
522
+ ```
523
+
524
+ ### Batch Extraction with Individual Options
525
+
526
+ ```typescript
527
+ const ocr = new OcrAIPromise({
528
+ provider: 'gemini',
529
+ apiKey: 'YOUR_API_KEY',
530
+ });
531
+
532
+ // Each file with its own options
533
+ const results = await ocr.extractBatch([
534
+ { source: './invoice.png', options: { format: 'json', schema: invoiceSchema } },
535
+ { source: './receipt.png', options: { format: 'text' } },
536
+ { source: './contract.pdf', options: { prompt: 'Extract key dates and amounts' } },
537
+ ]);
538
+ ```
539
+
540
+ ### Automatic Retry on Failure
541
+
542
+ ```typescript
543
+ const ocr = new OcrAIPromise({
544
+ provider: 'gemini',
545
+ apiKey: 'YOUR_API_KEY',
546
+ });
547
+
548
+ // Retry up to 3 times with 1 second delay between attempts
549
+ const result = await ocr.extractWithRetry(
550
+ './invoice.png',
551
+ { format: 'json', schema: invoiceSchema },
552
+ 3, // retries
553
+ 1000 // delay in ms
554
+ );
555
+
556
+ if (result.success) {
557
+ console.log('Extracted after retries:', result.data);
558
+ }
559
+ ```
560
+
561
+ ### Access Underlying OcrAI Instance
562
+
563
+ ```typescript
564
+ const ocrPromise = new OcrAIPromise({
565
+ provider: 'gemini',
566
+ apiKey: 'YOUR_API_KEY',
567
+ });
568
+
569
+ // Get the underlying OcrAI instance for direct access
570
+ const ocr = ocrPromise.getOcrAI();
571
+
572
+ // Use standard async/await if needed
573
+ const result = await ocr.extract('./invoice.png');
574
+ ```
575
+
456
576
  ## License
457
577
 
458
578
  MIT
package/dist/index.d.mts CHANGED
@@ -235,6 +235,79 @@ declare class OcrAI {
235
235
  * Factory function to create OcrAI instance
236
236
  */
237
237
  declare function createOcrAI(config: OcrConfig): OcrAI;
238
+ /**
239
+ * Callback types for Promise-based API
240
+ */
241
+ type ExtractionCallback<T = ExtractionResult> = (error: Error | null, result?: T) => void;
242
+ /**
243
+ * Promise-based wrapper for OcrAI with callback support
244
+ * Provides an alternative async API for users who prefer callbacks or need more control over promise handling
245
+ */
246
+ declare class OcrAIPromise {
247
+ private ocr;
248
+ constructor(config: OcrConfig);
249
+ /**
250
+ * Extract content from a file path or URL with callback support
251
+ * @param source - File path or URL
252
+ * @param options - Extraction options
253
+ * @param callback - Optional callback (error, result)
254
+ * @returns Promise if no callback provided
255
+ */
256
+ extract(source: string, options?: ExtractionOptions, callback?: ExtractionCallback): Promise<ExtractionResult> | void;
257
+ /**
258
+ * Extract content from a Buffer with callback support
259
+ */
260
+ extractFromBuffer(buffer: Buffer, fileName: string, options?: ExtractionOptions, callback?: ExtractionCallback): Promise<ExtractionResult> | void;
261
+ /**
262
+ * Extract content from a base64 string with callback support
263
+ */
264
+ extractFromBase64(base64: string, fileName: string, options?: ExtractionOptions, callback?: ExtractionCallback): Promise<ExtractionResult> | void;
265
+ /**
266
+ * Extract multiple files in parallel
267
+ * @param sources - Array of file paths or URLs
268
+ * @param options - Extraction options (applied to all)
269
+ * @returns Promise resolving to array of results
270
+ */
271
+ extractMany(sources: string[], options?: ExtractionOptions): Promise<ExtractionResult[]>;
272
+ /**
273
+ * Extract multiple files with individual options
274
+ * @param items - Array of { source, options } objects
275
+ * @returns Promise resolving to array of results
276
+ */
277
+ extractBatch(items: Array<{
278
+ source: string;
279
+ options?: ExtractionOptions;
280
+ }>): Promise<ExtractionResult[]>;
281
+ /**
282
+ * Extract with automatic retry on failure
283
+ * @param source - File path or URL
284
+ * @param options - Extraction options
285
+ * @param retries - Number of retries (default: 3)
286
+ * @param delayMs - Delay between retries in ms (default: 1000)
287
+ * @returns Promise resolving to extraction result
288
+ */
289
+ extractWithRetry(source: string, options?: ExtractionOptions, retries?: number, delayMs?: number): Promise<ExtractionResult>;
290
+ /**
291
+ * Get the underlying OcrAI instance
292
+ */
293
+ getOcrAI(): OcrAI;
294
+ /**
295
+ * Get current provider name
296
+ */
297
+ getProvider(): AIProvider;
298
+ /**
299
+ * Get current model
300
+ */
301
+ getModel(): string;
302
+ /**
303
+ * Change the AI provider
304
+ */
305
+ setProvider(provider: AIProvider, apiKey: string, model?: string): void;
306
+ }
307
+ /**
308
+ * Factory function to create OcrAIPromise instance
309
+ */
310
+ declare function createOcrAIPromise(config: OcrConfig): OcrAIPromise;
238
311
 
239
312
  /**
240
313
  * Load a file from disk and prepare it for AI processing
@@ -360,4 +433,4 @@ declare class VertexProvider extends BaseProvider {
360
433
  private buildContents;
361
434
  }
362
435
 
363
- export { type AIProvider, BaseProvider, ClaudeProvider, type ExtractionError, type ExtractionMetadata, type ExtractionOptions, type ExtractionResult, type FileInfo, GeminiProvider, GrokProvider, type IAIProvider, type JsonExtractionResult, type ModelConfig, OcrAI, type OcrConfig, OpenAIProvider, type OutputFormat, type ProviderConfig, type SupportedFileType, type TextExtractionResult, type TokenUsage, type VertexConfig$1 as VertexConfig, VertexProvider, createOcrAI, getSupportedExtensions, isExtensionSupported, isUrl, loadFile, loadFileFromBase64, loadFileFromBuffer, loadFileFromUrl, saveToFile };
436
+ export { type AIProvider, BaseProvider, ClaudeProvider, type ExtractionCallback, type ExtractionError, type ExtractionMetadata, type ExtractionOptions, type ExtractionResult, type FileInfo, GeminiProvider, GrokProvider, type IAIProvider, type JsonExtractionResult, type ModelConfig, OcrAI, OcrAIPromise, type OcrConfig, OpenAIProvider, type OutputFormat, type ProviderConfig, type SupportedFileType, type TextExtractionResult, type TokenUsage, type VertexConfig$1 as VertexConfig, VertexProvider, createOcrAI, createOcrAIPromise, getSupportedExtensions, isExtensionSupported, isUrl, loadFile, loadFileFromBase64, loadFileFromBuffer, loadFileFromUrl, saveToFile };
package/dist/index.d.ts CHANGED
@@ -235,6 +235,79 @@ declare class OcrAI {
235
235
  * Factory function to create OcrAI instance
236
236
  */
237
237
  declare function createOcrAI(config: OcrConfig): OcrAI;
238
+ /**
239
+ * Callback types for Promise-based API
240
+ */
241
+ type ExtractionCallback<T = ExtractionResult> = (error: Error | null, result?: T) => void;
242
+ /**
243
+ * Promise-based wrapper for OcrAI with callback support
244
+ * Provides an alternative async API for users who prefer callbacks or need more control over promise handling
245
+ */
246
+ declare class OcrAIPromise {
247
+ private ocr;
248
+ constructor(config: OcrConfig);
249
+ /**
250
+ * Extract content from a file path or URL with callback support
251
+ * @param source - File path or URL
252
+ * @param options - Extraction options
253
+ * @param callback - Optional callback (error, result)
254
+ * @returns Promise if no callback provided
255
+ */
256
+ extract(source: string, options?: ExtractionOptions, callback?: ExtractionCallback): Promise<ExtractionResult> | void;
257
+ /**
258
+ * Extract content from a Buffer with callback support
259
+ */
260
+ extractFromBuffer(buffer: Buffer, fileName: string, options?: ExtractionOptions, callback?: ExtractionCallback): Promise<ExtractionResult> | void;
261
+ /**
262
+ * Extract content from a base64 string with callback support
263
+ */
264
+ extractFromBase64(base64: string, fileName: string, options?: ExtractionOptions, callback?: ExtractionCallback): Promise<ExtractionResult> | void;
265
+ /**
266
+ * Extract multiple files in parallel
267
+ * @param sources - Array of file paths or URLs
268
+ * @param options - Extraction options (applied to all)
269
+ * @returns Promise resolving to array of results
270
+ */
271
+ extractMany(sources: string[], options?: ExtractionOptions): Promise<ExtractionResult[]>;
272
+ /**
273
+ * Extract multiple files with individual options
274
+ * @param items - Array of { source, options } objects
275
+ * @returns Promise resolving to array of results
276
+ */
277
+ extractBatch(items: Array<{
278
+ source: string;
279
+ options?: ExtractionOptions;
280
+ }>): Promise<ExtractionResult[]>;
281
+ /**
282
+ * Extract with automatic retry on failure
283
+ * @param source - File path or URL
284
+ * @param options - Extraction options
285
+ * @param retries - Number of retries (default: 3)
286
+ * @param delayMs - Delay between retries in ms (default: 1000)
287
+ * @returns Promise resolving to extraction result
288
+ */
289
+ extractWithRetry(source: string, options?: ExtractionOptions, retries?: number, delayMs?: number): Promise<ExtractionResult>;
290
+ /**
291
+ * Get the underlying OcrAI instance
292
+ */
293
+ getOcrAI(): OcrAI;
294
+ /**
295
+ * Get current provider name
296
+ */
297
+ getProvider(): AIProvider;
298
+ /**
299
+ * Get current model
300
+ */
301
+ getModel(): string;
302
+ /**
303
+ * Change the AI provider
304
+ */
305
+ setProvider(provider: AIProvider, apiKey: string, model?: string): void;
306
+ }
307
+ /**
308
+ * Factory function to create OcrAIPromise instance
309
+ */
310
+ declare function createOcrAIPromise(config: OcrConfig): OcrAIPromise;
238
311
 
239
312
  /**
240
313
  * Load a file from disk and prepare it for AI processing
@@ -360,4 +433,4 @@ declare class VertexProvider extends BaseProvider {
360
433
  private buildContents;
361
434
  }
362
435
 
363
- export { type AIProvider, BaseProvider, ClaudeProvider, type ExtractionError, type ExtractionMetadata, type ExtractionOptions, type ExtractionResult, type FileInfo, GeminiProvider, GrokProvider, type IAIProvider, type JsonExtractionResult, type ModelConfig, OcrAI, type OcrConfig, OpenAIProvider, type OutputFormat, type ProviderConfig, type SupportedFileType, type TextExtractionResult, type TokenUsage, type VertexConfig$1 as VertexConfig, VertexProvider, createOcrAI, getSupportedExtensions, isExtensionSupported, isUrl, loadFile, loadFileFromBase64, loadFileFromBuffer, loadFileFromUrl, saveToFile };
436
+ export { type AIProvider, BaseProvider, ClaudeProvider, type ExtractionCallback, type ExtractionError, type ExtractionMetadata, type ExtractionOptions, type ExtractionResult, type FileInfo, GeminiProvider, GrokProvider, type IAIProvider, type JsonExtractionResult, type ModelConfig, OcrAI, OcrAIPromise, type OcrConfig, OpenAIProvider, type OutputFormat, type ProviderConfig, type SupportedFileType, type TextExtractionResult, type TokenUsage, type VertexConfig$1 as VertexConfig, VertexProvider, createOcrAI, createOcrAIPromise, getSupportedExtensions, isExtensionSupported, isUrl, loadFile, loadFileFromBase64, loadFileFromBuffer, loadFileFromUrl, saveToFile };
package/dist/index.js CHANGED
@@ -990,15 +990,144 @@ var OcrAI = class {
990
990
  function createOcrAI(config) {
991
991
  return new OcrAI(config);
992
992
  }
993
+ var OcrAIPromise = class {
994
+ ocr;
995
+ constructor(config) {
996
+ this.ocr = new OcrAI(config);
997
+ }
998
+ /**
999
+ * Extract content from a file path or URL with callback support
1000
+ * @param source - File path or URL
1001
+ * @param options - Extraction options
1002
+ * @param callback - Optional callback (error, result)
1003
+ * @returns Promise if no callback provided
1004
+ */
1005
+ extract(source, options, callback) {
1006
+ const promise = this.ocr.extract(source, options);
1007
+ if (callback) {
1008
+ promise.then((result) => {
1009
+ if (result.success) {
1010
+ callback(null, result);
1011
+ } else {
1012
+ callback(new Error(result.error), result);
1013
+ }
1014
+ }).catch((error) => callback(error instanceof Error ? error : new Error(String(error))));
1015
+ return;
1016
+ }
1017
+ return promise;
1018
+ }
1019
+ /**
1020
+ * Extract content from a Buffer with callback support
1021
+ */
1022
+ extractFromBuffer(buffer, fileName, options, callback) {
1023
+ const promise = this.ocr.extractFromBuffer(buffer, fileName, options);
1024
+ if (callback) {
1025
+ promise.then((result) => {
1026
+ if (result.success) {
1027
+ callback(null, result);
1028
+ } else {
1029
+ callback(new Error(result.error), result);
1030
+ }
1031
+ }).catch((error) => callback(error instanceof Error ? error : new Error(String(error))));
1032
+ return;
1033
+ }
1034
+ return promise;
1035
+ }
1036
+ /**
1037
+ * Extract content from a base64 string with callback support
1038
+ */
1039
+ extractFromBase64(base64, fileName, options, callback) {
1040
+ const promise = this.ocr.extractFromBase64(base64, fileName, options);
1041
+ if (callback) {
1042
+ promise.then((result) => {
1043
+ if (result.success) {
1044
+ callback(null, result);
1045
+ } else {
1046
+ callback(new Error(result.error), result);
1047
+ }
1048
+ }).catch((error) => callback(error instanceof Error ? error : new Error(String(error))));
1049
+ return;
1050
+ }
1051
+ return promise;
1052
+ }
1053
+ /**
1054
+ * Extract multiple files in parallel
1055
+ * @param sources - Array of file paths or URLs
1056
+ * @param options - Extraction options (applied to all)
1057
+ * @returns Promise resolving to array of results
1058
+ */
1059
+ extractMany(sources, options) {
1060
+ return Promise.all(sources.map((source) => this.ocr.extract(source, options)));
1061
+ }
1062
+ /**
1063
+ * Extract multiple files with individual options
1064
+ * @param items - Array of { source, options } objects
1065
+ * @returns Promise resolving to array of results
1066
+ */
1067
+ extractBatch(items) {
1068
+ return Promise.all(items.map((item) => this.ocr.extract(item.source, item.options)));
1069
+ }
1070
+ /**
1071
+ * Extract with automatic retry on failure
1072
+ * @param source - File path or URL
1073
+ * @param options - Extraction options
1074
+ * @param retries - Number of retries (default: 3)
1075
+ * @param delayMs - Delay between retries in ms (default: 1000)
1076
+ * @returns Promise resolving to extraction result
1077
+ */
1078
+ async extractWithRetry(source, options, retries = 3, delayMs = 1e3) {
1079
+ let lastResult;
1080
+ for (let attempt = 0; attempt <= retries; attempt++) {
1081
+ const result = await this.ocr.extract(source, options);
1082
+ if (result.success) {
1083
+ return result;
1084
+ }
1085
+ lastResult = result;
1086
+ if (attempt < retries) {
1087
+ await new Promise((resolve2) => setTimeout(resolve2, delayMs));
1088
+ }
1089
+ }
1090
+ return lastResult;
1091
+ }
1092
+ /**
1093
+ * Get the underlying OcrAI instance
1094
+ */
1095
+ getOcrAI() {
1096
+ return this.ocr;
1097
+ }
1098
+ /**
1099
+ * Get current provider name
1100
+ */
1101
+ getProvider() {
1102
+ return this.ocr.getProvider();
1103
+ }
1104
+ /**
1105
+ * Get current model
1106
+ */
1107
+ getModel() {
1108
+ return this.ocr.getModel();
1109
+ }
1110
+ /**
1111
+ * Change the AI provider
1112
+ */
1113
+ setProvider(provider, apiKey, model) {
1114
+ this.ocr.setProvider(provider, apiKey, model);
1115
+ }
1116
+ };
1117
+ function createOcrAIPromise(config) {
1118
+ return new OcrAIPromise(config);
1119
+ }
993
1120
 
994
1121
  exports.BaseProvider = BaseProvider;
995
1122
  exports.ClaudeProvider = ClaudeProvider;
996
1123
  exports.GeminiProvider = GeminiProvider;
997
1124
  exports.GrokProvider = GrokProvider;
998
1125
  exports.OcrAI = OcrAI;
1126
+ exports.OcrAIPromise = OcrAIPromise;
999
1127
  exports.OpenAIProvider = OpenAIProvider;
1000
1128
  exports.VertexProvider = VertexProvider;
1001
1129
  exports.createOcrAI = createOcrAI;
1130
+ exports.createOcrAIPromise = createOcrAIPromise;
1002
1131
  exports.getSupportedExtensions = getSupportedExtensions;
1003
1132
  exports.isExtensionSupported = isExtensionSupported;
1004
1133
  exports.isUrl = isUrl;