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