@revenium/perplexity 2.0.4 → 2.0.5

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 (205) hide show
  1. package/CHANGELOG.md +32 -33
  2. package/README.md +120 -101
  3. package/SECURITY.md +34 -0
  4. package/dist/cjs/constants/models.js +38 -0
  5. package/dist/cjs/constants/models.js.map +1 -0
  6. package/dist/cjs/core/client/index.js +13 -0
  7. package/dist/cjs/core/client/index.js.map +1 -0
  8. package/dist/cjs/core/client/manager.js +85 -0
  9. package/dist/cjs/core/client/manager.js.map +1 -0
  10. package/dist/cjs/core/config/index.js +21 -0
  11. package/dist/cjs/core/config/index.js.map +1 -0
  12. package/dist/cjs/core/config/loader.js +48 -0
  13. package/dist/cjs/core/config/loader.js.map +1 -0
  14. package/dist/cjs/core/config/manager.js +77 -0
  15. package/dist/cjs/core/config/manager.js.map +1 -0
  16. package/dist/cjs/core/config/validator.js +45 -0
  17. package/dist/cjs/core/config/validator.js.map +1 -0
  18. package/dist/cjs/core/middleware/index.js +14 -0
  19. package/dist/cjs/core/middleware/index.js.map +1 -0
  20. package/dist/cjs/core/middleware/interfaces.js +94 -0
  21. package/dist/cjs/core/middleware/interfaces.js.map +1 -0
  22. package/dist/cjs/core/middleware/revenium-client.js +44 -0
  23. package/dist/cjs/core/middleware/revenium-client.js.map +1 -0
  24. package/dist/cjs/core/middleware/streaming-wrapper.js +80 -0
  25. package/dist/cjs/core/middleware/streaming-wrapper.js.map +1 -0
  26. package/dist/cjs/core/providers/detector.js +38 -0
  27. package/dist/cjs/core/providers/detector.js.map +1 -0
  28. package/dist/cjs/core/providers/index.js +14 -0
  29. package/dist/cjs/core/providers/index.js.map +1 -0
  30. package/dist/cjs/core/tracking/api-client.js +67 -0
  31. package/dist/cjs/core/tracking/api-client.js.map +1 -0
  32. package/dist/cjs/core/tracking/index.js +21 -0
  33. package/dist/cjs/core/tracking/index.js.map +1 -0
  34. package/dist/cjs/core/tracking/payload-builder.js +95 -0
  35. package/dist/cjs/core/tracking/payload-builder.js.map +1 -0
  36. package/dist/cjs/core/tracking/usage-tracker.js +83 -0
  37. package/dist/cjs/core/tracking/usage-tracker.js.map +1 -0
  38. package/dist/cjs/index.js +60 -59
  39. package/dist/cjs/index.js.map +1 -1
  40. package/dist/cjs/types/function-parameters.js +14 -0
  41. package/dist/cjs/types/function-parameters.js.map +1 -0
  42. package/dist/cjs/types/index.js +20 -1
  43. package/dist/cjs/types/index.js.map +1 -1
  44. package/dist/cjs/utils/constants.js +20 -0
  45. package/dist/cjs/utils/constants.js.map +1 -0
  46. package/dist/cjs/utils/error-handler.js +194 -0
  47. package/dist/cjs/utils/error-handler.js.map +1 -0
  48. package/dist/cjs/utils/metadata-builder.js +191 -0
  49. package/dist/cjs/utils/metadata-builder.js.map +1 -0
  50. package/dist/cjs/utils/stop-reason-mapper.js +74 -0
  51. package/dist/cjs/utils/stop-reason-mapper.js.map +1 -0
  52. package/dist/cjs/utils/transaction-id.js +19 -0
  53. package/dist/cjs/utils/transaction-id.js.map +1 -0
  54. package/dist/cjs/utils/url-builder.js +57 -0
  55. package/dist/cjs/utils/url-builder.js.map +1 -0
  56. package/dist/esm/constants/models.js +35 -0
  57. package/dist/esm/constants/models.js.map +1 -0
  58. package/dist/esm/core/client/index.js +5 -0
  59. package/dist/esm/core/client/index.js.map +1 -0
  60. package/dist/esm/core/client/manager.js +78 -0
  61. package/dist/esm/core/client/manager.js.map +1 -0
  62. package/dist/esm/core/config/index.js +11 -0
  63. package/dist/esm/core/config/index.js.map +1 -0
  64. package/dist/esm/core/config/loader.js +45 -0
  65. package/dist/esm/core/config/loader.js.map +1 -0
  66. package/dist/esm/core/config/manager.js +70 -0
  67. package/dist/esm/core/config/manager.js.map +1 -0
  68. package/dist/esm/core/config/validator.js +42 -0
  69. package/dist/esm/core/config/validator.js.map +1 -0
  70. package/dist/esm/core/middleware/index.js +7 -0
  71. package/dist/esm/core/middleware/index.js.map +1 -0
  72. package/dist/esm/core/middleware/interfaces.js +89 -0
  73. package/dist/esm/core/middleware/interfaces.js.map +1 -0
  74. package/dist/esm/core/middleware/revenium-client.js +37 -0
  75. package/dist/esm/core/middleware/revenium-client.js.map +1 -0
  76. package/dist/esm/core/middleware/streaming-wrapper.js +76 -0
  77. package/dist/esm/core/middleware/streaming-wrapper.js.map +1 -0
  78. package/dist/esm/core/providers/detector.js +34 -0
  79. package/dist/esm/core/providers/detector.js.map +1 -0
  80. package/dist/esm/core/providers/index.js +9 -0
  81. package/dist/esm/core/providers/index.js.map +1 -0
  82. package/dist/esm/core/tracking/api-client.js +64 -0
  83. package/dist/esm/core/tracking/api-client.js.map +1 -0
  84. package/dist/esm/core/tracking/index.js +13 -0
  85. package/dist/esm/core/tracking/index.js.map +1 -0
  86. package/dist/esm/core/tracking/payload-builder.js +92 -0
  87. package/dist/esm/core/tracking/payload-builder.js.map +1 -0
  88. package/dist/esm/core/tracking/usage-tracker.js +79 -0
  89. package/dist/esm/core/tracking/usage-tracker.js.map +1 -0
  90. package/dist/esm/index.js +46 -39
  91. package/dist/esm/index.js.map +1 -1
  92. package/dist/esm/types/function-parameters.js +13 -0
  93. package/dist/esm/types/function-parameters.js.map +1 -0
  94. package/dist/esm/types/index.js +6 -2
  95. package/dist/esm/types/index.js.map +1 -1
  96. package/dist/esm/utils/constants.js +17 -0
  97. package/dist/esm/utils/constants.js.map +1 -0
  98. package/dist/esm/utils/error-handler.js +182 -0
  99. package/dist/esm/utils/error-handler.js.map +1 -0
  100. package/dist/esm/utils/metadata-builder.js +183 -0
  101. package/dist/esm/utils/metadata-builder.js.map +1 -0
  102. package/dist/esm/utils/stop-reason-mapper.js +69 -0
  103. package/dist/esm/utils/stop-reason-mapper.js.map +1 -0
  104. package/dist/esm/utils/transaction-id.js +16 -0
  105. package/dist/esm/utils/transaction-id.js.map +1 -0
  106. package/dist/esm/utils/url-builder.js +53 -0
  107. package/dist/esm/utils/url-builder.js.map +1 -0
  108. package/dist/types/constants/models.d.ts +39 -0
  109. package/dist/types/constants/models.d.ts.map +1 -0
  110. package/dist/types/core/client/index.d.ts +5 -0
  111. package/dist/types/core/client/index.d.ts.map +1 -0
  112. package/dist/types/core/client/manager.d.ts +44 -0
  113. package/dist/types/core/client/manager.d.ts.map +1 -0
  114. package/dist/types/core/config/index.d.ts +10 -0
  115. package/dist/types/core/config/index.d.ts.map +1 -0
  116. package/dist/types/core/config/loader.d.ts +13 -0
  117. package/dist/types/core/config/loader.d.ts.map +1 -0
  118. package/dist/types/core/config/manager.d.ts +28 -0
  119. package/dist/types/core/config/manager.d.ts.map +1 -0
  120. package/dist/types/core/config/validator.d.ts +12 -0
  121. package/dist/types/core/config/validator.d.ts.map +1 -0
  122. package/dist/types/core/middleware/index.d.ts +7 -0
  123. package/dist/types/core/middleware/index.d.ts.map +1 -0
  124. package/dist/types/core/middleware/interfaces.d.ts +35 -0
  125. package/dist/types/core/middleware/interfaces.d.ts.map +1 -0
  126. package/dist/types/core/middleware/revenium-client.d.ts +25 -0
  127. package/dist/types/core/middleware/revenium-client.d.ts.map +1 -0
  128. package/dist/types/core/middleware/streaming-wrapper.d.ts +21 -0
  129. package/dist/types/core/middleware/streaming-wrapper.d.ts.map +1 -0
  130. package/dist/types/core/providers/detector.d.ts +24 -0
  131. package/dist/types/core/providers/detector.d.ts.map +1 -0
  132. package/dist/types/core/providers/index.d.ts +8 -0
  133. package/dist/types/core/providers/index.d.ts.map +1 -0
  134. package/dist/types/core/tracking/api-client.d.ts +17 -0
  135. package/dist/types/core/tracking/api-client.d.ts.map +1 -0
  136. package/dist/types/core/tracking/index.d.ts +11 -0
  137. package/dist/types/core/tracking/index.d.ts.map +1 -0
  138. package/dist/types/core/tracking/payload-builder.d.ts +24 -0
  139. package/dist/types/core/tracking/payload-builder.d.ts.map +1 -0
  140. package/dist/types/core/tracking/usage-tracker.d.ts +30 -0
  141. package/dist/types/core/tracking/usage-tracker.d.ts.map +1 -0
  142. package/dist/types/index.d.ts +56 -27
  143. package/dist/types/index.d.ts.map +1 -1
  144. package/dist/types/types/function-parameters.d.ts +185 -0
  145. package/dist/types/types/function-parameters.d.ts.map +1 -0
  146. package/dist/types/types/index.d.ts +108 -129
  147. package/dist/types/types/index.d.ts.map +1 -1
  148. package/dist/types/utils/constants.d.ts +9 -0
  149. package/dist/types/utils/constants.d.ts.map +1 -0
  150. package/dist/types/utils/error-handler.d.ts +95 -0
  151. package/dist/types/utils/error-handler.d.ts.map +1 -0
  152. package/dist/types/utils/metadata-builder.d.ts +65 -0
  153. package/dist/types/utils/metadata-builder.d.ts.map +1 -0
  154. package/dist/types/utils/stop-reason-mapper.d.ts +29 -0
  155. package/dist/types/utils/stop-reason-mapper.d.ts.map +1 -0
  156. package/dist/types/utils/transaction-id.d.ts +14 -0
  157. package/dist/types/utils/transaction-id.d.ts.map +1 -0
  158. package/dist/types/utils/url-builder.d.ts +22 -0
  159. package/dist/types/utils/url-builder.d.ts.map +1 -0
  160. package/examples/README.md +124 -220
  161. package/examples/advanced.ts +123 -0
  162. package/examples/basic.ts +32 -37
  163. package/examples/getting_started.ts +26 -49
  164. package/examples/metadata.ts +51 -48
  165. package/examples/stream.ts +53 -0
  166. package/package.json +9 -10
  167. package/dist/cjs/constants.js +0 -70
  168. package/dist/cjs/constants.js.map +0 -1
  169. package/dist/cjs/core/config/perplexity-config.js +0 -45
  170. package/dist/cjs/core/config/perplexity-config.js.map +0 -1
  171. package/dist/cjs/core/config/revenium-config.js +0 -80
  172. package/dist/cjs/core/config/revenium-config.js.map +0 -1
  173. package/dist/cjs/core/tracking/metering.js +0 -211
  174. package/dist/cjs/core/tracking/metering.js.map +0 -1
  175. package/dist/cjs/core/wrapper/perplexity-client.js +0 -187
  176. package/dist/cjs/core/wrapper/perplexity-client.js.map +0 -1
  177. package/dist/cjs/utils/logger.js +0 -23
  178. package/dist/cjs/utils/logger.js.map +0 -1
  179. package/dist/esm/constants.js +0 -67
  180. package/dist/esm/constants.js.map +0 -1
  181. package/dist/esm/core/config/perplexity-config.js +0 -40
  182. package/dist/esm/core/config/perplexity-config.js.map +0 -1
  183. package/dist/esm/core/config/revenium-config.js +0 -72
  184. package/dist/esm/core/config/revenium-config.js.map +0 -1
  185. package/dist/esm/core/tracking/metering.js +0 -206
  186. package/dist/esm/core/tracking/metering.js.map +0 -1
  187. package/dist/esm/core/wrapper/perplexity-client.js +0 -180
  188. package/dist/esm/core/wrapper/perplexity-client.js.map +0 -1
  189. package/dist/esm/utils/logger.js +0 -20
  190. package/dist/esm/utils/logger.js.map +0 -1
  191. package/dist/types/constants.d.ts +0 -67
  192. package/dist/types/constants.d.ts.map +0 -1
  193. package/dist/types/core/config/perplexity-config.d.ts +0 -24
  194. package/dist/types/core/config/perplexity-config.d.ts.map +0 -1
  195. package/dist/types/core/config/revenium-config.d.ts +0 -37
  196. package/dist/types/core/config/revenium-config.d.ts.map +0 -1
  197. package/dist/types/core/tracking/metering.d.ts +0 -31
  198. package/dist/types/core/tracking/metering.d.ts.map +0 -1
  199. package/dist/types/core/wrapper/perplexity-client.d.ts +0 -32
  200. package/dist/types/core/wrapper/perplexity-client.d.ts.map +0 -1
  201. package/dist/types/utils/logger.d.ts +0 -10
  202. package/dist/types/utils/logger.d.ts.map +0 -1
  203. package/examples/advanced-features.ts +0 -148
  204. package/examples/chat.ts +0 -73
  205. package/examples/streaming.ts +0 -50
