File size: 4,062 Bytes
5448d8b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | import { describe, it } from "node:test";
import assert from "node:assert/strict";
import {
applyRtkCompression,
detectCodeLanguage,
stripCode,
} from "../../../open-sse/services/compression/index.ts";
describe("RTK code stripper", () => {
it("detects common code languages", () => {
assert.equal(detectCodeLanguage("interface User { id: string }"), "typescript");
assert.equal(detectCodeLanguage("def run():\n print('x')"), "python");
assert.equal(detectCodeLanguage('fn main() { println!("x"); }'), "rust");
assert.equal(detectCodeLanguage("package main\nfunc main() {}"), "go");
assert.equal(detectCodeLanguage("class Main { }"), "java");
});
it("preserves comments and string literals safely", () => {
const js = stripCode(
"// comment\nconst url = 'https://example.com/a//b';\n/* block */\nconsole.log(url);",
"javascript"
);
assert.ok(js.text.includes("// comment"));
assert.ok(js.text.includes("https://example.com/a//b"));
assert.ok(js.text.includes("/* block */"));
});
it("preserves Python docstrings and comments", () => {
const result = stripCode('"""doc"""\n# comment\nprint("ok")', "python", {
preserveDocstrings: true,
});
assert.ok(result.text.includes("doc"));
assert.ok(result.text.includes("# comment"));
});
it("applies to fenced code blocks through RTK runtime", () => {
const body = {
messages: [
{
role: "assistant",
content: `Before.
\`\`\`txt
${Array.from({ length: 20 }, () => "same code line").join("\n")}
\`\`\`
After.`,
},
],
};
const result = applyRtkCompression(body, {
config: {
enabled: true,
intensity: "standard",
applyToToolResults: false,
applyToAssistantMessages: false,
applyToCodeBlocks: true,
enabledFilters: [],
disabledFilters: [],
maxLinesPerResult: 100,
maxCharsPerResult: 12000,
deduplicateThreshold: 3,
},
});
assert.equal(result.compressed, true);
const serialized = JSON.stringify(result.body.messages);
assert.match(serialized, /Before/);
assert.match(serialized, /After/);
assert.match(serialized, /same code line/);
assert.match(serialized, /\[rtk:dropped/);
assert.ok(result.stats?.techniquesUsed.includes("rtk-code-strip"));
});
it("does not compress non-code text when only code block compression is enabled", () => {
const content = Array.from({ length: 20 }, () => "same prose line").join("\n");
const body = {
messages: [{ role: "assistant", content }],
};
const result = applyRtkCompression(body, {
config: {
enabled: true,
intensity: "standard",
applyToToolResults: false,
applyToAssistantMessages: false,
applyToCodeBlocks: true,
enabledFilters: [],
disabledFilters: [],
maxLinesPerResult: 100,
maxCharsPerResult: 12000,
deduplicateThreshold: 3,
},
});
assert.equal(result.compressed, false);
assert.deepEqual(result.body, body);
});
it("does not compress fenced code when code block compression is disabled", () => {
const body = {
messages: [
{
role: "assistant",
content: `Before.
\`\`\`txt
${Array.from({ length: 20 }, () => "same code line").join("\n")}
\`\`\`
After.`,
},
],
};
const result = applyRtkCompression(body, {
config: {
enabled: true,
intensity: "standard",
applyToToolResults: false,
applyToAssistantMessages: false,
applyToCodeBlocks: false,
enabledFilters: [],
disabledFilters: [],
maxLinesPerResult: 100,
maxCharsPerResult: 12000,
deduplicateThreshold: 3,
},
});
assert.equal(result.compressed, false);
assert.deepEqual(result.body, body);
});
});
|