@revenium/openai 1.0.13 → 1.0.14
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/.env.example +10 -15
- package/CHANGELOG.md +44 -11
- package/CODE_OF_CONDUCT.md +57 -0
- package/CONTRIBUTING.md +38 -0
- package/README.md +104 -216
- package/SECURITY.md +34 -0
- package/dist/cjs/core/client/index.js +14 -0
- package/dist/cjs/core/client/index.js.map +1 -0
- package/dist/cjs/core/client/manager.js +109 -0
- package/dist/cjs/core/client/manager.js.map +1 -0
- package/dist/cjs/core/config/azure-config.js +5 -17
- package/dist/cjs/core/config/azure-config.js.map +1 -1
- package/dist/cjs/core/config/index.js +2 -2
- package/dist/cjs/core/config/index.js.map +1 -1
- package/dist/cjs/core/config/loader.js +34 -14
- package/dist/cjs/core/config/loader.js.map +1 -1
- package/dist/cjs/core/config/manager.js +11 -5
- package/dist/cjs/core/config/manager.js.map +1 -1
- package/dist/cjs/core/config/validator.js +3 -45
- package/dist/cjs/core/config/validator.js.map +1 -1
- package/dist/cjs/core/middleware/index.js +17 -0
- package/dist/cjs/core/middleware/index.js.map +1 -0
- package/dist/cjs/core/middleware/interfaces.js +361 -0
- package/dist/cjs/core/middleware/interfaces.js.map +1 -0
- package/dist/cjs/core/middleware/revenium-client.js +142 -0
- package/dist/cjs/core/middleware/revenium-client.js.map +1 -0
- package/dist/cjs/core/providers/detector.js +45 -23
- package/dist/cjs/core/providers/detector.js.map +1 -1
- package/dist/cjs/core/providers/index.js +2 -1
- package/dist/cjs/core/providers/index.js.map +1 -1
- package/dist/cjs/core/tracking/api-client.js +14 -13
- package/dist/cjs/core/tracking/api-client.js.map +1 -1
- package/dist/cjs/core/tracking/payload-builder.js +15 -25
- package/dist/cjs/core/tracking/payload-builder.js.map +1 -1
- package/dist/cjs/core/tracking/usage-tracker.js +22 -18
- package/dist/cjs/core/tracking/usage-tracker.js.map +1 -1
- package/dist/cjs/index.js +26 -195
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/types/index.js +0 -8
- package/dist/cjs/types/index.js.map +1 -1
- package/dist/cjs/types/openai-augmentation.js +0 -49
- package/dist/cjs/types/openai-augmentation.js.map +1 -1
- package/dist/cjs/utils/constants.js +17 -20
- package/dist/cjs/utils/constants.js.map +1 -1
- package/dist/cjs/utils/error-handler.js +18 -14
- package/dist/cjs/utils/error-handler.js.map +1 -1
- package/dist/cjs/utils/metadata-builder.js +17 -16
- package/dist/cjs/utils/metadata-builder.js.map +1 -1
- package/dist/cjs/utils/provider-detection.js +25 -28
- package/dist/cjs/utils/provider-detection.js.map +1 -1
- package/dist/esm/core/client/index.js +6 -0
- package/dist/esm/core/client/index.js.map +1 -0
- package/dist/esm/core/client/manager.js +102 -0
- package/dist/esm/core/client/manager.js.map +1 -0
- package/dist/esm/core/config/azure-config.js +6 -18
- package/dist/esm/core/config/azure-config.js.map +1 -1
- package/dist/esm/core/config/index.js +5 -4
- package/dist/esm/core/config/index.js.map +1 -1
- package/dist/esm/core/config/loader.js +33 -13
- package/dist/esm/core/config/loader.js.map +1 -1
- package/dist/esm/core/config/manager.js +13 -7
- package/dist/esm/core/config/manager.js.map +1 -1
- package/dist/esm/core/config/validator.js +3 -44
- package/dist/esm/core/config/validator.js.map +1 -1
- package/dist/esm/core/middleware/index.js +8 -0
- package/dist/esm/core/middleware/index.js.map +1 -0
- package/dist/esm/core/middleware/interfaces.js +353 -0
- package/dist/esm/core/middleware/interfaces.js.map +1 -0
- package/dist/esm/core/middleware/revenium-client.js +105 -0
- package/dist/esm/core/middleware/revenium-client.js.map +1 -0
- package/dist/esm/core/providers/detector.js +43 -22
- package/dist/esm/core/providers/detector.js.map +1 -1
- package/dist/esm/core/providers/index.js +2 -2
- package/dist/esm/core/providers/index.js.map +1 -1
- package/dist/esm/core/tracking/api-client.js +13 -12
- package/dist/esm/core/tracking/api-client.js.map +1 -1
- package/dist/esm/core/tracking/payload-builder.js +16 -26
- package/dist/esm/core/tracking/payload-builder.js.map +1 -1
- package/dist/esm/core/tracking/usage-tracker.js +24 -20
- package/dist/esm/core/tracking/usage-tracker.js.map +1 -1
- package/dist/esm/index.js +9 -177
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/types/index.js +2 -10
- package/dist/esm/types/index.js.map +1 -1
- package/dist/esm/types/openai-augmentation.js +0 -49
- package/dist/esm/types/openai-augmentation.js.map +1 -1
- package/dist/esm/utils/constants.js +16 -19
- package/dist/esm/utils/constants.js.map +1 -1
- package/dist/esm/utils/error-handler.js +19 -15
- package/dist/esm/utils/error-handler.js.map +1 -1
- package/dist/esm/utils/metadata-builder.js +17 -16
- package/dist/esm/utils/metadata-builder.js.map +1 -1
- package/dist/esm/utils/provider-detection.js +26 -29
- package/dist/esm/utils/provider-detection.js.map +1 -1
- package/dist/types/core/client/index.d.ts +6 -0
- package/dist/types/core/client/index.d.ts.map +1 -0
- package/dist/types/core/client/manager.d.ts +32 -0
- package/dist/types/core/client/manager.d.ts.map +1 -0
- package/dist/types/core/config/azure-config.d.ts +2 -2
- package/dist/types/core/config/azure-config.d.ts.map +1 -1
- package/dist/types/core/config/index.d.ts +4 -4
- package/dist/types/core/config/index.d.ts.map +1 -1
- package/dist/types/core/config/loader.d.ts +3 -1
- package/dist/types/core/config/loader.d.ts.map +1 -1
- package/dist/types/core/config/manager.d.ts +1 -1
- package/dist/types/core/config/manager.d.ts.map +1 -1
- package/dist/types/core/config/validator.d.ts +1 -12
- package/dist/types/core/config/validator.d.ts.map +1 -1
- package/dist/types/core/middleware/index.d.ts +8 -0
- package/dist/types/core/middleware/index.d.ts.map +1 -0
- package/dist/types/core/middleware/interfaces.d.ts +74 -0
- package/dist/types/core/middleware/interfaces.d.ts.map +1 -0
- package/dist/types/core/middleware/revenium-client.d.ts +58 -0
- package/dist/types/core/middleware/revenium-client.d.ts.map +1 -0
- package/dist/types/core/providers/detector.d.ts +9 -2
- package/dist/types/core/providers/detector.d.ts.map +1 -1
- package/dist/types/core/providers/index.d.ts +2 -2
- package/dist/types/core/providers/index.d.ts.map +1 -1
- package/dist/types/core/tracking/api-client.d.ts +1 -1
- package/dist/types/core/tracking/api-client.d.ts.map +1 -1
- package/dist/types/core/tracking/payload-builder.d.ts +3 -3
- package/dist/types/core/tracking/payload-builder.d.ts.map +1 -1
- package/dist/types/core/tracking/usage-tracker.d.ts +2 -2
- package/dist/types/core/tracking/usage-tracker.d.ts.map +1 -1
- package/dist/types/index.d.ts +11 -135
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/types/function-parameters.d.ts +2 -23
- package/dist/types/types/function-parameters.d.ts.map +1 -1
- package/dist/types/types/index.d.ts +11 -105
- package/dist/types/types/index.d.ts.map +1 -1
- package/dist/types/types/openai-augmentation.d.ts +4 -138
- package/dist/types/types/openai-augmentation.d.ts.map +1 -1
- package/dist/types/utils/constants.d.ts +7 -1
- package/dist/types/utils/constants.d.ts.map +1 -1
- package/dist/types/utils/error-handler.d.ts +2 -2
- package/dist/types/utils/error-handler.d.ts.map +1 -1
- package/dist/types/utils/metadata-builder.d.ts +2 -2
- package/dist/types/utils/metadata-builder.d.ts.map +1 -1
- package/dist/types/utils/provider-detection.d.ts +3 -3
- package/dist/types/utils/provider-detection.d.ts.map +1 -1
- package/examples/README.md +282 -198
- package/examples/azure/basic.ts +62 -0
- package/examples/azure/responses-basic.ts +45 -0
- package/examples/azure/responses-stream.ts +61 -0
- package/examples/azure/stream.ts +56 -0
- package/examples/getting_started.ts +31 -43
- package/examples/openai/basic.ts +45 -0
- package/examples/openai/metadata.ts +67 -0
- package/examples/openai/responses-basic.ts +44 -0
- package/examples/openai/responses-embed.ts +34 -0
- package/examples/openai/responses-streaming.ts +63 -0
- package/examples/openai/streaming.ts +59 -0
- package/package.json +20 -13
- package/dist/cjs/core/wrapper/index.js +0 -15
- package/dist/cjs/core/wrapper/index.js.map +0 -1
- package/dist/cjs/core/wrapper/instance-patcher.js +0 -202
- package/dist/cjs/core/wrapper/instance-patcher.js.map +0 -1
- package/dist/cjs/core/wrapper/request-handler.js +0 -317
- package/dist/cjs/core/wrapper/request-handler.js.map +0 -1
- package/dist/cjs/core/wrapper/stream-wrapper.js +0 -82
- package/dist/cjs/core/wrapper/stream-wrapper.js.map +0 -1
- package/dist/cjs/utils/azure-model-resolver.js +0 -211
- package/dist/cjs/utils/azure-model-resolver.js.map +0 -1
- package/dist/cjs/utils/request-handler-factory.js +0 -185
- package/dist/cjs/utils/request-handler-factory.js.map +0 -1
- package/dist/esm/core/wrapper/index.js +0 -9
- package/dist/esm/core/wrapper/index.js.map +0 -1
- package/dist/esm/core/wrapper/instance-patcher.js +0 -199
- package/dist/esm/core/wrapper/instance-patcher.js.map +0 -1
- package/dist/esm/core/wrapper/request-handler.js +0 -310
- package/dist/esm/core/wrapper/request-handler.js.map +0 -1
- package/dist/esm/core/wrapper/stream-wrapper.js +0 -79
- package/dist/esm/core/wrapper/stream-wrapper.js.map +0 -1
- package/dist/esm/utils/azure-model-resolver.js +0 -204
- package/dist/esm/utils/azure-model-resolver.js.map +0 -1
- package/dist/esm/utils/request-handler-factory.js +0 -146
- package/dist/esm/utils/request-handler-factory.js.map +0 -1
- package/dist/types/core/wrapper/index.d.ts +0 -8
- package/dist/types/core/wrapper/index.d.ts.map +0 -1
- package/dist/types/core/wrapper/instance-patcher.d.ts +0 -33
- package/dist/types/core/wrapper/instance-patcher.d.ts.map +0 -1
- package/dist/types/core/wrapper/request-handler.d.ts +0 -29
- package/dist/types/core/wrapper/request-handler.d.ts.map +0 -1
- package/dist/types/core/wrapper/stream-wrapper.d.ts +0 -13
- package/dist/types/core/wrapper/stream-wrapper.d.ts.map +0 -1
- package/dist/types/utils/azure-model-resolver.d.ts +0 -41
- package/dist/types/utils/azure-model-resolver.d.ts.map +0 -1
- package/dist/types/utils/request-handler-factory.d.ts +0 -81
- package/dist/types/utils/request-handler-factory.d.ts.map +0 -1
- package/examples/azure-basic.ts +0 -206
- package/examples/azure-responses-basic.ts +0 -233
- package/examples/azure-responses-streaming.ts +0 -255
- package/examples/azure-streaming.ts +0 -209
- package/examples/openai-basic.ts +0 -147
- package/examples/openai-function-calling.ts +0 -259
- package/examples/openai-responses-basic.ts +0 -212
- package/examples/openai-responses-streaming.ts +0 -232
- package/examples/openai-streaming.ts +0 -172
- package/examples/openai-vision.ts +0 -289
- package/src/core/config/azure-config.ts +0 -72
- package/src/core/config/index.ts +0 -23
- package/src/core/config/loader.ts +0 -66
- package/src/core/config/manager.ts +0 -95
- package/src/core/config/validator.ts +0 -89
- package/src/core/providers/detector.ts +0 -159
- package/src/core/providers/index.ts +0 -16
- package/src/core/tracking/api-client.ts +0 -78
- package/src/core/tracking/index.ts +0 -21
- package/src/core/tracking/payload-builder.ts +0 -137
- package/src/core/tracking/usage-tracker.ts +0 -189
- package/src/core/wrapper/index.ts +0 -9
- package/src/core/wrapper/instance-patcher.ts +0 -288
- package/src/core/wrapper/request-handler.ts +0 -423
- package/src/core/wrapper/stream-wrapper.ts +0 -100
- package/src/index.ts +0 -360
- package/src/types/function-parameters.ts +0 -251
- package/src/types/index.ts +0 -310
- package/src/types/openai-augmentation.ts +0 -232
- package/src/types/responses-api.ts +0 -308
- package/src/utils/azure-model-resolver.ts +0 -220
- package/src/utils/constants.ts +0 -21
- package/src/utils/error-handler.ts +0 -251
- package/src/utils/metadata-builder.ts +0 -228
- package/src/utils/provider-detection.ts +0 -257
- package/src/utils/request-handler-factory.ts +0 -285
- package/src/utils/stop-reason-mapper.ts +0 -78
- package/src/utils/type-guards.ts +0 -202
- package/src/utils/url-builder.ts +0 -68
package/dist/esm/index.js
CHANGED
|
@@ -2,194 +2,26 @@
|
|
|
2
2
|
* Revenium OpenAI Middleware for TypeScript
|
|
3
3
|
*
|
|
4
4
|
* This middleware tracks OpenAI usage and sends metrics to Revenium.
|
|
5
|
-
* Uses hybrid initialization: auto-initializes on import with graceful fallback to manual init.
|
|
6
|
-
*
|
|
7
|
-
* Environment Variables:
|
|
8
|
-
* REVENIUM_METERING_API_KEY=hak_your_api_key
|
|
9
|
-
* REVENIUM_METERING_BASE_URL=https://api.revenium.ai (optional)
|
|
10
|
-
* OPENAI_API_KEY=sk_your_openai_key
|
|
11
|
-
*
|
|
12
|
-
* Simple Usage (auto-initialization):
|
|
13
|
-
* import { patchOpenAIInstance } from '@revenium/openai';
|
|
14
|
-
* import OpenAI from 'openai';
|
|
15
|
-
*
|
|
16
|
-
* const openai = patchOpenAIInstance(new OpenAI());
|
|
17
|
-
* // Auto-initializes from environment variables
|
|
18
|
-
*
|
|
19
|
-
* Advanced Usage (explicit initialization):
|
|
20
|
-
* import { initializeReveniumFromEnv, patchOpenAIInstance } from '@revenium/openai';
|
|
21
|
-
* import OpenAI from 'openai';
|
|
22
|
-
*
|
|
23
|
-
* const result = initializeReveniumFromEnv();
|
|
24
|
-
* if (!result.success) {
|
|
25
|
-
* throw new Error(result.message);
|
|
26
|
-
* }
|
|
27
|
-
* const openai = patchOpenAIInstance(new OpenAI());
|
|
28
|
-
*
|
|
29
|
-
* const response = await openai.chat.completions.create({
|
|
30
|
-
* model: 'gpt-4',
|
|
31
|
-
* messages: [{ role: 'user', content: 'Hello!' }],
|
|
32
|
-
* usageMetadata: {
|
|
33
|
-
* subscriber: {
|
|
34
|
-
* id: 'user-123',
|
|
35
|
-
* email: 'user@my-org.com'
|
|
36
|
-
* },
|
|
37
|
-
* organizationId: 'my-org',
|
|
38
|
-
* productId: 'my-app'
|
|
39
|
-
* }
|
|
40
|
-
* });
|
|
41
5
|
*/
|
|
42
6
|
// Import type augmentations to extend OpenAI types with usageMetadata
|
|
43
|
-
import
|
|
44
|
-
// Import from new modular structure
|
|
45
|
-
import { setConfig, validateConfig, initializeConfig, getConfig, getLogger, } from './core/config/index.js';
|
|
46
|
-
import { patchOpenAI } from './core/wrapper/index.js';
|
|
47
|
-
// Track initialization state
|
|
48
|
-
let isInitialized = false;
|
|
49
|
-
let autoInitAttempted = false;
|
|
7
|
+
import "./types/openai-augmentation.js";
|
|
50
8
|
// Note: ExtendedChatCompletionCreateParams and ExtendedEmbeddingCreateParams are no longer exported
|
|
51
9
|
// as they have been replaced with seamless TypeScript module augmentation. The usageMetadata field
|
|
52
10
|
// is now natively available on OpenAI's types without requiring additional imports.
|
|
53
|
-
// Export tracking functions
|
|
54
|
-
export { trackUsageAsync, trackEmbeddingsUsageAsync } from './core/tracking/index.js';
|
|
55
|
-
// Export provider detection functions
|
|
56
|
-
export { detectProvider, hasAzureConfig, validateAzureConfig, getProviderMetadata, } from './core/providers/index.js';
|
|
57
|
-
export { getProviderInfo } from './core/wrapper/index.js';
|
|
58
|
-
// Export Azure model resolution functions
|
|
59
|
-
export { resolveAzureModelName, clearModelNameCache, getModelNameCacheStats, batchResolveModelNames, wouldTransformDeploymentName, } from './utils/azure-model-resolver.js';
|
|
60
|
-
// Global logger
|
|
61
|
-
const logger = getLogger();
|
|
62
|
-
/**
|
|
63
|
-
* Initialize Revenium middleware with configuration
|
|
64
|
-
*/
|
|
65
|
-
export function initializeRevenium(config) {
|
|
66
|
-
// Check if already initialized to prevent duplicate initialization
|
|
67
|
-
if (isInitialized) {
|
|
68
|
-
return {
|
|
69
|
-
success: true,
|
|
70
|
-
message: 'Revenium middleware already initialized',
|
|
71
|
-
};
|
|
72
|
-
}
|
|
73
|
-
try {
|
|
74
|
-
// Apply default base URL if not provided
|
|
75
|
-
const configWithDefaults = {
|
|
76
|
-
...config,
|
|
77
|
-
reveniumBaseUrl: config.reveniumBaseUrl || 'https://api.revenium.ai',
|
|
78
|
-
};
|
|
79
|
-
validateConfig(configWithDefaults);
|
|
80
|
-
setConfig(configWithDefaults);
|
|
81
|
-
// Mark as initialized
|
|
82
|
-
isInitialized = true;
|
|
83
|
-
// Patch OpenAI prototype methods
|
|
84
|
-
patchOpenAI();
|
|
85
|
-
return {
|
|
86
|
-
success: true,
|
|
87
|
-
message: 'Revenium middleware initialized successfully',
|
|
88
|
-
};
|
|
89
|
-
}
|
|
90
|
-
catch (error) {
|
|
91
|
-
isInitialized = false;
|
|
92
|
-
return {
|
|
93
|
-
success: false,
|
|
94
|
-
message: error instanceof Error ? error.message : 'Unknown initialization error',
|
|
95
|
-
};
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
/**
|
|
99
|
-
* Configure Revenium middleware manually
|
|
100
|
-
* Alias for initializeRevenium() with a more intuitive name
|
|
101
|
-
*
|
|
102
|
-
* @param config - Revenium configuration object
|
|
103
|
-
* @returns Initialization result with success status and message
|
|
104
|
-
*
|
|
105
|
-
* @example
|
|
106
|
-
* ```typescript
|
|
107
|
-
* import { configure } from '@revenium/openai';
|
|
108
|
-
*
|
|
109
|
-
* configure({
|
|
110
|
-
* reveniumApiKey: 'hak_your_api_key',
|
|
111
|
-
* reveniumBaseUrl: 'https://api.revenium.ai',
|
|
112
|
-
* });
|
|
113
|
-
* ```
|
|
114
|
-
*/
|
|
115
|
-
export function configure(config) {
|
|
116
|
-
return initializeRevenium(config);
|
|
117
|
-
}
|
|
118
|
-
/**
|
|
119
|
-
* Initialize Revenium middleware from environment variables
|
|
120
|
-
*/
|
|
121
|
-
export function initializeReveniumFromEnv() {
|
|
122
|
-
// Check if already initialized to prevent duplicate initialization
|
|
123
|
-
if (isInitialized) {
|
|
124
|
-
return {
|
|
125
|
-
success: true,
|
|
126
|
-
message: 'Revenium middleware already initialized',
|
|
127
|
-
};
|
|
128
|
-
}
|
|
129
|
-
const envSuccess = initializeConfig();
|
|
130
|
-
if (!envSuccess) {
|
|
131
|
-
isInitialized = false;
|
|
132
|
-
return {
|
|
133
|
-
success: false,
|
|
134
|
-
message: 'Failed to load configuration from environment variables. Check REVENIUM_METERING_API_KEY and REVENIUM_METERING_BASE_URL.',
|
|
135
|
-
};
|
|
136
|
-
}
|
|
137
|
-
// Mark as initialized
|
|
138
|
-
isInitialized = true;
|
|
139
|
-
// Patch OpenAI prototype methods
|
|
140
|
-
patchOpenAI();
|
|
141
|
-
return {
|
|
142
|
-
success: true,
|
|
143
|
-
message: 'Revenium middleware initialized from environment',
|
|
144
|
-
};
|
|
145
|
-
}
|
|
146
11
|
/**
|
|
147
|
-
*
|
|
12
|
+
* Main API
|
|
148
13
|
*/
|
|
149
|
-
export {
|
|
14
|
+
export { Initialize, GetClient, IsInitialized, Reset, Configure, } from "./core/client";
|
|
150
15
|
/**
|
|
151
|
-
*
|
|
152
|
-
* Attempts to initialize from environment variables on module load.
|
|
153
|
-
* If it fails, logs a debug message but doesn't throw - allows manual configuration later.
|
|
16
|
+
* Middleware classes
|
|
154
17
|
*/
|
|
155
|
-
|
|
156
|
-
if (autoInitAttempted || isInitialized)
|
|
157
|
-
return;
|
|
158
|
-
autoInitAttempted = true;
|
|
159
|
-
try {
|
|
160
|
-
const result = initializeReveniumFromEnv();
|
|
161
|
-
if (result.success) {
|
|
162
|
-
// Auto-init succeeded - log debug message
|
|
163
|
-
logger.debug('Revenium middleware auto-initialized from environment variables');
|
|
164
|
-
}
|
|
165
|
-
else {
|
|
166
|
-
// Auto-init failed - log debug message but don't throw
|
|
167
|
-
logger.debug('Auto-initialization failed, manual initialization required:', result.message);
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
catch (error) {
|
|
171
|
-
// Unexpected error during auto-init - log but don't throw
|
|
172
|
-
logger.debug('Auto-initialization encountered error:', error instanceof Error ? error.message : String(error));
|
|
173
|
-
}
|
|
174
|
-
}
|
|
18
|
+
export { ReveniumOpenAI, ChatInterface, CompletionsInterface, EmbeddingsInterface, ResponsesInterface, StreamingWrapper, } from "./core/middleware";
|
|
175
19
|
/**
|
|
176
|
-
*
|
|
20
|
+
* Tracking functions (for advanced use cases)
|
|
177
21
|
*/
|
|
178
|
-
export
|
|
179
|
-
return isInitialized;
|
|
180
|
-
}
|
|
22
|
+
export { trackUsageAsync, trackEmbeddingsUsageAsync } from "./core/tracking";
|
|
181
23
|
/**
|
|
182
|
-
*
|
|
24
|
+
* Provider detection functions
|
|
183
25
|
*/
|
|
184
|
-
export
|
|
185
|
-
const config = getConfig();
|
|
186
|
-
return {
|
|
187
|
-
initialized: isInitialized,
|
|
188
|
-
hasConfig: !!config,
|
|
189
|
-
hasApiKey: !!config?.reveniumApiKey,
|
|
190
|
-
autoInitAttempted,
|
|
191
|
-
};
|
|
192
|
-
}
|
|
193
|
-
// Perform auto-initialization when module is imported
|
|
194
|
-
attemptAutoInitialization();
|
|
26
|
+
export { detectProvider, hasAzureConfig, validateAzureConfig, getProviderMetadata, } from "./core/providers";
|
|
195
27
|
//# sourceMappingURL=index.js.map
|
package/dist/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,sEAAsE;AACtE,OAAO,gCAAgC,CAAC;AA8DxC,oGAAoG;AACpG,mGAAmG;AACnG,oFAAoF;AAEpF;;GAEG;AACH,OAAO,EACL,UAAU,EACV,SAAS,EACT,aAAa,EACb,KAAK,EACL,SAAS,GACV,MAAM,eAAe,CAAC;AAEvB;;GAEG;AACH,OAAO,EACL,cAAc,EACd,aAAa,EACb,oBAAoB,EACpB,mBAAmB,EACnB,kBAAkB,EAClB,gBAAgB,GACjB,MAAM,mBAAmB,CAAC;AAE3B;;GAEG;AACH,OAAO,EAAE,eAAe,EAAE,yBAAyB,EAAE,MAAM,iBAAiB,CAAC;AAE7E;;GAEG;AACH,OAAO,EACL,cAAc,EACd,cAAc,EACd,mBAAmB,EACnB,mBAAmB,GACpB,MAAM,kBAAkB,CAAC"}
|
package/dist/esm/types/index.js
CHANGED
|
@@ -5,22 +5,14 @@
|
|
|
5
5
|
* This module exports all core types used throughout the application.
|
|
6
6
|
*/
|
|
7
7
|
// Re-export function parameter types
|
|
8
|
-
export * from
|
|
8
|
+
export * from "./function-parameters.js";
|
|
9
9
|
// Re-export Responses API types
|
|
10
|
-
export * from
|
|
10
|
+
export * from "./responses-api.js";
|
|
11
11
|
/**
|
|
12
12
|
* Supported AI providers
|
|
13
13
|
*
|
|
14
14
|
* Enumeration of AI providers supported by the Revenium middleware.
|
|
15
15
|
* Used for automatic detection, routing, and provider-specific handling.
|
|
16
|
-
*
|
|
17
|
-
* @public
|
|
18
|
-
* @example
|
|
19
|
-
* ```typescript
|
|
20
|
-
* if (providerInfo.provider === Provider.AZURE_OPENAI) {
|
|
21
|
-
* console.log('Using Azure OpenAI');
|
|
22
|
-
* }
|
|
23
|
-
* ```
|
|
24
16
|
*/
|
|
25
17
|
export var Provider;
|
|
26
18
|
(function (Provider) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/types/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,qCAAqC;AACrC,cAAc,0BAA0B,CAAC;AAEzC,gCAAgC;AAChC,cAAc,oBAAoB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/types/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,qCAAqC;AACrC,cAAc,0BAA0B,CAAC;AAEzC,gCAAgC;AAChC,cAAc,oBAAoB,CAAC;AAiFnC;;;;;GAKG;AACH,MAAM,CAAN,IAAY,QAKX;AALD,WAAY,QAAQ;IAClB,0BAA0B;IAC1B,6BAAiB,CAAA;IACjB,2BAA2B;IAC3B,yCAA6B,CAAA;AAC/B,CAAC,EALW,QAAQ,KAAR,QAAQ,QAKnB"}
|
|
@@ -1,54 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* TypeScript module augmentation for OpenAI SDK
|
|
3
|
-
*
|
|
4
|
-
* This file extends OpenAI's existing types to include the usageMetadata field
|
|
5
|
-
* through TypeScript's declaration merging feature. This provides seamless
|
|
6
|
-
* integration with the OpenAI SDK, allowing developers to use usageMetadata
|
|
7
|
-
* directly in OpenAI API calls without type casting or additional imports.
|
|
8
|
-
*
|
|
9
|
-
* The augmentation covers all major OpenAI API endpoints including:
|
|
10
|
-
* - Chat completions (streaming and non-streaming)
|
|
11
|
-
* - Embeddings
|
|
12
|
-
* - Future API endpoints as they become available
|
|
13
|
-
*
|
|
14
|
-
* @fileoverview OpenAI SDK type augmentation for Revenium middleware
|
|
15
|
-
* @author Revenium
|
|
16
|
-
* @since 1.0.0
|
|
17
|
-
*
|
|
18
|
-
* @example Basic usage with chat completions
|
|
19
|
-
* ```typescript
|
|
20
|
-
* import '@revenium/openai';
|
|
21
|
-
* import OpenAI from 'openai';
|
|
22
|
-
*
|
|
23
|
-
* const openai = new OpenAI();
|
|
24
|
-
*
|
|
25
|
-
* const response = await openai.chat.completions.create({
|
|
26
|
-
* model: 'gpt-4o-mini',
|
|
27
|
-
* messages: [{ role: 'user', content: 'Hello!' }],
|
|
28
|
-
* usageMetadata: { // TypeScript recognizes this natively
|
|
29
|
-
* subscriber: {
|
|
30
|
-
* id: 'user-123',
|
|
31
|
-
* email: 'user@my-company.com'
|
|
32
|
-
* },
|
|
33
|
-
* organizationId: 'my-company',
|
|
34
|
-
* productId: 'chat-app',
|
|
35
|
-
* taskType: 'customer-support'
|
|
36
|
-
* }
|
|
37
|
-
* });
|
|
38
|
-
* ```
|
|
39
|
-
*
|
|
40
|
-
* @example Usage with embeddings
|
|
41
|
-
* ```typescript
|
|
42
|
-
* const embedding = await openai.embeddings.create({
|
|
43
|
-
* model: 'text-embedding-ada-002',
|
|
44
|
-
* input: 'Text to embed',
|
|
45
|
-
* usageMetadata: {
|
|
46
|
-
* subscriber: { id: 'user-456' },
|
|
47
|
-
* productId: 'search-engine',
|
|
48
|
-
* taskType: 'document-indexing'
|
|
49
|
-
* }
|
|
50
|
-
* });
|
|
51
|
-
* ```
|
|
52
3
|
*/
|
|
53
4
|
export {};
|
|
54
5
|
//# sourceMappingURL=openai-augmentation.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"openai-augmentation.js","sourceRoot":"","sources":["../../../src/types/openai-augmentation.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"openai-augmentation.js","sourceRoot":"","sources":["../../../src/types/openai-augmentation.ts"],"names":[],"mappings":"AAAA;;GAEG"}
|
|
@@ -1,21 +1,18 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
'gpt-3.5-turbo',
|
|
9
|
-
'gpt-3.5-turbo-instruct',
|
|
10
|
-
'text-embedding-3-large',
|
|
11
|
-
'text-embedding-3-small',
|
|
12
|
-
'text-embedding-ada-002',
|
|
13
|
-
'dall-e-3',
|
|
14
|
-
'dall-e-2',
|
|
15
|
-
'whisper-1',
|
|
16
|
-
'tts-1',
|
|
17
|
-
'tts-1-hd'
|
|
1
|
+
/**
|
|
2
|
+
* Error message patterns for network-related errors
|
|
3
|
+
*/
|
|
4
|
+
export const MESSAGE_PATTERNS_TYPE_NETWORK = [
|
|
5
|
+
"network",
|
|
6
|
+
"timeout",
|
|
7
|
+
"ECONNRESET",
|
|
18
8
|
];
|
|
19
|
-
|
|
20
|
-
|
|
9
|
+
/**
|
|
10
|
+
* Error message patterns for configuration-related errors
|
|
11
|
+
*/
|
|
12
|
+
export const ERROR_MESSAGE_PATTERNS_TYPE_CONFIG = [
|
|
13
|
+
"config",
|
|
14
|
+
"key",
|
|
15
|
+
"unauthorized",
|
|
16
|
+
];
|
|
17
|
+
export const DEFAULT_REVENIUM_BASE_URL = "https://api.revenium.ai";
|
|
21
18
|
//# sourceMappingURL=constants.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../../../src/utils/constants.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../../../src/utils/constants.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,CAAC,MAAM,6BAA6B,GAAG;IAC3C,SAAS;IACT,SAAS;IACT,YAAY;CACb,CAAC;AAEF;;GAEG;AACH,MAAM,CAAC,MAAM,kCAAkC,GAAG;IAChD,QAAQ;IACR,KAAK;IACL,cAAc;CACf,CAAC;AAEF,MAAM,CAAC,MAAM,yBAAyB,GAAG,yBAAyB,CAAC"}
|
|
@@ -4,16 +4,16 @@
|
|
|
4
4
|
* Centralized error handling patterns to eliminate repetitive try/catch blocks
|
|
5
5
|
* and provide consistent error logging and recovery strategies.
|
|
6
6
|
*/
|
|
7
|
-
import { ERROR_MESSAGE_PATTERNS_TYPE_CONFIG, MESSAGE_PATTERNS_TYPE_NETWORK } from
|
|
7
|
+
import { ERROR_MESSAGE_PATTERNS_TYPE_CONFIG, MESSAGE_PATTERNS_TYPE_NETWORK, } from "./constants.js";
|
|
8
8
|
/**
|
|
9
9
|
* Default error handling strategy
|
|
10
10
|
*/
|
|
11
11
|
const DEFAULT_STRATEGY = {
|
|
12
12
|
logError: true,
|
|
13
13
|
rethrow: true,
|
|
14
|
-
messagePrefix:
|
|
14
|
+
messagePrefix: "",
|
|
15
15
|
fallbackValue: undefined,
|
|
16
|
-
transformError: (error) =>
|
|
16
|
+
transformError: (error) => error instanceof Error ? error : new Error(String(error)),
|
|
17
17
|
};
|
|
18
18
|
/**
|
|
19
19
|
* Safe async operation wrapper with comprehensive error handling
|
|
@@ -126,7 +126,7 @@ export async function withRetry(operation, maxRetries = 3, delayMs = 1000, logge
|
|
|
126
126
|
nextRetryIn: delayMs,
|
|
127
127
|
});
|
|
128
128
|
}
|
|
129
|
-
await new Promise(resolve => setTimeout(resolve, delayMs));
|
|
129
|
+
await new Promise((resolve) => setTimeout(resolve, delayMs));
|
|
130
130
|
}
|
|
131
131
|
}
|
|
132
132
|
// eslint-disable-next-line no-throw-literal
|
|
@@ -139,21 +139,21 @@ export class ValidationError extends Error {
|
|
|
139
139
|
constructor(message, context) {
|
|
140
140
|
super(message);
|
|
141
141
|
this.context = context;
|
|
142
|
-
this.name =
|
|
142
|
+
this.name = "ValidationError";
|
|
143
143
|
}
|
|
144
144
|
}
|
|
145
145
|
export class ConfigurationError extends Error {
|
|
146
146
|
constructor(message, context) {
|
|
147
147
|
super(message);
|
|
148
148
|
this.context = context;
|
|
149
|
-
this.name =
|
|
149
|
+
this.name = "ConfigurationError";
|
|
150
150
|
}
|
|
151
151
|
}
|
|
152
152
|
export class NetworkError extends Error {
|
|
153
153
|
constructor(message, context) {
|
|
154
154
|
super(message);
|
|
155
155
|
this.context = context;
|
|
156
|
-
this.name =
|
|
156
|
+
this.name = "NetworkError";
|
|
157
157
|
}
|
|
158
158
|
}
|
|
159
159
|
/**
|
|
@@ -161,22 +161,26 @@ export class NetworkError extends Error {
|
|
|
161
161
|
*/
|
|
162
162
|
export function classifyError(error) {
|
|
163
163
|
if (error instanceof ValidationError) {
|
|
164
|
-
return { type:
|
|
164
|
+
return { type: "validation", message: error.message, isRetryable: false };
|
|
165
165
|
}
|
|
166
166
|
if (error instanceof ConfigurationError) {
|
|
167
|
-
return {
|
|
167
|
+
return {
|
|
168
|
+
type: "configuration",
|
|
169
|
+
message: error.message,
|
|
170
|
+
isRetryable: false,
|
|
171
|
+
};
|
|
168
172
|
}
|
|
169
173
|
if (error instanceof NetworkError) {
|
|
170
|
-
return { type:
|
|
174
|
+
return { type: "network", message: error.message, isRetryable: true };
|
|
171
175
|
}
|
|
172
176
|
const message = error instanceof Error ? error.message : String(error);
|
|
173
177
|
// Classify based on message patterns
|
|
174
|
-
if (MESSAGE_PATTERNS_TYPE_NETWORK.some(pattern => message.includes(pattern))) {
|
|
175
|
-
return { type:
|
|
178
|
+
if (MESSAGE_PATTERNS_TYPE_NETWORK.some((pattern) => message.includes(pattern))) {
|
|
179
|
+
return { type: "network", message, isRetryable: true };
|
|
176
180
|
}
|
|
177
|
-
if (ERROR_MESSAGE_PATTERNS_TYPE_CONFIG.some(pattern => message.includes(pattern))) {
|
|
178
|
-
return { type:
|
|
181
|
+
if (ERROR_MESSAGE_PATTERNS_TYPE_CONFIG.some((pattern) => message.includes(pattern))) {
|
|
182
|
+
return { type: "configuration", message, isRetryable: false };
|
|
179
183
|
}
|
|
180
|
-
return { type:
|
|
184
|
+
return { type: "unknown", message, isRetryable: false };
|
|
181
185
|
}
|
|
182
186
|
//# sourceMappingURL=error-handler.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"error-handler.js","sourceRoot":"","sources":["../../../src/utils/error-handler.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,
|
|
1
|
+
{"version":3,"file":"error-handler.js","sourceRoot":"","sources":["../../../src/utils/error-handler.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,EACL,kCAAkC,EAClC,6BAA6B,GAC9B,MAAM,gBAAgB,CAAC;AAkBxB;;GAEG;AACH,MAAM,gBAAgB,GAAoC;IACxD,QAAQ,EAAE,IAAI;IACd,OAAO,EAAE,IAAI;IACb,aAAa,EAAE,EAAE;IACjB,aAAa,EAAE,SAAS;IACxB,cAAc,EAAE,CAAC,KAAc,EAAE,EAAE,CACjC,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;CAC5D,CAAC;AAEF;;;;;;;;GAQG;AACH,MAAM,CAAC,KAAK,UAAU,kBAAkB,CACtC,SAA2B,EAC3B,OAAe,EACf,WAAkC,EAAE,EACpC,MAAe;IAEf,MAAM,MAAM,GAAG,EAAE,GAAG,gBAAgB,EAAE,GAAG,QAAQ,EAAE,CAAC;IAEpD,IAAI,CAAC;QACH,OAAO,MAAM,SAAS,EAAE,CAAC;IAC3B,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,gBAAgB,GAAG,MAAM,CAAC,cAAc,CAAC,KAAK,CAAC,CAAC;QAEtD,IAAI,MAAM,CAAC,QAAQ,IAAI,MAAM,EAAE,CAAC;YAC9B,MAAM,CAAC,KAAK,CAAC,GAAG,MAAM,CAAC,aAAa,GAAG,OAAO,EAA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IAAI,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC,EAC1E,CAAC;QACD,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,OAAO,EAAE,WAAW,EAAE,IAAI,EAAE,CAAC;IACzD,CAAC;IAED,IACE,kCAAkC,CAAC,IAAI,CAAC,CAAC,OAAO,EAAE,EAAE,CAClD,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAC,CAC1B,EACD,CAAC;QACD,OAAO,EAAE,IAAI,EAAE,eAAe,EAAE,OAAO,EAAE,WAAW,EAAE,KAAK,EAAE,CAAC;IAChE,CAAC;IACD,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,OAAO,EAAE,WAAW,EAAE,KAAK,EAAE,CAAC;AAC1D,CAAC"}
|
|
@@ -10,18 +10,18 @@
|
|
|
10
10
|
* Subscriber object is passed through directly without transformation
|
|
11
11
|
*/
|
|
12
12
|
const METADATA_FIELD_MAP = [
|
|
13
|
-
{ source:
|
|
14
|
-
{ source:
|
|
15
|
-
{ source:
|
|
16
|
-
{ source:
|
|
17
|
-
{ source:
|
|
18
|
-
{ source:
|
|
19
|
-
{ source:
|
|
13
|
+
{ source: "traceId" },
|
|
14
|
+
{ source: "taskType" },
|
|
15
|
+
{ source: "agent" },
|
|
16
|
+
{ source: "organizationId" },
|
|
17
|
+
{ source: "productId" },
|
|
18
|
+
{ source: "subscriber" }, // Pass through nested subscriber object directly
|
|
19
|
+
{ source: "subscriptionId" },
|
|
20
20
|
{
|
|
21
|
-
source:
|
|
21
|
+
source: "responseQualityScore",
|
|
22
22
|
transform: (value) => {
|
|
23
23
|
// Ensure quality score is between 0.0 and 1.0 (API spec requirement)
|
|
24
|
-
if (typeof value ===
|
|
24
|
+
if (typeof value === "number")
|
|
25
25
|
return Math.max(0, Math.min(1, value));
|
|
26
26
|
return value;
|
|
27
27
|
},
|
|
@@ -69,7 +69,7 @@ export function validateMetadata(usageMetadata, requiredFields = []) {
|
|
|
69
69
|
return {
|
|
70
70
|
isValid: false,
|
|
71
71
|
missingFields: requiredFields,
|
|
72
|
-
warnings: [
|
|
72
|
+
warnings: ["No metadata provided"],
|
|
73
73
|
};
|
|
74
74
|
}
|
|
75
75
|
if (usageMetadata) {
|
|
@@ -83,12 +83,13 @@ export function validateMetadata(usageMetadata, requiredFields = []) {
|
|
|
83
83
|
const score = usageMetadata.responseQualityScore;
|
|
84
84
|
// API Spec: https://revenium.readme.io/reference/meter_ai_completion (responseQualityScore)
|
|
85
85
|
// "typically on a 0.0-1.0 scale"
|
|
86
|
-
if (typeof score !==
|
|
87
|
-
warnings.push(
|
|
86
|
+
if (typeof score !== "number" || score < 0 || score > 1) {
|
|
87
|
+
warnings.push("responseQualityScore should be a number between 0.0 and 1.0");
|
|
88
88
|
}
|
|
89
89
|
}
|
|
90
|
-
if (usageMetadata.subscriber?.email &&
|
|
91
|
-
|
|
90
|
+
if (usageMetadata.subscriber?.email &&
|
|
91
|
+
!usageMetadata.subscriber.email.includes("@")) {
|
|
92
|
+
warnings.push("subscriber.email does not appear to be a valid email address");
|
|
92
93
|
}
|
|
93
94
|
}
|
|
94
95
|
return {
|
|
@@ -168,12 +169,12 @@ export function sanitizeMetadataForLogging(usageMetadata) {
|
|
|
168
169
|
}
|
|
169
170
|
if (subscriber.email) {
|
|
170
171
|
// Mask email: handles single-char emails (a@x.com → a***@x.com)
|
|
171
|
-
sanitizedSubscriber.email = subscriber.email.replace(/(.{1,2}).*(@.*)/,
|
|
172
|
+
sanitizedSubscriber.email = subscriber.email.replace(/(.{1,2}).*(@.*)/, "$1***$2");
|
|
172
173
|
}
|
|
173
174
|
if (subscriber.credential) {
|
|
174
175
|
sanitizedSubscriber.credential = {
|
|
175
176
|
name: subscriber.credential.name,
|
|
176
|
-
value:
|
|
177
|
+
value: "[REDACTED]",
|
|
177
178
|
};
|
|
178
179
|
}
|
|
179
180
|
result.subscriber = sanitizedSubscriber;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"metadata-builder.js","sourceRoot":"","sources":["../../../src/utils/metadata-builder.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAkBH;;;;GAIG;AACH,MAAM,kBAAkB,GAA0B;IAChD,EAAE,MAAM,EAAE,SAAS,EAAE;IACrB,EAAE,MAAM,EAAE,UAAU,EAAE;IACtB,EAAE,MAAM,EAAE,OAAO,EAAE;IACnB,EAAE,MAAM,EAAE,gBAAgB,EAAE;IAC5B,EAAE,MAAM,EAAE,WAAW,EAAE;IACvB,EAAE,MAAM,EAAE,YAAY,EAAE,EAAE,iDAAiD;IAC3E,EAAE,MAAM,EAAE,gBAAgB,EAAE;IAC5B;QACE,MAAM,EAAE,sBAAsB;QAC9B,SAAS,EAAE,CAAC,KAAc,EAAE,EAAE;YAC5B,qEAAqE;YACrE,IAAI,OAAO,KAAK,KAAK,QAAQ;gBAAE,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC,CAAC;YACtE,OAAO,KAAK,CAAC;QACf,CAAC;KACF;CACF,CAAC;AAEF;;;;;;;;;GASG;AACH,MAAM,UAAU,mBAAmB,
|
|
1
|
+
{"version":3,"file":"metadata-builder.js","sourceRoot":"","sources":["../../../src/utils/metadata-builder.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAkBH;;;;GAIG;AACH,MAAM,kBAAkB,GAA0B;IAChD,EAAE,MAAM,EAAE,SAAS,EAAE;IACrB,EAAE,MAAM,EAAE,UAAU,EAAE;IACtB,EAAE,MAAM,EAAE,OAAO,EAAE;IACnB,EAAE,MAAM,EAAE,gBAAgB,EAAE;IAC5B,EAAE,MAAM,EAAE,WAAW,EAAE;IACvB,EAAE,MAAM,EAAE,YAAY,EAAE,EAAE,iDAAiD;IAC3E,EAAE,MAAM,EAAE,gBAAgB,EAAE;IAC5B;QACE,MAAM,EAAE,sBAAsB;QAC9B,SAAS,EAAE,CAA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