openzoo 0.48.62 → 0.48.63

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/proxy.js +31 -4
  2. package/package.json +1 -1
package/lib/proxy.js CHANGED
@@ -829,6 +829,9 @@ export async function startProxy({ silent = false, requireToken = null, sessionM
829
829
  // cost can.
830
830
  let sessionActual = 0;
831
831
  let actualCalls = 0;
832
+ // billed for ONLY those calls whose real cost we learned — the honest
833
+ // numerator for markupX. See the comment at the usage.cost site.
834
+ let billedWithActual = 0;
832
835
  // CREDIT, CACHED. Users cannot tell prepaid credit from wallet balance and
833
836
  // have to guess whether a call was even paid for ("I don't think x402 made me
834
837
  // pay this at all"). The status line runs EVERY turn, so this is refreshed at
@@ -980,9 +983,9 @@ export async function startProxy({ silent = false, requireToken = null, sessionM
980
983
  actual: {
981
984
  calls: actualCalls,
982
985
  upstreamUsd: Number(sessionActual.toFixed(6)),
983
- billedUsd: Number(sessionSpent.toFixed(6)),
984
- marginUsd: Number((sessionSpent - sessionActual).toFixed(6)),
985
- markupX: sessionActual > 0 ? Number((sessionSpent / sessionActual).toFixed(2)) : null,
986
+ billedUsd: Number(billedWithActual.toFixed(6)),
987
+ marginUsd: Number((billedWithActual - sessionActual).toFixed(6)),
988
+ markupX: sessionActual > 0 ? Number((billedWithActual / sessionActual).toFixed(2)) : null,
986
989
  },
987
990
  mcp: `${self.replace(/\/v1$/, '')}/mcp`,
988
991
  upstream: config.apiBase,
@@ -1268,6 +1271,21 @@ export async function startProxy({ silent = false, requireToken = null, sessionM
1268
1271
  // Retry of a body we answered seconds ago? Serve the cached completion —
1269
1272
  // never pay twice for a harness's reconnect loop.
1270
1273
  const isChat = req.method === 'POST' && (req.url || '').includes('/chat/completions');
1274
+ // GROUND TRUTH ON THE OUTGOING BODY. Three sessions have now reported "no
1275
+ // actual question or task from you" while the proxy log showed a healthy
1276
+ // forward, and two rounds of reasoning about the cut were wrong. Log what
1277
+ // is actually in messages[] on the way out — roles, and the tail of the
1278
+ // last user turn — so the question stops being a matter of opinion.
1279
+ if (process.env.OPENZOO_LOG_BODY === '1' && isChat) {
1280
+ try {
1281
+ const b = JSON.parse(bodyBuf.toString('utf8'));
1282
+ const ms = Array.isArray(b?.messages) ? b.messages : [];
1283
+ const roles = ms.map((m) => (m.role || '?')[0]).join('');
1284
+ const lastUser = [...ms].reverse().find((m) => m.role === 'user');
1285
+ const txt = lastUser ? String(msgText(lastUser)).slice(-160).replace(/\s+/g, ' ') : '(NO USER MESSAGE)';
1286
+ log(` OUT roles=${roles} n=${ms.length} lastUser="${txt}"`);
1287
+ } catch { /* not json */ }
1288
+ }
1271
1289
  const rKey = isChat ? replayKey(bodyBuf, req.headers) : null;
1272
1290
  if (rKey) {
1273
1291
  const hit = replayGet(rKey);
@@ -1436,6 +1454,15 @@ export async function startProxy({ silent = false, requireToken = null, sessionM
1436
1454
  if (typeof data?.usage?.cost === 'number' && data.usage.cost >= 0) {
1437
1455
  sessionActual += data.usage.cost;
1438
1456
  actualCalls += 1;
1457
+ // PAIR THE NUMERATOR WITH THE DENOMINATOR. sessionSpent is summed on
1458
+ // three paths and sessionActual on two, so markupX divided ALL billed
1459
+ // by the SUBSET that reported a real cost — a 402-receipt call added
1460
+ // to billed and nothing to real, and the ratio read 12.55x on a stack
1461
+ // running at ~1.0x. Track the billed side of exactly the calls whose
1462
+ // cost we actually learned.
1463
+ // Both figures ride the SAME response object, so read them together
1464
+ // rather than carrying one across sites and hoping the order holds.
1465
+ billedWithActual += Number(data?.x402?.billedUsd) || 0;
1439
1466
  }
1440
1467
  // PREPAID CALLS STILL COST MONEY. The block above only meters calls
1441
1468
  // where THIS proxy answered a 402 and paid. When prepaid credit covers
@@ -1513,7 +1540,7 @@ export async function startProxy({ silent = false, requireToken = null, sessionM
1513
1540
  sessionSpent += x.billedUsd;
1514
1541
  sessionCogs += typeof x.cogsUsd === 'number' ? x.cogsUsd : x.billedUsd / MARKUP;
1515
1542
  sessionDirect += typeof x.directUsd === 'number' ? x.directUsd : x.billedUsd;
1516
- if (typeof x.actualUsd === 'number' && x.actualUsd >= 0) { sessionActual += x.actualUsd; actualCalls += 1; }
1543
+ if (typeof x.actualUsd === 'number' && x.actualUsd >= 0) { sessionActual += x.actualUsd; actualCalls += 1; billedWithActual += x.billedUsd || 0; }
1517
1544
  if (didSpill) {
1518
1545
  const lc = x.lecore || {};
1519
1546
  log(`spill priced (streamed): ${x.pricing} · basis ${x.counterfactualTokensUsed ?? '?'} tok vs sent ${lc.tokensBefore ?? '?'} -> ${lc.tokensAfter ?? '?'} · billed ${(x.billedUsd ?? 0).toFixed(5)} direct ${(x.directUsd ?? 0).toFixed(5)}`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "openzoo",
3
- "version": "0.48.62",
3
+ "version": "0.48.63",
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",