@revenium/openai 1.0.10 → 1.0.12
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 +20 -0
- package/CHANGELOG.md +52 -0
- package/LICENSE +21 -21
- package/README.md +682 -1152
- package/dist/cjs/core/config/loader.js +1 -1
- package/dist/cjs/core/config/loader.js.map +1 -1
- package/dist/cjs/core/tracking/api-client.js +1 -1
- package/dist/cjs/core/tracking/api-client.js.map +1 -1
- package/dist/cjs/index.js +4 -4
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/types/openai-augmentation.js +1 -1
- package/dist/cjs/utils/url-builder.js +32 -7
- package/dist/cjs/utils/url-builder.js.map +1 -1
- package/dist/esm/core/config/loader.js +1 -1
- package/dist/esm/core/config/loader.js.map +1 -1
- package/dist/esm/core/tracking/api-client.js +1 -1
- package/dist/esm/core/tracking/api-client.js.map +1 -1
- package/dist/esm/index.js +4 -4
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/types/openai-augmentation.js +1 -1
- package/dist/esm/utils/url-builder.js +32 -7
- package/dist/esm/utils/url-builder.js.map +1 -1
- package/dist/types/index.d.ts +4 -4
- package/dist/types/types/index.d.ts +2 -2
- package/dist/types/types/index.d.ts.map +1 -1
- package/dist/types/types/openai-augmentation.d.ts +1 -1
- package/dist/types/utils/url-builder.d.ts +11 -3
- package/dist/types/utils/url-builder.d.ts.map +1 -1
- package/examples/README.md +357 -0
- package/examples/azure-basic.ts +206 -0
- package/examples/azure-responses-basic.ts +233 -0
- package/examples/azure-responses-streaming.ts +255 -0
- package/examples/azure-streaming.ts +209 -0
- package/examples/getting_started.ts +54 -0
- package/examples/openai-basic.ts +147 -0
- package/examples/openai-function-calling.ts +259 -0
- package/examples/openai-responses-basic.ts +212 -0
- package/examples/openai-responses-streaming.ts +232 -0
- package/examples/openai-streaming.ts +172 -0
- package/examples/openai-vision.ts +289 -0
- package/package.json +81 -84
- package/src/core/config/azure-config.ts +72 -0
- package/src/core/config/index.ts +23 -0
- package/src/core/config/loader.ts +66 -0
- package/src/core/config/manager.ts +94 -0
- package/src/core/config/validator.ts +89 -0
- package/src/core/providers/detector.ts +159 -0
- package/src/core/providers/index.ts +16 -0
- package/src/core/tracking/api-client.ts +78 -0
- package/src/core/tracking/index.ts +21 -0
- package/src/core/tracking/payload-builder.ts +132 -0
- package/src/core/tracking/usage-tracker.ts +189 -0
- package/src/core/wrapper/index.ts +9 -0
- package/src/core/wrapper/instance-patcher.ts +288 -0
- package/src/core/wrapper/request-handler.ts +423 -0
- package/src/core/wrapper/stream-wrapper.ts +100 -0
- package/src/index.ts +336 -0
- package/src/types/function-parameters.ts +251 -0
- package/src/types/index.ts +313 -0
- package/src/types/openai-augmentation.ts +233 -0
- package/src/types/responses-api.ts +308 -0
- package/src/utils/azure-model-resolver.ts +220 -0
- package/src/utils/constants.ts +21 -0
- package/src/utils/error-handler.ts +251 -0
- package/src/utils/metadata-builder.ts +219 -0
- package/src/utils/provider-detection.ts +257 -0
- package/src/utils/request-handler-factory.ts +285 -0
- package/src/utils/stop-reason-mapper.ts +74 -0
- package/src/utils/type-guards.ts +202 -0
- package/src/utils/url-builder.ts +68 -0
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Core Types Module
|
|
3
|
+
*
|
|
4
|
+
* Central type definitions for the Revenium OpenAI middleware.
|
|
5
|
+
* This module exports all core types used throughout the application.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
// Re-export function parameter types
|
|
9
|
+
export * from './function-parameters.js';
|
|
10
|
+
|
|
11
|
+
// Re-export Responses API types
|
|
12
|
+
export * from './responses-api.js';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Credential information for subscriber authentication
|
|
16
|
+
*
|
|
17
|
+
* Represents authentication credentials that can be attached to subscriber information
|
|
18
|
+
* for enhanced security and tracking capabilities.
|
|
19
|
+
*
|
|
20
|
+
* @public
|
|
21
|
+
* @example
|
|
22
|
+
* ```typescript
|
|
23
|
+
* const credential: Credential = {
|
|
24
|
+
* name: 'api_token',
|
|
25
|
+
* value: 'user_token_abc123'
|
|
26
|
+
* };
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export interface Credential {
|
|
30
|
+
/** The name/type of the credential (e.g., 'api_token', 'session_id') */
|
|
31
|
+
name: string;
|
|
32
|
+
/** The credential value (should be handled securely) */
|
|
33
|
+
value: string;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Subscriber information for Revenium API
|
|
38
|
+
*
|
|
39
|
+
* Represents end-user information for tracking and billing purposes.
|
|
40
|
+
* All fields are optional to provide maximum flexibility in implementation.
|
|
41
|
+
*
|
|
42
|
+
* @public
|
|
43
|
+
* @example
|
|
44
|
+
* ```typescript
|
|
45
|
+
* const subscriber: Subscriber = {
|
|
46
|
+
* id: 'user-12345',
|
|
47
|
+
* email: 'john.doe@company.com',
|
|
48
|
+
* credential: {
|
|
49
|
+
* name: 'session_token',
|
|
50
|
+
* value: 'abc123xyz'
|
|
51
|
+
* }
|
|
52
|
+
* };
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
export interface Subscriber {
|
|
56
|
+
/** Unique identifier for the subscriber/user */
|
|
57
|
+
id?: string;
|
|
58
|
+
/** Email address of the subscriber */
|
|
59
|
+
email?: string;
|
|
60
|
+
/** Optional authentication credential for the subscriber */
|
|
61
|
+
credential?: Credential;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Usage metadata interface for tracking additional context
|
|
66
|
+
*
|
|
67
|
+
* Comprehensive metadata structure that enables detailed tracking of AI API usage
|
|
68
|
+
* for analytics, billing, and business intelligence purposes. All fields are optional
|
|
69
|
+
* to provide maximum flexibility while maintaining consistency across language implementations.
|
|
70
|
+
*
|
|
71
|
+
* @public
|
|
72
|
+
* @example
|
|
73
|
+
* ```typescript
|
|
74
|
+
* const metadata: UsageMetadata = {
|
|
75
|
+
* subscriber: {
|
|
76
|
+
* id: 'user-123',
|
|
77
|
+
* email: 'user@company.com'
|
|
78
|
+
* },
|
|
79
|
+
* organizationId: 'org-456',
|
|
80
|
+
* productId: 'chat-assistant',
|
|
81
|
+
* taskType: 'customer-support',
|
|
82
|
+
* traceId: 'trace-789',
|
|
83
|
+
* responseQualityScore: 0.95,
|
|
84
|
+
* agent: 'support-bot-v2'
|
|
85
|
+
* };
|
|
86
|
+
* ```
|
|
87
|
+
*/
|
|
88
|
+
export interface UsageMetadata {
|
|
89
|
+
/** User identification information (nested structure for detailed tracking) */
|
|
90
|
+
subscriber?: Subscriber;
|
|
91
|
+
|
|
92
|
+
/** Organization or company identifier for multi-tenant applications */
|
|
93
|
+
organizationId?: string;
|
|
94
|
+
/** Product or application identifier for usage segmentation */
|
|
95
|
+
productId?: string;
|
|
96
|
+
/** Subscription identifier for billing and plan management */
|
|
97
|
+
subscriptionId?: string;
|
|
98
|
+
|
|
99
|
+
/** Task type classification (e.g., 'chat', 'summarization', 'translation') */
|
|
100
|
+
taskType?: string;
|
|
101
|
+
/** Unique task identifier for request correlation */
|
|
102
|
+
taskId?: string;
|
|
103
|
+
/** Distributed tracing identifier for request tracking across services */
|
|
104
|
+
traceId?: string;
|
|
105
|
+
|
|
106
|
+
/** Quality score for response evaluation (0.0 to 1.0) */
|
|
107
|
+
responseQualityScore?: number;
|
|
108
|
+
|
|
109
|
+
/** Agent or model variant identifier for A/B testing and performance tracking */
|
|
110
|
+
agent?: string;
|
|
111
|
+
|
|
112
|
+
/** Allow additional custom fields for extensibility */
|
|
113
|
+
[key: string]: unknown;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Provider information for tracking API source
|
|
118
|
+
*
|
|
119
|
+
* Detailed information about the detected AI provider, including configuration
|
|
120
|
+
* details and Azure-specific settings when applicable. Used internally for
|
|
121
|
+
* provider-specific handling and metrics collection.
|
|
122
|
+
*
|
|
123
|
+
* @public
|
|
124
|
+
* @example
|
|
125
|
+
* ```typescript
|
|
126
|
+
* const providerInfo: ProviderInfo = {
|
|
127
|
+
* provider: Provider.AZURE_OPENAI,
|
|
128
|
+
* isAzure: true,
|
|
129
|
+
* endpoint: 'https://my-resource.openai.azure.com',
|
|
130
|
+
* apiVersion: '2024-02-01',
|
|
131
|
+
* deployment: 'gpt-4-turbo'
|
|
132
|
+
* };
|
|
133
|
+
* ```
|
|
134
|
+
*/
|
|
135
|
+
export interface ProviderInfo {
|
|
136
|
+
/** The detected AI provider type */
|
|
137
|
+
provider: Provider;
|
|
138
|
+
/** Whether this is an Azure OpenAI instance */
|
|
139
|
+
isAzure: boolean;
|
|
140
|
+
/** API endpoint URL (for Azure OpenAI) */
|
|
141
|
+
endpoint?: string;
|
|
142
|
+
/** API version (for Azure OpenAI) */
|
|
143
|
+
apiVersion?: string;
|
|
144
|
+
/** Deployment name (for Azure OpenAI) */
|
|
145
|
+
deployment?: string;
|
|
146
|
+
/** Complete Azure configuration when available */
|
|
147
|
+
azureConfig?: AzureConfig;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Supported AI providers
|
|
152
|
+
*
|
|
153
|
+
* Enumeration of AI providers supported by the Revenium middleware.
|
|
154
|
+
* Used for automatic detection, routing, and provider-specific handling.
|
|
155
|
+
*
|
|
156
|
+
* @public
|
|
157
|
+
* @example
|
|
158
|
+
* ```typescript
|
|
159
|
+
* if (providerInfo.provider === Provider.AZURE_OPENAI) {
|
|
160
|
+
* console.log('Using Azure OpenAI');
|
|
161
|
+
* }
|
|
162
|
+
* ```
|
|
163
|
+
*/
|
|
164
|
+
export enum Provider {
|
|
165
|
+
/** Standard OpenAI API */
|
|
166
|
+
OPENAI = 'OPENAI',
|
|
167
|
+
/** Azure OpenAI Service */
|
|
168
|
+
AZURE_OPENAI = 'AZURE_OPENAI',
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Azure OpenAI configuration
|
|
173
|
+
*
|
|
174
|
+
* Configuration interface for Azure OpenAI Service integration.
|
|
175
|
+
* Provides all necessary settings for connecting to Azure OpenAI endpoints
|
|
176
|
+
* with proper authentication and resource identification.
|
|
177
|
+
*
|
|
178
|
+
* @public
|
|
179
|
+
* @example
|
|
180
|
+
* ```typescript
|
|
181
|
+
* const azureConfig: AzureConfig = {
|
|
182
|
+
* endpoint: 'https://my-resource.openai.azure.com',
|
|
183
|
+
* apiKey: process.env.AZURE_OPENAI_API_KEY,
|
|
184
|
+
* apiVersion: '2024-02-01',
|
|
185
|
+
* deployment: 'gpt-4-turbo',
|
|
186
|
+
* tenantId: 'your-tenant-id'
|
|
187
|
+
* };
|
|
188
|
+
* ```
|
|
189
|
+
*/
|
|
190
|
+
export interface AzureConfig {
|
|
191
|
+
/** Azure OpenAI endpoint URL */
|
|
192
|
+
endpoint?: string;
|
|
193
|
+
/** Azure OpenAI API key */
|
|
194
|
+
apiKey?: string;
|
|
195
|
+
/** Azure OpenAI API version */
|
|
196
|
+
apiVersion?: string;
|
|
197
|
+
/** Azure OpenAI deployment name */
|
|
198
|
+
deployment?: string;
|
|
199
|
+
/** Azure tenant ID for authentication */
|
|
200
|
+
tenantId?: string;
|
|
201
|
+
/** Azure resource group name */
|
|
202
|
+
resourceGroup?: string;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Revenium configuration interface
|
|
207
|
+
*
|
|
208
|
+
* Main configuration interface for initializing the Revenium middleware.
|
|
209
|
+
* Defines all required and optional settings for connecting to Revenium's
|
|
210
|
+
* metering API and configuring middleware behavior.
|
|
211
|
+
*
|
|
212
|
+
* @public
|
|
213
|
+
* @example
|
|
214
|
+
* ```typescript
|
|
215
|
+
* const config: ReveniumConfig = {
|
|
216
|
+
* reveniumApiKey: 'hak_your_revenium_api_key',
|
|
217
|
+
* reveniumBaseUrl: 'https://api.revenium.io',
|
|
218
|
+
* debug: true,
|
|
219
|
+
* openaiApiKey: process.env.OPENAI_API_KEY
|
|
220
|
+
* };
|
|
221
|
+
* ```
|
|
222
|
+
*/
|
|
223
|
+
export interface ReveniumConfig {
|
|
224
|
+
/** Revenium API key for authentication (required) */
|
|
225
|
+
reveniumApiKey: string;
|
|
226
|
+
/** Revenium API base URL (optional, defaults to https://api.revenium.io) */
|
|
227
|
+
reveniumBaseUrl?: string;
|
|
228
|
+
/** Enable debug logging (optional, defaults to false) */
|
|
229
|
+
debug?: boolean;
|
|
230
|
+
/** Azure OpenAI configuration (optional, for Azure OpenAI usage) */
|
|
231
|
+
azure?: AzureConfig;
|
|
232
|
+
/** OpenAI API key (optional, can be set via environment variable) */
|
|
233
|
+
openaiApiKey?: string;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* Logger interface for consistent logging
|
|
238
|
+
*
|
|
239
|
+
* Standardized logging interface that allows custom logger integration
|
|
240
|
+
* while maintaining consistent log levels and metadata support throughout
|
|
241
|
+
* the middleware. Supports both structured and string metadata.
|
|
242
|
+
*
|
|
243
|
+
* @public
|
|
244
|
+
* @example
|
|
245
|
+
* ```typescript
|
|
246
|
+
* const customLogger: Logger = {
|
|
247
|
+
* debug: (msg, meta) => console.debug(`[DEBUG] ${msg}`, meta),
|
|
248
|
+
* info: (msg, meta) => console.info(`[INFO] ${msg}`, meta),
|
|
249
|
+
* warn: (msg, meta) => console.warn(`[WARN] ${msg}`, meta),
|
|
250
|
+
* error: (msg, meta) => console.error(`[ERROR] ${msg}`, meta)
|
|
251
|
+
* };
|
|
252
|
+
* ```
|
|
253
|
+
*/
|
|
254
|
+
export interface Logger {
|
|
255
|
+
/** Log debug-level messages with optional metadata */
|
|
256
|
+
debug(message: string, meta?: Record<string, unknown> | string): void;
|
|
257
|
+
/** Log info-level messages with optional metadata */
|
|
258
|
+
info(message: string, meta?: Record<string, unknown> | string): void;
|
|
259
|
+
/** Log warning-level messages with optional metadata */
|
|
260
|
+
warn(message: string, meta?: Record<string, unknown> | string): void;
|
|
261
|
+
/** Log error-level messages with optional metadata */
|
|
262
|
+
error(message: string, meta?: Record<string, unknown> | string | unknown): void;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* Revenium API payload structure
|
|
267
|
+
*/
|
|
268
|
+
export interface ReveniumPayload {
|
|
269
|
+
// Core identification
|
|
270
|
+
transactionId: string;
|
|
271
|
+
operationType: 'CHAT' | 'EMBED';
|
|
272
|
+
costType: 'AI';
|
|
273
|
+
|
|
274
|
+
// Model and provider info
|
|
275
|
+
model: string;
|
|
276
|
+
provider: string;
|
|
277
|
+
modelSource?: string;
|
|
278
|
+
middlewareSource: string;
|
|
279
|
+
|
|
280
|
+
// Timing information
|
|
281
|
+
requestTime: string;
|
|
282
|
+
responseTime: string;
|
|
283
|
+
requestDuration: number;
|
|
284
|
+
completionStartTime: string;
|
|
285
|
+
|
|
286
|
+
// Token counts
|
|
287
|
+
inputTokenCount: number;
|
|
288
|
+
outputTokenCount: number;
|
|
289
|
+
totalTokenCount: number;
|
|
290
|
+
reasoningTokenCount: number;
|
|
291
|
+
cacheCreationTokenCount: number;
|
|
292
|
+
cacheReadTokenCount: number;
|
|
293
|
+
|
|
294
|
+
// Chat-specific fields
|
|
295
|
+
stopReason: string;
|
|
296
|
+
isStreamed: boolean;
|
|
297
|
+
timeToFirstToken?: number;
|
|
298
|
+
|
|
299
|
+
// Cost information (calculated by backend)
|
|
300
|
+
inputTokenCost?: number;
|
|
301
|
+
outputTokenCost?: number;
|
|
302
|
+
totalCost?: number;
|
|
303
|
+
|
|
304
|
+
// Metadata fields (optional)
|
|
305
|
+
traceId?: string;
|
|
306
|
+
taskType?: string;
|
|
307
|
+
agent?: string;
|
|
308
|
+
organizationId?: string;
|
|
309
|
+
productId?: string;
|
|
310
|
+
subscriber?: Subscriber;
|
|
311
|
+
subscriptionId?: string;
|
|
312
|
+
responseQualityScore?: number;
|
|
313
|
+
}
|
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
/**
|
|
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
|
+
*/
|
|
53
|
+
|
|
54
|
+
import { UsageMetadata } from './index.js';
|
|
55
|
+
|
|
56
|
+
// Export something to make this a module (required for TypeScript compilation)
|
|
57
|
+
export {};
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* OpenAI Chat Completions API augmentation
|
|
61
|
+
*
|
|
62
|
+
* Extends the OpenAI chat completions interfaces to include usageMetadata
|
|
63
|
+
* for all completion types (base, streaming, and non-streaming).
|
|
64
|
+
*/
|
|
65
|
+
declare module 'openai/resources/chat/completions/completions' {
|
|
66
|
+
interface ChatCompletionCreateParamsBase {
|
|
67
|
+
/**
|
|
68
|
+
* Optional metadata for enhanced tracking and analytics.
|
|
69
|
+
*
|
|
70
|
+
* Provides rich context for business analytics, user tracking, and billing purposes.
|
|
71
|
+
* All fields are optional to maintain backward compatibility and provide maximum flexibility.
|
|
72
|
+
*
|
|
73
|
+
* This metadata is automatically captured by the Revenium middleware and sent to
|
|
74
|
+
* the Revenium API for detailed usage analytics and billing calculations.
|
|
75
|
+
*
|
|
76
|
+
* @since 1.0.0
|
|
77
|
+
* @example Basic user tracking
|
|
78
|
+
* ```typescript
|
|
79
|
+
* usageMetadata: {
|
|
80
|
+
* subscriber: {
|
|
81
|
+
* id: 'user-123',
|
|
82
|
+
* email: 'user@my-company.com'
|
|
83
|
+
* },
|
|
84
|
+
* organizationId: 'my-company',
|
|
85
|
+
* productId: 'support-app'
|
|
86
|
+
* }
|
|
87
|
+
* ```
|
|
88
|
+
*
|
|
89
|
+
* @example Advanced tracking with quality metrics
|
|
90
|
+
* ```typescript
|
|
91
|
+
* usageMetadata: {
|
|
92
|
+
* subscriber: { id: 'user-456' },
|
|
93
|
+
* organizationId: 'enterprise-corp',
|
|
94
|
+
* productId: 'ai-assistant',
|
|
95
|
+
* taskType: 'customer-support',
|
|
96
|
+
* traceId: 'session-abc-123',
|
|
97
|
+
* responseQualityScore: 0.95,
|
|
98
|
+
* agent: 'support-bot-v2'
|
|
99
|
+
* }
|
|
100
|
+
* ```
|
|
101
|
+
*/
|
|
102
|
+
usageMetadata?: UsageMetadata;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
interface ChatCompletionCreateParamsNonStreaming {
|
|
106
|
+
/**
|
|
107
|
+
* Optional metadata for enhanced tracking and analytics.
|
|
108
|
+
*
|
|
109
|
+
* Provides rich context for business analytics, user tracking, and billing purposes.
|
|
110
|
+
* Specifically for non-streaming chat completions where the full response is returned at once.
|
|
111
|
+
*
|
|
112
|
+
* @see {@link UsageMetadata} for detailed field descriptions
|
|
113
|
+
*/
|
|
114
|
+
usageMetadata?: UsageMetadata;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
interface ChatCompletionCreateParamsStreaming {
|
|
118
|
+
/**
|
|
119
|
+
* Optional metadata for enhanced tracking and analytics.
|
|
120
|
+
*
|
|
121
|
+
* Provides rich context for business analytics, user tracking, and billing purposes.
|
|
122
|
+
* Specifically for streaming chat completions where the response is delivered incrementally.
|
|
123
|
+
*
|
|
124
|
+
* @see {@link UsageMetadata} for detailed field descriptions
|
|
125
|
+
*/
|
|
126
|
+
usageMetadata?: UsageMetadata;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* OpenAI Embeddings API augmentation
|
|
132
|
+
*
|
|
133
|
+
* Extends the OpenAI embeddings interface to include usageMetadata
|
|
134
|
+
* for comprehensive tracking of embedding generation requests.
|
|
135
|
+
*/
|
|
136
|
+
declare module 'openai/resources/embeddings' {
|
|
137
|
+
interface EmbeddingCreateParams {
|
|
138
|
+
/**
|
|
139
|
+
* Optional metadata for enhanced tracking and analytics.
|
|
140
|
+
*
|
|
141
|
+
* Provides rich context for business analytics, user tracking, and billing purposes
|
|
142
|
+
* specifically for embedding generation requests. Particularly useful for tracking
|
|
143
|
+
* vector database operations, search functionality, and document processing workflows.
|
|
144
|
+
*
|
|
145
|
+
* All fields are optional to maintain backward compatibility and provide maximum flexibility.
|
|
146
|
+
*
|
|
147
|
+
* @since 1.0.0
|
|
148
|
+
* @example Document indexing workflow
|
|
149
|
+
* ```typescript
|
|
150
|
+
* usageMetadata: {
|
|
151
|
+
* subscriber: {
|
|
152
|
+
* id: 'user-123',
|
|
153
|
+
* email: 'user@my-company.com'
|
|
154
|
+
* },
|
|
155
|
+
* organizationId: 'my-company',
|
|
156
|
+
* productId: 'vector-search',
|
|
157
|
+
* taskType: 'document-indexing',
|
|
158
|
+
* taskId: 'batch-index-2024-01'
|
|
159
|
+
* }
|
|
160
|
+
* ```
|
|
161
|
+
*
|
|
162
|
+
* @example Semantic search application
|
|
163
|
+
* ```typescript
|
|
164
|
+
* usageMetadata: {
|
|
165
|
+
* subscriber: { id: 'user-456' },
|
|
166
|
+
* organizationId: 'enterprise-corp',
|
|
167
|
+
* productId: 'knowledge-base',
|
|
168
|
+
* taskType: 'semantic-search',
|
|
169
|
+
* traceId: 'search-session-789'
|
|
170
|
+
* }
|
|
171
|
+
* ```
|
|
172
|
+
*
|
|
173
|
+
* @see {@link UsageMetadata} for detailed field descriptions
|
|
174
|
+
*/
|
|
175
|
+
usageMetadata?: UsageMetadata;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* OpenAI Responses API augmentation
|
|
181
|
+
*
|
|
182
|
+
* Extends the new Responses API to support usageMetadata for comprehensive tracking.
|
|
183
|
+
* The Responses API is OpenAI's new unified interface for agent-like applications.
|
|
184
|
+
*/
|
|
185
|
+
declare module 'openai' {
|
|
186
|
+
namespace Responses {
|
|
187
|
+
interface ResponseCreateParams {
|
|
188
|
+
/**
|
|
189
|
+
* Custom usage metadata for Revenium tracking
|
|
190
|
+
*
|
|
191
|
+
* Enables comprehensive tracking and analytics for Responses API calls.
|
|
192
|
+
* All fields are optional and can be customized based on your application needs.
|
|
193
|
+
*
|
|
194
|
+
* @example Basic Responses API usage with metadata
|
|
195
|
+
* ```typescript
|
|
196
|
+
* const response = await openai.responses.create({
|
|
197
|
+
* model: 'gpt-4.1',
|
|
198
|
+
* input: 'Analyze this data and provide insights',
|
|
199
|
+
* usageMetadata: {
|
|
200
|
+
* subscriber: {
|
|
201
|
+
* id: 'analyst-123',
|
|
202
|
+
* email: 'analyst@company.com'
|
|
203
|
+
* },
|
|
204
|
+
* organizationId: 'data-corp',
|
|
205
|
+
* productId: 'analytics-platform',
|
|
206
|
+
* taskType: 'data-analysis',
|
|
207
|
+
* agent: 'responses-api-v1'
|
|
208
|
+
* }
|
|
209
|
+
* });
|
|
210
|
+
* ```
|
|
211
|
+
*
|
|
212
|
+
* @example Streaming Responses API with metadata
|
|
213
|
+
* ```typescript
|
|
214
|
+
* const stream = await openai.responses.create({
|
|
215
|
+
* model: 'gpt-4.1',
|
|
216
|
+
* input: [
|
|
217
|
+
* { role: 'user', content: 'Generate a detailed report' }
|
|
218
|
+
* ],
|
|
219
|
+
* stream: true,
|
|
220
|
+
* usageMetadata: {
|
|
221
|
+
* subscriber: { id: 'user-456' },
|
|
222
|
+
* taskType: 'report-generation',
|
|
223
|
+
* traceId: 'session-789'
|
|
224
|
+
* }
|
|
225
|
+
* });
|
|
226
|
+
* ```
|
|
227
|
+
*
|
|
228
|
+
* @see {@link UsageMetadata} for detailed field descriptions
|
|
229
|
+
*/
|
|
230
|
+
usageMetadata?: UsageMetadata;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
}
|