@credal/actions 0.2.44 → 0.2.45
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.
@@ -10,6 +10,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
10
10
|
import { axiosClient } from "../../util/axiosClient.js";
|
11
11
|
import mammoth from "mammoth";
|
12
12
|
import { MISSING_AUTH_TOKEN } from "../../util/missingAuthConstants.js";
|
13
|
+
import { extractTextFromPdf } from "../../../utils/pdf.js";
|
13
14
|
const getDriveFileContentById = (_a) => __awaiter(void 0, [_a], void 0, function* ({ params, authParams, }) {
|
14
15
|
if (!authParams.authToken) {
|
15
16
|
return { success: false, error: MISSING_AUTH_TOKEN };
|
@@ -75,10 +76,24 @@ const getDriveFileContentById = (_a) => __awaiter(void 0, [_a], void 0, function
|
|
75
76
|
content = exportRes.data;
|
76
77
|
}
|
77
78
|
else if (mimeType === "application/pdf") {
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
79
|
+
// PDF files - download and extract text using pdfjs-dist
|
80
|
+
const downloadUrl = `https://www.googleapis.com/drive/v3/files/${encodeURIComponent(fileId)}?alt=media${sharedDriveParams}`;
|
81
|
+
const downloadRes = yield axiosClient.get(downloadUrl, {
|
82
|
+
headers: {
|
83
|
+
Authorization: `Bearer ${authParams.authToken}`,
|
84
|
+
},
|
85
|
+
responseType: "arraybuffer",
|
86
|
+
});
|
87
|
+
try {
|
88
|
+
content = yield extractTextFromPdf(downloadRes.data);
|
89
|
+
}
|
90
|
+
catch (e) {
|
91
|
+
return {
|
92
|
+
success: false,
|
93
|
+
error: `Failed to parse PDF document: ${e instanceof Error ? e.message : JSON.stringify(e)}`,
|
94
|
+
};
|
95
|
+
}
|
96
|
+
// Extract text from PDF
|
82
97
|
}
|
83
98
|
else if (mimeType === "application/vnd.openxmlformats-officedocument.wordprocessingml.document" ||
|
84
99
|
mimeType === "application/msword") {
|
@@ -0,0 +1 @@
|
|
1
|
+
export declare function extractTextFromPdf(buffer: ArrayBuffer): Promise<string>;
|
@@ -0,0 +1,40 @@
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
8
|
+
});
|
9
|
+
};
|
10
|
+
import PDFParser from "pdf2json";
|
11
|
+
export function extractTextFromPdf(buffer) {
|
12
|
+
return __awaiter(this, void 0, void 0, function* () {
|
13
|
+
try {
|
14
|
+
const extractedText = yield new Promise((resolve, reject) => {
|
15
|
+
const pdfParser = new PDFParser();
|
16
|
+
pdfParser.on("pdfParser_dataError", (errData) => {
|
17
|
+
reject(errData.parserError || new Error("PDF parsing failed"));
|
18
|
+
});
|
19
|
+
pdfParser.on("pdfParser_dataReady", (pdfData) => {
|
20
|
+
try {
|
21
|
+
const text = pdfData.Pages.map((page) => page.Texts.map((textItem) => {
|
22
|
+
// Handle cases where R array might be empty or have multiple runs
|
23
|
+
return textItem.R.map((run) => decodeURIComponent(run.T)).join("");
|
24
|
+
}).join("")).join("\n");
|
25
|
+
resolve(text);
|
26
|
+
}
|
27
|
+
catch (error) {
|
28
|
+
reject(error);
|
29
|
+
}
|
30
|
+
});
|
31
|
+
pdfParser.parseBuffer(Buffer.from(buffer));
|
32
|
+
});
|
33
|
+
return extractedText;
|
34
|
+
}
|
35
|
+
catch (error) {
|
36
|
+
console.error("Error extracting PDF text:", error);
|
37
|
+
throw error;
|
38
|
+
}
|
39
|
+
});
|
40
|
+
}
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@credal/actions",
|
3
|
-
"version": "0.2.
|
3
|
+
"version": "0.2.45",
|
4
4
|
"type": "module",
|
5
5
|
"description": "AI Actions by Credal AI",
|
6
6
|
"sideEffects": false,
|
@@ -61,6 +61,7 @@
|
|
61
61
|
"mongodb": "^6.13.1",
|
62
62
|
"node-forge": "^1.3.1",
|
63
63
|
"octokit": "^4.1.2",
|
64
|
+
"pdf2json": "^3.1.6",
|
64
65
|
"resend": "^4.1.2",
|
65
66
|
"snowflake-sdk": "^2.0.2",
|
66
67
|
"ts-node": "^10.9.2",
|