@revenium/litellm 0.0.1
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 +630 -0
- package/dist/client.d.ts +17 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +713 -0
- package/dist/client.js.map +1 -0
- package/dist/config.d.ts +42 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +332 -0
- package/dist/config.js.map +1 -0
- package/dist/constants.d.ts +15 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/constants.js +101 -0
- package/dist/constants.js.map +1 -0
- package/dist/index.d.ts +42 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +189 -0
- package/dist/index.js.map +1 -0
- package/dist/prompt-extraction.d.ts +11 -0
- package/dist/prompt-extraction.d.ts.map +1 -0
- package/dist/prompt-extraction.js +201 -0
- package/dist/prompt-extraction.js.map +1 -0
- package/dist/tracking.d.ts +47 -0
- package/dist/tracking.d.ts.map +1 -0
- package/dist/tracking.js +299 -0
- package/dist/tracking.js.map +1 -0
- package/dist/types.d.ts +348 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/dist/utils/circuit-breaker.d.ts +114 -0
- package/dist/utils/circuit-breaker.d.ts.map +1 -0
- package/dist/utils/circuit-breaker.js +216 -0
- package/dist/utils/circuit-breaker.js.map +1 -0
- package/dist/utils/error-handling.d.ts +166 -0
- package/dist/utils/error-handling.d.ts.map +1 -0
- package/dist/utils/error-handling.js +306 -0
- package/dist/utils/error-handling.js.map +1 -0
- package/dist/utils/logger-types.d.ts +171 -0
- package/dist/utils/logger-types.d.ts.map +1 -0
- package/dist/utils/logger-types.js +210 -0
- package/dist/utils/logger-types.js.map +1 -0
- package/dist/utils/provider-detection.d.ts +43 -0
- package/dist/utils/provider-detection.d.ts.map +1 -0
- package/dist/utils/provider-detection.js +103 -0
- package/dist/utils/provider-detection.js.map +1 -0
- package/dist/utils/stop-reason.d.ts +58 -0
- package/dist/utils/stop-reason.d.ts.map +1 -0
- package/dist/utils/stop-reason.js +136 -0
- package/dist/utils/stop-reason.js.map +1 -0
- package/dist/utils/summary-printer.d.ts +23 -0
- package/dist/utils/summary-printer.d.ts.map +1 -0
- package/dist/utils/summary-printer.js +234 -0
- package/dist/utils/summary-printer.js.map +1 -0
- package/dist/utils/trace-fields.d.ts +10 -0
- package/dist/utils/trace-fields.d.ts.map +1 -0
- package/dist/utils/trace-fields.js +117 -0
- package/dist/utils/trace-fields.js.map +1 -0
- package/dist/utils/validation.d.ts +121 -0
- package/dist/utils/validation.d.ts.map +1 -0
- package/dist/utils/validation.js +451 -0
- package/dist/utils/validation.js.map +1 -0
- package/examples/README.md +321 -0
- package/examples/litellm-basic.ts +240 -0
- package/examples/litellm-streaming.ts +309 -0
- package/examples/prompt-capture.ts +128 -0
- package/package.json +85 -0
|
@@ -0,0 +1,306 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Error handling utilities for consistent error management
|
|
4
|
+
*
|
|
5
|
+
* This module provides centralized error handling and custom error types
|
|
6
|
+
* for better error reporting and debugging.
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.DEFAULT_ERROR_STRATEGY = exports.ErrorContext = exports.StreamProcessingError = exports.ValidationError = exports.ReveniumApiError = exports.RequestProcessingError = exports.PatchingError = exports.ConfigurationError = exports.ReveniumError = void 0;
|
|
10
|
+
exports.getErrorMessage = getErrorMessage;
|
|
11
|
+
exports.getErrorStack = getErrorStack;
|
|
12
|
+
exports.createErrorContext = createErrorContext;
|
|
13
|
+
exports.handleError = handleError;
|
|
14
|
+
exports.withErrorHandling = withErrorHandling;
|
|
15
|
+
exports.executeWithErrorHandling = executeWithErrorHandling;
|
|
16
|
+
exports.withRetry = withRetry;
|
|
17
|
+
/**
|
|
18
|
+
* Base error class for Revenium middleware errors
|
|
19
|
+
*/
|
|
20
|
+
class ReveniumError extends Error {
|
|
21
|
+
constructor(message, code, context) {
|
|
22
|
+
super(message);
|
|
23
|
+
this.name = 'ReveniumError';
|
|
24
|
+
this.code = code;
|
|
25
|
+
this.context = context;
|
|
26
|
+
// Maintain proper stack trace for where our error was thrown
|
|
27
|
+
if (Error.captureStackTrace)
|
|
28
|
+
Error.captureStackTrace(this, ReveniumError);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
exports.ReveniumError = ReveniumError;
|
|
32
|
+
/**
|
|
33
|
+
* Configuration-related errors
|
|
34
|
+
*/
|
|
35
|
+
class ConfigurationError extends ReveniumError {
|
|
36
|
+
constructor(message, context) {
|
|
37
|
+
super(message, 'CONFIGURATION_ERROR', context);
|
|
38
|
+
this.name = 'ConfigurationError';
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
exports.ConfigurationError = ConfigurationError;
|
|
42
|
+
/**
|
|
43
|
+
* HTTP client patching errors
|
|
44
|
+
*/
|
|
45
|
+
class PatchingError extends ReveniumError {
|
|
46
|
+
constructor(message, context) {
|
|
47
|
+
super(message, 'PATCHING_ERROR', context);
|
|
48
|
+
this.name = 'PatchingError';
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
exports.PatchingError = PatchingError;
|
|
52
|
+
/**
|
|
53
|
+
* Request processing errors
|
|
54
|
+
*/
|
|
55
|
+
class RequestProcessingError extends ReveniumError {
|
|
56
|
+
constructor(message, context) {
|
|
57
|
+
super(message, 'REQUEST_PROCESSING_ERROR', context);
|
|
58
|
+
this.name = 'RequestProcessingError';
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
exports.RequestProcessingError = RequestProcessingError;
|
|
62
|
+
/**
|
|
63
|
+
* Revenium API communication errors
|
|
64
|
+
*/
|
|
65
|
+
class ReveniumApiError extends ReveniumError {
|
|
66
|
+
constructor(message, statusCode, responseBody, context) {
|
|
67
|
+
super(message, 'REVENIUM_API_ERROR', context);
|
|
68
|
+
this.name = 'ReveniumApiError';
|
|
69
|
+
this.statusCode = statusCode;
|
|
70
|
+
this.responseBody = responseBody;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
exports.ReveniumApiError = ReveniumApiError;
|
|
74
|
+
/**
|
|
75
|
+
* Validation errors
|
|
76
|
+
*/
|
|
77
|
+
class ValidationError extends ReveniumError {
|
|
78
|
+
constructor(message, validationErrors, context) {
|
|
79
|
+
super(message, 'VALIDATION_ERROR', context);
|
|
80
|
+
this.name = 'ValidationError';
|
|
81
|
+
this.validationErrors = validationErrors;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
exports.ValidationError = ValidationError;
|
|
85
|
+
/**
|
|
86
|
+
* Stream processing errors
|
|
87
|
+
*/
|
|
88
|
+
class StreamProcessingError extends ReveniumError {
|
|
89
|
+
constructor(message, context) {
|
|
90
|
+
super(message, 'STREAM_PROCESSING_ERROR', context);
|
|
91
|
+
this.name = 'StreamProcessingError';
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
exports.StreamProcessingError = StreamProcessingError;
|
|
95
|
+
/**
|
|
96
|
+
* Error context builder for consistent error reporting
|
|
97
|
+
*/
|
|
98
|
+
class ErrorContext {
|
|
99
|
+
constructor() {
|
|
100
|
+
this.context = {};
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Add request ID to error context
|
|
104
|
+
*/
|
|
105
|
+
withRequestId(requestId) {
|
|
106
|
+
this.context.requestId = requestId;
|
|
107
|
+
return this;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Add model information to error context
|
|
111
|
+
*/
|
|
112
|
+
withModel(model) {
|
|
113
|
+
this.context.model = model;
|
|
114
|
+
return this;
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Add URL to error context
|
|
118
|
+
*/
|
|
119
|
+
withUrl(url) {
|
|
120
|
+
this.context.url = url;
|
|
121
|
+
return this;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Add HTTP status to error context
|
|
125
|
+
*/
|
|
126
|
+
withStatus(status) {
|
|
127
|
+
this.context.status = status;
|
|
128
|
+
return this;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Add duration to error context
|
|
132
|
+
*/
|
|
133
|
+
withDuration(duration) {
|
|
134
|
+
this.context.duration = duration;
|
|
135
|
+
return this;
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Add custom field to error context
|
|
139
|
+
*/
|
|
140
|
+
with(key, value) {
|
|
141
|
+
this.context[key] = value;
|
|
142
|
+
return this;
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Build the context object
|
|
146
|
+
*/
|
|
147
|
+
build() {
|
|
148
|
+
return { ...this.context };
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
exports.ErrorContext = ErrorContext;
|
|
152
|
+
/**
|
|
153
|
+
* Safe error message extraction
|
|
154
|
+
* @param error - Error to extract message from
|
|
155
|
+
* @returns Safe error message string
|
|
156
|
+
*/
|
|
157
|
+
function getErrorMessage(error) {
|
|
158
|
+
if (error instanceof Error)
|
|
159
|
+
return error.message;
|
|
160
|
+
if (typeof error === 'string')
|
|
161
|
+
return error;
|
|
162
|
+
if (error && typeof error === 'object' && 'message' in error) {
|
|
163
|
+
const message = error.message;
|
|
164
|
+
if (typeof message === 'string') {
|
|
165
|
+
return message;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
return 'Unknown error occurred';
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Safe error stack extraction
|
|
172
|
+
* @param error - Error to extract stack from
|
|
173
|
+
* @returns Error stack or undefined
|
|
174
|
+
*/
|
|
175
|
+
function getErrorStack(error) {
|
|
176
|
+
if (error instanceof Error && error.stack)
|
|
177
|
+
return error.stack;
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Create error context builder
|
|
182
|
+
* @returns New error context builder
|
|
183
|
+
*/
|
|
184
|
+
function createErrorContext() {
|
|
185
|
+
return new ErrorContext();
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Handle and log errors consistently
|
|
189
|
+
* @param error - Error to handle
|
|
190
|
+
* @param logger - Logger instance
|
|
191
|
+
* @param context - Additional context
|
|
192
|
+
*/
|
|
193
|
+
function handleError(error, logger, context) {
|
|
194
|
+
const message = getErrorMessage(error);
|
|
195
|
+
const stack = getErrorStack(error);
|
|
196
|
+
const logContext = {
|
|
197
|
+
...context,
|
|
198
|
+
error: message,
|
|
199
|
+
stack: stack
|
|
200
|
+
};
|
|
201
|
+
if (error instanceof ReveniumError) {
|
|
202
|
+
logContext.errorCode = error.code;
|
|
203
|
+
logContext.errorContext = error.context;
|
|
204
|
+
if (error instanceof ReveniumApiError) {
|
|
205
|
+
logContext.statusCode = error.statusCode;
|
|
206
|
+
logContext.responseBody = error.responseBody;
|
|
207
|
+
}
|
|
208
|
+
if (error instanceof ValidationError) {
|
|
209
|
+
logContext.validationErrors = error.validationErrors;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
logger.error('Error occurred', logContext);
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Wrap async function with error handling
|
|
216
|
+
* @param fn - Function to wrap
|
|
217
|
+
* @param errorHandler - Error handler function
|
|
218
|
+
* @returns Wrapped function
|
|
219
|
+
*/
|
|
220
|
+
function withErrorHandling(fn, errorHandler) {
|
|
221
|
+
return async (...args) => {
|
|
222
|
+
try {
|
|
223
|
+
return await fn(...args);
|
|
224
|
+
}
|
|
225
|
+
catch (error) {
|
|
226
|
+
errorHandler(error, ...args);
|
|
227
|
+
return undefined;
|
|
228
|
+
}
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* Default error handling strategy
|
|
233
|
+
*/
|
|
234
|
+
exports.DEFAULT_ERROR_STRATEGY = {
|
|
235
|
+
failSilent: true,
|
|
236
|
+
maxRetries: 3,
|
|
237
|
+
baseDelay: 1000,
|
|
238
|
+
logErrors: true
|
|
239
|
+
};
|
|
240
|
+
/**
|
|
241
|
+
* Execute operation with consistent error handling
|
|
242
|
+
* @param fn - Function to execute
|
|
243
|
+
* @param strategy - Error handling strategy
|
|
244
|
+
* @param logger - Logger instance
|
|
245
|
+
* @param context - Additional context for logging
|
|
246
|
+
* @returns Operation result
|
|
247
|
+
*/
|
|
248
|
+
async function executeWithErrorHandling(fn, strategy = exports.DEFAULT_ERROR_STRATEGY, logger, context) {
|
|
249
|
+
let lastError;
|
|
250
|
+
for (let attempt = 0; attempt <= strategy.maxRetries; attempt++) {
|
|
251
|
+
try {
|
|
252
|
+
const result = await fn();
|
|
253
|
+
return {
|
|
254
|
+
success: true,
|
|
255
|
+
data: result,
|
|
256
|
+
retryCount: attempt
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
catch (error) {
|
|
260
|
+
lastError = error instanceof Error ? error : new Error(String(error));
|
|
261
|
+
if (strategy.logErrors && logger) {
|
|
262
|
+
logger.error(`Operation failed (attempt ${attempt + 1}/${strategy.maxRetries + 1})`, {
|
|
263
|
+
...context,
|
|
264
|
+
error: lastError.message,
|
|
265
|
+
stack: lastError.stack,
|
|
266
|
+
attempt: attempt + 1
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
if (attempt === strategy.maxRetries) {
|
|
270
|
+
break;
|
|
271
|
+
}
|
|
272
|
+
// Exponential backoff with jitter
|
|
273
|
+
const delay = Math.min(strategy.baseDelay * Math.pow(2, attempt), 5000);
|
|
274
|
+
const jitter = Math.random() * 0.1 * delay;
|
|
275
|
+
await new Promise(resolve => setTimeout(resolve, delay + jitter));
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
const result = {
|
|
279
|
+
success: false,
|
|
280
|
+
error: lastError,
|
|
281
|
+
retryCount: strategy.maxRetries + 1
|
|
282
|
+
};
|
|
283
|
+
if (!strategy.failSilent && lastError)
|
|
284
|
+
throw lastError;
|
|
285
|
+
return result;
|
|
286
|
+
}
|
|
287
|
+
/**
|
|
288
|
+
* Retry function with exponential backoff
|
|
289
|
+
* @param fn - Function to retry
|
|
290
|
+
* @param maxRetries - Maximum number of retries
|
|
291
|
+
* @param baseDelay - Base delay in milliseconds
|
|
292
|
+
* @returns Promise that resolves with function result or rejects with last error
|
|
293
|
+
*/
|
|
294
|
+
async function withRetry(fn, maxRetries = 3, baseDelay = 1000) {
|
|
295
|
+
const strategy = {
|
|
296
|
+
failSilent: false,
|
|
297
|
+
maxRetries,
|
|
298
|
+
baseDelay,
|
|
299
|
+
logErrors: false
|
|
300
|
+
};
|
|
301
|
+
const result = await executeWithErrorHandling(fn, strategy);
|
|
302
|
+
if (result.success && result.data)
|
|
303
|
+
return result.data;
|
|
304
|
+
throw result.error || new Error('Operation failed');
|
|
305
|
+
}
|
|
306
|
+
//# sourceMappingURL=error-handling.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"error-handling.js","sourceRoot":"","sources":["../../src/utils/error-handling.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAgKH,0CAUC;AAOD,sCAGC;AAMD,gDAEC;AAQD,kCA4BC;AAQD,8CAYC;AA4CD,4DA+CC;AASD,8BAeC;AArWD;;GAEG;AACH,MAAa,aAAc,SAAQ,KAAK;IAItC,YAAY,OAAe,EAAE,IAAY,EAAE,OAAiC;QAC1E,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,eAAe,CAAC;QAC5B,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QAEvB,6DAA6D;QAC7D,IAAI,KAAK,CAAC,iBAAiB;YAAE,KAAK,CAAC,iBAAiB,CAAC,IAAI,EAAE,aAAa,CAAC,CAAC;IAC5E,CAAC;CACF;AAbD,sCAaC;AAED;;GAEG;AACH,MAAa,kBAAmB,SAAQ,aAAa;IACnD,YAAY,OAAe,EAAE,OAAiC;QAC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,EAAE,EAAE,QAAQ,CAAC,CAAC;IAC5D,IAAI,MAAM,CAAC,OAAO,IAAI,MAAM,CAAC,IAAI;QAAE,OAAO,MAAM,CAAC,IAAI,CAAC;IACtD,MAAM,MAAM,CAAC,KAAK,IAAI,IAAI,KAAK,CAAC,kBAAkB,CAAC,CAAC;AACtD,CAAC"}
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Enhanced logger types and utilities
|
|
3
|
+
*
|
|
4
|
+
* This module provides type-safe logging interfaces and utilities
|
|
5
|
+
* to replace 'any' usage in logger implementations.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Supported log levels
|
|
9
|
+
*/
|
|
10
|
+
export declare enum LogLevel {
|
|
11
|
+
DEBUG = "DEBUG",
|
|
12
|
+
INFO = "INFO",
|
|
13
|
+
WARNING = "WARNING",
|
|
14
|
+
ERROR = "ERROR"
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Serializable log data types
|
|
18
|
+
*/
|
|
19
|
+
export type LogData = string | number | boolean | null | undefined | LogData[] | {
|
|
20
|
+
[key: string]: LogData;
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* Log context object with known structure
|
|
24
|
+
*/
|
|
25
|
+
export interface LogContext {
|
|
26
|
+
/** Request identifier */
|
|
27
|
+
requestId?: string;
|
|
28
|
+
/** Model being used */
|
|
29
|
+
model?: string;
|
|
30
|
+
/** URL being accessed */
|
|
31
|
+
url?: string;
|
|
32
|
+
/** HTTP status code */
|
|
33
|
+
status?: number;
|
|
34
|
+
/** Request duration in milliseconds */
|
|
35
|
+
duration?: number;
|
|
36
|
+
/** Error message */
|
|
37
|
+
error?: string;
|
|
38
|
+
/** Error stack trace */
|
|
39
|
+
stack?: string;
|
|
40
|
+
/** Token counts */
|
|
41
|
+
promptTokens?: number;
|
|
42
|
+
completionTokens?: number;
|
|
43
|
+
totalTokens?: number;
|
|
44
|
+
/** Provider information */
|
|
45
|
+
provider?: string;
|
|
46
|
+
/** Additional structured data */
|
|
47
|
+
[key: string]: LogData;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Enhanced logger interface with type safety
|
|
51
|
+
*/
|
|
52
|
+
export interface TypeSafeLogger {
|
|
53
|
+
/**
|
|
54
|
+
* Log debug message
|
|
55
|
+
* @param message - Log message
|
|
56
|
+
* @param context - Optional structured context
|
|
57
|
+
*/
|
|
58
|
+
debug(message: string, context?: LogContext): void;
|
|
59
|
+
/**
|
|
60
|
+
* Log info message
|
|
61
|
+
* @param message - Log message
|
|
62
|
+
* @param context - Optional structured context
|
|
63
|
+
*/
|
|
64
|
+
info(message: string, context?: LogContext): void;
|
|
65
|
+
/**
|
|
66
|
+
* Log warning message
|
|
67
|
+
* @param message - Log message
|
|
68
|
+
* @param context - Optional structured context
|
|
69
|
+
*/
|
|
70
|
+
warn(message: string, context?: LogContext): void;
|
|
71
|
+
/**
|
|
72
|
+
* Log error message
|
|
73
|
+
* @param message - Log message
|
|
74
|
+
* @param context - Optional structured context
|
|
75
|
+
*/
|
|
76
|
+
error(message: string, context?: LogContext): void;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Logger configuration options
|
|
80
|
+
*/
|
|
81
|
+
export interface LoggerConfig {
|
|
82
|
+
/** Minimum log level to output */
|
|
83
|
+
level: LogLevel;
|
|
84
|
+
/** Whether to include timestamps */
|
|
85
|
+
includeTimestamp: boolean;
|
|
86
|
+
/** Whether to format output as JSON */
|
|
87
|
+
jsonFormat: boolean;
|
|
88
|
+
/** Custom prefix for log messages */
|
|
89
|
+
prefix: string;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Default logger configuration
|
|
93
|
+
*/
|
|
94
|
+
export declare const DEFAULT_LOGGER_CONFIG: LoggerConfig;
|
|
95
|
+
/**
|
|
96
|
+
* Check if a log level should be output based on configuration
|
|
97
|
+
* @param level - Log level to check
|
|
98
|
+
* @param configLevel - Configured minimum log level
|
|
99
|
+
* @returns True if the level should be logged
|
|
100
|
+
*/
|
|
101
|
+
export declare function shouldLog(level: LogLevel, configLevel: LogLevel): boolean;
|
|
102
|
+
/**
|
|
103
|
+
* Format log context for output
|
|
104
|
+
* @param context - Log context to format
|
|
105
|
+
* @param jsonFormat - Whether to format as JSON
|
|
106
|
+
* @returns Formatted context string
|
|
107
|
+
*/
|
|
108
|
+
export declare function formatLogContext(context: LogContext | undefined, jsonFormat: boolean): string;
|
|
109
|
+
/**
|
|
110
|
+
* Create a timestamp string
|
|
111
|
+
* @returns ISO timestamp string
|
|
112
|
+
*/
|
|
113
|
+
export declare function createTimestamp(): string;
|
|
114
|
+
/**
|
|
115
|
+
* Sanitize log data to ensure it's serializable
|
|
116
|
+
* @param data - Data to sanitize
|
|
117
|
+
* @returns Sanitized data
|
|
118
|
+
*/
|
|
119
|
+
export declare function sanitizeLogData(data: unknown): LogData;
|
|
120
|
+
/**
|
|
121
|
+
* Create a type-safe logger context builder
|
|
122
|
+
*/
|
|
123
|
+
export declare class LogContextBuilder {
|
|
124
|
+
private context;
|
|
125
|
+
/**
|
|
126
|
+
* Add request ID to context
|
|
127
|
+
*/
|
|
128
|
+
withRequestId(requestId: string): this;
|
|
129
|
+
/**
|
|
130
|
+
* Add model to context
|
|
131
|
+
*/
|
|
132
|
+
withModel(model: string): this;
|
|
133
|
+
/**
|
|
134
|
+
* Add URL to context
|
|
135
|
+
*/
|
|
136
|
+
withUrl(url: string): this;
|
|
137
|
+
/**
|
|
138
|
+
* Add HTTP status to context
|
|
139
|
+
*/
|
|
140
|
+
withStatus(status: number): this;
|
|
141
|
+
/**
|
|
142
|
+
* Add duration to context
|
|
143
|
+
*/
|
|
144
|
+
withDuration(duration: number): this;
|
|
145
|
+
/**
|
|
146
|
+
* Add error information to context
|
|
147
|
+
*/
|
|
148
|
+
withError(error: string, stack?: string): this;
|
|
149
|
+
/**
|
|
150
|
+
* Add token counts to context
|
|
151
|
+
*/
|
|
152
|
+
withTokens(prompt: number, completion: number, total: number): this;
|
|
153
|
+
/**
|
|
154
|
+
* Add provider to context
|
|
155
|
+
*/
|
|
156
|
+
withProvider(provider: string): this;
|
|
157
|
+
/**
|
|
158
|
+
* Add custom field to context
|
|
159
|
+
*/
|
|
160
|
+
with(key: string, value: LogData): this;
|
|
161
|
+
/**
|
|
162
|
+
* Build the context object
|
|
163
|
+
*/
|
|
164
|
+
build(): LogContext;
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Create a new log context builder
|
|
168
|
+
* @returns New log context builder instance
|
|
169
|
+
*/
|
|
170
|
+
export declare function createLogContext(): LogContextBuilder;
|
|
171
|
+
//# sourceMappingURL=logger-types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"logger-types.d.ts","sourceRoot":"","sources":["../../src/utils/logger-types.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH;;GAEG;AACH,oBAAY,QAAQ;IAClB,KAAK,UAAU;IACf,IAAI,SAAS;IACb,OAAO,YAAY;IACnB,KAAK,UAAU;CAChB;AAYD;;GAEG;AACH,MAAM,MAAM,OAAO,GACf,MAAM,GACN,MAAM,GACN,OAAO,GACP,IAAI,GACJ,SAAS,GACT,OAAO,EAAE,GACT;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAA;CAAE,CAAC;AAE/B;;GAEG;AACH,MAAM,WAAW,UAAU;IACzB,yBAAyB;IACzB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,uBAAuB;IACvB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,yBAAyB;IACzB,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,uBAAuB;IACvB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,uCAAuC;IACvC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,oBAAoB;IACpB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,wBAAwB;IACxB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,mBAA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AED;;;;GAIG;AACH,wBAAgB,eAAe,CAAC,IAAI,EAAE,OAAO,GAAG,OAAO,CAqBtD;AAED;;GAEG;AACH,qBAAa,iBAAiB;IAC5B,OAAO,CAAC,OAAO,CAAkB;IAEjC;;OAEG;IACH,aAAa,CAAC,SAAS,EAAE,MAAM,GAAG,IAAI;IAKtC;;OAEG;IACH,SAAS,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI;IAK9B;;OAEG;IACH,OAAO,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI;IAK1B;;OAEG;IACH,UAAU,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI;IAKhC;;OAEG;IACH,YAAY,CAAC,QAAQ,EAAE,MAAM,GAAG,IAAI;IAKpC;;OAEG;IACH,SAAS,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,MAAM,GAAG,IAAI;IAM9C;;OAEG;IACH,UAAU,CAAC,MAAM,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,IAAI;IAOnE;;OAEG;IACH,YAAY,CAAC,QAAQ,EAAE,MAAM,GAAG,IAAI;IAKpC;;OAEG;IACH,IAAI,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,GAAG,IAAI;IAKvC;;OAEG;IACH,KAAK,IAAI,UAAU;CAGpB;AAED;;;GAGG;AACH,wBAAgB,gBAAgB,IAAI,iBAAiB,CAEpD"}
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Enhanced logger types and utilities
|
|
4
|
+
*
|
|
5
|
+
* This module provides type-safe logging interfaces and utilities
|
|
6
|
+
* to replace 'any' usage in logger implementations.
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.LogContextBuilder = exports.DEFAULT_LOGGER_CONFIG = exports.LogLevel = void 0;
|
|
10
|
+
exports.shouldLog = shouldLog;
|
|
11
|
+
exports.formatLogContext = formatLogContext;
|
|
12
|
+
exports.createTimestamp = createTimestamp;
|
|
13
|
+
exports.sanitizeLogData = sanitizeLogData;
|
|
14
|
+
exports.createLogContext = createLogContext;
|
|
15
|
+
/**
|
|
16
|
+
* Supported log levels
|
|
17
|
+
*/
|
|
18
|
+
var LogLevel;
|
|
19
|
+
(function (LogLevel) {
|
|
20
|
+
LogLevel["DEBUG"] = "DEBUG";
|
|
21
|
+
LogLevel["INFO"] = "INFO";
|
|
22
|
+
LogLevel["WARNING"] = "WARNING";
|
|
23
|
+
LogLevel["ERROR"] = "ERROR";
|
|
24
|
+
})(LogLevel || (exports.LogLevel = LogLevel = {}));
|
|
25
|
+
/**
|
|
26
|
+
* Log level hierarchy for filtering
|
|
27
|
+
*/
|
|
28
|
+
const LOG_LEVEL_HIERARCHY = {
|
|
29
|
+
[LogLevel.DEBUG]: 0,
|
|
30
|
+
[LogLevel.INFO]: 1,
|
|
31
|
+
[LogLevel.WARNING]: 2,
|
|
32
|
+
[LogLevel.ERROR]: 3
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* Default logger configuration
|
|
36
|
+
*/
|
|
37
|
+
exports.DEFAULT_LOGGER_CONFIG = {
|
|
38
|
+
level: LogLevel.INFO,
|
|
39
|
+
includeTimestamp: true,
|
|
40
|
+
jsonFormat: false,
|
|
41
|
+
prefix: '[Revenium LiteLLM]'
|
|
42
|
+
};
|
|
43
|
+
/**
|
|
44
|
+
* Check if a log level should be output based on configuration
|
|
45
|
+
* @param level - Log level to check
|
|
46
|
+
* @param configLevel - Configured minimum log level
|
|
47
|
+
* @returns True if the level should be logged
|
|
48
|
+
*/
|
|
49
|
+
function shouldLog(level, configLevel) {
|
|
50
|
+
return LOG_LEVEL_HIERARCHY[level] >= LOG_LEVEL_HIERARCHY[configLevel];
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Format log context for output
|
|
54
|
+
* @param context - Log context to format
|
|
55
|
+
* @param jsonFormat - Whether to format as JSON
|
|
56
|
+
* @returns Formatted context string
|
|
57
|
+
*/
|
|
58
|
+
function formatLogContext(context, jsonFormat) {
|
|
59
|
+
if (!context || Object.keys(context).length === 0)
|
|
60
|
+
return '';
|
|
61
|
+
if (jsonFormat) {
|
|
62
|
+
try {
|
|
63
|
+
return JSON.stringify(context);
|
|
64
|
+
}
|
|
65
|
+
catch {
|
|
66
|
+
return '[Invalid JSON context]';
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
// Format as key-value pairs
|
|
70
|
+
const pairs = Object.entries(context)
|
|
71
|
+
.filter(([, value]) => value)
|
|
72
|
+
.map(([key, value]) => {
|
|
73
|
+
if (typeof value === 'object') {
|
|
74
|
+
try {
|
|
75
|
+
return `${key}=${JSON.stringify(value)}`;
|
|
76
|
+
}
|
|
77
|
+
catch {
|
|
78
|
+
return `${key}=[object]`;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return `${key}=${value}`;
|
|
82
|
+
});
|
|
83
|
+
return pairs.length > 0 ? ` ${pairs.join(' ')}` : '';
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Create a timestamp string
|
|
87
|
+
* @returns ISO timestamp string
|
|
88
|
+
*/
|
|
89
|
+
function createTimestamp() {
|
|
90
|
+
return new Date().toISOString();
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Sanitize log data to ensure it's serializable
|
|
94
|
+
* @param data - Data to sanitize
|
|
95
|
+
* @returns Sanitized data
|
|
96
|
+
*/
|
|
97
|
+
function sanitizeLogData(data) {
|
|
98
|
+
if (Array.isArray(data))
|
|
99
|
+
return data.map(sanitizeLogData);
|
|
100
|
+
if (data === null)
|
|
101
|
+
return data;
|
|
102
|
+
if (typeof data === 'object') {
|
|
103
|
+
const sanitized = {};
|
|
104
|
+
for (const [key, value] of Object.entries(data)) {
|
|
105
|
+
// Skip functions and symbols
|
|
106
|
+
if (typeof value === 'function' || typeof value === 'symbol')
|
|
107
|
+
continue;
|
|
108
|
+
// Handle circular references and complex objects
|
|
109
|
+
try {
|
|
110
|
+
sanitized[key] = sanitizeLogData(value);
|
|
111
|
+
}
|
|
112
|
+
catch {
|
|
113
|
+
sanitized[key] = '[Circular or Complex Object]';
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return sanitized;
|
|
117
|
+
}
|
|
118
|
+
// Fallback for other types
|
|
119
|
+
return String(data);
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Create a type-safe logger context builder
|
|
123
|
+
*/
|
|
124
|
+
class LogContextBuilder {
|
|
125
|
+
constructor() {
|
|
126
|
+
this.context = {};
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Add request ID to context
|
|
130
|
+
*/
|
|
131
|
+
withRequestId(requestId) {
|
|
132
|
+
this.context.requestId = requestId;
|
|
133
|
+
return this;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Add model to context
|
|
137
|
+
*/
|
|
138
|
+
withModel(model) {
|
|
139
|
+
this.context.model = model;
|
|
140
|
+
return this;
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Add URL to context
|
|
144
|
+
*/
|
|
145
|
+
withUrl(url) {
|
|
146
|
+
this.context.url = url;
|
|
147
|
+
return this;
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Add HTTP status to context
|
|
151
|
+
*/
|
|
152
|
+
withStatus(status) {
|
|
153
|
+
this.context.status = status;
|
|
154
|
+
return this;
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Add duration to context
|
|
158
|
+
*/
|
|
159
|
+
withDuration(duration) {
|
|
160
|
+
this.context.duration = duration;
|
|
161
|
+
return this;
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Add error information to context
|
|
165
|
+
*/
|
|
166
|
+
withError(error, stack) {
|
|
167
|
+
this.context.error = error;
|
|
168
|
+
if (stack)
|
|
169
|
+
this.context.stack = stack;
|
|
170
|
+
return this;
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Add token counts to context
|
|
174
|
+
*/
|
|
175
|
+
withTokens(prompt, completion, total) {
|
|
176
|
+
this.context.promptTokens = prompt;
|
|
177
|
+
this.context.completionTokens = completion;
|
|
178
|
+
this.context.totalTokens = total;
|
|
179
|
+
return this;
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Add provider to context
|
|
183
|
+
*/
|
|
184
|
+
withProvider(provider) {
|
|
185
|
+
this.context.provider = provider;
|
|
186
|
+
return this;
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Add custom field to context
|
|
190
|
+
*/
|
|
191
|
+
with(key, value) {
|
|
192
|
+
this.context[key] = value;
|
|
193
|
+
return this;
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Build the context object
|
|
197
|
+
*/
|
|
198
|
+
build() {
|
|
199
|
+
return { ...this.context };
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
exports.LogContextBuilder = LogContextBuilder;
|
|
203
|
+
/**
|
|
204
|
+
* Create a new log context builder
|
|
205
|
+
* @returns New log context builder instance
|
|
206
|
+
*/
|
|
207
|
+
function createLogContext() {
|
|
208
|
+
return new LogContextBuilder();
|
|
209
|
+
}
|
|
210
|
+
//# sourceMappingURL=logger-types.js.map
|