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
TypeScript a2aai-agentsai-gatewayanthropicclaudeclaude-codeclinecodexcopilotcursordeepseekfree-aigeminikimillm-gatewaymcpopenaiopenai-proxyqwentoken-saver
feat(compression): implement lite compression techniques
oyi77cb89abc53acf
diff --git a/open-sse/services/compression/lite.ts b/open-sse/services/compression/lite.tsnew file mode 100644index 0000000000..bc64e13ef6--- /dev/null+++ b/open-sse/services/compression/lite.ts@@ -0,0 +1,170 @@+import type { CompressionResult, CompressionMode } from "./types";+import { createCompressionStats } from "./stats";++interface Message {+ role: string;+ content: string | Array<Record<string, unknown>>;+ [key: string]: unknown;+}++interface ChatBody {+ messages?: Message[];+ [key: string]: unknown;+}++export function collapseWhitespace(body: ChatBody): {+ body: ChatBody;+ applied: boolean;+} {+ if (!body.messages) return { body, applied: false };+ let applied = false;+ const messages = body.messages.map((msg) => {+ if (typeof msg.content !== "string") return msg;+ const normalized = msg.content.replace(/\n{3,}/g, "\n\n").replace(/[ \t]+$/gm, "");+ if (normalized !== msg.content) applied = true;+ return { ...msg, content: normalized };+ });+ return { body: { ...body, messages }, applied };+}++export function dedupSystemPrompt(body: ChatBody): {+ body: ChatBody;+ applied: boolean;+} {+ if (!body.messages) return { body, applied: false };+ const seen = new Set<string>();+ let applied = false;+ const messages = body.messages.filter((msg) => {+ if (msg.role !== "system" || typeof msg.content !== "string") return true;+ const key = msg.content.trim().slice(0, 200);+ if (seen.has(key)) {+ applied = true;+ return false;+ }+ seen.add(key);+ return true;+ });+ return { body: { ...body, messages }, applied };+}++export function compressToolResults(body: ChatBody): {+ body: ChatBody;+ applied: boolean;+} {+ if (!body.messages) return { body, applied: false };+ const MAX_TOOL_LENGTH = 2000;+ let applied = false;+ const messages = body.messages.map((msg) => {+ if (msg.role !== "tool" || typeof msg.content !== "string") return msg;+ if (msg.content.length <= MAX_TOOL_LENGTH) return msg;+ applied = true;+ return {+ ...msg,+ content: msg.content.slice(0, MAX_TOOL_LENGTH) + "\n...[truncated]",+ };+ });+ return { body: { ...body, messages }, applied };+}++export function removeRedundantContent(body: ChatBody): {+ body: ChatBody;+ applied: boolean;+} {+ if (!body.messages) return { body, applied: false };+ let applied = false;+ const messages: Message[] = [];+ for (let i = 0; i < body.messages.length; i++) {+ const msg = body.messages[i];+ const contentStr = typeof msg.content === "string" ? msg.content : JSON.stringify(msg.content);+ if (+ i > 0 &&+ body.messages[i - 1].role === msg.role &&+ typeof body.messages[i - 1].content === "string" &&+ body.messages[i - 1].content === contentStr+ ) {+ applied = true;+ continue;+ }+ messages.push(msg);+ }+ return { body: { ...body, messages }, applied };+}++export function replaceImageUrls(+ body: ChatBody,+ model?: string+): { body: ChatBody; applied: boolean } {+ if (!body.messages) return { body, applied: false };+ const isVisionModel = model && /\b(vision|gpt-4o|claude-3|gemini-1\.5|gemini-2)\b/i.test(model);+ if (isVisionModel) return { body, applied: false };+ let applied = false;+ const messages = body.messages.map((msg) => {+ if (!Array.isArray(msg.content)) return msg;+ const newContent = msg.content.map((part) => {+ if (+ typeof part === "object" &&+ part !== null &&+ part.type === "image_url" &&+ typeof (part as Record<string, unknown>).image_url === "object" &&+ ((part as Record<string, unknown>).image_url as Record<string, unknown>)?.url+ ) {+ const url = String(+ ((part as Record<string, unknown>).image_url as Record<string, unknown>).url+ );+ if (url.startsWith("data:image/")) {+ applied = true;+ const format = url.slice(url.indexOf("/") + 1, url.indexOf(";")) || "unknown";+ return { type: "text", text: `[image: ${format}]` };+ }+ }+ return part;+ });+ return { ...msg, content: newContent };+ });+ return { body: { ...body, messages }, applied };+}++export function applyLiteCompression(+ body: Record<string, unknown>,+ options?: { model?: string }+): CompressionResult {+ const originalBody = body;+ let current = body as ChatBody;+ const techniquesApplied: string[] = [];++ const r1 = collapseWhitespace(current);+ current = r1.body;+ if (r1.applied) techniquesApplied.push("whitespace");++ const r2 = dedupSystemPrompt(current);+ current = r2.body;+ if (r2.applied) techniquesApplied.push("system-dedup");++ const r3 = compressToolResults(current);+ current = r3.body;+ if (r3.applied) techniquesApplied.push("tool-compress");++ const r4 = removeRedundantContent(current);+ current = r4.body;+ if (r4.applied) techniquesApplied.push("redundant-remove");++ const r5 = replaceImageUrls(current, options?.model);+ current = r5.body;+ if (r5.applied) techniquesApplied.push("image-placeholder");++ const compressed = techniquesApplied.length > 0;+ const stats = compressed+ ? createCompressionStats(+ originalBody,+ current as Record<string, unknown>,+ "lite",+ techniquesApplied+ )+ : null;++ return {+ body: current as Record<string, unknown>,+ compressed,+ stats,+ };+}