@absolutejs/rag 0.4.0 → 0.6.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
@@ -15975,6 +15975,7 @@ var extractNativePDFText = (data) => {
15975
15975
  var readUInt16LE = (data, offset) => data[offset] | data[offset + 1] << 8;
15976
15976
  var readUInt32LE = (data, offset) => (data[offset] | data[offset + 1] << 8 | data[offset + 2] << 16 | data[offset + 3] << 24) >>> 0;
15977
15977
  var decodeUtf8 = (data) => Buffer.from(data).toString("utf8");
15978
+ var decodeLatin1 = (data) => Buffer.from(data).toString("latin1");
15978
15979
  var isZipData = (data) => data.length >= 4 && data[0] === 80 && data[1] === 75 && data[2] === 3 && data[3] === 4;
15979
15980
  var unzipEntries = (data) => {
15980
15981
  const entries = [];
@@ -17159,7 +17160,19 @@ var decodeEmailPartBody = (body, encoding) => {
17159
17160
  if (normalizedEncoding === "quoted-printable") {
17160
17161
  return new Uint8Array(Buffer.from(decodeQuotedPrintable(body), "latin1"));
17161
17162
  }
17162
- return new Uint8Array(Buffer.from(body, "utf8"));
17163
+ return new Uint8Array(Buffer.from(body, "latin1"));
17164
+ };
17165
+ var parseMimeCharset = (contentType) => contentType?.match(/charset\s*=\s*"?([^";\s]+)"?/i)?.[1];
17166
+ var decodeWithCharset = (data, contentType) => {
17167
+ const charset = parseMimeCharset(contentType)?.trim().toLowerCase();
17168
+ if (!charset || charset === "utf-8" || charset === "utf8") {
17169
+ return decodeUtf8(data);
17170
+ }
17171
+ try {
17172
+ return new TextDecoder(charset).decode(data);
17173
+ } catch {
17174
+ return decodeUtf8(data);
17175
+ }
17163
17176
  };
17164
17177
  var parseMimeBoundary = (contentType) => {
17165
17178
  const match = contentType?.match(/boundary="?([^";]+)"?/i);
@@ -17578,7 +17591,7 @@ var parseEmailMimeParts = (body, contentType, transferEncoding) => {
17578
17591
  const contentLocation = normalizeWhitespace(headers.get("content-location") ?? "");
17579
17592
  const filename = disposition?.match(/filename="?([^";]+)"?/i)?.[1] ?? nestedContentType?.match(/name="?([^";]+)"?/i)?.[1];
17580
17593
  const decodedBytes = decodeEmailPartBody(nestedBody, transferEncoding2);
17581
- const decodedText = Buffer.from(decodedBytes).toString("utf8");
17594
+ const decodedText = decodeWithCharset(decodedBytes, nestedContentType);
17582
17595
  const normalizedContentType = nestedContentType?.toLowerCase() ?? "";
17583
17596
  const isMultipart = normalizedContentType.startsWith("multipart/");
17584
17597
  const isHtml = normalizedContentType.includes("text/html");
@@ -17625,7 +17638,7 @@ var parseEmailMimeParts = (body, contentType, transferEncoding) => {
17625
17638
  }
17626
17639
  }
17627
17640
  };
17628
- const topLevelBody = transferEncoding && !parseMimeBoundary(contentType) ? decodeUtf8(decodeEmailPartBody(body, transferEncoding)) : body;
17641
+ const topLevelBody = transferEncoding && !parseMimeBoundary(contentType) ? decodeWithCharset(decodeEmailPartBody(body, transferEncoding), contentType) : body;
17629
17642
  collectMimeParts(topLevelBody, contentType);
