@revenium/perplexity 2.0.4 → 2.0.6

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.
Files changed (200) hide show
  1. package/CHANGELOG.md +113 -98
  2. package/LICENSE +21 -21
  3. package/README.md +292 -271
  4. package/SECURITY.md +34 -0
  5. package/dist/cjs/core/client/index.js +13 -0
  6. package/dist/cjs/core/client/index.js.map +1 -0
  7. package/dist/cjs/core/client/manager.js +88 -0
  8. package/dist/cjs/core/client/manager.js.map +1 -0
  9. package/dist/cjs/core/config/index.js +21 -0
  10. package/dist/cjs/core/config/index.js.map +1 -0
  11. package/dist/cjs/core/config/loader.js +48 -0
  12. package/dist/cjs/core/config/loader.js.map +1 -0
  13. package/dist/cjs/core/config/manager.js +77 -0
  14. package/dist/cjs/core/config/manager.js.map +1 -0
  15. package/dist/cjs/core/config/validator.js +47 -0
  16. package/dist/cjs/core/config/validator.js.map +1 -0
  17. package/dist/cjs/core/middleware/index.js +14 -0
  18. package/dist/cjs/core/middleware/index.js.map +1 -0
  19. package/dist/cjs/core/middleware/interfaces.js +94 -0
  20. package/dist/cjs/core/middleware/interfaces.js.map +1 -0
  21. package/dist/cjs/core/middleware/revenium-client.js +44 -0
  22. package/dist/cjs/core/middleware/revenium-client.js.map +1 -0
  23. package/dist/cjs/core/middleware/streaming-wrapper.js +80 -0
  24. package/dist/cjs/core/middleware/streaming-wrapper.js.map +1 -0
  25. package/dist/cjs/core/providers/detector.js +38 -0
  26. package/dist/cjs/core/providers/detector.js.map +1 -0
  27. package/dist/cjs/core/providers/index.js +14 -0
  28. package/dist/cjs/core/providers/index.js.map +1 -0
  29. package/dist/cjs/core/tracking/api-client.js +67 -0
  30. package/dist/cjs/core/tracking/api-client.js.map +1 -0
  31. package/dist/cjs/core/tracking/index.js +21 -0
  32. package/dist/cjs/core/tracking/index.js.map +1 -0
  33. package/dist/cjs/core/tracking/payload-builder.js +95 -0
  34. package/dist/cjs/core/tracking/payload-builder.js.map +1 -0
  35. package/dist/cjs/core/tracking/usage-tracker.js +83 -0
  36. package/dist/cjs/core/tracking/usage-tracker.js.map +1 -0
  37. package/dist/cjs/index.js +55 -59
  38. package/dist/cjs/index.js.map +1 -1
  39. package/dist/cjs/types/function-parameters.js +14 -0
  40. package/dist/cjs/types/function-parameters.js.map +1 -0
  41. package/dist/cjs/types/index.js +20 -1
  42. package/dist/cjs/types/index.js.map +1 -1
  43. package/dist/cjs/utils/constants.js +20 -0
  44. package/dist/cjs/utils/constants.js.map +1 -0
  45. package/dist/cjs/utils/error-handler.js +194 -0
  46. package/dist/cjs/utils/error-handler.js.map +1 -0
  47. package/dist/cjs/utils/metadata-builder.js +191 -0
  48. package/dist/cjs/utils/metadata-builder.js.map +1 -0
  49. package/dist/cjs/utils/stop-reason-mapper.js +74 -0
  50. package/dist/cjs/utils/stop-reason-mapper.js.map +1 -0
  51. package/dist/cjs/utils/transaction-id.js +19 -0
  52. package/dist/cjs/utils/transaction-id.js.map +1 -0
  53. package/dist/cjs/utils/url-builder.js +57 -0
  54. package/dist/cjs/utils/url-builder.js.map +1 -0
  55. package/dist/esm/core/client/index.js +5 -0
  56. package/dist/esm/core/client/index.js.map +1 -0
  57. package/dist/esm/core/client/manager.js +81 -0
  58. package/dist/esm/core/client/manager.js.map +1 -0
  59. package/dist/esm/core/config/index.js +11 -0
  60. package/dist/esm/core/config/index.js.map +1 -0
  61. package/dist/esm/core/config/loader.js +45 -0
  62. package/dist/esm/core/config/loader.js.map +1 -0
  63. package/dist/esm/core/config/manager.js +70 -0
  64. package/dist/esm/core/config/manager.js.map +1 -0
  65. package/dist/esm/core/config/validator.js +44 -0
  66. package/dist/esm/core/config/validator.js.map +1 -0
  67. package/dist/esm/core/middleware/index.js +7 -0
  68. package/dist/esm/core/middleware/index.js.map +1 -0
  69. package/dist/esm/core/middleware/interfaces.js +89 -0
  70. package/dist/esm/core/middleware/interfaces.js.map +1 -0
  71. package/dist/esm/core/middleware/revenium-client.js +37 -0
  72. package/dist/esm/core/middleware/revenium-client.js.map +1 -0
  73. package/dist/esm/core/middleware/streaming-wrapper.js +76 -0
  74. package/dist/esm/core/middleware/streaming-wrapper.js.map +1 -0
  75. package/dist/esm/core/providers/detector.js +34 -0
  76. package/dist/esm/core/providers/detector.js.map +1 -0
  77. package/dist/esm/core/providers/index.js +9 -0
  78. package/dist/esm/core/providers/index.js.map +1 -0
  79. package/dist/esm/core/tracking/api-client.js +64 -0
  80. package/dist/esm/core/tracking/api-client.js.map +1 -0
  81. package/dist/esm/core/tracking/index.js +13 -0
  82. package/dist/esm/core/tracking/index.js.map +1 -0
  83. package/dist/esm/core/tracking/payload-builder.js +92 -0
  84. package/dist/esm/core/tracking/payload-builder.js.map +1 -0
  85. package/dist/esm/core/tracking/usage-tracker.js +79 -0
  86. package/dist/esm/core/tracking/usage-tracker.js.map +1 -0
  87. package/dist/esm/index.js +42 -39
  88. package/dist/esm/index.js.map +1 -1
  89. package/dist/esm/types/function-parameters.js +13 -0
  90. package/dist/esm/types/function-parameters.js.map +1 -0
  91. package/dist/esm/types/index.js +6 -2
  92. package/dist/esm/types/index.js.map +1 -1
  93. package/dist/esm/utils/constants.js +17 -0
  94. package/dist/esm/utils/constants.js.map +1 -0
  95. package/dist/esm/utils/error-handler.js +182 -0
  96. package/dist/esm/utils/error-handler.js.map +1 -0
  97. package/dist/esm/utils/metadata-builder.js +183 -0
  98. package/dist/esm/utils/metadata-builder.js.map +1 -0
  99. package/dist/esm/utils/stop-reason-mapper.js +69 -0
  100. package/dist/esm/utils/stop-reason-mapper.js.map +1 -0
  101. package/dist/esm/utils/transaction-id.js +16 -0
  102. package/dist/esm/utils/transaction-id.js.map +1 -0
  103. package/dist/esm/utils/url-builder.js +53 -0
  104. package/dist/esm/utils/url-builder.js.map +1 -0
  105. package/dist/types/core/client/index.d.ts +5 -0
  106. package/dist/types/core/client/index.d.ts.map +1 -0
  107. package/dist/types/core/client/manager.d.ts +44 -0
  108. package/dist/types/core/client/manager.d.ts.map +1 -0
  109. package/dist/types/core/config/index.d.ts +10 -0
  110. package/dist/types/core/config/index.d.ts.map +1 -0
  111. package/dist/types/core/config/loader.d.ts +13 -0
  112. package/dist/types/core/config/loader.d.ts.map +1 -0
  113. package/dist/types/core/config/manager.d.ts +28 -0
  114. package/dist/types/core/config/manager.d.ts.map +1 -0
  115. package/dist/types/core/config/validator.d.ts +12 -0
  116. package/dist/types/core/config/validator.d.ts.map +1 -0
  117. package/dist/types/core/middleware/index.d.ts +7 -0
  118. package/dist/types/core/middleware/index.d.ts.map +1 -0
  119. package/dist/types/core/middleware/interfaces.d.ts +35 -0
  120. package/dist/types/core/middleware/interfaces.d.ts.map +1 -0
  121. package/dist/types/core/middleware/revenium-client.d.ts +25 -0
  122. package/dist/types/core/middleware/revenium-client.d.ts.map +1 -0
  123. package/dist/types/core/middleware/streaming-wrapper.d.ts +21 -0
  124. package/dist/types/core/middleware/streaming-wrapper.d.ts.map +1 -0
  125. package/dist/types/core/providers/detector.d.ts +24 -0
  126. package/dist/types/core/providers/detector.d.ts.map +1 -0
  127. package/dist/types/core/providers/index.d.ts +8 -0
  128. package/dist/types/core/providers/index.d.ts.map +1 -0
  129. package/dist/types/core/tracking/api-client.d.ts +17 -0
  130. package/dist/types/core/tracking/api-client.d.ts.map +1 -0
  131. package/dist/types/core/tracking/index.d.ts +11 -0
  132. package/dist/types/core/tracking/index.d.ts.map +1 -0
  133. package/dist/types/core/tracking/payload-builder.d.ts +24 -0
  134. package/dist/types/core/tracking/payload-builder.d.ts.map +1 -0
  135. package/dist/types/core/tracking/usage-tracker.d.ts +30 -0
  136. package/dist/types/core/tracking/usage-tracker.d.ts.map +1 -0
  137. package/dist/types/index.d.ts +50 -26
  138. package/dist/types/index.d.ts.map +1 -1
  139. package/dist/types/types/function-parameters.d.ts +185 -0
  140. package/dist/types/types/function-parameters.d.ts.map +1 -0
  141. package/dist/types/types/index.d.ts +108 -129
  142. package/dist/types/types/index.d.ts.map +1 -1
  143. package/dist/types/utils/constants.d.ts +9 -0
  144. package/dist/types/utils/constants.d.ts.map +1 -0
  145. package/dist/types/utils/error-handler.d.ts +95 -0
  146. package/dist/types/utils/error-handler.d.ts.map +1 -0
  147. package/dist/types/utils/metadata-builder.d.ts +65 -0
  148. package/dist/types/utils/metadata-builder.d.ts.map +1 -0
  149. package/dist/types/utils/stop-reason-mapper.d.ts +29 -0
  150. package/dist/types/utils/stop-reason-mapper.d.ts.map +1 -0
  151. package/dist/types/utils/transaction-id.d.ts +14 -0
  152. package/dist/types/utils/transaction-id.d.ts.map +1 -0
  153. package/dist/types/utils/url-builder.d.ts +22 -0
  154. package/dist/types/utils/url-builder.d.ts.map +1 -0
  155. package/examples/README.md +226 -322
  156. package/examples/advanced.ts +123 -0
  157. package/examples/basic.ts +45 -50
  158. package/examples/getting_started.ts +41 -64
  159. package/examples/metadata.ts +68 -65
  160. package/examples/stream.ts +53 -0
  161. package/package.json +72 -72
  162. package/dist/cjs/constants.js +0 -70
  163. package/dist/cjs/constants.js.map +0 -1
  164. package/dist/cjs/core/config/perplexity-config.js +0 -45
  165. package/dist/cjs/core/config/perplexity-config.js.map +0 -1
  166. package/dist/cjs/core/config/revenium-config.js +0 -80
  167. package/dist/cjs/core/config/revenium-config.js.map +0 -1
  168. package/dist/cjs/core/tracking/metering.js +0 -211
  169. package/dist/cjs/core/tracking/metering.js.map +0 -1
  170. package/dist/cjs/core/wrapper/perplexity-client.js +0 -187
  171. package/dist/cjs/core/wrapper/perplexity-client.js.map +0 -1
  172. package/dist/cjs/utils/logger.js +0 -23
  173. package/dist/cjs/utils/logger.js.map +0 -1
  174. package/dist/esm/constants.js +0 -67
  175. package/dist/esm/constants.js.map +0 -1
  176. package/dist/esm/core/config/perplexity-config.js +0 -40
  177. package/dist/esm/core/config/perplexity-config.js.map +0 -1
  178. package/dist/esm/core/config/revenium-config.js +0 -72
  179. package/dist/esm/core/config/revenium-config.js.map +0 -1
  180. package/dist/esm/core/tracking/metering.js +0 -206
  181. package/dist/esm/core/tracking/metering.js.map +0 -1
  182. package/dist/esm/core/wrapper/perplexity-client.js +0 -180
  183. package/dist/esm/core/wrapper/perplexity-client.js.map +0 -1
  184. package/dist/esm/utils/logger.js +0 -20
  185. package/dist/esm/utils/logger.js.map +0 -1
  186. package/dist/types/constants.d.ts +0 -67
  187. package/dist/types/constants.d.ts.map +0 -1
  188. package/dist/types/core/config/perplexity-config.d.ts +0 -24
  189. package/dist/types/core/config/perplexity-config.d.ts.map +0 -1
  190. package/dist/types/core/config/revenium-config.d.ts +0 -37
  191. package/dist/types/core/config/revenium-config.d.ts.map +0 -1
  192. package/dist/types/core/tracking/metering.d.ts +0 -31
  193. package/dist/types/core/tracking/metering.d.ts.map +0 -1
  194. package/dist/types/core/wrapper/perplexity-client.d.ts +0 -32
  195. package/dist/types/core/wrapper/perplexity-client.d.ts.map +0 -1
  196. package/dist/types/utils/logger.d.ts +0 -10
  197. package/dist/types/utils/logger.d.ts.map +0 -1
  198. package/examples/advanced-features.ts +0 -148
  199. package/examples/chat.ts +0 -73
  200. package/examples/streaming.ts +0 -50
