# -*- coding: utf-8 -*- """ 聊天记录 Web UI 运行: python scripts/chat_ui.py 访问: http://localhost:5678 """ import sys from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parent.parent)) from flask import Flask, jsonify, render_template_string, request import asyncio from core.pydantic_ai_agent import CustomerServiceAgent, AgentDeps from db import chat_log_db as db app = Flask(__name__) pricing_agent = None try: pricing_agent = CustomerServiceAgent() except Exception as e: print(f"[ChatUI] 初始化报价Agent失败: {e}") HTML = r""" 聊天记录

💬 聊天记录

● 实时
💬

选择一位客户查看对话记录

""" PRICING_HTML = r""" AI 报价测试
🧪 AI 报价测试
提示:含图片URL时,Agent会自动调用图片分析并结合复杂度、尺寸、人脸与风险给出建议价;文本砍价低于最近图片底线会被礼貌拒绝。
""" @app.route("/") def index(): return render_template_string(HTML) @app.route("/pricing") def pricing_index(): return render_template_string(PRICING_HTML) @app.route("/api/customers") def api_customers(): return jsonify(db.get_customers(limit=200)) @app.route("/api/conversation/") def api_conversation(customer_id): return jsonify(db.get_conversation(customer_id, limit=500)) @app.route("/api/search") def api_search(): kw = request.args.get("q", "").strip() if not kw: return jsonify([]) return jsonify(db.search_messages(kw, limit=60)) if __name__ == "__main__": print("聊天记录 UI 启动中...") print("访问 → http://localhost:5678") app.run(host="0.0.0.0", port=5678, debug=False) @app.route("/api/pricing/run", methods=["POST"]) def api_pricing_run(): global pricing_agent if pricing_agent is None: return jsonify({"error":"报价Agent未初始化"}) data = request.get_json(force=True) or {} from_id = (data.get("from_id") or "").strip() acc_id = (data.get("acc_id") or "").strip() msg = (data.get("msg") or "").strip() if not from_id or not msg: return jsonify({"error":"缺少参数 from_id 或 msg"}) # 构造提示词:直接使用用户输入,保持与正式场景一致 user_prompt = msg deps = AgentDeps( msg_id="pricing-test", acc_id=acc_id or "TEST_SHOP", from_id=from_id, platform="taobao" ) try: # 强制使用报价Agent result = asyncio.run(pricing_agent.agent_pricing.run(user_prompt, deps=deps, message_history=[])) # 读取底线 try: from config.config import MIN_PRICE_FLOOR st = pricing_agent._get_conversation_state(from_id) floor = st.last_min_price if isinstance(st.last_min_price,int) and st.last_min_price>0 else MIN_PRICE_FLOOR except Exception: floor = None return jsonify({ "reply": result.output, "should_reply": True, "agent": "pricing", "floor": floor }) except Exception as e: return jsonify({"error": str(e)})