From 23fb8fc3bd41ca794bf0a25f9e10807c21f1d521 Mon Sep 17 00:00:00 2001 From: Renato Rosa Date: Wed, 19 Aug 2026 20:50:04 -0300 Subject: [PATCH] Atualizar app/app.py --- app/app.py | 143 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 141 insertions(+), 2 deletions(-) diff --git a/app/app.py b/app/app.py index 30f6124..44036a3 100644 --- a/app/app.py +++ b/app/app.py @@ -64,11 +64,97 @@ def new_conversation(): @login_required def list_conversations(): db = SessionLocal() - convs = db.query(Conversation).all() - data = [{"id": c.id, "title": c.title} for c in convs] + user_email = session["user"]["email"] + convs = db.query(Conversation).filter( + (Conversation.user_email == user_email) | + (Conversation.shared == user_email) + ).all() + data = [{"id": c.id, "title": c.title, "tags": c.tags} for c in convs] db.close() return jsonify(data) +@app.route("/api/export/md/") +@login_required +def export_md(cid): + db = SessionLocal() + conv = db.query(Conversation).filter_by(id=cid).first() + msgs = conv.messages + + md = f"# {conv.title}\n\n" + if conv.tags: + md += f"**Tags:** {conv.tags}\n\n" + + for m in msgs: + md += f"### {m.role.capitalize()}\n\n{m.content}\n\n" + + db.close() + return Response(md, mimetype="text/markdown") + +from weasyprint import HTML + +import uuid +import pypandoc +from pdfminer.high_level import extract_text +from docx import Document + +@app.route("/api/import/", methods=["POST"]) +@login_required +def import_file(cid): + file = request.files.get("file") + if not file: + return jsonify({"error": "No file"}), 400 + + ext = file.filename.lower().split(".")[-1] + temp_path = f"/data/{uuid.uuid4()}.{ext}" + file.save(temp_path) + + if ext == "pdf": + text = extract_text(temp_path) + elif ext == "docx": + doc = Document(temp_path) + text = "\n".join([p.text for p in doc.paragraphs]) + elif ext == "doc": + text = pypandoc.convert_file(temp_path, "md") + else: + return jsonify({"error": "Unsupported format"}), 400 + + md = pypandoc.convert_text(text, "md", format="plain") + + db = SessionLocal() + msg = Message( + id=str(uuid.uuid4()), + conversation_id=cid, + role="user", + content=md + ) + db.add(msg) + db.commit() + db.close() + + return jsonify({"status": "ok", "imported_as_markdown": True}) + + +@app.route("/api/export/pdf/") +@login_required +def export_pdf(cid): + db = SessionLocal() + conv = db.query(Conversation).filter_by(id=cid).first() + msgs = conv.messages + + html = "

{}

".format(conv.title) + if conv.tags: + html += "

Tags: {}

".format(conv.tags) + + for m in msgs: + html += "

{}

{}

".format(m.role.capitalize(), m.content) + + db.close() + + pdf = HTML(string=html).write_pdf() + return Response(pdf, mimetype="application/pdf") + + + @app.route("/api/history/") @login_required def history(cid): @@ -156,6 +242,59 @@ def stream(): return Response(generate(), mimetype="text/event-stream") +@app.route("/api/share/", methods=["POST"]) +@login_required +def share(cid): + target_email = request.json.get("email") + db = SessionLocal() + conv = db.query(Conversation).filter_by(id=cid).first() + conv.shared = target_email + db.commit() + db.close() + return jsonify({"status": "ok"}) + + + +@app.route("/api/tags/", methods=["POST"]) +@login_required +def update_tags(cid): + tags = request.json.get("tags", []) + db = SessionLocal() + conv = db.query(Conversation).filter_by(id=cid).first() + conv.tags = ",".join(tags) + db.commit() + db.close() + return jsonify({"status": "ok"}) + + +@app.route("/api/search") +@login_required +def search(): + q = request.args.get("q", "").lower() + user_email = session["user"]["email"] + + db = SessionLocal() + convs = db.query(Conversation).filter_by(user_email=user_email).all() + + results = [] + for c in convs: + for m in c.messages: + if q in m.content.lower(): + results.append({ + "conversation_id": c.id, + "title": c.title, + "role": m.role, + "content": m.content, + "created_at": m.created_at.isoformat() + }) + + db.close() + return jsonify(results) + + + + + # *** import json