@revenium/openai 1.0.12 → 1.0.13
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 +8 -8
- package/CHANGELOG.md +26 -0
- package/README.md +57 -351
- package/dist/cjs/core/config/loader.js +1 -1
- package/dist/cjs/core/config/manager.js +2 -1
- package/dist/cjs/core/config/manager.js.map +1 -1
- package/dist/cjs/core/providers/detector.js +3 -3
- package/dist/cjs/core/providers/detector.js.map +1 -1
- package/dist/cjs/core/tracking/api-client.js +1 -1
- package/dist/cjs/core/tracking/payload-builder.js +17 -12
- package/dist/cjs/core/tracking/payload-builder.js.map +1 -1
- package/dist/cjs/index.js +23 -2
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/types/index.js.map +1 -1
- package/dist/cjs/utils/metadata-builder.js +12 -5
- package/dist/cjs/utils/metadata-builder.js.map +1 -1
- package/dist/cjs/utils/stop-reason-mapper.js +4 -0
- package/dist/cjs/utils/stop-reason-mapper.js.map +1 -1
- package/dist/cjs/utils/url-builder.js +3 -3
- package/dist/esm/core/config/loader.js +1 -1
- package/dist/esm/core/config/manager.js +2 -1
- package/dist/esm/core/config/manager.js.map +1 -1
- package/dist/esm/core/providers/detector.js +3 -3
- package/dist/esm/core/providers/detector.js.map +1 -1
- package/dist/esm/core/tracking/api-client.js +1 -1
- package/dist/esm/core/tracking/payload-builder.js +17 -12
- package/dist/esm/core/tracking/payload-builder.js.map +1 -1
- package/dist/esm/index.js +22 -2
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/types/index.js.map +1 -1
- package/dist/esm/utils/metadata-builder.js +12 -5
- package/dist/esm/utils/metadata-builder.js.map +1 -1
- package/dist/esm/utils/stop-reason-mapper.js +4 -0
- package/dist/esm/utils/stop-reason-mapper.js.map +1 -1
- package/dist/esm/utils/url-builder.js +3 -3
- package/dist/types/core/config/manager.d.ts.map +1 -1
- package/dist/types/core/tracking/payload-builder.d.ts.map +1 -1
- package/dist/types/index.d.ts +23 -2
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/types/index.d.ts +9 -13
- package/dist/types/types/index.d.ts.map +1 -1
- package/dist/types/types/openai-augmentation.d.ts +1 -2
- package/dist/types/types/openai-augmentation.d.ts.map +1 -1
- package/dist/types/utils/metadata-builder.d.ts +2 -1
- package/dist/types/utils/metadata-builder.d.ts.map +1 -1
- package/dist/types/utils/stop-reason-mapper.d.ts.map +1 -1
- package/dist/types/utils/url-builder.d.ts +3 -3
- package/examples/README.md +3 -41
- package/examples/azure-basic.ts +1 -1
- package/examples/azure-responses-basic.ts +3 -3
- package/examples/azure-responses-streaming.ts +3 -3
- package/examples/azure-streaming.ts +2 -2
- package/examples/getting_started.ts +1 -1
- package/examples/openai-basic.ts +2 -2
- package/examples/openai-function-calling.ts +2 -2
- package/examples/openai-responses-basic.ts +2 -2
- package/examples/openai-responses-streaming.ts +2 -2
- package/examples/openai-streaming.ts +1 -1
- package/examples/openai-vision.ts +2 -2
- package/package.json +1 -1
- package/src/core/config/loader.ts +1 -1
- package/src/core/config/manager.ts +2 -1
- package/src/core/providers/detector.ts +3 -3
- package/src/core/tracking/api-client.ts +1 -1
- package/src/core/tracking/payload-builder.ts +17 -12
- package/src/index.ts +27 -3
- package/src/types/index.ts +11 -14
- package/src/types/openai-augmentation.ts +1 -2
- package/src/utils/metadata-builder.ts +16 -7
- package/src/utils/stop-reason-mapper.ts +4 -0
- package/src/utils/url-builder.ts +3 -3
package/.env.example
CHANGED
|
@@ -4,17 +4,17 @@
|
|
|
4
4
|
# Required: Your Revenium API key (starts with hak_)
|
|
5
5
|
REVENIUM_METERING_API_KEY=hak_your_revenium_api_key_here
|
|
6
6
|
|
|
7
|
-
# Optional: Revenium API base URL (defaults to https://api.revenium.
|
|
8
|
-
#REVENIUM_METERING_BASE_URL=https://api.revenium.
|
|
7
|
+
# Optional: Revenium API base URL (defaults to https://api.revenium.ai)
|
|
8
|
+
#REVENIUM_METERING_BASE_URL=https://api.revenium.ai
|
|
9
9
|
|
|
10
10
|
# Required: Your OpenAI API key (starts with sk-)
|
|
11
11
|
OPENAI_API_KEY=sk_your_openai_api_key_here
|
|
12
12
|
|
|
13
|
-
# Optional:
|
|
14
|
-
AZURE_OPENAI_ENDPOINT=https://your-resource-name.openai.azure.com/
|
|
15
|
-
AZURE_OPENAI_API_KEY=your-azure-openai-api-key-here
|
|
16
|
-
AZURE_OPENAI_DEPLOYMENT=your-deployment-name-here
|
|
17
|
-
AZURE_OPENAI_API_VERSION=2024-12-01-preview
|
|
13
|
+
# Optional: Azure OpenAI configuration (uncomment if using Azure)
|
|
14
|
+
#AZURE_OPENAI_ENDPOINT=https://your-resource-name.openai.azure.com/
|
|
15
|
+
#AZURE_OPENAI_API_KEY=your-azure-openai-api-key-here
|
|
16
|
+
#AZURE_OPENAI_DEPLOYMENT=your-deployment-name-here
|
|
17
|
+
#AZURE_OPENAI_API_VERSION=2024-12-01-preview
|
|
18
18
|
|
|
19
19
|
# Optional: Enable debug logging
|
|
20
|
-
REVENIUM_DEBUG=
|
|
20
|
+
#REVENIUM_DEBUG=true
|
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,31 @@ All notable changes to this project will be documented in this file.
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
7
|
|
|
8
|
+
## [1.0.13] - 2025-11-06
|
|
9
|
+
|
|
10
|
+
### Added
|
|
11
|
+
- configure() function for manual configuration (simpler alias for initializeRevenium)
|
|
12
|
+
- Support for COST_LIMIT and COMPLETION_LIMIT stop reasons
|
|
13
|
+
- Support for all 7 operationType enum values (CHAT, GENERATE, EMBED, CLASSIFY, SUMMARIZE, TRANSLATE, OTHER)
|
|
14
|
+
|
|
15
|
+
### Changed
|
|
16
|
+
- **BREAKING:** responseQualityScore now uses 0.0-1.0 scale (was 0-100) per updated API specification
|
|
17
|
+
- API endpoint updated from api.revenium.io to api.revenium.ai (new production domain)
|
|
18
|
+
- Improved API compliance with proper enum values and field casing
|
|
19
|
+
- Enhanced token counting accuracy for cache operations
|
|
20
|
+
- Email addresses now masked in debug logs for PII protection
|
|
21
|
+
- All examples updated to use 0.0-1.0 scale for responseQualityScore
|
|
22
|
+
|
|
23
|
+
### Fixed
|
|
24
|
+
- Improved documentation accuracy and removed deprecated configuration options
|
|
25
|
+
- config.debug property now properly enables debug logging
|
|
26
|
+
- Manual configuration example now includes required patchOpenAIInstance call
|
|
27
|
+
- Updated OpenAI version requirement to match peer dependency (v5.0.0+)
|
|
28
|
+
- Enhanced metadata validation for responseQualityScore scale
|
|
29
|
+
- Improved provider detection fallback handling
|
|
30
|
+
- Fixed cache token reporting for embeddings API
|
|
31
|
+
- reasoningTokenCount now correctly optional based on provider support
|
|
32
|
+
|
|
8
33
|
## [1.0.12] - 2025-10-30
|
|
9
34
|
|
|
10
35
|
### Changed
|
|
@@ -45,6 +70,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
45
70
|
- Azure OpenAI support
|
|
46
71
|
- TypeScript support with native type integration
|
|
47
72
|
|
|
73
|
+
[1.0.13]: https://github.com/revenium/revenium-middleware-openai-node/releases/tag/v1.0.13
|
|
48
74
|
[1.0.12]: https://github.com/revenium/revenium-middleware-openai-node/releases/tag/v1.0.12
|
|
49
75
|
[1.0.11]: https://github.com/revenium/revenium-middleware-openai-node/releases/tag/v1.0.11
|
|
50
76
|
[1.0.10]: https://github.com/revenium/revenium-middleware-openai-node/releases/tag/v1.0.10
|
package/README.md
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
[](https://www.npmjs.com/package/@revenium/openai)
|
|
4
4
|
[](https://nodejs.org/)
|
|
5
5
|
[](https://docs.revenium.io)
|
|
6
|
+
[](https://www.revenium.ai)
|
|
6
7
|
[](https://opensource.org/licenses/MIT)
|
|
7
8
|
|
|
8
9
|
**Transparent TypeScript middleware for automatic Revenium usage tracking with OpenAI**
|
|
@@ -12,8 +13,8 @@ A professional-grade Node.js middleware that seamlessly integrates with OpenAI a
|
|
|
12
13
|
## Features
|
|
13
14
|
|
|
14
15
|
- **Seamless Integration** - Native TypeScript support, no type casting required
|
|
15
|
-
- **Optional Metadata** - Track users, organizations, and
|
|
16
|
-
- **Dual API Support** - Chat Completions API +
|
|
16
|
+
- **Optional Metadata** - Track users, organizations, and business context (12 predefined fields, all optional)
|
|
17
|
+
- **Dual API Support** - Chat Completions API + Responses API
|
|
17
18
|
- **Azure OpenAI Support** - Full Azure OpenAI integration with automatic detection
|
|
18
19
|
- **Type Safety** - Complete TypeScript support with IntelliSense
|
|
19
20
|
- **Streaming Support** - Handles regular and streaming requests seamlessly
|
|
@@ -44,7 +45,7 @@ Create a `.env` file:
|
|
|
44
45
|
**NOTE: YOU MUST REPLACE THE PLACEHOLDERS WITH YOUR OWN API KEYS**
|
|
45
46
|
|
|
46
47
|
```env
|
|
47
|
-
REVENIUM_METERING_BASE_URL=https://api.revenium.
|
|
48
|
+
REVENIUM_METERING_BASE_URL=https://api.revenium.ai
|
|
48
49
|
REVENIUM_METERING_API_KEY=hak_your_revenium_api_key_here
|
|
49
50
|
OPENAI_API_KEY=sk_your_openai_api_key_here
|
|
50
51
|
```
|
|
@@ -74,7 +75,7 @@ $env:REVENIUM_DEBUG="true"; npx tsx node_modules/@revenium/openai/examples/getti
|
|
|
74
75
|
## Requirements
|
|
75
76
|
|
|
76
77
|
- Node.js 16+
|
|
77
|
-
- OpenAI package
|
|
78
|
+
- OpenAI package v5.0.0 or later
|
|
78
79
|
- TypeScript 5.0+ (for TypeScript projects)
|
|
79
80
|
|
|
80
81
|
---
|
|
@@ -95,7 +96,7 @@ The middleware automatically captures comprehensive usage data:
|
|
|
95
96
|
- **User Tracking** - Subscriber ID, email, credentials
|
|
96
97
|
- **Organization Data** - Organization ID, subscription ID, product ID
|
|
97
98
|
- **Task Classification** - Task type, agent identifier, trace ID
|
|
98
|
-
- **Quality Metrics** - Response quality scores,
|
|
99
|
+
- **Quality Metrics** - Response quality scores, task identifiers
|
|
99
100
|
|
|
100
101
|
### **Technical Details**
|
|
101
102
|
|
|
@@ -104,226 +105,6 @@ The middleware automatically captures comprehensive usage data:
|
|
|
104
105
|
- **Error Tracking** - Failed requests, error types, retry attempts
|
|
105
106
|
- **Environment Info** - Development vs production usage
|
|
106
107
|
|
|
107
|
-
## OpenAI Responses API Support
|
|
108
|
-
|
|
109
|
-
This middleware includes **full support** for OpenAI's new Responses API, which is designed to replace the traditional Chat Completions API with enhanced capabilities for agent-like applications.
|
|
110
|
-
|
|
111
|
-
### What is the Responses API?
|
|
112
|
-
|
|
113
|
-
The Responses API is OpenAI's new stateful API that:
|
|
114
|
-
|
|
115
|
-
- Uses `input` instead of `messages` parameter for simplified interaction
|
|
116
|
-
- Provides unified experience combining chat completions and assistants capabilities
|
|
117
|
-
- Supports advanced features like background tasks, function calling, and code interpreter
|
|
118
|
-
- Offers better streaming and real-time response generation
|
|
119
|
-
- Works with GPT-5 and other advanced models
|
|
120
|
-
|
|
121
|
-
### API Comparison
|
|
122
|
-
|
|
123
|
-
**Traditional Chat Completions:**
|
|
124
|
-
|
|
125
|
-
```javascript
|
|
126
|
-
const response = await openai.chat.completions.create({
|
|
127
|
-
model: 'gpt-4o',
|
|
128
|
-
messages: [{ role: 'user', content: 'Hello' }],
|
|
129
|
-
});
|
|
130
|
-
```
|
|
131
|
-
|
|
132
|
-
**New Responses API:**
|
|
133
|
-
|
|
134
|
-
```javascript
|
|
135
|
-
const response = await openai.responses.create({
|
|
136
|
-
model: 'gpt-5',
|
|
137
|
-
input: 'Hello', // Simplified input parameter
|
|
138
|
-
});
|
|
139
|
-
```
|
|
140
|
-
|
|
141
|
-
### Key Differences
|
|
142
|
-
|
|
143
|
-
| Feature | Chat Completions | Responses API |
|
|
144
|
-
| ---------------------- | ---------------------------- | ----------------------------------- |
|
|
145
|
-
| **Input Format** | `messages: [...]` | `input: "string"` or `input: [...]` |
|
|
146
|
-
| **Models** | GPT-4, GPT-4o, etc. | GPT-5, GPT-4o, etc. |
|
|
147
|
-
| **Response Structure** | `choices[0].message.content` | `output_text` |
|
|
148
|
-
| **Stateful** | No | Yes (with `store: true`) |
|
|
149
|
-
| **Advanced Features** | Limited | Built-in tools, reasoning, etc. |
|
|
150
|
-
| **Temperature** | Supported | Not supported with GPT-5 |
|
|
151
|
-
|
|
152
|
-
### Requirements & Installation
|
|
153
|
-
|
|
154
|
-
**OpenAI SDK Version:**
|
|
155
|
-
|
|
156
|
-
- **Minimum:** `5.8.0` (when Responses API was officially released)
|
|
157
|
-
- **Recommended:** `5.8.2` or later (tested and verified)
|
|
158
|
-
- **Current:** `6.2.0` (latest available)
|
|
159
|
-
|
|
160
|
-
**Installation:**
|
|
161
|
-
|
|
162
|
-
```bash
|
|
163
|
-
# Install latest version with Responses API support
|
|
164
|
-
npm install openai@^5.8.0
|
|
165
|
-
|
|
166
|
-
# Or install specific tested version
|
|
167
|
-
npm install openai@5.8.2
|
|
168
|
-
```
|
|
169
|
-
|
|
170
|
-
### Current Status
|
|
171
|
-
|
|
172
|
-
**The Responses API is officially available in OpenAI SDK 5.8+**
|
|
173
|
-
|
|
174
|
-
**Official Release:**
|
|
175
|
-
|
|
176
|
-
- Released by OpenAI in SDK version 5.8.0
|
|
177
|
-
- Fully documented in official OpenAI documentation
|
|
178
|
-
- Production-ready with GPT-5 and other supported models
|
|
179
|
-
- Complete middleware support with Revenium integration
|
|
180
|
-
|
|
181
|
-
**Middleware Features:**
|
|
182
|
-
|
|
183
|
-
- Full Responses API support (streaming & non-streaming)
|
|
184
|
-
- Seamless metadata tracking identical to Chat Completions
|
|
185
|
-
- Type-safe TypeScript integration
|
|
186
|
-
- Complete token tracking including reasoning tokens
|
|
187
|
-
- Azure OpenAI compatibility
|
|
188
|
-
|
|
189
|
-
**References:**
|
|
190
|
-
|
|
191
|
-
- [OpenAI Responses API Documentation](https://platform.openai.com/docs/guides/migrate-to-responses)
|
|
192
|
-
- [Azure OpenAI Responses API Documentation](https://learn.microsoft.com/en-us/azure/ai-foundry/openai/how-to/responses)
|
|
193
|
-
|
|
194
|
-
### Working Examples
|
|
195
|
-
|
|
196
|
-
Complete working examples are included with this package. Each example is fully documented and ready to run.
|
|
197
|
-
|
|
198
|
-
#### Available Examples
|
|
199
|
-
|
|
200
|
-
**OpenAI Chat Completions API:**
|
|
201
|
-
- `openai-basic.ts` - Basic chat + embeddings with optional metadata
|
|
202
|
-
- `openai-streaming.ts` - Streaming responses + batch embeddings
|
|
203
|
-
|
|
204
|
-
**OpenAI Responses API (SDK 5.8+):**
|
|
205
|
-
- `openai-responses-basic.ts` - New Responses API with string input
|
|
206
|
-
- `openai-responses-streaming.ts` - Streaming with Responses API
|
|
207
|
-
|
|
208
|
-
**Azure OpenAI:**
|
|
209
|
-
- `azure-basic.ts` - Azure chat completions + embeddings
|
|
210
|
-
- `azure-streaming.ts` - Azure streaming responses
|
|
211
|
-
- `azure-responses-basic.ts` - Azure Responses API
|
|
212
|
-
- `azure-responses-streaming.ts` - Azure streaming Responses API
|
|
213
|
-
|
|
214
|
-
**Detailed Guide:**
|
|
215
|
-
- `examples/README.md` - Complete setup guide with TypeScript and JavaScript patterns
|
|
216
|
-
|
|
217
|
-
#### Running Examples
|
|
218
|
-
|
|
219
|
-
**Installed via npm?**
|
|
220
|
-
```bash
|
|
221
|
-
# Try these in order:
|
|
222
|
-
npx tsx node_modules/@revenium/openai/examples/openai-basic.ts
|
|
223
|
-
npx tsx node_modules/@revenium/openai/examples/openai-streaming.ts
|
|
224
|
-
npx tsx node_modules/@revenium/openai/examples/openai-responses-basic.ts
|
|
225
|
-
|
|
226
|
-
# View all examples:
|
|
227
|
-
ls node_modules/@revenium/openai/examples/
|
|
228
|
-
```
|
|
229
|
-
|
|
230
|
-
**Cloned from GitHub?**
|
|
231
|
-
```bash
|
|
232
|
-
npm install
|
|
233
|
-
npm run example:openai-basic
|
|
234
|
-
npm run example:openai-streaming
|
|
235
|
-
npm run example:openai-responses-basic
|
|
236
|
-
|
|
237
|
-
# See all example scripts:
|
|
238
|
-
npm run
|
|
239
|
-
```
|
|
240
|
-
|
|
241
|
-
**Browse online:** [`examples/` directory on GitHub](https://github.com/revenium/revenium-middleware-openai-node/tree/HEAD/examples)
|
|
242
|
-
|
|
243
|
-
### Temporarily Disabling Tracking
|
|
244
|
-
|
|
245
|
-
If you need to disable Revenium tracking temporarily, you can unpatch the OpenAI client:
|
|
246
|
-
|
|
247
|
-
```javascript
|
|
248
|
-
import { unpatchOpenAI, patchOpenAI } from '@revenium/openai-middleware';
|
|
249
|
-
|
|
250
|
-
// Disable tracking
|
|
251
|
-
unpatchOpenAI();
|
|
252
|
-
|
|
253
|
-
// Your OpenAI calls now bypass Revenium tracking
|
|
254
|
-
await openai.chat.completions.create({...});
|
|
255
|
-
|
|
256
|
-
// Re-enable tracking
|
|
257
|
-
patchOpenAI();
|
|
258
|
-
```
|
|
259
|
-
|
|
260
|
-
**Common use cases:**
|
|
261
|
-
|
|
262
|
-
- **Debugging**: Isolate whether issues are caused by the middleware
|
|
263
|
-
- **Testing**: Compare behavior with/without tracking
|
|
264
|
-
- **Conditional tracking**: Enable/disable based on environment
|
|
265
|
-
- **Troubleshooting**: Temporary bypass during incident response
|
|
266
|
-
|
|
267
|
-
**Note**: This affects all OpenAI instances globally since we patch the prototype methods.
|
|
268
|
-
|
|
269
|
-
## Azure OpenAI Integration
|
|
270
|
-
|
|
271
|
-
**Azure OpenAI support** The middleware automatically detects Azure OpenAI clients and provides accurate usage tracking and cost calculation.
|
|
272
|
-
|
|
273
|
-
### Quick Start with Azure OpenAI
|
|
274
|
-
|
|
275
|
-
**Use case:** Automatic Azure OpenAI client detection with deployment name mapping and accurate usage tracking.
|
|
276
|
-
|
|
277
|
-
See complete Azure examples:
|
|
278
|
-
- `examples/azure-basic.ts` - Azure chat completions with environment variable setup
|
|
279
|
-
- `examples/azure-streaming.ts` - Azure streaming responses
|
|
280
|
-
- `examples/azure-responses-basic.ts` - Azure Responses API integration
|
|
281
|
-
|
|
282
|
-
**Environment variables needed:**
|
|
283
|
-
```bash
|
|
284
|
-
# Azure OpenAI configuration
|
|
285
|
-
AZURE_OPENAI_ENDPOINT="https://your-resource.openai.azure.com/"
|
|
286
|
-
AZURE_OPENAI_API_KEY="your-azure-api-key"
|
|
287
|
-
AZURE_OPENAI_DEPLOYMENT="gpt-4o"
|
|
288
|
-
AZURE_OPENAI_API_VERSION="2024-12-01-preview"
|
|
289
|
-
|
|
290
|
-
# Revenium configuration
|
|
291
|
-
REVENIUM_METERING_API_KEY="hak_your_revenium_api_key"
|
|
292
|
-
REVENIUM_METERING_BASE_URL="https://api.revenium.io"
|
|
293
|
-
```
|
|
294
|
-
|
|
295
|
-
### Azure Features
|
|
296
|
-
|
|
297
|
-
- **Automatic Detection**: Detects Azure OpenAI clients automatically
|
|
298
|
-
- **Model Name Resolution**: Maps Azure deployment names to standard model names for accurate pricing
|
|
299
|
-
- **Provider Metadata**: Correctly tags requests with `provider: "Azure"` and `modelSource: "OPENAI"`
|
|
300
|
-
- **Deployment Support**: Works with any Azure deployment name (simple or complex)
|
|
301
|
-
- **Endpoint Flexibility**: Supports all Azure OpenAI endpoint formats
|
|
302
|
-
- **Zero Code Changes**: Existing Azure OpenAI code works without modification
|
|
303
|
-
|
|
304
|
-
### Azure Environment Variables
|
|
305
|
-
|
|
306
|
-
| Variable | Required | Description | Example |
|
|
307
|
-
| -------------------------- | -------- | ---------------------------------------------- | ------------------------------------ |
|
|
308
|
-
| `AZURE_OPENAI_ENDPOINT` | Yes | Your Azure OpenAI endpoint URL | `https://acme.openai.azure.com/` |
|
|
309
|
-
| `AZURE_OPENAI_API_KEY` | Yes | Your Azure OpenAI API key | `abc123...` |
|
|
310
|
-
| `AZURE_OPENAI_DEPLOYMENT` | No | Default deployment name | `gpt-4o` or `text-embedding-3-large` |
|
|
311
|
-
| `AZURE_OPENAI_API_VERSION` | No | API version (defaults to `2024-12-01-preview`) | `2024-12-01-preview` |
|
|
312
|
-
|
|
313
|
-
### Azure Model Name Resolution
|
|
314
|
-
|
|
315
|
-
The middleware automatically maps Azure deployment names to standard model names for accurate pricing:
|
|
316
|
-
|
|
317
|
-
```typescript
|
|
318
|
-
// Azure deployment names → Standard model names for pricing
|
|
319
|
-
"gpt-4o-2024-11-20" → "gpt-4o"
|
|
320
|
-
"gpt4o-prod" → "gpt-4o"
|
|
321
|
-
"o4-mini" → "gpt-4o-mini"
|
|
322
|
-
"gpt-35-turbo-dev" → "gpt-3.5-turbo"
|
|
323
|
-
"text-embedding-3-large" → "text-embedding-3-large" // Direct match
|
|
324
|
-
"embedding-3-large" → "text-embedding-3-large"
|
|
325
|
-
```
|
|
326
|
-
|
|
327
108
|
## Advanced Usage
|
|
328
109
|
|
|
329
110
|
### Initialization Options
|
|
@@ -361,13 +142,13 @@ For detailed examples of all initialization patterns, see [`examples/`](https://
|
|
|
361
142
|
|
|
362
143
|
### Streaming Responses
|
|
363
144
|
|
|
364
|
-
Streaming is fully supported with real-time token tracking
|
|
145
|
+
Streaming is fully supported with real-time token tracking. The middleware automatically tracks streaming responses without any additional configuration.
|
|
365
146
|
|
|
366
147
|
See [`examples/openai-streaming.ts`](https://github.com/revenium/revenium-middleware-openai-node/blob/HEAD/examples/openai-streaming.ts) and [`examples/azure-streaming.ts`](https://github.com/revenium/revenium-middleware-openai-node/blob/HEAD/examples/azure-streaming.ts) for working streaming examples.
|
|
367
148
|
|
|
368
149
|
### Custom Metadata Tracking
|
|
369
150
|
|
|
370
|
-
Add business context to track usage by organization, user, task type, or custom
|
|
151
|
+
Add business context to track usage by organization, user, task type, or custom identifiers. Pass a `usageMetadata` object with any of these optional fields:
|
|
371
152
|
|
|
372
153
|
| Field | Description | Use Case |
|
|
373
154
|
|-------|-------------|----------|
|
|
@@ -387,7 +168,7 @@ Add business context to track usage by organization, user, task type, or custom
|
|
|
387
168
|
- [API Reference](https://revenium.readme.io/reference/meter_ai_completion) - Complete metadata field documentation
|
|
388
169
|
|
|
389
170
|
### OpenAI Responses API
|
|
390
|
-
**Use case:** Using OpenAI's
|
|
171
|
+
**Use case:** Using OpenAI's Responses API with string inputs and simplified interface.
|
|
391
172
|
|
|
392
173
|
See working examples:
|
|
393
174
|
- `examples/openai-responses-basic.ts` - Basic Responses API usage
|
|
@@ -412,15 +193,16 @@ Embeddings examples are included in:
|
|
|
412
193
|
For advanced use cases, configure the middleware manually:
|
|
413
194
|
|
|
414
195
|
```typescript
|
|
415
|
-
import { configure } from '@revenium/openai';
|
|
196
|
+
import { configure, patchOpenAIInstance } from '@revenium/openai';
|
|
197
|
+
import OpenAI from 'openai';
|
|
416
198
|
|
|
417
199
|
configure({
|
|
418
200
|
reveniumApiKey: 'hak_your_api_key',
|
|
419
|
-
reveniumBaseUrl: 'https://api.revenium.
|
|
420
|
-
|
|
421
|
-
failSilent: true,
|
|
422
|
-
maxRetries: 3,
|
|
201
|
+
reveniumBaseUrl: 'https://api.revenium.ai',
|
|
202
|
+
debug: true,
|
|
423
203
|
});
|
|
204
|
+
|
|
205
|
+
const openai = patchOpenAIInstance(new OpenAI());
|
|
424
206
|
```
|
|
425
207
|
|
|
426
208
|
## Configuration Options
|
|
@@ -431,7 +213,7 @@ configure({
|
|
|
431
213
|
| ------------------------------ | -------- | ------------------------------- | ---------------------------------------------- |
|
|
432
214
|
| `REVENIUM_METERING_API_KEY` | true | - | Your Revenium API key (starts with `hak_`) |
|
|
433
215
|
| `OPENAI_API_KEY` | true | - | Your OpenAI API key (starts with `sk-`) |
|
|
434
|
-
| `REVENIUM_METERING_BASE_URL` | false | `https://api.revenium.
|
|
216
|
+
| `REVENIUM_METERING_BASE_URL` | false | `https://api.revenium.ai` | Revenium metering API base URL |
|
|
435
217
|
| `REVENIUM_DEBUG` | false | `false` | Enable debug logging (`true`/`false`) |
|
|
436
218
|
| `AZURE_OPENAI_ENDPOINT` | false | - | Azure OpenAI endpoint URL (for Azure testing) |
|
|
437
219
|
| `AZURE_OPENAI_API_KEY` | false | - | Azure OpenAI API key (for Azure testing) |
|
|
@@ -440,65 +222,34 @@ configure({
|
|
|
440
222
|
|
|
441
223
|
**Important Note about `REVENIUM_METERING_BASE_URL`:**
|
|
442
224
|
|
|
443
|
-
- This variable is **optional** and defaults to the production URL (`https://api.revenium.
|
|
225
|
+
- This variable is **optional** and defaults to the production URL (`https://api.revenium.ai`)
|
|
444
226
|
- If you don't set it explicitly, the middleware will use the default production endpoint
|
|
445
227
|
- However, you may see console warnings or errors if the middleware cannot determine the correct environment
|
|
446
228
|
- **Best practice:** Always set this variable explicitly to match your environment:
|
|
447
229
|
|
|
448
230
|
```bash
|
|
449
231
|
# Default production URL (recommended)
|
|
450
|
-
REVENIUM_METERING_BASE_URL=https://api.revenium.
|
|
232
|
+
REVENIUM_METERING_BASE_URL=https://api.revenium.ai
|
|
451
233
|
```
|
|
452
234
|
|
|
453
235
|
- **Remember:** Your `REVENIUM_METERING_API_KEY` must match your base URL environment
|
|
454
236
|
|
|
455
|
-
### Usage Metadata Options
|
|
456
|
-
|
|
457
|
-
All metadata fields are optional and help provide better analytics:
|
|
458
|
-
|
|
459
|
-
```typescript
|
|
460
|
-
interface UsageMetadata {
|
|
461
|
-
traceId?: string; // Session or conversation ID
|
|
462
|
-
taskType?: string; // Type of AI task (e.g., "chat", "summary")
|
|
463
|
-
subscriber?: {
|
|
464
|
-
// User information (nested structure)
|
|
465
|
-
id?: string; // User ID from your system
|
|
466
|
-
email?: string; // User's email address
|
|
467
|
-
credential?: {
|
|
468
|
-
// User credentials
|
|
469
|
-
name?: string; // Credential name
|
|
470
|
-
value?: string; // Credential value
|
|
471
|
-
};
|
|
472
|
-
};
|
|
473
|
-
organizationId?: string; // Organization/company ID
|
|
474
|
-
subscriptionId?: string; // Billing plan ID
|
|
475
|
-
productId?: string; // Your product/feature ID
|
|
476
|
-
agent?: string; // AI agent identifier
|
|
477
|
-
responseQualityScore?: number; // Quality score (0-1)
|
|
478
|
-
}
|
|
479
|
-
```
|
|
480
|
-
|
|
481
237
|
## Included Examples
|
|
482
238
|
|
|
483
|
-
The package includes
|
|
484
|
-
|
|
485
|
-
**OpenAI Examples:**
|
|
486
|
-
- **openai-basic.ts**: Basic chat completions with metadata tracking
|
|
487
|
-
- **openai-streaming.ts**: Streaming responses with real-time output
|
|
488
|
-
- **openai-responses-basic.ts**: New Responses API usage (OpenAI SDK 5.8+)
|
|
489
|
-
- **openai-responses-streaming.ts**: Streaming with Responses API
|
|
490
|
-
|
|
491
|
-
**Azure OpenAI Examples:**
|
|
492
|
-
- **azure-basic.ts**: Azure OpenAI chat completions
|
|
493
|
-
- **azure-streaming.ts**: Azure streaming responses
|
|
494
|
-
- **azure-responses-basic.ts**: Azure Responses API
|
|
495
|
-
- **azure-responses-streaming.ts**: Azure streaming Responses API
|
|
239
|
+
The package includes comprehensive example files covering:
|
|
496
240
|
|
|
497
|
-
**
|
|
241
|
+
- **Getting Started** - Simple entry point with all metadata fields documented
|
|
242
|
+
- **Chat Completions** - Basic and streaming usage patterns
|
|
243
|
+
- **Responses API** - OpenAI's new API with simplified interface
|
|
244
|
+
- **Azure OpenAI** - Automatic Azure detection and integration
|
|
245
|
+
- **Embeddings** - Text embedding generation with tracking
|
|
498
246
|
|
|
499
|
-
|
|
247
|
+
Run the getting started example:
|
|
248
|
+
```bash
|
|
249
|
+
npx tsx node_modules/@revenium/openai/examples/getting_started.ts
|
|
250
|
+
```
|
|
500
251
|
|
|
501
|
-
For
|
|
252
|
+
For complete example documentation, setup instructions, and all available examples, see [examples/README.md](https://github.com/revenium/revenium-middleware-openai-node/blob/HEAD/examples/README.md).
|
|
502
253
|
|
|
503
254
|
## How It Works
|
|
504
255
|
|
|
@@ -517,121 +268,76 @@ The middleware never blocks your application - if Revenium tracking fails, your
|
|
|
517
268
|
|
|
518
269
|
### Common Issues
|
|
519
270
|
|
|
520
|
-
####
|
|
271
|
+
#### No tracking data appears
|
|
521
272
|
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
**Solution**: Enable debug logging to check middleware status:
|
|
273
|
+
Ensure environment variables are set and enable debug logging:
|
|
525
274
|
|
|
526
275
|
```bash
|
|
276
|
+
export REVENIUM_METERING_API_KEY="hak_your_key"
|
|
277
|
+
export OPENAI_API_KEY="sk_your_key"
|
|
527
278
|
export REVENIUM_DEBUG=true
|
|
528
279
|
```
|
|
529
280
|
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
```bash
|
|
281
|
+
Look for these log messages:
|
|
282
|
+
```
|
|
533
283
|
[Revenium Debug] OpenAI chat.completions.create intercepted
|
|
534
284
|
[Revenium Debug] Revenium tracking successful
|
|
535
|
-
|
|
536
|
-
# For Responses API:
|
|
537
|
-
[Revenium Debug] OpenAI responses.create intercepted
|
|
538
|
-
[Revenium Debug] Revenium tracking successful
|
|
539
285
|
```
|
|
540
286
|
|
|
541
|
-
####
|
|
287
|
+
#### TypeScript errors with usageMetadata
|
|
542
288
|
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
**Solution**: Ensure your API key matches your base URL environment:
|
|
546
|
-
|
|
547
|
-
```bash
|
|
548
|
-
# Correct - Key and URL from same environment
|
|
549
|
-
REVENIUM_METERING_API_KEY=hak_your_api_key_here
|
|
550
|
-
REVENIUM_METERING_BASE_URL=https://api.revenium.io
|
|
551
|
-
|
|
552
|
-
# Wrong - Key and URL from different environments
|
|
553
|
-
REVENIUM_METERING_API_KEY=hak_wrong_environment_key
|
|
554
|
-
REVENIUM_METERING_BASE_URL=https://api.revenium.io
|
|
555
|
-
```
|
|
556
|
-
|
|
557
|
-
#### 3. **TypeScript type errors**
|
|
558
|
-
|
|
559
|
-
**Symptoms**: TypeScript errors about `usageMetadata` property
|
|
560
|
-
|
|
561
|
-
**Solution**: Ensure you're importing the middleware before OpenAI:
|
|
289
|
+
Import the middleware before OpenAI to enable type augmentation:
|
|
562
290
|
|
|
563
291
|
```typescript
|
|
564
|
-
// Correct order
|
|
565
292
|
import { initializeReveniumFromEnv, patchOpenAIInstance } from '@revenium/openai';
|
|
566
293
|
import OpenAI from 'openai';
|
|
567
|
-
|
|
568
|
-
// Wrong order
|
|
569
|
-
import OpenAI from 'openai';
|
|
570
|
-
import { initializeReveniumFromEnv, patchOpenAIInstance } from '@revenium/openai';
|
|
571
294
|
```
|
|
572
295
|
|
|
573
|
-
####
|
|
296
|
+
#### Azure OpenAI not tracking
|
|
574
297
|
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
**Solution**: Ensure you're using `patchOpenAIInstance()` with your Azure client:
|
|
298
|
+
Ensure you patch the Azure client:
|
|
578
299
|
|
|
579
300
|
```typescript
|
|
580
301
|
import { AzureOpenAI } from 'openai';
|
|
581
302
|
import { patchOpenAIInstance } from '@revenium/openai';
|
|
582
303
|
|
|
583
|
-
// Correct
|
|
584
304
|
const azure = patchOpenAIInstance(new AzureOpenAI({...}));
|
|
585
|
-
|
|
586
|
-
// Wrong - not patched
|
|
587
|
-
const azure = new AzureOpenAI({...});
|
|
588
|
-
```
|
|
589
|
-
|
|
590
|
-
#### 5. **Responses API not available**
|
|
591
|
-
|
|
592
|
-
**Symptoms**: `openai.responses.create` is undefined
|
|
593
|
-
|
|
594
|
-
**Solution**: Upgrade to OpenAI SDK 5.8+ for Responses API support:
|
|
595
|
-
|
|
596
|
-
```bash
|
|
597
|
-
npm install openai@^5.8.0
|
|
598
305
|
```
|
|
599
306
|
|
|
600
307
|
### Debug Mode
|
|
601
308
|
|
|
602
|
-
Enable
|
|
309
|
+
Enable detailed logging:
|
|
603
310
|
|
|
604
311
|
```bash
|
|
605
312
|
export REVENIUM_DEBUG=true
|
|
606
313
|
```
|
|
607
314
|
|
|
608
|
-
|
|
315
|
+
### Getting Help
|
|
609
316
|
|
|
610
|
-
|
|
611
|
-
- Request interception confirmations
|
|
612
|
-
- Metadata extraction details
|
|
613
|
-
- Tracking success/failure messages
|
|
614
|
-
- Error details and stack traces
|
|
317
|
+
If issues persist:
|
|
615
318
|
|
|
616
|
-
|
|
319
|
+
1. Check logs with `REVENIUM_DEBUG=true`
|
|
320
|
+
2. Verify environment variables are set
|
|
321
|
+
3. Test with `examples/getting_started.ts`
|
|
322
|
+
4. Contact support@revenium.io with debug logs
|
|
617
323
|
|
|
618
|
-
|
|
324
|
+
## Supported Models
|
|
619
325
|
|
|
620
|
-
|
|
621
|
-
2. **Verify environment variables** are set correctly
|
|
622
|
-
3. **Test with minimal example** from our documentation
|
|
623
|
-
4. **Contact support** with debug logs and error details
|
|
326
|
+
This middleware works with any OpenAI model. Examples in this package include:
|
|
624
327
|
|
|
625
|
-
|
|
328
|
+
**Chat Completions:**
|
|
329
|
+
- `gpt-4o-mini`, `gpt-4o` (GPT-4 family)
|
|
330
|
+
- `gpt-5`, `gpt-5-mini`, `gpt-5-nano` (GPT-5 family)
|
|
626
331
|
|
|
627
|
-
|
|
332
|
+
**Embeddings:**
|
|
333
|
+
- `text-embedding-3-small`, `text-embedding-3-large`
|
|
628
334
|
|
|
629
|
-
|
|
335
|
+
**Azure OpenAI:**
|
|
336
|
+
- Works with any Azure deployment (deployment names automatically resolved)
|
|
630
337
|
|
|
631
|
-
|
|
632
|
-
- [Revenium AI Models API](https://revenium.readme.io/v2.0.0/reference/get_ai_model)
|
|
338
|
+
For the complete model list and latest specifications, see the [OpenAI Models Documentation](https://platform.openai.com/docs/models).
|
|
633
339
|
|
|
634
|
-
|
|
340
|
+
For cost tracking across providers, see the [Revenium Model Catalog](https://revenium.readme.io/v2.0.0/reference/get_ai_model).
|
|
635
341
|
|
|
636
342
|
### API Support Matrix
|
|
637
343
|
|
|
@@ -12,7 +12,7 @@ exports.hasAzureConfigInEnv = hasAzureConfigInEnv;
|
|
|
12
12
|
/**
|
|
13
13
|
* Default Revenium base URL for consistency with other middleware
|
|
14
14
|
*/
|
|
15
|
-
const DEFAULT_REVENIUM_BASE_URL = 'https://api.revenium.
|
|
15
|
+
const DEFAULT_REVENIUM_BASE_URL = 'https://api.revenium.ai';
|
|
16
16
|
/**
|
|
17
17
|
* Load configuration from environment variables
|
|
18
18
|
*/
|
|
@@ -19,7 +19,8 @@ const validator_js_1 = require("./validator.js");
|
|
|
19
19
|
*/
|
|
20
20
|
exports.defaultLogger = {
|
|
21
21
|
debug: (message, ...args) => {
|
|
22
|
-
|
|
22
|
+
// Check both config.debug and environment variable
|
|
23
|
+
if (globalConfig?.debug || process.env.REVENIUM_DEBUG === 'true') {
|
|
23
24
|
console.debug(`[Revenium Debug] ${message}`, ...args);
|
|
24
25
|
}
|
|
25
26
|
},
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"manager.js","sourceRoot":"","sources":["../../../../src/core/config/manager.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;
|
|
1
|
+
{"version":3,"file":"manager.js","sourceRoot":"","sources":["../../../../src/core/config/manager.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAoCH,8BAEC;AAKD,8BAQC;AAKD,8BAEC;AAKD,8BAEC;AAKD,4CAmBC;AAtFD,2CAAqE;AACrE,iDAAgD;AAEhD;;GAEG;AACU,QAAA,aAAa,GAAW;IACnC,KAAK,EAAE,CAAC,OAAe,EAAE,GAAG,IAAe,EAAE,EAAE;QAC7C,mDAAmD;QACnD,IAAI,YAAY,EAAE,KAAK,IAAI,OAAO,CAAC,GAAG,CAAC,cAAc,KAAK,MAAM,EAAE,CAAC;YACjE,OAAO,CAAC,KAAK,CAAC,oBAAoB,OAAO,EAAE,EAAE,GAAG,IAAI,CAAC,CAAC;QACxD,CAAC;IACH,CAAC;IACD,IAAI,EAAE,CAAC,OAAe,EAAE,GAAG,IAAe,EAAE,EAAE;QAC5C,OAAO,CAAC,IAAI,CAAC,cAAc,OAAO,EAAE,EAAE,GAAG,IAAI,CAAC,CAAC;IACjD,CAAC;IACD,IAAI,EAAE,CAAC,OAAe,EAAE,GAAG,IAAe,EAAE,EAAE;QAC5C,OAAO,CAAC,IAAI,CAAC,sBAAsB,OAAO,EAAE,EAAE,GAAG,IAAI,CAAC,CAAC;IACzD,CAAC;IACD,KAAK,EAAE,CAAC,OAAe,EAAE,GAAG,IAAe,EAAE,EAAE;QAC7C,OAAO,CAAC,KAAK,CAAC,oBAAoB,OAAO,EAAE,EAAE,GAAG,IAAI,CAAC,CAAC;IACxD,CAAC;CACF,CAAC;AAEF;;GAEG;AACH,IAAI,YAAY,GAA0B,IAAI,CAAC;AAC/C,IAAI,YAAY,GAAW,qBAAa,CAAC;AAEzC;;GAEG;AACH,SAAgB,SAAS;IACvB,OAAO,YAAY,CAAC;AACtB,CAAC;AAED;;GAEG;AACH,SAAgB,SAAS,CAAC,MAAsB;IAC9C,IAAA,6BAAc,EAAC,MAAM,CAAC,CAAC;IACvB,YAAY,GAAG,MAAM,CAAC;IACtB,YAAY,CAAC,KAAK,CAAC,gCAAgC,EAAE;QACnD,OAAO,EAAE,MAAM,CAAC,eAAe;QAC/B,SAAS,EAAE,CAAC,CAAC,MAAM,CAAC,cAAc;QAClC,YAAY,EAAE,CAAC,CAAC,MAAM,CAAC,YAAY;KACpC,CAAC,CAAC;AACL,CAAC;AAED;;GAEG;AACH,SAAgB,SAAS;IACvB,OAAO,YAAY,CAAC;AACtB,CAAC;AAED;;GAEG;AACH,SAAgB,SAAS,CAAC,MAAc;IACtC,YAAY,GAAG,MAAM,CAAC;AACxB,CAAC;AAED;;GAEG;AACH,SAAgB,gBAAgB;IAC9B,MAAM,SAAS,GAAG,IAAA,6BAAiB,GAAE,CAAC;IACtC,IAAI,SAAS,EAAE,CAAC;QACd,IAAI,CAAC;YACH,SAAS,CAAC,SAAS,CAAC,CAAC;YACrB,YAAY,CAAC,KAAK,CAAC,4DAA4D,CAAC,CAAC;YAEjF,8CAA8C;YAC9C,IAAI,IAAA,+BAAmB,GAAE,EAAE,CAAC;gBAC1B,YAAY,CAAC,KAAK,CAAC,oDAAoD,CAAC,CAAC;YAC3E,CAAC;YAED,OAAO,IAAI,CAAC;QACd,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,YAAY,CAAC,KAAK,CAAC,8CAA8C,EAAE,KAAK,CAAC,CAAC;YAC1E,OAAO,KAAK,CAAC;QACf,CAAC;IACH,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC"}
|
|
@@ -129,12 +129,12 @@ function getProviderMetadata(providerInfo) {
|
|
|
129
129
|
if (providerInfo.isAzure) {
|
|
130
130
|
return {
|
|
131
131
|
provider: 'Azure',
|
|
132
|
-
modelSource: '
|
|
132
|
+
modelSource: 'AZURE_OPENAI',
|
|
133
133
|
};
|
|
134
134
|
}
|
|
135
135
|
return {
|
|
136
|
-
provider: '
|
|
137
|
-
modelSource: 'OPENAI',
|
|
136
|
+
provider: 'OpenAI',
|
|
137
|
+
modelSource: 'OPENAI', // Provider name when calling directly per spec
|
|
138
138
|
};
|
|
139
139
|
}
|
|
140
140
|
//# sourceMappingURL=detector.js.map
|