metergraph 0.6.3 → 0.6.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/README.md +11 -0
- package/dist/capture.d.ts +1 -0
- package/dist/capture.js +2 -0
- package/dist/gateway.d.ts +11 -0
- package/dist/gateway.js +97 -0
- package/dist/index.d.ts +5 -1
- package/dist/index.js +2 -8
- package/dist/vercel-ai.js +41 -20
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/dist/wrap.d.ts +10 -1
- package/dist/wrap.js +178 -37
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -46,6 +46,17 @@ Node's asynchronous execution and isolate concurrent jobs. Legacy
|
|
|
46
46
|
scope, but warn and do nothing when called outside one. Use `setDefaultTags()`
|
|
47
47
|
only for deliberate process-wide tags; sessions are always execution-scoped.
|
|
48
48
|
|
|
49
|
+
## OpenRouter
|
|
50
|
+
|
|
51
|
+
Point an OpenAI client at `https://openrouter.ai/api/v1` and `wrap()` auto-detects
|
|
52
|
+
the host, adding `served_model` and — when OpenRouter supplies a valid
|
|
53
|
+
`usage.cost` — `reported_cost_usd` to Chat Completions rows without changing the
|
|
54
|
+
requested model or the capture provider. A trusted custom domain uses
|
|
55
|
+
`wrap(client, { gateway: "openrouter" })`;
|
|
56
|
+
the existing `wrap(client, "openai")` form is unchanged. The runnable
|
|
57
|
+
[Node OpenRouter example](../examples/node-openrouter/) covers requested-vs-served
|
|
58
|
+
model, reported-vs-catalog cost, streaming usage, and the BYOK limitation.
|
|
59
|
+
|
|
49
60
|
## Vercel AI SDK
|
|
50
61
|
|
|
51
62
|
Use Metergraph as language-model middleware. It records each actual provider
|
package/dist/capture.d.ts
CHANGED
package/dist/capture.js
CHANGED
|
@@ -4,6 +4,7 @@ exports.CaptureRuntime = exports.DEFAULT_TEXT_MAX_BYTES = void 0;
|
|
|
4
4
|
exports.chunkText = chunkText;
|
|
5
5
|
const node_crypto_1 = require("node:crypto");
|
|
6
6
|
const context_js_1 = require("./context.js");
|
|
7
|
+
const gateway_js_1 = require("./gateway.js");
|
|
7
8
|
const template_js_1 = require("./template.js");
|
|
8
9
|
const version_js_1 = require("./version.js");
|
|
9
10
|
exports.DEFAULT_TEXT_MAX_BYTES = 1024 * 1024;
|
|
@@ -538,6 +539,7 @@ class CaptureRuntime {
|
|
|
538
539
|
provider: state.provider,
|
|
539
540
|
model: state.request.model,
|
|
540
541
|
...usage(response),
|
|
542
|
+
...(0, gateway_js_1.gatewayEvidence)(state.gateway, state.endpoint, response),
|
|
541
543
|
latency_ms: Math.round(performance.now() - state.started),
|
|
542
544
|
status,
|
|
543
545
|
status_code: statusCode,
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export interface GatewayContract {
|
|
2
|
+
name: string;
|
|
3
|
+
hosts: Set<string>;
|
|
4
|
+
qualifiedEndpoints: Set<string>;
|
|
5
|
+
costSource: string;
|
|
6
|
+
upstreamCostSource: string;
|
|
7
|
+
}
|
|
8
|
+
export declare const GATEWAYS: Record<string, GatewayContract>;
|
|
9
|
+
export declare function detectGateway(client: unknown): string | undefined;
|
|
10
|
+
export declare function resolveGateway(value: unknown): string;
|
|
11
|
+
export declare function gatewayEvidence(gateway: string | undefined, endpoint: string, response: unknown): Record<string, unknown>;
|
package/dist/gateway.js
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Generic OpenAI-compatible gateway detection and billing-evidence extraction.
|
|
3
|
+
// A gateway is described by trusted hosts, the endpoints whose responses are
|
|
4
|
+
// qualified for cost evidence, and fixed provenance strings. OpenRouter is the
|
|
5
|
+
// first qualified contract. The SDK only transports a small scalar allowlist
|
|
6
|
+
// from a response it already observes; it never computes cost, fetches catalog
|
|
7
|
+
// data, or infers a served provider.
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.GATEWAYS = void 0;
|
|
10
|
+
exports.detectGateway = detectGateway;
|
|
11
|
+
exports.resolveGateway = resolveGateway;
|
|
12
|
+
exports.gatewayEvidence = gatewayEvidence;
|
|
13
|
+
const OPENROUTER = {
|
|
14
|
+
name: "openrouter",
|
|
15
|
+
hosts: new Set(["openrouter.ai"]),
|
|
16
|
+
qualifiedEndpoints: new Set(["chat.completions"]),
|
|
17
|
+
costSource: "openrouter.usage.cost",
|
|
18
|
+
upstreamCostSource: "openrouter.usage.cost_details.upstream_inference_cost",
|
|
19
|
+
};
|
|
20
|
+
exports.GATEWAYS = { [OPENROUTER.name]: OPENROUTER };
|
|
21
|
+
const MAX_IDENTIFIER_LEN = 512;
|
|
22
|
+
function get(value, key) {
|
|
23
|
+
return value && typeof value === "object" ? value[key] : undefined;
|
|
24
|
+
}
|
|
25
|
+
// Exact-hostname match over HTTPS, never substring. Paths do not affect it. The
|
|
26
|
+
// caller restricts auto-detection to OpenAI-compatible clients.
|
|
27
|
+
function detectGateway(client) {
|
|
28
|
+
const baseUrl = get(client, "baseURL") ?? get(client, "base_url");
|
|
29
|
+
if (typeof baseUrl !== "string" || !baseUrl.trim())
|
|
30
|
+
return undefined;
|
|
31
|
+
let url;
|
|
32
|
+
try {
|
|
33
|
+
url = new URL(baseUrl.trim());
|
|
34
|
+
}
|
|
35
|
+
catch {
|
|
36
|
+
return undefined;
|
|
37
|
+
}
|
|
38
|
+
if (url.protocol !== "https:")
|
|
39
|
+
return undefined;
|
|
40
|
+
for (const contract of Object.values(exports.GATEWAYS)) {
|
|
41
|
+
if (contract.hosts.has(url.hostname))
|
|
42
|
+
return contract.name;
|
|
43
|
+
}
|
|
44
|
+
return undefined;
|
|
45
|
+
}
|
|
46
|
+
// The caller value is never interpolated into the error: it may be a secret.
|
|
47
|
+
function resolveGateway(value) {
|
|
48
|
+
const name = String(value).trim().toLowerCase();
|
|
49
|
+
if (!(name in exports.GATEWAYS)) {
|
|
50
|
+
throw new Error("metergraph.wrap() received an unsupported gateway; supported gateways are: "
|
|
51
|
+
+ Object.keys(exports.GATEWAYS).sort().join(", "));
|
|
52
|
+
}
|
|
53
|
+
return name;
|
|
54
|
+
}
|
|
55
|
+
// Finite, non-negative number; booleans (typeof "boolean") and non-numbers are
|
|
56
|
+
// rejected, zero is preserved.
|
|
57
|
+
function billingAmount(value) {
|
|
58
|
+
if (typeof value !== "number" || !Number.isFinite(value) || value < 0)
|
|
59
|
+
return undefined;
|
|
60
|
+
return value;
|
|
61
|
+
}
|
|
62
|
+
// Identity (gateway, served_model) is emitted for any detected gateway call;
|
|
63
|
+
// cost evidence and its fixed sources only on the contract's qualified
|
|
64
|
+
// endpoints. A source appears only alongside the value it describes. The served
|
|
65
|
+
// provider is never inferred.
|
|
66
|
+
function gatewayEvidence(gateway, endpoint, response) {
|
|
67
|
+
try {
|
|
68
|
+
if (!gateway)
|
|
69
|
+
return {};
|
|
70
|
+
const contract = exports.GATEWAYS[gateway];
|
|
71
|
+
if (!contract)
|
|
72
|
+
return {};
|
|
73
|
+
const evidence = { gateway: contract.name };
|
|
74
|
+
const servedModel = get(response, "model");
|
|
75
|
+
if (typeof servedModel === "string" && servedModel.trim()) {
|
|
76
|
+
evidence.served_model = servedModel.slice(0, MAX_IDENTIFIER_LEN);
|
|
77
|
+
}
|
|
78
|
+
if (contract.qualifiedEndpoints.has(endpoint)) {
|
|
79
|
+
const usage = get(response, "usage");
|
|
80
|
+
const cost = billingAmount(get(usage, "cost"));
|
|
81
|
+
if (cost !== undefined) {
|
|
82
|
+
evidence.reported_cost_usd = cost;
|
|
83
|
+
evidence.reported_cost_source = contract.costSource;
|
|
84
|
+
}
|
|
85
|
+
const details = get(usage, "cost_details");
|
|
86
|
+
const upstream = billingAmount(get(details, "upstream_inference_cost"));
|
|
87
|
+
if (upstream !== undefined) {
|
|
88
|
+
evidence.reported_upstream_cost_usd = upstream;
|
|
89
|
+
evidence.reported_upstream_cost_source = contract.upstreamCostSource;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return evidence;
|
|
93
|
+
}
|
|
94
|
+
catch {
|
|
95
|
+
return {};
|
|
96
|
+
}
|
|
97
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { route, setDefaultTags, setSession, setTags, trace, withContext, withSes
|
|
|
2
2
|
import { BatchFirstIneligibleError, batchFirst, type BatchFirstMetadata, type BatchFirstPolicy, type BatchFirstProvider, type BatchFirstRequest, type BatchFirstResult, type BatchFirstSource, type LateBatchInfo } from "./batch-first.js";
|
|
3
3
|
import { track } from "./track.js";
|
|
4
4
|
import { type TransportMode, type WaitUntil } from "./transport.js";
|
|
5
|
+
import { type WrapOptions } from "./wrap.js";
|
|
5
6
|
import { type VercelAISDKMiddleware, type VercelAISDKMiddlewareOptions as VercelAISDKCompatibilityOptions, type VercelAISDKMiddlewareSpecificationOptions, type VercelAISDKMiddlewareVersionOptions, type VercelAISDKSpecificationVersion } from "./vercel-ai.js";
|
|
6
7
|
export interface MetergraphOptions {
|
|
7
8
|
token?: string;
|
|
@@ -53,8 +54,11 @@ export declare function wrapHandler<TArgs extends unknown[], TResult>(handler: (
|
|
|
53
54
|
*
|
|
54
55
|
* Calls init() automatically, so with env-var configuration this is the
|
|
55
56
|
* only setup line needed. Call init(...) first to pass options in code.
|
|
57
|
+
* OpenAI-compatible clients pointed at OpenRouter are detected automatically;
|
|
58
|
+
* pass `{ gateway: "openrouter" }` to force it for a trusted custom domain.
|
|
56
59
|
*/
|
|
57
60
|
export declare function wrap<T extends Record<PropertyKey, any>>(client: T, provider?: "openai" | "anthropic" | "google"): T;
|
|
61
|
+
export declare function wrap<T extends Record<PropertyKey, any>>(client: T, options: WrapOptions): T;
|
|
58
62
|
export declare const wrapClient: typeof wrap;
|
|
59
63
|
/**
|
|
60
64
|
* Capture Vercel AI SDK language-model calls through its middleware API.
|
|
@@ -64,7 +68,7 @@ export declare const wrapClient: typeof wrap;
|
|
|
64
68
|
export declare function vercelAISDKMiddleware(options: MetergraphOptions & VercelAISDKMiddlewareVersionOptions): VercelAISDKMiddleware<"v2">;
|
|
65
69
|
export declare function vercelAISDKMiddleware<TVersion extends VercelAISDKSpecificationVersion = "v3">(options?: MetergraphOptions & VercelAISDKMiddlewareSpecificationOptions<TVersion>): VercelAISDKMiddleware<TVersion>;
|
|
66
70
|
export { route, setDefaultTags, setSession, setTags, trace, track, withContext, withSession, withTags, };
|
|
67
|
-
export type { ContextOptions, RouteOptions, TraceOptions, TransportMode, VercelAISDKMiddleware, VercelAISDKSpecificationVersion, };
|
|
71
|
+
export type { ContextOptions, RouteOptions, TraceOptions, TransportMode, VercelAISDKMiddleware, VercelAISDKSpecificationVersion, WrapOptions, };
|
|
68
72
|
/**
|
|
69
73
|
* Explicit, opt-in batch-first execution over a provider's Batch API —
|
|
70
74
|
* never enabled by wrap()/capture defaults or by an environment variable.
|
package/dist/index.js
CHANGED
|
@@ -197,16 +197,10 @@ function wrapHandler(handler) {
|
|
|
197
197
|
}
|
|
198
198
|
};
|
|
199
199
|
}
|
|
200
|
-
|
|
201
|
-
* Wrap an OpenAI, Anthropic, or Google client for capture.
|
|
202
|
-
*
|
|
203
|
-
* Calls init() automatically, so with env-var configuration this is the
|
|
204
|
-
* only setup line needed. Call init(...) first to pass options in code.
|
|
205
|
-
*/
|
|
206
|
-
function wrap(client, provider) {
|
|
200
|
+
function wrap(client, providerOrOptions) {
|
|
207
201
|
if (!initialized)
|
|
208
202
|
init();
|
|
209
|
-
return (0, wrap_js_1.wrap)(client,
|
|
203
|
+
return (0, wrap_js_1.wrap)(client, providerOrOptions);
|
|
210
204
|
}
|
|
211
205
|
exports.wrapClient = wrap;
|
|
212
206
|
function vercelAISDKMiddleware(options = {}) {
|
package/dist/vercel-ai.js
CHANGED
|
@@ -82,6 +82,8 @@ function wrapStreamResult(result, capture, state, started) {
|
|
|
82
82
|
let streamError;
|
|
83
83
|
const content = [];
|
|
84
84
|
const responseMetadata = {};
|
|
85
|
+
// Telemetry inspection of one part. Reads part fields, so callers run it
|
|
86
|
+
// fail-open; a fault must not block delivery or become a stream error.
|
|
85
87
|
const observe = (part) => {
|
|
86
88
|
if (firstOutputAt === undefined && outputChunk(part))
|
|
87
89
|
firstOutputAt = performance.now();
|
|
@@ -101,19 +103,36 @@ function wrapStreamResult(result, capture, state, started) {
|
|
|
101
103
|
streamError = part.error ?? new Error("AI SDK stream error");
|
|
102
104
|
}
|
|
103
105
|
};
|
|
106
|
+
const observeSafely = (part) => {
|
|
107
|
+
try {
|
|
108
|
+
observe(part);
|
|
109
|
+
}
|
|
110
|
+
catch {
|
|
111
|
+
// Telemetry inspection fault: skip this part, deliver it unchanged.
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
// End-of-stream response assembly reads finish-part fields, so it is telemetry
|
|
115
|
+
// and fail-open; a partial or lost trace is acceptable.
|
|
116
|
+
const assembleResponse = () => {
|
|
117
|
+
try {
|
|
118
|
+
if (text)
|
|
119
|
+
content.unshift({ type: "text", text });
|
|
120
|
+
return {
|
|
121
|
+
content,
|
|
122
|
+
usage: finishPart?.usage,
|
|
123
|
+
finishReason: finishPart?.finishReason,
|
|
124
|
+
response: {
|
|
125
|
+
...responseMetadata,
|
|
126
|
+
...(result.response ?? {}),
|
|
127
|
+
},
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
catch {
|
|
131
|
+
return undefined;
|
|
132
|
+
}
|
|
133
|
+
};
|
|
104
134
|
const finish = (status, error = streamError) => {
|
|
105
|
-
|
|
106
|
-
content.unshift({ type: "text", text });
|
|
107
|
-
const response = {
|
|
108
|
-
content,
|
|
109
|
-
usage: finishPart?.usage,
|
|
110
|
-
finishReason: finishPart?.finishReason,
|
|
111
|
-
response: {
|
|
112
|
-
...responseMetadata,
|
|
113
|
-
...(result.response ?? {}),
|
|
114
|
-
},
|
|
115
|
-
};
|
|
116
|
-
finishCapture(capture, state, response, {
|
|
135
|
+
finishCapture(capture, state, assembleResponse(), {
|
|
117
136
|
error,
|
|
118
137
|
status,
|
|
119
138
|
stream: true,
|
|
@@ -123,20 +142,22 @@ function wrapStreamResult(result, capture, state, started) {
|
|
|
123
142
|
};
|
|
124
143
|
const stream = new ReadableStream({
|
|
125
144
|
async pull(controller) {
|
|
145
|
+
let next;
|
|
126
146
|
try {
|
|
127
|
-
|
|
128
|
-
if (next.done) {
|
|
129
|
-
finish();
|
|
130
|
-
controller.close();
|
|
131
|
-
return;
|
|
132
|
-
}
|
|
133
|
-
observe(next.value);
|
|
134
|
-
controller.enqueue(next.value);
|
|
147
|
+
next = await reader.read();
|
|
135
148
|
}
|
|
136
149
|
catch (error) {
|
|
137
150
|
finish("error", error);
|
|
138
151
|
controller.error(error);
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
if (next.done) {
|
|
155
|
+
finish();
|
|
156
|
+
controller.close();
|
|
157
|
+
return;
|
|
139
158
|
}
|
|
159
|
+
observeSafely(next.value);
|
|
160
|
+
controller.enqueue(next.value);
|
|
140
161
|
},
|
|
141
162
|
async cancel(reason) {
|
|
142
163
|
try {
|
package/dist/version.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const SDK_VERSION = "0.6.
|
|
1
|
+
export declare const SDK_VERSION = "0.6.5";
|
package/dist/version.js
CHANGED
package/dist/wrap.d.ts
CHANGED
|
@@ -12,6 +12,15 @@ export declare const OPENAI_SEAMS: Seam[];
|
|
|
12
12
|
export declare const ANTHROPIC_SEAMS: Seam[];
|
|
13
13
|
export declare const GOOGLE_SEAMS: Seam[];
|
|
14
14
|
export declare const SEAM_TABLES: Record<string, Seam[]>;
|
|
15
|
-
export
|
|
15
|
+
export type ProviderName = "openai" | "anthropic" | "google";
|
|
16
|
+
export type WrapOptions = {
|
|
17
|
+
provider?: ProviderName;
|
|
18
|
+
gateway?: never;
|
|
19
|
+
} | {
|
|
20
|
+
provider?: Extract<ProviderName, "openai">;
|
|
21
|
+
gateway: "openrouter";
|
|
22
|
+
};
|
|
23
|
+
export declare function wrap<T extends AnyRecord>(client: T, provider?: ProviderName): T;
|
|
24
|
+
export declare function wrap<T extends AnyRecord>(client: T, options: WrapOptions): T;
|
|
16
25
|
export declare const wrapClient: typeof wrap;
|
|
17
26
|
export {};
|
package/dist/wrap.js
CHANGED
|
@@ -6,6 +6,7 @@ exports.getCaptureRuntime = getCaptureRuntime;
|
|
|
6
6
|
exports.wrap = wrap;
|
|
7
7
|
const capture_js_1 = require("./capture.js");
|
|
8
8
|
const context_js_1 = require("./context.js");
|
|
9
|
+
const gateway_js_1 = require("./gateway.js");
|
|
9
10
|
let runtime;
|
|
10
11
|
const seenBatchItems = new Set();
|
|
11
12
|
// Tracks which resource *owners* (e.g. a specific chat.completions instance)
|
|
@@ -31,6 +32,26 @@ function requestFrom(args) {
|
|
|
31
32
|
function get(value, key) {
|
|
32
33
|
return value && typeof value === "object" ? value[key] : undefined;
|
|
33
34
|
}
|
|
35
|
+
// Capture is fail-open: a start() fault drops instrumentation for the call, a
|
|
36
|
+
// finish() fault drops the row. Neither may reach the provider path.
|
|
37
|
+
function startCapture(capture, provider, endpoint, request, stack) {
|
|
38
|
+
try {
|
|
39
|
+
return capture.start(provider, endpoint, request, stack);
|
|
40
|
+
}
|
|
41
|
+
catch {
|
|
42
|
+
return undefined;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
function finishCapture(capture, state, response, extra = {}) {
|
|
46
|
+
if (!state)
|
|
47
|
+
return;
|
|
48
|
+
try {
|
|
49
|
+
capture.finish(state, response, extra);
|
|
50
|
+
}
|
|
51
|
+
catch {
|
|
52
|
+
// Telemetry finalization is fail-open.
|
|
53
|
+
}
|
|
54
|
+
}
|
|
34
55
|
function markBatchItem(key) {
|
|
35
56
|
if (seenBatchItems.has(key))
|
|
36
57
|
return false;
|
|
@@ -204,7 +225,7 @@ function patchAnthropicBatchResults(owner) {
|
|
|
204
225
|
owner.results = wrapped;
|
|
205
226
|
return true;
|
|
206
227
|
}
|
|
207
|
-
function streamProxy(stream, state, capture) {
|
|
228
|
+
function streamProxy(stream, state, capture, suppressUsageOnly) {
|
|
208
229
|
let last;
|
|
209
230
|
let ttftMs;
|
|
210
231
|
const parts = [];
|
|
@@ -244,7 +265,7 @@ function streamProxy(stream, state, capture) {
|
|
|
244
265
|
return chunk;
|
|
245
266
|
};
|
|
246
267
|
const finish = (response = last, error, status) => {
|
|
247
|
-
capture
|
|
268
|
+
finishCapture(capture, state, response, {
|
|
248
269
|
error,
|
|
249
270
|
status,
|
|
250
271
|
stream: true,
|
|
@@ -262,41 +283,57 @@ function streamProxy(stream, state, capture) {
|
|
|
262
283
|
return async function* () {
|
|
263
284
|
try {
|
|
264
285
|
for await (const chunk of target) {
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
&& Array.isArray(chunk?.choices)
|
|
269
|
-
&& chunk.choices.length === 0
|
|
270
|
-
&& chunk.usage != null;
|
|
271
|
-
if (!usageOnly)
|
|
272
|
-
yield value;
|
|
273
|
-
}
|
|
274
|
-
let final = last;
|
|
275
|
-
if (typeof target.finalMessage === "function") {
|
|
286
|
+
// Telemetry boundary: observation and usage-only classification.
|
|
287
|
+
// An ordinary telemetry fault yields the raw chunk and continues.
|
|
288
|
+
let usageOnly = false;
|
|
276
289
|
try {
|
|
277
|
-
|
|
290
|
+
observe(chunk);
|
|
291
|
+
// Suppress the usage-only final chunk only when MeterGraph
|
|
292
|
+
// injected stream_options.include_usage; a caller who asked for
|
|
293
|
+
// it keeps their chunk. Capture reads usage from it either way.
|
|
294
|
+
usageOnly = suppressUsageOnly
|
|
295
|
+
&& state.provider === "openai"
|
|
296
|
+
&& state.endpoint === "chat.completions"
|
|
297
|
+
&& Array.isArray(chunk?.choices)
|
|
298
|
+
&& chunk.choices.length === 0
|
|
299
|
+
&& chunk.usage != null;
|
|
300
|
+
}
|
|
301
|
+
catch {
|
|
302
|
+
yield chunk;
|
|
303
|
+
continue;
|
|
278
304
|
}
|
|
279
|
-
|
|
305
|
+
if (!usageOnly)
|
|
306
|
+
yield chunk;
|
|
280
307
|
}
|
|
281
|
-
finish(final);
|
|
282
308
|
}
|
|
283
309
|
catch (error) {
|
|
310
|
+
// Provider iteration error: finalize fail-open, keep its identity.
|
|
284
311
|
finish(last, error);
|
|
285
312
|
throw error;
|
|
286
313
|
}
|
|
314
|
+
// Exhaustion: enrich with finalMessage when present, then finalize.
|
|
315
|
+
let final = last;
|
|
316
|
+
if (typeof target.finalMessage === "function") {
|
|
317
|
+
try {
|
|
318
|
+
final = await target.finalMessage();
|
|
319
|
+
}
|
|
320
|
+
catch { /* enrichment is best-effort */ }
|
|
321
|
+
}
|
|
322
|
+
finish(final);
|
|
287
323
|
};
|
|
288
324
|
}
|
|
289
325
|
if (property === "finalMessage" && typeof target.finalMessage === "function") {
|
|
290
326
|
return async (...args) => {
|
|
327
|
+
let final;
|
|
291
328
|
try {
|
|
292
|
-
|
|
293
|
-
finish(final);
|
|
294
|
-
return final;
|
|
329
|
+
final = await target.finalMessage(...args);
|
|
295
330
|
}
|
|
296
331
|
catch (error) {
|
|
297
332
|
finish(last, error);
|
|
298
333
|
throw error;
|
|
299
334
|
}
|
|
335
|
+
finish(final);
|
|
336
|
+
return final;
|
|
300
337
|
};
|
|
301
338
|
}
|
|
302
339
|
if (property === "close" || property === "abort") {
|
|
@@ -304,12 +341,20 @@ function streamProxy(stream, state, capture) {
|
|
|
304
341
|
if (typeof original !== "function")
|
|
305
342
|
return original;
|
|
306
343
|
return (...args) => {
|
|
344
|
+
let returned;
|
|
345
|
+
let error;
|
|
346
|
+
let threw = false;
|
|
307
347
|
try {
|
|
308
|
-
|
|
348
|
+
returned = original.apply(target, args);
|
|
309
349
|
}
|
|
310
|
-
|
|
311
|
-
|
|
350
|
+
catch (caught) {
|
|
351
|
+
threw = true;
|
|
352
|
+
error = caught;
|
|
312
353
|
}
|
|
354
|
+
finish(last, undefined, "abandoned");
|
|
355
|
+
if (threw)
|
|
356
|
+
throw error;
|
|
357
|
+
return returned;
|
|
313
358
|
};
|
|
314
359
|
}
|
|
315
360
|
const value = Reflect.get(target, property, receiver);
|
|
@@ -317,7 +362,50 @@ function streamProxy(stream, state, capture) {
|
|
|
317
362
|
},
|
|
318
363
|
});
|
|
319
364
|
}
|
|
320
|
-
|
|
365
|
+
// The provider SDKs (openai, @anthropic-ai/sdk) return an APIPromise — a
|
|
366
|
+
// Promise subclass carrying helpers like .withResponse()/.asResponse(). Chaining
|
|
367
|
+
// .then() would resolve through Promise species into a plain Promise and drop
|
|
368
|
+
// those helpers, so wrap the original in a proxy that routes await/then/catch/
|
|
369
|
+
// finally through capture while forwarding every helper to the untouched
|
|
370
|
+
// original. The provider memoizes its single in-flight request, so preserving
|
|
371
|
+
// the helpers does not issue it twice.
|
|
372
|
+
function preserveProviderPromise(target, complete, fail) {
|
|
373
|
+
let chained;
|
|
374
|
+
const captured = () => (chained ??= target.then(complete, fail));
|
|
375
|
+
return new Proxy(target, {
|
|
376
|
+
get(t, property, receiver) {
|
|
377
|
+
if (property === "then")
|
|
378
|
+
return (onF, onR) => captured().then(onF, onR);
|
|
379
|
+
if (property === "catch")
|
|
380
|
+
return (onR) => captured().catch(onR);
|
|
381
|
+
if (property === "finally")
|
|
382
|
+
return (onFin) => captured().finally(onFin);
|
|
383
|
+
if (property === "withResponse" && typeof t.withResponse === "function") {
|
|
384
|
+
// .withResponse() carries the parsed body (or a stream) plus the raw
|
|
385
|
+
// Response. Route data through complete() so a streaming body stays
|
|
386
|
+
// instrumented and capture fires exactly once, then hand back a fresh
|
|
387
|
+
// envelope — the provider's own object is left untouched.
|
|
388
|
+
return async (...a) => {
|
|
389
|
+
let envelope;
|
|
390
|
+
try {
|
|
391
|
+
envelope = await t.withResponse(...a);
|
|
392
|
+
}
|
|
393
|
+
catch (error) {
|
|
394
|
+
fail(error);
|
|
395
|
+
}
|
|
396
|
+
return { ...envelope, data: complete(envelope.data) };
|
|
397
|
+
};
|
|
398
|
+
}
|
|
399
|
+
// Every other helper (.asResponse(), ._thenUnwrap(), ...) is forwarded to
|
|
400
|
+
// the untouched original. .asResponse() intentionally returns the raw
|
|
401
|
+
// Response with no parsed body, so there is nothing for MeterGraph to
|
|
402
|
+
// normalize on that path; capture stays with the parsing paths above.
|
|
403
|
+
const value = Reflect.get(t, property, receiver);
|
|
404
|
+
return typeof value === "function" ? value.bind(t) : value;
|
|
405
|
+
},
|
|
406
|
+
});
|
|
407
|
+
}
|
|
408
|
+
function patch(owner, method, provider, endpoint, gatewayName) {
|
|
321
409
|
if (!owner || typeof owner[method] !== "function")
|
|
322
410
|
return false;
|
|
323
411
|
if (owner[method].__metergraph__)
|
|
@@ -347,40 +435,66 @@ function patch(owner, method, provider, endpoint) {
|
|
|
347
435
|
const incoming = requestFrom(args);
|
|
348
436
|
const patchUsage = typeof process === "undefined"
|
|
349
437
|
|| process.env.METERGRAPH_PATCH_STREAM_USAGE !== "0";
|
|
438
|
+
let injectedUsage = false;
|
|
350
439
|
if (provider === "openai"
|
|
351
440
|
&& endpoint === "chat.completions"
|
|
441
|
+
&& !gatewayName // a gateway supplies its final usage event itself; no request mutation
|
|
352
442
|
&& incoming.stream === true
|
|
353
443
|
&& incoming.stream_options == null
|
|
354
444
|
&& patchUsage) {
|
|
355
445
|
args = [{ ...incoming, stream_options: { include_usage: true } }, ...args.slice(1)];
|
|
446
|
+
injectedUsage = true;
|
|
447
|
+
}
|
|
448
|
+
const state = startCapture(capture, provider, endpoint, requestFrom(args), new Error().stack);
|
|
449
|
+
if (state && gatewayName)
|
|
450
|
+
state.gateway = gatewayName;
|
|
451
|
+
if (!state) {
|
|
452
|
+
// start faulted: invoke the provider once, return its result unwrapped.
|
|
453
|
+
reentrantOwners.add(owner);
|
|
454
|
+
try {
|
|
455
|
+
return original.apply(owner, args);
|
|
456
|
+
}
|
|
457
|
+
finally {
|
|
458
|
+
reentrantOwners.delete(owner);
|
|
459
|
+
}
|
|
356
460
|
}
|
|
357
|
-
const state = capture.start(provider, endpoint, requestFrom(args), new Error().stack);
|
|
358
461
|
let result;
|
|
359
462
|
reentrantOwners.add(owner);
|
|
360
463
|
try {
|
|
361
464
|
result = original.apply(owner, args);
|
|
362
465
|
}
|
|
363
466
|
catch (error) {
|
|
364
|
-
capture
|
|
467
|
+
finishCapture(capture, state, undefined, { error });
|
|
365
468
|
throw error;
|
|
366
469
|
}
|
|
367
470
|
finally {
|
|
368
471
|
reentrantOwners.delete(owner);
|
|
369
472
|
}
|
|
473
|
+
// complete()/fail() finalize capture at most once regardless of which
|
|
474
|
+
// consumption path (await, .withResponse(), ...) reaches the value first.
|
|
475
|
+
let settled = false;
|
|
476
|
+
let streamValue;
|
|
370
477
|
const complete = (response) => {
|
|
371
478
|
const request = requestFrom(args);
|
|
372
479
|
const streaming = endpoint.endsWith(".stream") || request.stream === true;
|
|
373
480
|
if (streaming && response && (response[Symbol.asyncIterator] || response.finalMessage)) {
|
|
374
|
-
return streamProxy(response, state, capture);
|
|
481
|
+
return (streamValue ??= streamProxy(response, state, capture, injectedUsage));
|
|
482
|
+
}
|
|
483
|
+
if (!settled) {
|
|
484
|
+
settled = true;
|
|
485
|
+
finishCapture(capture, state, response);
|
|
375
486
|
}
|
|
376
|
-
capture.finish(state, response);
|
|
377
487
|
return response;
|
|
378
488
|
};
|
|
489
|
+
const fail = (error) => {
|
|
490
|
+
if (!settled) {
|
|
491
|
+
settled = true;
|
|
492
|
+
finishCapture(capture, state, undefined, { error });
|
|
493
|
+
}
|
|
494
|
+
throw error;
|
|
495
|
+
};
|
|
379
496
|
if (result && typeof result.then === "function") {
|
|
380
|
-
return result
|
|
381
|
-
capture.finish(state, undefined, { error });
|
|
382
|
-
throw error;
|
|
383
|
-
});
|
|
497
|
+
return preserveProviderPromise(result, complete, fail);
|
|
384
498
|
}
|
|
385
499
|
return complete(result);
|
|
386
500
|
};
|
|
@@ -441,7 +555,7 @@ function resolveSeam(client, path) {
|
|
|
441
555
|
}
|
|
442
556
|
return obj;
|
|
443
557
|
}
|
|
444
|
-
function applySeams(client, provider) {
|
|
558
|
+
function applySeams(client, provider, gatewayName) {
|
|
445
559
|
const patched = [];
|
|
446
560
|
for (const seam of exports.SEAM_TABLES[provider] ?? []) {
|
|
447
561
|
let owner;
|
|
@@ -451,7 +565,7 @@ function applySeams(client, provider) {
|
|
|
451
565
|
catch {
|
|
452
566
|
continue; // a pathological client property must never break wrap()
|
|
453
567
|
}
|
|
454
|
-
if (owner !== undefined && patch(owner, seam.method, provider, seam.endpoint)) {
|
|
568
|
+
if (owner !== undefined && patch(owner, seam.method, provider, seam.endpoint, gatewayName)) {
|
|
455
569
|
patched.push(`${seam.path}.${seam.method}`);
|
|
456
570
|
}
|
|
457
571
|
}
|
|
@@ -469,16 +583,43 @@ function applyBatchExtras(client, provider) {
|
|
|
469
583
|
}
|
|
470
584
|
return patched;
|
|
471
585
|
}
|
|
472
|
-
|
|
586
|
+
// A gateway override requires the OpenAI-compatible contract, so a provider is
|
|
587
|
+
// accepted only when it is "openai". Unsupported/contradictory setup throws
|
|
588
|
+
// before any provider call, without echoing a URL or credential.
|
|
589
|
+
function validateGatewayConfig(client, provider, gateway) {
|
|
590
|
+
if (gateway === undefined)
|
|
591
|
+
return undefined;
|
|
592
|
+
const overrideGateway = (0, gateway_js_1.resolveGateway)(gateway);
|
|
593
|
+
if (provider !== undefined && String(provider).toLowerCase() !== "openai") {
|
|
594
|
+
throw new Error("metergraph.wrap() gateway requires an OpenAI-compatible provider");
|
|
595
|
+
}
|
|
596
|
+
if (detectProvider(client) !== "openai") {
|
|
597
|
+
throw new Error("metergraph.wrap() gateway requires an OpenAI-compatible client");
|
|
598
|
+
}
|
|
599
|
+
return overrideGateway;
|
|
600
|
+
}
|
|
601
|
+
function wrap(client, providerOrOptions) {
|
|
602
|
+
let provider;
|
|
603
|
+
let gateway;
|
|
604
|
+
if (typeof providerOrOptions === "string") {
|
|
605
|
+
provider = providerOrOptions;
|
|
606
|
+
}
|
|
607
|
+
else if (providerOrOptions && typeof providerOrOptions === "object") {
|
|
608
|
+
provider = providerOrOptions.provider;
|
|
609
|
+
gateway = providerOrOptions.gateway;
|
|
610
|
+
}
|
|
611
|
+
const overrideGateway = validateGatewayConfig(client, provider, gateway);
|
|
473
612
|
try {
|
|
474
613
|
const name = provider ?? detectProvider(client);
|
|
475
|
-
const
|
|
614
|
+
const gatewayName = overrideGateway ?? (name === "openai" ? (0, gateway_js_1.detectGateway)(client) : undefined);
|
|
615
|
+
const patched = applySeams(client, name, gatewayName);
|
|
476
616
|
const patchedCount = patched.length + applyBatchExtras(client, name);
|
|
617
|
+
const label = gatewayName ? `${gatewayName} gateway via ${name}` : name;
|
|
477
618
|
if (!patchedCount) {
|
|
478
|
-
console.warn(`Metergraph found no supported methods on ${
|
|
619
|
+
console.warn(`Metergraph found no supported methods on ${label} client`);
|
|
479
620
|
}
|
|
480
621
|
else {
|
|
481
|
-
console.info(`Metergraph patched ${patchedCount} seam(s) on ${
|
|
622
|
+
console.info(`Metergraph patched ${patchedCount} seam(s) on ${label} client: ${patched.join(", ") || "(batch-only)"}`);
|
|
482
623
|
}
|
|
483
624
|
}
|
|
484
625
|
catch (error) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "metergraph",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.5",
|
|
4
4
|
"description": "Fire-and-forget LLM spend capture for Metergraph",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
"test": "npm run build && npm run test:types && npm run test:examples && node --test test/*.test.mjs",
|
|
23
23
|
"test:examples": "tsc --noEmit --strict --skipLibCheck --target ES2022 --module NodeNext --moduleResolution NodeNext test/ai-sdk-factories.types.ts",
|
|
24
24
|
"test:package": "npm run build && node --test test/package/packed-package.test.mjs",
|
|
25
|
-
"test:types": "tsc --noEmit --strict --skipLibCheck --target ES2022 --module NodeNext --moduleResolution NodeNext test/ai-sdk-current.types.ts"
|
|
25
|
+
"test:types": "tsc --noEmit --strict --skipLibCheck --target ES2022 --module NodeNext --moduleResolution NodeNext test/ai-sdk-current.types.ts test/wrap-gateway.types.ts"
|
|
26
26
|
},
|
|
27
27
|
"engines": {
|
|
28
28
|
"node": ">=18"
|