@exulu/backend 5.21.0 → 5.23.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.
@@ -1130,6 +1130,131 @@ var officeToText = async (bytes) => {
1130
1130
  return String(value);
1131
1131
  };
1132
1132
 
1133
+ // src/templates/tools/document-render-helpers.ts
1134
+ import { execFile } from "child_process";
1135
+ import { promisify } from "util";
1136
+ import { existsSync as existsSync2 } from "fs";
1137
+ import { mkdtemp, readdir, readFile, rm, writeFile } from "fs/promises";
1138
+ import { tmpdir } from "os";
1139
+ import { join } from "path";
1140
+ var execFileAsync = promisify(execFile);
1141
+ var MAX_STDOUT_BYTES = 64 * 1024 * 1024;
1142
+ var LEGACY_OFFICE_CONVERSION_TARGETS = {
1143
+ ".doc": "docx",
1144
+ ".xls": "xlsx",
1145
+ ".ppt": "pptx",
1146
+ ".rtf": "docx"
1147
+ };
1148
+ function isLegacyOfficeFormat(ext) {
1149
+ return ext in LEGACY_OFFICE_CONVERSION_TARGETS;
1150
+ }
1151
+ async function convertLegacyOfficeToModern(bytes, ext) {
1152
+ const target = LEGACY_OFFICE_CONVERSION_TARGETS[ext];
1153
+ if (!target) {
1154
+ throw new Error(`No modern-format conversion target registered for "${ext}"`);
1155
+ }
1156
+ const dir = await mkdtemp(join(tmpdir(), "exulu-officeconv-"));
1157
+ try {
1158
+ const inputPath = join(dir, `input${ext}`);
1159
+ await writeFile(inputPath, bytes);
1160
+ try {
1161
+ await execFileAsync(
1162
+ "soffice",
1163
+ ["--headless", "--convert-to", target, inputPath, "--outdir", dir],
1164
+ { timeout: 6e4, maxBuffer: MAX_STDOUT_BYTES }
1165
+ );
1166
+ } catch (err) {
1167
+ throw new Error(
1168
+ `LibreOffice could not convert the legacy "${ext}" file to ${target}: ` + (err instanceof Error ? err.message : String(err))
1169
+ );
1170
+ }
1171
+ const outputPath = join(dir, `input.${target}`);
1172
+ if (!existsSync2(outputPath)) {
1173
+ throw new Error(`LibreOffice produced no output converting "${ext}" to ${target}`);
1174
+ }
1175
+ return await readFile(outputPath);
1176
+ } finally {
1177
+ await rm(dir, { recursive: true, force: true });
1178
+ }
1179
+ }
1180
+ var PRESENTATION_EXTENSIONS = /* @__PURE__ */ new Set([".pptx", ".ppt", ".pptm", ".odp"]);
1181
+ function isPresentationFormat(ext) {
1182
+ return PRESENTATION_EXTENSIONS.has(ext.toLowerCase());
1183
+ }
1184
+ async function convertOfficeToPdf(bytes, ext) {
1185
+ const dir = await mkdtemp(join(tmpdir(), "exulu-office2pdf-"));
1186
+ try {
1187
+ const inputPath = join(dir, `input${ext.toLowerCase()}`);
1188
+ await writeFile(inputPath, bytes);
1189
+ try {
1190
+ await execFileAsync(
1191
+ "soffice",
1192
+ // Own profile directory: two conversions at once otherwise fight over the user profile lock.
1193
+ [`-env:UserInstallation=file://${join(dir, "profile")}`, "--headless", "--convert-to", "pdf", inputPath, "--outdir", dir],
1194
+ { timeout: 18e4, maxBuffer: MAX_STDOUT_BYTES }
1195
+ );
1196
+ } catch (err) {
1197
+ throw new Error(`LibreOffice could not convert the "${ext}" file to PDF: ` + (err instanceof Error ? err.message : String(err)));
1198
+ }
1199
+ const outputPath = join(dir, "input.pdf");
1200
+ if (!existsSync2(outputPath)) {
1201
+ throw new Error(`LibreOffice produced no PDF for the "${ext}" file`);
1202
+ }
1203
+ return await readFile(outputPath);
1204
+ } finally {
1205
+ await rm(dir, { recursive: true, force: true });
1206
+ }
1207
+ }
1208
+ async function pdfToText(pdf) {
1209
+ const dir = await mkdtemp(join(tmpdir(), "exulu-parse-"));
1210
+ try {
1211
+ const inputPath = join(dir, "input.pdf");
1212
+ await writeFile(inputPath, pdf);
1213
+ const { stdout } = await execFileAsync("pdftotext", ["-layout", inputPath, "-"], {
1214
+ timeout: 6e4,
1215
+ maxBuffer: MAX_STDOUT_BYTES
1216
+ });
1217
+ return stdout;
1218
+ } finally {
1219
+ await rm(dir, { recursive: true, force: true });
1220
+ }
1221
+ }
1222
+ async function pdfPageCount(pdf) {
1223
+ const dir = await mkdtemp(join(tmpdir(), "exulu-pdfinfo-"));
1224
+ try {
1225
+ const inputPath = join(dir, "input.pdf");
1226
+ await writeFile(inputPath, pdf);
1227
+ const { stdout } = await execFileAsync("pdfinfo", [inputPath], { timeout: 3e4, maxBuffer: 1e6 });
1228
+ const match = /^Pages:\s+(\d+)/m.exec(stdout);
1229
+ if (!match) throw new Error("pdfinfo did not report a page count");
1230
+ return Number(match[1]);
1231
+ } finally {
1232
+ await rm(dir, { recursive: true, force: true });
1233
+ }
1234
+ }
1235
+ async function renderPdfPageToPng(pdf, page, scaleTo) {
1236
+ const dir = await mkdtemp(join(tmpdir(), "exulu-render-"));
1237
+ try {
1238
+ const inputPath = join(dir, "input.pdf");
1239
+ await writeFile(inputPath, pdf);
1240
+ try {
1241
+ await execFileAsync(
1242
+ "pdftoppm",
1243
+ ["-png", "-f", String(page), "-l", String(page), "-scale-to", String(scaleTo), inputPath, join(dir, "page")],
1244
+ { timeout: 6e4, maxBuffer: MAX_STDOUT_BYTES }
1245
+ );
1246
+ } catch (err) {
1247
+ if (err?.code === 99) return null;
1248
+ throw err;
1249
+ }
1250
+ const produced = (await readdir(dir)).find((f) => f.startsWith("page") && f.endsWith(".png"));
1251
+ if (!produced) return null;
1252
+ return await readFile(join(dir, produced));
1253
+ } finally {
1254
+ await rm(dir, { recursive: true, force: true });
1255
+ }
1256
+ }
1257
+
1133
1258
  // ee/python/documents/processing/limits.ts
