@revenium/anthropic 1.0.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/LICENSE +21 -0
- package/README.md +807 -0
- package/dist/cjs/config.js +208 -0
- package/dist/cjs/constants.js +144 -0
- package/dist/cjs/index.js +148 -0
- package/dist/cjs/tracking.js +252 -0
- package/dist/cjs/types/anthropic-augmentation.js +87 -0
- package/dist/cjs/types.js +6 -0
- package/dist/cjs/utils/circuit-breaker.js +232 -0
- package/dist/cjs/utils/error-handling.js +233 -0
- package/dist/cjs/utils/validation.js +307 -0
- package/dist/cjs/wrapper.js +374 -0
- package/dist/esm/config.js +197 -0
- package/dist/esm/constants.js +141 -0
- package/dist/esm/index.js +121 -0
- package/dist/esm/tracking.js +243 -0
- package/dist/esm/types/anthropic-augmentation.js +86 -0
- package/dist/esm/types.js +5 -0
- package/dist/esm/utils/circuit-breaker.js +223 -0
- package/dist/esm/utils/error-handling.js +216 -0
- package/dist/esm/utils/validation.js +296 -0
- package/dist/esm/wrapper.js +366 -0
- package/dist/types/config.d.ts +43 -0
- package/dist/types/constants.d.ts +141 -0
- package/dist/types/index.d.ts +54 -0
- package/dist/types/tracking.d.ts +42 -0
- package/dist/types/types/anthropic-augmentation.d.ts +182 -0
- package/dist/types/types.d.ts +647 -0
- package/dist/types/utils/circuit-breaker.d.ts +110 -0
- package/dist/types/utils/error-handling.d.ts +108 -0
- package/dist/types/utils/validation.d.ts +57 -0
- package/dist/types/wrapper.d.ts +16 -0
- package/package.json +74 -0
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Circuit breaker pattern implementation for handling repeated failures
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Circuit breaker states
|
|
6
|
+
*/
|
|
7
|
+
export declare enum CircuitState {
|
|
8
|
+
CLOSED = "CLOSED",// Normal operation
|
|
9
|
+
OPEN = "OPEN",// Failing fast
|
|
10
|
+
HALF_OPEN = "HALF_OPEN"
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Circuit breaker configuration
|
|
14
|
+
*/
|
|
15
|
+
export interface CircuitBreakerConfig {
|
|
16
|
+
/** Number of failures before opening circuit */
|
|
17
|
+
failureThreshold: number;
|
|
18
|
+
/** Time in ms to wait before attempting recovery */
|
|
19
|
+
recoveryTimeout: number;
|
|
20
|
+
/** Number of successful calls needed to close circuit from half-open */
|
|
21
|
+
successThreshold: number;
|
|
22
|
+
/** Time window in ms for counting failures */
|
|
23
|
+
timeWindow: number;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Default circuit breaker configuration
|
|
27
|
+
*/
|
|
28
|
+
export declare const DEFAULT_CIRCUIT_CONFIG: CircuitBreakerConfig;
|
|
29
|
+
/**
|
|
30
|
+
* Circuit breaker implementation
|
|
31
|
+
*/
|
|
32
|
+
export declare class CircuitBreaker {
|
|
33
|
+
private config;
|
|
34
|
+
private state;
|
|
35
|
+
private failureCount;
|
|
36
|
+
private successCount;
|
|
37
|
+
private lastFailureTime;
|
|
38
|
+
private failures;
|
|
39
|
+
private lastCleanupTime;
|
|
40
|
+
private readonly maxFailureHistorySize;
|
|
41
|
+
constructor(config?: CircuitBreakerConfig);
|
|
42
|
+
/**
|
|
43
|
+
* Execute a function with circuit breaker protection
|
|
44
|
+
*/
|
|
45
|
+
execute<T>(fn: () => Promise<T>): Promise<T>;
|
|
46
|
+
/**
|
|
47
|
+
* Check if circuit breaker allows execution
|
|
48
|
+
*/
|
|
49
|
+
canExecute(): boolean;
|
|
50
|
+
/**
|
|
51
|
+
* Get current circuit breaker state
|
|
52
|
+
*/
|
|
53
|
+
getState(): CircuitState;
|
|
54
|
+
/**
|
|
55
|
+
* Get circuit breaker statistics
|
|
56
|
+
*/
|
|
57
|
+
getStats(): {
|
|
58
|
+
state: CircuitState;
|
|
59
|
+
failureCount: number;
|
|
60
|
+
successCount: number;
|
|
61
|
+
recentFailures: number;
|
|
62
|
+
timeUntilRecovery?: number;
|
|
63
|
+
};
|
|
64
|
+
/**
|
|
65
|
+
* Reset circuit breaker to closed state
|
|
66
|
+
*/
|
|
67
|
+
reset(): void;
|
|
68
|
+
/**
|
|
69
|
+
* Handle successful execution
|
|
70
|
+
*/
|
|
71
|
+
private onSuccess;
|
|
72
|
+
/**
|
|
73
|
+
* Handle failed execution
|
|
74
|
+
*/
|
|
75
|
+
private onFailure;
|
|
76
|
+
/**
|
|
77
|
+
* Check if we should attempt recovery from open state
|
|
78
|
+
*/
|
|
79
|
+
private shouldAttemptRecovery;
|
|
80
|
+
/**
|
|
81
|
+
* Remove old failure timestamps outside the time window
|
|
82
|
+
*/
|
|
83
|
+
private cleanupOldFailures;
|
|
84
|
+
/**
|
|
85
|
+
* Perform periodic cleanup to prevent memory leaks
|
|
86
|
+
* Only runs cleanup if enough time has passed since last cleanup
|
|
87
|
+
*/
|
|
88
|
+
private performPeriodicCleanup;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Get or create the global circuit breaker instance
|
|
92
|
+
*/
|
|
93
|
+
export declare function getCircuitBreaker(config?: Partial<CircuitBreakerConfig>): CircuitBreaker;
|
|
94
|
+
/**
|
|
95
|
+
* Reset the global circuit breaker
|
|
96
|
+
*/
|
|
97
|
+
export declare function resetCircuitBreaker(): void;
|
|
98
|
+
/**
|
|
99
|
+
* Check if the global circuit breaker allows execution
|
|
100
|
+
*/
|
|
101
|
+
export declare function canExecuteRequest(): boolean;
|
|
102
|
+
/**
|
|
103
|
+
* Execute a function with global circuit breaker protection
|
|
104
|
+
*/
|
|
105
|
+
export declare function executeWithCircuitBreaker<T>(fn: () => Promise<T>): Promise<T>;
|
|
106
|
+
/**
|
|
107
|
+
* Get global circuit breaker statistics
|
|
108
|
+
*/
|
|
109
|
+
export declare function getCircuitBreakerStats(): ReturnType<CircuitBreaker['getStats']>;
|
|
110
|
+
//# sourceMappingURL=circuit-breaker.d.ts.map
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Error handling utilities for Anthropic middleware with structured errors and context building
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Base error class for Revenium middleware errors
|
|
6
|
+
*/
|
|
7
|
+
export declare class ReveniumError extends Error {
|
|
8
|
+
readonly code: string;
|
|
9
|
+
readonly context?: Record<string, unknown>;
|
|
10
|
+
constructor(message: string, code: string, context?: Record<string, unknown>);
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Configuration-related errors
|
|
14
|
+
*/
|
|
15
|
+
export declare class ConfigurationError extends ReveniumError {
|
|
16
|
+
constructor(message: string, context?: Record<string, unknown>);
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Anthropic SDK patching errors
|
|
20
|
+
*/
|
|
21
|
+
export declare class AnthropicPatchingError extends ReveniumError {
|
|
22
|
+
constructor(message: string, context?: Record<string, unknown>);
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Request processing errors
|
|
26
|
+
*/
|
|
27
|
+
export declare class RequestProcessingError extends ReveniumError {
|
|
28
|
+
constructor(message: string, context?: Record<string, unknown>);
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Revenium API communication errors
|
|
32
|
+
*/
|
|
33
|
+
export declare class ReveniumApiError extends ReveniumError {
|
|
34
|
+
readonly statusCode?: number;
|
|
35
|
+
readonly responseBody?: string;
|
|
36
|
+
constructor(message: string, statusCode?: number, responseBody?: string, context?: Record<string, unknown>);
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Validation errors
|
|
40
|
+
*/
|
|
41
|
+
export declare class ValidationError extends ReveniumError {
|
|
42
|
+
readonly validationErrors: string[];
|
|
43
|
+
constructor(message: string, validationErrors: string[], context?: Record<string, unknown>);
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Stream processing errors
|
|
47
|
+
*/
|
|
48
|
+
export declare class StreamProcessingError extends ReveniumError {
|
|
49
|
+
constructor(message: string, context?: Record<string, unknown>);
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Error context builder for consistent error reporting
|
|
53
|
+
*/
|
|
54
|
+
export declare class ErrorContext {
|
|
55
|
+
private context;
|
|
56
|
+
/**
|
|
57
|
+
* Add request ID to error context
|
|
58
|
+
*/
|
|
59
|
+
withRequestId(requestId: string): this;
|
|
60
|
+
/**
|
|
61
|
+
* Add model information to error context
|
|
62
|
+
*/
|
|
63
|
+
withModel(model: string): this;
|
|
64
|
+
/**
|
|
65
|
+
* Add duration to error context
|
|
66
|
+
*/
|
|
67
|
+
withDuration(duration: number): this;
|
|
68
|
+
/**
|
|
69
|
+
* Add HTTP status to error context
|
|
70
|
+
*/
|
|
71
|
+
withStatus(status: number): this;
|
|
72
|
+
/**
|
|
73
|
+
* Add custom field to error context
|
|
74
|
+
*/
|
|
75
|
+
with(key: string, value: unknown): this;
|
|
76
|
+
/**
|
|
77
|
+
* Build the context object
|
|
78
|
+
*/
|
|
79
|
+
build(): Record<string, unknown>;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Safe error message extraction
|
|
83
|
+
*/
|
|
84
|
+
export declare function getErrorMessage(error: unknown): string;
|
|
85
|
+
/**
|
|
86
|
+
* Safe error stack extraction
|
|
87
|
+
*/
|
|
88
|
+
export declare function getErrorStack(error: unknown): string | undefined;
|
|
89
|
+
/**
|
|
90
|
+
* Create error context builder
|
|
91
|
+
*/
|
|
92
|
+
export declare function createErrorContext(): ErrorContext;
|
|
93
|
+
/**
|
|
94
|
+
* Handle and log errors consistently
|
|
95
|
+
*/
|
|
96
|
+
export declare function handleError(error: unknown, logger: {
|
|
97
|
+
error: (message: string, context?: Record<string, unknown>) => void;
|
|
98
|
+
}, context?: Record<string, unknown>): void;
|
|
99
|
+
/**
|
|
100
|
+
* Wrap async function with error handling
|
|
101
|
+
*/
|
|
102
|
+
export declare function withErrorHandling<T extends unknown[], R>(fn: (...args: T) => Promise<R>, errorHandler: (error: unknown, ...args: T) => void): (...args: T) => Promise<R | undefined>;
|
|
103
|
+
/**
|
|
104
|
+
* Retry function with exponential backoff
|
|
105
|
+
*/
|
|
106
|
+
export declare function withRetry<T>(fn: () => Promise<T>, maxRetries?: number, // Convert to attempts for backward compatibility
|
|
107
|
+
baseDelay?: number): Promise<T>;
|
|
108
|
+
//# sourceMappingURL=error-handling.d.ts.map
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Validation utilities for Anthropic middleware
|
|
3
|
+
* Provides type-safe validation with detailed error reporting
|
|
4
|
+
*/
|
|
5
|
+
import { ReveniumConfig, UsageMetadata } from '../types';
|
|
6
|
+
/**
|
|
7
|
+
* Type guard for checking if a value is a non-null object
|
|
8
|
+
*/
|
|
9
|
+
export declare function isObject(value: unknown): value is Record<string, unknown>;
|
|
10
|
+
/**
|
|
11
|
+
* Type guard for checking if a value is a string
|
|
12
|
+
*/
|
|
13
|
+
export declare function isString(value: unknown): value is string;
|
|
14
|
+
/**
|
|
15
|
+
* Type guard for checking if a value is a number
|
|
16
|
+
*/
|
|
17
|
+
export declare function isNumber(value: unknown): value is number;
|
|
18
|
+
/**
|
|
19
|
+
* Type guard for checking if a value is a boolean
|
|
20
|
+
*/
|
|
21
|
+
export declare function isBoolean(value: unknown): value is boolean;
|
|
22
|
+
/**
|
|
23
|
+
* Validate and extract string from unknown value
|
|
24
|
+
*/
|
|
25
|
+
export declare function validateString(value: unknown, defaultValue?: string): string;
|
|
26
|
+
/**
|
|
27
|
+
* Validate and extract number from unknown value
|
|
28
|
+
*/
|
|
29
|
+
export declare function validateNumber(value: unknown, defaultValue?: number): number;
|
|
30
|
+
/**
|
|
31
|
+
* Validate usage metadata object with Anthropic-specific considerations
|
|
32
|
+
*/
|
|
33
|
+
export declare function validateUsageMetadata(metadata: unknown): UsageMetadata;
|
|
34
|
+
/**
|
|
35
|
+
* Configuration validation result interface
|
|
36
|
+
*/
|
|
37
|
+
interface ConfigValidationResult {
|
|
38
|
+
isValid: boolean;
|
|
39
|
+
errors: string[];
|
|
40
|
+
warnings: string[];
|
|
41
|
+
config?: ReveniumConfig;
|
|
42
|
+
suggestions?: string[];
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Comprehensive Revenium configuration validation for Anthropic
|
|
46
|
+
*/
|
|
47
|
+
export declare function validateReveniumConfig(config: unknown): ConfigValidationResult;
|
|
48
|
+
/**
|
|
49
|
+
* Validate Anthropic message creation parameters
|
|
50
|
+
*/
|
|
51
|
+
export declare function validateAnthropicMessageParams(params: unknown): {
|
|
52
|
+
isValid: boolean;
|
|
53
|
+
errors: string[];
|
|
54
|
+
warnings: string[];
|
|
55
|
+
};
|
|
56
|
+
export {};
|
|
57
|
+
//# sourceMappingURL=validation.d.ts.map
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Anthropic SDK wrapper with type safety and structured error handling
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Patch Anthropic SDK by modifying prototype methods
|
|
6
|
+
*/
|
|
7
|
+
export declare function patchAnthropic(): void;
|
|
8
|
+
/**
|
|
9
|
+
* Unpatch Anthropic SDK (restore original methods)
|
|
10
|
+
*/
|
|
11
|
+
export declare function unpatchAnthropic(): void;
|
|
12
|
+
/**
|
|
13
|
+
* Check if Anthropic SDK is patched
|
|
14
|
+
*/
|
|
15
|
+
export declare function isAnthropicPatched(): boolean;
|
|
16
|
+
//# sourceMappingURL=wrapper.d.ts.map
|
package/package.json
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@revenium/anthropic",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Transparent TypeScript middleware for automatic Revenium usage tracking with Anthropic Claude AI",
|
|
5
|
+
"main": "dist/cjs/index.js",
|
|
6
|
+
"module": "dist/esm/index.js",
|
|
7
|
+
"types": "dist/types/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"import": {
|
|
11
|
+
"types": "./dist/types/index.d.ts",
|
|
12
|
+
"default": "./dist/esm/index.js"
|
|
13
|
+
},
|
|
14
|
+
"require": {
|
|
15
|
+
"types": "./dist/types/index.d.ts",
|
|
16
|
+
"default": "./dist/cjs/index.js"
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"scripts": {
|
|
21
|
+
"build": "npm run build:cjs && npm run build:esm && npm run build:types && npm run fix-esm",
|
|
22
|
+
"build:cjs": "tsc -p tsconfig.json",
|
|
23
|
+
"build:esm": "tsc -p tsconfig.esm.json",
|
|
24
|
+
"build:types": "tsc -p tsconfig.types.json",
|
|
25
|
+
"fix-esm": "node scripts/fix-esm-imports.js",
|
|
26
|
+
"dev": "tsc --watch",
|
|
27
|
+
"test": "npm run build && echo 'Build successful - no automated tests configured'",
|
|
28
|
+
"example": "npm run build && tsx examples/basic-usage.ts",
|
|
29
|
+
"example:basic": "npm run build && tsx examples/basic-usage.ts",
|
|
30
|
+
"example:advanced": "npm run build && tsx examples/advanced-features.ts",
|
|
31
|
+
"clean": "rimraf dist",
|
|
32
|
+
"prepublishOnly": "npm run clean && npm run build"
|
|
33
|
+
},
|
|
34
|
+
"keywords": [
|
|
35
|
+
"anthropic",
|
|
36
|
+
"claude",
|
|
37
|
+
"revenium",
|
|
38
|
+
"middleware",
|
|
39
|
+
"typescript",
|
|
40
|
+
"nodejs",
|
|
41
|
+
"usage-tracking",
|
|
42
|
+
"metering",
|
|
43
|
+
"ai"
|
|
44
|
+
],
|
|
45
|
+
"author": "Revenium",
|
|
46
|
+
"license": "MIT",
|
|
47
|
+
"dependencies": {
|
|
48
|
+
"node-fetch": "^3.3.2"
|
|
49
|
+
},
|
|
50
|
+
"peerDependencies": {
|
|
51
|
+
"@anthropic-ai/sdk": ">=0.24.0"
|
|
52
|
+
},
|
|
53
|
+
"devDependencies": {
|
|
54
|
+
"@anthropic-ai/sdk": "^0.55.1",
|
|
55
|
+
"@types/node": "^20.0.0",
|
|
56
|
+
"@types/node-fetch": "^2.6.12",
|
|
57
|
+
"dotenv": "^16.5.0",
|
|
58
|
+
"rimraf": "^6.0.1",
|
|
59
|
+
"ts-node": "^10.9.2",
|
|
60
|
+
"tsx": "^4.20.3",
|
|
61
|
+
"typescript": "^5.8.3"
|
|
62
|
+
},
|
|
63
|
+
"files": [
|
|
64
|
+
"dist/cjs/**/*.js",
|
|
65
|
+
"dist/esm/**/*.js",
|
|
66
|
+
"dist/types/**/*.d.ts",
|
|
67
|
+
"README.md",
|
|
68
|
+
"LICENSE"
|
|
69
|
+
],
|
|
70
|
+
"sideEffects": false,
|
|
71
|
+
"engines": {
|
|
72
|
+
"node": ">=16.0.0"
|
|
73
|
+
}
|
|
74
|
+
}
|