@alexgreensh/token-optimizer 0.0.0-stage → 5.13.51
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.
- package/.claude-plugin/marketplace.json +76 -0
- package/.claude-plugin/plugin.json +21 -0
- package/LICENSE +130 -0
- package/LICENSE-SMALL-BUSINESS.md +62 -0
- package/PRIVACY.md +200 -0
- package/README.md +13 -2
- package/hooks/codex-hooks.json +4 -0
- package/hooks/hooks.json +182 -0
- package/hooks/module_runner.py +448 -0
- package/hooks/posttooluse_runner.py +1110 -0
- package/hooks/python-launcher.sh +816 -0
- package/hooks/run.py +788 -0
- package/hooks/sessionstart_runner.py +629 -0
- package/hooks/stop_runner.py +408 -0
- package/hooks/userpromptsubmit_runner.py +722 -0
- package/hooks/windows-launcher.py +163 -0
- package/install-cloud.sh +23 -0
- package/package.json +29 -4
- package/skills/fleet-auditor/SKILL.md +149 -0
- package/skills/fleet-auditor/references/fleet-systems.md +167 -0
- package/skills/fleet-auditor/references/report-template.md +50 -0
- package/skills/fleet-auditor/references/waste-patterns.md +126 -0
- package/skills/fleet-auditor/scripts/fleet.py +3633 -0
- package/skills/fleet-auditor/scripts/shared.py +277 -0
- package/skills/resume-checkpoint/SKILL.md +52 -0
- package/skills/resume-checkpoint/scripts/pull_checkpoint.py +188 -0
- package/skills/token-coach/SKILL.md +141 -0
- package/skills/token-coach/assets/coach-dashboard-tab.html +45 -0
- package/skills/token-coach/examples/coaching-session-agentic.md +62 -0
- package/skills/token-coach/examples/coaching-session-heavy-setup.md +46 -0
- package/skills/token-coach/examples/coaching-session-new-project.md +38 -0
- package/skills/token-coach/references/agentic-systems.md +147 -0
- package/skills/token-coach/references/coach-patterns.md +120 -0
- package/skills/token-coach/references/coaching-scripts.md +146 -0
- package/skills/token-coach/references/deterministic-stages.md +102 -0
- package/skills/token-coach/references/keepwarm-consent.md +48 -0
- package/skills/token-coach/references/quick-reference.md +155 -0
- package/skills/token-dashboard/SKILL.md +93 -0
- package/skills/token-optimizer/SKILL.md +150 -0
- package/skills/token-optimizer/assets/dashboard.html +7251 -0
- package/skills/token-optimizer/examples/claude-md-optimized.md +40 -0
- package/skills/token-optimizer/examples/hooks-starter.json +179 -0
- package/skills/token-optimizer/examples/permissions-deny-template.json +24 -0
- package/skills/token-optimizer/pricing/prices.json +695 -0
- package/skills/token-optimizer/references/agent-prompts.md +687 -0
- package/skills/token-optimizer/references/cli-reference.md +92 -0
- package/skills/token-optimizer/references/codex-workflow.md +188 -0
- package/skills/token-optimizer/references/cursor-workflow.md +74 -0
- package/skills/token-optimizer/references/error-recovery.md +37 -0
- package/skills/token-optimizer/references/implementation-playbook.md +416 -0
- package/skills/token-optimizer/references/keepwarm-consent.md +48 -0
- package/skills/token-optimizer/references/opencode-workflow.md +72 -0
- package/skills/token-optimizer/references/optimization-checklist.md +714 -0
- package/skills/token-optimizer/references/phase0-setup.md +142 -0
- package/skills/token-optimizer/references/presentation-workflow.md +72 -0
- package/skills/token-optimizer/references/session-continuity.md +19 -0
- package/skills/token-optimizer/references/token-flow-architecture.md +629 -0
- package/skills/token-optimizer/scripts/activity_tracker.py +143 -0
- package/skills/token-optimizer/scripts/antigravity_doctor.py +548 -0
- package/skills/token-optimizer/scripts/antigravity_hook_bridge.py +511 -0
- package/skills/token-optimizer/scripts/antigravity_install.py +379 -0
- package/skills/token-optimizer/scripts/antigravity_proto.py +413 -0
- package/skills/token-optimizer/scripts/antigravity_session.py +346 -0
- package/skills/token-optimizer/scripts/antigravity_state.py +486 -0
- package/skills/token-optimizer/scripts/archive_result.py +1725 -0
- package/skills/token-optimizer/scripts/bash_compress.py +2490 -0
- package/skills/token-optimizer/scripts/bash_compress_hook.py +766 -0
- package/skills/token-optimizer/scripts/bash_hook.py +61 -0
- package/skills/token-optimizer/scripts/bash_whitelist.py +243 -0
- package/skills/token-optimizer/scripts/benchmark.py +1765 -0
- package/skills/token-optimizer/scripts/build_output_compress.py +776 -0
- package/skills/token-optimizer/scripts/cloud_kit.py +1011 -0
- package/skills/token-optimizer/scripts/codex_command_compress.py +434 -0
- package/skills/token-optimizer/scripts/codex_compact_prompt.py +273 -0
- package/skills/token-optimizer/scripts/codex_doctor.py +454 -0
- package/skills/token-optimizer/scripts/codex_hook_bridge.py +311 -0
- package/skills/token-optimizer/scripts/codex_install.py +810 -0
- package/skills/token-optimizer/scripts/codex_io.py +119 -0
- package/skills/token-optimizer/scripts/codex_log_index.py +219 -0
- package/skills/token-optimizer/scripts/codex_models.py +34 -0
- package/skills/token-optimizer/scripts/codex_session.py +1186 -0
- package/skills/token-optimizer/scripts/codex_state.py +707 -0
- package/skills/token-optimizer/scripts/codex_statusline.py +303 -0
- package/skills/token-optimizer/scripts/command_filters.py +386 -0
- package/skills/token-optimizer/scripts/compression_backfill.py +600 -0
- package/skills/token-optimizer/scripts/compression_log.py +230 -0
- package/skills/token-optimizer/scripts/context_intel.py +331 -0
- package/skills/token-optimizer/scripts/context_pressure.py +115 -0
- package/skills/token-optimizer/scripts/copilot_doctor.py +406 -0
- package/skills/token-optimizer/scripts/copilot_hook_bridge.py +709 -0
- package/skills/token-optimizer/scripts/copilot_install.py +288 -0
- package/skills/token-optimizer/scripts/copilot_session.py +496 -0
- package/skills/token-optimizer/scripts/copilot_state.py +569 -0
- package/skills/token-optimizer/scripts/copilot_vscode.py +832 -0
- package/skills/token-optimizer/scripts/cowork_doctor.py +270 -0
- package/skills/token-optimizer/scripts/cowork_install.py +380 -0
- package/skills/token-optimizer/scripts/credential_patterns.py +1514 -0
- package/skills/token-optimizer/scripts/cursor_doctor.py +602 -0
- package/skills/token-optimizer/scripts/cursor_hook_bridge.py +843 -0
- package/skills/token-optimizer/scripts/cursor_install.py +426 -0
- package/skills/token-optimizer/scripts/cursor_session.py +303 -0
- package/skills/token-optimizer/scripts/cursor_state.py +348 -0
- package/skills/token-optimizer/scripts/delta_diff.py +110 -0
- package/skills/token-optimizer/scripts/detectors/__init__.py +2 -0
- package/skills/token-optimizer/scripts/detectors/bad_decomposition.py +78 -0
- package/skills/token-optimizer/scripts/detectors/cache_instability.py +504 -0
- package/skills/token-optimizer/scripts/detectors/looping.py +88 -0
- package/skills/token-optimizer/scripts/detectors/output_waste.py +143 -0
- package/skills/token-optimizer/scripts/detectors/overpowered.py +116 -0
- package/skills/token-optimizer/scripts/detectors/pdf_ingestion.py +52 -0
- package/skills/token-optimizer/scripts/detectors/registry.py +65 -0
- package/skills/token-optimizer/scripts/detectors/respond_to_bash.py +76 -0
- package/skills/token-optimizer/scripts/detectors/retry_churn.py +83 -0
- package/skills/token-optimizer/scripts/detectors/tool_cascade.py +62 -0
- package/skills/token-optimizer/scripts/detectors/wasteful_thinking.py +75 -0
- package/skills/token-optimizer/scripts/detectors/weak_model.py +55 -0
- package/skills/token-optimizer/scripts/detectors/websearch_routing.py +72 -0
- package/skills/token-optimizer/scripts/deterministic_candidates.py +1458 -0
- package/skills/token-optimizer/scripts/devin_compress.py +790 -0
- package/skills/token-optimizer/scripts/devin_doctor.py +403 -0
- package/skills/token-optimizer/scripts/devin_hooks.py +453 -0
- package/skills/token-optimizer/scripts/devin_install.py +1086 -0
- package/skills/token-optimizer/scripts/devin_source.py +119 -0
- package/skills/token-optimizer/scripts/devin_state.py +330 -0
- package/skills/token-optimizer/scripts/devin_verify.py +67 -0
- package/skills/token-optimizer/scripts/grok_doctor.py +560 -0
- package/skills/token-optimizer/scripts/grok_hook_bridge.py +699 -0
- package/skills/token-optimizer/scripts/grok_install.py +400 -0
- package/skills/token-optimizer/scripts/grok_session.py +317 -0
- package/skills/token-optimizer/scripts/grok_state.py +299 -0
- package/skills/token-optimizer/scripts/hermes_doctor.py +479 -0
- package/skills/token-optimizer/scripts/hermes_hook_bridge.py +285 -0
- package/skills/token-optimizer/scripts/hermes_install.py +668 -0
- package/skills/token-optimizer/scripts/hermes_session.py +691 -0
- package/skills/token-optimizer/scripts/hermes_state.py +448 -0
- package/skills/token-optimizer/scripts/hook_io.py +120 -0
- package/skills/token-optimizer/scripts/hook_runtime.py +716 -0
- package/skills/token-optimizer/scripts/injection.py +156 -0
- package/skills/token-optimizer/scripts/install_reconcile.py +952 -0
- package/skills/token-optimizer/scripts/measure.py +59531 -0
- package/skills/token-optimizer/scripts/outline.py +53 -0
- package/skills/token-optimizer/scripts/pipeline_analyzer.py +723 -0
- package/skills/token-optimizer/scripts/plugin_env.py +562 -0
- package/skills/token-optimizer/scripts/py_trust.py +80 -0
- package/skills/token-optimizer/scripts/quality_cache_gate.py +320 -0
- package/skills/token-optimizer/scripts/read_cache.py +2489 -0
- package/skills/token-optimizer/scripts/recovery_output.py +110 -0
- package/skills/token-optimizer/scripts/refetch_fingerprint.py +236 -0
- package/skills/token-optimizer/scripts/refetch_guard.py +244 -0
- package/skills/token-optimizer/scripts/routing_advisor.py +389 -0
- package/skills/token-optimizer/scripts/runtime_env.py +1923 -0
- package/skills/token-optimizer/scripts/session_store.py +979 -0
- package/skills/token-optimizer/scripts/spawn_utils.py +122 -0
- package/skills/token-optimizer/scripts/statusline.js +660 -0
- package/skills/token-optimizer/scripts/structure_map.py +2880 -0
- package/skills/token-optimizer/scripts/structure_map_ts.py +676 -0
- package/skills/token-optimizer/scripts/structure_replay.py +1556 -0
- package/skills/token-optimizer/scripts/thrash_guard.py +604 -0
- package/skills/token-optimizer/scripts/token_estimate.py +80 -0
- package/skills/token-optimizer/scripts/tool_search_diagnostic.py +61 -0
- package/skills/token-optimizer/scripts/utf8_io.py +219 -0
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""Token Optimizer — GitHub Copilot CLI installer.
|
|
3
|
+
|
|
4
|
+
Wires Token Optimizer into a Copilot CLI setup:
|
|
5
|
+
|
|
6
|
+
1. Copies the adapter modules into <copilot_home>/token-optimizer/plugin/
|
|
7
|
+
so the hook bridge runs from a stable path that survives repo moves.
|
|
8
|
+
2. Writes the hooks config to <copilot_home>/hooks/token-optimizer.json
|
|
9
|
+
(USER-LEVEL ONLY — never .github/hooks/, which would silently affect a
|
|
10
|
+
whole team's repo without consent; user-level hooks load in all modes
|
|
11
|
+
including non-interactive `copilot -p`, per the Copilot CLI project).
|
|
12
|
+
3. Seeds capabilities.json for the installed CLI version.
|
|
13
|
+
|
|
14
|
+
Idempotent: re-running refreshes the payload and rewrites OUR hook file only.
|
|
15
|
+
Uninstall removes only files Token Optimizer created.
|
|
16
|
+
|
|
17
|
+
Usage:
|
|
18
|
+
python3 copilot_install.py install [--dry-run]
|
|
19
|
+
python3 copilot_install.py uninstall [--dry-run]
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
from __future__ import annotations
|
|
23
|
+
|
|
24
|
+
import argparse
|
|
25
|
+
import json
|
|
26
|
+
import os
|
|
27
|
+
import shlex
|
|
28
|
+
import shutil
|
|
29
|
+
import stat as _stat
|
|
30
|
+
import sys
|
|
31
|
+
from pathlib import Path
|
|
32
|
+
|
|
33
|
+
_SCRIPT_DIR = Path(__file__).resolve().parent
|
|
34
|
+
sys.path.insert(0, str(_SCRIPT_DIR))
|
|
35
|
+
|
|
36
|
+
from py_trust import py_path_is_trusted, py_trust_reason # noqa: E402
|
|
37
|
+
from runtime_env import copilot_home, measure_cli as _measure_cli, with_measure_cli as _hint # noqa: E402
|
|
38
|
+
|
|
39
|
+
HOOK_FILE_NAME = "token-optimizer.json"
|
|
40
|
+
HOOK_TIMEOUT_SEC = 10
|
|
41
|
+
|
|
42
|
+
# Modules the bridge needs at runtime, copied next to it so the installed
|
|
43
|
+
# hook never depends on the repo checkout location.
|
|
44
|
+
_PAYLOAD_MODULES = (
|
|
45
|
+
"copilot_hook_bridge.py",
|
|
46
|
+
"copilot_state.py",
|
|
47
|
+
"copilot_session.py",
|
|
48
|
+
"copilot_vscode.py",
|
|
49
|
+
"bash_hook.py",
|
|
50
|
+
"bash_compress.py",
|
|
51
|
+
# Dependency-free whitelist gate shared by bash_hook + bash_compress.
|
|
52
|
+
"bash_whitelist.py",
|
|
53
|
+
"hook_io.py",
|
|
54
|
+
"plugin_env.py",
|
|
55
|
+
"runtime_env.py",
|
|
56
|
+
# codex_io supplies atomic_write_json — without it the bridge's tally and
|
|
57
|
+
# capabilities writes silently no-op (crash recovery would be dead).
|
|
58
|
+
"codex_io.py",
|
|
59
|
+
# hermes_session supplies compute_quality_score — without it copilot_session
|
|
60
|
+
# silently falls back to a single-signal estimate (degraded quality grades).
|
|
61
|
+
"hermes_session.py",
|
|
62
|
+
# spawn_utils supplies spawn_detached — without it the bridge logs
|
|
63
|
+
# "spawn_utils import failed" on every hook and its degraded fallback does
|
|
64
|
+
# NOT detach, so Stop-hook spawns die with the hook. hook_runtime supplies
|
|
65
|
+
# lease_lock (tally RMW); utf8_io is enforced at startup. Cursor and Grok
|
|
66
|
+
# already ship all three.
|
|
67
|
+
"spawn_utils.py",
|
|
68
|
+
"hook_runtime.py",
|
|
69
|
+
"utf8_io.py",
|
|
70
|
+
# credential_patterns: bash_compress's credential-preservation patterns and
|
|
71
|
+
# copilot_session's topic redaction (both fall back to less without it).
|
|
72
|
+
"credential_patterns.py",
|
|
73
|
+
)
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def _plugin_dir(root: Path) -> Path:
|
|
77
|
+
return root / "token-optimizer" / "plugin"
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def _hooks_dir(root: Path) -> Path:
|
|
81
|
+
return root / "hooks"
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def _py_trust_reason(p: str) -> str | None:
|
|
85
|
+
"""None when trusted, else a short human-readable rejection reason."""
|
|
86
|
+
return py_trust_reason(p)
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def _py_path_is_trusted(p: str) -> bool:
|
|
90
|
+
"""Trusted iff the interpreter's bytes are admin-owned (euid or root) and
|
|
91
|
+
not group/other-writable, and its dir is not world-writable and not
|
|
92
|
+
group-writable by a third party -- the launcher's ownership boundary
|
|
93
|
+
(ssh/sudo/git), in Python. Pure stat, never runs the target. On Windows,
|
|
94
|
+
stat ownership is unreliable under Git-Bash, so require only that the path
|
|
95
|
+
is a real file (the launcher leans on hardcoded allowlists there too)."""
|
|
96
|
+
return py_path_is_trusted(p)
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def _resolve_safe_python() -> str:
|
|
100
|
+
"""An ABSOLUTE, trusted python for the persisted Copilot hook command.
|
|
101
|
+
|
|
102
|
+
Never emit a bare "python3": that string is resolved via $PATH every time the
|
|
103
|
+
hook fires, so a hijacked PATH entry runs attacker code (the launcher's whole
|
|
104
|
+
allowlist exists to stop exactly this, and the Copilot bridge does not use the
|
|
105
|
+
launcher). Resolution order:
|
|
106
|
+
1. TOKEN_OPTIMIZER_PYTHON, if it names a trusted file (user escape hatch,
|
|
107
|
+
same env the launcher honours);
|
|
108
|
+
2. sys.executable -- the interpreter already running this installer, an
|
|
109
|
+
absolute path baked in ONCE here so the hook never does a PATH lookup,
|
|
110
|
+
but only through the same ownership gate (a writable venv interpreter
|
|
111
|
+
must never be persisted);
|
|
112
|
+
3. a $PATH search, but only accepting a candidate that passes the ownership
|
|
113
|
+
gate above.
|
|
114
|
+
Raises RuntimeError rather than persist an unsafe command -- recoverable by
|
|
115
|
+
setting TOKEN_OPTIMIZER_PYTHON.
|
|
116
|
+
"""
|
|
117
|
+
override = os.environ.get("TOKEN_OPTIMIZER_PYTHON", "").strip()
|
|
118
|
+
candidates = []
|
|
119
|
+
if override:
|
|
120
|
+
candidates.append(("TOKEN_OPTIMIZER_PYTHON", override))
|
|
121
|
+
if sys.executable:
|
|
122
|
+
candidates.append(("sys.executable", sys.executable))
|
|
123
|
+
for name in ("python3", "python"):
|
|
124
|
+
cand = shutil.which(name)
|
|
125
|
+
if cand:
|
|
126
|
+
candidates.append((name, cand))
|
|
127
|
+
for _label, cand in candidates:
|
|
128
|
+
if _py_path_is_trusted(cand):
|
|
129
|
+
# Persist the RESOLVED realpath, not abspath: the gate validated
|
|
130
|
+
# realpath(cand) (the symlink target + its parent dir), so persisting
|
|
131
|
+
# the original symlink path would leave a swap window between install
|
|
132
|
+
# and hook fire (an attacker with write access to the symlink's parent
|
|
133
|
+
# dir could redirect the symlink to a malicious interpreter).
|
|
134
|
+
return os.path.realpath(cand)
|
|
135
|
+
reasons = [f"{label}={cand}: {_py_trust_reason(cand)}"
|
|
136
|
+
for label, cand in candidates]
|
|
137
|
+
raise RuntimeError(
|
|
138
|
+
"no trusted python interpreter found for the Copilot hook; "
|
|
139
|
+
"set TOKEN_OPTIMIZER_PYTHON to an absolute python3 path and re-run install. "
|
|
140
|
+
"Candidates: " + "; ".join(reasons)
|
|
141
|
+
)
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
def _hook_config(bridge_path: Path) -> dict:
|
|
145
|
+
"""The hooks file Copilot loads. Format per the official hooks reference:
|
|
146
|
+
{"version": 1, "hooks": {event: [{"type": "command", "bash": ...}]}}.
|
|
147
|
+
"""
|
|
148
|
+
py = _resolve_safe_python()
|
|
149
|
+
# shlex.quote both paths: a HOME/COPILOT_HOME containing a space, $, or
|
|
150
|
+
# backtick would otherwise break the bash string or inject a subcommand.
|
|
151
|
+
# TOKEN_OPTIMIZER_RUNTIME is pinned so the bridge never process-scans on
|
|
152
|
+
# the hot path and never falls through to the Claude default.
|
|
153
|
+
py_q = shlex.quote(py)
|
|
154
|
+
bridge_q = shlex.quote(str(bridge_path))
|
|
155
|
+
|
|
156
|
+
def cmd(event: str) -> dict:
|
|
157
|
+
return {
|
|
158
|
+
"type": "command",
|
|
159
|
+
"bash": f"TOKEN_OPTIMIZER_RUNTIME=copilot {py_q} {bridge_q} {event}",
|
|
160
|
+
"timeoutSec": HOOK_TIMEOUT_SEC,
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
pre = cmd("pre-tool-use")
|
|
164
|
+
# Only the bash tool is rewritten; the matcher keeps every other tool
|
|
165
|
+
# call out of the bridge's hot path entirely.
|
|
166
|
+
pre["matcher"] = {"toolName": "bash"}
|
|
167
|
+
|
|
168
|
+
return {
|
|
169
|
+
"version": 1,
|
|
170
|
+
"hooks": {
|
|
171
|
+
"sessionStart": [cmd("session-start")],
|
|
172
|
+
"preToolUse": [pre],
|
|
173
|
+
"postToolUse": [cmd("post-tool-use")],
|
|
174
|
+
"stop": [cmd("stop")],
|
|
175
|
+
},
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
def install(*, dry_run: bool = False, home: Path = None) -> dict:
|
|
180
|
+
"""Install the adapter. Returns a summary dict of actions taken."""
|
|
181
|
+
root = home if home is not None else copilot_home()
|
|
182
|
+
actions = {"copied": [], "hook_file": None, "skipped": [], "dry_run": dry_run}
|
|
183
|
+
|
|
184
|
+
plugin_dir = _plugin_dir(root)
|
|
185
|
+
hooks_dir = _hooks_dir(root)
|
|
186
|
+
|
|
187
|
+
if hooks_dir.exists() and not hooks_dir.is_dir():
|
|
188
|
+
raise RuntimeError(
|
|
189
|
+
f"{hooks_dir} exists but is not a directory — refusing to install. "
|
|
190
|
+
"Move it aside and re-run."
|
|
191
|
+
)
|
|
192
|
+
|
|
193
|
+
for name in _PAYLOAD_MODULES:
|
|
194
|
+
src = _SCRIPT_DIR / name
|
|
195
|
+
if not src.exists():
|
|
196
|
+
actions["skipped"].append(name)
|
|
197
|
+
continue
|
|
198
|
+
dest = plugin_dir / name
|
|
199
|
+
if not dry_run:
|
|
200
|
+
try:
|
|
201
|
+
plugin_dir.mkdir(parents=True, exist_ok=True)
|
|
202
|
+
shutil.copy2(src, dest)
|
|
203
|
+
except OSError as exc:
|
|
204
|
+
# A partial payload would silently break the bridge. Abort
|
|
205
|
+
# BEFORE writing the hook config so a broken install never wires
|
|
206
|
+
# hooks that point at missing modules.
|
|
207
|
+
raise RuntimeError(f"failed copying {name}: {exc}") from exc
|
|
208
|
+
actions["copied"].append(name)
|
|
209
|
+
|
|
210
|
+
if actions["skipped"]:
|
|
211
|
+
raise RuntimeError(
|
|
212
|
+
"missing payload modules in this checkout: "
|
|
213
|
+
f"{actions['skipped']} — refusing to wire hooks against an incomplete bridge."
|
|
214
|
+
)
|
|
215
|
+
|
|
216
|
+
hook_path = hooks_dir / HOOK_FILE_NAME
|
|
217
|
+
config = _hook_config(plugin_dir / "copilot_hook_bridge.py")
|
|
218
|
+
if not dry_run:
|
|
219
|
+
try:
|
|
220
|
+
hooks_dir.mkdir(parents=True, exist_ok=True)
|
|
221
|
+
hook_path.write_text(json.dumps(config, indent=2) + "\n", encoding="utf-8")
|
|
222
|
+
except OSError as exc:
|
|
223
|
+
raise RuntimeError(f"failed writing hook config: {exc}") from exc
|
|
224
|
+
actions["hook_file"] = str(hook_path)
|
|
225
|
+
|
|
226
|
+
# Seed capabilities for the installed CLI version (best-effort).
|
|
227
|
+
if not dry_run:
|
|
228
|
+
try:
|
|
229
|
+
import copilot_hook_bridge
|
|
230
|
+
|
|
231
|
+
copilot_hook_bridge.load_capabilities(refresh=True)
|
|
232
|
+
except Exception:
|
|
233
|
+
pass
|
|
234
|
+
|
|
235
|
+
return actions
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
def uninstall(*, dry_run: bool = False, home: Path = None) -> dict:
|
|
239
|
+
"""Remove ONLY what install() created. Session data and trends stay."""
|
|
240
|
+
root = home if home is not None else copilot_home()
|
|
241
|
+
actions = {"removed": [], "dry_run": dry_run}
|
|
242
|
+
|
|
243
|
+
hook_path = _hooks_dir(root) / HOOK_FILE_NAME
|
|
244
|
+
if hook_path.exists():
|
|
245
|
+
if not dry_run:
|
|
246
|
+
hook_path.unlink()
|
|
247
|
+
actions["removed"].append(str(hook_path))
|
|
248
|
+
|
|
249
|
+
plugin_dir = _plugin_dir(root)
|
|
250
|
+
if plugin_dir.exists():
|
|
251
|
+
if not dry_run:
|
|
252
|
+
shutil.rmtree(plugin_dir)
|
|
253
|
+
actions["removed"].append(str(plugin_dir))
|
|
254
|
+
|
|
255
|
+
return actions
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
def build_parser() -> argparse.ArgumentParser:
|
|
259
|
+
parser = argparse.ArgumentParser(description=__doc__)
|
|
260
|
+
parser.add_argument("action", choices=("install", "uninstall"))
|
|
261
|
+
parser.add_argument("--dry-run", action="store_true")
|
|
262
|
+
return parser
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
def main(argv=None) -> int:
|
|
266
|
+
args = build_parser().parse_args(argv)
|
|
267
|
+
try:
|
|
268
|
+
if args.action == "install":
|
|
269
|
+
result = install(dry_run=args.dry_run)
|
|
270
|
+
verb = "Would install" if args.dry_run else "Installed"
|
|
271
|
+
print(f"{verb} Token Optimizer for GitHub Copilot CLI.")
|
|
272
|
+
print(f" Hook config: {result['hook_file']}")
|
|
273
|
+
print(f" Modules: {len(result['copied'])} copied"
|
|
274
|
+
+ (f", {len(result['skipped'])} missing: {result['skipped']}" if result["skipped"] else ""))
|
|
275
|
+
print(_hint(" Run `python3 measure.py copilot-doctor` to verify readiness."))
|
|
276
|
+
else:
|
|
277
|
+
result = uninstall(dry_run=args.dry_run)
|
|
278
|
+
verb = "Would remove" if args.dry_run else "Removed"
|
|
279
|
+
for item in result["removed"] or ["(nothing installed)"]:
|
|
280
|
+
print(f"{verb}: {item}")
|
|
281
|
+
return 0
|
|
282
|
+
except RuntimeError as exc:
|
|
283
|
+
print(f"[Token Optimizer] {exc}", file=sys.stderr)
|
|
284
|
+
return 1
|
|
285
|
+
|
|
286
|
+
|
|
287
|
+
if __name__ == "__main__":
|
|
288
|
+
sys.exit(main())
|