1134
1259
  import pLimit from "p-limit";
1135
1260
  var PROCESS_WIDE_LIMIT = 8;
@@ -1196,6 +1321,13 @@ export {
1196
1321
  ResolveModelError,
1197
1322
  resolveModel,
1198
1323
  officeToText,
1324
+ isLegacyOfficeFormat,
1325
+ convertLegacyOfficeToModern,
1326
+ isPresentationFormat,
1327
+ convertOfficeToPdf,
1328
+ pdfToText,
1329
+ pdfPageCount,
1330
+ renderPdfPageToPng,
1199
1331
  processWideLimiters,
1200
1332
  ocrConcurrency,
1201
1333
  withRetry
@@ -5,6 +5,7 @@ import {
5
5
  import {
6
6
  buildTags,
7
7
  checkLicense,
8
+ convertOfficeToPdf,
8
9
  isLiteLLMEnabled,
9
10
  ocrConcurrency,
10
11
  officeToText,
@@ -12,7 +13,7 @@ import {
12
13
  resolveModel,
13
14
  waitForLiteLLMReady,
14
15
  withRetry
15
- } from "./chunk-43QIPMLT.js";
16
+ } from "./chunk-BFIDSFRQ.js";
16
17
  import {
17
18
  resolveLiteLLMTarget
18
19
  } from "./chunk-6URU62NI.js";
@@ -706,6 +707,19 @@ async function processDocument(filePath, fileType, buffer, tempDir, config, verb
706
707
  case "webp":
707
708
  result = await processImage(buffer, paths, config, verbose, tolerateVisionFailures, onProgress, limiters);
708
709
  break;
710
+ // A presentation is rendered to PDF and then read like any PDF, with whatever
711
+ // processor is configured. The caller does not choose a parser by file type:
712
+ // slides keep their pictures, charts and screenshots through OCR and the vision pass.
713
+ case "pptx":
714
+ case "ppt":
715
+ case "pptm":
716
+ case "odp": {
717
+ const pdf = await convertOfficeToPdf(buffer, `.${stripped}`);
718
+ const pdfPath = path.join(tempDir, "converted.pdf");
719
+ await fs.promises.writeFile(pdfPath, pdf);
720
+ result = await processPdf(pdf, { ...paths, source: pdfPath }, config, verbose, tolerateVisionFailures, onProgress, limiters);
721
+ break;
722
+ }
709
723
  // Todo other file types with docx and officeparser
710
724
  default:
711
725
  throw new Error(`[EXULU] Unsupported file type: ${fileType}`);
@@ -946,13 +960,13 @@ async function documentProcessor({
946
960
  let supportedTypes = [];
947
961
  switch (config?.processor.name) {
948
962
  case "officeparser":
949
- supportedTypes = ["docx", "pptx", "xlsx", "odt", "odp", "ods", "pdf", "rtf", "csv", "md", "html"];
963
+ supportedTypes = ["docx", "pptx", "ppt", "pptm", "xlsx", "odt", "odp", "ods", "pdf", "rtf", "csv", "md", "html"];
950
964
  break;
951
965
  case "liteparse":
952
966
  supportedTypes = ["pdf", "doc", "docx", "docm", "odt", "rtf", "ppt", "pptx", "pptm", "odp", "xls", "xlsx", "xlsm", "ods", "csv", "tsv"];
953
967
  break;
954
968
  case "mistral":
955
- supportedTypes = ["pdf", "docx", "doc", "txt", "md", "jpg", "jpeg", "png", "gif", "webp"];
969
+ supportedTypes = ["pdf", "docx", "doc", "pptx", "ppt", "pptm", "odp", "txt", "md", "jpg", "jpeg", "png", "gif", "webp"];
956
970
  break;
957
971
  }
958
972
  if (!supportedTypes.includes(fileType.toLowerCase())) {