checkpoint: Working on getting gemini cli to actually have parity with gemini api.
This commit is contained in:
@@ -4,93 +4,99 @@ import subprocess
|
||||
import os
|
||||
|
||||
def main():
|
||||
# The GUI calls: <binary> 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()
|
||||
# Read prompt from stdin
|
||||
try:
|
||||
prompt = sys.stdin.read()
|
||||
except EOFError:
|
||||
prompt = ""
|
||||
|
||||
sys.stderr.write(f"DEBUG: Received prompt via stdin ({len(prompt)} chars)\n")
|
||||
sys.stderr.flush()
|
||||
|
||||
if "run" not in sys.argv:
|
||||
# Skip management commands
|
||||
if len(sys.argv) > 1 and sys.argv[1] in ["mcp", "extensions", "skills", "hooks"]:
|
||||
return
|
||||
|
||||
# If the prompt contains tool results (indicated by "role": "tool"),
|
||||
# it means we are in the second round and should provide a final answer.
|
||||
if '"role": "tool"' in prompt:
|
||||
# If the prompt contains tool results, provide final answer
|
||||
if '"role": "tool"' in prompt or '"tool_call_id"' in prompt:
|
||||
print(json.dumps({
|
||||
"type": "message",
|
||||
"text": "I have processed the tool results. Everything looks good!"
|
||||
"role": "assistant",
|
||||
"content": "I have processed the tool results. Everything looks good!"
|
||||
}), flush=True)
|
||||
print(json.dumps({
|
||||
"type": "result",
|
||||
"usage": {"total_tokens": 100},
|
||||
"status": "success",
|
||||
"stats": {"total_tokens": 100, "input_tokens": 80, "output_tokens": 20},
|
||||
"session_id": "mock-session-final"
|
||||
}), flush=True)
|
||||
return
|
||||
|
||||
# Simulate the 'BeforeTool' hook by calling the bridge directly.
|
||||
# Default flow: simulate a tool call
|
||||
bridge_path = os.path.abspath("scripts/cli_tool_bridge.py")
|
||||
|
||||
tool_call = {
|
||||
"tool_name": "read_file",
|
||||
"tool_input": {"path": "test.txt"}
|
||||
# Using format that bridge understands
|
||||
bridge_tool_call = {
|
||||
"name": "read_file",
|
||||
"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,
|
||||
env=os.environ # Ensure environment variables are inherited
|
||||
)
|
||||
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:
|
||||
process = subprocess.Popen(
|
||||
[sys.executable, bridge_path],
|
||||
stdin=subprocess.PIPE,
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.PIPE,
|
||||
text=True,
|
||||
env=os.environ
|
||||
)
|
||||
stdout, stderr = process.communicate(input=json.dumps(bridge_tool_call))
|
||||
|
||||
sys.stderr.write(f"DEBUG: Bridge stdout: {stdout}\n")
|
||||
sys.stderr.write(f"DEBUG: Bridge stderr: {stderr}\n")
|
||||
|
||||
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")
|
||||
sys.stderr.write(f"DEBUG: Bridge failed: {e}\n")
|
||||
decision = "deny"
|
||||
|
||||
# Output JSONL to stdout
|
||||
if decision == "allow":
|
||||
# Simulate REAL CLI field names for adapter normalization test
|
||||
print(json.dumps({
|
||||
"type": "tool_use",
|
||||
"name": "read_file",
|
||||
"args": {"path": "test.txt"}
|
||||
"tool_name": "read_file",
|
||||
"tool_id": "call_123",
|
||||
"parameters": {"path": "test.txt"}
|
||||
}), flush=True)
|
||||
|
||||
print(json.dumps({
|
||||
"type": "message",
|
||||
"text": "I read the file. It contains: 'Hello from mock!'"
|
||||
"role": "assistant",
|
||||
"content": "I am reading the file now..."
|
||||
}), flush=True)
|
||||
|
||||
print(json.dumps({
|
||||
"type": "result",
|
||||
"usage": {"total_tokens": 50},
|
||||
"status": "success",
|
||||
"stats": {"total_tokens": 50, "input_tokens": 40, "output_tokens": 10},
|
||||
"session_id": "mock-session-123"
|
||||
}), flush=True)
|
||||
else:
|
||||
print(json.dumps({
|
||||
"type": "message",
|
||||
"text": f"Tool execution was denied. Decision: {decision}"
|
||||
"role": "assistant",
|
||||
"content": f"Tool execution was denied. Decision: {decision}"
|
||||
}), flush=True)
|
||||
print(json.dumps({
|
||||
"type": "result",
|
||||
"usage": {"total_tokens": 10},
|
||||
"status": "success",
|
||||
"stats": {"total_tokens": 10, "input_tokens": 10, "output_tokens": 0},
|
||||
"session_id": "mock-session-denied"
|
||||
}), flush=True)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user