metergraph 0.3.0 → 0.3.2
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 +54 -6
- package/dist/capture.d.ts +1 -1
- package/dist/capture.js +74 -15
- package/dist/index.d.ts +8 -1
- package/dist/index.js +5 -0
- package/dist/vercel-ai.d.ts +38 -0
- package/dist/vercel-ai.js +187 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/dist/wrap.d.ts +2 -0
- package/dist/wrap.js +29 -1
- package/package.json +8 -4
package/README.md
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
# metergraph (TypeScript)
|
|
2
2
|
|
|
3
|
-
Zero-runtime-dependency capture for OpenAI, Anthropic, and Gemini clients
|
|
4
|
-
|
|
5
|
-
are not part of the v1
|
|
3
|
+
Zero-runtime-dependency capture for OpenAI, Anthropic, and Gemini clients plus
|
|
4
|
+
provider-independent Vercel AI SDK language models on Node 18+, including AWS
|
|
5
|
+
Lambda. Worker-style `waitUntil` hooks are present but are not part of the v1
|
|
6
|
+
public-package qualification contract.
|
|
6
7
|
|
|
7
8
|
`wrap()` initializes capture from the environment, so setup is one line per
|
|
8
9
|
client; call `init(...)` before the first `wrap()` only to pass options in
|
|
@@ -33,6 +34,51 @@ recordOutcome("ticket-classifier", {
|
|
|
33
34
|
});
|
|
34
35
|
```
|
|
35
36
|
|
|
37
|
+
## Vercel AI SDK
|
|
38
|
+
|
|
39
|
+
Use Metergraph as language-model middleware. It records each actual provider
|
|
40
|
+
request made by `generateText` or `streamText`, so multi-step tool loops produce
|
|
41
|
+
one costed span per model call rather than an inaccurate aggregate:
|
|
42
|
+
|
|
43
|
+
```ts
|
|
44
|
+
import { generateText, wrapLanguageModel } from "ai";
|
|
45
|
+
import { openai } from "@ai-sdk/openai";
|
|
46
|
+
import * as mg from "metergraph";
|
|
47
|
+
|
|
48
|
+
const model = wrapLanguageModel({
|
|
49
|
+
model: openai("gpt-5.6-luna"),
|
|
50
|
+
middleware: mg.vercelAISDKMiddleware(),
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
const result = await mg.trace("support-answer", () =>
|
|
54
|
+
mg.route("support.answer", () =>
|
|
55
|
+
generateText({ model, prompt: "Help this customer" })
|
|
56
|
+
)
|
|
57
|
+
);
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
The middleware is provider-independent and retains the model/provider identity,
|
|
61
|
+
normalized input, output, cache, and reasoning tokens, response ID, finish
|
|
62
|
+
reason, latency, streaming TTFT, and tool calls. Vercel AI Gateway model IDs
|
|
63
|
+
such as `anthropic/claude-sonnet-5` are attributed to their upstream provider
|
|
64
|
+
for server-side pricing. Provider options, abort signals, and transport headers
|
|
65
|
+
are never serialized. Calls outside `trace()` remain valid one-span traces;
|
|
66
|
+
wrap a multi-call workflow in `trace()` to group its spans.
|
|
67
|
+
|
|
68
|
+
| Vercel AI SDK | Metergraph middleware | Node.js |
|
|
69
|
+
|---|---|---|
|
|
70
|
+
| 5 | `vercelAISDKMiddleware({ aiSdkVersion: 5 })` | 18+ |
|
|
71
|
+
| 6 | `vercelAISDKMiddleware()` | 18+ |
|
|
72
|
+
| 7 | `vercelAISDKMiddleware()` | 22+ |
|
|
73
|
+
|
|
74
|
+
Metergraph itself supports Node.js 18+; the AI SDK version you choose may
|
|
75
|
+
impose a newer runtime requirement — AI SDK 7 requires Node.js 22+.
|
|
76
|
+
|
|
77
|
+
Advanced/backward compatibility: the middleware also accepts a raw
|
|
78
|
+
`specificationVersion` (`"v2"` | `"v3"` | `"v4"`) instead of `aiSdkVersion`,
|
|
79
|
+
for callers pinning the underlying Vercel middleware protocol directly.
|
|
80
|
+
`aiSdkVersion` and `specificationVersion` cannot be combined.
|
|
81
|
+
|
|
36
82
|
`modelFor(routeName, { default, sessionKey })` takes an options object.
|
|
37
83
|
**Breaking change from `metergraph@0.1.0`**, which took positional
|
|
38
84
|
`modelFor(routeName, fallback, sessionKey?)` arguments; `default` is
|
|
@@ -99,7 +145,9 @@ wrapped client with mg.track("stable.name", fn), because stack-based
|
|
|
99
145
|
attribution is unreliable under bundlers. On serverless (Lambda / Workers /
|
|
100
146
|
Vercel), ensure delivery before the runtime freezes: mg.wrapHandler(handler),
|
|
101
147
|
or mg.bindWaitUntil(ctx) once per request, or await mg.flush() before
|
|
102
|
-
returning; long-running servers need nothing extra.
|
|
103
|
-
|
|
104
|
-
|
|
148
|
+
returning; long-running servers need nothing extra. For Vercel AI SDK calls,
|
|
149
|
+
wrap each language model with wrapLanguageModel({ model, middleware:
|
|
150
|
+
mg.vercelAISDKMiddleware() }); do not also wrap its internal provider transport.
|
|
151
|
+
When done, list every client and AI SDK model you instrumented, and flag LLM
|
|
152
|
+
calls made outside these supported paths.
|
|
105
153
|
```
|
package/dist/capture.d.ts
CHANGED
package/dist/capture.js
CHANGED
|
@@ -9,30 +9,64 @@ function first(value) {
|
|
|
9
9
|
return Array.isArray(value) ? value[0] : undefined;
|
|
10
10
|
}
|
|
11
11
|
function number(value) {
|
|
12
|
+
if (typeof value === "boolean")
|
|
13
|
+
return undefined;
|
|
12
14
|
const parsed = Number(value);
|
|
13
|
-
return value == null || !Number.
|
|
15
|
+
return value == null || !Number.isSafeInteger(parsed) || parsed < 0 ? undefined : parsed;
|
|
14
16
|
}
|
|
15
|
-
function
|
|
16
|
-
const value = get(response, "usage") ?? get(response, "usage_metadata") ?? get(response, "usageMetadata");
|
|
17
|
+
function usageValue(value) {
|
|
17
18
|
const prompt = get(value, "prompt_tokens_details") ?? get(value, "input_tokens_details")
|
|
18
19
|
?? get(value, "promptTokensDetails") ?? get(value, "inputTokensDetails");
|
|
19
20
|
const completion = get(value, "completion_tokens_details") ?? get(value, "output_tokens_details")
|
|
20
21
|
?? get(value, "completionTokensDetails") ?? get(value, "outputTokensDetails");
|
|
22
|
+
const aiInput = get(value, "inputTokens");
|
|
23
|
+
const aiOutput = get(value, "outputTokens");
|
|
24
|
+
const aiInputDetails = get(value, "inputTokenDetails");
|
|
25
|
+
const aiOutputDetails = get(value, "outputTokenDetails");
|
|
21
26
|
const cacheCreation = get(value, "cache_creation") ?? get(value, "cacheCreation");
|
|
22
27
|
return {
|
|
23
|
-
input_tokens: number(get(value, "prompt_tokens") ?? get(value, "input_tokens")
|
|
24
|
-
|
|
25
|
-
|
|
28
|
+
input_tokens: number(get(value, "prompt_tokens") ?? get(value, "input_tokens")
|
|
29
|
+
?? get(value, "promptTokenCount") ?? get(aiInput, "total") ?? aiInput),
|
|
30
|
+
output_tokens: number(get(value, "completion_tokens") ?? get(value, "output_tokens")
|
|
31
|
+
?? get(value, "candidatesTokenCount") ?? get(aiOutput, "total") ?? aiOutput),
|
|
32
|
+
cache_read_tokens: number(get(value, "cache_read_input_tokens")
|
|
33
|
+
?? get(prompt, "cached_tokens") ?? get(value, "cachedContentTokenCount")
|
|
34
|
+
?? get(aiInput, "cacheRead") ?? get(aiInputDetails, "cacheReadTokens")),
|
|
26
35
|
cache_write_tokens: number(get(value, "cache_creation_input_tokens")
|
|
27
36
|
?? get(prompt, "cache_write_tokens") ?? get(value, "cacheCreationInputTokens")
|
|
28
|
-
?? get(prompt, "cacheWriteTokens"))
|
|
37
|
+
?? get(prompt, "cacheWriteTokens") ?? get(aiInput, "cacheWrite")
|
|
38
|
+
?? get(aiInputDetails, "cacheWriteTokens")),
|
|
29
39
|
cache_write_5m_tokens: number(get(cacheCreation, "ephemeral_5m_input_tokens")
|
|
30
40
|
?? get(cacheCreation, "ephemeral5mInputTokens")),
|
|
31
41
|
cache_write_1h_tokens: number(get(cacheCreation, "ephemeral_1h_input_tokens")
|
|
32
42
|
?? get(cacheCreation, "ephemeral1hInputTokens")),
|
|
33
|
-
reasoning_tokens: number(get(completion, "reasoning_tokens")
|
|
43
|
+
reasoning_tokens: number(get(completion, "reasoning_tokens")
|
|
44
|
+
?? get(value, "thoughtsTokenCount") ?? get(aiOutput, "reasoning")
|
|
45
|
+
?? get(aiOutputDetails, "reasoningTokens")),
|
|
34
46
|
};
|
|
35
47
|
}
|
|
48
|
+
function usage(response) {
|
|
49
|
+
const value = get(response, "usage") ?? get(response, "usage_metadata")
|
|
50
|
+
?? get(response, "usageMetadata");
|
|
51
|
+
const normalized = usageValue(value);
|
|
52
|
+
const raw = get(value, "raw");
|
|
53
|
+
if (!raw || typeof raw !== "object")
|
|
54
|
+
return normalized;
|
|
55
|
+
// AI SDK v4 retains provider-native usage under `raw`. Standardized totals
|
|
56
|
+
// and cache counts have consistent gateway semantics, while raw data fills
|
|
57
|
+
// provider-only details such as Anthropic's cache-write TTL split.
|
|
58
|
+
const provider = usageValue(raw);
|
|
59
|
+
const providerSpecific = new Set([
|
|
60
|
+
"cache_write_5m_tokens",
|
|
61
|
+
"cache_write_1h_tokens",
|
|
62
|
+
]);
|
|
63
|
+
return Object.fromEntries(Object.entries(normalized).map(([key, fallback]) => [
|
|
64
|
+
key,
|
|
65
|
+
providerSpecific.has(key)
|
|
66
|
+
? provider[key] ?? fallback
|
|
67
|
+
: fallback ?? provider[key],
|
|
68
|
+
]));
|
|
69
|
+
}
|
|
36
70
|
function responseText(response) {
|
|
37
71
|
const direct = get(response, "output_text") ?? get(response, "text");
|
|
38
72
|
if (typeof direct === "string")
|
|
@@ -62,12 +96,17 @@ function responseText(response) {
|
|
|
62
96
|
}
|
|
63
97
|
export function chunkText(chunk) {
|
|
64
98
|
const delta = get(first(get(chunk, "choices")), "delta") ?? get(chunk, "delta");
|
|
99
|
+
if (typeof delta === "string" && get(chunk, "type") === "text-delta")
|
|
100
|
+
return delta;
|
|
65
101
|
const text = get(delta, "content") ?? get(delta, "text") ?? get(chunk, "text");
|
|
66
102
|
return typeof text === "string" ? text : undefined;
|
|
67
103
|
}
|
|
68
104
|
function stopReason(response) {
|
|
69
|
-
const value = get(response, "stop_reason") ?? get(response, "status")
|
|
70
|
-
|
|
105
|
+
const value = get(response, "stop_reason") ?? get(response, "status")
|
|
106
|
+
?? get(response, "finishReason") ?? get(response, "finish_reason")
|
|
107
|
+
?? get(first(get(response, "choices")), "finish_reason");
|
|
108
|
+
const normalized = get(value, "unified") ?? get(value, "raw") ?? value;
|
|
109
|
+
return normalized == null ? undefined : String(normalized);
|
|
71
110
|
}
|
|
72
111
|
function toolNames(request) {
|
|
73
112
|
if (!Array.isArray(request.tools))
|
|
@@ -144,6 +183,12 @@ function toolEvents(request, response, streamChunks = []) {
|
|
|
144
183
|
else if (kind === "tool_result") {
|
|
145
184
|
complete(get(block, "tool_use_id"), get(block, "content"), Boolean(get(block, "is_error")));
|
|
146
185
|
}
|
|
186
|
+
else if (kind === "tool-call") {
|
|
187
|
+
add(get(block, "toolCallId") ?? get(block, "tool_call_id") ?? get(block, "id"), get(block, "toolName") ?? get(block, "tool_name") ?? get(block, "name"), get(block, "input") ?? get(block, "arguments"));
|
|
188
|
+
}
|
|
189
|
+
else if (kind === "tool-result" || kind === "tool-error") {
|
|
190
|
+
complete(get(block, "toolCallId") ?? get(block, "tool_call_id") ?? get(block, "id"), get(block, "output") ?? get(block, "result") ?? get(block, "error"), kind === "tool-error" || Boolean(get(block, "isError") ?? get(block, "is_error")));
|
|
191
|
+
}
|
|
147
192
|
}
|
|
148
193
|
};
|
|
149
194
|
const geminiParts = (value) => {
|
|
@@ -165,7 +210,9 @@ function toolEvents(request, response, streamChunks = []) {
|
|
|
165
210
|
? request.input
|
|
166
211
|
: Array.isArray(request.contents)
|
|
167
212
|
? request.contents
|
|
168
|
-
:
|
|
213
|
+
: Array.isArray(request.prompt)
|
|
214
|
+
? request.prompt
|
|
215
|
+
: [];
|
|
169
216
|
for (const message of history) {
|
|
170
217
|
const messageCalls = get(message, "tool_calls");
|
|
171
218
|
if (Array.isArray(messageCalls)) {
|
|
@@ -233,7 +280,13 @@ function toolEvents(request, response, streamChunks = []) {
|
|
|
233
280
|
}
|
|
234
281
|
}
|
|
235
282
|
const kind = get(chunk, "type");
|
|
236
|
-
if (kind === "
|
|
283
|
+
if (kind === "tool-call") {
|
|
284
|
+
add(get(chunk, "toolCallId") ?? get(chunk, "tool_call_id") ?? get(chunk, "id"), get(chunk, "toolName") ?? get(chunk, "tool_name") ?? get(chunk, "name"), get(chunk, "input") ?? get(chunk, "arguments"));
|
|
285
|
+
}
|
|
286
|
+
else if (kind === "tool-result" || kind === "tool-error") {
|
|
287
|
+
complete(get(chunk, "toolCallId") ?? get(chunk, "tool_call_id") ?? get(chunk, "id"), get(chunk, "output") ?? get(chunk, "result") ?? get(chunk, "error"), kind === "tool-error" || Boolean(get(chunk, "isError") ?? get(chunk, "is_error")));
|
|
288
|
+
}
|
|
289
|
+
else if (kind === "content_block_start") {
|
|
237
290
|
const block = get(chunk, "content_block");
|
|
238
291
|
if (get(block, "type") === "tool_use") {
|
|
239
292
|
const key = String(get(chunk, "index") ?? anthropicDeltas.size);
|
|
@@ -295,15 +348,16 @@ function responseContent(response, aggregate) {
|
|
|
295
348
|
return candidates === undefined ? undefined : scrub(candidates);
|
|
296
349
|
}
|
|
297
350
|
function responseEnvelope(response, aggregate, tools, error, status) {
|
|
351
|
+
const responseMetadata = get(response, "response");
|
|
298
352
|
const envelope = {
|
|
299
353
|
role: "assistant",
|
|
300
354
|
content: responseContent(response, aggregate),
|
|
301
355
|
tool_calls: tools ?? [],
|
|
302
356
|
finish_reason: stopReason(response),
|
|
303
357
|
request_id: get(response, "_request_id") ?? get(response, "response_id")
|
|
304
|
-
?? get(response, "responseId") ?? get(response, "id"),
|
|
358
|
+
?? get(response, "responseId") ?? get(response, "id") ?? get(responseMetadata, "id"),
|
|
305
359
|
model: get(response, "model") ?? get(response, "model_version")
|
|
306
|
-
?? get(response, "modelVersion"),
|
|
360
|
+
?? get(response, "modelVersion") ?? get(responseMetadata, "modelId"),
|
|
307
361
|
status,
|
|
308
362
|
};
|
|
309
363
|
if (error !== undefined) {
|
|
@@ -426,6 +480,9 @@ export class CaptureRuntime {
|
|
|
426
480
|
}));
|
|
427
481
|
}
|
|
428
482
|
const firstFrame = state.frames[0];
|
|
483
|
+
const observedToolNames = fullTools
|
|
484
|
+
? [...new Set(fullTools.map((item) => item.name))]
|
|
485
|
+
: undefined;
|
|
429
486
|
this.transport.enqueue({
|
|
430
487
|
ts: state.ts,
|
|
431
488
|
route: state.context.route,
|
|
@@ -444,9 +501,11 @@ export class CaptureRuntime {
|
|
|
444
501
|
unit_name: state.context.unitName,
|
|
445
502
|
unit_count: state.context.unitCount,
|
|
446
503
|
tool_calls: fullTools ? persistedTools : toolNames(state.request),
|
|
504
|
+
tool_names: observedToolNames?.length ? observedToolNames : undefined,
|
|
447
505
|
endpoint: state.endpoint,
|
|
448
506
|
request_id: get(response, "_request_id") ?? get(response, "response_id")
|
|
449
|
-
?? get(response, "responseId") ?? get(response, "id")
|
|
507
|
+
?? get(response, "responseId") ?? get(response, "id")
|
|
508
|
+
?? get(get(response, "response"), "id"),
|
|
450
509
|
batch: state.request.batch === true,
|
|
451
510
|
batch_custom_id: state.request.batch_custom_id,
|
|
452
511
|
// Explicit false is the sensitive-operation opt-out; hosted ingestion
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { route, setSession, setTags, trace, type RouteOptions, type TraceOptions } from "./context.js";
|
|
2
2
|
import { track } from "./track.js";
|
|
3
3
|
import { type TransportMode, type WaitUntil } from "./transport.js";
|
|
4
|
+
import { type VercelAISDKMiddleware, type VercelAISDKMiddlewareOptions, type VercelAISDKMiddlewareSpecificationOptions, type VercelAISDKMiddlewareVersionOptions, type VercelAISDKSpecificationVersion } from "./vercel-ai.js";
|
|
4
5
|
export interface MetergraphOptions {
|
|
5
6
|
token?: string;
|
|
6
7
|
ingestUrl?: string;
|
|
@@ -51,5 +52,11 @@ export declare function wrapHandler<TArgs extends unknown[], TResult>(handler: (
|
|
|
51
52
|
*/
|
|
52
53
|
export declare function wrap<T extends Record<PropertyKey, any>>(client: T, provider?: "openai" | "anthropic" | "google"): T;
|
|
53
54
|
export declare const wrapClient: typeof wrap;
|
|
55
|
+
/**
|
|
56
|
+
* Capture Vercel AI SDK language-model calls through its middleware API.
|
|
57
|
+
* The returned middleware has no runtime dependency on `ai`.
|
|
58
|
+
*/
|
|
59
|
+
export declare function vercelAISDKMiddleware(options: VercelAISDKMiddlewareVersionOptions): VercelAISDKMiddleware<"v2">;
|
|
60
|
+
export declare function vercelAISDKMiddleware<TVersion extends VercelAISDKSpecificationVersion = "v3">(options?: VercelAISDKMiddlewareSpecificationOptions<TVersion>): VercelAISDKMiddleware<TVersion>;
|
|
54
61
|
export { route, setSession, setTags, trace, track };
|
|
55
|
-
export type { RouteOptions, TraceOptions, TransportMode };
|
|
62
|
+
export type { RouteOptions, TraceOptions, TransportMode, VercelAISDKMiddleware, VercelAISDKMiddlewareOptions, VercelAISDKSpecificationVersion, };
|
package/dist/index.js
CHANGED
|
@@ -4,6 +4,7 @@ import { contextSnapshot, route, setSession, setTags, trace, } from "./context.j
|
|
|
4
4
|
import { track } from "./track.js";
|
|
5
5
|
import { Transport } from "./transport.js";
|
|
6
6
|
import { setCaptureRuntime, wrap as wrapProvider } from "./wrap.js";
|
|
7
|
+
import { createVercelAISDKMiddleware, } from "./vercel-ai.js";
|
|
7
8
|
let initialized = false;
|
|
8
9
|
let warnedNoToken = false;
|
|
9
10
|
let transport;
|
|
@@ -138,4 +139,8 @@ export function wrap(client, provider) {
|
|
|
138
139
|
return wrapProvider(client, provider);
|
|
139
140
|
}
|
|
140
141
|
export const wrapClient = wrap;
|
|
142
|
+
export function vercelAISDKMiddleware(options = {}) {
|
|
143
|
+
init();
|
|
144
|
+
return createVercelAISDKMiddleware(options);
|
|
145
|
+
}
|
|
141
146
|
export { route, setSession, setTags, trace, track };
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
type AnyRecord = Record<PropertyKey, any>;
|
|
2
|
+
export type VercelAISDKSpecificationVersion = "v2" | "v3" | "v4";
|
|
3
|
+
export interface VercelAISDKMiddlewareVersionOptions {
|
|
4
|
+
/**
|
|
5
|
+
* Set to 5 when instrumenting AI SDK 5. Maps to middleware protocol v2.
|
|
6
|
+
* Cannot be combined with specificationVersion.
|
|
7
|
+
*/
|
|
8
|
+
aiSdkVersion: 5;
|
|
9
|
+
specificationVersion?: never;
|
|
10
|
+
}
|
|
11
|
+
export interface VercelAISDKMiddlewareSpecificationOptions<TVersion extends VercelAISDKSpecificationVersion = "v3"> {
|
|
12
|
+
aiSdkVersion?: never;
|
|
13
|
+
/**
|
|
14
|
+
* Advanced option: the raw Vercel middleware protocol version. AI SDK 6
|
|
15
|
+
* uses v3 middleware and AI SDK 7 accepts it for backwards compatibility.
|
|
16
|
+
* Prefer aiSdkVersion for AI SDK 5.
|
|
17
|
+
*/
|
|
18
|
+
specificationVersion?: TVersion;
|
|
19
|
+
}
|
|
20
|
+
export type VercelAISDKMiddlewareOptions<TVersion extends VercelAISDKSpecificationVersion = "v3"> = VercelAISDKMiddlewareVersionOptions | VercelAISDKMiddlewareSpecificationOptions<TVersion>;
|
|
21
|
+
export interface VercelAISDKMiddleware<TVersion extends VercelAISDKSpecificationVersion = "v3"> {
|
|
22
|
+
readonly specificationVersion: TVersion;
|
|
23
|
+
wrapGenerate(options: {
|
|
24
|
+
doGenerate: () => PromiseLike<any>;
|
|
25
|
+
doStream: () => PromiseLike<any>;
|
|
26
|
+
params: AnyRecord;
|
|
27
|
+
model: AnyRecord;
|
|
28
|
+
}): Promise<any>;
|
|
29
|
+
wrapStream(options: {
|
|
30
|
+
doGenerate: () => PromiseLike<any>;
|
|
31
|
+
doStream: () => PromiseLike<any>;
|
|
32
|
+
params: AnyRecord;
|
|
33
|
+
model: AnyRecord;
|
|
34
|
+
}): Promise<any>;
|
|
35
|
+
}
|
|
36
|
+
export declare function createVercelAISDKMiddleware(options: VercelAISDKMiddlewareVersionOptions): VercelAISDKMiddleware<"v2">;
|
|
37
|
+
export declare function createVercelAISDKMiddleware<TVersion extends VercelAISDKSpecificationVersion = "v3">(options?: VercelAISDKMiddlewareSpecificationOptions<TVersion>): VercelAISDKMiddleware<TVersion>;
|
|
38
|
+
export {};
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
import { chunkText } from "./capture.js";
|
|
2
|
+
import { getCaptureRuntime } from "./wrap.js";
|
|
3
|
+
const GATEWAY_PROVIDERS = new Set(["gateway", "vercel", "vercel-ai-gateway"]);
|
|
4
|
+
function canonicalProvider(provider, modelId) {
|
|
5
|
+
const raw = String(provider ?? "unknown").trim().toLowerCase();
|
|
6
|
+
const modelParts = String(modelId ?? "").trim().toLowerCase().split("/");
|
|
7
|
+
if (GATEWAY_PROVIDERS.has(raw) && modelParts.length > 1 && modelParts[0]) {
|
|
8
|
+
return canonicalProvider(modelParts[0], "");
|
|
9
|
+
}
|
|
10
|
+
if (raw === "amazon-bedrock" || raw === "aws-bedrock" || raw === "aws")
|
|
11
|
+
return "bedrock";
|
|
12
|
+
if (raw === "gemini" || raw === "google-genai" || raw.startsWith("google."))
|
|
13
|
+
return "google";
|
|
14
|
+
for (const known of ["openai", "anthropic", "google", "bedrock"]) {
|
|
15
|
+
if (raw === known || raw.startsWith(`${known}.`) || raw.startsWith(`${known}-`)) {
|
|
16
|
+
return known;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
return raw.split(/[.:]/, 1)[0] || "unknown";
|
|
20
|
+
}
|
|
21
|
+
function requestFrom(params, model) {
|
|
22
|
+
// Provider options and transport fields can contain credentials, callbacks,
|
|
23
|
+
// or non-serializable values. The standardized prompt/settings are enough
|
|
24
|
+
// for trace reconstruction and deterministic template hashing.
|
|
25
|
+
const { abortSignal: _abortSignal, headers: _headers, providerOptions: _providerOptions, prompt, ...settings } = params;
|
|
26
|
+
return {
|
|
27
|
+
...settings,
|
|
28
|
+
model: String(model?.modelId ?? settings.model ?? "unknown"),
|
|
29
|
+
messages: prompt ?? settings.messages,
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
function startCapture(capture, operation, params, model) {
|
|
33
|
+
try {
|
|
34
|
+
return capture.start(canonicalProvider(model?.provider, model?.modelId), `ai.${operation === "generate" ? "doGenerate" : "doStream"}`, requestFrom(params, model), new Error().stack);
|
|
35
|
+
}
|
|
36
|
+
catch {
|
|
37
|
+
return undefined;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
function finishCapture(capture, state, response, extra = {}) {
|
|
41
|
+
if (!state)
|
|
42
|
+
return;
|
|
43
|
+
try {
|
|
44
|
+
capture.finish(state, response, extra);
|
|
45
|
+
}
|
|
46
|
+
catch {
|
|
47
|
+
// Framework instrumentation is always fail-open.
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
function outputChunk(part) {
|
|
51
|
+
return [
|
|
52
|
+
"text-delta",
|
|
53
|
+
"reasoning-delta",
|
|
54
|
+
"tool-input-delta",
|
|
55
|
+
"tool-call",
|
|
56
|
+
"tool-result",
|
|
57
|
+
"tool-error",
|
|
58
|
+
"file",
|
|
59
|
+
"source",
|
|
60
|
+
].includes(String(part?.type));
|
|
61
|
+
}
|
|
62
|
+
function wrapStreamResult(result, capture, state, started) {
|
|
63
|
+
const source = result?.stream;
|
|
64
|
+
if (!source || typeof source.getReader !== "function") {
|
|
65
|
+
finishCapture(capture, state, result, { stream: true });
|
|
66
|
+
return result;
|
|
67
|
+
}
|
|
68
|
+
let reader;
|
|
69
|
+
try {
|
|
70
|
+
reader = source.getReader();
|
|
71
|
+
}
|
|
72
|
+
catch (error) {
|
|
73
|
+
finishCapture(capture, state, undefined, { error, stream: true });
|
|
74
|
+
return result;
|
|
75
|
+
}
|
|
76
|
+
let text = "";
|
|
77
|
+
let firstOutputAt;
|
|
78
|
+
let finishPart;
|
|
79
|
+
let streamError;
|
|
80
|
+
const content = [];
|
|
81
|
+
const responseMetadata = {};
|
|
82
|
+
const observe = (part) => {
|
|
83
|
+
if (firstOutputAt === undefined && outputChunk(part))
|
|
84
|
+
firstOutputAt = performance.now();
|
|
85
|
+
const delta = chunkText(part);
|
|
86
|
+
if (delta !== undefined)
|
|
87
|
+
text += delta;
|
|
88
|
+
if (["tool-call", "tool-result", "tool-error", "file", "source"].includes(part?.type)) {
|
|
89
|
+
content.push(part);
|
|
90
|
+
}
|
|
91
|
+
else if (part?.type === "response-metadata") {
|
|
92
|
+
Object.assign(responseMetadata, part);
|
|
93
|
+
}
|
|
94
|
+
else if (part?.type === "finish") {
|
|
95
|
+
finishPart = part;
|
|
96
|
+
}
|
|
97
|
+
else if (part?.type === "error") {
|
|
98
|
+
streamError = part.error ?? new Error("AI SDK stream error");
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
const finish = (status, error = streamError) => {
|
|
102
|
+
if (text)
|
|
103
|
+
content.unshift({ type: "text", text });
|
|
104
|
+
const response = {
|
|
105
|
+
content,
|
|
106
|
+
usage: finishPart?.usage,
|
|
107
|
+
finishReason: finishPart?.finishReason,
|
|
108
|
+
response: {
|
|
109
|
+
...responseMetadata,
|
|
110
|
+
...(result.response ?? {}),
|
|
111
|
+
},
|
|
112
|
+
};
|
|
113
|
+
finishCapture(capture, state, response, {
|
|
114
|
+
error,
|
|
115
|
+
status,
|
|
116
|
+
stream: true,
|
|
117
|
+
responseText: text,
|
|
118
|
+
ttftMs: firstOutputAt === undefined ? undefined : Math.round(firstOutputAt - started),
|
|
119
|
+
});
|
|
120
|
+
};
|
|
121
|
+
const stream = new ReadableStream({
|
|
122
|
+
async pull(controller) {
|
|
123
|
+
try {
|
|
124
|
+
const next = await reader.read();
|
|
125
|
+
if (next.done) {
|
|
126
|
+
finish();
|
|
127
|
+
controller.close();
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
observe(next.value);
|
|
131
|
+
controller.enqueue(next.value);
|
|
132
|
+
}
|
|
133
|
+
catch (error) {
|
|
134
|
+
finish("error", error);
|
|
135
|
+
controller.error(error);
|
|
136
|
+
}
|
|
137
|
+
},
|
|
138
|
+
async cancel(reason) {
|
|
139
|
+
try {
|
|
140
|
+
await reader.cancel(reason);
|
|
141
|
+
}
|
|
142
|
+
finally {
|
|
143
|
+
finish("abandoned");
|
|
144
|
+
}
|
|
145
|
+
},
|
|
146
|
+
});
|
|
147
|
+
return { ...result, stream };
|
|
148
|
+
}
|
|
149
|
+
export function createVercelAISDKMiddleware(options = {}) {
|
|
150
|
+
if (options.aiSdkVersion === 5 && options.specificationVersion !== undefined) {
|
|
151
|
+
throw new TypeError("aiSdkVersion and specificationVersion cannot be combined");
|
|
152
|
+
}
|
|
153
|
+
const specificationVersion = (options.aiSdkVersion === 5 ? "v2" : options.specificationVersion ?? "v3");
|
|
154
|
+
return {
|
|
155
|
+
specificationVersion,
|
|
156
|
+
async wrapGenerate({ doGenerate, params, model }) {
|
|
157
|
+
const capture = getCaptureRuntime();
|
|
158
|
+
if (!capture)
|
|
159
|
+
return await doGenerate();
|
|
160
|
+
const state = startCapture(capture, "generate", params, model);
|
|
161
|
+
try {
|
|
162
|
+
const result = await doGenerate();
|
|
163
|
+
finishCapture(capture, state, result);
|
|
164
|
+
return result;
|
|
165
|
+
}
|
|
166
|
+
catch (error) {
|
|
167
|
+
finishCapture(capture, state, undefined, { error });
|
|
168
|
+
throw error;
|
|
169
|
+
}
|
|
170
|
+
},
|
|
171
|
+
async wrapStream({ doStream, params, model }) {
|
|
172
|
+
const capture = getCaptureRuntime();
|
|
173
|
+
if (!capture)
|
|
174
|
+
return await doStream();
|
|
175
|
+
const started = performance.now();
|
|
176
|
+
const state = startCapture(capture, "stream", params, model);
|
|
177
|
+
try {
|
|
178
|
+
const result = await doStream();
|
|
179
|
+
return state ? wrapStreamResult(result, capture, state, started) : result;
|
|
180
|
+
}
|
|
181
|
+
catch (error) {
|
|
182
|
+
finishCapture(capture, state, undefined, { error, stream: true });
|
|
183
|
+
throw error;
|
|
184
|
+
}
|
|
185
|
+
},
|
|
186
|
+
};
|
|
187
|
+
}
|
package/dist/version.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const SDK_VERSION = "0.3.
|
|
1
|
+
export declare const SDK_VERSION = "0.3.2";
|
package/dist/version.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const SDK_VERSION = "0.3.
|
|
1
|
+
export const SDK_VERSION = "0.3.2";
|
package/dist/wrap.d.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { type CaptureRuntime } from "./capture.js";
|
|
2
2
|
type AnyRecord = Record<PropertyKey, any>;
|
|
3
3
|
export declare function setCaptureRuntime(value?: CaptureRuntime): void;
|
|
4
|
+
/** @internal Used by optional framework adapters without adding runtime dependencies. */
|
|
5
|
+
export declare function getCaptureRuntime(): CaptureRuntime | undefined;
|
|
4
6
|
export interface Seam {
|
|
5
7
|
path: string;
|
|
6
8
|
method: string;
|
package/dist/wrap.js
CHANGED
|
@@ -14,6 +14,10 @@ const reentrantOwners = new WeakSet();
|
|
|
14
14
|
export function setCaptureRuntime(value) {
|
|
15
15
|
runtime = value;
|
|
16
16
|
}
|
|
17
|
+
/** @internal Used by optional framework adapters without adding runtime dependencies. */
|
|
18
|
+
export function getCaptureRuntime() {
|
|
19
|
+
return runtime;
|
|
20
|
+
}
|
|
17
21
|
function requestFrom(args) {
|
|
18
22
|
const first = args[0];
|
|
19
23
|
return first && typeof first === "object" ? { ...first } : {};
|
|
@@ -199,12 +203,36 @@ function streamProxy(stream, state, capture) {
|
|
|
199
203
|
let ttftMs;
|
|
200
204
|
const parts = [];
|
|
201
205
|
const chunks = [];
|
|
206
|
+
const hasOutput = (chunk) => {
|
|
207
|
+
if (chunkText(chunk))
|
|
208
|
+
return true;
|
|
209
|
+
const value = chunk;
|
|
210
|
+
if (Array.isArray(value?.choices)
|
|
211
|
+
&& value.choices.some((choice) => choice?.delta?.tool_calls?.length)) {
|
|
212
|
+
return true;
|
|
213
|
+
}
|
|
214
|
+
if (typeof value?.delta === "string" && String(value?.type).includes("reasoning")) {
|
|
215
|
+
return value.delta.length > 0;
|
|
216
|
+
}
|
|
217
|
+
if (value?.delta?.thinking || value?.delta?.reasoning)
|
|
218
|
+
return true;
|
|
219
|
+
if (value?.type === "content_block_start") {
|
|
220
|
+
return value.content_block?.type === "tool_use";
|
|
221
|
+
}
|
|
222
|
+
if (value?.type === "content_block_delta") {
|
|
223
|
+
return value.delta?.type === "input_json_delta";
|
|
224
|
+
}
|
|
225
|
+
return Array.isArray(value?.candidates)
|
|
226
|
+
&& value.candidates.some((candidate) => candidate?.content?.parts?.some((part) => part?.function_call || part?.functionCall));
|
|
227
|
+
};
|
|
202
228
|
const observe = (chunk) => {
|
|
203
229
|
last = chunk;
|
|
204
230
|
chunks.push(chunk);
|
|
205
231
|
const text = chunkText(chunk);
|
|
232
|
+
if (ttftMs === undefined && hasOutput(chunk)) {
|
|
233
|
+
ttftMs = Math.round(performance.now() - state.started);
|
|
234
|
+
}
|
|
206
235
|
if (text) {
|
|
207
|
-
ttftMs ??= Math.round(performance.now() - state.started);
|
|
208
236
|
parts.push(text);
|
|
209
237
|
}
|
|
210
238
|
return chunk;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "metergraph",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.2",
|
|
4
4
|
"description": "Fire-and-forget LLM spend capture for Metergraph",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -15,7 +15,8 @@
|
|
|
15
15
|
"scripts": {
|
|
16
16
|
"build": "tsc -p tsconfig.json",
|
|
17
17
|
"prepack": "npm run build",
|
|
18
|
-
"test": "npm run build && node --test test/*.test.mjs"
|
|
18
|
+
"test": "npm run build && npm run test:types && node --test test/*.test.mjs",
|
|
19
|
+
"test:types": "tsc --noEmit --strict --skipLibCheck --target ES2022 --module NodeNext --moduleResolution NodeNext test/ai-sdk-current.types.ts"
|
|
19
20
|
},
|
|
20
21
|
"engines": {
|
|
21
22
|
"node": ">=18"
|
|
@@ -31,9 +32,10 @@
|
|
|
31
32
|
"bugs": {
|
|
32
33
|
"url": "https://github.com/PioneerSquareLabs/metergraphsdk/issues"
|
|
33
34
|
},
|
|
34
|
-
"keywords": ["llm", "observability", "openai", "anthropic", "gemini", "cost-tracking"],
|
|
35
|
+
"keywords": ["llm", "observability", "openai", "anthropic", "gemini", "vercel-ai-sdk", "cost-tracking"],
|
|
35
36
|
"dependencies": {},
|
|
36
37
|
"devDependencies": {
|
|
38
|
+
"ai": "^7.0.55",
|
|
37
39
|
"@types/node": "^24.0.0",
|
|
38
40
|
"typescript": "^5.8.0",
|
|
39
41
|
"openai": "^7",
|
|
@@ -43,11 +45,13 @@
|
|
|
43
45
|
"peerDependencies": {
|
|
44
46
|
"@anthropic-ai/sdk": ">=0.30",
|
|
45
47
|
"@google/genai": ">=1",
|
|
48
|
+
"ai": ">=5 <8",
|
|
46
49
|
"openai": ">=4"
|
|
47
50
|
},
|
|
48
51
|
"peerDependenciesMeta": {
|
|
49
52
|
"openai": {"optional": true},
|
|
50
53
|
"@anthropic-ai/sdk": {"optional": true},
|
|
51
|
-
"@google/genai": {"optional": true}
|
|
54
|
+
"@google/genai": {"optional": true},
|
|
55
|
+
"ai": {"optional": true}
|
|
52
56
|
}
|
|
53
57
|
}
|