"""A bounded, local-only tool-call check for an OpenAI-compatible model server.

Usage: python private-file-agent.py MODEL_ID [BASE_URL]
The only tool reads the notes.txt file in this script's directory. It cannot
read arbitrary paths or run commands. Keep the model server behind a private
network or SSH tunnel; never publish an unauthenticated inference endpoint.
"""
import json
import sys
from pathlib import Path
from openai import OpenAI

model = sys.argv[1]
base_url = sys.argv[2] if len(sys.argv) > 2 else "http://127.0.0.1:8000/v1"
client = OpenAI(base_url=base_url, api_key="local-only")
notes = Path(__file__).with_name("notes.txt")
if not notes.exists():
    notes.write_text("Project Aurora launches Friday. Risk: missing backup.\n", encoding="utf-8")
tools = [{"type": "function", "function": {"name": "read_notes", "description": "Read the local disposable notes.txt file.", "parameters": {"type": "object", "properties": {}, "additionalProperties": False}}}]
messages = [{"role": "user", "content": "Read my notes with the tool, then write a three-line summary with the launch day and risk."}]
first = client.chat.completions.create(model=model, messages=messages, tools=tools, tool_choice="auto", max_tokens=512)
assistant = first.choices[0].message
if not assistant.tool_calls:
    raise RuntimeError("No tool call returned. Check the model's tool parser and the serving flags before using it as an agent.")
messages.append(assistant)
for call in assistant.tool_calls:
    if call.function.name != "read_notes" or json.loads(call.function.arguments or "{}") != {}:
        raise RuntimeError("Unexpected tool request; no action was taken.")
    messages.append({"role": "tool", "tool_call_id": call.id, "content": notes.read_text(encoding="utf-8")})
final = client.chat.completions.create(model=model, messages=messages, max_tokens=512).choices[0].message.content
if not final or "Friday" not in final or "backup" not in final.lower():
    raise RuntimeError(f"The final answer missed the expected facts: {final!r}")
Path(__file__).with_name("agent-result.txt").write_text(final, encoding="utf-8")
print("Verified tool call and final answer. Saved agent-result.txt")
