pareta 2.0.0 → 2.1.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.cjs CHANGED
@@ -174,7 +174,7 @@ function parseSSE(reader, opts) {
174
174
  }
175
175
 
176
176
  // src/version.ts
177
- var VERSION = "2.0.0";
177
+ var VERSION = "2.1.0";
178
178
 
179
179
  // src/money.ts
180
180
  var MICRO_PER_CENT = 10000n;
@@ -255,6 +255,36 @@ var ChatCompletion = class extends BaseModel {
255
255
  get usage() {
256
256
  return new Usage(this.raw.usage ?? {});
257
257
  }
258
+ /** #164 receipt, held OFF `.raw` so `toDict()` stays the raw server JSON. */
259
+ _cost;
260
+ /** #164: attach the per-call receipt from the response headers (billed +
261
+ * the frontier counterfactual, micro-USD ints). Chainable. */
262
+ withCost(headers) {
263
+ const int = (name) => {
264
+ const v = headers?.get(name);
265
+ if (v == null) return null;
266
+ const n = parseInt(v, 10);
267
+ return Number.isNaN(n) ? null : n;
268
+ };
269
+ this._cost = {
270
+ billed: int("X-Pareta-Billed"),
271
+ frontier: int("X-Pareta-Frontier-Would-Have-Cost")
272
+ };
273
+ return this;
274
+ }
275
+ /** What Pareta charged, micro-USD (0 on an idempotent replay); null if absent. */
276
+ get billedMicroUsd() {
277
+ return this._cost?.billed ?? null;
278
+ }
279
+ /** What one list-priced frontier call would have cost, micro-USD. */
280
+ get frontierWouldHaveCostMicroUsd() {
281
+ return this._cost?.frontier ?? null;
282
+ }
283
+ /** frontier / billed — e.g. 17 = 17× cheaper; null if either is missing or billed is 0. */
284
+ get savingsFactor() {
285
+ const b = this.billedMicroUsd, f = this.frontierWouldHaveCostMicroUsd;
286
+ return b && f && b > 0 ? Math.round(f / b * 10) / 10 : null;
287
+ }
258
288
  };
259
289
  var ChatCompletionChunk = class extends ChatCompletion {
260
290
  };
@@ -644,10 +674,14 @@ var Completions = class {
644
674
  cast: (raw) => new ChatCompletionChunk(raw)
645
675
  });
646
676
  }
677
+ let headers;
647
678
  return this.client.request("POST", PATH, {
648
679
  body,
649
- cast: (raw) => new ChatCompletion(raw)
650
- });
680
+ cast: (raw) => new ChatCompletion(raw),
681
+ onHeaders: (h) => {
682
+ headers = h;
683
+ }
684
+ }).then((completion) => completion.withCost(headers));
651
685
  }
652
686
  };
653
687
  var Chat = class {
@@ -1310,6 +1344,7 @@ var Pareta = class _Pareta {
1310
1344
  break;
1311
1345
  }
1312
1346
  if (res.ok) {
1347
+ if (opts.onHeaders) opts.onHeaders(res.headers);
1313
1348
  const text = await res.text();
1314
1349
  const raw = text ? JSON.parse(text) : {};
1315
1350
  return cast ? cast(raw) : raw;