openzoo 0.51.23 → 0.51.24
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 +79 -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,25 @@ export async function startProxy({ silent = false, requireToken = null, sessionM
|
|
|
958
998
|
result = await client.fetch(url, init);
|
|
959
999
|
}
|
|
960
1000
|
}
|
|
1001
|
+
// A PAID 400 `invalid_tools` on a Responses body we did not flatten:
|
|
1002
|
+
// this model is a function-tools-only door we had not learned yet.
|
|
1003
|
+
// Flatten, remember it, and buy the turn once more — one billed miss
|
|
1004
|
+
// instead of every turn for the rest of the session.
|
|
1005
|
+
if (isResponses && !nsMap && result.response?.status === 400) {
|
|
1006
|
+
let e400 = null;
|
|
1007
|
+
try { e400 = await result.response.clone().json(); } catch { e400 = null; }
|
|
1008
|
+
if (isInvalidToolsError(e400)) {
|
|
1009
|
+
let flat = null;
|
|
1010
|
+
try { flat = flattenResponsesTools(JSON.parse(String(init.body || '{}'))); } catch { flat = null; }
|
|
1011
|
+
if (flat?.map) {
|
|
1012
|
+
flattenLearned.add(outageKey(init));
|
|
1013
|
+
nsMap = flat.map;
|
|
1014
|
+
init = { ...init, body: JSON.stringify(flat.body) };
|
|
1015
|
+
say(` invalid_tools from ${outageKey(init)} — flattened ${flat.map.size} namespaced tools${flat.dropped.length ? `, dropped ${flat.dropped.join(', ')}` : ''}; retrying`);
|
|
1016
|
+
result = await client.fetch(url, init);
|
|
1017
|
+
}
|
|
1018
|
+
}
|
|
1019
|
+
}
|
|
961
1020
|
const { response, paid, receipt, accept } = result;
|
|
962
1021
|
if (paid && receipt) {
|
|
963
1022
|
if (receipt.ok && typeof receipt.billedUsd === 'number') {
|
|
@@ -1038,6 +1097,16 @@ export async function startProxy({ silent = false, requireToken = null, sessionM
|
|
|
1038
1097
|
res.end(JSON.stringify(data));
|
|
1039
1098
|
return;
|
|
1040
1099
|
}
|
|
1100
|
+
// Un-flatten a JSON Responses reply so Codex can route the calls.
|
|
1101
|
+
if (nsMap && data?.object === 'response') {
|
|
1102
|
+
restoreResponsesPayload(data, nsMap);
|
|
1103
|
+
const h = { 'content-type': 'application/json' };
|
|
1104
|
+
const settleHdr = response.headers.get('x-payment-response');
|
|
1105
|
+
if (settleHdr) h['x-payment-response'] = settleHdr;
|
|
1106
|
+
res.writeHead(200, h);
|
|
1107
|
+
res.end(JSON.stringify(data));
|
|
1108
|
+
return;
|
|
1109
|
+
}
|
|
1041
1110
|
}
|
|
1042
1111
|
// A STREAMED call is metered from the gateway's trailing SSE comment —
|
|
1043
1112
|
// same figures the JSON path reads out of `data.x402`, same counters, so
|
|
@@ -1085,7 +1154,7 @@ export async function startProxy({ silent = false, requireToken = null, sessionM
|
|
|
1085
1154
|
paymentError(res, req, init, paid ? copy.status : 402, msg);
|
|
1086
1155
|
return;
|
|
1087
1156
|
}
|
|
1088
|
-
await relay(res, response, meterStreamed);
|
|
1157
|
+
await relay(res, response, meterStreamed, nsMap ? sseNamespaceRestorer(nsMap) : null);
|
|
1089
1158
|
} catch (err) {
|
|
1090
1159
|
if (err instanceof QuoteTooHighError) {
|
|
1091
1160
|
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.24",
|
|
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",
|