@absolutejs/absolute 0.19.0-beta.501 → 0.19.0-beta.503

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/ai/index.js CHANGED
@@ -6833,6 +6833,294 @@ var openaiTranscriber = (config) => {
6833
6833
  }
6834
6834
  });
6835
6835
  };
6836
+ // src/ai/rag/emailProviders.ts
6837
+ import { connect as connectTls } from "tls";
6838
+ var defaultFetch = (...args) => fetch(...args);
6839
+ var stripHtml2 = (value) => value.replace(/<style[\s\S]*?<\/style>/gi, " ").replace(/<script[\s\S]*?<\/script>/gi, " ").replace(/<[^>]+>/g, " ").replace(/&nbsp;/gi, " ").replace(/&amp;/gi, "&").replace(/\s+/g, " ").trim();
6840
+ var decodeBase64Url = (value) => {
6841
+ const normalized = value.replace(/-/g, "+").replace(/_/g, "/");
6842
+ const padding = normalized.length % 4 === 0 ? "" : "=".repeat(4 - normalized.length % 4);
6843
+ return Buffer.from(normalized + padding, "base64");
6844
+ };
6845
+ var firstHeader = (headers, name) => headers?.find((header) => header.name?.toLowerCase() === name.toLowerCase())?.value;
6846
+ var collectGmailPayloadParts = (payload) => {
6847
+ if (!payload) {
6848
+ return [];
6849
+ }
6850
+ return [
6851
+ payload,
6852
+ ...(payload.parts ?? []).flatMap((part) => collectGmailPayloadParts(part))
6853
+ ];
6854
+ };
6855
+ var extractGmailBodyText = (payload) => {
6856
+ const parts = collectGmailPayloadParts(payload);
6857
+ const plain = parts.find((part) => part.mimeType?.toLowerCase() === "text/plain" && part.body?.data);
6858
+ if (plain?.body?.data) {
6859
+ return decodeBase64Url(plain.body.data).toString("utf8");
6860
+ }
6861
+ const html = parts.find((part) => part.mimeType?.toLowerCase() === "text/html" && part.body?.data);
6862
+ if (html?.body?.data) {
6863
+ return stripHtml2(decodeBase64Url(html.body.data).toString("utf8"));
6864
+ }
6865
+ if (payload?.body?.data) {
6866
+ return decodeBase64Url(payload.body.data).toString("utf8");
6867
+ }
6868
+ return "";
6869
+ };
6870
+ var toGraphAddressList = (value) => value?.map((entry) => entry.emailAddress?.address).filter(Boolean);
6871
+ var toGraphAttachment = (attachment) => attachment.name && attachment.contentBytes ? {
6872
+ content: attachment.contentBytes,
6873
+ contentType: attachment.contentType,
6874
+ encoding: "base64",
6875
+ id: attachment.id,
6876
+ name: attachment.name
6877
+ } : null;
6878
+ var buildGraphPath = (config) => {
6879
+ const base = config.userId && config.userId !== "me" ? `/users/${encodeURIComponent(config.userId)}` : "/me";
6880
+ return config.folderId ? `${base}/mailFolders/${encodeURIComponent(config.folderId)}/messages` : `${base}/messages`;
6881
+ };
6882
+ var parseRawEmail = (raw) => {
6883
+ const [headerBlock, ...bodyBlocks] = raw.split(/\r?\n\r?\n/);
6884
+ const body = bodyBlocks.join(`
6885
+
6886
+ `).trim();
6887
+ const lines = (headerBlock ?? "").split(/\r?\n/);
6888
+ const headers = new Map;
6889
+ let current = "";
6890
+ for (const line of lines) {
6891
+ if (/^\s/.test(line) && current) {
6892
+ headers.set(current, `${headers.get(current) ?? ""} ${line.trim()}`.trim());
6893
+ continue;
6894
+ }
6895
+ const separator = line.indexOf(":");
6896
+ if (separator < 0) {
6897
+ continue;
6898
+ }
6899
+ current = line.slice(0, separator).trim().toLowerCase();
6900
+ headers.set(current, line.slice(separator + 1).trim());
6901
+ }
6902
+ const toList = (value) => value?.split(",").map((entry) => entry.trim()).filter(Boolean);
6903
+ return {
6904
+ bodyText: body,
6905
+ cc: toList(headers.get("cc")),
6906
+ from: headers.get("from"),
6907
+ id: headers.get("message-id") ?? headers.get("subject") ?? crypto.randomUUID(),
6908
+ receivedAt: headers.get("date"),
6909
+ subject: headers.get("subject"),
6910
+ threadId: headers.get("thread-topic") ?? headers.get("references") ?? headers.get("subject"),
6911
+ to: toList(headers.get("to"))
6912
+ };
6913
+ };
6914
+ var createRAGGmailEmailSyncClient = (config) => ({
6915
+ listMessages: async (input) => {
6916
+ const fetchImpl = config.fetch ?? defaultFetch;
6917
+ const userId = config.userId ?? "me";
6918
+ const listUrl = new URL(`https://gmail.googleapis.com/gmail/v1/users/${encodeURIComponent(userId)}/messages`);
6919
+ listUrl.searchParams.set("maxResults", String(input?.maxResults ?? config.maxResults ?? 100));
6920
+ if (input?.cursor) {
6921
+ listUrl.searchParams.set("pageToken", input.cursor);
6922
+ }
6923
+ if (config.query) {
6924
+ listUrl.searchParams.set("q", config.query);
6925
+ }
6926
+ for (const labelId of config.labelIds ?? []) {
6927
+ listUrl.searchParams.append("labelIds", labelId);
6928
+ }
6929
+ if (config.includeSpamTrash) {
6930
+ listUrl.searchParams.set("includeSpamTrash", "true");
6931
+ }
6932
+ const listResponse = await fetchImpl(listUrl, {
6933
+ headers: { Authorization: `Bearer ${config.accessToken}` }
6934
+ });
6935
+ if (!listResponse.ok) {
6936
+ throw new Error(`Gmail list failed: ${listResponse.status} ${listResponse.statusText}`);
6937
+ }
6938
+ const listJson = await listResponse.json();
6939
+ const messages = await Promise.all((listJson.messages ?? []).map(async (messageRef) => {
6940
+ const getUrl = new URL(`https://gmail.googleapis.com/gmail/v1/users/${encodeURIComponent(userId)}/messages/${encodeURIComponent(messageRef.id)}`);
6941
+ getUrl.searchParams.set("format", "full");
6942
+ const response = await fetchImpl(getUrl, {
6943
+ headers: { Authorization: `Bearer ${config.accessToken}` }
6944
+ });
6945
+ if (!response.ok) {
6946
+ throw new Error(`Gmail get failed for ${messageRef.id}: ${response.status} ${response.statusText}`);
6947
+ }
6948
+ const json = await response.json();
6949
+ const attachments = await Promise.all(collectGmailPayloadParts(json.payload).filter((part) => !!part.filename && !!part.body?.attachmentId).map(async (part) => {
6950
+ const attachmentUrl = new URL(`https://gmail.googleapis.com/gmail/v1/users/${encodeURIComponent(userId)}/messages/${encodeURIComponent(json.id)}/attachments/${encodeURIComponent(part.body?.attachmentId ?? "")}`);
6951
+ const attachmentResponse = await fetchImpl(attachmentUrl, {
6952
+ headers: {
6953
+ Authorization: `Bearer ${config.accessToken}`
6954
+ }
6955
+ });
6956
+ if (!attachmentResponse.ok) {
6957
+ return null;
6958
+ }
6959
+ const attachmentJson = await attachmentResponse.json();
6960
+ if (!attachmentJson.data || !part.filename) {
6961
+ return null;
6962
+ }
6963
+ return {
6964
+ content: decodeBase64Url(attachmentJson.data).toString("base64"),
6965
+ contentType: part.mimeType,
6966
+ encoding: "base64",
6967
+ id: part.body?.attachmentId,
6968
+ name: part.filename
6969
+ };
6970
+ }));
6971
+ return {
6972
+ attachments: attachments.filter(Boolean),
6973
+ bodyText: extractGmailBodyText(json.payload) || json.snippet || "",
6974
+ cc: firstHeader(json.payload?.headers, "Cc")?.split(",").map((entry) => entry.trim()).filter(Boolean),
6975
+ from: firstHeader(json.payload?.headers, "From"),
6976
+ id: json.id,
6977
+ metadata: {
6978
+ gmailLabelIds: json.labelIds,
6979
+ provider: "gmail"
6980
+ },
6981
+ receivedAt: json.internalDate ? Number(json.internalDate) : undefined,
6982
+ subject: firstHeader(json.payload?.headers, "Subject"),
6983
+ threadId: json.threadId ?? messageRef.threadId,
6984
+ to: firstHeader(json.payload?.headers, "To")?.split(",").map((entry) => entry.trim()).filter(Boolean)
6985
+ };
6986
+ }));
6987
+ return {
6988
+ messages,
6989
+ nextCursor: listJson.nextPageToken
6990
+ };
6991
+ }
6992
+ });
6993
+ var createRAGGraphEmailSyncClient = (config) => ({
6994
+ listMessages: async (input) => {
6995
+ const fetchImpl = config.fetch ?? defaultFetch;
6996
+ const url = new URL(`${config.baseUrl ?? "https://graph.microsoft.com/v1.0"}${buildGraphPath(config)}`);
6997
+ url.searchParams.set("$select", "id,conversationId,subject,from,toRecipients,ccRecipients,sentDateTime,receivedDateTime,body,hasAttachments,internetMessageId");
6998
+ url.searchParams.set("$top", String(input?.maxResults ?? config.top ?? 100));
6999
+ if (config.filter) {
7000
+ url.searchParams.set("$filter", config.filter);
7001
+ }
7002
+ if (config.search) {
7003
+ url.searchParams.set("$search", config.search);
7004
+ }
7005
+ if (input?.cursor) {
7006
+ return {
7007
+ messages: [],
7008
+ nextCursor: input.cursor
7009
+ };
7010
+ }
7011
+ const listResponse = await fetchImpl(url, {
7012
+ headers: {
7013
+ Authorization: `Bearer ${config.accessToken}`,
7014
+ Prefer: 'outlook.body-content-type="text"'
7015
+ }
7016
+ });
7017
+ if (!listResponse.ok) {
7018
+ throw new Error(`Graph message list failed: ${listResponse.status} ${listResponse.statusText}`);
7019
+ }
7020
+ const listJson = await listResponse.json();
7021
+ const messages = await Promise.all((listJson.value ?? []).map(async (message) => {
7022
+ const attachments = message.hasAttachments === true ? await (async () => {
7023
+ const attachmentsResponse = await fetchImpl(`${config.baseUrl ?? "https://graph.microsoft.com/v1.0"}${buildGraphPath(config)}/${encodeURIComponent(String(message.id))}/attachments`, {
7024
+ headers: {
7025
+ Authorization: `Bearer ${config.accessToken}`
7026
+ }
7027
+ });
7028
+ if (!attachmentsResponse.ok) {
7029
+ return [];
7030
+ }
7031
+ const attachmentsJson = await attachmentsResponse.json();
7032
+ return (attachmentsJson.value ?? []).map((attachment) => toGraphAttachment(attachment)).filter(Boolean);
7033
+ })() : [];
7034
+ return {
7035
+ attachments,
7036
+ bodyText: typeof message.body?.content === "string" ? String(message.body.content) : "",
7037
+ cc: toGraphAddressList(message.ccRecipients),
7038
+ from: message.from?.emailAddress?.address,
7039
+ id: String(message.id),
7040
+ metadata: {
7041
+ internetMessageId: message.internetMessageId,
7042
+ provider: "graph"
7043
+ },
7044
+ receivedAt: message.receivedDateTime,
7045
+ sentAt: message.sentDateTime,
7046
+ subject: message.subject,
7047
+ threadId: message.conversationId,
7048
+ to: toGraphAddressList(message.toRecipients)
7049
+ };
7050
+ }));
7051
+ return {
7052
+ messages,
7053
+ nextCursor: listJson["@odata.nextLink"]
7054
+ };
7055
+ }
7056
+ });
7057
+ var createRAGIMAPEmailSyncClient = (config) => ({
7058
+ listMessages: async (input) => {
7059
+ const mailbox = config.mailbox ?? "INBOX";
7060
+ const searchTerms = config.search?.length ? config.search : ["ALL"];
7061
+ const maxResults = input?.maxResults ?? config.maxResults ?? 100;
7062
+ const socket = connectTls({
7063
+ host: config.host,
7064
+ port: config.port ?? 993,
7065
+ rejectUnauthorized: true
7066
+ });
7067
+ socket.setEncoding("utf8");
7068
+ let buffer = "";
7069
+ const readUntil = async (predicate) => await new Promise((resolve2, reject) => {
7070
+ const onData = (chunk) => {
7071
+ buffer += chunk;
7072
+ if (predicate(buffer)) {
7073
+ socket.off("data", onData);
7074
+ resolve2(buffer);
7075
+ }
7076
+ };
7077
+ const onError = (error) => {
7078
+ socket.off("data", onData);
7079
+ reject(error);
7080
+ };
7081
+ socket.on("data", onData);
7082
+ socket.once("error", onError);
7083
+ });
7084
+ const send = async (tag, command) => {
7085
+ socket.write(`${tag} ${command}\r
7086
+ `);
7087
+ const response = await readUntil((chunk) => chunk.includes(`\r
7088
+ ${tag} OK`) || chunk.startsWith(`${tag} OK`));
7089
+ buffer = "";
7090
+ return response;
7091
+ };
7092
+ await readUntil((chunk) => chunk.includes(`\r
7093
+ `) || chunk.startsWith("*"));
7094
+ buffer = "";
7095
+ await send("A001", `LOGIN "${config.username}" "${config.password}"`);
7096
+ await send("A002", `SELECT "${mailbox}"`);
7097
+ const searchResponse = await send("A003", `UID SEARCH ${searchTerms.join(" ")}`);
7098
+ const uidLine = searchResponse.split(/\r?\n/).find((line) => line.startsWith("* SEARCH"));
7099
+ const uids = (uidLine?.replace("* SEARCH", "").trim().split(/\s+/) ?? []).filter(Boolean).slice(-maxResults);
7100
+ const messages = [];
7101
+ for (const uid of uids) {
7102
+ const response = await send(`F${uid}`, `UID FETCH ${uid} BODY.PEEK[]`);
7103
+ const rawMatch = response.match(/\{(\d+)\}\r\n([\s\S]*?)\r\nF[^\r\n]+ OK/);
7104
+ const raw = rawMatch?.[2]?.trim();
7105
+ if (!raw) {
7106
+ continue;
7107
+ }
7108
+ const parsed = parseRawEmail(raw);
7109
+ messages.push({
7110
+ ...parsed,
7111
+ metadata: {
7112
+ ...parsed.metadata ?? {},
7113
+ imapMailbox: mailbox,
7114
+ imapUid: uid,
7115
+ provider: "imap"
7116
+ }
7117
+ });
7118
+ }
7119
+ socket.end(`ZZZZ LOGOUT\r
7120
+ `);
7121
+ return { messages };
7122
+ }
7123
+ });
6836
7124
  // src/ai/rag/presentation.ts
