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