@credal/actions 0.2.44 → 0.2.46

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
- return {
79
- success: false,
80
- error: `Failed to parse PDF file - currently unsupported`,
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") {
@@ -136,6 +151,8 @@ const getDriveFileContentById = (_a) => __awaiter(void 0, [_a], void 0, function
136
151
  if (limit && content.length > limit) {
137
152
  content = content.substring(0, limit);
138
153
  }
154
+ // Replace all newline characters with spaces, then collapse multiple spaces
155
+ content = content.replace(/\r?\n+/g, " ").replace(/ +/g, " ");
139
156
  return {
140
157
  success: true,
141
158
  content,
@@ -25,33 +25,33 @@ const searchDriveByQueryAndGetFileContent = (_a) => __awaiter(void 0, [_a], void
25
25
  if (!searchResult.success) {
26
26
  return { success: false, error: searchResult.error, files: [] };
27
27
  }
28
- // For each file, fetch its content
29
- const filesWithContent = [];
28
+ // For each file, fetch its content in parallel
30
29
  const files = (_b = searchResult.files) !== null && _b !== void 0 ? _b : [];
31
- for (const file of files) {
30
+ const contentPromises = files.map((file) => __awaiter(void 0, void 0, void 0, function* () {
32
31
  try {
33
32
  const contentResult = yield getDriveFileContentById({
34
33
  params: { fileId: file.id, limit: fileSizeLimit !== null && fileSizeLimit !== void 0 ? fileSizeLimit : 1000 },
35
34
  authParams,
36
35
  });
37
- filesWithContent.push({
36
+ return {
38
37
  id: file.id,
39
38
  name: file.name,
40
39
  mimeType: file.mimeType,
41
40
  url: file.url,
42
41
  content: contentResult.success ? contentResult.content : undefined,
43
- });
42
+ };
44
43
  }
45
44
  catch (error) {
46
45
  console.error(`Error fetching content for file ${file.id}:`, error);
47
- filesWithContent.push({
46
+ return {
48
47
  id: file.id,
49
48
  name: file.name,
50
49
  mimeType: file.mimeType,
51
50
  url: file.url,
52
- });
51
+ };
53
52
  }
54
- }
53
+ }));
54
+ const filesWithContent = yield Promise.all(contentPromises);
55
55
  // Return combined results
56
56
  return { success: true, files: filesWithContent };
57
57
  });
@@ -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.44",
3
+ "version": "0.2.46",
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",