6837
7125
  var buildSourceGroupKey = (source) => source.source ?? source.title ?? source.chunkId;
6838
7126
  var buildSourceLabel = (source) => source.source ?? source.title ?? source.chunkId;
@@ -7217,6 +7505,23 @@ var toManagedSyncDocument = (sourceId, document, syncKey) => ({
7217
7505
  syncSourceId: sourceId
7218
7506
  }
7219
7507
  });
7508
+ var encodeAttachmentContent = (attachment) => typeof attachment.content === "string" ? {
7509
+ content: attachment.content,
7510
+ encoding: attachment.encoding ?? "utf8"
7511
+ } : {
7512
+ content: Buffer.from(attachment.content).toString("base64"),
7513
+ encoding: "base64"
7514
+ };
7515
+ var toTimestamp = (value) => {
7516
+ if (typeof value === "number" && Number.isFinite(value)) {
7517
+ return value;
7518
+ }
7519
+ if (typeof value === "string" || value instanceof Date) {
7520
+ const parsed = new Date(value).getTime();
7521
+ return Number.isFinite(parsed) ? parsed : undefined;
7522
+ }
7523
+ return;
7524
+ };
7220
7525
  var isManagedBySyncSource = (document, sourceId) => document.metadata?.syncSourceId === sourceId;
