@asiyst/sdk 0.1.15 → 0.1.18
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.cjs +208 -60
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +208 -60
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.cts
CHANGED
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -71,7 +71,7 @@ var ALL_ACTION_KINDS = [
|
|
|
71
71
|
// src/core/constants.ts
|
|
72
72
|
function resolveSdkVersion() {
|
|
73
73
|
{
|
|
74
|
-
return "0.1.
|
|
74
|
+
return "0.1.16";
|
|
75
75
|
}
|
|
76
76
|
}
|
|
77
77
|
var SDK_VERSION = resolveSdkVersion();
|
|
@@ -421,59 +421,148 @@ var EventBus = class {
|
|
|
421
421
|
};
|
|
422
422
|
|
|
423
423
|
// src/communication/HttpTransport.ts
|
|
424
|
+
var DEFAULT_TIMEOUT_MS = 1e4;
|
|
425
|
+
function generateRequestId() {
|
|
426
|
+
return `req_${Math.random().toString(36).slice(2, 10)}_${Date.now().toString(36)}`;
|
|
427
|
+
}
|
|
424
428
|
var HttpTransport = class {
|
|
425
429
|
constructor(options) {
|
|
426
430
|
this.options = options;
|
|
427
431
|
}
|
|
428
432
|
async request(req) {
|
|
429
433
|
const url = `${this.options.apiBaseUrl.replace(/\/$/, "")}${req.path}`;
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
});
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
434
|
+
const requestId = req.requestId || generateRequestId();
|
|
435
|
+
const timeoutMs = req.timeoutMs ?? this.options.defaultTimeoutMs ?? DEFAULT_TIMEOUT_MS;
|
|
436
|
+
const isSafeOrIdempotent = req.method === "GET" || Boolean(req.idempotent);
|
|
437
|
+
const maxRetries = isSafeOrIdempotent ? req.retries ?? 2 : 0;
|
|
438
|
+
let lastError;
|
|
439
|
+
for (let attempt = 0; attempt <= maxRetries; attempt++) {
|
|
440
|
+
if (attempt > 0) {
|
|
441
|
+
const delay = Math.min(50 * Math.pow(2, attempt - 1), 500);
|
|
442
|
+
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
443
|
+
}
|
|
444
|
+
const controller = new AbortController();
|
|
445
|
+
let timedOut = false;
|
|
446
|
+
const timeoutId = setTimeout(() => {
|
|
447
|
+
timedOut = true;
|
|
448
|
+
controller.abort();
|
|
449
|
+
}, timeoutMs);
|
|
450
|
+
const onExternalAbort = () => controller.abort();
|
|
451
|
+
if (req.signal) {
|
|
452
|
+
if (req.signal.aborted) {
|
|
453
|
+
clearTimeout(timeoutId);
|
|
454
|
+
controller.abort();
|
|
455
|
+
} else {
|
|
456
|
+
req.signal.addEventListener("abort", onExternalAbort, { once: true });
|
|
457
|
+
}
|
|
458
|
+
}
|
|
452
459
|
try {
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
460
|
+
let response;
|
|
461
|
+
try {
|
|
462
|
+
response = await fetch(url, {
|
|
463
|
+
method: req.method,
|
|
464
|
+
headers: {
|
|
465
|
+
Accept: "application/json",
|
|
466
|
+
"Content-Type": "application/json",
|
|
467
|
+
"X-Asiyst-Project-Id": this.options.projectId,
|
|
468
|
+
"X-Asiyst-Public-Key": this.options.publicKey,
|
|
469
|
+
...this.options.avatarId ? { "X-Asiyst-Avatar-Id": this.options.avatarId } : {},
|
|
470
|
+
"X-Asiyst-SDK-Version": SDK_VERSION,
|
|
471
|
+
"X-Asiyst-Request-Id": requestId,
|
|
472
|
+
...req.sessionId ? { "X-Asiyst-Session-Id": req.sessionId } : {},
|
|
473
|
+
...req.conversationId ? { "X-Asiyst-Conversation-Id": req.conversationId } : {},
|
|
474
|
+
...req.authorizationToken ? { Authorization: `Bearer ${req.authorizationToken}` } : {}
|
|
475
|
+
},
|
|
476
|
+
body: req.body === void 0 ? void 0 : JSON.stringify(req.body),
|
|
477
|
+
signal: controller.signal
|
|
478
|
+
});
|
|
479
|
+
} finally {
|
|
480
|
+
clearTimeout(timeoutId);
|
|
481
|
+
if (req.signal) {
|
|
482
|
+
req.signal.removeEventListener("abort", onExternalAbort);
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
if (isSafeOrIdempotent && attempt < maxRetries && [502, 503, 504].includes(response.status)) {
|
|
486
|
+
continue;
|
|
487
|
+
}
|
|
488
|
+
if (response.status === 429 && isSafeOrIdempotent && attempt < maxRetries) {
|
|
489
|
+
const retryAfterHeader = response.headers.get("retry-after");
|
|
490
|
+
const retryAfterMs = retryAfterHeader ? Math.min(Number(retryAfterHeader) * 1e3 || 200, 1e3) : 100;
|
|
491
|
+
await new Promise((resolve) => setTimeout(resolve, retryAfterMs));
|
|
492
|
+
continue;
|
|
493
|
+
}
|
|
494
|
+
let data = null;
|
|
495
|
+
const contentType = response.headers.get("content-type") ?? "";
|
|
496
|
+
if (contentType.includes("application/json")) {
|
|
497
|
+
try {
|
|
498
|
+
data = await response.json();
|
|
499
|
+
} catch {
|
|
500
|
+
data = null;
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
if (!response.ok) {
|
|
504
|
+
return { ok: false, status: response.status, data };
|
|
505
|
+
}
|
|
506
|
+
return { ok: true, status: response.status, data };
|
|
507
|
+
} catch (err) {
|
|
508
|
+
clearTimeout(timeoutId);
|
|
509
|
+
if (req.signal) {
|
|
510
|
+
req.signal.removeEventListener("abort", onExternalAbort);
|
|
511
|
+
}
|
|
512
|
+
if (timedOut) {
|
|
513
|
+
throw new NetworkError("Asiyst Cloud request timed out");
|
|
514
|
+
}
|
|
515
|
+
lastError = err;
|
|
516
|
+
if (isSafeOrIdempotent && attempt < maxRetries) {
|
|
517
|
+
continue;
|
|
518
|
+
}
|
|
519
|
+
throw new NetworkError("Asiyst Cloud is unreachable");
|
|
456
520
|
}
|
|
457
521
|
}
|
|
458
|
-
|
|
459
|
-
return { ok: false, status: response.status, data };
|
|
460
|
-
}
|
|
461
|
-
return { ok: true, status: response.status, data };
|
|
522
|
+
throw lastError instanceof Error ? lastError : new NetworkError("Asiyst Cloud is unreachable");
|
|
462
523
|
}
|
|
463
524
|
};
|
|
464
525
|
|
|
465
526
|
// src/communication/CloudClient.ts
|
|
466
527
|
var CloudClient = class {
|
|
467
|
-
constructor(transport, projectId, publicKey = "", runtimeAuth) {
|
|
528
|
+
constructor(transport, projectId, publicKey = "", runtimeAuth, sessionId) {
|
|
468
529
|
this.transport = transport;
|
|
469
530
|
this.projectId = projectId;
|
|
470
531
|
this.publicKey = publicKey;
|
|
471
532
|
this.runtimeAuth = runtimeAuth;
|
|
533
|
+
this.inFlightConversations = /* @__PURE__ */ new Map();
|
|
534
|
+
this.sessionId = sessionId;
|
|
535
|
+
}
|
|
536
|
+
setSessionId(sessionId) {
|
|
537
|
+
this.sessionId = sessionId;
|
|
538
|
+
}
|
|
539
|
+
getSessionId() {
|
|
540
|
+
return this.sessionId;
|
|
541
|
+
}
|
|
542
|
+
setConversationId(conversationId) {
|
|
543
|
+
this.conversationId = conversationId;
|
|
544
|
+
}
|
|
545
|
+
getConversationId() {
|
|
546
|
+
return this.conversationId;
|
|
547
|
+
}
|
|
548
|
+
async getAuthToken(capability) {
|
|
549
|
+
if (!this.runtimeAuth) return void 0;
|
|
550
|
+
if (this.tokenRefreshPromise) return this.tokenRefreshPromise;
|
|
551
|
+
if (!this.inFlightToken) {
|
|
552
|
+
this.inFlightToken = this.runtimeAuth.getToken(capability).then((token) => {
|
|
553
|
+
this.activeToken = token;
|
|
554
|
+
return token;
|
|
555
|
+
}).finally(() => {
|
|
556
|
+
this.inFlightToken = void 0;
|
|
557
|
+
});
|
|
558
|
+
}
|
|
559
|
+
return this.inFlightToken;
|
|
472
560
|
}
|
|
473
561
|
async fetchConfig() {
|
|
474
562
|
const response = await this.runtimeRequest({
|
|
475
563
|
path: `/projects/${encodeURIComponent(this.projectId)}/avatar`,
|
|
476
|
-
method: "GET"
|
|
564
|
+
method: "GET",
|
|
565
|
+
idempotent: true
|
|
477
566
|
});
|
|
478
567
|
if (response.status === 401 || response.status === 403) {
|
|
479
568
|
throw new AuthenticationError("Project credentials were rejected");
|
|
@@ -487,6 +576,7 @@ var CloudClient = class {
|
|
|
487
576
|
const response = await this.runtimeRequest({
|
|
488
577
|
path: "/sdk/heartbeat",
|
|
489
578
|
method: "POST",
|
|
579
|
+
idempotent: true,
|
|
490
580
|
body: {
|
|
491
581
|
project_id: this.projectId,
|
|
492
582
|
public_key: this.publicKey,
|
|
@@ -511,7 +601,9 @@ var CloudClient = class {
|
|
|
511
601
|
const response = await this.transport.request({
|
|
512
602
|
path: "/sdk/installations/register",
|
|
513
603
|
method: "POST",
|
|
514
|
-
body: registration
|
|
604
|
+
body: registration,
|
|
605
|
+
idempotent: true,
|
|
606
|
+
sessionId: this.sessionId
|
|
515
607
|
});
|
|
516
608
|
if (!response.ok) {
|
|
517
609
|
throw new NetworkError(`SDK installation registration failed (${response.status}).`);
|
|
@@ -536,7 +628,8 @@ var CloudClient = class {
|
|
|
536
628
|
async fetchWorkflow(workflowId) {
|
|
537
629
|
const response = await this.runtimeRequest({
|
|
538
630
|
path: `/workflows/${encodeURIComponent(workflowId)}`,
|
|
539
|
-
method: "GET"
|
|
631
|
+
method: "GET",
|
|
632
|
+
idempotent: true
|
|
540
633
|
}, "assistant:conversation");
|
|
541
634
|
if (!response.ok || !response.data?.workflow?.id || !Array.isArray(response.data.workflow.steps)) {
|
|
542
635
|
throw new NetworkError("Workflow is unavailable");
|
|
@@ -544,15 +637,30 @@ var CloudClient = class {
|
|
|
544
637
|
return response.data.workflow;
|
|
545
638
|
}
|
|
546
639
|
async sendConversationMessage(text, pageUrl) {
|
|
547
|
-
const
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
if (!response.ok || !response.data?.message?.text) {
|
|
553
|
-
throw new NetworkError("Conversation service is unavailable");
|
|
640
|
+
const trimmed = text.trim();
|
|
641
|
+
const dedupeKey2 = `${this.conversationId ?? ""}:${trimmed}:${pageUrl}`;
|
|
642
|
+
const existing = this.inFlightConversations.get(dedupeKey2);
|
|
643
|
+
if (existing) {
|
|
644
|
+
return existing;
|
|
554
645
|
}
|
|
555
|
-
|
|
646
|
+
const conversationPromise = (async () => {
|
|
647
|
+
try {
|
|
648
|
+
const response = await this.runtimeRequest({
|
|
649
|
+
path: "/conversations/messages",
|
|
650
|
+
method: "POST",
|
|
651
|
+
body: { projectId: this.projectId, text: trimmed, pageUrl },
|
|
652
|
+
conversationId: this.conversationId
|
|
653
|
+
}, "assistant:conversation");
|
|
654
|
+
if (!response.ok || !response.data?.message?.text) {
|
|
655
|
+
throw new NetworkError("Conversation service is unavailable");
|
|
656
|
+
}
|
|
657
|
+
return response.data;
|
|
658
|
+
} finally {
|
|
659
|
+
this.inFlightConversations.delete(dedupeKey2);
|
|
660
|
+
}
|
|
661
|
+
})();
|
|
662
|
+
this.inFlightConversations.set(dedupeKey2, conversationPromise);
|
|
663
|
+
return conversationPromise;
|
|
556
664
|
}
|
|
557
665
|
async sendWebsiteMap(snapshot) {
|
|
558
666
|
await this.safePost("/website-maps", snapshot);
|
|
@@ -568,20 +676,35 @@ var CloudClient = class {
|
|
|
568
676
|
}
|
|
569
677
|
async safePost(path, body) {
|
|
570
678
|
try {
|
|
571
|
-
await this.runtimeRequest({ path, method: "POST", body }, "assistant:telemetry");
|
|
679
|
+
await this.runtimeRequest({ path, method: "POST", body, idempotent: true }, "assistant:telemetry");
|
|
572
680
|
} catch {
|
|
573
681
|
}
|
|
574
682
|
}
|
|
575
683
|
async runtimeRequest(request, capability = "assistant:config") {
|
|
684
|
+
const fullRequest = {
|
|
685
|
+
...request,
|
|
686
|
+
sessionId: request.sessionId ?? this.sessionId,
|
|
687
|
+
conversationId: request.conversationId ?? this.conversationId
|
|
688
|
+
};
|
|
576
689
|
if (!this.runtimeAuth) {
|
|
577
|
-
return this.transport.request(
|
|
690
|
+
return this.transport.request(fullRequest);
|
|
578
691
|
}
|
|
579
|
-
let token = await this.
|
|
580
|
-
let response = await this.transport.request({ ...
|
|
692
|
+
let token = await this.getAuthToken(capability);
|
|
693
|
+
let response = await this.transport.request({ ...fullRequest, authorizationToken: token });
|
|
581
694
|
if (response.status === 401) {
|
|
582
|
-
this.
|
|
583
|
-
|
|
584
|
-
|
|
695
|
+
if (!this.tokenRefreshPromise) {
|
|
696
|
+
this.runtimeAuth.invalidate();
|
|
697
|
+
this.tokenRefreshPromise = this.runtimeAuth.getToken(capability).then((newToken) => {
|
|
698
|
+
this.activeToken = newToken;
|
|
699
|
+
return newToken;
|
|
700
|
+
}).finally(() => {
|
|
701
|
+
setTimeout(() => {
|
|
702
|
+
this.tokenRefreshPromise = void 0;
|
|
703
|
+
}, 50);
|
|
704
|
+
});
|
|
705
|
+
}
|
|
706
|
+
token = await this.tokenRefreshPromise ?? this.activeToken;
|
|
707
|
+
response = await this.transport.request({ ...fullRequest, authorizationToken: token });
|
|
585
708
|
}
|
|
586
709
|
return response;
|
|
587
710
|
}
|
|
@@ -2271,8 +2394,8 @@ var ConversationPanel = class {
|
|
|
2271
2394
|
this.conversationId = "";
|
|
2272
2395
|
this.openState = false;
|
|
2273
2396
|
const doc = root.ownerDocument;
|
|
2274
|
-
|
|
2275
|
-
style.textContent = `
|
|
2397
|
+
this.style = doc.createElement("style");
|
|
2398
|
+
this.style.textContent = `
|
|
2276
2399
|
.asiyst-panel {
|
|
2277
2400
|
position: fixed;
|
|
2278
2401
|
right: 16px;
|
|
@@ -2308,7 +2431,7 @@ var ConversationPanel = class {
|
|
|
2308
2431
|
}
|
|
2309
2432
|
.asiyst-close { background: transparent; color: #f8fafc; }
|
|
2310
2433
|
`;
|
|
2311
|
-
root.appendChild(style);
|
|
2434
|
+
root.appendChild(this.style);
|
|
2312
2435
|
this.panel = doc.createElement("section");
|
|
2313
2436
|
this.panel.className = "asiyst-panel";
|
|
2314
2437
|
this.panel.hidden = true;
|
|
@@ -2342,11 +2465,12 @@ var ConversationPanel = class {
|
|
|
2342
2465
|
});
|
|
2343
2466
|
this.panel.append(header, this.log, this.form);
|
|
2344
2467
|
root.appendChild(this.panel);
|
|
2345
|
-
|
|
2468
|
+
this.keydownListener = (event) => {
|
|
2346
2469
|
if (event.key === "Escape" && this.openState) {
|
|
2347
2470
|
this.close();
|
|
2348
2471
|
}
|
|
2349
|
-
}
|
|
2472
|
+
};
|
|
2473
|
+
doc.addEventListener("keydown", this.keydownListener);
|
|
2350
2474
|
}
|
|
2351
2475
|
isOpen() {
|
|
2352
2476
|
return this.openState;
|
|
@@ -2385,6 +2509,14 @@ var ConversationPanel = class {
|
|
|
2385
2509
|
this.append("user", text);
|
|
2386
2510
|
await this.onSubmit(text);
|
|
2387
2511
|
}
|
|
2512
|
+
destroy() {
|
|
2513
|
+
if (this.keydownListener) {
|
|
2514
|
+
this.panel.ownerDocument.removeEventListener("keydown", this.keydownListener);
|
|
2515
|
+
this.keydownListener = void 0;
|
|
2516
|
+
}
|
|
2517
|
+
this.panel.remove();
|
|
2518
|
+
this.style.remove();
|
|
2519
|
+
}
|
|
2388
2520
|
};
|
|
2389
2521
|
|
|
2390
2522
|
// src/core/isolate.ts
|
|
@@ -2643,6 +2775,7 @@ var AsiystRuntime = class {
|
|
|
2643
2775
|
}
|
|
2644
2776
|
this.options = options;
|
|
2645
2777
|
this.installationId = loadOrCreateInstallationId(options.projectId);
|
|
2778
|
+
this.visitorSessionId = options.visitorSessionId || `vsess_${Math.random().toString(36).slice(2, 10)}_${Date.now().toString(36)}`;
|
|
2646
2779
|
const isLocalOrigin = /^(localhost|127(?:\.\d{1,3}){3}|\[::1\])$/i.test(win.location.hostname);
|
|
2647
2780
|
this.environment = options.apiBaseUrl || isLocalOrigin ? "development" : "production";
|
|
2648
2781
|
const transport = new HttpTransport({
|
|
@@ -2659,7 +2792,7 @@ var AsiystRuntime = class {
|
|
|
2659
2792
|
origin: win.location.origin,
|
|
2660
2793
|
environment: this.environment
|
|
2661
2794
|
}) : void 0;
|
|
2662
|
-
this.cloud = new CloudClient(transport, options.projectId, options.publicKey, runtimeAuth);
|
|
2795
|
+
this.cloud = new CloudClient(transport, options.projectId, options.publicKey, runtimeAuth, this.visitorSessionId);
|
|
2663
2796
|
this.config = new ConfigManager(options, this.cloud, this.events);
|
|
2664
2797
|
this.analytics = new Analytics(options.projectId, this.cloud);
|
|
2665
2798
|
this.host = new HostRoot(doc);
|
|
@@ -2689,12 +2822,14 @@ var AsiystRuntime = class {
|
|
|
2689
2822
|
this.host.chrome,
|
|
2690
2823
|
(text) => this.handleUserMessage(text, doc),
|
|
2691
2824
|
(open) => {
|
|
2825
|
+
const convId = this.panel.getConversationId();
|
|
2826
|
+
this.cloud.setConversationId(convId);
|
|
2692
2827
|
if (open) {
|
|
2693
2828
|
this.analytics.track("assistant_opened");
|
|
2694
|
-
this.events.emit("asiyst:conversation:started", { conversationId:
|
|
2829
|
+
this.events.emit("asiyst:conversation:started", { conversationId: convId });
|
|
2695
2830
|
} else {
|
|
2696
2831
|
this.analytics.track("assistant_closed");
|
|
2697
|
-
this.events.emit("asiyst:conversation:closed", { conversationId:
|
|
2832
|
+
this.events.emit("asiyst:conversation:closed", { conversationId: convId });
|
|
2698
2833
|
}
|
|
2699
2834
|
}
|
|
2700
2835
|
);
|
|
@@ -2706,10 +2841,15 @@ var AsiystRuntime = class {
|
|
|
2706
2841
|
this.analytics.start();
|
|
2707
2842
|
this.rescan();
|
|
2708
2843
|
this.avatar.show();
|
|
2709
|
-
this.renderer.getFigure()
|
|
2710
|
-
|
|
2844
|
+
const figure = this.renderer.getFigure();
|
|
2845
|
+
if (figure) {
|
|
2846
|
+
this.figureClickListener = () => this.open();
|
|
2847
|
+
figure.addEventListener("click", this.figureClickListener);
|
|
2848
|
+
}
|
|
2849
|
+
this.pagehideListener = () => {
|
|
2711
2850
|
void this.analytics.flush();
|
|
2712
|
-
}
|
|
2851
|
+
};
|
|
2852
|
+
win.addEventListener("pagehide", this.pagehideListener);
|
|
2713
2853
|
}
|
|
2714
2854
|
async start() {
|
|
2715
2855
|
this.events.emit("asiyst:initialized", { projectId: this.options.projectId });
|
|
@@ -2862,6 +3002,14 @@ var AsiystRuntime = class {
|
|
|
2862
3002
|
return;
|
|
2863
3003
|
}
|
|
2864
3004
|
this.destroyed = true;
|
|
3005
|
+
if (this.pagehideListener) {
|
|
3006
|
+
win.removeEventListener("pagehide", this.pagehideListener);
|
|
3007
|
+
}
|
|
3008
|
+
const figure = this.renderer.getFigure();
|
|
3009
|
+
if (figure && this.figureClickListener) {
|
|
3010
|
+
figure.removeEventListener("click", this.figureClickListener);
|
|
3011
|
+
}
|
|
3012
|
+
this.panel.destroy();
|
|
2865
3013
|
this.observer.stop();
|
|
2866
3014
|
this.history.stop(win);
|
|
2867
3015
|
this.movement.stop();
|