@nuancelabs/js-sdk 0.2.0 → 0.2.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.
Files changed (2) hide show
  1. package/index.js +27 -2
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -4,7 +4,7 @@ function isRecord(value) {
4
4
  }
5
5
 
6
6
  // src/core/sdk-version.ts
7
- var SDK_HEADER = `js/${"0.2.0"}`;
7
+ var SDK_HEADER = `js/${"0.2.1"}`;
8
8
 
9
9
  // src/utils.ts
10
10
  function normalizeHost(host) {
@@ -2086,6 +2086,8 @@ var DEFAULT_HOST = "https://api.nuancelabs.ai";
2086
2086
  var QUEUE_HEARTBEAT_INTERVAL_MS = 5000;
2087
2087
  var HEARTBEAT_INTERVAL_MS = 5000;
2088
2088
  var ADMISSION_FAILED_BEATS = 6;
2089
+ var BUSY_RETRY_LIMIT_MS = 60000;
2090
+ var BUSY_RETRY_DEFAULT_MS = 3000;
2089
2091
  var LOST_WINDOW_MS = 30000;
2090
2092
  function createNuanceConversation(options = {}) {
2091
2093
  return new NuanceConversation(options);
@@ -2115,6 +2117,9 @@ function isLegalRequiredBody(body) {
2115
2117
  function isMaintenanceBody(body) {
2116
2118
  return isRecord(body) && body.code === "maintenance";
2117
2119
  }
2120
+ function isBusyBody(body) {
2121
+ return isRecord(body) && body.code === "busy";
2122
+ }
2118
2123
  function isCurfewBody(body) {
2119
2124
  return isRecord(body) && body.code === "policy_curfew";
2120
2125
  }
@@ -2212,7 +2217,7 @@ class NuanceConversation {
2212
2217
  request.personality = this.personality;
2213
2218
  if (this.variant)
2214
2219
  request.variant = this.variant;
2215
- conversation = await this.api.createConversation(request);
2220
+ conversation = await this.createWhenNotBusy(request, attempt);
2216
2221
  } catch (error) {
2217
2222
  if (error instanceof ApiError && error.status === 409) {
2218
2223
  throw new ActiveSessionError;
@@ -2354,6 +2359,26 @@ class NuanceConversation {
2354
2359
  resilientReconnect: this.resilient
2355
2360
  });
2356
2361
  }
2362
+ async createWhenNotBusy(request, attempt) {
2363
+ const giveUpAt = Date.now() + BUSY_RETRY_LIMIT_MS;
2364
+ for (;; ) {
2365
+ try {
2366
+ return await this.api.createConversation(request);
2367
+ } catch (error) {
2368
+ if (!(error instanceof ApiError) || error.status !== 503 || !isBusyBody(error.body) || attempt !== this.attempt) {
2369
+ throw error;
2370
+ }
2371
+ const waitMs = typeof error.body.retry_after_seconds === "number" ? error.body.retry_after_seconds * 1000 : BUSY_RETRY_DEFAULT_MS;
2372
+ if (Date.now() + waitMs > giveUpAt)
2373
+ throw error;
2374
+ if (this._state !== "queued")
2375
+ this.setState("queued");
2376
+ await new Promise((resolve) => setTimeout(resolve, waitMs));
2377
+ if (attempt !== this.attempt)
2378
+ throw error;
2379
+ }
2380
+ }
2381
+ }
2357
2382
  startHeartbeat(conversationId, intervalMs) {
2358
2383
  this.stopHeartbeat();
2359
2384
  this.failedBeats = 0;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nuancelabs/js-sdk",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "description": "JavaScript SDK for Nuance Labs conversation video AI avatar",
5
5
  "license": "UNLICENSED",
6
6
  "repository": {