@asiyst/sdk 0.1.12 → 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/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.11" ;
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";
@@ -494,12 +494,14 @@ var CloudClient = class {
494
494
  }
495
495
  }, "assistant:heartbeat");
496
496
  const body = response.data && typeof response.data === "object" && !Array.isArray(response.data) ? response.data : void 0;
497
- const rawStatus = body?.status ?? body?.connectionStatus ?? body?.verificationStatus;
498
- 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";
499
500
  return {
500
- ok: response.ok && (verificationStatus === void 0 || verificationStatus === "connected"),
501
+ ok: response.ok && (verificationStatus === void 0 || verificationStatus === "connected" || verificationStatus === "verified"),
501
502
  status: response.status,
502
- verificationStatus
503
+ verificationStatus,
504
+ versionOutdated
503
505
  };
504
506
  }
505
507
  async registerInstallation(registration) {
@@ -2525,12 +2527,110 @@ var RuntimeAuth = class {
2525
2527
  }
2526
2528
  };
2527
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
+
2528
2627
  // src/core/Runtime.ts
2529
2628
  var AsiystRuntime = class {
2530
2629
  constructor(options, doc, win) {
2531
2630
  this.events = new EventBus();
2532
2631
  this.map = new WebsiteMap();
2533
2632
  this.observer = new DomObserver(() => this.rescan());
2633
+ this.lifecycle = createSdkLifecycleEmitter();
2534
2634
  this.destroyed = false;
2535
2635
  this.connectionStatus = "disconnected";
2536
2636
  this.verificationStatus = "not_detected";
@@ -2623,7 +2723,15 @@ var AsiystRuntime = class {
2623
2723
  }
2624
2724
  this.avatar.applyConfig(cfg);
2625
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
+ });
2626
2733
  await this.registerInstallation();
2734
+ const previousConnection = this.connectionStatus === "connected";
2627
2735
  try {
2628
2736
  const heartbeat = await this.cloud.heartbeat({
2629
2737
  origin: window.location.origin,
@@ -2631,10 +2739,73 @@ var AsiystRuntime = class {
2631
2739
  timestamp: (/* @__PURE__ */ new Date()).toISOString()
2632
2740
  });
2633
2741
  this.connectionStatus = heartbeat.ok ? "connected" : "disconnected";
2634
- this.verificationStatus = heartbeat.verificationStatus ?? (heartbeat.ok ? "connected" : "not_detected");
2635
- } catch {
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) {
2636
2790
  this.connectionStatus = "offline";
2637
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
+ }
2638
2809
  }
2639
2810
  this.events.emit("asiyst:ready", { projectId: this.options.projectId, configVersion: cfg.version });
2640
2811
  }
@@ -2709,6 +2880,7 @@ var AsiystRuntime = class {
2709
2880
  }
2710
2881
  return;
2711
2882
  }
2883
+ let lastError;
2712
2884
  for (let attempt = 0; attempt < 2; attempt += 1) {
2713
2885
  try {
2714
2886
  await this.cloud.registerInstallation({
@@ -2721,15 +2893,32 @@ var AsiystRuntime = class {
2721
2893
  });
2722
2894
  return;
2723
2895
  } catch (error) {
2896
+ lastError = error;
2724
2897
  if (attempt === 1) {
2725
2898
  if (typeof process !== "undefined" && process.env?.NODE_ENV !== "production") {
2726
2899
  console.debug("[asiyst] SDK installation registration failed.", error);
2727
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
+ });
2728
2909
  return;
2729
2910
  }
2730
2911
  await new Promise((resolve) => setTimeout(resolve, 250));
2731
2912
  }
2732
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
+ });
2733
2922
  }
2734
2923
  rescan() {
2735
2924
  withIsolation(() => {