ai-chat-websdk 1.1.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.
Files changed (2) hide show
  1. package/README.md +180 -0
  2. package/package.json +47 -0
package/README.md ADDED
@@ -0,0 +1,180 @@
1
+ # @sksharma72000/ai-chat-websdk
2
+
3
+ An embeddable, fully themeable AI chat widget for web applications.
4
+ Drop it into any page with a single `initialize()` call — no framework required.
5
+
6
+ ## Features
7
+
8
+ - 🔐 **JWT Authentication** — pass a token on init, update it at any time, or auto-refresh on 401
9
+ - 🎨 **Fully Themeable** — colors, fonts, border-radius via CSS custom properties
10
+ - 📊 **Rich Content** — tables, code blocks, markdown, clarification flows & execution plans
11
+ - 📝 **Chat History** — loads and replays earlier conversations automatically
12
+ - ⚡ **Edit & Retry** — edit previous messages and regenerate responses inline
13
+ - 🔴 **Connection Monitor** — live online/offline indicator
14
+
15
+ ## Installation
16
+
17
+ ```bash
18
+ npm install @sksharma72000/ai-chat-websdk
19
+ ```
20
+
21
+ > **Peer dependencies** — React 18 and ReactDOM 18 must be present in your host application (Option A only).
22
+
23
+ ## Quick Start
24
+
25
+ ### Option A — npm (React / Vue / Angular / any bundler)
26
+
27
+ ```bash
28
+ npm install @sksharma72000/ai-chat-websdk react react-dom
29
+ ```
30
+
31
+ ```ts
32
+ import AIChatSDK from '@sksharma72000/ai-chat-websdk';
33
+ import '@sksharma72000/ai-chat-websdk/style.css';
34
+
35
+ // 1. Mount the widget
36
+ AIChatSDK.initialize({
37
+ // Required
38
+ apiUrl: 'https://api.example.com',
39
+
40
+ // Auth — omit for public endpoints
41
+ jwtToken: 'Bearer eyJ...',
42
+
43
+ // Auto-refresh token on 401 — SDK retries the failed request automatically
44
+ onTokenExpired: async () => {
45
+ const res = await fetch('/auth/refresh', { method: 'POST' });
46
+ const { token } = await res.json();
47
+ return token; // 'Bearer ' prefix is optional — the SDK normalises it
48
+ },
49
+ onBeforeMessageSend:(data)=>{
50
+ return data
51
+ }
52
+ // Branding
53
+ title: 'Support Chat',
54
+ logo: 'https://example.com/logo.png',
55
+ welcomeMessage: '## 👋 Hi there!\nHow can I help you today?',
56
+
57
+ // Pre-fill input on first open (user can edit or send immediately)
58
+ initialMessage: 'Show me the latest reports',
59
+
60
+ // Theme — all fields optional, defaults to indigo/violet palette
61
+ theme: {
62
+ primaryColor: '#6366f1', // accent: buttons, header gradient start
63
+ secondaryColor: '#8b5cf6', // header gradient end
64
+ backgroundColor: '#f8fafc', // chat message area background
65
+ fontFamily: "'Inter', sans-serif",
66
+ borderRadius: '12px', // message bubble corner radius
67
+ userBubbleColor: '#6366f1', // user message bubble fill
68
+ userBubbleTextColor: '#ffffff', // user message bubble text
69
+ },
70
+
71
+ // Mount target — CSS selector or DOM element.
72
+ // Omit to render as a floating FAB in the bottom-right corner.
73
+ container: '#chat-widget-root',
74
+ });
75
+
76
+ // 2. Update token after silent refresh / login
77
+ AIChatSDK.setToken('Bearer eyJnew...');
78
+
79
+ // 3. Tear down on logout / page unload
80
+ AIChatSDK.destroy();
81
+ ```
82
+
83
+ ### Option B — Script Tag / CDN (No React, No npm, No bundler)
84
+
85
+ The **standalone bundle** has React baked in — drop two tags into any HTML page and you're done.
86
+
87
+ ```html
88
+ <!-- Styles (shared with all bundle variants) -->
89
+ <link rel="stylesheet" href="https://unpkg.com/@sksharma72000/ai-chat-websdk/style.css" />
90
+ <!-- Standalone bundle (React included) -->
91
+ <script src="https://unpkg.com/@sksharma72000/ai-chat-websdk/ai-chat-websdk.standalone.js"></script>
92
+
93
+ <script>
94
+ AIChatSDK.initialize({
95
+ apiUrl: 'https://api.example.com',
96
+ jwtToken: 'Bearer eyJ...',
97
+
98
+ onTokenExpired: async () => {
99
+ const res = await fetch('/auth/refresh', { method: 'POST' });
100
+ const { token } = await res.json();
101
+ return token;
102
+ },
103
+
104
+ title: 'Support Chat',
105
+ theme: {
106
+ primaryColor: '#6366f1',
107
+ secondaryColor: '#8b5cf6',
108
+ },
109
+ });
110
+
111
+ // Update token later
112
+ AIChatSDK.setToken('Bearer new-token');
113
+
114
+ // Remove widget
115
+ AIChatSDK.destroy();
116
+ </script>
117
+ ```
118
+
119
+ > The standalone bundle is larger (~2 MB unminified) because React is included.
120
+ > For production React projects use **Option A** to avoid shipping React twice.
121
+
122
+ ### Build outputs
123
+
124
+ | File | Use case |
125
+ |---|---|
126
+ | `ai-chat-websdk.es.js` | ES module — React apps, bundlers (Vite, webpack, etc.) |
127
+ | `ai-chat-websdk.umd.js` | CommonJS / UMD — Node SSR, older bundlers |
128
+ | `ai-chat-websdk.standalone.js` | IIFE — plain HTML, CMS, no-framework pages |
129
+ | `style.css` | Stylesheet for all bundle variants |
130
+
131
+ ## Configuration Reference
132
+ |---|---|---|---|
133
+ | `apiUrl` | `string` | **required** | Base URL of the AI server |
134
+ | `jwtToken` | `string` | — | JWT token (with or without `Bearer ` prefix) |
135
+ | `onTokenExpired` | `() => Promise<string>` | — | Called on 401; return new token; SDK retries automatically |
136
+ | `onBeforeMessageSend` | `(body: any) => Promise<any> \| any` | — | Called with the outgoing request body before it's JSON-stringified; may modify and return the body. |
137
+ | `title` | `string` | `'AI Assistant'` | Header title |
138
+ | `logo` | `string` | — | URL or base64 of logo image |
139
+ | `welcomeMessage` | `string` | — | Markdown string shown as the first AI message |
140
+ | `initialMessage` | `string` | — | Pre-fills the input when the widget first opens |
141
+ | `container` | `string \| HTMLElement` | `document.body` | Mount target; omit for floating FAB |
142
+ | `theme` | `SDKTheme` | — | See theme options below |
143
+
144
+ ### Theme Options
145
+
146
+ | Option | Default | Description |
147
+ |---|---|---|
148
+ | `primaryColor` | `#6366f1` | Accent color — buttons, header gradient start |
149
+ | `secondaryColor` | `#8b5cf6` | Header gradient end |
150
+ | `backgroundColor` | `#f8fafc` | Chat message area background |
151
+ | `fontFamily` | system-ui | CSS font-family string |
152
+ | `borderRadius` | `12px` | Message bubble corner radius |
153
+ | `userBubbleColor` | `#6366f1` | User message bubble fill |
154
+ | `userBubbleTextColor` | `#ffffff` | User message bubble text |
155
+
156
+ ## React Usage
157
+
158
+ ```tsx
159
+ import { ChatWindow } from '@sksharma72000/ai-chat-websdk';
160
+ import '@sksharma72000/ai-chat-websdk/style.css';
161
+
162
+ function App() {
163
+ return (
164
+ <ChatWindow config={{
165
+ apiUrl: 'https://api.example.com',
166
+ jwtToken: token,
167
+ title: 'Support Chat',
168
+ }} />
169
+ );
170
+ }
171
+ ```
172
+
173
+ ## Chat Mode
174
+
175
+ The chat mode (FAQ / client / database) is determined **server-side** via the JWT claims.
176
+ No manual mode selector is exposed — the server routes queries automatically based on the token.
177
+
178
+ ## License
179
+
180
+ MIT © Shree Krishna Acharya
package/package.json ADDED
@@ -0,0 +1,47 @@
1
+ {
2
+ "name": "ai-chat-websdk",
3
+ "version": "1.1.0",
4
+ "description": "Embeddable AI Chat WebSDK — configurable widget with JWT auth, theming, and rich content rendering",
5
+ "type": "module",
6
+ "main": "dist/ai-chat-websdk.umd.js",
7
+ "module": "dist/ai-chat-websdk.es.js",
8
+ "types": "dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "import": "./dist/ai-chat-websdk.es.js",
12
+ "require": "./dist/ai-chat-websdk.umd.js"
13
+ }
14
+ },
15
+ "files": ["dist"],
16
+ "scripts": {
17
+ "dev": "vite",
18
+ "build": "tsc && vite build",
19
+ "build:standalone": "vite build --config vite.standalone.config.ts",
20
+ "build:all": "npm run build && npm run build:standalone",
21
+ "build:watch": "vite build --watch",
22
+ "preview": "vite preview",
23
+ "type-check": "tsc --noEmit",
24
+ "publish:prepare": "npm run build:all && node scripts/prepare-publish.js",
25
+ "publish:npm": "npm run publish:prepare && cd publish && npm publish --access public"
26
+ },
27
+ "peerDependencies": {
28
+ "react": "^18.0.0",
29
+ "react-dom": "^18.0.0"
30
+ },
31
+ "dependencies": {
32
+ "react-markdown": "^9.0.1",
33
+ "react-syntax-highlighter": "^15.5.0",
34
+ "@tanstack/react-table": "^8.10.7"
35
+ },
36
+ "devDependencies": {
37
+ "@types/react": "^18.2.0",
38
+ "@types/react-dom": "^18.2.0",
39
+ "@types/react-syntax-highlighter": "^15.5.11",
40
+ "@vitejs/plugin-react": "^4.2.1",
41
+ "typescript": "^5.3.3",
42
+ "vite": "^5.2.0",
43
+ "vite-plugin-dts": "^3.7.3",
44
+ "react": "^18.2.0",
45
+ "react-dom": "^18.2.0"
46
+ }
47
+ }