@hyperbytes/wappler-all-in-one-ai-v2 1.0.4 → 1.0.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hyperbytes/wappler-all-in-one-ai-v2",
3
- "version": "1.0.4",
3
+ "version": "1.0.5",
4
4
  "description": "Versitile interface to chatGPT, Gemini Claude with file analysis cababilities",
5
5
  "license": "MIT",
6
6
  "author": {
@@ -5,66 +5,79 @@ const fs = require('fs');
5
5
  const path = require('path');
6
6
  const XLSX = require('xlsx');
7
7
  const pdf = require('pdf-parse');
8
+ const { PDFExtract } = require('pdf.js-extract');
9
+ const pdfExtract = new PDFExtract();
8
10
  const sharp = require('sharp');
9
11
 
12
+
10
13
  const clamp = (val, min, max) => Math.max(min, Math.min(max, val));
11
14
 
12
15
  /**
13
16
  * HELPER: Compresses and converts image to Base64
14
17
  */
15
- async function getCompressedImageBase64(filePath) {
18
+ async function getFileContent(filePath) {
16
19
  try {
17
- // Remove leading slash if present to avoid path joining issues
18
20
  const cleanPath = filePath.startsWith('/') ? filePath.substring(1) : filePath;
19
21
  const fullPath = path.join(process.cwd(), cleanPath);
20
22
 
21
- console.log("Checking for image at:", fullPath);
22
-
23
23
  if (!fs.existsSync(fullPath)) {
24
24
  console.error("FILE NOT FOUND:", fullPath);
25
25
  return null;
26
26
  }
27
27
 
28
- const buffer = await sharp(fullPath)
29
- .rotate() // <--- CRITICAL: Fixes portrait/landscape flip
30
- .resize(1500, 1500, { fit: 'inside', withoutEnlargement: true })
31
- .jpeg({ quality: 80 })
32
- .toBuffer();
28
+ const ext = path.extname(fullPath).toLowerCase();
29
+ const dataBuffer = fs.readFileSync(fullPath);
33
30
 
34
- console.log("Image compressed successfully. Size:", (buffer.length / 1024).toFixed(2), "KB");
35
- return `data:image/jpeg;base64,${buffer.toString('base64')}`;
36
- } catch (err) {
37
- console.error("Sharp Compression Error:", err.message);
38
- return null;
39
- }
40
- }
31
+ if (ext === '.pdf') {
32
+ console.log("--- ATTEMPTING PDF EXTRACTION (INTERNAL REQUIRE) ---");
41
33
 
42
- /**
43
- * HELPER: Extract text from Docs
44
- */
45
- async function getFileContent(filePath) {
46
- try {
47
- const fullPath = path.join(process.cwd(), filePath);
48
- if (!fs.existsSync(fullPath)) return null;
49
- const ext = path.extname(fullPath).toLowerCase();
34
+ try {
35
+ // FORCE LOAD here to bypass Wappler's scope issues
36
+ const pdfParser = require('pdf-parse');
37
+
38
+ // If pdfParser itself is an object containing the function (Wappler quirk)
39
+ const actualFunction = typeof pdfParser === 'function' ? pdfParser : pdfParser.default;
40
+
41
+ if (typeof actualFunction !== 'function') {
42
+ throw new Error("Library loaded but no function found.");
43
+ }
44
+
45
+ const data = await actualFunction(dataBuffer, {
46
+ pagerender: async function (pageData) {
47
+ const textContent = await pageData.getTextContent();
48
+ return textContent.items.map(s => s.str).join(' ');
49
+ }
50
+ });
51
+
52
+ if (!data || !data.text) {
53
+ return "[SYSTEM: PDF read but no text found - likely a scan.]";
54
+ }
55
+
56
+ const extractedText = data.text.trim();
57
+ console.log("SUCCESS: Extracted", extractedText.length, "characters.");
58
+ return extractedText;
59
+
60
+ } catch (innerError) {
61
+ console.error("INTERNAL PDF ERROR:", innerError.message);
62
+ return `[SYSTEM: Error reading PDF structure: ${innerError.message}]`;
63
+ }
64
+ }
50
65
 
51
- if (ext === '.txt' || ext === '.csv') return fs.readFileSync(fullPath, 'utf8');
66
+ if (ext === '.txt' || ext === '.csv') return dataBuffer.toString('utf8');
52
67
  if (ext === '.xlsx' || ext === '.xls') {
53
- const wb = XLSX.readFile(fullPath);
68
+ const wb = XLSX.read(dataBuffer);
54
69
  return XLSX.utils.sheet_to_csv(wb.Sheets[wb.SheetNames[0]]);
55
70
  }
56
- if (ext === '.pdf') {
57
- const data = await pdf(fs.readFileSync(fullPath));
58
- return data.text;
59
- }
60
- } catch (e) { return null; }
61
- return null;
62
- }
63
71
 
72
+ } catch (e) {
73
+ console.error("GENERAL EXTRACTION ERROR:", e.message);
74
+ return null;
75
+ }
76
+ }
64
77
  exports.multiaiv2 = async function (options) {
65
78
 
66
- // 1. Ensure we grab the question correctly
67
- let question = this.parseRequired(options.question, "*", 'No Question passed');
79
+ // 1. Ensure we grab the question correctly and append any training instrutions
80
+ let question = this.parseRequired(options.question, "*", 'No Question passed') + ' ' + this.parseOptional(options.mytraining, "*", '');
68
81
 
69
82
  // 2. Fix the file input assignment
70
83
  // Use the exact key from your console log