metergraph 0.2.1 → 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 CHANGED
@@ -1,15 +1,16 @@
1
1
  # metergraph (TypeScript)
2
2
 
3
- Zero-runtime-dependency capture for OpenAI, Anthropic, and Gemini clients on
4
- Node 18+, including AWS Lambda. Worker-style `waitUntil` hooks are present but
5
- are not part of the v1 public-package qualification contract.
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
9
10
  code.
10
11
 
11
12
  ```ts
12
- import { wrap, route, modelFor, recordOutcome, setSession } from "metergraph";
13
+ import { wrap, route, trace, modelFor, recordOutcome, setSession } from "metergraph";
13
14
  import OpenAI from "openai";
14
15
 
15
16
  // new Anthropic() and new GoogleGenAI({}) wrap the same way.
@@ -17,10 +18,12 @@ const client = wrap(new OpenAI());
17
18
  setSession("ticket-123");
18
19
 
19
20
  let model = "gpt-4.1-mini";
20
- await route("ticket-classifier", async () => {
21
- model = modelFor("ticket-classifier", { default: model });
22
- await client.chat.completions.create({ model, messages: [] });
23
- }, { unit: "answer", captureText: true });
21
+ await trace("ticket-workflow", () =>
22
+ route("ticket-classifier", async () => {
23
+ model = modelFor("ticket-classifier", { default: model });
24
+ await client.chat.completions.create({ model, messages: [] });
25
+ }, { unit: "answer" })
26
+ );
24
27
 
25
28
  recordOutcome("ticket-classifier", {
26
29
  model,
@@ -31,6 +34,51 @@ recordOutcome("ticket-classifier", {
31
34
  });
32
35
  ```
33
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
+
34
82
  `modelFor(routeName, { default, sessionKey })` takes an options object.
35
83
  **Breaking change from `metergraph@0.1.0`**, which took positional
36
84
  `modelFor(routeName, fallback, sessionKey?)` arguments; `default` is
@@ -38,9 +86,23 @@ required and throws if missing so a caller error surfaces immediately
38
86
  instead of an `undefined` model reaching a provider call.
39
87
 
40
88
  Set `METERGRAPH_APP_TOKEN`; `METERGRAPH_INGEST_URL` is only needed to override
41
- the hosted HTTPS endpoint. Content is metadata-only by default; set
42
- `METERGRAPH_CAPTURE_TEXT=1` globally or `captureText: true` on an individual
43
- route to opt in. Actual wire batches never exceed 512 KiB after optional gzip.
89
+ the hosted HTTPS endpoint. SDK 0.3 captures the scrubbed provider request and a
90
+ normalized response envelope, including assistant content and tool calls, by
91
+ default. Provider credentials and transport headers are removed. Request and
92
+ response are each limited to 100 KiB of UTF-8 with an explicit truncation
93
+ marker. Set `METERGRAPH_CAPTURE_TEXT=0`, initialize with
94
+ `captureText: false`, or set `captureText: false` on an individual `route()` or
95
+ `trace()` to opt out for sensitive operations. The public open-source server
96
+ still discards content; hosted workspaces retain it under their metadata
97
+ retention period.
98
+
99
+ `trace(name, fn, { traceId, parentSpanId, captureText })` groups calls into a
100
+ logical trace using `AsyncLocalStorage`. Nested traces reuse the active trace
101
+ unless given a different trace ID. Calls outside a trace become one-span
102
+ traces. Manual IDs can join work across process boundaries; automatic W3C HTTP
103
+ propagation is not included.
104
+
105
+ Actual wire batches never exceed 512 KiB after optional gzip.
44
106
  Long-running Node processes use an unref'd
45
107
  background timer. On Workers or Vercel, call
46
108
  `bindWaitUntil(ctx)` once per request. On Lambda, use `wrapHandler(handler)`
@@ -72,14 +134,20 @@ import * as mg from "metergraph". wrap() returns the same client and
72
134
  initializes itself from the environment: METERGRAPH_APP_TOKEN is required
73
135
  (capture is silently off without it) and METERGRAPH_INGEST_URL is only for
74
136
  self-hosted servers. Add both to .env.example, and never commit a real token.
