import sys import json import subprocess import os def main(): # The GUI calls: run --output-format stream-json # The prompt is now passed via stdin. # Debug log to stderr sys.stderr.write(f"DEBUG: mock_gemini_cli called with args: {sys.argv}\n") # Read prompt from stdin for debug prompt = sys.stdin.read() sys.stderr.write(f"DEBUG: Received prompt via stdin ({len(prompt)} chars)\n") sys.stderr.flush() if "run" not in sys.argv: return # Simulate the 'BeforeTool' hook by calling the bridge directly. bridge_path = os.path.abspath("scripts/cli_tool_bridge.py") tool_call = { "tool_name": "read_file", "tool_input": {"path": "test.txt"} } sys.stderr.write(f"DEBUG: Calling bridge at {bridge_path}\n") sys.stderr.flush() # Bridge reads from stdin process = subprocess.Popen( [sys.executable, bridge_path], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True ) stdout, stderr = process.communicate(input=json.dumps(tool_call)) sys.stderr.write(f"DEBUG: Bridge stdout: {stdout}\n") sys.stderr.write(f"DEBUG: Bridge stderr: {stderr}\n") sys.stderr.flush() try: decision_data = json.loads(stdout.strip()) decision = decision_data.get("decision") except Exception as e: sys.stderr.write(f"DEBUG: Failed to parse bridge output: {e}\n") decision = "deny" # Output JSONL to stdout if decision == "allow": print(json.dumps({ "type": "tool_use", "name": "read_file", "args": {"path": "test.txt"} }), flush=True) print(json.dumps({ "type": "message", "text": "I read the file. It contains: 'Hello from mock!'" }), flush=True) print(json.dumps({ "type": "result", "usage": {"total_tokens": 50}, "session_id": "mock-session-123" }), flush=True) else: print(json.dumps({ "type": "message", "text": f"Tool execution was denied. Decision: {decision}" }), flush=True) print(json.dumps({ "type": "result", "usage": {"total_tokens": 10}, "session_id": "mock-session-denied" }), flush=True) if __name__ == "__main__": main()