import json
import os
import pathlib
import subprocess
import sys

import requests

BASE_URL = os.environ.get("HARNESS_BASE_URL", "http://127.0.0.1:1234/v1")
MODEL = os.environ.get("HARNESS_MODEL", "google/gemma-4-12b-qat")
API_KEY = os.environ.get("LMSTUDIO_API_KEY", "")


def read_file(path):
    return pathlib.Path(path).read_text()


def run_command(cmd):
    print(f"[tool] wants to run: {cmd}")
    if input("allow? [y/N] ").strip().lower() != "y":
        return "command denied by user"
    finished = subprocess.run(cmd, shell=True, capture_output=True, text=True, timeout=60)  # nosemgrep
    return finished.stdout + finished.stderr


TOOL_FUNCTIONS = {"read_file": read_file, "run_command": run_command}

TOOL_SCHEMAS = [
    {
        "type": "function",
        "function": {
            "name": "read_file",
            "description": "Read a text file and return its contents.",
            "parameters": {
                "type": "object",
                "properties": {"path": {"type": "string"}},
                "required": ["path"],
            },
        },
    },
    {
        "type": "function",
        "function": {
            "name": "run_command",
            "description": "Run a shell command and return its output.",
            "parameters": {
                "type": "object",
                "properties": {"cmd": {"type": "string"}},
                "required": ["cmd"],
            },
        },
    },
]

TASK = sys.argv[1] if len(sys.argv) > 1 else (
    "Read data/measurements.txt and compute the mean period "
    "using run_command with python3 -c"
)

messages = [
    {"role": "system", "content": "You are an agent. Use tools to complete the task, then say DONE."},
    {"role": "user", "content": TASK},
]

# --- written live on stage ---
