tokensafe 1.3.4 → 1.3.5
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/README.md +117 -28
- package/bin/agy-tokens +4 -292
- package/bin/tokensafe +17 -0
- package/bin/tokensafe.js +27 -69
- package/extensions/cursor-tokensafe/extension.js +2 -1
- package/hooks/pre_invocation.py +27 -9
- package/package.json +2 -1
- package/plugin.json +1 -1
- package/skills/token-optimizer/SKILL.md +1 -1
- package/src/benchmark.py +2 -2
- package/src/cli.py +332 -0
- package/src/compactor.py +318 -60
- package/src/dashboard.py +46 -2
- package/src/deepseek_distiller.py +6 -3
- package/src/distiller.py +6 -3
- package/src/environment_auditor.py +6 -6
- package/src/license_manager.py +62 -2
- package/src/metrics.py +13 -0
- package/src/version_checker.py +1 -1
|
@@ -129,11 +129,11 @@ class EnvironmentAuditor:
|
|
|
129
129
|
return None
|
|
130
130
|
|
|
131
131
|
bloated = report["bloated_servers"]
|
|
132
|
-
names = ", ".join([f"`{b['name']}` ({b['tool_count']}
|
|
132
|
+
names = ", ".join([f"`{b['name']}` ({b['tool_count']} tools, ~{b['est_tokens_per_turn']:,} tokens/turn)" for b in bloated])
|
|
133
133
|
return (
|
|
134
|
-
f"⚠️ **TokenSafe Bloat Alert**:
|
|
135
|
-
f"
|
|
136
|
-
f"
|
|
134
|
+
f"⚠️ **TokenSafe Bloat Alert**: Heavy MCP server detected: {names}. "
|
|
135
|
+
f"Static context overhead per turn: **~{report['total_static_tokens_per_turn']:,} tokens**. "
|
|
136
|
+
f"Disable unused servers via `tokensafe mcp disable <name>`."
|
|
137
137
|
)
|
|
138
138
|
|
|
139
139
|
@classmethod
|
|
@@ -228,7 +228,7 @@ class EnvironmentAuditor:
|
|
|
228
228
|
|
|
229
229
|
@classmethod
|
|
230
230
|
def format_cli_report(cls) -> str:
|
|
231
|
-
"""Formats visual CLI report for
|
|
231
|
+
"""Formats visual CLI report for tokensafe doctor."""
|
|
232
232
|
report = cls.audit()
|
|
233
233
|
lines = [
|
|
234
234
|
"\n\033[1m\033[36m⚡ TOKENSAFE ENVIRONMENT AUDIT (CONTEXT TAX DOCTOR) ⚡\033[0m\n",
|
|
@@ -251,7 +251,7 @@ class EnvironmentAuditor:
|
|
|
251
251
|
if report["has_critical_bloat"]:
|
|
252
252
|
lines.append("\n \033[33m\033[1mDiagnostic Recommendation:\033[0m")
|
|
253
253
|
for b in report["bloated_servers"]:
|
|
254
|
-
lines.append(f" ➜ Run: \033[
|
|
254
|
+
lines.append(f" ➜ Run: \033[36mtokensafe mcp disable {b['name']}\033[0m to save ~{b['est_tokens_per_turn']:,} tokens per turn!")
|
|
255
255
|
else:
|
|
256
256
|
lines.append("\n \033[32m✔ Environment healthy: No critical MCP bloat detected.\033[0m")
|
|
257
257
|
|
package/src/license_manager.py
CHANGED
|
@@ -129,7 +129,7 @@ class LicenseManager:
|
|
|
129
129
|
"""Manages TokenSafe subscription tiers, offline signature verification, and 7-day trials."""
|
|
130
130
|
|
|
131
131
|
TIERS = {
|
|
132
|
-
"free": {"max_tokens_monthly":
|
|
132
|
+
"free": {"max_tokens_monthly": 500_000, "deepseek_cloud": False, "team_analytics": False},
|
|
133
133
|
"solo": {"max_tokens_monthly": None, "deepseek_cloud": True, "team_analytics": False},
|
|
134
134
|
"team": {"max_tokens_monthly": None, "deepseek_cloud": True, "team_analytics": True},
|
|
135
135
|
"enterprise": {"max_tokens_monthly": None, "deepseek_cloud": True, "team_analytics": True, "self_hosted": True}
|
|
@@ -257,7 +257,7 @@ class LicenseManager:
|
|
|
257
257
|
if valid:
|
|
258
258
|
return payload.get("tier", "solo"), payload
|
|
259
259
|
|
|
260
|
-
return "free", {"tier": "free", "max_tokens_monthly":
|
|
260
|
+
return "free", {"tier": "free", "max_tokens_monthly": 500_000}
|
|
261
261
|
|
|
262
262
|
@classmethod
|
|
263
263
|
def save_license(cls, license_key: str) -> Tuple[bool, str]:
|
|
@@ -280,3 +280,63 @@ class LicenseManager:
|
|
|
280
280
|
tier_name = payload.get("tier", "").upper()
|
|
281
281
|
trial_note = " (7-Day Trial)" if payload.get("trial") else ""
|
|
282
282
|
return True, f"Successfully activated TokenSafe {tier_name}{trial_note}!"
|
|
283
|
+
|
|
284
|
+
@classmethod
|
|
285
|
+
def check_quota(cls, tracker: Optional[Any] = None) -> Dict[str, Any]:
|
|
286
|
+
"""
|
|
287
|
+
Evaluates whether the current active license quota is exceeded.
|
|
288
|
+
Returns:
|
|
289
|
+
{
|
|
290
|
+
"tier": str,
|
|
291
|
+
"is_quota_exceeded": bool,
|
|
292
|
+
"monthly_limit": Optional[int],
|
|
293
|
+
"monthly_saved": int,
|
|
294
|
+
"warning": Optional[str]
|
|
295
|
+
}
|
|
296
|
+
"""
|
|
297
|
+
tier, payload = cls.get_active_license()
|
|
298
|
+
tier_cfg = cls.TIERS.get(tier, cls.TIERS["free"])
|
|
299
|
+
max_tokens = tier_cfg.get("max_tokens_monthly")
|
|
300
|
+
|
|
301
|
+
# Paid tiers (solo, team, enterprise) have unlimited local optimization
|
|
302
|
+
if max_tokens is None:
|
|
303
|
+
return {
|
|
304
|
+
"tier": tier,
|
|
305
|
+
"is_quota_exceeded": False,
|
|
306
|
+
"monthly_limit": None,
|
|
307
|
+
"monthly_saved": 0,
|
|
308
|
+
"warning": None
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
# For free tier, calculate trailing 30 days saved tokens
|
|
312
|
+
if tracker is None:
|
|
313
|
+
try:
|
|
314
|
+
from src.metrics import MetricsTracker
|
|
315
|
+
tracker = MetricsTracker()
|
|
316
|
+
except Exception:
|
|
317
|
+
tracker = None
|
|
318
|
+
|
|
319
|
+
monthly_saved = tracker.get_monthly_saved_tokens() if tracker else 0
|
|
320
|
+
exceeded = monthly_saved >= max_tokens
|
|
321
|
+
|
|
322
|
+
warning = None
|
|
323
|
+
if exceeded:
|
|
324
|
+
warning = (
|
|
325
|
+
f"⚡ **TokenSafe Quota Alert**: Community Edition limit reached "
|
|
326
|
+
f"({monthly_saved:,}/{max_tokens:,} tokens saved this month). "
|
|
327
|
+
f"Advanced optimization is throttled. Upgrade to Solo for unlimited optimization: `tokensafe activate <key>` or visit https://tokensafe.dev."
|
|
328
|
+
)
|
|
329
|
+
elif monthly_saved >= int(max_tokens * 0.85):
|
|
330
|
+
warning = (
|
|
331
|
+
f"⚡ **TokenSafe Quota Notice**: Community Edition is at {int(monthly_saved / max_tokens * 100)}% of monthly limit "
|
|
332
|
+
f"({monthly_saved:,}/{max_tokens:,} tokens). "
|
|
333
|
+
f"Upgrade to Solo for unlimited optimization: `tokensafe activate <key>` or https://tokensafe.dev."
|
|
334
|
+
)
|
|
335
|
+
|
|
336
|
+
return {
|
|
337
|
+
"tier": tier,
|
|
338
|
+
"is_quota_exceeded": exceeded,
|
|
339
|
+
"monthly_limit": max_tokens,
|
|
340
|
+
"monthly_saved": monthly_saved,
|
|
341
|
+
"warning": warning
|
|
342
|
+
}
|
package/src/metrics.py
CHANGED
|
@@ -211,6 +211,19 @@ class MetricsTracker:
|
|
|
211
211
|
"breakdown": combined_breakdown
|
|
212
212
|
}
|
|
213
213
|
|
|
214
|
+
def get_monthly_saved_tokens(self) -> int:
|
|
215
|
+
"""Returns total tokens saved in the trailing 30 days for quota calculations."""
|
|
216
|
+
thirty_days_ago = (datetime.utcnow() - timedelta(days=30)).isoformat()
|
|
217
|
+
with self._get_connection() as conn:
|
|
218
|
+
cursor = conn.cursor()
|
|
219
|
+
cursor.execute("""
|
|
220
|
+
SELECT COALESCE(SUM(saved_tokens), 0) as monthly_saved
|
|
221
|
+
FROM events
|
|
222
|
+
WHERE timestamp >= ?
|
|
223
|
+
""", (thirty_days_ago,))
|
|
224
|
+
row = cursor.fetchone()
|
|
225
|
+
return int(row["monthly_saved"]) if row else 0
|
|
226
|
+
|
|
214
227
|
def get_recent_events(self, limit: int = 10) -> List[Dict[str, Any]]:
|
|
215
228
|
"""Returns the most recent optimization events."""
|
|
216
229
|
with self._get_connection() as conn:
|
package/src/version_checker.py
CHANGED
|
@@ -14,7 +14,7 @@ import urllib.request
|
|
|
14
14
|
import urllib.error
|
|
15
15
|
from typing import Dict, Any, Optional, Tuple
|
|
16
16
|
|
|
17
|
-
CURRENT_VERSION = "1.3.
|
|
17
|
+
CURRENT_VERSION = "1.3.5"
|
|
18
18
|
CACHE_TTL = 86400 # 24 hours
|
|
19
19
|
|
|
20
20
|
PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|