Spaces:
Runtime error
Runtime error
File size: 8,698 Bytes
cd8bd0a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 | import { readFileSync } from "node:fs";
import { apiFetch } from "../api.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 {
engine: settings.engine ?? null,
settings,
combos: combosBody.combos ?? combosBody,
analytics,
};
}
async function restCompressionConfigure(config) {
const body = { ...config };
if (body.engine) body.engine = normalizeEngine(body.engine);
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 res = await apiFetch("/api/settings/compression", {
method: "PUT",
body: { engine: normalizeEngine(name) },
});
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) {
const res = await apiFetch("/api/mcp/tools/call", {
method: "POST",
body: { name, arguments: args },
});
if (res.ok) return res.json();
// 404 = MCP tool surface not mounted on this build; 501 = not implemented.
// Anything else is a genuine error and we surface it.
if ((res.status === 404 || res.status === 501) && typeof restFallback === "function") {
return restFallback();
}
process.stderr.write(`Error: ${res.status}\n`);
process.exit(1);
}
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 = {};
if (opts.engine) config.engine = opts.engine;
if (opts.cavemanAggressiveness !== undefined)
config.caveman = { aggressiveness: opts.cavemanAggressiveness };
if (opts.rtkBudget !== undefined) config.rtk = { tokenBudget: opts.rtkBudget };
if (opts.languagePack) config.languagePack = opts.languagePack;
const data = await mcpCall("omniroute_compression_configure", config, () =>
restCompressionConfigure(config)
);
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("--engine <e>", t("compression.configure.engine"))
.option("--caveman-aggressiveness <n>", t("compression.configure.caveman_agg"), parseFloat)
.option("--rtk-budget <n>", t("compression.configure.rtk_budget"), parseInt)
.option("--language-pack <p>", t("compression.configure.language_pack"))
.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.engine ?? "(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(async (opts, cmd) => {
const data = await mcpCall(
"omniroute_compression_combo_stats",
{ period: opts.period ?? "7d" },
() => restComboStats(opts.period)
);
emit(data, cmd.optsWithGlobals());
});
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);
}
|