import json, urllib.request
PORT="8081"; MODEL="Qwen3.8-27B-Uncensored-HauhauCS-Aggressive-Q4_K_P.gguf"
def gen(body):
    req=urllib.request.Request(f"http://localhost:{PORT}/v1/chat/completions",data=json.dumps(body).encode(),headers={"Content-Type":"application/json"})
    return json.loads(urllib.request.urlopen(req,timeout=240).read())

TOOLS=[{"type":"function","function":{"name":"terminal","parameters":{"type":"object","properties":{"command":{"type":"string","description":"The shell command to run"}},"required":["command"]}}}]
sysp="You are Hermes Agent, an autonomous coding agent. You MUST use the terminal tool to run any shell command the user asks for. Do not answer from memory; call the tool."

# 5 tool-call attempts
ok=0
for i in range(5):
    body={"model":MODEL,"messages":[{"role":"system","content":sysp},{"role":"user","content":f"List the files in /home/gxs and show the count."}],
          "tools":TOOLS,"tool_choice":"auto","max_tokens":250}
    d=gen(body); m=d["choices"][0]["message"]
    tcs=m.get("tool_calls") or []
    if tcs:
        fn=tcs[0]["function"]; args=fn.get("arguments","")
        good = fn.get("name")=="terminal" and "ls" in args
        ok+=good
        print(f"  attempt{i+1}: TOOL name={fn.get('name')} args={args!r} -> {good}")
    else:
        print(f"  attempt{i+1}: NO tool call. content={(m.get('content') or '')[:80]!r}")
print(f"tool-call success: {ok}/5")

# hard reasoning problem (needs real thought)
hard="A train leaves station A at 60 km/h. At the same time another leaves station B (240 km away) heading toward A at 90 km/h. When do they meet? Give time in minutes from departure."
body={"model":MODEL,"messages":[{"role":"user","content":hard}],"max_tokens":500}
d=gen(body); m=d["choices"][0]["message"]
out=(m.get("reasoning_content") or "")+(m.get("content") or "")
print(f"hard-probe: contains '96'={'96' in out} | content={(m.get('content') or '')[-100:]!r}")
