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