mitra-interactions-sdk 1.0.69 → 1.0.71
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/README.md +12 -0
- package/dist/index.d.mts +33 -1
- package/dist/index.d.ts +33 -1
- package/dist/index.js +133 -15
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +133 -15
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -118,19 +118,28 @@ async function loginMitra(method, options) {
|
|
|
118
118
|
return doLogin(method, options);
|
|
119
119
|
}
|
|
120
120
|
var REFRESH_TIMEOUT_MS = 15e3;
|
|
121
|
+
var AUTH_HANDOFF_READY_TYPE = "mitra-auth-ready";
|
|
122
|
+
var AUTH_HANDOFF_TOKEN_TYPE = "mitra-auth-refresh-token";
|
|
121
123
|
var _refreshPromise = null;
|
|
122
|
-
function openAuthIframe(url, expectedOrigin) {
|
|
124
|
+
function openAuthIframe(url, expectedOrigin, handoffToken) {
|
|
123
125
|
return new Promise((resolve, reject) => {
|
|
124
126
|
const iframe = document.createElement("iframe");
|
|
125
127
|
iframe.style.display = "none";
|
|
126
128
|
iframe.src = url;
|
|
127
|
-
const
|
|
129
|
+
const timer2 = setTimeout(() => {
|
|
128
130
|
cleanup();
|
|
129
131
|
reject(new Error("Token refresh timeout"));
|
|
130
132
|
}, REFRESH_TIMEOUT_MS);
|
|
131
133
|
function onMessage(event) {
|
|
132
134
|
if (event.origin !== expectedOrigin) return;
|
|
133
|
-
if (!event.data
|
|
135
|
+
if (!event.data) return;
|
|
136
|
+
if (event.data.type === AUTH_HANDOFF_READY_TYPE) {
|
|
137
|
+
if (handoffToken && iframe.contentWindow && event.source === iframe.contentWindow) {
|
|
138
|
+
iframe.contentWindow.postMessage({ type: AUTH_HANDOFF_TOKEN_TYPE, token: handoffToken }, expectedOrigin);
|
|
139
|
+
}
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
if (event.data.type !== AUTH_MESSAGE_TYPE) return;
|
|
134
143
|
cleanup();
|
|
135
144
|
if (event.data.success) {
|
|
136
145
|
resolve(event.data.data);
|
|
@@ -140,20 +149,20 @@ function openAuthIframe(url, expectedOrigin) {
|
|
|
140
149
|
}
|
|
141
150
|
function cleanup() {
|
|
142
151
|
window.removeEventListener("message", onMessage);
|
|
143
|
-
clearTimeout(
|
|
152
|
+
clearTimeout(timer2);
|
|
144
153
|
if (iframe.parentNode) iframe.parentNode.removeChild(iframe);
|
|
145
154
|
}
|
|
146
155
|
window.addEventListener("message", onMessage);
|
|
147
156
|
document.body.appendChild(iframe);
|
|
148
157
|
});
|
|
149
158
|
}
|
|
150
|
-
async function refreshTokenSilently(authUrl, projectId) {
|
|
159
|
+
async function refreshTokenSilently(authUrl, projectId, currentToken) {
|
|
151
160
|
if (_refreshPromise) return _refreshPromise;
|
|
152
161
|
_refreshPromise = (async () => {
|
|
153
162
|
try {
|
|
154
163
|
const origin = getOriginFromUrl(authUrl);
|
|
155
|
-
const url = buildAuthUrl(authUrl, "refresh", projectId);
|
|
156
|
-
const response = await openAuthIframe(url, origin);
|
|
164
|
+
const url = buildAuthUrl(authUrl, "refresh", projectId) + (currentToken ? "&handoff=1" : "");
|
|
165
|
+
const response = await openAuthIframe(url, origin, currentToken);
|
|
157
166
|
return response;
|
|
158
167
|
} finally {
|
|
159
168
|
_refreshPromise = null;
|
|
@@ -175,7 +184,7 @@ function openSilentAuthIframe(authUrl, projectId, credentials) {
|
|
|
175
184
|
}
|
|
176
185
|
const iframe = document.createElement("iframe");
|
|
177
186
|
iframe.style.display = "none";
|
|
178
|
-
const
|
|
187
|
+
const timer2 = setTimeout(() => {
|
|
179
188
|
cleanup();
|
|
180
189
|
reject(new Error("Timeout na autentica\xE7\xE3o silenciosa"));
|
|
181
190
|
}, SILENT_TIMEOUT_MS);
|
|
@@ -199,7 +208,7 @@ function openSilentAuthIframe(authUrl, projectId, credentials) {
|
|
|
199
208
|
}
|
|
200
209
|
function cleanup() {
|
|
201
210
|
window.removeEventListener("message", onMessage);
|
|
202
|
-
clearTimeout(
|
|
211
|
+
clearTimeout(timer2);
|
|
203
212
|
if (iframe.parentNode) iframe.parentNode.removeChild(iframe);
|
|
204
213
|
}
|
|
205
214
|
window.addEventListener("message", onMessage);
|
|
@@ -381,6 +390,105 @@ function withServerFunctionProof(headers, proof = readServerFunctionProof()) {
|
|
|
381
390
|
return { ...headers, [SF_EXECUTION_TOKEN_HEADER]: proof };
|
|
382
391
|
}
|
|
383
392
|
|
|
393
|
+
// src/session-watch.ts
|
|
394
|
+
var RENEW_BEFORE_MS = 6e4;
|
|
395
|
+
var RETRY_MS = 1e4;
|
|
396
|
+
var MAX_TIMEOUT_MS = 2147483647;
|
|
397
|
+
var refresher = null;
|
|
398
|
+
var timer = null;
|
|
399
|
+
var listening = false;
|
|
400
|
+
var expiryAtLastAttempt = null;
|
|
401
|
+
var notifiedFor = null;
|
|
402
|
+
function registerSessionRefresher(fn) {
|
|
403
|
+
refresher = fn;
|
|
404
|
+
}
|
|
405
|
+
function limitedSessionExpiry(token) {
|
|
406
|
+
if (!token) return null;
|
|
407
|
+
const payload = decodeJwtPayload(token);
|
|
408
|
+
if (!payload || typeof payload.authTime !== "number" || typeof payload.exp !== "number") return null;
|
|
409
|
+
return payload.exp * 1e3;
|
|
410
|
+
}
|
|
411
|
+
function currentExpiry() {
|
|
412
|
+
if (typeof window === "undefined" || !isConfigured()) return null;
|
|
413
|
+
const config = getConfig();
|
|
414
|
+
if (!config.token || !config.authUrl || config.projectId == null) return null;
|
|
415
|
+
return limitedSessionExpiry(config.token);
|
|
416
|
+
}
|
|
417
|
+
function clearTimer() {
|
|
418
|
+
if (timer) {
|
|
419
|
+
clearTimeout(timer);
|
|
420
|
+
timer = null;
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
function startSessionWatch() {
|
|
424
|
+
expiryAtLastAttempt = null;
|
|
425
|
+
notifiedFor = null;
|
|
426
|
+
watchSession();
|
|
427
|
+
}
|
|
428
|
+
function watchSession() {
|
|
429
|
+
clearTimer();
|
|
430
|
+
const expiry = currentExpiry();
|
|
431
|
+
if (expiry == null) return;
|
|
432
|
+
listenForReturn();
|
|
433
|
+
let delay = Math.max(expiry - Date.now() - RENEW_BEFORE_MS, 0);
|
|
434
|
+
if (expiryAtLastAttempt != null && expiry <= expiryAtLastAttempt + 1e3) {
|
|
435
|
+
delay = Math.max(expiry - Date.now() + 1e3, RETRY_MS);
|
|
436
|
+
}
|
|
437
|
+
if (delay > MAX_TIMEOUT_MS) return;
|
|
438
|
+
timer = setTimeout(() => {
|
|
439
|
+
void renewNow();
|
|
440
|
+
}, delay);
|
|
441
|
+
}
|
|
442
|
+
async function renewNow() {
|
|
443
|
+
clearTimer();
|
|
444
|
+
const before = currentExpiry();
|
|
445
|
+
if (before == null || !refresher) return;
|
|
446
|
+
expiryAtLastAttempt = before;
|
|
447
|
+
const renewed = await refresher().catch(() => false);
|
|
448
|
+
if (renewed) return;
|
|
449
|
+
const expiry = currentExpiry();
|
|
450
|
+
if (expiry != null && Date.now() < expiry) {
|
|
451
|
+
timer = setTimeout(() => {
|
|
452
|
+
void renewNow();
|
|
453
|
+
}, RETRY_MS);
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
function onReturn() {
|
|
457
|
+
const expiry = currentExpiry();
|
|
458
|
+
if (expiry == null) return;
|
|
459
|
+
const atSessionEnd = expiryAtLastAttempt != null && expiry <= expiryAtLastAttempt + 1e3;
|
|
460
|
+
if (expiry - Date.now() <= RENEW_BEFORE_MS && (!atSessionEnd || Date.now() >= expiry)) {
|
|
461
|
+
void renewNow();
|
|
462
|
+
} else {
|
|
463
|
+
watchSession();
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
function listenForReturn() {
|
|
467
|
+
if (listening || typeof window === "undefined") return;
|
|
468
|
+
listening = true;
|
|
469
|
+
if (typeof document !== "undefined" && typeof document.addEventListener === "function") {
|
|
470
|
+
document.addEventListener("visibilitychange", () => {
|
|
471
|
+
if (document.visibilityState === "visible") onReturn();
|
|
472
|
+
});
|
|
473
|
+
}
|
|
474
|
+
if (typeof window.addEventListener === "function") {
|
|
475
|
+
window.addEventListener("online", onReturn);
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
function reportRefreshFailure() {
|
|
479
|
+
var _a, _b;
|
|
480
|
+
const expiry = currentExpiry();
|
|
481
|
+
if (expiry == null || Date.now() < expiry) return;
|
|
482
|
+
const config = getConfig();
|
|
483
|
+
if (notifiedFor === config.token) return;
|
|
484
|
+
notifiedFor = (_a = config.token) != null ? _a : null;
|
|
485
|
+
clearTimer();
|
|
486
|
+
try {
|
|
487
|
+
(_b = config.onSessionExpired) == null ? void 0 : _b.call(config);
|
|
488
|
+
} catch (e) {
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
|
|
384
492
|
// src/client.ts
|
|
385
493
|
function getFetch2() {
|
|
386
494
|
if (typeof globalThis.fetch === "function") {
|
|
@@ -415,14 +523,18 @@ function tryRefreshToken() {
|
|
|
415
523
|
if (refreshInFlight === attempt) refreshInFlight = null;
|
|
416
524
|
};
|
|
417
525
|
attempt.then(clear, clear);
|
|
526
|
+
attempt.then((renewed) => {
|
|
527
|
+
if (!renewed) reportRefreshFailure();
|
|
528
|
+
}, () => void 0);
|
|
418
529
|
return attempt;
|
|
419
530
|
}
|
|
531
|
+
registerSessionRefresher(tryRefreshToken);
|
|
420
532
|
async function doRefreshToken() {
|
|
421
533
|
const config = getConfig();
|
|
422
534
|
if (!config.token || config.projectId == null || !config.baseURL) return false;
|
|
423
535
|
if (!config.authUrl || typeof window === "undefined") return false;
|
|
424
536
|
try {
|
|
425
|
-
const session = await refreshTokenSilently(config.authUrl, config.projectId);
|
|
537
|
+
const session = await refreshTokenSilently(config.authUrl, config.projectId, config.token);
|
|
426
538
|
updateGlobalToken(session.token);
|
|
427
539
|
if (config.onTokenRefresh) config.onTokenRefresh(session);
|
|
428
540
|
return true;
|
|
@@ -709,6 +821,9 @@ async function callIntegrationMitra(options) {
|
|
|
709
821
|
if (options.body !== void 0) {
|
|
710
822
|
body.body = options.body;
|
|
711
823
|
}
|
|
824
|
+
if (options.headers !== void 0) {
|
|
825
|
+
body.headers = options.headers;
|
|
826
|
+
}
|
|
712
827
|
return http.post("/interactions/integrations/call", body);
|
|
713
828
|
}
|
|
714
829
|
async function stopServerFunctionExecutionMitra(options) {
|
|
@@ -1498,7 +1613,7 @@ function openSocket() {
|
|
|
1498
1613
|
return new Promise((resolve, reject) => {
|
|
1499
1614
|
const socket = new WebSocket(url);
|
|
1500
1615
|
let opened = false;
|
|
1501
|
-
const
|
|
1616
|
+
const timer2 = setTimeout(() => {
|
|
1502
1617
|
if (!opened) {
|
|
1503
1618
|
try {
|
|
1504
1619
|
socket.close();
|
|
@@ -1509,13 +1624,13 @@ function openSocket() {
|
|
|
1509
1624
|
}, 15e3);
|
|
1510
1625
|
socket.onopen = () => {
|
|
1511
1626
|
opened = true;
|
|
1512
|
-
clearTimeout(
|
|
1627
|
+
clearTimeout(timer2);
|
|
1513
1628
|
ws = socket;
|
|
1514
1629
|
resolve(socket);
|
|
1515
1630
|
};
|
|
1516
1631
|
socket.onerror = () => {
|
|
1517
1632
|
if (!opened) {
|
|
1518
|
-
clearTimeout(
|
|
1633
|
+
clearTimeout(timer2);
|
|
1519
1634
|
reject(new Error("Agent Chat: erro ao conectar no WebSocket"));
|
|
1520
1635
|
}
|
|
1521
1636
|
};
|
|
@@ -1523,7 +1638,7 @@ function openSocket() {
|
|
|
1523
1638
|
if (ws === socket) ws = null;
|
|
1524
1639
|
const detalhe = closeDetail(event);
|
|
1525
1640
|
if (!opened) {
|
|
1526
|
-
clearTimeout(
|
|
1641
|
+
clearTimeout(timer2);
|
|
1527
1642
|
reject(new Error(`Agent Chat: conex\xE3o fechada${detalhe}`));
|
|
1528
1643
|
}
|
|
1529
1644
|
for (const [id, pending] of pendingRequests) {
|
|
@@ -1898,7 +2013,7 @@ function createMitraInstance(initialConfig) {
|
|
|
1898
2013
|
if (!_config.token || _config.projectId == null || !_config.baseURL) return false;
|
|
1899
2014
|
if (!_config.authUrl || typeof window === "undefined") return false;
|
|
1900
2015
|
try {
|
|
1901
|
-
const session = await refreshTokenSilently(_config.authUrl, _config.projectId);
|
|
2016
|
+
const session = await refreshTokenSilently(_config.authUrl, _config.projectId, _config.token);
|
|
1902
2017
|
_config = { ..._config, token: stripBearer(session.token) };
|
|
1903
2018
|
updateGlobalToken(session.token);
|
|
1904
2019
|
if (_config.onTokenRefresh) _config.onTokenRefresh(session);
|
|
@@ -2126,6 +2241,7 @@ function createMitraInstance(initialConfig) {
|
|
|
2126
2241
|
if (options.endpoint !== void 0) body.endpoint = options.endpoint;
|
|
2127
2242
|
if (options.params !== void 0) body.params = options.params;
|
|
2128
2243
|
if (options.body !== void 0) body.body = options.body;
|
|
2244
|
+
if (options.headers !== void 0) body.headers = options.headers;
|
|
2129
2245
|
return request("POST", "/interactions/integrations/call", { body });
|
|
2130
2246
|
},
|
|
2131
2247
|
// Dynamic Schema CRUD
|
|
@@ -2308,6 +2424,7 @@ function configureSdkMitra(config = {}) {
|
|
|
2308
2424
|
const chat = window.__mitraChat;
|
|
2309
2425
|
if (chat) chat.init(merged.token);
|
|
2310
2426
|
}
|
|
2427
|
+
startSessionWatch();
|
|
2311
2428
|
return createMitraInstance(globalConfig);
|
|
2312
2429
|
}
|
|
2313
2430
|
function getConfig() {
|
|
@@ -2322,6 +2439,7 @@ function isConfigured() {
|
|
|
2322
2439
|
function updateGlobalToken(token) {
|
|
2323
2440
|
if (globalConfig) {
|
|
2324
2441
|
globalConfig = { ...globalConfig, token: stripBearer(token) };
|
|
2442
|
+
watchSession();
|
|
2325
2443
|
}
|
|
2326
2444
|
}
|
|
2327
2445
|
function inferProjectId(projectId) {
|