@defai.digital/provider-adapters 13.0.3
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/LICENSE +214 -0
- package/dist/cli-adapter.d.ts +23 -0
- package/dist/cli-adapter.d.ts.map +1 -0
- package/dist/cli-adapter.js +151 -0
- package/dist/cli-adapter.js.map +1 -0
- package/dist/error-classifier.d.ts +34 -0
- package/dist/error-classifier.d.ts.map +1 -0
- package/dist/error-classifier.js +257 -0
- package/dist/error-classifier.js.map +1 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +25 -0
- package/dist/index.js.map +1 -0
- package/dist/output-parser.d.ts +37 -0
- package/dist/output-parser.d.ts.map +1 -0
- package/dist/output-parser.js +251 -0
- package/dist/output-parser.js.map +1 -0
- package/dist/process-manager.d.ts +43 -0
- package/dist/process-manager.d.ts.map +1 -0
- package/dist/process-manager.js +184 -0
- package/dist/process-manager.js.map +1 -0
- package/dist/providers/claude.d.ts +18 -0
- package/dist/providers/claude.d.ts.map +1 -0
- package/dist/providers/claude.js +39 -0
- package/dist/providers/claude.js.map +1 -0
- package/dist/providers/codex.d.ts +18 -0
- package/dist/providers/codex.d.ts.map +1 -0
- package/dist/providers/codex.js +39 -0
- package/dist/providers/codex.js.map +1 -0
- package/dist/providers/gemini.d.ts +18 -0
- package/dist/providers/gemini.d.ts.map +1 -0
- package/dist/providers/gemini.js +39 -0
- package/dist/providers/gemini.js.map +1 -0
- package/dist/providers/glm.d.ts +18 -0
- package/dist/providers/glm.d.ts.map +1 -0
- package/dist/providers/glm.js +40 -0
- package/dist/providers/glm.js.map +1 -0
- package/dist/providers/grok.d.ts +18 -0
- package/dist/providers/grok.d.ts.map +1 -0
- package/dist/providers/grok.js +40 -0
- package/dist/providers/grok.js.map +1 -0
- package/dist/providers/index.d.ts +32 -0
- package/dist/providers/index.d.ts.map +1 -0
- package/dist/providers/index.js +49 -0
- package/dist/providers/index.js.map +1 -0
- package/dist/providers/qwen.d.ts +18 -0
- package/dist/providers/qwen.d.ts.map +1 -0
- package/dist/providers/qwen.js +39 -0
- package/dist/providers/qwen.js.map +1 -0
- package/dist/registry.d.ts +73 -0
- package/dist/registry.d.ts.map +1 -0
- package/dist/registry.js +145 -0
- package/dist/registry.js.map +1 -0
- package/dist/resilient-registry.d.ts +134 -0
- package/dist/resilient-registry.d.ts.map +1 -0
- package/dist/resilient-registry.js +296 -0
- package/dist/resilient-registry.js.map +1 -0
- package/dist/types.d.ts +238 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +14 -0
- package/dist/types.js.map +1 -0
- package/package.json +43 -0
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Error classifier for CLI-based providers
|
|
3
|
+
*
|
|
4
|
+
* Classifies errors from:
|
|
5
|
+
* - CLI exit codes
|
|
6
|
+
* - stderr output
|
|
7
|
+
* - Error messages
|
|
8
|
+
*
|
|
9
|
+
* Provides retry/fallback guidance based on error type
|
|
10
|
+
*/
|
|
11
|
+
/**
|
|
12
|
+
* Error patterns for classification
|
|
13
|
+
* Organized by category, each pattern is case-insensitive regex
|
|
14
|
+
*/
|
|
15
|
+
const ERROR_PATTERNS = {
|
|
16
|
+
// Quota exhausted - fallback to different provider
|
|
17
|
+
quota: [
|
|
18
|
+
/insufficient.?quota/i,
|
|
19
|
+
/quota.?exceeded/i,
|
|
20
|
+
/billing.?hard.?limit/i,
|
|
21
|
+
/RESOURCE_EXHAUSTED/i,
|
|
22
|
+
/credit.?limit/i,
|
|
23
|
+
/usage.?limit/i,
|
|
24
|
+
],
|
|
25
|
+
// Rate limit - retry with backoff
|
|
26
|
+
rate_limit: [
|
|
27
|
+
/rate.?limit/i,
|
|
28
|
+
/too.?many.?requests/i,
|
|
29
|
+
/overloaded/i,
|
|
30
|
+
/RATE_LIMIT_EXCEEDED/i,
|
|
31
|
+
/throttl/i,
|
|
32
|
+
/\b429\b/, // Word boundary to prevent false matches like "4290"
|
|
33
|
+
],
|
|
34
|
+
// Authentication - don't retry
|
|
35
|
+
authentication: [
|
|
36
|
+
/invalid.?api.?key/i,
|
|
37
|
+
/unauthorized/i,
|
|
38
|
+
/UNAUTHENTICATED/i,
|
|
39
|
+
/PERMISSION_DENIED/i,
|
|
40
|
+
/authentication.?failed/i,
|
|
41
|
+
/not.?authenticated/i,
|
|
42
|
+
/\b401\b/, // Word boundary to prevent false matches
|
|
43
|
+
/\b403\b/, // Word boundary to prevent false matches
|
|
44
|
+
],
|
|
45
|
+
// Validation - don't retry
|
|
46
|
+
validation: [
|
|
47
|
+
/invalid.?request/i,
|
|
48
|
+
/malformed/i,
|
|
49
|
+
/bad.?request/i,
|
|
50
|
+
/validation.?error/i,
|
|
51
|
+
/invalid.?parameter/i,
|
|
52
|
+
/\b400\b/, // Word boundary to prevent false matches
|
|
53
|
+
],
|
|
54
|
+
// Network - retry
|
|
55
|
+
network: [
|
|
56
|
+
/ECONNRESET/,
|
|
57
|
+
/ETIMEDOUT/,
|
|
58
|
+
/ENOTFOUND/,
|
|
59
|
+
/ECONNREFUSED/,
|
|
60
|
+
/network.?error/i,
|
|
61
|
+
/connection.?failed/i,
|
|
62
|
+
/DEADLINE_EXCEEDED/i,
|
|
63
|
+
/socket.?hang.?up/i,
|
|
64
|
+
],
|
|
65
|
+
// Server errors - retry then fallback
|
|
66
|
+
server: [
|
|
67
|
+
/internal.?server.?error/i,
|
|
68
|
+
/service.?unavailable/i,
|
|
69
|
+
/bad.?gateway/i,
|
|
70
|
+
/\b500\b/, // Word boundary to prevent false matches
|
|
71
|
+
/\b502\b/, // Word boundary to prevent false matches
|
|
72
|
+
/\b503\b/, // Word boundary to prevent false matches
|
|
73
|
+
/\b504\b/, // Word boundary to prevent false matches
|
|
74
|
+
],
|
|
75
|
+
// Timeout - retry or fallback
|
|
76
|
+
timeout: [
|
|
77
|
+
/timed?.?out/i,
|
|
78
|
+
/timeout/i,
|
|
79
|
+
/SIGTERM/,
|
|
80
|
+
/SIGKILL/,
|
|
81
|
+
],
|
|
82
|
+
// Not found - don't retry
|
|
83
|
+
not_found: [
|
|
84
|
+
/command.?not.?found/i,
|
|
85
|
+
/ENOENT/,
|
|
86
|
+
/not.?found/i,
|
|
87
|
+
/model.?not.?found/i,
|
|
88
|
+
/\b404\b/, // Word boundary to prevent false matches
|
|
89
|
+
],
|
|
90
|
+
// Configuration - don't retry
|
|
91
|
+
configuration: [
|
|
92
|
+
/not.?configured/i,
|
|
93
|
+
/missing.?config/i,
|
|
94
|
+
/invalid.?config/i,
|
|
95
|
+
/cli.?not.?installed/i,
|
|
96
|
+
],
|
|
97
|
+
// Unknown - default
|
|
98
|
+
unknown: [],
|
|
99
|
+
};
|
|
100
|
+
/**
|
|
101
|
+
* Retry guidance by error category
|
|
102
|
+
*/
|
|
103
|
+
const RETRY_GUIDANCE = {
|
|
104
|
+
quota: { shouldRetry: false, shouldFallback: true },
|
|
105
|
+
rate_limit: { shouldRetry: true, shouldFallback: false },
|
|
106
|
+
authentication: { shouldRetry: false, shouldFallback: false },
|
|
107
|
+
validation: { shouldRetry: false, shouldFallback: false },
|
|
108
|
+
network: { shouldRetry: true, shouldFallback: true },
|
|
109
|
+
server: { shouldRetry: true, shouldFallback: true },
|
|
110
|
+
timeout: { shouldRetry: true, shouldFallback: true },
|
|
111
|
+
not_found: { shouldRetry: false, shouldFallback: true },
|
|
112
|
+
configuration: { shouldRetry: false, shouldFallback: false },
|
|
113
|
+
unknown: { shouldRetry: false, shouldFallback: true },
|
|
114
|
+
};
|
|
115
|
+
/**
|
|
116
|
+
* Classifies an error from various sources
|
|
117
|
+
*
|
|
118
|
+
* @param error - The error to classify (Error, string, or unknown)
|
|
119
|
+
* @returns Classified error with retry/fallback guidance
|
|
120
|
+
*/
|
|
121
|
+
export function classifyError(error) {
|
|
122
|
+
const message = extractErrorMessage(error);
|
|
123
|
+
const category = categorizeError(message);
|
|
124
|
+
const guidance = RETRY_GUIDANCE[category];
|
|
125
|
+
return {
|
|
126
|
+
category,
|
|
127
|
+
message,
|
|
128
|
+
shouldRetry: guidance.shouldRetry,
|
|
129
|
+
shouldFallback: guidance.shouldFallback,
|
|
130
|
+
retryAfterMs: extractRetryAfter(message),
|
|
131
|
+
originalError: error,
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Safely truncates a string without cutting in the middle of multi-byte UTF-8 characters
|
|
136
|
+
* Uses Array.from to properly handle grapheme clusters
|
|
137
|
+
*/
|
|
138
|
+
function safeTruncate(str, maxLength) {
|
|
139
|
+
const chars = Array.from(str);
|
|
140
|
+
if (chars.length <= maxLength) {
|
|
141
|
+
return str;
|
|
142
|
+
}
|
|
143
|
+
return chars.slice(0, maxLength).join('');
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Classifies a CLI spawn result
|
|
147
|
+
*
|
|
148
|
+
* @param result - The spawn result to classify
|
|
149
|
+
* @returns Classified error with retry/fallback guidance
|
|
150
|
+
*/
|
|
151
|
+
export function classifySpawnResult(result) {
|
|
152
|
+
// Check for timeout
|
|
153
|
+
if (result.timedOut) {
|
|
154
|
+
return {
|
|
155
|
+
category: 'timeout',
|
|
156
|
+
message: 'Process timed out',
|
|
157
|
+
shouldRetry: true,
|
|
158
|
+
shouldFallback: true,
|
|
159
|
+
retryAfterMs: null,
|
|
160
|
+
originalError: result,
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
// Check stderr first, then exit code
|
|
164
|
+
const errorText = result.stderr.length > 0 ? result.stderr : `Exit code: ${String(result.exitCode)}`;
|
|
165
|
+
const category = categorizeError(errorText);
|
|
166
|
+
const guidance = RETRY_GUIDANCE[category];
|
|
167
|
+
return {
|
|
168
|
+
category,
|
|
169
|
+
message: safeTruncate(errorText, 500), // Truncate long error messages safely
|
|
170
|
+
shouldRetry: guidance.shouldRetry,
|
|
171
|
+
shouldFallback: guidance.shouldFallback,
|
|
172
|
+
retryAfterMs: extractRetryAfter(errorText),
|
|
173
|
+
originalError: result,
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Extracts error message from various error types
|
|
178
|
+
*/
|
|
179
|
+
function extractErrorMessage(error) {
|
|
180
|
+
if (error instanceof Error) {
|
|
181
|
+
return error.message;
|
|
182
|
+
}
|
|
183
|
+
if (typeof error === 'string') {
|
|
184
|
+
return error;
|
|
185
|
+
}
|
|
186
|
+
if (typeof error === 'object' && error !== null) {
|
|
187
|
+
// Handle objects with message property
|
|
188
|
+
const obj = error;
|
|
189
|
+
if (typeof obj.message === 'string') {
|
|
190
|
+
return obj.message;
|
|
191
|
+
}
|
|
192
|
+
if (typeof obj.error === 'string') {
|
|
193
|
+
return obj.error;
|
|
194
|
+
}
|
|
195
|
+
if (typeof obj.error === 'object' && obj.error !== null) {
|
|
196
|
+
const nested = obj.error;
|
|
197
|
+
if (typeof nested.message === 'string') {
|
|
198
|
+
return nested.message;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
return String(error);
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Categorizes an error message using pattern matching
|
|
206
|
+
*/
|
|
207
|
+
function categorizeError(message) {
|
|
208
|
+
// Check each category's patterns
|
|
209
|
+
for (const [category, patterns] of Object.entries(ERROR_PATTERNS)) {
|
|
210
|
+
for (const pattern of patterns) {
|
|
211
|
+
if (pattern.test(message)) {
|
|
212
|
+
return category;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
// Default to unknown
|
|
217
|
+
return 'unknown';
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Extracts retry-after duration from error message
|
|
221
|
+
* Looks for common patterns like "retry after X seconds"
|
|
222
|
+
*/
|
|
223
|
+
function extractRetryAfter(message) {
|
|
224
|
+
// Pattern: "retry after X seconds"
|
|
225
|
+
const secondsMatch = /retry.?after\s+(\d+)\s*s/i.exec(message);
|
|
226
|
+
if (secondsMatch?.[1] !== undefined) {
|
|
227
|
+
return parseInt(secondsMatch[1], 10) * 1000;
|
|
228
|
+
}
|
|
229
|
+
// Pattern: "retry after X milliseconds" or "retry after Xms"
|
|
230
|
+
const msMatch = /retry.?after\s+(\d+)\s*(?:ms|milliseconds)/i.exec(message);
|
|
231
|
+
if (msMatch?.[1] !== undefined) {
|
|
232
|
+
return parseInt(msMatch[1], 10);
|
|
233
|
+
}
|
|
234
|
+
// Pattern: "wait X seconds"
|
|
235
|
+
const waitMatch = /wait\s+(\d+)\s*s/i.exec(message);
|
|
236
|
+
if (waitMatch?.[1] !== undefined) {
|
|
237
|
+
return parseInt(waitMatch[1], 10) * 1000;
|
|
238
|
+
}
|
|
239
|
+
// For rate limits without explicit retry-after, suggest 1 second
|
|
240
|
+
if (/rate.?limit/i.test(message) || /too.?many.?requests/i.test(message)) {
|
|
241
|
+
return 1000;
|
|
242
|
+
}
|
|
243
|
+
return null;
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* Checks if an error is retryable
|
|
247
|
+
*/
|
|
248
|
+
export function isRetryable(error) {
|
|
249
|
+
return error.shouldRetry;
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* Checks if fallback to another provider should be attempted
|
|
253
|
+
*/
|
|
254
|
+
export function shouldFallback(error) {
|
|
255
|
+
return error.shouldFallback;
|
|
256
|
+
}
|
|
257
|
+
//# sourceMappingURL=error-classifier.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"error-classifier.js","sourceRoot":"","sources":["../src/error-classifier.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAIH;;;GAGG;AACH,MAAM,cAAc,GAA6C;IAC/D,mDAAmD;IACnD,KAAK,EAAE;QACL,sBAAsB;QACtB,kBAAkB;QAClB,uBAAuB;QACvB,qBAAqB;QACrB,gBAAgB;QAChB,eAAe;KAChB;IAED,kCAAkC;IAClC,UAAU,EAAE;QACV,cAAc;QACd,sBAAsB;QACtB,aAAa;QACb,sBA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AC,GAAG,IAAI,CAAC;IAC9C,CAAC;IAED,6DAA6D;IAC7D,MAAM,OAAO,GAAG,6CAA6C,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;IAC5E,IAAI,OAAO,EAAE,CAAC,CAAC,CAAC,KAAK,SAAS,EAAE,CAAC;QAC/B,OAAO,QAAQ,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;IAClC,CAAC;IAED,4BAA4B;IAC5B,MAAM,SAAS,GAAG,mBAAmB,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;IACpD,IAAI,SAAS,EAAE,CAAC,CAAC,CAAC,KAAK,SAAS,EAAE,CAAC;QACjC,OAAO,QAAQ,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,GAAG,IAAI,CAAC;IAC3C,CAAC;IAED,iEAAiE;IACjE,IAAI,cAAc,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,sBAAsB,CAAC,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC;QACzE,OAAO,IAAI,CAAC;IACd,CAAC;IAED,OAAO,IAAI,CAAC;AACd,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,WAAW,CAAC,KAAsB;IAChD,OAAO,KAAK,CAAC,WAAW,CAAC;AAC3B,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,cAAc,CAAC,KAAsB;IACnD,OAAO,KAAK,CAAC,cAAc,CAAC;AAC9B,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Provider adapters (CLI-based)
|
|
3
|
+
*
|
|
4
|
+
* DESIGN: AutomatosX does NOT manage credentials.
|
|
5
|
+
* Each provider CLI handles its own authentication:
|
|
6
|
+
* - claude, gemini, codex: Official CLIs with built-in auth
|
|
7
|
+
* - qwen: Qwen CLI with DashScope auth
|
|
8
|
+
* - ax-glm: GLM CLI wrapper (handles ZAI_API_KEY)
|
|
9
|
+
* - ax-grok: Grok CLI wrapper (handles XAI_API_KEY)
|
|
10
|
+
*/
|
|
11
|
+
export type { MessageRole, Message, CompletionRequest, CompletionResponse, CompletionSuccess, CompletionFailure, TokenUsage, LLMProvider, CLIProviderConfig, ModelConfig, HealthCheckResult, ErrorCategory, ClassifiedError, SpawnResult, SpawnOptions, ParsedOutput, } from './types.js';
|
|
12
|
+
export { createCLIAdapter, createCLIAdapters } from './cli-adapter.js';
|
|
13
|
+
export { spawnCLI, isCommandAvailable, getCommandVersion, buildPromptFromMessages, } from './process-manager.js';
|
|
14
|
+
export { parseOutput, estimateTokenUsage, extractOrEstimateTokenUsage, } from './output-parser.js';
|
|
15
|
+
export { classifyError, classifySpawnResult, isRetryable, shouldFallback, } from './error-classifier.js';
|
|
16
|
+
export { claudeConfig, geminiConfig, codexConfig, qwenConfig, glmConfig, grokConfig, ALL_PROVIDER_CONFIGS, getProviderConfig, getAllProviderIds, } from './providers/index.js';
|
|
17
|
+
export { ProviderRegistry, createProviderRegistry, createEmptyRegistry, } from './registry.js';
|
|
18
|
+
export { ResilientProviderRegistry, createResilientProviderRegistry, type ResilientRegistryConfig, } from './resilient-registry.js';
|
|
19
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAGH,YAAY,EACV,WAAW,EACX,OAAO,EACP,iBAAiB,EACjB,kBAAkB,EAClB,iBAAiB,EACjB,iBAAiB,EACjB,UAAU,EACV,WAAW,EACX,iBAAiB,EACjB,WAAW,EACX,iBAAiB,EACjB,aAAa,EACb,eAAe,EACf,WAAW,EACX,YAAY,EACZ,YAAY,GACb,MAAM,YAAY,CAAC;AAGpB,OAAO,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AAGvE,OAAO,EACL,QAAQ,EACR,kBAAkB,EAClB,iBAAiB,EACjB,uBAAuB,GACxB,MAAM,sBAAsB,CAAC;AAG9B,OAAO,EACL,WAAW,EACX,kBAAkB,EAClB,2BAA2B,GAC5B,MAAM,oBAAoB,CAAC;AAG5B,OAAO,EACL,aAAa,EACb,mBAAmB,EACnB,WAAW,EACX,cAAc,GACf,MAAM,uBAAuB,CAAC;AAG/B,OAAO,EACL,YAAY,EACZ,YAAY,EACZ,WAAW,EACX,UAAU,EACV,SAAS,EACT,UAAU,EACV,oBAAoB,EACpB,iBAAiB,EACjB,iBAAiB,GAClB,MAAM,sBAAsB,CAAC;AAG9B,OAAO,EACL,gBAAgB,EAChB,sBAAsB,EACtB,mBAAmB,GACpB,MAAM,eAAe,CAAC;AAGvB,OAAO,EACL,yBAAyB,EACzB,+BAA+B,EAC/B,KAAK,uBAAuB,GAC7B,MAAM,yBAAyB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Provider adapters (CLI-based)
|
|
3
|
+
*
|
|
4
|
+
* DESIGN: AutomatosX does NOT manage credentials.
|
|
5
|
+
* Each provider CLI handles its own authentication:
|
|
6
|
+
* - claude, gemini, codex: Official CLIs with built-in auth
|
|
7
|
+
* - qwen: Qwen CLI with DashScope auth
|
|
8
|
+
* - ax-glm: GLM CLI wrapper (handles ZAI_API_KEY)
|
|
9
|
+
* - ax-grok: Grok CLI wrapper (handles XAI_API_KEY)
|
|
10
|
+
*/
|
|
11
|
+
// CLI Adapter
|
|
12
|
+
export { createCLIAdapter, createCLIAdapters } from './cli-adapter.js';
|
|
13
|
+
// Process Manager
|
|
14
|
+
export { spawnCLI, isCommandAvailable, getCommandVersion, buildPromptFromMessages, } from './process-manager.js';
|
|
15
|
+
// Output Parser
|
|
16
|
+
export { parseOutput, estimateTokenUsage, extractOrEstimateTokenUsage, } from './output-parser.js';
|
|
17
|
+
// Error Classifier
|
|
18
|
+
export { classifyError, classifySpawnResult, isRetryable, shouldFallback, } from './error-classifier.js';
|
|
19
|
+
// Provider Configurations
|
|
20
|
+
export { claudeConfig, geminiConfig, codexConfig, qwenConfig, glmConfig, grokConfig, ALL_PROVIDER_CONFIGS, getProviderConfig, getAllProviderIds, } from './providers/index.js';
|
|
21
|
+
// Provider Registry
|
|
22
|
+
export { ProviderRegistry, createProviderRegistry, createEmptyRegistry, } from './registry.js';
|
|
23
|
+
// Resilient Provider Registry
|
|
24
|
+
export { ResilientProviderRegistry, createResilientProviderRegistry, } from './resilient-registry.js';
|
|
25
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAsBH,cAAc;AACd,OAAO,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AAEvE,kBAAkB;AAClB,OAAO,EACL,QAAQ,EACR,kBAAkB,EAClB,iBAAiB,EACjB,uBAAuB,GACxB,MAAM,sBAAsB,CAAC;AAE9B,gBAAgB;AAChB,OAAO,EACL,WAAW,EACX,kBAAkB,EAClB,2BAA2B,GAC5B,MAAM,oBAAoB,CAAC;AAE5B,mBAAmB;AACnB,OAAO,EACL,aAAa,EACb,mBAAmB,EACnB,WAAW,EACX,cAAc,GACf,MAAM,uBAAuB,CAAC;AAE/B,0BAA0B;AAC1B,OAAO,EACL,YAAY,EACZ,YAAY,EACZ,WAAW,EACX,UAAU,EACV,SAAS,EACT,UAAU,EACV,oBAAoB,EACpB,iBAAiB,EACjB,iBAAiB,GAClB,MAAM,sBAAsB,CAAC;AAE9B,oBAAoB;AACpB,OAAO,EACL,gBAAgB,EAChB,sBAAsB,EACtB,mBAAmB,GACpB,MAAM,eAAe,CAAC;AAEvB,8BAA8B;AAC9B,OAAO,EACL,yBAAyB,EACzB,+BAA+B,GAEhC,MAAM,yBAAyB,CAAC"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Output parser for CLI responses
|
|
3
|
+
*
|
|
4
|
+
* Handles different output formats:
|
|
5
|
+
* - json: Single JSON object
|
|
6
|
+
* - stream-json: JSON Lines (one JSON object per line)
|
|
7
|
+
* - text: Plain text output
|
|
8
|
+
*/
|
|
9
|
+
import type { ParsedOutput, TokenUsage, CLIProviderConfig } from './types.js';
|
|
10
|
+
/**
|
|
11
|
+
* Parses CLI output based on the expected format
|
|
12
|
+
*
|
|
13
|
+
* @param stdout - Raw stdout from CLI process
|
|
14
|
+
* @param format - Expected output format
|
|
15
|
+
* @returns Parsed output with content and optional metadata
|
|
16
|
+
*/
|
|
17
|
+
export declare function parseOutput(stdout: string, format: CLIProviderConfig['outputFormat']): ParsedOutput;
|
|
18
|
+
/**
|
|
19
|
+
* Estimates token usage from prompt and completion text
|
|
20
|
+
* Uses approximate ratio of 4 characters per token
|
|
21
|
+
*
|
|
22
|
+
* @param prompt - Input prompt text
|
|
23
|
+
* @param completion - Generated completion text
|
|
24
|
+
* @returns Estimated token usage
|
|
25
|
+
*/
|
|
26
|
+
export declare function estimateTokenUsage(prompt: string, completion: string): TokenUsage;
|
|
27
|
+
/**
|
|
28
|
+
* Extracts token usage from CLI response metadata if available
|
|
29
|
+
* Falls back to estimation if not present
|
|
30
|
+
*
|
|
31
|
+
* @param metadata - Parsed metadata from CLI response
|
|
32
|
+
* @param prompt - Input prompt for estimation fallback
|
|
33
|
+
* @param completion - Completion text for estimation fallback
|
|
34
|
+
* @returns Token usage (extracted or estimated)
|
|
35
|
+
*/
|
|
36
|
+
export declare function extractOrEstimateTokenUsage(metadata: Record<string, unknown> | undefined, prompt: string, completion: string): TokenUsage;
|
|
37
|
+
//# sourceMappingURL=output-parser.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"output-parser.d.ts","sourceRoot":"","sources":["../src/output-parser.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,UAAU,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAW9E;;;;;;GAMG;AACH,wBAAgB,WAAW,CACzB,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,iBAAiB,CAAC,cAAc,CAAC,GACxC,YAAY,CAYd;AAkLD;;;;;;;GAOG;AACH,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,UAAU,CAUjF;AAED;;;;;;;;GAQG;AACH,wBAAgB,2BAA2B,CACzC,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,SAAS,EAC7C,MAAM,EAAE,MAAM,EACd,UAAU,EAAE,MAAM,GACjB,UAAU,CA+BZ"}
|
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Output parser for CLI responses
|
|
3
|
+
*
|
|
4
|
+
* Handles different output formats:
|
|
5
|
+
* - json: Single JSON object
|
|
6
|
+
* - stream-json: JSON Lines (one JSON object per line)
|
|
7
|
+
* - text: Plain text output
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Strips ANSI escape codes from text
|
|
11
|
+
* These are terminal control sequences like colors, cursor movement, etc.
|
|
12
|
+
*/
|
|
13
|
+
function stripAnsi(text) {
|
|
14
|
+
// eslint-disable-next-line no-control-regex
|
|
15
|
+
return text.replace(/\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])/g, '');
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Parses CLI output based on the expected format
|
|
19
|
+
*
|
|
20
|
+
* @param stdout - Raw stdout from CLI process
|
|
21
|
+
* @param format - Expected output format
|
|
22
|
+
* @returns Parsed output with content and optional metadata
|
|
23
|
+
*/
|
|
24
|
+
export function parseOutput(stdout, format) {
|
|
25
|
+
// Strip ANSI escape codes first
|
|
26
|
+
const cleaned = stripAnsi(stdout);
|
|
27
|
+
switch (format) {
|
|
28
|
+
case 'json':
|
|
29
|
+
return parseJSON(cleaned);
|
|
30
|
+
case 'stream-json':
|
|
31
|
+
return parseStreamJSON(cleaned);
|
|
32
|
+
case 'text':
|
|
33
|
+
return parseText(cleaned);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Parses a single JSON object from stdout
|
|
38
|
+
*/
|
|
39
|
+
function parseJSON(stdout) {
|
|
40
|
+
try {
|
|
41
|
+
const trimmed = stdout.trim();
|
|
42
|
+
if (trimmed.length === 0) {
|
|
43
|
+
return { content: '' };
|
|
44
|
+
}
|
|
45
|
+
const data = JSON.parse(trimmed);
|
|
46
|
+
// Try common response fields
|
|
47
|
+
const content = extractContent(data);
|
|
48
|
+
return {
|
|
49
|
+
content,
|
|
50
|
+
metadata: data,
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
catch {
|
|
54
|
+
// If JSON parsing fails, treat as text
|
|
55
|
+
return { content: stdout.trim() };
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Parses JSON Lines format (one JSON object per line)
|
|
60
|
+
* Used by streaming CLI outputs
|
|
61
|
+
*/
|
|
62
|
+
function parseStreamJSON(stdout) {
|
|
63
|
+
const lines = stdout.trim().split('\n');
|
|
64
|
+
const contentChunks = [];
|
|
65
|
+
let lastMetadata;
|
|
66
|
+
for (const line of lines) {
|
|
67
|
+
const trimmedLine = line.trim();
|
|
68
|
+
if (trimmedLine.length === 0) {
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
// Skip known informational lines from CLIs
|
|
72
|
+
if (trimmedLine.startsWith('Reading prompt from stdin')) {
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
try {
|
|
76
|
+
const data = JSON.parse(trimmedLine);
|
|
77
|
+
const chunk = extractContent(data);
|
|
78
|
+
if (chunk.length > 0) {
|
|
79
|
+
contentChunks.push(chunk);
|
|
80
|
+
}
|
|
81
|
+
// Store metadata from turn.completed for usage info
|
|
82
|
+
if (data.type === 'turn.completed') {
|
|
83
|
+
lastMetadata = data;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
catch {
|
|
87
|
+
// Non-JSON line - skip non-content lines
|
|
88
|
+
// Only add if it looks like actual content (not CLI progress messages)
|
|
89
|
+
if (!trimmedLine.includes('...') && trimmedLine.length > 10) {
|
|
90
|
+
contentChunks.push(trimmedLine);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return {
|
|
95
|
+
content: contentChunks.join(''),
|
|
96
|
+
metadata: lastMetadata,
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Parses plain text output
|
|
101
|
+
*/
|
|
102
|
+
function parseText(stdout) {
|
|
103
|
+
return {
|
|
104
|
+
content: stdout.trim(),
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Extracts content from a parsed JSON response
|
|
109
|
+
* Tries common field names used by different providers
|
|
110
|
+
*/
|
|
111
|
+
function extractContent(data) {
|
|
112
|
+
// Handle Codex-style item.completed events
|
|
113
|
+
// {"type":"item.completed","item":{"type":"agent_message","text":"..."}}
|
|
114
|
+
if (data.type === 'item.completed' && typeof data.item === 'object' && data.item !== null) {
|
|
115
|
+
const item = data.item;
|
|
116
|
+
// Only extract agent_message type, not reasoning
|
|
117
|
+
if (item.type === 'agent_message' && typeof item.text === 'string') {
|
|
118
|
+
return item.text;
|
|
119
|
+
}
|
|
120
|
+
return '';
|
|
121
|
+
}
|
|
122
|
+
// Skip non-content events (thread.started, turn.started, turn.completed, etc.)
|
|
123
|
+
if (data.type === 'thread.started' || data.type === 'turn.started' || data.type === 'turn.completed') {
|
|
124
|
+
return '';
|
|
125
|
+
}
|
|
126
|
+
// Handle ax-glm/ax-grok style: {"role":"assistant","content":"..."}
|
|
127
|
+
// Only extract assistant messages, skip user messages
|
|
128
|
+
if (data.role === 'assistant' && typeof data.content === 'string') {
|
|
129
|
+
return data.content;
|
|
130
|
+
}
|
|
131
|
+
if (data.role === 'user') {
|
|
132
|
+
return ''; // Skip user echo
|
|
133
|
+
}
|
|
134
|
+
// Handle ax-glm/ax-grok --json output: {"messages":[{"role":"assistant","content":"..."}]}
|
|
135
|
+
if (Array.isArray(data.messages)) {
|
|
136
|
+
const assistantMessages = data.messages
|
|
137
|
+
.filter((msg) => {
|
|
138
|
+
if (typeof msg !== 'object' || msg === null)
|
|
139
|
+
return false;
|
|
140
|
+
const m = msg;
|
|
141
|
+
return m.role === 'assistant' && typeof m.content === 'string';
|
|
142
|
+
})
|
|
143
|
+
.map((msg) => msg.content);
|
|
144
|
+
if (assistantMessages.length > 0) {
|
|
145
|
+
return assistantMessages.join('\n');
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
// Common response field names
|
|
149
|
+
const contentFields = ['content', 'text', 'response', 'message', 'output', 'result'];
|
|
150
|
+
for (const field of contentFields) {
|
|
151
|
+
const value = data[field];
|
|
152
|
+
if (typeof value === 'string') {
|
|
153
|
+
return value;
|
|
154
|
+
}
|
|
155
|
+
// Handle nested content (e.g., { message: { content: "..." } })
|
|
156
|
+
if (typeof value === 'object' && value !== null) {
|
|
157
|
+
const nested = value;
|
|
158
|
+
if (typeof nested.content === 'string') {
|
|
159
|
+
return nested.content;
|
|
160
|
+
}
|
|
161
|
+
if (typeof nested.text === 'string') {
|
|
162
|
+
return nested.text;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
// Handle array of content blocks (Anthropic style)
|
|
167
|
+
if (Array.isArray(data.content)) {
|
|
168
|
+
const textBlocks = data.content
|
|
169
|
+
.filter((block) => {
|
|
170
|
+
if (typeof block !== 'object' || block === null) {
|
|
171
|
+
return false;
|
|
172
|
+
}
|
|
173
|
+
const obj = block;
|
|
174
|
+
return obj.type === 'text' && typeof obj.text === 'string';
|
|
175
|
+
})
|
|
176
|
+
.map((block) => block.text);
|
|
177
|
+
if (textBlocks.length > 0) {
|
|
178
|
+
return textBlocks.join('');
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
// Handle choices array (OpenAI style)
|
|
182
|
+
if (Array.isArray(data.choices) && data.choices.length > 0) {
|
|
183
|
+
const firstChoice = data.choices[0];
|
|
184
|
+
if (typeof firstChoice.message === 'object' && firstChoice.message !== null) {
|
|
185
|
+
const message = firstChoice.message;
|
|
186
|
+
if (typeof message.content === 'string') {
|
|
187
|
+
return message.content;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
if (typeof firstChoice.text === 'string') {
|
|
191
|
+
return firstChoice.text;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
return '';
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Estimates token usage from prompt and completion text
|
|
198
|
+
* Uses approximate ratio of 4 characters per token
|
|
199
|
+
*
|
|
200
|
+
* @param prompt - Input prompt text
|
|
201
|
+
* @param completion - Generated completion text
|
|
202
|
+
* @returns Estimated token usage
|
|
203
|
+
*/
|
|
204
|
+
export function estimateTokenUsage(prompt, completion) {
|
|
205
|
+
// Conservative estimate: ~4 characters per token
|
|
206
|
+
const inputTokens = Math.ceil(prompt.length / 4);
|
|
207
|
+
const outputTokens = Math.ceil(completion.length / 4);
|
|
208
|
+
return {
|
|
209
|
+
inputTokens,
|
|
210
|
+
outputTokens,
|
|
211
|
+
totalTokens: inputTokens + outputTokens,
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Extracts token usage from CLI response metadata if available
|
|
216
|
+
* Falls back to estimation if not present
|
|
217
|
+
*
|
|
218
|
+
* @param metadata - Parsed metadata from CLI response
|
|
219
|
+
* @param prompt - Input prompt for estimation fallback
|
|
220
|
+
* @param completion - Completion text for estimation fallback
|
|
221
|
+
* @returns Token usage (extracted or estimated)
|
|
222
|
+
*/
|
|
223
|
+
export function extractOrEstimateTokenUsage(metadata, prompt, completion) {
|
|
224
|
+
if (metadata === undefined) {
|
|
225
|
+
return estimateTokenUsage(prompt, completion);
|
|
226
|
+
}
|
|
227
|
+
// Try to extract usage from metadata
|
|
228
|
+
const usage = metadata.usage;
|
|
229
|
+
if (usage !== undefined) {
|
|
230
|
+
const inputTokens = typeof usage.input_tokens === 'number'
|
|
231
|
+
? usage.input_tokens
|
|
232
|
+
: typeof usage.prompt_tokens === 'number'
|
|
233
|
+
? usage.prompt_tokens
|
|
234
|
+
: undefined;
|
|
235
|
+
const outputTokens = typeof usage.output_tokens === 'number'
|
|
236
|
+
? usage.output_tokens
|
|
237
|
+
: typeof usage.completion_tokens === 'number'
|
|
238
|
+
? usage.completion_tokens
|
|
239
|
+
: undefined;
|
|
240
|
+
if (inputTokens !== undefined && outputTokens !== undefined) {
|
|
241
|
+
return {
|
|
242
|
+
inputTokens,
|
|
243
|
+
outputTokens,
|
|
244
|
+
totalTokens: inputTokens + outputTokens,
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
// Fallback to estimation
|
|
249
|
+
return estimateTokenUsage(prompt, completion);
|
|
250
|
+
}
|
|
251
|
+
//# sourceMappingURL=output-parser.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"output-parser.js","sourceRoot":"","sources":["../src/output-parser.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAIH;;;GAGG;AACH,SAAS,SAAS,CAAC,IAAY;IAC7B,4CAA4C;IAC5C,OAAO,IAAI,CAAC,OAAO,CAAC,wCAAwC,EAAE,EAAE,CAAC,CAAC;AACpE,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,WAAW,CACzB,MAAc,EACd,MAAyC;IAEzC,gCAAgC;IAChC,MAAM,OAAO,GAAG,SAAS,CAAC,MAAM,CAAC,CAAC;IAElC,QAAQ,MAAM,EAAE,CAAC;QACf,KAAK,MAAM;YACT,OAAO,SAAS,CAAC,OAAO,CAAC,CAAC;QAC5B,KAAK,aAAa;YAChB,OAAO,eAAe,CAAC,OAAO,CAAC,CAAC;QAClC,KAAK,MAAM;YACT,O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AAK,CAAC,YAAY;YACpB,CAAC,CAAC,OAAO,KAAK,CAAC,aAAa,KAAK,QAAQ;gBACvC,CAAC,CAAC,KAAK,CAAC,aAAa;gBACrB,CAAC,CAAC,SAAS,CAAC;QAEhB,MAAM,YAAY,GAAG,OAAO,KAAK,CAAC,aAAa,KAAK,QAAQ;YAC1D,CAAC,CAAC,KAAK,CAAC,aAAa;YACrB,CAAC,CAAC,OAAO,KAAK,CAAC,iBAAiB,KAAK,QAAQ;gBAC3C,CAAC,CAAC,KAAK,CAAC,iBAAiB;gBACzB,CAAC,CAAC,SAAS,CAAC;QAEhB,IAAI,WAAW,KAAK,SAAS,IAAI,YAAY,KAAK,SAAS,EAAE,CAAC;YAC5D,OAAO;gBACL,WAAW;gBACX,YAAY;gBACZ,WAAW,EAAE,WAAW,GAAG,YAAY;aACxC,CAAC;QACJ,CAAC;IACH,CAAC;IAED,yBAAyB;IACzB,OAAO,kBAAkB,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC;AAChD,CAAC"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Process manager for spawning CLI processes
|
|
3
|
+
*
|
|
4
|
+
* Handles:
|
|
5
|
+
* - Process spawning with stdin/stdout
|
|
6
|
+
* - Timeout handling with graceful shutdown
|
|
7
|
+
* - Command availability checking
|
|
8
|
+
*/
|
|
9
|
+
import type { SpawnOptions, SpawnResult } from './types.js';
|
|
10
|
+
/**
|
|
11
|
+
* Spawns a CLI process and returns the result
|
|
12
|
+
*
|
|
13
|
+
* @param options - Spawn options including command, args, stdin, env, timeout
|
|
14
|
+
* @returns Promise resolving to spawn result with stdout, stderr, exitCode
|
|
15
|
+
*/
|
|
16
|
+
export declare function spawnCLI(options: SpawnOptions): Promise<SpawnResult>;
|
|
17
|
+
/**
|
|
18
|
+
* Checks if a CLI command is available on the system PATH
|
|
19
|
+
*
|
|
20
|
+
* @param command - The command to check
|
|
21
|
+
* @returns Promise resolving to true if available, false otherwise
|
|
22
|
+
*/
|
|
23
|
+
export declare function isCommandAvailable(command: string): Promise<boolean>;
|
|
24
|
+
/**
|
|
25
|
+
* Gets the version of a CLI command if available
|
|
26
|
+
*
|
|
27
|
+
* @param command - The command to check
|
|
28
|
+
* @param versionFlag - The flag to get version (default: --version)
|
|
29
|
+
* @returns Promise resolving to version string or undefined
|
|
30
|
+
*/
|
|
31
|
+
export declare function getCommandVersion(command: string, versionFlag?: string): Promise<string | undefined>;
|
|
32
|
+
/**
|
|
33
|
+
* Builds a prompt string from messages for CLI input
|
|
34
|
+
*
|
|
35
|
+
* @param messages - Array of messages
|
|
36
|
+
* @param systemPrompt - Optional system prompt
|
|
37
|
+
* @returns Formatted prompt string
|
|
38
|
+
*/
|
|
39
|
+
export declare function buildPromptFromMessages(messages: {
|
|
40
|
+
role: string;
|
|
41
|
+
content: string;
|
|
42
|
+
}[], systemPrompt?: string): string;
|
|
43
|
+
//# sourceMappingURL=process-manager.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"process-manager.d.ts","sourceRoot":"","sources":["../src/process-manager.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAKH,OAAO,KAAK,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAsB5D;;;;;GAKG;AACH,wBAAsB,QAAQ,CAAC,OAAO,EAAE,YAAY,GAAG,OAAO,CAAC,WAAW,CAAC,CAqF1E;AAED;;;;;GAKG;AACH,wBAAsB,kBAAkB,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAc1E;AAED;;;;;;GAMG;AACH,wBAAsB,iBAAiB,CACrC,OAAO,EAAE,MAAM,EACf,WAAW,SAAc,GACxB,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CAmB7B;AAED;;;;;;GAMG;AACH,wBAAgB,uBAAuB,CACrC,QAAQ,EAAE;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,EAAE,EAC7C,YAAY,CAAC,EAAE,MAAM,GACpB,MAAM,CAqBR"}
|