@revenium/perplexity 2.0.7 → 2.0.9
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 +23 -0
- package/CHANGELOG.md +99 -113
- package/LICENSE +21 -21
- package/README.md +528 -376
- package/SECURITY.md +34 -34
- package/dist/cjs/core/config/loader.js +16 -0
- package/dist/cjs/core/config/loader.js.map +1 -1
- package/dist/cjs/core/config/manager.js +9 -0
- package/dist/cjs/core/config/manager.js.map +1 -1
- package/dist/cjs/core/tracking/api-client.js +2 -0
- package/dist/cjs/core/tracking/api-client.js.map +1 -1
- package/dist/cjs/core/tracking/payload-builder.js +20 -1
- package/dist/cjs/core/tracking/payload-builder.js.map +1 -1
- package/dist/cjs/core/tracking/usage-tracker.js +1 -1
- package/dist/cjs/core/tracking/usage-tracker.js.map +1 -1
- package/dist/cjs/utils/summary-printer.js +237 -0
- package/dist/cjs/utils/summary-printer.js.map +1 -0
- package/dist/cjs/utils/trace-fields.js +136 -0
- package/dist/cjs/utils/trace-fields.js.map +1 -0
- package/dist/esm/core/config/loader.js +16 -0
- package/dist/esm/core/config/loader.js.map +1 -1
- package/dist/esm/core/config/manager.js +9 -0
- package/dist/esm/core/config/manager.js.map +1 -1
- package/dist/esm/core/tracking/api-client.js +2 -0
- package/dist/esm/core/tracking/api-client.js.map +1 -1
- package/dist/esm/core/tracking/payload-builder.js +20 -1
- package/dist/esm/core/tracking/payload-builder.js.map +1 -1
- package/dist/esm/core/tracking/usage-tracker.js +1 -1
- package/dist/esm/core/tracking/usage-tracker.js.map +1 -1
- package/dist/esm/utils/summary-printer.js +233 -0
- package/dist/esm/utils/summary-printer.js.map +1 -0
- package/dist/esm/utils/trace-fields.js +121 -0
- package/dist/esm/utils/trace-fields.js.map +1 -0
- package/dist/types/core/config/loader.d.ts.map +1 -1
- package/dist/types/core/config/manager.d.ts.map +1 -1
- package/dist/types/core/tracking/api-client.d.ts.map +1 -1
- package/dist/types/core/tracking/payload-builder.d.ts +1 -1
- package/dist/types/core/tracking/payload-builder.d.ts.map +1 -1
- package/dist/types/types/index.d.ts +13 -0
- package/dist/types/types/index.d.ts.map +1 -1
- package/dist/types/utils/summary-printer.d.ts +23 -0
- package/dist/types/utils/summary-printer.d.ts.map +1 -0
- package/dist/types/utils/trace-fields.d.ts +11 -0
- package/dist/types/utils/trace-fields.d.ts.map +1 -0
- package/examples/README.md +274 -230
- package/examples/advanced.ts +123 -123
- package/examples/basic.ts +45 -45
- package/examples/getting_started.ts +41 -41
- package/examples/metadata.ts +68 -68
- package/examples/stream.ts +53 -53
- package/package.json +82 -72
package/README.md
CHANGED
|
@@ -1,376 +1,528 @@
|
|
|
1
|
-
# Revenium Middleware for Perplexity
|
|
2
|
-
|
|
3
|
-
A lightweight, production-ready middleware that adds **Revenium metering and tracking** to Perplexity AI API calls.
|
|
4
|
-
|
|
5
|
-
[](https://www.npmjs.com/package/@revenium/perplexity)
|
|
6
|
-
[](https://nodejs.org/)
|
|
7
|
-
[](https://docs.revenium.io)
|
|
8
|
-
[](https://www.revenium.ai)
|
|
9
|
-
[](https://opensource.org/licenses/MIT)
|
|
10
|
-
|
|
11
|
-
## Features
|
|
12
|
-
|
|
13
|
-
- **
|
|
14
|
-
- **
|
|
15
|
-
- **
|
|
16
|
-
- **
|
|
17
|
-
- **
|
|
18
|
-
- **
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
```
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
```
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
```
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
- **
|
|
187
|
-
- **
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
- **
|
|
194
|
-
- **
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
- **
|
|
201
|
-
- **
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
|
220
|
-
|
|
|
221
|
-
|
|
|
222
|
-
| `
|
|
223
|
-
| `
|
|
224
|
-
| `
|
|
225
|
-
| `
|
|
226
|
-
| `
|
|
227
|
-
| `
|
|
228
|
-
| `
|
|
229
|
-
|
|
|
230
|
-
| `
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
###
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
1
|
+
# Revenium Middleware for Perplexity
|
|
2
|
+
|
|
3
|
+
A lightweight, production-ready middleware that adds **Revenium metering and tracking** to Perplexity AI API calls.
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/@revenium/perplexity)
|
|
6
|
+
[](https://nodejs.org/)
|
|
7
|
+
[](https://docs.revenium.io)
|
|
8
|
+
[](https://www.revenium.ai)
|
|
9
|
+
[](https://opensource.org/licenses/MIT)
|
|
10
|
+
|
|
11
|
+
## Features
|
|
12
|
+
|
|
13
|
+
- **Automatic Metering** - Tracks all API calls with detailed usage metrics
|
|
14
|
+
- **Streaming Support** - Full support for streaming responses
|
|
15
|
+
- **TypeScript First** - Built with TypeScript, includes full type definitions
|
|
16
|
+
- **Multi-Format** - Supports both ESM and CommonJS
|
|
17
|
+
- **Custom Metadata** - Add custom tracking metadata to any request
|
|
18
|
+
- **Production Ready** - Battle-tested and optimized for production use
|
|
19
|
+
|
|
20
|
+
## Getting Started
|
|
21
|
+
|
|
22
|
+
### Quick Start
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
npm install @revenium/perplexity
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
**Note:** The `dotenv` package is optional. The middleware automatically loads `.env` files via `Initialize()`.
|
|
29
|
+
|
|
30
|
+
For complete setup instructions, TypeScript patterns, and usage examples, see [examples/README.md](https://github.com/revenium/revenium-middleware-perplexity-node/blob/HEAD/examples/README.md).
|
|
31
|
+
|
|
32
|
+
### Step-by-Step Guide
|
|
33
|
+
|
|
34
|
+
The following guide walks you through creating a new project from scratch:
|
|
35
|
+
|
|
36
|
+
#### Step 1: Create Your Project
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
# Create project directory
|
|
40
|
+
mkdir my-perplexity-project
|
|
41
|
+
cd my-perplexity-project
|
|
42
|
+
|
|
43
|
+
# Initialize npm project
|
|
44
|
+
npm init -y
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
#### Step 2: Install Dependencies
|
|
48
|
+
|
|
49
|
+
```bash
|
|
50
|
+
# Install Revenium middleware
|
|
51
|
+
npm install @revenium/perplexity
|
|
52
|
+
|
|
53
|
+
# For TypeScript projects (optional)
|
|
54
|
+
npm install -D typescript tsx @types/node
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
#### Step 3: Setup Environment Variables
|
|
58
|
+
|
|
59
|
+
Create a `.env` file in your project root:
|
|
60
|
+
|
|
61
|
+
```bash
|
|
62
|
+
# Create .env file
|
|
63
|
+
echo. > .env # On Windows (CMD)
|
|
64
|
+
touch .env # On Mac/Linux
|
|
65
|
+
# OR
|
|
66
|
+
# PowerShell
|
|
67
|
+
New-Item -Path .env -ItemType File
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
Copy and paste the following into `.env`:
|
|
71
|
+
|
|
72
|
+
```env
|
|
73
|
+
# Perplexity Configuration
|
|
74
|
+
PERPLEXITY_API_KEY=pplx_your_perplexity_api_key
|
|
75
|
+
|
|
76
|
+
# Revenium Configuration
|
|
77
|
+
REVENIUM_METERING_API_KEY=hak_your_revenium_api_key
|
|
78
|
+
|
|
79
|
+
# Optional: For development/testing (defaults to https://api.revenium.ai)
|
|
80
|
+
# REVENIUM_METERING_BASE_URL=https://api.revenium.ai
|
|
81
|
+
|
|
82
|
+
# Optional: Perplexity API base URL (defaults to https://api.perplexity.ai)
|
|
83
|
+
# PERPLEXITY_API_BASE_URL=https://api.perplexity.ai
|
|
84
|
+
|
|
85
|
+
# Optional: Enable debug logging
|
|
86
|
+
# REVENIUM_DEBUG=false
|
|
87
|
+
|
|
88
|
+
# Optional: Terminal cost/metrics summary
|
|
89
|
+
# REVENIUM_PRINT_SUMMARY=true # or 'human' or 'json'
|
|
90
|
+
# REVENIUM_TEAM_ID=your_team_id # Required for cost retrieval
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
**Replace the placeholder values with your actual keys!**
|
|
94
|
+
|
|
95
|
+
For a complete list of all available environment variables, see the [Configuration Options](#configuration-options) section below.
|
|
96
|
+
|
|
97
|
+
#### Step 4: Implement in Your Code
|
|
98
|
+
|
|
99
|
+
Use the examples as reference for implementing the middleware in your project. See [examples/README.md](https://github.com/revenium/revenium-middleware-perplexity-node/blob/HEAD/examples/README.md) for complete implementation examples including:
|
|
100
|
+
|
|
101
|
+
- How to initialize the middleware with your configuration
|
|
102
|
+
- Making API calls with automatic metering
|
|
103
|
+
- Handling streaming responses
|
|
104
|
+
- Adding custom metadata to track business context
|
|
105
|
+
|
|
106
|
+
**Note for ESM projects:** If you get a "Cannot use import statement outside a module" error, make sure your `package.json` includes `"type": "module"`:
|
|
107
|
+
|
|
108
|
+
```json
|
|
109
|
+
{
|
|
110
|
+
"name": "my-perplexity-project",
|
|
111
|
+
"version": "1.0.0",
|
|
112
|
+
"type": "module",
|
|
113
|
+
"dependencies": {
|
|
114
|
+
"@revenium/perplexity": "^2.0.0"
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
---
|
|
120
|
+
|
|
121
|
+
## Running Examples from Cloned Repository
|
|
122
|
+
|
|
123
|
+
If you've cloned this repository from GitHub and want to **run the included examples** to see how the middleware works (without modifying the middleware source code):
|
|
124
|
+
|
|
125
|
+
### Setup
|
|
126
|
+
|
|
127
|
+
```bash
|
|
128
|
+
# Install dependencies
|
|
129
|
+
npm install
|
|
130
|
+
|
|
131
|
+
# Build the package
|
|
132
|
+
npm run build
|
|
133
|
+
|
|
134
|
+
# Configure environment variables
|
|
135
|
+
cp .env.example .env # On Mac/Linux
|
|
136
|
+
copy .env.example .env # On Windows (CMD)
|
|
137
|
+
# OR PowerShell
|
|
138
|
+
Copy-Item .env.example .env
|
|
139
|
+
|
|
140
|
+
# Edit .env with your API keys
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
### Run Examples
|
|
144
|
+
|
|
145
|
+
**Using npm scripts:**
|
|
146
|
+
|
|
147
|
+
```bash
|
|
148
|
+
npm run example:getting-started # Getting started example
|
|
149
|
+
npm run example:basic # Basic chat completion
|
|
150
|
+
npm run example:stream # Streaming response
|
|
151
|
+
npm run example:metadata # Custom metadata
|
|
152
|
+
npm run example:advanced # Advanced features
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
**Or use npx tsx directly:**
|
|
156
|
+
|
|
157
|
+
```bash
|
|
158
|
+
npx tsx examples/getting_started.ts
|
|
159
|
+
npx tsx examples/basic.ts
|
|
160
|
+
npx tsx examples/stream.ts
|
|
161
|
+
npx tsx examples/metadata.ts
|
|
162
|
+
npx tsx examples/advanced.ts
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
For detailed information about each example, see [examples/README.md](examples/README.md).
|
|
166
|
+
|
|
167
|
+
---
|
|
168
|
+
|
|
169
|
+
## Local Development and Contributing
|
|
170
|
+
|
|
171
|
+
For information on modifying the middleware source code, development workflow, and contributing to the project, see:
|
|
172
|
+
|
|
173
|
+
- **[DEVELOPMENT.md](DEVELOPMENT.md)** - Complete development guide, build system, and testing
|
|
174
|
+
- **[CONTRIBUTING.md](CONTRIBUTING.md)** - Contribution guidelines and process
|
|
175
|
+
|
|
176
|
+
---
|
|
177
|
+
|
|
178
|
+
## What Gets Tracked
|
|
179
|
+
|
|
180
|
+
The middleware automatically captures comprehensive usage data:
|
|
181
|
+
|
|
182
|
+
### **Usage Metrics**
|
|
183
|
+
|
|
184
|
+
- **Token Counts** - Input tokens, output tokens, total tokens
|
|
185
|
+
- **Model Information** - Model name, provider (Perplexity)
|
|
186
|
+
- **Request Timing** - Request duration, response time
|
|
187
|
+
- **Cost Calculation** - Estimated costs based on current pricing
|
|
188
|
+
|
|
189
|
+
### **Business Context (Optional)**
|
|
190
|
+
|
|
191
|
+
- **User Tracking** - Subscriber ID, email, credentials
|
|
192
|
+
- **Organization Data** - Organization ID, subscription ID, product ID
|
|
193
|
+
- **Task Classification** - Task type, agent identifier, trace ID
|
|
194
|
+
- **Quality Metrics** - Response quality scores, task identifiers
|
|
195
|
+
|
|
196
|
+
### **Technical Details**
|
|
197
|
+
|
|
198
|
+
- **API Endpoints** - Chat completions
|
|
199
|
+
- **Request Types** - Streaming vs non-streaming
|
|
200
|
+
- **Error Tracking** - Failed requests, error types, retry attempts
|
|
201
|
+
- **Environment Info** - Development vs production usage
|
|
202
|
+
|
|
203
|
+
## Metadata Fields
|
|
204
|
+
|
|
205
|
+
The following table shows all fields this middleware sends to the Revenium API:
|
|
206
|
+
|
|
207
|
+
| Field | Type | Required | Description |
|
|
208
|
+
| ----------------------- | ------- | --------- | -------------------------------------------------- |
|
|
209
|
+
| **Core Fields** | | | |
|
|
210
|
+
| `model` | string | Yes | Perplexity model name (e.g., "sonar-pro") |
|
|
211
|
+
| `provider` | string | Yes | Always "Perplexity" |
|
|
212
|
+
| `inputTokenCount` | integer | Yes | Number of input tokens consumed |
|
|
213
|
+
| `outputTokenCount` | integer | Yes | Number of output tokens generated |
|
|
214
|
+
| `totalTokenCount` | integer | Yes | Total tokens (input + output) |
|
|
215
|
+
| `requestDuration` | integer | Yes | Request duration in milliseconds |
|
|
216
|
+
| **Timing** | | | |
|
|
217
|
+
| `requestTime` | string | Auto | ISO 8601 timestamp when request started |
|
|
218
|
+
| `responseTime` | string | Auto | ISO 8601 timestamp when response completed |
|
|
219
|
+
| `completionStartTime` | string | Auto | ISO 8601 timestamp when completion started |
|
|
220
|
+
| `timeToFirstToken` | integer | Streaming | Time to first token in ms (streaming only) |
|
|
221
|
+
| **Request Details** | | | |
|
|
222
|
+
| `transactionId` | string | Auto | Unique transaction identifier |
|
|
223
|
+
| `operationType` | string | Auto | Always "CHAT" for chat completions |
|
|
224
|
+
| `stopReason` | string | Auto | Completion finish reason ("END", "STOP", etc.) |
|
|
225
|
+
| `isStreamed` | boolean | Auto | Whether response was streamed |
|
|
226
|
+
| `costType` | string | Auto | Always "AI" |
|
|
227
|
+
| `modelSource` | string | Auto | Always "PERPLEXITY" |
|
|
228
|
+
| `middlewareSource` | string | Auto | Always "revenium-perplexity-node" |
|
|
229
|
+
| **Cost Information** | | | |
|
|
230
|
+
| `inputTokenCost` | number | Optional | Cost for input tokens (if provided by Perplexity) |
|
|
231
|
+
| `outputTokenCost` | number | Optional | Cost for output tokens (if provided by Perplexity) |
|
|
232
|
+
| `totalCost` | number | Optional | Total cost (if provided by Perplexity) |
|
|
233
|
+
| **Business Context** | | | |
|
|
234
|
+
| `organizationId` | string | Optional | Your organization identifier |
|
|
235
|
+
| `productId` | string | Optional | Your product identifier |
|
|
236
|
+
| `subscriptionId` | string | Optional | Your subscription identifier |
|
|
237
|
+
| `taskType` | string | Optional | Type of AI task (e.g., "chat", "research") |
|
|
238
|
+
| `traceId` | string | Optional | Session or conversation tracking ID |
|
|
239
|
+
| `agent` | string | Optional | AI agent or bot identifier |
|
|
240
|
+
| `responseQualityScore` | number | Optional | Custom quality rating (0.0-1.0) |
|
|
241
|
+
| `subscriber.id` | string | Optional | User identifier |
|
|
242
|
+
| `subscriber.email` | string | Optional | User email address |
|
|
243
|
+
| `subscriber.credential` | object | Optional | Authentication credential (name, value) |
|
|
244
|
+
|
|
245
|
+
**Notes:**
|
|
246
|
+
|
|
247
|
+
- **Required** fields are always sent with every request
|
|
248
|
+
- **Auto** fields are automatically populated by the middleware
|
|
249
|
+
- **Optional** fields are only sent if you provide them via `usageMetadata`
|
|
250
|
+
- **Streaming** fields are only sent for streaming requests
|
|
251
|
+
|
|
252
|
+
**Reference:**
|
|
253
|
+
|
|
254
|
+
- [API Reference](https://revenium.readme.io/reference/meter_ai_completion) - Complete metadata field documentation
|
|
255
|
+
|
|
256
|
+
## Trace Visualization Fields
|
|
257
|
+
|
|
258
|
+
The middleware automatically captures trace visualization fields for distributed tracing and analytics:
|
|
259
|
+
|
|
260
|
+
| Field | Type | Description | Environment Variable |
|
|
261
|
+
| --------------------- | ------ | ------------------------------------------------------------------------------- | ---------------------------------- |
|
|
262
|
+
| `environment` | string | Deployment environment (production, staging, development) | `REVENIUM_ENVIRONMENT`, `NODE_ENV` |
|
|
263
|
+
| `operationType` | string | Operation classification (CHAT, EMBED, etc.) - automatically detected | N/A (auto-detected) |
|
|
264
|
+
| `operationSubtype` | string | Additional detail (function_call, etc.) - automatically detected | N/A (auto-detected) |
|
|
265
|
+
| `retryNumber` | number | Retry attempt number (0 for first attempt, 1+ for retries) | `REVENIUM_RETRY_NUMBER` |
|
|
266
|
+
| `parentTransactionId` | string | Parent transaction reference for distributed tracing | `REVENIUM_PARENT_TRANSACTION_ID` |
|
|
267
|
+
| `transactionName` | string | Human-friendly operation label | `REVENIUM_TRANSACTION_NAME` |
|
|
268
|
+
| `region` | string | Cloud region (us-east-1, etc.) - auto-detected from AWS/Azure/GCP | `AWS_REGION`, `REVENIUM_REGION` |
|
|
269
|
+
| `credentialAlias` | string | Human-readable credential name | `REVENIUM_CREDENTIAL_ALIAS` |
|
|
270
|
+
| `traceType` | string | Categorical identifier (alphanumeric, hyphens, underscores only, max 128 chars) | `REVENIUM_TRACE_TYPE` |
|
|
271
|
+
| `traceName` | string | Human-readable label for trace instances (max 256 chars) | `REVENIUM_TRACE_NAME` |
|
|
272
|
+
|
|
273
|
+
**All trace visualization fields are optional.** The middleware will automatically detect and populate these fields when possible.
|
|
274
|
+
|
|
275
|
+
### Example Configuration
|
|
276
|
+
|
|
277
|
+
```env
|
|
278
|
+
REVENIUM_ENVIRONMENT=production
|
|
279
|
+
REVENIUM_REGION=us-east-1
|
|
280
|
+
REVENIUM_CREDENTIAL_ALIAS=Perplexity Production Key
|
|
281
|
+
REVENIUM_TRACE_TYPE=customer_support
|
|
282
|
+
REVENIUM_TRACE_NAME=Support Ticket #12345
|
|
283
|
+
REVENIUM_PARENT_TRANSACTION_ID=parent-txn-123
|
|
284
|
+
REVENIUM_TRANSACTION_NAME=Answer Customer Question
|
|
285
|
+
REVENIUM_RETRY_NUMBER=0
|
|
286
|
+
```
|
|
287
|
+
|
|
288
|
+
For a complete example, see [`.env.example`](https://github.com/revenium/revenium-middleware-perplexity-node/blob/HEAD/.env.example).
|
|
289
|
+
|
|
290
|
+
## Terminal Cost/Metrics Summary
|
|
291
|
+
|
|
292
|
+
The middleware can print a cost and metrics summary to your terminal after each API request. This is useful for development and debugging.
|
|
293
|
+
|
|
294
|
+
### Configuration
|
|
295
|
+
|
|
296
|
+
Enable terminal summary output using environment variables or programmatic configuration:
|
|
297
|
+
|
|
298
|
+
**Environment Variables:**
|
|
299
|
+
|
|
300
|
+
```bash
|
|
301
|
+
# Enable human-readable summary (default format)
|
|
302
|
+
export REVENIUM_PRINT_SUMMARY=true
|
|
303
|
+
|
|
304
|
+
# Or specify format explicitly
|
|
305
|
+
export REVENIUM_PRINT_SUMMARY=human # Human-readable format
|
|
306
|
+
export REVENIUM_PRINT_SUMMARY=json # JSON format for log parsing
|
|
307
|
+
|
|
308
|
+
# Optional: Set team ID to fetch cost data
|
|
309
|
+
export REVENIUM_TEAM_ID=your_team_id
|
|
310
|
+
```
|
|
311
|
+
|
|
312
|
+
**Programmatic Configuration:**
|
|
313
|
+
|
|
314
|
+
```typescript
|
|
315
|
+
import { Initialize } from "@revenium/perplexity";
|
|
316
|
+
|
|
317
|
+
Initialize({
|
|
318
|
+
reveniumApiKey: "hak_your_api_key",
|
|
319
|
+
reveniumBaseUrl: "https://api.revenium.ai",
|
|
320
|
+
perplexityApiKey: "pplx_your_api_key",
|
|
321
|
+
printSummary: true, // or 'human' or 'json'
|
|
322
|
+
teamId: "your_team_id", // Optional: for cost retrieval
|
|
323
|
+
});
|
|
324
|
+
```
|
|
325
|
+
|
|
326
|
+
### Output Formats
|
|
327
|
+
|
|
328
|
+
**Human-readable format** (`printSummary: true` or `printSummary: 'human'`):
|
|
329
|
+
|
|
330
|
+
```
|
|
331
|
+
============================================================
|
|
332
|
+
📊 REVENIUM USAGE SUMMARY
|
|
333
|
+
============================================================
|
|
334
|
+
🤖 Model: sonar-pro
|
|
335
|
+
🏢 Provider: Perplexity
|
|
336
|
+
⏱️ Duration: 1.23s
|
|
337
|
+
|
|
338
|
+
💬 Token Usage:
|
|
339
|
+
📥 Input Tokens: 150
|
|
340
|
+
📤 Output Tokens: 75
|
|
341
|
+
📊 Total Tokens: 225
|
|
342
|
+
|
|
343
|
+
💰 Cost: $0.004500
|
|
344
|
+
🔖 Trace ID: trace-abc-123
|
|
345
|
+
============================================================
|
|
346
|
+
```
|
|
347
|
+
|
|
348
|
+
**JSON format** (`printSummary: 'json'`):
|
|
349
|
+
|
|
350
|
+
```json
|
|
351
|
+
{
|
|
352
|
+
"model": "sonar-pro",
|
|
353
|
+
"provider": "Perplexity",
|
|
354
|
+
"durationSeconds": 1.23,
|
|
355
|
+
"inputTokenCount": 150,
|
|
356
|
+
"outputTokenCount": 75,
|
|
357
|
+
"totalTokenCount": 225,
|
|
358
|
+
"cost": 0.0045,
|
|
359
|
+
"traceId": "trace-abc-123"
|
|
360
|
+
}
|
|
361
|
+
```
|
|
362
|
+
|
|
363
|
+
### Cost Retrieval
|
|
364
|
+
|
|
365
|
+
- **Without `teamId`**: Shows token counts and duration, displays hint to set `REVENIUM_TEAM_ID`
|
|
366
|
+
- **With `teamId`**: Fetches actual cost from Revenium API with automatic retry logic
|
|
367
|
+
- **Cost pending**: Shows "(pending aggregation)" if cost data isn't available yet
|
|
368
|
+
- **Fire-and-forget**: Never blocks your application, even if cost fetch fails
|
|
369
|
+
|
|
370
|
+
## API Overview
|
|
371
|
+
|
|
372
|
+
- **`Initialize(config?)`** - Initialize the middleware (from environment or explicit config)
|
|
373
|
+
- **`GetClient()`** - Get the global Revenium client instance
|
|
374
|
+
- **`Configure(config)`** - Alias for `Initialize()` for programmatic configuration
|
|
375
|
+
- **`IsInitialized()`** - Check if the middleware is initialized
|
|
376
|
+
- **`Reset()`** - Reset the global client (useful for testing)
|
|
377
|
+
|
|
378
|
+
**For complete API documentation and usage examples, see [`examples/README.md`](https://github.com/revenium/revenium-middleware-perplexity-node/blob/HEAD/examples/README.md).**
|
|
379
|
+
|
|
380
|
+
## Configuration Options
|
|
381
|
+
|
|
382
|
+
### Environment Variables
|
|
383
|
+
|
|
384
|
+
For a complete list of all available environment variables with examples, see [`.env.example`](https://github.com/revenium/revenium-middleware-perplexity-node/blob/HEAD/.env.example).
|
|
385
|
+
|
|
386
|
+
## Examples
|
|
387
|
+
|
|
388
|
+
The package includes comprehensive examples in the [`examples/`](https://github.com/revenium/revenium-middleware-perplexity-node/blob/HEAD/examples/) directory.
|
|
389
|
+
|
|
390
|
+
## Project Structure
|
|
391
|
+
|
|
392
|
+
```
|
|
393
|
+
revenium-middleware-perplexity-node/
|
|
394
|
+
├── src/
|
|
395
|
+
│ ├── core/
|
|
396
|
+
│ │ ├── client/ # Client manager (Initialize/GetClient)
|
|
397
|
+
│ │ ├── config/ # Configuration management
|
|
398
|
+
│ │ ├── middleware/ # Perplexity API middleware wrapper
|
|
399
|
+
│ │ ├── providers/ # Provider detection
|
|
400
|
+
│ │ └── tracking/ # Metering and tracking
|
|
401
|
+
│ ├── types/ # TypeScript type definitions
|
|
402
|
+
│ ├── utils/ # Utility functions
|
|
403
|
+
│ └── index.ts # Main entry point
|
|
404
|
+
├── examples/ # TypeScript examples
|
|
405
|
+
├── package.json
|
|
406
|
+
├── tsconfig.json
|
|
407
|
+
└── README.md
|
|
408
|
+
```
|
|
409
|
+
|
|
410
|
+
## How It Works
|
|
411
|
+
|
|
412
|
+
1. **Initialize**: Call `Initialize()` to set up the middleware with your configuration
|
|
413
|
+
2. **Get Client**: Call `GetClient()` to get a wrapped Perplexity client instance
|
|
414
|
+
3. **Make Requests**: Use the client normally - all requests are automatically tracked
|
|
415
|
+
4. **Async Tracking**: Usage data is sent to Revenium in the background (fire-and-forget)
|
|
416
|
+
5. **Transparent Response**: Original Perplexity responses are returned unchanged
|
|
417
|
+
|
|
418
|
+
The middleware never blocks your application - if Revenium tracking fails, your Perplexity requests continue normally.
|
|
419
|
+
|
|
420
|
+
**Supported APIs:**
|
|
421
|
+
|
|
422
|
+
- Chat Completions API (`client.chat().completions().create()`)
|
|
423
|
+
- Streaming API (`client.chat().completions().createStreaming()`)
|
|
424
|
+
|
|
425
|
+
## Troubleshooting
|
|
426
|
+
|
|
427
|
+
### Common Issues
|
|
428
|
+
|
|
429
|
+
**No tracking data appears:**
|
|
430
|
+
|
|
431
|
+
1. Verify environment variables are set correctly in `.env`
|
|
432
|
+
2. Enable debug logging by setting `REVENIUM_DEBUG=true` in `.env`
|
|
433
|
+
3. Check console for `[Revenium]` log messages
|
|
434
|
+
4. Verify your `REVENIUM_METERING_API_KEY` is valid
|
|
435
|
+
|
|
436
|
+
**Client not initialized error:**
|
|
437
|
+
|
|
438
|
+
- Make sure you call `Initialize()` before `GetClient()`
|
|
439
|
+
- Check that your `.env` file is in the project root
|
|
440
|
+
- Verify `REVENIUM_METERING_API_KEY` is set
|
|
441
|
+
|
|
442
|
+
**Perplexity API errors:**
|
|
443
|
+
|
|
444
|
+
- Verify `PERPLEXITY_API_KEY` is set correctly
|
|
445
|
+
- Check that your API key starts with `pplx-`
|
|
446
|
+
- Ensure you're using a valid model name
|
|
447
|
+
|
|
448
|
+
**Windows-specific issues:**
|
|
449
|
+
|
|
450
|
+
If you're developing on Windows and encounter build errors with `npm run clean`:
|
|
451
|
+
|
|
452
|
+
- The `clean` script uses `rm -rf` which may not work in Windows CMD
|
|
453
|
+
- Use PowerShell or Git Bash instead
|
|
454
|
+
- Or manually delete the `dist` folder before building
|
|
455
|
+
- Alternatively, install `rimraf` globally: `npm install -g rimraf` and update the script to use `rimraf dist`
|
|
456
|
+
|
|
457
|
+
### Debug Mode
|
|
458
|
+
|
|
459
|
+
Enable detailed logging by adding to your `.env`:
|
|
460
|
+
|
|
461
|
+
```env
|
|
462
|
+
REVENIUM_DEBUG=true
|
|
463
|
+
```
|
|
464
|
+
|
|
465
|
+
### Getting Help
|
|
466
|
+
|
|
467
|
+
If issues persist:
|
|
468
|
+
|
|
469
|
+
1. Enable debug logging (`REVENIUM_DEBUG=true`)
|
|
470
|
+
2. Check the [`examples/`](https://github.com/revenium/revenium-middleware-perplexity-node/blob/HEAD/examples/) directory for working examples
|
|
471
|
+
3. Review [`examples/README.md`](https://github.com/revenium/revenium-middleware-perplexity-node/blob/HEAD/examples/README.md) for detailed setup instructions
|
|
472
|
+
4. Contact support@revenium.io with debug logs
|
|
473
|
+
|
|
474
|
+
## Supported Models
|
|
475
|
+
|
|
476
|
+
This middleware works with any Perplexity model. For the complete model list, see the [Perplexity Models Documentation](https://docs.perplexity.ai/getting-started/models).
|
|
477
|
+
|
|
478
|
+
### API Support Matrix
|
|
479
|
+
|
|
480
|
+
The following table shows what has been tested and verified with working examples:
|
|
481
|
+
|
|
482
|
+
| Feature | Chat Completions | Streaming |
|
|
483
|
+
| --------------------- | ---------------- | --------- |
|
|
484
|
+
| **Basic Usage** | Yes | Yes |
|
|
485
|
+
| **Metadata Tracking** | Yes | Yes |
|
|
486
|
+
| **Token Counting** | Yes | Yes |
|
|
487
|
+
|
|
488
|
+
**Note:** "Yes" = Tested with working examples in [`examples/`](https://github.com/revenium/revenium-middleware-perplexity-node/blob/HEAD/examples/) directory
|
|
489
|
+
|
|
490
|
+
## Requirements
|
|
491
|
+
|
|
492
|
+
- Node.js 20+
|
|
493
|
+
- TypeScript 5.0+ (for TypeScript projects)
|
|
494
|
+
- Revenium API key
|
|
495
|
+
- Perplexity API key
|
|
496
|
+
|
|
497
|
+
## Documentation
|
|
498
|
+
|
|
499
|
+
For detailed documentation, visit [docs.revenium.io](https://docs.revenium.io)
|
|
500
|
+
|
|
501
|
+
## Contributing
|
|
502
|
+
|
|
503
|
+
See [CONTRIBUTING.md](https://github.com/revenium/revenium-middleware-perplexity-node/blob/HEAD/CONTRIBUTING.md)
|
|
504
|
+
|
|
505
|
+
## Code of Conduct
|
|
506
|
+
|
|
507
|
+
See [CODE_OF_CONDUCT.md](https://github.com/revenium/revenium-middleware-perplexity-node/blob/HEAD/CODE_OF_CONDUCT.md)
|
|
508
|
+
|
|
509
|
+
## Security
|
|
510
|
+
|
|
511
|
+
See [SECURITY.md](https://github.com/revenium/revenium-middleware-perplexity-node/blob/HEAD/SECURITY.md)
|
|
512
|
+
|
|
513
|
+
## License
|
|
514
|
+
|
|
515
|
+
This project is licensed under the MIT License - see the [LICENSE](https://github.com/revenium/revenium-middleware-perplexity-node/blob/HEAD/LICENSE) file for details.
|
|
516
|
+
|
|
517
|
+
## Support
|
|
518
|
+
|
|
519
|
+
For issues, feature requests, or contributions:
|
|
520
|
+
|
|
521
|
+
- **GitHub Repository**: [revenium/revenium-middleware-perplexity-node](https://github.com/revenium/revenium-middleware-perplexity-node)
|
|
522
|
+
- **Issues**: [Report bugs or request features](https://github.com/revenium/revenium-middleware-perplexity-node/issues)
|
|
523
|
+
- **Documentation**: [docs.revenium.io](https://docs.revenium.io)
|
|
524
|
+
- **Contact**: Reach out to the Revenium team for additional support
|
|
525
|
+
|
|
526
|
+
---
|
|
527
|
+
|
|
528
|
+
**Built by Revenium**
|