@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.
Files changed (53) hide show
  1. package/README.md +61 -1
  2. package/dist/index.js +19 -19
  3. package/dist/interfaces/chatCompletionRequest.d.ts +2 -1
  4. package/dist/interfaces/credential.d.ts +4 -4
  5. package/dist/interfaces/credential.js +2 -2
  6. package/dist/interfaces/meteringRequest.d.ts +13 -13
  7. package/dist/interfaces/meteringResponse.d.ts +27 -27
  8. package/dist/interfaces/meteringResponse.js +2 -2
  9. package/dist/interfaces/operation.d.ts +4 -4
  10. package/dist/interfaces/operation.js +8 -8
  11. package/dist/interfaces/subscriber.d.ts +8 -8
  12. package/dist/interfaces/subscriber.js +2 -2
  13. package/dist/interfaces/tokenCounts.d.ts +7 -7
  14. package/dist/interfaces/tokenCounts.js +2 -2
  15. package/dist/interfaces/usageMetadata.d.ts +14 -0
  16. package/dist/middleware.d.ts +22 -22
  17. package/dist/middleware.js +2 -2
  18. package/dist/models/Logger.js +35 -35
  19. package/dist/models/Metering.d.ts +1 -1
  20. package/dist/models/Metering.js +28 -20
  21. package/dist/utils/calculateDurationMs.d.ts +1 -1
  22. package/dist/utils/calculateDurationMs.js +6 -6
  23. package/dist/utils/constants/constants.d.ts +6 -6
  24. package/dist/utils/constants/constants.js +11 -11
  25. package/dist/utils/constants/logLevels.d.ts +1 -1
  26. package/dist/utils/constants/logLevels.js +4 -4
  27. package/dist/utils/constants/messages.d.ts +5 -5
  28. package/dist/utils/constants/messages.js +8 -8
  29. package/dist/utils/constants/models.d.ts +1 -1
  30. package/dist/utils/constants/models.js +21 -21
  31. package/dist/utils/extractTokenCount.d.ts +2 -2
  32. package/dist/utils/extractTokenCount.js +28 -28
  33. package/dist/utils/formatTimeStamp.d.ts +1 -1
  34. package/dist/utils/formatTimeStamp.js +6 -6
  35. package/dist/utils/generateTransactionId.d.ts +1 -1
  36. package/dist/utils/generateTransactionId.js +7 -7
  37. package/dist/utils/index.d.ts +6 -6
  38. package/dist/utils/index.js +23 -23
  39. package/dist/utils/loadEnv.d.ts +1 -1
  40. package/dist/utils/loadEnv.js +7 -7
  41. package/dist/utils/safeExtract.d.ts +29 -29
  42. package/dist/utils/safeExtract.js +67 -67
  43. package/examples/metadata.ts +43 -0
  44. package/package.json +3 -2
  45. package/playground/basic.js +7 -11
  46. package/playground/chat-completions.js +22 -0
  47. package/playground/enhanced.js +6 -9
  48. package/playground/metadata.js +43 -0
  49. package/playground/streaming.js +2 -2
  50. package/src/interfaces/chatCompletionRequest.ts +2 -1
  51. package/src/interfaces/usageMetadata.ts +14 -0
  52. package/src/middleware.ts +24 -18
  53. package/src/models/Metering.ts +48 -20
