metergraph 0.6.5 → 0.6.7

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
@@ -5,6 +5,10 @@ provider-independent Vercel AI SDK language models on Node 18+, including AWS
5
5
  Lambda. Worker-style `waitUntil` hooks are present but are not part of the v1
6
6
  public-package qualification contract.
7
7
 
8
+ Metergraph supports OpenAI JavaScript SDK majors 4 through 7. The current
9
+ major is tested in normal CI; the oldest supported major is qualified with a
10
+ manually triggered compatibility workflow.
11
+
8
12
  Initialize Metergraph once, then wrap each provider client. `init()` reads the
9
13
  token and other omitted options from the environment.
10
14
 
package/dist/capture.js CHANGED
@@ -73,9 +73,18 @@ function usage(response) {
73
73
  : fallback ?? provider[key],
74
74
  ]));
75
75
  }
76
+ // Only a non-empty string is reply text. On an OpenAI Responses body `text` is
77
+ // the text *config*, and a function-call-only reply lives in `output`.
78
+ function directText(response) {
79
+ const outputText = get(response, "output_text");
80
+ if (typeof outputText === "string" && outputText)
81
+ return outputText;
82
+ const text = get(response, "text");
83
+ return typeof text === "string" ? text : undefined;
84
+ }
76
85
  function responseText(response) {
77
- const direct = get(response, "output_text") ?? get(response, "text");
78
- if (typeof direct === "string")
86
+ const direct = directText(response);
87
+ if (direct !== undefined)
79
88
  return direct;
80
89
  const message = get(first(get(response, "choices")), "message");
81
90
  const content = get(message, "content");
@@ -169,7 +178,7 @@ function toolArgument(value) {
169
178
  return value;
170
179
  }
171
180
  }
172
- return (0, template_js_1.scrub)(value);
181
+ return value;
173
182
  }
174
183
  function toolEvents(request, response, streamChunks = []) {
175
184
  const calls = new Map();
@@ -340,7 +349,7 @@ function toolEvents(request, response, streamChunks = []) {
340
349
  arguments: input && typeof input === "object"
341
350
  && Object.keys(input).length === 0
342
351
  ? ""
343
- : JSON.stringify((0, template_js_1.scrub)(input ?? {})),
352
+ : JSON.stringify(input ?? {}),
344
353
  });
345
354
  }
346
355
  }