17630
17643
  return {
17631
17644
  attachments,
@@ -18275,7 +18288,7 @@ var createEmailExtractor = () => ({
18275
18288
  const source = input.source ?? input.path ?? input.name ?? `${slugify(input.title ?? DEFAULT_BINARY_NAME)}.eml`;
18276
18289
  const extension = inferExtensionFromInput(input);
18277
18290
  const emlx = extension === ".emlx" ? decodeEmlxMessageData(input.data) : undefined;
18278
- const raw = emlx?.raw ?? decodeUtf8(input.data);
18291
+ const raw = emlx?.raw ?? decodeLatin1(input.data);
18279
18292
  if (extension === ".emlx") {
18280
18293
  return extractEmailDocumentsFromRawMessage(input, raw, {
18281
18294
  metadata: {
@@ -36086,6 +36099,49 @@ var createSyncRAGStore = (options = {}) => {
36086
36099
  upsert
36087
36100
  };
36088
36101
  };
36102
+ // src/retrieval/corpus.ts
36103
+ var corpusTextHash = async (text) => {
36104
+ const digest = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(text));
36105
+ return Array.from(new Uint8Array(digest)).map((byte) => byte.toString(16).padStart(2, "0")).join("");
36106
+ };
36107
+ var planRAGCorpus = async (store, owner, desired) => {
36108
+ const wanted = new Map;
36109
+ for (const doc of desired) {
36110
+ if (!wanted.has(doc.chunkId))
36111
+ wanted.set(doc.chunkId, doc);
36112
+ }
36113
+ const stored = new Map((await store.list(owner)).map((record) => [record.chunkId, record.textHash]));
36114
+ const embed = [];
36115
+ let unchanged = 0;
36116
+ for (const [chunkId, doc] of wanted) {
36117
+ const hash = await corpusTextHash(doc.text);
36118
+ if (stored.get(chunkId) === hash)
36119
+ unchanged += 1;
36120
+ else
36121
+ embed.push(doc);
36122
+ }
36123
+ return {
36124
+ embed,
36125
+ remove: [...stored.keys()].filter((chunkId) => !wanted.has(chunkId)),
36126
+ unchanged
36127
+ };
36128
+ };
36129
+ var reconcileRAGCorpus = async (store, owner, desired, apply) => {
36130
+ const plan = await planRAGCorpus(store, owner, desired);
36131
+ if (plan.remove.length > 0) {
36132
+ await apply.remove(plan.remove, owner);
36133
+ await store.forget(owner, plan.remove);
36134
+ }
36135
+ let embedded = 0;
36136
+ if (plan.embed.length > 0) {
36137
+ embedded = await apply.embed(plan.embed, owner);
36138
+ await store.remember(owner, await Promise.all(plan.embed.map(async (doc) => ({
36139
+ chunkId: doc.chunkId,
36140
+ textHash: await corpusTextHash(doc.text)
36141
+ }))));
36142
+ }
36143
+ return { embedded, removed: plan.remove.length, unchanged: plan.unchanged };
36144
+ };
36089
36145
  export {
36090
36146
  xaiEmbeddings,
36091
36147
  withEmbeddingBudget,
@@ -36110,6 +36166,7 @@ export {
36110
36166
  removeRAGSource,
36111
36167
  removeRAGEvaluationSuiteCaseHardNegative,
36112
36168
  removeRAGEvaluationSuiteCase,
36169
+ reconcileRAGCorpus,
36113
36170
  ragChat as ragPlugin,
36114
36171
  ragChat,
36115
36172
  querySimilarity,
@@ -36121,6 +36178,7 @@ export {
36121
36178
  prepareRAGDocumentFile,
36122
36179
  prepareRAGDocument,
36123
36180
  prepareRAGDirectoryDocuments,
36181
+ planRAGCorpus,
36124
36182
  persistRAGSearchTraceRecord,
36125
36183
  persistRAGSearchTracePruneRun,
36126
36184
  persistRAGRetrievalReleaseLanePolicyHistory,
@@ -36300,6 +36358,7 @@ export {
36300
36358
  createEPUBExtractor,
36301
36359
  createCohereRAGReranker,
36302
36360
  createBuiltinArchiveExpander,
36361
+ corpusTextHash,
36303
36362
  compareRAGRetrievalTraceSummaries,
36304
36363
  compareRAGRetrievalStrategies,
36305
36364
  compareRAGRerankers,
@@ -36383,5 +36442,5 @@ export {
36383
36442
  addRAGEvaluationSuiteCase
36384
36443
  };
36385
36444
 
36386
- //# debugId=E2463B33C7EF84EC64756E2164756E21
36445
+ //# debugId=8567BF91160369B364756E2164756E21
36387
36446
  //# sourceMappingURL=index.js.map