@absolutejs/rag 0.2.0 → 0.4.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.
package/dist/index.js CHANGED
@@ -15103,6 +15103,8 @@ var emailStructureUnits = (value, metadata) => {
15103
15103
  return "Forwarded Headers";
15104
15104
  case "quoted_history":
15105
15105
  return "Quoted History";
15106
+ case "signature":
15107
+ return "Signature";
15106
15108
  }
15107
15109
  };
15108
15110
  const familyKeyBase = threadTopic || from || "Email Message";
@@ -17124,6 +17126,16 @@ var parseMaildirMetadata = (source) => {
17124
17126
  ...maildirKey ? { emailMailboxKey: maildirKey } : {}
17125
17127
  };
17126
17128
  };
17129
+ var RFC2047_WORD = /=\?([^?]+)\?([BbQq])\?([^?]*)\?=/g;
17130
+ var decodeEncodedWord = (charset, encoding, text) => {
17131
+ const bytes = encoding.toUpperCase() === "B" ? Buffer.from(text, "base64") : Buffer.from(decodeQuotedPrintable(text.replace(/_/g, " ")), "latin1");
17132
+ try {
17133
+ return new TextDecoder(charset.trim().toLowerCase()).decode(bytes);
17134
+ } catch {
17135
+ return bytes.toString("utf8");
17136
+ }
17137
+ };
17138
+ var decodeRfc2047 = (value) => value.includes("=?") ? value.replace(/\?=\s+=\?/g, "?==?").replace(RFC2047_WORD, (_match, charset, enc, text) => decodeEncodedWord(charset, enc, text)) : value;
17127
17139
  var parseHeaderBlock = (headerBlock) => {
17128
17140
  const unfolded = headerBlock.replace(/\n[ \t]+/g, " ");
17129
17141
  const headers = new Map;
@@ -17133,7 +17145,7 @@ var parseHeaderBlock = (headerBlock) => {
17133
17145
  if (separator < 0) {
17134
17146
  continue;
17135
17147
  }
17136
- headers.set(line.slice(0, separator).trim().toLowerCase(), line.slice(separator + 1).trim());
17148
+ headers.set(line.slice(0, separator).trim().toLowerCase(), decodeRfc2047(line.slice(separator + 1).trim()));
17137
17149
  }
17138
17150
  return headers;
17139
17151
  };
@@ -17145,7 +17157,7 @@ var decodeEmailPartBody = (body, encoding) => {
17145
17157
  return new Uint8Array(Buffer.from(trimmed.replace(/\s+/g, ""), "base64"));
17146
17158
  }
17147
17159
  if (normalizedEncoding === "quoted-printable") {
17148
- return new Uint8Array(Buffer.from(decodeQuotedPrintable(body), "utf8"));
17160
+ return new Uint8Array(Buffer.from(decodeQuotedPrintable(body), "latin1"));
17149
17161
  }
17150
17162
  return new Uint8Array(Buffer.from(body, "utf8"));
17151
17163
  };
