metergraph 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,8 +1,9 @@
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
@@ -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
@@ -40,7 +86,12 @@ required and throws if missing so a caller error surfaces immediately
40
86
  instead of an `undefined` model reaching a provider call.
41
87
 
42
88
  Set `METERGRAPH_APP_TOKEN`; `METERGRAPH_INGEST_URL` is only needed to override
43
- the hosted HTTPS endpoint. SDK 0.3 captures the scrubbed provider request and a
89
+ the hosted HTTPS endpoint. SDK 0.4 automatically reads the GitHub `origin` on
90
+ first initialization and creates `.metergraph/config.json` at the repository
91
+ root if absent. Commit this non-secret file so production can use
92
+ repository-aware ingest without Git metadata. Existing config is authoritative
93
+ and never overwritten; when discovery or creation is unavailable, the SDK
94
+ continues with protocol v1. SDK 0.4 captures the scrubbed provider request and a
44
95
  normalized response envelope, including assistant content and tool calls, by
45
96
  default. Provider credentials and transport headers are removed. Request and
46
97
  response are each limited to 100 KiB of UTF-8 with an explicit truncation
@@ -88,7 +139,7 @@ import * as mg from "metergraph". wrap() returns the same client and
88
139
  initializes itself from the environment: METERGRAPH_APP_TOKEN is required
89
140
  (capture is silently off without it) and METERGRAPH_INGEST_URL is only for
90
141
  self-hosted servers. Add both to .env.example, and never commit a real token.
91
- SDK 0.3 captures scrubbed provider requests and normalized responses by
142
+ SDK 0.4 captures scrubbed provider requests and normalized responses by
92
143
  default for the hosted dashboard; use METERGRAPH_CAPTURE_TEXT=0 or
93
144
  captureText: false around sensitive operations. Provider credentials and
94
145
  transport headers must never be captured. Capture is fail-open, so do not
@@ -99,7 +150,9 @@ wrapped client with mg.track("stable.name", fn), because stack-based
99
150
  attribution is unreliable under bundlers. On serverless (Lambda / Workers /
100
151
  Vercel), ensure delivery before the runtime freezes: mg.wrapHandler(handler),
101
152
  or mg.bindWaitUntil(ctx) once per request, or await mg.flush() before
