mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-08-03 22:02:08 +03:00
* chore(rtk): initialize compression roadmap branch * feat(compression): add RTK engine and compression combos Introduce RTK command-aware tool-output compression alongside stacked RTK -> Caveman pipelines for mixed prompt contexts. Add engine registration, declarative RTK filter packs, language-aware Caveman rule loading, compression combo persistence and assignments, analytics grouped by engine/combo, and new MCP/API endpoints for configuration, previews, filters, and combo management. Expose the new capabilities in the dashboard with dedicated Context & Cache pages for Caveman, RTK, and compression combos, and update docs, i18n strings, migrations, and tests to cover the expanded compression surface. * feat(compression): expand RTK DSL, filter catalog, and recovery APIs Add RTK parity features across the compression pipeline, dashboard, and management APIs. This expands the built-in filter catalog, adds trust-gated custom filter loading, inline filter verification, code stripping, smarter detection, and optional redacted raw-output retention for authenticated recovery. Also extend Caveman with file-based multilingual rule packs, localized output-mode instructions, stricter preview/config schemas, engine registry metadata, analytics fields, and broad unit test coverage for RTK, rule loading, and stacked compression behavior. * fix(auth): protect oauth routes and health reset operations Require authenticated dashboard access for OAuth endpoints that can create or import provider connections when login enforcement is enabled. Move `/api/monitoring/health` to the readonly public route list so safe methods remain public while DELETE now returns 401 for anonymous requests. Also update Next.js native `.node` handling to avoid webpack parse failures from external packages such as ngrok and keytar, and add coverage for the new auth behavior. * build(compression): ship RTK rule and filter assets with app bundles Include compression JSON assets in Next output tracing, prepublish copies, and pack artifact policy checks so standalone and packaged builds can load RTK filters and caveman rule packs at runtime. Also harden compression runtime behavior by resolving alternate asset directories, scoping rule cache entries by source path, carrying RTK raw output pointers through stacked runs, degrading oversized preview diffs, and applying combo language/output mode defaults during chat routing. Add coverage for packaging rules, provider-scoped model parsing, smart truncate edge cases, raw output retention, and combo-driven compression behavior. * docs(workflows): update local repo paths to OmniRoute Replace outdated `/home/diegosouzapw/dev/proxys/9router` references with the current `OmniRoute` directory across deploy, release, and version bump workflow guides so local command examples match the renamed repository layout * feat(compression): complete RTK parity coverage * test(build): align next config assertions --------- Co-authored-by: diegosouzapw <diego.souza.pw@gmail.com>
118 lines
3.5 KiB
TypeScript
118 lines
3.5 KiB
TypeScript
import type { CompressionStats } from "./types.ts";
|
|
import { extractPreservedBlocks } from "./preservation.ts";
|
|
import { validateCompression } from "./validation.ts";
|
|
|
|
export interface CompressionDiffSegment {
|
|
type: "same" | "removed" | "added";
|
|
text: string;
|
|
}
|
|
|
|
export interface CompressionPreviewDiff {
|
|
segments: CompressionDiffSegment[];
|
|
preservedBlocks: Array<{ kind: string; preview: string }>;
|
|
ruleRemovals: string[];
|
|
validationWarnings: string[];
|
|
validationErrors: string[];
|
|
fallbackApplied: boolean;
|
|
}
|
|
|
|
export interface CompressionPreviewDiffOptions {
|
|
maxTokenProduct?: number;
|
|
}
|
|
|
|
export const DEFAULT_MAX_PREVIEW_DIFF_TOKEN_PRODUCT = 1_000_000;
|
|
|
|
function tokenize(text: string): string[] {
|
|
return text.match(/\s+|[^\s]+/g) ?? [];
|
|
}
|
|
|
|
function getDiffSkipWarning(
|
|
original: string,
|
|
compressed: string,
|
|
options: CompressionPreviewDiffOptions = {}
|
|
): string | null {
|
|
const maxTokenProduct = options.maxTokenProduct ?? DEFAULT_MAX_PREVIEW_DIFF_TOKEN_PRODUCT;
|
|
if (maxTokenProduct <= 0) return null;
|
|
|
|
const originalTokens = tokenize(original).length;
|
|
const compressedTokens = tokenize(compressed).length;
|
|
if (originalTokens * compressedTokens <= maxTokenProduct) return null;
|
|
|
|
return `Preview diff omitted because token product ${originalTokens}x${compressedTokens} exceeds safe limit ${maxTokenProduct}.`;
|
|
}
|
|
|
|
export function buildCompressionDiff(
|
|
original: string,
|
|
compressed: string
|
|
): CompressionDiffSegment[] {
|
|
const a = tokenize(original);
|
|
const b = tokenize(compressed);
|
|
const dp = Array.from({ length: a.length + 1 }, () => Array<number>(b.length + 1).fill(0));
|
|
|
|
for (let i = a.length - 1; i >= 0; i--) {
|
|
for (let j = b.length - 1; j >= 0; j--) {
|
|
dp[i][j] = a[i] === b[j] ? dp[i + 1][j + 1] + 1 : Math.max(dp[i + 1][j], dp[i][j + 1]);
|
|
}
|
|
}
|
|
|
|
const segments: CompressionDiffSegment[] = [];
|
|
const push = (type: CompressionDiffSegment["type"], text: string) => {
|
|
if (!text) return;
|
|
const last = segments[segments.length - 1];
|
|
if (last?.type === type) {
|
|
last.text += text;
|
|
} else {
|
|
segments.push({ type, text });
|
|
}
|
|
};
|
|
|
|
let i = 0;
|
|
let j = 0;
|
|
while (i < a.length && j < b.length) {
|
|
if (a[i] === b[j]) {
|
|
push("same", a[i]);
|
|
i++;
|
|
j++;
|
|
} else if (dp[i + 1][j] >= dp[i][j + 1]) {
|
|
push("removed", a[i]);
|
|
i++;
|
|
} else {
|
|
push("added", b[j]);
|
|
j++;
|
|
}
|
|
}
|
|
while (i < a.length) push("removed", a[i++]);
|
|
while (j < b.length) push("added", b[j++]);
|
|
|
|
return segments;
|
|
}
|
|
|
|
export function buildCompressionPreviewDiff(
|
|
original: string,
|
|
compressed: string,
|
|
stats: CompressionStats | null | undefined,
|
|
options: CompressionPreviewDiffOptions = {}
|
|
): CompressionPreviewDiff {
|
|
const validation = validateCompression(original, compressed);
|
|
const preserved = extractPreservedBlocks(original).blocks.map((block) => ({
|
|
kind: block.kind,
|
|
preview: block.content.replace(/\s+/g, " ").slice(0, 120),
|
|
}));
|
|
const diffSkipWarning = getDiffSkipWarning(original, compressed, options);
|
|
|
|
return {
|
|
segments: diffSkipWarning
|
|
? [{ type: "same", text: "[diff omitted: input too large]" }]
|
|
: buildCompressionDiff(original, compressed),
|
|
preservedBlocks: preserved,
|
|
ruleRemovals: stats?.rulesApplied ?? [],
|
|
validationWarnings: [
|
|
...(stats?.validationWarnings ?? []),
|
|
...validation.warnings,
|
|
...(diffSkipWarning ? [diffSkipWarning] : []),
|
|
],
|
|
validationErrors: [...(stats?.validationErrors ?? []), ...validation.errors],
|
|
fallbackApplied: Boolean(stats?.fallbackApplied || validation.fallbackApplied),
|
|
};
|
|
}
|