agent402-openclaw 0.5.2 → 0.5.3

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/package.json +1 -1
  2. package/proxy.js +45 -2
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agent402-openclaw",
3
- "version": "0.5.2",
3
+ "version": "0.5.3",
4
4
  "description": "OpenClaw model provider for Agent402: an auto-routed tier at a flat per-call price and explicit LLM models priced per request, paid in USDC over x402 or with a prepaid credits key through a local proxy. Zero dependencies; x402 wallet payment is an optional peer.",
5
5
  "type": "module",
6
6
  "main": "index.js",
package/proxy.js CHANGED
@@ -47,9 +47,52 @@ export const DEFAULT_UPSTREAM = "https://agent402.tools";
47
47
  export const PKG_VERSION = JSON.parse(readFileSync(new URL("./package.json", import.meta.url), "utf8")).version;
48
48
  const MAX_BODY = 2 * 1024 * 1024;
49
49
 
50
+ // The model catalog read (GET /v1/models) settles nothing, so a 503 the
51
+ // server sheds under load is safe to repeat. It is retried only when it names
52
+ // its own delay (a Retry-After header or a retryAfterSeconds body field), at
53
+ // most DISCOVERY_ATTEMPTS tries in all, each wait capped at
54
+ // DISCOVERY_MAX_WAIT_MS. A paid call never goes through here: a repeat of
55
+ // one could settle twice.
56
+ const DISCOVERY_ATTEMPTS = 3;
57
+ const DISCOVERY_MAX_WAIT_MS = 5000;
58
+
59
+ async function shedRetryWaitMs(r) {
60
+ if (!r || r.status !== 503) return null;
61
+ const header = r.headers && typeof r.headers.get === "function" ? r.headers.get("retry-after") : null;
62
+ let seconds = header != null && String(header).trim() !== "" ? Number(header) : NaN;
63
+ if (!Number.isFinite(seconds)) {
64
+ try {
65
+ const body = await (typeof r.clone === "function" ? r.clone() : r).json();
66
+ seconds = Number(body && body.retryAfterSeconds);
67
+ } catch { seconds = NaN; }
68
+ }
69
+ if (!Number.isFinite(seconds) || seconds < 0) return null;
70
+ return Math.min(seconds * 1000, DISCOVERY_MAX_WAIT_MS);
71
+ }
72
+
73
+ // Resolves with the 2xx response, or throws `${label}: HTTP <status>` (plus
74
+ // "after N attempts" when it retried) with `.status` and `.attempts` set.
75
+ // `init` may be a function, so each attempt gets its own timeout signal.
76
+ async function discoveryFetch(fetchImpl, label, url, init) {
77
+ let r;
78
+ let attempts = 0;
79
+ for (;;) {
80
+ attempts++;
81
+ r = await fetchImpl(url, typeof init === "function" ? init() : init);
82
+ if (r.ok) return r;
83
+ if (attempts >= DISCOVERY_ATTEMPTS) break;
84
+ const wait = await shedRetryWaitMs(r);
85
+ if (wait == null) break;
86
+ await new Promise((resolve) => setTimeout(resolve, wait));
87
+ }
88
+ const err = new Error(`${label}: HTTP ${r.status}${attempts > 1 ? ` after ${attempts} attempts` : ""}`);
89
+ err.status = r.status;
90
+ err.attempts = attempts;
91
+ throw err;
92
+ }
93
+
50
94
  export async function loadRoutes(upstream, fetchImpl = fetch, { pricing = "metered" } = {}) {
51
- const r = await fetchImpl(`${upstream}/v1/models`, { headers: { accept: "application/json" }, signal: AbortSignal.timeout(15_000) });
52
- if (!r.ok) throw new Error(`GET /v1/models -> HTTP ${r.status}`);
95
+ const r = await discoveryFetch(fetchImpl, "GET /v1/models", `${upstream}/v1/models`, () => ({ headers: { accept: "application/json" }, signal: AbortSignal.timeout(15_000) }));
53
96
  return routesFromCatalog(await r.json(), { pricing });
54
97
  }
55
98