cosmic-ai-genius 0.3.6

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,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Genius Team
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.
package/README.md ADDED
@@ -0,0 +1,689 @@
1
+ # Genius SDK for React
2
+
3
+ A powerful, customizable AI chatbot SDK for React applications with voice capabilities and modern UI design.
4
+
5
+ ## Features
6
+
7
+ - ✨ **Modern UI** - Beautiful, responsive chatbot interface matching Figma designs
8
+ - 🎤 **Voice Integration** - Voice call functionality with animated waveforms
9
+ - 🎨 **Fully Customizable** - Themes, positioning, and behavior customization
10
+ - 📱 **Mobile Responsive** - Optimized for all screen sizes
11
+ - 🚀 **Easy Integration** - Simple provider pattern for seamless integration
12
+ - ⚡ **TypeScript Support** - Full type safety and IntelliSense
13
+ - 🎯 **Multiple Components** - Flexible component architecture
14
+ - 🔄 **Streaming Support** - Real-time streaming responses with SSE
15
+ - 🤖 **Backend Integration** - Connect to AI backends with RAG support
16
+ - 📝 **Markdown Rendering** - Rich text formatting in messages
17
+ - 📚 **Source Citations** - Display grounding sources for AI responses
18
+
19
+ ## Installation
20
+
21
+ This package is published to a private Google Cloud Artifact Registry. Development team members need to set up authentication:
22
+
23
+ #### Prerequisites
24
+ - Google Cloud CLI installed: `gcloud --version`
25
+ - Authenticated with your Juspay Google account: `gcloud auth login`
26
+ - Access to the `genius-dev-393512` GCP project
27
+
28
+ #### Quick Setup
29
+
30
+ 1. **Run the authentication script:**
31
+ ```bash
32
+ ./refresh-npm-auth.sh
33
+ ```
34
+
35
+ 2. **Install the package in the repository where you want to integrate the Genius SDK:**
36
+ ```bash
37
+ npm install @genius/sdk-js
38
+ ```
39
+
40
+ #### Manual Setup (Alternative)
41
+
42
+ If you prefer to set up authentication manually:
43
+
44
+ 1. **Get an access token:**
45
+ ```bash
46
+ gcloud auth print-access-token
47
+ ```
48
+
49
+ 2. **Update your `~/.npmrc` file:**
50
+ ```
51
+ @genius:registry=https://us-central1-npm.pkg.dev/genius-dev-393512/genius-npm-registry/
52
+ //us-central1-npm.pkg.dev/genius-dev-393512/genius-npm-registry/:_authToken="YOUR_ACCESS_TOKEN_HERE"
53
+ //us-central1-npm.pkg.dev/genius-dev-393512/genius-npm-registry/:always-auth=true
54
+ ```
55
+
56
+ #### Token Refresh
57
+
58
+ Access tokens expire after ~1 hour. When you get authentication errors:
59
+
60
+ ```bash
61
+ # Run the refresh script again
62
+ ./refresh-npm-auth.sh
63
+
64
+ # Or manually refresh
65
+ gcloud auth print-access-token
66
+ # Update your ~/.npmrc with the new token
67
+ ```
68
+
69
+ #### Troubleshooting Registry Access
70
+
71
+ - **Authentication errors**: Run `./refresh-npm-auth.sh` to refresh your token
72
+ - **Permission denied**: Ensure you're authenticated with the correct Google account
73
+ - **Package not found**: Verify the package name and registry URL are correct
74
+
75
+ ## Quick Start
76
+
77
+ ### 1. Basic Setup
78
+
79
+ Wrap your app with the `GeniusChatbotProvider`:
80
+
81
+ ```tsx
82
+ import { GeniusChatbotProvider, GeniusToggle } from '@genius/sdk-js';
83
+
84
+ function App() {
85
+ return (
86
+ <GeniusChatbotProvider
87
+ config={{
88
+ welcomeMessage: "Hi! How can I help you today?",
89
+ suggestions: ['How to get started?', 'Pricing', 'Support'],
90
+ position: 'bottom-right'
91
+ }}
92
+ >
93
+ <YourAppContent />
94
+ <GeniusToggle />
95
+ </GeniusChatbotProvider>
96
+ );
97
+ }
98
+ ```
99
+
100
+ ### 2. Programmatic Control
101
+
102
+ Use the `useGeniusChatbot` hook for programmatic control:
103
+
104
+ ```tsx
105
+ import { useGeniusChatbot } from '@genius/sdk-js';
106
+
107
+ function MyComponent() {
108
+ const { openChatbot, closeChatbot, toggleChatbot, isOpen } = useGeniusChatbot();
109
+
110
+ return (
111
+ <div>
112
+ <button onClick={openChatbot}>Open Chat</button>
113
+ <button onClick={toggleChatbot}>Toggle Chat</button>
114
+ <p>Chat is {isOpen ? 'open' : 'closed'}</p>
115
+ </div>
116
+ );
117
+ }
118
+ ```
119
+
120
+ ## Components
121
+
122
+ ### GeniusChatbotProvider
123
+
124
+ The main provider component that manages chatbot state and configuration.
125
+
126
+ ```tsx
127
+ <GeniusChatbotProvider
128
+ config={{
129
+ // API Configuration
130
+ apiKey: "your-api-key",
131
+ apiEndpoint: "https://your-api.com/chat",
132
+
133
+ // UI Customization
134
+ welcomeMessage: "Welcome to Genius AI!",
135
+ placeholder: "Type your message...",
136
+ suggestions: ['FAQ', 'Pricing', 'Contact'],
137
+
138
+ // Positioning & Theming
139
+ position: 'bottom-right',
140
+ width: 440,
141
+ height: 680,
142
+ theme: {
143
+ primaryColor: '#1b85ff',
144
+ backgroundColor: '#ffffff'
145
+ },
146
+
147
+ // Event Handlers
148
+ onMessage: (message) => console.log('New message:', message),
149
+ onClose: () => console.log('Chatbot closed'),
150
+ onExpand: () => console.log('Chatbot expanded')
151
+ }}
152
+ defaultOpen={false}
153
+ >
154
+ {children}
155
+ </GeniusChatbotProvider>
156
+ ```
157
+
158
+ ### GeniusChatbot
159
+
160
+ The main chatbot interface component (automatically rendered by provider):
161
+
162
+ ```tsx
163
+ // Rendered automatically when chatbot is open
164
+ // Features:
165
+ // - Expandable interface (50% screen width on desktop)
166
+ // - Voice call integration
167
+ // - Message history
168
+ // - Suggested replies
169
+ // - Mobile responsive design
170
+ ```
171
+
172
+ ### GeniusToggle
173
+
174
+ A floating toggle button to open the chatbot:
175
+
176
+ ```tsx
177
+ <GeniusToggle
178
+ position="bottom-right" // 'bottom-right' | 'bottom-left' | 'top-right' | 'top-left'
179
+ offsetX={20} // Horizontal offset from edge
180
+ offsetY={20} // Vertical offset from edge
181
+ />
182
+ ```
183
+
184
+ ### VoiceCall
185
+
186
+ Voice call interface component (automatically shown when voice is activated):
187
+
188
+ ```tsx
189
+ // Activated when waveform icon is clicked
190
+ // Features:
191
+ // - Animated waveform visualization
192
+ // - Microphone and end call controls
193
+ // - Compact floating design
194
+ // - Smooth transitions
195
+ ```
196
+
197
+ ## Configuration Options
198
+
199
+ ### GeniusChatbotConfig
200
+
201
+ ```typescript
202
+ interface GeniusChatbotConfig {
203
+ // API Settings
204
+ apiKey?: string;
205
+ baseUrl?: string; // Base URL for backend (e.g., 'https://api.example.com')
206
+ endpoints?: { // Custom endpoints (optional)
207
+ chat?: string; // Non-streaming chat endpoint
208
+ stream?: string; // Streaming chat endpoint
209
+ };
210
+ useStreaming?: boolean; // Enable real-time streaming responses
211
+
212
+ // Content
213
+ welcomeMessage?: string;
214
+ placeholder?: string;
215
+ suggestions?: string[];
216
+
217
+ // Layout
218
+ position?: 'bottom-right' | 'bottom-left' | 'center';
219
+ width?: number;
220
+ height?: number;
221
+
222
+ // Theming
223
+ theme?: {
224
+ primaryColor?: string;
225
+ backgroundColor?: string;
226
+ textColor?: string;
227
+ borderRadius?: number;
228
+ fontFamily?: string;
229
+ [key: string]: any;
230
+ };
231
+
232
+ // Event Handlers
233
+ onMessage?: (message: Message) => void;
234
+ onClose?: () => void;
235
+ onExpand?: () => void;
236
+ }
237
+ ```
238
+
239
+ ### Message Interface
240
+
241
+ ```typescript
242
+ interface Message {
243
+ id: string;
244
+ content: string;
245
+ role: 'user' | 'assistant';
246
+ timestamp: Date;
247
+ sources?: GroundingChunk[]; // Optional source citations
248
+ }
249
+
250
+ interface GroundingChunk {
251
+ text: string;
252
+ title: string;
253
+ uri: string;
254
+ }
255
+ ```
256
+
257
+ ## Backend Integration
258
+
259
+ The SDK supports integration with AI backends, including the provided Python FastAPI server with Google Gemini and RAG support.
260
+
261
+ ### Quick Backend Setup
262
+
263
+ 1. **Start the backend server** (see `/backend/README.md` for details):
264
+ ```bash
265
+ cd backend
266
+ ./run.sh
267
+ ```
268
+
269
+ 2. **Configure the SDK with backend URL**:
270
+ ```tsx
271
+ <GeniusChatbotProvider
272
+ config={{
273
+ baseUrl: 'http://localhost:8001',
274
+ useStreaming: true, // Enable real-time responses
275
+ }}
276
+ >
277
+ ```
278
+
279
+ ### Backend Configuration Options
280
+
281
+ ```tsx
282
+ // Option 1: Simple base URL (recommended)
283
+ config={{
284
+ baseUrl: 'https://api.example.com',
285
+ // SDK will append /chat and /chat/stream
286
+ }}
287
+
288
+ // Option 2: Custom endpoints
289
+ config={{
290
+ endpoints: {
291
+ chat: 'https://api.example.com/v1/chat',
292
+ stream: 'https://streaming.example.com/sse'
293
+ }
294
+ }}
295
+
296
+ // Option 3: Mix and match
297
+ config={{
298
+ baseUrl: 'https://api.example.com',
299
+ endpoints: {
300
+ stream: 'https://fast-stream.example.com/chat'
301
+ }
302
+ }}
303
+ ```
304
+
305
+ ## Advanced Usage
306
+
307
+ ### Custom API Integration
308
+
309
+ ```tsx
310
+ <GeniusChatbotProvider
311
+ config={{
312
+ baseUrl: "https://your-api.com",
313
+ apiKey: "your-secret-key",
314
+ useStreaming: true,
315
+ onMessage: async (message) => {
316
+ // Custom message handling
317
+ console.log('User sent:', message.content);
318
+
319
+ // Send to your analytics
320
+ analytics.track('chat_message_sent', {
321
+ content: message.content,
322
+ timestamp: message.timestamp
323
+ });
324
+ }
325
+ }}
326
+ >
327
+ ```
328
+
329
+ ### Custom Theming
330
+
331
+ ```tsx
332
+ <GeniusChatbotProvider
333
+ config={{
334
+ theme: {
335
+ // Override CSS custom properties
336
+ '--genius-primary-blue': '#your-brand-color',
337
+ '--genius-background': '#f8f9fa',
338
+ '--genius-text-primary': '#1a1a1a',
339
+ '--genius-border-radius': '16px',
340
+ fontFamily: 'Inter, sans-serif'
341
+ }
342
+ }}
343
+ >
344
+ ```
345
+
346
+ ### Multiple Chatbot Instances
347
+
348
+ ```tsx
349
+ // You can have multiple chatbot providers for different contexts
350
+ function App() {
351
+ return (
352
+ <div>
353
+ {/* Support chatbot */}
354
+ <GeniusChatbotProvider config={{
355
+ welcomeMessage: "Need help? I'm here to assist!",
356
+ position: 'bottom-right'
357
+ }}>
358
+ <SupportSection />
359
+ <GeniusToggle />
360
+ </GeniusChatbotProvider>
361
+
362
+ {/* Sales chatbot */}
363
+ <GeniusChatbotProvider config={{
364
+ welcomeMessage: "Interested in our products?",
365
+ position: 'bottom-left'
366
+ }}>
367
+ <SalesSection />
368
+ <GeniusToggle position="bottom-left" />
369
+ </GeniusChatbotProvider>
370
+ </div>
371
+ );
372
+ }
373
+ ```
374
+
375
+ ### Voice Call Integration
376
+
377
+ The voice call feature is automatically available when using the chatbot:
378
+
379
+ 1. **Activation**: Click the waveform icon when no text is entered
380
+ 2. **Interface**: Shows compact voice call UI with animated waveform
381
+ 3. **Controls**: Microphone status and end call button
382
+ 4. **Exit**: Click end call button to return to chat interface
383
+
384
+ ```tsx
385
+ // Voice call is automatically integrated
386
+ // No additional setup required
387
+ // Activated by clicking waveform icon in chat input
388
+ ```
389
+
390
+ ## Responsive Behavior
391
+
392
+ ### Desktop
393
+ - Default size: 440px × 680px
394
+ - Expandable to 50% screen width
395
+ - Floating position with shadows
396
+
397
+ ### Mobile
398
+ - Full screen overlay
399
+ - Auto-collapse when expanded
400
+ - Hide expand/collapse buttons
401
+ - Touch-optimized interface
402
+
403
+ ### Tablet
404
+ - Responsive sizing between mobile and desktop
405
+ - Optimized button sizes and spacing
406
+
407
+ ## Events & Callbacks
408
+
409
+ ### Message Events
410
+
411
+ ```tsx
412
+ const handleMessage = (message: Message) => {
413
+ console.log('New message:', message);
414
+
415
+ // Send to analytics
416
+ if (message.role === 'user') {
417
+ analytics.track('user_message', { content: message.content });
418
+ }
419
+ };
420
+
421
+ <GeniusChatbotProvider config={{ onMessage: handleMessage }} />
422
+ ```
423
+
424
+ ### Lifecycle Events
425
+
426
+ ```tsx
427
+ const config = {
428
+ onClose: () => {
429
+ console.log('Chatbot closed');
430
+ // Save conversation state
431
+ },
432
+
433
+ onExpand: () => {
434
+ console.log('Chatbot expanded');
435
+ // Track engagement
436
+ }
437
+ };
438
+ ```
439
+
440
+ ## Styling & Customization
441
+
442
+ ### CSS Custom Properties
443
+
444
+ The SDK uses CSS custom properties for easy theming:
445
+
446
+ ```css
447
+ :root {
448
+ --genius-primary-blue: #1B85FF;
449
+ --genius-grey-600: #8A8A8E;
450
+ --genius-grey-500: #9C9C9F;
451
+ --genius-grey-200: #E0E0E0;
452
+ --genius-grey-100: #EDEDED;
453
+ --genius-text-primary: #1c1c1c;
454
+ --genius-text-secondary: #5f5f5f;
455
+ --genius-white: #ffffff;
456
+ --genius-background: #fcfcfc;
457
+ }
458
+ ```
459
+
460
+ ### Override Styles
461
+
462
+ ```css
463
+ /* Custom styles for your application */
464
+ .genius-chatbot-container {
465
+ border-radius: 20px !important;
466
+ box-shadow: 0 20px 40px rgba(0,0,0,0.1) !important;
467
+ }
468
+
469
+ .genius-chatbot-send-button {
470
+ background: linear-gradient(45deg, #ff6b6b, #4ecdc4) !important;
471
+ }
472
+ ```
473
+
474
+ ## TypeScript Support
475
+
476
+ The SDK is fully typed with TypeScript. Import types as needed:
477
+
478
+ ```typescript
479
+ import type {
480
+ GeniusChatbotConfig,
481
+ Message,
482
+ GeniusChatbotContextType
483
+ } from '@genius/sdk-js';
484
+
485
+ const config: GeniusChatbotConfig = {
486
+ welcomeMessage: "Hello!",
487
+ suggestions: ["Help", "Info"]
488
+ };
489
+
490
+ const handleMessage = (message: Message) => {
491
+ // Fully typed message object
492
+ console.log(message.content, message.role, message.timestamp);
493
+ };
494
+ ```
495
+
496
+ ## Browser Support
497
+
498
+ - ✅ Chrome 90+
499
+ - ✅ Firefox 88+
500
+ - ✅ Safari 14+
501
+ - ✅ Edge 90+
502
+ - ✅ Mobile browsers (iOS Safari, Chrome Mobile)
503
+
504
+ ## Performance
505
+
506
+ - **Bundle size**: ~45KB gzipped
507
+ - **Runtime**: Lightweight React components
508
+ - **Memory**: Efficient state management
509
+ - **Animations**: Hardware-accelerated CSS animations
510
+
511
+ ## Development
512
+
513
+ ### Publishing to Private Registry
514
+
515
+ For development team members with publishing access:
516
+
517
+ 1. **Ensure authentication is set up** (see Installation section above)
518
+
519
+ 2. **Update package version:**
520
+ ```bash
521
+ npm version patch # or minor/major
522
+ ```
523
+
524
+ 3. **Publish to private registry:**
525
+ ```bash
526
+ npm publish
527
+ ```
528
+
529
+ 4. **Verify publication:**
530
+ ```bash
531
+ npm view @genius/sdk-js --registry=https://us-central1-npm.pkg.dev/genius-dev-393512/genius-npm-registry/
532
+ ```
533
+
534
+ ### Registry Authentication for CI/CD
535
+
536
+ For automated publishing in CI/CD pipelines, use service account authentication:
537
+
538
+ ```bash
539
+ # In your CI/CD environment
540
+ gcloud auth activate-service-account --key-file=service-account-key.json
541
+ gcloud auth configure-docker us-central1-docker.pkg.dev
542
+ ```
543
+
544
+ ## Troubleshooting
545
+
546
+ ### Common Issues
547
+
548
+ 1. **Chatbot not appearing**
549
+ ```tsx
550
+ // Ensure you're using the provider
551
+ <GeniusChatbotProvider>
552
+ <YourApp />
553
+ </GeniusChatbotProvider>
554
+ ```
555
+
556
+ 2. **Styling conflicts**
557
+ ```css
558
+ /* Use more specific selectors */
559
+ .your-app .genius-chatbot-container {
560
+ /* Your overrides */
561
+ }
562
+ ```
563
+
564
+ 3. **TypeScript errors**
565
+ ```tsx
566
+ // Import types explicitly
567
+ import type { GeniusChatbotConfig } from '@genius/sdk-js';
568
+ ```
569
+
570
+ 4. **Registry authentication errors**
571
+ ```bash
572
+ # Refresh your authentication
573
+ ./refresh-npm-auth.sh
574
+
575
+ # Or check your gcloud authentication
576
+ gcloud auth list
577
+ gcloud auth login # if needed
578
+ ```
579
+
580
+ ### Debug Mode
581
+
582
+ Enable debug logging:
583
+
584
+ ```tsx
585
+ <GeniusChatbotProvider
586
+ config={{
587
+ debug: true, // Enable console logs
588
+ onMessage: (msg) => console.log('Debug:', msg)
589
+ }}
590
+ />
591
+ ```
592
+
593
+ ## ReScript Integration
594
+
595
+ The SDK can be used with ReScript by creating bindings:
596
+
597
+ ### 1. Create SDK bindings (`GeniusSDK.res`):
598
+
599
+ ```rescript
600
+ module GeniusChatbotProvider = {
601
+ @module("@genius/sdk-js") @react.component
602
+ external make: (
603
+ ~config: GeniusConfig.t,
604
+ ~defaultOpen: bool=?,
605
+ ~children: React.element
606
+ ) => React.element = "GeniusChatbotProvider"
607
+ }
608
+
609
+ module GeniusToggle = {
610
+ @module("@genius/sdk-js") @react.component
611
+ external make: (
612
+ ~position: [#"bottom-right" | #"bottom-left" | #"top-right" | #"top-left"]=?,
613
+ ~offsetX: int=?,
614
+ ~offsetY: int=?
615
+ ) => React.element = "GeniusToggle"
616
+ }
617
+
618
+ @module("@genius/sdk-js")
619
+ external useGeniusChatbot: unit => {
620
+ "openChatbot": unit => unit,
621
+ "closeChatbot": unit => unit,
622
+ "toggleChatbot": unit => unit,
623
+ "isOpen": bool
624
+ } = "useGeniusChatbot"
625
+ ```
626
+
627
+ ### 2. Define configuration types (`GeniusConfig.res`):
628
+
629
+ ```rescript
630
+ type theme = {
631
+ primaryColor?: string,
632
+ backgroundColor?: string,
633
+ textColor?: string,
634
+ }
635
+
636
+ type endpoints = {
637
+ chat?: string,
638
+ stream?: string,
639
+ }
640
+
641
+ type message = {
642
+ id: string,
643
+ content: string,
644
+ role: [#user | #assistant],
645
+ timestamp: Js.Date.t,
646
+ }
647
+
648
+ type t = {
649
+ apiKey?: string,
650
+ baseUrl?: string,
651
+ endpoints?: endpoints,
652
+ welcomeMessage?: string,
653
+ placeholder?: string,
654
+ suggestions?: array<string>,
655
+ position?: [#"bottom-right" | #"bottom-left" | #center],
656
+ useStreaming?: bool,
657
+ onMessage?: message => unit,
658
+ onClose?: unit => unit,
659
+ onExpand?: unit => unit,
660
+ }
661
+ ```
662
+
663
+ ### 3. Use in your app:
664
+
665
+ ```rescript
666
+ @react.component
667
+ let make = () => {
668
+ let config = {
669
+ GeniusConfig.welcomeMessage: Some("How can I help you?"),
670
+ suggestions: Some(["Question 1", "Question 2"]),
671
+ baseUrl: Some("http://localhost:8001"),
672
+ useStreaming: Some(true),
673
+ // ... other fields
674
+ }
675
+
676
+ <GeniusSDK.GeniusChatbotProvider config defaultOpen=false>
677
+ <div className="app">
678
+ <YourAppContent />
679
+ <GeniusSDK.GeniusToggle />
680
+ </div>
681
+ </GeniusSDK.GeniusChatbotProvider>
682
+ }
683
+ ```
684
+
685
+ ## Examples
686
+
687
+ Check out the `/src/App.tsx` file in this repository for a complete working example with all features demonstrated.
688
+
689
+ Made with ❤️ by the Genius team