openzoo 0.9.2 → 0.9.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.
- package/lib/proxy.js +80 -7
- package/package.json +1 -1
package/lib/proxy.js
CHANGED
|
@@ -2,10 +2,11 @@ import http from 'node:http';
|
|
|
2
2
|
import crypto from 'node:crypto';
|
|
3
3
|
import { Readable } from 'node:stream';
|
|
4
4
|
import {
|
|
5
|
-
config, FUNDING_ASSETS, fundingLine, liveRails, railFundingHint, railFundingAddresses, unfundableRails, RAIL_FUNDING,
|
|
5
|
+
config, FUNDING_ASSETS, EVM_FUNDING_ASSETS, evmRpcFor, fundingLine, liveRails, railFundingHint, railFundingAddresses, unfundableRails, RAIL_FUNDING,
|
|
6
6
|
} from './config.js';
|
|
7
7
|
import { PayClient, QuoteTooHighError, UnderfundedError } from './pay.js';
|
|
8
8
|
import { tokenBalance } from './x402.js';
|
|
9
|
+
import { evmTokenBalance } from './evm.js';
|
|
9
10
|
import { bindCorpus, contextCacheDisabled, BIND_MIN_CHARS } from './hrr.js';
|
|
10
11
|
import { maybeRewriteModel, rewritablePath, augmentModelList, ALIAS_IDS } from './models.js';
|
|
11
12
|
import { forgetContext } from './contexts.js';
|
|
@@ -56,6 +57,42 @@ function jsonErr(res, status, message, extraFields = {}) {
|
|
|
56
57
|
|
|
57
58
|
const mb = (n) => (n / 1048576).toFixed(1);
|
|
58
59
|
|
|
60
|
+
/**
|
|
61
|
+
* Every fundable balance across all three chains, for the startup line and
|
|
62
|
+
* the live refresh. Each read is independent and advisory — one lagging RPC
|
|
63
|
+
* drops its entry rather than blanking the whole line.
|
|
64
|
+
*/
|
|
65
|
+
async function snapshotBalances(client) {
|
|
66
|
+
const out = [];
|
|
67
|
+
try {
|
|
68
|
+
const bals = await Promise.all(
|
|
69
|
+
FUNDING_ASSETS.map((a) => tokenBalance(client.connection, client.keypair.publicKey, a.mint)),
|
|
70
|
+
);
|
|
71
|
+
FUNDING_ASSETS.forEach((a, i) => out.push({ symbol: a.symbol, ui: Number(bals[i].ui ?? 0), chain: 'solana' }));
|
|
72
|
+
} catch { /* Solana RPC hiccup — EVM entries still report */ }
|
|
73
|
+
const owner = client.evmAddress;
|
|
74
|
+
if (owner) {
|
|
75
|
+
await Promise.all(Object.entries(EVM_FUNDING_ASSETS).flatMap(([rail, assets]) => assets.map(async (a) => {
|
|
76
|
+
try {
|
|
77
|
+
const raw = await evmTokenBalance({ rpcUrl: evmRpcFor(rail), token: a.address, owner });
|
|
78
|
+
out.push({ symbol: a.symbol, ui: Number(raw) / 10 ** a.decimals, chain: rail });
|
|
79
|
+
} catch { /* advisory */ }
|
|
80
|
+
})));
|
|
81
|
+
}
|
|
82
|
+
// Parallel reads land in racy order; sort so the printed line is stable
|
|
83
|
+
// and diffs against the previous snapshot read cleanly.
|
|
84
|
+
const rank = { solana: 0, base: 1, robinhood: 2 };
|
|
85
|
+
return out.sort((a, b) => (rank[a.chain] ?? 9) - (rank[b.chain] ?? 9) || a.symbol.localeCompare(b.symbol));
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** Solana entries always show; EVM entries only once they hold something. */
|
|
89
|
+
function balanceLine(snap) {
|
|
90
|
+
return snap
|
|
91
|
+
.filter((b) => b.chain === 'solana' || b.ui > 0)
|
|
92
|
+
.map((b) => `${b.ui} ${b.symbol}${b.chain !== 'solana' ? ` (${b.chain})` : ''}`)
|
|
93
|
+
.join(' · ');
|
|
94
|
+
}
|
|
95
|
+
|
|
59
96
|
/**
|
|
60
97
|
* The zoo answers chat completions as ONE JSON object (it settles payment
|
|
61
98
|
* before serving — there is nothing to stream until generation is done).
|
|
@@ -175,6 +212,17 @@ export async function startProxy({ silent = false, requireToken = null, sessionM
|
|
|
175
212
|
const log = silent ? () => {} : (...a) => console.log(...a);
|
|
176
213
|
let sessionSpent = 0;
|
|
177
214
|
let tunnelSpent = 0;
|
|
215
|
+
// Live balance refresh state — the real implementation is assigned in the
|
|
216
|
+
// banner section below; the handler only ever calls scheduleRefresh().
|
|
217
|
+
let lastSnap = null;
|
|
218
|
+
let refreshBalances = async () => {};
|
|
219
|
+
let refreshPending = false;
|
|
220
|
+
const scheduleRefresh = (ms) => {
|
|
221
|
+
if (silent || refreshPending) return;
|
|
222
|
+
refreshPending = true;
|
|
223
|
+
const t = setTimeout(async () => { refreshPending = false; await refreshBalances(); }, ms);
|
|
224
|
+
t.unref?.();
|
|
225
|
+
};
|
|
178
226
|
// Set once cloudflared is up (see below). Gating keys off the REQUEST's
|
|
179
227
|
// origin, not off whether the URL exists yet, so there is no startup window
|
|
180
228
|
// where public traffic slips through ungated.
|
|
@@ -319,6 +367,7 @@ export async function startProxy({ silent = false, requireToken = null, sessionM
|
|
|
319
367
|
if (requireToken) log(`${line} · session $${sessionSpent.toFixed(6)}`);
|
|
320
368
|
else if (viaTunnel) log(`${line} · public-url session $${tunnelSpent.toFixed(6)}`);
|
|
321
369
|
else log(line);
|
|
370
|
+
scheduleRefresh(4000); // settlement lands on-chain in a few seconds
|
|
322
371
|
}
|
|
323
372
|
// Chat completions come back as one JSON object (settle-before-serve).
|
|
324
373
|
// Cache it against retries, and if the harness asked to stream, honour
|
|
@@ -366,15 +415,39 @@ export async function startProxy({ silent = false, requireToken = null, sessionM
|
|
|
366
415
|
console.log(`wallet (fund me) · solana: ${client.address}`);
|
|
367
416
|
if (client.evmAddress) console.log(`wallet (fund me) · evm (base / robinhood): ${client.evmAddress}`);
|
|
368
417
|
try {
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
)
|
|
372
|
-
const parts = FUNDING_ASSETS.map((a, i) => `${bals[i].ui ?? 0} ${a.symbol}`);
|
|
373
|
-
console.log(`balance: ${parts.join(' · ')}`);
|
|
374
|
-
if (!bals.some((b) => b.raw)) {
|
|
418
|
+
lastSnap = await snapshotBalances(client);
|
|
419
|
+
console.log(`balance: ${balanceLine(lastSnap) || '(no RPC reachable — advisory only)'}`);
|
|
420
|
+
if (!lastSnap.some((b) => b.ui > 0)) {
|
|
375
421
|
console.log(`fund it: ${fundingLine('the address above')} — a few cents goes a long way.`);
|
|
376
422
|
}
|
|
377
423
|
} catch { /* RPC hiccup: balance is advisory */ }
|
|
424
|
+
// LIVE REFRESH: the startup line goes stale the moment a call settles or
|
|
425
|
+
// the user funds mid-session. Poll on an interval (and shortly after each
|
|
426
|
+
// paid call), print ONLY on change, and call out arrivals explicitly so
|
|
427
|
+
// "did my top-up land?" answers itself in the running log.
|
|
428
|
+
refreshBalances = async () => {
|
|
429
|
+
try {
|
|
430
|
+
const snap = await snapshotBalances(client);
|
|
431
|
+
if (!snap.length) return;
|
|
432
|
+
const prev = new Map((lastSnap || []).map((b) => [`${b.chain}:${b.symbol}`, b.ui]));
|
|
433
|
+
const changed = snap.some((b) => Math.abs((prev.get(`${b.chain}:${b.symbol}`) ?? 0) - b.ui) > 1e-9)
|
|
434
|
+
|| snap.length !== (lastSnap || []).length;
|
|
435
|
+
if (!changed) return;
|
|
436
|
+
if (lastSnap) {
|
|
437
|
+
for (const b of snap) {
|
|
438
|
+
const gain = b.ui - (prev.get(`${b.chain}:${b.symbol}`) ?? 0);
|
|
439
|
+
if (gain > 1e-9) console.log(`funding arrived: +${gain.toFixed(6)} ${b.symbol}${b.chain !== 'solana' ? ` (${b.chain})` : ''}`);
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
lastSnap = snap;
|
|
443
|
+
console.log(`balance: ${balanceLine(snap)}`);
|
|
444
|
+
} catch { /* advisory — never noisy on RPC trouble */ }
|
|
445
|
+
};
|
|
446
|
+
const pollSecs = Number(process.env.OPENZOO_BALANCE_POLL_SECS ?? 45);
|
|
447
|
+
if (pollSecs > 0) {
|
|
448
|
+
const timer = setInterval(refreshBalances, pollSecs * 1000);
|
|
449
|
+
timer.unref?.();
|
|
450
|
+
}
|
|
378
451
|
// Which rails the zoo will actually settle right now, straight off a live
|
|
379
452
|
// 402 — so nobody funds a lane the resource is not currently offering.
|
|
380
453
|
try {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "openzoo",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.3",
|
|
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",
|