@blockrun/clawrouter 0.12.309 → 0.12.310
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/dist/cli.js +52 -18
- package/dist/index.js +52 -18
- package/docs/features.md +3 -1
- package/package.json +1 -1
package/dist/cli.js
CHANGED
|
@@ -66170,7 +66170,7 @@ var init_response_cache = __esm({
|
|
|
66170
66170
|
*/
|
|
66171
66171
|
shouldCache(body, headers) {
|
|
66172
66172
|
if (!this.config.enabled) return false;
|
|
66173
|
-
if (headers?.["cache-control"]
|
|
66173
|
+
if (/\bno-(?:cache|store)\b/i.test(headers?.["cache-control"] ?? "")) {
|
|
66174
66174
|
return false;
|
|
66175
66175
|
}
|
|
66176
66176
|
try {
|
|
@@ -125890,7 +125890,7 @@ var require_webidl = __commonJS({
|
|
|
125890
125890
|
var require_util2 = __commonJS({
|
|
125891
125891
|
"node_modules/undici/lib/web/fetch/util.js"(exports, module) {
|
|
125892
125892
|
"use strict";
|
|
125893
|
-
var { Transform } = __require("stream");
|
|
125893
|
+
var { Transform: Transform2 } = __require("stream");
|
|
125894
125894
|
var zlib2 = __require("zlib");
|
|
125895
125895
|
var { redirectStatusSet, referrerPolicyTokens, badPortsSet } = require_constants3();
|
|
125896
125896
|
var { getGlobalOrigin } = require_global();
|
|
@@ -126484,7 +126484,7 @@ var require_util2 = __commonJS({
|
|
|
126484
126484
|
contentRange += isomorphicEncode(`${fullLength}`);
|
|
126485
126485
|
return contentRange;
|
|
126486
126486
|
}
|
|
126487
|
-
var InflateStream = class extends
|
|
126487
|
+
var InflateStream = class extends Transform2 {
|
|
126488
126488
|
#zlibOptions;
|
|
126489
126489
|
/** @param {zlib.ZlibOptions} [zlibOptions] */
|
|
126490
126490
|
constructor(zlibOptions2) {
|
|
@@ -140329,13 +140329,13 @@ var require_mock_pool = __commonJS({
|
|
|
140329
140329
|
var require_pending_interceptors_formatter = __commonJS({
|
|
140330
140330
|
"node_modules/undici/lib/mock/pending-interceptors-formatter.js"(exports, module) {
|
|
140331
140331
|
"use strict";
|
|
140332
|
-
var { Transform } = __require("stream");
|
|
140332
|
+
var { Transform: Transform2 } = __require("stream");
|
|
140333
140333
|
var { Console } = __require("console");
|
|
140334
140334
|
var PERSISTENT = process.versions.icu ? "\u2705" : "Y ";
|
|
140335
140335
|
var NOT_PERSISTENT = process.versions.icu ? "\u274C" : "N ";
|
|
140336
140336
|
module.exports = class PendingInterceptorsFormatter {
|
|
140337
140337
|
constructor({ disableColors } = {}) {
|
|
140338
|
-
this.transform = new
|
|
140338
|
+
this.transform = new Transform2({
|
|
140339
140339
|
transform(chunk, _enc, cb) {
|
|
140340
140340
|
cb(null, chunk);
|
|
140341
140341
|
}
|
|
@@ -152637,7 +152637,7 @@ var require_eventsource_stream = __commonJS({
|
|
|
152637
152637
|
"node_modules/undici/lib/web/eventsource/eventsource-stream.js"(exports, module) {
|
|
152638
152638
|
"use strict";
|
|
152639
152639
|
var buffer2 = __require("buffer");
|
|
152640
|
-
var { Transform } = __require("stream");
|
|
152640
|
+
var { Transform: Transform2 } = __require("stream");
|
|
152641
152641
|
var { isASCIINumber, isValidLastEventId } = require_util6();
|
|
152642
152642
|
var BOM = [239, 187, 191];
|
|
152643
152643
|
var LF = 10;
|
|
@@ -152687,7 +152687,7 @@ var require_eventsource_stream = __commonJS({
|
|
|
152687
152687
|
error62.aborted = false;
|
|
152688
152688
|
return error62;
|
|
152689
152689
|
}
|
|
152690
|
-
var EventSourceStream = class extends
|
|
152690
|
+
var EventSourceStream = class extends Transform2 {
|
|
152691
152691
|
/**
|
|
152692
152692
|
* @type {eventSourceSettings}
|
|
152693
152693
|
*/
|
|
@@ -154811,7 +154811,7 @@ var init_v1 = __esm({
|
|
|
154811
154811
|
import { AsyncLocalStorage as AsyncLocalStorage2 } from "async_hooks";
|
|
154812
154812
|
import { createHmac } from "crypto";
|
|
154813
154813
|
import { createServer } from "http";
|
|
154814
|
-
import { finished, Readable as Readable2 } from "stream";
|
|
154814
|
+
import { finished, Readable as Readable2, Transform } from "stream";
|
|
154815
154815
|
import { pipeline } from "stream/promises";
|
|
154816
154816
|
import { homedir as homedir13 } from "os";
|
|
154817
154817
|
import { join as join17 } from "path";
|
|
@@ -155747,9 +155747,32 @@ function withZeroCapGuard(inner, isFree) {
|
|
|
155747
155747
|
return inner(input2, init2);
|
|
155748
155748
|
};
|
|
155749
155749
|
}
|
|
155750
|
+
function isMessagesPath(url3) {
|
|
155751
|
+
return url3 === "/v1/messages" || (url3?.startsWith("/v1/messages?") ?? false);
|
|
155752
|
+
}
|
|
155753
|
+
async function relayWithIdleTimeout(source, destination, idleMs, signal) {
|
|
155754
|
+
let timer2;
|
|
155755
|
+
const arm = () => {
|
|
155756
|
+
clearTimeout(timer2);
|
|
155757
|
+
timer2 = setTimeout(() => watchdog.destroy(new Error("Body read timeout")), idleMs);
|
|
155758
|
+
};
|
|
155759
|
+
const watchdog = new Transform({
|
|
155760
|
+
transform(chunk, _encoding, done) {
|
|
155761
|
+
arm();
|
|
155762
|
+
done(null, chunk);
|
|
155763
|
+
}
|
|
155764
|
+
});
|
|
155765
|
+
arm();
|
|
155766
|
+
try {
|
|
155767
|
+
await pipeline(source, watchdog, destination, { signal });
|
|
155768
|
+
} finally {
|
|
155769
|
+
clearTimeout(timer2);
|
|
155770
|
+
}
|
|
155771
|
+
}
|
|
155750
155772
|
async function proxyPaidApiRequest(req, res, apiBase, payFetch, getActualPaymentUsd, accountPassthrough = false, authMode = "wallet", capRequest) {
|
|
155751
155773
|
const startTime = Date.now();
|
|
155752
155774
|
const upstreamUrl = `${apiBase}${req.url}`;
|
|
155775
|
+
const isMessages = isMessagesPath(req.url);
|
|
155753
155776
|
const isBlockrunExa = req.url?.startsWith("/v1/exa/") ?? false;
|
|
155754
155777
|
const isModalSandbox = req.url?.startsWith("/v1/modal/") ?? false;
|
|
155755
155778
|
const isPhone = (req.url?.startsWith("/v1/phone/") ?? false) || (req.url?.startsWith("/v1/voice/") ?? false);
|
|
@@ -155791,8 +155814,8 @@ async function proxyPaidApiRequest(req, res, apiBase, payFetch, getActualPayment
|
|
|
155791
155814
|
if (accountPassthrough || authMode === "api-key") {
|
|
155792
155815
|
responseHeaders["cache-control"] = "no-store";
|
|
155793
155816
|
}
|
|
155794
|
-
res.writeHead(upstream.status, responseHeaders);
|
|
155795
155817
|
if (accountPassthrough) {
|
|
155818
|
+
res.writeHead(upstream.status, responseHeaders);
|
|
155796
155819
|
if (upstream.body) {
|
|
155797
155820
|
await pipeline(
|
|
155798
155821
|
Readable2.fromWeb(upstream.body),
|
|
@@ -155802,12 +155825,23 @@ async function proxyPaidApiRequest(req, res, apiBase, payFetch, getActualPayment
|
|
|
155802
155825
|
} else {
|
|
155803
155826
|
res.end();
|
|
155804
155827
|
}
|
|
155805
|
-
} else {
|
|
155828
|
+
} else if (isMessages) {
|
|
155829
|
+
res.writeHead(upstream.status, responseHeaders);
|
|
155806
155830
|
if (upstream.body) {
|
|
155807
|
-
|
|
155808
|
-
|
|
155809
|
-
|
|
155810
|
-
|
|
155831
|
+
await relayWithIdleTimeout(
|
|
155832
|
+
Readable2.fromWeb(upstream.body),
|
|
155833
|
+
res,
|
|
155834
|
+
MODEL_BODY_READ_TIMEOUT_MS,
|
|
155835
|
+
clientAbort.signal
|
|
155836
|
+
);
|
|
155837
|
+
} else {
|
|
155838
|
+
res.end();
|
|
155839
|
+
}
|
|
155840
|
+
} else {
|
|
155841
|
+
const chunks = upstream.body ? await readBodyWithTimeout(upstream.body, ERROR_BODY_READ_TIMEOUT_MS) : [];
|
|
155842
|
+
res.writeHead(upstream.status, responseHeaders);
|
|
155843
|
+
for (const chunk of chunks) {
|
|
155844
|
+
safeWrite(res, Buffer.from(chunk));
|
|
155811
155845
|
}
|
|
155812
155846
|
res.end();
|
|
155813
155847
|
}
|
|
@@ -157128,8 +157162,8 @@ async function startProxy(options) {
|
|
|
157128
157162
|
}
|
|
157129
157163
|
return;
|
|
157130
157164
|
}
|
|
157131
|
-
const
|
|
157132
|
-
const isVerbatimGatewayPath =
|
|
157165
|
+
const isMessages = isMessagesPath(req.url);
|
|
157166
|
+
const isVerbatimGatewayPath = isMessages || req.url === "/v1/audio/speech" || (req.url?.startsWith("/v1/audio/speech?") ?? false);
|
|
157133
157167
|
if (isVerbatimGatewayPath || req.url?.match(PAID_API_PATH)) {
|
|
157134
157168
|
try {
|
|
157135
157169
|
await proxyPaidApiRequest(
|
|
@@ -157141,7 +157175,7 @@ async function startProxy(options) {
|
|
|
157141
157175
|
false,
|
|
157142
157176
|
authMode,
|
|
157143
157177
|
// $0 is enforced below this, where calls are signed or fetched.
|
|
157144
|
-
|
|
157178
|
+
isMessages && options.maxCostPerRunUsd !== void 0 && options.maxCostPerRunUsd !== 0 ? (body) => capMessagesRequest(body, req.headers, {
|
|
157145
157179
|
usd: options.maxCostPerRunUsd,
|
|
157146
157180
|
mode: options.maxCostPerRunMode ?? "graceful",
|
|
157147
157181
|
authMode,
|
|
@@ -159746,7 +159780,7 @@ data: [DONE]
|
|
|
159746
159780
|
body: responseBody,
|
|
159747
159781
|
completedAt: Date.now()
|
|
159748
159782
|
});
|
|
159749
|
-
if (upstream.status === 200 && responseCache2.shouldCache(body)) {
|
|
159783
|
+
if (upstream.status === 200 && responseCache2.shouldCache(body, reqHeaders)) {
|
|
159750
159784
|
responseCache2.set(cacheKey3, {
|
|
159751
159785
|
body: responseBody,
|
|
159752
159786
|
status: upstream.status,
|
package/dist/index.js
CHANGED
|
@@ -101767,7 +101767,7 @@ var require_webidl = __commonJS({
|
|
|
101767
101767
|
var require_util2 = __commonJS({
|
|
101768
101768
|
"node_modules/undici/lib/web/fetch/util.js"(exports, module) {
|
|
101769
101769
|
"use strict";
|
|
101770
|
-
var { Transform } = __require("stream");
|
|
101770
|
+
var { Transform: Transform2 } = __require("stream");
|
|
101771
101771
|
var zlib2 = __require("zlib");
|
|
101772
101772
|
var { redirectStatusSet, referrerPolicyTokens, badPortsSet } = require_constants3();
|
|
101773
101773
|
var { getGlobalOrigin } = require_global();
|
|
@@ -102361,7 +102361,7 @@ var require_util2 = __commonJS({
|
|
|
102361
102361
|
contentRange += isomorphicEncode(`${fullLength}`);
|
|
102362
102362
|
return contentRange;
|
|
102363
102363
|
}
|
|
102364
|
-
var InflateStream = class extends
|
|
102364
|
+
var InflateStream = class extends Transform2 {
|
|
102365
102365
|
#zlibOptions;
|
|
102366
102366
|
/** @param {zlib.ZlibOptions} [zlibOptions] */
|
|
102367
102367
|
constructor(zlibOptions2) {
|
|
@@ -116206,13 +116206,13 @@ var require_mock_pool = __commonJS({
|
|
|
116206
116206
|
var require_pending_interceptors_formatter = __commonJS({
|
|
116207
116207
|
"node_modules/undici/lib/mock/pending-interceptors-formatter.js"(exports, module) {
|
|
116208
116208
|
"use strict";
|
|
116209
|
-
var { Transform } = __require("stream");
|
|
116209
|
+
var { Transform: Transform2 } = __require("stream");
|
|
116210
116210
|
var { Console } = __require("console");
|
|
116211
116211
|
var PERSISTENT = process.versions.icu ? "\u2705" : "Y ";
|
|
116212
116212
|
var NOT_PERSISTENT = process.versions.icu ? "\u274C" : "N ";
|
|
116213
116213
|
module.exports = class PendingInterceptorsFormatter {
|
|
116214
116214
|
constructor({ disableColors } = {}) {
|
|
116215
|
-
this.transform = new
|
|
116215
|
+
this.transform = new Transform2({
|
|
116216
116216
|
transform(chunk, _enc, cb) {
|
|
116217
116217
|
cb(null, chunk);
|
|
116218
116218
|
}
|
|
@@ -128514,7 +128514,7 @@ var require_eventsource_stream = __commonJS({
|
|
|
128514
128514
|
"node_modules/undici/lib/web/eventsource/eventsource-stream.js"(exports, module) {
|
|
128515
128515
|
"use strict";
|
|
128516
128516
|
var buffer2 = __require("buffer");
|
|
128517
|
-
var { Transform } = __require("stream");
|
|
128517
|
+
var { Transform: Transform2 } = __require("stream");
|
|
128518
128518
|
var { isASCIINumber, isValidLastEventId } = require_util6();
|
|
128519
128519
|
var BOM = [239, 187, 191];
|
|
128520
128520
|
var LF = 10;
|
|
@@ -128564,7 +128564,7 @@ var require_eventsource_stream = __commonJS({
|
|
|
128564
128564
|
error62.aborted = false;
|
|
128565
128565
|
return error62;
|
|
128566
128566
|
}
|
|
128567
|
-
var EventSourceStream = class extends
|
|
128567
|
+
var EventSourceStream = class extends Transform2 {
|
|
128568
128568
|
/**
|
|
128569
128569
|
* @type {eventSourceSettings}
|
|
128570
128570
|
*/
|
|
@@ -275774,7 +275774,7 @@ function fingerprint(view) {
|
|
|
275774
275774
|
import { AsyncLocalStorage as AsyncLocalStorage2 } from "async_hooks";
|
|
275775
275775
|
import { createHmac } from "crypto";
|
|
275776
275776
|
import { createServer } from "http";
|
|
275777
|
-
import { finished, Readable as Readable2 } from "stream";
|
|
275777
|
+
import { finished, Readable as Readable2, Transform } from "stream";
|
|
275778
275778
|
import { pipeline } from "stream/promises";
|
|
275779
275779
|
import { homedir as homedir13 } from "os";
|
|
275780
275780
|
import { join as join17 } from "path";
|
|
@@ -286533,7 +286533,7 @@ var ResponseCache = class {
|
|
|
286533
286533
|
*/
|
|
286534
286534
|
shouldCache(body, headers) {
|
|
286535
286535
|
if (!this.config.enabled) return false;
|
|
286536
|
-
if (headers?.["cache-control"]
|
|
286536
|
+
if (/\bno-(?:cache|store)\b/i.test(headers?.["cache-control"] ?? "")) {
|
|
286537
286537
|
return false;
|
|
286538
286538
|
}
|
|
286539
286539
|
try {
|
|
@@ -291091,9 +291091,32 @@ function withZeroCapGuard(inner, isFree) {
|
|
|
291091
291091
|
return inner(input2, init2);
|
|
291092
291092
|
};
|
|
291093
291093
|
}
|
|
291094
|
+
function isMessagesPath(url3) {
|
|
291095
|
+
return url3 === "/v1/messages" || (url3?.startsWith("/v1/messages?") ?? false);
|
|
291096
|
+
}
|
|
291097
|
+
async function relayWithIdleTimeout(source, destination, idleMs, signal) {
|
|
291098
|
+
let timer2;
|
|
291099
|
+
const arm = () => {
|
|
291100
|
+
clearTimeout(timer2);
|
|
291101
|
+
timer2 = setTimeout(() => watchdog.destroy(new Error("Body read timeout")), idleMs);
|
|
291102
|
+
};
|
|
291103
|
+
const watchdog = new Transform({
|
|
291104
|
+
transform(chunk, _encoding, done) {
|
|
291105
|
+
arm();
|
|
291106
|
+
done(null, chunk);
|
|
291107
|
+
}
|
|
291108
|
+
});
|
|
291109
|
+
arm();
|
|
291110
|
+
try {
|
|
291111
|
+
await pipeline(source, watchdog, destination, { signal });
|
|
291112
|
+
} finally {
|
|
291113
|
+
clearTimeout(timer2);
|
|
291114
|
+
}
|
|
291115
|
+
}
|
|
291094
291116
|
async function proxyPaidApiRequest(req, res, apiBase, payFetch, getActualPaymentUsd, accountPassthrough = false, authMode = "wallet", capRequest) {
|
|
291095
291117
|
const startTime = Date.now();
|
|
291096
291118
|
const upstreamUrl = `${apiBase}${req.url}`;
|
|
291119
|
+
const isMessages = isMessagesPath(req.url);
|
|
291097
291120
|
const isBlockrunExa = req.url?.startsWith("/v1/exa/") ?? false;
|
|
291098
291121
|
const isModalSandbox = req.url?.startsWith("/v1/modal/") ?? false;
|
|
291099
291122
|
const isPhone = (req.url?.startsWith("/v1/phone/") ?? false) || (req.url?.startsWith("/v1/voice/") ?? false);
|
|
@@ -291135,8 +291158,8 @@ async function proxyPaidApiRequest(req, res, apiBase, payFetch, getActualPayment
|
|
|
291135
291158
|
if (accountPassthrough || authMode === "api-key") {
|
|
291136
291159
|
responseHeaders["cache-control"] = "no-store";
|
|
291137
291160
|
}
|
|
291138
|
-
res.writeHead(upstream.status, responseHeaders);
|
|
291139
291161
|
if (accountPassthrough) {
|
|
291162
|
+
res.writeHead(upstream.status, responseHeaders);
|
|
291140
291163
|
if (upstream.body) {
|
|
291141
291164
|
await pipeline(
|
|
291142
291165
|
Readable2.fromWeb(upstream.body),
|
|
@@ -291146,12 +291169,23 @@ async function proxyPaidApiRequest(req, res, apiBase, payFetch, getActualPayment
|
|
|
291146
291169
|
} else {
|
|
291147
291170
|
res.end();
|
|
291148
291171
|
}
|
|
291149
|
-
} else {
|
|
291172
|
+
} else if (isMessages) {
|
|
291173
|
+
res.writeHead(upstream.status, responseHeaders);
|
|
291150
291174
|
if (upstream.body) {
|
|
291151
|
-
|
|
291152
|
-
|
|
291153
|
-
|
|
291154
|
-
|
|
291175
|
+
await relayWithIdleTimeout(
|
|
291176
|
+
Readable2.fromWeb(upstream.body),
|
|
291177
|
+
res,
|
|
291178
|
+
MODEL_BODY_READ_TIMEOUT_MS,
|
|
291179
|
+
clientAbort.signal
|
|
291180
|
+
);
|
|
291181
|
+
} else {
|
|
291182
|
+
res.end();
|
|
291183
|
+
}
|
|
291184
|
+
} else {
|
|
291185
|
+
const chunks = upstream.body ? await readBodyWithTimeout(upstream.body, ERROR_BODY_READ_TIMEOUT_MS) : [];
|
|
291186
|
+
res.writeHead(upstream.status, responseHeaders);
|
|
291187
|
+
for (const chunk of chunks) {
|
|
291188
|
+
safeWrite(res, Buffer.from(chunk));
|
|
291155
291189
|
}
|
|
291156
291190
|
res.end();
|
|
291157
291191
|
}
|
|
@@ -292472,8 +292506,8 @@ async function startProxy(options) {
|
|
|
292472
292506
|
}
|
|
292473
292507
|
return;
|
|
292474
292508
|
}
|
|
292475
|
-
const
|
|
292476
|
-
const isVerbatimGatewayPath =
|
|
292509
|
+
const isMessages = isMessagesPath(req.url);
|
|
292510
|
+
const isVerbatimGatewayPath = isMessages || req.url === "/v1/audio/speech" || (req.url?.startsWith("/v1/audio/speech?") ?? false);
|
|
292477
292511
|
if (isVerbatimGatewayPath || req.url?.match(PAID_API_PATH)) {
|
|
292478
292512
|
try {
|
|
292479
292513
|
await proxyPaidApiRequest(
|
|
@@ -292485,7 +292519,7 @@ async function startProxy(options) {
|
|
|
292485
292519
|
false,
|
|
292486
292520
|
authMode,
|
|
292487
292521
|
// $0 is enforced below this, where calls are signed or fetched.
|
|
292488
|
-
|
|
292522
|
+
isMessages && options.maxCostPerRunUsd !== void 0 && options.maxCostPerRunUsd !== 0 ? (body) => capMessagesRequest(body, req.headers, {
|
|
292489
292523
|
usd: options.maxCostPerRunUsd,
|
|
292490
292524
|
mode: options.maxCostPerRunMode ?? "graceful",
|
|
292491
292525
|
authMode,
|
|
@@ -295090,7 +295124,7 @@ data: [DONE]
|
|
|
295090
295124
|
body: responseBody,
|
|
295091
295125
|
completedAt: Date.now()
|
|
295092
295126
|
});
|
|
295093
|
-
if (upstream.status === 200 && responseCache2.shouldCache(body)) {
|
|
295127
|
+
if (upstream.status === 200 && responseCache2.shouldCache(body, reqHeaders)) {
|
|
295094
295128
|
responseCache2.set(cacheKey3, {
|
|
295095
295129
|
body: responseBody,
|
|
295096
295130
|
status: upstream.status,
|
package/docs/features.md
CHANGED
|
@@ -46,8 +46,10 @@ The cache key is a SHA-256 hash of the request body (model + messages + params),
|
|
|
46
46
|
|
|
47
47
|
**Bypass cache:**
|
|
48
48
|
|
|
49
|
+
Either way, the request is neither answered from the cache nor stored in it. The separate 30-second dedup cache still applies, so an identical retry inside that window can replay the response.
|
|
50
|
+
|
|
49
51
|
```typescript
|
|
50
|
-
// Via header
|
|
52
|
+
// Via header ("no-store" works too)
|
|
51
53
|
fetch("/v1/chat/completions", {
|
|
52
54
|
headers: { "Cache-Control": "no-cache" }
|
|
53
55
|
})
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@blockrun/clawrouter",
|
|
3
|
-
"version": "0.12.
|
|
3
|
+
"version": "0.12.310",
|
|
4
4
|
"description": "Smart LLM router — save 84% on inference costs. 91 models (8 free). Pay by credit card (API key) or USDC (x402 wallet).",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|