@revenium/openai 1.0.8
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 +1095 -0
- package/dist/cjs/core/config/azure-config.js +64 -0
- package/dist/cjs/core/config/azure-config.js.map +1 -0
- package/dist/cjs/core/config/index.js +41 -0
- package/dist/cjs/core/config/index.js.map +1 -0
- package/dist/cjs/core/config/loader.js +63 -0
- package/dist/cjs/core/config/loader.js.map +1 -0
- package/dist/cjs/core/config/manager.js +93 -0
- package/dist/cjs/core/config/manager.js.map +1 -0
- package/dist/cjs/core/config/validator.js +73 -0
- package/dist/cjs/core/config/validator.js.map +1 -0
- package/dist/cjs/core/providers/detector.js +140 -0
- package/dist/cjs/core/providers/detector.js.map +1 -0
- package/dist/cjs/core/providers/index.js +18 -0
- package/dist/cjs/core/providers/index.js.map +1 -0
- package/dist/cjs/core/tracking/api-client.js +68 -0
- package/dist/cjs/core/tracking/api-client.js.map +1 -0
- package/dist/cjs/core/tracking/index.js +23 -0
- package/dist/cjs/core/tracking/index.js.map +1 -0
- package/dist/cjs/core/tracking/payload-builder.js +107 -0
- package/dist/cjs/core/tracking/payload-builder.js.map +1 -0
- package/dist/cjs/core/tracking/usage-tracker.js +120 -0
- package/dist/cjs/core/tracking/usage-tracker.js.map +1 -0
- package/dist/cjs/core/wrapper/index.js +15 -0
- package/dist/cjs/core/wrapper/index.js.map +1 -0
- package/dist/cjs/core/wrapper/instance-patcher.js +202 -0
- package/dist/cjs/core/wrapper/instance-patcher.js.map +1 -0
- package/dist/cjs/core/wrapper/request-handler.js +317 -0
- package/dist/cjs/core/wrapper/request-handler.js.map +1 -0
- package/dist/cjs/core/wrapper/stream-wrapper.js +82 -0
- package/dist/cjs/core/wrapper/stream-wrapper.js.map +1 -0
- package/dist/cjs/index.js +195 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/types/function-parameters.js +14 -0
- package/dist/cjs/types/function-parameters.js.map +1 -0
- package/dist/cjs/types/index.js +49 -0
- package/dist/cjs/types/index.js.map +1 -0
- package/dist/cjs/types/openai-augmentation.js +55 -0
- package/dist/cjs/types/openai-augmentation.js.map +1 -0
- package/dist/cjs/types/responses-api.js +30 -0
- package/dist/cjs/types/responses-api.js.map +1 -0
- package/dist/cjs/utils/azure-model-resolver.js +211 -0
- package/dist/cjs/utils/azure-model-resolver.js.map +1 -0
- package/dist/cjs/utils/constants.js +24 -0
- package/dist/cjs/utils/constants.js.map +1 -0
- package/dist/cjs/utils/error-handler.js +194 -0
- package/dist/cjs/utils/error-handler.js.map +1 -0
- package/dist/cjs/utils/metadata-builder.js +184 -0
- package/dist/cjs/utils/metadata-builder.js.map +1 -0
- package/dist/cjs/utils/provider-detection.js +212 -0
- package/dist/cjs/utils/provider-detection.js.map +1 -0
- package/dist/cjs/utils/request-handler-factory.js +185 -0
- package/dist/cjs/utils/request-handler-factory.js.map +1 -0
- package/dist/cjs/utils/stop-reason-mapper.js +70 -0
- package/dist/cjs/utils/stop-reason-mapper.js.map +1 -0
- package/dist/cjs/utils/type-guards.js +175 -0
- package/dist/cjs/utils/type-guards.js.map +1 -0
- package/dist/cjs/utils/url-builder.js +43 -0
- package/dist/cjs/utils/url-builder.js.map +1 -0
- package/dist/esm/core/config/azure-config.js +61 -0
- package/dist/esm/core/config/azure-config.js.map +1 -0
- package/dist/esm/core/config/index.js +13 -0
- package/dist/esm/core/config/index.js.map +1 -0
- package/dist/esm/core/config/loader.js +58 -0
- package/dist/esm/core/config/loader.js.map +1 -0
- package/dist/esm/core/config/manager.js +85 -0
- package/dist/esm/core/config/manager.js.map +1 -0
- package/dist/esm/core/config/validator.js +69 -0
- package/dist/esm/core/config/validator.js.map +1 -0
- package/dist/esm/core/providers/detector.js +134 -0
- package/dist/esm/core/providers/detector.js.map +1 -0
- package/dist/esm/core/providers/index.js +10 -0
- package/dist/esm/core/providers/index.js.map +1 -0
- package/dist/esm/core/tracking/api-client.js +65 -0
- package/dist/esm/core/tracking/api-client.js.map +1 -0
- package/dist/esm/core/tracking/index.js +13 -0
- package/dist/esm/core/tracking/index.js.map +1 -0
- package/dist/esm/core/tracking/payload-builder.js +104 -0
- package/dist/esm/core/tracking/payload-builder.js.map +1 -0
- package/dist/esm/core/tracking/usage-tracker.js +114 -0
- package/dist/esm/core/tracking/usage-tracker.js.map +1 -0
- package/dist/esm/core/wrapper/index.js +9 -0
- package/dist/esm/core/wrapper/index.js.map +1 -0
- package/dist/esm/core/wrapper/instance-patcher.js +199 -0
- package/dist/esm/core/wrapper/instance-patcher.js.map +1 -0
- package/dist/esm/core/wrapper/request-handler.js +310 -0
- package/dist/esm/core/wrapper/request-handler.js.map +1 -0
- package/dist/esm/core/wrapper/stream-wrapper.js +79 -0
- package/dist/esm/core/wrapper/stream-wrapper.js.map +1 -0
- package/dist/esm/index.js +175 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/types/function-parameters.js +13 -0
- package/dist/esm/types/function-parameters.js.map +1 -0
- package/dist/esm/types/index.js +32 -0
- package/dist/esm/types/index.js.map +1 -0
- package/dist/esm/types/openai-augmentation.js +54 -0
- package/dist/esm/types/openai-augmentation.js.map +1 -0
- package/dist/esm/types/responses-api.js +26 -0
- package/dist/esm/types/responses-api.js.map +1 -0
- package/dist/esm/utils/azure-model-resolver.js +204 -0
- package/dist/esm/utils/azure-model-resolver.js.map +1 -0
- package/dist/esm/utils/constants.js +21 -0
- package/dist/esm/utils/constants.js.map +1 -0
- package/dist/esm/utils/error-handler.js +182 -0
- package/dist/esm/utils/error-handler.js.map +1 -0
- package/dist/esm/utils/metadata-builder.js +176 -0
- package/dist/esm/utils/metadata-builder.js.map +1 -0
- package/dist/esm/utils/provider-detection.js +206 -0
- package/dist/esm/utils/provider-detection.js.map +1 -0
- package/dist/esm/utils/request-handler-factory.js +146 -0
- package/dist/esm/utils/request-handler-factory.js.map +1 -0
- package/dist/esm/utils/stop-reason-mapper.js +65 -0
- package/dist/esm/utils/stop-reason-mapper.js.map +1 -0
- package/dist/esm/utils/type-guards.js +158 -0
- package/dist/esm/utils/type-guards.js.map +1 -0
- package/dist/esm/utils/url-builder.js +39 -0
- package/dist/esm/utils/url-builder.js.map +1 -0
- package/dist/types/core/config/azure-config.d.ts +16 -0
- package/dist/types/core/config/azure-config.d.ts.map +1 -0
- package/dist/types/core/config/index.d.ts +11 -0
- package/dist/types/core/config/index.d.ts.map +1 -0
- package/dist/types/core/config/loader.d.ts +20 -0
- package/dist/types/core/config/loader.d.ts.map +1 -0
- package/dist/types/core/config/manager.d.ts +32 -0
- package/dist/types/core/config/manager.d.ts.map +1 -0
- package/dist/types/core/config/validator.d.ts +23 -0
- package/dist/types/core/config/validator.d.ts.map +1 -0
- package/dist/types/core/providers/detector.d.ts +44 -0
- package/dist/types/core/providers/detector.d.ts.map +1 -0
- package/dist/types/core/providers/index.d.ts +9 -0
- package/dist/types/core/providers/index.d.ts.map +1 -0
- package/dist/types/core/tracking/api-client.d.ts +17 -0
- package/dist/types/core/tracking/api-client.d.ts.map +1 -0
- package/dist/types/core/tracking/index.d.ts +11 -0
- package/dist/types/core/tracking/index.d.ts.map +1 -0
- package/dist/types/core/tracking/payload-builder.d.ts +24 -0
- package/dist/types/core/tracking/payload-builder.d.ts.map +1 -0
- package/dist/types/core/tracking/usage-tracker.d.ts +48 -0
- package/dist/types/core/tracking/usage-tracker.d.ts.map +1 -0
- package/dist/types/core/wrapper/index.d.ts +8 -0
- package/dist/types/core/wrapper/index.d.ts.map +1 -0
- package/dist/types/core/wrapper/instance-patcher.d.ts +33 -0
- package/dist/types/core/wrapper/instance-patcher.d.ts.map +1 -0
- package/dist/types/core/wrapper/request-handler.d.ts +29 -0
- package/dist/types/core/wrapper/request-handler.d.ts.map +1 -0
- package/dist/types/core/wrapper/stream-wrapper.d.ts +13 -0
- package/dist/types/core/wrapper/stream-wrapper.d.ts.map +1 -0
- package/dist/types/index.d.ts +179 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/types/function-parameters.d.ts +229 -0
- package/dist/types/types/function-parameters.d.ts.map +1 -0
- package/dist/types/types/index.d.ts +283 -0
- package/dist/types/types/index.d.ts.map +1 -0
- package/dist/types/types/openai-augmentation.d.ts +226 -0
- package/dist/types/types/openai-augmentation.d.ts.map +1 -0
- package/dist/types/types/responses-api.d.ts +247 -0
- package/dist/types/types/responses-api.d.ts.map +1 -0
- package/dist/types/utils/azure-model-resolver.d.ts +41 -0
- package/dist/types/utils/azure-model-resolver.d.ts.map +1 -0
- package/dist/types/utils/constants.d.ts +4 -0
- package/dist/types/utils/constants.d.ts.map +1 -0
- package/dist/types/utils/error-handler.d.ts +95 -0
- package/dist/types/utils/error-handler.d.ts.map +1 -0
- package/dist/types/utils/metadata-builder.d.ts +64 -0
- package/dist/types/utils/metadata-builder.d.ts.map +1 -0
- package/dist/types/utils/provider-detection.d.ts +51 -0
- package/dist/types/utils/provider-detection.d.ts.map +1 -0
- package/dist/types/utils/request-handler-factory.d.ts +81 -0
- package/dist/types/utils/request-handler-factory.d.ts.map +1 -0
- package/dist/types/utils/stop-reason-mapper.d.ts +29 -0
- package/dist/types/utils/stop-reason-mapper.d.ts.map +1 -0
- package/dist/types/utils/type-guards.d.ts +73 -0
- package/dist/types/utils/type-guards.d.ts.map +1 -0
- package/dist/types/utils/url-builder.d.ts +25 -0
- package/dist/types/utils/url-builder.d.ts.map +1 -0
- package/package.json +84 -0
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Request Handler Factory
|
|
3
|
+
*
|
|
4
|
+
* Eliminates dynamic require() calls and provides a clean factory pattern
|
|
5
|
+
* for creating request handlers with proper dependency injection.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Request handler factory class
|
|
9
|
+
*
|
|
10
|
+
* This eliminates the need for dynamic require() calls and provides
|
|
11
|
+
* a clean dependency injection pattern.
|
|
12
|
+
*/
|
|
13
|
+
export class RequestHandlerFactory {
|
|
14
|
+
constructor() {
|
|
15
|
+
this.dependencies = null;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Initialize the factory with dependencies
|
|
19
|
+
*/
|
|
20
|
+
initialize(dependencies) {
|
|
21
|
+
this.dependencies = dependencies;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Get streaming request handler
|
|
25
|
+
*/
|
|
26
|
+
getStreamingHandler() {
|
|
27
|
+
if (!this.dependencies) {
|
|
28
|
+
throw new Error('RequestHandlerFactory not initialized. Call initialize() first.');
|
|
29
|
+
}
|
|
30
|
+
return this.dependencies.handleStreamingRequest;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Get non-streaming request handler
|
|
34
|
+
*/
|
|
35
|
+
getNonStreamingHandler() {
|
|
36
|
+
if (!this.dependencies) {
|
|
37
|
+
throw new Error('RequestHandlerFactory not initialized. Call initialize() first.');
|
|
38
|
+
}
|
|
39
|
+
return this.dependencies.handleNonStreamingRequest;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Get embeddings request handler
|
|
43
|
+
*/
|
|
44
|
+
getEmbeddingsHandler() {
|
|
45
|
+
if (!this.dependencies) {
|
|
46
|
+
throw new Error('RequestHandlerFactory not initialized. Call initialize() first.');
|
|
47
|
+
}
|
|
48
|
+
return this.dependencies.handleEmbeddingsRequest;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Route chat request to appropriate handler
|
|
52
|
+
*/
|
|
53
|
+
routeChatRequest(originalCreate, params, options, usageMetadata, requestStartTime, instance) {
|
|
54
|
+
if (!this.dependencies) {
|
|
55
|
+
throw new Error('RequestHandlerFactory not initialized. Call initialize() first.');
|
|
56
|
+
}
|
|
57
|
+
// Pass clean params (without usageMetadata) to handlers
|
|
58
|
+
// The handlers will receive usageMetadata separately
|
|
59
|
+
if (params.stream) {
|
|
60
|
+
return this.dependencies.handleStreamingRequest(originalCreate, params, options, usageMetadata, requestStartTime, instance);
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
return this.dependencies.handleNonStreamingRequest(originalCreate, params, options, usageMetadata, requestStartTime, instance);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Route embeddings request to appropriate handler
|
|
68
|
+
*/
|
|
69
|
+
routeEmbeddingsRequest(originalCreate, params, options, usageMetadata, requestStartTime, instance) {
|
|
70
|
+
if (!this.dependencies) {
|
|
71
|
+
throw new Error('RequestHandlerFactory not initialized. Call initialize() first.');
|
|
72
|
+
}
|
|
73
|
+
// Pass clean params (without usageMetadata) to handler
|
|
74
|
+
// The handler will receive usageMetadata separately
|
|
75
|
+
return this.dependencies.handleEmbeddingsRequest(originalCreate, params, options, usageMetadata, requestStartTime, instance);
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Route responses request to appropriate handler (new OpenAI Responses API)
|
|
79
|
+
*/
|
|
80
|
+
routeResponsesRequest(originalCreate, params, options, usageMetadata, requestStartTime, instance) {
|
|
81
|
+
if (!this.dependencies) {
|
|
82
|
+
throw new Error('RequestHandlerFactory not initialized. Call initialize() first.');
|
|
83
|
+
}
|
|
84
|
+
// Route to appropriate handler based on streaming
|
|
85
|
+
if (params.stream) {
|
|
86
|
+
return this.dependencies.handleResponsesStreamingRequest(originalCreate, params, options, usageMetadata, requestStartTime, instance);
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
return this.dependencies.handleResponsesNonStreamingRequest(originalCreate, params, options, usageMetadata, requestStartTime, instance);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Check if factory is initialized
|
|
94
|
+
*/
|
|
95
|
+
isInitialized() {
|
|
96
|
+
return this.dependencies !== null;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Global factory instance
|
|
101
|
+
*/
|
|
102
|
+
export const requestHandlerFactory = new RequestHandlerFactory();
|
|
103
|
+
/**
|
|
104
|
+
* Initialize the global factory (called once during module setup)
|
|
105
|
+
* Uses dynamic import to avoid circular dependencies
|
|
106
|
+
*/
|
|
107
|
+
export async function initializeRequestHandlerFactory() {
|
|
108
|
+
// Use dynamic import instead of require for better TypeScript support
|
|
109
|
+
const handlerModule = await import('../core/wrapper/request-handler.js');
|
|
110
|
+
requestHandlerFactory.initialize({
|
|
111
|
+
handleStreamingRequest: handlerModule.handleStreamingRequest,
|
|
112
|
+
handleNonStreamingRequest: handlerModule.handleNonStreamingRequest,
|
|
113
|
+
handleEmbeddingsRequest: handlerModule.handleEmbeddingsRequest,
|
|
114
|
+
handleResponsesStreamingRequest: handlerModule.handleResponsesStreamingRequest,
|
|
115
|
+
handleResponsesNonStreamingRequest: handlerModule.handleResponsesNonStreamingRequest,
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
// Promise cache to prevent race conditions during initialization
|
|
119
|
+
let initializationPromise = null;
|
|
120
|
+
/**
|
|
121
|
+
* Utility function to ensure factory is initialized
|
|
122
|
+
*/
|
|
123
|
+
export async function ensureFactoryInitialized() {
|
|
124
|
+
if (!requestHandlerFactory.isInitialized()) {
|
|
125
|
+
if (!initializationPromise) {
|
|
126
|
+
initializationPromise = initializeRequestHandlerFactoryAsync();
|
|
127
|
+
}
|
|
128
|
+
await initializationPromise;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Async initialization for ES Module compatibility
|
|
133
|
+
* Uses dynamic import for ES Module compatibility
|
|
134
|
+
*/
|
|
135
|
+
async function initializeRequestHandlerFactoryAsync() {
|
|
136
|
+
// Use dynamic import for ES Module compatibility
|
|
137
|
+
const handlerModule = await import('../core/wrapper/request-handler.js');
|
|
138
|
+
requestHandlerFactory.initialize({
|
|
139
|
+
handleStreamingRequest: handlerModule.handleStreamingRequest,
|
|
140
|
+
handleNonStreamingRequest: handlerModule.handleNonStreamingRequest,
|
|
141
|
+
handleEmbeddingsRequest: handlerModule.handleEmbeddingsRequest,
|
|
142
|
+
handleResponsesStreamingRequest: handlerModule.handleResponsesStreamingRequest,
|
|
143
|
+
handleResponsesNonStreamingRequest: handlerModule.handleResponsesNonStreamingRequest,
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
//# sourceMappingURL=request-handler-factory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"request-handler-factory.js","sourceRoot":"","sources":["../../../src/utils/request-handler-factory.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAuEH;;;;;GAKG;AACH,MAAM,OAAO,qBAAqB;IAAlC;QACU,iBAAY,GAAsC,IAAI,CAAC;IAmJjE,CAAC;IAjJC;;OAEG;IACH,UAAU,CAAC,YAAwC;QACjD,IAAI,CAAC,YAAY,GAAG,YAAY,CAAC;IACnC,CAAC;IAED;;OAEG;IACH,mBAAmB;QACjB,IAAI,CAAC,IAAI,CAAC,YAAY,EAAE,CAAC;YACvB,MAAM,IAAI,KAAK,CAAC,iEAAiE,CAAC,CAAC;QACrF,CAAC;QACD,OAAO,IAAI,CAAC,YAAY,CAAC,sBAAsB,CAAC;IAClD,CAAC;IAED;;OAEG;IACH,sBAAsB;QACpB,IAAI,CAAC,IAAI,CAAC,YAAY,EAAE,CAAC;YACvB,MAAM,IAAI,KAAK,CAAC,iEAAiE,CAAC,CAAC;QACrF,CAAC;QACD,OAAO,IAAI,CAAC,YAAY,CAAC,yBAAyB,CAAC;IACrD,CAAC;IAED;;OAEG;IACH,oBAAoB;QAClB,IAAI,CAAC,IAAI,CAAC,YAAY,EAAE,CAAC;YACvB,MAAM,IAAI,KAAK,CAAC,iEAAiE,CAAC,CAAC;QACrF,CAAC;QACD,OAAO,IAAI,CAAC,YAAY,CAAC,uBAAuB,CAAC;IACnD,CAAC;IAED;;OAEG;IACH,gBAAgB,CACd,cAAsC,EACtC,MAAgD,EAChD,OAAyC,EACzC,aAAwC,EACxC,gBAAwB,EACxB,QAA8B;QAE9B,IAAI,CAAC,IAAI,CAAC,YAAY,EAAE,CAAC;YACvB,MAAM,IAAI,KAAK,C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yB;QAClE,uBAAuB,EAAE,aAAa,CAAC,uBAAuB;QAC9D,+BAA+B,EAAE,aAAa,CAAC,+BAA+B;QAC9E,kCAAkC,EAAE,aAAa,CAAC,kCAAkC;KACrF,CAAC,CAAC;AACL,CAAC"}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Stop Reason Mapper Utilities
|
|
3
|
+
*
|
|
4
|
+
* Centralized stop reason mapping logic using lookup tables
|
|
5
|
+
* instead of nested conditionals. Based on the good example
|
|
6
|
+
* from the anthropic-node middleware.
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* Stop reason mapping configuration
|
|
10
|
+
* Maps provider-specific stop reasons to Revenium's standardized set
|
|
11
|
+
*/
|
|
12
|
+
const STOP_REASON_MAP = {
|
|
13
|
+
// OpenAI/Azure OpenAI stop reasons
|
|
14
|
+
stop: 'END',
|
|
15
|
+
function_call: 'END_SEQUENCE',
|
|
16
|
+
tool_calls: 'END_SEQUENCE',
|
|
17
|
+
timeout: 'TIMEOUT',
|
|
18
|
+
length: 'TOKEN_LIMIT',
|
|
19
|
+
max_tokens: 'TOKEN_LIMIT',
|
|
20
|
+
content_filter: 'ERROR',
|
|
21
|
+
error: 'ERROR',
|
|
22
|
+
// Anthropic stop reasons (for consistency across middleware)
|
|
23
|
+
end_turn: 'END',
|
|
24
|
+
stop_sequence: 'END_SEQUENCE',
|
|
25
|
+
tool_use: 'END_SEQUENCE',
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Default stop reason when mapping fails
|
|
29
|
+
*/
|
|
30
|
+
const DEFAULT_STOP_REASON = 'END';
|
|
31
|
+
/**
|
|
32
|
+
* Map provider stop reasons to Revenium stop reasons
|
|
33
|
+
*
|
|
34
|
+
* This replaces the nested if/switch logic with a clean lookup table approach.
|
|
35
|
+
* Based on the good example from the anthropic-node middleware.
|
|
36
|
+
*
|
|
37
|
+
* @param providerStopReason - Stop reason from the AI provider
|
|
38
|
+
* @param logger - Optional logger for warnings about unknown reasons
|
|
39
|
+
* @returns Standardized Revenium stop reason
|
|
40
|
+
*/
|
|
41
|
+
export function mapStopReason(providerStopReason, logger) {
|
|
42
|
+
if (!providerStopReason)
|
|
43
|
+
return DEFAULT_STOP_REASON;
|
|
44
|
+
const normalizedReason = providerStopReason.toLowerCase();
|
|
45
|
+
const mappedReason = STOP_REASON_MAP[normalizedReason];
|
|
46
|
+
if (!mappedReason) {
|
|
47
|
+
// Log warning for unknown stop reasons to help with future mapping
|
|
48
|
+
logger?.warn(`Unknown stop reason: ${providerStopReason}, mapping to ${DEFAULT_STOP_REASON}`);
|
|
49
|
+
return DEFAULT_STOP_REASON;
|
|
50
|
+
}
|
|
51
|
+
return mappedReason;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Get all supported stop reasons for documentation/testing
|
|
55
|
+
*/
|
|
56
|
+
export function getSupportedStopReasons() {
|
|
57
|
+
return Object.keys(STOP_REASON_MAP);
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Check if a stop reason is supported
|
|
61
|
+
*/
|
|
62
|
+
export function isStopReasonSupported(reason) {
|
|
63
|
+
return reason.toLowerCase() in STOP_REASON_MAP;
|
|
64
|
+
}
|
|
65
|
+
//# sourceMappingURL=stop-reason-mapper.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stop-reason-mapper.js","sourceRoot":"","sources":["../../../src/utils/stop-reason-mapper.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH;;;GAGG;AACH,MAAM,eAAe,GAA2B;IAC9C,mCAAmC;IACnC,IAAI,EAAE,KAAK;IACX,aAAa,EAAE,cAAc;IAC7B,UAAU,EAAE,cAAc;IAC1B,OAAO,EAAE,SAAS;IAClB,MAAM,EAAE,aAAa;IACrB,UAAU,EAAE,aAAa;IACzB,cAAc,EAAE,OAAO;IACvB,KAAK,EAAE,OAAO;IAEd,6DAA6D;IAC7D,QAAQ,EAAE,KAAK;IACf,aAAa,EAAE,cAAc;IAC7B,QAAQ,EAAE,cAAc;CACzB,CAAC;AAEF;;GAEG;AACH,MAAM,mBAAmB,GAAG,KAAK,CAAC;AAElC;;;;;;;;;GASG;AACH,MAAM,UAAU,aAAa,CAC3B,kBAA6C,EAC7C,MAA4D;IAE5D,IAAI,CAAC,kBAAkB;QAAE,OAAO,mBAAmB,CAAC;IACpD,MAAM,gBAAgB,GAAG,kBAAkB,CAAC,WAAW,EAAE,CAAC;IAC1D,MAAM,YAAY,GAAG,eAAe,CAAC,gBAAgB,CAAC,CAAC;IAEvD,IAAI,CAAC,YAAY,EAAE,CAAC;QAClB,mEAAmE;QACnE,MAAM,EAAE,IAAI,CAAC,wBAAwB,kBAAkB,gBAAgB,mBAAmB,EAAE,CAAC,CAAC;QAC9F,OAAO,mBAAmB,CAAC;IAC7B,CAAC;IAED,OAAO,YAAY,CAAC;AACtB,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,uBAAuB;IACrC,OAAO,MAAM,CAAC,IAAI,CAAC,eAAe,CAAC,CAAC;AACtC,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,qBAAqB,CAAC,MAAc;IAClD,OAAO,MAAM,CAAC,WAAW,EAAE,IAAI,eAAe,CAAC;AACjD,CAAC"}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type Guards Module
|
|
3
|
+
*
|
|
4
|
+
* Runtime type checking utilities to ensure type safety.
|
|
5
|
+
* These functions validate that objects match expected interfaces.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Type guard for OpenAI chat response
|
|
9
|
+
*/
|
|
10
|
+
export function isOpenAIChatResponse(obj) {
|
|
11
|
+
if (!obj || typeof obj !== 'object')
|
|
12
|
+
return false;
|
|
13
|
+
const response = obj;
|
|
14
|
+
return (typeof response.id === 'string' &&
|
|
15
|
+
typeof response.model === 'string' &&
|
|
16
|
+
typeof response.usage === 'object' &&
|
|
17
|
+
response.usage !== null &&
|
|
18
|
+
typeof response.usage.prompt_tokens === 'number' &&
|
|
19
|
+
typeof response.usage.completion_tokens === 'number' &&
|
|
20
|
+
typeof response.usage.total_tokens === 'number' &&
|
|
21
|
+
Array.isArray(response.choices));
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Type guard for OpenAI embedding response
|
|
25
|
+
*/
|
|
26
|
+
export function isOpenAIEmbeddingResponse(obj) {
|
|
27
|
+
if (!obj || typeof obj !== 'object')
|
|
28
|
+
return false;
|
|
29
|
+
const response = obj;
|
|
30
|
+
return (typeof response.model === 'string' &&
|
|
31
|
+
typeof response.usage === 'object' &&
|
|
32
|
+
response.usage !== null &&
|
|
33
|
+
typeof response.usage.prompt_tokens === 'number' &&
|
|
34
|
+
typeof response.usage.total_tokens === 'number' &&
|
|
35
|
+
Array.isArray(response.data) &&
|
|
36
|
+
typeof response.object === 'string');
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Type guard for OpenAI chat request
|
|
40
|
+
*/
|
|
41
|
+
export function isOpenAIChatRequest(obj) {
|
|
42
|
+
if (!obj || typeof obj !== 'object')
|
|
43
|
+
return false;
|
|
44
|
+
const request = obj;
|
|
45
|
+
return (typeof request.model === 'string' &&
|
|
46
|
+
Array.isArray(request.messages) &&
|
|
47
|
+
request.messages.length > 0 &&
|
|
48
|
+
request.messages.every((msg) => {
|
|
49
|
+
if (!msg || typeof msg !== 'object')
|
|
50
|
+
return false;
|
|
51
|
+
const message = msg;
|
|
52
|
+
return typeof message.role === 'string' && typeof message.content === 'string';
|
|
53
|
+
}));
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Type guard for OpenAI embedding request
|
|
57
|
+
*/
|
|
58
|
+
export function isOpenAIEmbeddingRequest(obj) {
|
|
59
|
+
if (!obj || typeof obj !== 'object')
|
|
60
|
+
return false;
|
|
61
|
+
const request = obj;
|
|
62
|
+
return (typeof request.model === 'string' &&
|
|
63
|
+
(typeof request.input === 'string' || Array.isArray(request.input)));
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Type guard for OpenAI client instance
|
|
67
|
+
*/
|
|
68
|
+
export function isOpenAIClientInstance(obj) {
|
|
69
|
+
if (!obj || typeof obj !== 'object')
|
|
70
|
+
return false;
|
|
71
|
+
const client = obj;
|
|
72
|
+
// Must have at least one of the expected OpenAI client properties
|
|
73
|
+
const hasBaseURL = typeof client.baseURL === 'string' || client.baseURL instanceof URL;
|
|
74
|
+
const hasChat = typeof client.chat === 'object' && client.chat !== null;
|
|
75
|
+
const hasEmbeddings = typeof client.embeddings === 'object' && client.embeddings !== null;
|
|
76
|
+
// A valid OpenAI client should have at least one of these properties
|
|
77
|
+
return hasBaseURL || hasChat || hasEmbeddings;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Type guard for stream chunk
|
|
81
|
+
*/
|
|
82
|
+
export function isStreamChunk(obj) {
|
|
83
|
+
if (!obj || typeof obj !== 'object')
|
|
84
|
+
return false;
|
|
85
|
+
const chunk = obj;
|
|
86
|
+
return typeof chunk.id === 'string' && typeof chunk.model === 'string';
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Type guard for usage object
|
|
90
|
+
*/
|
|
91
|
+
export function hasValidUsage(obj) {
|
|
92
|
+
if (!obj || typeof obj !== 'object')
|
|
93
|
+
return false;
|
|
94
|
+
const response = obj;
|
|
95
|
+
return (typeof response.usage === 'object' &&
|
|
96
|
+
response.usage !== null &&
|
|
97
|
+
typeof response.usage.prompt_tokens === 'number' &&
|
|
98
|
+
typeof response.usage.total_tokens === 'number');
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Type guard for function
|
|
102
|
+
*/
|
|
103
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
104
|
+
export function isFunction(obj) {
|
|
105
|
+
return typeof obj === 'function';
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Type guard for string
|
|
109
|
+
*/
|
|
110
|
+
export function isString(obj) {
|
|
111
|
+
return typeof obj === 'string';
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Type guard for number
|
|
115
|
+
*/
|
|
116
|
+
export function isNumber(obj) {
|
|
117
|
+
return typeof obj === 'number' && !isNaN(obj);
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Type guard for boolean
|
|
121
|
+
*/
|
|
122
|
+
export function isBoolean(obj) {
|
|
123
|
+
return typeof obj === 'boolean';
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Type guard for object
|
|
127
|
+
*/
|
|
128
|
+
export function isObject(obj) {
|
|
129
|
+
return obj !== null && typeof obj === 'object' && !Array.isArray(obj);
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Type guard for array
|
|
133
|
+
*/
|
|
134
|
+
export function isArray(obj) {
|
|
135
|
+
return Array.isArray(obj);
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Safe property access with type checking
|
|
139
|
+
*/
|
|
140
|
+
export function safeGetProperty(obj, property, typeGuard) {
|
|
141
|
+
if (!isObject(obj))
|
|
142
|
+
return undefined;
|
|
143
|
+
const value = obj[property];
|
|
144
|
+
return typeGuard(value) ? value : undefined;
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Safe nested property access
|
|
148
|
+
*/
|
|
149
|
+
export function safeGetNestedProperty(obj, path, typeGuard) {
|
|
150
|
+
let current = obj;
|
|
151
|
+
for (const property of path) {
|
|
152
|
+
if (!isObject(current))
|
|
153
|
+
return undefined;
|
|
154
|
+
current = current[property];
|
|
155
|
+
}
|
|
156
|
+
return typeGuard(current) ? current : undefined;
|
|
157
|
+
}
|
|
158
|
+
//# sourceMappingURL=type-guards.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"type-guards.js","sourceRoot":"","sources":["../../../src/utils/type-guards.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAWH;;GAEG;AACH,MAAM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
|
|
4
|
+
* Centralized URL construction logic to eliminate nested conditionals.
|
|
5
|
+
* Replaces the complex URL building logic from tracking.ts.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Build Revenium API URL with proper path handling
|
|
9
|
+
*
|
|
10
|
+
* This function expects the base URL to NOT include the /v2 suffix for consistency
|
|
11
|
+
* with other Revenium middleware. It automatically appends the correct path.
|
|
12
|
+
*
|
|
13
|
+
* @param baseUrl - The base URL from configuration (e.g., 'https://api.revenium.io/meter')
|
|
14
|
+
* @param endpoint - The API endpoint to append (e.g., '/ai/completions')
|
|
15
|
+
* @returns Complete URL for the API call
|
|
16
|
+
*/
|
|
17
|
+
export function buildReveniumUrl(baseUrl, endpoint) {
|
|
18
|
+
// Normalize the base URL by removing trailing slashes
|
|
19
|
+
const normalizedBase = baseUrl.replace(/\/+$/, '');
|
|
20
|
+
// For consistency with other middleware, expect base URL without /v2
|
|
21
|
+
// and automatically append the correct versioned path
|
|
22
|
+
return `${normalizedBase}/v2${endpoint}`;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Validate URL format
|
|
26
|
+
*
|
|
27
|
+
* @param url - URL to validate
|
|
28
|
+
* @returns true if valid, false otherwise
|
|
29
|
+
*/
|
|
30
|
+
export function isValidUrl(url) {
|
|
31
|
+
try {
|
|
32
|
+
new URL(url);
|
|
33
|
+
return true;
|
|
34
|
+
}
|
|
35
|
+
catch {
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
//# sourceMappingURL=url-builder.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"url-builder.js","sourceRoot":"","sources":["../../../src/utils/url-builder.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH;;;;;;;;;GASG;AACH,MAAM,UAAU,gBAAgB,CAAC,OAAe,EAAE,QAAgB;IAChE,sDAAsD;IACtD,MAAM,cAAc,GAAG,OAAO,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;IAEnD,qEAAqE;IACrE,sDAAsD;IACtD,OAAO,GAAG,cAAc,MAAM,QAAQ,EAAE,CAAC;AAC3C,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,UAAU,CAAC,GAAW;IACpC,IAAI,CAAC;QACH,IAAI,GAAG,CAAC,GAAG,CAAC,CAAC;QACb,OAAO,IAAI,CAAC;IACd,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,KAAK,CAAC;IACf,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Azure Configuration Module
|
|
3
|
+
*
|
|
4
|
+
* Handles Azure-specific configuration logic.
|
|
5
|
+
* Extracted from provider.ts for better separation of concerns.
|
|
6
|
+
*/
|
|
7
|
+
import { AzureConfig } from '../../types/index.js';
|
|
8
|
+
import { OpenAIClientInstance } from '../../types/function-parameters.js';
|
|
9
|
+
/**
|
|
10
|
+
* Gather Azure configuration from client and environment
|
|
11
|
+
*
|
|
12
|
+
* @param client - OpenAI/AzureOpenAI client instance
|
|
13
|
+
* @returns AzureConfig object
|
|
14
|
+
*/
|
|
15
|
+
export declare function gatherAzureConfig(client: OpenAIClientInstance): AzureConfig;
|
|
16
|
+
//# sourceMappingURL=azure-config.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"azure-config.d.ts","sourceRoot":"","sources":["../../../../src/core/config/azure-config.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AACnD,OAAO,EAAE,oBAAoB,EAAE,MAAM,oCAAoC,CAAC;AAM1E;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,oBAAoB,GAAG,WAAW,CAmD3E"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Configuration module - Main exports
|
|
3
|
+
*
|
|
4
|
+
* This module provides a clean interface for configuration management,
|
|
5
|
+
* separating concerns into focused sub-modules.
|
|
6
|
+
*/
|
|
7
|
+
export { loadConfigFromEnv, loadAzureConfigFromEnv, hasAzureConfigInEnv } from './loader.js';
|
|
8
|
+
export { validateConfig, validateAzureConfig } from './validator.js';
|
|
9
|
+
export { getConfig, setConfig, getLogger, setLogger, initializeConfig, defaultLogger, } from './manager.js';
|
|
10
|
+
export * from './azure-config.js';
|
|
11
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/core/config/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,EAAE,iBAAiB,EAAE,sBAAsB,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAC;AAE7F,OAAO,EAAE,cAAc,EAAE,mBAAmB,EAAE,MAAM,gBAAgB,CAAC;AAErE,OAAO,EACL,SAAS,EACT,SAAS,EACT,SAAS,EACT,SAAS,EACT,gBAAgB,EAChB,aAAa,GACd,MAAM,cAAc,CAAC;AAGtB,cAAc,mBAAmB,CAAC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Configuration Loader Module
|
|
3
|
+
*
|
|
4
|
+
* Handles loading configuration from environment variables.
|
|
5
|
+
* Separated from validation and management for single responsibility.
|
|
6
|
+
*/
|
|
7
|
+
import { ReveniumConfig, AzureConfig } from '../../types/index.js';
|
|
8
|
+
/**
|
|
9
|
+
* Load configuration from environment variables
|
|
10
|
+
*/
|
|
11
|
+
export declare function loadConfigFromEnv(): ReveniumConfig | null;
|
|
12
|
+
/**
|
|
13
|
+
* Load Azure configuration from environment variables
|
|
14
|
+
*/
|
|
15
|
+
export declare function loadAzureConfigFromEnv(): AzureConfig | null;
|
|
16
|
+
/**
|
|
17
|
+
* Check if Azure configuration is available in environment
|
|
18
|
+
*/
|
|
19
|
+
export declare function hasAzureConfigInEnv(): boolean;
|
|
20
|
+
//# sourceMappingURL=loader.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"loader.d.ts","sourceRoot":"","sources":["../../../../src/core/config/loader.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,cAAc,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AAOnE;;GAEG;AACH,wBAAgB,iBAAiB,IAAI,cAAc,GAAG,IAAI,CAczD;AAED;;GAEG;AACH,wBAAgB,sBAAsB,IAAI,WAAW,GAAG,IAAI,CAkB3D;AAED;;GAEG;AACH,wBAAgB,mBAAmB,IAAI,OAAO,CAM7C"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Configuration Manager Module
|
|
3
|
+
*
|
|
4
|
+
* Handles global configuration state management and logging.
|
|
5
|
+
* Separated from loading and validation for single responsibility.
|
|
6
|
+
*/
|
|
7
|
+
import { ReveniumConfig, Logger } from '../../types/index.js';
|
|
8
|
+
/**
|
|
9
|
+
* Default console logger implementation
|
|
10
|
+
*/
|
|
11
|
+
export declare const defaultLogger: Logger;
|
|
12
|
+
/**
|
|
13
|
+
* Get the current global configuration
|
|
14
|
+
*/
|
|
15
|
+
export declare function getConfig(): ReveniumConfig | null;
|
|
16
|
+
/**
|
|
17
|
+
* Set the global configuration
|
|
18
|
+
*/
|
|
19
|
+
export declare function setConfig(config: ReveniumConfig): void;
|
|
20
|
+
/**
|
|
21
|
+
* Get the current logger
|
|
22
|
+
*/
|
|
23
|
+
export declare function getLogger(): Logger;
|
|
24
|
+
/**
|
|
25
|
+
* Set a custom logger
|
|
26
|
+
*/
|
|
27
|
+
export declare function setLogger(logger: Logger): void;
|
|
28
|
+
/**
|
|
29
|
+
* Initialize configuration from environment variables
|
|
30
|
+
*/
|
|
31
|
+
export declare function initializeConfig(): boolean;
|
|
32
|
+
//# sourceMappingURL=manager.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"manager.d.ts","sourceRoot":"","sources":["../../../../src/core/config/manager.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,cAAc,EAAE,MAAM,EAAE,MAAM,sBAAsB,CAAC;AAI9D;;GAEG;AACH,eAAO,MAAM,aAAa,EAAE,MAe3B,CAAC;AAQF;;GAEG;AACH,wBAAgB,SAAS,IAAI,cAAc,GAAG,IAAI,CAEjD;AAED;;GAEG;AACH,wBAAgB,SAAS,CAAC,MAAM,EAAE,cAAc,GAAG,IAAI,CAQtD;AAED;;GAEG;AACH,wBAAgB,SAAS,IAAI,MAAM,CAElC;AAED;;GAEG;AACH,wBAAgB,SAAS,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAE9C;AAED;;GAEG;AACH,wBAAgB,gBAAgB,IAAI,OAAO,CAmB1C"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Configuration Validator Module
|
|
3
|
+
*
|
|
4
|
+
* Handles validation of configuration objects.
|
|
5
|
+
* Separated from loading and management for single responsibility.
|
|
6
|
+
*/
|
|
7
|
+
import { ReveniumConfig, AzureConfig } from '../../types/index.js';
|
|
8
|
+
/**
|
|
9
|
+
* Validate Revenium configuration
|
|
10
|
+
*/
|
|
11
|
+
export declare function validateConfig(config: ReveniumConfig): void;
|
|
12
|
+
/**
|
|
13
|
+
* Validate Azure configuration completeness
|
|
14
|
+
*
|
|
15
|
+
* @param config - Azure configuration to validate
|
|
16
|
+
* @returns validation result with missing fields
|
|
17
|
+
*/
|
|
18
|
+
export declare function validateAzureConfig(config: AzureConfig): {
|
|
19
|
+
isValid: boolean;
|
|
20
|
+
missingFields: string[];
|
|
21
|
+
warnings: string[];
|
|
22
|
+
};
|
|
23
|
+
//# sourceMappingURL=validator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validator.d.ts","sourceRoot":"","sources":["../../../../src/core/config/validator.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,cAAc,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AAEnE;;GAEG;AACH,wBAAgB,cAAc,CAAC,MAAM,EAAE,cAAc,GAAG,IAAI,CAuB3D;AAED;;;;;GAKG;AACH,wBAAgB,mBAAmB,CAAC,MAAM,EAAE,WAAW,GAAG;IACxD,OAAO,EAAE,OAAO,CAAC;IACjB,aAAa,EAAE,MAAM,EAAE,CAAC;IACxB,QAAQ,EAAE,MAAM,EAAE,CAAC;CACpB,CAyCA"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Provider Detection Module
|
|
3
|
+
*
|
|
4
|
+
* Handles detection of AI providers (OpenAI vs Azure OpenAI).
|
|
5
|
+
* Extracted from provider.ts for better organization.
|
|
6
|
+
*/
|
|
7
|
+
import { ProviderInfo, AzureConfig } from '../../types/index.js';
|
|
8
|
+
import { OpenAIClientInstance } from '../../types/function-parameters.js';
|
|
9
|
+
/**
|
|
10
|
+
* Detect Azure OpenAI provider from client instance
|
|
11
|
+
*
|
|
12
|
+
* @param client - OpenAI or AzureOpenAI client instance
|
|
13
|
+
* @returns ProviderInfo with detection results
|
|
14
|
+
*/
|
|
15
|
+
export declare function detectProvider(client: OpenAIClientInstance): ProviderInfo;
|
|
16
|
+
/**
|
|
17
|
+
* Check if the current environment has Azure configuration
|
|
18
|
+
* This is a lightweight check for lazy loading decisions
|
|
19
|
+
*
|
|
20
|
+
* @returns boolean indicating if Azure config is present
|
|
21
|
+
*/
|
|
22
|
+
export declare function hasAzureConfig(): boolean;
|
|
23
|
+
/**
|
|
24
|
+
* Validate Azure configuration completeness
|
|
25
|
+
*
|
|
26
|
+
* @param config - Azure configuration to validate
|
|
27
|
+
* @returns validation result with missing fields
|
|
28
|
+
*/
|
|
29
|
+
export declare function validateAzureConfig(config: AzureConfig): {
|
|
30
|
+
isValid: boolean;
|
|
31
|
+
missingFields: string[];
|
|
32
|
+
warnings: string[];
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* Get provider metadata for Revenium payload
|
|
36
|
+
*
|
|
37
|
+
* @param providerInfo - Provider detection result
|
|
38
|
+
* @returns metadata object for Revenium
|
|
39
|
+
*/
|
|
40
|
+
export declare function getProviderMetadata(providerInfo: ProviderInfo): {
|
|
41
|
+
provider: string;
|
|
42
|
+
modelSource: string;
|
|
43
|
+
};
|
|
44
|
+
//# sourceMappingURL=detector.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"detector.d.ts","sourceRoot":"","sources":["../../../../src/core/providers/detector.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAY,YAAY,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AAC3E,OAAO,EAAE,oBAAoB,EAAE,MAAM,oCAAoC,CAAC;AAQ1E;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,MAAM,EAAE,oBAAoB,GAAG,YAAY,CA+CzE;AAED;;;;;GAKG;AACH,wBAAgB,cAAc,IAAI,OAAO,CAMxC;AAED;;;;;GAKG;AACH,wBAAgB,mBAAmB,CAAC,MAAM,EAAE,WAAW,GAAG;IACxD,OAAO,EAAE,OAAO,CAAC;IACjB,aAAa,EAAE,MAAM,EAAE,CAAC;IACxB,QAAQ,EAAE,MAAM,EAAE,CAAC;CACpB,CAyCA;AAED;;;;;GAKG;AACH,wBAAgB,mBAAmB,CAAC,YAAY,EAAE,YAAY,GAAG;IAC/D,QAAQ,EAAE,MAAM,CAAC;IACjB,WAAW,EAAE,MAAM,CAAC;CACrB,CAWA"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Providers module - Main exports
|
|
3
|
+
*
|
|
4
|
+
* This module provides a clean interface for provider detection and management,
|
|
5
|
+
* separating concerns into focused sub-modules.
|
|
6
|
+
*/
|
|
7
|
+
export { detectProvider, hasAzureConfig, validateAzureConfig, getProviderMetadata, } from './detector.js';
|
|
8
|
+
export { gatherAzureConfig } from '../config/azure-config.js';
|
|
9
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/core/providers/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,EACL,cAAc,EACd,cAAc,EACd,mBAAmB,EACnB,mBAAmB,GACpB,MAAM,eAAe,CAAC;AAEvB,OAAO,EAAE,iBAAiB,EAAE,MAAM,2BAA2B,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Revenium API Client Module
|
|
3
|
+
*
|
|
4
|
+
* Handles HTTP communication with the Revenium API.
|
|
5
|
+
* Extracted from tracking.ts for single responsibility.
|
|
6
|
+
*/
|
|
7
|
+
import { ReveniumPayload } from '../../types/index.js';
|
|
8
|
+
/**
|
|
9
|
+
* Send payload to Revenium API
|
|
10
|
+
*
|
|
11
|
+
* This is the shared HTTP function that eliminates all duplication
|
|
12
|
+
* between chat completions and embeddings tracking.
|
|
13
|
+
*
|
|
14
|
+
* @param payload - The payload to send to Revenium
|
|
15
|
+
*/
|
|
16
|
+
export declare function sendToRevenium(payload: ReveniumPayload): Promise<void>;
|
|
17
|
+
//# sourceMappingURL=api-client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"api-client.d.ts","sourceRoot":"","sources":["../../../../src/core/tracking/api-client.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AAOvD;;;;;;;GAOG;AACH,wBAAsB,cAAc,CAAC,OAAO,EAAE,eAAe,GAAG,OAAO,CAAC,IAAI,CAAC,CAuD5E"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tracking module - Main exports
|
|
3
|
+
*
|
|
4
|
+
* This module provides a clean interface for usage tracking,
|
|
5
|
+
* separating concerns into focused sub-modules.
|
|
6
|
+
*/
|
|
7
|
+
export { sendToRevenium } from './api-client.js';
|
|
8
|
+
export { buildPayload } from './payload-builder.js';
|
|
9
|
+
export { sendReveniumMetrics, sendReveniumEmbeddingsMetrics, trackUsageAsync, trackEmbeddingsUsageAsync, } from './usage-tracker.js';
|
|
10
|
+
export { mapStopReason } from '../../utils/stop-reason-mapper.js';
|
|
11
|
+
//# sourceMappingURL=index.d.ts.map
|