feat(compression/phase6): integrate caching-aware strategy into strategySelector

This commit is contained in:
oyi77
2026-04-29 13:05:33 +07:00
parent 90873cd3e2
commit e12814fb98
4 changed files with 35 additions and 3 deletions

View File

@@ -1272,7 +1272,12 @@ export async function handleChatCore({
try {
const compressionConfig = getCompressionSettings();
estimatedTokens = estimateCompressionTokens(body);
const mode = selectCompressionStrategy(compressionConfig, comboName ?? null, estimatedTokens);
const mode = selectCompressionStrategy(
compressionConfig,
comboName ?? null,
estimatedTokens,
body as Record<string, unknown>
);
if (mode !== "off") {
const compressionResult = applyCompression(body as Record<string, unknown>, mode, {
model: effectiveModel,

View File

@@ -4,6 +4,7 @@ import { cavemanCompress } from "./caveman.ts";
import { compressAggressive } from "./aggressive.ts";
import { ultraCompress } from "./ultra.ts";
import { createCompressionStats } from "./stats.ts";
import { detectCachingContext, getCacheAwareStrategy } from "./cachingAware.ts";
export function checkComboOverride(
config: CompressionConfig,
@@ -35,9 +36,19 @@ export function getEffectiveMode(
export function selectCompressionStrategy(
config: CompressionConfig,
comboId: string | null,
estimatedTokens: number
estimatedTokens: number,
body?: Record<string, unknown>
): CompressionMode {
return getEffectiveMode(config, comboId, estimatedTokens);
const selectedMode = getEffectiveMode(config, comboId, estimatedTokens);
// Apply caching-aware adjustments if body is provided
if (body) {
const ctx = detectCachingContext(body);
const cacheAware = getCacheAwareStrategy(selectedMode, ctx);
return cacheAware.strategy as CompressionMode;
}
return selectedMode;
}
export async function applyCompression(

View File

@@ -0,0 +1,14 @@
CREATE TABLE IF NOT EXISTS compression_cache_stats (
id INTEGER PRIMARY KEY AUTOINCREMENT,
provider TEXT NOT NULL,
model TEXT NOT NULL DEFAULT '',
compression_mode TEXT NOT NULL,
cache_control_present INTEGER NOT NULL DEFAULT 0,
estimated_cache_hit INTEGER NOT NULL DEFAULT 0,
tokens_saved_compression INTEGER NOT NULL DEFAULT 0,
tokens_saved_caching INTEGER NOT NULL DEFAULT 0,
net_savings INTEGER NOT NULL DEFAULT 0,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX IF NOT EXISTS idx_compression_cache_stats_provider ON compression_cache_stats(provider);
CREATE INDEX IF NOT EXISTS idx_compression_cache_stats_created ON compression_cache_stats(created_at);

View File

@@ -79,6 +79,8 @@ export {
deleteCombo,
} from "./db/combos";
export * from "./db/compressionCacheStats";
export {
// API Keys
getApiKeys,