@cossistant/core 0.0.7

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.
Files changed (76) hide show
  1. package/dist/_virtual/rolldown_runtime.js +33 -0
  2. package/dist/client.d.ts +82 -0
  3. package/dist/client.d.ts.map +1 -0
  4. package/dist/client.js +275 -0
  5. package/dist/client.js.map +1 -0
  6. package/dist/contact.d.ts +28 -0
  7. package/dist/contact.d.ts.map +1 -0
  8. package/dist/conversation.d.ts +85 -0
  9. package/dist/conversation.d.ts.map +1 -0
  10. package/dist/index.d.ts +15 -0
  11. package/dist/index.js +16 -0
  12. package/dist/locale-utils.d.ts +17 -0
  13. package/dist/locale-utils.d.ts.map +1 -0
  14. package/dist/locale-utils.js +22 -0
  15. package/dist/locale-utils.js.map +1 -0
  16. package/dist/logger.d.ts +11 -0
  17. package/dist/logger.d.ts.map +1 -0
  18. package/dist/logger.js +22 -0
  19. package/dist/logger.js.map +1 -0
  20. package/dist/realtime-events.d.ts +120 -0
  21. package/dist/realtime-events.d.ts.map +1 -0
  22. package/dist/rest-client.d.ts +65 -0
  23. package/dist/rest-client.d.ts.map +1 -0
  24. package/dist/rest-client.js +367 -0
  25. package/dist/rest-client.js.map +1 -0
  26. package/dist/schemas.d.ts +33 -0
  27. package/dist/schemas.d.ts.map +1 -0
  28. package/dist/store/conversations-store.d.ts +22 -0
  29. package/dist/store/conversations-store.d.ts.map +1 -0
  30. package/dist/store/conversations-store.js +108 -0
  31. package/dist/store/conversations-store.js.map +1 -0
  32. package/dist/store/create-store.d.ts +14 -0
  33. package/dist/store/create-store.d.ts.map +1 -0
  34. package/dist/store/create-store.js +47 -0
  35. package/dist/store/create-store.js.map +1 -0
  36. package/dist/store/seen-store.d.ts +37 -0
  37. package/dist/store/seen-store.d.ts.map +1 -0
  38. package/dist/store/seen-store.js +131 -0
  39. package/dist/store/seen-store.js.map +1 -0
  40. package/dist/store/support-store.d.ts +63 -0
  41. package/dist/store/support-store.d.ts.map +1 -0
  42. package/dist/store/support-store.js +170 -0
  43. package/dist/store/support-store.js.map +1 -0
  44. package/dist/store/timeline-items-store.d.ts +27 -0
  45. package/dist/store/timeline-items-store.d.ts.map +1 -0
  46. package/dist/store/timeline-items-store.js +142 -0
  47. package/dist/store/timeline-items-store.js.map +1 -0
  48. package/dist/store/typing-store.d.ts +50 -0
  49. package/dist/store/typing-store.d.ts.map +1 -0
  50. package/dist/store/typing-store.js +149 -0
  51. package/dist/store/typing-store.js.map +1 -0
  52. package/dist/store/website-store.d.ts +24 -0
  53. package/dist/store/website-store.d.ts.map +1 -0
  54. package/dist/store/website-store.js +60 -0
  55. package/dist/store/website-store.js.map +1 -0
  56. package/dist/timeline-item.d.ts +207 -0
  57. package/dist/timeline-item.d.ts.map +1 -0
  58. package/dist/types/src/enums.js +24 -0
  59. package/dist/types/src/enums.js.map +1 -0
  60. package/dist/types.d.ts +17 -0
  61. package/dist/types.d.ts.map +1 -0
  62. package/dist/types.js +17 -0
  63. package/dist/types.js.map +1 -0
  64. package/dist/utils.d.ts +8 -0
  65. package/dist/utils.d.ts.map +1 -0
  66. package/dist/utils.js +20 -0
  67. package/dist/utils.js.map +1 -0
  68. package/dist/visitor-data.d.ts +33 -0
  69. package/dist/visitor-data.d.ts.map +1 -0
  70. package/dist/visitor-data.js +212 -0
  71. package/dist/visitor-data.js.map +1 -0
  72. package/dist/visitor-tracker.d.ts +31 -0
  73. package/dist/visitor-tracker.d.ts.map +1 -0
  74. package/dist/visitor-tracker.js +109 -0
  75. package/dist/visitor-tracker.js.map +1 -0
  76. package/package.json +48 -0
