@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.
@@ -14824,6 +14824,8 @@ var emailStructureUnits = (value, metadata) => {
14824
14824
  return "Forwarded Headers";
14825
14825
  case "quoted_history":
14826
14826
  return "Quoted History";
14827
+ case "signature":
14828
+ return "Signature";
14827
14829
  }
14828
14830
  };
14829
14831
  const familyKeyBase = threadTopic || from || "Email Message";
@@ -16845,6 +16847,16 @@ var parseMaildirMetadata = (source) => {
16845
16847
  ...maildirKey ? { emailMailboxKey: maildirKey } : {}
16846
16848
  };
16847
16849
  };
16850
+ var RFC2047_WORD = /=\?([^?]+)\?([BbQq])\?([^?]*)\?=/g;
16851
+ var decodeEncodedWord = (charset, encoding, text) => {
16852
+ const bytes = encoding.toUpperCase() === "B" ? Buffer.from(text, "base64") : Buffer.from(decodeQuotedPrintable(text.replace(/_/g, " ")), "latin1");
16853
+ try {
16854
+ return new TextDecoder(charset.trim().toLowerCase()).decode(bytes);
16855
+ } catch {
16856
+ return bytes.toString("utf8");
16857
+ }
16858
+ };
16859
+ var decodeRfc2047 = (value) => value.includes("=?") ? value.replace(/\?=\s+=\?/g, "?==?").replace(RFC2047_WORD, (_match, charset, enc, text) => decodeEncodedWord(charset, enc, text)) : value;
16848
16860
  var parseHeaderBlock = (headerBlock) => {
16849
16861
  const unfolded = headerBlock.replace(/\n[ \t]+/g, " ");
16850
16862
  const headers = new Map;
@@ -16854,7 +16866,7 @@ var parseHeaderBlock = (headerBlock) => {
16854
16866
  if (separator < 0) {
16855
16867
  continue;
16856
16868
  }
16857
- headers.set(line.slice(0, separator).trim().toLowerCase(), line.slice(separator + 1).trim());
16869
+ headers.set(line.slice(0, separator).trim().toLowerCase(), decodeRfc2047(line.slice(separator + 1).trim()));
16858
16870
  }
16859
16871
  return headers;
16860
16872
  };
@@ -16866,7 +16878,7 @@ var decodeEmailPartBody = (body, encoding) => {
16866
16878
  return new Uint8Array(Buffer.from(trimmed.replace(/\s+/g, ""), "base64"));
16867
16879
  }
16868
16880
  if (normalizedEncoding === "quoted-printable") {
16869
- return new Uint8Array(Buffer.from(decodeQuotedPrintable(body), "utf8"));
16881
+ return new Uint8Array(Buffer.from(decodeQuotedPrintable(body), "latin1"));
16870
16882
  }
16871
16883
  return new Uint8Array(Buffer.from(body, "utf8"));
16872
16884
  };
