@pipelex/sdk 0.17.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/dist/index.js CHANGED
@@ -7,7 +7,7 @@
7
7
  * errors. Consumers should import everything they need from `@pipelex/sdk`.
8
8
  */
9
9
  /** Package version. Kept in sync with `package.json` — enforced by `tests/index.test.ts`. */
10
- export const SDK_VERSION = "0.17.0";
10
+ export const SDK_VERSION = "0.18.0";
11
11
  // ── Pure MTHDS Protocol surface (re-exported from the `mthds/protocol` subpath) ──
12
12
  // The standard's interface, wire models, request/options surface, abstract domain
13
13
  // shapes, and the protocol-base `PipelineRequestError`. Re-exported so apps have a
@@ -19,6 +19,11 @@ export { PipelexApiClient, DEFAULT_API_BASE_URL } from "./client.js";
19
19
  export { methodSourceToContents } from "./method-source.js";
20
20
  // The blocking `execute()` result — a `DictRunResultExecute` with a resolved `.main_stuff`.
21
21
  export { PipelexExecuteResult } from "./execute-result.js";
22
+ // ── Artifacts (the download twin of input preparation) ──────────────
23
+ // `collectArtifacts` and `artifactFilename` are pure and standalone; the network
24
+ // operations (`resolveArtifacts` / `fetchArtifact` / `downloadArtifacts`) are client
25
+ // methods, and only their public types and bounds travel with the barrel.
26
+ export { collectArtifacts, artifactFilename, PIPELEX_STORAGE_SCHEME, BULK_RESOLVE_MAX_URIS, DEFAULT_ARTIFACT_MAX_BYTES, DEFAULT_ARTIFACT_TIMEOUT_MS, DEFAULT_DOWNLOAD_MAX_TOTAL_BYTES, DEFAULT_DOWNLOAD_CONCURRENCY, } from "./artifacts.js";
22
27
  // ── Offline codegen drift check (pure — no filesystem, no network, no key) ──
23
28
  // The CI half of the codegen trust chain: hand it a committed tree plus its
24
29
  // `codegen.lock` and it returns the structured drift verdict. Regeneration needs
@@ -26,6 +31,8 @@ export { PipelexExecuteResult } from "./execute-result.js";
26
31
  export { runCodegenCheck, isStampableArtifactPath, STAMPABLE_ARTIFACT_SUFFIXES, CodegenLockError, } from "./codegen-check.js";
27
32
  // ── Run lifecycle (hosted extension — NOT part of the protocol) ──────
28
33
  export { isTerminalRunStatus, isSuccessRunStatus } from "./runs.js";
34
+ // ── Usage summary (pure — folds a run's usage pair into one null-aware reading) ──
35
+ export { summarizeUsage } from "./usage.js";
29
36
  // ── Typed errors (PipelineRequestError rides the protocol re-export above) ──
