@aiqa/sdk 0.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.
package/README.md ADDED
@@ -0,0 +1,730 @@
1
+ # AIQA SDK
2
+
3
+ A TypeScript/JavaScript SDK for interacting with the AIQA WebSocket service. This SDK provides methods for managing applications, recording audio, and tracking facts during conversations.
4
+
5
+ ## Table of Contents
6
+
7
+ - [Installation](#installation)
8
+ - [Getting Started](#getting-started)
9
+ - [Configuration](#configuration)
10
+ - [API Reference](#api-reference)
11
+ - [Connection Management](#connection-management)
12
+ - [Application Management](#application-management)
13
+ - [Recording Management](#recording-management)
14
+ - [Fact Management](#fact-management)
15
+ - [Event Handling](#event-handling)
16
+ - [Types](#types)
17
+ - [Examples](#examples)
18
+ - [Error Handling](#error-handling)
19
+
20
+ ## Installation
21
+
22
+ The SDK can be used directly in your project. Make sure you have the SDK source files available and include Socket.IO client library in your HTML:
23
+
24
+ ```html
25
+ <script src="https://cdn.socket.io/4.5.4/socket.io.min.js"></script>
26
+ ```
27
+
28
+ ## Getting Started
29
+
30
+ ### Basic Setup
31
+
32
+ ```typescript
33
+ import AiQaSdk from './sdk-src';
34
+
35
+ // Initialize the SDK
36
+ const sdk = new AiQaSdk({
37
+ url: 'https://api.solutionson.ai/',
38
+ path: '/ws',
39
+ enableLogging: true
40
+ });
41
+
42
+ // Set up event handlers
43
+ sdk.on('connect', () => {
44
+ console.log('Connected to AIQA service');
45
+ });
46
+
47
+ sdk.on('error', (error) => {
48
+ console.error('Connection error:', error);
49
+ });
50
+
51
+ // Connect to the service
52
+ sdk.connect();
53
+ ```
54
+
55
+ ## Configuration
56
+
57
+ ### SDKConfig Interface
58
+
59
+ ```typescript
60
+ interface SDKConfig {
61
+ url?: string; // WebSocket server URL (default: 'ws://localhost:3025')
62
+ jwtToken?: string; // JWT token for authentication
63
+ path?: string; // WebSocket path (default: '/ws')
64
+ enableLogging?: boolean; // Enable/disable logging (default: true)
65
+ }
66
+ ```
67
+
68
+ ### Configuration Methods
69
+
70
+ #### Constructor Configuration
71
+
72
+ ```typescript
73
+ const sdk = new AiQaSdk({
74
+ url: 'ws://api.solutionson.ai/',
75
+ jwtToken: 'your-jwt-token',
76
+ path: '/ws',
77
+ enableLogging: false
78
+ });
79
+ ```
80
+
81
+ #### Runtime Configuration
82
+
83
+ ```typescript
84
+ sdk.configure({
85
+ url: 'ws://api.solutionson.ai/',
86
+ enableLogging: true
87
+ });
88
+ ```
89
+
90
+ ## API Reference
91
+
92
+ ### Connection Management
93
+
94
+ #### `connect()`
95
+
96
+ Establishes a WebSocket connection to the AIQA service.
97
+
98
+ ```typescript
99
+ sdk.connect();
100
+ ```
101
+
102
+ #### `disconnect()`
103
+
104
+ Closes the WebSocket connection.
105
+
106
+ ```typescript
107
+ sdk.disconnect();
108
+ ```
109
+
110
+ #### `isAlive(callback)`
111
+
112
+ Checks if the WebSocket connection is alive (callback-based).
113
+
114
+ ```typescript
115
+ sdk.isAlive(({ isAlive, error }) => {
116
+ if (error) {
117
+ console.error('Error checking connection:', error);
118
+ } else {
119
+ console.log('Connection is alive:', isAlive);
120
+ }
121
+ });
122
+ ```
123
+
124
+ #### `isAliveAsync()`
125
+
126
+ Checks if the WebSocket connection is alive (Promise-based).
127
+
128
+ ```typescript
129
+ try {
130
+ const isAlive = await sdk.isAliveAsync();
131
+ console.log('Connection is alive:', isAlive);
132
+ } catch (error) {
133
+ console.error('Error checking connection:', error);
134
+ }
135
+ ```
136
+
137
+ ### Application Management
138
+
139
+ #### `createApplication(data, callback)`
140
+
141
+ Creates a new application (callback-based).
142
+
143
+ **Parameters:**
144
+ - `data: TApplicationPayload`
145
+ ```typescript
146
+ {
147
+ policyNumber: string;
148
+ product: string;
149
+ insuredPhone: string;
150
+ clientName: string;
151
+ }
152
+ ```
153
+ - `callback: (response: TResponse) => void`
154
+
155
+ **Example:**
156
+ ```typescript
157
+ sdk.createApplication({
158
+ policyNumber: 'POL-12345',
159
+ product: 'Insurance Product',
160
+ insuredPhone: '+1234567890',
161
+ clientName: 'John Doe'
162
+ }, ({ application, error }) => {
163
+ if (error) {
164
+ console.error('Error creating application:', error);
165
+ } else {
166
+ console.log('Application created:', application);
167
+ }
168
+ });
169
+ ```
170
+
171
+ #### `createApplicationAsync(data)`
172
+
173
+ Creates a new application (Promise-based).
174
+
175
+ ```typescript
176
+ try {
177
+ const application = await sdk.createApplicationAsync({
178
+ policyNumber: 'POL-12345',
179
+ product: 'Insurance Product',
180
+ insuredPhone: '+1234567890',
181
+ clientName: 'John Doe'
182
+ });
183
+ console.log('Application created:', application);
184
+ } catch (error) {
185
+ console.error('Error creating application:', error);
186
+ }
187
+ ```
188
+
189
+ #### `getApplication(id, callback)`
190
+
191
+ Retrieves an application by ID (callback-based).
192
+
193
+ ```typescript
194
+ sdk.getApplication('app-id-123', ({ application, error }) => {
195
+ if (error) {
196
+ console.error('Error getting application:', error);
197
+ } else {
198
+ console.log('Application:', application);
199
+ }
200
+ });
201
+ ```
202
+
203
+ #### `getApplicationAsync(id)`
204
+
205
+ Retrieves an application by ID (Promise-based).
206
+
207
+ ```typescript
208
+ try {
209
+ const application = await sdk.getApplicationAsync('app-id-123');
210
+ console.log('Application:', application);
211
+ } catch (error) {
212
+ console.error('Error getting application:', error);
213
+ }
214
+ ```
215
+
216
+ #### `updateApplication(id, referenceId, callback)`
217
+
218
+ Updates an application with a reference ID (callback-based).
219
+
220
+ ```typescript
221
+ sdk.updateApplication('app-id-123', 'ref-456', ({ application, error }) => {
222
+ if (error) {
223
+ console.error('Error updating application:', error);
224
+ } else {
225
+ console.log('Application updated:', application);
226
+ }
227
+ });
228
+ ```
229
+
230
+ #### `updateApplicationAsync(id, referenceId)`
231
+
232
+ Updates an application with a reference ID (Promise-based).
233
+
234
+ ```typescript
235
+ try {
236
+ const application = await sdk.updateApplicationAsync('app-id-123', 'ref-456');
237
+ console.log('Application updated:', application);
238
+ } catch (error) {
239
+ console.error('Error updating application:', error);
240
+ }
241
+ ```
242
+
243
+ #### `setActiveApplication(applicationId, callback)`
244
+
245
+ Sets an application as active (callback-based).
246
+
247
+ ```typescript
248
+ sdk.setActiveApplication('app-id-123', ({ applicationIsActive, error }) => {
249
+ if (error) {
250
+ console.error('Error setting active application:', error);
251
+ } else {
252
+ console.log('Application is active:', applicationIsActive);
253
+ }
254
+ });
255
+ ```
256
+
257
+ #### `setActiveApplicationAsync(applicationId)`
258
+
259
+ Sets an application as active (Promise-based).
260
+
261
+ ```typescript
262
+ try {
263
+ const isActive = await sdk.setActiveApplicationAsync('app-id-123');
264
+ console.log('Application is active:', isActive);
265
+ } catch (error) {
266
+ console.error('Error setting active application:', error);
267
+ }
268
+ ```
269
+
270
+ #### `getActiveApplication(callback)`
271
+
272
+ Gets the currently active application (callback-based).
273
+
274
+ ```typescript
275
+ sdk.getActiveApplication(({ application, error }) => {
276
+ if (error) {
277
+ console.error('Error getting active application:', error);
278
+ } else {
279
+ console.log('Active application:', application);
280
+ }
281
+ });
282
+ ```
283
+
284
+ #### `getActiveApplicationAsync()`
285
+
286
+ Gets the currently active application (Promise-based).
287
+
288
+ ```typescript
289
+ try {
290
+ const application = await sdk.getActiveApplicationAsync();
291
+ console.log('Active application:', application);
292
+ } catch (error) {
293
+ console.error('Error getting active application:', error);
294
+ }
295
+ ```
296
+
297
+ #### `resetActiveApplication(callback)`
298
+
299
+ Resets (deactivates) the active application (callback-based).
300
+
301
+ ```typescript
302
+ sdk.resetActiveApplication(({ applicationIsActive, error }) => {
303
+ if (error) {
304
+ console.error('Error resetting active application:', error);
305
+ } else {
306
+ console.log('Application is active:', applicationIsActive);
307
+ }
308
+ });
309
+ ```
310
+
311
+ #### `resetActiveApplicationAsync()`
312
+
313
+ Resets (deactivates) the active application (Promise-based).
314
+
315
+ ```typescript
316
+ try {
317
+ const isActive = await sdk.resetActiveApplicationAsync();
318
+ console.log('Application is active:', isActive);
319
+ } catch (error) {
320
+ console.error('Error resetting active application:', error);
321
+ }
322
+ ```
323
+
324
+ ### Recording Management
325
+
326
+ #### `startRecording(callback)`
327
+
328
+ Starts audio recording (callback-based).
329
+
330
+ ```typescript
331
+ sdk.startRecording(({ recordingIsStarted, error }) => {
332
+ if (error) {
333
+ console.error('Error starting recording:', error);
334
+ } else {
335
+ console.log('Recording started:', recordingIsStarted);
336
+ }
337
+ });
338
+ ```
339
+
340
+ #### `startRecordingAsync()`
341
+
342
+ Starts audio recording (Promise-based).
343
+
344
+ ```typescript
345
+ try {
346
+ const started = await sdk.startRecordingAsync();
347
+ console.log('Recording started:', started);
348
+ } catch (error) {
349
+ console.error('Error starting recording:', error);
350
+ }
351
+ ```
352
+
353
+ #### `stopRecording(callback)`
354
+
355
+ Stops audio recording (callback-based).
356
+
357
+ ```typescript
358
+ sdk.stopRecording(({ recordingIsStopped, error }) => {
359
+ if (error) {
360
+ console.error('Error stopping recording:', error);
361
+ } else {
362
+ console.log('Recording stopped:', recordingIsStopped);
363
+ }
364
+ });
365
+ ```
366
+
367
+ #### `stopRecordingAsync()`
368
+
369
+ Stops audio recording (Promise-based).
370
+
371
+ ```typescript
372
+ try {
373
+ const stopped = await sdk.stopRecordingAsync();
374
+ console.log('Recording stopped:', stopped);
375
+ } catch (error) {
376
+ console.error('Error stopping recording:', error);
377
+ }
378
+ ```
379
+
380
+ #### `isRecordingNow(callback)`
381
+
382
+ Checks if recording is currently active (callback-based).
383
+
384
+ ```typescript
385
+ sdk.isRecordingNow(({ recordingIsActive, error }) => {
386
+ if (error) {
387
+ console.error('Error checking recording status:', error);
388
+ } else {
389
+ console.log('Recording is active:', recordingIsActive);
390
+ }
391
+ });
392
+ ```
393
+
394
+ #### `isRecordingNowAsync()`
395
+
396
+ Checks if recording is currently active (Promise-based).
397
+
398
+ ```typescript
399
+ try {
400
+ const isRecording = await sdk.isRecordingNowAsync();
401
+ console.log('Recording is active:', isRecording);
402
+ } catch (error) {
403
+ console.error('Error checking recording status:', error);
404
+ }
405
+ ```
406
+
407
+ ### Fact Management
408
+
409
+ #### `addFact(fact, callback)`
410
+
411
+ Adds a fact to the active application (callback-based).
412
+
413
+ **Parameters:**
414
+ - `fact: TApplicationFactPayload`
415
+ ```typescript
416
+ {
417
+ fact: {
418
+ id: string;
419
+ expected_answer: string;
420
+ };
421
+ applicationId: string;
422
+ }
423
+ ```
424
+ - `callback: (response: TResponse) => void`
425
+
426
+ **Example:**
427
+ ```typescript
428
+ sdk.addFact({
429
+ fact: {
430
+ id: 'fact-123',
431
+ expected_answer: 'Yes'
432
+ },
433
+ applicationId: 'app-id-123'
434
+ }, ({ factIsMatched, error }) => {
435
+ if (error) {
436
+ console.error('Error adding fact:', error);
437
+ } else {
438
+ console.log('Fact matched:', factIsMatched);
439
+ }
440
+ });
441
+ ```
442
+
443
+ #### `addFactAsync(fact)`
444
+
445
+ Adds a fact to the active application (Promise-based).
446
+
447
+ ```typescript
448
+ try {
449
+ const isMatched = await sdk.addFactAsync({
450
+ fact: {
451
+ id: 'fact-123',
452
+ expected_answer: 'Yes'
453
+ },
454
+ applicationId: 'app-id-123'
455
+ });
456
+ console.log('Fact matched:', isMatched);
457
+ } catch (error) {
458
+ console.error('Error adding fact:', error);
459
+ }
460
+ ```
461
+
462
+ ### Event Handling
463
+
464
+ #### `on(eventName, callback)`
465
+
466
+ Registers an event listener.
467
+
468
+ **Available Events:**
469
+ - `connect` - Fired when WebSocket connection is established
470
+ - `disconnect` - Fired when WebSocket disconnects
471
+ - `close` - Fired when WebSocket connection closes
472
+ - `error` - Fired when an error occurs
473
+ - `reconnect_attempt` - Fired during reconnection attempts
474
+ - `reconnect_error` - Fired when reconnection fails
475
+ - `reconnect_failed` - Fired when reconnection completely fails
476
+
477
+ **Example:**
478
+ ```typescript
479
+ sdk.on('connect', () => {
480
+ console.log('Connected to AIQA service');
481
+ });
482
+
483
+ sdk.on('disconnect', () => {
484
+ console.log('Disconnected from AIQA service');
485
+ });
486
+
487
+ sdk.on('error', (error) => {
488
+ console.error('Error occurred:', error);
489
+ });
490
+ ```
491
+
492
+ #### `off(eventName)`
493
+
494
+ Removes an event listener.
495
+
496
+ ```typescript
497
+ sdk.off('connect');
498
+ ```
499
+
500
+ ## Types
501
+
502
+ ### TApplicationPayload
503
+
504
+ ```typescript
505
+ type TApplicationPayload = {
506
+ policyNumber: string;
507
+ product: string;
508
+ insuredPhone: string;
509
+ clientName: string;
510
+ }
511
+ ```
512
+
513
+ ### TApplication
514
+
515
+ ```typescript
516
+ type TApplication = {
517
+ application_id: string;
518
+ adviser_id: string;
519
+ start_date: Date;
520
+ language: string;
521
+ rtStatus: string;
522
+ policy_number?: string;
523
+ insured_phone?: string;
524
+ client_name?: string;
525
+ utterances: TApplicationUtterance[];
526
+ facts: TApplicationFact[];
527
+ }
528
+ ```
529
+
530
+ ### TApplicationUtterance
531
+
532
+ ```typescript
533
+ type TApplicationUtterance = {
534
+ id: string;
535
+ application_id?: string;
536
+ call_id?: number;
537
+ order?: number;
538
+ is_agent?: boolean;
539
+ speaker?: number;
540
+ text?: string;
541
+ text_en?: string;
542
+ start_time?: number;
543
+ end_time?: number;
544
+ extra?: any;
545
+ }
546
+ ```
547
+
548
+ ### TApplicationFact
549
+
550
+ ```typescript
551
+ type TApplicationFact = {
552
+ id: string;
553
+ expected_answer: string;
554
+ actual_answer: string;
555
+ is_matched: boolean;
556
+ }
557
+ ```
558
+
559
+ ### TApplicationFactPayload
560
+
561
+ ```typescript
562
+ type TApplicationFactPayload = {
563
+ fact: {
564
+ id: string;
565
+ expected_answer: string;
566
+ };
567
+ applicationId: string;
568
+ }
569
+ ```
570
+
571
+ ### TResponse
572
+
573
+ ```typescript
574
+ type TResponse = {
575
+ isAlive?: boolean;
576
+ application?: TApplication | null;
577
+ applicationIsActive?: boolean;
578
+ factIsMatched?: boolean;
579
+ recordingIsStarted?: boolean;
580
+ recordingIsStopped?: boolean;
581
+ recordingIsActive?: boolean;
582
+ error?: Error;
583
+ }
584
+ ```
585
+
586
+ ## Examples
587
+
588
+ ### Complete Example: Recording Session
589
+
590
+ ```typescript
591
+ import AiQaSdk from './sdk-src';
592
+
593
+ // Initialize SDK
594
+ const sdk = new AiQaSdk({
595
+ url: 'ws://localhost:3025',
596
+ enableLogging: true
597
+ });
598
+
599
+ // Set up event handlers
600
+ sdk.on('connect', async () => {
601
+ console.log('Connected!');
602
+
603
+ try {
604
+ // Create an application
605
+ const application = await sdk.createApplicationAsync({
606
+ policyNumber: 'POL-12345',
607
+ product: 'Life Insurance',
608
+ insuredPhone: '+1234567890',
609
+ clientName: 'John Doe'
610
+ });
611
+
612
+ console.log('Application created:', application.application_id);
613
+
614
+ // Set as active
615
+ await sdk.setActiveApplicationAsync(application.application_id);
616
+
617
+ // Start recording
618
+ await sdk.startRecordingAsync();
619
+ console.log('Recording started');
620
+
621
+ // Add a fact
622
+ const factMatched = await sdk.addFactAsync({
623
+ fact: {
624
+ id: 'fact-1',
625
+ expected_answer: 'Yes, I understand'
626
+ },
627
+ applicationId: application.application_id
628
+ });
629
+
630
+ console.log('Fact matched:', factMatched);
631
+
632
+ // Stop recording
633
+ await sdk.stopRecordingAsync();
634
+ console.log('Recording stopped');
635
+
636
+ } catch (error) {
637
+ console.error('Error:', error);
638
+ }
639
+ });
640
+
641
+ sdk.on('error', (error) => {
642
+ console.error('SDK Error:', error);
643
+ });
644
+
645
+ // Connect
646
+ sdk.connect();
647
+ ```
648
+
649
+ ### Example: Using Callbacks
650
+
651
+ ```typescript
652
+ const sdk = new AiQaSdk({
653
+ url: 'ws://localhost:3025'
654
+ });
655
+
656
+ sdk.on('connect', () => {
657
+ // Create application with callback
658
+ sdk.createApplication({
659
+ policyNumber: 'POL-12345',
660
+ product: 'Insurance',
661
+ insuredPhone: '+1234567890',
662
+ clientName: 'Jane Doe'
663
+ }, ({ application, error }) => {
664
+ if (error) {
665
+ console.error('Error:', error);
666
+ return;
667
+ }
668
+
669
+ // Set as active
670
+ sdk.setActiveApplication(application.application_id, ({ applicationIsActive, error }) => {
671
+ if (error) {
672
+ console.error('Error:', error);
673
+ return;
674
+ }
675
+
676
+ console.log('Application is active:', applicationIsActive);
677
+ });
678
+ });
679
+ });
680
+
681
+ sdk.connect();
682
+ ```
683
+
684
+ ## Error Handling
685
+
686
+ All methods support error handling through callbacks or Promise rejections.
687
+
688
+ ### Callback-based Error Handling
689
+
690
+ ```typescript
691
+ sdk.createApplication(data, ({ application, error }) => {
692
+ if (error) {
693
+ // Handle error
694
+ console.error('Error:', error);
695
+ return;
696
+ }
697
+
698
+ // Handle success
699
+ console.log('Success:', application);
700
+ });
701
+ ```
702
+
703
+ ### Promise-based Error Handling
704
+
705
+ ```typescript
706
+ try {
707
+ const application = await sdk.createApplicationAsync(data);
708
+ // Handle success
709
+ console.log('Success:', application);
710
+ } catch (error) {
711
+ // Handle error
712
+ console.error('Error:', error);
713
+ }
714
+ ```
715
+
716
+ ### Common Error Scenarios
717
+
718
+ 1. **Connection Errors**: Fired through the `error` event
719
+ 2. **Version Mismatch**: Thrown when the server version is incompatible
720
+ 3. **Invalid Data**: Returned in the error field of callbacks or rejected promises
721
+ 4. **Network Issues**: Handled through reconnection events
722
+
723
+ ## Version Requirements
724
+
725
+ The SDK requires the AIQA service to be at least version `1.0.0`. If the server version is incompatible, the connection will be automatically closed and an error will be thrown.
726
+
727
+ ## License
728
+
729
+ [Add your license information here]
730
+