48 lines
1.3 KiB
Python
48 lines
1.3 KiB
Python
|
|
"""Uniform stdin parsing for the memory plugin hooks (deep module).
|
||
|
|
|
||
|
|
Replaces four inline stdin parsers and the python3-vs-jq inconsistency with a
|
||
|
|
single read_input() returning a HookInput dataclass. Fail-open: malformed or
|
||
|
|
empty stdin yields empty-string fields, never an exception. Per-hook fallback
|
||
|
|
semantics (e.g. 'unknown' defaults, PID fallback) are applied by each hook, not
|
||
|
|
here, to stay faithful to the original bash behavior.
|
||
|
|
"""
|
||
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
import json
|
||
|
|
import sys
|
||
|
|
from dataclasses import dataclass
|
||
|
|
|
||
|
|
|
||
|
|
@dataclass(frozen=True)
|
||
|
|
class HookInput:
|
||
|
|
session_id: str
|
||
|
|
cwd: str
|
||
|
|
file_path: str
|
||
|
|
reason: str
|
||
|
|
source: str
|
||
|
|
raw: dict
|
||
|
|
|
||
|
|
|
||
|
|
def read_input() -> "HookInput":
|
||
|
|
data = {}
|
||
|
|
try:
|
||
|
|
raw = sys.stdin.read()
|
||
|
|
data = json.loads(raw) if raw.strip() else {}
|
||
|
|
except Exception:
|
||
|
|
data = {}
|
||
|
|
if not isinstance(data, dict):
|
||
|
|
data = {}
|
||
|
|
|
||
|
|
tool_input = data.get("tool_input") or {}
|
||
|
|
if not isinstance(tool_input, dict):
|
||
|
|
tool_input = {}
|
||
|
|
|
||
|
|
return HookInput(
|
||
|
|
session_id=str(data.get("session_id", "") or ""),
|
||
|
|
cwd=str(data.get("cwd", "") or ""),
|
||
|
|
file_path=str(tool_input.get("file_path", "") or ""),
|
||
|
|
reason=str(data.get("reason", "") or ""),
|
||
|
|
source=str(data.get("source", "") or ""),
|
||
|
|
raw=data,
|
||
|
|
)
|