tokensafe 1.3.5 → 1.3.6
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/hooks/__pycache__/pre_tool_use.cpython-313.pyc +0 -0
- package/hooks/pre_tool_use.py +16 -0
- package/package.json +1 -1
- package/plugin.json +1 -1
- package/src/cli.py +20 -10
- package/src/compactor.py +40 -36
- package/src/deepseek_distiller.py +6 -0
- package/src/distiller.py +10 -0
- package/src/version_checker.py +1 -1
|
Binary file
|
package/hooks/pre_tool_use.py
CHANGED
|
@@ -24,6 +24,7 @@ from src.metrics import MetricsTracker
|
|
|
24
24
|
from src.tokenizer import count_tokens
|
|
25
25
|
from src.agent_firewall import AgentFirewall
|
|
26
26
|
from src.diff_slicer import DiffSlicer
|
|
27
|
+
from src.license_manager import LicenseManager
|
|
27
28
|
|
|
28
29
|
def main():
|
|
29
30
|
try:
|
|
@@ -44,6 +45,8 @@ def main():
|
|
|
44
45
|
last_user_input = payload.get("lastUserInput", "")
|
|
45
46
|
|
|
46
47
|
tracker = MetricsTracker()
|
|
48
|
+
quota = LicenseManager.check_quota(tracker)
|
|
49
|
+
is_quota_exceeded = quota.get("is_quota_exceeded", False)
|
|
47
50
|
|
|
48
51
|
# 1. Loop Detector Guard (Applies to all tools)
|
|
49
52
|
is_loop, repeat_count, loop_msg = LoopDetector.check_loop(conversation_id, tool_name, args)
|
|
@@ -107,6 +110,11 @@ def main():
|
|
|
107
110
|
start_line = args.get("StartLine")
|
|
108
111
|
end_line = args.get("EndLine")
|
|
109
112
|
|
|
113
|
+
if is_quota_exceeded:
|
|
114
|
+
# Free tier quota reached: pass through raw read_file without AST skeletonization
|
|
115
|
+
print(json.dumps(response))
|
|
116
|
+
return
|
|
117
|
+
|
|
110
118
|
# Step 3A: Subagent Firewall — Check if file was already read in active session
|
|
111
119
|
is_hit, cached_text, saved_dedup = AgentFirewall.check_cache(
|
|
112
120
|
session_id=conversation_id,
|
|
@@ -193,6 +201,14 @@ def main():
|
|
|
193
201
|
# 4. run_command Optimization (Command Sanitizer & Diff Slicer)
|
|
194
202
|
elif tool_name == "run_command":
|
|
195
203
|
cmd = args.get("CommandLine", "")
|
|
204
|
+
if is_quota_exceeded:
|
|
205
|
+
# Free tier quota reached: pass through without AST diff slicing or savings tracking
|
|
206
|
+
new_cmd, est_saved, reason = CommandSanitizer.sanitize(cmd)
|
|
207
|
+
if new_cmd and est_saved > 0:
|
|
208
|
+
response["overwrite"] = {"CommandLine": new_cmd}
|
|
209
|
+
print(json.dumps(response))
|
|
210
|
+
return
|
|
211
|
+
|
|
196
212
|
# Check AST Diff Slicer first
|
|
197
213
|
diff_cmd, diff_saved, diff_reason = DiffSlicer.optimize_git_diff_command(cmd)
|
|
198
214
|
if diff_cmd:
|
package/package.json
CHANGED
package/plugin.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "agy-token-optimizer",
|
|
3
3
|
"displayName": "TokenSafe",
|
|
4
|
-
"version": "1.3.
|
|
4
|
+
"version": "1.3.6",
|
|
5
5
|
"description": "Zero-Loss Token Economy & Context Optimizer for AI Agents. Compresses prompts, slices AST/tool outputs, and prevents stuck loops.",
|
|
6
6
|
"logo": "assets/logo.svg",
|
|
7
7
|
"author": {
|
package/src/cli.py
CHANGED
|
@@ -179,22 +179,32 @@ def main():
|
|
|
179
179
|
else:
|
|
180
180
|
print(f" Expires: Never (Lifetime)")
|
|
181
181
|
else:
|
|
182
|
-
print(" Status: Free Tier (up to
|
|
182
|
+
print(" Status: Free Tier (up to 500,000 tokens/mo)")
|
|
183
183
|
print(" Upgrade: https://tokensafe.dev ($9/mo Solo with 7-Day Free Trial)\n")
|
|
184
184
|
print()
|
|
185
185
|
|
|
186
186
|
elif command in ("providers", "models", "keys"):
|
|
187
|
+
from src.license_manager import LicenseManager
|
|
187
188
|
from src.distiller import UniversalDistiller
|
|
188
|
-
|
|
189
|
+
tier, _ = LicenseManager.get_active_license()
|
|
189
190
|
print("\n\033[1m\033[36m⚡ TOKENSAFE LLM DISTILLATION PROVIDERS ⚡\033[0m\n")
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
191
|
+
if tier == "free":
|
|
192
|
+
print(" \033[33mTier: Free Community Edition\033[0m")
|
|
193
|
+
print(" Neural Semantic Distillation (Gemini / OpenAI / DeepSeek) is reserved for Solo & Team tiers.")
|
|
194
|
+
print(" Free Community Edition operates with the zero-latency, zero-cost Local Regex Compactor.\n")
|
|
195
|
+
print(" \033[1mTo unlock Neural LLM Distillation:\033[0m")
|
|
196
|
+
print(" 1. Start 7-day free trial or upgrade at: \033[36mhttps://tokensafe.dev\033[0m")
|
|
197
|
+
print(" 2. Activate your key: \033[32mtokensafe activate <KEY>\033[0m\n")
|
|
198
|
+
else:
|
|
199
|
+
configured = UniversalDistiller.get_configured_providers()
|
|
200
|
+
all_p = UniversalDistiller.PROVIDERS
|
|
201
|
+
for key, p in all_p.items():
|
|
202
|
+
if key in configured:
|
|
203
|
+
c = configured[key]
|
|
204
|
+
print(f" \033[32m✔\033[0m \033[1m{p['name']:<18}\033[0m [Active: {c['model']}]")
|
|
205
|
+
else:
|
|
206
|
+
print(f" \033[2m○ {p['name']:<18} [Set {p['env_key']} to activate]\033[0m")
|
|
207
|
+
print("\nSet environment variable or add to data/config.json to activate.\n")
|
|
198
208
|
|
|
199
209
|
elif command in ("markdown", "md", "export"):
|
|
200
210
|
print(dashboard.render_markdown())
|
package/src/compactor.py
CHANGED
|
@@ -12,6 +12,7 @@ from typing import Dict, List, Optional, Any
|
|
|
12
12
|
from src.tokenizer import count_tokens
|
|
13
13
|
from src.distiller import UniversalDistiller
|
|
14
14
|
from src.secret_redactor import SecretRedactor
|
|
15
|
+
from src.license_manager import LicenseManager
|
|
15
16
|
|
|
16
17
|
class PromptCompactor:
|
|
17
18
|
"""Transforms verbose natural language requests into compact Intent IR across major world languages."""
|
|
@@ -405,44 +406,47 @@ class PromptCompactor:
|
|
|
405
406
|
"tier": "PASS_THROUGH"
|
|
406
407
|
}
|
|
407
408
|
|
|
408
|
-
# Tier 2: LLM Intent Distiller (Gemini / OpenAI / DeepSeek)
|
|
409
|
+
# Tier 2: Neural LLM Intent Distiller (Gemini / OpenAI / DeepSeek)
|
|
410
|
+
# Exclusive to TokenSafe Solo, Team, and Enterprise (or active trials)
|
|
409
411
|
min_distill = min_distill_tokens if min_distill_tokens is not None else cls._get_min_distill_tokens()
|
|
410
412
|
if enable_deepseek and original_tokens >= min_distill:
|
|
411
|
-
|
|
412
|
-
if
|
|
413
|
-
|
|
414
|
-
if
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
413
|
+
active_tier, _ = LicenseManager.get_active_license()
|
|
414
|
+
if active_tier != "free":
|
|
415
|
+
distilled = UniversalDistiller.distill(raw_text)
|
|
416
|
+
if distilled:
|
|
417
|
+
dist_tokens = count_tokens(distilled)
|
|
418
|
+
if dist_tokens < original_tokens:
|
|
419
|
+
saved = original_tokens - dist_tokens
|
|
420
|
+
pct = (saved / original_tokens * 100.0)
|
|
421
|
+
|
|
422
|
+
# Extract action from S-Expression: e.g. "(DEBUG ..."
|
|
423
|
+
act_match = re.match(r"^\(([A-Z]+)", distilled)
|
|
424
|
+
action = act_match.group(1) if act_match else "TASK"
|
|
425
|
+
|
|
426
|
+
# Extract file if present
|
|
427
|
+
files = cls.FILE_REGEX.findall(distilled)
|
|
428
|
+
target_file = files[0] if files else ""
|
|
429
|
+
|
|
430
|
+
# Extract symbol
|
|
431
|
+
symbol = cls.extract_symbol(raw_text, target_file)
|
|
432
|
+
|
|
433
|
+
# Extract traits if present in distilled S-Expression: e.g. [async redis ...]
|
|
434
|
+
trait_match = re.search(r"\[(.*?)\]", distilled)
|
|
435
|
+
traits = trait_match.group(1).split() if trait_match else []
|
|
436
|
+
|
|
437
|
+
return {
|
|
438
|
+
"original_text": raw_text,
|
|
439
|
+
"compact_text": distilled,
|
|
440
|
+
"original_tokens": original_tokens,
|
|
441
|
+
"compact_tokens": dist_tokens,
|
|
442
|
+
"saved_tokens": saved,
|
|
443
|
+
"reduction_pct": round(pct, 1),
|
|
444
|
+
"action": action,
|
|
445
|
+
"target_file": target_file,
|
|
446
|
+
"symbol": symbol,
|
|
447
|
+
"traits": traits,
|
|
448
|
+
"tier": "DEEPSEEK_V3"
|
|
449
|
+
}
|
|
446
450
|
|
|
447
451
|
# Tier 1: Multilingual Local Regex Compactor
|
|
448
452
|
files = cls.FILE_REGEX.findall(raw_text)
|
|
@@ -11,6 +11,7 @@ import urllib.request
|
|
|
11
11
|
import urllib.error
|
|
12
12
|
from typing import Optional, Dict, Any
|
|
13
13
|
from src.distiller_cache import DistillationCache
|
|
14
|
+
from src.license_manager import LicenseManager
|
|
14
15
|
|
|
15
16
|
class DeepSeekDistiller:
|
|
16
17
|
"""Uses DeepSeek-Flash as a high-density semantic compiler for prompts."""
|
|
@@ -72,7 +73,12 @@ Rules:
|
|
|
72
73
|
"""
|
|
73
74
|
Sends raw text to DeepSeek-V3 to compile into an S-Expression.
|
|
74
75
|
Returns distilled S-Expression or None if unavailable/timed out.
|
|
76
|
+
Requires active TokenSafe Solo or Team subscription.
|
|
75
77
|
"""
|
|
78
|
+
tier, _ = LicenseManager.get_active_license()
|
|
79
|
+
if tier == "free":
|
|
80
|
+
return None
|
|
81
|
+
|
|
76
82
|
conf = cls._get_config()
|
|
77
83
|
if not conf["enabled"] or not conf["api_key"] or not raw_text or len(raw_text.strip()) < 10:
|
|
78
84
|
return None
|
package/src/distiller.py
CHANGED
|
@@ -12,6 +12,7 @@ import urllib.request
|
|
|
12
12
|
import urllib.error
|
|
13
13
|
from typing import Optional, Dict, Any, Tuple
|
|
14
14
|
from src.distiller_cache import DistillationCache
|
|
15
|
+
from src.license_manager import LicenseManager
|
|
15
16
|
|
|
16
17
|
class UniversalDistiller:
|
|
17
18
|
"""Multi-provider semantic intent compiler supporting DeepSeek, OpenAI, and Google Gemini."""
|
|
@@ -65,6 +66,10 @@ Rules:
|
|
|
65
66
|
@classmethod
|
|
66
67
|
def get_configured_providers(cls) -> Dict[str, Dict[str, Any]]:
|
|
67
68
|
"""Scans environment and data/config.json for configured LLM distillation keys."""
|
|
69
|
+
tier, _ = LicenseManager.get_active_license()
|
|
70
|
+
if tier == "free":
|
|
71
|
+
return {}
|
|
72
|
+
|
|
68
73
|
config_file_data = {}
|
|
69
74
|
base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
70
75
|
config_path = os.path.join(base_dir, "data", "config.json")
|
|
@@ -104,7 +109,12 @@ Rules:
|
|
|
104
109
|
"""
|
|
105
110
|
Distills raw conversational text into an S-Expression using the best available provider.
|
|
106
111
|
Priority: Explicit preferred -> Gemini -> OpenAI -> DeepSeek.
|
|
112
|
+
Requires active TokenSafe Solo or Team subscription.
|
|
107
113
|
"""
|
|
114
|
+
tier, _ = LicenseManager.get_active_license()
|
|
115
|
+
if tier == "free":
|
|
116
|
+
return None
|
|
117
|
+
|
|
108
118
|
if not raw_text or len(raw_text.strip()) < 10:
|
|
109
119
|
return None
|
|
110
120
|
|
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.6"
|
|
18
18
|
CACHE_TTL = 86400 # 24 hours
|
|
19
19
|
|
|
20
20
|
PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|