metergraph 0.6.21 → 0.6.22
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/perplexity-retrieval.d.ts +1 -0
- package/dist/perplexity-retrieval.js +26 -0
- package/dist/retrieval-common.d.ts +2 -0
- package/dist/retrieval-common.js +11 -0
- package/dist/retrieval.js +16 -31
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function perplexityRetrievalUsage(endpoint: string, request: Record<string, unknown>, values: unknown[], items: unknown[] | undefined): Record<string, unknown>;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.perplexityRetrievalUsage = perplexityRetrievalUsage;
|
|
4
|
+
// Perplexity Sonar and Agent search usage and request defaults.
|
|
5
|
+
const retrieval_common_js_1 = require("./retrieval-common.js");
|
|
6
|
+
function perplexityRetrievalUsage(endpoint, request, values, items) {
|
|
7
|
+
if (!endpoint.includes('responses'))
|
|
8
|
+
return { retrieval_api: 'sonar' };
|
|
9
|
+
let searches;
|
|
10
|
+
for (const body of values) {
|
|
11
|
+
const usage = (0, retrieval_common_js_1.get)((0, retrieval_common_js_1.get)((0, retrieval_common_js_1.get)((0, retrieval_common_js_1.get)(body, 'usage'), 'tool_calls_details'), 'search_web'), 'invocation');
|
|
12
|
+
searches = (0, retrieval_common_js_1.count)(usage) ?? searches;
|
|
13
|
+
}
|
|
14
|
+
searches ??= items?.filter(item => (0, retrieval_common_js_1.get)(item, 'type') === 'search_results').length;
|
|
15
|
+
const extra = request.extra_body;
|
|
16
|
+
if (extra && typeof extra === 'object' && !Array.isArray(extra))
|
|
17
|
+
request = { ...request, ...extra };
|
|
18
|
+
const tools = Array.isArray(request.tools) ? request.tools : [];
|
|
19
|
+
// Active effort bundles use fast search; pinned versions and profiles are not inferred.
|
|
20
|
+
const activeFast = typeof request.effort === 'string' && ['fast', 'low', 'medium', 'high', 'xhigh'].some(level => request.effort === level || request.effort === `${level}@latest`);
|
|
21
|
+
const defaultType = 'profile' in request ? undefined : activeFast ? 'fast' : !('effort' in request) ? 'web' : undefined;
|
|
22
|
+
const types = request.tools === undefined && defaultType === 'fast' ? ['fast'] : tools.filter(tool => (0, retrieval_common_js_1.get)(tool, 'type') === 'web_search')
|
|
23
|
+
.map(tool => (0, retrieval_common_js_1.get)(tool, 'search_type') === undefined ? defaultType : (0, retrieval_common_js_1.get)(tool, 'search_type'));
|
|
24
|
+
const searchType = types.length && types.every(type => type === types[0]) && ['web', 'fast'].includes(types[0]) ? types[0] : undefined;
|
|
25
|
+
return { retrieval_api: 'agent', ...(searches !== undefined ? { web_search_calls: searches } : {}), ...(searchType ? { search_type: searchType } : {}) };
|
|
26
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.get = get;
|
|
4
|
+
exports.count = count;
|
|
5
|
+
// Shared accessors for native retrieval usage.
|
|
6
|
+
function get(value, key) {
|
|
7
|
+
return value && typeof value === 'object' ? value[key] : undefined;
|
|
8
|
+
}
|
|
9
|
+
function count(value) {
|
|
10
|
+
return typeof value === 'number' && Number.isSafeInteger(value) && value >= 0 ? value : undefined;
|
|
11
|
+
}
|
package/dist/retrieval.js
CHANGED
|
@@ -2,31 +2,27 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.retrievalUsage = retrievalUsage;
|
|
4
4
|
// Capture native usage only. Billing units and prices belong to the receiver.
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
}
|
|
8
|
-
function count(value) {
|
|
9
|
-
return typeof value === 'number' && Number.isSafeInteger(value) && value >= 0 ? value : undefined;
|
|
10
|
-
}
|
|
5
|
+
const retrieval_common_js_1 = require("./retrieval-common.js");
|
|
6
|
+
const perplexity_retrieval_js_1 = require("./perplexity-retrieval.js");
|
|
11
7
|
function bodies(response, chunks) {
|
|
12
|
-
return [...chunks.flatMap(chunk => [get(chunk, 'message'), get(chunk, 'response'), chunk]), response];
|
|
8
|
+
return [...chunks.flatMap(chunk => [(0, retrieval_common_js_1.get)(chunk, 'message'), (0, retrieval_common_js_1.get)(chunk, 'response'), chunk]), response];
|
|
13
9
|
}
|
|
14
10
|
function outputItems(response, chunks) {
|
|
15
|
-
const direct = get(response, 'output');
|
|
11
|
+
const direct = (0, retrieval_common_js_1.get)(response, 'output');
|
|
16
12
|
if (Array.isArray(direct))
|
|
17
13
|
return direct;
|
|
18
14
|
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');
|
|
15
|
+
if (['response.completed', 'response.incomplete', 'response.failed'].includes((0, retrieval_common_js_1.get)(chunk, 'type'))) {
|
|
16
|
+
const output = (0, retrieval_common_js_1.get)((0, retrieval_common_js_1.get)(chunk, 'response'), 'output');
|
|
21
17
|
if (Array.isArray(output))
|
|
22
18
|
return output;
|
|
23
19
|
}
|
|
24
20
|
}
|
|
25
21
|
const items = new Map();
|
|
26
22
|
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');
|
|
23
|
+
if ((0, retrieval_common_js_1.get)(chunk, 'type') === 'response.output_item.done') {
|
|
24
|
+
const item = (0, retrieval_common_js_1.get)(chunk, 'item');
|
|
25
|
+
const id = (0, retrieval_common_js_1.get)(item, 'id') ?? (0, retrieval_common_js_1.get)(chunk, 'output_index');
|
|
30
26
|
if (id !== undefined)
|
|
31
27
|
items.set(id, item);
|
|
32
28
|
}
|
|
@@ -38,9 +34,9 @@ function retrievalUsage(provider, endpoint, request, response, chunks = []) {
|
|
|
38
34
|
if (provider === 'anthropic') {
|
|
39
35
|
const server = {};
|
|
40
36
|
for (const body of values) {
|
|
41
|
-
const usage = get(get(body, 'usage'), 'server_tool_use');
|
|
37
|
+
const usage = (0, retrieval_common_js_1.get)((0, retrieval_common_js_1.get)(body, 'usage'), 'server_tool_use');
|
|
42
38
|
for (const key of ['web_search_requests', 'web_fetch_requests']) {
|
|
43
|
-
const value = count(get(usage, key));
|
|
39
|
+
const value = (0, retrieval_common_js_1.count)((0, retrieval_common_js_1.get)(usage, key));
|
|
44
40
|
if (value !== undefined)
|
|
45
41
|
server[key] = value;
|
|
46
42
|
}
|
|
@@ -51,9 +47,9 @@ function retrievalUsage(provider, endpoint, request, response, chunks = []) {
|
|
|
51
47
|
const queries = new Set();
|
|
52
48
|
let observed = false;
|
|
53
49
|
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');
|
|
50
|
+
for (const candidate of Array.isArray((0, retrieval_common_js_1.get)(body, 'candidates')) ? (0, retrieval_common_js_1.get)(body, 'candidates') : []) {
|
|
51
|
+
const metadata = (0, retrieval_common_js_1.get)(candidate, 'groundingMetadata') ?? (0, retrieval_common_js_1.get)(candidate, 'grounding_metadata');
|
|
52
|
+
const items = (0, retrieval_common_js_1.get)(metadata, 'webSearchQueries') ?? (0, retrieval_common_js_1.get)(metadata, 'web_search_queries');
|
|
57
53
|
if (!Array.isArray(items))
|
|
58
54
|
continue;
|
|
59
55
|
observed = true;
|
|
@@ -67,18 +63,7 @@ function retrievalUsage(provider, endpoint, request, response, chunks = []) {
|
|
|
67
63
|
return {};
|
|
68
64
|
const items = outputItems(response, chunks);
|
|
69
65
|
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;
|
|
66
|
+
return items ? { web_search_calls: items.filter(item => (0, retrieval_common_js_1.get)(item, 'type') === 'web_search_call').length } : {};
|
|
78
67
|
}
|
|
79
|
-
|
|
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 } : {}) };
|
|
68
|
+
return (0, perplexity_retrieval_js_1.perplexityRetrievalUsage)(endpoint, request, values, items);
|
|
84
69
|
}
|
package/dist/version.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const SDK_VERSION = "0.6.
|
|
1
|
+
export declare const SDK_VERSION = "0.6.22";
|
package/dist/version.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "metergraph",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.22",
|
|
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",
|