package/README.md CHANGED
@@ -1,271 +1,292 @@
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
- [![npm version](https://img.shields.io/npm/v/@revenium/perplexity.svg)](https://www.npmjs.com/package/@revenium/perplexity)
6
- [![Node.js](https://img.shields.io/badge/Node.js-20%2B-green)](https://nodejs.org/)
7
- [![Documentation](https://img.shields.io/badge/docs-revenium.io-blue)](https://docs.revenium.io)
8
- [![Website](https://img.shields.io/badge/website-revenium.ai-blue)](https://www.revenium.ai)
9
- [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
10
-
11
- ## Features
12
-
13
- - **Zero Configuration** - Works out of the box with environment variables
14
- - **Automatic Metering** - Tracks all API calls with detailed usage metrics
15
- - **Streaming Support** - Full support for streaming responses
16
- - **TypeScript First** - Built with TypeScript, includes full type definitions
17
- - **Multi-Format** - Supports both ESM and CommonJS
18
- - **Custom Metadata** - Add custom tracking metadata to any request
19
- - **Production Ready** - Battle-tested and optimized for production use
20
-
21
- ## Getting Started
22
-
23
- ### Quick Start
24
-
25
- ```bash
26
- npm install @revenium/perplexity dotenv
27
- ```
28
-
29
- 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).
30
-
31
- ### Step-by-Step Guide
32
-
33
- The following guide walks you through creating a new project from scratch:
34
-
35
- #### Step 1: Create Your Project
36
-
37
- ```bash
38
- # Create project directory
39
- mkdir my-perplexity-project
40
- cd my-perplexity-project
41
-
42
- # Initialize Node.js project
43
- npm init -y
44
- ```
45
-
46
- #### Step 2: Install Dependencies
47
-
48
- ```bash
49
- # Install Revenium middleware and Perplexity dependencies
50
- npm install @revenium/perplexity dotenv
51
-
52
- # For TypeScript projects (optional)
53
- npm install -D typescript tsx @types/node
54
- ```
55
-
56
- #### Step 3: Setup Environment Variables
57
-
58
- Create a `.env` file in your project root:
59
-
60
- ```bash
61
- # Copy the example file
62
- cp .env.example .env
63
-
64
- # Then edit .env with your actual API keys
65
- ```
66
-
67
- See [`.env.example`](https://github.com/revenium/revenium-middleware-perplexity-node/blob/HEAD/.env.example) for the complete list of environment variables and where to get your API keys.
68
-
69
- **Replace the placeholder values with your actual keys!**
70
-
71
- #### Step 4: Follow the Examples
72
-
73
- See [examples/README.md](https://github.com/revenium/revenium-middleware-perplexity-node/blob/HEAD/examples/README.md) for complete examples and setup instructions.
74
-
75
- **Cloned from GitHub?** Run examples with:
76
-
77
- ```bash
78
- npm install
79
- npm run example:basic
80
- npm run example:streaming
81
- npm run example:chat
82
- npm run example:metadata
83
- npm run example:advanced
84
- npm run example:getting-started
85
- ```
86
-
87
- **Browse online:** [`examples/` directory](https://github.com/revenium/revenium-middleware-perplexity-node/tree/HEAD/examples)
88
-
89
- ---
90
-
91
- ## What Gets Tracked
92
-
93
- The middleware automatically captures:
94
-
95
- - **Token Usage**: Prompt and completion tokens for accurate billing
96
- - **Request Duration**: Total time for each API call
97
- - **Model Information**: Which Perplexity model was used
98
- - **Operation Type**: Chat completion, streaming, etc.
99
- - **Error Tracking**: Failed requests and error details
100
- - **Streaming Metrics**: Time to first token for streaming responses
101
- - **Custom Metadata**: Business context you provide
102
-
103
- ## Advanced Usage
104
-
105
- For advanced features including:
106
-
107
- - **Initialization Options** - Environment variables vs manual configuration
108
- - **Streaming Responses** - Real-time streaming with usage tracking
109
- - **Custom Metadata Tracking** - Add business context to your API calls
110
-
111
- See the **[Examples Guide](./examples/README.md)** for detailed documentation and working code examples.
112
-
113
- ### Metadata Fields Reference
114
-
115
- The `usageMetadata` parameter supports the following fields for detailed tracking:
116
-
117
- | Field | Description | Use Case |
118
- | ----------------------------- | ------------------------------------------------------ | -------------------------------------------------------------------------------------------------------------- |
119
- | `traceId` | Unique identifier for session or conversation tracking | Link multiple API calls together for debugging, user session analytics, or distributed tracing across services |
120
- | `taskType` | Type of AI task being performed | Categorize usage by workload (e.g., "search", "research", "qa") for cost analysis and optimization |
121
- | `subscriber.id` | Unique user identifier | Track individual user consumption for billing, rate limiting, or user analytics |
122
- | `subscriber.email` | User's email address | Associate usage with specific users for reporting and communication |
123
- | `subscriber.credential` | Object containing credential information | Track usage by API keys and credentials |
124
- | `subscriber.credential.name` | Authentication credential name | Track which API key or service account made the request |
125
- | `subscriber.credential.value` | Authentication credential value | Associate usage with specific credentials for security auditing |
126
- | `organizationId` | Organization or company identifier | Multi-tenant cost allocation, usage quotas per organization |
127
- | `subscriptionId` | Subscription plan identifier | Track usage against subscription limits, identify plan upgrade opportunities |
128
- | `productId` | Your product or feature identifier | Attribute AI costs to specific features in your application (e.g., "search-bot", "research-assistant") |
129
- | `agent` | AI agent or bot identifier | Distinguish between multiple AI agents or automation workflows in your system |
130
- | `responseQualityScore` | Custom quality rating (0.0-1.0) | Track user satisfaction or automated quality metrics for model performance analysis |
131
-
132
- For complete API documentation and additional details, see the [Revenium API Reference](https://revenium.readme.io/reference/meter_ai_completion).
133
-
134
- ## Troubleshooting
135
-
136
- ### Common Issues
137
-
138
- #### "Missing API Key" Error
139
-
140
- ```bash
141
- # Verify your .env file exists and has the required keys
142
- cat .env
143
-
144
- # Or use .env.example as a template
145
- cp .env.example .env
146
- # Then edit .env with your actual keys
147
- ```
148
-
149
- #### "Requests not being tracked"
150
-
151
- ```bash
152
- # Enable debug logging to see what's happening
153
- export DEBUG="true"
154
-
155
- # Check your .env file has correct values
156
- cat .env | grep REVENIUM
157
-
158
- # Verify initialization is called before making requests
159
- ```
160
-
161
- #### TypeScript errors with usageMetadata
162
-
163
- - Ensure you're importing from `@revenium/perplexity`
164
- - Check that your TypeScript version is 5.0+
165
- - Verify you're using the latest version: `npm update @revenium/perplexity`
166
-
167
- #### Build/Import Errors
168
-
169
- ```bash
170
- # Clean and reinstall
171
- rm -rf node_modules
172
- npm install
173
-
174
- # For TypeScript projects
175
- npm run build
176
- ```
177
-
178
- ### Debug Mode
179
-
180
- **SECURITY WARNING: Never enable DEBUG mode in production!**
181
-
182
- Debug mode is intended for local development only. While sensitive fields (PII, credentials) are automatically redacted from debug logs, debug mode should only be enabled in development environments with test data.
183
-
184
- Enable debug logging to troubleshoot issues:
185
-
186
- ```bash
187
- export DEBUG="true"
188
- node your-script.js
189
- ```
190
-
191
- This will show:
192
-
193
- - Initialization details
194
- - Configuration loading
195
- - API call information (with PII/credentials redacted)
196
- - Error details
197
-
198
- **Production environments must NEVER set DEBUG=true.**
199
-
200
- ## Project Structure
201
-
202
- ```
203
- revenium-middleware-perplexity-node/
204
- ├── src/
205
- │ ├── core/
206
- │ │ ├── config/ # Configuration management
207
- │ │ ├── tracking/ # Metering and tracking
208
- │ │ └── wrapper/ # Perplexity API wrapper
209
- │ ├── types/ # TypeScript type definitions
210
- │ ├── utils/ # Utility functions
211
- │ └── index.ts # Main entry point
212
- ├── examples/ # TypeScript examples
213
- └── dist/
214
- ├── cjs/ # CommonJS build
215
- ├── esm/ # ES Modules build
216
- └── types/ # TypeScript definitions
217
- ```
218
-
219
- ## How It Works
220
-
221
- 1. **Initialization**: When you call `initializePerplexityFromEnv()` and `initializeReveniumFromEnv()`, the middleware sets up configurations
222
- 2. **Request Wrapping**: All Perplexity API calls go through the middleware wrapper
223
- 3. **Usage Extraction**: Token counts, model info, and timing data are captured from responses
224
- 4. **Async Tracking**: Usage data is sent to Revenium in the background (fire-and-forget)
225
- 5. **Transparent Response**: Original Perplexity responses are returned unchanged
226
-
227
- The middleware never blocks your application - if Revenium tracking fails, your Perplexity requests continue normally.
228
-
229
- ## Requirements
230
-
231
- - Node.js 20+
232
- - TypeScript 5.0+ (for TypeScript projects)
233
- - Revenium API key
234
- - Perplexity API key
235
-
236
- ## Documentation
237
-
238
- For detailed documentation, visit [docs.revenium.io](https://docs.revenium.io)
239
-
240
- ## Contributing
241
-
242
- See [CONTRIBUTING.md](https://github.com/revenium/revenium-middleware-perplexity-node/blob/HEAD/CONTRIBUTING.md)
243
-
244
- ## Code of Conduct
245
-
246
- See [CODE_OF_CONDUCT.md](https://github.com/revenium/revenium-middleware-perplexity-node/blob/HEAD/CODE_OF_CONDUCT.md)
247
-
248
- ## Security
249
-
250
- See [SECURITY.md](https://github.com/revenium/revenium-middleware-perplexity-node/blob/HEAD/SECURITY.md)
251
-
252
- ## License
253
-
254
- 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.
255
-
256
- ## Support
257
-
258
- For issues, feature requests, or contributions:
259
-
260
- - **GitHub Repository**: [revenium/revenium-middleware-perplexity-node](https://github.com/revenium/revenium-middleware-perplexity-node)
261
- - **Issues**: [Report bugs or request features](https://github.com/revenium/revenium-middleware-perplexity-node/issues)
262
- - **Documentation**: [docs.revenium.io](https://docs.revenium.io)
263
- - **Contact**: Reach out to the Revenium team for additional support
264
-
265
- ## Development
266
-
267
- For development and testing instructions, see [DEVELOPMENT.md](https://github.com/revenium/revenium-middleware-perplexity-node/blob/HEAD/DEVELOPMENT.md).
268
-
269
- ---
270
-
271
- **Built by Revenium**
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
+ [![npm version](https://img.shields.io/npm/v/@revenium/perplexity.svg)](https://www.npmjs.com/package/@revenium/perplexity)
6
+ [![Node.js](https://img.shields.io/badge/Node.js-20%2B-green)](https://nodejs.org/)
7
+ [![Documentation](https://img.shields.io/badge/docs-revenium.io-blue)](https://docs.revenium.io)
8
+ [![Website](https://img.shields.io/badge/website-revenium.ai-blue)](https://www.revenium.ai)
9
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
10
+
11
+ ## Features
12
+
13
+ - **Zero Configuration** - Works out of the box with environment variables
14
+ - **Automatic Metering** - Tracks all API calls with detailed usage metrics
15
+ - **Streaming Support** - Full support for streaming responses
16
+ - **TypeScript First** - Built with TypeScript, includes full type definitions
17
+ - **Multi-Format** - Supports both ESM and CommonJS
18
+ - **Custom Metadata** - Add custom tracking metadata to any request
19
+ - **Production Ready** - Battle-tested and optimized for production use
20
+
21
+ ## Getting Started
22
+
23
+ ### Quick Start
24
+
25
+ ```bash
26
+ npm install @revenium/perplexity dotenv
27
+ ```
28
+
29
+ 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).
30
+
31
+ ### Step-by-Step Guide
32
+
33
+ The following guide walks you through creating a new project from scratch:
34
+
35
+ #### Step 1: Create Your Project
36
+
37
+ ```bash
38
+ # Create project directory
39
+ mkdir my-perplexity-project
40
+ cd my-perplexity-project
41
+
42
+ # Initialize npm project
43
+ npm init -y
44
+ ```
45
+
46
+ #### Step 2: Install Dependencies
47
+
48
+ ```bash
49
+ # Install Revenium middleware and Perplexity dependencies
50
+ npm install @revenium/perplexity dotenv
51
+
52
+ # For TypeScript projects (optional)
53
+ npm install -D typescript tsx @types/node
54
+ ```
55
+
56
+ #### Step 3: Setup Environment Variables
57
+
58
+ Create a `.env` file in your project root.
59
+
60
+ ```bash
61
+ # Copy the example file
62
+ cp .env.example .env
63
+
64
+ # Then edit .env with your actual API keys
65
+ ```
66
+
67
+ See [`.env.example`](https://github.com/revenium/revenium-middleware-perplexity-node/blob/HEAD/.env.example) for the complete list of environment variables and where to get your API keys.
68
+
69
+ **Replace the placeholder values with your actual keys!**
70
+
71
+ #### Step 4: Follow the Examples
72
+
73
+ **For complete examples and usage patterns, see [examples/README.md](https://github.com/revenium/revenium-middleware-perplexity-node/blob/HEAD/examples/README.md) for complete examples and setup instructions.
74
+
75
+ **Cloned from GitHub?** Run examples with:
76
+
77
+ ```bash
78
+ npm install
79
+ npm run example:basic
80
+ npm run example:streaming
81
+ npm run example:chat
82
+ npm run example:metadata
83
+ npm run example:advanced
84
+ npm run example:getting-started
85
+ ```
86
+
87
+ ---
88
+
89
+ ## What Gets Tracked
90
+
91
+ The middleware automatically captures comprehensive usage data:
92
+
93
+ ### **Usage Metrics**
94
+
95
+ - **Token Counts** - Input tokens, output tokens, total tokens
96
+ - **Model Information** - Model name, provider (Perplexity)
97
+ - **Request Timing** - Request duration, response time
98
+ - **Cost Calculation** - Estimated costs based on current pricing
99
+
100
+ ### **Business Context (Optional)**
101
+
102
+ - **User Tracking** - Subscriber ID, email, credentials
103
+ - **Organization Data** - Organization ID, subscription ID, product ID
104
+ - **Task Classification** - Task type, agent identifier, trace ID
105
+ - **Quality Metrics** - Response quality scores, task identifiers
106
+
107
+ ### **Technical Details**
108
+
109
+ - **API Endpoints** - Chat completions
110
+ - **Request Types** - Streaming vs non-streaming
111
+ - **Error Tracking** - Failed requests, error types, retry attempts
112
+ - **Environment Info** - Development vs production usage
113
+
114
+ ## API Overview
115
+
116
+ The middleware provides a Go-aligned API with the following main functions:
117
+
118
+ - **`Initialize(config?)`** - Initialize the middleware (from environment or explicit config)
119
+ - **`GetClient()`** - Get the global Revenium client instance
120
+ - **`Configure(config)`** - Alias for `Initialize()` for programmatic configuration
121
+ - **`IsInitialized()`** - Check if the middleware is initialized
122
+ - **`Reset()`** - Reset the global client (useful for testing)
123
+
124
+ **For complete API documentation and usage examples, see [`examples/README.md`](https://github.com/revenium/revenium-middleware-perplexity-node/blob/HEAD/examples/README.md).**
125
+
126
+ ## Metadata Fields
127
+
128
+ The middleware supports the following optional metadata fields for tracking:
129
+
130
+ | Field | Type | Description |
131
+ | ----------------------- | ------ | ---------------------------------------------------------- |
132
+ | `traceId` | string | Unique identifier for session or conversation tracking |
133
+ | `taskType` | string | Type of AI task being performed (e.g., "chat", "research") |
134
+ | `agent` | string | AI agent or bot identifier |
135
+ | `organizationId` | string | Organization or company identifier |
136
+ | `productId` | string | Your product or feature identifier |
137
+ | `subscriptionId` | string | Subscription plan identifier |
138
+ | `responseQualityScore` | number | Custom quality rating (0.0-1.0) |
139
+ | `subscriber.id` | string | Unique user identifier |
140
+ | `subscriber.email` | string | User email address |
141
+ | `subscriber.credential` | object | Authentication credential (`name` and `value` fields) |
142
+
143
+ **All metadata fields are optional.** For complete metadata documentation and usage examples, see:
144
+
145
+ - [`examples/README.md`](https://github.com/revenium/revenium-middleware-perplexity-node/blob/HEAD/examples/README.md) - All usage examples
146
+ - [Revenium API Reference](https://revenium.readme.io/reference/meter_ai_completion) - Complete API documentation
147
+
148
+ ## Configuration Options
149
+
150
+ ### Environment Variables
151
+
152
+ 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).
153
+
154
+ ## Examples
155
+
156
+ The package includes comprehensive examples in the [`examples/`](https://github.com/revenium/revenium-middleware-perplexity-node/blob/HEAD/examples/) directory.
157
+
158
+
159
+ ## Project Structure
160
+
161
+ ```
162
+ revenium-middleware-perplexity-node/
163
+ ├── src/
164
+ │ ├── core/
165
+ │ │ ├── config/ # Configuration management
166
+ │ │ ├── tracking/ # Metering and tracking
167
+ │ │ └── wrapper/ # Perplexity API wrapper
168
+ │ ├── types/ # TypeScript type definitions
169
+ │ ├── utils/ # Utility functions
170
+ │ └── index.ts # Main entry point
171
+ ├── examples/ # TypeScript examples
172
+ ├── .env.example # Example environment variables
173
+ ├── package.json
174
+ ├── tsconfig.json
175
+ └── README.md
176
+ ```
177
+
178
+ ## How It Works
179
+
180
+ 1. **Initialize**: Call `Initialize()` to set up the middleware with your configuration
181
+ 2. **Get Client**: Call `GetClient()` to get a wrapped Perplexity client instance
182
+ 3. **Make Requests**: Use the client normally - all requests are automatically tracked
183
+ 4. **Async Tracking**: Usage data is sent to Revenium in the background (fire-and-forget)
184
+ 5. **Transparent Response**: Original Perplexity responses are returned unchanged
185
+
186
+ The middleware never blocks your application - if Revenium tracking fails, your Perplexity requests continue normally.
187
+
188
+ **Supported APIs:**
189
+
190
+ - Chat Completions API (`client.chat().completions().create()`)
191
+ - Streaming API (`client.chat().completions().createStreaming()`)
192
+
193
+ ## Troubleshooting
194
+
195
+ ### Common Issues
196
+
197
+ **No tracking data appears:**
198
+
199
+ 1. Verify environment variables are set correctly in `.env`
200
+ 2. Enable debug logging by setting `REVENIUM_DEBUG=true` in `.env`
201
+ 3. Check console for `[Revenium]` log messages
202
+ 4. Verify your `REVENIUM_METERING_API_KEY` is valid
203
+
204
+ **Client not initialized error:**
205
+
206
+ - Make sure you call `Initialize()` before `GetClient()`
207
+ - Check that your `.env` file is in the project root
208
+ - Verify `REVENIUM_METERING_API_KEY` is set
209
+
210
+ **Perplexity API errors:**
211
+
212
+ - Verify `PERPLEXITY_API_KEY` is set correctly
213
+ - Check that your API key starts with `pplx-`
214
+ - Ensure you're using a valid model name
215
+
216
+ ### Debug Mode
217
+
218
+ Enable detailed logging by adding to your `.env`:
219
+
220
+ ```env
221
+ REVENIUM_DEBUG=true
222
+ ```
223
+
224
+ ### Getting Help
225
+
226
+ If issues persist:
227
+
228
+ 1. Enable debug logging (`REVENIUM_DEBUG=true`)
229
+ 2. Check the [`examples/`](https://github.com/revenium/revenium-middleware-perplexity-node/blob/HEAD/examples/) directory for working examples
230
+ 3. Review [`examples/README.md`](https://github.com/revenium/revenium-middleware-perplexity-node/blob/HEAD/examples/README.md) for detailed setup instructions
231
+ 4. Contact support@revenium.io with debug logs
232
+
233
+ ## Supported Models
234
+
235
+ This middleware works with any Perplexity model. For the complete model list, see the [Perplexity Models Documentation](https://docs.perplexity.ai/getting-started/models).
236
+
237
+ ### API Support Matrix
238
+
239
+ The following table shows what has been tested and verified with working examples:
240
+
241
+ | Feature | Chat Completions | Streaming |
242
+ | --------------------- | ---------------- | --------- |
243
+ | **Basic Usage** | Yes | Yes |
244
+ | **Metadata Tracking** | Yes | Yes |
245
+ | **Token Counting** | Yes | Yes |
246
+
247
+ **Note:** "Yes" = Tested with working examples in [`examples/`](https://github.com/revenium/revenium-middleware-perplexity-node/blob/HEAD/examples/) directory
248
+
249
+
250
+ ## Requirements
251
+
252
+ - Node.js 20+
253
+ - TypeScript 5.0+ (for TypeScript projects)
254
+ - Revenium API key
255
+ - Perplexity API key
256
+
257
+ ## Documentation
258
+
259
+ For detailed documentation, visit [docs.revenium.io](https://docs.revenium.io)
260
+
261
+ ## Contributing
262
+
263
+ See [CONTRIBUTING.md](https://github.com/revenium/revenium-middleware-perplexity-node/blob/HEAD/CONTRIBUTING.md)
264
+
265
+ ## Code of Conduct
266
+
267
+ See [CODE_OF_CONDUCT.md](https://github.com/revenium/revenium-middleware-perplexity-node/blob/HEAD/CODE_OF_CONDUCT.md)
268
+
269
+ ## Security
270
+
271
+ See [SECURITY.md](https://github.com/revenium/revenium-middleware-perplexity-node/blob/HEAD/SECURITY.md)
272
+
273
+ ## License
274
+
275
+ 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.
276
+
277
+ ## Support
278
+
279
+ For issues, feature requests, or contributions:
280
+
281
+ - **GitHub Repository**: [revenium/revenium-middleware-perplexity-node](https://github.com/revenium/revenium-middleware-perplexity-node)
282
+ - **Issues**: [Report bugs or request features](https://github.com/revenium/revenium-middleware-perplexity-node/issues)
283
+ - **Documentation**: [docs.revenium.io](https://docs.revenium.io)
284
+ - **Contact**: Reach out to the Revenium team for additional support
285
+
286
+ ## Development
287
+
288
+ For development and testing instructions, see [DEVELOPMENT.md](https://github.com/revenium/revenium-middleware-perplexity-node/blob/HEAD/DEVELOPMENT.md).
289
+
290
+ ---
291
+
292
+ **Built by Revenium**
package/SECURITY.md ADDED
@@ -0,0 +1,34 @@
1
+ # Security Policy
2
+
3
+ ## Reporting a Vulnerability
4
+
5
+ If you discover a security vulnerability in this package, please report it to us.
6
+
7
+ **DO NOT** create a public GitHub issue for security vulnerabilities.
8
+
9
+ ### How to Report
10
+
11
+ Email: support@revenium.io
12
+
13
+ Please include:
14
+ - Package name and version
15
+ - Description of the vulnerability
16
+ - Steps to reproduce (if applicable)
17
+ - Potential impact
18
+ - Suggested fix (if available)
19
+
20
+ We will review and respond to security reports in a timely manner.
21
+
22
+ ## Security Best Practices
23
+
24
+ When using this middleware:
25
+
26
+ 1. **API Keys**: Never commit API keys to version control
27
+ 2. **Environment Variables**: Use environment variables for sensitive configuration
28
+ 3. **PII Handling**: Ensure no PII is sent to Revenium unless explicitly configured for billing purposes
29
+ 4. **Network Security**: Always use HTTPS connections
30
+ 5. **Updates**: Keep the package updated to the latest version
31
+
32
+ ## Additional Resources
33
+
34
+ - [Revenium Documentation](https://docs.revenium.io)
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ /**
3
+ * Client module exports
4
+ */
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.Configure = exports.Reset = exports.IsInitialized = exports.GetClient = exports.Initialize = void 0;
7
+ var manager_js_1 = require("./manager.js");
8
+ Object.defineProperty(exports, "Initialize", { enumerable: true, get: function () { return manager_js_1.Initialize; } });
9
+ Object.defineProperty(exports, "GetClient", { enumerable: true, get: function () { return manager_js_1.GetClient; } });
10
+ Object.defineProperty(exports, "IsInitialized", { enumerable: true, get: function () { return manager_js_1.IsInitialized; } });
11
+ Object.defineProperty(exports, "Reset", { enumerable: true, get: function () { return manager_js_1.Reset; } });
12
+ Object.defineProperty(exports, "Configure", { enumerable: true, get: function () { return manager_js_1.Configure; } });
13
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/core/client/index.ts"],"names":[],"mappings":";AAAA;;GAEG;;;AAEH,2CAAsF;AAA7E,wGAAA,UAAU,OAAA;AAAE,uGAAA,SAAS,OAAA;AAAE,2GAAA,aAAa,OAAA;AAAE,mGAAA,KAAK,OAAA;AAAE,uGAAA,SAAS,OAAA"}