@revenium/perplexity 1.0.14 → 1.0.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.env.example +3 -3
- package/README.md +504 -448
- package/dist/index.js +19 -0
- package/dist/interfaces/chatCompletionRequest.d.ts +8 -0
- package/dist/interfaces/chatCompletionRequest.js +2 -0
- package/dist/interfaces/credential.d.ts +4 -0
- package/dist/interfaces/credential.js +2 -0
- package/dist/interfaces/meteringRequest.d.ts +13 -0
- package/dist/interfaces/meteringRequest.js +2 -0
- package/dist/interfaces/meteringResponse.d.ts +27 -0
- package/dist/interfaces/meteringResponse.js +2 -0
- package/dist/interfaces/operation.d.ts +4 -0
- package/dist/interfaces/operation.js +8 -0
- package/dist/interfaces/subscriber.d.ts +8 -0
- package/dist/interfaces/subscriber.js +2 -0
- package/dist/interfaces/tokenCounts.d.ts +7 -0
- package/dist/interfaces/tokenCounts.js +2 -0
- package/dist/interfaces/usageMetadata.d.ts +13 -0
- package/dist/interfaces/usageMetadata.js +2 -0
- package/dist/middleware.d.ts +22 -0
- package/dist/middleware.js +129 -0
- package/dist/models/Logger.js +35 -0
- package/dist/models/Metering.d.ts +9 -0
- package/dist/models/Metering.js +72 -0
- package/dist/utils/calculateDurationMs.d.ts +1 -0
- package/dist/utils/calculateDurationMs.js +6 -0
- package/dist/utils/constants/constants.d.ts +6 -0
- package/dist/utils/constants/constants.js +11 -0
- package/dist/utils/constants/messages.d.ts +5 -0
- package/dist/utils/constants/messages.js +8 -0
- package/dist/utils/constants/models.d.ts +1 -0
- package/dist/utils/constants/models.js +21 -0
- package/dist/utils/extractTokenCount.d.ts +2 -0
- package/dist/utils/extractTokenCount.js +28 -0
- package/dist/utils/formatTimeStamp.d.ts +1 -0
- package/dist/utils/formatTimeStamp.js +6 -0
- package/dist/utils/generateTransactionId.d.ts +1 -0
- package/dist/utils/generateTransactionId.js +7 -0
- package/dist/utils/index.d.ts +6 -0
- package/dist/utils/index.js +23 -0
- package/dist/utils/loadEnv.d.ts +1 -0
- package/dist/utils/loadEnv.js +7 -0
- package/dist/utils/safeExtract.d.ts +29 -0
- package/dist/utils/safeExtract.js +67 -0
- package/package.json +46 -45
- package/src/middleware.ts +1 -1
package/dist/index.js
ADDED
|
@@ -0,0 +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);
|
|
@@ -0,0 +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
|
+
}
|
|
@@ -0,0 +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
|
+
}
|
|
@@ -0,0 +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 = {}));
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export interface IUsageMetadata {
|
|
2
|
+
traceId?: string;
|
|
3
|
+
taskType?: string;
|
|
4
|
+
subscriberEmail?: string;
|
|
5
|
+
subscriberId?: string;
|
|
6
|
+
subscriberCredentialName?: string;
|
|
7
|
+
subscriberCredential?: string;
|
|
8
|
+
organizationId?: string;
|
|
9
|
+
subscriptionId?: string;
|
|
10
|
+
productId?: string;
|
|
11
|
+
agent?: string;
|
|
12
|
+
responseQualityScore?: number;
|
|
13
|
+
}
|
|
@@ -0,0 +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
|
+
}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.PerplexityReveniumMiddleware = void 0;
|
|
7
|
+
const openai_1 = __importDefault(require("openai"));
|
|
8
|
+
const utils_1 = require("./utils");
|
|
9
|
+
const Logger_1 = require("./models/Logger");
|
|
10
|
+
const Metering_1 = require("./models/Metering");
|
|
11
|
+
const operation_1 = require("./interfaces/operation");
|
|
12
|
+
const extractTokenCount_1 = require("./utils/extractTokenCount");
|
|
13
|
+
class PerplexityReveniumMiddleware {
|
|
14
|
+
constructor() {
|
|
15
|
+
this.working = true;
|
|
16
|
+
this.modelName = "";
|
|
17
|
+
this.embeddings = async (_body, _options) => { };
|
|
18
|
+
this.streamingChatCompletion = async (params) => {
|
|
19
|
+
var _a;
|
|
20
|
+
const startTime = new Date();
|
|
21
|
+
try {
|
|
22
|
+
const { usageMetadata, ...openaiParams } = params;
|
|
23
|
+
const requestParams = {
|
|
24
|
+
...openaiParams,
|
|
25
|
+
model: this.modelName,
|
|
26
|
+
};
|
|
27
|
+
const result = await ((_a = this.client) === null || _a === void 0 ? void 0 : _a.chat.completions.create({
|
|
28
|
+
...requestParams,
|
|
29
|
+
stream: true,
|
|
30
|
+
}));
|
|
31
|
+
if (!this.working) {
|
|
32
|
+
Logger_1.logger.warning("Metering is not working. Check your configuration.");
|
|
33
|
+
return result;
|
|
34
|
+
}
|
|
35
|
+
const tokenCounts = (0, extractTokenCount_1.extractGoogleAITokenCounts)(result);
|
|
36
|
+
const endTime = new Date();
|
|
37
|
+
Logger_1.logger.info("Metering is working.");
|
|
38
|
+
const metering = new Metering_1.Metering(utils_1.REVENIUM_METERING_API_KEY !== null && utils_1.REVENIUM_METERING_API_KEY !== void 0 ? utils_1.REVENIUM_METERING_API_KEY : "", utils_1.REVENIUM_METERING_BASE_URL !== null && utils_1.REVENIUM_METERING_BASE_URL !== void 0 ? utils_1.REVENIUM_METERING_BASE_URL : "");
|
|
39
|
+
const getMetering = metering.createMetering({
|
|
40
|
+
modelName: this.modelName,
|
|
41
|
+
endTime,
|
|
42
|
+
startTime,
|
|
43
|
+
operationType: operation_1.IOperationType.CHAT,
|
|
44
|
+
stopReason: "END",
|
|
45
|
+
tokenCounts,
|
|
46
|
+
usageMetadata,
|
|
47
|
+
});
|
|
48
|
+
await metering.sendMeteringData(getMetering);
|
|
49
|
+
return result;
|
|
50
|
+
}
|
|
51
|
+
catch (error) {
|
|
52
|
+
throw error;
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
this.chatCompletion = async (params) => {
|
|
56
|
+
var _a;
|
|
57
|
+
const startTime = new Date();
|
|
58
|
+
try {
|
|
59
|
+
const { usageMetadata, ...openaiParams } = params;
|
|
60
|
+
const requestParams = {
|
|
61
|
+
...openaiParams,
|
|
62
|
+
model: this.modelName,
|
|
63
|
+
};
|
|
64
|
+
const result = await ((_a = this.client) === null || _a === void 0 ? void 0 : _a.chat.completions.create({
|
|
65
|
+
...requestParams,
|
|
66
|
+
stream: false,
|
|
67
|
+
}));
|
|
68
|
+
if (!this.working) {
|
|
69
|
+
Logger_1.logger.warning("Metering is not working. Check your configuration.");
|
|
70
|
+
return result;
|
|
71
|
+
}
|
|
72
|
+
const tokenCounts = (0, extractTokenCount_1.extractGoogleAITokenCounts)(result);
|
|
73
|
+
const endTime = new Date();
|
|
74
|
+
Logger_1.logger.info(" Metering is working.");
|
|
75
|
+
const metering = new Metering_1.Metering(utils_1.REVENIUM_METERING_API_KEY !== null && utils_1.REVENIUM_METERING_API_KEY !== void 0 ? utils_1.REVENIUM_METERING_API_KEY : "", utils_1.REVENIUM_METERING_BASE_URL !== null && utils_1.REVENIUM_METERING_BASE_URL !== void 0 ? utils_1.REVENIUM_METERING_BASE_URL : "");
|
|
76
|
+
const getMetering = metering.createMetering({
|
|
77
|
+
modelName: this.modelName,
|
|
78
|
+
endTime,
|
|
79
|
+
startTime,
|
|
80
|
+
operationType: operation_1.IOperationType.CHAT,
|
|
81
|
+
stopReason: "END",
|
|
82
|
+
tokenCounts,
|
|
83
|
+
usageMetadata,
|
|
84
|
+
});
|
|
85
|
+
await metering.sendMeteringData(getMetering);
|
|
86
|
+
return result;
|
|
87
|
+
}
|
|
88
|
+
catch (error) {
|
|
89
|
+
throw error;
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
this.verifyEnv();
|
|
93
|
+
this.client = new openai_1.default({
|
|
94
|
+
apiKey: utils_1.PERPLEXITY_API_KEY,
|
|
95
|
+
baseURL: utils_1.PERPLEXITY_API_BASE_URL,
|
|
96
|
+
});
|
|
97
|
+
Logger_1.logger.info(utils_1.PERPLEXITY_CLIENT_INITIALIZED_MESSAGE);
|
|
98
|
+
}
|
|
99
|
+
getGenerativeModel(model) {
|
|
100
|
+
this.modelName = model;
|
|
101
|
+
if (!utils_1.models.includes(model)) {
|
|
102
|
+
throw new Error(`Model ${model} is not supported`);
|
|
103
|
+
}
|
|
104
|
+
return {
|
|
105
|
+
createChatCompletion: this.chatCompletion,
|
|
106
|
+
createChatCompletionStream: this.streamingChatCompletion,
|
|
107
|
+
createEmbeddings: this.embeddings,
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
verifyEnv() {
|
|
111
|
+
if (!utils_1.PERPLEXITY_API_KEY) {
|
|
112
|
+
Logger_1.logger.error(utils_1.PERPLEXITY_REQUIRED_API_KEY_MESSAGE);
|
|
113
|
+
throw new Error(utils_1.PERPLEXITY_REQUIRED_API_KEY_MESSAGE);
|
|
114
|
+
}
|
|
115
|
+
if (!utils_1.PERPLEXITY_API_KEY.includes("pplx-")) {
|
|
116
|
+
Logger_1.logger.error(utils_1.PERPLEXITY_API_KEY_INVALID_MESSAGE);
|
|
117
|
+
throw new Error(utils_1.PERPLEXITY_API_KEY_INVALID_MESSAGE);
|
|
118
|
+
}
|
|
119
|
+
if (!utils_1.REVENIUM_METERING_API_KEY) {
|
|
120
|
+
Logger_1.logger.warning(utils_1.PERPLEXITY_METERING_API_KEY_IS_NOT_SET_MESSAGE);
|
|
121
|
+
this.working = false;
|
|
122
|
+
}
|
|
123
|
+
if (!utils_1.REVENIUM_METERING_BASE_URL) {
|
|
124
|
+
Logger_1.logger.warning(utils_1.PERPLEXITY_METERING_BASE_URL_IS_NOT_SET_MESSAGE);
|
|
125
|
+
this.working = false;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
exports.PerplexityReveniumMiddleware = PerplexityReveniumMiddleware;
|
|
@@ -0,0 +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;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { IMeteringRequest } from "../interfaces/meteringRequest";
|
|
2
|
+
import { IMeteringResponse } from "../interfaces/meteringResponse";
|
|
3
|
+
export declare class Metering {
|
|
4
|
+
private endpoint;
|
|
5
|
+
private apiKey;
|
|
6
|
+
constructor(clientApiKey: string, clientEndpoint: string);
|
|
7
|
+
createMetering(metering: IMeteringRequest): IMeteringResponse;
|
|
8
|
+
sendMeteringData: (metering: IMeteringResponse) => Promise<void>;
|
|
9
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Metering = void 0;
|
|
4
|
+
const utils_1 = require("../utils");
|
|
5
|
+
const calculateDurationMs_1 = require("../utils/calculateDurationMs");
|
|
6
|
+
const formatTimeStamp_1 = require("../utils/formatTimeStamp");
|
|
7
|
+
const generateTransactionId_1 = require("../utils/generateTransactionId");
|
|
8
|
+
const Logger_1 = require("./Logger");
|
|
9
|
+
class Metering {
|
|
10
|
+
constructor(clientApiKey, clientEndpoint) {
|
|
11
|
+
this.endpoint = "";
|
|
12
|
+
this.apiKey = "";
|
|
13
|
+
this.sendMeteringData = async (metering) => {
|
|
14
|
+
try {
|
|
15
|
+
const response = await fetch(`${this.endpoint}/v2/ai/completions`, {
|
|
16
|
+
method: "POST",
|
|
17
|
+
headers: {
|
|
18
|
+
"Content-Type": "application/json",
|
|
19
|
+
"x-api-key": this.apiKey,
|
|
20
|
+
accept: "application/json",
|
|
21
|
+
},
|
|
22
|
+
body: JSON.stringify(metering),
|
|
23
|
+
});
|
|
24
|
+
if (!response.ok) {
|
|
25
|
+
const errorData = await (response === null || response === void 0 ? void 0 : response.text());
|
|
26
|
+
Logger_1.logger.error(`Metering API request failed with status ${response.status} - ${errorData}`);
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
Logger_1.logger.info(`Metering data sent successfully to Revenium`);
|
|
30
|
+
}
|
|
31
|
+
catch (error) {
|
|
32
|
+
Logger_1.logger.error(`Error to sent metering data ${error}`);
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
this.apiKey = clientApiKey;
|
|
36
|
+
this.endpoint = clientEndpoint;
|
|
37
|
+
}
|
|
38
|
+
createMetering(metering) {
|
|
39
|
+
const agent = "perplexity";
|
|
40
|
+
return {
|
|
41
|
+
stopReason: metering.stopReason,
|
|
42
|
+
costType: utils_1.COST_TYPE,
|
|
43
|
+
isStreamed: false,
|
|
44
|
+
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 || 0,
|
|
50
|
+
cacheCreationTokenCount: metering.tokenCounts.cachedTokens || 0,
|
|
51
|
+
cacheReadTokenCount: 0,
|
|
52
|
+
totalTokenCount: metering.tokenCounts.totalTokens,
|
|
53
|
+
organizationId: `my-customer-name-${(0, generateTransactionId_1.generateTransactionId)()}`,
|
|
54
|
+
productId: utils_1.PRODUCT_ID_FREE,
|
|
55
|
+
subscriber: {
|
|
56
|
+
id: `user-${(0, generateTransactionId_1.generateTransactionId)()}`,
|
|
57
|
+
email: `user-@${agent.toLowerCase()}.ai`,
|
|
58
|
+
credential: utils_1.CURRENT_CREDENTIAL,
|
|
59
|
+
},
|
|
60
|
+
model: metering.modelName,
|
|
61
|
+
transactionId: (0, generateTransactionId_1.generateTransactionId)(),
|
|
62
|
+
responseTime: (0, formatTimeStamp_1.formatTimestamp)(metering.endTime),
|
|
63
|
+
requestDuration: (0, calculateDurationMs_1.calculateDurationMs)(metering.startTime, metering.endTime),
|
|
64
|
+
provider: agent,
|
|
65
|
+
requestTime: (0, formatTimeStamp_1.formatTimestamp)(metering.startTime),
|
|
66
|
+
completionStartTime: (0, formatTimeStamp_1.formatTimestamp)(metering.endTime),
|
|
67
|
+
timeToFirstToken: 0,
|
|
68
|
+
middleware_source: utils_1.MIDDLEWARE_SOURCE,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
exports.Metering = Metering;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function calculateDurationMs(startTime: Date, endTime: Date): number;
|
|
@@ -0,0 +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;
|
|
@@ -0,0 +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
|
+
};
|
|
@@ -0,0 +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";
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PERPLEXITY_CLIENT_INITIALIZED_MESSAGE = exports.PERPLEXITY_METERING_BASE_URL_IS_NOT_SET_MESSAGE = exports.PERPLEXITY_METERING_API_KEY_IS_NOT_SET_MESSAGE = exports.PERPLEXITY_API_KEY_INVALID_MESSAGE = exports.PERPLEXITY_REQUIRED_API_KEY_MESSAGE = void 0;
|
|
4
|
+
exports.PERPLEXITY_REQUIRED_API_KEY_MESSAGE = "PERPLEXITY_API_KEY is required. Set it in environment variables or pass it to the constructor.";
|
|
5
|
+
exports.PERPLEXITY_API_KEY_INVALID_MESSAGE = "PERPLEXITY_API_KEY is invalid. Please check your API key.";
|
|
6
|
+
exports.PERPLEXITY_METERING_API_KEY_IS_NOT_SET_MESSAGE = "REVENIUM_METERING_API_KEY is not set. Metering will not work.";
|
|
7
|
+
exports.PERPLEXITY_METERING_BASE_URL_IS_NOT_SET_MESSAGE = "REVENIUM_METERING_BASE_URL is not set. Metering will not work.";
|
|
8
|
+
exports.PERPLEXITY_CLIENT_INITIALIZED_MESSAGE = "Perplexity AI client initialized with middleware";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const models: string[];
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.models = void 0;
|
|
4
|
+
exports.models = [
|
|
5
|
+
// Perplexity Sonar Models (2025) - Chat Completions
|
|
6
|
+
"sonar", // Lightweight, cost-effective search model
|
|
7
|
+
"sonar-pro", // Advanced search with deeper content understanding
|
|
8
|
+
"sonar-reasoning", // Quick problem-solving with step-by-step logic and search
|
|
9
|
+
"sonar-reasoning-pro", // Enhanced multi-step reasoning with web search
|
|
10
|
+
"sonar-deep-research", // Exhaustive research and detailed report generation
|
|
11
|
+
// Legacy models (deprecated - will be removed)
|
|
12
|
+
"sonar-small",
|
|
13
|
+
"sonar-small-online",
|
|
14
|
+
"sonar-medium-online",
|
|
15
|
+
"sonar-small-chat",
|
|
16
|
+
"sonar-medium-chat",
|
|
17
|
+
// OpenAI Embedding Models (use with OpenAI client directly)
|
|
18
|
+
"text-embedding-3-small", // Most capable small embedding model
|
|
19
|
+
"text-embedding-3-large", // Most capable large embedding model
|
|
20
|
+
"text-embedding-ada-002", // Legacy embedding model
|
|
21
|
+
];
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.extractGoogleAITokenCounts = extractGoogleAITokenCounts;
|
|
4
|
+
const Logger_1 = require("../models/Logger");
|
|
5
|
+
const safeExtract_1 = require("./safeExtract");
|
|
6
|
+
function extractGoogleAITokenCounts(response) {
|
|
7
|
+
var _a;
|
|
8
|
+
console.log;
|
|
9
|
+
try {
|
|
10
|
+
const usageMetadata = response.usage || ((_a = response.response) === null || _a === void 0 ? void 0 : _a.usage);
|
|
11
|
+
if (!usageMetadata)
|
|
12
|
+
return { inputTokens: 0, outputTokens: 0, totalTokens: 0 };
|
|
13
|
+
const inputTokens = safeExtract_1.safeExtract.number(usageMetadata, "prompt_tokens");
|
|
14
|
+
const outputTokens = safeExtract_1.safeExtract.number(usageMetadata, "completion_tokens");
|
|
15
|
+
const totalTokens = safeExtract_1.safeExtract.number(usageMetadata, "total_tokens");
|
|
16
|
+
return {
|
|
17
|
+
inputTokens,
|
|
18
|
+
outputTokens,
|
|
19
|
+
totalTokens,
|
|
20
|
+
cachedTokens: 0,
|
|
21
|
+
reasoningTokens: 0,
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
catch (error) {
|
|
25
|
+
Logger_1.logger.warning("Failed to extract Google AI token counts:", error);
|
|
26
|
+
return { inputTokens: 0, outputTokens: 0, totalTokens: 0 };
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function formatTimestamp(date: Date): string;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function generateTransactionId(): string;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { PERPLEXITY_API_KEY, REVENIUM_METERING_API_KEY, REVENIUM_METERING_BASE_URL } from "./loadEnv";
|
|
2
|
+
import { PERPLEXITY_REQUIRED_API_KEY_MESSAGE, PERPLEXITY_API_KEY_INVALID_MESSAGE, PERPLEXITY_METERING_API_KEY_IS_NOT_SET_MESSAGE, PERPLEXITY_METERING_BASE_URL_IS_NOT_SET_MESSAGE, PERPLEXITY_CLIENT_INITIALIZED_MESSAGE } from "./constants/messages";
|
|
3
|
+
import { LOG_LEVELS } from "./constants/logLevels";
|
|
4
|
+
import { models } from "./constants/models";
|
|
5
|
+
import { PERPLEXITY_API_BASE_URL, COST_TYPE, MIDDLEWARE_SOURCE, PRODUCT_ID_FREE, CURRENT_CREDENTIAL } from "./constants/constants";
|
|
6
|
+
export { PERPLEXITY_API_KEY, REVENIUM_METERING_API_KEY, REVENIUM_METERING_BASE_URL, PERPLEXITY_REQUIRED_API_KEY_MESSAGE, PERPLEXITY_API_KEY_INVALID_MESSAGE, LOG_LEVELS, PERPLEXITY_METERING_API_KEY_IS_NOT_SET_MESSAGE, PERPLEXITY_METERING_BASE_URL_IS_NOT_SET_MESSAGE, PERPLEXITY_CLIENT_INITIALIZED_MESSAGE, models, PERPLEXITY_API_BASE_URL, COST_TYPE, MIDDLEWARE_SOURCE, PRODUCT_ID_FREE, CURRENT_CREDENTIAL, };
|
|
@@ -0,0 +1,23 @@
|
|
|
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 = exports.models = exports.PERPLEXITY_CLIENT_INITIALIZED_MESSAGE = exports.PERPLEXITY_METERING_BASE_URL_IS_NOT_SET_MESSAGE = exports.PERPLEXITY_METERING_API_KEY_IS_NOT_SET_MESSAGE = exports.LOG_LEVELS = exports.PERPLEXITY_API_KEY_INVALID_MESSAGE = exports.PERPLEXITY_REQUIRED_API_KEY_MESSAGE = exports.REVENIUM_METERING_BASE_URL = exports.REVENIUM_METERING_API_KEY = exports.PERPLEXITY_API_KEY = void 0;
|
|
4
|
+
const loadEnv_1 = require("./loadEnv");
|
|
5
|
+
Object.defineProperty(exports, "PERPLEXITY_API_KEY", { enumerable: true, get: function () { return loadEnv_1.PERPLEXITY_API_KEY; } });
|
|
6
|
+
Object.defineProperty(exports, "REVENIUM_METERING_API_KEY", { enumerable: true, get: function () { return loadEnv_1.REVENIUM_METERING_API_KEY; } });
|
|
7
|
+
Object.defineProperty(exports, "REVENIUM_METERING_BASE_URL", { enumerable: true, get: function () { return loadEnv_1.REVENIUM_METERING_BASE_URL; } });
|
|
8
|
+
const messages_1 = require("./constants/messages");
|
|
9
|
+
Object.defineProperty(exports, "PERPLEXITY_REQUIRED_API_KEY_MESSAGE", { enumerable: true, get: function () { return messages_1.PERPLEXITY_REQUIRED_API_KEY_MESSAGE; } });
|
|
10
|
+
Object.defineProperty(exports, "PERPLEXITY_API_KEY_INVALID_MESSAGE", { enumerable: true, get: function () { return messages_1.PERPLEXITY_API_KEY_INVALID_MESSAGE; } });
|
|
11
|
+
Object.defineProperty(exports, "PERPLEXITY_METERING_API_KEY_IS_NOT_SET_MESSAGE", { enumerable: true, get: function () { return messages_1.PERPLEXITY_METERING_API_KEY_IS_NOT_SET_MESSAGE; } });
|
|
12
|
+
Object.defineProperty(exports, "PERPLEXITY_METERING_BASE_URL_IS_NOT_SET_MESSAGE", { enumerable: true, get: function () { return messages_1.PERPLEXITY_METERING_BASE_URL_IS_NOT_SET_MESSAGE; } });
|
|
13
|
+
Object.defineProperty(exports, "PERPLEXITY_CLIENT_INITIALIZED_MESSAGE", { enumerable: true, get: function () { return messages_1.PERPLEXITY_CLIENT_INITIALIZED_MESSAGE; } });
|
|
14
|
+
const logLevels_1 = require("./constants/logLevels");
|
|
15
|
+
Object.defineProperty(exports, "LOG_LEVELS", { enumerable: true, get: function () { return logLevels_1.LOG_LEVELS; } });
|
|
16
|
+
const models_1 = require("./constants/models");
|
|
17
|
+
Object.defineProperty(exports, "models", { enumerable: true, get: function () { return models_1.models; } });
|
|
18
|
+
const constants_1 = require("./constants/constants");
|
|
19
|
+
Object.defineProperty(exports, "PERPLEXITY_API_BASE_URL", { enumerable: true, get: function () { return constants_1.PERPLEXITY_API_BASE_URL; } });
|
|
20
|
+
Object.defineProperty(exports, "COST_TYPE", { enumerable: true, get: function () { return constants_1.COST_TYPE; } });
|
|
21
|
+
Object.defineProperty(exports, "MIDDLEWARE_SOURCE", { enumerable: true, get: function () { return constants_1.MIDDLEWARE_SOURCE; } });
|
|
22
|
+
Object.defineProperty(exports, "PRODUCT_ID_FREE", { enumerable: true, get: function () { return constants_1.PRODUCT_ID_FREE; } });
|
|
23
|
+
Object.defineProperty(exports, "CURRENT_CREDENTIAL", { enumerable: true, get: function () { return constants_1.CURRENT_CREDENTIAL; } });
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const PERPLEXITY_API_KEY: string | undefined, REVENIUM_METERING_API_KEY: string | undefined, REVENIUM_METERING_BASE_URL: string | undefined;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var _a;
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.REVENIUM_METERING_BASE_URL = exports.REVENIUM_METERING_API_KEY = exports.PERPLEXITY_API_KEY = void 0;
|
|
5
|
+
const dotenv_1 = require("dotenv");
|
|
6
|
+
(0, dotenv_1.config)();
|
|
7
|
+
_a = process.env, exports.PERPLEXITY_API_KEY = _a.PERPLEXITY_API_KEY, exports.REVENIUM_METERING_API_KEY = _a.REVENIUM_METERING_API_KEY, exports.REVENIUM_METERING_BASE_URL = _a.REVENIUM_METERING_BASE_URL;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Safe extraction utility functions.
|
|
3
|
+
*/
|
|
4
|
+
export declare namespace safeExtract {
|
|
5
|
+
/**
|
|
6
|
+
* Safely extract a value from an object, returning undefined if the path doesn't exist.
|
|
7
|
+
*/
|
|
8
|
+
function get<T>(obj: any, path: string, defaultValue?: T): T | undefined;
|
|
9
|
+
/**
|
|
10
|
+
* Safely extract a string value, returning empty string if not found.
|
|
11
|
+
*/
|
|
12
|
+
function string(obj: any, path: string): string;
|
|
13
|
+
/**
|
|
14
|
+
* Safely extract a number value, returning 0 if not found.
|
|
15
|
+
*/
|
|
16
|
+
function number(obj: any, path: string): number;
|
|
17
|
+
/**
|
|
18
|
+
* Safely extract a boolean value, returning false if not found.
|
|
19
|
+
*/
|
|
20
|
+
function boolean(obj: any, path: string): boolean;
|
|
21
|
+
/**
|
|
22
|
+
* Safely extract an object value, returning empty object if not found.
|
|
23
|
+
*/
|
|
24
|
+
function object(obj: any, path: string): Record<string, any>;
|
|
25
|
+
/**
|
|
26
|
+
* Safely extract an array value, returning empty array if not found.
|
|
27
|
+
*/
|
|
28
|
+
function array(obj: any, path: string): any[];
|
|
29
|
+
}
|