@prodact.ai/sdk 0.0.7 → 0.0.8

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
@@ -97,7 +97,8 @@ var DEFAULT_CAPTURE_CONFIG = {
97
97
  };
98
98
  function mergeConfig(userConfig) {
99
99
  return {
100
- enabled: userConfig?.enabled ?? false,
100
+ enabled: userConfig?.enabled ?? true,
101
+ // Enabled by default!
101
102
  samplingRate: userConfig?.samplingRate ?? 1,
102
103
  privacy: { ...DEFAULT_PRIVACY_CONFIG, ...userConfig?.privacy },
103
104
  performance: { ...DEFAULT_PERFORMANCE_CONFIG, ...userConfig?.performance },
@@ -1150,7 +1151,8 @@ async function captureElementScreenshot(element, maxSize = 200) {
1150
1151
  }
1151
1152
  function mergeConfig2(config) {
1152
1153
  const defaults = {
1153
- enabled: false,
1154
+ enabled: true,
1155
+ // Enabled by default!
1154
1156
  samplingRate: 1,
1155
1157
  events: {
1156
1158
  clicks: true,
@@ -1621,16 +1623,25 @@ var UserFlowTracker = class {
1621
1623
  // Link to rrweb recording session if available
1622
1624
  linkedRecordingSessionId: this.linkedRecordingSessionId
1623
1625
  };
1624
- const response = await fetch(endpoint, {
1625
- method: "POST",
1626
- headers: {
1627
- "Content-Type": "application/json",
1628
- ...this.sdkKey ? { "X-SDK-Key": this.sdkKey } : {}
1629
- },
1630
- body: JSON.stringify(body)
1631
- });
1632
- if (!response.ok) {
1633
- throw new Error(`Failed to start session: ${response.status}`);
1626
+ try {
1627
+ const response = await fetch(endpoint, {
1628
+ method: "POST",
1629
+ headers: {
1630
+ "Content-Type": "application/json",
1631
+ ...this.sdkKey ? { "X-SDK-Key": this.sdkKey } : {}
1632
+ },
1633
+ body: JSON.stringify(body)
1634
+ });
1635
+ if (!response.ok) {
1636
+ console.error("[UserFlow] Failed to start session:", response.status, response.statusText);
1637
+ console.error("[UserFlow] Endpoint:", endpoint);
1638
+ throw new Error(`Failed to start session: ${response.status}`);
1639
+ }
1640
+ console.log("[UserFlow] Session started successfully:", this.sessionId);
1641
+ } catch (error) {
1642
+ console.error("[UserFlow] Network error starting session:", error);
1643
+ console.error("[UserFlow] Attempted endpoint:", endpoint);
1644
+ throw error;
1634
1645
  }
1635
1646
  }
1636
1647
  /**
@@ -1659,9 +1670,15 @@ var UserFlowTracker = class {
1659
1670
  });
1660
1671
  if (response.ok) {
1661
1672
  this.batchesSent++;
1673
+ console.log(`[UserFlow] Event batch ${this.batchIndex - 1} sent (${eventsToSend.length} events)`);
1674
+ } else {
1675
+ console.error("[UserFlow] Failed to send event batch:", response.status, response.statusText);
1676
+ console.error("[UserFlow] Endpoint:", endpoint);
1677
+ this.events = [...eventsToSend, ...this.events];
1662
1678
  }
1663
1679
  } catch (error) {
1664
- console.error("[UserFlow] Failed to send event batch:", error);
1680
+ console.error("[UserFlow] Network error sending event batch:", error);
1681
+ console.error("[UserFlow] Endpoint:", endpoint);
1665
1682
  this.events = [...eventsToSend, ...this.events];
1666
1683
  }
1667
1684
  }
@@ -1782,9 +1799,32 @@ var ProduckSDK = class {
1782
1799
  /** User flow tracker instance (optional, isolated module) */
1783
1800
  this.userFlowTracker = null;
1784
1801
  let apiUrl = config.apiUrl;
1802
+ let apiUrlAutoDetected = false;
1785
1803
  if (!apiUrl) {
1804
+ apiUrlAutoDetected = true;
1786
1805
  if (typeof window !== "undefined") {
1787
- apiUrl = window.location.hostname === "localhost" || window.location.hostname === "127.0.0.1" ? "http://localhost:4001/api/v1" : `${window.location.protocol}//${window.location.host}/api/v1`;
1806
+ const isLocalhost = window.location.hostname === "localhost" || window.location.hostname === "127.0.0.1";
1807
+ if (isLocalhost) {
1808
+ apiUrl = "http://localhost:4001/api/v1";
1809
+ } else {
1810
+ apiUrl = `${window.location.protocol}//${window.location.host}/api/v1`;
1811
+ console.warn(
1812
+ "%c\u26A0\uFE0F Produck SDK Warning: apiUrl not configured!",
1813
+ "background: #fbbf24; color: #000; padding: 4px 8px; font-weight: bold;"
1814
+ );
1815
+ console.warn(
1816
+ `The SDK is auto-detecting apiUrl as: ${apiUrl}
1817
+ This is likely WRONG for production. Your backend is probably at a different URL.
1818
+
1819
+ To fix this, provide apiUrl in your SDK config:
1820
+
1821
+ createProduck({
1822
+ sdkKey: 'your_sdk_key',
1823
+ apiUrl: 'https://your-backend.com/api/v1', // <-- Add this!
1824
+ })
1825
+ `
1826
+ );
1827
+ }
1788
1828
  } else {
1789
1829
  apiUrl = "http://localhost:4001/api/v1";
1790
1830
  }
@@ -1793,11 +1833,30 @@ var ProduckSDK = class {
1793
1833
  apiUrl,
1794
1834
  ...config
1795
1835
  };
1836
+ if (typeof window !== "undefined") {
1837
+ console.log(
1838
+ "%c\u{1F527} Produck SDK Config",
1839
+ "background: #3b82f6; color: #fff; padding: 2px 6px; border-radius: 3px;",
1840
+ {
1841
+ apiUrl: this.config.apiUrl,
1842
+ apiUrlAutoDetected,
1843
+ sdkKey: this.config.sdkKey ? `${this.config.sdkKey.substring(0, 10)}...` : "not set",
1844
+ guiderId: this.config.guiderId || "not set"
1845
+ }
1846
+ );
1847
+ }
1796
1848
  }
1797
1849
  /**
1798
1850
  * Initialize the SDK and create a chat session
1799
1851
  */
1800
1852
  async init() {
1853
+ if (typeof window !== "undefined") {
1854
+ console.log(
1855
+ "%c\u{1F680} Produck SDK Initializing...",
1856
+ "background: #10b981; color: #fff; padding: 2px 6px; border-radius: 3px;",
1857
+ { endpoint: this.config.apiUrl }
1858
+ );
1859
+ }
1801
1860
  await this.log("info", "SDK Initializing", {
1802
1861
  apiUrl: this.config.apiUrl,
1803
1862
  hasSDKKey: !!this.config.sdkKey,
@@ -1824,20 +1883,43 @@ var ProduckSDK = class {
1824
1883
  headers
1825
1884
  });
1826
1885
  if (!response.ok) {
1827
- await this.log("error", "Failed to create session", { status: response.status });
1886
+ let errorDetail = "";
1887
+ try {
1888
+ errorDetail = await response.text();
1889
+ } catch {
1890
+ errorDetail = "Could not read response body";
1891
+ }
1892
+ console.error(
1893
+ "%c\u274C Produck SDK: Failed to create session",
1894
+ "background: #ef4444; color: #fff; padding: 2px 6px; border-radius: 3px;"
1895
+ );
1896
+ console.error(`Status: ${response.status} ${response.statusText}`);
1897
+ console.error(`Endpoint: ${endpoint}`);
1898
+ console.error(`Response: ${errorDetail}`);
1899
+ if (response.status === 404) {
1900
+ console.error(
1901
+ `
1902
+ \u{1F4A1} Tip: A 404 error usually means the apiUrl is wrong.
1903
+ Current apiUrl: ${this.config.apiUrl}
1904
+ Make sure your backend is running and the URL is correct.`
1905
+ );
1906
+ }
1907
+ await this.log("error", "Failed to create session", { status: response.status, errorDetail });
1828
1908
  throw new Error(`Failed to create session: ${response.status}`);
1829
1909
  }
1830
1910
  const session = await response.json();
1831
1911
  this.sessionToken = session.session_token;
1832
1912
  this.isReady = true;
1833
1913
  await this.log("info", "SDK initialized successfully", { hasSessionToken: !!this.sessionToken });
1834
- if (this.config.recording?.enabled) {
1914
+ const recordingEnabled = this.config.recording?.enabled !== false;
1915
+ if (recordingEnabled) {
1835
1916
  await this.initRecording();
1836
1917
  }
1837
1918
  if (this.config.proactive?.enabled) {
1838
1919
  this.initProactive();
1839
1920
  }
1840
- if (this.config.userFlows?.enabled) {
1921
+ const userFlowsEnabled = this.config.userFlows?.enabled !== false;
1922
+ if (userFlowsEnabled) {
1841
1923
  await this.initUserFlowTracking();
1842
1924
  }
1843
1925
  this.emit("ready", { sessionToken: this.sessionToken });
@@ -1845,7 +1927,27 @@ var ProduckSDK = class {
1845
1927
  await this.sendInitialMessage();
1846
1928
  }
1847
1929
  } catch (error) {
1848
- await this.log("error", "SDK initialization failed", { error: error instanceof Error ? error.message : String(error) });
1930
+ const errorMessage = error instanceof Error ? error.message : String(error);
1931
+ const isNetworkError = errorMessage.includes("fetch") || errorMessage.includes("network") || errorMessage.includes("Failed to fetch") || errorMessage.includes("NetworkError");
1932
+ console.error(
1933
+ "%c\u274C Produck SDK initialization failed",
1934
+ "background: #ef4444; color: #fff; padding: 4px 8px; font-weight: bold;"
1935
+ );
1936
+ console.error(`Error: ${errorMessage}`);
1937
+ console.error(`API URL: ${this.config.apiUrl}`);
1938
+ if (isNetworkError) {
1939
+ console.error(
1940
+ `
1941
+ \u{1F4A1} This looks like a network error. Common causes:
1942
+ 1. The apiUrl is incorrect or the backend is not running
1943
+ 2. CORS is blocking the request
1944
+ 3. The backend server is unreachable
1945
+
1946
+ Current apiUrl: ${this.config.apiUrl}
1947
+ Check the Network tab in DevTools for more details.`
1948
+ );
1949
+ }
1950
+ await this.log("error", "SDK initialization failed", { error: errorMessage });
1849
1951
  this.emit("error", error);
1850
1952
  throw error;
1851
1953
  }