@revenium/perplexity 1.0.19 → 1.0.21
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/README.md +61 -1
- package/dist/index.js +19 -19
- package/dist/interfaces/chatCompletionRequest.d.ts +2 -1
- package/dist/interfaces/credential.d.ts +4 -4
- package/dist/interfaces/credential.js +2 -2
- package/dist/interfaces/meteringRequest.d.ts +13 -13
- package/dist/interfaces/meteringResponse.d.ts +27 -27
- package/dist/interfaces/meteringResponse.js +2 -2
- package/dist/interfaces/operation.d.ts +4 -4
- package/dist/interfaces/operation.js +8 -8
- package/dist/interfaces/subscriber.d.ts +8 -8
- package/dist/interfaces/subscriber.js +2 -2
- package/dist/interfaces/tokenCounts.d.ts +7 -7
- package/dist/interfaces/tokenCounts.js +2 -2
- package/dist/interfaces/usageMetadata.d.ts +14 -0
- package/dist/middleware.d.ts +22 -22
- package/dist/middleware.js +2 -2
- package/dist/models/Logger.js +35 -35
- package/dist/models/Metering.d.ts +1 -1
- package/dist/models/Metering.js +28 -20
- package/dist/utils/calculateDurationMs.d.ts +1 -1
- package/dist/utils/calculateDurationMs.js +6 -6
- package/dist/utils/constants/constants.d.ts +6 -6
- package/dist/utils/constants/constants.js +11 -11
- package/dist/utils/constants/logLevels.d.ts +1 -1
- package/dist/utils/constants/logLevels.js +4 -4
- package/dist/utils/constants/messages.d.ts +5 -5
- package/dist/utils/constants/messages.js +8 -8
- package/dist/utils/constants/models.d.ts +1 -1
- package/dist/utils/constants/models.js +21 -21
- package/dist/utils/extractTokenCount.d.ts +2 -2
- package/dist/utils/extractTokenCount.js +28 -28
- package/dist/utils/formatTimeStamp.d.ts +1 -1
- package/dist/utils/formatTimeStamp.js +6 -6
- package/dist/utils/generateTransactionId.d.ts +1 -1
- package/dist/utils/generateTransactionId.js +7 -7
- package/dist/utils/index.d.ts +6 -6
- package/dist/utils/index.js +23 -23
- package/dist/utils/loadEnv.d.ts +1 -1
- package/dist/utils/loadEnv.js +7 -7
- package/dist/utils/safeExtract.d.ts +29 -29
- package/dist/utils/safeExtract.js +67 -67
- package/examples/metadata.ts +43 -0
- package/package.json +3 -2
- package/playground/basic.js +7 -11
- package/playground/chat-completions.js +22 -0
- package/playground/enhanced.js +6 -9
- package/playground/metadata.js +43 -0
- package/playground/streaming.js +2 -2
- package/src/interfaces/chatCompletionRequest.ts +2 -1
- package/src/interfaces/usageMetadata.ts +14 -0
- package/src/middleware.ts +24 -18
- package/src/models/Metering.ts +48 -20
package/README.md
CHANGED
|
@@ -187,6 +187,58 @@ const streamingExample = async () => {
|
|
|
187
187
|
streamingExample();
|
|
188
188
|
```
|
|
189
189
|
|
|
190
|
+
#### METADATA Example
|
|
191
|
+
|
|
192
|
+
Create `examples/metadata-perplexity.js`:
|
|
193
|
+
|
|
194
|
+
```javascript
|
|
195
|
+
// examples/metadata-perplexity.js
|
|
196
|
+
import { PerplexityReveniumMiddleware } from "@revenium/perplexity";
|
|
197
|
+
|
|
198
|
+
const metadataExample = async () => {
|
|
199
|
+
try {
|
|
200
|
+
const middleware = new PerplexityReveniumMiddleware();
|
|
201
|
+
const model = middleware.getGenerativeModel("sonar-pro");
|
|
202
|
+
const result = await model.createChatCompletion({
|
|
203
|
+
model: "sonar-pro",
|
|
204
|
+
messages: [{ role: "user", content: "What is the capital of France?" }],
|
|
205
|
+
usageMetadata: {
|
|
206
|
+
taskType: "test",
|
|
207
|
+
subscriberEmail: "test@revenium.ai",
|
|
208
|
+
subscriberId: "123456",
|
|
209
|
+
subscriberCredentialName: "apiKey",
|
|
210
|
+
subscriberCredential: "keyValue",
|
|
211
|
+
organizationId: "123456",
|
|
212
|
+
subscriptionId: "123456",
|
|
213
|
+
productId: "free-trial",
|
|
214
|
+
agent: "perplexity",
|
|
215
|
+
responseQualityScore: 100,
|
|
216
|
+
transactionId: "123456",
|
|
217
|
+
timeToFirstToken: 1000,
|
|
218
|
+
requestTime: new Date(),
|
|
219
|
+
completionStartTime: new Date(),
|
|
220
|
+
operationType: "CHAT",
|
|
221
|
+
inputTokenCount: 10,
|
|
222
|
+
outputTokenCount: 10,
|
|
223
|
+
reasoningTokenCount: 20,
|
|
224
|
+
cacheCreationTokenCount: 0,
|
|
225
|
+
cacheReadTokenCount: 0,
|
|
226
|
+
totalTokenCount: 40,
|
|
227
|
+
responseTime: new Date(),
|
|
228
|
+
requestDuration: 1000,
|
|
229
|
+
stopReason: "END",
|
|
230
|
+
},
|
|
231
|
+
});
|
|
232
|
+
console.log("[BASIC REQUEST]", result.choices[0].message);
|
|
233
|
+
} catch (error) {
|
|
234
|
+
console.error("❌ Perplexity streaming example failed:", error);
|
|
235
|
+
process.exit(1);
|
|
236
|
+
}
|
|
237
|
+
};
|
|
238
|
+
|
|
239
|
+
metadataExample();
|
|
240
|
+
```
|
|
241
|
+
|
|
190
242
|
### Step 8: Update package.json
|
|
191
243
|
|
|
192
244
|
```json
|
|
@@ -196,7 +248,8 @@ streamingExample();
|
|
|
196
248
|
"type": "module",
|
|
197
249
|
"scripts": {
|
|
198
250
|
"test-perplexity": "node test-perplexity.js",
|
|
199
|
-
"test-perplexity-stream": "node examples/streaming-perplexity.js"
|
|
251
|
+
"test-perplexity-stream": "node examples/streaming-perplexity.js",
|
|
252
|
+
"test-perplexity-metadata": "node examples/metadata-perplexity.js"
|
|
200
253
|
},
|
|
201
254
|
"dependencies": {
|
|
202
255
|
"@revenium/perplexity": "^1.0.0"
|
|
@@ -210,6 +263,9 @@ streamingExample();
|
|
|
210
263
|
# Test streaming
|
|
211
264
|
npm run test-perplexity-stream
|
|
212
265
|
|
|
266
|
+
# Test metadata
|
|
267
|
+
npm run test-perplexity-metadata
|
|
268
|
+
|
|
213
269
|
```
|
|
214
270
|
|
|
215
271
|
---
|
|
@@ -252,14 +308,18 @@ npm run e-basic # Basic chat completion
|
|
|
252
308
|
npm run e-streaming # Streaming response
|
|
253
309
|
npm run e-enhanced # Enhanced request
|
|
254
310
|
npm run e-chat-completions # Chat completions
|
|
311
|
+
npm run e-metadata # Metadata request
|
|
255
312
|
|
|
256
313
|
# Playground examples
|
|
257
314
|
# Required build first
|
|
258
315
|
npm run build
|
|
316
|
+
## Change type to module in package.json
|
|
317
|
+
"type": "module",
|
|
259
318
|
# Then run any of the following
|
|
260
319
|
npm run p-basic
|
|
261
320
|
npm run p-streaming
|
|
262
321
|
npm run p-enhanced
|
|
322
|
+
npm run p-metadata
|
|
263
323
|
```
|
|
264
324
|
|
|
265
325
|
---
|
package/dist/index.js
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
const dotenv_1 = require("dotenv");
|
|
18
|
-
(0, dotenv_1.config)();
|
|
19
|
-
__exportStar(require("./middleware"), exports);
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
const dotenv_1 = require("dotenv");
|
|
18
|
+
(0, dotenv_1.config)();
|
|
19
|
+
__exportStar(require("./middleware"), exports);
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { ChatCompletionMessageParam } from "openai/resources/index";
|
|
2
|
+
import { IUsageMetadata } from "./usageMetadata";
|
|
2
3
|
export interface IChatCompletionRequest {
|
|
3
4
|
model?: string;
|
|
4
5
|
messages: ChatCompletionMessageParam[];
|
|
5
6
|
stream?: boolean;
|
|
6
|
-
usageMetadata?:
|
|
7
|
+
usageMetadata?: IUsageMetadata;
|
|
7
8
|
[key: string]: any;
|
|
8
9
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export interface ICredential {
|
|
2
|
-
name: string;
|
|
3
|
-
value: string;
|
|
4
|
-
}
|
|
1
|
+
export interface ICredential {
|
|
2
|
+
name: string;
|
|
3
|
+
value: string;
|
|
4
|
+
}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import { IOperationType } from "./operation";
|
|
2
|
-
import { ITokenCounts } from "./tokenCounts";
|
|
3
|
-
import { IUsageMetadata } from "./usageMetadata";
|
|
4
|
-
export interface IMeteringRequest {
|
|
5
|
-
transactionId?: string;
|
|
6
|
-
startTime: Date;
|
|
7
|
-
endTime: Date;
|
|
8
|
-
modelName: string;
|
|
9
|
-
tokenCounts: ITokenCounts;
|
|
10
|
-
stopReason: string;
|
|
11
|
-
operationType: IOperationType;
|
|
12
|
-
usageMetadata?: IUsageMetadata;
|
|
13
|
-
}
|
|
1
|
+
import { IOperationType } from "./operation";
|
|
2
|
+
import { ITokenCounts } from "./tokenCounts";
|
|
3
|
+
import { IUsageMetadata } from "./usageMetadata";
|
|
4
|
+
export interface IMeteringRequest {
|
|
5
|
+
transactionId?: string;
|
|
6
|
+
startTime: Date;
|
|
7
|
+
endTime: Date;
|
|
8
|
+
modelName: string;
|
|
9
|
+
tokenCounts: ITokenCounts;
|
|
10
|
+
stopReason: string;
|
|
11
|
+
operationType: IOperationType;
|
|
12
|
+
usageMetadata?: IUsageMetadata;
|
|
13
|
+
}
|
|
@@ -1,27 +1,27 @@
|
|
|
1
|
-
import { ISubscriber } from "./subscriber";
|
|
2
|
-
export interface IMeteringResponse {
|
|
3
|
-
stopReason: string;
|
|
4
|
-
costType: string;
|
|
5
|
-
isStreamed: boolean;
|
|
6
|
-
taskType: string;
|
|
7
|
-
agent: string;
|
|
8
|
-
operationType: string;
|
|
9
|
-
inputTokenCount: number;
|
|
10
|
-
outputTokenCount: number;
|
|
11
|
-
reasoningTokenCount: number;
|
|
12
|
-
cacheCreationTokenCount: number;
|
|
13
|
-
cacheReadTokenCount: number;
|
|
14
|
-
totalTokenCount: number;
|
|
15
|
-
organizationId: string;
|
|
16
|
-
productId: string;
|
|
17
|
-
subscriber: ISubscriber;
|
|
18
|
-
model: string;
|
|
19
|
-
transactionId: string;
|
|
20
|
-
responseTime: string;
|
|
21
|
-
requestDuration: number;
|
|
22
|
-
provider: string;
|
|
23
|
-
requestTime: string;
|
|
24
|
-
completionStartTime: string;
|
|
25
|
-
timeToFirstToken: number;
|
|
26
|
-
middleware_source: string;
|
|
27
|
-
}
|
|
1
|
+
import { ISubscriber } from "./subscriber";
|
|
2
|
+
export interface IMeteringResponse {
|
|
3
|
+
stopReason: string;
|
|
4
|
+
costType: string;
|
|
5
|
+
isStreamed: boolean;
|
|
6
|
+
taskType: string;
|
|
7
|
+
agent: string;
|
|
8
|
+
operationType: string;
|
|
9
|
+
inputTokenCount: number;
|
|
10
|
+
outputTokenCount: number;
|
|
11
|
+
reasoningTokenCount: number;
|
|
12
|
+
cacheCreationTokenCount: number;
|
|
13
|
+
cacheReadTokenCount: number;
|
|
14
|
+
totalTokenCount: number;
|
|
15
|
+
organizationId: string;
|
|
16
|
+
productId: string;
|
|
17
|
+
subscriber: ISubscriber;
|
|
18
|
+
model: string;
|
|
19
|
+
transactionId: string;
|
|
20
|
+
responseTime: string;
|
|
21
|
+
requestDuration: number;
|
|
22
|
+
provider: string;
|
|
23
|
+
requestTime: string;
|
|
24
|
+
completionStartTime: string;
|
|
25
|
+
timeToFirstToken: number;
|
|
26
|
+
middleware_source: string;
|
|
27
|
+
}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export declare enum IOperationType {
|
|
2
|
-
CHAT = "CHAT",
|
|
3
|
-
EMBED = "EMBED"
|
|
4
|
-
}
|
|
1
|
+
export declare enum IOperationType {
|
|
2
|
+
CHAT = "CHAT",
|
|
3
|
+
EMBED = "EMBED"
|
|
4
|
+
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.IOperationType = void 0;
|
|
4
|
-
var IOperationType;
|
|
5
|
-
(function (IOperationType) {
|
|
6
|
-
IOperationType["CHAT"] = "CHAT";
|
|
7
|
-
IOperationType["EMBED"] = "EMBED";
|
|
8
|
-
})(IOperationType || (exports.IOperationType = IOperationType = {}));
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.IOperationType = void 0;
|
|
4
|
+
var IOperationType;
|
|
5
|
+
(function (IOperationType) {
|
|
6
|
+
IOperationType["CHAT"] = "CHAT";
|
|
7
|
+
IOperationType["EMBED"] = "EMBED";
|
|
8
|
+
})(IOperationType || (exports.IOperationType = IOperationType = {}));
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
export interface ISubscriber {
|
|
2
|
-
id: string;
|
|
3
|
-
email: string;
|
|
4
|
-
credential: {
|
|
5
|
-
name: string;
|
|
6
|
-
value: string;
|
|
7
|
-
};
|
|
8
|
-
}
|
|
1
|
+
export interface ISubscriber {
|
|
2
|
+
id: string;
|
|
3
|
+
email: string;
|
|
4
|
+
credential: {
|
|
5
|
+
name: string;
|
|
6
|
+
value: string;
|
|
7
|
+
};
|
|
8
|
+
}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
export interface ITokenCounts {
|
|
2
|
-
inputTokens: number;
|
|
3
|
-
outputTokens: number;
|
|
4
|
-
totalTokens: number;
|
|
5
|
-
cachedTokens?: number;
|
|
6
|
-
reasoningTokens?: number;
|
|
7
|
-
}
|
|
1
|
+
export interface ITokenCounts {
|
|
2
|
+
inputTokens: number;
|
|
3
|
+
outputTokens: number;
|
|
4
|
+
totalTokens: number;
|
|
5
|
+
cachedTokens?: number;
|
|
6
|
+
reasoningTokens?: number;
|
|
7
|
+
}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
@@ -10,4 +10,18 @@ export interface IUsageMetadata {
|
|
|
10
10
|
productId?: string;
|
|
11
11
|
agent?: string;
|
|
12
12
|
responseQualityScore?: number;
|
|
13
|
+
transactionId?: string;
|
|
14
|
+
timeToFirstToken?: number;
|
|
15
|
+
requestTime?: Date;
|
|
16
|
+
completionStartTime?: Date;
|
|
17
|
+
operationType?: string;
|
|
18
|
+
inputTokenCount?: number;
|
|
19
|
+
outputTokenCount?: number;
|
|
20
|
+
reasoningTokenCount?: number;
|
|
21
|
+
cacheCreationTokenCount?: number;
|
|
22
|
+
cacheReadTokenCount?: number;
|
|
23
|
+
totalTokenCount?: number;
|
|
24
|
+
responseTime?: Date;
|
|
25
|
+
requestDuration?: number;
|
|
26
|
+
stopReason?: string;
|
|
13
27
|
}
|
package/dist/middleware.d.ts
CHANGED
|
@@ -1,22 +1,22 @@
|
|
|
1
|
-
import OpenAI from "openai";
|
|
2
|
-
import { IChatCompletionRequest } from "./interfaces/chatCompletionRequest";
|
|
3
|
-
import { RequestOptions } from "openai/internal/request-options";
|
|
4
|
-
export declare class PerplexityReveniumMiddleware {
|
|
5
|
-
private client;
|
|
6
|
-
private working;
|
|
7
|
-
private modelName;
|
|
8
|
-
constructor();
|
|
9
|
-
getGenerativeModel(model: string): {
|
|
10
|
-
createChatCompletion: (params: IChatCompletionRequest) => Promise<OpenAI.Chat.Completions.ChatCompletion & {
|
|
11
|
-
_request_id?: string | null | undefined;
|
|
12
|
-
}>;
|
|
13
|
-
createChatCompletionStream: (params: IChatCompletionRequest) => Promise<import("openai/core/streaming").Stream<OpenAI.Chat.Completions.ChatCompletionChunk> & {
|
|
14
|
-
_request_id?: string | null;
|
|
15
|
-
}>;
|
|
16
|
-
createEmbeddings: (_body: Omit<OpenAI.Embeddings.EmbeddingCreateParams, "model">, _options?: RequestOptions) => Promise<void>;
|
|
17
|
-
};
|
|
18
|
-
private embeddings;
|
|
19
|
-
private streamingChatCompletion;
|
|
20
|
-
private chatCompletion;
|
|
21
|
-
private verifyEnv;
|
|
22
|
-
}
|
|
1
|
+
import OpenAI from "openai";
|
|
2
|
+
import { IChatCompletionRequest } from "./interfaces/chatCompletionRequest";
|
|
3
|
+
import { RequestOptions } from "openai/internal/request-options";
|
|
4
|
+
export declare class PerplexityReveniumMiddleware {
|
|
5
|
+
private client;
|
|
6
|
+
private working;
|
|
7
|
+
private modelName;
|
|
8
|
+
constructor();
|
|
9
|
+
getGenerativeModel(model: string): {
|
|
10
|
+
createChatCompletion: (params: IChatCompletionRequest) => Promise<OpenAI.Chat.Completions.ChatCompletion & {
|
|
11
|
+
_request_id?: string | null | undefined;
|
|
12
|
+
}>;
|
|
13
|
+
createChatCompletionStream: (params: IChatCompletionRequest) => Promise<import("openai/core/streaming").Stream<OpenAI.Chat.Completions.ChatCompletionChunk> & {
|
|
14
|
+
_request_id?: string | null;
|
|
15
|
+
}>;
|
|
16
|
+
createEmbeddings: (_body: Omit<OpenAI.Embeddings.EmbeddingCreateParams, "model">, _options?: RequestOptions) => Promise<void>;
|
|
17
|
+
};
|
|
18
|
+
private embeddings;
|
|
19
|
+
private streamingChatCompletion;
|
|
20
|
+
private chatCompletion;
|
|
21
|
+
private verifyEnv;
|
|
22
|
+
}
|
package/dist/middleware.js
CHANGED
|
@@ -44,7 +44,7 @@ class PerplexityReveniumMiddleware {
|
|
|
44
44
|
stopReason: "END",
|
|
45
45
|
tokenCounts,
|
|
46
46
|
usageMetadata,
|
|
47
|
-
});
|
|
47
|
+
}, true);
|
|
48
48
|
await metering.sendMeteringData(getMetering);
|
|
49
49
|
return result;
|
|
50
50
|
}
|
|
@@ -81,7 +81,7 @@ class PerplexityReveniumMiddleware {
|
|
|
81
81
|
stopReason: "END",
|
|
82
82
|
tokenCounts,
|
|
83
83
|
usageMetadata,
|
|
84
|
-
});
|
|
84
|
+
}, false);
|
|
85
85
|
await metering.sendMeteringData(getMetering);
|
|
86
86
|
return result;
|
|
87
87
|
}
|
package/dist/models/Logger.js
CHANGED
|
@@ -1,35 +1,35 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.logger = exports.Logger = void 0;
|
|
4
|
-
const utils_1 = require("../utils");
|
|
5
|
-
class Logger {
|
|
6
|
-
static debug(message, data) {
|
|
7
|
-
if (this.shouldLog(utils_1.LOG_LEVELS[0])) {
|
|
8
|
-
console.log(`[${utils_1.LOG_LEVELS[0]}] ${message}`, data || "");
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
static info(message, data) {
|
|
12
|
-
if (this.shouldLog(utils_1.LOG_LEVELS[1])) {
|
|
13
|
-
console.log(`[${utils_1.LOG_LEVELS[1]}] ${message}`, data || "");
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
static warning(message, data) {
|
|
17
|
-
if (this.shouldLog(utils_1.LOG_LEVELS[2])) {
|
|
18
|
-
console.warn(`[${utils_1.LOG_LEVELS[2]}] ${message}`, data || "");
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
static error(message, data) {
|
|
22
|
-
if (this.shouldLog(utils_1.LOG_LEVELS[3])) {
|
|
23
|
-
console.error(`[${utils_1.LOG_LEVELS[3]}] ${message}`, data || "");
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
static shouldLog(level) {
|
|
27
|
-
const levels = utils_1.LOG_LEVELS;
|
|
28
|
-
const currentLevel = levels.indexOf(this.logLevel.toUpperCase());
|
|
29
|
-
const messageLevel = levels.indexOf(level);
|
|
30
|
-
return messageLevel >= currentLevel;
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
exports.Logger = Logger;
|
|
34
|
-
Logger.logLevel = process.env.REVENIUM_LOG_LEVEL || "INFO";
|
|
35
|
-
exports.logger = Logger;
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.logger = exports.Logger = void 0;
|
|
4
|
+
const utils_1 = require("../utils");
|
|
5
|
+
class Logger {
|
|
6
|
+
static debug(message, data) {
|
|
7
|
+
if (this.shouldLog(utils_1.LOG_LEVELS[0])) {
|
|
8
|
+
console.log(`[${utils_1.LOG_LEVELS[0]}] ${message}`, data || "");
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
static info(message, data) {
|
|
12
|
+
if (this.shouldLog(utils_1.LOG_LEVELS[1])) {
|
|
13
|
+
console.log(`[${utils_1.LOG_LEVELS[1]}] ${message}`, data || "");
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
static warning(message, data) {
|
|
17
|
+
if (this.shouldLog(utils_1.LOG_LEVELS[2])) {
|
|
18
|
+
console.warn(`[${utils_1.LOG_LEVELS[2]}] ${message}`, data || "");
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
static error(message, data) {
|
|
22
|
+
if (this.shouldLog(utils_1.LOG_LEVELS[3])) {
|
|
23
|
+
console.error(`[${utils_1.LOG_LEVELS[3]}] ${message}`, data || "");
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
static shouldLog(level) {
|
|
27
|
+
const levels = utils_1.LOG_LEVELS;
|
|
28
|
+
const currentLevel = levels.indexOf(this.logLevel.toUpperCase());
|
|
29
|
+
const messageLevel = levels.indexOf(level);
|
|
30
|
+
return messageLevel >= currentLevel;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
exports.Logger = Logger;
|
|
34
|
+
Logger.logLevel = process.env.REVENIUM_LOG_LEVEL || "INFO";
|
|
35
|
+
exports.logger = Logger;
|
|
@@ -4,6 +4,6 @@ export declare class Metering {
|
|
|
4
4
|
private endpoint;
|
|
5
5
|
private apiKey;
|
|
6
6
|
constructor(clientApiKey: string, clientEndpoint: string);
|
|
7
|
-
createMetering(metering: IMeteringRequest): IMeteringResponse;
|
|
7
|
+
createMetering(metering: IMeteringRequest, isStreamed: boolean): IMeteringResponse;
|
|
8
8
|
sendMeteringData: (metering: IMeteringResponse) => Promise<void>;
|
|
9
9
|
}
|
package/dist/models/Metering.js
CHANGED
|
@@ -35,36 +35,44 @@ class Metering {
|
|
|
35
35
|
this.apiKey = clientApiKey;
|
|
36
36
|
this.endpoint = clientEndpoint;
|
|
37
37
|
}
|
|
38
|
-
createMetering(metering) {
|
|
38
|
+
createMetering(metering, isStreamed) {
|
|
39
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w;
|
|
40
|
+
const usageMetadata = metering.usageMetadata;
|
|
39
41
|
const agent = "perplexity";
|
|
40
42
|
return {
|
|
41
|
-
stopReason: metering.stopReason,
|
|
43
|
+
stopReason: (_a = usageMetadata === null || usageMetadata === void 0 ? void 0 : usageMetadata.stopReason) !== null && _a !== void 0 ? _a : metering.stopReason,
|
|
42
44
|
costType: utils_1.COST_TYPE,
|
|
43
|
-
isStreamed
|
|
45
|
+
isStreamed,
|
|
44
46
|
taskType: utils_1.COST_TYPE,
|
|
45
|
-
agent,
|
|
46
|
-
operationType: metering.operationType.toString(),
|
|
47
|
-
inputTokenCount: metering.tokenCounts.inputTokens,
|
|
48
|
-
outputTokenCount: metering.tokenCounts.outputTokens,
|
|
49
|
-
reasoningTokenCount: metering.tokenCounts.reasoningTokens
|
|
50
|
-
cacheCreationTokenCount: metering.tokenCounts.cachedTokens
|
|
51
|
-
cacheReadTokenCount: 0,
|
|
52
|
-
totalTokenCount: metering.tokenCounts.totalTokens,
|
|
53
|
-
organizationId: `my-customer-name-${(0, generateTransactionId_1.generateTransactionId)()}`,
|
|
54
|
-
productId: utils_1.PRODUCT_ID_FREE,
|
|
47
|
+
agent: (_b = usageMetadata === null || usageMetadata === void 0 ? void 0 : usageMetadata.agent) !== null && _b !== void 0 ? _b : agent,
|
|
48
|
+
operationType: (_c = usageMetadata === null || usageMetadata === void 0 ? void 0 : usageMetadata.operationType) !== null && _c !== void 0 ? _c : metering.operationType.toString(),
|
|
49
|
+
inputTokenCount: (_d = usageMetadata === null || usageMetadata === void 0 ? void 0 : usageMetadata.inputTokenCount) !== null && _d !== void 0 ? _d : metering.tokenCounts.inputTokens,
|
|
50
|
+
outputTokenCount: (_e = usageMetadata === null || usageMetadata === void 0 ? void 0 : usageMetadata.outputTokenCount) !== null && _e !== void 0 ? _e : metering.tokenCounts.outputTokens,
|
|
51
|
+
reasoningTokenCount: (_g = (_f = usageMetadata === null || usageMetadata === void 0 ? void 0 : usageMetadata.reasoningTokenCount) !== null && _f !== void 0 ? _f : metering.tokenCounts.reasoningTokens) !== null && _g !== void 0 ? _g : 0,
|
|
52
|
+
cacheCreationTokenCount: (_j = (_h = usageMetadata === null || usageMetadata === void 0 ? void 0 : usageMetadata.cacheCreationTokenCount) !== null && _h !== void 0 ? _h : metering.tokenCounts.cachedTokens) !== null && _j !== void 0 ? _j : 0,
|
|
53
|
+
cacheReadTokenCount: (_k = usageMetadata === null || usageMetadata === void 0 ? void 0 : usageMetadata.cacheReadTokenCount) !== null && _k !== void 0 ? _k : 0,
|
|
54
|
+
totalTokenCount: (_l = usageMetadata === null || usageMetadata === void 0 ? void 0 : usageMetadata.totalTokenCount) !== null && _l !== void 0 ? _l : metering.tokenCounts.totalTokens,
|
|
55
|
+
organizationId: (_m = usageMetadata === null || usageMetadata === void 0 ? void 0 : usageMetadata.organizationId) !== null && _m !== void 0 ? _m : `my-customer-name-${(0, generateTransactionId_1.generateTransactionId)()}`,
|
|
56
|
+
productId: (_o = usageMetadata === null || usageMetadata === void 0 ? void 0 : usageMetadata.productId) !== null && _o !== void 0 ? _o : utils_1.PRODUCT_ID_FREE,
|
|
55
57
|
subscriber: {
|
|
56
|
-
id: `user-${(0, generateTransactionId_1.generateTransactionId)()}`,
|
|
57
|
-
email: `user-@${agent.toLowerCase()}.ai`,
|
|
58
|
-
credential:
|
|
58
|
+
id: (_p = usageMetadata === null || usageMetadata === void 0 ? void 0 : usageMetadata.subscriberId) !== null && _p !== void 0 ? _p : `user-${(0, generateTransactionId_1.generateTransactionId)()}`,
|
|
59
|
+
email: (_q = usageMetadata === null || usageMetadata === void 0 ? void 0 : usageMetadata.subscriberEmail) !== null && _q !== void 0 ? _q : `user-@${agent.toLowerCase()}.ai`,
|
|
60
|
+
credential: (usageMetadata === null || usageMetadata === void 0 ? void 0 : usageMetadata.subscriberCredentialName) &&
|
|
61
|
+
(usageMetadata === null || usageMetadata === void 0 ? void 0 : usageMetadata.subscriberCredential)
|
|
62
|
+
? {
|
|
63
|
+
name: usageMetadata.subscriberCredentialName,
|
|
64
|
+
value: usageMetadata.subscriberCredential,
|
|
65
|
+
}
|
|
66
|
+
: utils_1.CURRENT_CREDENTIAL,
|
|
59
67
|
},
|
|
60
68
|
model: metering.modelName,
|
|
61
|
-
transactionId: (0, generateTransactionId_1.generateTransactionId)(),
|
|
69
|
+
transactionId: (_r = usageMetadata === null || usageMetadata === void 0 ? void 0 : usageMetadata.transactionId) !== null && _r !== void 0 ? _r : (0, generateTransactionId_1.generateTransactionId)(),
|
|
62
70
|
responseTime: (0, formatTimeStamp_1.formatTimestamp)(metering.endTime),
|
|
63
71
|
requestDuration: (0, calculateDurationMs_1.calculateDurationMs)(metering.startTime, metering.endTime),
|
|
64
72
|
provider: agent,
|
|
65
|
-
requestTime: (0, formatTimeStamp_1.formatTimestamp)(metering.startTime),
|
|
66
|
-
completionStartTime: (0, formatTimeStamp_1.formatTimestamp)(metering.endTime),
|
|
67
|
-
timeToFirstToken: 0,
|
|
73
|
+
requestTime: (_t = (_s = usageMetadata === null || usageMetadata === void 0 ? void 0 : usageMetadata.requestTime) === null || _s === void 0 ? void 0 : _s.toString()) !== null && _t !== void 0 ? _t : (0, formatTimeStamp_1.formatTimestamp)(metering.startTime),
|
|
74
|
+
completionStartTime: (_v = (_u = usageMetadata === null || usageMetadata === void 0 ? void 0 : usageMetadata.completionStartTime) === null || _u === void 0 ? void 0 : _u.toString()) !== null && _v !== void 0 ? _v : (0, formatTimeStamp_1.formatTimestamp)(metering.endTime),
|
|
75
|
+
timeToFirstToken: (_w = usageMetadata === null || usageMetadata === void 0 ? void 0 : usageMetadata.timeToFirstToken) !== null && _w !== void 0 ? _w : 0,
|
|
68
76
|
middleware_source: utils_1.MIDDLEWARE_SOURCE,
|
|
69
77
|
};
|
|
70
78
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare function calculateDurationMs(startTime: Date, endTime: Date): number;
|
|
1
|
+
export declare function calculateDurationMs(startTime: Date, endTime: Date): number;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.calculateDurationMs = calculateDurationMs;
|
|
4
|
-
function calculateDurationMs(startTime, endTime) {
|
|
5
|
-
return Math.round(endTime.getTime() - startTime.getTime());
|
|
6
|
-
}
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.calculateDurationMs = calculateDurationMs;
|
|
4
|
+
function calculateDurationMs(startTime, endTime) {
|
|
5
|
+
return Math.round(endTime.getTime() - startTime.getTime());
|
|
6
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { ICredential } from "../../interfaces/credential";
|
|
2
|
-
export declare const PERPLEXITY_API_BASE_URL: string;
|
|
3
|
-
export declare const COST_TYPE: string;
|
|
4
|
-
export declare const MIDDLEWARE_SOURCE: string;
|
|
5
|
-
export declare const PRODUCT_ID_FREE: string;
|
|
6
|
-
export declare const CURRENT_CREDENTIAL: ICredential;
|
|
1
|
+
import { ICredential } from "../../interfaces/credential";
|
|
2
|
+
export declare const PERPLEXITY_API_BASE_URL: string;
|
|
3
|
+
export declare const COST_TYPE: string;
|
|
4
|
+
export declare const MIDDLEWARE_SOURCE: string;
|
|
5
|
+
export declare const PRODUCT_ID_FREE: string;
|
|
6
|
+
export declare const CURRENT_CREDENTIAL: ICredential;
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.CURRENT_CREDENTIAL = exports.PRODUCT_ID_FREE = exports.MIDDLEWARE_SOURCE = exports.COST_TYPE = exports.PERPLEXITY_API_BASE_URL = void 0;
|
|
4
|
-
exports.PERPLEXITY_API_BASE_URL = "https://api.perplexity.ai";
|
|
5
|
-
exports.COST_TYPE = "AI";
|
|
6
|
-
exports.MIDDLEWARE_SOURCE = "node";
|
|
7
|
-
exports.PRODUCT_ID_FREE = "free-trial";
|
|
8
|
-
exports.CURRENT_CREDENTIAL = {
|
|
9
|
-
name: "apiKey",
|
|
10
|
-
value: "keyValue",
|
|
11
|
-
};
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CURRENT_CREDENTIAL = exports.PRODUCT_ID_FREE = exports.MIDDLEWARE_SOURCE = exports.COST_TYPE = exports.PERPLEXITY_API_BASE_URL = void 0;
|
|
4
|
+
exports.PERPLEXITY_API_BASE_URL = "https://api.perplexity.ai";
|
|
5
|
+
exports.COST_TYPE = "AI";
|
|
6
|
+
exports.MIDDLEWARE_SOURCE = "node";
|
|
7
|
+
exports.PRODUCT_ID_FREE = "free-trial";
|
|
8
|
+
exports.CURRENT_CREDENTIAL = {
|
|
9
|
+
name: "apiKey",
|
|
10
|
+
value: "keyValue",
|
|
11
|
+
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const LOG_LEVELS: string[];
|
|
1
|
+
export declare const LOG_LEVELS: string[];
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.LOG_LEVELS = void 0;
|
|
4
|
-
exports.LOG_LEVELS = ["DEBUG", "INFO", "WARNING", "ERROR"];
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.LOG_LEVELS = void 0;
|
|
4
|
+
exports.LOG_LEVELS = ["DEBUG", "INFO", "WARNING", "ERROR"];
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
export declare const PERPLEXITY_REQUIRED_API_KEY_MESSAGE: string;
|
|
2
|
-
export declare const PERPLEXITY_API_KEY_INVALID_MESSAGE: string;
|
|
3
|
-
export declare const PERPLEXITY_METERING_API_KEY_IS_NOT_SET_MESSAGE: string;
|
|
4
|
-
export declare const PERPLEXITY_METERING_BASE_URL_IS_NOT_SET_MESSAGE: string;
|
|
5
|
-
export declare const PERPLEXITY_CLIENT_INITIALIZED_MESSAGE = "Perplexity AI client initialized with middleware";
|
|
1
|
+
export declare const PERPLEXITY_REQUIRED_API_KEY_MESSAGE: string;
|
|
2
|
+
export declare const PERPLEXITY_API_KEY_INVALID_MESSAGE: string;
|
|
3
|
+
export declare const PERPLEXITY_METERING_API_KEY_IS_NOT_SET_MESSAGE: string;
|
|
4
|
+
export declare const PERPLEXITY_METERING_BASE_URL_IS_NOT_SET_MESSAGE: string;
|
|
5
|
+
export declare const PERPLEXITY_CLIENT_INITIALIZED_MESSAGE = "Perplexity AI client initialized with middleware";
|