@absolutejs/rag 0.0.3 → 0.0.5
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/rag/index.js +57 -17
- package/dist/ai/rag/index.js.map +3 -3
- package/package.json +131 -104
package/dist/ai/rag/index.js
CHANGED
|
@@ -31524,12 +31524,61 @@ var parseRawEmail = (raw) => {
|
|
|
31524
31524
|
var DEFAULT_GMAIL_READONLY_SCOPES = [
|
|
31525
31525
|
"https://www.googleapis.com/auth/gmail.readonly"
|
|
31526
31526
|
];
|
|
31527
|
+
var GMAIL_MAX_RETRY_ATTEMPTS = 2;
|
|
31528
|
+
var GMAIL_BASE_RETRY_DELAY_MS = 500;
|
|
31529
|
+
var sleep = (ms) => new Promise((resolve2) => setTimeout(resolve2, ms));
|
|
31530
|
+
var getRetryDelayMs = (response, attempt) => {
|
|
31531
|
+
const retryAfterHeader = response.headers.get("retry-after");
|
|
31532
|
+
const retryAfterSeconds = retryAfterHeader ? Number(retryAfterHeader) : Number.NaN;
|
|
31533
|
+
if (Number.isFinite(retryAfterSeconds) && retryAfterSeconds >= 0) {
|
|
31534
|
+
return retryAfterSeconds * 1000;
|
|
31535
|
+
}
|
|
31536
|
+
return GMAIL_BASE_RETRY_DELAY_MS * 2 ** attempt;
|
|
31537
|
+
};
|
|
31538
|
+
var readGmailError = async (response) => {
|
|
31539
|
+
let reason;
|
|
31540
|
+
let detailMessage;
|
|
31541
|
+
try {
|
|
31542
|
+
const body = await response.clone().json();
|
|
31543
|
+
reason = body.error?.errors?.[0]?.reason;
|
|
31544
|
+
detailMessage = body.error?.errors?.[0]?.message ?? body.error?.message;
|
|
31545
|
+
} catch {
|
|
31546
|
+
const text = await response.clone().text();
|
|
31547
|
+
detailMessage = text.trim().length > 0 ? text.trim() : undefined;
|
|
31548
|
+
}
|
|
31549
|
+
return { detailMessage, reason };
|
|
31550
|
+
};
|
|
31551
|
+
var createGmailHttpError = async (label, response) => {
|
|
31552
|
+
const { detailMessage, reason } = await readGmailError(response);
|
|
31553
|
+
const suffix = [reason, detailMessage].filter((value, index, values) => typeof value === "string" && value.length > 0 && values.indexOf(value) === index).join(" | ");
|
|
31554
|
+
return new Error(`${label}: ${response.status} ${response.statusText}${suffix ? ` (${suffix})` : ""}`);
|
|
31555
|
+
};
|
|
31556
|
+
var fetchGmailWithRetry = async (fetchImpl, url, init, label) => {
|
|
31557
|
+
for (let attempt = 0;attempt <= GMAIL_MAX_RETRY_ATTEMPTS; attempt += 1) {
|
|
31558
|
+
const response = await fetchImpl(url, init);
|
|
31559
|
+
if (response.ok) {
|
|
31560
|
+
return response;
|
|
31561
|
+
}
|
|
31562
|
+
if (response.status === 429 && attempt < GMAIL_MAX_RETRY_ATTEMPTS) {
|
|
31563
|
+
await sleep(getRetryDelayMs(response, attempt));
|
|
31564
|
+
continue;
|
|
31565
|
+
}
|
|
31566
|
+
throw await createGmailHttpError(label, response);
|
|
31567
|
+
}
|
|
31568
|
+
throw new Error(`${label}: retry budget exhausted`);
|
|
31569
|
+
};
|
|
31527
31570
|
var inferLinkedProviderFailureCode = (error) => {
|
|
31528
31571
|
const message = error instanceof Error ? error.message : String(error ?? "Unknown error");
|
|
31529
31572
|
if (/\b401\b/.test(message)) {
|
|
31530
31573
|
return "unauthorized";
|
|
31531
31574
|
}
|
|
31532
|
-
if (/\
|
|
31575
|
+
if (/\b429\b|too many requests|rate.?limit/i.test(message)) {
|
|
31576
|
+
return "rate_limited";
|
|
31577
|
+
}
|
|
31578
|
+
if (/accessnotconfigured|service_disabled|permission_denied/i.test(message)) {
|
|
31579
|
+
return "provider_error";
|
|
31580
|
+
}
|
|
31581
|
+
if (/\b403\b|insufficientpermissions|insufficient_scope/i.test(message)) {
|
|
31533
31582
|
return "insufficient_scope";
|
|
31534
31583
|
}
|
|
31535
31584
|
return "provider_error";
|
|
@@ -31552,33 +31601,24 @@ var createRAGGmailEmailSyncClient = (config) => ({
|
|
|
31552
31601
|
if (config.includeSpamTrash) {
|
|
31553
31602
|
listUrl.searchParams.set("includeSpamTrash", "true");
|
|
31554
31603
|
}
|
|
31555
|
-
const listResponse = await fetchImpl
|
|
31604
|
+
const listResponse = await fetchGmailWithRetry(fetchImpl, listUrl, {
|
|
31556
31605
|
headers: { Authorization: `Bearer ${config.accessToken}` }
|
|
31557
|
-
});
|
|
31558
|
-
if (!listResponse.ok) {
|
|
31559
|
-
throw new Error(`Gmail list failed: ${listResponse.status} ${listResponse.statusText}`);
|
|
31560
|
-
}
|
|
31606
|
+
}, "Gmail list failed");
|
|
31561
31607
|
const listJson = await listResponse.json();
|
|
31562
31608
|
const messages = await Promise.all((listJson.messages ?? []).map(async (messageRef) => {
|
|
31563
31609
|
const getUrl = new URL(`https://gmail.googleapis.com/gmail/v1/users/${encodeURIComponent(userId)}/messages/${encodeURIComponent(messageRef.id)}`);
|
|
31564
31610
|
getUrl.searchParams.set("format", "full");
|
|
31565
|
-
const response = await fetchImpl
|
|
31611
|
+
const response = await fetchGmailWithRetry(fetchImpl, getUrl, {
|
|
31566
31612
|
headers: { Authorization: `Bearer ${config.accessToken}` }
|
|
31567
|
-
});
|
|
31568
|
-
if (!response.ok) {
|
|
31569
|
-
throw new Error(`Gmail get failed for ${messageRef.id}: ${response.status} ${response.statusText}`);
|
|
31570
|
-
}
|
|
31613
|
+
}, `Gmail get failed for ${messageRef.id}`);
|
|
31571
31614
|
const json = await response.json();
|
|
31572
31615
|
const attachments = await Promise.all(collectGmailPayloadParts(json.payload).filter((part) => !!part.filename && !!part.body?.attachmentId).map(async (part) => {
|
|
31573
31616
|
const attachmentUrl = new URL(`https://gmail.googleapis.com/gmail/v1/users/${encodeURIComponent(userId)}/messages/${encodeURIComponent(json.id)}/attachments/${encodeURIComponent(part.body?.attachmentId ?? "")}`);
|
|
31574
|
-
const attachmentResponse = await fetchImpl
|
|
31617
|
+
const attachmentResponse = await fetchGmailWithRetry(fetchImpl, attachmentUrl, {
|
|
31575
31618
|
headers: {
|
|
31576
31619
|
Authorization: `Bearer ${config.accessToken}`
|
|
31577
31620
|
}
|
|
31578
|
-
});
|
|
31579
|
-
if (!attachmentResponse.ok) {
|
|
31580
|
-
return null;
|
|
31581
|
-
}
|
|
31621
|
+
}, `Gmail attachment get failed for ${json.id}:${part.body?.attachmentId ?? "attachment"}`);
|
|
31582
31622
|
const attachmentJson = await attachmentResponse.json();
|
|
31583
31623
|
if (!attachmentJson.data || !part.filename) {
|
|
31584
31624
|
return null;
|
|
@@ -37019,5 +37059,5 @@ export {
|
|
|
37019
37059
|
addRAGEvaluationSuiteCase
|
|
37020
37060
|
};
|
|
37021
37061
|
|
|
37022
|
-
//# debugId=
|
|
37062
|
+
//# debugId=46D96811C8BB335F64756E2164756E21
|
|
37023
37063
|
//# sourceMappingURL=index.js.map
|