30
- export { ApiResponseError, ApiUnreachableError, ClientAuthenticationError, MissingMainStuffError, PipelineExecuteTimeoutError, RunFailedError, RunTimeoutError, RunStillRunningError, RunLifecycleUnavailableError, InputPreparationError, EmptyMethodSourceError, InvalidLocalSourceError, RejectedAssetError, UnsupportedUploadCapabilityError, UploadAuthenticationError, UploadTransportError, } from "./errors.js";
37
+ export { ApiResponseError, ApiUnreachableError, ClientAuthenticationError, MissingMainStuffError, PipelineExecuteTimeoutError, RunFailedError, RunTimeoutError, RunStillRunningError, RunLifecycleUnavailableError, InputPreparationError, EmptyMethodSourceError, InvalidLocalSourceError, RejectedAssetError, UnsupportedUploadCapabilityError, UploadAuthenticationError, UploadTransportError, ArtifactOperationError, ArtifactAuthenticationError, ArtifactFetchError, ScopeUnavailableError, } from "./errors.js";
31
38
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,6FAA6F;AAC7F,MAAM,CAAC,MAAM,WAAW,GAAG,QAAQ,CAAC;AAEpC,oFAAoF;AACpF,kFAAkF;AAClF,mFAAmF;AACnF,wBAAwB;AACxB,cAAc,gBAAgB,CAAC;AAE/B,wEAAwE;AACxE,OAAO,EAAE,gBAAgB,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAC;AAYrE,+FAA+F;AAC/F,OAAO,EAAE,sBAAsB,EAAE,MAAM,oBAAoB,CAAC;AAE5D,4FAA4F;AAC5F,OAAO,EAAE,oBAAoB,EAAE,MAAM,qBAAqB,CAAC;AA8E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jF,0EAA0E;AAC1E,OAAO,EACL,eAAe,EACf,uBAAuB,EACvB,2BAA2B,EAC3B,gBAAgB,GACjB,MAAM,oBAAoB,CAAC;AAiD5B,wEAAwE;AACxE,OAAO,EAAE,mBAAmB,EAAE,kBAAkB,EAAE,MAAM,WAAW,CAAC;AAWpE,oFAAoF;AACpF,OAAO,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAS5C,+EAA+E;AAC/E,OAAO,EACL,gBAAgB,EAChB,mBAAmB,EACnB,yBAAyB,EACzB,qBAAqB,EACrB,2BAA2B,EAC3B,cAAc,EACd,eAAe,EACf,oBAAoB,EACpB,4BAA4B,EAC5B,qBAAqB,EACrB,sBAAsB,EACtB,uBAAuB,EACvB,kBAAkB,EAClB,gCAAgC,EAChC,yBAAyB,EACzB,oBAAoB,EACpB,sBAAsB,EACtB,2BAA2B,EAC3B,kBAAkB,EAClB,qBAAqB,GACtB,MAAM,aAAa,CAAC"}
package/dist/runs.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { DictPipeOutput } from "./models.js";
1
+ import type { DictPipeOutput, DictWorkingMemory } from "./models.js";
2
2
  /**
3
3
  * Run-lifecycle types + polling for the hosted polling surface (`/v1/runs/*`).
4
4
  *
@@ -131,12 +131,43 @@ export interface RunResults {
131
131
  * output to an object) and may be a valid falsy value (empty array, `0`); it is never absent.
132
132
  */
133
133
  main_stuff: unknown;
134
- /** Method graph spec (`graphspec.json`); null if missing mid-write or on the bare-runner path. */
134
+ /**
135
+ * The run's working memory — every named stuff of the run (`{ root, aliases }`), the inputs it
136
+ * was given and the intermediates it produced as well as the main output, each stuff carried as
137
+ * its concept ref and its content. It reads the same on both paths: the hosted path relays the
138
+ * `working_memory.json` artifact verbatim, and on the blocking path the SDK lifts it off
139
+ * `pipe_output`. `main_stuff` is the content of one of its entries, already resolved. Null on the
140
+ * hosted path when the artifact was not yet written. `DictWorkingMemory` mirrors the MTHDS
141
+ * standard's `DictWorkingMemory` field for field. A run delivered by a runtime older than
142
+ * pipelex 0.60.0 carries each `concept` as the concept object instead, and a stored artifact is
143
+ * relayed as written, never migrated: the type stays the standard's `string`, so read `concept`
144
+ * as `unknown` when a run may predate that release. See `docs/run-results.md`.
145
+ */
146
+ working_memory?: DictWorkingMemory | null;
147
+ /**
148
+ * The executed graph — the same document a local run writes as `graphspec.json`: `meta.mode`
149
+ * `"live"`, one node per pipe with its status, its timings and its own usage. It reaches the client on both
150
+ * paths: the hosted path relays the `graphspec.json` artifact verbatim, and on the blocking path
151
+ * the SDK lifts it off `pipe_output`. Null when the runner assembled no graph (see
152
+ * `graph_assembly_error`) or, on the hosted path, when the artifact was not yet written. Typed
153
+ * `unknown` on purpose — the canonical declaration is `GraphSpec` in `@pipelex/mthds-ui`, which
154
+ * carries a React peer dependency this server-side SDK does not take. See `docs/run-results.md`.
155
+ */
135
156
  graph_spec?: unknown;
