metergraph 0.4.0 → 0.6.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/README.md +83 -16
- package/dist/batch-first.d.ts +112 -0
- package/dist/batch-first.js +213 -0
- package/dist/capture.js +29 -27
- package/dist/config.js +9 -4
- package/dist/context.js +18 -10
- package/dist/failure-log.js +5 -1
- package/dist/index.d.ts +18 -5
- package/dist/index.js +86 -38
- package/dist/index.mjs +43 -0
- package/dist/package.json +1 -0
- package/dist/provider-batch.d.ts +160 -0
- package/dist/provider-batch.js +247 -0
- package/dist/repo-config.d.ts +1 -1
- package/dist/repo-config.js +43 -36
- package/dist/session.js +16 -12
- package/dist/template.js +6 -2
- package/dist/track.js +6 -3
- package/dist/transport.js +9 -5
- package/dist/vercel-ai.js +9 -6
- package/dist/version.d.ts +1 -1
- package/dist/version.js +4 -1
- package/dist/wrap.js +24 -18
- package/package.json +17 -8
package/dist/wrap.js
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.wrapClient = exports.SEAM_TABLES = exports.GOOGLE_SEAMS = exports.ANTHROPIC_SEAMS = exports.OPENAI_SEAMS = void 0;
|
|
4
|
+
exports.setCaptureRuntime = setCaptureRuntime;
|
|
5
|
+
exports.getCaptureRuntime = getCaptureRuntime;
|
|
6
|
+
exports.wrap = wrap;
|
|
7
|
+
const capture_js_1 = require("./capture.js");
|
|
8
|
+
const context_js_1 = require("./context.js");
|
|
3
9
|
let runtime;
|
|
4
10
|
const seenBatchItems = new Set();
|
|
5
11
|
// Tracks which resource *owners* (e.g. a specific chat.completions instance)
|
|
@@ -11,11 +17,11 @@ const seenBatchItems = new Set();
|
|
|
11
17
|
// same synchronous window are never mistaken for one delegating into the
|
|
12
18
|
// other.
|
|
13
19
|
const reentrantOwners = new WeakSet();
|
|
14
|
-
|
|
20
|
+
function setCaptureRuntime(value) {
|
|
15
21
|
runtime = value;
|
|
16
22
|
}
|
|
17
23
|
/** @internal Used by optional framework adapters without adding runtime dependencies. */
|
|
18
|
-
|
|
24
|
+
function getCaptureRuntime() {
|
|
19
25
|
return runtime;
|
|
20
26
|
}
|
|
21
27
|
function requestFrom(args) {
|
|
@@ -163,7 +169,7 @@ function patchOpenAIBatchContent(owner, method) {
|
|
|
163
169
|
if (!capture)
|
|
164
170
|
return original.apply(owner, args);
|
|
165
171
|
const sourceId = String(args[0] ?? "unknown");
|
|
166
|
-
const context = contextSnapshot();
|
|
172
|
+
const context = (0, context_js_1.contextSnapshot)();
|
|
167
173
|
const stack = new Error().stack;
|
|
168
174
|
const result = original.apply(owner, args);
|
|
169
175
|
if (result && typeof result.then === "function") {
|
|
@@ -186,7 +192,7 @@ function patchAnthropicBatchResults(owner) {
|
|
|
186
192
|
if (!capture)
|
|
187
193
|
return original.apply(owner, args);
|
|
188
194
|
const batchId = String(args[0] ?? "unknown");
|
|
189
|
-
const context = contextSnapshot();
|
|
195
|
+
const context = (0, context_js_1.contextSnapshot)();
|
|
190
196
|
const stack = new Error().stack;
|
|
191
197
|
const result = original.apply(owner, args);
|
|
192
198
|
if (result && typeof result.then === "function") {
|
|
@@ -204,7 +210,7 @@ function streamProxy(stream, state, capture) {
|
|
|
204
210
|
const parts = [];
|
|
205
211
|
const chunks = [];
|
|
206
212
|
const hasOutput = (chunk) => {
|
|
207
|
-
if (chunkText(chunk))
|
|
213
|
+
if ((0, capture_js_1.chunkText)(chunk))
|
|
208
214
|
return true;
|
|
209
215
|
const value = chunk;
|
|
210
216
|
if (Array.isArray(value?.choices)
|
|
@@ -228,7 +234,7 @@ function streamProxy(stream, state, capture) {
|
|
|
228
234
|
const observe = (chunk) => {
|
|
229
235
|
last = chunk;
|
|
230
236
|
chunks.push(chunk);
|
|
231
|
-
const text = chunkText(chunk);
|
|
237
|
+
const text = (0, capture_js_1.chunkText)(chunk);
|
|
232
238
|
if (ttftMs === undefined && hasOutput(chunk)) {
|
|
233
239
|
ttftMs = Math.round(performance.now() - state.started);
|
|
234
240
|
}
|
|
@@ -382,7 +388,7 @@ function patch(owner, method, provider, endpoint) {
|
|
|
382
388
|
owner[method] = wrapped;
|
|
383
389
|
return true;
|
|
384
390
|
}
|
|
385
|
-
|
|
391
|
+
exports.OPENAI_SEAMS = [
|
|
386
392
|
{ path: "chat.completions", method: "create", endpoint: "chat.completions" },
|
|
387
393
|
{ path: "chat.completions", method: "parse", endpoint: "chat.completions.parse" },
|
|
388
394
|
// beta.chat.completions.parse is v4.x-only — removed at the v4->v5
|
|
@@ -402,11 +408,11 @@ export const OPENAI_SEAMS = [
|
|
|
402
408
|
// beta.chat.completions.parse above is exempted from that check since it
|
|
403
409
|
// will correctly show as absent there — see the test for details.
|
|
404
410
|
];
|
|
405
|
-
|
|
411
|
+
exports.ANTHROPIC_SEAMS = [
|
|
406
412
|
{ path: "messages", method: "create", endpoint: "messages" },
|
|
407
413
|
{ path: "messages", method: "stream", endpoint: "messages.stream" },
|
|
408
414
|
];
|
|
409
|
-
|
|
415
|
+
exports.GOOGLE_SEAMS = [
|
|
410
416
|
{ path: "models", method: "generateContent", endpoint: "models.generate_content" },
|
|
411
417
|
{ path: "models", method: "generateContentStream", endpoint: "models.generate_content.stream" },
|
|
412
418
|
// Note: unlike the Python google-genai client, the JS/TS @google/genai
|
|
@@ -414,10 +420,10 @@ export const GOOGLE_SEAMS = [
|
|
|
414
420
|
// installed package — client.aio is undefined; JS methods are already
|
|
415
421
|
// Promise-based) — do not add aio.models entries here.
|
|
416
422
|
];
|
|
417
|
-
|
|
418
|
-
openai: OPENAI_SEAMS,
|
|
419
|
-
anthropic: ANTHROPIC_SEAMS,
|
|
420
|
-
google: GOOGLE_SEAMS,
|
|
423
|
+
exports.SEAM_TABLES = {
|
|
424
|
+
openai: exports.OPENAI_SEAMS,
|
|
425
|
+
anthropic: exports.ANTHROPIC_SEAMS,
|
|
426
|
+
google: exports.GOOGLE_SEAMS,
|
|
421
427
|
};
|
|
422
428
|
function detectProvider(client) {
|
|
423
429
|
if (client.models?.generateContent)
|
|
@@ -437,7 +443,7 @@ function resolveSeam(client, path) {
|
|
|
437
443
|
}
|
|
438
444
|
function applySeams(client, provider) {
|
|
439
445
|
const patched = [];
|
|
440
|
-
for (const seam of SEAM_TABLES[provider] ?? []) {
|
|
446
|
+
for (const seam of exports.SEAM_TABLES[provider] ?? []) {
|
|
441
447
|
let owner;
|
|
442
448
|
try {
|
|
443
449
|
owner = resolveSeam(client, seam.path);
|
|
@@ -463,7 +469,7 @@ function applyBatchExtras(client, provider) {
|
|
|
463
469
|
}
|
|
464
470
|
return patched;
|
|
465
471
|
}
|
|
466
|
-
|
|
472
|
+
function wrap(client, provider) {
|
|
467
473
|
try {
|
|
468
474
|
const name = provider ?? detectProvider(client);
|
|
469
475
|
const patched = applySeams(client, name);
|
|
@@ -480,4 +486,4 @@ export function wrap(client, provider) {
|
|
|
480
486
|
}
|
|
481
487
|
return client;
|
|
482
488
|
}
|
|
483
|
-
|
|
489
|
+
exports.wrapClient = wrap;
|
package/package.json
CHANGED
|
@@ -1,21 +1,27 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "metergraph",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "Fire-and-forget LLM spend capture for Metergraph",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
7
|
+
"module": "./dist/index.mjs",
|
|
7
8
|
"types": "./dist/index.d.ts",
|
|
8
9
|
"exports": {
|
|
9
10
|
".": {
|
|
10
11
|
"types": "./dist/index.d.ts",
|
|
11
|
-
"import": "./dist/index.
|
|
12
|
+
"import": "./dist/index.mjs",
|
|
13
|
+
"require": "./dist/index.js",
|
|
14
|
+
"default": "./dist/index.mjs"
|
|
12
15
|
}
|
|
13
16
|
},
|
|
14
17
|
"files": ["dist", "README.md"],
|
|
15
18
|
"scripts": {
|
|
16
|
-
"build": "tsc -p tsconfig.json",
|
|
19
|
+
"build": "node scripts/prepare-build.mjs && tsc -p tsconfig.json && node scripts/finish-build.mjs",
|
|
17
20
|
"prepack": "npm run build",
|
|
18
|
-
"
|
|
21
|
+
"prepublishOnly": "npm test && npm run test:package",
|
|
22
|
+
"test": "npm run build && npm run test:types && npm run test:examples && node --test test/*.test.mjs",
|
|
23
|
+
"test:examples": "tsc --noEmit --strict --skipLibCheck --target ES2022 --module NodeNext --moduleResolution NodeNext test/ai-sdk-factories.types.ts",
|
|
24
|
+
"test:package": "npm run build && node --test test/package/packed-package.test.mjs",
|
|
19
25
|
"test:types": "tsc --noEmit --strict --skipLibCheck --target ES2022 --module NodeNext --moduleResolution NodeNext test/ai-sdk-current.types.ts"
|
|
20
26
|
},
|
|
21
27
|
"engines": {
|
|
@@ -35,12 +41,15 @@
|
|
|
35
41
|
"keywords": ["llm", "observability", "openai", "anthropic", "gemini", "vercel-ai-sdk", "cost-tracking"],
|
|
36
42
|
"dependencies": {},
|
|
37
43
|
"devDependencies": {
|
|
38
|
-
"ai": "^
|
|
44
|
+
"@ai-sdk/anthropic": "^4.0.39",
|
|
45
|
+
"@ai-sdk/openai": "^4.0.43",
|
|
46
|
+
"@ai-sdk/openai-compatible": "^3.0.31",
|
|
47
|
+
"@anthropic-ai/sdk": "^0.30",
|
|
48
|
+
"@google/genai": "^1",
|
|
39
49
|
"@types/node": "^24.0.0",
|
|
40
|
-
"
|
|
50
|
+
"ai": "^7.0.55",
|
|
41
51
|
"openai": "^7",
|
|
42
|
-
"
|
|
43
|
-
"@google/genai": "^1"
|
|
52
|
+
"typescript": "^5.8.0"
|
|
44
53
|
},
|
|
45
54
|
"peerDependencies": {
|
|
46
55
|
"@anthropic-ai/sdk": ">=0.30",
|