7221
7526
  var getDocumentSyncFingerprint = (document) => typeof document.metadata?.syncFingerprint === "string" ? document.metadata.syncFingerprint : undefined;
7222
7527
  var reconcileManagedDocuments = async (input) => {
@@ -7442,6 +7747,95 @@ var createRAGStorageSyncSource = (options) => ({
7442
7747
  };
7443
7748
  }
7444
7749
  });
7750
+ var createRAGStaticEmailSyncClient = (input) => ({
7751
+ listMessages: (options) => ({
7752
+ messages: typeof options?.maxResults === "number" ? input.messages.slice(0, options.maxResults) : input.messages
7753
+ })
7754
+ });
7755
+ var createRAGEmailSyncSource = (options) => ({
7756
+ description: options.description,
7757
+ id: options.id,
7758
+ kind: "email",
7759
+ label: options.label,
7760
+ metadata: options.metadata,
7761
+ retryAttempts: options.retryAttempts,
7762
+ retryDelayMs: options.retryDelayMs,
7763
+ target: options.label,
7764
+ sync: async ({ collection, deleteDocument, listDocuments }) => {
7765
+ const listed = await options.client.listMessages({
7766
+ maxResults: options.maxResults
7767
+ });
7768
+ const messageDocuments = listed.messages.map((message) => ({
7769
+ chunking: options.defaultChunking,
7770
+ format: message.bodyHtml ? "html" : "text",
7771
+ id: `email-${message.id}`,
7772
+ metadata: {
7773
+ ...options.baseMetadata ?? {},
7774
+ ...message.metadata ?? {},
7775
+ emailKind: "message",
7776
+ from: message.from,
7777
+ hasAttachments: (message.attachments?.length ?? 0) > 0,
7778
+ messageId: message.id,
7779
+ receivedAt: toTimestamp(message.receivedAt),
7780
+ sentAt: toTimestamp(message.sentAt),
7781
+ threadId: message.threadId,
7782
+ threadTopic: message.subject,
7783
+ to: message.to,
7784
+ cc: message.cc
7785
+ },
7786
+ source: `email/${message.threadId ?? message.id}`,
7787
+ text: message.bodyText,
7788
+ title: message.subject ?? message.id
7789
+ }));
7790
+ const attachmentUploads = listed.messages.flatMap((message) => (message.attachments ?? []).map((attachment, index) => ({
7791
+ ...encodeAttachmentContent(attachment),
7792
+ chunking: attachment.chunking ?? options.defaultChunking,
7793
+ contentType: attachment.contentType,
7794
+ format: attachment.format,
7795
+ metadata: {
7796
+ ...options.baseMetadata ?? {},
7797
+ ...attachment.metadata ?? {},
7798
+ attachmentId: attachment.id ?? `${message.id}-attachment-${index + 1}`,
7799
+ emailKind: "attachment",
7800
+ from: message.from,
7801
+ messageId: message.id,
7802
+ sentAt: toTimestamp(message.sentAt),
7803
+ threadId: message.threadId,
7804
+ threadTopic: message.subject
7805
+ },
7806
+ name: attachment.name,
7807
+ source: attachment.source ?? `email/${message.threadId ?? message.id}/attachments/${attachment.name}`,
7808
+ title: attachment.title ?? `${message.subject ?? message.id} \xB7 ${attachment.name}`
7809
+ })));
7810
+ const loadedAttachments = attachmentUploads.length > 0 ? await loadRAGDocumentsFromUploads({
7811
+ baseMetadata: options.baseMetadata,
7812
+ defaultChunking: options.defaultChunking,
7813
+ extractors: options.extractors,
7814
+ uploads: attachmentUploads
7815
+ }) : { documents: [] };
7816
+ const managedDocuments = [
7817
+ ...messageDocuments.map((document) => toManagedSyncDocument(options.id, document, `message:${document.metadata?.messageId}`)),
7818
+ ...loadedAttachments.documents.map((document) => toManagedSyncDocument(options.id, document, `attachment:${String(document.metadata?.attachmentId ?? document.source ?? document.title ?? "")}`))
7819
+ ];
7820
+ const reconciled = await reconcileManagedDocuments({
7821
+ collection,
7822
+ deleteDocument,
7823
+ documents: managedDocuments,
7824
+ listDocuments,
7825
+ sourceId: options.id
7826
+ });
7827
+ return {
7828
+ chunkCount: reconciled.chunkCount,
7829
+ documentCount: reconciled.documentCount,
7830
+ metadata: {
7831
+ deletedCount: reconciled.deletedCount,
7832
+ messageCount: listed.messages.length,
7833
+ nextCursor: listed.nextCursor,
7834
+ updatedCount: reconciled.updatedCount
7835
+ }
7836
+ };
7837
+ }
7838
+ });
7445
7839
  var createRAGSyncManager = (options) => {
7446
7840
  const sourceMap = new Map(options.sources.map((source) => [source.id, source]));
7447
7841
  const state = new Map(options.sources.map((source) => [source.id, toSourceRecord(source)]));
@@ -9642,6 +10036,7 @@ export {
9642
10036
  createRAGSyncScheduler,
9643
10037
  createRAGSyncManager,
9644
10038
  createRAGStorageSyncSource,
10039
+ createRAGStaticEmailSyncClient,
9645
10040
  createRAGReranker,
9646
10041
  createRAGQueryTransform,
9647
10042
  createRAGPDFOCRExtractor,
@@ -9649,12 +10044,16 @@ export {
9649
10044
  createRAGMediaTranscriber,
9650
10045
  createRAGMediaFileExtractor,
9651
10046
  createRAGImageOCRExtractor,
10047
+ createRAGIMAPEmailSyncClient,
9652
10048
  createRAGHTMXWorkflowRenderConfig,
9653
10049
  createRAGHTMXConfig,
10050
+ createRAGGraphEmailSyncClient,
10051
+ createRAGGmailEmailSyncClient,
9654
10052
  createRAGFileSyncStateStore,
9655
10053
  createRAGFileExtractor,
9656
10054
  createRAGEvaluationSuite,
9657
10055
  createRAGEmbeddingProvider,
10056
+ createRAGEmailSyncSource,
9658
10057
  createRAGDirectorySyncSource,
9659
10058
  createRAGCollection,
9660
10059
  createRAGClient,
@@ -9697,5 +10096,5 @@ export {
9697
10096
  aiChat
9698
10097
  };
9699
10098
 
9700
- //# debugId=1890BBB7D959483A64756E2164756E21
10099
+ //# debugId=BD95265DF2B97B7764756E2164756E21
9701
10100
  //# sourceMappingURL=index.js.map