@courseecho/ai-widget-jquery 1.0.0 → 1.0.3

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/LICENSE ADDED
@@ -0,0 +1,25 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 CourseEcho
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
23
+ ---
24
+
25
+ For more information about CourseEcho, visit https://courseecho.com
package/README.md ADDED
@@ -0,0 +1,396 @@
1
+ # 📜 @courseecho/ai-widget-jquery
2
+
3
+ **jQuery plugin for AI chat widget** - Script tag integration for vanilla JavaScript and legacy projects.
4
+
5
+ [![npm version](https://img.shields.io/npm/v/@courseecho/ai-widget-jquery.svg)](https://www.npmjs.com/package/@courseecho/ai-widget-jquery)
6
+ [![CDN](https://img.shields.io/badge/CDN-unpkg-blue)](https://unpkg.com/@courseecho/ai-widget-jquery)
7
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](LICENSE)
8
+
9
+ ## 📖 Overview
10
+
11
+ A **jQuery plugin** for integrating CourseEcho AI chat into vanilla JavaScript, HTML, WordPress, and legacy projects.
12
+
13
+ - ✅ jQuery 1.0+ compatible
14
+ - ✅ Works with script tags (no build required)
15
+ - ✅ Available on CDN (unpkg, jsDelivr)
16
+ - ✅ 10KB minified
17
+ - ✅ Zero build dependencies
18
+
19
+ ## 🚀 Quick Start
20
+
21
+ ### Option 1: Script Tag (Easiest)
22
+
23
+ ```html
24
+ <!DOCTYPE html>
25
+ <html>
26
+ <head>
27
+ <!-- jQuery -->
28
+ <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
29
+
30
+ <!-- AI Widget -->
31
+ <script src="https://unpkg.com/@courseecho/ai-widget-jquery@1.0.1/dist/ai-widget-jquery.js"></script>
32
+ </head>
33
+ <body>
34
+ <div id="widget"></div>
35
+
36
+ <script>
37
+ $(document).ready(function() {
38
+ $('#widget').aiChatWidget({
39
+ apiEndpoint: 'https://api.courseecho.com/api',
40
+ context: {
41
+ pageType: 'course',
42
+ entityId: 'course-123',
43
+ userId: 'user-456'
44
+ },
45
+ jwtToken: 'your-token-here'
46
+ });
47
+ });
48
+ </script>
49
+ </body>
50
+ </html>
51
+ ```
52
+
53
+ ### Option 2: npm + Bundler
54
+
55
+ ```bash
56
+ npm install @courseecho/ai-widget-jquery
57
+ ```
58
+
59
+ ```javascript
60
+ import jQuery from 'jquery';
61
+ import { createJQueryPlugin } from '@courseecho/ai-widget-jquery';
62
+
63
+ const $ = jQuery;
64
+ createJQueryPlugin($);
65
+
66
+ $('#widget').aiChatWidget({ /* options */ });
67
+ ```
68
+
69
+ ## 📦 CDN URLs
70
+
71
+ **CourseEcho CDN (Recommended):**
72
+ ```html
73
+ <script src="https://cdn.courseecho.com/ai-widget-jquery.js"></script>
74
+ ```
75
+
76
+ **unpkg (Alternative):**
77
+ ```html
78
+ <script src="https://unpkg.com/@courseecho/ai-widget-jquery/dist/ai-widget-jquery.js"></script>
79
+ ```
80
+
81
+ **jsDelivr (Alternative):**
82
+ ```html
83
+ <script src="https://cdn.jsdelivr.net/npm/@courseecho/ai-widget-jquery/dist/ai-widget-jquery.js"></script>
84
+ ```
85
+
86
+ ## 🎯 API Methods
87
+
88
+ ### Initialization
89
+
90
+ ```javascript
91
+ $('#container').aiChatWidget({
92
+ apiEndpoint: 'https://api.courseecho.com/api',
93
+ context: {
94
+ pageType: 'course',
95
+ entityId: '123',
96
+ userId: 'user@example.com'
97
+ },
98
+ jwtToken: 'your-jwt-token',
99
+ apiKey: 'alternative-api-key',
100
+ theme: 'light', // or 'dark'
101
+ position: 'bottom-right', // or 'bottom-left', 'top-left', 'top-right'
102
+ onMessage: function(msg) { },
103
+ onError: function(err) { },
104
+ onLoading: function(isLoading) { }
105
+ });
106
+ ```
107
+
108
+ ### Send Message
109
+
110
+ ```javascript
111
+ $('#widget').aiChatWidget('send', 'Hello, ask me anything!');
112
+ ```
113
+
114
+ ### Get Messages
115
+
116
+ ```javascript
117
+ const messages = $('#widget').aiChatWidget('getMessages');
118
+ console.log(messages);
119
+ ```
120
+
121
+ ### Get Loading State
122
+
123
+ ```javascript
124
+ const isLoading = $('#widget').aiChatWidget('isLoading');
125
+ if (isLoading) {
126
+ console.log('Waiting for response...');
127
+ }
128
+ ```
129
+
130
+ ### Set Context
131
+
132
+ ```javascript
133
+ $('#widget').aiChatWidget('setContext', {
134
+ userId: 'new-user-id',
135
+ courseId: 'new-course-id'
136
+ });
137
+ ```
138
+
139
+ ### Clear History
140
+
141
+ ```javascript
142
+ $('#widget').aiChatWidget('clear');
143
+ ```
144
+
145
+ ### Destroy
146
+
147
+ ```javascript
148
+ $('#widget').aiChatWidget('destroy');
149
+ ```
150
+
151
+ ## 💡 Examples
152
+
153
+ ### Example 1: Static HTML Page
154
+
155
+ ```html
156
+ <!DOCTYPE html>
157
+ <html lang="en">
158
+ <head>
159
+ <meta charset="UTF-8">
160
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
161
+ <title>Course Page</title>
162
+ <style>
163
+ body { font-family: Arial, sans-serif; }
164
+ .content { max-width: 1000px; margin: 0 auto; padding: 20px; }
165
+ </style>
166
+ </head>
167
+ <body>
168
+ <div class="content">
169
+ <h1>Learn JavaScript</h1>
170
+ <p>Welcome to our JavaScript course...</p>
171
+ </div>
172
+
173
+ <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
174
+ <script src="https://unpkg.com/@courseecho/ai-widget-jquery@1.0.1/dist/ai-widget-jquery.js"></script>
175
+
176
+ <script>
177
+ $(function() {
178
+ $('#ai-widget').aiChatWidget({
179
+ apiEndpoint: 'https://api.courseecho.com/api',
180
+ context: {
181
+ pageType: 'course',
182
+ entityId: 'js-101',
183
+ userId: 'student-123'
184
+ },
185
+ jwtToken: localStorage.getItem('token'),
186
+ theme: 'light',
187
+ position: 'bottom-right'
188
+ });
189
+ });
190
+ </script>
191
+
192
+ <!-- Widget container -->
193
+ <div id="ai-widget"></div>
194
+ </body>
195
+ </html>
196
+ ```
197
+
198
+ ### Example 2: WordPress Plugin
199
+
200
+ Add to your WordPress theme's `functions.php`:
201
+
202
+ ```php
203
+ <?php
204
+ add_action('wp_footer', function() {
205
+ if (is_single() && get_post_type() === 'course') {
206
+ ?>
207
+ <div id="course-ai-widget"></div>
208
+
209
+ <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
210
+ <script src="https://unpkg.com/@courseecho/ai-widget-jquery@1.0.1/dist/ai-widget-jquery.js"></script>
211
+
212
+ <script>
213
+ jQuery(function($) {
214
+ $('#course-ai-widget').aiChatWidget({
215
+ apiEndpoint: '<?php echo get_option("ai_api_endpoint"); ?>',
216
+ context: {
217
+ pageType: 'course',
218
+ entityId: '<?php echo get_the_ID(); ?>',
219
+ userId: '<?php echo get_current_user_id(); ?>'
220
+ },
221
+ jwtToken: '<?php echo get_user_meta(get_current_user_id(), "jwt_token", true); ?>'
222
+ });
223
+ });
224
+ </script>
225
+ <?php
226
+ }
227
+ });
228
+ ```
229
+
230
+ ### Example 3: Multiple Widgets
231
+
232
+ ```html
233
+ <button onclick="toggleWidget(1)">Chat about Course 1</button>
234
+ <div id="widget-1"></div>
235
+
236
+ <button onclick="toggleWidget(2)">Chat about Course 2</button>
237
+ <div id="widget-2"></div>
238
+
239
+ <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
240
+ <script src="https://unpkg.com/@courseecho/ai-widget-jquery@1.0.1/dist/ai-widget-jquery.js"></script>
241
+
242
+ <script>
243
+ function toggleWidget(courseId) {
244
+ const widgetId = '#widget-' + courseId;
245
+
246
+ $(widgetId).aiChatWidget({
247
+ apiEndpoint: 'https://api.courseecho.com/api',
248
+ context: {
249
+ pageType: 'course',
250
+ entityId: 'course-' + courseId,
251
+ userId: getUserId()
252
+ },
253
+ jwtToken: getToken()
254
+ });
255
+ }
256
+ </script>
257
+ ```
258
+
259
+ ### Example 4: Real-time User Tracking
260
+
261
+ ```html
262
+ <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
263
+ <script src="https://unpkg.com/@courseecho/ai-widget-jquery@1.0.1/dist/ai-widget-jquery.js"></script>
264
+
265
+ <script>
266
+ $(function() {
267
+ const $widget = $('#ai-widget');
268
+
269
+ // Initialize
270
+ $widget.aiChatWidget({
271
+ apiEndpoint: 'https://api.courseecho.com/api',
272
+ context: {
273
+ pageType: getCurrentPage(),
274
+ entityId: getCurrentEntityId(),
275
+ userId: getCurrentUserId()
276
+ },
277
+ jwtToken: getToken()
278
+ });
279
+
280
+ // Update context when page changes
281
+ $(document).on('pagechange', function(e) {
282
+ $widget.aiChatWidget('setContext', {
283
+ pageType: e.pageType,
284
+ entityId: e.entityId
285
+ });
286
+ });
287
+
288
+ // Handle messages
289
+ $widget.on('message', function(e, message) {
290
+ console.log('User sent:', message);
291
+ logToAnalytics('ai_chat_message', { message });
292
+ });
293
+
294
+ // Handle errors
295
+ $widget.on('error', function(e, error) {
296
+ console.error('Chat error:', error);
297
+ showErrorNotification(error);
298
+ });
299
+ });
300
+ </script>
301
+ ```
302
+
303
+ ## 🔐 Authentication
304
+
305
+ ### JWT Token
306
+
307
+ ```javascript
308
+ $('#widget').aiChatWidget({
309
+ apiEndpoint: 'https://api.courseecho.com/api',
310
+ context: { pageType: 'course' },
311
+ jwtToken: 'eyJhbGciOiJIUzI1NiIs...'
312
+ });
313
+ ```
314
+
315
+ ### API Key
316
+
317
+ ```javascript
318
+ $('#widget').aiChatWidget({
319
+ apiEndpoint: 'https://api.courseecho.com/api',
320
+ context: { pageType: 'course' },
321
+ apiKey: 'sk-123456789...'
322
+ });
323
+ ```
324
+
325
+ ## 🎨 Styling
326
+
327
+ Default styles are included. Override with CSS:
328
+
329
+ ```css
330
+ .ai-chat-widget {
331
+ border-radius: 8px;
332
+ box-shadow: 0 2px 8px rgba(0,0,0,0.1);
333
+ }
334
+
335
+ .ai-chat-message.user {
336
+ background: #3b82f6;
337
+ color: white;
338
+ }
339
+
340
+ .ai-chat-message.assistant {
341
+ background: #f3f4f6;
342
+ color: #1f2937;
343
+ }
344
+
345
+ .ai-chat-input {
346
+ border: 1px solid #e5e7eb;
347
+ border-radius: 4px;
348
+ padding: 8px;
349
+ }
350
+ ```
351
+
352
+ ## 📊 Data Export
353
+
354
+ ```javascript
355
+ // Get all messages
356
+ const messages = $('#widget').aiChatWidget('getMessages');
357
+
358
+ // Export as JSON
359
+ const json = JSON.stringify(messages);
360
+
361
+ // Save to file
362
+ const blob = new Blob([json], { type: 'application/json' });
363
+ const url = URL.createObjectURL(blob);
364
+ const a = document.createElement('a');
365
+ a.href = url;
366
+ a.download = 'chat-history.json';
367
+ a.click();
368
+ ```
369
+
370
+ ## 🌍 About CourseEcho
371
+
372
+ [CourseEcho](https://courseecho.com) - Intelligent education powered by AI.
373
+
374
+ - 🌐 Website: https://courseecho.com
375
+ - 📧 Support: support@courseecho.com
376
+ - 📱 Product: https://courseecho.com/products/ai-widget
377
+
378
+ ## 📄 License
379
+
380
+ MIT © 2026 CourseEcho
381
+
382
+ ## 📞 Support
383
+
384
+ - 📖 Docs: https://courseecho.com/docs
385
+ - 🐛 Issues: [GitHub Issues](https://github.com/courseecho/ai-widget-sdk/issues)
386
+ - 💬 Chat: [Discord](https://discord.gg/courseecho)
387
+ - 📚 Full Guide: [Script Tag Integration](https://github.com/courseecho/ai-widget-sdk/blob/main/SCRIPT_TAG_USAGE.html)
388
+
389
+ ---
390
+
391
+ **Other frameworks:**
392
+ - [React](https://www.npmjs.com/package/@courseecho/ai-widget-react)
393
+ - [Vue 3](https://www.npmjs.com/package/@courseecho/ai-widget-vue)
394
+ - [Angular 19+](https://www.npmjs.com/package/@courseecho/ai-widget-angular)
395
+ - [Node.js](https://www.npmjs.com/package/@courseecho/ai-client-node)
396
+ - [Core SDK](https://www.npmjs.com/package/@courseecho/ai-core-sdk)
@@ -0,0 +1,27 @@
1
+ /**
2
+ * AI Chat Widget - jQuery Plugin
3
+ * Usage: $('#container').aiChatWidget(options)
4
+ */
5
+ export interface AiWidgetOptions {
6
+ apiEndpoint: string;
7
+ context: {
8
+ pageType: string;
9
+ entityId?: string;
10
+ userId?: string;
11
+ customData?: Record<string, any>;
12
+ };
13
+ jwtToken?: string;
14
+ apiKey?: string;
15
+ theme?: 'light' | 'dark';
16
+ position?: 'bottom-right' | 'bottom-left' | 'top-right' | 'top-left';
17
+ onMessage?: (message: any) => void;
18
+ onError?: (error: string) => void;
19
+ onLoading?: (isLoading: boolean) => void;
20
+ }
21
+ declare global {
22
+ interface JQuery {
23
+ aiChatWidget(options?: AiWidgetOptions | string, arg?: any): JQuery;
24
+ }
25
+ }
26
+ export declare function createJQueryPlugin(jQuery: any): void;
27
+ //# sourceMappingURL=ai-widget-jquery.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ai-widget-jquery.d.ts","sourceRoot":"","sources":["../ai-widget-jquery.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,MAAM,WAAW,eAAe;IAC9B,WAAW,EAAE,MAAM,CAAC;IACpB,OAAO,EAAE;QACP,QAAQ,EAAE,MAAM,CAAC;QACjB,QAAQ,CAAC,EAAE,MAAM,CAAC;QAClB,MAAM,CAAC,EAAE,MAAM,CAAC;QAChB,UAAU,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;KAClC,CAAC;IACF,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,KAAK,CAAC,EAAE,OAAO,GAAG,MAAM,CAAC;IACzB,QAAQ,CAAC,EAAE,cAAc,GAAG,aAAa,GAAG,WAAW,GAAG,UAAU,CAAC;IACrE,SAAS,CAAC,EAAE,CAAC,OAAO,EAAE,GAAG,KAAK,IAAI,CAAC;IACnC,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;IAClC,SAAS,CAAC,EAAE,CAAC,SAAS,EAAE,OAAO,KAAK,IAAI,CAAC;CAC1C;AAED,OAAO,CAAC,MAAM,CAAC;IACb,UAAU,MAAM;QACd,YAAY,CAAC,OAAO,CAAC,EAAE,eAAe,GAAG,MAAM,EAAE,GAAG,CAAC,EAAE,GAAG,GAAG,MAAM,CAAC;KACrE;CACF;AAED,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,GAAG,QA8E7C"}
@@ -0,0 +1,260 @@
1
+ /**
2
+ * AI Chat Widget - jQuery Plugin
3
+ * Usage: $('#container').aiChatWidget(options)
4
+ */
5
+ export function createJQueryPlugin(jQuery) {
6
+ const methods = {
7
+ init(element, options) {
8
+ const $elem = jQuery(element);
9
+ const instance = new AiWidgetInstance(element, options);
10
+ $elem.data('aiWidget', instance);
11
+ return $elem;
12
+ },
13
+ send(element, message) {
14
+ const instance = jQuery(element).data('aiWidget');
15
+ if (instance) {
16
+ return instance.sendMessage(message);
17
+ }
18
+ },
19
+ getMessages(element) {
20
+ const instance = jQuery(element).data('aiWidget');
21
+ if (instance) {
22
+ return instance.getMessages();
23
+ }
24
+ return [];
25
+ },
26
+ clear(element) {
27
+ const instance = jQuery(element).data('aiWidget');
28
+ if (instance) {
29
+ instance.clearMessages();
30
+ }
31
+ return jQuery(element);
32
+ },
33
+ setContext(element, context) {
34
+ const instance = jQuery(element).data('aiWidget');
35
+ if (instance) {
36
+ instance.updateContext(context);
37
+ }
38
+ return jQuery(element);
39
+ },
40
+ isLoading(element) {
41
+ const instance = jQuery(element).data('aiWidget');
42
+ if (instance) {
43
+ return instance.isLoadingState();
44
+ }
45
+ return false;
46
+ },
47
+ destroy(element) {
48
+ const instance = jQuery(element).data('aiWidget');
49
+ if (instance) {
50
+ instance.destroy();
51
+ }
52
+ jQuery(element).removeData('aiWidget').empty();
53
+ return jQuery(element);
54
+ },
55
+ };
56
+ jQuery.fn.aiChatWidget = function (...args) {
57
+ const methodOrOptions = args[0];
58
+ const additionalArgs = args.slice(1);
59
+ if (methodOrOptions === undefined || typeof methodOrOptions === 'object') {
60
+ // Init
61
+ return this.each(function () {
62
+ if (!jQuery(this).data('aiWidget')) {
63
+ methods.init(this, methodOrOptions || {});
64
+ }
65
+ });
66
+ }
67
+ else if (methods[methodOrOptions]) {
68
+ // Method call
69
+ return this.each(function () {
70
+ methods[methodOrOptions](this, ...additionalArgs);
71
+ });
72
+ }
73
+ return this;
74
+ };
75
+ }
76
+ class AiWidgetInstance {
77
+ constructor(element, options) {
78
+ this.messages = [];
79
+ this.isOpen = false;
80
+ this.isLoading = false;
81
+ this.element = element;
82
+ this.options = {
83
+ theme: 'light',
84
+ position: 'bottom-right',
85
+ ...options,
86
+ };
87
+ this.initialize();
88
+ }
89
+ initialize() {
90
+ const $ = window.$;
91
+ const $elem = $(this.element);
92
+ $elem.addClass(`ai-widget-container ai-widget-${this.options.theme} ai-widget-${this.options.position}`);
93
+ // Create toggle button
94
+ this.$button = $(`
95
+ <button class="ai-widget-toggle-button" aria-label="Open AI Chat">
96
+ <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
97
+ <path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z" />
98
+ </svg>
99
+ </button>
100
+ `);
101
+ // Create widget window
102
+ this.$widget = $(`
103
+ <div class="ai-widget-window" style="display: none;">
104
+ <div class="ai-widget-header">
105
+ <h3>AI Assistant</h3>
106
+ <button class="ai-widget-close-button">×</button>
107
+ </div>
108
+ <div class="ai-widget-messages">
109
+ <div class="ai-widget-welcome">
110
+ <p>👋 Hello! I'm your AI assistant.</p>
111
+ <p>How can I help you today?</p>
112
+ </div>
113
+ </div>
114
+ <form class="ai-widget-form">
115
+ <input type="text" class="ai-widget-input" placeholder="Type your message..." />
116
+ <button type="submit" class="ai-widget-send-button">→</button>
117
+ </form>
118
+ </div>
119
+ `);
120
+ this.$messages = this.$widget.find('.ai-widget-messages');
121
+ this.$input = this.$widget.find('.ai-widget-input');
122
+ // Event handlers
123
+ this.$button.on('click', () => this.open());
124
+ this.$widget.find('.ai-widget-close-button').on('click', () => this.close());
125
+ this.$widget.find('.ai-widget-form').on('submit', (e) => {
126
+ e.preventDefault();
127
+ this.sendMessage(this.$input.val());
128
+ this.$input.val('');
129
+ });
130
+ // Append to element
131
+ $(this.element).append(this.$button).append(this.$widget);
132
+ }
133
+ open() {
134
+ this.isOpen = true;
135
+ window.$(this.element)
136
+ .find('.ai-widget-button')
137
+ .hide();
138
+ window.$(this.element)
139
+ .find('.ai-widget-window')
140
+ .show();
141
+ }
142
+ close() {
143
+ this.isOpen = false;
144
+ window.$(this.element)
145
+ .find('.ai-widget-button')
146
+ .show();
147
+ window.$(this.element)
148
+ .find('.ai-widget-window')
149
+ .hide();
150
+ }
151
+ async sendMessage(message) {
152
+ if (!message.trim() || this.isLoading)
153
+ return;
154
+ // Add user message
155
+ const userMsg = {
156
+ id: `${Date.now()}-${Math.random()}`,
157
+ role: 'user',
158
+ content: message,
159
+ timestamp: new Date(),
160
+ };
161
+ this.messages.push(userMsg);
162
+ this.appendMessageToUI(userMsg);
163
+ // Set loading
164
+ this.isLoadingState(true);
165
+ if (this.options.onLoading) {
166
+ this.options.onLoading(true);
167
+ }
168
+ try {
169
+ // Call backend
170
+ const response = await window.fetch(`${this.options.apiEndpoint}/api/aiorchestrator/query`, {
171
+ method: 'POST',
172
+ headers: {
173
+ 'Content-Type': 'application/json',
174
+ Authorization: `Bearer ${this.options.jwtToken || ''}`,
175
+ },
176
+ body: JSON.stringify({
177
+ userQuery: message,
178
+ pageType: this.options.context.pageType,
179
+ entityId: this.options.context.entityId,
180
+ contextData: this.options.context.customData,
181
+ }),
182
+ });
183
+ if (!response.ok) {
184
+ throw new Error(`HTTP ${response.status}`);
185
+ }
186
+ const data = await response.json();
187
+ // Add assistant message
188
+ const assistantMsg = {
189
+ id: data.sessionId,
190
+ role: 'assistant',
191
+ content: data.response,
192
+ timestamp: new Date(data.timestamp),
193
+ };
194
+ this.messages.push(assistantMsg);
195
+ this.appendMessageToUI(assistantMsg);
196
+ if (this.options.onMessage) {
197
+ this.options.onMessage(assistantMsg);
198
+ }
199
+ }
200
+ catch (error) {
201
+ const errorText = String(error);
202
+ if (this.options.onError) {
203
+ this.options.onError(errorText);
204
+ }
205
+ }
206
+ finally {
207
+ this.isLoadingState(false);
208
+ if (this.options.onLoading) {
209
+ this.options.onLoading(false);
210
+ }
211
+ }
212
+ }
213
+ appendMessageToUI(message) {
214
+ const $ = window.$;
215
+ const messageEl = $(`
216
+ <div class="ai-widget-message ai-widget-message-${message.role}">
217
+ <div class="ai-widget-message-content">${message.content}</div>
218
+ <div class="ai-widget-message-time">
219
+ ${new Date(message.timestamp).toLocaleTimeString([], {
220
+ hour: '2-digit',
221
+ minute: '2-digit',
222
+ })}
223
+ </div>
224
+ </div>
225
+ `);
226
+ if (this.$messages.find('.ai-widget-welcome').length) {
227
+ this.$messages.find('.ai-widget-welcome').remove();
228
+ }
229
+ this.$messages.append(messageEl);
230
+ this.$messages.scrollTop(this.$messages.prop('scrollHeight'));
231
+ }
232
+ getMessages() {
233
+ return [...this.messages];
234
+ }
235
+ clearMessages() {
236
+ this.messages = [];
237
+ const $ = window.$;
238
+ this.$messages.html(`
239
+ <div class="ai-widget-welcome">
240
+ <p>👋 Hello! I'm your AI assistant.</p>
241
+ <p>How can I help you today?</p>
242
+ </div>
243
+ `);
244
+ }
245
+ updateContext(context) {
246
+ this.options.context = { ...this.options.context, ...context };
247
+ }
248
+ isLoadingState(value) {
249
+ if (value !== undefined) {
250
+ this.isLoading = value;
251
+ }
252
+ return this.isLoading;
253
+ }
254
+ destroy() {
255
+ this.$button.off('click');
256
+ this.$widget.find('.ai-widget-close-button').off('click');
257
+ this.$widget.find('.ai-widget-form').off('submit');
258
+ }
259
+ }
260
+ //# sourceMappingURL=ai-widget-jquery.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ai-widget-jquery.js","sourceRoot":"","sources":["../ai-widget-jquery.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAyBH,MAAM,UAAU,kBAAkB,CAAC,MAAW;IAC5C,MAAM,OAAO,GAA6B;QACxC,IAAI,CAAC,OAAY,EAAE,OAAwB;YACzC,MAAM,KAAK,GAAG,MAAM,CAAC,OAAO,CAAC,CAAC;YAC9B,MAAM,QAAQ,GAAG,IAAI,gBAAgB,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;YACxD,KAAK,CAAC,IAAI,CAAC,UAAU,EAAE,QAAQ,CAAC,CAAC;YACjC,OAAO,KAAK,CAAC;QACf,CAAC;QAED,IAAI,CAAC,OAAY,EAAE,OAAe;YAChC,MAAM,QAAQ,GAAG,MAAM,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;YAClD,IAAI,QAAQ,EAAE,CAAC;gBACb,OAAO,QAAQ,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC;YACvC,CAAC;QACH,CAAC;QAED,WAAW,CAAC,OAAY;YACtB,MAAM,QAAQ,GAAG,MAAM,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,UAAU,CAAC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,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC;;;;;KAKnB,CAAC,CAAC;IACL,CAAC;IAED,aAAa,CAAC,OAAY;QACxB,IAAI,CAAC,OAAO,CAAC,OAAO,GAAG,EAAE,GAAG,IAAI,CAAC,OAAO,CAAC,OAAO,EAAE,GAAG,OAAO,EAAE,CAAC;IACjE,CAAC;IAED,cAAc,CAAC,KAAe;QAC5B,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;YACxB,IAAI,CAAC,SAAS,GAAG,KAAK,CAAC;QACzB,CAAC;QACD,OAAO,IAAI,CAAC,SAAS,CAAC;IACxB,CAAC;IAED,OAAO;QACL,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;QAC1B,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,yBAAyB,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;QAC1D,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,iBAAiB,CAAC,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;IACrD,CAAC;CACF"}
package/package.json CHANGED
@@ -1,28 +1,51 @@
1
1
  {
2
- "name": "@courseecho/ai-widget-jquery",
3
- "version": "1.0.0",
4
- "description": "jQuery plugin for AI Chat Widget - enables vanilla JavaScript/HTML integration",
5
- "main": "ai-widget-jquery.js",
6
- "types": "ai-widget-jquery.d.ts",
7
- "scripts": {
8
- "build": "tsc",
9
- "dev": "tsc --watch"
10
- },
11
- "peerDependencies": {
12
- "jquery": ">=1.0.0"
13
- },
14
- "keywords": [
15
- "ai",
16
- "chatbot",
17
- "jquery",
18
- "widget",
19
- "vanilla-js",
20
- "html"
21
- ],
22
- "author": "CourseEcho",
23
- "license": "MIT",
24
- "repository": {
25
- "type": "git",
26
- "url": "https://github.com/courseecho/ai-widget-sdk"
27
- }
2
+ "name": "@courseecho/ai-widget-jquery",
3
+ "version": "1.0.3",
4
+ "description": "jQuery plugin for AI Chat Widget - enables vanilla JavaScript/HTML integration via npm or script tag",
5
+ "main": "dist/ai-widget-jquery.cjs",
6
+ "module": "dist/ai-widget-jquery.mjs",
7
+ "browser": "dist/ai-widget-jquery.umd.js",
8
+ "types": "dist/index.d.ts",
9
+ "unpkg": "dist/ai-widget-jquery.umd.min.js",
10
+ "exports": {
11
+ ".": {
12
+ "import": "./dist/ai-widget-jquery.mjs",
13
+ "require": "./dist/ai-widget-jquery.cjs",
14
+ "browser": "./dist/ai-widget-jquery.umd.js",
15
+ "types": "./dist/index.d.ts"
16
+ }
17
+ },
18
+ "files": [
19
+ "dist",
20
+ "README.md"
21
+ ],
22
+ "scripts": {
23
+ "build": "tsc",
24
+ "build:umd": "tsc \u0026\u0026 npm run esbuild",
25
+ "esbuild": "esbuild dist/index.js --bundle --minify --format=umd --global-name=AiWidgetjQuery --outfile=dist/ai-widget-jquery.umd.min.js",
26
+ "dev": "tsc --watch"
27
+ },
28
+ "peerDependencies": {
29
+ "jquery": "\u003e=1.0.0"
30
+ },
31
+ "devDependencies": {
32
+ "typescript": "^5.3.0",
33
+ "esbuild": "^0.19.0"
34
+ },
35
+ "keywords": [
36
+ "ai",
37
+ "chatbot",
38
+ "jquery",
39
+ "widget",
40
+ "vanilla-js",
41
+ "html",
42
+ "script-tag",
43
+ "cdn"
44
+ ],
45
+ "author": "CourseEcho",
46
+ "license": "MIT",
47
+ "repository": {
48
+ "type": "git",
49
+ "url": "git+https://github.com/courseecho/ai-widget-sdk.git"
50
+ }
28
51
  }
@@ -1,328 +0,0 @@
1
- /**
2
- * AI Chat Widget - jQuery Plugin
3
- * Usage: $('#container').aiChatWidget(options)
4
- */
5
-
6
- export interface AiWidgetOptions {
7
- apiEndpoint: string;
8
- context: {
9
- pageType: string;
10
- entityId?: string;
11
- userId?: string;
12
- customData?: Record<string, any>;
13
- };
14
- jwtToken?: string;
15
- apiKey?: string;
16
- theme?: 'light' | 'dark';
17
- position?: 'bottom-right' | 'bottom-left' | 'top-right' | 'top-left';
18
- onMessage?: (message: any) => void;
19
- onError?: (error: string) => void;
20
- onLoading?: (isLoading: boolean) => void;
21
- }
22
-
23
- declare global {
24
- interface JQuery {
25
- aiChatWidget(options?: AiWidgetOptions | string, arg?: any): JQuery;
26
- }
27
- }
28
-
29
- export function createJQueryPlugin(jQuery: any) {
30
- const methods: Record<string, Function> = {
31
- init(element: any, options: AiWidgetOptions) {
32
- const $elem = jQuery(element);
33
- const instance = new AiWidgetInstance(element, options);
34
- $elem.data('aiWidget', instance);
35
- return $elem;
36
- },
37
-
38
- send(element: any, message: string) {
39
- const instance = jQuery(element).data('aiWidget');
40
- if (instance) {
41
- return instance.sendMessage(message);
42
- }
43
- },
44
-
45
- getMessages(element: any) {
46
- const instance = jQuery(element).data('aiWidget');
47
- if (instance) {
48
- return instance.getMessages();
49
- }
50
- return [];
51
- },
52
-
53
- clear(element: any) {
54
- const instance = jQuery(element).data('aiWidget');
55
- if (instance) {
56
- instance.clearMessages();
57
- }
58
- return jQuery(element);
59
- },
60
-
61
- setContext(element: any, context: any) {
62
- const instance = jQuery(element).data('aiWidget');
63
- if (instance) {
64
- instance.updateContext(context);
65
- }
66
- return jQuery(element);
67
- },
68
-
69
- isLoading(element: any) {
70
- const instance = jQuery(element).data('aiWidget');
71
- if (instance) {
72
- return instance.isLoadingState();
73
- }
74
- return false;
75
- },
76
-
77
- destroy(element: any) {
78
- const instance = jQuery(element).data('aiWidget');
79
- if (instance) {
80
- instance.destroy();
81
- }
82
- jQuery(element).removeData('aiWidget').empty();
83
- return jQuery(element);
84
- },
85
- };
86
-
87
- jQuery.fn.aiChatWidget = function (...args: any[]) {
88
- const methodOrOptions = args[0];
89
- const additionalArgs = args.slice(1);
90
-
91
- if (methodOrOptions === undefined || typeof methodOrOptions === 'object') {
92
- // Init
93
- return this.each(function () {
94
- if (!jQuery(this).data('aiWidget')) {
95
- methods.init(this, methodOrOptions || {});
96
- }
97
- });
98
- } else if (methods[methodOrOptions]) {
99
- // Method call
100
- return this.each(function () {
101
- methods[methodOrOptions](this, ...additionalArgs);
102
- });
103
- }
104
-
105
- return this;
106
- };
107
- }
108
-
109
- class AiWidgetInstance {
110
- private element: HTMLElement;
111
- private options: AiWidgetOptions;
112
- private messages: any[] = [];
113
- private isOpen = false;
114
- private isLoading = false;
115
- private $widget: any;
116
- private $messages: any;
117
- private $input: any;
118
- private $button: any;
119
-
120
- constructor(element: HTMLElement, options: AiWidgetOptions) {
121
- this.element = element;
122
- this.options = {
123
- theme: 'light',
124
- position: 'bottom-right',
125
- ...options,
126
- };
127
- this.initialize();
128
- }
129
-
130
- private initialize(): void {
131
- const $ = (window as any).$;
132
- const $elem = $(this.element);
133
-
134
- $elem.addClass(
135
- `ai-widget-container ai-widget-${this.options.theme} ai-widget-${this.options.position}`
136
- );
137
-
138
- // Create toggle button
139
- this.$button = $(`
140
- <button class="ai-widget-toggle-button" aria-label="Open AI Chat">
141
- <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
142
- <path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z" />
143
- </svg>
144
- </button>
145
- `);
146
-
147
- // Create widget window
148
- this.$widget = $(`
149
- <div class="ai-widget-window" style="display: none;">
150
- <div class="ai-widget-header">
151
- <h3>AI Assistant</h3>
152
- <button class="ai-widget-close-button">×</button>
153
- </div>
154
- <div class="ai-widget-messages">
155
- <div class="ai-widget-welcome">
156
- <p>👋 Hello! I'm your AI assistant.</p>
157
- <p>How can I help you today?</p>
158
- </div>
159
- </div>
160
- <form class="ai-widget-form">
161
- <input type="text" class="ai-widget-input" placeholder="Type your message..." />
162
- <button type="submit" class="ai-widget-send-button">→</button>
163
- </form>
164
- </div>
165
- `);
166
-
167
- this.$messages = this.$widget.find('.ai-widget-messages');
168
- this.$input = this.$widget.find('.ai-widget-input');
169
-
170
- // Event handlers
171
- this.$button.on('click', () => this.open());
172
- this.$widget.find('.ai-widget-close-button').on('click', () => this.close());
173
- this.$widget.find('.ai-widget-form').on('submit', (e: any) => {
174
- e.preventDefault();
175
- this.sendMessage(this.$input.val());
176
- this.$input.val('');
177
- });
178
-
179
- // Append to element
180
- $(this.element).append(this.$button).append(this.$widget);
181
- }
182
-
183
- private open(): void {
184
- this.isOpen = true;
185
- (window as any).$(this.element)
186
- .find('.ai-widget-button')
187
- .hide();
188
- (window as any).$(this.element)
189
- .find('.ai-widget-window')
190
- .show();
191
- }
192
-
193
- private close(): void {
194
- this.isOpen = false;
195
- (window as any).$(this.element)
196
- .find('.ai-widget-button')
197
- .show();
198
- (window as any).$(this.element)
199
- .find('.ai-widget-window')
200
- .hide();
201
- }
202
-
203
- async sendMessage(message: string): Promise<void> {
204
- if (!message.trim() || this.isLoading) return;
205
-
206
- // Add user message
207
- const userMsg = {
208
- id: `${Date.now()}-${Math.random()}`,
209
- role: 'user',
210
- content: message,
211
- timestamp: new Date(),
212
- };
213
-
214
- this.messages.push(userMsg);
215
- this.appendMessageToUI(userMsg);
216
-
217
- // Set loading
218
- this.isLoadingState(true);
219
- if (this.options.onLoading) {
220
- this.options.onLoading(true);
221
- }
222
-
223
- try {
224
- // Call backend
225
- const response = await (window as any).fetch(
226
- `${this.options.apiEndpoint}/api/aiorchestrator/query`,
227
- {
228
- method: 'POST',
229
- headers: {
230
- 'Content-Type': 'application/json',
231
- Authorization: `Bearer ${this.options.jwtToken || ''}`,
232
- },
233
- body: JSON.stringify({
234
- userQuery: message,
235
- pageType: this.options.context.pageType,
236
- entityId: this.options.context.entityId,
237
- contextData: this.options.context.customData,
238
- }),
239
- }
240
- );
241
-
242
- if (!response.ok) {
243
- throw new Error(`HTTP ${response.status}`);
244
- }
245
-
246
- const data = await response.json();
247
-
248
- // Add assistant message
249
- const assistantMsg = {
250
- id: data.sessionId,
251
- role: 'assistant',
252
- content: data.response,
253
- timestamp: new Date(data.timestamp),
254
- };
255
-
256
- this.messages.push(assistantMsg);
257
- this.appendMessageToUI(assistantMsg);
258
-
259
- if (this.options.onMessage) {
260
- this.options.onMessage(assistantMsg);
261
- }
262
- } catch (error) {
263
- const errorText = String(error);
264
- if (this.options.onError) {
265
- this.options.onError(errorText);
266
- }
267
- } finally {
268
- this.isLoadingState(false);
269
- if (this.options.onLoading) {
270
- this.options.onLoading(false);
271
- }
272
- }
273
- }
274
-
275
- private appendMessageToUI(message: any): void {
276
- const $ = (window as any).$;
277
- const messageEl = $(`
278
- <div class="ai-widget-message ai-widget-message-${message.role}">
279
- <div class="ai-widget-message-content">${message.content}</div>
280
- <div class="ai-widget-message-time">
281
- ${new Date(message.timestamp).toLocaleTimeString([], {
282
- hour: '2-digit',
283
- minute: '2-digit',
284
- })}
285
- </div>
286
- </div>
287
- `);
288
-
289
- if (this.$messages.find('.ai-widget-welcome').length) {
290
- this.$messages.find('.ai-widget-welcome').remove();
291
- }
292
-
293
- this.$messages.append(messageEl);
294
- this.$messages.scrollTop(this.$messages.prop('scrollHeight'));
295
- }
296
-
297
- getMessages(): any[] {
298
- return [...this.messages];
299
- }
300
-
301
- clearMessages(): void {
302
- this.messages = [];
303
- const $ = (window as any).$;
304
- this.$messages.html(`
305
- <div class="ai-widget-welcome">
306
- <p>👋 Hello! I'm your AI assistant.</p>
307
- <p>How can I help you today?</p>
308
- </div>
309
- `);
310
- }
311
-
312
- updateContext(context: any): void {
313
- this.options.context = { ...this.options.context, ...context };
314
- }
315
-
316
- isLoadingState(value?: boolean): boolean {
317
- if (value !== undefined) {
318
- this.isLoading = value;
319
- }
320
- return this.isLoading;
321
- }
322
-
323
- destroy(): void {
324
- this.$button.off('click');
325
- this.$widget.find('.ai-widget-close-button').off('click');
326
- this.$widget.find('.ai-widget-form').off('submit');
327
- }
328
- }
package/tsconfig.json DELETED
@@ -1,25 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "target": "ES2020",
4
- "module": "ESNext",
5
- "lib": ["ES2020", "DOM"],
6
- "declaration": true,
7
- "declarationMap": true,
8
- "sourceMap": true,
9
- "outDir": "./dist",
10
- "rootDir": "./",
11
- "strict": true,
12
- "esModuleInterop": true,
13
- "skipLibCheck": true,
14
- "forceConsistentCasingInFileNames": true,
15
- "resolveJsonModule": true,
16
- "moduleResolution": "node"
17
- },
18
- "include": [
19
- "*.ts"
20
- ],
21
- "exclude": [
22
- "node_modules",
23
- "dist"
24
- ]
25
- }