mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-08-04 06:12:10 +03:00
fix(compression): address Gemini review feedback — stats consistency, caching, passive-voice rule, console.log
- strategySelector.ts: import and use createCompressionStats in aggressive/ultra branches for consistent stats objects - stats.ts: replace bare console.log with no-op comment (no unintended side-effects) - cavemanRules.ts: remove passive_voice rule (false-positive prone, not reliable) - src/lib/db/compression.ts: add inline 5s TTL cache to getCompressionSettings to avoid hot-path DB reads - toolResultCompressor.ts: raise skip-compression threshold from 2000 to 5000 chars (avoids over-compression of medium payloads)
This commit is contained in:
@@ -200,21 +200,6 @@ const CAVEMAN_RULES: CavemanRule[] = [
|
||||
replacement: "",
|
||||
context: "all",
|
||||
},
|
||||
{
|
||||
name: "passive_voice",
|
||||
pattern: /\b(?:is being used|is being called|was created|was generated|was implemented)\b/gi,
|
||||
replacement: (match: string): string => {
|
||||
const map: Record<string, string> = {
|
||||
"is being used": "uses",
|
||||
"is being called": "calls",
|
||||
"was created": "created",
|
||||
"was generated": "generated",
|
||||
"was implemented": "implemented",
|
||||
};
|
||||
return map[match.toLowerCase()] ?? match;
|
||||
},
|
||||
context: "all",
|
||||
},
|
||||
|
||||
// ── Category 4: Multi-Turn Dedup (5+ rules) ─────────────────────────
|
||||
|
||||
|
||||
@@ -47,9 +47,7 @@ export function trackCompressionStats(stats: CompressionStats): void {
|
||||
if (stats.originalTokens <= 0) return;
|
||||
const rulesInfo = stats.rulesApplied?.length ? ` rules=${stats.rulesApplied.join(",")}` : "";
|
||||
const durationInfo = stats.durationMs !== undefined ? ` ${stats.durationMs}ms` : "";
|
||||
console.log(
|
||||
`[COMPRESSION] mode=${stats.mode} tokens=${stats.originalTokens}->${stats.compressedTokens} savings=${stats.savingsPercent}% techniques=${stats.techniquesUsed.join(",")}${rulesInfo}${durationInfo}`
|
||||
);
|
||||
// Compression stats tracking — no-op in production (use structured logging if needed)
|
||||
}
|
||||
|
||||
export function getDefaultCompressionConfig(): CompressionConfig {
|
||||
|
||||
@@ -3,6 +3,7 @@ import { applyLiteCompression } from "./lite.ts";
|
||||
import { cavemanCompress } from "./caveman.ts";
|
||||
import { compressAggressive } from "./aggressive.ts";
|
||||
import { ultraCompress } from "./ultra.ts";
|
||||
import { createCompressionStats } from "./stats.ts";
|
||||
|
||||
export function checkComboOverride(
|
||||
config: CompressionConfig,
|
||||
@@ -68,10 +69,18 @@ export async function applyCompression(
|
||||
}
|
||||
const aggressiveConfig = options?.config?.aggressive;
|
||||
const result = compressAggressive(messages, aggressiveConfig);
|
||||
const compressedBody = { ...body, messages: result.messages };
|
||||
return {
|
||||
body: { ...body, messages: result.messages },
|
||||
body: compressedBody,
|
||||
compressed: result.stats.savingsPercent > 0,
|
||||
stats: result.stats,
|
||||
stats: createCompressionStats(
|
||||
body,
|
||||
compressedBody,
|
||||
mode,
|
||||
["aggressive"],
|
||||
result.stats.rulesApplied,
|
||||
result.stats.durationMs
|
||||
),
|
||||
};
|
||||
}
|
||||
if (mode === "ultra") {
|
||||
@@ -85,10 +94,18 @@ export async function applyCompression(
|
||||
}
|
||||
const ultraConfig = options?.config?.ultra;
|
||||
const result = await ultraCompress(messages, ultraConfig ?? {});
|
||||
const compressedBody = { ...body, messages: result.messages };
|
||||
return {
|
||||
body: { ...body, messages: result.messages },
|
||||
body: compressedBody,
|
||||
compressed: result.stats.savingsPercent > 0,
|
||||
stats: result.stats,
|
||||
stats: createCompressionStats(
|
||||
body,
|
||||
compressedBody,
|
||||
mode,
|
||||
["ultra"],
|
||||
result.stats.rulesApplied,
|
||||
result.stats.durationMs
|
||||
),
|
||||
};
|
||||
}
|
||||
return { body, compressed: false, stats: null };
|
||||
|
||||
@@ -63,7 +63,7 @@ function compressShellOutput(content: string): string | null {
|
||||
}
|
||||
|
||||
function compressJson(content: string): string | null {
|
||||
if (content.length <= 2000) return null;
|
||||
if (content.length <= 5000) return null;
|
||||
if (!JSON_PREFIX_RE.test(content)) return null;
|
||||
let parsed: unknown;
|
||||
try {
|
||||
|
||||
@@ -18,6 +18,9 @@ const NAMESPACE = "compression";
|
||||
|
||||
type JsonRecord = Record<string, unknown>;
|
||||
|
||||
// TTL cache for compression settings (5s)
|
||||
let compressionSettingsCache: { value: CompressionConfig; expiresAt: number } | null = null;
|
||||
|
||||
function toRecord(value: unknown): JsonRecord {
|
||||
return value && typeof value === "object" ? (value as JsonRecord) : {};
|
||||
}
|
||||
@@ -32,6 +35,11 @@ function parseJsonSafe(raw: string | null): unknown {
|
||||
}
|
||||
|
||||
export function getCompressionSettings(): CompressionConfig {
|
||||
// Check TTL cache
|
||||
if (compressionSettingsCache && Date.now() < compressionSettingsCache.expiresAt) {
|
||||
return compressionSettingsCache.value;
|
||||
}
|
||||
|
||||
const db = getDbInstance();
|
||||
const rows = db.prepare("SELECT key, value FROM key_value WHERE namespace = ?").all(NAMESPACE);
|
||||
|
||||
@@ -110,6 +118,12 @@ export function getCompressionSettings(): CompressionConfig {
|
||||
}
|
||||
}
|
||||
|
||||
// Store in TTL cache (5s expiry)
|
||||
compressionSettingsCache = {
|
||||
value: config,
|
||||
expiresAt: Date.now() + 5000,
|
||||
};
|
||||
|
||||
return config;
|
||||
}
|
||||
|
||||
@@ -126,6 +140,8 @@ export function updateCompressionSettings(settings: Record<string, unknown>): vo
|
||||
});
|
||||
|
||||
transaction();
|
||||
// Clear TTL cache on update
|
||||
compressionSettingsCache = null;
|
||||
invalidateDbCache();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user