@pipelex/sdk 0.16.0 → 0.18.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 +4 -2
- package/dist/artifacts.d.ts +284 -0
- package/dist/artifacts.js +841 -0
- package/dist/artifacts.js.map +1 -0
- package/dist/client.d.ts +82 -25
- package/dist/client.js +99 -22
- package/dist/client.js.map +1 -1
- package/dist/errors.d.ts +69 -3
- package/dist/errors.js +80 -3
- package/dist/errors.js.map +1 -1
- package/dist/index.d.ts +7 -3
- package/dist/index.js +9 -2
- package/dist/index.js.map +1 -1
- package/dist/models.d.ts +49 -1
- package/dist/prepare-inputs.d.ts +72 -43
- package/dist/prepare-inputs.js +218 -81
- package/dist/prepare-inputs.js.map +1 -1
- package/dist/runs.d.ts +36 -5
- package/dist/runs.js.map +1 -1
- package/dist/usage.d.ts +87 -0
- package/dist/usage.js +121 -0
- package/dist/usage.js.map +1 -0
- package/package.json +4 -3
package/dist/usage.js
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Fold a run's usage pair into one summary: run totals, the call count, the assembly error and
|
|
3
|
+
* a per-pipe rollup. See `docs/run-usage.md` for the rules it applies.
|
|
4
|
+
*
|
|
5
|
+
* Pre-contract records, relayed verbatim from artifacts written before the usage contract,
|
|
6
|
+
* carry no `cost` and no `pipe_code`: they count as unrated and unattributed. The legacy
|
|
7
|
+
* `job_metadata` / `unit_costs` fields are relics and are never read.
|
|
8
|
+
*/
|
|
9
|
+
export function summarizeUsage(results) {
|
|
10
|
+
const records = results.tokens_usages;
|
|
11
|
+
const assemblyError = results.usage_assembly_error ?? null;
|
|
12
|
+
if (records == null) {
|
|
13
|
+
return {
|
|
14
|
+
state: "unavailable",
|
|
15
|
+
total_cost_usd: null,
|
|
16
|
+
cost_partial: false,
|
|
17
|
+
tokens: { input: null, output: null },
|
|
18
|
+
calls: 0,
|
|
19
|
+
assembly_error: assemblyError,
|
|
20
|
+
by_pipe: [],
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
if (records.length === 0) {
|
|
24
|
+
return {
|
|
25
|
+
state: "no_inference",
|
|
26
|
+
total_cost_usd: 0,
|
|
27
|
+
cost_partial: false,
|
|
28
|
+
tokens: { input: 0, output: 0 },
|
|
29
|
+
calls: 0,
|
|
30
|
+
assembly_error: assemblyError,
|
|
31
|
+
by_pipe: [],
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
return {
|
|
35
|
+
state: "records",
|
|
36
|
+
...foldRecords(records),
|
|
37
|
+
calls: records.length,
|
|
38
|
+
assembly_error: assemblyError,
|
|
39
|
+
by_pipe: rollUpByPipe(records),
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
/** Null-aware totals over a non-empty set of records. */
|
|
43
|
+
function foldRecords(records) {
|
|
44
|
+
let pricedSum = 0;
|
|
45
|
+
let anyPriced = false;
|
|
46
|
+
let anyUnrated = false;
|
|
47
|
+
let inputSum = null;
|
|
48
|
+
let outputSum = null;
|
|
49
|
+
for (const record of records) {
|
|
50
|
+
// `typeof` rather than `!= null`: artifacts are relayed unvalidated, and a non-numeric `cost`
|
|
51
|
+
// on a malformed one must count as unrated, not turn the sum into string concatenation.
|
|
52
|
+
if (typeof record.cost === "number") {
|
|
53
|
+
pricedSum += record.cost;
|
|
54
|
+
anyPriced = true;
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
anyUnrated = true;
|
|
58
|
+
}
|
|
59
|
+
const byCategory = record.nb_tokens_by_category;
|
|
60
|
+
if (byCategory != null) {
|
|
61
|
+
// Only the two joined totals are additive; every other category is a subset of one.
|
|
62
|
+
const input = byCategory["input"];
|
|
63
|
+
const output = byCategory["output"];
|
|
64
|
+
if (typeof input === "number")
|
|
65
|
+
inputSum = (inputSum ?? 0) + input;
|
|
66
|
+
if (typeof output === "number")
|
|
67
|
+
outputSum = (outputSum ?? 0) + output;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return {
|
|
71
|
+
total_cost_usd: anyPriced ? pricedSum : null,
|
|
72
|
+
cost_partial: anyPriced && anyUnrated,
|
|
73
|
+
tokens: { input: inputSum, output: outputSum },
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
/** Group the records by `pipe_code` (a `null` key gathers the unattributed calls) and sort. */
|
|
77
|
+
function rollUpByPipe(records) {
|
|
78
|
+
const groups = new Map();
|
|
79
|
+
for (const record of records) {
|
|
80
|
+
const pipeCode = typeof record.pipe_code === "string" ? record.pipe_code : null;
|
|
81
|
+
const group = groups.get(pipeCode);
|
|
82
|
+
if (group === undefined) {
|
|
83
|
+
groups.set(pipeCode, [record]);
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
group.push(record);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
const rows = [];
|
|
90
|
+
for (const [pipeCode, pipeRecords] of groups) {
|
|
91
|
+
const fold = foldRecords(pipeRecords);
|
|
92
|
+
rows.push({
|
|
93
|
+
pipe_code: pipeCode,
|
|
94
|
+
total_cost_usd: fold.total_cost_usd,
|
|
95
|
+
cost_partial: fold.cost_partial,
|
|
96
|
+
tokens: fold.tokens,
|
|
97
|
+
calls: pipeRecords.length,
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
return rows.sort(comparePipeRows);
|
|
101
|
+
}
|
|
102
|
+
/** Cost descending with unrated pipes last, then calls descending, then pipe code (null last). */
|
|
103
|
+
function comparePipeRows(a, b) {
|
|
104
|
+
if (a.total_cost_usd !== b.total_cost_usd) {
|
|
105
|
+
if (a.total_cost_usd === null)
|
|
106
|
+
return 1;
|
|
107
|
+
if (b.total_cost_usd === null)
|
|
108
|
+
return -1;
|
|
109
|
+
return b.total_cost_usd - a.total_cost_usd;
|
|
110
|
+
}
|
|
111
|
+
if (a.calls !== b.calls)
|
|
112
|
+
return b.calls - a.calls;
|
|
113
|
+
if (a.pipe_code === b.pipe_code)
|
|
114
|
+
return 0;
|
|
115
|
+
if (a.pipe_code === null)
|
|
116
|
+
return 1;
|
|
117
|
+
if (b.pipe_code === null)
|
|
118
|
+
return -1;
|
|
119
|
+
return a.pipe_code < b.pipe_code ? -1 : 1;
|
|
120
|
+
}
|
|
121
|
+
//# sourceMappingURL=usage.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"usage.js","sourceRoot":"","sources":["../src/usage.ts"],"names":[],"mappings":"AA2FA;;;;;;;GAOG;AACH,MAAM,UAAU,cAAc,CAAC,OAAqB;IAClD,MAAM,OAAO,GAAG,OAAO,CAAC,aAAa,CAAC;IACtC,MAAM,aAAa,GAAG,OAAO,CAAC,oBAAoB,IAAI,IAAI,CAAC;IAE3D,IAAI,OAAO,IAAI,IAAI,EAAE,CAAC;QACpB,OAAO;YACL,KAAK,EAAE,aAAa;YACpB,cAAc,EAAE,IAAI;YACpB,YAAY,EAAE,KAAK;YACnB,MAAM,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE;YACrC,KAAK,EAAE,CAAC;YACR,cAAc,EAAE,aAAa;YAC7B,OAAO,EAAE,EAAE;SACZ,CAAC;IACJ,CAAC;IAED,IAAI,OAAO,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACzB,OAAO;YACL,KAAK,EAAE,cAAc;YACrB,cAAc,EAAE,CAAC;YACjB,YAAY,EAAE,KAAK;YACnB,MAAM,EAAE,EAAE,KAAK,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE;YAC/B,KAAK,EAAE,CAAC;YACR,cAAc,EAAE,aAAa;YAC7B,OAAO,EAAE,EAAE;SACZ,CAAC;IACJ,CAAC;IAED,OAAO;QACL,KAAK,EAAE,SAAS;QAChB,GAAG,WAAW,CAAC,OAAO,CAAC;QACvB,KAAK,EAAE,OAAO,CAAC,MAAM;QACrB,cAAc,EAAE,aAAa;QAC7B,OAAO,EAAE,YAAY,CAAC,OAAO,CAAC;KAC/B,CAAC;AACJ,CAAC;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AAC,CAAC,cAAc,GAAG,CAAC,CAAC,cAAc,CAAC;IAC7C,CAAC;IACD,IAAI,CAAC,CAAC,KAAK,KAAK,CAAC,CAAC,KAAK;QAAE,OAAO,CAAC,CAAC,KAAK,GAAG,CAAC,CAAC,KAAK,CAAC;IAClD,IAAI,CAAC,CAAC,SAAS,KAAK,CAAC,CAAC,SAAS;QAAE,OAAO,CAAC,CAAC;IAC1C,IAAI,CAAC,CAAC,SAAS,KAAK,IAAI;QAAE,OAAO,CAAC,CAAC;IACnC,IAAI,CAAC,CAAC,SAAS,KAAK,IAAI;QAAE,OAAO,CAAC,CAAC,CAAC;IACpC,OAAO,CAAC,CAAC,SAAS,GAAG,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AAC5C,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pipelex/sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.18.0",
|
|
4
4
|
"description": "TypeScript SDK for the Pipelex hosted API — execute methods, manage runs, and call the product surface.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -59,7 +59,8 @@
|
|
|
59
59
|
"vitest": "^4.0.18"
|
|
60
60
|
},
|
|
61
61
|
"dependencies": {
|
|
62
|
-
"mthds": "^0.
|
|
63
|
-
"smol-toml": "^1.6.0"
|
|
62
|
+
"mthds": "^0.25.0",
|
|
63
|
+
"smol-toml": "^1.6.0",
|
|
64
|
+
"undici": "^7.29.1"
|
|
64
65
|
}
|
|
65
66
|
}
|