@truto/truto-jsonata 1.0.10 → 1.0.12

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
@@ -881,6 +881,23 @@ expression.evaluate({ file}).then(result => { console.log(result); });
881
881
 
882
882
  </details>
883
883
 
884
+ <details>
885
+ <summary>getDataUri(file)</summary>
886
+
887
+ Converts a `Blob` or `Buffer` or `Readable Stream` to a data URI.
888
+
889
+ **Example:**
890
+
891
+ ```javascript
892
+ import trutoJsonata from '@truto/truto-jsonata';
893
+ const file = new Blob(['Hello, World!'], { type: 'text/plain' });
894
+ const expression = trutoJsonata("getDataUri(file)");
895
+ expression.evaluate({ file}).then(result => { console.log(result); });
896
+ // Output: 'data:text/plain;base64,SGVsbG8sIFdvcmxkIQ=='
897
+ ```
898
+
899
+ </details>
900
+
884
901
  <details>
885
902
  <summary>blob(content, options)</summary>
886
903
 
@@ -1241,6 +1258,25 @@ expression3.evaluate({ jsonData: jsonData3, options: options3 }).then(result =>
1241
1258
 
1242
1259
  </details>
1243
1260
 
1261
+ <details>
1262
+ <summary>parseDocument(file)</summary>
1263
+
1264
+ Parses a document file (e.g., PDF, DOCX) and extracts text content.
1265
+
1266
+ **Example:**
1267
+
1268
+ ```javascript
1269
+ import trutoJsonata from '@truto/truto-jsonata';
1270
+ const file = new Blob(['Hello, World!'], { type: 'application/pdf' });
1271
+ const buffer = await file.arrayBuffer();
1272
+ const expression = trutoJsonata("$parseDocument(buffer)");
1273
+ expression.evaluate({ file}).then(result => { console.log(result); });
1274
+ // Output: 'Hello, World!'
1275
+
1276
+ ````
1277
+
1278
+ </details>
1279
+
1244
1280
  ### Markdown and Text Conversion
1245
1281
 
1246
1282
  <details>
@@ -1855,6 +1891,35 @@ expression.evaluate({ body, api_key }).then(result => { console.log(result); });
1855
1891
 
1856
1892
  </details>
1857
1893
 
1894
+ <details>
1895
+ <summary>recursiveCharacterTextSplitter(text, options)</summary>
1896
+
1897
+ Splits a text into an array of characters, words, or sentences, recursively.
1898
+
1899
+ **Parameters:**
1900
+
1901
+ - **text**: The input text to split.
1902
+ - **options**: An object containing the following properties:
1903
+ - **chunkSize**: The maximum number of characters, words, or sentences per chunk (default is `200`).
1904
+ - **chunkOverlap**: The number of characters, words, or sentences to overlap between chunks (default is `60`).
1905
+
1906
+ **Example Usage:**
1907
+
1908
+ ```javascript
1909
+ import trutoJsonata from '@truto/truto-jsonata';
1910
+
1911
+ const text = "Hello, World! This is a sample text.";
1912
+ const options = {
1913
+ chunkSize: 10,
1914
+ chunkOverlap: 3
1915
+ };
1916
+ const expression = trutoJsonata("$recursiveCharacterTextSplitter(text, options)");
1917
+ expression.evaluate(text).then(result => { console.log(result); });
1918
+ // Output: ["Hello, Wo", "lo, World", "rld! This", "is a samp", "ample text", "text."]
1919
+ ```
1920
+
1921
+ </details>
1922
+
1858
1923
 
1859
1924
  ### Miscellaneous
1860
1925
 
@@ -2015,3 +2080,23 @@ expression.evaluate({}).then(result => { console.log(result); });
2015
2080
  ```
2016
2081
 
2017
2082
  </details>
2083
+
2084
+
2085
+
2086
+ <details>
2087
+ <summary>chunk(arr,size)</summary>
2088
+
2089
+ Chunks an array into smaller arrays of a specified size.
2090
+
2091
+ **Example:**
2092
+
2093
+ ```javascript
2094
+ import trutoJsonata from '@truto/truto-jsonata';
2095
+
2096
+
2097
+ const expression = trutoJsonata("$chunk([1, 2, 3, 4, 5], 2)");
2098
+ expression.evaluate({}).then(result => { console.log(result); });
2099
+ // Output: [[1,2],[3,4],[5]]
2100
+ ```
2101
+
2102
+ </details>
package/dist/main.cjs CHANGED
@@ -9,6 +9,12 @@ var $dxT2C$turndownplugingfm = require("turndown-plugin-gfm");
9
9
  var $dxT2C$xmljs = require("xml-js");
10
10
  var $dxT2C$json2md = require("json2md");
11
11
  var $dxT2C$mime = require("mime");
12
+ var $dxT2C$pmap = require("p-map");
13
+ var $dxT2C$pretry = require("p-retry");
14
+ var $dxT2C$officeparser = require("officeparser");
15
+ var $dxT2C$filetype = require("file-type");
16
+ var $dxT2C$pdfjsserverless = require("pdfjs-serverless");
17
+ var $dxT2C$langchaintextsplitters = require("@langchain/textsplitters");
12
18
 
13
19
  function $parcel$interopDefault(a) {
14
20
  return a && a.__esModule ? a.default : a;
@@ -1349,23 +1355,134 @@ async function $2c639152271462a6$var$convertMarkdownToHtml(markdown, options) {
1349
1355
  var $2c639152271462a6$export$2e2bcd8739ae039 = $2c639152271462a6$var$convertMarkdownToHtml;
1350
1356
 
1351
1357
 
1358
+
1359
+
1360
+
1352
1361
  async function $caaee789061bb8bb$var$generateEmbeddingsCohere(body, api_key) {
1353
- const response = await fetch("https://api.cohere.com/v2/embed", {
1354
- method: "POST",
1355
- headers: {
1356
- accept: "application/json",
1357
- "content-type": "application/json",
1358
- "user-agent": "truto",
1359
- Authorization: `Bearer ${api_key}`
1360
- },
1361
- body: JSON.stringify(body)
1362
+ if (!(0, $dxT2C$lodashes.isEmpty)(body.texts)) {
1363
+ const chunks = (0, $dxT2C$lodashes.chunk)((0, $dxT2C$lodashes.castArray)(body.texts), 20);
1364
+ return await (0, ($parcel$interopDefault($dxT2C$pmap)))(chunks, async (chunk)=>{
1365
+ return await (0, ($parcel$interopDefault($dxT2C$pretry)))(async ()=>{
1366
+ const response = await fetch("https://api.cohere.com/v2/embed", {
1367
+ method: "POST",
1368
+ headers: {
1369
+ accept: "application/json",
1370
+ "content-type": "application/json",
1371
+ "user-agent": "truto",
1372
+ Authorization: `Bearer ${api_key}`
1373
+ },
1374
+ body: JSON.stringify({
1375
+ ...body,
1376
+ texts: chunk
1377
+ })
1378
+ });
1379
+ if (!response.ok) throw new Error(await response.text());
1380
+ return await response.json();
1381
+ }, {
1382
+ retries: 10,
1383
+ maxTimeout: 600000
1384
+ });
1385
+ }, {
1386
+ concurrency: 1
1387
+ });
1388
+ } else if (!(0, $dxT2C$lodashes.isEmpty)(body.images)) return await (0, ($parcel$interopDefault($dxT2C$pretry)))(async ()=>{
1389
+ const response = await fetch("https://api.cohere.com/v2/embed", {
1390
+ method: "POST",
1391
+ headers: {
1392
+ accept: "application/json",
1393
+ "content-type": "application/json",
1394
+ "user-agent": "truto",
1395
+ Authorization: `Bearer ${api_key}`
1396
+ },
1397
+ body: JSON.stringify(body)
1398
+ });
1399
+ if (!response.ok) throw new Error(await response.text());
1400
+ return await response.json();
1401
+ }, {
1402
+ retries: 10,
1403
+ maxTimeout: 600000
1362
1404
  });
1363
- if (!response.ok) throw new Error(await response.text());
1364
- return await response.json();
1365
1405
  }
1366
1406
  var $caaee789061bb8bb$export$2e2bcd8739ae039 = $caaee789061bb8bb$var$generateEmbeddingsCohere;
1367
1407
 
1368
1408
 
1409
+
1410
+
1411
+
1412
+
1413
+ async function $05e3378f7c17d263$var$parsePdf(buffer) {
1414
+ const data = buffer.buffer.slice(buffer?.byteOffset, buffer?.byteOffset + buffer?.byteLength);
1415
+ const { getDocument: getDocument } = await (0, $dxT2C$pdfjsserverless.resolvePDFJS)();
1416
+ const doc = await getDocument({
1417
+ data: data,
1418
+ useSystemFonts: true
1419
+ }).promise;
1420
+ // Get metadata and initialize output object
1421
+ const output = [];
1422
+ // Iterate through each page and fetch the text content
1423
+ for(let i = 1; i <= doc.numPages; i++){
1424
+ const page = await doc.getPage(i);
1425
+ const textContent = await page.getTextContent();
1426
+ const contents = textContent.items.map((item)=>(0, $dxT2C$lodashes.get)(item, "str")).join(" ");
1427
+ // Add page content to output
1428
+ if (contents) output.push((0, $dxT2C$lodashes.join)([
1429
+ `Page Number: ${i}`,
1430
+ contents
1431
+ ], "/\n"));
1432
+ }
1433
+ // Return the results as JSON
1434
+ return (0, $dxT2C$lodashes.join)(output, "\n");
1435
+ }
1436
+ async function $05e3378f7c17d263$var$parseDocument(file) {
1437
+ if (file instanceof ReadableStream) {
1438
+ const chunks = [];
1439
+ for await (const chunk of file)chunks.push(Buffer.from(chunk));
1440
+ const buffer = Buffer.concat(chunks);
1441
+ const fileExt = (await (0, $dxT2C$filetype.fileTypeFromBuffer)(buffer))?.ext;
1442
+ if (fileExt === "pdf") return await $05e3378f7c17d263$var$parsePdf(buffer);
1443
+ return await (0, $dxT2C$officeparser.parseOfficeAsync)(buffer);
1444
+ }
1445
+ return await (0, $dxT2C$officeparser.parseOfficeAsync)(file);
1446
+ }
1447
+ var $05e3378f7c17d263$export$2e2bcd8739ae039 = $05e3378f7c17d263$var$parseDocument;
1448
+
1449
+
1450
+
1451
+ function $fe4dcef142601b8c$export$e600492876ee595b(text, options = {
1452
+ chunkSize: 200,
1453
+ chunkOverlap: 60
1454
+ }) {
1455
+ const splitter = new (0, $dxT2C$langchaintextsplitters.RecursiveCharacterTextSplitter)(options);
1456
+ return splitter.splitText(text);
1457
+ }
1458
+
1459
+
1460
+
1461
+ async function $15c432f5f036a88a$var$getDataUri(file) {
1462
+ if (file instanceof ReadableStream) {
1463
+ const chunks = [];
1464
+ for await (const chunk of file)chunks.push(Buffer.from(chunk));
1465
+ const buffer = Buffer.concat(chunks);
1466
+ const base64Image = buffer.toString("base64");
1467
+ const mimeType = (await (0, $dxT2C$filetype.fileTypeFromBuffer)(buffer))?.mime;
1468
+ // Construct the data URI for a PNG image
1469
+ return `data:${mimeType};base64,${base64Image}`;
1470
+ }
1471
+ const arrayBuffer = file instanceof Blob ? await file.arrayBuffer() : file;
1472
+ const buffer = Buffer.from(arrayBuffer);
1473
+ const base64Image = buffer.toString("base64");
1474
+ const mimeType = (await (0, $dxT2C$filetype.fileTypeFromBuffer)(buffer))?.mime;
1475
+ return `data:${mimeType};base64,${base64Image}`;
1476
+ }
1477
+ var $15c432f5f036a88a$export$2e2bcd8739ae039 = $15c432f5f036a88a$var$getDataUri;
1478
+
1479
+
1480
+ async function $9a2529096849a04f$var$teeStream(stream) {
1481
+ return stream.tee();
1482
+ }
1483
+ var $9a2529096849a04f$export$2e2bcd8739ae039 = $9a2529096849a04f$var$teeStream;
1484
+
1485
+
1369
1486
  function $af351c41b7fd6f79$export$2e2bcd8739ae039(expression) {
1370
1487
  expression.registerFunction("dtFromIso", (0, $bab42b5e4be720d3$export$2e2bcd8739ae039));
1371
1488
  expression.registerFunction("base64decode", (0, $77081a2d6d46cd50$export$2e2bcd8739ae039));
@@ -1433,6 +1550,9 @@ function $af351c41b7fd6f79$export$2e2bcd8739ae039(expression) {
1433
1550
  expression.registerFunction("values", function(obj) {
1434
1551
  return (0, $dxT2C$lodashes.values)(obj);
1435
1552
  });
1553
+ expression.registerFunction("chunk", function(arr, size) {
1554
+ return (0, $dxT2C$lodashes.chunk)((0, $dxT2C$lodashes.castArray)(arr), size);
1555
+ });
1436
1556
  expression.registerFunction("wrap", function(value, wrapper, endWrapper) {
1437
1557
  return (0, $dxT2C$lodashes.join)([
1438
1558
  wrapper,
@@ -1440,6 +1560,10 @@ function $af351c41b7fd6f79$export$2e2bcd8739ae039(expression) {
1440
1560
  endWrapper || wrapper
1441
1561
  ], "");
1442
1562
  });
1563
+ expression.registerFunction("parseDocument", (0, $05e3378f7c17d263$export$2e2bcd8739ae039));
1564
+ expression.registerFunction("recursiveCharacterTextSplitter", (0, $fe4dcef142601b8c$export$e600492876ee595b));
1565
+ expression.registerFunction("getDataUri", (0, $15c432f5f036a88a$export$2e2bcd8739ae039));
1566
+ expression.registerFunction("teeStream", (0, $9a2529096849a04f$export$2e2bcd8739ae039));
1443
1567
  return expression;
1444
1568
  }
1445
1569