75
- Capture is metadata-only (tokens, latency, model, no prompt/completion
76
- content) and fail-open, so do not change call sites, arguments, or error
77
- handling; async and streaming work unchanged. Wrap each function that calls a
137
+ SDK 0.3 captures scrubbed provider requests and normalized responses by
138
+ default for the hosted dashboard; use METERGRAPH_CAPTURE_TEXT=0 or
139
+ captureText: false around sensitive operations. Provider credentials and
140
+ transport headers must never be captured. Capture is fail-open, so do not
141
+ change call sites, arguments, or error handling; async and streaming work
142
+ unchanged. Use mg.trace("stable-name", fn) to group multi-call workflows. Wrap
143
+ each function that calls a
78
144
  wrapped client with mg.track("stable.name", fn), because stack-based
79
145
  attribution is unreliable under bundlers. On serverless (Lambda / Workers /
80
146
  Vercel), ensure delivery before the runtime freezes: mg.wrapHandler(handler),
81
147
  or mg.bindWaitUntil(ctx) once per request, or await mg.flush() before
82
- returning; long-running servers need nothing extra. When done, list every
83
- client you wrapped and flag LLM calls made outside the official openai /
84
- @anthropic-ai/sdk / @google/genai SDKs, since those are not captured.
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.
85
153
  ```
package/dist/capture.d.ts CHANGED
@@ -13,7 +13,7 @@ interface Frame {
13
13
  f: string;
14
14
  l: number;
15
15
  }
16
- interface CallState {
16
+ export interface CallState {
17
17
  provider: string;
18
18
  endpoint: string;
19
19
  request: Record<string, unknown>;
@@ -21,6 +21,10 @@ interface CallState {
21
21
  started: number;
22
22
  ts: string;
23
23
  frames: Frame[];
24
+ traceId: string;
25
+ spanId: string;
26
+ parentSpanId?: string;
27
+ traceName: string;
24
28
  done: boolean;
25
29
  }
26
30
  export declare function chunkText(chunk: unknown): string | undefined;
@@ -35,6 +39,7 @@ export declare class CaptureRuntime {
35
39
  stream?: boolean;
36
40
  ttftMs?: number;
37
41
  responseText?: string;
42
+ responseChunks?: unknown[];
38
43
  }): void;
39
44
  }
40
45
  export {};
package/dist/capture.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { randomBytes } from "node:crypto";
1
2
  import { contextSnapshot } from "./context.js";
2
3
  import { scrub, templateHash } from "./template.js";
3
4
  import { SDK_VERSION } from "./version.js";
@@ -8,30 +9,64 @@ function first(value) {
8
9
  return Array.isArray(value) ? value[0] : undefined;
9
10
  }
10
11
  function number(value) {
12
+ if (typeof value === "boolean")
13
+ return undefined;
11
14
  const parsed = Number(value);
12
- return value == null || !Number.isFinite(parsed) ? undefined : parsed;
15
+ return value == null || !Number.isSafeInteger(parsed) || parsed < 0 ? undefined : parsed;
13
16
  }
14
- function usage(response) {
15
- const value = get(response, "usage") ?? get(response, "usage_metadata") ?? get(response, "usageMetadata");
17
+ function usageValue(value) {
16
18
  const prompt = get(value, "prompt_tokens_details") ?? get(value, "input_tokens_details")
17
19
  ?? get(value, "promptTokensDetails") ?? get(value, "inputTokensDetails");
18
20
  const completion = get(value, "completion_tokens_details") ?? get(value, "output_tokens_details")
19
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");
20
26
  const cacheCreation = get(value, "cache_creation") ?? get(value, "cacheCreation");
21
27
  return {
22
- input_tokens: number(get(value, "prompt_tokens") ?? get(value, "input_tokens") ?? get(value, "promptTokenCount")),
23
- output_tokens: number(get(value, "completion_tokens") ?? get(value, "output_tokens") ?? get(value, "candidatesTokenCount")),
24
- cache_read_tokens: number(get(value, "cache_read_input_tokens") ?? get(prompt, "cached_tokens") ?? get(value, "cachedContentTokenCount")),
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")),
25
35
  cache_write_tokens: number(get(value, "cache_creation_input_tokens")
26
36
  ?? get(prompt, "cache_write_tokens") ?? get(value, "cacheCreationInputTokens")
27
- ?? get(prompt, "cacheWriteTokens")),
37
+ ?? get(prompt, "cacheWriteTokens") ?? get(aiInput, "cacheWrite")
38
+ ?? get(aiInputDetails, "cacheWriteTokens")),
28
39
  cache_write_5m_tokens: number(get(cacheCreation, "ephemeral_5m_input_tokens")
29
40
  ?? get(cacheCreation, "ephemeral5mInputTokens")),
30
41
  cache_write_1h_tokens: number(get(cacheCreation, "ephemeral_1h_input_tokens")
31
42
  ?? get(cacheCreation, "ephemeral1hInputTokens")),
32
- reasoning_tokens: number(get(completion, "reasoning_tokens") ?? get(value, "thoughtsTokenCount")),
43
+ reasoning_tokens: number(get(completion, "reasoning_tokens")
44
+ ?? get(value, "thoughtsTokenCount") ?? get(aiOutput, "reasoning")
45
+ ?? get(aiOutputDetails, "reasoningTokens")),
33
46
  };
34
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
+ }
35
70
  function responseText(response) {
36
71
  const direct = get(response, "output_text") ?? get(response, "text");
37
72
  if (typeof direct === "string")
@@ -61,12 +96,17 @@ function responseText(response) {
61
96
  }
62
97
  export function chunkText(chunk) {
63
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;
64
101
  const text = get(delta, "content") ?? get(delta, "text") ?? get(chunk, "text");
65
102
  return typeof text === "string" ? text : undefined;
66
103
  }
67
104
  function stopReason(response) {
68
- const value = get(response, "stop_reason") ?? get(response, "status") ?? get(first(get(response, "choices")), "finish_reason");
69
- return value == null ? undefined : String(value);
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);
70
110
  }
71
111
  function toolNames(request) {
72
112
  if (!Array.isArray(request.tools))
@@ -77,6 +117,257 @@ function toolNames(request) {
77
117
  });
78
118
  return names.length ? names : undefined;
79
119
  }
120
+ function toolArgument(value) {
121
+ if (typeof value === "string") {
122
+ try {
123
+ return JSON.parse(value);
124
+ }
125
+ catch {
126
+ return value;
127
+ }
128
+ }
129
+ return scrub(value);
130
+ }
131
+ function toolEvents(request, response, streamChunks = []) {
132
+ const calls = new Map();
133
+ const pending = new Map();
134
+ const policies = new Map();
135
+ if (Array.isArray(request.tools)) {
136
+ for (const tool of request.tools) {
137
+ const fn = get(tool, "function");
138
+ const name = get(fn, "name") ?? get(tool, "name");
139
+ const policy = get(fn, "x-metergraph-idempotency")
140
+ ?? get(tool, "x-metergraph-idempotency")
141
+ ?? get(fn, "metergraph_idempotency")
142
+ ?? get(tool, "metergraph_idempotency");
143
+ if (name)
144
+ policies.set(String(name), policy === "idempotent" ? "idempotent" : "non_idempotent");
145
+ }
146
+ }
147
+ const complete = (id, result, error = false) => {
148
+ const key = String(id ?? "");
149
+ const item = calls.get(key);
150
+ if (!item) {
151
+ pending.set(key, { result, error });
152
+ return;
153
+ }
154
+ item.result = toolArgument(result);
155
+ item.status = error ? "error" : "completed";
156
+ };
157
+ const add = (id, name, args) => {
158
+ if (!name)
159
+ return;
160
+ const key = String(id ?? `${String(name)}:${calls.size}`);
161
+ const item = {
162
+ call_id: key,
163
+ name: String(name),
164
+ arguments: toolArgument(args),
165
+ status: "requested",
166
+ idempotency: policies.get(String(name)) ?? "non_idempotent",
167
+ };
168
+ calls.set(key, item);
169
+ const waiting = pending.get(key);
170
+ if (waiting) {
171
+ pending.delete(key);
172
+ complete(key, waiting.result, waiting.error);
173
+ }
174
+ };
175
+ const blocks = (value) => {
176
+ if (!Array.isArray(value))
177
+ return;
178
+ for (const block of value) {
179
+ const kind = get(block, "type");
180
+ if (kind === "tool_use") {
181
+ add(get(block, "id"), get(block, "name"), get(block, "input"));
182
+ }
183
+ else if (kind === "tool_result") {
184
+ complete(get(block, "tool_use_id"), get(block, "content"), Boolean(get(block, "is_error")));
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
+ }
192
+ }
193
+ };
194
+ const geminiParts = (value) => {
195
+ const parts = get(value, "parts");
196
+ if (!Array.isArray(parts))
197
+ return;
198
+ for (const part of parts) {
199
+ const call = get(part, "function_call") ?? get(part, "functionCall");
200
+ if (call)
201
+ add(get(call, "id"), get(call, "name"), get(call, "args") ?? get(call, "arguments"));
202
+ const result = get(part, "function_response") ?? get(part, "functionResponse");
203
+ if (result)
204
+ complete(get(result, "id") ?? get(result, "name"), get(result, "response"));
205
+ }
206
+ };
207
+ const history = Array.isArray(request.messages)
208
+ ? request.messages
209
+ : Array.isArray(request.input)
210
+ ? request.input
211
+ : Array.isArray(request.contents)
212
+ ? request.contents
213
+ : Array.isArray(request.prompt)
214
+ ? request.prompt
215
+ : [];
216
+ for (const message of history) {
217
+ const messageCalls = get(message, "tool_calls");
218
+ if (Array.isArray(messageCalls)) {
219
+ for (const tool of messageCalls) {
220
+ const fn = get(tool, "function");
221
+ add(get(tool, "id") ?? get(tool, "call_id"), get(fn, "name") ?? get(tool, "name"), get(fn, "arguments") ?? get(tool, "arguments"));
222
+ }
223
+ }
224
+ if (get(message, "role") === "tool") {
225
+ complete(get(message, "tool_call_id") ?? get(message, "call_id"), get(message, "content"), Boolean(get(message, "is_error")));
226
+ }
227
+ const kind = get(message, "type");
228
+ if (kind === "function_call" || kind === "tool_call") {
229
+ add(get(message, "call_id") ?? get(message, "id"), get(message, "name"), get(message, "arguments"));
230
+ }
231
+ else if (kind === "function_call_output" || kind === "tool_result") {
232
+ complete(get(message, "call_id") ?? get(message, "tool_use_id"), get(message, "output") ?? get(message, "content"), Boolean(get(message, "is_error")));
233
+ }
234
+ blocks(get(message, "content"));
235
+ geminiParts(message);
236
+ }
237
+ const message = get(first(get(response, "choices")), "message");
238
+ const responseCalls = get(message, "tool_calls");
239
+ if (Array.isArray(responseCalls)) {
240
+ for (const tool of responseCalls) {
241
+ const fn = get(tool, "function");
242
+ add(get(tool, "id") ?? get(tool, "call_id"), get(fn, "name") ?? get(tool, "name"), get(fn, "arguments") ?? get(tool, "arguments"));
243
+ }
244
+ }
245
+ blocks(get(response, "content"));
246
+ const outputs = get(response, "output");
247
+ if (Array.isArray(outputs)) {
248
+ for (const output of outputs) {
249
+ const kind = get(output, "type");
250
+ if (kind === "function_call" || kind === "tool_call") {
251
+ add(get(output, "call_id") ?? get(output, "id"), get(output, "name"), get(output, "arguments"));
252
+ }
253
+ }
254
+ }
255
+ const candidates = get(response, "candidates");
256
+ if (Array.isArray(candidates)) {
257
+ for (const candidate of candidates)
258
+ geminiParts(get(candidate, "content"));
259
+ }
260
+ const openAIDeltas = new Map();
261
+ const anthropicDeltas = new Map();
262
+ for (const chunk of streamChunks) {
263
+ const choices = get(chunk, "choices");
264
+ if (Array.isArray(choices)) {
265
+ for (const choice of choices) {
266
+ const tools = get(get(choice, "delta"), "tool_calls");
267
+ if (!Array.isArray(tools))
268
+ continue;
269
+ tools.forEach((tool, position) => {
270
+ const key = String(get(tool, "index") ?? position);
271
+ const item = openAIDeltas.get(key) ?? { id: key, name: "", arguments: "" };
272
+ item.id = String(get(tool, "id") ?? item.id);
273
+ const fn = get(tool, "function");
274
+ if (get(fn, "name"))
275
+ item.name += String(get(fn, "name"));
276
+ if (get(fn, "arguments"))
277
+ item.arguments += String(get(fn, "arguments"));
278
+ openAIDeltas.set(key, item);
279
+ });
280
+ }
281
+ }
282
+ const kind = get(chunk, "type");
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") {
290
+ const block = get(chunk, "content_block");
291
+ if (get(block, "type") === "tool_use") {
292
+ const key = String(get(chunk, "index") ?? anthropicDeltas.size);
293
+ const input = get(block, "input");
294
+ anthropicDeltas.set(key, {
295
+ id: String(get(block, "id") ?? key),
296
+ name: String(get(block, "name") ?? ""),
297
+ arguments: input && typeof input === "object"
298
+ && Object.keys(input).length === 0
299
+ ? ""
300
+ : JSON.stringify(scrub(input ?? {})),
301
+ });
302
+ }
303
+ }
304
+ else if (kind === "content_block_delta") {
305
+ const delta = get(chunk, "delta");
306
+ if (get(delta, "type") === "input_json_delta") {
307
+ const key = String(get(chunk, "index") ?? "0");
308
+ const item = anthropicDeltas.get(key) ?? { id: key, name: "", arguments: "" };
309
+ item.arguments += String(get(delta, "partial_json") ?? "");
310
+ anthropicDeltas.set(key, item);
311
+ }
312
+ }
313
+ const chunkCandidates = get(chunk, "candidates");
314
+ if (Array.isArray(chunkCandidates)) {
315
+ for (const candidate of chunkCandidates)
316
+ geminiParts(get(candidate, "content"));
317
+ }
318
+ }
319
+ for (const item of [...openAIDeltas.values(), ...anthropicDeltas.values()]) {
320
+ if (item.name)
321
+ add(item.id, item.name, item.arguments);
322
+ }
323
+ return calls.size ? [...calls.values()] : undefined;
324
+ }
325
+ function responseContent(response, aggregate) {
326
+ if (aggregate !== undefined)
327
+ return aggregate;
328
+ const direct = get(response, "output_text") ?? get(response, "text");
329
+ if (direct !== undefined)
330
+ return scrub(direct);
331
+ const message = get(first(get(response, "choices")), "message");
332
+ const content = get(message, "content");
333
+ if (content !== undefined)
334
+ return scrub(content);
335
+ const parsed = get(message, "parsed");
336
+ if (parsed !== undefined)
337
+ return scrub(parsed);
338
+ const normalizedText = responseText(response);
339
+ if (normalizedText !== undefined)
340
+ return normalizedText;
341
+ const blocks = get(response, "content");
342
+ if (blocks !== undefined)
343
+ return scrub(blocks);
344
+ const output = get(response, "output");
345
+ if (output !== undefined)
346
+ return scrub(output);
347
+ const candidates = get(response, "candidates");
348
+ return candidates === undefined ? undefined : scrub(candidates);
349
+ }
350
+ function responseEnvelope(response, aggregate, tools, error, status) {
351
+ const responseMetadata = get(response, "response");
352
+ const envelope = {
353
+ role: "assistant",
354
+ content: responseContent(response, aggregate),
355
+ tool_calls: tools ?? [],
356
+ finish_reason: stopReason(response),
357
+ request_id: get(response, "_request_id") ?? get(response, "response_id")
358
+ ?? get(response, "responseId") ?? get(response, "id") ?? get(responseMetadata, "id"),
359
+ model: get(response, "model") ?? get(response, "model_version")
360
+ ?? get(response, "modelVersion") ?? get(responseMetadata, "modelId"),
361
+ status,
362
+ };
363
+ if (error !== undefined) {
364
+ envelope.error = {
365
+ type: error instanceof Error ? error.name : "Error",
366
+ message: error instanceof Error ? error.message : String(error),
367
+ };
368
+ }
369
+ return Object.fromEntries(Object.entries(envelope).filter(([, value]) => value !== undefined));
370
+ }
80
371
  const SDK_DIR = (() => {
81
372
  try {
82
373
  return new URL(".", import.meta.url).pathname;
@@ -124,6 +415,11 @@ export class CaptureRuntime {
124
415
  started: performance.now(),
125
416
  ts: new Date().toISOString(),
126
417
  frames: frames(stack, this.options.appRoot, this.options.skipFrames),
418
+ traceId: context.traceId ?? randomBytes(16).toString("hex"),
419
+ spanId: randomBytes(8).toString("hex"),
420
+ parentSpanId: context.parentSpanId,
421
+ traceName: context.traceName ?? context.route ?? context.funcName
422
+ ?? endpoint,
127
423
  done: false,
128
424
  };
129
425
  }
@@ -144,12 +440,49 @@ export class CaptureRuntime {
144
440
  const bytes = new TextEncoder().encode(value);
145
441
  if (bytes.byteLength <= this.options.textMaxBytes)
146
442
  return { value, truncated: false };
147
- const clipped = new TextDecoder().decode(bytes.slice(0, this.options.textMaxBytes - 24));
148
- return { value: `${clipped}\n<metergraph:truncated>`, truncated: true };
443
+ const marker = "\n<metergraph:truncated>";
444
+ const markerBytes = new TextEncoder().encode(marker).byteLength;
445
+ let clipped = new TextDecoder().decode(bytes.slice(0, Math.max(0, this.options.textMaxBytes - markerBytes)));
446
+ while (clipped
447
+ && new TextEncoder().encode(`${clipped}${marker}`).byteLength
448
+ > this.options.textMaxBytes) {
449
+ clipped = clipped.slice(0, -1);
450
+ }
451
+ return { value: `${clipped}${marker}`, truncated: true };
149
452
  };
150
453
  const request = text(JSON.stringify(scrub(state.request)), "request");
151
- const output = text(extra.responseText ?? responseText(response), "response");
454
+ const fullTools = toolEvents(scrub(state.request), response, extra.responseChunks);
455
+ const status = extra.status ?? (extra.error ? "error" : stopReason(response) ?? "success");
456
+ const output = text(JSON.stringify(responseEnvelope(response, extra.responseText, fullTools, extra.error, status)), "response");
457
+ let toolTruncated = false;
458
+ let persistedTools;
459
+ if (fullTools && captureText) {
460
+ const encodedTools = text(JSON.stringify(fullTools), "response");
461
+ toolTruncated = encodedTools.truncated;
462
+ try {
463
+ persistedTools = encodedTools.value
464
+ ? JSON.parse(encodedTools.value)
465
+ : undefined;
466
+ }
467
+ catch {
468
+ // A truncated JSON payload is intentionally omitted. The bounded,
469
+ // redacted response envelope still describes the call and the row's
470
+ // text_truncated flag tells ingestion why this field is unavailable.
471
+ persistedTools = undefined;
472
+ }
473
+ }
474
+ else if (fullTools) {
475
+ persistedTools = fullTools.map((item) => ({
476
+ call_id: item.call_id,
477
+ name: item.name,
478
+ status: item.status,
479
+ idempotency: item.idempotency,
480
+ }));
481
+ }
152
482
  const firstFrame = state.frames[0];
483
+ const observedToolNames = fullTools
484
+ ? [...new Set(fullTools.map((item) => item.name))]
485
+ : undefined;
153
486
  this.transport.enqueue({
154
487
  ts: state.ts,
155
488
  route: state.context.route,
@@ -157,22 +490,30 @@ export class CaptureRuntime {
157
490
  model: state.request.model,
158
491
  ...usage(response),
159
492
  latency_ms: Math.round(performance.now() - state.started),
160
- status: extra.status ?? (extra.error ? "error" : stopReason(response) ?? "success"),
493
+ status,
161
494
  session_id: state.context.sessionId,
495
+ conversation_id: state.context.sessionId,
496
+ trace_id: state.traceId,
497
+ span_id: state.spanId,
498
+ parent_span_id: state.parentSpanId,
499
+ trace_name: state.traceName,
162
500
  template_hash: templateHash(state.request),
163
501
  unit_name: state.context.unitName,
164
502
  unit_count: state.context.unitCount,
165
- tool_calls: toolNames(state.request),
503
+ tool_calls: fullTools ? persistedTools : toolNames(state.request),
504
+ tool_names: observedToolNames?.length ? observedToolNames : undefined,
166
505
  endpoint: state.endpoint,
167
- request_id: get(response, "_request_id") ?? get(response, "response_id") ?? get(response, "id"),
506
+ request_id: get(response, "_request_id") ?? get(response, "response_id")
507
+ ?? get(response, "responseId") ?? get(response, "id")
508
+ ?? get(get(response, "response"), "id"),
168
509
  batch: state.request.batch === true,
169
510
  batch_custom_id: state.request.batch_custom_id,
170
- // Explicit positive consent stamp; the worker never infers opt-in from
171
- // content fields that happen to be present.
511
+ // Explicit false is the sensitive-operation opt-out; hosted ingestion
512
+ // otherwise preserves content that is present.
172
513
  content_opted_in: captureText,
173
514
  request_json: request.value,
174
515
  response_text: output.value,
175
- text_truncated: request.truncated || output.truncated,
516
+ text_truncated: request.truncated || output.truncated || toolTruncated,
176
517
  stream: extra.stream ?? false,
177
518
  ttft_ms: extra.ttftMs,
178
519
  func: state.context.funcName
package/dist/context.d.ts CHANGED
@@ -6,6 +6,9 @@ export interface CaptureContext {
6
6
  unitCount?: number;
7
7
  captureText?: boolean;
8
8
  funcName?: string;
9
+ traceId?: string;
10
+ traceName?: string;
11
+ parentSpanId?: string;
9
12
  }
10
13
  export interface RouteOptions {
11
14
  unit?: string;
@@ -13,8 +16,14 @@ export interface RouteOptions {
13
16
  tags?: Record<string, unknown>;
14
17
  captureText?: boolean;
15
18
  }
19
+ export interface TraceOptions {
20
+ traceId?: string;
21
+ parentSpanId?: string;
22
+ captureText?: boolean;
23
+ }
16
24
  export declare function contextSnapshot(): CaptureContext;
17
25
  export declare function runWithContext<T>(context: CaptureContext, fn: () => T): T;
18
26
  export declare function route<T>(name: string, fn: () => T | Promise<T>, options?: RouteOptions): Promise<T>;
27
+ export declare function trace<T>(name: string, fn: () => T | Promise<T>, options?: TraceOptions): T | Promise<T>;
19
28
  export declare function setSession(sessionId?: string): void;
20
29
  export declare function setTags(tags: Record<string, unknown>): void;
package/dist/context.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { AsyncLocalStorage } from "node:async_hooks";
2
+ import { randomBytes } from "node:crypto";
2
3
  const storage = new AsyncLocalStorage();
3
4
  let ambient = { tags: {} };
4
5
  export function contextSnapshot() {
@@ -23,6 +24,20 @@ export async function route(name, fn, options = {}) {
23
24
  };
24
25
  return storage.run(child, fn);
25
26
  }
27
+ export function trace(name, fn, options = {}) {
28
+ const parent = contextSnapshot();
29
+ const requested = options.traceId?.trim();
30
+ const reuse = parent.traceId !== undefined
31
+ && (requested === undefined || requested === parent.traceId);
32
+ const child = {
33
+ ...parent,
34
+ traceId: reuse ? parent.traceId : requested || randomBytes(16).toString("hex"),
35
+ traceName: reuse ? parent.traceName : String(name),
36
+ parentSpanId: options.parentSpanId ?? (reuse ? parent.parentSpanId : undefined),
37
+ captureText: options.captureText ?? parent.captureText,
38
+ };
39
+ return storage.run(child, fn);
40
+ }
26
41
  export function setSession(sessionId) {
27
42
  const store = storage.getStore();
28
43
  if (store)
package/dist/index.d.ts CHANGED
@@ -1,6 +1,7 @@
1
- import { route, setSession, setTags, type RouteOptions } from "./context.js";
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;
54
- export { route, setSession, setTags, track };
55
- export type { RouteOptions, TransportMode };
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>;
61
+ export { route, setSession, setTags, trace, track };
62
+ export type { RouteOptions, TraceOptions, TransportMode, VercelAISDKMiddleware, VercelAISDKMiddlewareOptions, VercelAISDKSpecificationVersion, };
package/dist/index.js CHANGED
@@ -1,9 +1,10 @@
1
1
  import { CaptureRuntime } from "./capture.js";
2
2
  import { ConfigPoller } from "./config.js";
3
- import { contextSnapshot, route, setSession, setTags, } from "./context.js";
3
+ import { contextSnapshot, route, setSession, setTags, trace, } from "./context.js";
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;
@@ -44,12 +45,12 @@ export function init(options = {}) {
44
45
  mode: options.transport ?? "auto",
45
46
  });
46
47
  setCaptureRuntime(new CaptureRuntime(transport, {
47
- captureText: options.captureText ?? envBool("METERGRAPH_CAPTURE_TEXT", false),
48
+ captureText: options.captureText ?? envBool("METERGRAPH_CAPTURE_TEXT", true),
48
49
  redact: options.redact,
49
50
  appRoot: options.appRoot ?? (typeof process === "undefined" ? "" : process.cwd()),
50
51
  skipFrames: options.skipFrames ?? [],
51
52
  environment: options.environment ?? env("METERGRAPH_ENV"),
52
- textMaxBytes: Number(env("METERGRAPH_TEXT_MAX_BYTES") ?? 100_000),
53
+ textMaxBytes: Math.min(100 * 1024, Math.max(1, Number(env("METERGRAPH_TEXT_MAX_BYTES") ?? 100 * 1024))),
53
54
  }));
54
55
  config = new ConfigPoller(token, ingestUrl, options.configPollMs, options.configHardTtlMs);
55
56
  }
@@ -138,4 +139,8 @@ export function wrap(client, provider) {
138
139
  return wrapProvider(client, provider);
139
140
  }
140
141
  export const wrapClient = wrap;
141
- export { route, setSession, setTags, track };
142
+ export function vercelAISDKMiddleware(options = {}) {
143
+ init();
144
+ return createVercelAISDKMiddleware(options);
145
+ }
146
+ export { route, setSession, setTags, trace, track };
package/dist/template.js CHANGED
@@ -1,10 +1,26 @@
1
- const sensitive = new Set(["api_key", "apikey", "authorization", "headers", "token", "secret"]);
1
+ const sensitive = new Set([
2
+ "api-key",
3
+ "api_key",
4
+ "apikey",
5
+ "authorization",
6
+ "client_secret",
7
+ "cookie",
8
+ "headers",
9
+ "id_token",
10
+ "password",
11
+ "proxy-authorization",
12
+ "refresh_token",
13
+ "secret",
14
+ "set-cookie",
15
+ "token",
16
+ "x-api-key",
17
+ ]);
2
18
  export function scrub(value) {
3
19
  if (Array.isArray(value))
4
20
  return value.map(scrub);
5
21
  if (value && typeof value === "object") {
6
22
  return Object.fromEntries(Object.entries(value)
7
- .filter(([key]) => !sensitive.has(key.toLowerCase()))
23
+ .filter(([key]) => !sensitive.has(key.trim().toLowerCase()))
8
24
  .map(([key, item]) => [key, scrub(item)]));
9
25
  }
10
26
  return value;
@@ -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.2.1";
1
+ export declare const SDK_VERSION = "0.3.2";
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const SDK_VERSION = "0.2.1";
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 } : {};
@@ -198,11 +202,37 @@ function streamProxy(stream, state, capture) {
198
202
  let last;
199
203
  let ttftMs;
200
204
  const parts = [];
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
+ };
201
228
  const observe = (chunk) => {
202
229
  last = chunk;
230
+ chunks.push(chunk);
203
231
  const text = chunkText(chunk);
232
+ if (ttftMs === undefined && hasOutput(chunk)) {
233
+ ttftMs = Math.round(performance.now() - state.started);
234
+ }
204
235
  if (text) {
205
- ttftMs ??= Math.round(performance.now() - state.started);
206
236
  parts.push(text);
207
237
  }
208
238
  return chunk;
@@ -214,6 +244,7 @@ function streamProxy(stream, state, capture) {
214
244
  stream: true,
215
245
  ttftMs,
216
246
  responseText: parts.join("") || undefined,
247
+ responseChunks: chunks,
217
248
  });
218
249
  };
219
250
  if (typeof stream.on === "function") {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "metergraph",
3
- "version": "0.2.1",
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",
@@ -14,7 +14,9 @@
14
14
  "files": ["dist", "README.md"],
15
15
  "scripts": {
16
16
  "build": "tsc -p tsconfig.json",
17
- "test": "npm run build && node --test test/*.test.mjs"
17
+ "prepack": "npm run build",
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"
18
20
  },
19
21
  "engines": {
20
22
  "node": ">=18"
@@ -30,9 +32,10 @@
30
32
  "bugs": {
31
33
  "url": "https://github.com/PioneerSquareLabs/metergraphsdk/issues"
32
34
  },
33
- "keywords": ["llm", "observability", "openai", "anthropic", "gemini", "cost-tracking"],
35
+ "keywords": ["llm", "observability", "openai", "anthropic", "gemini", "vercel-ai-sdk", "cost-tracking"],
34
36
  "dependencies": {},
35
37
  "devDependencies": {
38
+ "ai": "^7.0.55",
36
39
  "@types/node": "^24.0.0",
37
40
  "typescript": "^5.8.0",
38
41
  "openai": "^7",
@@ -42,11 +45,13 @@
42
45
  "peerDependencies": {
43
46
  "@anthropic-ai/sdk": ">=0.30",
44
47
  "@google/genai": ">=1",
48
+ "ai": ">=5 <8",
45
49
  "openai": ">=4"
46
50
  },
47
51
  "peerDependenciesMeta": {
48
52
  "openai": {"optional": true},
49
53
  "@anthropic-ai/sdk": {"optional": true},
50
- "@google/genai": {"optional": true}
54
+ "@google/genai": {"optional": true},
55
+ "ai": {"optional": true}
51
56
  }
52
57
  }