@xapp/stentor-service-generative-ai 2.2.0 → 2.2.2

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.
@@ -6,7 +6,7 @@ export interface FallbackLLMServiceProps {
6
6
  */
7
7
  primary: LLMService;
8
8
  /**
9
- * The fallback LLM service to use when the primary is rate limited
9
+ * The fallback LLM service to use when the primary is rate limited or unavailable
10
10
  */
11
11
  fallback: LLMService;
12
12
  /**
@@ -16,7 +16,8 @@ export interface FallbackLLMServiceProps {
16
16
  }
17
17
  /**
18
18
  * An LLM service that wraps a primary service with a fallback service.
19
- * When the primary service returns a rate limit error, the fallback service is used.
19
+ * When the primary service is rate limited or unavailable (a server fault, a network error, a
20
+ * timeout), the fallback service is used.
20
21
  *
21
22
  * @example
22
23
  * ```typescript
@@ -34,7 +35,7 @@ export declare class FallbackLLMService implements LLMService {
34
35
  constructor(props: FallbackLLMServiceProps);
35
36
  /**
36
37
  * Generate a response from the primary LLM service, falling back to the
37
- * secondary service if the primary returns a rate limit error.
38
+ * secondary service if the primary is rate limited or unavailable.
38
39
  *
39
40
  * @param prompt - The prompt to send to the LLM
40
41
  * @param options - Optional configuration including timeout
@@ -12,32 +12,55 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
12
12
  Object.defineProperty(exports, "__esModule", { value: true });
13
13
  exports.FallbackLLMService = void 0;
14
14
  /**
15
- * Error patterns that indicate rate limiting and should trigger fallback
15
+ * Error patterns that indicate rate limiting or an unavailable service and should trigger fallback
16
16
  */
17
- const RATE_LIMIT_PATTERNS = [
17
+ const FALLBACK_PATTERNS = [
18
18
  /rate limit/i,
19
19
  /throttl/i,
20
20
  /too many requests/i,
21
21
  /429/,
22
22
  /ThrottlingException/i,
23
23
  /ServiceQuotaExceededException/i,
24
- /ProvisionedThroughputExceededException/i
24
+ /ProvisionedThroughputExceededException/i,
25
+ /ServiceUnavailable/i,
26
+ /service unavailable/i,
27
+ /unable to process your request/i,
28
+ /InternalServer/i,
29
+ /ModelNotReady/i,
30
+ /ModelTimeout/i,
31
+ /timed out/i
25
32
  ];
26
33
  /**
27
- * Check if an error is a rate limit error
34
+ * Network error codes: the provider could not be reached, so another one might be.
28
35
  */
29
- function isRateLimitError(error) {
30
- if (error instanceof Error) {
31
- return RATE_LIMIT_PATTERNS.some((pattern) => pattern.test(error.message));
32
- }
36
+ const NETWORK_ERROR_CODES = ["ECONNRESET", "ECONNREFUSED", "ETIMEDOUT", "ENOTFOUND", "EAI_AGAIN", "EPIPE"];
37
+ /**
38
+ * Check if an error means the primary is rate limited or unavailable, so the fallback may succeed.
39
+ *
40
+ * Errors that would fail the same way anywhere -- a malformed request, a prompt the service does
41
+ * not support, denied access -- are not matched and are rethrown.
42
+ */
43
+ function shouldFallback(error) {
33
44
  if (typeof error === "string") {
34
- return RATE_LIMIT_PATTERNS.some((pattern) => pattern.test(error));
45
+ return FALLBACK_PATTERNS.some((pattern) => pattern.test(error));
46
+ }
47
+ if (!(error instanceof Error)) {
48
+ return false;
49
+ }
50
+ // AWS SDK v3 marks errors the service is responsible for as server faults.
51
+ const { $fault, code } = error;
52
+ if ($fault === "server") {
53
+ return true;
54
+ }
55
+ if (code && NETWORK_ERROR_CODES.includes(code)) {
56
+ return true;
35
57
  }
36
- return false;
58
+ return FALLBACK_PATTERNS.some((pattern) => pattern.test(error.name) || pattern.test(error.message));
37
59
  }
38
60
  /**
39
61
  * An LLM service that wraps a primary service with a fallback service.
40
- * When the primary service returns a rate limit error, the fallback service is used.
62
+ * When the primary service is rate limited or unavailable (a server fault, a network error, a
63
+ * timeout), the fallback service is used.
41
64
  *
42
65
  * @example
43
66
  * ```typescript
@@ -56,7 +79,7 @@ class FallbackLLMService {
56
79
  }
57
80
  /**
58
81
  * Generate a response from the primary LLM service, falling back to the
59
- * secondary service if the primary returns a rate limit error.
82
+ * secondary service if the primary is rate limited or unavailable.
60
83
  *
61
84
  * @param prompt - The prompt to send to the LLM
62
85
  * @param options - Optional configuration including timeout
@@ -68,7 +91,7 @@ class FallbackLLMService {
68
91
  return yield this.primary.generate(prompt, options);
69
92
  }
70
93
  catch (error) {
71
- if (isRateLimitError(error)) {
94
+ if (shouldFallback(error)) {
72
95
  if (this.onFallback && error instanceof Error) {
73
96
  this.onFallback(error);
74
97
  }
@@ -1 +1 @@
1
- {"version":3,"file":"FallbackLLMService.js","sourceRoot":"","sources":["../src/FallbackLLMService.ts"],"names":[],"mappings":";AAAA,kCAAkC;;;;;;;;;;;;AAIlC;;GAEG;AACH,MAAM,mBAAmB,GAAG;IACxB,aAAa;IACb,UAAU;IACV,oBAAoB;IACpB,KAAK;IACL,sBAAsB;IACtB,gCAAgC;IAChC,yCAAyC;CAC5C,CAAC;AAEF;;GAEG;AACH,SAAS,gBAAgB,CAAC,KAAc;IACpC,IAAI,KAAK,YAAY,KAAK,EAAE,CAAC;QACzB,OAAO,mBAAmB,CAAC,IAAI,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC;IAC9E,CAAC;IACD,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QAC5B,OAAO,mBAAmB,CAAC,IAAI,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC;IACtE,CAAC;IACD,OAAO,KAAK,CAAC;AACjB,CAAC;AAiBD;;;;;;;;;;;;GAYG;AACH,MAAa,kBAAkB;IAK3B,YAAY,KAA8B;QACtC,IAAI,CAAC,OAAO,GAAG,KAAK,CAAC,OAAO,CAAC;QAC7B,IAAI,CAAC,QAAQ,GAAG,KAAK,CAAC,QAAQ,CAAC;QAC/B,IAAI,CAAC,UAAU,GAAG,KAAK,CAAC,UAAU,CAAC;IACvC,CAAC;IAED;;;;;;;OAOG;IACG,QAAQ,CAAC,MAAc,EAAE,OAA8B;;YACzD,IAAI,CAAC;gBACD,OAAO,MAAM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ChB,CAAC;QACL,CAAC;KAAA;CACJ;AAhCD,gDAgCC"}
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
7
- "version": "2.2.0",
7
+ "version": "2.2.2",
8
8
  "description": "Generative AI Service",
9
9
  "types": "lib/index",
10
10
  "main": "lib/index",
@@ -16,15 +16,15 @@
16
16
  },
17
17
  "devDependencies": {
18
18
  "@types/chai": "5.2.3",
19
- "@types/lodash": "4.17.26",
19
+ "@types/lodash": "4.17.27",
20
20
  "@xapp/config": "0.3.0",
21
21
  "chai": "4.5.0",
22
22
  "dotenv": "17.4.2",
23
23
  "mocha": "11.8.0",
24
- "stentor-logger": "1.74.13",
24
+ "stentor-logger": "1.74.14",
25
25
  "stentor-models": "1.82.0",
26
- "stentor-service-fetch": "1.74.7",
27
- "stentor-utils": "1.76.4",
26
+ "stentor-service-fetch": "1.74.8",
27
+ "stentor-utils": "1.76.5",
28
28
  "ts-node": "10.9.2",
29
29
  "typescript": "5.9.3"
30
30
  },