agent402-client 0.8.6 → 0.8.7

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.
Files changed (2) hide show
  1. package/index.js +13 -6
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -23,7 +23,7 @@ import { createHash, randomBytes } from "node:crypto";
23
23
  // `User-Agent: agent402-client/<version>` - a standard header, no extra
24
24
  // network calls - so a seller can attribute traffic (and settled payments)
25
25
  // to this SDK. Product token only; nothing about the caller rides along.
26
- const VERSION = "0.8.6";
26
+ const VERSION = "0.8.7";
27
27
  const USER_AGENT = `agent402-client/${VERSION}`;
28
28
  // 32MB: about a hundred times any realistic response from this catalog (the
29
29
  // largest is a base64 image at a few MB), so it cannot break a legitimate
@@ -365,7 +365,7 @@ export class Agent402 {
365
365
  let settled = false;
366
366
  try {
367
367
  const r = await send({}, this.payFetch);
368
- if (!r.ok) throw new Error(await this._failureDetail(slug, r));
368
+ if (!r.ok) throw this._fail(slug, r, await this._failureDetail(slug, r));
369
369
  this._spendSettle(reservation); // confirm the reservation as settled spend
370
370
  settled = true;
371
371
  return this._deliverResponse(slug, r, cacheKey, cache, validator, { maxResponseBytes, paid: true });
@@ -388,9 +388,9 @@ export class Agent402 {
388
388
  const r = await send({ Authorization: `Bearer ${this.creditsKey}` });
389
389
  if (r.status === 402) {
390
390
  const body = await r.json().catch(() => ({}));
391
- throw new Error(`call "${slug}" refused by credits: ${body.error || "payment required"}${body.balanceUsd != null ? ` (balance $${body.balanceUsd})` : ""} - top up at ${body.topup || `${this.baseUrl}/credits`}`);
391
+ throw this._fail(slug, r, `call "${slug}" refused by credits: ${body.error || "payment required"}${body.balanceUsd != null ? ` (balance $${body.balanceUsd})` : ""} - top up at ${body.topup || `${this.baseUrl}/credits`}`);
392
392
  }
393
- if (!r.ok) throw new Error(await this._failureDetail(slug, r));
393
+ if (!r.ok) throw this._fail(slug, r, await this._failureDetail(slug, r));
394
394
  this._spendSettle(reservation);
395
395
  settled = true;
396
396
  return this._deliverResponse(slug, r, cacheKey, cache, validator, { maxResponseBytes, paid: true });
@@ -398,7 +398,7 @@ export class Agent402 {
398
398
  }
399
399
  const r = await send(); // no wallet - succeeds only on a FREE_MODE instance
400
400
  if (r.ok) return this._deliverResponse(slug, r, cacheKey, cache, validator, { maxResponseBytes, paid: false });
401
- throw new Error(`call "${slug}" failed: HTTP ${r.status} - wallet-only tool; construct with { fetch: payFetch } (an @x402/fetch-wrapped fetch) or { creditsKey } (prepaid card credits from ${this.baseUrl}/credits)`);
401
+ throw this._fail(slug, r, `call "${slug}" failed: HTTP ${r.status} - wallet-only tool; construct with { fetch: payFetch } (an @x402/fetch-wrapped fetch) or { creditsKey } (prepaid card credits from ${this.baseUrl}/credits)`);
402
402
  }
403
403
 
404
404
  // Free (compute-payable) tool: succeeds plainly on a FREE_MODE instance,
@@ -409,7 +409,7 @@ export class Agent402 {
409
409
  const chal = await this._powChallenge(slug);
410
410
  r = await send({ "X-Pow-Solution": Agent402.solvePow(chal) });
411
411
  }
412
- if (!r.ok) throw new Error(await this._failureDetail(slug, r, " after proof-of-work"));
412
+ if (!r.ok) throw this._fail(slug, r, await this._failureDetail(slug, r, " after proof-of-work"));
413
413
  return this._deliverResponse(slug, r, cacheKey, cache, validator, { maxResponseBytes, paid: false });
414
414
  }
415
415
 
@@ -485,6 +485,13 @@ export class Agent402 {
485
485
  * it is parsed (maxResponseBytes, 32 MB default), so a second cut here
486
486
  * protected nothing and lost the part of the message an agent needs to
487
487
  * act on. A body that is not JSON contributes nothing but its status. */
488
+ /** A refusal a caller can branch on: `status` (the HTTP status), `slug` and
489
+ * `paid: false` ride on the Error, so a 402 is not something to regex out of
490
+ * the message. Nothing was spent on any path that reaches this. */
491
+ _fail(slug, r, message) {
492
+ return Object.assign(new Error(message), { status: r?.status, slug, paid: false });
493
+ }
494
+
488
495
  async _failureDetail(slug, r, when = "") {
489
496
  let detail = "";
490
497
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agent402-client",
3
- "version": "0.8.6",
3
+ "version": "0.8.7",
4
4
  "description": "Agent402 buyer SDK: resolve a task to a tool across 500+ pay-per-call endpoints and call it with payment handled - free via proof-of-work, or over x402 or MPP (a stock mppx fetch works) with a wallet you keep; the buy side of Agentic Finance. Caching, idempotent retries (Idempotency-Key), non-custodial, zero dependencies.",
5
5
  "type": "module",
6
6
  "main": "index.js",