#!/usr/bin/env python
"""Web Research Agent (#13) — Flask UI.
Golden-standard research UX (inline domain chips, visible research steps, source passages on click)
plus our wedge: 'dig deeper' on any citation, selection, or source — with YOUR own steering text —
spawns a scoped research pass appended as an expandable, recursive branch.
pip install -r requirements.txt
python app.py # http://127.0.0.1:7860
"""
import os
from flask import Flask, request, jsonify, render_template_string
import agent
import llm
app = Flask(__name__)
AREAS = ["web", "opportunity", "technical", "competitive", "client"]
PAGE = r"""
GritAI · Research
GritAI · Research
Cited web research · dig deeper into anything
#13 · 30-in-15
Sources
Backend: __BACKEND__ · local fleet by default · GritAI Solutions
"""
@app.route("/")
def index():
opts = "".join(f'' for a in AREAS)
html = PAGE.replace("__AREAS__", opts).replace("__BACKEND__", llm.backend_label())
return render_template_string(html)
@app.route("/research", methods=["POST"])
def do_research():
d = request.get_json(force=True)
q = (d.get("question") or "").strip()[:500]
if not q:
return jsonify({"report": "Ask a question.", "sources": [], "trace": []})
mode = d.get("mode", "deep") if d.get("mode") in ("quick", "deep", "swarm") else "deep"
area = d.get("area", "web")
try:
res = agent.research(q, mode=mode, area=area)
return jsonify(res)
except Exception as e:
print(f"research error: {type(e).__name__}")
return jsonify({"report": "Research failed — try again.", "sources": [], "trace": []}), 200
@app.route("/dig", methods=["POST"])
def do_dig():
d = request.get_json(force=True)
focus = (d.get("focus") or "").strip()[:600]
instruction = (d.get("instruction") or "").strip()[:400]
parent = (d.get("parent") or "").strip()[:500]
area = d.get("area", "web")
mode = d.get("mode", "deep") if d.get("mode") in ("quick", "deep", "swarm") else "deep"
try:
res = agent.dig(focus, parent, instruction=instruction, area=area, mode=mode)
return jsonify(res)
except Exception as e:
print(f"dig error: {type(e).__name__}")
return jsonify({"report": "Dig failed — try again.", "sources": [], "trace": []}), 200
if __name__ == "__main__":
port = int(os.environ.get("PORT", "7860"))
app.run(host="0.0.0.0", port=port, debug=False)