"""chat_ui.py — a tiny Flask web UI for the same local Hermes agent.

Run from the windows_stack/ folder where agent_pro.py also lives:
    .\.venv\Scripts\Activate.ps1
    pip install flask
    python chat_ui.py
    start http://localhost:5001

It serves chat.html from the same folder and exposes one POST /chat
endpoint that forwards messages to the build_agent() you wrote in
agent_pro.py. The agent inherits every tool: web search, image gen,
LINE push, anything else you've registered.
"""
from flask import Flask, request, jsonify, send_from_directory

from agent_pro import build_agent

app = Flask(__name__)
agent = build_agent()


@app.route("/")
def index():
    return send_from_directory(".", "chat.html")


@app.route("/chat", methods=["POST"])
def chat():
    message = (request.json or {}).get("message", "")
    if not message.strip():
        return jsonify({"reply": ""})
    reply = str(agent.chat(message))
    return jsonify({"reply": reply})


if __name__ == "__main__":
    # threaded=True so a classroom of students doesn't queue behind one
    # in-flight request. Keep host on 127.0.0.1 — there is no auth here,
    # and 127.0.0.1 also avoids the Windows Defender Firewall prompt.
    app.run(host="127.0.0.1", port=5001, threaded=True)