136
157
  /**
137
- * Bare runner's native pipe output — the full working memory (`{ root, aliases }`),
138
- * blocking-execute path only; null on the hosted path. Supplementary to `main_stuff`,
139
- * which is already resolved out of it; kept for consumers that need the whole working memory.
158
+ * Non-null when the runner's graph assembly failed for the run — the graph's twin of
159
+ * `usage_assembly_error`, and the only thing that separates "the graph broke" from "this run
160
+ * produced no graph". Lifted off `pipe_output` on the blocking path; the hosted results body
161
+ * carries nothing of the kind yet, so on that path the field is absent until the platform writes
162
+ * and relays it.
163
+ */
164
+ graph_assembly_error?: string | null;
165
+ /**
166
+ * Bare runner's native pipe output, blocking-execute path only; absent on the hosted path, whose
167
+ * results body carries no such key, so it reads `undefined` there. Supplementary: `main_stuff`,
168
+ * `working_memory`, the graph pair and the usage pair are all lifted out of it onto their own
169
+ * fields, which read the same on both paths. Read `working_memory` for the run's named stuffs;
170
+ * `pipe_output` is the runner's output as it arrived, extension fields included.
140
171
  */
141
172
  pipe_output?: DictPipeOutput | null;
142
173
  /**
package/dist/runs.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"runs.js","sourceRoot":"","sources":["../src/run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AG,EAAE;YAC5B,MAAM,EAAE,mBAAmB,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;YAC9C,OAAO,EAAE,CAAC;QACZ,CAAC,EAAE,EAAE,CAAC,CAAC;QACP,IAAI,MAAM,EAAE,CAAC;YACX,IAAI,MAAM,CAAC,OAAO,EAAE,CAAC;gBACnB,YAAY,CAAC,KAAK,CAAC,CAAC;gBACpB,MAAM,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,CAAC;gBAC3B,OAAO;YACT,CAAC;YACD,MAAM,CAAC,gBAAgB,CAAC,OAAO,EAAE,OAAO,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC;QAC5D,CAAC;IACH,CAAC,CAAC,CAAC;AACL,CAAC"}
@@ -0,0 +1,87 @@
1
+ import type { RunResults } from "./runs.js";
2
+ /**
3
+ * `summarizeUsage` — one run-level reading of a run's usage pair.
4
+ *
5
+ * A completed run reports usage as `RunResults.tokens_usages` (one record per inference call)
6
+ * beside `RunResults.usage_assembly_error`, and the wire carries no run-level aggregate. This
7
+ * module folds the pair into a single summary under the rules `docs/run-usage.md` states, so
8
+ * every consumer reads the same totals instead of re-deriving them:
9
+ *
10
+ * - a `null` cost is unrated (the model has no rate table), a `0` cost is priced at zero;
11
+ * - `input` and `output` are the only additive token categories (`input_cached` is a subset of
12
+ * `input`, and summing every category double-counts);
13
+ * - a null list means usage is unavailable, and only `usage_assembly_error` says it broke;
14
+ * - an empty list is a run that did no inference, which costs `0`, not `null`.
15
+ *
16
+ * Pure: no I/O, no client, and the input is never mutated.
17
+ */
18
+ /**
19
+ * Which of the three readings of `tokens_usages` the summary describes:
20
+ * - `records` — a non-empty list: the totals are folded from it.
21
+ * - `no_inference` — `[]`: usage assembly ran and no inference happened, so the cost is `0`
22
+ * and the token totals are zero.
23
+ * - `unavailable` — the list is null or absent: usage assembly was off, it broke (then
24
+ * `assembly_error` is non-null), or on the hosted path the run was delivered before the
25
+ * usage artifact existed. Nothing is known, so the cost and the token totals are `null`.
26
+ */
27
+ export type UsageSummaryState = "records" | "no_inference" | "unavailable";
28
+ /**
29
+ * The two additive token totals. Each is the sum of that category over the records that
30
+ * reported it, and `null` when no record reported it at all — which is different from a
31
+ * reported `0`.
32
+ */
33
+ export interface UsageTokenTotals {
34
+ input: number | null;
35
+ output: number | null;
36
+ }
37
+ /** One pipe's share of a run's usage — the same fold as the run level, over its calls only. */
38
+ export interface PipeUsageSummary {
39
+ /** The pipe that made the calls; `null` groups the calls the runtime did not attribute. */
40
+ pipe_code: string | null;
41
+ /** Sum of the priced calls' costs in USD; `null` when none of this pipe's calls was priced. */
42
+ total_cost_usd: number | null;
43
+ /** True when this pipe mixes priced and unrated calls, so `total_cost_usd` is a lower bound. */
44
+ cost_partial: boolean;
45
+ tokens: UsageTokenTotals;
46
+ /** Number of inference calls this pipe made. */
47
+ calls: number;
48
+ }
49
+ /** A run's usage pair folded into one null-aware reading. */
50
+ export interface UsageSummary {
51
+ state: UsageSummaryState;
52
+ /**
53
+ * Sum of the priced calls' costs in USD. `0` for `no_inference`. `null` for `records` when no
54
+ * call was priced (unrated), and for `unavailable`, where nothing is known — read `state`
55
+ * first to tell the two apart.
56
+ */
57
+ total_cost_usd: number | null;
58
+ /**
59
+ * True when priced and unrated calls are mixed, so `total_cost_usd` covers the priced calls
60
+ * only and is a lower bound. Never true outside `records`.
61
+ */
62
+ cost_partial: boolean;
63
+ tokens: UsageTokenTotals;
64
+ /** Number of usage records summarized — one per inference call; `0` outside `records`. */
65
+ calls: number;
66
+ /**
67
+ * `usage_assembly_error` as relayed: non-null when the runner's usage assembly failed, which
68
+ * is the only thing that separates a broken assembly from an `unavailable` one that was off.
69
+ */
70
+ assembly_error: string | null;
71
+ /**
72
+ * Per-pipe rollup, most expensive first: priced pipes by cost descending, then unrated pipes,
73
+ * with ties broken by call count (descending) and then by pipe code. Empty outside `records`.
74
+ */
75
+ by_pipe: PipeUsageSummary[];
76
+ }
77
+ /** The part of `RunResults` the summary reads — a whole `RunResults` is accepted as is. */
78
+ export type RunUsagePair = Pick<RunResults, "tokens_usages" | "usage_assembly_error">;
79
+ /**
80
+ * Fold a run's usage pair into one summary: run totals, the call count, the assembly error and
81
+ * a per-pipe rollup. See `docs/run-usage.md` for the rules it applies.
82
+ *
83
+ * Pre-contract records, relayed verbatim from artifacts written before the usage contract,
84
+ * carry no `cost` and no `pipe_code`: they count as unrated and unattributed. The legacy
85
+ * `job_metadata` / `unit_costs` fields are relics and are never read.
86
+ */
87
+ export declare function summarizeUsage(results: RunUsagePair): UsageSummary;
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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I,CAAC,CAAC,cAAc,KAAK,IAAI;YAAE,OAAO,CAAC,CAAC,CAAC;QACzC,OAAO,CAAC,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.17.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": {
@@ -60,6 +60,7 @@
60
60
  },
61
61
  "dependencies": {
62
62
  "mthds": "^0.25.0",
63
- "smol-toml": "^1.6.0"
63
+ "smol-toml": "^1.6.0",
64
+ "undici": "^7.29.1"
64
65
  }
65
66
  }