@@ -0,0 +1 @@
1
+ {"version":3,"file":"visitor-data.js","names":["WINDOWS_VERSION_MAP: Record<string, string>","version: string | null"],"sources":["../src/visitor-data.ts"],"sourcesContent":["/**\n * Utilities for collecting visitor data including browser, device, and location information\n * Moved from packages/react for better separation of concerns\n */\n/** biome-ignore-all lint/complexity/useOptionalChain: ok */\n\ntype VisitorData = {\n\tbrowser: string | null;\n\tbrowserVersion: string | null;\n\tos: string | null;\n\tosVersion: string | null;\n\tdevice: string | null;\n\tdeviceType: \"desktop\" | \"mobile\" | \"tablet\" | \"unknown\";\n\tlanguage: string | null;\n\ttimezone: string | null;\n\tscreenResolution: string | null;\n\tviewport: string | null;\n\tip: string | null;\n\tcity: string | null;\n\tregion: string | null;\n\tcountry: string | null;\n\tcountryCode: string | null;\n\tlatitude: number | null;\n\tlongitude: number | null;\n};\n\n// Browser detection patterns\nconst EDGE_PATTERN = /Edg\\/([0-9.]+)/;\nconst CHROME_PATTERN = /Chrome\\/([0-9.]+)/;\nconst SAFARI_PATTERN = /Version\\/([0-9.]+).*Safari/;\nconst FIREFOX_PATTERN = /Firefox\\/([0-9.]+)/;\nconst OPERA_PATTERN = /OPR\\/([0-9.]+)/;\n\n/**\n * Parse user agent to extract browser information\n */\nfunction parseBrowser(userAgent: string): {\n\tbrowser: string | null;\n\tversion: string | null;\n} {\n\tconst browsers = [\n\t\t{ name: \"Edge\", pattern: EDGE_PATTERN },\n\t\t{ name: \"Chrome\", pattern: CHROME_PATTERN },\n\t\t{ name: \"Safari\", pattern: SAFARI_PATTERN },\n\t\t{ name: \"Firefox\", pattern: FIREFOX_PATTERN },\n\t\t{ name: \"Opera\", pattern: OPERA_PATTERN },\n\t];\n\n\tfor (const { name, pattern } of browsers) {\n\t\tconst match = userAgent.match(pattern);\n\t\tif (match) {\n\t\t\treturn { browser: name, version: match[1] || null };\n\t\t}\n\t}\n\n\treturn { browser: null, version: null };\n}\n\n// OS detection patterns\nconst WINDOWS_PATTERN = /Windows NT ([0-9.]+)/;\nconst MACOS_PATTERN = /Mac OS X ([0-9_]+)/;\nconst IOS_PATTERN = /OS ([0-9_]+) like Mac OS X/;\nconst ANDROID_PATTERN = /Android ([0-9.]+)/;\nconst LINUX_PATTERN = /Linux/;\n\nconst WINDOWS_VERSION_MAP: Record<string, string> = {\n\t\"10.0\": \"10\",\n\t\"6.3\": \"8.1\",\n\t\"6.2\": \"8\",\n\t\"6.1\": \"7\",\n};\n\n/**\n * Transform version string by replacing underscores with dots\n */\nfunction transformVersion(version: string): string {\n\treturn version.replace(/_/g, \".\");\n}\n\n/**\n * Parse user agent to extract OS information\n */\nfunction parseOS(userAgent: string): {\n\tos: string | null;\n\tversion: string | null;\n} {\n\t// Check Windows\n\tconst windowsMatch = userAgent.match(WINDOWS_PATTERN);\n\tif (windowsMatch) {\n\t\tconst rawVersion = windowsMatch[1];\n\t\tlet version: string | null = null;\n\t\tif (rawVersion) {\n\t\t\tversion = WINDOWS_VERSION_MAP[rawVersion] || rawVersion;\n\t\t}\n\t\treturn { os: \"Windows\", version };\n\t}\n\n\t// Check macOS\n\tconst macMatch = userAgent.match(MACOS_PATTERN);\n\tif (macMatch) {\n\t\tconst version = macMatch[1] ? transformVersion(macMatch[1]) : null;\n\t\treturn { os: \"macOS\", version };\n\t}\n\n\t// Check iOS\n\tconst iosMatch = userAgent.match(IOS_PATTERN);\n\tif (iosMatch) {\n\t\tconst version = iosMatch[1] ? transformVersion(iosMatch[1]) : null;\n\t\treturn { os: \"iOS\", version };\n\t}\n\n\t// Check Android\n\tconst androidMatch = userAgent.match(ANDROID_PATTERN);\n\tif (androidMatch) {\n\t\treturn { os: \"Android\", version: androidMatch[1] || null };\n\t}\n\n\t// Check Linux\n\tif (LINUX_PATTERN.test(userAgent)) {\n\t\treturn { os: \"Linux\", version: null };\n\t}\n\n\treturn { os: null, version: null };\n}\n\n// Device type detection patterns\nconst MOBILE_PATTERN = /Mobile|Android|iPhone|iPod/i;\nconst TABLET_PATTERN = /iPad|Tablet|Tab/i;\n\n// Device name detection patterns\nconst IPHONE_PATTERN = /iPhone/;\nconst IPAD_PATTERN = /iPad/;\nconst IPOD_PATTERN = /iPod/;\nconst ANDROID_MOBILE_PATTERN = /Android.*Mobile/;\nconst ANDROID_TABLET_PATTERN = /Android.*Tablet/;\nconst WINDOWS_PHONE_PATTERN = /Windows Phone/;\nconst MACINTOSH_PATTERN = /Macintosh/;\nconst WINDOWS_PATTERN_DEVICE = /Windows/;\nconst LINUX_PATTERN_DEVICE = /Linux/;\n\n/**\n * Detect device type from user agent\n */\nfunction detectDeviceType(\n\tuserAgent: string\n): \"desktop\" | \"mobile\" | \"tablet\" | \"unknown\" {\n\tconst isMobile = MOBILE_PATTERN.test(userAgent);\n\tconst isTablet = TABLET_PATTERN.test(userAgent);\n\n\tif (isTablet) {\n\t\treturn \"tablet\";\n\t}\n\tif (isMobile) {\n\t\treturn \"mobile\";\n\t}\n\tif (\n\t\tuserAgent.includes(\"Windows\") ||\n\t\tuserAgent.includes(\"Mac\") ||\n\t\tuserAgent.includes(\"Linux\")\n\t) {\n\t\treturn \"desktop\";\n\t}\n\n\treturn \"unknown\";\n}\n\n/**\n * Get device name from user agent\n */\nfunction getDeviceName(userAgent: string): string | null {\n\tconst devices = [\n\t\t{ pattern: IPHONE_PATTERN, name: \"iPhone\" },\n\t\t{ pattern: IPAD_PATTERN, name: \"iPad\" },\n\t\t{ pattern: IPOD_PATTERN, name: \"iPod\" },\n\t\t{ pattern: ANDROID_MOBILE_PATTERN, name: \"Android Phone\" },\n\t\t{ pattern: ANDROID_TABLET_PATTERN, name: \"Android Tablet\" },\n\t\t{ pattern: WINDOWS_PHONE_PATTERN, name: \"Windows Phone\" },\n\t\t{ pattern: MACINTOSH_PATTERN, name: \"Mac\" },\n\t\t{ pattern: WINDOWS_PATTERN_DEVICE, name: \"Windows PC\" },\n\t\t{ pattern: LINUX_PATTERN_DEVICE, name: \"Linux PC\" },\n\t];\n\n\tfor (const { pattern, name } of devices) {\n\t\tif (pattern.test(userAgent)) {\n\t\t\treturn name;\n\t\t}\n\t}\n\n\treturn null;\n}\n\n/**\n * Check if we're running in a browser environment\n */\nfunction isBrowser(): boolean {\n\treturn typeof window !== \"undefined\" && typeof navigator !== \"undefined\";\n}\n\nfunction inferCityFromTimezone(timezone: string | null): string | null {\n\tif (!timezone?.includes(\"/\")) {\n\t\treturn null;\n\t}\n\tconst [, city] = timezone.split(\"/\");\n\treturn city ? city.replace(/_/g, \" \") : null;\n}\n\n/**\n * Collect visitor data from the browser environment\n * Returns null if not in browser environment\n */\nexport async function collectVisitorData(): Promise<VisitorData | null> {\n\tif (!isBrowser()) {\n\t\treturn null;\n\t}\n\n\tconst userAgent = navigator.userAgent || \"\";\n\tconst { browser, version: browserVersion } = parseBrowser(userAgent);\n\tconst { os, version: osVersion } = parseOS(userAgent);\n\tconst language = navigator.language || null;\n\tconst timezone = Intl.DateTimeFormat().resolvedOptions().timeZone || null;\n\tconst inferredCity = inferCityFromTimezone(timezone);\n\n\treturn {\n\t\tbrowser,\n\t\tbrowserVersion,\n\t\tos,\n\t\tosVersion,\n\t\tdevice: getDeviceName(userAgent),\n\t\tdeviceType: detectDeviceType(userAgent),\n\t\tlanguage,\n\t\ttimezone,\n\t\tscreenResolution:\n\t\t\ttypeof window !== \"undefined\" && window.screen\n\t\t\t\t? `${window.screen.width}x${window.screen.height}`\n\t\t\t\t: null,\n\t\tviewport:\n\t\t\ttypeof window !== \"undefined\"\n\t\t\t\t? `${window.innerWidth}x${window.innerHeight}`\n\t\t\t\t: null,\n\t\tip: null,\n\t\tcity: inferredCity,\n\t\tregion: null,\n\t\tcountry: null,\n\t\tcountryCode: null,\n\t\tlatitude: null,\n\t\tlongitude: null,\n\t};\n}\n\n/**\n * Export the interface for use by consumers\n */\nexport type { VisitorData };\n"],"mappings":";AA2BA,MAAM,eAAe;AACrB,MAAM,iBAAiB;AACvB,MAAM,iBAAiB;AACvB,MAAM,kBAAkB;AACxB,MAAM,gBAAgB;;;;AAKtB,SAAS,aAAa,WAGpB;CACD,MAAM,WAAW;EAChB;GAAE,MAAM;GAAQ,SAAS;GAAc;EACvC;GAAE,MAAM;GAAU,SAAS;GAAgB;EAC3C;GAAE,MAAM;GAAU,SAAS;GAAgB;EAC3C;GAAE,MAAM;GAAW,SAAS;GAAiB;EAC7C;GAAE,MAAM;GAAS,SAAS;GAAe;EACzC;AAED,MAAK,MAAM,EAA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AAI,SAAS,cAAc,QAAQ,UAAU;CACrD,MAAM,WAAW,UAAU,YAAY;CACvC,MAAM,WAAW,KAAK,gBAAgB,CAAC,iBAAiB,CAAC,YAAY;CACrE,MAAM,eAAe,sBAAsB,SAAS;AAEpD,QAAO;EACN;EACA;EACA;EACA;EACA,QAAQ,cAAc,UAAU;EAChC,YAAY,iBAAiB,UAAU;EACvC;EACA;EACA,kBACC,OAAO,WAAW,eAAe,OAAO,SACrC,GAAG,OAAO,OAAO,MAAM,GAAG,OAAO,OAAO,WACxC;EACJ,UACC,OAAO,WAAW,cACf,GAAG,OAAO,WAAW,GAAG,OAAO,gBAC/B;EACJ,IAAI;EACJ,MAAM;EACN,QAAQ;EACR,SAAS;EACT,aAAa;EACb,UAAU;EACV,WAAW;EACX"}
@@ -0,0 +1,31 @@
1
+ //#region src/visitor-tracker.d.ts
2
+ /**
3
+ * Visitor tracking utilities for managing visitor IDs in localStorage
4
+ */
5
+ /**
6
+ * Get visitor ID from localStorage for a specific website
7
+ */
8
+ declare function getVisitorId(websiteId: string): string | null;
9
+ /**
10
+ * Set visitor ID in localStorage for a specific website
11
+ */
12
+ declare function setVisitorId(websiteId: string, visitorId: string): void;
13
+ /**
14
+ * Clear visitor ID for a specific website
15
+ */
16
+ declare function clearVisitorId(websiteId: string): void;
17
+ /**
18
+ * Clear all visitor IDs
19
+ */
20
+ declare function clearAllVisitorIds(): void;
21
+ /**
22
+ * Get any existing visitor ID from localStorage by matching the public key
23
+ * This is useful when we don't yet know the website ID but need to check for existing visitors
24
+ */
25
+ declare function getExistingVisitorId(publicKey: string): {
26
+ websiteId: string;
27
+ visitorId: string;
28
+ } | null;
29
+ //#endregion
30
+ export { clearAllVisitorIds, clearVisitorId, getExistingVisitorId, getVisitorId, setVisitorId };
31
+ //# sourceMappingURL=visitor-tracker.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"visitor-tracker.d.ts","names":[],"sources":["../src/visitor-tracker.ts"],"sourcesContent":[],"mappings":";;AAmCA;AA4BA;AAwBA;AAkBA;AAwBA;iBA9FgB,YAAA;;;;iBA4BA,YAAA;;;;iBAwBA,cAAA;;;;iBAkBA,kBAAA,CAAA;;;;;iBAwBA,oBAAA"}
@@ -0,0 +1,109 @@
1
+ import { isValidULID } from "./utils.js";
2
+
3
+ //#region src/visitor-tracker.ts
4
+ /**
5
+ * Visitor tracking utilities for managing visitor IDs in localStorage
6
+ */
7
+ const STORAGE_KEY_PREFIX = "cossistant_visitor";
8
+ /**
9
+ * Get the localStorage key for a specific website
10
+ */
11
+ function getStorageKey(websiteId) {
12
+ return `${STORAGE_KEY_PREFIX}_${websiteId}`;
13
+ }
14
+ /**
15
+ * Check if localStorage is available
16
+ */
17
+ function isLocalStorageAvailable() {
18
+ try {
19
+ if (typeof window === "undefined" || !window.localStorage) return false;
20
+ const testKey = "__cossistant_test__";
21
+ localStorage.setItem(testKey, "test");
22
+ localStorage.removeItem(testKey);
23
+ return true;
24
+ } catch {
25
+ return false;
26
+ }
27
+ }
28
+ /**
29
+ * Get visitor ID from localStorage for a specific website
30
+ */
31
+ function getVisitorId(websiteId) {
32
+ if (!isLocalStorageAvailable()) return null;
33
+ try {
34
+ if (typeof window === "undefined") return null;
35
+ const data = localStorage.getItem(getStorageKey(websiteId));
36
+ if (!data) return null;
37
+ const parsed = JSON.parse(data);
38
+ if (parsed.visitorId && isValidULID(parsed.visitorId)) return parsed.visitorId;
39
+ return null;
40
+ } catch {
41
+ return null;
42
+ }
43
+ }
44
+ /**
45
+ * Set visitor ID in localStorage for a specific website
46
+ */
47
+ function setVisitorId(websiteId, visitorId) {
48
+ if (!isLocalStorageAvailable()) return;
49
+ try {
50
+ const data = {
51
+ visitorId,
52
+ timestamp: Date.now(),
53
+ websiteId
54
+ };
55
+ if (typeof window === "undefined") return;
56
+ const key = getStorageKey(websiteId);
57
+ localStorage.setItem(key, JSON.stringify(data));
58
+ } catch {}
59
+ }
60
+ /**
61
+ * Clear visitor ID for a specific website
62
+ */
63
+ function clearVisitorId(websiteId) {
64
+ if (!isLocalStorageAvailable()) return;
65
+ try {
66
+ if (typeof window === "undefined") return;
67
+ localStorage.removeItem(getStorageKey(websiteId));
68
+ } catch {}
69
+ }
70
+ /**
71
+ * Clear all visitor IDs
72
+ */
73
+ function clearAllVisitorIds() {
74
+ if (!isLocalStorageAvailable()) return;
75
+ try {
76
+ if (typeof window === "undefined") return;
77
+ const keys = Object.keys(localStorage).filter((key) => key.startsWith(STORAGE_KEY_PREFIX));
78
+ for (const key of keys) localStorage.removeItem(key);
79
+ } catch {}
80
+ }
81
+ /**
82
+ * Get any existing visitor ID from localStorage by matching the public key
83
+ * This is useful when we don't yet know the website ID but need to check for existing visitors
84
+ */
85
+ function getExistingVisitorId(publicKey) {
86
+ if (!isLocalStorageAvailable()) return null;
87
+ try {
88
+ if (typeof window === "undefined") return null;
89
+ const keys = Object.keys(localStorage).filter((key) => key.startsWith(STORAGE_KEY_PREFIX));
90
+ for (const key of keys) {
91
+ const data = typeof window !== "undefined" ? localStorage.getItem(key) : null;
92
+ if (!data) continue;
93
+ try {
94
+ const parsed = JSON.parse(data);
95
+ if (parsed.visitorId && isValidULID(parsed.visitorId) && parsed.websiteId) return {
96
+ websiteId: parsed.websiteId,
97
+ visitorId: parsed.visitorId
98
+ };
99
+ } catch {}
100
+ }
101
+ return null;
102
+ } catch {
103
+ return null;
104
+ }
105
+ }
106
+
107
+ //#endregion
108
+ export { clearAllVisitorIds, clearVisitorId, getExistingVisitorId, getVisitorId, setVisitorId };
109
+ //# sourceMappingURL=visitor-tracker.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"visitor-tracker.js","names":[],"sources":["../src/visitor-tracker.ts"],"sourcesContent":["/**\n * Visitor tracking utilities for managing visitor IDs in localStorage\n */\n\nimport { isValidULID } from \"./utils\";\n\nconst STORAGE_KEY_PREFIX = \"cossistant_visitor\";\n\n/**\n * Get the localStorage key for a specific website\n */\nfunction getStorageKey(websiteId: string): string {\n\treturn `${STORAGE_KEY_PREFIX}_${websiteId}`;\n}\n\n/**\n * Check if localStorage is available\n */\nfunction isLocalStorageAvailable(): boolean {\n\ttry {\n\t\tif (typeof window === \"undefined\" || !window.localStorage) {\n\t\t\treturn false;\n\t\t}\n\t\tconst testKey = \"__cossistant_test__\";\n\t\tlocalStorage.setItem(testKey, \"test\");\n\t\tlocalStorage.removeItem(testKey);\n\t\treturn true;\n\t} catch {\n\t\treturn false;\n\t}\n}\n\n/**\n * Get visitor ID from localStorage for a specific website\n */\nexport function getVisitorId(websiteId: string): string | null {\n\tif (!isLocalStorageAvailable()) {\n\t\treturn null;\n\t}\n\n\ttry {\n\t\tif (typeof window === \"undefined\") {\n\t\t\treturn null;\n\t\t}\n\t\tconst data = localStorage.getItem(getStorageKey(websiteId));\n\t\tif (!data) {\n\t\t\treturn null;\n\t\t}\n\n\t\tconst parsed = JSON.parse(data);\n\t\t// Validate that the visitor ID is a valid ULID (26 characters)\n\t\tif (parsed.visitorId && isValidULID(parsed.visitorId)) {\n\t\t\treturn parsed.visitorId;\n\t\t}\n\t\treturn null;\n\t} catch {\n\t\treturn null;\n\t}\n}\n\n/**\n * Set visitor ID in localStorage for a specific website\n */\nexport function setVisitorId(websiteId: string, visitorId: string): void {\n\tif (!isLocalStorageAvailable()) {\n\t\treturn;\n\t}\n\n\ttry {\n\t\tconst data = {\n\t\t\tvisitorId,\n\t\t\ttimestamp: Date.now(),\n\t\t\twebsiteId,\n\t\t};\n\t\tif (typeof window === \"undefined\") {\n\t\t\treturn;\n\t\t}\n\t\tconst key = getStorageKey(websiteId);\n\t\tlocalStorage.setItem(key, JSON.stringify(data));\n\t} catch {\n\t\t// Silently fail - visitor tracking is not critical\n\t}\n}\n\n/**\n * Clear visitor ID for a specific website\n */\nexport function clearVisitorId(websiteId: string): void {\n\tif (!isLocalStorageAvailable()) {\n\t\treturn;\n\t}\n\n\ttry {\n\t\tif (typeof window === \"undefined\") {\n\t\t\treturn;\n\t\t}\n\t\tlocalStorage.removeItem(getStorageKey(websiteId));\n\t} catch {\n\t\t// Silently fail - visitor tracking is not critical\n\t}\n}\n\n/**\n * Clear all visitor IDs\n */\nexport function clearAllVisitorIds(): void {\n\tif (!isLocalStorageAvailable()) {\n\t\treturn;\n\t}\n\n\ttry {\n\t\tif (typeof window === \"undefined\") {\n\t\t\treturn;\n\t\t}\n\t\tconst keys = Object.keys(localStorage).filter((key) =>\n\t\t\tkey.startsWith(STORAGE_KEY_PREFIX)\n\t\t);\n\t\tfor (const key of keys) {\n\t\t\tlocalStorage.removeItem(key);\n\t\t}\n\t} catch {\n\t\t// Silently fail - visitor tracking is not critical\n\t}\n}\n\n/**\n * Get any existing visitor ID from localStorage by matching the public key\n * This is useful when we don't yet know the website ID but need to check for existing visitors\n */\nexport function getExistingVisitorId(\n\tpublicKey: string\n): { websiteId: string; visitorId: string } | null {\n\tif (!isLocalStorageAvailable()) {\n\t\treturn null;\n\t}\n\n\ttry {\n\t\tif (typeof window === \"undefined\") {\n\t\t\treturn null;\n\t\t}\n\t\t// Find all visitor keys in localStorage\n\t\tconst keys = Object.keys(localStorage).filter((key) =>\n\t\t\tkey.startsWith(STORAGE_KEY_PREFIX)\n\t\t);\n\n\t\t// Check each one to see if we have a valid visitor ID\n\t\tfor (const key of keys) {\n\t\t\tconst data =\n\t\t\t\ttypeof window !== \"undefined\" ? localStorage.getItem(key) : null;\n\t\t\tif (!data) {\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\ttry {\n\t\t\t\tconst parsed = JSON.parse(data);\n\t\t\t\t// Validate that the visitor ID is a valid ULID\n\t\t\t\tif (\n\t\t\t\t\tparsed.visitorId &&\n\t\t\t\t\tisValidULID(parsed.visitorId) &&\n\t\t\t\t\tparsed.websiteId\n\t\t\t\t) {\n\t\t\t\t\t// We found a valid visitor ID - return it\n\t\t\t\t\t// Note: In a multi-website scenario, you might want to match by public key\n\t\t\t\t\t// For now, we'll return the first valid one found\n\t\t\t\t\treturn {\n\t\t\t\t\t\twebsiteId: parsed.website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;AACH,MAAI,OAAO,WAAW,YACrB;EAED,MAAM,OAAO,OAAO,KAAK,aAAa,CAAC,QAAQ,QAC9C,IAAI,WAAW,mBAAmB,CAClC;AACD,OAAK,MAAM,OAAO,KACjB,cAAa,WAAW,IAAI;SAEtB;;;;;;AAST,SAAgB,qBACf,WACkD;AAClD,KAAI,CAAC,yBAAyB,CAC7B,QAAO;AAGR,KAAI;AACH,MAAI,OAAO,WAAW,YACrB,QAAO;EAGR,MAAM,OAAO,OAAO,KAAK,aAAa,CAAC,QAAQ,QAC9C,IAAI,WAAW,mBAAmB,CAClC;AAGD,OAAK,MAAM,OAAO,MAAM;GACvB,MAAM,OACL,OAAO,WAAW,cAAc,aAAa,QAAQ,IAAI,GAAG;AAC7D,OAAI,CAAC,KACJ;AAGD,OAAI;IACH,MAAM,SAAS,KAAK,MAAM,KAAK;AAE/B,QACC,OAAO,aACP,YAAY,OAAO,UAAU,IAC7B,OAAO,UAKP,QAAO;KACN,WAAW,OAAO;KAClB,WAAW,OAAO;KAClB;WAEK;;AAGT,SAAO;SACA;AACP,SAAO"}
package/package.json ADDED
@@ -0,0 +1,48 @@
1
+ {
2
+ "name": "@cossistant/core",
3
+ "type": "module",
4
+ "version": "0.0.7",
5
+ "private": false,
6
+ "author": "Cossistant Team",
7
+ "description": "Core library for connecting to Cossistant API and managing data fetching",
8
+ "keywords": [
9
+ "api",
10
+ "websocket",
11
+ "rest",
12
+ "core",
13
+ "typescript",
14
+ "support-ai"
15
+ ],
16
+ "license": "MIT",
17
+ "exports": {
18
+ ".": {
19
+ "import": "./dist/index.js",
20
+ "require": "./dist/index.js",
21
+ "types": "./dist/index.d.ts"
22
+ }
23
+ },
24
+ "main": "./dist/index.js",
25
+ "module": "./dist/index.js",
26
+ "types": "./dist/index.d.ts",
27
+ "sideEffects": false,
28
+ "files": [
29
+ "dist"
30
+ ],
31
+ "publishConfig": {
32
+ "access": "public"
33
+ },
34
+ "devDependencies": {
35
+ "@cossistant/typescript-config": "workspace:*",
36
+ "@cossistant/types": "workspace:*"
37
+ },
38
+ "dependencies": {
39
+ "nanoid": "^5.1.5",
40
+ "ulid": "^3.0.1",
41
+ "zod": "^4.1.8"
42
+ },
43
+ "scripts": {
44
+ "build": "tsdown",
45
+ "lint": "biome check .",
46
+ "check-types": "tsc --noEmit --project tsconfig.json"
47
+ }
48
+ }