mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-08-24 08:02:14 +03:00
271 lines
9.3 KiB
JavaScript
271 lines
9.3 KiB
JavaScript
import { readFileSync } from "node:fs";
|
|
import { apiFetch } from "../api.mjs";
|
|
import { mcpCallTool } from "../mcpClient.mjs";
|
|
import { emit } from "../output.mjs";
|
|
import { t } from "../i18n.mjs";
|
|
|
|
// #2688 — CLI no longer assumes MCP is enabled. Engine names are normalized
|
|
// to the current core set; legacy aliases continue to work.
|
|
const VALID_ENGINES = ["off", "caveman", "rtk", "stacked"];
|
|
const ENGINE_ALIASES = { none: "off", hybrid: "stacked" };
|
|
|
|
function normalizeEngine(name) {
|
|
return ENGINE_ALIASES[name] ?? name;
|
|
}
|
|
|
|
// Direct REST fallbacks used when the MCP tool surface is not mounted (404).
|
|
// Keeps every subcommand working on minimal builds.
|
|
async function restCompressionStatus() {
|
|
const [settingsRes, combosRes, analyticsRes] = await Promise.all([
|
|
apiFetch("/api/settings/compression"),
|
|
apiFetch("/api/context/combos"),
|
|
apiFetch("/api/context/analytics?period=7d").catch(() => null),
|
|
]);
|
|
const settings = settingsRes.ok ? await settingsRes.json() : {};
|
|
const combosBody = combosRes.ok ? await combosRes.json() : { combos: [] };
|
|
const analytics = analyticsRes && analyticsRes.ok ? await analyticsRes.json() : null;
|
|
return {
|
|
strategy: settings.defaultMode || "standard",
|
|
settings,
|
|
combos: combosBody.combos ?? combosBody,
|
|
analytics,
|
|
};
|
|
}
|
|
|
|
async function restCompressionConfigure(config) {
|
|
const body = { ...config };
|
|
if (body.strategy) {
|
|
body.defaultMode = body.strategy === "caveman" ? "standard" : normalizeEngine(body.strategy);
|
|
delete body.strategy;
|
|
}
|
|
const res = await apiFetch("/api/settings/compression", { method: "PUT", body });
|
|
if (!res.ok) {
|
|
process.stderr.write(`Error: ${res.status}\n`);
|
|
process.exit(1);
|
|
}
|
|
return res.json();
|
|
}
|
|
|
|
async function restSetEngine(name) {
|
|
const normalized = normalizeEngine(name);
|
|
const res = await apiFetch("/api/settings/compression", {
|
|
method: "PUT",
|
|
body: { defaultMode: normalized === "caveman" ? "standard" : normalized },
|
|
});
|
|
if (!res.ok) {
|
|
process.stderr.write(`Error: ${res.status}\n`);
|
|
process.exit(1);
|
|
}
|
|
return res.json();
|
|
}
|
|
|
|
async function restListCombos() {
|
|
const res = await apiFetch("/api/context/combos");
|
|
if (!res.ok) {
|
|
process.stderr.write(`Error: ${res.status}\n`);
|
|
process.exit(1);
|
|
}
|
|
const body = await res.json();
|
|
return body.combos ?? body;
|
|
}
|
|
|
|
async function restComboStats(period) {
|
|
const res = await apiFetch(`/api/context/analytics?period=${encodeURIComponent(period ?? "7d")}`);
|
|
if (!res.ok) {
|
|
process.stderr.write(`Error: ${res.status}\n`);
|
|
process.exit(1);
|
|
}
|
|
return res.json();
|
|
}
|
|
|
|
async function mcpCall(name, args, restFallback) {
|
|
try {
|
|
return await mcpCallTool(name, args);
|
|
} catch (err) {
|
|
// Keep the REST fallback behavior for builds where the MCP surface
|
|
// is unreachable / not mounted. Anything else rethrows as an error.
|
|
const status = err?.status || err?.cause?.status;
|
|
if ((status === 404 || status === 501) && typeof restFallback === "function") {
|
|
return restFallback();
|
|
}
|
|
throw err;
|
|
}
|
|
}
|
|
|
|
async function confirm(q) {
|
|
return new Promise((resolve) => {
|
|
process.stdout.write(`${q} (yes/no) `);
|
|
process.stdin.setEncoding("utf8");
|
|
process.stdin.once("data", (c) => resolve(c.toString().trim().toLowerCase().startsWith("y")));
|
|
});
|
|
}
|
|
|
|
export async function runCompressionStatus(opts, cmd) {
|
|
const data = await mcpCall("omniroute_compression_status", {}, restCompressionStatus);
|
|
emit(data, cmd.optsWithGlobals());
|
|
}
|
|
|
|
export async function runCompressionConfigure(opts, cmd) {
|
|
const config = {};
|
|
// Keep this payload byte-for-byte within compressionConfigureInput. Engine-
|
|
// specific Caveman/RTK controls belong to `compression engine set`, whose
|
|
// MCP tool has a different schema.
|
|
const strategy = opts.strategy ?? opts.engine;
|
|
if (strategy) config.strategy = normalizeEngine(strategy);
|
|
for (const key of [
|
|
"enabled",
|
|
"autoTriggerMode",
|
|
"maxTokens",
|
|
"targetRatio",
|
|
"preserveSystemPrompt",
|
|
"mcpDescriptionCompressionEnabled",
|
|
]) {
|
|
if (opts[key] !== undefined) config[key] = opts[key];
|
|
}
|
|
const data = await mcpCall("omniroute_compression_configure", config, () =>
|
|
restCompressionConfigure(config)
|
|
);
|
|
emit(data, cmd.optsWithGlobals());
|
|
}
|
|
|
|
export async function runCompressionComboStats(opts, cmd) {
|
|
const since = opts.period ?? "7d";
|
|
const data = await mcpCall("omniroute_compression_combo_stats", { since }, () =>
|
|
restComboStats(since)
|
|
);
|
|
emit(data, cmd.optsWithGlobals());
|
|
}
|
|
|
|
export async function runCompressionEngineSet(name, opts, cmd) {
|
|
const normalized = normalizeEngine(name);
|
|
if (!VALID_ENGINES.includes(normalized)) {
|
|
process.stderr.write(`Unknown engine: ${name}. Valid: ${VALID_ENGINES.join(", ")}\n`);
|
|
process.exit(2);
|
|
}
|
|
await mcpCall("omniroute_set_compression_engine", { engine: normalized }, () =>
|
|
restSetEngine(normalized)
|
|
);
|
|
process.stdout.write(`Engine: ${normalized}\n`);
|
|
}
|
|
|
|
export async function runCompressionPreview(opts, cmd) {
|
|
const body = JSON.parse(readFileSync(opts.file, "utf8"));
|
|
const res = await apiFetch("/api/compression/preview", { method: "POST", body });
|
|
if (!res.ok) {
|
|
process.stderr.write(`Error: ${res.status}\n`);
|
|
process.exit(1);
|
|
}
|
|
const data = await res.json();
|
|
emit(data, cmd.optsWithGlobals());
|
|
if (cmd.optsWithGlobals().output !== "json") {
|
|
process.stderr.write(
|
|
`\nOriginal: ${data.beforeTokens ?? "?"} tok → After: ${data.afterTokens ?? "?"} tok (${data.savingsPct ?? "?"}%)\n`
|
|
);
|
|
}
|
|
}
|
|
|
|
export function registerCompression(program) {
|
|
const cmp = program.command("compression").description(t("compression.description"));
|
|
|
|
cmp
|
|
.command("status")
|
|
.description(t("compression.status.description"))
|
|
.action(runCompressionStatus);
|
|
|
|
cmp
|
|
.command("configure")
|
|
.description(t("compression.configure.description"))
|
|
.option("--strategy <mode>", t("compression.configure.engine"))
|
|
.option("--enabled <boolean>", "Enable or disable compression", (value) => value === "true")
|
|
.option("--auto-trigger-mode <mode>", "Compression mode used by automatic triggering")
|
|
.option("--max-tokens <n>", "Maximum tokens before compression triggers", parseInt)
|
|
.option("--target-ratio <n>", "Target compression ratio (0.0-1.0)", parseFloat)
|
|
.option(
|
|
"--preserve-system-prompt <boolean>",
|
|
"Preserve the system prompt",
|
|
(value) => value === "true"
|
|
)
|
|
.option(
|
|
"--mcp-description-compression-enabled <boolean>",
|
|
"Compress MCP tool descriptions",
|
|
(value) => value === "true"
|
|
)
|
|
.action(runCompressionConfigure);
|
|
|
|
const engine = cmp.command("engine").description(t("compression.engine.description"));
|
|
engine.command("set <name>").action(runCompressionEngineSet);
|
|
engine.command("get").action(async (opts, cmd) => {
|
|
const data = await mcpCall("omniroute_compression_status", {}, restCompressionStatus);
|
|
process.stdout.write(`${data.strategy ?? "(default)"}\n`);
|
|
});
|
|
|
|
const combos = cmp.command("combos").description(t("compression.combos.description"));
|
|
combos.command("list").action(async (opts, cmd) => {
|
|
const data = await mcpCall("omniroute_list_compression_combos", {}, async () => ({
|
|
combos: await restListCombos(),
|
|
}));
|
|
emit(data.combos ?? data, cmd.optsWithGlobals());
|
|
});
|
|
combos.command("stats").option("--period <p>", null, "7d").action(runCompressionComboStats);
|
|
|
|
const rules = cmp.command("rules").description(t("compression.rules.description"));
|
|
rules.command("list").action(async (opts, cmd) => {
|
|
const res = await apiFetch("/api/compression/rules");
|
|
if (!res.ok) {
|
|
process.stderr.write(`Error: ${res.status}\n`);
|
|
process.exit(1);
|
|
}
|
|
emit(await res.json(), cmd.optsWithGlobals());
|
|
});
|
|
rules
|
|
.command("add")
|
|
.requiredOption("--pattern <p>", t("compression.rules.add.pattern"))
|
|
.requiredOption("--action <a>", t("compression.rules.add.action"))
|
|
.option("--replacement <r>")
|
|
.action(async (opts, cmd) => {
|
|
const body = { pattern: opts.pattern, action: opts.action };
|
|
if (opts.replacement) body.replacement = opts.replacement;
|
|
const res = await apiFetch("/api/compression/rules", { method: "POST", body });
|
|
if (!res.ok) {
|
|
process.stderr.write(`Error: ${res.status}\n`);
|
|
process.exit(1);
|
|
}
|
|
emit(await res.json(), cmd.optsWithGlobals());
|
|
});
|
|
rules
|
|
.command("remove <id>")
|
|
.option("--yes")
|
|
.action(async (id, opts, cmd) => {
|
|
if (!opts.yes) {
|
|
const ok = await confirm(`Remove rule ${id}?`);
|
|
if (!ok) return;
|
|
}
|
|
const res = await apiFetch(`/api/compression/rules?id=${encodeURIComponent(id)}`, {
|
|
method: "DELETE",
|
|
});
|
|
if (!res.ok) {
|
|
process.stderr.write(`Error: ${res.status}\n`);
|
|
process.exit(1);
|
|
}
|
|
process.stdout.write("Removed\n");
|
|
});
|
|
|
|
cmp
|
|
.command("language-packs")
|
|
.description(t("compression.language_packs.description"))
|
|
.action(async (opts, cmd) => {
|
|
const res = await apiFetch("/api/compression/language-packs");
|
|
if (!res.ok) {
|
|
process.stderr.write(`Error: ${res.status}\n`);
|
|
process.exit(1);
|
|
}
|
|
emit(await res.json(), cmd.optsWithGlobals());
|
|
});
|
|
|
|
cmp
|
|
.command("preview")
|
|
.description(t("compression.preview.description"))
|
|
.requiredOption("--file <path>", t("compression.preview.file"))
|
|
.action(runCompressionPreview);
|
|
}
|