utils/omniroute_offload/omniroute_offload/_hook.py
Krzysztof kuhy Rudnicki c2165e9fb8
Some checks are pending
dependency freshness / dependencies on newest stable (push) Waiting to run
file length / file-length (push) Waiting to run
md-naming / markdown naming convention (push) Waiting to run
omniroute_offload pre-commit / pre-commit (push) Waiting to run
palette drift / palette (push) Waiting to run
repo-contract / repo contract (push) Waiting to run
shared gate tests / gate unit tests (push) Waiting to run
omniroute_offload: free-llm, claude-free
free-llm sends big texts to the free-best OmniRoute chain behind a
fail-closed privacy gate (public repos, /tmp, /var/log; credential
scan). claude-free runs Claude Code with the full ~/.claude setup on
free-best. Skill offload-free in ~/.claude says when to use them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WzxdWYwGi2FJwfZ6QRB1im
2026-11-05 13:14:16 +01:00

68 lines
2.0 KiB
Python

"""PreToolUse hook on Read: nudge to free-llm before reading a big shareable file.
Never blocks (editing a big file still needs a Read); only adds context when a
whole-file Read of a file over MIN_BYTES would pass the free-llm privacy gate.
"""
from __future__ import annotations
import json
import sys
from pathlib import Path
from omniroute_offload import _privacy as privacy
MIN_BYTES = 60_000
NUDGE = (
"{path} is {kb} KB (~{tokens}k tokens). If you only need to understand or "
'summarize it (not edit it), run `free-llm -f {path} "<narrow question>"` '
"(skill offload-free) and read only its answer; if a pattern suffices, grep it. "
"Read it whole only if you must edit it or need exact text."
)
def nudge(payload: object) -> str | None:
"""Context to add for this tool call, or None to stay silent."""
if not isinstance(payload, dict) or payload.get("tool_name") != "Read":
return None
tool_input = payload.get("tool_input")
if (
not isinstance(tool_input, dict)
or "limit" in tool_input
or "offset" in tool_input
):
return None
path = Path(str(tool_input.get("file_path", "")))
try:
size = path.stat().st_size
except OSError:
return None
if size <= MIN_BYTES or privacy.file_refusal(path, home=Path.home()):
return None
return NUDGE.format(path=path, kb=size // 1000, tokens=size // 4000)
def main() -> int:
"""Read the hook JSON from stdin, print optional context JSON, always exit 0."""
try:
payload = json.loads(sys.stdin.read())
except ValueError:
return 0
context = nudge(payload)
if context:
print(
json.dumps(
{
"hookSpecificOutput": {
"hookEventName": "PreToolUse",
"additionalContext": context,
}
}
)
)
return 0
if __name__ == "__main__":
sys.exit(main())