"""AutoAdmin.dk local agent lab. Install: python -m pip install ollama.
Only a fictional read-only tool is allowed. No shell or filesystem tools.
"""
import json
import sys
from ollama import Client

client = Client(host="http://127.0.0.1:11434", timeout=120)
MODEL = "qwen3:4b"

def demo_service_status(service: str) -> str:
    """Return fictional demo status for portal, mail, or vpn. Never a live status."""
    if not isinstance(service, str) or service.lower() not in {"portal", "mail", "vpn"}:
        return "Unsupported service. Allowed: portal, mail, vpn."
    return json.dumps({"service": service.lower(), "status": "operational", "data": "fictional demo"})

messages = [
    {"role": "system", "content": "You are a local lab assistant. For service status, use demo_service_status. The data is fictional. You cannot perform any other action. Never claim to execute an action without a tool result."},
    {"role": "user", "content": " ".join(sys.argv[1:]) or "What is the status of the demo portal?"},
]

def main():
    for _ in range(3):
        response = client.chat(model=MODEL, messages=messages, tools=[demo_service_status])
        messages.append(response.message)
        if not response.message.tool_calls:
            print(response.message.content)
            return
        # Bound both rounds and calls; the model never chooses an executable function.
        for call in response.message.tool_calls[:3]:
            args = call.function.arguments
            if call.function.name != "demo_service_status" or not isinstance(args, dict) or set(args) != {"service"}:
                result = "Tool request refused: unknown function or invalid arguments."
            else:
                result = demo_service_status(args["service"])
                print("TOOL:", result)
            messages.append({"role": "tool", "tool_name": call.function.name, "content": result})
    print("Stopped: maximum tool rounds reached. No further action is taken.")

if __name__ == "__main__":
    try:
        main()
    except Exception as exc:
        print("Local agent failed:", exc, file=sys.stderr)
        sys.exit(1)
