agent402-openclaw 0.4.3 → 0.4.4

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 (3) hide show
  1. package/README.md +8 -6
  2. package/package.json +1 -1
  3. package/proxy.js +42 -0
package/README.md CHANGED
@@ -57,11 +57,11 @@ wallet. The key never leaves the machine.
57
57
  Two ways the wallet can pay a metered call:
58
58
 
59
59
  - **exact** (default): the 402 quotes the call from its body (input + your
60
- `max_tokens` at the model's list price, x1.15) and the wallet pays that quote.
60
+ `max_tokens`) and the wallet pays that quote.
61
61
  - **upto, actual usage**: run `agent402-openclaw permit2-approve` once (one USDC
62
62
  approval transaction on Base; the wallet needs a little ETH for gas). From
63
63
  then on the proxy authorizes the quote as a CEILING and the gateway settles
64
- what the call actually cost x1.15, so a short answer costs a fraction of the
64
+ what the call actually used, so a short answer costs a fraction of the
65
65
  quote. `agent402-openclaw doctor` says which mode the wallet is in.
66
66
 
67
67
  Card / credits buyers already pay actual usage on metered calls.
@@ -71,12 +71,14 @@ Card / credits buyers already pay actual usage on metered calls.
71
71
  `auto` (routed per prompt, flat $0.01/call) plus every id from
72
72
  `GET https://agent402.tools/v1/models`. Explicit models are **metered by default**:
73
73
  the proxy sends them to the gateway's metered route, where each request is quoted
74
- from its body (exact-BPE input plus your `max_tokens` at the model's list price,
75
- times 1.15, from $0.001, capped at $2 per call), so a short call costs a fraction
74
+ from its body (input plus your `max_tokens`, from $0.001, capped at $2 per
75
+ call), so a short call costs a fraction
76
76
  of a cent and a long one pays for what it asks. `--flat` (or
77
77
  `AGENT402_PRICING=flat`) keeps every model on its flat per-call tier instead.
78
- Either way OpenClaw's per-token cost fields stay zero; the price is per call. A model sent to the wrong tier is answered with a
79
- 400 naming the right one; nothing is charged. A client-supplied `Idempotency-Key`
78
+ Either way OpenClaw's per-token cost fields stay zero; the price is per call. A model sent to another flat
79
+ tier's route is priced at its home tier: the 402 quotes that tier's price, and the paid call is served under
80
+ that tier's caps (the answer says so in `agent402_tier`). The router, grounded and stealth tiers still answer a
81
+ 400 naming the right route; nothing is charged. A client-supplied `Idempotency-Key`
80
82
  is passed through (an x402 retry with the same key replays the paid answer);
81
83
  without one, each call is its own payment.
82
84
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agent402-openclaw",
3
- "version": "0.4.3",
3
+ "version": "0.4.4",
4
4
  "description": "OpenClaw model provider for Agent402: auto-routed and explicit LLM models at a flat per-call price, paid by card (prepaid credits key) or in USDC over x402 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
@@ -20,6 +20,15 @@
20
20
  // own 402->pay retry), which does NOT make two client calls one payment.
21
21
  // Streams pass through byte for byte.
22
22
  //
23
+ // Refusals: a refused paid call's 402 from an x402 v2 seller carries the whole
24
+ // payment offer in its JSON body (Agent402 mirrors its PAYMENT-REQUIRED header
25
+ // there) beside the refusal's own reason, hint and retry. OpenClaw's model
26
+ // client builds its error message from `error.message`, else from the whole
27
+ // body, so relaying that body put kilobytes of accepts and schemas into the
28
+ // agent's error text in place of the one sentence that says what to fix. Such
29
+ // a body is answered in the OpenAI error shape instead (refusalAsOpenAIError);
30
+ // every other upstream body passes through byte for byte.
31
+ //
23
32
  // Loopback only, and browser-hostile on purpose: any web page can POST to
24
33
  // 127.0.0.1 with a "simple" no-cors request, and this proxy spends the
25
34
  // user's key, so a request carrying an Origin header (browsers always send
@@ -49,6 +58,27 @@ function readBody(req, limit = MAX_BODY) {
49
58
  });
50
59
  }
51
60
 
61
+ // The x402 offer keys a refusal body may carry beside its own fields.
62
+ const OFFER_KEYS = ["x402Version", "resource", "accepts", "extensions"];
63
+ const text = (v) => (typeof v === "string" && v.trim() ? v : null);
64
+
65
+ /** An upstream error body that carries an x402 offer (a numeric x402Version),
66
+ * as an OpenAI-shaped error: the explanation (a problem's detail, else the
67
+ * hint, else the error sentence) as error.message, the retry class or reason as
68
+ * error.code, the refusal's own fields beside it, and no offer. Null for any
69
+ * other body, which the caller relays unchanged. */
70
+ export function refusalAsOpenAIError(status, doc) {
71
+ if (!doc || typeof doc !== "object" || Array.isArray(doc) || typeof doc.x402Version !== "number") return null;
72
+ const ours = { ...doc };
73
+ for (const k of OFFER_KEYS) delete ours[k];
74
+ const { error, ...fields } = ours;
75
+ const message = text(fields.detail) || text(fields.hint) || text(error) || text(fields.title) || `Agent402 answered HTTP ${status}`;
76
+ return {
77
+ error: { message, type: status === 402 ? "payment_required" : "upstream_error", code: text(fields.retry) || text(fields.reason) || null },
78
+ ...fields,
79
+ };
80
+ }
81
+
52
82
  const json = (res, status, obj, headers = {}) => {
53
83
  res.writeHead(status, { "content-type": "application/json; charset=utf-8", ...headers });
54
84
  res.end(JSON.stringify(obj));
@@ -113,6 +143,18 @@ export async function startProxy({ upstream = DEFAULT_UPSTREAM, creditsKey = nul
113
143
  stats.forwarded++;
114
144
  const passthrough = {};
115
145
  for (const h of ["content-type", "x-credits-balance", "payment-receipt", "x-cache", "cache-control"]) { const v = up.headers.get(h); if (v) passthrough[h] = v; }
146
+ if (!up.ok && /json/i.test(passthrough["content-type"] || "")) {
147
+ const raw = await up.text();
148
+ let doc = null;
149
+ try { doc = JSON.parse(raw); } catch { /* not JSON after all: relayed as sent */ }
150
+ const mapped = refusalAsOpenAIError(up.status, doc);
151
+ if (mapped) {
152
+ const { "content-type": _ct, ...rest } = passthrough;
153
+ return json(res, up.status, mapped, rest);
154
+ }
155
+ res.writeHead(up.status, passthrough);
156
+ return res.end(raw);
157
+ }
116
158
  res.writeHead(up.status, passthrough);
117
159
  if (!up.body) return res.end();
118
160
  const reader = up.body.getReader();