metergraph 0.2.1 → 0.3.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
@@ -9,7 +9,7 @@ client; call `init(...)` before the first `wrap()` only to pass options in
9
9
  code.
10
10
 
11
11
  ```ts
12
- import { wrap, route, modelFor, recordOutcome, setSession } from "metergraph";
12
+ import { wrap, route, trace, modelFor, recordOutcome, setSession } from "metergraph";
13
13
  import OpenAI from "openai";
14
14
 
15
15
  // new Anthropic() and new GoogleGenAI({}) wrap the same way.
@@ -17,10 +17,12 @@ const client = wrap(new OpenAI());
17
17
  setSession("ticket-123");
18
18
 
19
19
  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 });
20
+ await trace("ticket-workflow", () =>
21
+ route("ticket-classifier", async () => {
22
+ model = modelFor("ticket-classifier", { default: model });
23
+ await client.chat.completions.create({ model, messages: [] });
24
+ }, { unit: "answer" })
25
+ );
24
26
 
25
27
  recordOutcome("ticket-classifier", {
26
28
  model,
@@ -38,9 +40,23 @@ required and throws if missing so a caller error surfaces immediately
38
40
  instead of an `undefined` model reaching a provider call.
39
41
 
40
42
  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.
43
+ the hosted HTTPS endpoint. SDK 0.3 captures the scrubbed provider request and a
44
+ normalized response envelope, including assistant content and tool calls, by
45
+ default. Provider credentials and transport headers are removed. Request and
46
+ response are each limited to 100 KiB of UTF-8 with an explicit truncation
47
+ marker. Set `METERGRAPH_CAPTURE_TEXT=0`, initialize with
48
+ `captureText: false`, or set `captureText: false` on an individual `route()` or
49
+ `trace()` to opt out for sensitive operations. The public open-source server
50
+ still discards content; hosted workspaces retain it under their metadata
51
+ retention period.
52
+
53
+ `trace(name, fn, { traceId, parentSpanId, captureText })` groups calls into a
54
+ logical trace using `AsyncLocalStorage`. Nested traces reuse the active trace
55
+ unless given a different trace ID. Calls outside a trace become one-span
56
+ traces. Manual IDs can join work across process boundaries; automatic W3C HTTP
57
+ propagation is not included.
58
+
59
+ Actual wire batches never exceed 512 KiB after optional gzip.
44
60
  Long-running Node processes use an unref'd
45
61
  background timer. On Workers or Vercel, call
46
62
  `bindWaitUntil(ctx)` once per request. On Lambda, use `wrapHandler(handler)`
@@ -72,9 +88,13 @@ import * as mg from "metergraph". wrap() returns the same client and
72
88
  initializes itself from the environment: METERGRAPH_APP_TOKEN is required
73
89
  (capture is silently off without it) and METERGRAPH_INGEST_URL is only for
74
90
  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
91
+ SDK 0.3 captures scrubbed provider requests and normalized responses by
92
+ default for the hosted dashboard; use METERGRAPH_CAPTURE_TEXT=0 or
93
+ captureText: false around sensitive operations. Provider credentials and
94
+ transport headers must never be captured. Capture is fail-open, so do not
95
+ change call sites, arguments, or error handling; async and streaming work
96
+ unchanged. Use mg.trace("stable-name", fn) to group multi-call workflows. Wrap
97
+ each function that calls a
78
98
  wrapped client with mg.track("stable.name", fn), because stack-based
79
99
  attribution is unreliable under bundlers. On serverless (Lambda / Workers /
80
100
  Vercel), ensure delivery before the runtime freezes: mg.wrapHandler(handler),
package/dist/capture.d.ts CHANGED
@@ -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";
@@ -77,6 +78,242 @@ function toolNames(request) {
77
78
  });
78
79
  return names.length ? names : undefined;
79
80
  }
81
+ function toolArgument(value) {
82
+ if (typeof value === "string") {
83
+ try {
84
+ return JSON.parse(value);
85
+ }
86
+ catch {
87
+ return value;
88
+ }
89
+ }
90
+ return scrub(value);
91
+ }
92
+ function toolEvents(request, response, streamChunks = []) {
93
+ const calls = new Map();
94
+ const pending = new Map();
95
+ const policies = new Map();
96
+ if (Array.isArray(request.tools)) {
97
+ for (const tool of request.tools) {
98
+ const fn = get(tool, "function");
99
+ const name = get(fn, "name") ?? get(tool, "name");
100
+ const policy = get(fn, "x-metergraph-idempotency")
101
+ ?? get(tool, "x-metergraph-idempotency")
102
+ ?? get(fn, "metergraph_idempotency")
103
+ ?? get(tool, "metergraph_idempotency");
104
+ if (name)
105
+ policies.set(String(name), policy === "idempotent" ? "idempotent" : "non_idempotent");
106
+ }
107
+ }
108
+ const complete = (id, result, error = false) => {
109
+ const key = String(id ?? "");
110
+ const item = calls.get(key);
111
+ if (!item) {
112
+ pending.set(key, { result, error });
113
+ return;
114
+ }
115
+ item.result = toolArgument(result);
116
+ item.status = error ? "error" : "completed";
117
+ };
118
+ const add = (id, name, args) => {
119
+ if (!name)
120
+ return;
121
+ const key = String(id ?? `${String(name)}:${calls.size}`);
122
+ const item = {
123
+ call_id: key,
124
+ name: String(name),
125
+ arguments: toolArgument(args),
126
+ status: "requested",
127
+ idempotency: policies.get(String(name)) ?? "non_idempotent",
128
+ };
129
+ calls.set(key, item);
130
+ const waiting = pending.get(key);
131
+ if (waiting) {
132
+ pending.delete(key);
133
+ complete(key, waiting.result, waiting.error);
134
+ }
135
+ };
136
+ const blocks = (value) => {
137
+ if (!Array.isArray(value))
138
+ return;
139
+ for (const block of value) {
140
+ const kind = get(block, "type");
141
+ if (kind === "tool_use") {
142
+ add(get(block, "id"), get(block, "name"), get(block, "input"));
143
+ }
144
+ else if (kind === "tool_result") {
145
+ complete(get(block, "tool_use_id"), get(block, "content"), Boolean(get(block, "is_error")));
146
+ }
147
+ }
148
+ };
149
+ const geminiParts = (value) => {
150
+ const parts = get(value, "parts");
151
+ if (!Array.isArray(parts))
152
+ return;
153
+ for (const part of parts) {
154
+ const call = get(part, "function_call") ?? get(part, "functionCall");
155
+ if (call)
156
+ add(get(call, "id"), get(call, "name"), get(call, "args") ?? get(call, "arguments"));
157
+ const result = get(part, "function_response") ?? get(part, "functionResponse");
158
+ if (result)
159
+ complete(get(result, "id") ?? get(result, "name"), get(result, "response"));
160
+ }
161
+ };
162
+ const history = Array.isArray(request.messages)
163
+ ? request.messages
164
+ : Array.isArray(request.input)
165
+ ? request.input
166
+ : Array.isArray(request.contents)
167
+ ? request.contents
168
+ : [];
169
+ for (const message of history) {
170
+ const messageCalls = get(message, "tool_calls");
171
+ if (Array.isArray(messageCalls)) {
172
+ for (const tool of messageCalls) {
173
+ const fn = get(tool, "function");
174
+ add(get(tool, "id") ?? get(tool, "call_id"), get(fn, "name") ?? get(tool, "name"), get(fn, "arguments") ?? get(tool, "arguments"));
175
+ }
176
+ }
177
+ if (get(message, "role") === "tool") {
178
+ complete(get(message, "tool_call_id") ?? get(message, "call_id"), get(message, "content"), Boolean(get(message, "is_error")));
179
+ }
180
+ const kind = get(message, "type");
181
+ if (kind === "function_call" || kind === "tool_call") {
182
+ add(get(message, "call_id") ?? get(message, "id"), get(message, "name"), get(message, "arguments"));
183
+ }
184
+ else if (kind === "function_call_output" || kind === "tool_result") {
185
+ complete(get(message, "call_id") ?? get(message, "tool_use_id"), get(message, "output") ?? get(message, "content"), Boolean(get(message, "is_error")));
186
+ }
187
+ blocks(get(message, "content"));
188
+ geminiParts(message);
189
+ }
190
+ const message = get(first(get(response, "choices")), "message");
191
+ const responseCalls = get(message, "tool_calls");
192
+ if (Array.isArray(responseCalls)) {
193
+ for (const tool of responseCalls) {
194
+ const fn = get(tool, "function");
195
+ add(get(tool, "id") ?? get(tool, "call_id"), get(fn, "name") ?? get(tool, "name"), get(fn, "arguments") ?? get(tool, "arguments"));
196
+ }
197
+ }
198
+ blocks(get(response, "content"));
199
+ const outputs = get(response, "output");
200
+ if (Array.isArray(outputs)) {
201
+ for (const output of outputs) {
202
+ const kind = get(output, "type");
203
+ if (kind === "function_call" || kind === "tool_call") {
204
+ add(get(output, "call_id") ?? get(output, "id"), get(output, "name"), get(output, "arguments"));
205
+ }
206
+ }
207
+ }
208
+ const candidates = get(response, "candidates");
209
+ if (Array.isArray(candidates)) {
210
+ for (const candidate of candidates)
211
+ geminiParts(get(candidate, "content"));
212
+ }
213
+ const openAIDeltas = new Map();
214
+ const anthropicDeltas = new Map();
215
+ for (const chunk of streamChunks) {
216
+ const choices = get(chunk, "choices");
217
+ if (Array.isArray(choices)) {
218
+ for (const choice of choices) {
219
+ const tools = get(get(choice, "delta"), "tool_calls");
220
+ if (!Array.isArray(tools))
221
+ continue;
222
+ tools.forEach((tool, position) => {
223
+ const key = String(get(tool, "index") ?? position);
224
+ const item = openAIDeltas.get(key) ?? { id: key, name: "", arguments: "" };
225
+ item.id = String(get(tool, "id") ?? item.id);
226
+ const fn = get(tool, "function");
227
+ if (get(fn, "name"))
228
+ item.name += String(get(fn, "name"));
229
+ if (get(fn, "arguments"))
230
+ item.arguments += String(get(fn, "arguments"));
231
+ openAIDeltas.set(key, item);
232
+ });
233
+ }
234
+ }
235
+ const kind = get(chunk, "type");
236
+ if (kind === "content_block_start") {
237
+ const block = get(chunk, "content_block");
238
+ if (get(block, "type") === "tool_use") {
239
+ const key = String(get(chunk, "index") ?? anthropicDeltas.size);
240
+ const input = get(block, "input");
241
+ anthropicDeltas.set(key, {
242
+ id: String(get(block, "id") ?? key),
243
+ name: String(get(block, "name") ?? ""),
244
+ arguments: input && typeof input === "object"
245
+ && Object.keys(input).length === 0
246
+ ? ""
247
+ : JSON.stringify(scrub(input ?? {})),
248
+ });
249
+ }
250
+ }
251
+ else if (kind === "content_block_delta") {
252
+ const delta = get(chunk, "delta");
253
+ if (get(delta, "type") === "input_json_delta") {
254
+ const key = String(get(chunk, "index") ?? "0");
255
+ const item = anthropicDeltas.get(key) ?? { id: key, name: "", arguments: "" };
256
+ item.arguments += String(get(delta, "partial_json") ?? "");
257
+ anthropicDeltas.set(key, item);
258
+ }
259
+ }
260
+ const chunkCandidates = get(chunk, "candidates");
261
+ if (Array.isArray(chunkCandidates)) {
262
+ for (const candidate of chunkCandidates)
263
+ geminiParts(get(candidate, "content"));
264
+ }
265
+ }
266
+ for (const item of [...openAIDeltas.values(), ...anthropicDeltas.values()]) {
267
+ if (item.name)
268
+ add(item.id, item.name, item.arguments);
269
+ }
270
+ return calls.size ? [...calls.values()] : undefined;
271
+ }
272
+ function responseContent(response, aggregate) {
273
+ if (aggregate !== undefined)
274
+ return aggregate;
275
+ const direct = get(response, "output_text") ?? get(response, "text");
276
+ if (direct !== undefined)
277
+ return scrub(direct);
278
+ const message = get(first(get(response, "choices")), "message");
279
+ const content = get(message, "content");
280
+ if (content !== undefined)
281
+ return scrub(content);
282
+ const parsed = get(message, "parsed");
283
+ if (parsed !== undefined)
284
+ return scrub(parsed);
285
+ const normalizedText = responseText(response);
286
+ if (normalizedText !== undefined)
287
+ return normalizedText;
288
+ const blocks = get(response, "content");
289
+ if (blocks !== undefined)
290
+ return scrub(blocks);
291
+ const output = get(response, "output");
292
+ if (output !== undefined)
293
+ return scrub(output);
294
+ const candidates = get(response, "candidates");
295
+ return candidates === undefined ? undefined : scrub(candidates);
296
+ }
297
+ function responseEnvelope(response, aggregate, tools, error, status) {
298
+ const envelope = {
299
+ role: "assistant",
300
+ content: responseContent(response, aggregate),
301
+ tool_calls: tools ?? [],
302
+ finish_reason: stopReason(response),
303
+ request_id: get(response, "_request_id") ?? get(response, "response_id")
304
+ ?? get(response, "responseId") ?? get(response, "id"),
305
+ model: get(response, "model") ?? get(response, "model_version")
306
+ ?? get(response, "modelVersion"),
307
+ status,
308
+ };
309
+ if (error !== undefined) {
310
+ envelope.error = {
311
+ type: error instanceof Error ? error.name : "Error",
312
+ message: error instanceof Error ? error.message : String(error),
313
+ };
314
+ }
315
+ return Object.fromEntries(Object.entries(envelope).filter(([, value]) => value !== undefined));
316
+ }
80
317
  const SDK_DIR = (() => {
81
318
  try {
82
319
  return new URL(".", import.meta.url).pathname;
@@ -124,6 +361,11 @@ export class CaptureRuntime {
124
361
  started: performance.now(),
125
362
  ts: new Date().toISOString(),
126
363
  frames: frames(stack, this.options.appRoot, this.options.skipFrames),
364
+ traceId: context.traceId ?? randomBytes(16).toString("hex"),
365
+ spanId: randomBytes(8).toString("hex"),
366
+ parentSpanId: context.parentSpanId,
367
+ traceName: context.traceName ?? context.route ?? context.funcName
368
+ ?? endpoint,
127
369
  done: false,
128
370
  };
129
371
  }
@@ -144,11 +386,45 @@ export class CaptureRuntime {
144
386
  const bytes = new TextEncoder().encode(value);
145
387
  if (bytes.byteLength <= this.options.textMaxBytes)
146
388
  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 };
389
+ const marker = "\n<metergraph:truncated>";
390
+ const markerBytes = new TextEncoder().encode(marker).byteLength;
391
+ let clipped = new TextDecoder().decode(bytes.slice(0, Math.max(0, this.options.textMaxBytes - markerBytes)));
392
+ while (clipped
393
+ && new TextEncoder().encode(`${clipped}${marker}`).byteLength
394
+ > this.options.textMaxBytes) {
395
+ clipped = clipped.slice(0, -1);
396
+ }
397
+ return { value: `${clipped}${marker}`, truncated: true };
149
398
  };
150
399
  const request = text(JSON.stringify(scrub(state.request)), "request");
151
- const output = text(extra.responseText ?? responseText(response), "response");
400
+ const fullTools = toolEvents(scrub(state.request), response, extra.responseChunks);
401
+ const status = extra.status ?? (extra.error ? "error" : stopReason(response) ?? "success");
402
+ const output = text(JSON.stringify(responseEnvelope(response, extra.responseText, fullTools, extra.error, status)), "response");
403
+ let toolTruncated = false;
404
+ let persistedTools;
405
+ if (fullTools && captureText) {
406
+ const encodedTools = text(JSON.stringify(fullTools), "response");
407
+ toolTruncated = encodedTools.truncated;
408
+ try {
409
+ persistedTools = encodedTools.value
410
+ ? JSON.parse(encodedTools.value)
411
+ : undefined;
412
+ }
413
+ catch {
414
+ // A truncated JSON payload is intentionally omitted. The bounded,
415
+ // redacted response envelope still describes the call and the row's
416
+ // text_truncated flag tells ingestion why this field is unavailable.
417
+ persistedTools = undefined;
418
+ }
419
+ }
420
+ else if (fullTools) {
421
+ persistedTools = fullTools.map((item) => ({
422
+ call_id: item.call_id,
423
+ name: item.name,
424
+ status: item.status,
425
+ idempotency: item.idempotency,
426
+ }));
427
+ }
152
428
  const firstFrame = state.frames[0];
153
429
  this.transport.enqueue({
154
430
  ts: state.ts,
@@ -157,22 +433,28 @@ export class CaptureRuntime {
157
433
  model: state.request.model,
158
434
  ...usage(response),
159
435
  latency_ms: Math.round(performance.now() - state.started),
160
- status: extra.status ?? (extra.error ? "error" : stopReason(response) ?? "success"),
436
+ status,
161
437
  session_id: state.context.sessionId,
438
+ conversation_id: state.context.sessionId,
439
+ trace_id: state.traceId,
440
+ span_id: state.spanId,
441
+ parent_span_id: state.parentSpanId,
442
+ trace_name: state.traceName,
162
443
  template_hash: templateHash(state.request),
163
444
  unit_name: state.context.unitName,
164
445
  unit_count: state.context.unitCount,
165
- tool_calls: toolNames(state.request),
446
+ tool_calls: fullTools ? persistedTools : toolNames(state.request),
166
447
  endpoint: state.endpoint,
167
- request_id: get(response, "_request_id") ?? get(response, "response_id") ?? get(response, "id"),
448
+ request_id: get(response, "_request_id") ?? get(response, "response_id")
449
+ ?? get(response, "responseId") ?? get(response, "id"),
168
450
  batch: state.request.batch === true,
169
451
  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.
452
+ // Explicit false is the sensitive-operation opt-out; hosted ingestion
453
+ // otherwise preserves content that is present.
172
454
  content_opted_in: captureText,
173
455
  request_json: request.value,
174
456
  response_text: output.value,
175
- text_truncated: request.truncated || output.truncated,
457
+ text_truncated: request.truncated || output.truncated || toolTruncated,
176
458
  stream: extra.stream ?? false,
177
459
  ttft_ms: extra.ttftMs,
178
460
  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,4 +1,4 @@
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
4
  export interface MetergraphOptions {
@@ -51,5 +51,5 @@ export declare function wrapHandler<TArgs extends unknown[], TResult>(handler: (
51
51
  */
52
52
  export declare function wrap<T extends Record<PropertyKey, any>>(client: T, provider?: "openai" | "anthropic" | "google"): T;
53
53
  export declare const wrapClient: typeof wrap;
54
- export { route, setSession, setTags, track };
55
- export type { RouteOptions, TransportMode };
54
+ export { route, setSession, setTags, trace, track };
55
+ export type { RouteOptions, TraceOptions, TransportMode };
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
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";
@@ -44,12 +44,12 @@ export function init(options = {}) {
44
44
  mode: options.transport ?? "auto",
45
45
  });
46
46
  setCaptureRuntime(new CaptureRuntime(transport, {
47
- captureText: options.captureText ?? envBool("METERGRAPH_CAPTURE_TEXT", false),
47
+ captureText: options.captureText ?? envBool("METERGRAPH_CAPTURE_TEXT", true),
48
48
  redact: options.redact,
49
49
  appRoot: options.appRoot ?? (typeof process === "undefined" ? "" : process.cwd()),
50
50
  skipFrames: options.skipFrames ?? [],
51
51
  environment: options.environment ?? env("METERGRAPH_ENV"),
52
- textMaxBytes: Number(env("METERGRAPH_TEXT_MAX_BYTES") ?? 100_000),
52
+ textMaxBytes: Math.min(100 * 1024, Math.max(1, Number(env("METERGRAPH_TEXT_MAX_BYTES") ?? 100 * 1024))),
53
53
  }));
54
54
  config = new ConfigPoller(token, ingestUrl, options.configPollMs, options.configHardTtlMs);
55
55
  }
@@ -138,4 +138,4 @@ export function wrap(client, provider) {
138
138
  return wrapProvider(client, provider);
139
139
  }
140
140
  export const wrapClient = wrap;
141
- export { route, setSession, setTags, track };
141
+ 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;
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.0";
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const SDK_VERSION = "0.2.1";
1
+ export const SDK_VERSION = "0.3.0";
package/dist/wrap.js CHANGED
@@ -198,8 +198,10 @@ function streamProxy(stream, state, capture) {
198
198
  let last;
199
199
  let ttftMs;
200
200
  const parts = [];
201
+ const chunks = [];
201
202
  const observe = (chunk) => {
202
203
  last = chunk;
204
+ chunks.push(chunk);
203
205
  const text = chunkText(chunk);
204
206
  if (text) {
205
207
  ttftMs ??= Math.round(performance.now() - state.started);
@@ -214,6 +216,7 @@ function streamProxy(stream, state, capture) {
214
216
  stream: true,
215
217
  ttftMs,
216
218
  responseText: parts.join("") || undefined,
219
+ responseChunks: chunks,
217
220
  });
218
221
  };
219
222
  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.0",
4
4
  "description": "Fire-and-forget LLM spend capture for Metergraph",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -14,6 +14,7 @@
14
14
  "files": ["dist", "README.md"],
15
15
  "scripts": {
16
16
  "build": "tsc -p tsconfig.json",
17
+ "prepack": "npm run build",
17
18
  "test": "npm run build && node --test test/*.test.mjs"
18
19
  },
19
20
  "engines": {