@aiqa/sdk 2.0.1 → 2.1.0-develop.1

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.
@@ -285,6 +285,13 @@ declare class AiQaSdk implements IAiqaSdk {
285
285
  internalListeners: Map<SdkEvent, Set<(...args: any[]) => void>>;
286
286
  internalEvents: Set<SdkEvent>;
287
287
  private wrappersByEvent;
288
+ /**
289
+ * Trailing-debounce window (ms) applied per fact in {@link addFact}. Defaults
290
+ * to {@link ADD_FACT_DEBOUNCE_MS}; set to 0 to disable debouncing and send
291
+ * every call immediately.
292
+ */
293
+ addFactDebounceMs: number;
294
+ private pendingAddFacts;
288
295
  constructor(applicationId: string, applicationSecret: string, url?: string);
289
296
  on(name: SdkEvent, callback: (...args: any[]) => any): void;
290
297
  off(name: SdkEvent, callback?: (...args: any[]) => any): void;
@@ -294,6 +301,8 @@ declare class AiQaSdk implements IAiqaSdk {
294
301
  getApplication(): Promise<Application>;
295
302
  getFacts(): Promise<ApplicationFact[]>;
296
303
  addFact(fact: ApplicationFactInput): Promise<AddFactResult>;
304
+ private flushAddFact;
305
+ private sendAddFact;
297
306
  flagFact(factId: string): Promise<FlagFactResult>;
298
307
  setConversationProduct(product: string): Promise<SetConversationProductResult>;
299
308
  setConversationLanguage(language: ConversationLanguage): Promise<SetConversationLanguageResult>;
@@ -3397,7 +3397,7 @@ var compareVersions = (left, right) => {
3397
3397
  // package.json
3398
3398
  var package_default = {
3399
3399
  name: "@aiqa/sdk",
3400
- version: "2.1.0-develop.1",
3400
+ version: "2.0.0",
3401
3401
  main: "dist/node/node.js",
3402
3402
  module: "dist/node/node.js",
3403
3403
  types: "dist/node/node.d.ts",
@@ -3479,6 +3479,7 @@ var package_default = {
3479
3479
  var SDK_VERSION = package_default.version ?? "0.0.1";
3480
3480
  var DEFAULT_URL = "ws://localhost:3025/ws";
3481
3481
  var DEFAULT_TIMEOUT_MS = 6e4;
3482
+ var ADD_FACT_DEBOUNCE_MS = 500;
3482
3483
  var RUNTIME = typeof window !== "undefined" && typeof window.document !== "undefined" ? "browser" : "node";
3483
3484
  var USER_AGENT = RUNTIME === "browser" ? typeof navigator !== "undefined" ? navigator.userAgent : "browser/unknown" : typeof process !== "undefined" ? `node ${process.version} ${process.platform}` : "node/unknown";
3484
3485
  function isThenable(value2) {
@@ -3524,6 +3525,15 @@ var AiQaSdk = class {
3524
3525
  // same fn twice registers two wrappers and two off() calls clear
3525
3526
  // both.
3526
3527
  this.wrappersByEvent = /* @__PURE__ */ new Map();
3528
+ /**
3529
+ * Trailing-debounce window (ms) applied per fact in {@link addFact}. Defaults
3530
+ * to {@link ADD_FACT_DEBOUNCE_MS}; set to 0 to disable debouncing and send
3531
+ * every call immediately.
3532
+ */
3533
+ this.addFactDebounceMs = ADD_FACT_DEBOUNCE_MS;
3534
+ // In-flight debounce state keyed by fact id: the pending timer, the latest
3535
+ // payload to send when it fires, and every caller awaiting that send.
3536
+ this.pendingAddFacts = /* @__PURE__ */ new Map();
3527
3537
  this.url = url2 ?? DEFAULT_URL;
3528
3538
  this.internalListeners = /* @__PURE__ */ new Map();
3529
3539
  this.internalEvents = /* @__PURE__ */ new Set(["authenticated"]);
@@ -3650,6 +3660,13 @@ var AiQaSdk = class {
3650
3660
  this.socket.disconnect();
3651
3661
  }
3652
3662
  this.wrappersByEvent.clear();
3663
+ for (const entry of this.pendingAddFacts.values()) {
3664
+ clearTimeout(entry.timer);
3665
+ entry.waiters.forEach(
3666
+ (w) => w.reject(new Error("Socket disconnected before addFact was sent"))
3667
+ );
3668
+ }
3669
+ this.pendingAddFacts.clear();
3653
3670
  }
3654
3671
  sendMessage(method, data) {
3655
3672
  return new Promise((resolve, reject) => {
@@ -3699,7 +3716,40 @@ var AiQaSdk = class {
3699
3716
  const result = await this.sendMessage("GET_FACTS" /* GET_FACTS */);
3700
3717
  return result && result.data !== void 0 ? result.data : result;
3701
3718
  }
3702
- async addFact(fact) {
3719
+ addFact(fact) {
3720
+ if (this.addFactDebounceMs <= 0) {
3721
+ return this.sendAddFact(fact);
3722
+ }
3723
+ const key = fact.questionId ?? fact.id;
3724
+ return new Promise((resolve, reject) => {
3725
+ const existing = this.pendingAddFacts.get(key);
3726
+ if (existing) {
3727
+ clearTimeout(existing.timer);
3728
+ existing.latest = fact;
3729
+ existing.waiters.push({ resolve, reject });
3730
+ existing.timer = setTimeout(
3731
+ () => this.flushAddFact(key),
3732
+ this.addFactDebounceMs
3733
+ );
3734
+ return;
3735
+ }
3736
+ this.pendingAddFacts.set(key, {
3737
+ latest: fact,
3738
+ waiters: [{ resolve, reject }],
3739
+ timer: setTimeout(() => this.flushAddFact(key), this.addFactDebounceMs)
3740
+ });
3741
+ });
3742
+ }
3743
+ flushAddFact(key) {
3744
+ const entry = this.pendingAddFacts.get(key);
3745
+ if (!entry) return;
3746
+ this.pendingAddFacts.delete(key);
3747
+ this.sendAddFact(entry.latest).then(
3748
+ (result) => entry.waiters.forEach((w) => w.resolve(result)),
3749
+ (error) => entry.waiters.forEach((w) => w.reject(error))
3750
+ );
3751
+ }
3752
+ async sendAddFact(fact) {
3703
3753
  const payload = {
3704
3754
  ...fact,
3705
3755
  critical: fact.critical ?? false,