metergraph 0.6.2 → 0.6.4
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/dist/vercel-ai.js +41 -20
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/dist/wrap.js +140 -30
- package/package.json +1 -1
package/dist/vercel-ai.js
CHANGED
|
@@ -82,6 +82,8 @@ function wrapStreamResult(result, capture, state, started) {
|
|
|
82
82
|
let streamError;
|
|
83
83
|
const content = [];
|
|
84
84
|
const responseMetadata = {};
|
|
85
|
+
// Telemetry inspection of one part. Reads part fields, so callers run it
|
|
86
|
+
// fail-open; a fault must not block delivery or become a stream error.
|
|
85
87
|
const observe = (part) => {
|
|
86
88
|
if (firstOutputAt === undefined && outputChunk(part))
|
|
87
89
|
firstOutputAt = performance.now();
|
|
@@ -101,19 +103,36 @@ function wrapStreamResult(result, capture, state, started) {
|
|
|
101
103
|
streamError = part.error ?? new Error("AI SDK stream error");
|
|
102
104
|
}
|
|
103
105
|
};
|
|
106
|
+
const observeSafely = (part) => {
|
|
107
|
+
try {
|
|
108
|
+
observe(part);
|
|
109
|
+
}
|
|
110
|
+
catch {
|
|
111
|
+
// Telemetry inspection fault: skip this part, deliver it unchanged.
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
// End-of-stream response assembly reads finish-part fields, so it is telemetry
|
|
115
|
+
// and fail-open; a partial or lost trace is acceptable.
|
|
116
|
+
const assembleResponse = () => {
|
|
117
|
+
try {
|
|
118
|
+
if (text)
|
|
119
|
+
content.unshift({ type: "text", text });
|
|
120
|
+
return {
|
|
121
|
+
content,
|
|
122
|
+
usage: finishPart?.usage,
|
|
123
|
+
finishReason: finishPart?.finishReason,
|
|
124
|
+
response: {
|
|
125
|
+
...responseMetadata,
|
|
126
|
+
...(result.response ?? {}),
|
|
127
|
+
},
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
catch {
|
|
131
|
+
return undefined;
|
|
132
|
+
}
|
|
133
|
+
};
|
|
104
134
|
const finish = (status, error = streamError) => {
|
|
105
|
-
|
|
106
|
-
content.unshift({ type: "text", text });
|
|
107
|
-
const response = {
|
|
108
|
-
content,
|
|
109
|
-
usage: finishPart?.usage,
|
|
110
|
-
finishReason: finishPart?.finishReason,
|
|
111
|
-
response: {
|
|
112
|
-
...responseMetadata,
|
|
113
|
-
...(result.response ?? {}),
|
|
114
|
-
},
|
|
115
|
-
};
|
|
116
|
-
finishCapture(capture, state, response, {
|
|
135
|
+
finishCapture(capture, state, assembleResponse(), {
|
|
117
136
|
error,
|
|
118
137
|
status,
|
|
119
138
|
stream: true,
|
|
@@ -123,20 +142,22 @@ function wrapStreamResult(result, capture, state, started) {
|
|
|
123
142
|
};
|
|
124
143
|
const stream = new ReadableStream({
|
|
125
144
|
async pull(controller) {
|
|
145
|
+
let next;
|
|
126
146
|
try {
|
|
127
|
-
|
|
128
|
-
if (next.done) {
|
|
129
|
-
finish();
|
|
130
|
-
controller.close();
|
|
131
|
-
return;
|
|
132
|
-
}
|
|
133
|
-
observe(next.value);
|
|
134
|
-
controller.enqueue(next.value);
|
|
147
|
+
next = await reader.read();
|
|
135
148
|
}
|
|
136
149
|
catch (error) {
|
|
137
150
|
finish("error", error);
|
|
138
151
|
controller.error(error);
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
if (next.done) {
|
|
155
|
+
finish();
|
|
156
|
+
controller.close();
|
|
157
|
+
return;
|
|
139
158
|
}
|
|
159
|
+
observeSafely(next.value);
|
|
160
|
+
controller.enqueue(next.value);
|
|
140
161
|
},
|
|
141
162
|
async cancel(reason) {
|
|
142
163
|
try {
|
package/dist/version.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const SDK_VERSION = "0.6.
|
|
1
|
+
export declare const SDK_VERSION = "0.6.4";
|
package/dist/version.js
CHANGED
package/dist/wrap.js
CHANGED
|
@@ -31,6 +31,26 @@ function requestFrom(args) {
|
|
|
31
31
|
function get(value, key) {
|
|
32
32
|
return value && typeof value === "object" ? value[key] : undefined;
|
|
33
33
|
}
|
|
34
|
+
// Capture is fail-open: a start() fault drops instrumentation for the call, a
|
|
35
|
+
// finish() fault drops the row. Neither may reach the provider path.
|
|
36
|
+
function startCapture(capture, provider, endpoint, request, stack) {
|
|
37
|
+
try {
|
|
38
|
+
return capture.start(provider, endpoint, request, stack);
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
return undefined;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
function finishCapture(capture, state, response, extra = {}) {
|
|
45
|
+
if (!state)
|
|
46
|
+
return;
|
|
47
|
+
try {
|
|
48
|
+
capture.finish(state, response, extra);
|
|
49
|
+
}
|
|
50
|
+
catch {
|
|
51
|
+
// Telemetry finalization is fail-open.
|
|
52
|
+
}
|
|
53
|
+
}
|
|
34
54
|
function markBatchItem(key) {
|
|
35
55
|
if (seenBatchItems.has(key))
|
|
36
56
|
return false;
|
|
@@ -204,7 +224,7 @@ function patchAnthropicBatchResults(owner) {
|
|
|
204
224
|
owner.results = wrapped;
|
|
205
225
|
return true;
|
|
206
226
|
}
|
|
207
|
-
function streamProxy(stream, state, capture) {
|
|
227
|
+
function streamProxy(stream, state, capture, suppressUsageOnly) {
|
|
208
228
|
let last;
|
|
209
229
|
let ttftMs;
|
|
210
230
|
const parts = [];
|
|
@@ -244,7 +264,7 @@ function streamProxy(stream, state, capture) {
|
|
|
244
264
|
return chunk;
|
|
245
265
|
};
|
|
246
266
|
const finish = (response = last, error, status) => {
|
|
247
|
-
capture
|
|
267
|
+
finishCapture(capture, state, response, {
|
|
248
268
|
error,
|
|
249
269
|
status,
|
|
250
270
|
stream: true,
|
|
@@ -262,41 +282,57 @@ function streamProxy(stream, state, capture) {
|
|
|
262
282
|
return async function* () {
|
|
263
283
|
try {
|
|
264
284
|
for await (const chunk of target) {
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
&& Array.isArray(chunk?.choices)
|
|
269
|
-
&& chunk.choices.length === 0
|
|
270
|
-
&& chunk.usage != null;
|
|
271
|
-
if (!usageOnly)
|
|
272
|
-
yield value;
|
|
273
|
-
}
|
|
274
|
-
let final = last;
|
|
275
|
-
if (typeof target.finalMessage === "function") {
|
|
285
|
+
// Telemetry boundary: observation and usage-only classification.
|
|
286
|
+
// An ordinary telemetry fault yields the raw chunk and continues.
|
|
287
|
+
let usageOnly = false;
|
|
276
288
|
try {
|
|
277
|
-
|
|
289
|
+
observe(chunk);
|
|
290
|
+
// Suppress the usage-only final chunk only when MeterGraph
|
|
291
|
+
// injected stream_options.include_usage; a caller who asked for
|
|
292
|
+
// it keeps their chunk. Capture reads usage from it either way.
|
|
293
|
+
usageOnly = suppressUsageOnly
|
|
294
|
+
&& state.provider === "openai"
|
|
295
|
+
&& state.endpoint === "chat.completions"
|
|
296
|
+
&& Array.isArray(chunk?.choices)
|
|
297
|
+
&& chunk.choices.length === 0
|
|
298
|
+
&& chunk.usage != null;
|
|
278
299
|
}
|
|
279
|
-
catch {
|
|
300
|
+
catch {
|
|
301
|
+
yield chunk;
|
|
302
|
+
continue;
|
|
303
|
+
}
|
|
304
|
+
if (!usageOnly)
|
|
305
|
+
yield chunk;
|
|
280
306
|
}
|
|
281
|
-
finish(final);
|
|
282
307
|
}
|
|
283
308
|
catch (error) {
|
|
309
|
+
// Provider iteration error: finalize fail-open, keep its identity.
|
|
284
310
|
finish(last, error);
|
|
285
311
|
throw error;
|
|
286
312
|
}
|
|
313
|
+
// Exhaustion: enrich with finalMessage when present, then finalize.
|
|
314
|
+
let final = last;
|
|
315
|
+
if (typeof target.finalMessage === "function") {
|
|
316
|
+
try {
|
|
317
|
+
final = await target.finalMessage();
|
|
318
|
+
}
|
|
319
|
+
catch { /* enrichment is best-effort */ }
|
|
320
|
+
}
|
|
321
|
+
finish(final);
|
|
287
322
|
};
|
|
288
323
|
}
|
|
289
324
|
if (property === "finalMessage" && typeof target.finalMessage === "function") {
|
|
290
325
|
return async (...args) => {
|
|
326
|
+
let final;
|
|
291
327
|
try {
|
|
292
|
-
|
|
293
|
-
finish(final);
|
|
294
|
-
return final;
|
|
328
|
+
final = await target.finalMessage(...args);
|
|
295
329
|
}
|
|
296
330
|
catch (error) {
|
|
297
331
|
finish(last, error);
|
|
298
332
|
throw error;
|
|
299
333
|
}
|
|
334
|
+
finish(final);
|
|
335
|
+
return final;
|
|
300
336
|
};
|
|
301
337
|
}
|
|
302
338
|
if (property === "close" || property === "abort") {
|
|
@@ -304,12 +340,20 @@ function streamProxy(stream, state, capture) {
|
|
|
304
340
|
if (typeof original !== "function")
|
|
305
341
|
return original;
|
|
306
342
|
return (...args) => {
|
|
343
|
+
let returned;
|
|
344
|
+
let error;
|
|
345
|
+
let threw = false;
|
|
307
346
|
try {
|
|
308
|
-
|
|
347
|
+
returned = original.apply(target, args);
|
|
309
348
|
}
|
|
310
|
-
|
|
311
|
-
|
|
349
|
+
catch (caught) {
|
|
350
|
+
threw = true;
|
|
351
|
+
error = caught;
|
|
312
352
|
}
|
|
353
|
+
finish(last, undefined, "abandoned");
|
|
354
|
+
if (threw)
|
|
355
|
+
throw error;
|
|
356
|
+
return returned;
|
|
313
357
|
};
|
|
314
358
|
}
|
|
315
359
|
const value = Reflect.get(target, property, receiver);
|
|
@@ -317,6 +361,49 @@ function streamProxy(stream, state, capture) {
|
|
|
317
361
|
},
|
|
318
362
|
});
|
|
319
363
|
}
|
|
364
|
+
// The provider SDKs (openai, @anthropic-ai/sdk) return an APIPromise — a
|
|
365
|
+
// Promise subclass carrying helpers like .withResponse()/.asResponse(). Chaining
|
|
366
|
+
// .then() would resolve through Promise species into a plain Promise and drop
|
|
367
|
+
// those helpers, so wrap the original in a proxy that routes await/then/catch/
|
|
368
|
+
// finally through capture while forwarding every helper to the untouched
|
|
369
|
+
// original. The provider memoizes its single in-flight request, so preserving
|
|
370
|
+
// the helpers does not issue it twice.
|
|
371
|
+
function preserveProviderPromise(target, complete, fail) {
|
|
372
|
+
let chained;
|
|
373
|
+
const captured = () => (chained ??= target.then(complete, fail));
|
|
374
|
+
return new Proxy(target, {
|
|
375
|
+
get(t, property, receiver) {
|
|
376
|
+
if (property === "then")
|
|
377
|
+
return (onF, onR) => captured().then(onF, onR);
|
|
378
|
+
if (property === "catch")
|
|
379
|
+
return (onR) => captured().catch(onR);
|
|
380
|
+
if (property === "finally")
|
|
381
|
+
return (onFin) => captured().finally(onFin);
|
|
382
|
+
if (property === "withResponse" && typeof t.withResponse === "function") {
|
|
383
|
+
// .withResponse() carries the parsed body (or a stream) plus the raw
|
|
384
|
+
// Response. Route data through complete() so a streaming body stays
|
|
385
|
+
// instrumented and capture fires exactly once, then hand back a fresh
|
|
386
|
+
// envelope — the provider's own object is left untouched.
|
|
387
|
+
return async (...a) => {
|
|
388
|
+
let envelope;
|
|
389
|
+
try {
|
|
390
|
+
envelope = await t.withResponse(...a);
|
|
391
|
+
}
|
|
392
|
+
catch (error) {
|
|
393
|
+
fail(error);
|
|
394
|
+
}
|
|
395
|
+
return { ...envelope, data: complete(envelope.data) };
|
|
396
|
+
};
|
|
397
|
+
}
|
|
398
|
+
// Every other helper (.asResponse(), ._thenUnwrap(), ...) is forwarded to
|
|
399
|
+
// the untouched original. .asResponse() intentionally returns the raw
|
|
400
|
+
// Response with no parsed body, so there is nothing for MeterGraph to
|
|
401
|
+
// normalize on that path; capture stays with the parsing paths above.
|
|
402
|
+
const value = Reflect.get(t, property, receiver);
|
|
403
|
+
return typeof value === "function" ? value.bind(t) : value;
|
|
404
|
+
},
|
|
405
|
+
});
|
|
406
|
+
}
|
|
320
407
|
function patch(owner, method, provider, endpoint) {
|
|
321
408
|
if (!owner || typeof owner[method] !== "function")
|
|
322
409
|
return false;
|
|
@@ -347,40 +434,63 @@ function patch(owner, method, provider, endpoint) {
|
|
|
347
434
|
const incoming = requestFrom(args);
|
|
348
435
|
const patchUsage = typeof process === "undefined"
|
|
349
436
|
|| process.env.METERGRAPH_PATCH_STREAM_USAGE !== "0";
|
|
437
|
+
let injectedUsage = false;
|
|
350
438
|
if (provider === "openai"
|
|
351
439
|
&& endpoint === "chat.completions"
|
|
352
440
|
&& incoming.stream === true
|
|
353
441
|
&& incoming.stream_options == null
|
|
354
442
|
&& patchUsage) {
|
|
355
443
|
args = [{ ...incoming, stream_options: { include_usage: true } }, ...args.slice(1)];
|
|
444
|
+
injectedUsage = true;
|
|
445
|
+
}
|
|
446
|
+
const state = startCapture(capture, provider, endpoint, requestFrom(args), new Error().stack);
|
|
447
|
+
if (!state) {
|
|
448
|
+
// start faulted: invoke the provider once, return its result unwrapped.
|
|
449
|
+
reentrantOwners.add(owner);
|
|
450
|
+
try {
|
|
451
|
+
return original.apply(owner, args);
|
|
452
|
+
}
|
|
453
|
+
finally {
|
|
454
|
+
reentrantOwners.delete(owner);
|
|
455
|
+
}
|
|
356
456
|
}
|
|
357
|
-
const state = capture.start(provider, endpoint, requestFrom(args), new Error().stack);
|
|
358
457
|
let result;
|
|
359
458
|
reentrantOwners.add(owner);
|
|
360
459
|
try {
|
|
361
460
|
result = original.apply(owner, args);
|
|
362
461
|
}
|
|
363
462
|
catch (error) {
|
|
364
|
-
capture
|
|
463
|
+
finishCapture(capture, state, undefined, { error });
|
|
365
464
|
throw error;
|
|
366
465
|
}
|
|
367
466
|
finally {
|
|
368
467
|
reentrantOwners.delete(owner);
|
|
369
468
|
}
|
|
469
|
+
// complete()/fail() finalize capture at most once regardless of which
|
|
470
|
+
// consumption path (await, .withResponse(), ...) reaches the value first.
|
|
471
|
+
let settled = false;
|
|
472
|
+
let streamValue;
|
|
370
473
|
const complete = (response) => {
|
|
371
474
|
const request = requestFrom(args);
|
|
372
475
|
const streaming = endpoint.endsWith(".stream") || request.stream === true;
|
|
373
476
|
if (streaming && response && (response[Symbol.asyncIterator] || response.finalMessage)) {
|
|
374
|
-
return streamProxy(response, state, capture);
|
|
477
|
+
return (streamValue ??= streamProxy(response, state, capture, injectedUsage));
|
|
478
|
+
}
|
|
479
|
+
if (!settled) {
|
|
480
|
+
settled = true;
|
|
481
|
+
finishCapture(capture, state, response);
|
|
375
482
|
}
|
|
376
|
-
capture.finish(state, response);
|
|
377
483
|
return response;
|
|
378
484
|
};
|
|
485
|
+
const fail = (error) => {
|
|
486
|
+
if (!settled) {
|
|
487
|
+
settled = true;
|
|
488
|
+
finishCapture(capture, state, undefined, { error });
|
|
489
|
+
}
|
|
490
|
+
throw error;
|
|
491
|
+
};
|
|
379
492
|
if (result && typeof result.then === "function") {
|
|
380
|
-
return result
|
|
381
|
-
capture.finish(state, undefined, { error });
|
|
382
|
-
throw error;
|
|
383
|
-
});
|
|
493
|
+
return preserveProviderPromise(result, complete, fail);
|
|
384
494
|
}
|
|
385
495
|
return complete(result);
|
|
386
496
|
};
|