Files
ruflo/plugins/ruflo-cost-tracker/scripts/compact.mjs
T
ReuvenandRuFlo 8f580f207b feat(cost-tracker): P4 — encapsulate compact-context Node block as compact.mjs (v0.8.0)
Drops the inline `node --input-type=module -e '...'` block from
cost-compact-context. Replaced with a proper script that:
- resolves @claude-flow/integration from process.cwd() (so users can
  `cd v3 && node ../plugins/.../compact.mjs "<query>"`)
- prints a markdown table by default, JSON via COMPACT_QUIET=1
- catches bridge-unavailable cases cleanly

True MCP-tool wrapping (modifying @claude-flow/cli) is deferred —
directive excludes unrelated v3 changes. This is the plugin-local
equivalent: one entry point, no inline JS in skill text.

Live verified from v3/: returns documented fallback shape
(agenticFlowAvailable: false, "bridge returns inert results") because
agentic-flow isn't installed in v3/node_modules.

Smoke: 37/37. Version: 0.7.0 → 0.8.0.

Co-Authored-By: RuFlo <ruv@ruv.net>
2026-05-04 23:58:00 -04:00

81 lines
3.1 KiB
JavaScript
Executable File

#!/usr/bin/env node
// cost-compact — wraps getTokenOptimizer().getCompactContext() so the
// cost-compact-context skill can invoke a single command instead of an
// inlined Node one-liner. A proper MCP tool wrapping getTokenOptimizer is
// still deferred (would require modifying @claude-flow/cli source); this
// is the plugin-local equivalent.
//
// Resolution: must run from a directory where `@claude-flow/integration`
// resolves (typically anywhere under `v3/`). The script resolves from
// process.cwd() rather than from its own location so the user's `cd v3`
// works without npm-installing the bridge into the plugin tree.
//
// Usage:
// node scripts/compact.mjs "<query>"
//
// Optional env:
// COMPACT_QUIET=1 emit JSON only (no markdown banner)
import { createRequire } from 'node:module';
import { pathToFileURL } from 'node:url';
import { join } from 'node:path';
async function main() {
const query = process.argv[2] || '';
if (!query) {
console.error('usage: compact.mjs "<query>"');
process.exit(2);
}
let mod;
try {
const requireFromCwd = createRequire(join(process.cwd(), 'package.json'));
const resolved = requireFromCwd.resolve('@claude-flow/integration/token-optimizer');
mod = await import(pathToFileURL(resolved).href);
} catch (err) {
const out = {
bridgeUnavailable: true,
reason: String(err.message || err).slice(0, 300),
memoriesRetrieved: 0,
tokensSaved: 0,
agenticFlowAvailable: false,
};
if (process.env.COMPACT_QUIET === '1') return console.log(JSON.stringify(out));
console.log(`# cost-compact-context\n\nbridge unavailable: ${out.reason}`);
console.log('Run from a directory where `@claude-flow/integration` resolves, e.g. `cd v3 && ...`.');
return;
}
const { getTokenOptimizer } = mod;
const opt = await getTokenOptimizer();
const ctx = await opt.getCompactContext(query);
const stats = opt.getStats();
const out = {
query,
memoriesRetrieved: ctx.memories?.length ?? 0,
tokensSaved: ctx.tokensSaved ?? 0,
agenticFlowAvailable: !!stats?.agenticFlowAvailable,
cacheHitRate: stats?.cacheHitRate || '0%',
upstreamReported: 'tokensSaved is bridge-reported (heuristic), not measured against a no-RAG baseline',
};
if (process.env.COMPACT_QUIET === '1') return console.log(JSON.stringify(out));
console.log(`# cost-compact-context — query: "${query}"`);
console.log('');
console.log('| Metric | Value |');
console.log('|---|---:|');
console.log(`| Memories retrieved | ${out.memoriesRetrieved} |`);
console.log(`| Tokens saved (bridge-reported) | ${out.tokensSaved} |`);
console.log(`| agentic-flow bridge available | ${out.agenticFlowAvailable} |`);
console.log(`| Cache hit rate | ${out.cacheHitRate} |`);
console.log('');
console.log(`> ${out.upstreamReported}`);
if (!out.agenticFlowAvailable) {
console.log('');
console.log('agentic-flow not installed — bridge returns inert results. No compact-context savings.');
}
}
main().catch((e) => { console.error('compact.mjs failed:', e.message || e); process.exit(1); });