@@ -368,27 +377,27 @@ function toolEvents(request, response, streamChunks = []) {
368
377
  function responseContent(response, aggregate) {
369
378
  if (aggregate !== undefined)
370
379
  return aggregate;
371
- const direct = get(response, "output_text") ?? get(response, "text");
380
+ const direct = directText(response);
372
381
  if (direct !== undefined)
373
- return (0, template_js_1.scrub)(direct);
382
+ return direct;
374
383
  const message = get(first(get(response, "choices")), "message");
375
384
  const content = get(message, "content");
376
385
  if (content !== undefined)
377
- return (0, template_js_1.scrub)(content);
386
+ return content;
378
387
  const parsed = get(message, "parsed");
379
388
  if (parsed !== undefined)
380
- return (0, template_js_1.scrub)(parsed);
389
+ return parsed;
381
390
  const normalizedText = responseText(response);
382
391
  if (normalizedText !== undefined)
383
392
  return normalizedText;
384
393
  const blocks = get(response, "content");
385
394
  if (blocks !== undefined)
386
- return (0, template_js_1.scrub)(blocks);
395
+ return blocks;
387
396
  const output = get(response, "output");
388
397
  if (output !== undefined)
389
- return (0, template_js_1.scrub)(output);
398
+ return output;
390
399
  const candidates = get(response, "candidates");
391
- return candidates === undefined ? undefined : (0, template_js_1.scrub)(candidates);
400
+ return candidates;
392
401
  }
393
402
  function responseEnvelope(response, aggregate, tools, error, status) {
394
403
  const responseMetadata = get(response, "response");
@@ -496,8 +505,9 @@ class CaptureRuntime {
496
505
  }
497
506
  return { value: `${clipped}${marker}`, truncated: true };
498
507
  };
499
- const request = text(JSON.stringify((0, template_js_1.scrub)(state.request)), "request");
500
- const fullTools = toolEvents((0, template_js_1.scrub)(state.request), response, extra.responseChunks);
508
+ const capturedRequest = (0, template_js_1.scrubRequest)(state.request);
509
+ const request = text(JSON.stringify(capturedRequest), "request");
510
+ const fullTools = toolEvents(capturedRequest, response, extra.responseChunks);
501
511
  const status = extra.status ?? (extra.error ? "error" : stopReason(response) ?? "success");
502
512
  const { finishReason, finishReasonRaw } = finishReasonDetails(response);
503
513
  const statusCode = extra.error || extra.status === "error" || finishReason === "error"
@@ -1,2 +1,11 @@
1
- export declare function scrub(value: unknown): unknown;
1
+ /**
2
+ * The provider request as captured, without its credentials.
3
+ *
4
+ * Credentials travel as request-level parameters and in header or query
5
+ * containers. `extra_body` is merged into the request body, so its keys are
6
+ * request-level too. Messages, tools and schemas are kept exactly as sent,
7
+ * even where a name matches a credential's (a tool parameter called `token`),
8
+ * because they are the request analysis replays.
9
+ */
10
+ export declare function scrubRequest(request: unknown): unknown;
2
11
  export declare function templateHash(request: Record<string, unknown>): string;
package/dist/template.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.scrub = scrub;
3
+ exports.scrubRequest = scrubRequest;
4
4
  exports.templateHash = templateHash;
5
5
  const sensitive = new Set([
6
6
  "api-key",
@@ -19,13 +19,56 @@ const sensitive = new Set([
19
19
  "token",
20
20
  "x-api-key",
21
21
  ]);
22
- function scrub(value) {
22
+ // Header and query containers carry transport credentials and nothing analysis
23
+ // reads, so they are dropped whole. Keys compare lower-cased, so camelCase
24
+ // client options match.
25
+ const transportContainers = new Set([
26
+ "default_headers",
27
+ "default_query",
28
+ "defaultheaders",
29
+ "defaultquery",
30
+ "extra_headers",
31
+ "extra_query",
32
+ "extraheaders",
33
+ "extraquery",
34
+ "headers",
35
+ "query",
36
+ ]);
37
+ function credentialKey(key) {
38
+ const lower = key.trim().toLowerCase();
39
+ return sensitive.has(lower) || transportContainers.has(lower);
40
+ }
41
+ function withoutCredentialKeys(value) {
42
+ return Object.fromEntries(Object.entries(value).filter(([key]) => !credentialKey(key)));
43
+ }
44
+ /**
45
+ * The provider request as captured, without its credentials.
46
+ *
47
+ * Credentials travel as request-level parameters and in header or query
48
+ * containers. `extra_body` is merged into the request body, so its keys are
49
+ * request-level too. Messages, tools and schemas are kept exactly as sent,
50
+ * even where a name matches a credential's (a tool parameter called `token`),
51
+ * because they are the request analysis replays.
52
+ */
53
+ function scrubRequest(request) {
54
+ if (!request || typeof request !== "object" || Array.isArray(request))
55
+ return request;
56
+ const clean = withoutCredentialKeys(request);
57
+ for (const key of ["extra_body", "extraBody"]) {
58
+ const body = clean[key];
59
+ if (body && typeof body === "object" && !Array.isArray(body)) {
60
+ clean[key] = withoutCredentialKeys(body);
61
+ }
62
+ }
63
+ return clean;
64
+ }
65
+ function withoutCredentialNames(value) {
23
66
  if (Array.isArray(value))
24
- return value.map(scrub);
67
+ return value.map(withoutCredentialNames);
25
68
  if (value && typeof value === "object") {
26
69
  return Object.fromEntries(Object.entries(value)
27
70
  .filter(([key]) => !sensitive.has(key.trim().toLowerCase()))
28
- .map(([key, item]) => [key, scrub(item)]));
71
+ .map(([key, item]) => [key, withoutCredentialNames(item)]));
29
72
  }
30
73
  return value;
31
74
  }
@@ -56,6 +99,8 @@ function fnv1a(value) {
56
99
  }
57
100
  return hash.toString(16).padStart(16, "0");
58
101
  }
102
+ // Credential names are left out at every depth. This hash routes unnamed
103
+ // workloads, so its input must stay stable across SDK versions.
59
104
  function templateHash(request) {
60
- return fnv1a(JSON.stringify(normalized(scrub(request))));
105
+ return fnv1a(JSON.stringify(normalized(withoutCredentialNames(request))));
61
106
  }
package/dist/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const SDK_VERSION = "0.6.5";
1
+ export declare const SDK_VERSION = "0.6.7";
package/dist/version.js CHANGED
@@ -1,4 +1,4 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.SDK_VERSION = void 0;
4
- exports.SDK_VERSION = "0.6.5";
4
+ exports.SDK_VERSION = "0.6.7";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "metergraph",
3
- "version": "0.6.5",
3
+ "version": "0.6.7",
4
4
  "description": "Fire-and-forget LLM spend capture for Metergraph",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -21,6 +21,7 @@
21
21
  "prepublishOnly": "npm test && npm run test:package",
22
22
  "test": "npm run build && npm run test:types && npm run test:examples && node --test test/*.test.mjs",
23
23
  "test:examples": "tsc --noEmit --strict --skipLibCheck --target ES2022 --module NodeNext --moduleResolution NodeNext test/ai-sdk-factories.types.ts",
24
+ "test:openai-v4": "npm run build && node --test test/compatibility/openai-v4.test.mjs",
24
25
  "test:package": "npm run build && node --test test/package/packed-package.test.mjs",
25
26
  "test:types": "tsc --noEmit --strict --skipLibCheck --target ES2022 --module NodeNext --moduleResolution NodeNext test/ai-sdk-current.types.ts test/wrap-gateway.types.ts"
26
27
  },
@@ -55,7 +56,7 @@
55
56
  "@anthropic-ai/sdk": ">=0.30",
56
57
  "@google/genai": ">=1",
57
58
  "ai": ">=5 <8",
58
- "openai": ">=4"
59
+ "openai": ">=4 <8"
59
60
  },
60
61
  "peerDependenciesMeta": {
61
62
  "openai": {"optional": true},