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