@zerocost/sdk 0.12.0 → 0.13.0

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
@@ -1274,6 +1274,607 @@ var RecordingModule = class {
1274
1274
  }
1275
1275
  };
1276
1276
 
1277
+ // src/core/consent-ui.ts
1278
+ var STYLE_ID = "zerocost-consent-styles";
1279
+ function injectStyles(theme) {
1280
+ if (document.getElementById(STYLE_ID)) return;
1281
+ const darkVars = `
1282
+ --zc-bg: #111111;
1283
+ --zc-surface: #1a1a1a;
1284
+ --zc-border: #2a2a2a;
1285
+ --zc-text: #ffffff;
1286
+ --zc-text-secondary: #999999;
1287
+ --zc-accent: #ffffff;
1288
+ --zc-accent-bg: #ffffff;
1289
+ --zc-accent-fg: #000000;
1290
+ --zc-toggle-off-bg: #333333;
1291
+ --zc-toggle-on-bg: #00e599;
1292
+ --zc-toggle-knob: #ffffff;
1293
+ --zc-backdrop: rgba(0,0,0,0.65);
1294
+ --zc-link: #888888;
1295
+ --zc-link-hover: #cccccc;
1296
+ `;
1297
+ const lightVars = `
1298
+ --zc-bg: #ffffff;
1299
+ --zc-surface: #f5f5f5;
1300
+ --zc-border: #e0e0e0;
1301
+ --zc-text: #111111;
1302
+ --zc-text-secondary: #666666;
1303
+ --zc-accent: #111111;
1304
+ --zc-accent-bg: #111111;
1305
+ --zc-accent-fg: #ffffff;
1306
+ --zc-toggle-off-bg: #cccccc;
1307
+ --zc-toggle-on-bg: #00c77d;
1308
+ --zc-toggle-knob: #ffffff;
1309
+ --zc-backdrop: rgba(0,0,0,0.45);
1310
+ --zc-link: #666666;
1311
+ --zc-link-hover: #111111;
1312
+ `;
1313
+ let themeRule;
1314
+ if (theme === "dark") {
1315
+ themeRule = `.zc-consent-root { ${darkVars} }`;
1316
+ } else if (theme === "light") {
1317
+ themeRule = `.zc-consent-root { ${lightVars} }`;
1318
+ } else {
1319
+ themeRule = `
1320
+ .zc-consent-root { ${lightVars} }
1321
+ @media (prefers-color-scheme: dark) {
1322
+ .zc-consent-root { ${darkVars} }
1323
+ }
1324
+ `;
1325
+ }
1326
+ const css = `
1327
+ ${themeRule}
1328
+
1329
+ .zc-consent-root * {
1330
+ box-sizing: border-box;
1331
+ margin: 0;
1332
+ padding: 0;
1333
+ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Inter', Roboto, sans-serif;
1334
+ }
1335
+
1336
+ .zc-consent-backdrop {
1337
+ position: fixed;
1338
+ inset: 0;
1339
+ z-index: 999999;
1340
+ background: var(--zc-backdrop);
1341
+ display: flex;
1342
+ align-items: center;
1343
+ justify-content: center;
1344
+ animation: zc-fade-in 200ms ease;
1345
+ }
1346
+
1347
+ @keyframes zc-fade-in {
1348
+ from { opacity: 0; }
1349
+ to { opacity: 1; }
1350
+ }
1351
+
1352
+ @keyframes zc-slide-up {
1353
+ from { transform: translateY(100%); }
1354
+ to { transform: translateY(0); }
1355
+ }
1356
+
1357
+ .zc-consent-card {
1358
+ background: var(--zc-bg);
1359
+ border: 1px solid var(--zc-border);
1360
+ border-radius: 16px;
1361
+ width: 100%;
1362
+ max-width: 460px;
1363
+ max-height: 90vh;
1364
+ overflow-y: auto;
1365
+ padding: 28px 24px 24px;
1366
+ animation: zc-fade-in 200ms ease;
1367
+ }
1368
+
1369
+ /* Mobile: bottom-sheet style */
1370
+ @media (max-width: 640px) {
1371
+ .zc-consent-backdrop {
1372
+ align-items: flex-end;
1373
+ }
1374
+ .zc-consent-card {
1375
+ border-radius: 20px 20px 0 0;
1376
+ max-width: 100%;
1377
+ animation: zc-slide-up 200ms ease;
1378
+ }
1379
+ }
1380
+
1381
+ /* Scrollbar */
1382
+ .zc-consent-card::-webkit-scrollbar { width: 4px; }
1383
+ .zc-consent-card::-webkit-scrollbar-thumb { background: var(--zc-border); border-radius: 4px; }
1384
+
1385
+ .zc-consent-header {
1386
+ display: flex;
1387
+ align-items: center;
1388
+ gap: 10px;
1389
+ margin-bottom: 6px;
1390
+ }
1391
+
1392
+ .zc-consent-logo {
1393
+ width: 28px;
1394
+ height: 28px;
1395
+ border-radius: 6px;
1396
+ background: var(--zc-accent-bg);
1397
+ display: flex;
1398
+ align-items: center;
1399
+ justify-content: center;
1400
+ flex-shrink: 0;
1401
+ }
1402
+
1403
+ .zc-consent-logo svg {
1404
+ width: 16px;
1405
+ height: 16px;
1406
+ }
1407
+
1408
+ .zc-consent-title {
1409
+ font-size: 16px;
1410
+ font-weight: 700;
1411
+ color: var(--zc-text);
1412
+ letter-spacing: -0.02em;
1413
+ }
1414
+
1415
+ .zc-consent-subtitle {
1416
+ font-size: 13px;
1417
+ color: var(--zc-text-secondary);
1418
+ line-height: 1.5;
1419
+ margin-bottom: 20px;
1420
+ }
1421
+
1422
+ .zc-consent-toggles {
1423
+ display: flex;
1424
+ flex-direction: column;
1425
+ gap: 10px;
1426
+ margin-bottom: 20px;
1427
+ }
1428
+
1429
+ .zc-consent-toggle-card {
1430
+ background: var(--zc-surface);
1431
+ border: 1px solid var(--zc-border);
1432
+ border-radius: 12px;
1433
+ padding: 14px 16px;
1434
+ }
1435
+
1436
+ .zc-consent-toggle-row {
1437
+ display: flex;
1438
+ align-items: center;
1439
+ justify-content: space-between;
1440
+ margin-bottom: 6px;
1441
+ }
1442
+
1443
+ .zc-consent-toggle-label {
1444
+ font-size: 14px;
1445
+ font-weight: 600;
1446
+ color: var(--zc-text);
1447
+ }
1448
+
1449
+ .zc-consent-toggle-desc {
1450
+ font-size: 12px;
1451
+ color: var(--zc-text-secondary);
1452
+ line-height: 1.5;
1453
+ margin-bottom: 4px;
1454
+ }
1455
+
1456
+ .zc-consent-learn-more {
1457
+ font-size: 11px;
1458
+ color: var(--zc-link);
1459
+ text-decoration: none;
1460
+ cursor: pointer;
1461
+ transition: color 150ms;
1462
+ }
1463
+
1464
+ .zc-consent-learn-more:hover {
1465
+ color: var(--zc-link-hover);
1466
+ }
1467
+
1468
+ /* Toggle switch */
1469
+ .zc-toggle {
1470
+ position: relative;
1471
+ width: 40px;
1472
+ height: 22px;
1473
+ flex-shrink: 0;
1474
+ cursor: pointer;
1475
+ }
1476
+
1477
+ .zc-toggle input {
1478
+ opacity: 0;
1479
+ width: 0;
1480
+ height: 0;
1481
+ position: absolute;
1482
+ }
1483
+
1484
+ .zc-toggle-track {
1485
+ position: absolute;
1486
+ inset: 0;
1487
+ background: var(--zc-toggle-off-bg);
1488
+ border-radius: 11px;
1489
+ transition: background 200ms ease;
1490
+ }
1491
+
1492
+ .zc-toggle input:checked + .zc-toggle-track {
1493
+ background: var(--zc-toggle-on-bg);
1494
+ }
1495
+
1496
+ .zc-toggle-knob {
1497
+ position: absolute;
1498
+ top: 2px;
1499
+ left: 2px;
1500
+ width: 18px;
1501
+ height: 18px;
1502
+ background: var(--zc-toggle-knob);
1503
+ border-radius: 50%;
1504
+ transition: transform 200ms ease;
1505
+ box-shadow: 0 1px 3px rgba(0,0,0,0.2);
1506
+ }
1507
+
1508
+ .zc-toggle input:checked ~ .zc-toggle-knob {
1509
+ transform: translateX(18px);
1510
+ }
1511
+
1512
+ /* Footer */
1513
+ .zc-consent-footer {
1514
+ display: flex;
1515
+ flex-wrap: wrap;
1516
+ gap: 4px 12px;
1517
+ justify-content: center;
1518
+ margin-bottom: 16px;
1519
+ }
1520
+
1521
+ .zc-consent-footer a {
1522
+ font-size: 11px;
1523
+ color: var(--zc-link);
1524
+ text-decoration: none;
1525
+ transition: color 150ms;
1526
+ }
1527
+
1528
+ .zc-consent-footer a:hover {
1529
+ color: var(--zc-link-hover);
1530
+ }
1531
+
1532
+ .zc-consent-footer-sep {
1533
+ font-size: 11px;
1534
+ color: var(--zc-link);
1535
+ opacity: 0.5;
1536
+ }
1537
+
1538
+ /* Confirm button */
1539
+ .zc-consent-confirm {
1540
+ display: block;
1541
+ width: 100%;
1542
+ padding: 12px;
1543
+ font-size: 14px;
1544
+ font-weight: 600;
1545
+ border: none;
1546
+ border-radius: 10px;
1547
+ cursor: pointer;
1548
+ background: var(--zc-accent-bg);
1549
+ color: var(--zc-accent-fg);
1550
+ letter-spacing: -0.01em;
1551
+ transition: opacity 150ms;
1552
+ }
1553
+
1554
+ .zc-consent-confirm:hover {
1555
+ opacity: 0.88;
1556
+ }
1557
+
1558
+ .zc-consent-confirm:active {
1559
+ opacity: 0.75;
1560
+ }
1561
+ `;
1562
+ const style = document.createElement("style");
1563
+ style.id = STYLE_ID;
1564
+ style.textContent = css;
1565
+ document.head.appendChild(style);
1566
+ }
1567
+ function createToggle(id, checked) {
1568
+ const label = document.createElement("label");
1569
+ label.className = "zc-toggle";
1570
+ const input = document.createElement("input");
1571
+ input.type = "checkbox";
1572
+ input.checked = checked;
1573
+ input.id = id;
1574
+ const track = document.createElement("span");
1575
+ track.className = "zc-toggle-track";
1576
+ const knob = document.createElement("span");
1577
+ knob.className = "zc-toggle-knob";
1578
+ label.appendChild(input);
1579
+ label.appendChild(track);
1580
+ label.appendChild(knob);
1581
+ return label;
1582
+ }
1583
+ function createToggleCard(toggleId, title, description, learnMoreUrl, defaultOn) {
1584
+ const card = document.createElement("div");
1585
+ card.className = "zc-consent-toggle-card";
1586
+ const row = document.createElement("div");
1587
+ row.className = "zc-consent-toggle-row";
1588
+ const labelSpan = document.createElement("span");
1589
+ labelSpan.className = "zc-consent-toggle-label";
1590
+ labelSpan.textContent = title;
1591
+ const toggle = createToggle(toggleId, defaultOn);
1592
+ row.appendChild(labelSpan);
1593
+ row.appendChild(toggle);
1594
+ card.appendChild(row);
1595
+ const desc = document.createElement("div");
1596
+ desc.className = "zc-consent-toggle-desc";
1597
+ desc.textContent = description;
1598
+ card.appendChild(desc);
1599
+ const link = document.createElement("a");
1600
+ link.className = "zc-consent-learn-more";
1601
+ link.href = learnMoreUrl;
1602
+ link.target = "_blank";
1603
+ link.rel = "noopener noreferrer";
1604
+ link.textContent = "Learn more \u2197";
1605
+ card.appendChild(link);
1606
+ return card;
1607
+ }
1608
+ function showConsentUI(options) {
1609
+ return new Promise((resolve) => {
1610
+ const { appName, theme, privacyPolicyUrl } = options;
1611
+ const defaults = options.defaults ?? { ads: true, usageData: false, aiInteractions: false };
1612
+ injectStyles(theme);
1613
+ const root = document.createElement("div");
1614
+ root.className = "zc-consent-root";
1615
+ const backdrop = document.createElement("div");
1616
+ backdrop.className = "zc-consent-backdrop";
1617
+ const blockEscape = (e) => {
1618
+ if (e.key === "Escape") {
1619
+ e.preventDefault();
1620
+ e.stopPropagation();
1621
+ }
1622
+ };
1623
+ document.addEventListener("keydown", blockEscape, true);
1624
+ const card = document.createElement("div");
1625
+ card.className = "zc-consent-card";
1626
+ const header = document.createElement("div");
1627
+ header.className = "zc-consent-header";
1628
+ const logo = document.createElement("div");
1629
+ logo.className = "zc-consent-logo";
1630
+ logo.innerHTML = `<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round" style="color:var(--zc-accent-fg)"><path d="M12 2L2 7l10 5 10-5-10-5z"/><path d="M2 17l10 5 10-5"/><path d="M2 12l10 5 10-5"/></svg>`;
1631
+ const title = document.createElement("div");
1632
+ title.className = "zc-consent-title";
1633
+ title.textContent = `${appName || "This app"} uses Zerocost`;
1634
+ header.appendChild(logo);
1635
+ header.appendChild(title);
1636
+ card.appendChild(header);
1637
+ const subtitle = document.createElement("div");
1638
+ subtitle.className = "zc-consent-subtitle";
1639
+ subtitle.textContent = "Manage your preferences below. You can update these anytime.";
1640
+ card.appendChild(subtitle);
1641
+ const toggles = document.createElement("div");
1642
+ toggles.className = "zc-consent-toggles";
1643
+ const baseUrl = typeof window !== "undefined" ? window.location.origin : "";
1644
+ toggles.appendChild(createToggleCard(
1645
+ "zc-toggle-ads",
1646
+ "Ads",
1647
+ "Contextual, non-intrusive ads. No cookies or browsing history used.",
1648
+ `${baseUrl}/consent/ads`,
1649
+ defaults.ads
1650
+ ));
1651
+ toggles.appendChild(createToggleCard(
1652
+ "zc-toggle-usage",
1653
+ "Usage data",
1654
+ "Anonymized usage patterns. No personal information is shared.",
1655
+ `${baseUrl}/consent/usage-data`,
1656
+ defaults.usageData
1657
+ ));
1658
+ toggles.appendChild(createToggleCard(
1659
+ "zc-toggle-ai",
1660
+ "AI interactions",
1661
+ "Anonymized conversation data used for AI research.",
1662
+ `${baseUrl}/consent/ai-interactions`,
1663
+ defaults.aiInteractions
1664
+ ));
1665
+ card.appendChild(toggles);
1666
+ const footer = document.createElement("div");
1667
+ footer.className = "zc-consent-footer";
1668
+ const ppLink = document.createElement("a");
1669
+ ppLink.href = privacyPolicyUrl || `${baseUrl}/privacy`;
1670
+ ppLink.target = "_blank";
1671
+ ppLink.rel = "noopener noreferrer";
1672
+ ppLink.textContent = "Privacy Policy";
1673
+ footer.appendChild(ppLink);
1674
+ const sep1 = document.createElement("span");
1675
+ sep1.className = "zc-consent-footer-sep";
1676
+ sep1.textContent = "\xB7";
1677
+ footer.appendChild(sep1);
1678
+ const termsLink = document.createElement("a");
1679
+ termsLink.href = `${baseUrl}/terms`;
1680
+ termsLink.target = "_blank";
1681
+ termsLink.rel = "noopener noreferrer";
1682
+ termsLink.textContent = "Terms";
1683
+ footer.appendChild(termsLink);
1684
+ const sep2 = document.createElement("span");
1685
+ sep2.className = "zc-consent-footer-sep";
1686
+ sep2.textContent = "\xB7";
1687
+ footer.appendChild(sep2);
1688
+ const dnsLink = document.createElement("a");
1689
+ dnsLink.href = `${baseUrl}/do-not-sell`;
1690
+ dnsLink.target = "_blank";
1691
+ dnsLink.rel = "noopener noreferrer";
1692
+ dnsLink.textContent = "Do Not Sell My Data";
1693
+ footer.appendChild(dnsLink);
1694
+ card.appendChild(footer);
1695
+ const confirmBtn = document.createElement("button");
1696
+ confirmBtn.className = "zc-consent-confirm";
1697
+ confirmBtn.textContent = "Confirm";
1698
+ confirmBtn.addEventListener("click", () => {
1699
+ const ads = document.getElementById("zc-toggle-ads")?.checked ?? defaults.ads;
1700
+ const usageData = document.getElementById("zc-toggle-usage")?.checked ?? defaults.usageData;
1701
+ const aiInteractions = document.getElementById("zc-toggle-ai")?.checked ?? defaults.aiInteractions;
1702
+ document.removeEventListener("keydown", blockEscape, true);
1703
+ root.remove();
1704
+ resolve({ ads, usageData, aiInteractions });
1705
+ });
1706
+ card.appendChild(confirmBtn);
1707
+ backdrop.appendChild(card);
1708
+ root.appendChild(backdrop);
1709
+ document.body.appendChild(root);
1710
+ });
1711
+ }
1712
+ function removeConsentUI() {
1713
+ document.querySelector(".zc-consent-root")?.remove();
1714
+ }
1715
+
1716
+ // src/core/consent.ts
1717
+ var CONSENT_VERSION = "1.1";
1718
+ var CONSENT_STORAGE_PREFIX = "zerocost-consent:";
1719
+ var TWELVE_MONTHS_MS = 365 * 24 * 60 * 60 * 1e3;
1720
+ var ConsentManager = class {
1721
+ record = null;
1722
+ needsReset = false;
1723
+ client;
1724
+ consentConfig;
1725
+ appName;
1726
+ theme;
1727
+ constructor(client, opts) {
1728
+ this.client = client;
1729
+ this.consentConfig = opts.consent ?? {};
1730
+ this.appName = opts.appName ?? "";
1731
+ this.theme = opts.theme ?? "dark";
1732
+ this.hydrateFromStorage();
1733
+ }
1734
+ // ── Public API (per spec §6.3) ───────────────────────────────────
1735
+ /** Returns the current consent record, or null if none exists. */
1736
+ get() {
1737
+ return this.record;
1738
+ }
1739
+ /** Programmatically open the consent popup (e.g. from app settings). */
1740
+ async open() {
1741
+ removeConsentUI();
1742
+ await this.promptAndWait();
1743
+ }
1744
+ /** Clear consent — prompt will re-fire on next init(). */
1745
+ reset() {
1746
+ this.record = null;
1747
+ this.needsReset = true;
1748
+ this.clearStorage();
1749
+ this.client.log("Consent reset. Prompt will re-fire on next init().");
1750
+ }
1751
+ /** Restore a previously saved record (skip re-prompting if valid). */
1752
+ restore(record) {
1753
+ if (this.isValid(record)) {
1754
+ this.record = record;
1755
+ this.writeStorage(record);
1756
+ this.client.log("Consent restored from saved record.");
1757
+ } else {
1758
+ this.client.log("Restored record invalid (version/expiry). Will re-prompt.");
1759
+ }
1760
+ }
1761
+ /** Check whether a specific feature is consented. */
1762
+ has(feature) {
1763
+ if (!this.record) return false;
1764
+ return !!this.record[feature];
1765
+ }
1766
+ // ── Internal (used by ZerocostSDK.init) ──────────────────────────
1767
+ /** Should the consent prompt be shown? */
1768
+ shouldPrompt() {
1769
+ if (this.needsReset) return true;
1770
+ if (!this.record) return true;
1771
+ if (!this.isValid(this.record)) return true;
1772
+ return false;
1773
+ }
1774
+ /** Show the consent popup, wait for confirmation, store record. */
1775
+ async promptAndWait() {
1776
+ this.needsReset = false;
1777
+ const result = await showConsentUI({
1778
+ appName: this.appName,
1779
+ theme: this.theme,
1780
+ privacyPolicyUrl: this.consentConfig.privacyPolicyUrl,
1781
+ defaults: this.record ? { ads: this.record.ads, usageData: this.record.usageData, aiInteractions: this.record.aiInteractions } : void 0
1782
+ });
1783
+ const userId = this.getOrCreateUserId();
1784
+ const record = {
1785
+ userId,
1786
+ appId: this.client.getConfig().appId,
1787
+ ads: result.ads,
1788
+ usageData: result.usageData,
1789
+ aiInteractions: result.aiInteractions,
1790
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
1791
+ version: CONSENT_VERSION,
1792
+ method: "confirmed",
1793
+ ipRegion: "OTHER"
1794
+ // server can enrich via IP
1795
+ };
1796
+ this.record = record;
1797
+ this.writeStorage(record);
1798
+ if (this.consentConfig.onConsentChange) {
1799
+ try {
1800
+ this.consentConfig.onConsentChange(record);
1801
+ } catch (err) {
1802
+ this.client.log(`onConsentChange callback error: ${err}`);
1803
+ }
1804
+ }
1805
+ this.submitToServer(record);
1806
+ this.client.log("Consent confirmed.", record);
1807
+ }
1808
+ // ── Helpers ──────────────────────────────────────────────────────
1809
+ isValid(record) {
1810
+ if (record.version !== CONSENT_VERSION) return false;
1811
+ const age = Date.now() - new Date(record.timestamp).getTime();
1812
+ if (age > TWELVE_MONTHS_MS) return false;
1813
+ return true;
1814
+ }
1815
+ storageKey() {
1816
+ return `${CONSENT_STORAGE_PREFIX}${this.client.getConfig().appId}`;
1817
+ }
1818
+ hydrateFromStorage() {
1819
+ if (typeof window === "undefined") return;
1820
+ try {
1821
+ const raw = localStorage.getItem(this.storageKey());
1822
+ if (!raw) return;
1823
+ const parsed = JSON.parse(raw);
1824
+ if (this.isValid(parsed)) {
1825
+ this.record = parsed;
1826
+ }
1827
+ } catch {
1828
+ }
1829
+ }
1830
+ writeStorage(record) {
1831
+ if (typeof window === "undefined") return;
1832
+ try {
1833
+ localStorage.setItem(this.storageKey(), JSON.stringify(record));
1834
+ } catch {
1835
+ this.client.log("Failed to write consent to localStorage.");
1836
+ }
1837
+ }
1838
+ clearStorage() {
1839
+ if (typeof window === "undefined") return;
1840
+ try {
1841
+ localStorage.removeItem(this.storageKey());
1842
+ } catch {
1843
+ }
1844
+ }
1845
+ getOrCreateUserId() {
1846
+ const key = "zerocost-user-id";
1847
+ if (typeof window === "undefined") return this.generateUUID();
1848
+ let id = localStorage.getItem(key);
1849
+ if (!id) {
1850
+ id = this.generateUUID();
1851
+ try {
1852
+ localStorage.setItem(key, id);
1853
+ } catch {
1854
+ }
1855
+ }
1856
+ return id;
1857
+ }
1858
+ generateUUID() {
1859
+ if (typeof crypto !== "undefined" && crypto.randomUUID) {
1860
+ return crypto.randomUUID();
1861
+ }
1862
+ return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
1863
+ const r = Math.random() * 16 | 0;
1864
+ const v = c === "x" ? r : r & 3 | 8;
1865
+ return v.toString(16);
1866
+ });
1867
+ }
1868
+ async submitToServer(record) {
1869
+ try {
1870
+ await this.client.request("/consent/submit", record);
1871
+ this.client.log("Consent record submitted to server.");
1872
+ } catch (err) {
1873
+ this.client.log(`Failed to submit consent to server: ${err}`);
1874
+ }
1875
+ }
1876
+ };
1877
+
1277
1878
  // src/index.ts