102
- returning; long-running servers need nothing extra. When done, list every
103
- client you wrapped and flag LLM calls made outside the official openai /
104
- @anthropic-ai/sdk / @google/genai SDKs, since those are not captured.
153
+ returning; long-running servers need nothing extra. For Vercel AI SDK calls,
154
+ wrap each language model with wrapLanguageModel({ model, middleware:
155
+ mg.vercelAISDKMiddleware() }); do not also wrap its internal provider transport.
156
+ When done, list every client and AI SDK model you instrumented, and flag LLM
157
+ calls made outside these supported paths.
105
158
  ```
package/dist/capture.d.ts CHANGED
@@ -4,6 +4,7 @@ export interface RuntimeOptions {
4
4
  captureText: boolean;
5
5
  redact?: (text: string, kind: "request" | "response") => string;
6
6
  appRoot: string;
7
+ repoRoot?: string;
7
8
  skipFrames: string[];
8
9
  environment?: string;
9
10
  textMaxBytes: number;
@@ -12,8 +13,9 @@ interface Frame {
12
13
  m: string;
13
14
  f: string;
14
15
  l: number;
16
+ p?: string;
15
17
  }
16
- interface CallState {
18
+ export interface CallState {
17
19
  provider: string;
18
20
  endpoint: string;
19
21
  request: Record<string, unknown>;
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.isFinite(parsed) ? undefined : parsed;
15
+ return value == null || !Number.isSafeInteger(parsed) || parsed < 0 ? undefined : parsed;
14
16
  }
15
- function usage(response) {
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") ?? get(value, "promptTokenCount")),
24
- output_tokens: number(get(value, "completion_tokens") ?? get(value, "output_tokens") ?? get(value, "candidatesTokenCount")),
25
- 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")),
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") ?? get(value, "thoughtsTokenCount")),
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") ?? get(first(get(response, "choices")), "finish_reason");
70
- 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);
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 === "content_block_start") {
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) {
@@ -322,7 +376,7 @@ const SDK_DIR = (() => {
322
376
  return "metergraph-sdk-dir-unavailable";
323
377
  }
324
378
  })();
325
- function frames(stack, appRoot, skip) {
379
+ function frames(stack, appRoot, skip, repoRoot) {
326
380
  if (!stack)
327
381
  return [];
328
382
  const result = [];
@@ -335,11 +389,17 @@ function frames(stack, appRoot, skip) {
335
389
  continue;
336
390
  if (["node_modules", "node:internal", SDK_DIR, ...skip].some((value) => file.includes(value)))
337
391
  continue;
338
- result.push({
392
+ const entry = {
339
393
  m: file.slice(file.indexOf(appRoot) + appRoot.length).replace(/^\//, ""),
340
394
  f: fn,
341
395
  l: Number(lineNo),
342
- });
396
+ };
397
+ const normalizedFile = file.replaceAll("\\", "/").replace(/^file:\/\//, "");
398
+ const normalizedRepoRoot = repoRoot?.replaceAll("\\", "/").replace(/\/$/, "");
399
+ if (normalizedRepoRoot && normalizedFile.startsWith(`${normalizedRepoRoot}/`)) {
400
+ entry.p = normalizedFile.slice(normalizedRepoRoot.length + 1);
401
+ }
402
+ result.push(entry);
343
403
  if (result.length === 5)
344
404
  break;
345
405
  }
@@ -360,7 +420,7 @@ export class CaptureRuntime {
360
420
  context,
361
421
  started: performance.now(),
362
422
  ts: new Date().toISOString(),
363
- frames: frames(stack, this.options.appRoot, this.options.skipFrames),
423
+ frames: frames(stack, this.options.appRoot, this.options.skipFrames, this.options.repoRoot),
364
424
  traceId: context.traceId ?? randomBytes(16).toString("hex"),
365
425
  spanId: randomBytes(8).toString("hex"),
366
426
  parentSpanId: context.parentSpanId,
@@ -426,6 +486,9 @@ export class CaptureRuntime {
426
486
  }));
427
487
  }
428
488
  const firstFrame = state.frames[0];
489
+ const observedToolNames = fullTools
490
+ ? [...new Set(fullTools.map((item) => item.name))]
491
+ : undefined;
429
492
  this.transport.enqueue({
430
493
  ts: state.ts,
431
494
  route: state.context.route,
@@ -444,9 +507,11 @@ export class CaptureRuntime {
444
507
  unit_name: state.context.unitName,
445
508
  unit_count: state.context.unitCount,
446
509
  tool_calls: fullTools ? persistedTools : toolNames(state.request),
510
+ tool_names: observedToolNames?.length ? observedToolNames : undefined,
447
511
  endpoint: state.endpoint,
448
512
  request_id: get(response, "_request_id") ?? get(response, "response_id")
449
- ?? get(response, "responseId") ?? get(response, "id"),
513
+ ?? get(response, "responseId") ?? get(response, "id")
514
+ ?? get(get(response, "response"), "id"),
450
515
  batch: state.request.batch === true,
451
516
  batch_custom_id: state.request.batch_custom_id,
452
517
  // 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
@@ -1,13 +1,18 @@
1
1
  import { CaptureRuntime } from "./capture.js";
2
2
  import { ConfigPoller } from "./config.js";
3
3
  import { contextSnapshot, route, setSession, setTags, trace, } from "./context.js";
4
+ import { ensureRepoConfig } from "./repo-config.js";
5
+ import { SessionManager } from "./session.js";
4
6
  import { track } from "./track.js";
5
7
  import { Transport } from "./transport.js";
8
+ import { SDK_VERSION } from "./version.js";
6
9
  import { setCaptureRuntime, wrap as wrapProvider } from "./wrap.js";
10
+ import { createVercelAISDKMiddleware, } from "./vercel-ai.js";
7
11
  let initialized = false;
8
12
  let warnedNoToken = false;
9
13
  let transport;
10
14
  let config;
15
+ let sessionManager;
11
16
  export const DEFAULT_INGEST_URL = "https://d2xus7mp8zdv6t.cloudfront.net";
12
17
  function env(name) {
13
18
  return typeof process === "undefined" ? undefined : process.env[name];
@@ -37,16 +42,23 @@ export function init(options = {}) {
37
42
  }
38
43
  initialized = true;
39
44
  try {
45
+ const appRoot = options.appRoot ?? (typeof process === "undefined" ? "" : process.cwd());
46
+ const repoConfig = ensureRepoConfig(appRoot);
47
+ sessionManager = repoConfig
48
+ ? new SessionManager(token, ingestUrl, repoConfig.repository, SDK_VERSION)
49
+ : undefined;
40
50
  transport = new Transport(token, ingestUrl, {
41
51
  queueSize: options.queueSize ?? Number(env("METERGRAPH_QUEUE_SIZE") ?? 2_000),
42
52
  batchSize: options.batchSize ?? Number(env("METERGRAPH_BATCH_SIZE") ?? 100),
43
53
  flushMs: options.flushMs ?? Number(env("METERGRAPH_FLUSH_MS") ?? 5_000),
44
54
  mode: options.transport ?? "auto",
55
+ session: sessionManager,
45
56
  });
46
57
  setCaptureRuntime(new CaptureRuntime(transport, {
47
58
  captureText: options.captureText ?? envBool("METERGRAPH_CAPTURE_TEXT", true),
48
59
  redact: options.redact,
49
- appRoot: options.appRoot ?? (typeof process === "undefined" ? "" : process.cwd()),
60
+ appRoot,
61
+ repoRoot: repoConfig?.repoRoot,
50
62
  skipFrames: options.skipFrames ?? [],
51
63
  environment: options.environment ?? env("METERGRAPH_ENV"),
52
64
  textMaxBytes: Math.min(100 * 1024, Math.max(1, Number(env("METERGRAPH_TEXT_MAX_BYTES") ?? 100 * 1024))),
@@ -56,6 +68,7 @@ export function init(options = {}) {
56
68
  catch {
57
69
  transport = undefined;
58
70
  config = undefined;
71
+ sessionManager = undefined;
59
72
  setCaptureRuntime();
60
73
  console.warn("Metergraph initialization failed; application is running uninstrumented");
61
74
  }
@@ -115,6 +128,8 @@ export async function shutdown() {
115
128
  config = undefined;
116
129
  await transport?.shutdown();
117
130
  transport = undefined;
131
+ sessionManager?.stop();
132
+ sessionManager = undefined;
118
133
  setCaptureRuntime();
119
134
  }
120
135
  export function wrapHandler(handler) {
@@ -138,4 +153,8 @@ export function wrap(client, provider) {
138
153
  return wrapProvider(client, provider);
139
154
  }
140
155
  export const wrapClient = wrap;
156
+ export function vercelAISDKMiddleware(options = {}) {
157
+ init();
158
+ return createVercelAISDKMiddleware(options);
159
+ }
141
160
  export { route, setSession, setTags, trace, track };
@@ -0,0 +1,22 @@
1
+ export declare const CONFIG_DIRNAME = ".metergraph";
2
+ export declare const CONFIG_FILENAME = "config.json";
3
+ export declare const SUPPORTED_CONFIG_VERSION = 2;
4
+ export interface RepoConfig {
5
+ repository: string;
6
+ repoRoot: string;
7
+ }
8
+ /** Return 'owner/repo' from a GitHub SSH or HTTPS remote URL, or undefined
9
+ * if the URL isn't a recognized GitHub origin. */
10
+ export declare function normalizeGithubRemote(url: string): string | undefined;
11
+ /** Walk upward from appRoot looking for .metergraph/config.json. Returns
12
+ * undefined -- silently, the normal v1 state -- when nothing is found. */
13
+ export declare function discoverRepoConfig(appRoot: string): RepoConfig | undefined;
14
+ /** Create .metergraph/config.json if -- and only if -- it doesn't already
15
+ * exist. Uses O_CREAT|O_EXCL ("wx") for an atomic create-only-if-absent; a
16
+ * concurrent writer (or a file that appeared between discovery and this
17
+ * call) always wins over us, and we simply read back whatever is there. */
18
+ export declare function writeConfigAtomically(repoRoot: string, repository: string): RepoConfig | undefined;
19
+ /** Discover an existing repo config, or detect+write one once at the git top
20
+ * level. Fail-open: any detection or write failure returns undefined
21
+ * (legacy protocol v1), never throws. */
22
+ export declare function ensureRepoConfig(appRoot: string): RepoConfig | undefined;
@@ -0,0 +1,214 @@
1
+ /**
2
+ * Repository-aware ingest protocol v2: discover, detect, and write
3
+ * .metergraph/config.json.
4
+ *
5
+ * Discovery is purely file-based (never shells out to git), so a committed
6
+ * config is honored in production without needing a .git directory at all.
7
+ * Detection + write only ever runs when discovery finds nothing: it shells
8
+ * out to git to find the repo's top level and GitHub origin, then writes the
9
+ * config there exactly once. An existing file is always authoritative and is
10
+ * never overwritten. Every failure mode here is fail-open -- callers get
11
+ * undefined and fall back to legacy protocol v1, never a thrown error.
12
+ */
13
+ import { closeSync, mkdirSync, openSync, readFileSync, realpathSync, statSync, writeSync } from "node:fs";
14
+ import { dirname, isAbsolute, join, resolve } from "node:path";
15
+ export const CONFIG_DIRNAME = ".metergraph";
16
+ export const CONFIG_FILENAME = "config.json";
17
+ export const SUPPORTED_CONFIG_VERSION = 2;
18
+ const MAX_WALK_UP = 64;
19
+ const REMOTE_PATTERNS = [
20
+ /^git@github\.com:([^/]+\/[^/]+?)(\.git)?\/?$/,
21
+ /^https:\/\/github\.com\/([^/]+\/[^/]+?)(\.git)?\/?$/,
22
+ /^ssh:\/\/git@github\.com\/([^/]+\/[^/]+?)(\.git)?\/?$/,
23
+ ];
24
+ /** Return 'owner/repo' from a GitHub SSH or HTTPS remote URL, or undefined
25
+ * if the URL isn't a recognized GitHub origin. */
26
+ export function normalizeGithubRemote(url) {
27
+ const trimmed = url.trim();
28
+ for (const pattern of REMOTE_PATTERNS) {
29
+ const match = pattern.exec(trimmed);
30
+ if (match)
31
+ return match[1];
32
+ }
33
+ return undefined;
34
+ }
35
+ function load(path, repoRoot) {
36
+ let doc;
37
+ try {
38
+ doc = JSON.parse(readFileSync(path, "utf8"));
39
+ }
40
+ catch (error) {
41
+ console.warn(`metergraph: found ${path} but could not read it: ${error instanceof Error ? error.message : String(error)}`);
42
+ return undefined;
43
+ }
44
+ if (typeof doc !== "object" || doc === null || doc.version !== SUPPORTED_CONFIG_VERSION) {
45
+ console.warn(`metergraph: ${path} has an unsupported schema version; ignoring (expected version ${SUPPORTED_CONFIG_VERSION})`);
46
+ return undefined;
47
+ }
48
+ const repository = doc.repository;
49
+ if (typeof repository !== "string" || !repository.includes("/")) {
50
+ console.warn(`metergraph: ${path} is missing a valid 'repository' field; ignoring`);
51
+ return undefined;
52
+ }
53
+ return { repository, repoRoot };
54
+ }
55
+ /** Walk upward from appRoot looking for .metergraph/config.json. Returns
56
+ * undefined -- silently, the normal v1 state -- when nothing is found. */
57
+ export function discoverRepoConfig(appRoot) {
58
+ let current;
59
+ try {
60
+ current = realpathSync(appRoot);
61
+ }
62
+ catch {
63
+ return undefined;
64
+ }
65
+ for (let i = 0; i < MAX_WALK_UP; i += 1) {
66
+ const candidate = join(current, CONFIG_DIRNAME, CONFIG_FILENAME);
67
+ try {
68
+ readFileSync(candidate);
69
+ return load(candidate, current);
70
+ }
71
+ catch {
72
+ // no config at this level; keep walking up
73
+ }
74
+ const parent = dirname(current);
75
+ if (parent === current)
76
+ break;
77
+ current = parent;
78
+ }
79
+ return undefined;
80
+ }
81
+ function gitDirectory(repoRoot) {
82
+ const marker = join(repoRoot, ".git");
83
+ try {
84
+ if (statSync(marker).isDirectory())
85
+ return marker;
86
+ const match = /^gitdir:\s*(.+)$/m.exec(readFileSync(marker, "utf8"));
87
+ const path = match?.[1];
88
+ if (!path)
89
+ return undefined;
90
+ return isAbsolute(path) ? path : resolve(repoRoot, path);
91
+ }
92
+ catch {
93
+ return undefined;
94
+ }
95
+ }
96
+ function ownedByCurrentUser(path) {
97
+ if (typeof process.getuid !== "function")
98
+ return true;
99
+ try {
100
+ return statSync(path).uid === process.getuid();
101
+ }
102
+ catch {
103
+ return false;
104
+ }
105
+ }
106
+ function gitTopLevel(appRoot) {
107
+ try {
108
+ let current = realpathSync(appRoot);
109
+ for (let i = 0; i < MAX_WALK_UP; i += 1) {
110
+ const gitDir = gitDirectory(current);
111
+ if (gitDir) {
112
+ // Match Git's safe-directory default: never trust repository metadata
113
+ // owned by another Unix user. Unsupported platforms retain v1's
114
+ // existing fail-open behavior.
115
+ return ownedByCurrentUser(current) && ownedByCurrentUser(gitDir)
116
+ ? current
117
+ : undefined;
118
+ }
119
+ const parent = dirname(current);
120
+ if (parent === current)
121
+ break;
122
+ current = parent;
123
+ }
124
+ }
125
+ catch {
126
+ // Fall through to the normal v1 path.
127
+ }
128
+ return undefined;
129
+ }
130
+ function gitOriginUrl(repoRoot) {
131
+ try {
132
+ const gitDir = gitDirectory(repoRoot);
133
+ if (!gitDir)
134
+ return undefined;
135
+ let commonDir = gitDir;
136
+ try {
137
+ const relative = readFileSync(join(gitDir, "commondir"), "utf8").trim();
138
+ if (relative)
139
+ commonDir = resolve(gitDir, relative);
140
+ }
141
+ catch {
142
+ // Normal repositories keep config directly in .git.
143
+ }
144
+ const config = readFileSync(join(commonDir, "config"), "utf8");
145
+ let inOrigin = false;
146
+ for (const line of config.split(/\r?\n/)) {
147
+ const section = /^\s*\[remote\s+"([^"]+)"\]\s*$/.exec(line);
148
+ if (section) {
149
+ inOrigin = section[1] === "origin";
150
+ continue;
151
+ }
152
+ if (/^\s*\[/.test(line))
153
+ inOrigin = false;
154
+ if (inOrigin) {
155
+ const url = /^\s*url\s*=\s*(.+?)\s*$/.exec(line);
156
+ if (url)
157
+ return url[1];
158
+ }
159
+ }
160
+ }
161
+ catch {
162
+ // Missing or unreadable Git metadata is the normal production fallback.
163
+ }
164
+ return undefined;
165
+ }
166
+ /** Create .metergraph/config.json if -- and only if -- it doesn't already
167
+ * exist. Uses O_CREAT|O_EXCL ("wx") for an atomic create-only-if-absent; a
168
+ * concurrent writer (or a file that appeared between discovery and this
169
+ * call) always wins over us, and we simply read back whatever is there. */
170
+ export function writeConfigAtomically(repoRoot, repository) {
171
+ const configDir = join(repoRoot, CONFIG_DIRNAME);
172
+ const configPath = join(configDir, CONFIG_FILENAME);
173
+ const payload = `${JSON.stringify({ version: SUPPORTED_CONFIG_VERSION, repository })}\n`;
174
+ try {
175
+ mkdirSync(configDir, { recursive: true });
176
+ const fd = openSync(configPath, "wx", 0o644);
177
+ try {
178
+ writeSync(fd, payload);
179
+ }
180
+ finally {
181
+ closeSync(fd);
182
+ }
183
+ }
184
+ catch (error) {
185
+ if (error.code !== "EEXIST") {
186
+ console.warn(`metergraph: could not write ${configPath}: ${error instanceof Error ? error.message : String(error)}`);
187
+ return undefined;
188
+ }
189
+ }
190
+ return load(configPath, repoRoot);
191
+ }
192
+ /** Discover an existing repo config, or detect+write one once at the git top
193
+ * level. Fail-open: any detection or write failure returns undefined
194
+ * (legacy protocol v1), never throws. */
195
+ export function ensureRepoConfig(appRoot) {
196
+ const existing = discoverRepoConfig(appRoot);
197
+ if (existing)
198
+ return existing;
199
+ try {
200
+ const repoRoot = gitTopLevel(appRoot);
201
+ if (!repoRoot)
202
+ return undefined;
203
+ const origin = gitOriginUrl(repoRoot);
204
+ if (!origin)
205
+ return undefined;
206
+ const repository = normalizeGithubRemote(origin);
207
+ if (!repository)
208
+ return undefined;
209
+ return writeConfigAtomically(repoRoot, repository);
210
+ }
211
+ catch {
212
+ return undefined;
213
+ }
214
+ }
@@ -0,0 +1,23 @@
1
+ export declare const DEFAULT_TIMEOUT_MS = 3000;
2
+ export declare const DEFAULT_TTL_MS = 300000;
3
+ export declare const REFRESH_MARGIN_MS = 30000;
4
+ export declare const MAX_BACKOFF_MS = 60000;
5
+ export declare class SessionManager {
6
+ private readonly appToken;
7
+ private readonly repository;
8
+ private readonly sdkVersion;
9
+ private readonly timeoutMs;
10
+ private readonly url;
11
+ private readonly failureLog;
12
+ private token;
13
+ private expiresAt;
14
+ private stopped;
15
+ private retryAt;
16
+ private backoffMs;
17
+ constructor(appToken: string, baseUrl: string, repository: string, sdkVersion: string, timeoutMs?: number);
18
+ getToken(): Promise<string | undefined>;
19
+ invalidate(): void;
20
+ stop(): void;
21
+ private exchange;
22
+ private markFailed;
23
+ }
@@ -0,0 +1,109 @@
1
+ /**
2
+ * Repository-aware ingest protocol v2: app-token -> session-token exchange.
3
+ *
4
+ * The app token is sent only on POST /v1/ingest/sessions. The resulting
5
+ * session token is cached in memory and reused until it's within
6
+ * REFRESH_MARGIN_MS of expiry, at which point the next getToken() call
7
+ * transparently re-exchanges it. Exchanges happen lazily, off the caller's
8
+ * request path (Transport.deliver already runs asynchronously), so a slow
9
+ * round trip here adds no latency to the customer's LLM call. Every failure
10
+ * mode is fail-open: getToken() resolves to undefined and callers drop that
11
+ * batch rather than ever sending the app token to /v1/ingest.
12
+ */
13
+ import { FailureLogger } from "./failure-log.js";
14
+ export const DEFAULT_TIMEOUT_MS = 3_000;
15
+ export const DEFAULT_TTL_MS = 300_000;
16
+ export const REFRESH_MARGIN_MS = 30_000;
17
+ export const MAX_BACKOFF_MS = 60_000;
18
+ function parseExpiresAt(value) {
19
+ if (typeof value !== "string")
20
+ return undefined;
21
+ const parsed = Date.parse(value);
22
+ return Number.isNaN(parsed) ? undefined : parsed;
23
+ }
24
+ export class SessionManager {
25
+ appToken;
26
+ repository;
27
+ sdkVersion;
28
+ timeoutMs;
29
+ url;
30
+ failureLog = new FailureLogger();
31
+ token;
32
+ expiresAt = 0;
33
+ stopped = false;
34
+ retryAt = 0;
35
+ backoffMs = 1_000;
36
+ constructor(appToken, baseUrl, repository, sdkVersion, timeoutMs = DEFAULT_TIMEOUT_MS) {
37
+ this.appToken = appToken;
38
+ this.repository = repository;
39
+ this.sdkVersion = sdkVersion;
40
+ this.timeoutMs = timeoutMs;
41
+ this.url = `${baseUrl.replace(/\/$/, "")}/v1/ingest/sessions`;
42
+ }
43
+ async getToken() {
44
+ if (this.stopped)
45
+ return undefined;
46
+ if (this.token !== undefined && Date.now() < this.expiresAt - REFRESH_MARGIN_MS) {
47
+ return this.token;
48
+ }
49
+ if (Date.now() < this.retryAt)
50
+ return undefined;
51
+ await this.exchange();
52
+ return this.stopped ? undefined : this.token;
53
+ }
54
+ invalidate() {
55
+ this.token = undefined;
56
+ this.expiresAt = 0;
57
+ this.retryAt = 0;
58
+ this.backoffMs = 1_000;
59
+ }
60
+ stop() {
61
+ this.stopped = true;
62
+ this.token = undefined;
63
+ this.expiresAt = 0;
64
+ }
65
+ async exchange() {
66
+ try {
67
+ const response = await fetch(this.url, {
68
+ method: "POST",
69
+ headers: {
70
+ Authorization: `Bearer ${this.appToken}`,
71
+ "Content-Type": "application/json",
72
+ },
73
+ body: JSON.stringify({
74
+ protocol_version: 2,
75
+ repository: this.repository,
76
+ sdk_version: this.sdkVersion,
77
+ }),
78
+ signal: AbortSignal.timeout(this.timeoutMs),
79
+ });
80
+ if (!response.ok) {
81
+ this.failureLog.report("session_exchange_error", `session exchange to ${this.url} failed with HTTP ${response.status}`);
82
+ this.markFailed();
83
+ return;
84
+ }
85
+ const doc = (await response.json());
86
+ const token = doc?.session_token;
87
+ if (typeof token !== "string" || !token) {
88
+ this.failureLog.report("session_exchange_error", `session exchange to ${this.url} returned no session_token`);
89
+ this.markFailed();
90
+ return;
91
+ }
92
+ const expiresAt = parseExpiresAt(doc?.expires_at) ?? Date.now() + DEFAULT_TTL_MS;
93
+ if (!this.stopped) {
94
+ this.token = token;
95
+ this.expiresAt = expiresAt;
96
+ this.retryAt = 0;
97
+ this.backoffMs = 1_000;
98
+ }
99
+ }
100
+ catch (error) {
101
+ this.failureLog.report("session_exchange_error", `session exchange to ${this.url} failed: ${error instanceof Error ? error.message : String(error)}`);
102
+ this.markFailed();
103
+ }
104
+ }
105
+ markFailed() {
106
+ this.retryAt = Date.now() + this.backoffMs;
107
+ this.backoffMs = Math.min(this.backoffMs * 2, MAX_BACKOFF_MS);
108
+ }
109
+ }
@@ -1,3 +1,4 @@
1
+ import type { SessionManager } from "./session.js";
1
2
  export type TransportMode = "auto" | "background" | "buffered";
2
3
  export type WaitUntil = (promise: Promise<unknown>) => void;
3
4
  export declare const MAX_BATCH_BYTES: number;
@@ -6,6 +7,7 @@ export interface TransportOptions {
6
7
  batchSize?: number;
7
8
  flushMs?: number;
8
9
  mode?: TransportMode;
10
+ session?: SessionManager;
9
11
  }
10
12
  export declare class Transport {
11
13
  private readonly token;
@@ -24,6 +26,7 @@ export declare class Transport {
24
26
  private retryAt;
25
27
  private backoffMs;
26
28
  private readonly failureLog;
29
+ private readonly session?;
27
30
  constructor(token: string, baseUrl: string, options?: TransportOptions);
28
31
  bindWaitUntil(value: {
29
32
  waitUntil(promise: Promise<unknown>): void;
package/dist/transport.js CHANGED
@@ -30,9 +30,11 @@ export class Transport {
30
30
  retryAt = 0;
31
31
  backoffMs = 1_000;
32
32
  failureLog = new FailureLogger();
33
+ session;
33
34
  constructor(token, baseUrl, options = {}) {
34
35
  this.token = token;
35
36
  this.baseUrl = baseUrl;
37
+ this.session = options.session;
36
38
  this.queueSize = Math.max(1, options.queueSize ?? 2_000);
37
39
  this.batchSize = Math.max(1, Math.min(1_000, options.batchSize ?? 100));
38
40
  this.mode = options.mode === "auto" || !options.mode ? detectedMode() : options.mode;
@@ -99,6 +101,11 @@ export class Transport {
99
101
  this.dropped += rows.length;
100
102
  return;
101
103
  }
104
+ const token = this.session ? await this.session.getToken() : this.token;
105
+ if (token === undefined) {
106
+ this.dropped += rows.length;
107
+ return;
108
+ }
102
109
  const encoded = new TextEncoder().encode(JSON.stringify({
103
110
  schema_version: 1,
104
111
  rows,
@@ -121,7 +128,7 @@ export class Transport {
121
128
  const response = await fetch(`${this.baseUrl.replace(/\/$/, "")}/v1/ingest`, {
122
129
  method: "POST",
123
130
  headers: {
124
- Authorization: `Bearer ${this.token}`,
131
+ Authorization: `Bearer ${token}`,
125
132
  "Content-Type": "application/json",
126
133
  ...(compressed && body !== raw ? { "Content-Encoding": "gzip" } : {}),
127
134
  },
@@ -129,6 +136,11 @@ export class Transport {
129
136
  keepalive: this.mode === "buffered" && body.byteLength <= 64 * 1024,
130
137
  });
131
138
  if (response.status === 401 || response.status === 403) {
139
+ if (this.session) {
140
+ this.session.invalidate();
141
+ this.dropped += rows.length;
142
+ return;
143
+ }
132
144
  this.fatal = true;
133
145
  console.warn("Metergraph authentication failed; capture disabled for this process");
134
146
  return;
@@ -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.0";
1
+ export declare const SDK_VERSION = "0.4.0";
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const SDK_VERSION = "0.3.0";
1
+ export const SDK_VERSION = "0.4.0";
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.0",
3
+ "version": "0.4.0",
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
  }