@asiyst/sdk 0.1.12 → 0.1.14

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.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.11" ;
72
+ var SDK_VERSION = "0.1.13" ;
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";
@@ -492,12 +492,14 @@ var CloudClient = class {
492
492
  }
493
493
  }, "assistant:heartbeat");
494
494
  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;
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";
497
498
  return {
498
- ok: response.ok && (verificationStatus === void 0 || verificationStatus === "connected"),
499
+ ok: response.ok && (verificationStatus === void 0 || verificationStatus === "connected" || verificationStatus === "verified"),
499
500
  status: response.status,
500
- verificationStatus
501
+ verificationStatus,
502
+ versionOutdated
501
503
  };
502
504
  }
503
505
  async registerInstallation(registration) {
@@ -2523,12 +2525,110 @@ var RuntimeAuth = class {
2523
2525
  }
2524
2526
  };
2525
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
+
2526
2625
  // src/core/Runtime.ts
2527
2626
  var AsiystRuntime = class {
2528
2627
  constructor(options, doc, win) {
2529
2628
  this.events = new EventBus();
2530
2629
  this.map = new WebsiteMap();
2531
2630
  this.observer = new DomObserver(() => this.rescan());
2631
+ this.lifecycle = createSdkLifecycleEmitter();
2532
2632
  this.destroyed = false;
2533
2633
  this.connectionStatus = "disconnected";
2534
2634
  this.verificationStatus = "not_detected";
@@ -2621,7 +2721,15 @@ var AsiystRuntime = class {
2621
2721
  }
2622
2722
  this.avatar.applyConfig(cfg);
2623
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
+ });
2624
2731
  await this.registerInstallation();
2732
+ const previousConnection = this.connectionStatus === "connected";
2625
2733
  try {
2626
2734
  const heartbeat = await this.cloud.heartbeat({
2627
2735
  origin: window.location.origin,
@@ -2629,10 +2737,73 @@ var AsiystRuntime = class {
2629
2737
  timestamp: (/* @__PURE__ */ new Date()).toISOString()
2630
2738
  });
2631
2739
  this.connectionStatus = heartbeat.ok ? "connected" : "disconnected";
2632
- this.verificationStatus = heartbeat.verificationStatus ?? (heartbeat.ok ? "connected" : "not_detected");
2633
- } catch {
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) {
2634
2788
  this.connectionStatus = "offline";
2635
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
+ }
2636
2807
  }
2637
2808
  this.events.emit("asiyst:ready", { projectId: this.options.projectId, configVersion: cfg.version });
2638
2809
  }
@@ -2707,6 +2878,7 @@ var AsiystRuntime = class {
2707
2878
  }
2708
2879
  return;
2709
2880
  }
2881
+ let lastError;
2710
2882
  for (let attempt = 0; attempt < 2; attempt += 1) {
2711
2883
  try {
2712
2884
  await this.cloud.registerInstallation({
@@ -2719,15 +2891,32 @@ var AsiystRuntime = class {
2719
2891
  });
2720
2892
  return;
2721
2893
  } catch (error) {
2894
+ lastError = error;
2722
2895
  if (attempt === 1) {
2723
2896
  if (typeof process !== "undefined" && process.env?.NODE_ENV !== "production") {
2724
2897
  console.debug("[asiyst] SDK installation registration failed.", error);
2725
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
+ });
2726
2907
  return;
2727
2908
  }
2728
2909
  await new Promise((resolve) => setTimeout(resolve, 250));
2729
2910
  }
2730
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
+ });
2731
2920
  }
2732
2921
  rescan() {
2733
2922
  withIsolation(() => {