@@ -17032,15 +17044,30 @@ var EMAIL_FORWARDED_HEADER_PATTERN = /^(?:from|sent|to|cc|bcc|subject|date|reply
17032
17044
  var EMAIL_FORWARDED_SEPARATOR_PATTERN = /^-{2,}\s*forwarded message\s*-{2,}$/i;
17033
17045
  var EMAIL_HTML_QUOTE_OPEN_TOKEN = "[[ABS_EMAIL_QUOTE_OPEN]]";
17034
17046
  var EMAIL_HTML_QUOTE_CLOSE_TOKEN = "[[ABS_EMAIL_QUOTE_CLOSE]]";
17047
+ var EMAIL_ATTRIBUTION_PATTERNS = [
17048
+ /^on\s.+\bwrote:$/iu,
17049
+ /^on\s.+\bat\s.+\bwrote:$/iu,
17050
+ /^-+\s*original message\s*-+$/iu,
17051
+ /^_{5,}$/u,
17052
+ /^el\s.+\bescribi\u00F3:$/iu,
17053
+ /^le\s.+\ba \u00E9crit\s*:$/iu
17054
+ ];
17055
+ var EMAIL_SIGNATURE_DELIMITER = /^--\s?$/u;
17035
17056
  var classifyEmailBodyLine = (line, currentKind) => {
17036
17057
  const trimmed = line.trim();
17037
17058
  if (!trimmed) {
17038
17059
  return currentKind;
17039
17060
  }
17061
+ if (currentKind === "signature") {
17062
+ return "signature";
17063
+ }
17064
+ if (EMAIL_SIGNATURE_DELIMITER.test(line)) {
17065
+ return "signature";
17066
+ }
17040
17067
  if (/^>+/.test(trimmed)) {
17041
17068
  return "quoted_history";
17042
17069
  }
17043
- if (/^on .+wrote:$/i.test(trimmed)) {
17070
+ if (EMAIL_ATTRIBUTION_PATTERNS.some((pattern) => pattern.test(trimmed))) {
17044
17071
  return "quoted_history";
17045
17072
  }
17046
17073
  if (EMAIL_FORWARDED_SEPARATOR_PATTERN.test(trimmed)) {
@@ -17228,16 +17255,8 @@ var scoreEmailBodyCandidate = (candidate) => {
17228
17255
  return Number.NEGATIVE_INFINITY;
17229
17256
  }
17230
17257
  const sections = parseEmailBodySections(candidate);
17231
- const structuralScore = sections.reduce((score, section) => {
17232
- if (section.kind === "forwarded_headers") {
17233
- return score + 8;
17234
- }
17235
- if (section.kind === "quoted_history") {
17236
- return score + 6 + (section.quotedDepth ?? 0);
17237
- }
17238
- return score + 1;
17239
- }, 0);
17240
- return structuralScore * 10 + Math.min(candidate.length, 5000) / 100;
17258
+ const authoredChars = sections.filter((section) => section.kind === "authored_text").reduce((total, section) => total + section.text.trim().length, 0);
17259
+ return authoredChars * 10 + Math.min(candidate.length, 5000) / 1000;
17241
17260
  };
17242
17261
  var choosePreferredEmailBodyText = (htmlText, plainText) => {
17243
17262
  const htmlScore = scoreEmailBodyCandidate(htmlText);
@@ -17253,7 +17272,7 @@ var chooseEmailBodyCandidate = (current, candidate) => {
17253
17272
  }
17254
17273
  return candidate.length > current.length ? candidate : current;
17255
17274
  };
17256
- var parseEmailMimeParts = (body, contentType) => {
17275
+ var parseEmailMimeParts = (body, contentType, transferEncoding) => {
17257
17276
  const attachments = [];
17258
17277
  let bodyText;
17259
17278
  let bodyHtml;
@@ -17275,11 +17294,11 @@ var parseEmailMimeParts = (body, contentType) => {
17275
17294
  const nestedContentType = headers.get("content-type");
17276
17295
  const disposition = headers.get("content-disposition");
17277
17296
  const dispositionType = disposition?.split(";")[0]?.trim().toLowerCase();
17278
- const transferEncoding = headers.get("content-transfer-encoding");
17297
+ const transferEncoding2 = headers.get("content-transfer-encoding");
17279
17298
  const contentId = normalizeEmailContentId(headers.get("content-id"));
17280
17299
  const contentLocation = normalizeWhitespace(headers.get("content-location") ?? "");
17281
17300
  const filename = disposition?.match(/filename="?([^";]+)"?/i)?.[1] ?? nestedContentType?.match(/name="?([^";]+)"?/i)?.[1];
17282
- const decodedBytes = decodeEmailPartBody(nestedBody, transferEncoding);
17301
+ const decodedBytes = decodeEmailPartBody(nestedBody, transferEncoding2);
17283
17302
  const decodedText = Buffer.from(decodedBytes).toString("utf8");
17284
17303
  const normalizedContentType = nestedContentType?.toLowerCase() ?? "";
17285
17304
  const isMultipart = normalizedContentType.startsWith("multipart/");
@@ -17327,17 +17346,24 @@ var parseEmailMimeParts = (body, contentType) => {
17327
17346
  }
17328
17347
  }
17329
17348
  };
17330
- collectMimeParts(body, contentType);
17349
+ const topLevelBody = transferEncoding && !parseMimeBoundary(contentType) ? decodeUtf8(decodeEmailPartBody(body, transferEncoding)) : body;
17350
+ collectMimeParts(topLevelBody, contentType);
17331
17351
  return {
17332
17352
  attachments,
17333
17353
  bodyHtml,
17334
17354
  bodyText
17335
17355
  };
17336
17356
  };
17357
+ var authoredEmailText = (text) => {
17358
+ const authored = parseEmailBodySections(text).filter((section) => section.kind === "authored_text").map((section) => section.text.trim()).filter(Boolean).join(`
17359
+
17360
+ `).trim();
17361
+ return authored || text;
17362
+ };
17337
17363
  var extractEmailText = (raw) => {
17338
17364
  const { body, headerBlock } = splitEmailMessage(raw);
17339
17365
  const headers = parseHeaderBlock(headerBlock);
17340
- const parsed = parseEmailMimeParts(body, headers.get("content-type"));
17366
+ const parsed = parseEmailMimeParts(body, headers.get("content-type"), headers.get("content-transfer-encoding"));
17341
17367
  const htmlText = parsed.bodyHtml ? stripEmailHtml(parsed.bodyHtml) : undefined;
17342
17368
  const plainText = parsed.bodyText ? normalizeWhitespace(parsed.bodyText) : undefined;
17343
17369
  const preferredBodyText = choosePreferredEmailBodyText(htmlText, plainText);
@@ -17394,6 +17420,7 @@ var parseEmailHeaders = (raw) => {
17394
17420
  ccAddressEntries: ccParsed.entries,
17395
17421
  ccAddresses: ccParsed.addresses,
17396
17422
  contentType: getHeader("Content-Type"),
17423
+ contentTransferEncoding: getHeader("Content-Transfer-Encoding"),
17397
17424
  from,
17398
17425
  fromAddress: fromParsed.addresses[0],
17399
17426
  fromAddressEntries: fromParsed.entries,
@@ -17418,7 +17445,7 @@ var parseEmailHeaders = (raw) => {
17418
17445
  var extractEmailDocumentsFromRawMessage = async (input, raw, options) => {
17419
17446
  const headers = parseEmailHeaders(raw);
17420
17447
  const { body } = splitEmailMessage(raw);
17421
- const parsed = parseEmailMimeParts(body, headers.contentType);
17448
+ const parsed = parseEmailMimeParts(body, headers.contentType, headers.contentTransferEncoding);
17422
17449
  const source = options?.source ?? input.source ?? input.path ?? input.name ?? `${slugify(input.title ?? DEFAULT_BINARY_NAME)}.eml`;
17423
17450
  const maildirMetadata = parseMaildirMetadata(source);
17424
17451
  const mergedMetadata = {
@@ -17621,7 +17648,7 @@ var extractEmailDocumentsFromRawMessage = async (input, raw, options) => {
17621
17648
  return [messageDocument, ...attachmentDocuments.flat()];
17622
17649
  };
17623
17650
  var normalizeEmailThreadKey = (value) => {
17624
- const normalized = normalizeWhitespace(value?.replace(/^(re|fw|fwd)\s*:\s*/gi, "")?.replace(/[<>]/g, "")?.toLowerCase() ?? "");
17651
+ const normalized = normalizeWhitespace(value?.replace(/^(?:(?:re|fw|fwd|aw|sv|vs|antw)\s*:\s*)+/i, "")?.replace(/[<>]/g, "")?.toLowerCase() ?? "");
17625
17652
  return normalized || undefined;
17626
17653
  };
17627
17654
  var normalizeEmailMessageId = (value) => {
@@ -31325,5 +31352,5 @@ export {
31325
31352
  RAG_NATIVE_QUERY_CANDIDATE_LIMIT
31326
31353
  };
31327
31354
 
31328
- //# debugId=86AE9E92B8164CAE64756E2164756E21
31355
+ //# debugId=EDE8F57AE9AB8A5064756E2164756E21
31329
31356
  //# sourceMappingURL=index.js.map