@revenium/anthropic 1.0.8 → 1.1.0
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 +46 -1
- package/README.md +208 -49
- package/dist/cjs/config.js +80 -29
- package/dist/cjs/constants.js +45 -24
- package/dist/cjs/tracking.js +82 -20
- package/dist/cjs/types/anthropic-augmentation.js +0 -62
- package/dist/cjs/utils/prompt-extraction.js +158 -0
- package/dist/cjs/utils/summary-printer.js +189 -0
- package/dist/cjs/utils/trace-fields.js +117 -0
- package/dist/cjs/utils/validation.js +55 -23
- package/dist/cjs/wrapper.js +49 -41
- package/dist/esm/config.js +82 -31
- package/dist/esm/constants.js +44 -23
- package/dist/esm/tracking.js +82 -20
- package/dist/esm/types/anthropic-augmentation.js +0 -62
- package/dist/esm/utils/prompt-extraction.js +154 -0
- package/dist/esm/utils/summary-printer.js +186 -0
- package/dist/esm/utils/trace-fields.js +106 -0
- package/dist/esm/utils/validation.js +56 -24
- package/dist/esm/wrapper.js +55 -47
- package/dist/types/config.d.ts +2 -1
- package/dist/types/constants.d.ts +21 -0
- package/dist/types/types/anthropic-augmentation.d.ts +0 -92
- package/dist/types/types.d.ts +41 -198
- package/dist/types/utils/prompt-extraction.d.ts +10 -0
- package/dist/types/utils/summary-printer.d.ts +3 -0
- package/dist/types/utils/trace-fields.d.ts +10 -0
- package/examples/advanced.ts +128 -0
- package/examples/basic.ts +132 -0
- package/examples/getting_started.ts +6 -6
- package/examples/metadata.ts +58 -0
- package/package.json +4 -6
- package/examples/advanced-features.ts +0 -469
- package/examples/basic-usage.ts +0 -314
package/examples/basic-usage.ts
DELETED
|
@@ -1,314 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Revenium Anthropic Middleware - TypeScript-First Basic Usage Example
|
|
3
|
-
*
|
|
4
|
-
* This example demonstrates TypeScript-first patterns with all initialization methods:
|
|
5
|
-
* • Auto-initialization with module augmentation (TypeScript-native usageMetadata)
|
|
6
|
-
* • Explicit initialization with type-safe error handling
|
|
7
|
-
* • Manual configuration with full type control and validation
|
|
8
|
-
* • Interface validation using the `satisfies` operator
|
|
9
|
-
* • Comprehensive type safety throughout
|
|
10
|
-
*
|
|
11
|
-
* Features TypeScript patterns like:
|
|
12
|
-
* - Module augmentation for native usageMetadata support
|
|
13
|
-
* - Type-safe configuration with validation
|
|
14
|
-
* - Strongly typed metadata with satisfies operator
|
|
15
|
-
* - Generic functions with type constraints
|
|
16
|
-
* - Comprehensive error handling with typed responses
|
|
17
|
-
*/
|
|
18
|
-
|
|
19
|
-
// Load environment variables from .env file
|
|
20
|
-
import "dotenv/config";
|
|
21
|
-
|
|
22
|
-
// Import for module augmentation - enables native usageMetadata support
|
|
23
|
-
import "@revenium/anthropic";
|
|
24
|
-
import Anthropic from "@anthropic-ai/sdk";
|
|
25
|
-
|
|
26
|
-
// Import types and functions for full TypeScript support
|
|
27
|
-
import type {
|
|
28
|
-
UsageMetadata,
|
|
29
|
-
ReveniumConfig,
|
|
30
|
-
ConfigValidationResult,
|
|
31
|
-
MiddlewareStatus,
|
|
32
|
-
} from "@revenium/anthropic";
|
|
33
|
-
|
|
34
|
-
// Import initialization and configuration functions
|
|
35
|
-
import { initialize, getStatus, configure, getConfig } from "@revenium/anthropic";
|
|
36
|
-
|
|
37
|
-
async function demonstrateBasicUsage() {
|
|
38
|
-
console.log("Revenium Anthropic Middleware - Basic Usage Examples\n");
|
|
39
|
-
|
|
40
|
-
// =============================================================================
|
|
41
|
-
// OPTION A: Auto-Initialization (Simplest - Recommended for Most Projects)
|
|
42
|
-
// =============================================================================
|
|
43
|
-
console.log("Option A: Auto-Initialization (Simplest)");
|
|
44
|
-
console.log(
|
|
45
|
-
"Just import the middleware - it auto-configures from environment variables\n"
|
|
46
|
-
);
|
|
47
|
-
|
|
48
|
-
// The middleware was already imported at the top of the file - auto-initialization happens automatically
|
|
49
|
-
const anthropic = new Anthropic();
|
|
50
|
-
|
|
51
|
-
try {
|
|
52
|
-
// Example 1: Basic request without metadata (still tracked automatically)
|
|
53
|
-
console.log("Example 1: Basic request without metadata...");
|
|
54
|
-
const basicResponse = await anthropic.messages.create({
|
|
55
|
-
model: "claude-haiku-4-5",
|
|
56
|
-
max_tokens: 50,
|
|
57
|
-
messages: [
|
|
58
|
-
{ role: "user", content: "What is the capital of France? Be concise." },
|
|
59
|
-
],
|
|
60
|
-
// No metadata - still tracked automatically
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
console.log(
|
|
64
|
-
"Response:",
|
|
65
|
-
basicResponse.content[0].type === "text"
|
|
66
|
-
? basicResponse.content[0].text
|
|
67
|
-
: "Non-text response"
|
|
68
|
-
);
|
|
69
|
-
console.log(
|
|
70
|
-
` Tokens: ${basicResponse.usage?.input_tokens} input + ${basicResponse.usage?.output_tokens} output\n`
|
|
71
|
-
);
|
|
72
|
-
|
|
73
|
-
// Example 2: Request with rich metadata for enhanced tracking
|
|
74
|
-
console.log("Example 2: Request with rich metadata...");
|
|
75
|
-
const metadataResponse = await anthropic.messages.create({
|
|
76
|
-
model: "claude-haiku-4-5",
|
|
77
|
-
max_tokens: 80,
|
|
78
|
-
messages: [
|
|
79
|
-
{ role: "user", content: "Explain quantum computing in one sentence." },
|
|
80
|
-
],
|
|
81
|
-
// Add rich metadata for enhanced analytics and billing
|
|
82
|
-
usageMetadata: {
|
|
83
|
-
subscriber: {
|
|
84
|
-
id: "user-456",
|
|
85
|
-
email: "user@demo-org.com",
|
|
86
|
-
credential: {
|
|
87
|
-
name: "demo-api-key",
|
|
88
|
-
value: "demo-key-123",
|
|
89
|
-
},
|
|
90
|
-
},
|
|
91
|
-
organizationId: "demo-org-123",
|
|
92
|
-
productId: "ai-assistant-v2",
|
|
93
|
-
taskType: "educational-query",
|
|
94
|
-
agent: "anthropic-basic-demo",
|
|
95
|
-
traceId: "session-" + Date.now(),
|
|
96
|
-
},
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
console.log(
|
|
100
|
-
"Response:",
|
|
101
|
-
metadataResponse.content[0].type === "text"
|
|
102
|
-
? metadataResponse.content[0].text
|
|
103
|
-
: "Non-text response"
|
|
104
|
-
);
|
|
105
|
-
console.log(
|
|
106
|
-
` Tokens: ${metadataResponse.usage?.input_tokens} input + ${metadataResponse.usage?.output_tokens} output\n`
|
|
107
|
-
);
|
|
108
|
-
|
|
109
|
-
// Wait a moment for async tracking to complete
|
|
110
|
-
console.log("Waiting for tracking to complete...");
|
|
111
|
-
await new Promise((resolve) => setTimeout(resolve, 2000));
|
|
112
|
-
|
|
113
|
-
console.log(
|
|
114
|
-
"✓ Auto-initialization: Both requests completed and tracking attempted\n"
|
|
115
|
-
);
|
|
116
|
-
} catch (error) {
|
|
117
|
-
console.error("Error: Auto-initialization failed:", error);
|
|
118
|
-
console.log("Falling back to explicit initialization...\n");
|
|
119
|
-
|
|
120
|
-
// =============================================================================
|
|
121
|
-
// OPTION B: Explicit Initialization (Better Error Control)
|
|
122
|
-
// =============================================================================
|
|
123
|
-
await demonstrateExplicitInitialization();
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
async function demonstrateExplicitInitialization() {
|
|
128
|
-
console.log("Option B: Explicit Initialization (Better Error Control)");
|
|
129
|
-
console.log(
|
|
130
|
-
"Call initialize() explicitly for clear error feedback and better control\n"
|
|
131
|
-
);
|
|
132
|
-
|
|
133
|
-
try {
|
|
134
|
-
// Explicitly initialize with clear error feedback
|
|
135
|
-
initialize();
|
|
136
|
-
console.log("✓ Explicit initialization successful!");
|
|
137
|
-
|
|
138
|
-
// Check status after initialization
|
|
139
|
-
const status = getStatus();
|
|
140
|
-
console.log("Middleware status:", {
|
|
141
|
-
initialized: status.initialized,
|
|
142
|
-
patched: status.patched,
|
|
143
|
-
hasConfig: status.hasConfig,
|
|
144
|
-
});
|
|
145
|
-
|
|
146
|
-
// Now use Anthropic with guaranteed tracking
|
|
147
|
-
const anthropic = new Anthropic();
|
|
148
|
-
const response = await anthropic.messages.create({
|
|
149
|
-
model: "claude-haiku-4-5",
|
|
150
|
-
max_tokens: 60,
|
|
151
|
-
messages: [
|
|
152
|
-
{
|
|
153
|
-
role: "user",
|
|
154
|
-
content: "What are the benefits of explicit initialization?",
|
|
155
|
-
},
|
|
156
|
-
],
|
|
157
|
-
usageMetadata: {
|
|
158
|
-
subscriber: {
|
|
159
|
-
id: "explicit-user-789",
|
|
160
|
-
// email and credential are optional
|
|
161
|
-
},
|
|
162
|
-
organizationId: "explicit-demo",
|
|
163
|
-
taskType: "explicit-initialization-example",
|
|
164
|
-
},
|
|
165
|
-
});
|
|
166
|
-
|
|
167
|
-
console.log(
|
|
168
|
-
"Response:",
|
|
169
|
-
response.content[0].type === "text"
|
|
170
|
-
? response.content[0].text
|
|
171
|
-
: "Non-text response"
|
|
172
|
-
);
|
|
173
|
-
console.log(
|
|
174
|
-
"✓ Explicit initialization: Request tracked with guaranteed configuration!\n"
|
|
175
|
-
);
|
|
176
|
-
} catch (error) {
|
|
177
|
-
console.error("Explicit initialization failed:", (error as Error).message);
|
|
178
|
-
console.log("Falling back to manual configuration...\n");
|
|
179
|
-
|
|
180
|
-
// =============================================================================
|
|
181
|
-
// OPTION C: Manual Configuration (Full Control)
|
|
182
|
-
// =============================================================================
|
|
183
|
-
await demonstrateManualConfiguration();
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
async function demonstrateManualConfiguration() {
|
|
188
|
-
console.log("Option C: Manual Configuration (Full Control)");
|
|
189
|
-
|
|
190
|
-
try {
|
|
191
|
-
// Manual configuration with all options
|
|
192
|
-
configure({
|
|
193
|
-
// Required: Revenium API configuration
|
|
194
|
-
reveniumApiKey:
|
|
195
|
-
process.env.REVENIUM_METERING_API_KEY || "hak_your_api_key_here",
|
|
196
|
-
reveniumBaseUrl:
|
|
197
|
-
process.env.REVENIUM_METERING_BASE_URL || "https://api.revenium.ai",
|
|
198
|
-
|
|
199
|
-
// Optional: Anthropic API key (can also be set in Anthropic client)
|
|
200
|
-
anthropicApiKey: process.env.ANTHROPIC_API_KEY,
|
|
201
|
-
|
|
202
|
-
// Optional: Performance and reliability settings
|
|
203
|
-
apiTimeout: 8000, // 8 second timeout (default: 5000)
|
|
204
|
-
maxRetries: 5, // 5 retry attempts (default: 3)
|
|
205
|
-
failSilent: false, // Throw errors instead of silent failure (default: true)
|
|
206
|
-
});
|
|
207
|
-
|
|
208
|
-
console.log("Success: Manual configuration completed!");
|
|
209
|
-
|
|
210
|
-
// Verify configuration
|
|
211
|
-
const config = getConfig();
|
|
212
|
-
console.log("Configuration:", {
|
|
213
|
-
hasReveniumKey: !!config?.reveniumApiKey,
|
|
214
|
-
baseUrl: config?.reveniumBaseUrl,
|
|
215
|
-
timeout: config?.apiTimeout,
|
|
216
|
-
maxRetries: config?.maxRetries,
|
|
217
|
-
});
|
|
218
|
-
|
|
219
|
-
// Use Anthropic with custom configuration
|
|
220
|
-
const anthropic = new Anthropic({
|
|
221
|
-
apiKey: process.env.ANTHROPIC_API_KEY || "sk-ant-your_anthropic_key_here",
|
|
222
|
-
});
|
|
223
|
-
|
|
224
|
-
const response = await anthropic.messages.create({
|
|
225
|
-
model: "claude-haiku-4-5",
|
|
226
|
-
max_tokens: 80,
|
|
227
|
-
messages: [
|
|
228
|
-
{
|
|
229
|
-
role: "user",
|
|
230
|
-
content:
|
|
231
|
-
"What are the advantages of manual configuration in enterprise software?",
|
|
232
|
-
},
|
|
233
|
-
],
|
|
234
|
-
// Enterprise-grade metadata
|
|
235
|
-
usageMetadata: {
|
|
236
|
-
subscriber: {
|
|
237
|
-
id: "manual-config-user",
|
|
238
|
-
email: "enterprise@corp.com",
|
|
239
|
-
credential: {
|
|
240
|
-
name: "enterprise-api-key",
|
|
241
|
-
value: "ent-key-456",
|
|
242
|
-
},
|
|
243
|
-
},
|
|
244
|
-
organizationId: "enterprise-corp",
|
|
245
|
-
productId: "enterprise-ai-platform",
|
|
246
|
-
taskType: "manual-configuration-demo",
|
|
247
|
-
agent: "anthropic-enterprise-v1",
|
|
248
|
-
responseQualityScore: 0.98,
|
|
249
|
-
},
|
|
250
|
-
});
|
|
251
|
-
|
|
252
|
-
console.log(
|
|
253
|
-
"Response:",
|
|
254
|
-
response.content[0].type === "text"
|
|
255
|
-
? response.content[0].text
|
|
256
|
-
: "Non-text response"
|
|
257
|
-
);
|
|
258
|
-
console.log(
|
|
259
|
-
"Success: Manual configuration - Request tracked with custom enterprise settings!\n"
|
|
260
|
-
);
|
|
261
|
-
} catch (error) {
|
|
262
|
-
console.error(
|
|
263
|
-
"Error: Manual configuration failed:",
|
|
264
|
-
(error as Error).message
|
|
265
|
-
);
|
|
266
|
-
throw error;
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
// Environment validation
|
|
271
|
-
function checkEnvironment() {
|
|
272
|
-
const required = ["REVENIUM_METERING_API_KEY", "ANTHROPIC_API_KEY"];
|
|
273
|
-
const missing = required.filter((key) => !process.env[key]);
|
|
274
|
-
|
|
275
|
-
if (missing.length > 0) {
|
|
276
|
-
console.error("Error: Missing required environment variables:");
|
|
277
|
-
missing.forEach((key) => console.error(` ${key}`));
|
|
278
|
-
console.error("\nSet them in a .env file in the project root:");
|
|
279
|
-
console.error(" REVENIUM_METERING_API_KEY=hak_your_api_key");
|
|
280
|
-
console.error(" ANTHROPIC_API_KEY=sk-ant-your_anthropic_key");
|
|
281
|
-
console.error("\nOptional (uses defaults if not set):");
|
|
282
|
-
console.error(" REVENIUM_METERING_BASE_URL=https://api.revenium.ai");
|
|
283
|
-
console.error(" REVENIUM_DEBUG=true # For detailed logging");
|
|
284
|
-
process.exit(1);
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
// Run the example
|
|
289
|
-
if (require.main === module) {
|
|
290
|
-
checkEnvironment();
|
|
291
|
-
|
|
292
|
-
demonstrateBasicUsage()
|
|
293
|
-
.then(() => {
|
|
294
|
-
console.log("✓ Basic usage examples completed successfully!");
|
|
295
|
-
console.log("\nSummary of initialization methods:");
|
|
296
|
-
console.log(" • Auto-initialization: Simplest - just import and go");
|
|
297
|
-
console.log(
|
|
298
|
-
" • Explicit initialization: Better error control and debugging"
|
|
299
|
-
);
|
|
300
|
-
console.log(
|
|
301
|
-
" • Manual configuration: Full control for enterprise applications"
|
|
302
|
-
);
|
|
303
|
-
console.log(
|
|
304
|
-
"\n✓ All requests automatically tracked to Revenium with rich metadata support"
|
|
305
|
-
);
|
|
306
|
-
console.log(
|
|
307
|
-
"\nTo see detailed tracking logs, set REVENIUM_DEBUG=true in .env file"
|
|
308
|
-
);
|
|
309
|
-
})
|
|
310
|
-
.catch((error) => {
|
|
311
|
-
console.error("\nError: Basic usage examples failed:", error);
|
|
312
|
-
process.exit(1);
|
|
313
|
-
});
|
|
314
|
-
}
|