@@ -1,8 +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";
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";
@@ -1 +1 @@
1
- export declare const models: string[];
1
+ export declare const models: string[];
@@ -1,21 +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
- ];
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
+ ];
@@ -1,2 +1,2 @@
1
- import { ITokenCounts } from "../interfaces/tokenCounts";
2
- export declare function extractGoogleAITokenCounts(response: any): ITokenCounts;
1
+ import { ITokenCounts } from "../interfaces/tokenCounts";
2
+ export declare function extractGoogleAITokenCounts(response: any): ITokenCounts;
@@ -1,28 +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
- }
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
+ }
@@ -1 +1 @@
1
- export declare function formatTimestamp(date: Date): string;
1
+ export declare function formatTimestamp(date: Date): string;
@@ -1,6 +1,6 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.formatTimestamp = formatTimestamp;
4
- function formatTimestamp(date) {
5
- return date.toISOString();
6
- }
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.formatTimestamp = formatTimestamp;
4
+ function formatTimestamp(date) {
5
+ return date.toISOString();
6
+ }
@@ -1 +1 @@
1
- export declare function generateTransactionId(): string;
1
+ export declare function generateTransactionId(): string;
@@ -1,7 +1,7 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.generateTransactionId = generateTransactionId;
4
- const uuid_1 = require("uuid");
5
- function generateTransactionId() {
6
- return (0, uuid_1.v4)();
7
- }
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.generateTransactionId = generateTransactionId;
4
+ const uuid_1 = require("uuid");
5
+ function generateTransactionId() {
6
+ return (0, uuid_1.v4)();
7
+ }
@@ -1,6 +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, };
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, };
@@ -1,23 +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; } });
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; } });
@@ -1 +1 @@
1
- export declare const PERPLEXITY_API_KEY: string | undefined, REVENIUM_METERING_API_KEY: string | undefined, REVENIUM_METERING_BASE_URL: string | undefined;
1
+ export declare const PERPLEXITY_API_KEY: string | undefined, REVENIUM_METERING_API_KEY: string | undefined, REVENIUM_METERING_BASE_URL: string | undefined;
@@ -1,7 +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;
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;
@@ -1,29 +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
- }
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
+ }
@@ -1,67 +1,67 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.safeExtract = void 0;
4
- /**
5
- * Safe extraction utility functions.
6
- */
7
- var safeExtract;
8
- (function (safeExtract) {
9
- /**
10
- * Safely extract a value from an object, returning undefined if the path doesn't exist.
11
- */
12
- function get(obj, path, defaultValue) {
13
- try {
14
- const keys = path.split(".");
15
- let result = obj;
16
- for (const key of keys) {
17
- if (result == null || typeof result !== "object") {
18
- return defaultValue;
19
- }
20
- result = result[key];
21
- }
22
- return result !== null && result !== void 0 ? result : defaultValue;
23
- }
24
- catch {
25
- return defaultValue;
26
- }
27
- }
28
- safeExtract.get = get;
29
- /**
30
- * Safely extract a string value, returning empty string if not found.
31
- */
32
- function string(obj, path) {
33
- return get(obj, path, "") || "";
34
- }
35
- safeExtract.string = string;
36
- /**
37
- * Safely extract a number value, returning 0 if not found.
38
- */
39
- function number(obj, path) {
40
- const value = get(obj, path, 0);
41
- return typeof value === "number" ? value : 0;
42
- }
43
- safeExtract.number = number;
44
- /**
45
- * Safely extract a boolean value, returning false if not found.
46
- */
47
- function boolean(obj, path) {
48
- return get(obj, path, false) || false;
49
- }
50
- safeExtract.boolean = boolean;
51
- /**
52
- * Safely extract an object value, returning empty object if not found.
53
- */
54
- function object(obj, path) {
55
- const value = get(obj, path, {});
56
- return typeof value === "object" && value !== null ? value : {};
57
- }
58
- safeExtract.object = object;
59
- /**
60
- * Safely extract an array value, returning empty array if not found.
61
- */
62
- function array(obj, path) {
63
- const value = get(obj, path, []);
64
- return Array.isArray(value) ? value : [];
65
- }
66
- safeExtract.array = array;
67
- })(safeExtract || (exports.safeExtract = safeExtract = {}));
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.safeExtract = void 0;
4
+ /**
5
+ * Safe extraction utility functions.
6
+ */
7
+ var safeExtract;
8
+ (function (safeExtract) {
9
+ /**
10
+ * Safely extract a value from an object, returning undefined if the path doesn't exist.
11
+ */
12
+ function get(obj, path, defaultValue) {
13
+ try {
14
+ const keys = path.split(".");
15
+ let result = obj;
16
+ for (const key of keys) {
17
+ if (result == null || typeof result !== "object") {
18
+ return defaultValue;
19
+ }
20
+ result = result[key];
21
+ }
22
+ return result !== null && result !== void 0 ? result : defaultValue;
23
+ }
24
+ catch {
25
+ return defaultValue;
26
+ }
27
+ }
28
+ safeExtract.get = get;
29
+ /**
30
+ * Safely extract a string value, returning empty string if not found.
31
+ */
32
+ function string(obj, path) {
33
+ return get(obj, path, "") || "";
34
+ }
35
+ safeExtract.string = string;
36
+ /**
37
+ * Safely extract a number value, returning 0 if not found.
38
+ */
39
+ function number(obj, path) {
40
+ const value = get(obj, path, 0);
41
+ return typeof value === "number" ? value : 0;
42
+ }
43
+ safeExtract.number = number;
44
+ /**
45
+ * Safely extract a boolean value, returning false if not found.
46
+ */
47
+ function boolean(obj, path) {
48
+ return get(obj, path, false) || false;
49
+ }
50
+ safeExtract.boolean = boolean;
51
+ /**
52
+ * Safely extract an object value, returning empty object if not found.
53
+ */
54
+ function object(obj, path) {
55
+ const value = get(obj, path, {});
56
+ return typeof value === "object" && value !== null ? value : {};
57
+ }
58
+ safeExtract.object = object;
59
+ /**
60
+ * Safely extract an array value, returning empty array if not found.
61
+ */
62
+ function array(obj, path) {
63
+ const value = get(obj, path, []);
64
+ return Array.isArray(value) ? value : [];
65
+ }
66
+ safeExtract.array = array;
67
+ })(safeExtract || (exports.safeExtract = safeExtract = {}));
@@ -0,0 +1,43 @@
1
+ import { PerplexityReveniumMiddleware } from "../src";
2
+
3
+ async function metadataRequest() {
4
+ console.log("=".repeat(50));
5
+ try {
6
+ const middleware = new PerplexityReveniumMiddleware();
7
+ const model = middleware.getGenerativeModel("sonar-pro");
8
+ const result = await model.createChatCompletion({
9
+ model: "sonar-pro",
10
+ messages: [{ role: "user", content: "What is the capital of France?" }],
11
+ usageMetadata: {
12
+ taskType: "test",
13
+ subscriberEmail: "test@revenium.ai",
14
+ subscriberId: "123456",
15
+ subscriberCredentialName: "apiKey",
16
+ subscriberCredential: "keyValue",
17
+ organizationId: "123456",
18
+ subscriptionId: "123456",
19
+ productId: "free-trial",
20
+ agent: "perplexity",
21
+ responseQualityScore: 100,
22
+ transactionId: "123456",
23
+ timeToFirstToken: 1000,
24
+ requestTime: new Date(),
25
+ completionStartTime: new Date(),
26
+ operationType: "CHAT",
27
+ inputTokenCount: 10,
28
+ outputTokenCount: 10,
29
+ reasoningTokenCount: 20,
30
+ cacheCreationTokenCount: 0,
31
+ cacheReadTokenCount: 0,
32
+ totalTokenCount: 40,
33
+ responseTime: new Date(),
34
+ requestDuration: 1000,
35
+ stopReason: "END",
36
+ },
37
+ });
38
+ console.log("[METADATA REQUEST]", result.choices[0].message);
39
+ } catch (error) {
40
+ console.log(error);
41
+ }
42
+ }
43
+ metadataRequest();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@revenium/perplexity",
3
- "version": "1.0.19",
3
+ "version": "1.0.21",
4
4
  "description": "NodeJS middleware for perplexity's AI API",
