universal-chatbot-saas 1.0.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.
@@ -0,0 +1,37 @@
1
+ {
2
+ "name": "universal_ai_chatbot",
3
+ "version": "1.0.0",
4
+ "description": "React wrapper for universal chatbot.js embed",
5
+ "main": "dist/index.js",
6
+ "module": "dist/index.mjs",
7
+ "types": "dist/index.d.ts",
8
+ "files": [
9
+ "dist",
10
+ "README.md",
11
+ "LICENSE"
12
+ ],
13
+ "sideEffects": false,
14
+ "scripts": {
15
+ "build": "tsup src/index.tsx --format cjs,esm --dts --clean",
16
+ "prepublishOnly": "npm run build"
17
+ },
18
+ "exports": {
19
+ ".": {
20
+ "types": "./dist/index.d.ts",
21
+ "import": "./dist/index.mjs",
22
+ "require": "./dist/index.js"
23
+ }
24
+ },
25
+ "publishConfig": {
26
+ "access": "public"
27
+ },
28
+ "peerDependencies": {
29
+ "react": ">=17"
30
+ },
31
+ "devDependencies": {
32
+ "@types/react": "^18.3.11",
33
+ "react": "^18.3.1",
34
+ "tsup": "^8.2.4",
35
+ "typescript": "^5.6.3"
36
+ }
37
+ }
@@ -0,0 +1,137 @@
1
+ import { useEffect, useMemo } from "react";
2
+
3
+ export type ChatbotProvider = "claude" | "openai" | "groq" | "mistral";
4
+ export type ChatbotPosition = "right" | "left" | "bottom-right" | "bottom-left";
5
+
6
+ export interface ChatbotEmbedProps {
7
+ src: string;
8
+ enabled?: boolean;
9
+ scriptId?: string;
10
+ removeExistingOnMount?: boolean;
11
+ removeWidgetOnUnmount?: boolean;
12
+ onLoad?: () => void;
13
+ onError?: (event: Event | string) => void;
14
+ botId?: string;
15
+ botApiKey?: string;
16
+ aiApikey?: string;
17
+ authToken?: string;
18
+ botName?: string;
19
+ color?: string;
20
+ welcome?: string;
21
+ placeholder?: string;
22
+ apiBase?: string;
23
+ provider?: ChatbotProvider;
24
+ model?: string;
25
+ aimodel?: string;
26
+ systemPrompt?: string;
27
+ position?: ChatbotPosition;
28
+ userId?: string;
29
+ visitorId?: string;
30
+ sessionId?: string;
31
+ chatSessionId?: string;
32
+ userHandle?: string;
33
+ user?: string;
34
+ siteId?: string;
35
+ siteKey?: string;
36
+ siteName?: string;
37
+ siteUrl?: string;
38
+ storageKey?: string;
39
+ knowledge?: unknown;
40
+ knowledgeJson?: string;
41
+ knowledgeUrl?: string;
42
+ knowledgeQuery?: Record<string, unknown>;
43
+ knowledgeQueryJson?: string;
44
+ knowledgeSyncMs?: number;
45
+ knowledgeSyncOnLoad?: boolean;
46
+ knowledgeClientFetch?: boolean;
47
+ knowledgeUrlIncludeContext?: boolean;
48
+ customCss?: string;
49
+ dataAttributes?: Record<string, string | number | boolean | null | undefined>;
50
+ }
51
+
52
+ function toKebabDataKey(key: string): string {
53
+ return `data-${key.replace(/[A-Z]/g, (match) => `-${match.toLowerCase()}`)}`;
54
+ }
55
+
56
+ function toDataAttributes(props: Omit<ChatbotEmbedProps, "src" | "onLoad" | "onError">): Record<string, string> {
57
+ const attrs: Record<string, string> = {};
58
+
59
+ for (const [key, value] of Object.entries(props || {})) {
60
+ if (key === "enabled" || key === "scriptId" || key === "removeExistingOnMount" || key === "removeWidgetOnUnmount" || key === "dataAttributes") {
61
+ continue;
62
+ }
63
+ if (value === undefined || value === null) {
64
+ continue;
65
+ }
66
+ const dataKey = toKebabDataKey(key);
67
+ attrs[dataKey] = typeof value === "object" ? JSON.stringify(value) : String(value);
68
+ }
69
+
70
+ return attrs;
71
+ }
72
+
73
+ function removeExistingWidgetNodes(): void {
74
+ document.querySelectorAll(".ucb-root").forEach((node) => node.remove());
75
+ document.querySelectorAll("style[data-ucb-custom-css='inline']").forEach((node) => node.remove());
76
+ }
77
+
78
+ export function ChatbotEmbed({
79
+ src,
80
+ enabled = true,
81
+ scriptId = "universal-chatbot-embed",
82
+ removeExistingOnMount = true,
83
+ removeWidgetOnUnmount = true,
84
+ onLoad,
85
+ onError,
86
+ ...dataProps
87
+ }: ChatbotEmbedProps): null {
88
+ const attrsSignature = useMemo(() => JSON.stringify(dataProps), [dataProps]);
89
+
90
+ useEffect(() => {
91
+ if (!enabled || typeof document === "undefined") {
92
+ return;
93
+ }
94
+
95
+ if (removeExistingOnMount) {
96
+ document.getElementById(scriptId)?.remove();
97
+ removeExistingWidgetNodes();
98
+ }
99
+
100
+ const script = document.createElement("script");
101
+ script.id = scriptId;
102
+ script.src = src;
103
+ script.async = true;
104
+
105
+ const attrs = toDataAttributes(dataProps);
106
+ for (const [name, value] of Object.entries(attrs)) {
107
+ script.setAttribute(name, value);
108
+ }
109
+
110
+ if (dataProps.dataAttributes) {
111
+ for (const [name, value] of Object.entries(dataProps.dataAttributes)) {
112
+ if (value === undefined || value === null) continue;
113
+ script.setAttribute(toKebabDataKey(name), String(value));
114
+ }
115
+ }
116
+
117
+ const handleLoad = () => onLoad?.();
118
+ const handleError = (event: Event) => onError?.(event);
119
+
120
+ script.addEventListener("load", handleLoad);
121
+ script.addEventListener("error", handleError);
122
+ document.body.appendChild(script);
123
+
124
+ return () => {
125
+ script.removeEventListener("load", handleLoad);
126
+ script.removeEventListener("error", handleError);
127
+ script.remove();
128
+ if (removeWidgetOnUnmount) {
129
+ removeExistingWidgetNodes();
130
+ }
131
+ };
132
+ }, [src, enabled, scriptId, removeExistingOnMount, removeWidgetOnUnmount, attrsSignature, onLoad, onError]);
133
+
134
+ return null;
135
+ }
136
+
137
+ export default ChatbotEmbed;
@@ -0,0 +1,18 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2020",
4
+ "module": "ESNext",
5
+ "moduleResolution": "Bundler",
6
+ "jsx": "react-jsx",
7
+ "strict": true,
8
+ "declaration": true,
9
+ "emitDeclarationOnly": false,
10
+ "esModuleInterop": true,
11
+ "skipLibCheck": true,
12
+ "forceConsistentCasingInFileNames": true,
13
+ "resolveJsonModule": true,
14
+ "allowSyntheticDefaultImports": true,
15
+ "noEmit": true
16
+ },
17
+ "include": ["src"]
18
+ }