@memberjunction/ai 3.4.0 → 4.1.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/README.md +242 -0
- package/dist/generic/apiKeyDictionary.d.ts +22 -0
- package/dist/generic/apiKeyDictionary.d.ts.map +1 -1
- package/dist/generic/apiKeyDictionary.js +20 -13
- package/dist/generic/apiKeyDictionary.js.map +1 -1
- package/dist/generic/baseAudio.d.ts +107 -2
- package/dist/generic/baseAudio.d.ts.map +1 -1
- package/dist/generic/baseAudio.js +26 -20
- package/dist/generic/baseAudio.js.map +1 -1
- package/dist/generic/baseDiffusion.d.ts +5 -1
- package/dist/generic/baseDiffusion.d.ts.map +1 -1
- package/dist/generic/baseDiffusion.js +5 -17
- package/dist/generic/baseDiffusion.js.map +1 -1
- package/dist/generic/baseEmbeddings.d.ts +25 -2
- package/dist/generic/baseEmbeddings.d.ts.map +1 -1
- package/dist/generic/baseEmbeddings.js +26 -8
- package/dist/generic/baseEmbeddings.js.map +1 -1
- package/dist/generic/baseImage.d.ts +262 -2
- package/dist/generic/baseImage.d.ts.map +1 -1
- package/dist/generic/baseImage.js +83 -20
- package/dist/generic/baseImage.js.map +1 -1
- package/dist/generic/baseLLM.d.ts +100 -4
- package/dist/generic/baseLLM.d.ts.map +1 -1
- package/dist/generic/baseLLM.js +122 -14
- package/dist/generic/baseLLM.js.map +1 -1
- package/dist/generic/baseModel.d.ts +91 -0
- package/dist/generic/baseModel.d.ts.map +1 -1
- package/dist/generic/baseModel.js +37 -11
- package/dist/generic/baseModel.js.map +1 -1
- package/dist/generic/baseReranker.d.ts +81 -2
- package/dist/generic/baseReranker.d.ts.map +1 -1
- package/dist/generic/baseReranker.js +73 -6
- package/dist/generic/baseReranker.js.map +1 -1
- package/dist/generic/baseVideo.d.ts +24 -1
- package/dist/generic/baseVideo.d.ts.map +1 -1
- package/dist/generic/baseVideo.js +11 -14
- package/dist/generic/baseVideo.js.map +1 -1
- package/dist/generic/chat.types.d.ts +291 -2
- package/dist/generic/chat.types.d.ts.map +1 -1
- package/dist/generic/chat.types.js +105 -32
- package/dist/generic/chat.types.js.map +1 -1
- package/dist/generic/classify.types.d.ts +5 -2
- package/dist/generic/classify.types.d.ts.map +1 -1
- package/dist/generic/classify.types.js +8 -11
- package/dist/generic/classify.types.js.map +1 -1
- package/dist/generic/embed.types.d.ts +1 -1
- package/dist/generic/embed.types.js +1 -2
- package/dist/generic/errorAnalyzer.d.ts +94 -0
- package/dist/generic/errorAnalyzer.d.ts.map +1 -1
- package/dist/generic/errorAnalyzer.js +160 -28
- package/dist/generic/errorAnalyzer.js.map +1 -1
- package/dist/generic/errorTypes.d.ts +116 -0
- package/dist/generic/errorTypes.d.ts.map +1 -1
- package/dist/generic/errorTypes.js +1 -2
- package/dist/generic/reranker.types.d.ts +74 -0
- package/dist/generic/reranker.types.d.ts.map +1 -1
- package/dist/generic/reranker.types.js +10 -2
- package/dist/generic/reranker.types.js.map +1 -1
- package/dist/generic/summarize.types.d.ts +5 -2
- package/dist/generic/summarize.types.d.ts.map +1 -1
- package/dist/generic/summarize.types.js +8 -9
- package/dist/generic/summarize.types.js.map +1 -1
- package/dist/index.d.ts +15 -15
- package/dist/index.js +15 -31
- package/dist/index.js.map +1 -1
- package/package.json +8 -7
- package/readme.md +167 -967
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"classify.types.js","sourceRoot":"","sources":["../../src/generic/classify.types.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"classify.types.js","sourceRoot":"","sources":["../../src/generic/classify.types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAc,MAAM,aAAa,CAAA;AACpD,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAE1C;;GAEG;AACH,MAAM,OAAO,cAAe,SAAQ,UAAU;CAC7C;AAED,MAAM,OAAO,WAAW;IACpB,YAAY,GAAW,EAAE,UAAkB;QACvC,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC;QACf,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;IACjC,CAAC;CAGJ;AAED,MAAM,OAAO,cAAe,SAAQ,UAAU;CAI7C"}
|
|
@@ -1,11 +1,105 @@
|
|
|
1
1
|
import { AIErrorInfo } from './errorTypes.js';
|
|
2
|
+
/**
|
|
3
|
+
* Utility class for analyzing errors from various AI providers and mapping them to standardized error information.
|
|
4
|
+
* This class provides a consistent way to interpret errors across different provider SDKs.
|
|
5
|
+
*
|
|
6
|
+
* @class ErrorAnalyzer
|
|
7
|
+
* @since 2.47.0
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* try {
|
|
12
|
+
* const result = await provider.chat(params);
|
|
13
|
+
* } catch (error) {
|
|
14
|
+
* const errorInfo = ErrorAnalyzer.analyzeError(error, 'OpenAI');
|
|
15
|
+
* if (errorInfo.canFailover) {
|
|
16
|
+
* // Try another provider
|
|
17
|
+
* }
|
|
18
|
+
* }
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
2
21
|
export declare class ErrorAnalyzer {
|
|
22
|
+
/**
|
|
23
|
+
* Analyzes an error from an AI provider and returns standardized error information.
|
|
24
|
+
* This method extracts relevant details from provider-specific error formats and
|
|
25
|
+
* maps them to a consistent structure for easier handling.
|
|
26
|
+
*
|
|
27
|
+
* @static
|
|
28
|
+
* @param {any} error - The error object thrown by the provider SDK
|
|
29
|
+
* @param {string} [providerName] - Optional name of the provider for context
|
|
30
|
+
* @returns {AIErrorInfo} Standardized error information
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```typescript
|
|
34
|
+
* const errorInfo = ErrorAnalyzer.analyzeError(error, 'Anthropic');
|
|
35
|
+
* console.log(`Error type: ${errorInfo.errorType}`);
|
|
36
|
+
* console.log(`Can retry: ${errorInfo.severity !== 'Fatal'}`);
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
3
39
|
static analyzeError(error: any, providerName?: string): AIErrorInfo;
|
|
40
|
+
/**
|
|
41
|
+
* Extracts HTTP status code from various error object structures.
|
|
42
|
+
* Different provider SDKs store status codes in different locations.
|
|
43
|
+
*
|
|
44
|
+
* @private
|
|
45
|
+
* @static
|
|
46
|
+
* @param {any} error - The error object to extract status code from
|
|
47
|
+
* @returns {number | undefined} The HTTP status code if found, undefined otherwise
|
|
48
|
+
*/
|
|
4
49
|
private static extractHttpStatusCode;
|
|
50
|
+
/**
|
|
51
|
+
* Determines the standardized error type based on status code and error properties.
|
|
52
|
+
* Uses a combination of HTTP status codes, error messages, and error class names.
|
|
53
|
+
*
|
|
54
|
+
* @private
|
|
55
|
+
* @static
|
|
56
|
+
* @param {any} error - The error object to analyze
|
|
57
|
+
* @param {number} [statusCode] - The HTTP status code if available
|
|
58
|
+
* @returns {AIErrorType} The categorized error type
|
|
59
|
+
*/
|
|
5
60
|
private static determineErrorType;
|
|
61
|
+
/**
|
|
62
|
+
* Determines the error severity based on the error type.
|
|
63
|
+
* This helps decide whether to retry immediately, wait, or fail permanently.
|
|
64
|
+
*
|
|
65
|
+
* @private
|
|
66
|
+
* @static
|
|
67
|
+
* @param {AIErrorType} errorType - The categorized error type
|
|
68
|
+
* @returns {ErrorSeverity} The severity level of the error
|
|
69
|
+
*/
|
|
6
70
|
private static determineSeverity;
|
|
71
|
+
/**
|
|
72
|
+
* Determines whether an error can potentially be resolved by switching providers.
|
|
73
|
+
*
|
|
74
|
+
* Strategy: We're permissive with failover - most errors should allow trying another
|
|
75
|
+
* provider/model since vendors may use different status codes and error messages.
|
|
76
|
+
* Only block failover for clear client-side structural errors that won't be fixed by switching.
|
|
77
|
+
*
|
|
78
|
+
* @private
|
|
79
|
+
* @static
|
|
80
|
+
* @param {AIErrorType} errorType - The categorized error type
|
|
81
|
+
* @returns {boolean} True if failover might help, false otherwise
|
|
82
|
+
*/
|
|
7
83
|
private static canFailoverForError;
|
|
84
|
+
/**
|
|
85
|
+
* Extracts suggested retry delay from error response.
|
|
86
|
+
* Looks for Retry-After headers and provider-specific retry delay fields.
|
|
87
|
+
*
|
|
88
|
+
* @private
|
|
89
|
+
* @static
|
|
90
|
+
* @param {any} error - The error object to extract retry delay from
|
|
91
|
+
* @returns {number | undefined} Suggested retry delay in seconds, or undefined
|
|
92
|
+
*/
|
|
8
93
|
private static extractRetryDelay;
|
|
94
|
+
/**
|
|
95
|
+
* Extracts the provider-specific error code from the error object.
|
|
96
|
+
* Different providers store error codes in different locations.
|
|
97
|
+
*
|
|
98
|
+
* @private
|
|
99
|
+
* @static
|
|
100
|
+
* @param {any} error - The error object to extract provider code from
|
|
101
|
+
* @returns {string | undefined} The provider error code if found, undefined otherwise
|
|
102
|
+
*/
|
|
9
103
|
private static extractProviderErrorCode;
|
|
10
104
|
}
|
|
11
105
|
//# sourceMappingURL=errorAnalyzer.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errorAnalyzer.d.ts","sourceRoot":"","sources":["../../src/generic/errorAnalyzer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAA8B,MAAM,iBAAiB,CAAC;
|
|
1
|
+
{"version":3,"file":"errorAnalyzer.d.ts","sourceRoot":"","sources":["../../src/generic/errorAnalyzer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAA8B,MAAM,iBAAiB,CAAC;AAE1E;;;;;;;;;;;;;;;;;;GAkBG;AACH,qBAAa,aAAa;IACtB;;;;;;;;;;;;;;;;OAgBG;IACH,MAAM,CAAC,YAAY,CAAC,KAAK,EAAE,GAAG,EAAE,YAAY,CAAC,EAAE,MAAM,GAAG,WAAW;IAqDnE;;;;;;;;OAQG;IACH,OAAO,CAAC,MAAM,CAAC,qBAAqB;IAUpC;;;;;;;;;OASG;IACH,OAAO,CAAC,MAAM,CAAC,kBAAkB;IAmIjC;;;;;;;;OAQG;IACH,OAAO,CAAC,MAAM,CAAC,iBAAiB;IAyBhC;;;;;;;;;;;OAWG;IACH,OAAO,CAAC,MAAM,CAAC,mBAAmB;IAwBlC;;;;;;;;OAQG;IACH,OAAO,CAAC,MAAM,CAAC,iBAAiB;IA4BhC;;;;;;;;OAQG;IACH,OAAO,CAAC,MAAM,CAAC,wBAAwB;CA6B1C"}
|
|
@@ -1,14 +1,54 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Utility class for analyzing errors from various AI providers and mapping them to standardized error information.
|
|
3
|
+
* This class provides a consistent way to interpret errors across different provider SDKs.
|
|
4
|
+
*
|
|
5
|
+
* @class ErrorAnalyzer
|
|
6
|
+
* @since 2.47.0
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* try {
|
|
11
|
+
* const result = await provider.chat(params);
|
|
12
|
+
* } catch (error) {
|
|
13
|
+
* const errorInfo = ErrorAnalyzer.analyzeError(error, 'OpenAI');
|
|
14
|
+
* if (errorInfo.canFailover) {
|
|
15
|
+
* // Try another provider
|
|
16
|
+
* }
|
|
17
|
+
* }
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export class ErrorAnalyzer {
|
|
21
|
+
/**
|
|
22
|
+
* Analyzes an error from an AI provider and returns standardized error information.
|
|
23
|
+
* This method extracts relevant details from provider-specific error formats and
|
|
24
|
+
* maps them to a consistent structure for easier handling.
|
|
25
|
+
*
|
|
26
|
+
* @static
|
|
27
|
+
* @param {any} error - The error object thrown by the provider SDK
|
|
28
|
+
* @param {string} [providerName] - Optional name of the provider for context
|
|
29
|
+
* @returns {AIErrorInfo} Standardized error information
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```typescript
|
|
33
|
+
* const errorInfo = ErrorAnalyzer.analyzeError(error, 'Anthropic');
|
|
34
|
+
* console.log(`Error type: ${errorInfo.errorType}`);
|
|
35
|
+
* console.log(`Can retry: ${errorInfo.severity !== 'Fatal'}`);
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
5
38
|
static analyzeError(error, providerName) {
|
|
39
|
+
// Extract HTTP status code if available
|
|
6
40
|
const httpStatusCode = this.extractHttpStatusCode(error);
|
|
41
|
+
// Determine error type based on status code and error properties
|
|
7
42
|
const errorType = this.determineErrorType(error, httpStatusCode);
|
|
43
|
+
// Determine severity
|
|
8
44
|
const severity = this.determineSeverity(errorType);
|
|
45
|
+
// Check if failover is appropriate
|
|
9
46
|
const canFailover = this.canFailoverForError(errorType);
|
|
47
|
+
// Extract retry delay if available
|
|
10
48
|
const suggestedRetryDelaySeconds = this.extractRetryDelay(error);
|
|
49
|
+
// Extract provider error code
|
|
11
50
|
const providerErrorCode = this.extractProviderErrorCode(error);
|
|
51
|
+
// Check provider error code for context length exceeded (in case message parsing missed it)
|
|
12
52
|
if (providerErrorCode === 'context_length_exceeded') {
|
|
13
53
|
return {
|
|
14
54
|
error,
|
|
@@ -40,7 +80,17 @@ class ErrorAnalyzer {
|
|
|
40
80
|
}
|
|
41
81
|
};
|
|
42
82
|
}
|
|
83
|
+
/**
|
|
84
|
+
* Extracts HTTP status code from various error object structures.
|
|
85
|
+
* Different provider SDKs store status codes in different locations.
|
|
86
|
+
*
|
|
87
|
+
* @private
|
|
88
|
+
* @static
|
|
89
|
+
* @param {any} error - The error object to extract status code from
|
|
90
|
+
* @returns {number | undefined} The HTTP status code if found, undefined otherwise
|
|
91
|
+
*/
|
|
43
92
|
static extractHttpStatusCode(error) {
|
|
93
|
+
// Common patterns across different provider SDKs
|
|
44
94
|
return error?.status ||
|
|
45
95
|
error?.statusCode ||
|
|
46
96
|
error?.response?.status ||
|
|
@@ -48,27 +98,44 @@ class ErrorAnalyzer {
|
|
|
48
98
|
error?.code ||
|
|
49
99
|
undefined;
|
|
50
100
|
}
|
|
101
|
+
/**
|
|
102
|
+
* Determines the standardized error type based on status code and error properties.
|
|
103
|
+
* Uses a combination of HTTP status codes, error messages, and error class names.
|
|
104
|
+
*
|
|
105
|
+
* @private
|
|
106
|
+
* @static
|
|
107
|
+
* @param {any} error - The error object to analyze
|
|
108
|
+
* @param {number} [statusCode] - The HTTP status code if available
|
|
109
|
+
* @returns {AIErrorType} The categorized error type
|
|
110
|
+
*/
|
|
51
111
|
static determineErrorType(error, statusCode) {
|
|
112
|
+
// IMPORTANT: Check error message patterns FIRST before status codes
|
|
113
|
+
// This allows us to correctly classify vendor-specific errors that may use
|
|
114
|
+
// non-standard status codes (e.g., xAI using 403 for billing/quota issues)
|
|
52
115
|
const errorString = (error?.message || error?.name || '').toLowerCase();
|
|
116
|
+
// Check for context length exceeded errors first (highest priority)
|
|
53
117
|
if (errorString.includes('context_length_exceeded') ||
|
|
54
118
|
errorString.includes('context length exceeded') ||
|
|
55
119
|
errorString.includes('reduce the length of the messages') ||
|
|
56
120
|
errorString.includes('maximum context length')) {
|
|
57
121
|
return 'ContextLengthExceeded';
|
|
58
122
|
}
|
|
123
|
+
// Check for rate limit errors
|
|
59
124
|
if (errorString.includes('rate limit') ||
|
|
60
125
|
errorString.includes('too many requests')) {
|
|
61
126
|
return 'RateLimit';
|
|
62
127
|
}
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
errorString.includes('
|
|
128
|
+
// Check for billing/credit/quota errors (separate from rate limits)
|
|
129
|
+
if (errorString.includes('credit') || // xAI "no credits" errors
|
|
130
|
+
errorString.includes('billing') || // Generic billing issues
|
|
131
|
+
errorString.includes('payment') || // Payment required errors
|
|
66
132
|
errorString.includes('insufficient funds') ||
|
|
67
133
|
errorString.includes('quota exceeded') ||
|
|
68
|
-
errorString.includes('balance') ||
|
|
134
|
+
errorString.includes('balance') || // Account balance issues
|
|
69
135
|
errorString.includes('no funds')) {
|
|
70
136
|
return 'NoCredit';
|
|
71
137
|
}
|
|
138
|
+
// Check for clear authentication/authorization errors (must be specific)
|
|
72
139
|
if (errorString.includes('unauthorized') ||
|
|
73
140
|
errorString.includes('authentication failed') ||
|
|
74
141
|
errorString.includes('invalid api key') ||
|
|
@@ -76,12 +143,14 @@ class ErrorAnalyzer {
|
|
|
76
143
|
errorString.includes('api key is invalid')) {
|
|
77
144
|
return 'Authentication';
|
|
78
145
|
}
|
|
146
|
+
// Check for service availability issues
|
|
79
147
|
if (errorString.includes('service unavailable') ||
|
|
80
148
|
errorString.includes('service is down') ||
|
|
81
149
|
errorString.includes('maintenance') ||
|
|
82
150
|
errorString.includes('temporarily unavailable')) {
|
|
83
151
|
return 'ServiceUnavailable';
|
|
84
152
|
}
|
|
153
|
+
// Check for network errors
|
|
85
154
|
if (errorString.includes('network') ||
|
|
86
155
|
errorString.includes('timeout') ||
|
|
87
156
|
errorString.includes('econnrefused') ||
|
|
@@ -89,6 +158,7 @@ class ErrorAnalyzer {
|
|
|
89
158
|
errorString.includes('connection reset')) {
|
|
90
159
|
return 'NetworkError';
|
|
91
160
|
}
|
|
161
|
+
// Check for model-specific errors
|
|
92
162
|
if (errorString.includes('model') &&
|
|
93
163
|
(errorString.includes('not found') ||
|
|
94
164
|
errorString.includes('does not exist') ||
|
|
@@ -96,15 +166,19 @@ class ErrorAnalyzer {
|
|
|
96
166
|
errorString.includes('not available'))) {
|
|
97
167
|
return 'ModelError';
|
|
98
168
|
}
|
|
169
|
+
// Check for vendor-specific validation errors
|
|
170
|
+
// These are field/schema validation errors that may be vendor-specific
|
|
171
|
+
// Examples: "PartListUnion is required", "field X is missing", "property Y must be..."
|
|
99
172
|
if (errorString.includes('required') ||
|
|
100
173
|
errorString.includes('validation') ||
|
|
101
174
|
errorString.includes('schema') ||
|
|
102
|
-
/\w+\s+is\s+required/.test(errorString) ||
|
|
103
|
-
/missing.*(?:field|property)/.test(errorString) ||
|
|
104
|
-
/(?:field|property).*missing/.test(errorString) ||
|
|
105
|
-
/must\s+(?:be|have|contain)/.test(errorString)) {
|
|
175
|
+
/\w+\s+is\s+required/.test(errorString) || // Matches "X is required"
|
|
176
|
+
/missing.*(?:field|property)/.test(errorString) || // Matches "missing field/property X"
|
|
177
|
+
/(?:field|property).*missing/.test(errorString) || // Matches "field/property X missing"
|
|
178
|
+
/must\s+(?:be|have|contain)/.test(errorString)) { // Matches "X must be/have/contain Y"
|
|
106
179
|
return 'VendorValidationError';
|
|
107
180
|
}
|
|
181
|
+
// Check for structural invalid request patterns (truly malformed requests)
|
|
108
182
|
if (errorString.includes('malformed json') ||
|
|
109
183
|
errorString.includes('invalid json') ||
|
|
110
184
|
errorString.includes('json parse') ||
|
|
@@ -112,10 +186,13 @@ class ErrorAnalyzer {
|
|
|
112
186
|
errorString.includes('malformed request')) {
|
|
113
187
|
return 'InvalidRequest';
|
|
114
188
|
}
|
|
189
|
+
// Generic "bad request" or "invalid request" - if no specific patterns matched above,
|
|
190
|
+
// default to VendorValidationError to allow failover (permissive approach)
|
|
115
191
|
if (errorString.includes('invalid request') ||
|
|
116
192
|
errorString.includes('bad request')) {
|
|
117
193
|
return 'VendorValidationError';
|
|
118
194
|
}
|
|
195
|
+
// Check for specific error types from provider SDKs
|
|
119
196
|
const errorTypeName = error?.constructor?.name || error?.name || '';
|
|
120
197
|
if (errorTypeName.includes('RateLimit'))
|
|
121
198
|
return 'RateLimit';
|
|
@@ -123,13 +200,17 @@ class ErrorAnalyzer {
|
|
|
123
200
|
return 'Authentication';
|
|
124
201
|
if (errorTypeName.includes('APIError') && statusCode === 500)
|
|
125
202
|
return 'InternalServerError';
|
|
203
|
+
// NOW check status codes as fallback (lower priority than message content)
|
|
126
204
|
if (statusCode) {
|
|
127
205
|
switch (statusCode) {
|
|
128
206
|
case 429:
|
|
129
207
|
return 'RateLimit';
|
|
130
208
|
case 401:
|
|
209
|
+
// 401 is almost always authentication
|
|
131
210
|
return 'Authentication';
|
|
132
211
|
case 403:
|
|
212
|
+
// 403 can be auth OR quota/billing - if we got here, message didn't clarify,
|
|
213
|
+
// so treat as retriable ServiceUnavailable to allow failover
|
|
133
214
|
return 'ServiceUnavailable';
|
|
134
215
|
case 503:
|
|
135
216
|
return 'ServiceUnavailable';
|
|
@@ -139,24 +220,35 @@ class ErrorAnalyzer {
|
|
|
139
220
|
return 'InternalServerError';
|
|
140
221
|
case 400:
|
|
141
222
|
case 422:
|
|
223
|
+
// Only treat as InvalidRequest if we have a status code but message didn't match
|
|
224
|
+
// anything more specific above
|
|
142
225
|
return 'InvalidRequest';
|
|
143
226
|
}
|
|
144
227
|
}
|
|
145
228
|
return 'Unknown';
|
|
146
229
|
}
|
|
230
|
+
/**
|
|
231
|
+
* Determines the error severity based on the error type.
|
|
232
|
+
* This helps decide whether to retry immediately, wait, or fail permanently.
|
|
233
|
+
*
|
|
234
|
+
* @private
|
|
235
|
+
* @static
|
|
236
|
+
* @param {AIErrorType} errorType - The categorized error type
|
|
237
|
+
* @returns {ErrorSeverity} The severity level of the error
|
|
238
|
+
*/
|
|
147
239
|
static determineSeverity(errorType) {
|
|
148
240
|
switch (errorType) {
|
|
149
241
|
case 'RateLimit':
|
|
150
|
-
case 'NoCredit':
|
|
242
|
+
case 'NoCredit': // Billing/credit errors are retriable with another provider
|
|
151
243
|
case 'ServiceUnavailable':
|
|
152
244
|
case 'NetworkError':
|
|
153
|
-
case 'VendorValidationError':
|
|
245
|
+
case 'VendorValidationError': // Vendor-specific validation is retriable with another vendor
|
|
154
246
|
return 'Retriable';
|
|
155
247
|
case 'InternalServerError':
|
|
156
248
|
case 'ModelError':
|
|
157
249
|
return 'Transient';
|
|
158
250
|
case 'Authentication':
|
|
159
|
-
case 'InvalidRequest':
|
|
251
|
+
case 'InvalidRequest': // Structural errors (malformed JSON, etc.)
|
|
160
252
|
return 'Fatal';
|
|
161
253
|
case 'ContextLengthExceeded':
|
|
162
254
|
return 'Fatal';
|
|
@@ -164,25 +256,50 @@ class ErrorAnalyzer {
|
|
|
164
256
|
return 'Transient';
|
|
165
257
|
}
|
|
166
258
|
}
|
|
259
|
+
/**
|
|
260
|
+
* Determines whether an error can potentially be resolved by switching providers.
|
|
261
|
+
*
|
|
262
|
+
* Strategy: We're permissive with failover - most errors should allow trying another
|
|
263
|
+
* provider/model since vendors may use different status codes and error messages.
|
|
264
|
+
* Only block failover for clear client-side structural errors that won't be fixed by switching.
|
|
265
|
+
*
|
|
266
|
+
* @private
|
|
267
|
+
* @static
|
|
268
|
+
* @param {AIErrorType} errorType - The categorized error type
|
|
269
|
+
* @returns {boolean} True if failover might help, false otherwise
|
|
270
|
+
*/
|
|
167
271
|
static canFailoverForError(errorType) {
|
|
168
272
|
switch (errorType) {
|
|
169
|
-
|
|
170
|
-
case '
|
|
171
|
-
case '
|
|
172
|
-
case '
|
|
173
|
-
case '
|
|
174
|
-
case '
|
|
175
|
-
case '
|
|
176
|
-
case '
|
|
177
|
-
case '
|
|
273
|
+
// Provider-specific errors - ALWAYS allow failover
|
|
274
|
+
case 'RateLimit': // Different provider may have capacity
|
|
275
|
+
case 'NoCredit': // Different provider may have credits/quota
|
|
276
|
+
case 'ServiceUnavailable': // Different provider may be available
|
|
277
|
+
case 'InternalServerError': // Different provider may be stable
|
|
278
|
+
case 'NetworkError': // Different provider/region may be reachable
|
|
279
|
+
case 'ModelError': // Different provider may have working model
|
|
280
|
+
case 'ContextLengthExceeded': // Different model may have larger context
|
|
281
|
+
case 'Authentication': // Different vendor may have valid API key
|
|
282
|
+
case 'VendorValidationError': // Vendor-specific validation, different vendor may accept
|
|
178
283
|
return true;
|
|
179
|
-
|
|
284
|
+
// Clear client-side structural errors - DO NOT failover (won't help)
|
|
285
|
+
case 'InvalidRequest': // Malformed JSON/syntax won't work anywhere
|
|
180
286
|
return false;
|
|
287
|
+
// Unknown errors - DEFAULT to allowing failover (permissive approach)
|
|
181
288
|
default:
|
|
182
289
|
return true;
|
|
183
290
|
}
|
|
184
291
|
}
|
|
292
|
+
/**
|
|
293
|
+
* Extracts suggested retry delay from error response.
|
|
294
|
+
* Looks for Retry-After headers and provider-specific retry delay fields.
|
|
295
|
+
*
|
|
296
|
+
* @private
|
|
297
|
+
* @static
|
|
298
|
+
* @param {any} error - The error object to extract retry delay from
|
|
299
|
+
* @returns {number | undefined} Suggested retry delay in seconds, or undefined
|
|
300
|
+
*/
|
|
185
301
|
static extractRetryDelay(error) {
|
|
302
|
+
// Check for Retry-After header (can be in seconds or HTTP date)
|
|
186
303
|
const retryAfter = error?.response?.headers?.['retry-after'] ||
|
|
187
304
|
error?.headers?.['retry-after'];
|
|
188
305
|
if (retryAfter) {
|
|
@@ -190,18 +307,31 @@ class ErrorAnalyzer {
|
|
|
190
307
|
if (!isNaN(seconds)) {
|
|
191
308
|
return seconds;
|
|
192
309
|
}
|
|
193
|
-
return
|
|
310
|
+
// Could be an HTTP date - for now, just return a default
|
|
311
|
+
return 60; // Default to 1 minute
|
|
194
312
|
}
|
|
313
|
+
// Some providers include retry delay in error object
|
|
195
314
|
if (error?.retryDelay) {
|
|
196
315
|
return error.retryDelay;
|
|
197
316
|
}
|
|
317
|
+
// Default retry delays based on error type
|
|
198
318
|
const statusCode = this.extractHttpStatusCode(error);
|
|
199
319
|
if (statusCode === 429) {
|
|
200
|
-
return 30;
|
|
320
|
+
return 30; // Default 30 seconds for rate limits
|
|
201
321
|
}
|
|
202
322
|
return undefined;
|
|
203
323
|
}
|
|
324
|
+
/**
|
|
325
|
+
* Extracts the provider-specific error code from the error object.
|
|
326
|
+
* Different providers store error codes in different locations.
|
|
327
|
+
*
|
|
328
|
+
* @private
|
|
329
|
+
* @static
|
|
330
|
+
* @param {any} error - The error object to extract provider code from
|
|
331
|
+
* @returns {string | undefined} The provider error code if found, undefined otherwise
|
|
332
|
+
*/
|
|
204
333
|
static extractProviderErrorCode(error) {
|
|
334
|
+
// Try to extract from various common locations
|
|
205
335
|
const code = error?.code ||
|
|
206
336
|
error?.errorCode ||
|
|
207
337
|
error?.error?.code ||
|
|
@@ -210,9 +340,11 @@ class ErrorAnalyzer {
|
|
|
210
340
|
if (code) {
|
|
211
341
|
return code;
|
|
212
342
|
}
|
|
343
|
+
// Try to parse from error message if it contains JSON
|
|
213
344
|
const errorMessage = error?.message || error?.errorMessage || '';
|
|
214
345
|
if (errorMessage.includes('{') && errorMessage.includes('}')) {
|
|
215
346
|
try {
|
|
347
|
+
// Extract JSON from error message
|
|
216
348
|
const jsonMatch = errorMessage.match(/\{.*\}/);
|
|
217
349
|
if (jsonMatch) {
|
|
218
350
|
const parsed = JSON.parse(jsonMatch[0]);
|
|
@@ -220,10 +352,10 @@ class ErrorAnalyzer {
|
|
|
220
352
|
}
|
|
221
353
|
}
|
|
222
354
|
catch (parseError) {
|
|
355
|
+
// If JSON parsing fails, continue with undefined
|
|
223
356
|
}
|
|
224
357
|
}
|
|
225
358
|
return undefined;
|
|
226
359
|
}
|
|
227
360
|
}
|
|
228
|
-
exports.ErrorAnalyzer = ErrorAnalyzer;
|
|
229
361
|
//# sourceMappingURL=errorAnalyzer.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errorAnalyzer.js","sourceRoot":"","sources":["../../src/generic/errorAnalyzer.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"errorAnalyzer.js","sourceRoot":"","sources":["../../src/generic/errorAnalyzer.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,OAAO,aAAa;IACtB;;;;;;;;;;;;;;;;OAgBG;IACH,MAAM,CAAC,YAAY,CAAC,KAAU,EAAE,YAAqB;QACjD,wCAAwC;QACxC,MAAM,cAAc,GAAG,IAAI,CAAC,qBAAqB,CAAC,KAAK,CAAC,CAAC;QAEzD,iEAAiE;QACjE,MAAM,SAAS,GAAG,IAAI,CAAC,kBAAkB,CAAC,KAAK,EAAE,cAAc,CAAC,CAAC;QAEjE,qBAAqB;QACrB,MAAM,QAAQ,GAAG,IAAI,CAAC,iBAAiB,CAAC,SAAS,CAAC,CAAC;QAEnD,mCAAmC;QACnC,MAAM,WAAW,GAAG,IAAI,CAAC,mBAAmB,CAAC,SAAS,CAAC,CAAC;QAExD,mCAAmC;QACnC,MAAM,0BAA0B,GAAG,IAAI,CAAC,iBAAiB,CAAC,KAAK,CAAC,CAAC;QAEjE,8BAA8B;QAC9B,MAAM,iBAAiB,G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AAC,QAAQ,CAAC,GAAG,CAAC,IAAI,YAAY,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,CAAC;YAC3D,IAAI,CAAC;gBACD,kCAAkC;gBAClC,MAAM,SAAS,GAAG,YAAY,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC;gBAC/C,IAAI,SAAS,EAAE,CAAC;oBACZ,MAAM,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC;oBACxC,OAAO,MAAM,EAAE,KAAK,EAAE,IAAI,IAAI,MAAM,EAAE,IAAI,IAAI,SAAS,CAAC;gBAC5D,CAAC;YACL,CAAC;YAAC,OAAO,UAAU,EAAE,CAAC;gBAClB,iDAAiD;YACrD,CAAC;QACL,CAAC;QAED,OAAO,SAAS,CAAC;IACrB,CAAC;CACJ"}
|
|
@@ -1,13 +1,129 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Categorizes error types that can occur during AI model operations.
|
|
3
|
+
* These categories help determine appropriate retry and failover strategies.
|
|
4
|
+
*
|
|
5
|
+
* @typedef {'RateLimit' | 'NoCredit' | 'Authentication' | 'ServiceUnavailable' | 'InternalServerError' | 'NetworkError' | 'InvalidRequest' | 'VendorValidationError' | 'ContextLengthExceeded' | 'ModelError' | 'Unknown'} AIErrorType
|
|
6
|
+
*
|
|
7
|
+
* @description
|
|
8
|
+
* - `RateLimit`: Rate limit exceeded - typically HTTP 429. Suggests switching to another provider or waiting
|
|
9
|
+
* - `NoCredit`: Insufficient credits or quota for billing - typically HTTP 402/403. Account has no credits/balance or quota exhausted. Suggests switching to another provider
|
|
10
|
+
* - `Authentication`: Authentication or authorization failure - typically HTTP 401/403. Usually indicates invalid API key or permissions issue
|
|
11
|
+
* - `ServiceUnavailable`: Service temporarily unavailable - typically HTTP 503. Suggests the service is down or overloaded
|
|
12
|
+
* - `InternalServerError`: Internal server error - typically HTTP 500. Indicates a problem on the provider's side
|
|
13
|
+
* - `NetworkError`: Network connectivity issues. Connection timeouts, DNS failures, etc.
|
|
14
|
+
* - `InvalidRequest`: Invalid request format or parameters - typically HTTP 400. Usually indicates a structural problem with the request (malformed JSON, invalid syntax)
|
|
15
|
+
* - `VendorValidationError`: Vendor-specific schema or field validation error - typically HTTP 400/422. Field requirements, missing properties, schema validation that may be vendor-specific
|
|
16
|
+
* - `ContextLengthExceeded`: Context length exceeded - typically HTTP 400 with context_length_exceeded code. Suggests switching to a model with larger context window
|
|
17
|
+
* - `ModelError`: Model-specific errors. Model not found, model overloaded, etc.
|
|
18
|
+
* - `Unknown`: Unknown or unclassified error
|
|
19
|
+
*
|
|
20
|
+
* @since 2.47.0
|
|
21
|
+
*/
|
|
1
22
|
export type AIErrorType = 'RateLimit' | 'NoCredit' | 'Authentication' | 'ServiceUnavailable' | 'InternalServerError' | 'NetworkError' | 'InvalidRequest' | 'VendorValidationError' | 'ContextLengthExceeded' | 'ModelError' | 'Unknown';
|
|
23
|
+
/**
|
|
24
|
+
* Categorizes error severity levels to guide retry and failover strategies.
|
|
25
|
+
*
|
|
26
|
+
* @typedef {'Transient' | 'Retriable' | 'Fatal'} ErrorSeverity
|
|
27
|
+
*
|
|
28
|
+
* @description
|
|
29
|
+
* - `Transient`: Temporary error that may resolve with immediate retry
|
|
30
|
+
* - `Retriable`: Error that requires waiting or switching providers before retry
|
|
31
|
+
* - `Fatal`: Fatal error that won't be resolved by retrying
|
|
32
|
+
*
|
|
33
|
+
* @since 2.47.0
|
|
34
|
+
*/
|
|
2
35
|
export type ErrorSeverity = 'Transient' | 'Retriable' | 'Fatal';
|
|
36
|
+
/**
|
|
37
|
+
* Provides detailed, structured error information for AI operations.
|
|
38
|
+
* This interface enables intelligent error handling, retry logic, and provider failover decisions.
|
|
39
|
+
*
|
|
40
|
+
* @interface AIErrorInfo
|
|
41
|
+
* @since 2.47.0
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```typescript
|
|
45
|
+
* const errorInfo: AIErrorInfo = {
|
|
46
|
+
* httpStatusCode: 429,
|
|
47
|
+
* errorType: 'RateLimit',
|
|
48
|
+
* severity: 'Retriable',
|
|
49
|
+
* suggestedRetryDelaySeconds: 30,
|
|
50
|
+
* canFailover: true,
|
|
51
|
+
* providerErrorCode: 'rate_limit_exceeded'
|
|
52
|
+
* };
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
3
55
|
export interface AIErrorInfo {
|
|
56
|
+
/**
|
|
57
|
+
* HTTP status code returned by the provider's API.
|
|
58
|
+
* Common codes include:
|
|
59
|
+
* - 429: Rate limit exceeded
|
|
60
|
+
* - 401/403: Authentication/Authorization failure
|
|
61
|
+
* - 500: Internal server error
|
|
62
|
+
* - 503: Service unavailable
|
|
63
|
+
*
|
|
64
|
+
* @type {number | undefined}
|
|
65
|
+
* @memberof AIErrorInfo
|
|
66
|
+
*/
|
|
4
67
|
httpStatusCode?: number;
|
|
68
|
+
/**
|
|
69
|
+
* Categorized error type for standardized error handling.
|
|
70
|
+
* This allows consistent error handling across different AI providers.
|
|
71
|
+
*
|
|
72
|
+
* @type {AIErrorType}
|
|
73
|
+
* @memberof AIErrorInfo
|
|
74
|
+
*/
|
|
5
75
|
errorType: AIErrorType;
|
|
76
|
+
/**
|
|
77
|
+
* Severity level indicating how the error should be handled.
|
|
78
|
+
* Used to determine whether to retry immediately, wait, or fail permanently.
|
|
79
|
+
*
|
|
80
|
+
* @type {ErrorSeverity}
|
|
81
|
+
* @memberof AIErrorInfo
|
|
82
|
+
*/
|
|
6
83
|
severity: ErrorSeverity;
|
|
84
|
+
/**
|
|
85
|
+
* Suggested delay in seconds before retrying the operation.
|
|
86
|
+
* For rate limits, this often comes from the provider's Retry-After header.
|
|
87
|
+
* If undefined, the caller should use exponential backoff or default delays.
|
|
88
|
+
*
|
|
89
|
+
* @type {number | undefined}
|
|
90
|
+
* @memberof AIErrorInfo
|
|
91
|
+
*/
|
|
7
92
|
suggestedRetryDelaySeconds?: number;
|
|
93
|
+
/**
|
|
94
|
+
* Indicates whether this error might be resolved by switching to another provider.
|
|
95
|
+
* - `true`: Error is provider-specific (rate limit, service down)
|
|
96
|
+
* - `false`: Error is request-specific (bad API key, invalid parameters)
|
|
97
|
+
*
|
|
98
|
+
* @type {boolean}
|
|
99
|
+
* @memberof AIErrorInfo
|
|
100
|
+
*/
|
|
8
101
|
canFailover: boolean;
|
|
102
|
+
/**
|
|
103
|
+
* Original error code from the provider's SDK or API.
|
|
104
|
+
* This preserves provider-specific error codes for debugging.
|
|
105
|
+
* Examples: 'rate_limit_exceeded', 'model_not_found', 'invalid_api_key'
|
|
106
|
+
*
|
|
107
|
+
* @type {string | undefined}
|
|
108
|
+
* @memberof AIErrorInfo
|
|
109
|
+
*/
|
|
9
110
|
providerErrorCode?: string;
|
|
111
|
+
/**
|
|
112
|
+
* Additional context or metadata about the error.
|
|
113
|
+
* Can include provider name, error timestamps, request IDs, etc.
|
|
114
|
+
* This field is flexible to accommodate provider-specific information.
|
|
115
|
+
*
|
|
116
|
+
* @type {Record<string, any> | undefined}
|
|
117
|
+
* @memberof AIErrorInfo
|
|
118
|
+
*/
|
|
10
119
|
context?: Record<string, any>;
|
|
120
|
+
/**
|
|
121
|
+
* Original error object thrown by the provider's SDK or API.
|
|
122
|
+
* This allows for deeper inspection if needed.
|
|
123
|
+
*
|
|
124
|
+
* @type {any}
|
|
125
|
+
* @memberof AIErrorInfo
|
|
126
|
+
*/
|
|
11
127
|
error?: any;
|
|
12
128
|
}
|
|
13
129
|
//# sourceMappingURL=errorTypes.d.ts.map
|