@aiqa/sdk 0.0.5 → 0.0.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.
@@ -1,2 +1,100 @@
1
+ import { Socket } from 'socket.io-client';
1
2
 
2
- export { }
3
+ type Application = {
4
+ application_id: string;
5
+ client_name: string;
6
+ client_phone_number: string;
7
+ agent_id: string;
8
+ agent_first_name: string;
9
+ agent_last_name: string;
10
+ };
11
+ declare enum Methods {
12
+ AUTHENTICATE = "AUTHENTICATE",
13
+ GET_FACTS = "GET_FACTS",
14
+ START_RECORDING = "START_RECORDING",
15
+ STOP_RECORDING = "STOP_RECORDING",
16
+ IS_RECORDING = "IS_RECORDING",
17
+ ADD_FACT = "ADD_FACT"
18
+ }
19
+ type SdkEvent = Methods | 'connect' | 'disconnect' | 'close' | 'error' | 'reconnect_attempt' | 'reconnect_error' | 'reconnect_failed' | 'authenticated' | 'app_version';
20
+ declare enum FactAnswerFormatType {
21
+ string = "STRING",
22
+ number = "NUMBER",
23
+ integer = "INT",
24
+ float = "FLOAT",
25
+ bool = "BOOL",
26
+ list = "LIST",
27
+ strict = "STRICT",
28
+ single_choice = "SINGLE_CHOICE",
29
+ multiple_choices = "MULTIPLE_CHOICES"
30
+ }
31
+ type ApplicationFactAnswerChoice = {
32
+ key: string;
33
+ text: string;
34
+ };
35
+ type ApplicationFactAnswerFormat = {
36
+ type: FactAnswerFormatType;
37
+ choices?: ApplicationFactAnswerChoice[];
38
+ };
39
+ type ApplicationFactInput = {
40
+ id: string;
41
+ question: string;
42
+ expected_answer: any;
43
+ answer_format: ApplicationFactAnswerFormat;
44
+ category_id?: string;
45
+ category_name?: string;
46
+ language?: string;
47
+ critical?: boolean;
48
+ is_skipped?: boolean;
49
+ };
50
+ type QuestionStatus = 'searching' | 'asked' | 'not_asked';
51
+ type VerificationStatus = 'processing' | 'matched' | 'unmatched';
52
+ type ApplicationFact = ApplicationFactInput & {
53
+ ai_answer?: any;
54
+ question_status: QuestionStatus;
55
+ verification_status: VerificationStatus;
56
+ };
57
+ type FactIsMatched = boolean;
58
+ type SdkError = {
59
+ code: 'INVALID_CONFIG' | 'CONNECTION_ERROR' | 'TIMEOUT' | 'SERVER_ERROR' | 'UNEXPECTED_RESPONSE';
60
+ message: string;
61
+ details?: any;
62
+ };
63
+ declare abstract class IAiqaSdk {
64
+ abstract connect(): void;
65
+ abstract disconnect(): void;
66
+ abstract on(eventName: string, callback: (...args: any[]) => any): void;
67
+ abstract off(eventName: string): void;
68
+ abstract getApplication(): Promise<Application>;
69
+ abstract addFact(fact: ApplicationFactInput): Promise<FactIsMatched>;
70
+ abstract getFacts(): Promise<ApplicationFact[]>;
71
+ }
72
+
73
+ declare class AiQaSdk implements IAiqaSdk {
74
+ socket: Socket | null;
75
+ url: string;
76
+ applicationId: string;
77
+ applicationSecretKey: string;
78
+ cachedApplication?: Application;
79
+ internalListeners: Map<SdkEvent, Set<(...args: any[]) => void>>;
80
+ internalEvents: Set<SdkEvent>;
81
+ constructor(applicationId: string, applicationSecret: string, url?: string);
82
+ on(name: SdkEvent, callback: (...args: any[]) => any): void;
83
+ off(name: SdkEvent, callback?: (...args: any[]) => any): void;
84
+ connect(): void;
85
+ disconnect(): void;
86
+ sendMessage(method: Methods, data?: any): Promise<any>;
87
+ getApplication(): Promise<Application>;
88
+ getFacts(): Promise<ApplicationFact[]>;
89
+ addFact(fact: ApplicationFactInput): Promise<FactIsMatched>;
90
+ authenticate(): Promise<void>;
91
+ startRecording(): Promise<boolean>;
92
+ stopRecording(): Promise<boolean>;
93
+ isRecordingNow(): Promise<boolean>;
94
+ private compareVersions;
95
+ private emitInternal;
96
+ private createRequestId;
97
+ private isManagerEvent;
98
+ }
99
+
100
+ export { AiQaSdk, type Application, type ApplicationFact, type ApplicationFactAnswerChoice, type ApplicationFactAnswerFormat, type ApplicationFactInput, FactAnswerFormatType, type FactIsMatched, IAiqaSdk, Methods, type QuestionStatus, type SdkError, type SdkEvent, type VerificationStatus };
@@ -3398,16 +3398,41 @@ Object.assign(lookup2, {
3398
3398
  connect: lookup2
3399
3399
  });