5
5
  "homepage": "https://github.com/revenium/revenium-middleware-perplexity-node#readme",
6
6
  "bugs": {
@@ -20,7 +20,8 @@
20
20
  "e-basic": "npx ts-node examples/basic.ts",
21
21
  "e-streaming": "npx ts-node examples/streaming.ts",
22
22
  "e-chat-completions": "npx ts-node examples/chat-completions.ts",
23
- "e-enhanced": "npx ts-node examples/enhanced.ts"
23
+ "e-enhanced": "npx ts-node examples/enhanced.ts",
24
+ "e-metadata": "npx ts-node examples/metadata.ts"
24
25
  },
25
26
  "keywords": [
26
27
  "revenium",
@@ -1,21 +1,17 @@
1
1
  import { PerplexityReveniumMiddleware } from "@revenium/perplexity";
2
2
 
3
- const basicExample = async () => {
3
+ async function basicRequest() {
4
+ console.log("=".repeat(50));
4
5
  try {
5
6
  const middleware = new PerplexityReveniumMiddleware();
6
7
  const model = middleware.getGenerativeModel("sonar-pro");
7
8
  const result = await model.createChatCompletion({
8
- messages: [
9
- {
10
- role: "user",
11
- content: "Hello world",
12
- },
13
- ],
9
+ model: "sonar-pro",
10
+ messages: [{ role: "user", content: "What is the capital of France?" }],
14
11
  });
15
- console.log("[BASIC EXAMPLE]", result.choices[0].message.content);
12
+ console.log("[BASIC REQUEST]", result.choices[0].message);
16
13
  } catch (error) {
17
14
  console.log(error);
18
15
  }
19
- };
20
-
21
- basicExample();
16
+ }
17
+ basicRequest();
@@ -0,0 +1,22 @@
1
+ import { PerplexityReveniumMiddleware } from "@revenium/perplexity";
2
+
3
+ const chatCompletionsExample = async () => {
4
+ console.log("=".repeat(50));
5
+ try {
6
+ const middleware = new PerplexityReveniumMiddleware();
7
+ const model = middleware.getGenerativeModel("sonar-pro");
8
+ const result = await model.createChatCompletion({
9
+ messages: [
10
+ {
11
+ role: "user",
12
+ content: "Hello world",
13
+ },
14
+ ],
15
+ });
16
+ console.log(result.choices[0].message.content);
17
+ } catch (error) {
18
+ console.log(error);
19
+ }
20
+ };
21
+
22
+ chatCompletionsExample();
@@ -1,23 +1,20 @@
1
1
  import { PerplexityReveniumMiddleware } from "@revenium/perplexity";
2
2
 
3
- const enhancedExample = async () => {
3
+ async function enhancedRequest() {
4
+ console.log("=".repeat(50));
4
5
  try {
5
6
  const middleware = new PerplexityReveniumMiddleware();
6
7
  const model = middleware.getGenerativeModel("sonar-pro");
7
8
  const result = await model.createChatCompletion({
8
9
  messages: [
9
- {
10
- role: "user",
11
- content: "Explain quantum computing in simple terms",
12
- },
10
+ { role: "user", content: "Explain quantum computing in simple terms" },
13
11
  ],
14
12
  max_tokens: 100,
15
13
  temperature: 0.7,
16
14
  });
17
- console.log("[ENHANCED EXAMPLE]", result.choices[0].message.content);
15
+ console.log("[ENHANCED REQUEST]", result.choices[0].message.content);
18
16
  } catch (error) {
19
17
  console.log(error);
20
18
  }
21
- };
22
-
23
- enhancedExample();
19
+ }
20
+ enhancedRequest();
@@ -0,0 +1,43 @@
1
+ import { PerplexityReveniumMiddleware } from "@revenium/perplexity";
2
+
3
+ async function metadataRequest() {
4
+ console.log("=".repeat(50));
5
+ try {
6
+ const middleware = new PerplexityReveniumMiddleware();
7
+ const model = middleware.getGenerativeModel("sonar-pro");
8
+ const result = await model.createChatCompletion({
9
+ model: "sonar-pro",
10
+ messages: [{ role: "user", content: "What is the capital of France?" }],
11
+ usageMetadata: {
12
+ taskType: "test",
13
+ subscriberEmail: "test@revenium.ai",
14
+ subscriberId: "123456",
15
+ subscriberCredentialName: "apiKey",
16
+ subscriberCredential: "keyValue",
17
+ organizationId: "123456",
18
+ subscriptionId: "123456",
19
+ productId: "free-trial",
20
+ agent: "perplexity",
21
+ responseQualityScore: 100,
22
+ transactionId: "123456",
23
+ timeToFirstToken: 1000,
24
+ requestTime: new Date(),
25
+ completionStartTime: new Date(),
26
+ operationType: "CHAT",
27
+ inputTokenCount: 10,
28
+ outputTokenCount: 10,
29
+ reasoningTokenCount: 20,
30
+ cacheCreationTokenCount: 0,
31
+ cacheReadTokenCount: 0,
32
+ totalTokenCount: 40,
33
+ responseTime: new Date(),
34
+ requestDuration: 1000,
35
+ stopReason: "END",
36
+ },
37
+ });
38
+ console.log("[METADATA REQUEST]", result.choices[0].message);
39
+ } catch (error) {
40
+ console.log(error);
41
+ }
42
+ }
43
+ metadataRequest();