metergraph 0.6.14 → 0.6.16
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 +3 -1
- package/dist/capture.js +2 -0
- package/dist/retrieval.d.ts +1 -0
- package/dist/retrieval.js +84 -0
- package/dist/transport.d.ts +1 -0
- package/dist/transport.js +19 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/dist/wrap.d.ts +1 -1
- package/dist/wrap.js +8 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -233,7 +233,7 @@ outcome.result; // the provider response
|
|
|
233
233
|
outcome.metadata.batch_outcome; // "completed" | "failed" | "expired" | "pending_at_deadline"
|
|
234
234
|
```
|
|
235
235
|
|
|
236
|
-
`provider` is `"openai" | "anthropic" | "google"`,
|
|
236
|
+
`provider` is `"openai" | "anthropic" | "google"`, never inferred from the client instance. Perplexity capture support does not add a Perplexity batch adapter. A request with `stream: true` is rejected before any provider call. A request carrying `tools` is rejected unless `allowDuplicateToolCallPlans: true` is also set, acknowledging that the batch result and the direct fallback are independent provider executions that may each choose a different tool-call plan. `acceptDuplicateProviderExecution` must be exactly `true` — there is no default and no environment-variable override, and a missed deadline can execute (and bill) the same request twice. Neither the batch nor the direct path executes a tool call automatically; the caller receives the tool-call plan in `outcome.result` and is responsible for executing it, exactly as with a normal (non-batch-first) provider response.
|
|
237
237
|
|
|
238
238
|
`batchFirst()` is not integrated with `wrap()`'s capture/telemetry pipeline; its result and metadata are returned directly to the caller, never enqueued for delivery. The background poll that watches a losing batch for late telemetry uses an ordinary `setTimeout`, not an unref'd one — after a deadline-triggered direct fallback it keeps the Node process alive until the batch reaches a terminal state, which can take up to 24 hours; this is currently unmitigated, so a short-lived script or CLI process should not assume it exits promptly after a fallback.
|
|
239
239
|
|
|
@@ -277,3 +277,5 @@ calls made outside these supported paths.
|
|
|
277
277
|
### Service tier evidence
|
|
278
278
|
|
|
279
279
|
Calls record `service_tier` separately from `requested_service_tier`: requesting Flex does not prove it was served. Capture reads OpenAI's response tier, Anthropic's usage tier, and Google's usage tier or traffic type, retaining evidence across trailing stream events. Successful Batch API results record `batch` from the transport, without inventing a requested tier. Missing response evidence stays unknown.
|
|
280
|
+
|
|
281
|
+
Search usage is retained independently of text capture: OpenAI search-call counts, Anthropic server-tool usage, and Google grounding-query counts. Perplexity Responses capture uses invocation counters, with search-result items as a fallback. Clients using `https://api.perplexity.ai` are detected automatically; use `wrap(client, { provider: "perplexity" })` when a gateway hides the provider. Perplexity Agent and legacy Sonar calls are identified separately. Agent search type is recorded when explicitly set on the tool; missing evidence remains unknown. Prices are resolved by the receiver.
|
package/dist/capture.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.CaptureRuntime = exports.DEFAULT_TEXT_MAX_BYTES = void 0;
|
|
4
4
|
exports.chunkText = chunkText;
|
|
5
|
+
const retrieval_js_1 = require("./retrieval.js");
|
|
5
6
|
const model_evidence_js_1 = require("./model-evidence.js");
|
|
6
7
|
const node_crypto_1 = require("node:crypto");
|
|
7
8
|
const context_js_1 = require("./context.js");
|
|
@@ -816,6 +817,7 @@ class CaptureRuntime {
|
|
|
816
817
|
provider: state.provider,
|
|
817
818
|
model: state.request.model,
|
|
818
819
|
...usage(response),
|
|
820
|
+
...(0, retrieval_js_1.retrievalUsage)(state.provider, state.endpoint, state.request, response, extra.responseChunks),
|
|
819
821
|
// The Batch API confirms billing even when its body reports a default tier.
|
|
820
822
|
service_tier: state.endpoint.startsWith("batch.") && response != null
|
|
821
823
|
&& !extra.error && extra.status !== "error"
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function retrievalUsage(provider: string, endpoint: string, request: Record<string, unknown>, response: unknown, chunks?: unknown[]): Record<string, unknown>;
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.retrievalUsage = retrievalUsage;
|
|
4
|
+
// Capture native usage only. Billing units and prices belong to the receiver.
|
|
5
|
+
function get(value, key) {
|
|
6
|
+
return value && typeof value === 'object' ? value[key] : undefined;
|
|
7
|
+
}
|
|
8
|
+
function count(value) {
|
|
9
|
+
return typeof value === 'number' && Number.isSafeInteger(value) && value >= 0 ? value : undefined;
|
|
10
|
+
}
|
|
11
|
+
function bodies(response, chunks) {
|
|
12
|
+
return [...chunks.flatMap(chunk => [get(chunk, 'message'), get(chunk, 'response'), chunk]), response];
|
|
13
|
+
}
|
|
14
|
+
function outputItems(response, chunks) {
|
|
15
|
+
const direct = get(response, 'output');
|
|
16
|
+
if (Array.isArray(direct))
|
|
17
|
+
return direct;
|
|
18
|
+
for (const chunk of [...chunks].reverse()) {
|
|
19
|
+
if (['response.completed', 'response.incomplete', 'response.failed'].includes(get(chunk, 'type'))) {
|
|
20
|
+
const output = get(get(chunk, 'response'), 'output');
|
|
21
|
+
if (Array.isArray(output))
|
|
22
|
+
return output;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
const items = new Map();
|
|
26
|
+
for (const chunk of chunks) {
|
|
27
|
+
if (get(chunk, 'type') === 'response.output_item.done') {
|
|
28
|
+
const item = get(chunk, 'item');
|
|
29
|
+
const id = get(item, 'id') ?? get(chunk, 'output_index');
|
|
30
|
+
if (id !== undefined)
|
|
31
|
+
items.set(id, item);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return items.size ? [...items.values()] : undefined;
|
|
35
|
+
}
|
|
36
|
+
function retrievalUsage(provider, endpoint, request, response, chunks = []) {
|
|
37
|
+
const values = bodies(response, chunks);
|
|
38
|
+
if (provider === 'anthropic') {
|
|
39
|
+
const server = {};
|
|
40
|
+
for (const body of values) {
|
|
41
|
+
const usage = get(get(body, 'usage'), 'server_tool_use');
|
|
42
|
+
for (const key of ['web_search_requests', 'web_fetch_requests']) {
|
|
43
|
+
const value = count(get(usage, key));
|
|
44
|
+
if (value !== undefined)
|
|
45
|
+
server[key] = value;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return Object.keys(server).length ? { server_tool_use: server } : {};
|
|
49
|
+
}
|
|
50
|
+
if (provider === 'google') {
|
|
51
|
+
const queries = new Set();
|
|
52
|
+
let observed = false;
|
|
53
|
+
for (const body of values)
|
|
54
|
+
for (const candidate of Array.isArray(get(body, 'candidates')) ? get(body, 'candidates') : []) {
|
|
55
|
+
const metadata = get(candidate, 'groundingMetadata') ?? get(candidate, 'grounding_metadata');
|
|
56
|
+
const items = get(metadata, 'webSearchQueries') ?? get(metadata, 'web_search_queries');
|
|
57
|
+
if (!Array.isArray(items))
|
|
58
|
+
continue;
|
|
59
|
+
observed = true;
|
|
60
|
+
for (const query of items)
|
|
61
|
+
if (typeof query === 'string' && query.trim())
|
|
62
|
+
queries.add(query.trim());
|
|
63
|
+
}
|
|
64
|
+
return observed ? { grounding_queries: queries.size, grounded_prompt: queries.size > 0 ? 1 : 0 } : {};
|
|
65
|
+
}
|
|
66
|
+
if (provider !== 'openai' && provider !== 'perplexity')
|
|
67
|
+
return {};
|
|
68
|
+
const items = outputItems(response, chunks);
|
|
69
|
+
if (provider === 'openai') {
|
|
70
|
+
return items ? { web_search_calls: items.filter(item => get(item, 'type') === 'web_search_call').length } : {};
|
|
71
|
+
}
|
|
72
|
+
if (!endpoint.includes('responses'))
|
|
73
|
+
return { retrieval_api: 'sonar' };
|
|
74
|
+
let searches;
|
|
75
|
+
for (const body of values) {
|
|
76
|
+
const usage = get(get(get(get(body, 'usage'), 'tool_calls_details'), 'search_web'), 'invocation');
|
|
77
|
+
searches = count(usage) ?? searches;
|
|
78
|
+
}
|
|
79
|
+
searches ??= items?.filter(item => get(item, 'type') === 'search_results').length;
|
|
80
|
+
const tools = Array.isArray(request.tools) ? request.tools : [];
|
|
81
|
+
const types = tools.filter(tool => get(tool, 'type') === 'web_search').map(tool => get(tool, 'search_type'));
|
|
82
|
+
const searchType = types.length && types.every(type => type === types[0]) && ['web', 'fast'].includes(types[0]) ? types[0] : undefined;
|
|
83
|
+
return { retrieval_api: 'agent', ...(searches !== undefined ? { web_search_calls: searches } : {}), ...(searchType ? { search_type: searchType } : {}) };
|
|
84
|
+
}
|
package/dist/transport.d.ts
CHANGED
package/dist/transport.js
CHANGED
|
@@ -28,6 +28,7 @@ class Transport {
|
|
|
28
28
|
scheduled = false;
|
|
29
29
|
inFlight = Promise.resolve();
|
|
30
30
|
fatal = false;
|
|
31
|
+
upload;
|
|
31
32
|
dropped = 0;
|
|
32
33
|
errors = 0;
|
|
33
34
|
retryAt = 0;
|
|
@@ -127,9 +128,18 @@ class Transport {
|
|
|
127
128
|
await this.deliver(rows.slice(midpoint));
|
|
128
129
|
return;
|
|
129
130
|
}
|
|
131
|
+
if (this.fatal) {
|
|
132
|
+
this.dropped += rows.length;
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
const controller = new AbortController();
|
|
136
|
+
this.upload = controller;
|
|
137
|
+
const timeout = setTimeout(() => controller.abort(), 30_000);
|
|
138
|
+
timeout.unref?.();
|
|
130
139
|
try {
|
|
131
140
|
const response = await fetch(`${this.baseUrl.replace(/\/$/, "")}/v1/ingest`, {
|
|
132
141
|
method: "POST",
|
|
142
|
+
signal: controller.signal,
|
|
133
143
|
headers: {
|
|
134
144
|
Authorization: `Bearer ${token}`,
|
|
135
145
|
"Content-Type": "application/json",
|
|
@@ -171,11 +181,20 @@ class Transport {
|
|
|
171
181
|
this.markTransientFailure(rows.length);
|
|
172
182
|
this.failureLog.report("transport_error", `ingest request to ${this.baseUrl} failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
173
183
|
}
|
|
184
|
+
finally {
|
|
185
|
+
clearTimeout(timeout);
|
|
186
|
+
if (this.upload === controller)
|
|
187
|
+
this.upload = undefined;
|
|
188
|
+
}
|
|
174
189
|
}
|
|
175
190
|
async shutdown() {
|
|
176
191
|
if (this.timer)
|
|
177
192
|
clearInterval(this.timer);
|
|
178
193
|
await this.flush();
|
|
194
|
+
this.fatal = true;
|
|
195
|
+
this.dropped += this.queue.length;
|
|
196
|
+
this.queue.length = 0;
|
|
197
|
+
this.upload?.abort();
|
|
179
198
|
}
|
|
180
199
|
}
|
|
181
200
|
exports.Transport = Transport;
|
package/dist/version.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const SDK_VERSION = "0.6.
|
|
1
|
+
export declare const SDK_VERSION = "0.6.16";
|
package/dist/version.js
CHANGED
package/dist/wrap.d.ts
CHANGED
|
@@ -14,7 +14,7 @@ export declare const OPENAI_SEAMS: Seam[];
|
|
|
14
14
|
export declare const ANTHROPIC_SEAMS: Seam[];
|
|
15
15
|
export declare const GOOGLE_SEAMS: Seam[];
|
|
16
16
|
export declare const SEAM_TABLES: Record<string, Seam[]>;
|
|
17
|
-
export type ProviderName = "openai" | "anthropic" | "google";
|
|
17
|
+
export type ProviderName = "openai" | "anthropic" | "google" | "perplexity";
|
|
18
18
|
export type WrapOptions = {
|
|
19
19
|
provider?: ProviderName;
|
|
20
20
|
gateway?: never;
|
package/dist/wrap.js
CHANGED
|
@@ -699,8 +699,14 @@ exports.SEAM_TABLES = {
|
|
|
699
699
|
function detectProvider(client) {
|
|
700
700
|
if (client.models?.generateContent)
|
|
701
701
|
return "google";
|
|
702
|
-
if (client.chat || client.responses)
|
|
702
|
+
if (client.chat || client.responses) {
|
|
703
|
+
try {
|
|
704
|
+
if (new URL(String(client.baseURL)).hostname === "api.perplexity.ai")
|
|
705
|
+
return "perplexity";
|
|
706
|
+
}
|
|
707
|
+
catch { }
|
|
703
708
|
return "openai";
|
|
709
|
+
}
|
|
704
710
|
return "anthropic";
|
|
705
711
|
}
|
|
706
712
|
function resolveSeam(client, path) {
|
|
@@ -714,7 +720,7 @@ function resolveSeam(client, path) {
|
|
|
714
720
|
}
|
|
715
721
|
function applySeams(client, provider, gatewayName) {
|
|
716
722
|
const patched = [];
|
|
717
|
-
for (const seam of exports.SEAM_TABLES[provider] ?? []) {
|
|
723
|
+
for (const seam of exports.SEAM_TABLES[provider === "perplexity" ? "openai" : provider] ?? []) {
|
|
718
724
|
let owner;
|
|
719
725
|
try {
|
|
720
726
|
owner = resolveSeam(client, seam.path);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "metergraph",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.16",
|
|
4
4
|
"description": "Zero-dependency TypeScript SDK for function-level LLM cost attribution across major LLM providers and frameworks, including OpenAI, Anthropic, Gemini, and Vercel AI SDK.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|