3400
3400
 
3401
+ // src/types.ts
3402
+ var Methods = /* @__PURE__ */ ((Methods2) => {
3403
+ Methods2["AUTHENTICATE"] = "AUTHENTICATE";
3404
+ Methods2["GET_FACTS"] = "GET_FACTS";
3405
+ Methods2["START_RECORDING"] = "START_RECORDING";
3406
+ Methods2["STOP_RECORDING"] = "STOP_RECORDING";
3407
+ Methods2["IS_RECORDING"] = "IS_RECORDING";
3408
+ Methods2["ADD_FACT"] = "ADD_FACT";
3409
+ return Methods2;
3410
+ })(Methods || {});
3411
+ var FactAnswerFormatType = /* @__PURE__ */ ((FactAnswerFormatType2) => {
3412
+ FactAnswerFormatType2["string"] = "STRING";
3413
+ FactAnswerFormatType2["number"] = "NUMBER";
3414
+ FactAnswerFormatType2["integer"] = "INT";
3415
+ FactAnswerFormatType2["float"] = "FLOAT";
3416
+ FactAnswerFormatType2["bool"] = "BOOL";
3417
+ FactAnswerFormatType2["list"] = "LIST";
3418
+ FactAnswerFormatType2["strict"] = "STRICT";
3419
+ FactAnswerFormatType2["single_choice"] = "SINGLE_CHOICE";
3420
+ FactAnswerFormatType2["multiple_choices"] = "MULTIPLE_CHOICES";
3421
+ return FactAnswerFormatType2;
3422
+ })(FactAnswerFormatType || {});
3423
+ var IAiqaSdk = class {
3424
+ };
3425
+
3401
3426
  // package.json
