@revenium/perplexity 2.0.3 → 2.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +47 -10
- package/README.md +144 -720
- package/SECURITY.md +34 -0
- package/dist/cjs/constants/models.js +38 -0
- package/dist/cjs/constants/models.js.map +1 -0
- package/dist/cjs/core/client/index.js +13 -0
- package/dist/cjs/core/client/index.js.map +1 -0
- package/dist/cjs/core/client/manager.js +85 -0
- package/dist/cjs/core/client/manager.js.map +1 -0
- package/dist/cjs/core/config/index.js +21 -0
- package/dist/cjs/core/config/index.js.map +1 -0
- package/dist/cjs/core/config/loader.js +48 -0
- package/dist/cjs/core/config/loader.js.map +1 -0
- package/dist/cjs/core/config/manager.js +77 -0
- package/dist/cjs/core/config/manager.js.map +1 -0
- package/dist/cjs/core/config/validator.js +45 -0
- package/dist/cjs/core/config/validator.js.map +1 -0
- package/dist/cjs/core/middleware/index.js +14 -0
- package/dist/cjs/core/middleware/index.js.map +1 -0
- package/dist/cjs/core/middleware/interfaces.js +94 -0
- package/dist/cjs/core/middleware/interfaces.js.map +1 -0
- package/dist/cjs/core/middleware/revenium-client.js +44 -0
- package/dist/cjs/core/middleware/revenium-client.js.map +1 -0
- package/dist/cjs/core/middleware/streaming-wrapper.js +80 -0
- package/dist/cjs/core/middleware/streaming-wrapper.js.map +1 -0
- package/dist/cjs/core/providers/detector.js +38 -0
- package/dist/cjs/core/providers/detector.js.map +1 -0
- package/dist/cjs/core/providers/index.js +14 -0
- package/dist/cjs/core/providers/index.js.map +1 -0
- package/dist/cjs/core/tracking/api-client.js +67 -0
- package/dist/cjs/core/tracking/api-client.js.map +1 -0
- package/dist/cjs/core/tracking/index.js +21 -0
- package/dist/cjs/core/tracking/index.js.map +1 -0
- package/dist/cjs/core/tracking/payload-builder.js +95 -0
- package/dist/cjs/core/tracking/payload-builder.js.map +1 -0
- package/dist/cjs/core/tracking/usage-tracker.js +83 -0
- package/dist/cjs/core/tracking/usage-tracker.js.map +1 -0
- package/dist/cjs/index.js +60 -54
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/types/function-parameters.js +14 -0
- package/dist/cjs/types/function-parameters.js.map +1 -0
- package/dist/cjs/types/index.js +20 -16
- package/dist/cjs/types/index.js.map +1 -0
- package/dist/cjs/utils/constants.js +20 -0
- package/dist/cjs/utils/constants.js.map +1 -0
- package/dist/cjs/utils/error-handler.js +194 -0
- package/dist/cjs/utils/error-handler.js.map +1 -0
- package/dist/cjs/utils/metadata-builder.js +191 -0
- package/dist/cjs/utils/metadata-builder.js.map +1 -0
- package/dist/cjs/utils/stop-reason-mapper.js +74 -0
- package/dist/cjs/utils/stop-reason-mapper.js.map +1 -0
- package/dist/cjs/utils/transaction-id.js +19 -0
- package/dist/cjs/utils/transaction-id.js.map +1 -0
- package/dist/cjs/utils/url-builder.js +57 -0
- package/dist/cjs/utils/url-builder.js.map +1 -0
- package/dist/esm/constants/models.js +35 -0
- package/dist/esm/constants/models.js.map +1 -0
- package/dist/esm/core/client/index.js +5 -0
- package/dist/esm/core/client/index.js.map +1 -0
- package/dist/esm/core/client/manager.js +78 -0
- package/dist/esm/core/client/manager.js.map +1 -0
- package/dist/esm/core/config/index.js +11 -0
- package/dist/esm/core/config/index.js.map +1 -0
- package/dist/esm/core/config/loader.js +45 -0
- package/dist/esm/core/config/loader.js.map +1 -0
- package/dist/esm/core/config/manager.js +70 -0
- package/dist/esm/core/config/manager.js.map +1 -0
- package/dist/esm/core/config/validator.js +42 -0
- package/dist/esm/core/config/validator.js.map +1 -0
- package/dist/esm/core/middleware/index.js +7 -0
- package/dist/esm/core/middleware/index.js.map +1 -0
- package/dist/esm/core/middleware/interfaces.js +89 -0
- package/dist/esm/core/middleware/interfaces.js.map +1 -0
- package/dist/esm/core/middleware/revenium-client.js +37 -0
- package/dist/esm/core/middleware/revenium-client.js.map +1 -0
- package/dist/esm/core/middleware/streaming-wrapper.js +76 -0
- package/dist/esm/core/middleware/streaming-wrapper.js.map +1 -0
- package/dist/esm/core/providers/detector.js +34 -0
- package/dist/esm/core/providers/detector.js.map +1 -0
- package/dist/esm/core/providers/index.js +9 -0
- package/dist/esm/core/providers/index.js.map +1 -0
- package/dist/esm/core/tracking/api-client.js +64 -0
- package/dist/esm/core/tracking/api-client.js.map +1 -0
- package/dist/esm/core/tracking/index.js +13 -0
- package/dist/esm/core/tracking/index.js.map +1 -0
- package/dist/esm/core/tracking/payload-builder.js +92 -0
- package/dist/esm/core/tracking/payload-builder.js.map +1 -0
- package/dist/esm/core/tracking/usage-tracker.js +79 -0
- package/dist/esm/core/tracking/usage-tracker.js.map +1 -0
- package/dist/esm/index.js +46 -36
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/types/function-parameters.js +13 -0
- package/dist/esm/types/function-parameters.js.map +1 -0
- package/dist/esm/types/index.js +6 -15
- package/dist/esm/types/index.js.map +1 -0
- package/dist/esm/utils/constants.js +17 -0
- package/dist/esm/utils/constants.js.map +1 -0
- package/dist/esm/utils/error-handler.js +182 -0
- package/dist/esm/utils/error-handler.js.map +1 -0
- package/dist/esm/utils/metadata-builder.js +183 -0
- package/dist/esm/utils/metadata-builder.js.map +1 -0
- package/dist/esm/utils/stop-reason-mapper.js +69 -0
- package/dist/esm/utils/stop-reason-mapper.js.map +1 -0
- package/dist/esm/utils/transaction-id.js +16 -0
- package/dist/esm/utils/transaction-id.js.map +1 -0
- package/dist/esm/utils/url-builder.js +53 -0
- package/dist/esm/utils/url-builder.js.map +1 -0
- package/dist/types/constants/models.d.ts +39 -0
- package/dist/types/constants/models.d.ts.map +1 -0
- package/dist/types/core/client/index.d.ts +5 -0
- package/dist/types/core/client/index.d.ts.map +1 -0
- package/dist/types/core/client/manager.d.ts +44 -0
- package/dist/types/core/client/manager.d.ts.map +1 -0
- package/dist/types/core/config/index.d.ts +10 -0
- package/dist/types/core/config/index.d.ts.map +1 -0
- package/dist/types/core/config/loader.d.ts +13 -0
- package/dist/types/core/config/loader.d.ts.map +1 -0
- package/dist/types/core/config/manager.d.ts +28 -0
- package/dist/types/core/config/manager.d.ts.map +1 -0
- package/dist/types/core/config/validator.d.ts +12 -0
- package/dist/types/core/config/validator.d.ts.map +1 -0
- package/dist/types/core/middleware/index.d.ts +7 -0
- package/dist/types/core/middleware/index.d.ts.map +1 -0
- package/dist/types/core/middleware/interfaces.d.ts +35 -0
- package/dist/types/core/middleware/interfaces.d.ts.map +1 -0
- package/dist/types/core/middleware/revenium-client.d.ts +25 -0
- package/dist/types/core/middleware/revenium-client.d.ts.map +1 -0
- package/dist/types/core/middleware/streaming-wrapper.d.ts +21 -0
- package/dist/types/core/middleware/streaming-wrapper.d.ts.map +1 -0
- package/dist/types/core/providers/detector.d.ts +24 -0
- package/dist/types/core/providers/detector.d.ts.map +1 -0
- package/dist/types/core/providers/index.d.ts +8 -0
- package/dist/types/core/providers/index.d.ts.map +1 -0
- package/dist/types/core/tracking/api-client.d.ts +17 -0
- package/dist/types/core/tracking/api-client.d.ts.map +1 -0
- package/dist/types/core/tracking/index.d.ts +11 -0
- package/dist/types/core/tracking/index.d.ts.map +1 -0
- package/dist/types/core/tracking/payload-builder.d.ts +24 -0
- package/dist/types/core/tracking/payload-builder.d.ts.map +1 -0
- package/dist/types/core/tracking/usage-tracker.d.ts +30 -0
- package/dist/types/core/tracking/usage-tracker.d.ts.map +1 -0
- package/dist/types/index.d.ts +56 -27
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/types/function-parameters.d.ts +185 -0
- package/dist/types/types/function-parameters.d.ts.map +1 -0
- package/dist/types/types/index.d.ts +108 -137
- package/dist/types/types/index.d.ts.map +1 -0
- package/dist/types/utils/constants.d.ts +9 -0
- package/dist/types/utils/constants.d.ts.map +1 -0
- package/dist/types/utils/error-handler.d.ts +95 -0
- package/dist/types/utils/error-handler.d.ts.map +1 -0
- package/dist/types/utils/metadata-builder.d.ts +65 -0
- package/dist/types/utils/metadata-builder.d.ts.map +1 -0
- package/dist/types/utils/stop-reason-mapper.d.ts +29 -0
- package/dist/types/utils/stop-reason-mapper.d.ts.map +1 -0
- package/dist/types/utils/transaction-id.d.ts +14 -0
- package/dist/types/utils/transaction-id.d.ts.map +1 -0
- package/dist/types/utils/url-builder.d.ts +22 -0
- package/dist/types/utils/url-builder.d.ts.map +1 -0
- package/examples/README.md +136 -233
- package/examples/advanced.ts +123 -0
- package/examples/basic.ts +36 -46
- package/examples/getting_started.ts +41 -0
- package/examples/metadata.ts +55 -59
- package/examples/stream.ts +53 -0
- package/package.json +12 -17
- package/dist/cjs/core/config/perplexity-config.js +0 -45
- package/dist/cjs/core/config/revenium-config.js +0 -80
- package/dist/cjs/core/tracking/metering.js +0 -131
- package/dist/cjs/core/wrapper/perplexity-client.js +0 -177
- package/dist/cjs/utils/logger.js +0 -23
- package/dist/esm/core/config/perplexity-config.js +0 -40
- package/dist/esm/core/config/revenium-config.js +0 -72
- package/dist/esm/core/tracking/metering.js +0 -126
- package/dist/esm/core/wrapper/perplexity-client.js +0 -170
- package/dist/esm/utils/logger.js +0 -20
- package/dist/types/core/config/perplexity-config.d.ts +0 -24
- package/dist/types/core/config/revenium-config.d.ts +0 -37
- package/dist/types/core/tracking/metering.d.ts +0 -31
- package/dist/types/core/wrapper/perplexity-client.d.ts +0 -32
- package/dist/types/utils/logger.d.ts +0 -10
- package/examples/chat.ts +0 -87
- package/examples/streaming.ts +0 -61
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Payload Builder Module
|
|
3
|
+
*
|
|
4
|
+
* Handles construction of Revenium API payloads.
|
|
5
|
+
* Extracted from tracking.ts for single responsibility.
|
|
6
|
+
*/
|
|
7
|
+
import { randomUUID } from "crypto";
|
|
8
|
+
import { getLogger } from "../config/index.js";
|
|
9
|
+
import { mapStopReason } from "../../utils/stop-reason-mapper.js";
|
|
10
|
+
import { buildMetadataFields } from "../../utils/metadata-builder.js";
|
|
11
|
+
import { getProviderMetadata } from "../providers/index.js";
|
|
12
|
+
// Global logger
|
|
13
|
+
const logger = getLogger();
|
|
14
|
+
/**
|
|
15
|
+
* Build payload for Revenium API
|
|
16
|
+
*
|
|
17
|
+
* This shared payload builder eliminates payload duplication.
|
|
18
|
+
* Handles CHAT operation type for Perplexity.
|
|
19
|
+
*
|
|
20
|
+
* @param operationType - Type of operation (CHAT)
|
|
21
|
+
* @param response - API response from Perplexity
|
|
22
|
+
* @param request - Original request parameters
|
|
23
|
+
* @param startTime - Request start timestamp
|
|
24
|
+
* @param duration - Request duration in milliseconds
|
|
25
|
+
* @param providerInfo - Provider information (always Perplexity)
|
|
26
|
+
* @param timeToFirstToken - Time to first token in milliseconds (for streaming)
|
|
27
|
+
* @returns Constructed payload for Revenium API
|
|
28
|
+
*/
|
|
29
|
+
export function buildPayload(operationType, response, request, startTime, duration, providerInfo, timeToFirstToken) {
|
|
30
|
+
const now = new Date().toISOString();
|
|
31
|
+
const requestTime = new Date(startTime).toISOString();
|
|
32
|
+
const usage = response.usage;
|
|
33
|
+
if (!usage) {
|
|
34
|
+
throw new Error("Response usage data is missing");
|
|
35
|
+
}
|
|
36
|
+
const modelName = response.model;
|
|
37
|
+
// Get provider metadata (always Perplexity)
|
|
38
|
+
const providerMetadata = providerInfo
|
|
39
|
+
? getProviderMetadata()
|
|
40
|
+
: { provider: "Perplexity", modelSource: "PERPLEXITY" };
|
|
41
|
+
// Build metadata fields using utility (eliminates repetitive spreading)
|
|
42
|
+
const metadataFields = buildMetadataFields(request.usageMetadata);
|
|
43
|
+
// Map Perplexity cost object to Revenium cost fields
|
|
44
|
+
const costFields = usage.cost
|
|
45
|
+
? {
|
|
46
|
+
inputTokenCost: usage.cost.input_tokens_cost,
|
|
47
|
+
outputTokenCost: usage.cost.output_tokens_cost,
|
|
48
|
+
totalCost: usage.cost.total_cost,
|
|
49
|
+
}
|
|
50
|
+
: {
|
|
51
|
+
// Let Revenium calculate costs if not provided
|
|
52
|
+
inputTokenCost: undefined,
|
|
53
|
+
outputTokenCost: undefined,
|
|
54
|
+
totalCost: undefined,
|
|
55
|
+
};
|
|
56
|
+
// Common fields for all operations
|
|
57
|
+
const commonPayload = {
|
|
58
|
+
costType: "AI",
|
|
59
|
+
model: modelName,
|
|
60
|
+
responseTime: now,
|
|
61
|
+
requestDuration: duration,
|
|
62
|
+
provider: providerMetadata.provider,
|
|
63
|
+
modelSource: providerMetadata.modelSource,
|
|
64
|
+
requestTime,
|
|
65
|
+
completionStartTime: now,
|
|
66
|
+
// Common token counts
|
|
67
|
+
inputTokenCount: usage.prompt_tokens,
|
|
68
|
+
totalTokenCount: usage.total_tokens,
|
|
69
|
+
// Metadata fields (processed by utility)
|
|
70
|
+
...metadataFields,
|
|
71
|
+
// Fixed middleware source identifier (spec format: revenium-{provider}-{language})
|
|
72
|
+
middlewareSource: "revenium-perplexity-node",
|
|
73
|
+
// Cost fields from Perplexity
|
|
74
|
+
...costFields,
|
|
75
|
+
};
|
|
76
|
+
// Chat-specific fields
|
|
77
|
+
return {
|
|
78
|
+
...commonPayload,
|
|
79
|
+
operationType: "CHAT",
|
|
80
|
+
transactionId: response.id || `chat-${randomUUID()}`,
|
|
81
|
+
outputTokenCount: usage.completion_tokens || 0,
|
|
82
|
+
// Perplexity doesn't support reasoning or caching
|
|
83
|
+
reasoningTokenCount: undefined,
|
|
84
|
+
cacheCreationTokenCount: undefined,
|
|
85
|
+
cacheReadTokenCount: undefined,
|
|
86
|
+
stopReason: mapStopReason(response.choices?.[0]?.finish_reason, logger),
|
|
87
|
+
isStreamed: Boolean(request.stream),
|
|
88
|
+
// Time to first token (for streaming requests)
|
|
89
|
+
timeToFirstToken: timeToFirstToken,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
//# sourceMappingURL=payload-builder.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"payload-builder.js","sourceRoot":"","sources":["../../../../src/core/tracking/payload-builder.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,QAA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C,UAAU;SACjC;QACH,CAAC,CAAC;YACE,+CAA+C;YAC/C,cAAc,EAAE,SAAS;YACzB,eAAe,EAAE,SAAS;YAC1B,SAAS,EAAE,SAAS;SACrB,CAAC;IAEN,mCAAmC;IACnC,MAAM,aAAa,GAAG;QACpB,QAAQ,EAAE,IAAa;QACvB,KAAK,EAAE,SAAS;QAChB,YAAY,EAAE,GAAG;QACjB,eAAe,EAAE,QAAQ;QACzB,QAAQ,EAAE,gBAAgB,CAAC,QAAQ;QACnC,WAAW,EAAE,gBAAgB,CAAC,WAAW;QACzC,WAAW;QACX,mBAAmB,EAAE,GAAG;QAExB,sBAAsB;QACtB,eAAe,EAAE,KAAK,CAAC,aAAa;QACpC,eAAe,EAAE,KAAK,CAAC,YAAY;QAEnC,yCAAyC;QACzC,GAAG,cAAc;QAEjB,mFAAmF;QACnF,gBAAgB,EAAE,0BAA0B;QAE5C,8BAA8B;QAC9B,GAAG,UAAU;KACd,CAAC;IAEF,uBAAuB;IACvB,OAAO;QACL,GAAG,aAAa;QAChB,aAAa,EAAE,MAAM;QACrB,aAAa,EAAE,QAAQ,CAAC,EAAE,IAAI,QAAQ,UAAU,EAAE,EAAE;QACpD,gBAAgB,EAAE,KAAK,CAAC,iBAAiB,IAAI,CAAC;QAC9C,kDAAkD;QAClD,mBAAmB,EAAE,SAAS;QAC9B,uBAAuB,EAAE,SAAS;QAClC,mBAAmB,EAAE,SAAS;QAC9B,UAAU,EAAE,aAAa,CACvB,QAAQ,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,EAAE,aAAa,EACpC,MAAM,CACP;QACD,UAAU,EAAE,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC;QACnC,+CAA+C;QAC/C,gBAAgB,EAAE,gBAAgB;KACnC,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Usage Tracker Module
|
|
3
|
+
*
|
|
4
|
+
* High-level tracking functions that combine payload building and API communication.
|
|
5
|
+
* Extracted from tracking.ts for better organization.
|
|
6
|
+
*/
|
|
7
|
+
import { getLogger } from "../config/index.js";
|
|
8
|
+
import { sendToRevenium } from "./api-client.js";
|
|
9
|
+
import { buildPayload } from "./payload-builder.js";
|
|
10
|
+
import { safeAsyncOperation } from "../../utils/error-handler.js";
|
|
11
|
+
// Global logger
|
|
12
|
+
const logger = getLogger();
|
|
13
|
+
/**
|
|
14
|
+
* Chat completions tracking - thin wrapper
|
|
15
|
+
*/
|
|
16
|
+
export async function sendReveniumMetrics(response, request, startTime, duration, providerInfo, timeToFirstToken) {
|
|
17
|
+
await safeAsyncOperation(async () => {
|
|
18
|
+
const payload = buildPayload("CHAT", response, request, startTime, duration, providerInfo, timeToFirstToken);
|
|
19
|
+
await sendToRevenium(payload);
|
|
20
|
+
}, "Chat completion tracking", {
|
|
21
|
+
logError: true,
|
|
22
|
+
rethrow: false, // Don't rethrow to maintain fire-and-forget behavior
|
|
23
|
+
messagePrefix: "Chat completion tracking failed: ",
|
|
24
|
+
}, logger);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Fire-and-forget wrapper for chat completions
|
|
28
|
+
* Constructs DTO objects from simple tracking data to call sendReveniumMetrics
|
|
29
|
+
*/
|
|
30
|
+
export function trackUsageAsync(trackingData) {
|
|
31
|
+
// Build DTO response object from tracking data
|
|
32
|
+
const dtoResponse = {
|
|
33
|
+
id: trackingData.requestId,
|
|
34
|
+
model: trackingData.model,
|
|
35
|
+
object: "chat.completion",
|
|
36
|
+
created: Math.floor(Date.now() / 1000),
|
|
37
|
+
usage: {
|
|
38
|
+
prompt_tokens: trackingData.promptTokens,
|
|
39
|
+
completion_tokens: trackingData.completionTokens,
|
|
40
|
+
total_tokens: trackingData.totalTokens,
|
|
41
|
+
cost: trackingData.cost,
|
|
42
|
+
},
|
|
43
|
+
choices: [
|
|
44
|
+
{
|
|
45
|
+
index: 0,
|
|
46
|
+
finish_reason: trackingData.finishReason,
|
|
47
|
+
message: {
|
|
48
|
+
role: "assistant",
|
|
49
|
+
content: "",
|
|
50
|
+
},
|
|
51
|
+
},
|
|
52
|
+
],
|
|
53
|
+
};
|
|
54
|
+
// Build DTO request object from tracking data
|
|
55
|
+
const dtoRequest = {
|
|
56
|
+
model: trackingData.model,
|
|
57
|
+
messages: [],
|
|
58
|
+
usageMetadata: trackingData.usageMetadata,
|
|
59
|
+
stream: trackingData.isStreamed,
|
|
60
|
+
};
|
|
61
|
+
const startTime = Date.now() - trackingData.duration;
|
|
62
|
+
sendReveniumMetrics(dtoResponse, dtoRequest, startTime, trackingData.duration, trackingData.providerInfo, trackingData.timeToFirstToken)
|
|
63
|
+
.then(() => {
|
|
64
|
+
logger.debug("Usage tracking completed successfully", {
|
|
65
|
+
requestId: trackingData.requestId,
|
|
66
|
+
model: trackingData.model,
|
|
67
|
+
totalTokens: trackingData.totalTokens,
|
|
68
|
+
isStreamed: trackingData.isStreamed,
|
|
69
|
+
});
|
|
70
|
+
})
|
|
71
|
+
.catch((error) => {
|
|
72
|
+
logger.warn("Usage tracking failed", {
|
|
73
|
+
error: error instanceof Error ? error.message : String(error),
|
|
74
|
+
requestId: trackingData.requestId,
|
|
75
|
+
model: trackingData.model,
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
//# sourceMappingURL=usage-tracker.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"usage-tracker.js","sourceRoot":"","sources":["../../../../src/core/tracking/usage-tracker.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AASH,OAAO,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAC/C,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AACjD,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AACpD,OAAO,EAAE,kBAAkB,EAAE,MAAM,8BAA8B,CAAC;AAElE,gBAAgB;AAChB,MAAM,MAAM,GAAG,SAAS,EAAE,CAAC;AAE3B;;GAE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,EAAE,YAAY,CAAC,aAAa;QACzC,MAAM,EAAE,YAAY,CAAC,UAAU;KAChC,CAAC;IAEF,MAAM,SAAS,GAAG,IAAI,CAAC,GAAG,EAAE,GAAG,YAAY,CAAC,QAAQ,CAAC;IAErD,mBAAmB,CACjB,WAAW,EACX,UAAU,EACV,SAAS,EACT,YAAY,CAAC,QAAQ,EACrB,YAAY,CAAC,YAAY,EACzB,YAAY,CAAC,gBAAgB,CAC9B;SACE,IAAI,CAAC,GAAG,EAAE;QACT,MAAM,CAAC,KAAK,CAAC,uCAAuC,EAAE;YACpD,SAAS,EAAE,YAAY,CAAC,SAAS;YACjC,KAAK,EAAE,YAAY,CAAC,KAAK;YACzB,WAAW,EAAE,YAAY,CAAC,WAAW;YACrC,UAAU,EAAE,YAAY,CAAC,UAAU;SACpC,CAAC,CAAC;IACL,CAAC,CAAC;SACD,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE;QACf,MAAM,CAAC,IAAI,CAAC,uBAAuB,EAAE;YACnC,KAAK,EAAE,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC;YAC7D,SAAS,EAAE,YAAY,CAAC,SAAS;YACjC,KAAK,EAAE,YAAY,CAAC,KAAK;SAC1B,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACP,CAAC"}
|
package/dist/esm/index.js
CHANGED
|
@@ -1,44 +1,54 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Revenium Middleware for
|
|
2
|
+
* Revenium Perplexity Middleware for TypeScript
|
|
3
3
|
*
|
|
4
|
-
*
|
|
4
|
+
* This middleware tracks Perplexity AI usage and sends metrics to Revenium.
|
|
5
|
+
* Uses Go-aligned API pattern with Initialize/GetClient.
|
|
5
6
|
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
* initializePerplexityFromEnv,
|
|
11
|
-
* createChatCompletion,
|
|
12
|
-
* } from '@revenium/perplexity';
|
|
7
|
+
* Environment Variables:
|
|
8
|
+
* REVENIUM_METERING_API_KEY=hak_your_api_key
|
|
9
|
+
* REVENIUM_METERING_BASE_URL=https://api.revenium.ai (optional)
|
|
10
|
+
* PERPLEXITY_API_KEY=pplx_your_perplexity_key
|
|
13
11
|
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
* initializePerplexityFromEnv();
|
|
12
|
+
* Usage:
|
|
13
|
+
* import { Initialize, GetClient } from "@revenium/perplexity";
|
|
17
14
|
*
|
|
18
|
-
* //
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
15
|
+
* // Initialize from environment
|
|
16
|
+
* Initialize();
|
|
17
|
+
*
|
|
18
|
+
* // Get client
|
|
19
|
+
* const client = GetClient();
|
|
23
20
|
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
21
|
+
* // Use client
|
|
22
|
+
* const response = await client.chat().completions().create({
|
|
23
|
+
* model: "sonar",
|
|
24
|
+
* messages: [{ role: "user", content: "Hello!" }]
|
|
25
|
+
* }, {
|
|
26
|
+
* organizationId: "my-org",
|
|
27
|
+
* productId: "my-app",
|
|
28
|
+
* subscriber: {
|
|
29
|
+
* id: "user-123",
|
|
30
|
+
* email: "user@my-org.com"
|
|
31
|
+
* }
|
|
32
|
+
* });
|
|
33
|
+
*/
|
|
34
|
+
/**
|
|
35
|
+
* Main API - Go-aligned pattern
|
|
36
|
+
*/
|
|
37
|
+
export { Initialize, GetClient, IsInitialized, Reset, Configure, } from "./core/client/index.js";
|
|
38
|
+
/**
|
|
39
|
+
* Middleware classes
|
|
40
|
+
*/
|
|
41
|
+
export { ReveniumPerplexity, ChatInterface, CompletionsInterface, StreamingWrapper, } from "./core/middleware/index.js";
|
|
42
|
+
/**
|
|
43
|
+
* Tracking functions (for advanced use cases)
|
|
44
|
+
*/
|
|
45
|
+
export { trackUsageAsync } from "./core/tracking/index.js";
|
|
46
|
+
/**
|
|
47
|
+
* Provider detection functions
|
|
48
|
+
*/
|
|
49
|
+
export { detectProvider, getProviderMetadata } from "./core/providers/index.js";
|
|
50
|
+
/**
|
|
51
|
+
* Model constants
|
|
26
52
|
*/
|
|
27
|
-
|
|
28
|
-
config();
|
|
29
|
-
// ============================================================================
|
|
30
|
-
// Configuration
|
|
31
|
-
// ============================================================================
|
|
32
|
-
export { initializeReveniumFromEnv, initializeRevenium, getReveniumConfig, isReveniumEnabled, disableRevenium, enableRevenium, } from "./core/config/revenium-config";
|
|
33
|
-
export { initializePerplexityFromEnv, initializePerplexity, getPerplexityConfig, } from "./core/config/perplexity-config";
|
|
34
|
-
// ============================================================================
|
|
35
|
-
// Core API
|
|
36
|
-
// ============================================================================
|
|
37
|
-
export { createChatCompletion, createStreamingChatCompletion, } from "./core/wrapper/perplexity-client";
|
|
38
|
-
export { generateTransactionId, buildMeteringData, sendMeteringData, } from "./core/tracking/metering";
|
|
39
|
-
export { PERPLEXITY_MODELS, DEFAULT_MODEL } from "./types";
|
|
40
|
-
// ============================================================================
|
|
41
|
-
// Utilities
|
|
42
|
-
// ============================================================================
|
|
43
|
-
export { logger } from "./utils/logger";
|
|
53
|
+
export { PERPLEXITY_MODELS } from "./constants/models.js";
|
|
44
54
|
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AA8BH;;GAEG;AACH,OAAO,EACL,UAAU,EACV,SAAS,EACT,aAAa,EACb,KAAK,EACL,SAAS,GACV,MAAM,wBAAwB,CAAC;AAEhC;;GAEG;AACH,OAAO,EACL,kBAAkB,EAClB,aAAa,EACb,oBAAoB,EACpB,gBAAgB,GACjB,MAAM,4BAA4B,CAAC;AAEpC;;GAEG;AACH,OAAO,EAAE,eAAe,EAAE,MAAM,0BAA0B,CAAC;AAE3D;;GAEG;AACH,OAAO,EAAE,cAAc,EAAE,mBAAmB,EAAE,MAAM,2BAA2B,CAAC;AAEhF;;GAEG;AACH,OAAO,EAAE,iBAAiB,EAAE,MAAM,uBAAuB,CAAC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Function Parameter Types
|
|
3
|
+
*
|
|
4
|
+
* Comprehensive type definitions for function parameters throughout the middleware.
|
|
5
|
+
* These interfaces provide type safety by replacing 'any' types with proper,
|
|
6
|
+
* well-documented interfaces that match Perplexity API structures and internal requirements.
|
|
7
|
+
*
|
|
8
|
+
* @fileoverview Type-safe function parameter definitions
|
|
9
|
+
* @author Revenium
|
|
10
|
+
* @since 2.0.0
|
|
11
|
+
*/
|
|
12
|
+
export {};
|
|
13
|
+
//# sourceMappingURL=function-parameters.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"function-parameters.js","sourceRoot":"","sources":["../../../src/types/function-parameters.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG"}
|
package/dist/esm/types/index.js
CHANGED
|
@@ -1,18 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Core Types Module
|
|
3
|
+
*
|
|
4
|
+
* Central type definitions for the Revenium Perplexity middleware.
|
|
5
|
+
* This module exports all core types used throughout the application.
|
|
3
6
|
*/
|
|
4
|
-
//
|
|
5
|
-
|
|
6
|
-
// ============================================================================
|
|
7
|
-
export const PERPLEXITY_MODELS = {
|
|
8
|
-
// Sonar Models (Online)
|
|
9
|
-
SONAR: "sonar",
|
|
10
|
-
SONAR_PRO: "sonar-pro",
|
|
11
|
-
SONAR_REASONING: "sonar-reasoning",
|
|
12
|
-
// Chat Models (Offline)
|
|
13
|
-
LLAMA_3_1_SONAR_SMALL_128K_CHAT: "llama-3.1-sonar-small-128k-chat",
|
|
14
|
-
LLAMA_3_1_SONAR_LARGE_128K_CHAT: "llama-3.1-sonar-large-128k-chat",
|
|
15
|
-
LLAMA_3_1_SONAR_HUGE_128K_CHAT: "llama-3.1-sonar-huge-128k-chat",
|
|
16
|
-
};
|
|
17
|
-
export const DEFAULT_MODEL = PERPLEXITY_MODELS.SONAR_PRO;
|
|
7
|
+
// Re-export function parameter types
|
|
8
|
+
export * from "./function-parameters.js";
|
|
18
9
|
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/types/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,qCAAqC;AACrC,cAAc,0BAA0B,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Error message patterns for network-related errors
|
|
3
|
+
*/
|
|
4
|
+
export const MESSAGE_PATTERNS_TYPE_NETWORK = [
|
|
5
|
+
"network",
|
|
6
|
+
"timeout",
|
|
7
|
+
"ECONNRESET",
|
|
8
|
+
];
|
|
9
|
+
/**
|
|
10
|
+
* Error message patterns for configuration-related errors
|
|
11
|
+
*/
|
|
12
|
+
export const ERROR_MESSAGE_PATTERNS_TYPE_CONFIG = [
|
|
13
|
+
"config",
|
|
14
|
+
"key",
|
|
15
|
+
"unauthorized",
|
|
16
|
+
];
|
|
17
|
+
//# sourceMappingURL=constants.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../../../src/utils/constants.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,CAAC,MAAM,6BAA6B,GAAG;IAC3C,SAAS;IACT,SAAS;IACT,YAAY;CACb,CAAC;AAEF;;GAEG;AACH,MAAM,CAAC,MAAM,kCAAkC,GAAG;IAChD,QAAQ;IACR,KAAK;IACL,cAAc;CACf,CAAC"}
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Error Handler Utilities
|
|
3
|
+
*
|
|
4
|
+
* Centralized error handling patterns to eliminate repetitive try/catch blocks
|
|
5
|
+
* and provide consistent error logging and recovery strategies.
|
|
6
|
+
*/
|
|
7
|
+
import { ERROR_MESSAGE_PATTERNS_TYPE_CONFIG, MESSAGE_PATTERNS_TYPE_NETWORK } from './constants.js';
|
|
8
|
+
/**
|
|
9
|
+
* Default error handling strategy
|
|
10
|
+
*/
|
|
11
|
+
const DEFAULT_STRATEGY = {
|
|
12
|
+
logError: true,
|
|
13
|
+
rethrow: true,
|
|
14
|
+
messagePrefix: '',
|
|
15
|
+
fallbackValue: undefined,
|
|
16
|
+
transformError: (error) => (error instanceof Error ? error : new Error(String(error))),
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* Safe async operation wrapper with comprehensive error handling
|
|
20
|
+
*
|
|
21
|
+
* @param operation - The async operation to execute
|
|
22
|
+
* @param context - Context information for logging
|
|
23
|
+
* @param strategy - Error handling strategy
|
|
24
|
+
* @param logger - Logger instance
|
|
25
|
+
* @returns Promise with result or fallback value
|
|
26
|
+
*/
|
|
27
|
+
export async function safeAsyncOperation(operation, context, strategy = {}, logger) {
|
|
28
|
+
const config = { ...DEFAULT_STRATEGY, ...strategy };
|
|
29
|
+
try {
|
|
30
|
+
return await operation();
|
|
31
|
+
}
|
|
32
|
+
catch (error) {
|
|
33
|
+
const transformedError = config.transformError(error);
|
|
34
|
+
if (config.logError && logger) {
|
|
35
|
+
logger.error(`${config.messagePrefix}${context}`, {
|
|
36
|
+
error: transformedError.message,
|
|
37
|
+
stack: transformedError.stack,
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
if (config.rethrow)
|
|
41
|
+
throw transformedError;
|
|
42
|
+
return config.fallbackValue;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Safe sync operation wrapper
|
|
47
|
+
*
|
|
48
|
+
* @param operation - The sync operation to execute
|
|
49
|
+
* @param context - Context information for logging
|
|
50
|
+
* @param strategy - Error handling strategy
|
|
51
|
+
* @param logger - Logger instance
|
|
52
|
+
* @returns Result or fallback value
|
|
53
|
+
*/
|
|
54
|
+
export function safeSyncOperation(operation, context, strategy = {}, logger) {
|
|
55
|
+
const config = { ...DEFAULT_STRATEGY, ...strategy };
|
|
56
|
+
try {
|
|
57
|
+
return operation();
|
|
58
|
+
}
|
|
59
|
+
catch (error) {
|
|
60
|
+
const transformedError = config.transformError(error);
|
|
61
|
+
if (config.logError && logger) {
|
|
62
|
+
logger.error(`${config.messagePrefix}${context}`, {
|
|
63
|
+
error: transformedError.message,
|
|
64
|
+
stack: transformedError.stack,
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
if (config.rethrow)
|
|
68
|
+
throw transformedError;
|
|
69
|
+
return config.fallbackValue;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Validation wrapper that provides clear error messages
|
|
74
|
+
*
|
|
75
|
+
* @param value - Value to validate
|
|
76
|
+
* @param validator - Validation function
|
|
77
|
+
* @param errorMessage - Error message if validation fails
|
|
78
|
+
* @returns Validated value
|
|
79
|
+
*/
|
|
80
|
+
export function validateOrThrow(value, validator, errorMessage) {
|
|
81
|
+
if (!validator(value))
|
|
82
|
+
throw new Error(errorMessage);
|
|
83
|
+
return value;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Validation wrapper that returns undefined on failure
|
|
87
|
+
*
|
|
88
|
+
* @param value - Value to validate
|
|
89
|
+
* @param validator - Validation function
|
|
90
|
+
* @param logger - Optional logger for warnings
|
|
91
|
+
* @param context - Context for logging
|
|
92
|
+
* @returns Validated value or undefined
|
|
93
|
+
*/
|
|
94
|
+
export function validateOrUndefined(value, validator, logger, context) {
|
|
95
|
+
if (!validator(value)) {
|
|
96
|
+
if (logger && context) {
|
|
97
|
+
logger.warn(`Validation failed: ${context}`, { value });
|
|
98
|
+
}
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
return value;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Create a retry wrapper for operations that might fail temporarily
|
|
105
|
+
*
|
|
106
|
+
* @param operation - Operation to retry
|
|
107
|
+
* @param maxRetries - Maximum number of retries
|
|
108
|
+
* @param delayMs - Delay between retries in milliseconds
|
|
109
|
+
* @param logger - Logger for retry attempts
|
|
110
|
+
* @returns Promise with operation result
|
|
111
|
+
*/
|
|
112
|
+
export async function withRetry(operation, maxRetries = 3, delayMs = 1000, logger) {
|
|
113
|
+
let lastError;
|
|
114
|
+
for (let attempt = 1; attempt <= maxRetries; attempt++) {
|
|
115
|
+
try {
|
|
116
|
+
return await operation();
|
|
117
|
+
}
|
|
118
|
+
catch (error) {
|
|
119
|
+
lastError = error instanceof Error ? error : new Error(String(error));
|
|
120
|
+
if (attempt === maxRetries) {
|
|
121
|
+
break;
|
|
122
|
+
}
|
|
123
|
+
if (logger) {
|
|
124
|
+
logger.warn(`Operation failed, retrying (${attempt}/${maxRetries})`, {
|
|
125
|
+
error: lastError.message,
|
|
126
|
+
nextRetryIn: delayMs,
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
await new Promise(resolve => setTimeout(resolve, delayMs));
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
// eslint-disable-next-line no-throw-literal
|
|
133
|
+
throw lastError;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Common error types for better error handling
|
|
137
|
+
*/
|
|
138
|
+
export class ValidationError extends Error {
|
|
139
|
+
constructor(message, context) {
|
|
140
|
+
super(message);
|
|
141
|
+
this.context = context;
|
|
142
|
+
this.name = 'ValidationError';
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
export class ConfigurationError extends Error {
|
|
146
|
+
constructor(message, context) {
|
|
147
|
+
super(message);
|
|
148
|
+
this.context = context;
|
|
149
|
+
this.name = 'ConfigurationError';
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
export class NetworkError extends Error {
|
|
153
|
+
constructor(message, context) {
|
|
154
|
+
super(message);
|
|
155
|
+
this.context = context;
|
|
156
|
+
this.name = 'NetworkError';
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Error classification utility
|
|
161
|
+
*/
|
|
162
|
+
export function classifyError(error) {
|
|
163
|
+
if (error instanceof ValidationError) {
|
|
164
|
+
return { type: 'validation', message: error.message, isRetryable: false };
|
|
165
|
+
}
|
|
166
|
+
if (error instanceof ConfigurationError) {
|
|
167
|
+
return { type: 'configuration', message: error.message, isRetryable: false };
|
|
168
|
+
}
|
|
169
|
+
if (error instanceof NetworkError) {
|
|
170
|
+
return { type: 'network', message: error.message, isRetryable: true };
|
|
171
|
+
}
|
|
172
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
173
|
+
// Classify based on message patterns
|
|
174
|
+
if (MESSAGE_PATTERNS_TYPE_NETWORK.some(pattern => message.includes(pattern))) {
|
|
175
|
+
return { type: 'network', message, isRetryable: true };
|
|
176
|
+
}
|
|
177
|
+
if (ERROR_MESSAGE_PATTERNS_TYPE_CONFIG.some(pattern => message.includes(pattern))) {
|
|
178
|
+
return { type: 'configuration', message, isRetryable: false };
|
|
179
|
+
}
|
|
180
|
+
return { type: 'unknown', message, isRetryable: false };
|
|
181
|
+
}
|
|
182
|
+
//# sourceMappingURL=error-handler.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"error-handler.js","sourceRoot":"","sources":["../../../src/utils/error-handler.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,EAAE,kCAAkC,EAAE,6BAA6B,EAAE,MAAM,gBAAgB,CAAC;AAkBnG;;GAEG;AACH,MAAM,gBAAgB,GAAoC;IACxD,QAAQ,EAAE,IAAI;IACd,OAAO,EAAE,IAAI;IACb,aAAa,EAAE,EAAE;IACjB,aAAa,EAAE,SAAS;IACxB,cAAc,EAAE,CAAC,KAAc,EAAE,EAAE,CAAC,CAAC,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,KAAK,CA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