dn-scribe commited on
Commit
4a306aa
·
unverified ·
1 Parent(s): 9f63c08

fix: stream

Browse files
Files changed (2) hide show
  1. app/main.py +10 -2
  2. app/templates/index.html +43 -21
app/main.py CHANGED
@@ -84,19 +84,27 @@ async def stream(cve_id: str, refresh: bool = Query(False)):
84
 
85
  async def yield_live():
86
  full_text = ""
 
87
  try:
88
  async for chunk in generate_openai_stream(cve_id):
 
89
  full_text += chunk
 
 
 
 
90
  await write_report(cve_id, full_text)
91
  yield f"event: end\ndata: {full_text}\n\n"
 
 
 
92
  finally:
93
- os.remove(lock_path)
94
 
95
  return StreamingResponse(yield_live(), media_type="text/event-stream")
96
 
97
 
98
 
99
-
100
  @app.get("/json")
101
  async def json_analysis(cve_id: str, refresh: bool = Query(False)):
102
  logger.info(f"Received JSON analysis request for cve_id={cve_id}, refresh={refresh}")
 
84
 
85
  async def yield_live():
86
  full_text = ""
87
+ logger.info(f"Starting OpenAI stream for {cve_id}")
88
  try:
89
  async for chunk in generate_openai_stream(cve_id):
90
+ logger.debug(f"Got chunk: {repr(chunk)}")
91
  full_text += chunk
92
+ for line in chunk.splitlines():
93
+ yield f"data: {line}\n"
94
+ yield "\n"
95
+ logger.info(f"Completed OpenAI stream for {cve_id}, preview: {repr(full_text[:200])}")
96
  await write_report(cve_id, full_text)
97
  yield f"event: end\ndata: {full_text}\n\n"
98
+ except Exception as e:
99
+ logger.exception(f"Error during OpenAI stream: {e}")
100
+ yield f"data: ❌ Error generating report: {str(e)}\n\n"
101
  finally:
102
+ os.remove(_lock_path(cve_id))
103
 
104
  return StreamingResponse(yield_live(), media_type="text/event-stream")
105
 
106
 
107
 
 
108
  @app.get("/json")
109
  async def json_analysis(cve_id: str, refresh: bool = Query(False)):
110
  logger.info(f"Received JSON analysis request for cve_id={cve_id}, refresh={refresh}")
app/templates/index.html CHANGED
@@ -46,34 +46,56 @@
46
  const cveId = params.get("cve_id");
47
  const outputEl = document.getElementById("output");
48
  const spinnerEl = document.getElementById("spinner");
49
-
50
  if (!cveId) {
51
  spinnerEl.innerText = "❌ Missing CVE ID.";
52
  return;
53
  }
54
-
55
- try {
56
- const response = await fetch(`/raw?cve_id=${cveId}`);
57
- if (!response.ok) {
58
- spinnerEl.innerText = "❌ Report not found.";
59
- return;
 
 
 
 
 
60
  }
61
-
62
- const markdown = await response.text();
63
-
64
- // DEBUG: show what's fetched
65
- console.log("Fetched markdown:", markdown.slice(0, 100));
66
-
67
- // Parse and render markdown
68
- outputEl.innerHTML = marked.parse(markdown);
69
- spinnerEl.style.display = "none";
70
- outputEl.style.display = "block";
71
-
72
- } catch (err) {
73
- console.error(err);
74
- spinnerEl.innerText = "❌ Failed to load report.";
75
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76
  });
77
  </script>
 
 
78
  </body>
79
  </html>
 
46
  const cveId = params.get("cve_id");
47
  const outputEl = document.getElementById("output");
48
  const spinnerEl = document.getElementById("spinner");
49
+
50
  if (!cveId) {
51
  spinnerEl.innerText = "❌ Missing CVE ID.";
52
  return;
53
  }
54
+
55
+ async function fetchRawAndRender() {
56
+ try {
57
+ const res = await fetch(`/raw?cve_id=${cveId}`);
58
+ if (!res.ok) throw new Error("Report missing");
59
+ const text = await res.text();
60
+ spinnerEl.style.display = "none";
61
+ outputEl.innerHTML = marked.parse(text);
62
+ outputEl.style.display = "block";
63
+ } catch {
64
+ startStreaming();
65
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
  }
67
+
68
+ function startStreaming() {
69
+ spinnerEl.innerText = "⏳ Generating report...";
70
+ const evtSource = new EventSource(`/stream?cve_id=${cveId}&refresh=true`);
71
+ let buffer = "";
72
+
73
+ evtSource.onmessage = (e) => {
74
+ buffer += e.data;
75
+ outputEl.innerHTML = marked.parse(buffer);
76
+ outputEl.style.display = "block";
77
+ outputEl.scrollTop = outputEl.scrollHeight;
78
+ };
79
+
80
+ evtSource.addEventListener("end", (e) => {
81
+ evtSource.close();
82
+ buffer += e.data;
83
+ spinnerEl.style.display = "none";
84
+ outputEl.innerHTML = marked.parse(buffer);
85
+ outputEl.scrollTop = outputEl.scrollHeight;
86
+ });
87
+
88
+ evtSource.onerror = (err) => {
89
+ console.error("Streaming error", err);
90
+ evtSource.close();
91
+ spinnerEl.innerText = "❌ Failed to generate report.";
92
+ };
93
+ }
94
+
95
+ fetchRawAndRender();
96
  });
97
  </script>
98
+
99
+
100
  </body>
101
  </html>