agents-library 0.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/dist/base-agent.d.ts +172 -0
- package/dist/base-agent.d.ts.map +1 -0
- package/dist/base-agent.js +255 -0
- package/dist/base-agent.js.map +1 -0
- package/dist/base-bot.d.ts +282 -0
- package/dist/base-bot.d.ts.map +1 -0
- package/dist/base-bot.js +375 -0
- package/dist/base-bot.js.map +1 -0
- package/dist/common/result.d.ts +51 -0
- package/dist/common/result.d.ts.map +1 -0
- package/dist/common/result.js +45 -0
- package/dist/common/result.js.map +1 -0
- package/dist/common/types.d.ts +57 -0
- package/dist/common/types.d.ts.map +1 -0
- package/dist/common/types.js +42 -0
- package/dist/common/types.js.map +1 -0
- package/dist/index.d.ts +94 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +108 -0
- package/dist/index.js.map +1 -0
- package/dist/kadi-event-publisher.d.ts +163 -0
- package/dist/kadi-event-publisher.d.ts.map +1 -0
- package/dist/kadi-event-publisher.js +286 -0
- package/dist/kadi-event-publisher.js.map +1 -0
- package/dist/memory/arcadedb-adapter.d.ts +159 -0
- package/dist/memory/arcadedb-adapter.d.ts.map +1 -0
- package/dist/memory/arcadedb-adapter.js +314 -0
- package/dist/memory/arcadedb-adapter.js.map +1 -0
- package/dist/memory/file-storage-adapter.d.ts +122 -0
- package/dist/memory/file-storage-adapter.d.ts.map +1 -0
- package/dist/memory/file-storage-adapter.js +352 -0
- package/dist/memory/file-storage-adapter.js.map +1 -0
- package/dist/memory/memory-service.d.ts +208 -0
- package/dist/memory/memory-service.d.ts.map +1 -0
- package/dist/memory/memory-service.js +410 -0
- package/dist/memory/memory-service.js.map +1 -0
- package/dist/memory/types.d.ts +126 -0
- package/dist/memory/types.d.ts.map +1 -0
- package/dist/memory/types.js +41 -0
- package/dist/memory/types.js.map +1 -0
- package/dist/producer-tool-utils.d.ts +474 -0
- package/dist/producer-tool-utils.d.ts.map +1 -0
- package/dist/producer-tool-utils.js +664 -0
- package/dist/producer-tool-utils.js.map +1 -0
- package/dist/providers/anthropic-provider.d.ts +160 -0
- package/dist/providers/anthropic-provider.d.ts.map +1 -0
- package/dist/providers/anthropic-provider.js +527 -0
- package/dist/providers/anthropic-provider.js.map +1 -0
- package/dist/providers/model-manager-provider.d.ts +91 -0
- package/dist/providers/model-manager-provider.d.ts.map +1 -0
- package/dist/providers/model-manager-provider.js +355 -0
- package/dist/providers/model-manager-provider.js.map +1 -0
- package/dist/providers/provider-manager.d.ts +111 -0
- package/dist/providers/provider-manager.d.ts.map +1 -0
- package/dist/providers/provider-manager.js +337 -0
- package/dist/providers/provider-manager.js.map +1 -0
- package/dist/providers/types.d.ts +145 -0
- package/dist/providers/types.d.ts.map +1 -0
- package/dist/providers/types.js +23 -0
- package/dist/providers/types.js.map +1 -0
- package/dist/shadow-agent-factory.d.ts +623 -0
- package/dist/shadow-agent-factory.d.ts.map +1 -0
- package/dist/shadow-agent-factory.js +1117 -0
- package/dist/shadow-agent-factory.js.map +1 -0
- package/dist/types/agent-config.d.ts +307 -0
- package/dist/types/agent-config.d.ts.map +1 -0
- package/dist/types/agent-config.js +15 -0
- package/dist/types/agent-config.js.map +1 -0
- package/dist/types/event-schemas.d.ts +358 -0
- package/dist/types/event-schemas.d.ts.map +1 -0
- package/dist/types/event-schemas.js +188 -0
- package/dist/types/event-schemas.js.map +1 -0
- package/dist/types/tool-schemas.d.ts +498 -0
- package/dist/types/tool-schemas.d.ts.map +1 -0
- package/dist/types/tool-schemas.js +457 -0
- package/dist/types/tool-schemas.js.map +1 -0
- package/dist/utils/logger.d.ts +135 -0
- package/dist/utils/logger.d.ts.map +1 -0
- package/dist/utils/logger.js +205 -0
- package/dist/utils/logger.js.map +1 -0
- package/dist/utils/timer.d.ts +186 -0
- package/dist/utils/timer.d.ts.map +1 -0
- package/dist/utils/timer.js +211 -0
- package/dist/utils/timer.js.map +1 -0
- package/dist/worker-agent-factory.d.ts +688 -0
- package/dist/worker-agent-factory.d.ts.map +1 -0
- package/dist/worker-agent-factory.js +1517 -0
- package/dist/worker-agent-factory.js.map +1 -0
- package/package.json +38 -0
|
@@ -0,0 +1,498 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tool Schema Type Definitions
|
|
3
|
+
* ==============================
|
|
4
|
+
*
|
|
5
|
+
* Type definitions for KĀDI tool invocation, validation, and error handling.
|
|
6
|
+
* Provides standardized interfaces for tool schemas, invocation results, and error classification.
|
|
7
|
+
*
|
|
8
|
+
* Design Principles:
|
|
9
|
+
* - Discriminated unions for type-safe result handling
|
|
10
|
+
* - Clear error classification for retry logic
|
|
11
|
+
* - Compatible with @kadi.build/core tool registration
|
|
12
|
+
* - Runtime validation via Zod schemas
|
|
13
|
+
*
|
|
14
|
+
* @module tool-schemas
|
|
15
|
+
*/
|
|
16
|
+
import { z } from '@kadi.build/core';
|
|
17
|
+
/**
|
|
18
|
+
* Error type classification for retry logic
|
|
19
|
+
*
|
|
20
|
+
* Discriminates between transient errors (retry allowed) and permanent errors (fail-fast).
|
|
21
|
+
* Used by circuit breaker and exponential backoff retry logic in BaseBot.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```typescript
|
|
25
|
+
* function classifyError(error: Error): ErrorType {
|
|
26
|
+
* const message = error.message.toLowerCase();
|
|
27
|
+
*
|
|
28
|
+
* // Transient errors - retry allowed
|
|
29
|
+
* if (message.includes('timeout')) return ErrorType.Transient;
|
|
30
|
+
* if (message.includes('econnrefused')) return ErrorType.Transient;
|
|
31
|
+
* if (message.includes('enotfound')) return ErrorType.Transient;
|
|
32
|
+
* if (message.includes('rate limit')) return ErrorType.Transient;
|
|
33
|
+
*
|
|
34
|
+
* // Permanent errors - fail-fast
|
|
35
|
+
* if (message.includes('unauthorized')) return ErrorType.Permanent;
|
|
36
|
+
* if (message.includes('not found')) return ErrorType.Permanent;
|
|
37
|
+
* if (message.includes('invalid')) return ErrorType.Permanent;
|
|
38
|
+
* if (message.includes('validation')) return ErrorType.Permanent;
|
|
39
|
+
*
|
|
40
|
+
* // Default to permanent to avoid infinite retry loops
|
|
41
|
+
* return ErrorType.Permanent;
|
|
42
|
+
* }
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
export declare enum ErrorType {
|
|
46
|
+
/**
|
|
47
|
+
* Transient error - may succeed on retry
|
|
48
|
+
*
|
|
49
|
+
* Includes:
|
|
50
|
+
* - Network errors (ECONNREFUSED, ENOTFOUND)
|
|
51
|
+
* - Timeout errors
|
|
52
|
+
* - Rate limit errors (429)
|
|
53
|
+
* - Temporary service unavailable (503)
|
|
54
|
+
*/
|
|
55
|
+
Transient = "transient",
|
|
56
|
+
/**
|
|
57
|
+
* Permanent error - will not succeed on retry
|
|
58
|
+
*
|
|
59
|
+
* Includes:
|
|
60
|
+
* - Validation errors (400)
|
|
61
|
+
* - Authentication errors (401, 403)
|
|
62
|
+
* - Not found errors (404)
|
|
63
|
+
* - Invalid input errors
|
|
64
|
+
* - Business logic errors
|
|
65
|
+
*/
|
|
66
|
+
Permanent = "permanent"
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Tool schema interface for KĀDI tool registration
|
|
70
|
+
*
|
|
71
|
+
* Defines the structure of a tool including name, description, and Zod schemas
|
|
72
|
+
* for input/output validation. Compatible with @kadi.build/core tool registration.
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* ```typescript
|
|
76
|
+
* const sendMessageTool: ToolSchema = {
|
|
77
|
+
* name: 'send_slack_message',
|
|
78
|
+
* description: 'Send a message to a Slack channel',
|
|
79
|
+
* input: z.object({
|
|
80
|
+
* channel: z.string().describe('Channel ID (e.g., C12345678)'),
|
|
81
|
+
* text: z.string().describe('Message text to send'),
|
|
82
|
+
* thread_ts: z.string().optional().describe('Optional thread timestamp for replies')
|
|
83
|
+
* }),
|
|
84
|
+
* output: z.object({
|
|
85
|
+
* ok: z.boolean().describe('Whether the message was sent successfully'),
|
|
86
|
+
* ts: z.string().describe('Timestamp of the sent message'),
|
|
87
|
+
* channel: z.string().describe('Channel ID where message was sent')
|
|
88
|
+
* })
|
|
89
|
+
* };
|
|
90
|
+
* ```
|
|
91
|
+
*/
|
|
92
|
+
export interface ToolSchema<TInput = any, TOutput = any> {
|
|
93
|
+
/**
|
|
94
|
+
* Tool name (must be unique within agent)
|
|
95
|
+
*
|
|
96
|
+
* Convention: lowercase with underscores (e.g., 'send_slack_message', 'plan_task')
|
|
97
|
+
*/
|
|
98
|
+
name: string;
|
|
99
|
+
/**
|
|
100
|
+
* Human-readable tool description
|
|
101
|
+
*
|
|
102
|
+
* Describes what the tool does and when to use it.
|
|
103
|
+
* Used by Claude API for tool selection.
|
|
104
|
+
*/
|
|
105
|
+
description: string;
|
|
106
|
+
/**
|
|
107
|
+
* Input schema using Zod
|
|
108
|
+
*
|
|
109
|
+
* Defines and validates the structure of tool input parameters.
|
|
110
|
+
* Use .describe() on each field to provide documentation.
|
|
111
|
+
*/
|
|
112
|
+
input: z.ZodType<TInput>;
|
|
113
|
+
/**
|
|
114
|
+
* Output schema using Zod
|
|
115
|
+
*
|
|
116
|
+
* Defines and validates the structure of tool output.
|
|
117
|
+
* Use .describe() on each field to provide documentation.
|
|
118
|
+
*/
|
|
119
|
+
output: z.ZodType<TOutput>;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Success result from tool invocation
|
|
123
|
+
*
|
|
124
|
+
* Represents a successful tool execution with result data.
|
|
125
|
+
* Discriminated by `success: true`.
|
|
126
|
+
*
|
|
127
|
+
* @example
|
|
128
|
+
* ```typescript
|
|
129
|
+
* const result: ToolInvocationSuccess = {
|
|
130
|
+
* success: true,
|
|
131
|
+
* result: {
|
|
132
|
+
* ok: true,
|
|
133
|
+
* ts: '1234567890.123456',
|
|
134
|
+
* channel: 'C12345678'
|
|
135
|
+
* }
|
|
136
|
+
* };
|
|
137
|
+
* ```
|
|
138
|
+
*/
|
|
139
|
+
export interface ToolInvocationSuccess<T = any> {
|
|
140
|
+
/** Discriminator for type narrowing */
|
|
141
|
+
success: true;
|
|
142
|
+
/** Tool execution result (validated against output schema) */
|
|
143
|
+
result: T;
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Failure result from tool invocation
|
|
147
|
+
*
|
|
148
|
+
* Represents a failed tool execution with error details.
|
|
149
|
+
* Discriminated by `success: false`.
|
|
150
|
+
*
|
|
151
|
+
* @example
|
|
152
|
+
* ```typescript
|
|
153
|
+
* const result: ToolInvocationFailure = {
|
|
154
|
+
* success: false,
|
|
155
|
+
* error: new Error('Connection timeout after 10 seconds'),
|
|
156
|
+
* errorType: ErrorType.Transient
|
|
157
|
+
* };
|
|
158
|
+
* ```
|
|
159
|
+
*/
|
|
160
|
+
export interface ToolInvocationFailure {
|
|
161
|
+
/** Discriminator for type narrowing */
|
|
162
|
+
success: false;
|
|
163
|
+
/** Error object with message and stack trace */
|
|
164
|
+
error: Error;
|
|
165
|
+
/**
|
|
166
|
+
* Error classification for retry logic (optional)
|
|
167
|
+
*
|
|
168
|
+
* If not provided, retry logic should classify error based on error message.
|
|
169
|
+
*/
|
|
170
|
+
errorType?: ErrorType;
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Tool invocation result (discriminated union)
|
|
174
|
+
*
|
|
175
|
+
* Result of a tool execution - either success with result data or failure with error.
|
|
176
|
+
* Use discriminated union pattern for type-safe error handling.
|
|
177
|
+
*
|
|
178
|
+
* @example
|
|
179
|
+
* ```typescript
|
|
180
|
+
* async function invokeTool(toolName: string, input: any): Promise<ToolInvocationResult> {
|
|
181
|
+
* try {
|
|
182
|
+
* const result = await protocol.invokeTool({ toolName, toolInput: input });
|
|
183
|
+
* return { success: true, result };
|
|
184
|
+
* } catch (error) {
|
|
185
|
+
* return {
|
|
186
|
+
* success: false,
|
|
187
|
+
* error: error instanceof Error ? error : new Error(String(error)),
|
|
188
|
+
* errorType: classifyError(error)
|
|
189
|
+
* };
|
|
190
|
+
* }
|
|
191
|
+
* }
|
|
192
|
+
*
|
|
193
|
+
* // Usage with type narrowing
|
|
194
|
+
* const result = await invokeTool('send_message', { channel: 'C123', text: 'Hello' });
|
|
195
|
+
*
|
|
196
|
+
* if (result.success) {
|
|
197
|
+
* // TypeScript knows result.result exists
|
|
198
|
+
* console.log('Message sent:', result.result.ts);
|
|
199
|
+
* } else {
|
|
200
|
+
* // TypeScript knows result.error exists
|
|
201
|
+
* if (result.errorType === ErrorType.Transient) {
|
|
202
|
+
* console.log('Transient error - retrying:', result.error.message);
|
|
203
|
+
* } else {
|
|
204
|
+
* console.error('Permanent error - failing:', result.error.message);
|
|
205
|
+
* }
|
|
206
|
+
* }
|
|
207
|
+
* ```
|
|
208
|
+
*/
|
|
209
|
+
export type ToolInvocationResult<T = any> = ToolInvocationSuccess<T> | ToolInvocationFailure;
|
|
210
|
+
/**
|
|
211
|
+
* Parameters for invoking a tool via KĀDI broker protocol
|
|
212
|
+
*
|
|
213
|
+
* Used by BaseBot.invokeToolWithRetry() and protocol.invokeTool().
|
|
214
|
+
*
|
|
215
|
+
* @example
|
|
216
|
+
* ```typescript
|
|
217
|
+
* const params: ToolInvocationParams = {
|
|
218
|
+
* targetAgent: 'mcp-server-slack',
|
|
219
|
+
* toolName: 'send_slack_message',
|
|
220
|
+
* toolInput: {
|
|
221
|
+
* channel: 'C12345678',
|
|
222
|
+
* text: 'Hello from KĀDI!'
|
|
223
|
+
* },
|
|
224
|
+
* timeout: 10000
|
|
225
|
+
* };
|
|
226
|
+
*
|
|
227
|
+
* const result = await protocol.invokeTool(params);
|
|
228
|
+
* ```
|
|
229
|
+
*/
|
|
230
|
+
export interface ToolInvocationParams<T = any> {
|
|
231
|
+
/**
|
|
232
|
+
* Target agent ID that provides the tool
|
|
233
|
+
*
|
|
234
|
+
* @example 'mcp-server-slack', 'mcp-server-shrimp-agent-playground'
|
|
235
|
+
*/
|
|
236
|
+
targetAgent: string;
|
|
237
|
+
/**
|
|
238
|
+
* Name of the tool to invoke
|
|
239
|
+
*
|
|
240
|
+
* @example 'send_slack_message', 'shrimp_plan_task'
|
|
241
|
+
*/
|
|
242
|
+
toolName: string;
|
|
243
|
+
/**
|
|
244
|
+
* Input parameters for the tool (validated against tool's input schema)
|
|
245
|
+
*/
|
|
246
|
+
toolInput: T;
|
|
247
|
+
/**
|
|
248
|
+
* Timeout in milliseconds
|
|
249
|
+
*
|
|
250
|
+
* @default 30000 (30 seconds)
|
|
251
|
+
*/
|
|
252
|
+
timeout?: number;
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* Zod schema for ToolInvocationSuccess
|
|
256
|
+
*/
|
|
257
|
+
export declare const ToolInvocationSuccessSchema: z.ZodObject<{
|
|
258
|
+
success: z.ZodLiteral<true>;
|
|
259
|
+
result: z.ZodAny;
|
|
260
|
+
}, z.core.$strip>;
|
|
261
|
+
/**
|
|
262
|
+
* Zod schema for ToolInvocationFailure
|
|
263
|
+
*/
|
|
264
|
+
export declare const ToolInvocationFailureSchema: z.ZodObject<{
|
|
265
|
+
success: z.ZodLiteral<false>;
|
|
266
|
+
error: z.ZodCustom<Error, Error>;
|
|
267
|
+
errorType: z.ZodOptional<z.ZodEnum<typeof ErrorType>>;
|
|
268
|
+
}, z.core.$strip>;
|
|
269
|
+
/**
|
|
270
|
+
* Zod schema for ToolInvocationResult
|
|
271
|
+
*/
|
|
272
|
+
export declare const ToolInvocationResultSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
|
|
273
|
+
success: z.ZodLiteral<true>;
|
|
274
|
+
result: z.ZodAny;
|
|
275
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
276
|
+
success: z.ZodLiteral<false>;
|
|
277
|
+
error: z.ZodCustom<Error, Error>;
|
|
278
|
+
errorType: z.ZodOptional<z.ZodEnum<typeof ErrorType>>;
|
|
279
|
+
}, z.core.$strip>], "success">;
|
|
280
|
+
/**
|
|
281
|
+
* Zod schema for ToolInvocationParams
|
|
282
|
+
*/
|
|
283
|
+
export declare const ToolInvocationParamsSchema: z.ZodObject<{
|
|
284
|
+
targetAgent: z.ZodString;
|
|
285
|
+
toolName: z.ZodString;
|
|
286
|
+
toolInput: z.ZodAny;
|
|
287
|
+
timeout: z.ZodOptional<z.ZodNumber>;
|
|
288
|
+
}, z.core.$strip>;
|
|
289
|
+
/**
|
|
290
|
+
* Type guard to check if result is a success
|
|
291
|
+
*
|
|
292
|
+
* @param result - Tool invocation result
|
|
293
|
+
* @returns True if result is ToolInvocationSuccess
|
|
294
|
+
*
|
|
295
|
+
* @example
|
|
296
|
+
* ```typescript
|
|
297
|
+
* if (isToolInvocationSuccess(result)) {
|
|
298
|
+
* console.log('Success:', result.result);
|
|
299
|
+
* }
|
|
300
|
+
* ```
|
|
301
|
+
*/
|
|
302
|
+
export declare function isToolInvocationSuccess<T = any>(result: ToolInvocationResult<T>): result is ToolInvocationSuccess<T>;
|
|
303
|
+
/**
|
|
304
|
+
* Type guard to check if result is a failure
|
|
305
|
+
*
|
|
306
|
+
* @param result - Tool invocation result
|
|
307
|
+
* @returns True if result is ToolInvocationFailure
|
|
308
|
+
*
|
|
309
|
+
* @example
|
|
310
|
+
* ```typescript
|
|
311
|
+
* if (isToolInvocationFailure(result)) {
|
|
312
|
+
* console.error('Failure:', result.error.message);
|
|
313
|
+
* }
|
|
314
|
+
* ```
|
|
315
|
+
*/
|
|
316
|
+
export declare function isToolInvocationFailure(result: ToolInvocationResult): result is ToolInvocationFailure;
|
|
317
|
+
/**
|
|
318
|
+
* Detailed error classification result
|
|
319
|
+
*
|
|
320
|
+
* Provides comprehensive error information for intelligent retry logic and error handling.
|
|
321
|
+
* Used by BaseBot, producer utilities, and worker agents to decide retry strategies.
|
|
322
|
+
*
|
|
323
|
+
* @example
|
|
324
|
+
* ```typescript
|
|
325
|
+
* const classification = classifyToolError(error);
|
|
326
|
+
*
|
|
327
|
+
* if (classification.retryable) {
|
|
328
|
+
* console.log(`Transient ${classification.category}: ${classification.message}`);
|
|
329
|
+
* if (classification.category === 'rate_limit') {
|
|
330
|
+
* // Use exponential backoff for rate limiting
|
|
331
|
+
* await retryWithExponentialBackoff(() => operation());
|
|
332
|
+
* } else {
|
|
333
|
+
* // Use standard retry for other transient errors
|
|
334
|
+
* await retryWithBackoff(() => operation());
|
|
335
|
+
* }
|
|
336
|
+
* } else {
|
|
337
|
+
* console.error(`Permanent ${classification.category}: ${classification.message}`);
|
|
338
|
+
* throw error; // Fail-fast
|
|
339
|
+
* }
|
|
340
|
+
* ```
|
|
341
|
+
*/
|
|
342
|
+
export interface ErrorClassification {
|
|
343
|
+
/**
|
|
344
|
+
* Error type for basic classification
|
|
345
|
+
*
|
|
346
|
+
* - 'transient': Error may succeed on retry (network issues, timeouts, rate limits, 5xx)
|
|
347
|
+
* - 'permanent': Error will not succeed on retry (validation, auth, 4xx except 429)
|
|
348
|
+
*/
|
|
349
|
+
type: 'transient' | 'permanent';
|
|
350
|
+
/**
|
|
351
|
+
* Specific error category for fine-grained handling
|
|
352
|
+
*
|
|
353
|
+
* Categories:
|
|
354
|
+
* - Transient: 'network', 'timeout', 'rate_limit', 'server_error', 'service_unavailable'
|
|
355
|
+
* - Permanent: 'validation', 'authentication', 'authorization', 'not_found', 'bad_request', 'unknown'
|
|
356
|
+
*/
|
|
357
|
+
category: string;
|
|
358
|
+
/**
|
|
359
|
+
* Human-readable error message
|
|
360
|
+
*
|
|
361
|
+
* Describes what went wrong and provides context for debugging.
|
|
362
|
+
*/
|
|
363
|
+
message: string;
|
|
364
|
+
/**
|
|
365
|
+
* Whether the error is retriable
|
|
366
|
+
*
|
|
367
|
+
* True for transient errors, false for permanent errors.
|
|
368
|
+
* Use this flag to decide retry strategy.
|
|
369
|
+
*/
|
|
370
|
+
retryable: boolean;
|
|
371
|
+
/**
|
|
372
|
+
* Optional HTTP status code if available
|
|
373
|
+
*
|
|
374
|
+
* Extracted from error message, error object, or HTTP response.
|
|
375
|
+
*/
|
|
376
|
+
statusCode?: number;
|
|
377
|
+
/**
|
|
378
|
+
* Optional Node.js error code if available
|
|
379
|
+
*
|
|
380
|
+
* Examples: ECONNREFUSED, ETIMEDOUT, ENOTFOUND, ECONNRESET
|
|
381
|
+
*/
|
|
382
|
+
errorCode?: string;
|
|
383
|
+
}
|
|
384
|
+
/**
|
|
385
|
+
* Classify error as transient or permanent with detailed categorization
|
|
386
|
+
*
|
|
387
|
+
* Analyzes errors from multiple sources (Claude API, KĀDI broker, Node.js) and returns
|
|
388
|
+
* detailed classification for intelligent retry logic. Handles HTTP status codes,
|
|
389
|
+
* Node.js error codes, and error message patterns.
|
|
390
|
+
*
|
|
391
|
+
* **Classification Rules:**
|
|
392
|
+
*
|
|
393
|
+
* **Transient Errors (retry allowed):**
|
|
394
|
+
* - Network errors: ECONNREFUSED, ENOTFOUND, ECONNRESET, ETIMEDOUT
|
|
395
|
+
* - Timeout errors: timeout, ETIMEDOUT
|
|
396
|
+
* - Rate limiting: 429 status code, "rate limit" message (use exponential backoff)
|
|
397
|
+
* - Server errors: 5xx status codes, "service unavailable"
|
|
398
|
+
* - Connection errors: "socket hang up", "connection reset"
|
|
399
|
+
*
|
|
400
|
+
* **Permanent Errors (fail-fast):**
|
|
401
|
+
* - Validation errors: 400 Bad Request, "validation", "invalid"
|
|
402
|
+
* - Authentication: 401 Unauthorized, "unauthorized"
|
|
403
|
+
* - Authorization: 403 Forbidden, "forbidden"
|
|
404
|
+
* - Not found: 404 Not Found, "not found"
|
|
405
|
+
* - Other 4xx client errors (except 429)
|
|
406
|
+
*
|
|
407
|
+
* **Special Cases:**
|
|
408
|
+
* - 429 (Rate Limit): Classified as transient with category 'rate_limit' - use exponential backoff
|
|
409
|
+
* - Unknown errors: Default to permanent to avoid infinite retry loops
|
|
410
|
+
*
|
|
411
|
+
* **Error Source Handling:**
|
|
412
|
+
* - **Claude API errors**: Anthropic SDK error objects with status codes
|
|
413
|
+
* - **KĀDI protocol errors**: Protocol response errors with status/statusCode fields
|
|
414
|
+
* - **Node.js errors**: Error objects with code property (ECONNREFUSED, etc.)
|
|
415
|
+
* - **Generic errors**: Error message pattern matching
|
|
416
|
+
*
|
|
417
|
+
* @param error - Error to classify (Error object, unknown type, or any)
|
|
418
|
+
* @returns Detailed error classification with type, category, message, retryable flag
|
|
419
|
+
*
|
|
420
|
+
* @example
|
|
421
|
+
* ```typescript
|
|
422
|
+
* // Network error (transient)
|
|
423
|
+
* try {
|
|
424
|
+
* await fetch('http://unavailable-service.com');
|
|
425
|
+
* } catch (error) {
|
|
426
|
+
* const classification = classifyToolError(error);
|
|
427
|
+
* // { type: 'transient', category: 'network', message: 'ECONNREFUSED', retryable: true, errorCode: 'ECONNREFUSED' }
|
|
428
|
+
* }
|
|
429
|
+
* ```
|
|
430
|
+
*
|
|
431
|
+
* @example
|
|
432
|
+
* ```typescript
|
|
433
|
+
* // Rate limit error (transient with exponential backoff)
|
|
434
|
+
* try {
|
|
435
|
+
* await anthropic.messages.create({ ... });
|
|
436
|
+
* } catch (error) {
|
|
437
|
+
* const classification = classifyToolError(error);
|
|
438
|
+
* // { type: 'transient', category: 'rate_limit', message: 'Rate limit exceeded', retryable: true, statusCode: 429 }
|
|
439
|
+
*
|
|
440
|
+
* if (classification.category === 'rate_limit') {
|
|
441
|
+
* // Use exponential backoff specifically for rate limits
|
|
442
|
+
* await retryWithExponentialBackoff(() => operation(), { baseDelay: 1000, maxDelay: 60000 });
|
|
443
|
+
* }
|
|
444
|
+
* }
|
|
445
|
+
* ```
|
|
446
|
+
*
|
|
447
|
+
* @example
|
|
448
|
+
* ```typescript
|
|
449
|
+
* // Validation error (permanent)
|
|
450
|
+
* try {
|
|
451
|
+
* await invokeTool({ toolName: 'invalid', toolInput: null });
|
|
452
|
+
* } catch (error) {
|
|
453
|
+
* const classification = classifyToolError(error);
|
|
454
|
+
* // { type: 'permanent', category: 'validation', message: 'Invalid tool input', retryable: false, statusCode: 400 }
|
|
455
|
+
*
|
|
456
|
+
* // Don't retry - fail immediately
|
|
457
|
+
* console.error('Validation failed:', classification.message);
|
|
458
|
+
* throw error;
|
|
459
|
+
* }
|
|
460
|
+
* ```
|
|
461
|
+
*
|
|
462
|
+
* @example
|
|
463
|
+
* ```typescript
|
|
464
|
+
* // Generic error handling with classification
|
|
465
|
+
* async function executeWithRetry(operation: () => Promise<any>, maxRetries: number = 3) {
|
|
466
|
+
* let attempt = 0;
|
|
467
|
+
*
|
|
468
|
+
* while (attempt < maxRetries) {
|
|
469
|
+
* try {
|
|
470
|
+
* return await operation();
|
|
471
|
+
* } catch (error) {
|
|
472
|
+
* const classification = classifyToolError(error);
|
|
473
|
+
*
|
|
474
|
+
* console.error(`Attempt ${attempt + 1} failed:`, classification.message);
|
|
475
|
+
*
|
|
476
|
+
* if (!classification.retryable) {
|
|
477
|
+
* // Permanent error - fail immediately
|
|
478
|
+
* throw new Error(`Permanent error (${classification.category}): ${classification.message}`);
|
|
479
|
+
* }
|
|
480
|
+
*
|
|
481
|
+
* // Calculate delay based on error category
|
|
482
|
+
* const delay = classification.category === 'rate_limit'
|
|
483
|
+
* ? Math.pow(2, attempt) * 1000 // Exponential backoff for rate limits
|
|
484
|
+
* : 1000; // Fixed delay for other transient errors
|
|
485
|
+
*
|
|
486
|
+
* console.log(`Retrying in ${delay}ms...`);
|
|
487
|
+
* await new Promise(resolve => setTimeout(resolve, delay));
|
|
488
|
+
*
|
|
489
|
+
* attempt++;
|
|
490
|
+
* }
|
|
491
|
+
* }
|
|
492
|
+
*
|
|
493
|
+
* throw new Error(`Operation failed after ${maxRetries} attempts`);
|
|
494
|
+
* }
|
|
495
|
+
* ```
|
|
496
|
+
*/
|
|
497
|
+
export declare function classifyToolError(error: Error | unknown): ErrorClassification;
|
|
498
|
+
//# sourceMappingURL=tool-schemas.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tool-schemas.d.ts","sourceRoot":"","sources":["../../src/types/tool-schemas.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,EAAE,CAAC,EAAE,MAAM,kBAAkB,CAAC;AAMrC;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