@asiyst/sdk 0.1.11 → 0.1.13
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 +1 -0
- package/dist/index.cjs +416 -22
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +416 -22
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
package/dist/index.cjs
CHANGED
|
@@ -71,7 +71,7 @@ var ALL_ACTION_KINDS = [
|
|
|
71
71
|
];
|
|
72
72
|
|
|
73
73
|
// src/core/constants.ts
|
|
74
|
-
var SDK_VERSION = "0.1.
|
|
74
|
+
var SDK_VERSION = "0.1.12" ;
|
|
75
75
|
var DEFAULT_API_BASE_URL = "https://nqhxpgsjofzqudyqkqib.supabase.co/functions/v1/api";
|
|
76
76
|
var CONFIG_SCHEMA_VERSION = 1;
|
|
77
77
|
var HOST_ELEMENT_ID = "asiyst-host";
|
|
@@ -434,7 +434,8 @@ var HttpTransport = class {
|
|
|
434
434
|
"X-Asiyst-Project-Id": this.options.projectId,
|
|
435
435
|
"X-Asiyst-Public-Key": this.options.publicKey,
|
|
436
436
|
...this.options.avatarId ? { "X-Asiyst-Avatar-Id": this.options.avatarId } : {},
|
|
437
|
-
"X-Asiyst-SDK-Version": SDK_VERSION
|
|
437
|
+
"X-Asiyst-SDK-Version": SDK_VERSION,
|
|
438
|
+
...req.authorizationToken ? { Authorization: `Bearer ${req.authorizationToken}` } : {}
|
|
438
439
|
},
|
|
439
440
|
body: req.body === void 0 ? void 0 : JSON.stringify(req.body),
|
|
440
441
|
signal: req.signal
|
|
@@ -460,13 +461,14 @@ var HttpTransport = class {
|
|
|
460
461
|
|
|
461
462
|
// src/communication/CloudClient.ts
|
|
462
463
|
var CloudClient = class {
|
|
463
|
-
constructor(transport, projectId, publicKey = "") {
|
|
464
|
+
constructor(transport, projectId, publicKey = "", runtimeAuth) {
|
|
464
465
|
this.transport = transport;
|
|
465
466
|
this.projectId = projectId;
|
|
466
467
|
this.publicKey = publicKey;
|
|
468
|
+
this.runtimeAuth = runtimeAuth;
|
|
467
469
|
}
|
|
468
470
|
async fetchConfig() {
|
|
469
|
-
const response = await this.
|
|
471
|
+
const response = await this.runtimeRequest({
|
|
470
472
|
path: `/projects/${encodeURIComponent(this.projectId)}/avatar`,
|
|
471
473
|
method: "GET"
|
|
472
474
|
});
|
|
@@ -479,7 +481,7 @@ var CloudClient = class {
|
|
|
479
481
|
return normalizeProjectConfig(response.data.config ?? response.data);
|
|
480
482
|
}
|
|
481
483
|
async heartbeat(metadata) {
|
|
482
|
-
const response = await this.
|
|
484
|
+
const response = await this.runtimeRequest({
|
|
483
485
|
path: "/sdk/heartbeat",
|
|
484
486
|
method: "POST",
|
|
485
487
|
body: {
|
|
@@ -490,18 +492,31 @@ var CloudClient = class {
|
|
|
490
492
|
environment: metadata.environment,
|
|
491
493
|
timestamp: metadata.timestamp
|
|
492
494
|
}
|
|
493
|
-
});
|
|
495
|
+
}, "assistant:heartbeat");
|
|
494
496
|
const body = response.data && typeof response.data === "object" && !Array.isArray(response.data) ? response.data : void 0;
|
|
495
|
-
const rawStatus = body?.status ?? body?.connectionStatus ?? body?.verificationStatus;
|
|
496
|
-
const verificationStatus = rawStatus === "connected" || rawStatus === "inactive" || rawStatus === "not_detected" || rawStatus === "error" ? rawStatus : void 0;
|
|
497
|
+
const rawStatus = body?.status ?? body?.connectionStatus ?? body?.verificationStatus ?? (body?.verified === true ? "verified" : void 0);
|
|
498
|
+
const verificationStatus = rawStatus === "connected" || rawStatus === "verified" || rawStatus === "inactive" || rawStatus === "not_detected" || rawStatus === "error" ? rawStatus : void 0;
|
|
499
|
+
const versionOutdated = body?.versionOutdated === true || body?.sdkVersionOutdated === true || rawStatus === "outdated";
|
|
497
500
|
return {
|
|
498
|
-
ok: response.ok && (verificationStatus === void 0 || verificationStatus === "connected"),
|
|
501
|
+
ok: response.ok && (verificationStatus === void 0 || verificationStatus === "connected" || verificationStatus === "verified"),
|
|
499
502
|
status: response.status,
|
|
500
|
-
verificationStatus
|
|
503
|
+
verificationStatus,
|
|
504
|
+
versionOutdated
|
|
501
505
|
};
|
|
502
506
|
}
|
|
503
|
-
async
|
|
507
|
+
async registerInstallation(registration) {
|
|
504
508
|
const response = await this.transport.request({
|
|
509
|
+
path: "/sdk/installations/register",
|
|
510
|
+
method: "POST",
|
|
511
|
+
body: registration
|
|
512
|
+
});
|
|
513
|
+
if (!response.ok) {
|
|
514
|
+
throw new NetworkError(`SDK installation registration failed (${response.status}).`);
|
|
515
|
+
}
|
|
516
|
+
return response.data ?? {};
|
|
517
|
+
}
|
|
518
|
+
async requestTask(userText, pageUrl) {
|
|
519
|
+
const response = await this.runtimeRequest({
|
|
505
520
|
path: "/tasks",
|
|
506
521
|
method: "POST",
|
|
507
522
|
body: {
|
|
@@ -509,28 +524,28 @@ var CloudClient = class {
|
|
|
509
524
|
text: userText,
|
|
510
525
|
pageUrl
|
|
511
526
|
}
|
|
512
|
-
});
|
|
527
|
+
}, "assistant:conversation");
|
|
513
528
|
if (!response.ok || !response.data?.task?.id || !Array.isArray(response.data.task.steps)) {
|
|
514
529
|
throw new NetworkError("Task planning is unavailable");
|
|
515
530
|
}
|
|
516
531
|
return response.data.task;
|
|
517
532
|
}
|
|
518
533
|
async fetchWorkflow(workflowId) {
|
|
519
|
-
const response = await this.
|
|
534
|
+
const response = await this.runtimeRequest({
|
|
520
535
|
path: `/workflows/${encodeURIComponent(workflowId)}`,
|
|
521
536
|
method: "GET"
|
|
522
|
-
});
|
|
537
|
+
}, "assistant:conversation");
|
|
523
538
|
if (!response.ok || !response.data?.workflow?.id || !Array.isArray(response.data.workflow.steps)) {
|
|
524
539
|
throw new NetworkError("Workflow is unavailable");
|
|
525
540
|
}
|
|
526
541
|
return response.data.workflow;
|
|
527
542
|
}
|
|
528
543
|
async sendConversationMessage(text, pageUrl) {
|
|
529
|
-
const response = await this.
|
|
544
|
+
const response = await this.runtimeRequest({
|
|
530
545
|
path: "/conversations/messages",
|
|
531
546
|
method: "POST",
|
|
532
547
|
body: { projectId: this.projectId, text, pageUrl }
|
|
533
|
-
});
|
|
548
|
+
}, "assistant:conversation");
|
|
534
549
|
if (!response.ok || !response.data?.message?.text) {
|
|
535
550
|
throw new NetworkError("Conversation service is unavailable");
|
|
536
551
|
}
|
|
@@ -550,10 +565,23 @@ var CloudClient = class {
|
|
|
550
565
|
}
|
|
551
566
|
async safePost(path, body) {
|
|
552
567
|
try {
|
|
553
|
-
await this.
|
|
568
|
+
await this.runtimeRequest({ path, method: "POST", body }, "assistant:telemetry");
|
|
554
569
|
} catch {
|
|
555
570
|
}
|
|
556
571
|
}
|
|
572
|
+
async runtimeRequest(request, capability = "assistant:config") {
|
|
573
|
+
if (!this.runtimeAuth) {
|
|
574
|
+
return this.transport.request(request);
|
|
575
|
+
}
|
|
576
|
+
let token = await this.runtimeAuth.getToken(capability);
|
|
577
|
+
let response = await this.transport.request({ ...request, authorizationToken: token });
|
|
578
|
+
if (response.status === 401) {
|
|
579
|
+
this.runtimeAuth.invalidate();
|
|
580
|
+
token = await this.runtimeAuth.getToken(capability);
|
|
581
|
+
response = await this.transport.request({ ...request, authorizationToken: token });
|
|
582
|
+
}
|
|
583
|
+
return response;
|
|
584
|
+
}
|
|
557
585
|
};
|
|
558
586
|
|
|
559
587
|
// src/storage/NamespacedStorage.ts
|
|
@@ -1771,7 +1799,14 @@ var AvatarController = class {
|
|
|
1771
1799
|
|
|
1772
1800
|
// src/interaction/permissions.ts
|
|
1773
1801
|
function normalizeHost(host) {
|
|
1774
|
-
|
|
1802
|
+
const candidate = host.trim();
|
|
1803
|
+
try {
|
|
1804
|
+
const parsed = new URL(/^[a-z][a-z\d+\-.]*:\/\//i.test(candidate) ? candidate : `https://${candidate}`);
|
|
1805
|
+
return (parsed.hostname ?? "").replace(/^www\./i, "").toLowerCase();
|
|
1806
|
+
} catch {
|
|
1807
|
+
const fallbackHost = candidate.replace(/^https?:\/\//i, "").split("/", 1)[0] ?? "";
|
|
1808
|
+
return fallbackHost.replace(/^www\./i, "").replace(/:\d+$/, "").toLowerCase();
|
|
1809
|
+
}
|
|
1775
1810
|
}
|
|
1776
1811
|
function matchesRoutePattern(pathname, pattern) {
|
|
1777
1812
|
const candidate = pathname;
|
|
@@ -2364,12 +2399,238 @@ function withIsolation(fn) {
|
|
|
2364
2399
|
}
|
|
2365
2400
|
}
|
|
2366
2401
|
|
|
2402
|
+
// src/installation/Installation.ts
|
|
2403
|
+
var INSTALLATION_KEY = "installation_id";
|
|
2404
|
+
var INSTALLATION_ID_PATTERN = /^inst_[A-Za-z0-9][A-Za-z0-9_-]{7,127}$/;
|
|
2405
|
+
var memoryIds = /* @__PURE__ */ new Map();
|
|
2406
|
+
function randomInstallationId() {
|
|
2407
|
+
try {
|
|
2408
|
+
const cryptoApi = globalThis.crypto;
|
|
2409
|
+
if (typeof cryptoApi?.randomUUID === "function") {
|
|
2410
|
+
return `inst_${cryptoApi.randomUUID()}`;
|
|
2411
|
+
}
|
|
2412
|
+
if (typeof cryptoApi?.getRandomValues === "function") {
|
|
2413
|
+
const bytes = cryptoApi.getRandomValues(new Uint8Array(16));
|
|
2414
|
+
return `inst_${Array.from(bytes, (value) => value.toString(16).padStart(2, "0")).join("")}`;
|
|
2415
|
+
}
|
|
2416
|
+
} catch {
|
|
2417
|
+
return void 0;
|
|
2418
|
+
}
|
|
2419
|
+
return void 0;
|
|
2420
|
+
}
|
|
2421
|
+
function loadOrCreateInstallationId(projectId) {
|
|
2422
|
+
const inMemory = memoryIds.get(projectId);
|
|
2423
|
+
if (inMemory) return inMemory;
|
|
2424
|
+
const storage = new NamespacedStorage(projectId, browserLocalStorage());
|
|
2425
|
+
const existing = storage.read(INSTALLATION_KEY)?.trim();
|
|
2426
|
+
if (existing && INSTALLATION_ID_PATTERN.test(existing)) {
|
|
2427
|
+
memoryIds.set(projectId, existing);
|
|
2428
|
+
return existing;
|
|
2429
|
+
}
|
|
2430
|
+
const installationId = randomInstallationId();
|
|
2431
|
+
if (!installationId) return void 0;
|
|
2432
|
+
memoryIds.set(projectId, installationId);
|
|
2433
|
+
storage.write(INSTALLATION_KEY, installationId);
|
|
2434
|
+
return installationId;
|
|
2435
|
+
}
|
|
2436
|
+
|
|
2437
|
+
// src/communication/RuntimeAuth.ts
|
|
2438
|
+
var RUNTIME_AUDIENCE = "asiyst-sdk-runtime";
|
|
2439
|
+
var RUNTIME_CAPABILITIES = [
|
|
2440
|
+
"assistant:config",
|
|
2441
|
+
"assistant:conversation",
|
|
2442
|
+
"assistant:telemetry",
|
|
2443
|
+
"assistant:heartbeat"
|
|
2444
|
+
];
|
|
2445
|
+
var RuntimeAuth = class {
|
|
2446
|
+
constructor(options) {
|
|
2447
|
+
this.options = options;
|
|
2448
|
+
}
|
|
2449
|
+
async getToken(capability) {
|
|
2450
|
+
if (this.token && this.isUsable(this.token) && this.token.capabilities.has(capability)) {
|
|
2451
|
+
return this.token.value;
|
|
2452
|
+
}
|
|
2453
|
+
if (!this.requestInFlight) {
|
|
2454
|
+
this.requestInFlight = this.issueToken().finally(() => {
|
|
2455
|
+
this.requestInFlight = void 0;
|
|
2456
|
+
});
|
|
2457
|
+
}
|
|
2458
|
+
const token = await this.requestInFlight;
|
|
2459
|
+
if (!token.capabilities.has(capability)) {
|
|
2460
|
+
throw new AuthenticationError("ASIYST runtime authorization does not allow this operation.");
|
|
2461
|
+
}
|
|
2462
|
+
return token.value;
|
|
2463
|
+
}
|
|
2464
|
+
invalidate() {
|
|
2465
|
+
this.token = void 0;
|
|
2466
|
+
}
|
|
2467
|
+
isUsable(token) {
|
|
2468
|
+
return token.expiresAt - Date.now() > 3e4;
|
|
2469
|
+
}
|
|
2470
|
+
async issueToken() {
|
|
2471
|
+
const controller = new AbortController();
|
|
2472
|
+
const timeout = setTimeout(() => controller.abort(), 1e4);
|
|
2473
|
+
let response;
|
|
2474
|
+
try {
|
|
2475
|
+
response = await fetch(`${this.options.apiBaseUrl.replace(/\/$/, "")}/sdk/runtime/token`, {
|
|
2476
|
+
method: "POST",
|
|
2477
|
+
headers: {
|
|
2478
|
+
Accept: "application/json",
|
|
2479
|
+
"Content-Type": "application/json"
|
|
2480
|
+
},
|
|
2481
|
+
body: JSON.stringify({
|
|
2482
|
+
projectId: this.options.projectId,
|
|
2483
|
+
publicKey: this.options.publicKey,
|
|
2484
|
+
installationId: this.options.installationId,
|
|
2485
|
+
origin: this.options.origin,
|
|
2486
|
+
environment: this.options.environment,
|
|
2487
|
+
sdkVersion: SDK_VERSION,
|
|
2488
|
+
capabilities: [...RUNTIME_CAPABILITIES]
|
|
2489
|
+
}),
|
|
2490
|
+
signal: controller.signal
|
|
2491
|
+
});
|
|
2492
|
+
} catch {
|
|
2493
|
+
throw new NetworkError("ASIYST runtime authentication is unavailable.");
|
|
2494
|
+
} finally {
|
|
2495
|
+
clearTimeout(timeout);
|
|
2496
|
+
}
|
|
2497
|
+
let body;
|
|
2498
|
+
try {
|
|
2499
|
+
body = await response.json();
|
|
2500
|
+
} catch {
|
|
2501
|
+
body = void 0;
|
|
2502
|
+
}
|
|
2503
|
+
if (!response.ok) {
|
|
2504
|
+
if (response.status === 403) {
|
|
2505
|
+
throw new AuthenticationError("ASIYST runtime authorization was rejected.");
|
|
2506
|
+
}
|
|
2507
|
+
if (response.status === 429) {
|
|
2508
|
+
throw new NetworkError("ASIYST runtime authentication is rate limited.");
|
|
2509
|
+
}
|
|
2510
|
+
throw new NetworkError("ASIYST runtime authentication is unavailable.");
|
|
2511
|
+
}
|
|
2512
|
+
const data = body?.data;
|
|
2513
|
+
if (!data || body?.success !== true || typeof data.token !== "string" || data.token.length < 20 || data.tokenType !== "Bearer" || data.audience !== RUNTIME_AUDIENCE || typeof data.expiresIn !== "number" || !Number.isFinite(data.expiresIn) || data.expiresIn <= 0 || typeof data.expiresAt !== "string" || !Array.isArray(data.capabilities) || data.capabilities.some((capability) => !RUNTIME_CAPABILITIES.includes(capability))) {
|
|
2514
|
+
throw new AuthenticationError("ASIYST returned an invalid runtime authorization response.");
|
|
2515
|
+
}
|
|
2516
|
+
const expiresAt = Date.parse(data.expiresAt);
|
|
2517
|
+
if (!Number.isFinite(expiresAt) || expiresAt <= Date.now()) {
|
|
2518
|
+
throw new AuthenticationError("ASIYST runtime authorization has expired.");
|
|
2519
|
+
}
|
|
2520
|
+
const token = {
|
|
2521
|
+
value: data.token,
|
|
2522
|
+
expiresAt,
|
|
2523
|
+
capabilities: new Set(data.capabilities)
|
|
2524
|
+
};
|
|
2525
|
+
this.token = token;
|
|
2526
|
+
return token;
|
|
2527
|
+
}
|
|
2528
|
+
};
|
|
2529
|
+
|
|
2530
|
+
// src/lifecycle.ts
|
|
2531
|
+
var WEB_LIFECYCLE_CONTRACT = {
|
|
2532
|
+
endpoint: "/api/v1/events",
|
|
2533
|
+
status: "unsupported",
|
|
2534
|
+
supportedAnalytics: [
|
|
2535
|
+
"visitor",
|
|
2536
|
+
"page_view",
|
|
2537
|
+
"avatar_impression",
|
|
2538
|
+
"avatar_opened",
|
|
2539
|
+
"conversation_started",
|
|
2540
|
+
"question_asked",
|
|
2541
|
+
"assistant_response",
|
|
2542
|
+
"guidance_started",
|
|
2543
|
+
"guidance_completed",
|
|
2544
|
+
"guidance_abandoned",
|
|
2545
|
+
"cta_interaction",
|
|
2546
|
+
"task_completed",
|
|
2547
|
+
"sdk_initialized",
|
|
2548
|
+
"performance",
|
|
2549
|
+
"website_connection"
|
|
2550
|
+
],
|
|
2551
|
+
supportedLifecycleNames: [],
|
|
2552
|
+
runtimeCapability: "assistant:telemetry",
|
|
2553
|
+
projectBindingRequired: true,
|
|
2554
|
+
verifiedInstallationRequired: true,
|
|
2555
|
+
browserOriginRequired: true,
|
|
2556
|
+
rateLimitRequired: true,
|
|
2557
|
+
note: "Lifecycle names prefixed with sdk. or cli. are not dispatched to the current web telemetry endpoint."
|
|
2558
|
+
};
|
|
2559
|
+
var UnsupportedLifecycleSink = class {
|
|
2560
|
+
constructor() {
|
|
2561
|
+
this.contract = {
|
|
2562
|
+
...WEB_LIFECYCLE_CONTRACT,
|
|
2563
|
+
supportedLifecycleNames: [],
|
|
2564
|
+
supported: false
|
|
2565
|
+
};
|
|
2566
|
+
}
|
|
2567
|
+
emit() {
|
|
2568
|
+
}
|
|
2569
|
+
};
|
|
2570
|
+
function sanitizePayload(value) {
|
|
2571
|
+
const next = {};
|
|
2572
|
+
for (const [key, entry] of Object.entries(value ?? {})) {
|
|
2573
|
+
if (typeof entry !== "string" && typeof entry !== "number" && typeof entry !== "boolean") continue;
|
|
2574
|
+
if (typeof entry === "string" && !entry.trim()) continue;
|
|
2575
|
+
const normalized = key.toLowerCase();
|
|
2576
|
+
if (/(token|secret|cookie|session|api.?key|refresh.?token|authorization|password|bearer)/i.test(normalized)) {
|
|
2577
|
+
continue;
|
|
2578
|
+
}
|
|
2579
|
+
next[key] = entry;
|
|
2580
|
+
}
|
|
2581
|
+
return next;
|
|
2582
|
+
}
|
|
2583
|
+
function sortablePayload(value) {
|
|
2584
|
+
const entries = Object.entries(value).filter((entry) => entry[1] !== void 0).sort(([left], [right]) => left.localeCompare(right));
|
|
2585
|
+
return Object.fromEntries(entries);
|
|
2586
|
+
}
|
|
2587
|
+
function dedupeKey(name, value) {
|
|
2588
|
+
return `${name}:${JSON.stringify(sortablePayload(value))}`;
|
|
2589
|
+
}
|
|
2590
|
+
var LifecycleEmitter = class {
|
|
2591
|
+
constructor(sink = new UnsupportedLifecycleSink()) {
|
|
2592
|
+
this.sink = sink;
|
|
2593
|
+
this.seen = /* @__PURE__ */ new Set();
|
|
2594
|
+
}
|
|
2595
|
+
emit(name, payload = {}, options = {}) {
|
|
2596
|
+
const sanitized = sanitizePayload(payload);
|
|
2597
|
+
const key = dedupeKey(name, sanitized);
|
|
2598
|
+
if (!options.force && this.seen.has(key)) {
|
|
2599
|
+
return void 0;
|
|
2600
|
+
}
|
|
2601
|
+
this.seen.add(key);
|
|
2602
|
+
const event = {
|
|
2603
|
+
name,
|
|
2604
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
2605
|
+
source: name.startsWith("cli.") ? "cli" : "sdk",
|
|
2606
|
+
payload: sanitized,
|
|
2607
|
+
contract: {
|
|
2608
|
+
...this.sink.contract,
|
|
2609
|
+
supportedLifecycleNames: [...this.sink.contract.supportedLifecycleNames],
|
|
2610
|
+
supported: false
|
|
2611
|
+
}
|
|
2612
|
+
};
|
|
2613
|
+
try {
|
|
2614
|
+
this.sink.emit(event);
|
|
2615
|
+
} catch {
|
|
2616
|
+
}
|
|
2617
|
+
return event;
|
|
2618
|
+
}
|
|
2619
|
+
clear() {
|
|
2620
|
+
this.seen.clear();
|
|
2621
|
+
}
|
|
2622
|
+
};
|
|
2623
|
+
function createSdkLifecycleEmitter(sink) {
|
|
2624
|
+
return new LifecycleEmitter(new UnsupportedLifecycleSink());
|
|
2625
|
+
}
|
|
2626
|
+
|
|
2367
2627
|
// src/core/Runtime.ts
|
|
2368
2628
|
var AsiystRuntime = class {
|
|
2369
2629
|
constructor(options, doc, win) {
|
|
2370
2630
|
this.events = new EventBus();
|
|
2371
2631
|
this.map = new WebsiteMap();
|
|
2372
2632
|
this.observer = new DomObserver(() => this.rescan());
|
|
2633
|
+
this.lifecycle = createSdkLifecycleEmitter();
|
|
2373
2634
|
this.destroyed = false;
|
|
2374
2635
|
this.connectionStatus = "disconnected";
|
|
2375
2636
|
this.verificationStatus = "not_detected";
|
|
@@ -2378,13 +2639,24 @@ var AsiystRuntime = class {
|
|
|
2378
2639
|
throw new InitializationError("document.body is not available");
|
|
2379
2640
|
}
|
|
2380
2641
|
this.options = options;
|
|
2642
|
+
this.installationId = loadOrCreateInstallationId(options.projectId);
|
|
2643
|
+
const isLocalOrigin = /^(localhost|127(?:\.\d{1,3}){3}|\[::1\])$/i.test(win.location.hostname);
|
|
2644
|
+
this.environment = options.apiBaseUrl || isLocalOrigin ? "development" : "production";
|
|
2381
2645
|
const transport = new HttpTransport({
|
|
2382
2646
|
apiBaseUrl: options.apiBaseUrl ?? DEFAULT_API_BASE_URL,
|
|
2383
2647
|
projectId: options.projectId,
|
|
2384
2648
|
publicKey: options.publicKey,
|
|
2385
2649
|
avatarId: options.avatarId
|
|
2386
2650
|
});
|
|
2387
|
-
this.
|
|
2651
|
+
const runtimeAuth = this.installationId ? new RuntimeAuth({
|
|
2652
|
+
apiBaseUrl: options.apiBaseUrl ?? DEFAULT_API_BASE_URL,
|
|
2653
|
+
projectId: options.projectId,
|
|
2654
|
+
publicKey: options.publicKey,
|
|
2655
|
+
installationId: this.installationId,
|
|
2656
|
+
origin: win.location.origin,
|
|
2657
|
+
environment: this.environment
|
|
2658
|
+
}) : void 0;
|
|
2659
|
+
this.cloud = new CloudClient(transport, options.projectId, options.publicKey, runtimeAuth);
|
|
2388
2660
|
this.config = new ConfigManager(options, this.cloud, this.events);
|
|
2389
2661
|
this.analytics = new Analytics(options.projectId, this.cloud);
|
|
2390
2662
|
this.host = new HostRoot(doc);
|
|
@@ -2451,17 +2723,89 @@ var AsiystRuntime = class {
|
|
|
2451
2723
|
}
|
|
2452
2724
|
this.avatar.applyConfig(cfg);
|
|
2453
2725
|
this.avatar.show();
|
|
2726
|
+
this.lifecycle.emit("sdk.verification_started", {
|
|
2727
|
+
projectId: this.options.projectId,
|
|
2728
|
+
origin: window.location.origin,
|
|
2729
|
+
environment: this.environment,
|
|
2730
|
+
installationId: this.installationId,
|
|
2731
|
+
sdkVersion: SDK_VERSION
|
|
2732
|
+
});
|
|
2733
|
+
await this.registerInstallation();
|
|
2734
|
+
const previousConnection = this.connectionStatus === "connected";
|
|
2454
2735
|
try {
|
|
2455
2736
|
const heartbeat = await this.cloud.heartbeat({
|
|
2456
2737
|
origin: window.location.origin,
|
|
2457
|
-
environment: this.
|
|
2738
|
+
environment: this.environment,
|
|
2458
2739
|
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
2459
2740
|
});
|
|
2460
2741
|
this.connectionStatus = heartbeat.ok ? "connected" : "disconnected";
|
|
2461
|
-
this.verificationStatus = heartbeat.verificationStatus ?? (heartbeat.ok ? "connected" : "not_detected");
|
|
2462
|
-
|
|
2742
|
+
this.verificationStatus = heartbeat.verificationStatus === "verified" ? "connected" : heartbeat.verificationStatus ?? (heartbeat.ok ? "connected" : "not_detected");
|
|
2743
|
+
if (heartbeat.ok) {
|
|
2744
|
+
this.lifecycle.emit("sdk.connected", {
|
|
2745
|
+
projectId: this.options.projectId,
|
|
2746
|
+
origin: window.location.origin,
|
|
2747
|
+
environment: this.environment,
|
|
2748
|
+
installationId: this.installationId,
|
|
2749
|
+
sdkVersion: SDK_VERSION
|
|
2750
|
+
});
|
|
2751
|
+
if (heartbeat.verificationStatus === "connected" || heartbeat.verificationStatus === "verified") {
|
|
2752
|
+
this.lifecycle.emit("sdk.verified", {
|
|
2753
|
+
projectId: this.options.projectId,
|
|
2754
|
+
origin: window.location.origin,
|
|
2755
|
+
environment: this.environment,
|
|
2756
|
+
installationId: this.installationId,
|
|
2757
|
+
sdkVersion: SDK_VERSION
|
|
2758
|
+
});
|
|
2759
|
+
}
|
|
2760
|
+
if (heartbeat.versionOutdated) {
|
|
2761
|
+
this.lifecycle.emit("sdk.version_outdated", {
|
|
2762
|
+
projectId: this.options.projectId,
|
|
2763
|
+
origin: window.location.origin,
|
|
2764
|
+
environment: this.environment,
|
|
2765
|
+
installationId: this.installationId,
|
|
2766
|
+
sdkVersion: SDK_VERSION
|
|
2767
|
+
});
|
|
2768
|
+
}
|
|
2769
|
+
} else {
|
|
2770
|
+
const reason = heartbeat.verificationStatus ?? "unverified";
|
|
2771
|
+
this.lifecycle.emit("sdk.verification_failed", {
|
|
2772
|
+
projectId: this.options.projectId,
|
|
2773
|
+
origin: window.location.origin,
|
|
2774
|
+
environment: this.environment,
|
|
2775
|
+
status: reason,
|
|
2776
|
+
reason: "heartbeat_failed"
|
|
2777
|
+
});
|
|
2778
|
+
if (previousConnection) {
|
|
2779
|
+
this.lifecycle.emit("sdk.disconnected", {
|
|
2780
|
+
projectId: this.options.projectId,
|
|
2781
|
+
origin: window.location.origin,
|
|
2782
|
+
environment: this.environment,
|
|
2783
|
+
installationId: this.installationId,
|
|
2784
|
+
reason: "heartbeat_failed",
|
|
2785
|
+
status: reason
|
|
2786
|
+
});
|
|
2787
|
+
}
|
|
2788
|
+
}
|
|
2789
|
+
} catch (error) {
|
|
2463
2790
|
this.connectionStatus = "offline";
|
|
2464
2791
|
this.verificationStatus = "error";
|
|
2792
|
+
this.lifecycle.emit("sdk.verification_failed", {
|
|
2793
|
+
projectId: this.options.projectId,
|
|
2794
|
+
origin: window.location.origin,
|
|
2795
|
+
environment: this.environment,
|
|
2796
|
+
installationId: this.installationId,
|
|
2797
|
+
reason: "heartbeat_failed",
|
|
2798
|
+
category: error instanceof AuthenticationError ? "authorization" : "network"
|
|
2799
|
+
});
|
|
2800
|
+
if (previousConnection) {
|
|
2801
|
+
this.lifecycle.emit("sdk.disconnected", {
|
|
2802
|
+
projectId: this.options.projectId,
|
|
2803
|
+
origin: window.location.origin,
|
|
2804
|
+
environment: this.environment,
|
|
2805
|
+
reason: "heartbeat_failed",
|
|
2806
|
+
category: "network"
|
|
2807
|
+
});
|
|
2808
|
+
}
|
|
2465
2809
|
}
|
|
2466
2810
|
this.events.emit("asiyst:ready", { projectId: this.options.projectId, configVersion: cfg.version });
|
|
2467
2811
|
}
|
|
@@ -2526,6 +2870,56 @@ var AsiystRuntime = class {
|
|
|
2526
2870
|
this.events.removeAll();
|
|
2527
2871
|
await this.analytics.destroy();
|
|
2528
2872
|
}
|
|
2873
|
+
async registerInstallation() {
|
|
2874
|
+
const origin = window.location.origin;
|
|
2875
|
+
const environment = this.environment;
|
|
2876
|
+
const installationId = this.installationId;
|
|
2877
|
+
if (!installationId) {
|
|
2878
|
+
if (typeof process !== "undefined" && process.env?.NODE_ENV !== "production") {
|
|
2879
|
+
console.debug("[asiyst] SDK installation identity is unavailable; registration skipped.");
|
|
2880
|
+
}
|
|
2881
|
+
return;
|
|
2882
|
+
}
|
|
2883
|
+
let lastError;
|
|
2884
|
+
for (let attempt = 0; attempt < 2; attempt += 1) {
|
|
2885
|
+
try {
|
|
2886
|
+
await this.cloud.registerInstallation({
|
|
2887
|
+
project_id: this.options.projectId,
|
|
2888
|
+
public_key: this.options.publicKey,
|
|
2889
|
+
installation_id: installationId,
|
|
2890
|
+
origin,
|
|
2891
|
+
environment,
|
|
2892
|
+
sdk_version: SDK_VERSION
|
|
2893
|
+
});
|
|
2894
|
+
return;
|
|
2895
|
+
} catch (error) {
|
|
2896
|
+
lastError = error;
|
|
2897
|
+
if (attempt === 1) {
|
|
2898
|
+
if (typeof process !== "undefined" && process.env?.NODE_ENV !== "production") {
|
|
2899
|
+
console.debug("[asiyst] SDK installation registration failed.", error);
|
|
2900
|
+
}
|
|
2901
|
+
this.lifecycle.emit("sdk.verification_failed", {
|
|
2902
|
+
projectId: this.options.projectId,
|
|
2903
|
+
origin,
|
|
2904
|
+
environment,
|
|
2905
|
+
installationId,
|
|
2906
|
+
reason: "registration_failed",
|
|
2907
|
+
category: error instanceof AuthenticationError ? "authorization" : "network"
|
|
2908
|
+
});
|
|
2909
|
+
return;
|
|
2910
|
+
}
|
|
2911
|
+
await new Promise((resolve) => setTimeout(resolve, 250));
|
|
2912
|
+
}
|
|
2913
|
+
}
|
|
2914
|
+
this.lifecycle.emit("sdk.verification_failed", {
|
|
2915
|
+
projectId: this.options.projectId,
|
|
2916
|
+
origin,
|
|
2917
|
+
environment,
|
|
2918
|
+
installationId,
|
|
2919
|
+
reason: "registration_failed",
|
|
2920
|
+
category: lastError instanceof AuthenticationError ? "authorization" : "network"
|
|
2921
|
+
});
|
|
2922
|
+
}
|
|
2529
2923
|
rescan() {
|
|
2530
2924
|
withIsolation(() => {
|
|
2531
2925
|
const doc = this.host.host.ownerDocument;
|