Skip to content

Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors

fix(compression): address PR review — multi-part msg safety, preservation $& fix, rule name sync, no-op rule removal

oyi77819c0762b9f4
diff --git a/open-sse/services/compression/caveman.ts b/open-sse/services/compression/caveman.ts
index 8d6f54f00a..d2cdf8cf16 100644
--- a/open-sse/services/compression/caveman.ts
+++ b/open-sse/services/compression/caveman.ts
@@ -86,16 +86,21 @@ export function cavemanCompress(
const allAppliedRules: string[] = [];
const compressedMessages = body.messages.map((msg): ChatMessage => {
- const contentStr =
- typeof msg.content === "string"
+ // Only compress simple string content — multi-part messages (arrays)
+ // would duplicate compressed text across all text parts if naively handled
+ if (typeof msg.content !== "string") {
+ const contentStr = Array.isArray(msg.content)
? msg.content
- : Array.isArray(msg.content)
- ? msg.content
- .map((part) => (part.type === "text" && part.text ? part.text : ""))
- .filter(Boolean)
- .join("\n")
- : "";
+ .map((part) => (part.type === "text" && part.text ? part.text : ""))
+ .filter(Boolean)
+ .join("\n")
+ : "";
+ totalOriginalTokens += estimateCompressionTokens(contentStr);
+ totalCompressedTokens += estimateCompressionTokens(contentStr);
+ return msg;
+ }
+ const contentStr = msg.content;
totalOriginalTokens += estimateCompressionTokens(contentStr);
if (!contentStr || contentStr.length < config.minMessageLength) {
@@ -122,16 +127,7 @@ export function cavemanCompress(
totalCompressedTokens += estimateCompressionTokens(cleaned);
- const newContent =
- typeof msg.content === "string"
- ? cleaned
- : Array.isArray(msg.content)
- ? msg.content.map((part) =>
- part.type === "text" && part.text ? { ...part, text: cleaned } : part
- )
- : msg.content;
-
- return { ...msg, content: newContent };
+ return { ...msg, content: cleaned };
});
const durationMs = performance.now() - startMs;
diff --git a/open-sse/services/compression/cavemanRules.ts b/open-sse/services/compression/cavemanRules.ts
index 5ea2438aac..51e9958d04 100644
--- a/open-sse/services/compression/cavemanRules.ts
+++ b/open-sse/services/compression/cavemanRules.ts
@@ -245,12 +245,6 @@ const CAVEMAN_RULES: CavemanRule[] = [
replacement: "Summary: ",
context: "assistant",
},
- {
- name: "turn_marker",
- pattern: /^$/g,
- replacement: "",
- context: "assistant",
- },
];
export function getRulesForContext(context: string): CavemanRule[] {
diff --git a/open-sse/services/compression/preservation.ts b/open-sse/services/compression/preservation.ts
index 1071ba23f3..f1491f5d58 100644
--- a/open-sse/services/compression/preservation.ts
+++ b/open-sse/services/compression/preservation.ts
@@ -43,7 +43,7 @@ export function extractPreservedBlocks(text: string): { text: string; blocks: Pr
export function restorePreservedBlocks(text: string, blocks: PreservedBlock[]): string {
let result = text;
for (const block of blocks) {
- result = result.replace(block.placeholder, block.content);
+ result = result.replace(block.placeholder, () => block.content);
}
return result;
}
diff --git a/src/app/(dashboard)/dashboard/settings/components/CompressionSettingsTab.tsx b/src/app/(dashboard)/dashboard/settings/components/CompressionSettingsTab.tsx
index 91bb0c71d0..afb0ca04a6 100644
--- a/src/app/(dashboard)/dashboard/settings/components/CompressionSettingsTab.tsx
+++ b/src/app/(dashboard)/dashboard/settings/components/CompressionSettingsTab.tsx
@@ -52,26 +52,35 @@ const ROLE_OPTIONS: { value: "user" | "assistant" | "system"; labelKey: string }
];
const ALL_CAVEMAN_RULES = [
- "hedging_disclaimer",
- "hedging_apology",
- "hedging_uncertainty",
- "redundant_please",
- "redundant_note",
- "redundant_remember",
- "redundant_important_notice",
- "structural_numbered_list_intro",
- "structural_bullet_intro",
- "structural_section_divider",
- "structural_toc",
- "whitespace_excessive_newlines",
- "whitespace_trailing_spaces",
- "markup_bold_emphasis",
- "markup_italic_emphasis",
- "markup_heading_decorations",
- "content_repetition",
+ "polite_framing",
+ "hedging",
+ "verbose_instructions",
+ "filler_adverbs",
+ "filler_phrases",
+ "redundant_openers",
+ "verbose_requests",
+ "self_reference",
+ "excessive_gratitude",
+ "qualifier_removal",
+ "compound_collapse",
+ "explanatory_prefix",
"question_to_directive",
- "meta_instructional",
- "filler_transition",
+ "context_setup",
+ "intent_clarification",
+ "background_removal",
+ "meta_commentary",
+ "purpose_statement",
+ "list_conjunction",
+ "purpose_phrases",
+ "redundant_quantifiers",
+ "verbose_connectors",
+ "transition_removal",
+ "emphasis_removal",
+ "passive_voice",
+ "repeated_context",
+ "repeated_question",
+ "reestablished_context",
+ "summary_replacement",
];
export default function CompressionSettingsTab() {
diff --git a/tests/unit/api/compression/compression-api.test.ts b/tests/unit/api/compression/compression-api.test.ts
index 5f7cfc595f..e4636be8fa 100644
--- a/tests/unit/api/compression/compression-api.test.ts
+++ b/tests/unit/api/compression/compression-api.test.ts
@@ -61,11 +61,23 @@ describe("Compression Settings API Schema Validation", () => {
assert.equal(typeof defaultConfig.cavemanConfig, "object");
});
- it("should validate all 30 caveman compression rules are defined", async () => {
+ it("should validate all caveman compression rules are defined", async () => {
const { CAVEMAN_RULES } =
await import("../../../../open-sse/services/compression/cavemanRules.ts");
assert.ok(Array.isArray(CAVEMAN_RULES));
- assert.ok(CAVEMAN_RULES.length >= 30, `Expected >= 30 rules, got ${CAVEMAN_RULES.length}`);
+ assert.ok(CAVEMAN_RULES.length >= 29, `Expected >= 29 rules, got ${CAVEMAN_RULES.length}`);
+ for (const rule of CAVEMAN_RULES) {
+ assert.ok(rule.name && typeof rule.name === "string", `Rule must have a name`);
+ assert.ok(rule.pattern instanceof RegExp, `Rule ${rule.name} must have a RegExp pattern`);
+ assert.ok(
+ typeof rule.replacement === "string" || typeof rule.replacement === "function",
+ `Rule ${rule.name} must have string or function replacement`
+ );
+ assert.ok(
+ rule.pattern.source !== "^$" || rule.replacement !== "",
+ `Rule ${rule.name} must not be a no-op (empty pattern + empty replacement)`
+ );
+ }
});
it("should validate compression modes cover all CavemanConfig roles", () => {
diff --git a/tests/unit/compression/caveman-rules.test.ts b/tests/unit/compression/caveman-rules.test.ts
index a1134c0f53..92d59fe55f 100644
--- a/tests/unit/compression/caveman-rules.test.ts
+++ b/tests/unit/compression/caveman-rules.test.ts
@@ -7,8 +7,8 @@ import {
} from "../../../open-sse/services/compression/cavemanRules.ts";
describe("cavemanRules", () => {
- it("should have 30+ rules", () => {
- assert.ok(CAVEMAN_RULES.length >= 30, `Expected 30+ rules, got ${CAVEMAN_RULES.length}`);
+ it("should have 29+ rules", () => {
+ assert.ok(CAVEMAN_RULES.length >= 29, `Expected 29+ rules, got ${CAVEMAN_RULES.length}`);
});
it("every rule should have a name, pattern, replacement, and context", () => {