openzoo 0.51.23 → 0.51.25
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 +101 -10
- package/lib/responsestools.js +0 -0
- package/package.json +1 -1
package/lib/proxy.js
CHANGED
|
@@ -27,16 +27,27 @@ import { detectModelCommand, applyModelCommand, serveModelCommand, readOverride
|
|
|
27
27
|
* Free read, but harnesses probe on every /model keystroke confirmation —
|
|
28
28
|
* without a cache each probe is an upstream round trip.
|
|
29
29
|
*/
|
|
30
|
-
let catalogIdsMemo = { at: 0, ids: [] };
|
|
30
|
+
let catalogIdsMemo = { at: 0, ids: [], house: new Set() };
|
|
31
31
|
async function catalogIdsCached(modelsUrl, headers) {
|
|
32
32
|
if (Date.now() - catalogIdsMemo.at < 5 * 60_000 && catalogIdsMemo.ids.length) return catalogIdsMemo.ids;
|
|
33
33
|
const r = await fetch(modelsUrl, { headers, signal: AbortSignal.timeout(8000) });
|
|
34
34
|
if (!r.ok) throw new Error(`catalog ${r.status}`);
|
|
35
35
|
const payload = await r.json();
|
|
36
|
-
const
|
|
37
|
-
|
|
36
|
+
const rows = quoteableRows(payload?.data);
|
|
37
|
+
const ids = rows.map((m) => m.id);
|
|
38
|
+
// House doors (`owned_by: "openzoo"` — abliterated-model*, openzoo/auto)
|
|
39
|
+
// take plain `function` tools only; see responsestools.js.
|
|
40
|
+
const house = new Set(rows.filter((m) => m.owned_by === 'openzoo').map((m) => m.id));
|
|
41
|
+
if (ids.length) catalogIdsMemo = { at: Date.now(), ids, house };
|
|
38
42
|
return ids;
|
|
39
43
|
}
|
|
44
|
+
/** Models we have SEEN reject Responses tool groups with `invalid_tools`,
|
|
45
|
+
* so the next request flattens before paying instead of after. */
|
|
46
|
+
const flattenLearned = new Set();
|
|
47
|
+
function wantsFlatTools(model) {
|
|
48
|
+
const id = String(model || '');
|
|
49
|
+
return flattenLearned.has(id) || catalogIdsMemo.house.has(id) || /^abliterated-model/.test(id);
|
|
50
|
+
}
|
|
40
51
|
import { withNamespace } from './namespace.js';
|
|
41
52
|
import { loadSessionSpend, saveSessionSpend, sessionSpendFile } from './session.js';
|
|
42
53
|
import { creditBalance, quotedPrices } from './info.js';
|
|
@@ -44,6 +55,7 @@ import { priceHoldings } from './livestatus.js';
|
|
|
44
55
|
import { receiptUsedCogs, receiptDirectUsd, pairActualBilled } from './racesettle.js';
|
|
45
56
|
import { fetchHeaders } from './fetch.js';
|
|
46
57
|
import { attachX402Proof } from './spendProof.js';
|
|
58
|
+
import { flattenResponsesTools, restoreResponsesPayload, sseNamespaceRestorer, isInvalidToolsError } from './responsestools.js';
|
|
47
59
|
|
|
48
60
|
/** Kill whatever is LISTEN on this port except this process.
|
|
49
61
|
* lsof first (macOS), then fuser, then /proc (Omarchy/Arch with neither).
|
|
@@ -224,7 +236,7 @@ async function readBody(req) {
|
|
|
224
236
|
*
|
|
225
237
|
* Sniffing NEVER delays a byte: each chunk is written to the client first and
|
|
226
238
|
* only then scanned. */
|
|
227
|
-
function relay(res, upstream, onReceipt) {
|
|
239
|
+
function relay(res, upstream, onReceipt, rewriter = null) {
|
|
228
240
|
const headers = {};
|
|
229
241
|
upstream.headers.forEach((v, k) => {
|
|
230
242
|
if (!['transfer-encoding', 'connection', 'content-encoding', 'content-length'].includes(k)) headers[k] = v;
|
|
@@ -237,13 +249,20 @@ function relay(res, upstream, onReceipt) {
|
|
|
237
249
|
body.on('error', () => res.destroy());
|
|
238
250
|
res.on('close', () => body.destroy());
|
|
239
251
|
body.on('end', resolve);
|
|
240
|
-
if (!sse || typeof onReceipt !== 'function') { body.pipe(res); return; }
|
|
241
|
-
const observe = billingObserver(onReceipt);
|
|
252
|
+
if (!sse || (typeof onReceipt !== 'function' && !rewriter)) { body.pipe(res); return; }
|
|
253
|
+
const observe = typeof onReceipt === 'function' ? billingObserver(onReceipt) : () => {};
|
|
254
|
+
// A rewriter (Responses tool un-flattening) needs whole `data:` lines, so
|
|
255
|
+
// it re-chunks on newlines; the billing sniff reads the rewritten text,
|
|
256
|
+
// which leaves the trailing receipt comment untouched.
|
|
242
257
|
body.on('data', (c) => {
|
|
243
|
-
|
|
244
|
-
|
|
258
|
+
const text = rewriter ? rewriter.push(c.toString('utf8')) : c;
|
|
259
|
+
if (text.length) res.write(text);
|
|
260
|
+
observe(text.toString('utf8'));
|
|
261
|
+
});
|
|
262
|
+
body.on('end', () => {
|
|
263
|
+
if (rewriter) { const rest = rewriter.flush(); if (rest) { res.write(rest); observe(rest); } }
|
|
264
|
+
res.end();
|
|
245
265
|
});
|
|
246
|
-
body.on('end', () => res.end());
|
|
247
266
|
});
|
|
248
267
|
}
|
|
249
268
|
|
|
@@ -769,9 +788,13 @@ export async function startProxy({ silent = false, requireToken = null, sessionM
|
|
|
769
788
|
// The BODY IS NOT TOUCHED — the parse below is read-only, to learn the
|
|
770
789
|
// client's streaming intent for the JSON→SSE compatibility path.
|
|
771
790
|
const isChat = req.method === 'POST' && rawPath.includes('/chat/completions');
|
|
791
|
+
const isResponses = req.method === 'POST' && /\/responses$/.test(rawPath);
|
|
772
792
|
const isPaidPost = req.method === 'POST'
|
|
773
793
|
&& /\/(chat\/completions|completions|messages|responses)$/.test(rawPath);
|
|
774
794
|
let wantsStream = false;
|
|
795
|
+
// Flat-name → {namespace, name} for this request's Responses tools; set
|
|
796
|
+
// only when the body was flattened, so the reply can be un-flattened.
|
|
797
|
+
let nsMap = null;
|
|
775
798
|
// RECEIPTS ARE OPT-OUT. `disableStats: true` in the body (or the
|
|
776
799
|
// x-openzoo-disable-stats header) means this caller does not want the x402
|
|
777
800
|
// block, so we neither attach our settle proof nor pass the gateway's
|
|
@@ -843,6 +866,23 @@ export async function startProxy({ silent = false, requireToken = null, sessionM
|
|
|
843
866
|
}
|
|
844
867
|
}
|
|
845
868
|
} catch { /* not JSON */ }
|
|
869
|
+
// RESPONSES TOOL GROUPS vs HOUSE DOORS. Codex sends `namespace` tool
|
|
870
|
+
// groups + `web_search`; abliterated-model* rejects both with a PAID
|
|
871
|
+
// 400 `invalid_tools`. Flatten before the wire for models known to
|
|
872
|
+
// need it (catalog house doors, or learned from a prior 400 below).
|
|
873
|
+
if (isResponses) {
|
|
874
|
+
try {
|
|
875
|
+
const parsed = JSON.parse(bodyBuf.toString('utf8'));
|
|
876
|
+
if (parsed && wantsFlatTools(parsed.model)) {
|
|
877
|
+
const flat = flattenResponsesTools(parsed);
|
|
878
|
+
if (flat.map) {
|
|
879
|
+
nsMap = flat.map;
|
|
880
|
+
bodyBuf = Buffer.from(JSON.stringify(flat.body));
|
|
881
|
+
say(` tools flattened for ${parsed.model}: ${flat.map.size} namespaced → function${flat.dropped.length ? `, dropped ${flat.dropped.join(', ')}` : ''}`);
|
|
882
|
+
}
|
|
883
|
+
}
|
|
884
|
+
} catch { /* not JSON */ }
|
|
885
|
+
}
|
|
846
886
|
}
|
|
847
887
|
|
|
848
888
|
// Retry of a body we answered seconds ago? Serve the cached completion —
|
|
@@ -958,6 +998,47 @@ export async function startProxy({ silent = false, requireToken = null, sessionM
|
|
|
958
998
|
result = await client.fetch(url, init);
|
|
959
999
|
}
|
|
960
1000
|
}
|
|
1001
|
+
// A 5xx from the gateway: its upstream errored on a retired/renamed door
|
|
1002
|
+
// — e.g. gpt-6-astra maps to an upstream id the provider 404s
|
|
1003
|
+
// (`unknown model openai-gpt-6-astra`). It comes back BOTH ways: an
|
|
1004
|
+
// UNPAID 500 (the id cannot be priced, so no x402 quote is even reached)
|
|
1005
|
+
// and a PAID 502 (settled, then the upstream call failed). Left raw,
|
|
1006
|
+
// either makes the harness storm retries ("Reconnecting 5/5"). Route
|
|
1007
|
+
// once to the x402 door and gate the model 60s, same as the credits
|
|
1008
|
+
// outage above. 501/505 excluded — shape/protocol errors a different
|
|
1009
|
+
// model would hit too.
|
|
1010
|
+
const RETRYABLE_5XX = new Set([500, 502, 503, 504]);
|
|
1011
|
+
if (!usedFallback && FALLBACK_MODEL && outageKey(init) !== FALLBACK_MODEL
|
|
1012
|
+
&& RETRYABLE_5XX.has(result.response?.status)) {
|
|
1013
|
+
const st = result.response.status;
|
|
1014
|
+
let detail = '';
|
|
1015
|
+
try { const j = await result.response.clone().json(); detail = String(j?.error?.message || j?.error || '').slice(0, 120); } catch { /* opaque 5xx */ }
|
|
1016
|
+
const msg = `openzoo gateway upstream errored (HTTP ${st}${detail ? `: ${detail}` : ''}) for ${outageKey(init)}. Routing to ${FALLBACK_MODEL} (x402 door) for 60s.`;
|
|
1017
|
+
upstreamOutage_.set(outageKey(init), { until: Date.now() + 60_000, msg });
|
|
1018
|
+
log(`upstream ${st}: ${outageKey(init)} -> ${FALLBACK_MODEL}${detail ? ` (${detail})` : ''}`);
|
|
1019
|
+
init = withModel(init, FALLBACK_MODEL);
|
|
1020
|
+
usedFallback = true;
|
|
1021
|
+
result = await client.fetch(url, init);
|
|
1022
|
+
}
|
|
1023
|
+
// A PAID 400 `invalid_tools` on a Responses body we did not flatten:
|
|
1024
|
+
// this model is a function-tools-only door we had not learned yet.
|
|
1025
|
+
// Flatten, remember it, and buy the turn once more — one billed miss
|
|
1026
|
+
// instead of every turn for the rest of the session.
|
|
1027
|
+
if (isResponses && !nsMap && result.response?.status === 400) {
|
|
1028
|
+
let e400 = null;
|
|
1029
|
+
try { e400 = await result.response.clone().json(); } catch { e400 = null; }
|
|
1030
|
+
if (isInvalidToolsError(e400)) {
|
|
1031
|
+
let flat = null;
|
|
1032
|
+
try { flat = flattenResponsesTools(JSON.parse(String(init.body || '{}'))); } catch { flat = null; }
|
|
1033
|
+
if (flat?.map) {
|
|
1034
|
+
flattenLearned.add(outageKey(init));
|
|
1035
|
+
nsMap = flat.map;
|
|
1036
|
+
init = { ...init, body: JSON.stringify(flat.body) };
|
|
1037
|
+
say(` invalid_tools from ${outageKey(init)} — flattened ${flat.map.size} namespaced tools${flat.dropped.length ? `, dropped ${flat.dropped.join(', ')}` : ''}; retrying`);
|
|
1038
|
+
result = await client.fetch(url, init);
|
|
1039
|
+
}
|
|
1040
|
+
}
|
|
1041
|
+
}
|
|
961
1042
|
const { response, paid, receipt, accept } = result;
|
|
962
1043
|
if (paid && receipt) {
|
|
963
1044
|
if (receipt.ok && typeof receipt.billedUsd === 'number') {
|
|
@@ -1038,6 +1119,16 @@ export async function startProxy({ silent = false, requireToken = null, sessionM
|
|
|
1038
1119
|
res.end(JSON.stringify(data));
|
|
1039
1120
|
return;
|
|
1040
1121
|
}
|
|
1122
|
+
// Un-flatten a JSON Responses reply so Codex can route the calls.
|
|
1123
|
+
if (nsMap && data?.object === 'response') {
|
|
1124
|
+
restoreResponsesPayload(data, nsMap);
|
|
1125
|
+
const h = { 'content-type': 'application/json' };
|
|
1126
|
+
const settleHdr = response.headers.get('x-payment-response');
|
|
1127
|
+
if (settleHdr) h['x-payment-response'] = settleHdr;
|
|
1128
|
+
res.writeHead(200, h);
|
|
1129
|
+
res.end(JSON.stringify(data));
|
|
1130
|
+
return;
|
|
1131
|
+
}
|
|
1041
1132
|
}
|
|
1042
1133
|
// A STREAMED call is metered from the gateway's trailing SSE comment —
|
|
1043
1134
|
// same figures the JSON path reads out of `data.x402`, same counters, so
|
|
@@ -1085,7 +1176,7 @@ export async function startProxy({ silent = false, requireToken = null, sessionM
|
|
|
1085
1176
|
paymentError(res, req, init, paid ? copy.status : 402, msg);
|
|
1086
1177
|
return;
|
|
1087
1178
|
}
|
|
1088
|
-
await relay(res, response, meterStreamed);
|
|
1179
|
+
await relay(res, response, meterStreamed, nsMap ? sseNamespaceRestorer(nsMap) : null);
|
|
1089
1180
|
} catch (err) {
|
|
1090
1181
|
if (err instanceof QuoteTooHighError) {
|
|
1091
1182
|
log(err.message);
|
|
Binary file
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "openzoo",
|
|
3
|
-
"version": "0.51.
|
|
3
|
+
"version": "0.51.25",
|
|
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",
|