openzoo 0.50.36 → 0.50.37

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/lib/pay.js +21 -1
  2. package/package.json +1 -1
package/lib/pay.js CHANGED
@@ -413,7 +413,27 @@ export class PayClient {
413
413
  if (response.status === 402 && !this._rebuilt) {
414
414
  this._rebuilt = true;
415
415
  try {
416
- const fresh = await this.fetch(url, init, { onStage });
416
+ // TAKE THE GATEWAY'S ADVICE BEFORE RE-QUOTING BLIND. On "payer balance
417
+ // insufficient" the 402 now carries advice.retryWithMaxTokens — the
418
+ // max_tokens whose ceiling the wallet's ACTUAL balance covers. A blind
419
+ // re-quote asks the same price and dies the same death (the gateway
420
+ // ledger shows wallets doing exactly that, hundreds of times a day);
421
+ // shrinking the ask turns the retry into a sale. A shorter answer
422
+ // beats no answer, and the upto rail still bills only actual usage.
423
+ let retryInit = init;
424
+ try {
425
+ const body402 = await response.clone().json();
426
+ const fit = Number(body402?.advice?.retryWithMaxTokens);
427
+ if (Number.isFinite(fit) && fit >= 64 && typeof init.body === 'string') {
428
+ const req = JSON.parse(init.body);
429
+ if (!Number.isFinite(Number(req.max_tokens)) || Number(req.max_tokens) > fit) {
430
+ req.max_tokens = fit;
431
+ retryInit = { ...init, body: JSON.stringify(req) };
432
+ onStage?.('advice', `balance covers ~${body402?.advice?.coversPct ?? '?'}% — retrying at max_tokens=${fit}`);
433
+ }
434
+ }
435
+ } catch { /* no advice or unreadable body: plain re-quote */ }
436
+ const fresh = await this.fetch(url, retryInit, { onStage });
417
437
  return fresh;
418
438
  } finally {
419
439
  this._rebuilt = false;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "openzoo",
3
- "version": "0.50.36",
3
+ "version": "0.50.37",
4
4
  "description": "Local x402-paying proxy + MCP server for openzoo.fun — point any OpenAI-compatible harness (Cursor, Claude Code, aider, SDKs) at localhost and it pays per call from a local burner wallet. Solana and Base rails live; Robinhood experimental.",
5
5
  "license": "MIT",
6
6
  "type": "module",