Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions bin/tokencut.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ const flag = (name, def) => { const i = args.indexOf(name); return i >= 0 ? (arg
const has = (name) => args.includes(name);
const file = args.find((a) => !a.startsWith("--") && args[args.indexOf(a) - 1] !== "--max" && args[args.indexOf(a) - 1] !== "--max-tool" && args[args.indexOf(a) - 1] !== "--price" && args[args.indexOf(a) - 1] !== "--out");

if (has("--version") || has("-V")) {
try {
const pkg = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8"));
console.log(pkg.version ?? "0.0.0");
} catch {
console.log("0.0.0");
}
process.exit(0);
}

if (!file || has("--help")) {
console.log(`tokencut -- measure and cut the token cost of an LLM/agent payload

Expand All @@ -18,6 +28,7 @@ if (!file || has("--help")) {
--out <file> write the compacted payload
--price <n> $ per 1M input tokens for the cost estimate (default 3)
--json machine-readable output
--version, -V print package version

Payload: an array of messages, or { system, messages } (Anthropic or OpenAI style).`);
process.exit(file ? 0 : 1);
Expand Down
13 changes: 12 additions & 1 deletion test/cli.test.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import assert from "node:assert/strict";
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { spawnSync } from "node:child_process";
Expand Down Expand Up @@ -56,3 +56,14 @@ test("accepts zero as a compact token budget", () => {
assert.equal(result.status, 0, result.stderr);
assert.doesNotThrow(() => JSON.parse(result.stdout));
});


test("--version prints package version", () => {
const r = spawnSync(process.execPath, ["bin/tokencut.mjs", "--version"], {
cwd: new URL("..", import.meta.url),
encoding: "utf8",
});
assert.equal(r.status, 0);
const ver = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8")).version;
assert.equal(r.stdout.trim(), ver);
});
Loading