@courseecho/ai-widget-jquery 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,328 @@
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/package.json ADDED
@@ -0,0 +1,28 @@
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
+ }
28
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,25 @@
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
+ }