pi-freeflow 1.1.9 → 1.2.0
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/extensions/index.ts +36 -39
- package/package.json +1 -1
package/extensions/index.ts
CHANGED
|
@@ -1453,8 +1453,8 @@ function startProxy(
|
|
|
1453
1453
|
} catch {}
|
|
1454
1454
|
|
|
1455
1455
|
const upstream: Upstream = isKilo ? "kilo" : "opencode";
|
|
1456
|
+
const isStream = parsedBody?.stream === true;
|
|
1456
1457
|
if (!checkRateLimit(clientIP, upstream)) {
|
|
1457
|
-
log("warn", `rate limit hit for ${upstream} from ${clientIP}`, { ip: clientIP, upstream }, reqId);
|
|
1458
1458
|
res.writeHead(429, { "content-type": "application/json" });
|
|
1459
1459
|
res.end(JSON.stringify({ error: "rate limit exceeded" }));
|
|
1460
1460
|
return;
|
|
@@ -1508,52 +1508,49 @@ function startProxy(
|
|
|
1508
1508
|
activeHost,
|
|
1509
1509
|
);
|
|
1510
1510
|
try {
|
|
1511
|
-
if (
|
|
1511
|
+
if (parsedBody) {
|
|
1512
1512
|
const relayBodyObj = structuredClone(parsedBody);
|
|
1513
1513
|
normalizeRequestBody(relayBodyObj, true, isKilo, reqId);
|
|
1514
|
-
relayBody = Buffer.from(JSON.stringify(relayBodyObj));
|
|
1514
|
+
const relayBody = Buffer.from(JSON.stringify(relayBodyObj));
|
|
1515
|
+
const response = await relayFetch(fullUrl, {
|
|
1516
|
+
method: req.method || "POST",
|
|
1517
|
+
headers: relayHeaders,
|
|
1518
|
+
body: relayBody,
|
|
1519
|
+
signal: AbortSignal.timeout(300_000),
|
|
1520
|
+
}, reqId);
|
|
1521
|
+
if (isStream && response.ok && response.body) {
|
|
1522
|
+
const ct =
|
|
1523
|
+
response.headers.get("content-type") || "text/event-stream";
|
|
1524
|
+
res.writeHead(response.status, {
|
|
1525
|
+
"content-type": ct,
|
|
1526
|
+
"cache-control": "no-cache, no-transform",
|
|
1527
|
+
"connection": "keep-alive",
|
|
1528
|
+
"x-accel-buffering": "no",
|
|
1529
|
+
});
|
|
1530
|
+
pipeUpstreamStream(
|
|
1531
|
+
Readable.fromWeb(
|
|
1532
|
+
response.body as unknown as WebReadableStream,
|
|
1533
|
+
),
|
|
1534
|
+
res,
|
|
1535
|
+
req,
|
|
1536
|
+
reqId,
|
|
1537
|
+
);
|
|
1538
|
+
} else {
|
|
1539
|
+
const data = await response.text();
|
|
1540
|
+
const ct =
|
|
1541
|
+
response.headers.get("content-type") || "application/json";
|
|
1542
|
+
res.writeHead(response.status, { "content-type": ct });
|
|
1543
|
+
res.end(data);
|
|
1544
|
+
}
|
|
1545
|
+
return; // relay handled the response
|
|
1515
1546
|
}
|
|
1516
|
-
const response = await relayFetch(fullUrl, {
|
|
1517
|
-
method: req.method || "POST",
|
|
1518
|
-
headers: relayHeaders,
|
|
1519
|
-
body: relayBody,
|
|
1520
|
-
signal: AbortSignal.timeout(300_000),
|
|
1521
|
-
}, reqId);
|
|
1522
|
-
const ct =
|
|
1523
|
-
response.headers.get("content-type") || "application/json";
|
|
1524
|
-
if (response.ok && response.body) {
|
|
1525
|
-
const ct =
|
|
1526
|
-
response.headers.get("content-type") || "text/event-stream";
|
|
1527
|
-
res.writeHead(response.status, {
|
|
1528
|
-
"content-type": ct,
|
|
1529
|
-
"cache-control": "no-cache, no-transform",
|
|
1530
|
-
"connection": "keep-alive",
|
|
1531
|
-
"x-accel-buffering": "no",
|
|
1532
|
-
});
|
|
1533
|
-
pipeUpstreamStream(
|
|
1534
|
-
Readable.fromWeb(
|
|
1535
|
-
response.body as unknown as WebReadableStream,
|
|
1536
|
-
),
|
|
1537
|
-
res,
|
|
1538
|
-
req,
|
|
1539
|
-
reqId,
|
|
1540
|
-
);
|
|
1541
|
-
} else {
|
|
1542
|
-
const data = await response.text();
|
|
1543
|
-
const ct =
|
|
1544
|
-
response.headers.get("content-type") || "application/json";
|
|
1545
|
-
res.writeHead(response.status, { "content-type": ct });
|
|
1546
|
-
res.end(data);
|
|
1547
|
-
}
|
|
1548
|
-
return; // relay handled the response
|
|
1549
1547
|
} catch (e) {
|
|
1550
1548
|
log("warn", "opencode relay failed, falling back to direct", {
|
|
1551
1549
|
error: String(e),
|
|
1552
1550
|
}, reqId);
|
|
1553
|
-
|
|
1554
|
-
|
|
1551
|
+
if (res.headersSent) return; // can't recover mid-stream
|
|
1552
|
+
}
|
|
1555
1553
|
}
|
|
1556
|
-
// direct path (existing, untouched)
|
|
1557
1554
|
// direct path — with debug trace and thinking-aware normalize
|
|
1558
1555
|
let directBody = Buffer.concat(bodyChunks);
|
|
1559
1556
|
if (parsedBody) {
|