@revenium/anthropic 1.0.9 → 1.1.1
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 +12 -0
- package/README.md +62 -10
- package/dist/cjs/config.js +39 -36
- package/dist/cjs/constants.js +6 -0
- package/dist/cjs/tracking.js +21 -3
- package/dist/cjs/utils/prompt-extraction.js +158 -0
- package/dist/cjs/utils/trace-fields.js +36 -0
- package/dist/cjs/utils/validation.js +105 -69
- package/dist/cjs/wrapper.js +79 -0
- package/dist/esm/config.js +41 -38
- package/dist/esm/constants.js +6 -0
- package/dist/esm/tracking.js +21 -3
- package/dist/esm/utils/prompt-extraction.js +154 -0
- package/dist/esm/utils/trace-fields.js +35 -0
- package/dist/esm/utils/validation.js +106 -70
- package/dist/esm/wrapper.js +79 -0
- package/dist/types/config.d.ts +1 -1
- package/dist/types/constants.d.ts +6 -0
- package/dist/types/types.d.ts +38 -8
- package/dist/types/utils/prompt-extraction.d.ts +10 -0
- package/dist/types/utils/trace-fields.d.ts +1 -0
- package/dist/types/utils/validation.d.ts +1 -1
- package/examples/advanced.ts +4 -4
- package/examples/getting_started.ts +2 -2
- package/examples/metadata.ts +3 -3
- package/examples/prompt-capture-example.ts +105 -0
- package/package.json +1 -1
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { AnthropicMessageParams, AnthropicResponse, UsageMetadata } from "../types";
|
|
2
|
+
export interface PromptData {
|
|
3
|
+
systemPrompt?: string;
|
|
4
|
+
inputMessages?: string;
|
|
5
|
+
outputResponse?: string;
|
|
6
|
+
promptsTruncated: boolean;
|
|
7
|
+
}
|
|
8
|
+
export declare function shouldCapturePrompts(metadata?: UsageMetadata): boolean;
|
|
9
|
+
export declare function extractPrompts(params: AnthropicMessageParams, response: AnthropicResponse, metadata?: UsageMetadata): PromptData | null;
|
|
10
|
+
//# sourceMappingURL=prompt-extraction.d.ts.map
|
|
@@ -4,6 +4,7 @@ export declare function getCredentialAlias(): string | null;
|
|
|
4
4
|
export declare function getTraceType(): string | null;
|
|
5
5
|
export declare function getTraceName(): string | null;
|
|
6
6
|
export declare function detectOperationSubtype(requestBody?: any): string | null;
|
|
7
|
+
export declare function detectVisionContent(params?: any): boolean;
|
|
7
8
|
export declare function getParentTransactionId(): string | null;
|
|
8
9
|
export declare function getTransactionName(): string | null;
|
|
9
10
|
export declare function getRetryNumber(): number;
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* Validation utilities for Anthropic middleware
|
|
3
3
|
* Provides type-safe validation with detailed error reporting
|
|
4
4
|
*/
|
|
5
|
-
import { ReveniumConfig, UsageMetadata } from
|
|
5
|
+
import { ReveniumConfig, UsageMetadata } from "../types";
|
|
6
6
|
/**
|
|
7
7
|
* Type guard for checking if a value is a non-null object
|
|
8
8
|
*/
|
package/examples/advanced.ts
CHANGED
|
@@ -17,8 +17,8 @@ const streamingWithMetadata = async () => {
|
|
|
17
17
|
try {
|
|
18
18
|
const metadata: UsageMetadata = {
|
|
19
19
|
subscriber: { id: "user-123", email: "user@example.com" },
|
|
20
|
-
|
|
21
|
-
|
|
20
|
+
organizationName: "my-org",
|
|
21
|
+
productName: "my-product",
|
|
22
22
|
taskType: "streaming-demo",
|
|
23
23
|
traceId: `session_${Date.now()}`,
|
|
24
24
|
};
|
|
@@ -101,7 +101,7 @@ const manualTracking = async () => {
|
|
|
101
101
|
responseTime: now,
|
|
102
102
|
metadata: {
|
|
103
103
|
subscriber: { id: "manual-user" },
|
|
104
|
-
|
|
104
|
+
organizationName: "manual-org",
|
|
105
105
|
taskType: "manual-tracking",
|
|
106
106
|
},
|
|
107
107
|
};
|
|
@@ -111,7 +111,7 @@ const manualTracking = async () => {
|
|
|
111
111
|
console.log(
|
|
112
112
|
`Tracked: ${
|
|
113
113
|
trackingData.inputTokens + trackingData.outputTokens
|
|
114
|
-
} tokens\n
|
|
114
|
+
} tokens\n`,
|
|
115
115
|
);
|
|
116
116
|
|
|
117
117
|
const status = getStatus();
|
|
@@ -27,11 +27,11 @@ async function main() {
|
|
|
27
27
|
},
|
|
28
28
|
|
|
29
29
|
// Organization & billing
|
|
30
|
-
|
|
30
|
+
organizationName: 'my-customers-name',
|
|
31
31
|
subscriptionId: 'plan-enterprise-2024',
|
|
32
32
|
|
|
33
33
|
// Product & task tracking
|
|
34
|
-
|
|
34
|
+
productName: 'my-product',
|
|
35
35
|
taskType: 'doc-summary',
|
|
36
36
|
agent: 'customer-support',
|
|
37
37
|
|
package/examples/metadata.ts
CHANGED
|
@@ -15,9 +15,9 @@ async function main() {
|
|
|
15
15
|
},
|
|
16
16
|
traceId: "trace-123",
|
|
17
17
|
taskType: "task-123",
|
|
18
|
-
|
|
18
|
+
organizationName: "AcmeCorp",
|
|
19
19
|
subscriptionId: "sub-123",
|
|
20
|
-
|
|
20
|
+
productName: "ai-assistant",
|
|
21
21
|
agent: "agent-123",
|
|
22
22
|
responseQualityScore: 0.95,
|
|
23
23
|
};
|
|
@@ -48,7 +48,7 @@ async function main() {
|
|
|
48
48
|
: "Non-text response";
|
|
49
49
|
console.log("RESPONSE: \n", textResponse);
|
|
50
50
|
console.log(
|
|
51
|
-
`Tokens: ${response.usage?.input_tokens} input + ${response.usage?.output_tokens} output\n
|
|
51
|
+
`Tokens: ${response.usage?.input_tokens} input + ${response.usage?.output_tokens} output\n`,
|
|
52
52
|
);
|
|
53
53
|
} catch (error) {
|
|
54
54
|
console.error("Error: ", error);
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import Anthropic from "@anthropic-ai/sdk";
|
|
2
|
+
import { configure } from "../src/index";
|
|
3
|
+
|
|
4
|
+
async function main() {
|
|
5
|
+
console.log("=== Anthropic Prompt Capture Example ===\n");
|
|
6
|
+
|
|
7
|
+
if (!process.env.ANTHROPIC_API_KEY) {
|
|
8
|
+
console.error("Error: ANTHROPIC_API_KEY environment variable is required");
|
|
9
|
+
console.error("Please set it before running this example:");
|
|
10
|
+
console.error(" export ANTHROPIC_API_KEY=your_api_key_here");
|
|
11
|
+
process.exit(1);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const config = {
|
|
15
|
+
reveniumApiKey: process.env.REVENIUM_METERING_API_KEY || "hak_test_key",
|
|
16
|
+
reveniumBaseUrl:
|
|
17
|
+
process.env.REVENIUM_METERING_BASE_URL || "https://api.revenium.ai",
|
|
18
|
+
anthropicApiKey: process.env.ANTHROPIC_API_KEY,
|
|
19
|
+
capturePrompts: true,
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
configure(config);
|
|
23
|
+
|
|
24
|
+
const anthropic = new Anthropic({
|
|
25
|
+
apiKey: config.anthropicApiKey,
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
console.log("Example 1: Prompt capture enabled via config");
|
|
29
|
+
console.log("Making request with prompt capture enabled...\n");
|
|
30
|
+
|
|
31
|
+
try {
|
|
32
|
+
const response = await anthropic.messages.create({
|
|
33
|
+
model: "claude-3-5-sonnet-20241022",
|
|
34
|
+
max_tokens: 100,
|
|
35
|
+
system: "You are a helpful assistant that provides concise answers.",
|
|
36
|
+
messages: [
|
|
37
|
+
{
|
|
38
|
+
role: "user",
|
|
39
|
+
content: "What is the capital of France?",
|
|
40
|
+
},
|
|
41
|
+
],
|
|
42
|
+
usageMetadata: {
|
|
43
|
+
organizationName: "org-prompt-capture-demo",
|
|
44
|
+
productName: "prod-anthropic-prompt-capture",
|
|
45
|
+
},
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
console.log(
|
|
49
|
+
"Response:",
|
|
50
|
+
response.content[0].type === "text" ? response.content[0].text : "",
|
|
51
|
+
);
|
|
52
|
+
console.log("\nPrompts captured and sent to Revenium API!");
|
|
53
|
+
} catch (error) {
|
|
54
|
+
console.error(
|
|
55
|
+
"Error:",
|
|
56
|
+
error instanceof Error ? error.message : String(error),
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
console.log("\n" + "=".repeat(50) + "\n");
|
|
61
|
+
|
|
62
|
+
console.log("Example 2: Per-call override to disable prompt capture");
|
|
63
|
+
console.log("Making request with prompt capture disabled via metadata...\n");
|
|
64
|
+
|
|
65
|
+
try {
|
|
66
|
+
const response2 = await anthropic.messages.create({
|
|
67
|
+
model: "claude-3-5-sonnet-20241022",
|
|
68
|
+
max_tokens: 100,
|
|
69
|
+
system: "You are a helpful assistant.",
|
|
70
|
+
messages: [
|
|
71
|
+
{
|
|
72
|
+
role: "user",
|
|
73
|
+
content: "What is 2+2?",
|
|
74
|
+
},
|
|
75
|
+
],
|
|
76
|
+
usageMetadata: {
|
|
77
|
+
organizationName: "org-prompt-capture-demo",
|
|
78
|
+
productName: "prod-anthropic-prompt-capture",
|
|
79
|
+
capturePrompts: false,
|
|
80
|
+
},
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
console.log(
|
|
84
|
+
"Response:",
|
|
85
|
+
response2.content[0].type === "text" ? response2.content[0].text : "",
|
|
86
|
+
);
|
|
87
|
+
console.log("\nPrompts NOT captured (overridden via metadata)!");
|
|
88
|
+
} catch (error) {
|
|
89
|
+
console.error(
|
|
90
|
+
"Error:",
|
|
91
|
+
error instanceof Error ? error.message : String(error),
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
console.log("\n" + "=".repeat(50) + "\n");
|
|
96
|
+
console.log("Examples completed!");
|
|
97
|
+
console.log(
|
|
98
|
+
"\nNote: Set REVENIUM_CAPTURE_PROMPTS=true in .env to enable globally",
|
|
99
|
+
);
|
|
100
|
+
console.log(
|
|
101
|
+
"Or use capturePrompts: true in config or metadata for per-call control",
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
main().catch(console.error);
|
package/package.json
CHANGED