tokenops 0.2.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
tokenops_cli.py ADDED
@@ -0,0 +1,71 @@
1
+ """TokenOps for Claude Code, command line. The plugin's slash commands call it through bin/tokenops.
2
+
3
+ tokenops [doctor] [--days N] [--redact] Why am I hitting my limit? (doctor is the default)
4
+ tokenops saver [on|off|status] Saver mode
5
+ """
6
+
7
+ from __future__ import annotations
8
+
9
+ import argparse
10
+ import os
11
+ import sys
12
+
13
+ sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
14
+
15
+ from tokenops_cc import ab, caveman, rtk, settings # noqa: E402
16
+ from tokenops_cc.doctor import diagnose, render # noqa: E402
17
+
18
+
19
+ def projects_dir() -> str:
20
+ home = os.environ.get("CLAUDE_CONFIG_DIR") or os.path.expanduser("~/.claude")
21
+ return os.path.join(home, "projects")
22
+
23
+
24
+ def saver_mode(action: str) -> str:
25
+ config = settings.load_config()
26
+ if action in ("on", "off"):
27
+ config["saver"] = action == "on"
28
+ settings.save_config(config)
29
+ if not config.get("saver"):
30
+ return "Saver mode is off. Turn it on with /tokenops:saver on"
31
+ return (
32
+ "Saver mode is on.\n"
33
+ " Resume guard: on (holds the first message after the cache expires, once)\n"
34
+ f" Smart RTK: {rtk.status()}\n"
35
+ f" caveman: {caveman.status()}\n"
36
+ "Turn it off with /tokenops:saver off" + ab.render(ab.results())
37
+ )
38
+
39
+
40
+ def main(argv: list[str] | None = None) -> None:
41
+ argv = sys.argv[1:] if argv is None else list(argv)
42
+ if not argv or argv[0].startswith("-") and argv[0] not in ("-h", "--help"):
43
+ argv = ["doctor", *argv] # `tokenops` on its own runs the doctor
44
+ ap = argparse.ArgumentParser(prog="tokenops")
45
+ sub = ap.add_subparsers(dest="cmd", required=True)
46
+ doctor = sub.add_parser("doctor", help="why am I hitting my limit? (the default)")
47
+ doctor.add_argument("--days", type=int, default=7)
48
+ doctor.add_argument("--top", type=int, default=3, help="how many prompts to show")
49
+ doctor.add_argument(
50
+ "--redact", action="store_true", help="hide prompts and project names (for screenshots)"
51
+ )
52
+ doctor.add_argument("--color", choices=["auto", "always", "never"], default="auto")
53
+ saver = sub.add_parser("saver", help="on, off or status")
54
+ saver.add_argument("action", nargs="?", default="status", choices=["on", "off", "status"])
55
+ a = ap.parse_args(argv)
56
+ if a.cmd == "saver":
57
+ print(saver_mode(a.action))
58
+ return
59
+ color = a.color == "always" or (
60
+ a.color == "auto" and sys.stdout.isatty() and not os.environ.get("NO_COLOR")
61
+ )
62
+ ascii_only = "utf" not in (getattr(sys.stdout, "encoding", "") or "").lower()
63
+ # run from the plugin folder (it has commands/), or from the PyPI package
64
+ here = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
65
+ in_plugin = os.path.isdir(os.path.join(here, "commands"))
66
+ d = diagnose(projects_dir(), a.days, os.getcwd(), a.top)
67
+ print(render(d, a.redact, color, ascii_only, in_plugin) + ab.render(ab.results()))
68
+
69
+
70
+ if __name__ == "__main__":
71
+ main()