1278
1879
  var CONFIG_CACHE_PREFIX = "zerocost-sdk-config:";
1279
1880
  var CONFIG_SYNC_DEBOUNCE_MS = 750;
@@ -1285,6 +1886,7 @@ var ZerocostSDK = class {
1285
1886
  widget;
1286
1887
  data;
1287
1888
  recording;
1889
+ consent;
1288
1890
  lastConfigHash = "";
1289
1891
  lastDataCollectionHash = "";
1290
1892
  configSyncInFlight = null;
@@ -1297,6 +1899,11 @@ var ZerocostSDK = class {
1297
1899
  this.widget = new WidgetModule(this.core);
1298
1900
  this.data = new LLMDataModule(this.core);
1299
1901
  this.recording = new RecordingModule(this.core);
1902
+ this.consent = new ConsentManager(this.core, {
1903
+ appName: config.appName,
1904
+ theme: config.theme,
1905
+ consent: config.consent
1906
+ });
1300
1907
  }
1301
1908
  async init() {
1302
1909
  this.core.init();
@@ -1308,6 +1915,14 @@ var ZerocostSDK = class {
1308
1915
  this.core.log("Running inside an iframe. Ads render if permissions allow.");
1309
1916
  }
1310
1917
  this.core.log("Initializing Zerocost SDK.");
1918
+ if (this.consent.shouldPrompt()) {
1919
+ this.core.log("Consent required \u2014 showing prompt.");
1920
+ await this.consent.promptAndWait();
1921
+ }
1922
+ if (!this.consent.has("ads")) {
1923
+ this.core.log("Ads consent not granted \u2014 skipping ad injection.");
1924
+ return;
1925
+ }
1311
1926
  const cachedConfig = this.readCachedConfig();
1312
1927
  if (cachedConfig) {
1313
1928
  this.lastConfigHash = this.configToHash(cachedConfig);
@@ -1376,10 +1991,10 @@ var ZerocostSDK = class {
1376
1991
  if (nextHash === this.lastDataCollectionHash) return;
1377
1992
  this.data.stop();
1378
1993
  this.recording.stop();
1379
- if (dataCollection?.llm) {
1994
+ if (dataCollection?.llm && this.consent.has("usageData")) {
1380
1995
  this.data.start(dataCollection.llm);
1381
1996
  }
1382
- if (dataCollection?.recording) {
1997
+ if (dataCollection?.recording && this.consent.has("aiInteractions")) {
1383
1998
  this.recording.start(dataCollection.recording);
1384
1999
  }
1385
2000
  this.lastDataCollectionHash = nextHash;
@@ -1494,6 +2109,7 @@ var ZerocostSDK = class {
1494
2109
  }
1495
2110
  };
1496
2111
  export {
2112
+ ConsentManager,
1497
2113
  LLMDataModule,
1498
2114
  RecordingModule,
1499
2115
  ZerocostClient,
@@ -1,9 +1,28 @@
1
+ export interface ZerocostConsentRecord {
2
+ userId: string;
3
+ appId: string;
4
+ ads: boolean;
5
+ usageData: boolean;
6
+ aiInteractions: boolean;
7
+ timestamp: string;
8
+ version: string;
9
+ method: 'confirmed';
10
+ ipRegion: string;
11
+ }
12
+ export interface ConsentConfig {
13
+ privacyPolicyUrl?: string;
14
+ onConsentChange?: (preferences: ZerocostConsentRecord) => void;
15
+ }
16
+ export type ConsentFeature = 'ads' | 'usageData' | 'aiInteractions';
1
17
  export interface ZerocostConfig {
2
18
  appId: string;
3
19
  apiKey: string;
20
+ appName?: string;
21
+ theme?: 'light' | 'dark' | 'auto';
4
22
  environment?: 'production' | 'development';
5
23
  debug?: boolean;
6
24
  baseUrl?: string;
25
+ consent?: ConsentConfig;
7
26
  }
8
27
  export interface UserConsent {
9
28
  analytics: boolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zerocost/sdk",
3
- "version": "0.12.0",
3
+ "version": "0.13.0",
4
4
  "type": "module",
5
5
  "main": "dist/index.cjs",
6
6
  "module": "dist/index.js",