@allansantos-dev/smart-tool 0.9.21 → 0.9.23
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/CHANGELOG.md +36 -0
- package/README.md +10 -4
- package/client_hooks.py +68 -37
- package/code_graph.py +33 -6
- package/code_impact.py +1 -1
- package/dossier.py +130 -0
- package/duplicates.py +18 -1
- package/endpoint_sync.py +10 -6
- package/hook_decision.py +120 -46
- package/hooks/pretool_router.py +2 -1
- package/integration_metrics.py +5 -2
- package/package.json +1 -1
- package/redirect_rule.py +75 -9
- package/setup_ui.py +1 -1
- package/smart_tool_daemon.py +88 -15
- package/tool_redirect.py +103 -0
- package/version.py +1 -1
package/tool_redirect.py
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
"""Transparent redirect of native searches and web calls to the Smart Tool.
|
|
2
|
+
|
|
3
|
+
The PreToolUse hook starts the Smart Tool call that replaces a native one (smart_search for a folder search, web_fetch
|
|
4
|
+
or web_search for WebFetch/WebSearch) under the call's `tool_use_id`, and the PostToolUse hook puts its text in the
|
|
5
|
+
native tool's output, in the shape that tool returns: the agent reads the Smart Tool answer as the result of the call it
|
|
6
|
+
made. Measured on Claude Code 2.1.300: `updatedInput` without `permissionDecision` is applied (the user's permission
|
|
7
|
+
rules still judge the rewritten call) and `updatedToolOutput` replaces the output of Grep, Bash, WebFetch and WebSearch
|
|
8
|
+
when it keeps the tool's shape; a PostToolUseFailure cannot replace anything, so a shell search whose answer is ready in
|
|
9
|
+
the PreToolUse is rewritten to a no-op that always succeeds (a grep with no match exits 1). Codex has no output
|
|
10
|
+
replacement: its shell call is rewritten to print a file holding the answer, which Codex only applies with `allow`.
|
|
11
|
+
"""
|
|
12
|
+
import concurrent.futures
|
|
13
|
+
import os
|
|
14
|
+
import threading
|
|
15
|
+
import time
|
|
16
|
+
import uuid
|
|
17
|
+
|
|
18
|
+
import paths
|
|
19
|
+
|
|
20
|
+
RESULT_TTL_S = 600
|
|
21
|
+
RESULTS_DIR = os.path.join(paths.DATA_DIR, "redirects")
|
|
22
|
+
NO_OP = {"Bash": "true", "PowerShell": "$null"}
|
|
23
|
+
CODEX_PREFIX = "cat '"
|
|
24
|
+
_POOL = concurrent.futures.ThreadPoolExecutor(4, thread_name_prefix="redirect")
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class Jobs:
|
|
28
|
+
"""Smart Tool calls started for native tool calls, by `tool_use_id`; each is dropped once read or after
|
|
29
|
+
RESULT_TTL_S."""
|
|
30
|
+
|
|
31
|
+
def __init__(self, runners):
|
|
32
|
+
self._runners = runners
|
|
33
|
+
self._futures = {}
|
|
34
|
+
self._lock = threading.Lock()
|
|
35
|
+
|
|
36
|
+
def start(self, key, kind, arguments):
|
|
37
|
+
"""Runs `runners[kind](arguments)` in the background under key (a second start of the same key is ignored)."""
|
|
38
|
+
now = time.monotonic()
|
|
39
|
+
with self._lock:
|
|
40
|
+
for old in [k for k, (at, _f) in self._futures.items() if now - at > RESULT_TTL_S]:
|
|
41
|
+
del self._futures[old]
|
|
42
|
+
if key not in self._futures:
|
|
43
|
+
self._futures[key] = (now, _POOL.submit(self._runners[kind], arguments))
|
|
44
|
+
|
|
45
|
+
def wait(self, key, timeout):
|
|
46
|
+
"""The text of key's call when it finished within timeout seconds, else None (the call keeps running and a
|
|
47
|
+
later wait may still get it). Raises what the call raised."""
|
|
48
|
+
with self._lock:
|
|
49
|
+
entry = self._futures.get(key)
|
|
50
|
+
if entry is None:
|
|
51
|
+
return None
|
|
52
|
+
try:
|
|
53
|
+
return entry[1].result(timeout=timeout)
|
|
54
|
+
except concurrent.futures.TimeoutError:
|
|
55
|
+
return None
|
|
56
|
+
|
|
57
|
+
def forget(self, key):
|
|
58
|
+
"""Drops key's call once its text was used or will never be read."""
|
|
59
|
+
with self._lock:
|
|
60
|
+
self._futures.pop(key, None)
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def output_for(tool_name, response, text):
|
|
64
|
+
"""`updatedToolOutput` for a native tool: its own response with the Smart Tool text in the field the model reads,
|
|
65
|
+
or None when the response has an unknown shape."""
|
|
66
|
+
if not isinstance(response, dict):
|
|
67
|
+
return None
|
|
68
|
+
if tool_name == "Grep":
|
|
69
|
+
return {**response, "mode": "content", "content": text, "numLines": len(text.splitlines()), "numFiles": 0,
|
|
70
|
+
"filenames": []}
|
|
71
|
+
if tool_name in NO_OP:
|
|
72
|
+
return {**response, "stdout": text, "stderr": "", "interrupted": False}
|
|
73
|
+
if tool_name == "WebFetch":
|
|
74
|
+
return {**response, "result": text}
|
|
75
|
+
if tool_name == "WebSearch":
|
|
76
|
+
return {**response, "results": [text]}
|
|
77
|
+
return None
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def codex_command(text):
|
|
81
|
+
"""Shell command that prints text from a file: `cat` with a quoted forward-slash path runs the same in bash and
|
|
82
|
+
PowerShell (an alias of Get-Content there), the shells Codex uses."""
|
|
83
|
+
os.makedirs(RESULTS_DIR, exist_ok=True)
|
|
84
|
+
now = time.time()
|
|
85
|
+
for name in os.listdir(RESULTS_DIR):
|
|
86
|
+
path = os.path.join(RESULTS_DIR, name)
|
|
87
|
+
try:
|
|
88
|
+
if now - os.path.getmtime(path) > RESULT_TTL_S:
|
|
89
|
+
os.remove(path)
|
|
90
|
+
except OSError:
|
|
91
|
+
pass
|
|
92
|
+
path = os.path.join(RESULTS_DIR, f"{uuid.uuid4().hex}.txt")
|
|
93
|
+
with open(path, "w", encoding="utf-8", newline="\n") as stream:
|
|
94
|
+
stream.write(text)
|
|
95
|
+
return f"{CODEX_PREFIX}{path.replace(os.sep, '/')}'"
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def is_codex_command(command):
|
|
99
|
+
"""Whether a shell command is one codex_command wrote: the only input the hook approves with `allow`."""
|
|
100
|
+
if not isinstance(command, str) or not command.startswith(CODEX_PREFIX) or not command.endswith("'"):
|
|
101
|
+
return False
|
|
102
|
+
path = os.path.normpath(command[len(CODEX_PREFIX):-1])
|
|
103
|
+
return os.path.dirname(os.path.normcase(path)) == os.path.normcase(os.path.normpath(RESULTS_DIR)) and "'" not in path
|
package/version.py
CHANGED