app.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. const API_BASE = "http://127.0.0.1:8000";
  2. // 显示多Agent执行状态
  3. function showAgentProgress(agentContainer, agents, statusFunc) {
  4. agentContainer.innerHTML = "";
  5. agents.forEach(agent => {
  6. const li = document.createElement("li");
  7. const status = typeof statusFunc === "function" ? statusFunc(agent.key) : statusFunc;
  8. li.textContent = `${agent.label}: ${status}`;
  9. agentContainer.appendChild(li);
  10. });
  11. }
  12. // 公共函数:提交任务并轮询状态
  13. async function submitAndPollTask(url, body, agents, resultCard, reportDiv, analysisDiv, progressList, loadingText, doneText, errorText) {
  14. reportDiv.innerHTML = "";
  15. analysisDiv.innerText = loadingText;
  16. progressList.classList.remove("hidden");
  17. showAgentProgress(progressList, agents, () => "⏳ 执行中...");
  18. resultCard.classList.add("hidden");
  19. try {
  20. const response = await fetch(url, body);
  21. if (!response.ok) throw new Error(`服务器返回错误状态:${response.status}`);
  22. const data = await response.json();
  23. const taskId = data.task_id;
  24. let taskStatus = await fetch(`${API_BASE}/api/health/task_status/${taskId}`).then(r => r.json());
  25. while (taskStatus.state !== "completed") {
  26. showAgentProgress(progressList, agents, agentKey => taskStatus.agents?.[agentKey] ?? "⏳ 执行中...");
  27. await new Promise(res => setTimeout(res, 1000));
  28. taskStatus = await fetch(`${API_BASE}/api/health/task_status/${taskId}`).then(r => r.json());
  29. }
  30. // 任务完成后刷新一次 agent 状态,保证 ReportAgent 也显示 completed
  31. showAgentProgress(progressList, agents, agentKey => taskStatus.agents?.[agentKey] ?? "⏳ 执行中...");
  32. // 显示最终报告
  33. const summary = taskStatus.report?.report?.summary || "<p>❌ 未返回报告内容</p>";
  34. reportDiv.innerHTML = typeof summary === "string" ? summary : JSON.stringify(summary, null, 2);
  35. analysisDiv.innerText = doneText;
  36. resultCard.classList.remove("hidden");
  37. } catch (error) {
  38. const errorMessage = error?.message || JSON.stringify(error);
  39. console.error("任务提交或轮询出错:", errorMessage);
  40. reportDiv.innerHTML = `<p>❌ ${errorText}: ${errorMessage}</p>`;
  41. analysisDiv.innerText = `❌ ${errorText}`;
  42. progressList.innerHTML = "";
  43. }
  44. }
  45. // 文本报告分析
  46. async function analyze() {
  47. const reportText = document.getElementById("reportText").value;
  48. if (!reportText) {
  49. alert("请输入体检报告内容");
  50. return;
  51. }
  52. const resultCard = document.getElementById("resultCard");
  53. const reportDiv = document.getElementById("report");
  54. const analysisDiv = document.getElementById("analysis");
  55. const progressList = document.getElementById("progressList");
  56. const agents = [
  57. { key: "PlannerAgent", label: "PlannerAgent 规划分析" },
  58. { key: "HealthIndicatorAgent", label: "HealthIndicatorAgent 指标分析" },
  59. { key: "RiskAssessmentAgent", label: "RiskAssessmentAgent 风险评估" },
  60. { key: "AdviceAgent", label: "AdviceAgent 建议生成" },
  61. { key: "ReportAgent", label: "ReportAgent 报告生成" }
  62. ];
  63. await submitAndPollTask(
  64. `${API_BASE}/api/health/analysis`,
  65. {
  66. method: "POST",
  67. headers: { "Content-Type": "application/json" },
  68. body: JSON.stringify({ report_text: reportText })
  69. },
  70. agents,
  71. resultCard,
  72. reportDiv,
  73. analysisDiv,
  74. progressList,
  75. "⏳ 正在分析文本报告,请稍候...",
  76. "✅ 文本分析完成",
  77. "报告生成失败"
  78. );
  79. }
  80. // PDF报告分析
  81. async function uploadPDF() {
  82. const fileInput = document.getElementById("pdfFile");
  83. const file = fileInput.files[0];
  84. if (!file) {
  85. alert("请选择PDF文件");
  86. return;
  87. }
  88. const formData = new FormData();
  89. formData.append("file", file);
  90. const resultCard = document.getElementById("resultCard");
  91. const reportDiv = document.getElementById("report");
  92. const analysisDiv = document.getElementById("analysis");
  93. const progressList = document.getElementById("progressList");
  94. const agents = [
  95. { key: "PlannerAgent", label: "PlannerAgent 规划分析" },
  96. { key: "HealthIndicatorAgent", label: "HealthIndicatorAgent 指标分析" },
  97. { key: "RiskAssessmentAgent", label: "RiskAssessmentAgent 风险评估" },
  98. { key: "AdviceAgent", label: "AdviceAgent 建议生成" },
  99. { key: "ReportAgent", label: "ReportAgent 报告生成" }
  100. ];
  101. await submitAndPollTask(
  102. `${API_BASE}/api/health/analysis/pdf`,
  103. { method: "POST", body: formData },
  104. agents,
  105. resultCard,
  106. reportDiv,
  107. analysisDiv,
  108. progressList,
  109. "⏳ 正在分析 PDF 报告,请稍候...",
  110. "✅ PDF分析完成",
  111. "上传失败"
  112. );
  113. }
  114. // 绑定按钮事件
  115. document.getElementById("analyzeBtn")?.addEventListener("click", analyze);
  116. document.getElementById("uploadBtn")?.addEventListener("click", uploadPDF);