3402
3427
  var package_default = {
3403
3428
  name: "@aiqa/sdk",
3404
- version: "0.0.3",
3429
+ version: "0.0.5",
3405
3430
  main: "dist/node/node.js",
3406
3431
  module: "dist/node/node.js",
3407
3432
  types: "dist/node/node.d.ts",
3408
3433
  engines: { node: ">=18" },
3409
3434
  scripts: {
3410
- build: "tsup",
3435
+ build: `tsup && node -e "const fs=require('fs');['dist/node/node','dist/browser/browser'].forEach(f=>{const mts=f+'.d.mts';const ts=f+'.d.ts';if(fs.existsSync(mts)){fs.renameSync(mts,ts);}});"`,
3411
3436
  "dev:node": "tsx src/node.ts",
3412
3437
  "start:node": "node dist/node/node.js",
3413
3438
  "serve:browser": "serve .",
@@ -3457,4 +3482,219 @@ var package_default = {
3457
3482
 
3458
3483
  // src/shared.ts
3459
3484
  var SDK_VERSION = package_default.version ?? "0.0.1";
3485
+ var DEFAULT_URL = "ws://localhost:3025/ws";
3486
+ var DEFAULT_TIMEOUT_MS = 6e4;
3487
+ var AiQaSdk = class {
3488
+ constructor(applicationId, applicationSecret, url2) {
3489
+ this.socket = null;
3490
+ this.url = url2 ?? DEFAULT_URL;
3491
+ this.internalListeners = /* @__PURE__ */ new Map();
3492
+ this.internalEvents = /* @__PURE__ */ new Set(["authenticated"]);
3493
+ if (applicationId && applicationSecret) {
3494
+ this.applicationId = applicationId;
3495
+ this.applicationSecretKey = applicationSecret;
3496
+ } else {
3497
+ throw new Error("Application id and application secret are required");
3498
+ }
3499
+ }
3500
+ on(name, callback) {
3501
+ if (this.internalEvents.has(name)) {
3502
+ if (!this.internalListeners.has(name)) {
3503
+ this.internalListeners.set(name, /* @__PURE__ */ new Set());
3504
+ }
3505
+ this.internalListeners.get(name).add(callback);
3506
+ return;
3507
+ }
3508
+ if (!this.socket) {
3509
+ throw new Error("Not connected");
3510
+ }
3511
+ if (this.isManagerEvent(name)) {
3512
+ this.socket.io.on(name, callback);
3513
+ } else {
3514
+ this.socket.on(name, callback);
3515
+ }
3516
+ }
3517
+ off(name, callback) {
3518
+ if (this.internalEvents.has(name)) {
3519
+ if (!callback) {
3520
+ this.internalListeners.delete(name);
3521
+ return;
3522
+ }
3523
+ const handlers = this.internalListeners.get(name);
3524
+ if (handlers) {
3525
+ handlers.delete(callback);
3526
+ if (handlers.size === 0) {
3527
+ this.internalListeners.delete(name);
3528
+ }
3529
+ }
3530
+ return;
3531
+ }
3532
+ if (!this.socket) {
3533
+ throw new Error("Not connected");
3534
+ }
3535
+ if (this.isManagerEvent(name)) {
3536
+ this.socket.io.off(name, callback);
3537
+ } else {
3538
+ this.socket.off(name, callback);
3539
+ }
3540
+ }
3541
+ connect() {
3542
+ if (!this.url) {
3543
+ return;
3544
+ }
3545
+ const url2 = new URL(this.url);
3546
+ const socketOptions = {
3547
+ reconnectionDelayMax: 1e4,
3548
+ path: url2.pathname
3549
+ };
3550
+ this.socket = lookup2(url2.origin, socketOptions);
3551
+ this.socket.on("connect", () => {
3552
+ this.authenticate();
3553
+ });
3554
+ this.socket.on("app_version", (payload) => {
3555
+ let versionRange = payload;
3556
+ if (typeof payload === "string") {
3557
+ try {
3558
+ versionRange = JSON.parse(payload);
3559
+ } catch (error) {
3560
+ versionRange = payload;
3561
+ }
3562
+ }
3563
+ const minVersion = versionRange?.min;
3564
+ const maxVersion = versionRange?.max;
3565
+ if (minVersion || maxVersion) {
3566
+ const tooLow = minVersion ? this.compareVersions(SDK_VERSION, minVersion) < 0 : false;
3567
+ const tooHigh = maxVersion ? this.compareVersions(SDK_VERSION, maxVersion) > 0 : false;
3568
+ if (tooLow || tooHigh) {
3569
+ const error = new Error(
3570
+ `SDK version ${SDK_VERSION} is not supported. Supported range: ${minVersion || "-"} to ${maxVersion || "-"}.`
3571
+ );
3572
+ this.socket.disconnect();
3573
+ this.socket.emit("error", error);
3574
+ }
3575
+ }
3576
+ });
3577
+ }
3578
+ disconnect() {
3579
+ if (this.socket) {
3580
+ this.socket.disconnect();
3581
+ }
3582
+ }
3583
+ sendMessage(method, data) {
3584
+ return new Promise((resolve, reject) => {
3585
+ if (!this.socket || !this.socket.connected) {
3586
+ reject(new Error("Socket is not connected"));
3587
+ return;
3588
+ }
3589
+ const requestId = this.createRequestId();
3590
+ let timeout;
3591
+ const handler = (payload2) => {
3592
+ if (timeout) {
3593
+ clearTimeout(timeout);
3594
+ }
3595
+ this.socket.off(requestId, handler);
3596
+ let parsed = payload2;
3597
+ if (typeof payload2 === "string") {
3598
+ try {
3599
+ parsed = JSON.parse(payload2);
3600
+ } catch (error) {
3601
+ parsed = payload2;
3602
+ }
3603
+ }
3604
+ if (parsed && parsed.error !== void 0) {
3605
+ reject(parsed.error);
3606
+ return;
3607
+ }
3608
+ resolve(parsed && parsed.data !== void 0 ? parsed.data : parsed);
3609
+ };
3610
+ this.socket.on(requestId, handler);
3611
+ const payload = data && typeof data === "object" ? { ...data, requestId } : { requestId, data };
3612
+ this.socket.emit(method, JSON.stringify(payload));
3613
+ timeout = setTimeout(() => {
3614
+ this.socket.off(requestId, handler);
3615
+ reject(new Error(`Request timed out for ${method}`));
3616
+ }, DEFAULT_TIMEOUT_MS);
3617
+ });
3618
+ }
3619
+ async getApplication() {
3620
+ if (this.cachedApplication) {
3621
+ return this.cachedApplication;
3622
+ }
3623
+ throw new Error("Application is not available yet. Wait for authentication to complete.");
3624
+ }
3625
+ async getFacts() {
3626
+ const result = await this.sendMessage("GET_FACTS" /* GET_FACTS */);
3627
+ return result && result.data !== void 0 ? result.data : result;
3628
+ }
3629
+ async addFact(fact) {
3630
+ const payload = {
3631
+ ...fact,
3632
+ critical: fact.critical ?? false,
3633
+ is_skipped: fact.is_skipped ?? false
3634
+ };
3635
+ const result = await this.sendMessage("ADD_FACT" /* ADD_FACT */, payload);
3636
+ return result && result.data !== void 0 ? result.data : result;
3637
+ }
3638
+ async authenticate() {
3639
+ try {
3640
+ const result = await this.sendMessage("AUTHENTICATE" /* AUTHENTICATE */, {
3641
+ applicationId: this.applicationId,
3642
+ applicationSecretKey: this.applicationSecretKey
3643
+ });
3644
+ const application = result && result.data !== void 0 ? result.data : result;
3645
+ this.cachedApplication = application;
3646
+ this.emitInternal("authenticated", application);
3647
+ } catch (error) {
3648
+ this.socket.emit("error", error);
3649
+ }
3650
+ }
3651
+ async startRecording() {
3652
+ const result = await this.sendMessage("START_RECORDING" /* START_RECORDING */);
3653
+ return result && result.data !== void 0 ? result.data : result;
3654
+ }
3655
+ async stopRecording() {
3656
+ const result = await this.sendMessage("STOP_RECORDING" /* STOP_RECORDING */);
3657
+ return result && result.data !== void 0 ? result.data : result;
3658
+ }
3659
+ async isRecordingNow() {
3660
+ const result = await this.sendMessage("IS_RECORDING" /* IS_RECORDING */);
3661
+ return result && result.data !== void 0 ? result.data : result;
3662
+ }
3663
+ compareVersions(left, right) {
3664
+ const leftParts = left.split(".").map((part) => Number(part));
3665
+ const rightParts = right.split(".").map((part) => Number(part));
3666
+ const maxLength = Math.max(leftParts.length, rightParts.length);
3667
+ for (let i = 0; i < maxLength; i += 1) {
3668
+ const l = leftParts[i] ?? 0;
3669
+ const r = rightParts[i] ?? 0;
3670
+ if (l > r) {
3671
+ return 1;
3672
+ }
3673
+ if (l < r) {
3674
+ return -1;
3675
+ }
3676
+ }
3677
+ return 0;
3678
+ }
3679
+ emitInternal(name, ...args) {
3680
+ const handlers = this.internalListeners.get(name);
3681
+ if (!handlers) {
3682
+ return;
3683
+ }
3684
+ handlers.forEach((handler) => handler(...args));
3685
+ }
3686
+ createRequestId() {
3687
+ return `req_${Date.now().toString(36)}_${Math.random().toString(36).slice(2, 10)}`;
3688
+ }
3689
+ isManagerEvent(name) {
3690
+ return name === "reconnect_attempt" || name === "reconnect_error" || name === "reconnect_failed" || name === "error";
3691
+ }
3692
+ };
3693
+ var shared_default = AiQaSdk;
3694
+ export {
3695
+ shared_default as AiQaSdk,
3696
+ FactAnswerFormatType,
3697
+ IAiqaSdk,
3698
+ Methods
3699
+ };
3460
3700
  //# sourceMappingURL=browser.js.map