@@ -0,0 +1,29 @@
1
+ /**
2
+ * Stop Reason Mapper Utilities
3
+ *
4
+ * Centralized stop reason mapping logic using lookup tables
5
+ * instead of nested conditionals. Based on the good example
6
+ * from the anthropic-node middleware.
7
+ */
8
+ /**
9
+ * Map provider stop reasons to Revenium stop reasons
10
+ *
11
+ * This replaces the nested if/switch logic with a clean lookup table approach.
12
+ * Based on the good example from the anthropic-node middleware.
13
+ *
14
+ * @param providerStopReason - Stop reason from the AI provider
15
+ * @param logger - Optional logger for warnings about unknown reasons
16
+ * @returns Standardized Revenium stop reason
17
+ */
18
+ export declare function mapStopReason(providerStopReason: string | null | undefined, logger?: {
19
+ warn: (message: string, ...args: any[]) => void;
20
+ }): string;
21
+ /**
22
+ * Get all supported stop reasons for documentation/testing
23
+ */
24
+ export declare function getSupportedStopReasons(): string[];
25
+ /**
26
+ * Check if a stop reason is supported
27
+ */
28
+ export declare function isStopReasonSupported(reason: string): boolean;
29
+ //# sourceMappingURL=stop-reason-mapper.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stop-reason-mapper.d.ts","sourceRoot":"","sources":["../../../src/utils/stop-reason-mapper.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAgCH;;;;;;;;;GASG;AACH,wBAAgB,aAAa,CAC3B,kBAAkB,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,EAC7C,MAAM,CAAC,EAAE;IAAE,IAAI,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,CAAA;CAAE,GAC3D,MAAM,CAYR;AAED;;GAEG;AACH,wBAAgB,uBAAuB,IAAI,MAAM,EAAE,CAElD;AAED;;GAEG;AACH,wBAAgB,qBAAqB,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAE7D"}
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Transaction ID Generator
3
+ *
4
+ * Generates unique transaction IDs for tracking requests.
5
+ * Format: txn_{timestamp}_{random}
6
+ */
7
+ /**
8
+ * Generate a unique transaction ID
9
+ *
10
+ * @returns A unique transaction ID in format: txn_{timestamp}_{random}
11
+ *
12
+ */
13
+ export declare function generateTransactionId(): string;
14
+ //# sourceMappingURL=transaction-id.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"transaction-id.d.ts","sourceRoot":"","sources":["../../../src/utils/transaction-id.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH;;;;;GAKG;AACH,wBAAgB,qBAAqB,IAAI,MAAM,CAE9C"}
@@ -0,0 +1,22 @@
1
+ /**
2
+ * URL Builder Utilities
3
+ *
4
+ * Centralized URL construction logic to eliminate nested conditionals.
5
+ * Replaces the complex URL building logic from tracking.ts.
6
+ */
7
+ /**
8
+ * Build Revenium API URL with proper path handling
9
+ *
10
+ * @param baseUrl - The base URL from configuration (may include /meter or /meter/v2)
11
+ * @param endpoint - The API endpoint to append (e.g., '/ai/completions')
12
+ * @returns Complete URL for the API call
13
+ */
14
+ export declare function buildReveniumUrl(baseUrl: string, endpoint: string): string;
15
+ /**
16
+ * Validate URL format
17
+ *
18
+ * @param url - URL to validate
19
+ * @returns true if valid, false otherwise
20
+ */
21
+ export declare function isValidUrl(url: string): boolean;
22
+ //# sourceMappingURL=url-builder.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"url-builder.d.ts","sourceRoot":"","sources":["../../../src/utils/url-builder.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH;;;;;;GAMG;AACH,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM,CA2B1E;AAED;;;;;GAKG;AACH,wBAAgB,UAAU,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAO/C"}
@@ -1,322 +1,226 @@
1
- # Running Examples - Revenium Middleware for Perplexity (Node.js)
1
+ # Revenium Perplexity Middleware - Examples
2
2
 