@@ -17311,15 +17323,30 @@ var EMAIL_FORWARDED_HEADER_PATTERN = /^(?:from|sent|to|cc|bcc|subject|date|reply
17311
17323
  var EMAIL_FORWARDED_SEPARATOR_PATTERN = /^-{2,}\s*forwarded message\s*-{2,}$/i;
17312
17324
  var EMAIL_HTML_QUOTE_OPEN_TOKEN = "[[ABS_EMAIL_QUOTE_OPEN]]";
17313
17325
  var EMAIL_HTML_QUOTE_CLOSE_TOKEN = "[[ABS_EMAIL_QUOTE_CLOSE]]";
17326
+ var EMAIL_ATTRIBUTION_PATTERNS = [
17327
+ /^on\s.+\bwrote:$/iu,
17328
+ /^on\s.+\bat\s.+\bwrote:$/iu,
17329
+ /^-+\s*original message\s*-+$/iu,
17330
+ /^_{5,}$/u,
17331
+ /^el\s.+\bescribi\u00F3:$/iu,
17332
+ /^le\s.+\ba \u00E9crit\s*:$/iu
17333
+ ];
17334
+ var EMAIL_SIGNATURE_DELIMITER = /^--\s?$/u;
17314
17335
  var classifyEmailBodyLine = (line, currentKind) => {
17315
17336
  const trimmed = line.trim();
17316
17337
  if (!trimmed) {
17317
17338
  return currentKind;
17318
17339
  }
17340
+ if (currentKind === "signature") {
17341
+ return "signature";
17342
+ }
17343
+ if (EMAIL_SIGNATURE_DELIMITER.test(line)) {
17344
+ return "signature";
17345
+ }
17319
17346
  if (/^>+/.test(trimmed)) {
17320
17347
  return "quoted_history";
17321
17348
  }
17322
- if (/^on .+wrote:$/i.test(trimmed)) {
17349
+ if (EMAIL_ATTRIBUTION_PATTERNS.some((pattern) => pattern.test(trimmed))) {
17323
17350
  return "quoted_history";
17324
17351
  }
17325
17352
  if (EMAIL_FORWARDED_SEPARATOR_PATTERN.test(trimmed)) {
@@ -17507,16 +17534,8 @@ var scoreEmailBodyCandidate = (candidate) => {
17507
17534
  return Number.NEGATIVE_INFINITY;
17508
17535
  }
17509
17536
  const sections = parseEmailBodySections(candidate);
17510
- const structuralScore = sections.reduce((score, section) => {
17511
- if (section.kind === "forwarded_headers") {
17512
- return score + 8;
17513
- }
17514
- if (section.kind === "quoted_history") {
17515
- return score + 6 + (section.quotedDepth ?? 0);
17516
- }
17517
- return score + 1;
17518
- }, 0);
17519
- return structuralScore * 10 + Math.min(candidate.length, 5000) / 100;
17537
+ const authoredChars = sections.filter((section) => section.kind === "authored_text").reduce((total, section) => total + section.text.trim().length, 0);
17538
+ return authoredChars * 10 + Math.min(candidate.length, 5000) / 1000;
17520
17539
  };
17521
17540
  var choosePreferredEmailBodyText = (htmlText, plainText) => {
17522
17541
  const htmlScore = scoreEmailBodyCandidate(htmlText);
@@ -17532,7 +17551,7 @@ var chooseEmailBodyCandidate = (current, candidate) => {
17532
17551
  }
17533
17552
  return candidate.length > current.length ? candidate : current;
17534
17553
  };
17535
- var parseEmailMimeParts = (body, contentType) => {
17554
+ var parseEmailMimeParts = (body, contentType, transferEncoding) => {
17536
17555
  const attachments = [];
17537
17556
  let bodyText;
17538
17557
  let bodyHtml;
@@ -17554,11 +17573,11 @@ var parseEmailMimeParts = (body, contentType) => {
17554
17573
  const nestedContentType = headers.get("content-type");
17555
17574
  const disposition = headers.get("content-disposition");
17556
17575
  const dispositionType = disposition?.split(";")[0]?.trim().toLowerCase();
17557
- const transferEncoding = headers.get("content-transfer-encoding");
17576
+ const transferEncoding2 = headers.get("content-transfer-encoding");
17558
17577
  const contentId = normalizeEmailContentId(headers.get("content-id"));
17559
17578
  const contentLocation = normalizeWhitespace(headers.get("content-location") ?? "");
17560
17579
  const filename = disposition?.match(/filename="?([^";]+)"?/i)?.[1] ?? nestedContentType?.match(/name="?([^";]+)"?/i)?.[1];
17561
- const decodedBytes = decodeEmailPartBody(nestedBody, transferEncoding);
17580
+ const decodedBytes = decodeEmailPartBody(nestedBody, transferEncoding2);
17562
17581
  const decodedText = Buffer.from(decodedBytes).toString("utf8");
17563
17582
  const normalizedContentType = nestedContentType?.toLowerCase() ?? "";
17564
17583
  const isMultipart = normalizedContentType.startsWith("multipart/");
@@ -17606,17 +17625,24 @@ var parseEmailMimeParts = (body, contentType) => {
17606
17625
  }
17607
17626
  }
17608
17627
  };
17609
- collectMimeParts(body, contentType);
17628
+ const topLevelBody = transferEncoding && !parseMimeBoundary(contentType) ? decodeUtf8(decodeEmailPartBody(body, transferEncoding)) : body;
17629
+ collectMimeParts(topLevelBody, contentType);
17610
17630
  return {
17611
17631
  attachments,
17612
17632
  bodyHtml,
17613
17633
  bodyText
17614
17634
  };
17615
17635
  };
17636
+ var authoredEmailText = (text) => {
17637
+ const authored = parseEmailBodySections(text).filter((section) => section.kind === "authored_text").map((section) => section.text.trim()).filter(Boolean).join(`
17638
+
17639
+ `).trim();
17640
+ return authored || text;
17641
+ };
17616
17642
  var extractEmailText = (raw) => {
17617
17643
  const { body, headerBlock } = splitEmailMessage(raw);
17618
17644
  const headers = parseHeaderBlock(headerBlock);
17619
- const parsed = parseEmailMimeParts(body, headers.get("content-type"));
17645
+ const parsed = parseEmailMimeParts(body, headers.get("content-type"), headers.get("content-transfer-encoding"));
17620
17646
  const htmlText = parsed.bodyHtml ? stripEmailHtml(parsed.bodyHtml) : undefined;
17621
17647
  const plainText = parsed.bodyText ? normalizeWhitespace(parsed.bodyText) : undefined;
17622
17648
  const preferredBodyText = choosePreferredEmailBodyText(htmlText, plainText);
@@ -17673,6 +17699,7 @@ var parseEmailHeaders = (raw) => {
17673
17699
  ccAddressEntries: ccParsed.entries,
17674
17700
  ccAddresses: ccParsed.addresses,
17675
17701
  contentType: getHeader("Content-Type"),
17702
+ contentTransferEncoding: getHeader("Content-Transfer-Encoding"),
17676
17703
  from,
17677
17704
  fromAddress: fromParsed.addresses[0],
17678
17705
  fromAddressEntries: fromParsed.entries,
@@ -17697,7 +17724,7 @@ var parseEmailHeaders = (raw) => {
17697
17724
  var extractEmailDocumentsFromRawMessage = async (input, raw, options) => {
17698
17725
  const headers = parseEmailHeaders(raw);
17699
17726
  const { body } = splitEmailMessage(raw);
17700
- const parsed = parseEmailMimeParts(body, headers.contentType);
17727
+ const parsed = parseEmailMimeParts(body, headers.contentType, headers.contentTransferEncoding);
17701
17728
  const source = options?.source ?? input.source ?? input.path ?? input.name ?? `${slugify(input.title ?? DEFAULT_BINARY_NAME)}.eml`;
17702
17729
  const maildirMetadata = parseMaildirMetadata(source);
17703
17730
  const mergedMetadata = {
@@ -17900,7 +17927,7 @@ var extractEmailDocumentsFromRawMessage = async (input, raw, options) => {
17900
17927
  return [messageDocument, ...attachmentDocuments.flat()];
17901
17928
  };
17902
17929
  var normalizeEmailThreadKey = (value) => {
17903
- const normalized = normalizeWhitespace(value?.replace(/^(re|fw|fwd)\s*:\s*/gi, "")?.replace(/[<>]/g, "")?.toLowerCase() ?? "");
17930
+ const normalized = normalizeWhitespace(value?.replace(/^(?:(?:re|fw|fwd|aw|sv|vs|antw)\s*:\s*)+/i, "")?.replace(/[<>]/g, "")?.toLowerCase() ?? "");
17904
17931
  return normalized || undefined;
17905
17932
  };
17906
17933
  var normalizeEmailMessageId = (value) => {
@@ -36346,6 +36373,7 @@ export {
36346
36373
  buildRAGAdminJobPresentation,
36347
36374
  buildRAGAdminActionPresentations,
36348
36375
  buildRAGAdminActionPresentation,
36376
+ authoredEmailText,
36349
36377
  applyRAGSQLiteStoreMigrations,
36350
36378
  applyRAGReranking,
36351
36379
  applyRAGQueryTransform,
@@ -36355,5 +36383,5 @@ export {
36355
36383
  addRAGEvaluationSuiteCase
36356
36384
  };
36357
36385
 
36358
- //# debugId=A910166B20BDB76264756E2164756E21
36386
+ //# debugId=E2463B33C7EF84EC64756E2164756E21
36359
36387
  //# sourceMappingURL=index.js.map