Files
9router/open-sse/rtk/index.js
2026-04-22 15:36:51 +07:00

103 lines
3.6 KiB
JavaScript

// RTK port: compress tool_result content in LLM request bodies
// Injected at the top of translateRequest (before any format translation)
import { RAW_CAP, MIN_COMPRESS_SIZE } from "./constants.js";
import { autoDetectFilter } from "./autodetect.js";
import { safeApply } from "./applyFilter.js";
import { isRtkEnabled } from "./flag.js";
export { isRtkEnabled, setRtkEnabled } from "./flag.js";
// Compress tool_result content in-place. Returns stats or null if disabled/failed.
export function compressMessages(body) {
if (!isRtkEnabled()) return null;
if (!body || !Array.isArray(body.messages)) return null;
const stats = { bytesBefore: 0, bytesAfter: 0, hits: [] };
try {
for (let i = 0; i < body.messages.length; i++) {
const msg = body.messages[i];
if (!msg) continue;
// Shape 1: OpenAI tool message — { role:"tool", content: "string" }
if (msg.role === "tool" && typeof msg.content === "string") {
msg.content = compressText(msg.content, stats, "openai-tool");
continue;
}
if (!Array.isArray(msg.content)) continue;
// Shape 1b: OpenAI tool message — { role:"tool", content:[{type:"text", text:"..."}] }
if (msg.role === "tool") {
for (let k = 0; k < msg.content.length; k++) {
const part = msg.content[k];
if (part && part.type === "text" && typeof part.text === "string") {
part.text = compressText(part.text, stats, "openai-tool-array");
}
}
continue;
}
// Shape 2/3: blocks array with tool_result entries
for (let j = 0; j < msg.content.length; j++) {
const block = msg.content[j];
if (!block || block.type !== "tool_result") continue;
if (block.is_error === true) continue; // preserve error traces
if (typeof block.content === "string") {
// Shape 2: claude string form
block.content = compressText(block.content, stats, "claude-string");
} else if (Array.isArray(block.content)) {
// Shape 3: claude array form — compress each text part
for (let k = 0; k < block.content.length; k++) {
const part = block.content[k];
if (part && part.type === "text" && typeof part.text === "string") {
part.text = compressText(part.text, stats, "claude-array");
}
}
}
}
}
} catch (e) {
console.warn("[RTK] compressMessages error:", e.message);
return null;
}
return stats;
}
function compressText(text, stats, shape) {
const bytesIn = text.length;
stats.bytesBefore += bytesIn;
if (bytesIn < MIN_COMPRESS_SIZE || bytesIn > RAW_CAP) {
stats.bytesAfter += bytesIn;
return text;
}
const fn = autoDetectFilter(text);
if (!fn) {
stats.bytesAfter += bytesIn;
return text;
}
const out = safeApply(fn, text);
// Safety: never return empty, never grow the input
if (!out || out.length === 0 || out.length >= bytesIn) {
stats.bytesAfter += bytesIn;
return text;
}
stats.bytesAfter += out.length;
stats.hits.push({ shape, filter: fn.filterName || fn.name, saved: bytesIn - out.length });
return out;
}
// Convenience: format a log line from stats
export function formatRtkLog(stats) {
if (!stats || !stats.hits || stats.hits.length === 0) return null;
const saved = stats.bytesBefore - stats.bytesAfter;
const pct = stats.bytesBefore > 0 ? ((saved / stats.bytesBefore) * 100).toFixed(1) : "0";
const filters = Array.from(new Set(stats.hits.map(h => h.filter))).join(",");
return `[RTK] saved ${saved}B / ${stats.bytesBefore}B (${pct}%) via [${filters}] hits=${stats.hits.length}`;
}