3
- This guide shows you how to run the Node.js/TypeScript examples in this repository.
3
+ This directory contains examples demonstrating how to use the Revenium Perplexity middleware.
4
4
 
5
5
  ## Prerequisites
6
6
 
7
- - Node.js 20+ installed
8
- - Perplexity API key
9
- - Revenium API key
7
+ Before running the examples, make sure you have:
10
8
 
11
- ## Setup
12
-
13
- ### 1. Clone the Repository
14
-
15
- ```bash
16
- git clone https://github.com/revenium/revenium-middleware-perplexity-node.git
17
- cd revenium-middleware-perplexity-node
18
- ```
19
-
20
- ### 2. Install Dependencies
21
-
22
- ```bash
23
- npm install
24
- ```
9
+ 1. **Node.js 20 or later** installed
10
+ 2. **Revenium API Key** - Get one from [Revenium Dashboard](https://app.revenium.io)
11
+ 3. **Perplexity API Key** - Get one from [Perplexity Platform](https://www.perplexity.ai)
25
12
 
26
- ### 3. Configure Environment Variables
27
-
28
- Create a `.env` file in the root directory:
13
+ ## Setup
29
14
 
30
- ```bash
31
- # Create .env file
32
- cp .env.example .env
33
- ```
15
+ 1. **Clone the repository** (if you haven't already):
34
16
 
35
- 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.
17
+ ```bash
18
+ git clone https://github.com/revenium/revenium-middleware-perplexity-node.git
19
+ cd revenium-middleware-perplexity-node
20
+ ```
36
21
 
37
- **Replace the placeholder values with your actual keys!**
22
+ 2. **Install dependencies**:
38
23
 
39
- ## Running Examples
24
+ ```bash
25
+ npm install
26
+ ```
40
27
 
41
- ### Available Examples
28
+ 3. **Configure environment variables**:
42
29
 
43
- ```bash
44
- npm run example:basic # Basic chat completion
45
- npm run example:streaming # Streaming response
46
- npm run example:chat # Multi-turn conversation
47
- npm run example:metadata # With custom metadata
48
- npm run example:advanced # Advanced features
49
- npm run example:getting-started # Quick start example
50
- npm test # Run all examples
51
- ```
30
+ Copy the `.env.example` file to `.env` and edit it with your API keys:
52
31
 
53
- ## Example Structure
32
+ ```bash
33
+ cp .env.example .env
34
+ ```
54
35
 
55
- ```
56
- examples/
57
- ├── basic.ts # Basic chat completion
58
- ├── streaming.ts # Streaming response
59
- ├── chat.ts # Multi-turn conversation
60
- ├── metadata.ts # With custom metadata
61
- ├── advanced-features.ts # Advanced features
62
- ├── getting_started.ts # Quick start example
63
- └── README.md # This file
64
- ```
36
+ See [.env.example](https://github.com/revenium/revenium-middleware-perplexity-node/blob/HEAD/.env.example) for all available configuration options.
65
37
 
66
- ## Examples Included
38
+ ## Examples
67
39
 
68
- ### 1. Basic Usage
40
+ ### 1. Getting Started
69
41
 
70
- **Location**: `examples/basic.ts`
42
+ **File:** `getting_started.ts`
71
43
 
72
- **What it demonstrates:**
44
+ The simplest example to get you started with Revenium tracking:
73
45
 
74
- - Simple chat completion
75
- - Automatic metering
76
- - Basic error handling
46
+ - Initialize the middleware
47
+ - Create a basic chat completion
48
+ - Display response and usage metrics
77
49
 
78
- **Run it:**
50
+ **Run:**
79
51
 
80
52
  ```bash
81
- npm run example:basic
53
+ npm run example:getting-started
54
+ # or
55
+ npx tsx examples/getting_started.ts
82
56
  ```
83
57
 
84
- **Expected output:**
58
+ **What it does:**
85
59
 
86
- ```
87
- Chat Completion Successful
88
- Response: [AI response here]
89
- Model: sonar-pro
90
- Tokens Used: 50 (prompt: 10, completion: 40)
91
- ```
60
+ - Loads configuration from environment variables
61
+ - Creates a simple chat completion request
62
+ - Automatically sends metering data to Revenium API
63
+ - Displays the response
92
64
 
93
65
  ---
94
66
 
95
- ### 2. Streaming
67
+ ### 2. Basic Usage
96
68
 
97
- **Location**: `examples/streaming.ts`
69
+ **File:** `basic.ts`
98
70
 
99
- **What it demonstrates:**
71
+ Demonstrates standard Perplexity API usage:
100
72
 
101
- - Real-time streaming responses
102
- - Token tracking for streamed content
103
- - Automatic usage tracking when stream completes
73
+ - Chat completions with metadata
74
+ - Simple metadata tracking
104
75
 
105
- **Run it:**
76
+ **Run:**
106
77
 
107
78
  ```bash
108
- npm run example:streaming
109
- ```
110
-
111
- **Expected output:**
112
-
79
+ npm run example:basic
80
+ # or
81
+ npx tsx examples/basic.ts
113
82
  ```
114
- Model: sonar-pro
115
- Streaming response:
116
83
 
117
- [Streaming text appears here in real-time]
84
+ **What it does:**
118
85
 
119
- Streaming completed
120
- ```
86
+ - Creates chat completions with metadata tracking
87
+ - Demonstrates basic metadata usage
121
88
 
122
89
  ---
123
90
 
124
- ### 3. Multi-turn Chat
91
+ ### 3. Metadata
125
92
 
126
- **Location**: `examples/chat.ts`
93
+ **File:** `metadata.ts`
127
94
 
128
- **What it demonstrates:**
95
+ Demonstrates all available metadata fields:
129
96
 
130
- - Conversation history management
131
- - Context-aware responses
132
- - Multi-message interactions
97
+ - Complete metadata structure
98
+ - All optional fields documented
99
+ - Subscriber information
133
100
 
134
- **Run it:**
101
+ **Run:**
135
102
 
136
103
  ```bash
137
- npm run example:chat
104
+ npm run example:metadata
105
+ # or
106
+ npx tsx examples/metadata.ts
138
107
  ```
139
108
 
140
- **Expected output:**
109
+ **What it does:**
141
110
 
142
- ```
143
- --- Turn 1 ---
144
- User: What is the capital of France?
145
- Assistant: [Response]
111
+ - Shows all available metadata fields
112
+ - Demonstrates subscriber tracking
113
+ - Includes organization and product tracking
146
114
 
147
- --- Turn 2 ---
148
- User: What is its population?
149
- Assistant: [Response]
115
+ **Metadata fields supported:**
150
116
 
151
- Chat completed - Total tokens used: 120
152
- ```
117
+ - `traceId` - Session or conversation tracking identifier
118
+ - `taskType` - Type of AI task being performed
119
+ - `agent` - AI agent or bot identifier
120
+ - `organizationId` - Organization identifier
121
+ - `productId` - Product or service identifier
122
+ - `subscriptionId` - Subscription tier identifier
123
+ - `responseQualityScore` - Quality rating (0.0-1.0)
124
+ - `subscriber` - Nested subscriber object with `id`, `email`, `credential` (with `name` and `value`)
153
125
 
154
126
  ---
155
127
 
156
- ### 4. Custom Metadata
128
+ ### 4. Streaming
157
129
 
158
- **Location**: `examples/metadata.ts`
130
+ **File:** `stream.ts`
159
131
 
160
- **What it demonstrates:**
132
+ Demonstrates streaming responses:
161
133
 
162
- - Custom metadata tracking
163
- - Business context in metering
164
- - Subscriber and organization tracking
134
+ - Real-time token streaming
135
+ - Accumulating responses
136
+ - Streaming metrics
165
137
 
166
- **Run it:**
138
+ **Run:**
167
139
 
168
140
  ```bash
169
- npm run example:metadata
141
+ npm run example:stream
142
+ # or
143
+ npx tsx examples/stream.ts
170
144
  ```
171
145
 
172
- **Expected output:**
146
+ **What it does:**
173
147
 
174
- ```
175
- Chat Completion Successful
176
- Response: [AI response]
177
- Model: sonar-pro
178
- Tokens Used: 50 (prompt: 10, completion: 40)
179
- Metadata tracked: subscriber, organization, product, agent, trace
180
- ```
181
-
182
- **Note:** Custom metadata (subscriber, organizationId, productId, agent, traceId) is automatically sent to Revenium in the background.
148
+ - Creates a streaming chat completion
149
+ - Displays tokens as they arrive in real-time
150
+ - Tracks streaming metrics
151
+ - Sends metering data after stream completes
183
152
 
184
153
  ---
185
154
 
186
- // Manual configuration
187
- initializeRevenium({
188
- meteringApiKey: process.env.REVENIUM_METERING_API_KEY!,
189
- meteringBaseUrl:
190
- process.env.REVENIUM_METERING_BASE_URL || "https://api.revenium.ai",
191
- });
155
+ ### 5. Advanced Features
192
156
 
193
- **Location**: `examples/advanced-features.ts`
157
+ **File:** `advanced.ts`
194
158
 
195
- **What it demonstrates:**
159
+ Demonstrates advanced Perplexity features:
196
160
 
197
- - Streaming with metadata
198
- - Multiple model comparisons
199
- - Advanced configuration
161
+ - Multi-turn conversations
162
+ - Different temperature settings
163
+ - Multiple Perplexity models
200
164
 
201
- **Run it:**
165
+ **Run:**
202
166
 
203
167
  ```bash
204
168
  npm run example:advanced
169
+ # or
170
+ npx tsx examples/advanced.ts
205
171
  ```
206
172
 
207
- **Expected output:**
173
+ **What it does:**
208
174
 
209
- ```
210
- === Advanced Features Example ===
211
-
212
- --- Streaming Responses ---
213
- Model: sonar-pro
214
- Response:
215
- [Streaming text appears here in real-time]
216
- ✓ Streaming completed
217
-
218
- --- Comprehensive Metadata Tracking ---
219
- Response: [First 100 characters]...
220
- ✓ Metadata tracked successfully
221
-
222
- --- Multiple Model Types ---
223
- Using sonar (lightweight search)...
224
- Response: [First 80 characters]...
225
-
226
- Using sonar-reasoning (real-time reasoning)...
227
- Response: [First 80 characters]...
228
-
229
- ✓ Multiple models demonstrated
230
-
231
- === All Advanced Features Demonstrated ===
232
- ```
175
+ - Demonstrates multi-turn conversation with system prompt
176
+ - Shows creative responses with higher temperature
177
+ - Uses sonar-reasoning model for complex reasoning tasks
233
178
 
234
179
  ---
235
180
 
236
- ### 6. Getting Started
181
+ ## Common Issues
237
182
 
238
- **Location**: `examples/getting_started.ts`
183
+ ### "Client not initialized" error
239
184
 
240
- **What it demonstrates:**
185
+ **Solution:** Make sure to call `Initialize()` before using `GetClient()`.
241
186
 
242
- - Quick start example
243
- - Minimal configuration
244
- - Basic usage pattern
187
+ ### "REVENIUM_METERING_API_KEY is required" error
245
188
 
246
- **Run it:**
189
+ **Solution:** Set the `REVENIUM_METERING_API_KEY` environment variable in your `.env` file.
247
190
 
248
- ```bash
249
- npm run example:getting-started
250
- ```
251
-
252
- ---
191
+ ### "invalid Revenium API key format" error
253
192
 
254
- ## Troubleshooting
193
+ **Solution:** Revenium API keys should start with `hak_`. Check your API key format.
255
194
 
256
- ### "Missing API Key" Error
195
+ ### Environment variables not loading
257
196
 
258
- Make sure you've created a `.env` file with your API keys:
197
+ **Solution:** Make sure your `.env` file is in the project root directory and contains the required variables.
259
198
 
260
- ```bash
261
- # Check if .env exists
262
- ls .env
199
+ ### Perplexity API errors
263
200
 
264
- # Verify it has the required keys
265
- cat .env
266
- ```
201
+ **Solution:** Make sure you have set `PERPLEXITY_API_KEY` in your `.env` file and that it starts with `pplx-`.
267
202
 
268
- ### "Module not found" Error
203
+ ### Debug Mode
269
204
 
270
- Install dependencies first:
205
+ Enable detailed logging to troubleshoot issues:
271
206
 
272
207
  ```bash
273
- npm install
274
- ```
275
-
276
- ### "Requests not being tracked"
277
-
278
- 1. Check your `.env` file has correct values:
208
+ # In .env file
209
+ REVENIUM_DEBUG=true
279
210
 
280
- - `PERPLEXITY_API_KEY` is set
281
- - `REVENIUM_METERING_API_KEY` is set
282
- - `REVENIUM_METERING_BASE_URL` is set to `https://api.revenium.ai`
283
-
284
- 2. Enable debug logging:
285
-
286
- ```bash
287
- DEBUG=true npm run example:basic
288
- ```
289
-
290
- ### TypeScript Errors
291
-
292
- Ensure you have TypeScript and tsx installed:
293
-
294
- ```bash
295
- npm install -D typescript tsx @types/node
211
+ # Then run examples
212
+ npm run example:getting-started
296
213
  ```
297
214
 
298
- ---
299
-
300
- ## Getting Your API Keys
301
-
302
- ### Perplexity API Key
215
+ ## Next Steps
303
216
 
304
- 1. Go to [Perplexity AI Settings](https://www.perplexity.ai/settings/api)
305
- 2. Create a new API key
306
- 3. Add to `.env` as `PERPLEXITY_API_KEY`
307
-
308
- ### Revenium API Key
309
-
310
- 1. Sign up at [Revenium](https://ai.revenium.io)
311
- 2. Create a new API key in your dashboard
312
- 3. Add to `.env` as `REVENIUM_METERING_API_KEY`
313
-
314
- ---
217
+ - Check the [main README](https://github.com/revenium/revenium-middleware-perplexity-node/blob/HEAD/README.md) for detailed documentation
218
+ - Visit the [Revenium Dashboard](https://app.revenium.io) to view your metering data
219
+ - See [.env.example](https://github.com/revenium/revenium-middleware-perplexity-node/blob/HEAD/.env.example) for all configuration options
315
220
 
316
221
  ## Support
317
222
 
318
223
  For issues or questions:
319
224
 
320
- - [GitHub Issues](https://github.com/revenium/revenium-middleware-perplexity-node/issues)
321
- - [Documentation](https://docs.revenium.io)
322
- - Contact: Reach out to the Revenium team for additional support
225
+ - Documentation: https://docs.revenium.io
226
+ - Email: support@revenium.io
@@ -0,0 +1,123 @@
1
+ /**
2
+ * Advanced Example
3
+ * Demonstrates advanced features like system prompts, conversation history,
4
+ * and different model parameters with Revenium Perplexity middleware.
5
+ */
6
+
7
+ import { Initialize, GetClient, UsageMetadata } from "@revenium/perplexity";
8
+
9
+ async function main() {
10
+ // Initialize middleware
11
+ Initialize();
12
+
13
+ // Get client
14
+ const client = GetClient();
15
+
16
+ // Metadata for tracking
17
+ const metadata: UsageMetadata = {
18
+ organizationId: "org-advanced-demo",
19
+ productId: "prod-perplexity-advanced",
20
+ taskType: "multi-turn-conversation",
21
+ subscriber: {
22
+ id: "user-advanced-001",
23
+ email: "advanced@example.com",
24
+ },
25
+ };
26
+
27
+ console.log("=== Advanced Perplexity Example ===\n");
28
+
29
+ // Example 1: Using system prompt and conversation history
30
+ console.log("1. Multi-turn conversation with system prompt:\n");
31
+
32
+ const response1 = await client
33
+ .chat()
34
+ .completions()
35
+ .create(
36
+ {
37
+ model: "sonar-pro",
38
+ messages: [
39
+ {
40
+ role: "system",
41
+ content:
42
+ "You are a helpful AI assistant that provides concise, accurate answers.",
43
+ },
44
+ {
45
+ role: "user",
46
+ content: "What is the capital of France?",
47
+ },
48
+ {
49
+ role: "assistant",
50
+ content: "The capital of France is Paris.",
51
+ },
52
+ {
53
+ role: "user",
54
+ content: "What is its population?",
55
+ },
56
+ ],
57
+ max_tokens: 500,
58
+ temperature: 0.7,
59
+ },
60
+ metadata
61
+ );
62
+
63
+ console.log(`Assistant: ${response1.choices[0].message.content}\n`);
64
+ console.log(`Tokens used: ${response1.usage?.total_tokens}\n`);
65
+
66
+ // Example 2: Using different temperature for creative responses
67
+ console.log("2. Creative response with higher temperature:\n");
68
+
69
+ const response2 = await client
70
+ .chat()
71
+ .completions()
72
+ .create(
73
+ {
74
+ model: "sonar-pro",
75
+ messages: [
76
+ {
77
+ role: "user",
78
+ content:
79
+ "Write a creative tagline for an AI company in 10 words or less.",
80
+ },
81
+ ],
82
+ max_tokens: 100,
83
+ temperature: 1.0, // Higher temperature for more creativity
84
+ },
85
+ {
86
+ ...metadata,
87
+ taskType: "creative-generation",
88
+ }
89
+ );
90
+
91
+ console.log(`Assistant: ${response2.choices[0].message.content}\n`);
92
+
93
+ // Example 3: Using sonar-reasoning for complex reasoning tasks
94
+ console.log("3. Complex reasoning with sonar-reasoning model:\n");
95
+
96
+ const response3 = await client
97
+ .chat()
98
+ .completions()
99
+ .create(
100
+ {
101
+ model: "sonar-reasoning",
102
+ messages: [
103
+ {
104
+ role: "user",
105
+ content:
106
+ "If a train travels 120 km in 2 hours, and then 180 km in 3 hours, what is its average speed for the entire journey?",
107
+ },
108
+ ],
109
+ max_tokens: 500,
110
+ },
111
+ {
112
+ ...metadata,
113
+ taskType: "reasoning",
114
+ }
115
+ );
116
+
117
+ console.log(`Assistant: ${response3.choices[0].message.content}\n`);
118
+ console.log(`Tokens used: ${response3.usage?.total_tokens}\n`);
119
+
120
+ console.log("\nAll usage data sent to Revenium! Check your dashboard");
121
+ }
122
+
123
+ main().catch(console.error);