Stop plan loops after child success; harden stream UX.
Prefer DONE + runtime solidify when the child already returned a value, compact parent observations, drop the SSE 8MiB raw-buffer abort, and drip-stream the view panel via POST /api/run. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
2a9fceb51c
commit
7e3b193d49
7 files changed
+868
-172
No files matched your search
+56
-7
@@ -348,18 +348,22 @@ fn read_sse_body_to_events<R: BufRead>(
|
||||
echo: bool,
|
||||
) -> Result<Vec<Value>, String> {
|
||||
let mut buf = String::new();
|
||||
let mut raw = String::new();
|
||||
let mut events = Vec::new();
|
||||
let mut result = String::new();
|
||||
let mut data_buf: Vec<String> = Vec::new();
|
||||
let mut echoed = false;
|
||||
let mut saw_reasoning = false;
|
||||
// Soft cap on *stored* event payload (bus still gets every frame). DeepSeek
|
||||
// thinking can exceed tens of MiB; we must not retain the raw wire body.
|
||||
let mut stored_bytes: usize = 0;
|
||||
const STORE_CAP: usize = 2 * 1024 * 1024;
|
||||
|
||||
let flush_data = |data_buf: &mut Vec<String>,
|
||||
events: &mut Vec<Value>,
|
||||
result: &mut String,
|
||||
echoed: &mut bool,
|
||||
saw_reasoning: &mut bool,
|
||||
stored_bytes: &mut usize,
|
||||
echo: bool|
|
||||
-> Result<bool, String> {
|
||||
if data_buf.is_empty() {
|
||||
@@ -376,14 +380,38 @@ fn read_sse_body_to_events<R: BufRead>(
|
||||
}
|
||||
let v: serde_json::Value =
|
||||
serde_json::from_str(data).map_err(|e| format!("openai sse json: {e}"))?;
|
||||
Ok(push_openai_delta_events(
|
||||
// Always publish to the view bus; optionally skip retaining huge histories.
|
||||
let before = events.len();
|
||||
let stop = push_openai_delta_events(
|
||||
&v,
|
||||
events,
|
||||
result,
|
||||
echoed,
|
||||
saw_reasoning,
|
||||
echo,
|
||||
))
|
||||
);
|
||||
// Cap retained history only; EventBus already has every frame for the view.
|
||||
let mut i = before;
|
||||
while i < events.len() {
|
||||
let add = match &events[i] {
|
||||
Value::Map(m) => m
|
||||
.iter()
|
||||
.filter(|(k, _)| *k == "text" || *k == "result" || *k == "message")
|
||||
.filter_map(|(_, val)| match val {
|
||||
Value::Text(t) => Some(t.len()),
|
||||
_ => None,
|
||||
})
|
||||
.sum::<usize>(),
|
||||
_ => 0,
|
||||
};
|
||||
if stored_bytes.saturating_add(add) > STORE_CAP {
|
||||
events.truncate(i);
|
||||
break;
|
||||
}
|
||||
*stored_bytes = stored_bytes.saturating_add(add);
|
||||
i += 1;
|
||||
}
|
||||
Ok(stop)
|
||||
};
|
||||
|
||||
loop {
|
||||
@@ -394,10 +422,6 @@ fn read_sse_body_to_events<R: BufRead>(
|
||||
if n == 0 {
|
||||
break;
|
||||
}
|
||||
raw.push_str(&buf);
|
||||
if raw.len() > 8 * 1024 * 1024 {
|
||||
return Err("http sse response exceeds 8MiB limit".into());
|
||||
}
|
||||
let line = buf.strip_suffix('\n').unwrap_or(&buf);
|
||||
let line = line.strip_suffix('\r').unwrap_or(line);
|
||||
if line.is_empty() {
|
||||
@@ -407,6 +431,7 @@ fn read_sse_body_to_events<R: BufRead>(
|
||||
&mut result,
|
||||
&mut echoed,
|
||||
&mut saw_reasoning,
|
||||
&mut stored_bytes,
|
||||
echo,
|
||||
)? {
|
||||
break;
|
||||
@@ -424,6 +449,7 @@ fn read_sse_body_to_events<R: BufRead>(
|
||||
&mut result,
|
||||
&mut echoed,
|
||||
&mut saw_reasoning,
|
||||
&mut stored_bytes,
|
||||
echo,
|
||||
)?;
|
||||
if echo && echoed {
|
||||
@@ -570,4 +596,27 @@ data: [DONE]\n\
|
||||
_ => panic!("expected done map"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sse_body_over_8mib_does_not_error() {
|
||||
// Formerly failed with "http sse response exceeds 8MiB limit" because raw
|
||||
// wire bytes were accumulated. Streaming must keep going; store is soft-capped.
|
||||
let frame =
|
||||
"data: {\"choices\":[{\"delta\":{\"reasoning_content\":\"0123456789\"}}]}\n\n";
|
||||
let mut body = String::with_capacity(9 * 1024 * 1024);
|
||||
while body.len() < 8 * 1024 * 1024 + 1024 {
|
||||
body.push_str(frame);
|
||||
}
|
||||
body.push_str("data: {\"choices\":[{\"delta\":{\"content\":\"ok\"}}]}\n\n");
|
||||
body.push_str("data: [DONE]\n\n");
|
||||
let ev = read_sse_body_to_events(std::io::Cursor::new(body), false).unwrap();
|
||||
assert!(
|
||||
ev.iter().any(|e| matches!(
|
||||
e,
|
||||
Value::Map(m) if m.iter().any(|(k, v)| k == "type" && matches!(v, Value::Text(t) if t == "done"))
|
||||
&& m.iter().any(|(k, v)| k == "result" && matches!(v, Value::Text(t) if t == "ok"))
|
||||
)),
|
||||
"expected done with result ok"
|
||||
);
|
||||
}
|
||||
}
|
||||
+525
-97
@@ -522,55 +522,168 @@ section.block > h2 {
|
||||
margin: 0 0 0.85rem;
|
||||
border: 1px solid var(--line);
|
||||
border-radius: var(--radius);
|
||||
background: #0f172a;
|
||||
color: #e2e8f0;
|
||||
background: var(--surface);
|
||||
overflow: hidden;
|
||||
position: relative;
|
||||
}
|
||||
.stream-head {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.65rem;
|
||||
padding: 0.55rem 0.75rem;
|
||||
background: #1e293b;
|
||||
border-bottom: 1px solid #334155;
|
||||
font: 0.8rem/1 var(--sans);
|
||||
padding: 0.65rem 0.85rem;
|
||||
background: #fafafa;
|
||||
border-bottom: 1px solid var(--line);
|
||||
font: 0.85rem/1 var(--sans);
|
||||
color: var(--ink);
|
||||
}
|
||||
.stream-head button {
|
||||
font: 0.78rem/1 var(--sans);
|
||||
padding: 0.35rem 0.7rem;
|
||||
border-radius: 6px;
|
||||
border: 1px solid #475569;
|
||||
background: #334155;
|
||||
color: #f8fafc;
|
||||
padding: 0.4rem 0.85rem;
|
||||
border-radius: 8px;
|
||||
border: 1px solid var(--line);
|
||||
background: #1d1d1f;
|
||||
color: #fff;
|
||||
cursor: pointer;
|
||||
}
|
||||
.stream-head button:disabled {
|
||||
opacity: 0.5;
|
||||
opacity: 0.45;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
.stream-status {
|
||||
margin-left: auto;
|
||||
color: #94a3b8;
|
||||
color: var(--muted);
|
||||
font: 0.72rem/1 var(--mono);
|
||||
}
|
||||
.stream-log {
|
||||
margin: 0;
|
||||
padding: 0.75rem 0.85rem;
|
||||
min-height: 6rem;
|
||||
max-height: 18rem;
|
||||
.stream-head button.stream-stop {
|
||||
background: #fff;
|
||||
color: #1d1d1f;
|
||||
border-color: var(--line);
|
||||
}
|
||||
.stream-jump {
|
||||
position: absolute;
|
||||
left: 50%;
|
||||
bottom: 0.65rem;
|
||||
transform: translateX(-50%);
|
||||
z-index: 2;
|
||||
font: 0.72rem/1 var(--sans);
|
||||
padding: 0.35rem 0.7rem;
|
||||
border-radius: 999px;
|
||||
border: 1px solid var(--line);
|
||||
background: #fff;
|
||||
color: var(--ink);
|
||||
cursor: pointer;
|
||||
box-shadow: 0 1px 2px rgba(0,0,0,0.06);
|
||||
}
|
||||
.stream-jump[hidden] { display: none; }
|
||||
.stream-body {
|
||||
position: relative;
|
||||
padding: 0.85rem 1rem 1rem;
|
||||
min-height: 5rem;
|
||||
max-height: 28rem;
|
||||
overflow: auto;
|
||||
font: 0.78rem/1.45 var(--mono);
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 0.75rem;
|
||||
}
|
||||
.stream-empty {
|
||||
color: var(--muted);
|
||||
font: 0.85rem/1.4 var(--sans);
|
||||
margin: 0;
|
||||
}
|
||||
.stream-turn {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 0.55rem;
|
||||
}
|
||||
.stream-thinking {
|
||||
border: 1px solid var(--line);
|
||||
border-radius: 10px;
|
||||
background: #f5f5f7;
|
||||
padding: 0.35rem 0.65rem 0.55rem;
|
||||
}
|
||||
.stream-thinking > summary {
|
||||
cursor: pointer;
|
||||
list-style: none;
|
||||
font: 0.78rem/1.4 var(--sans);
|
||||
color: var(--muted);
|
||||
user-select: none;
|
||||
padding: 0.25rem 0;
|
||||
}
|
||||
.stream-thinking > summary::-webkit-details-marker { display: none; }
|
||||
.stream-thinking > summary::before {
|
||||
content: "▸ ";
|
||||
color: #aeaeb2;
|
||||
}
|
||||
.stream-thinking[open] > summary::before { content: "▾ "; }
|
||||
.stream-thinking-body {
|
||||
margin: 0.15rem 0 0;
|
||||
font: 0.82rem/1.5 var(--sans);
|
||||
color: #6e6e73;
|
||||
white-space: pre-wrap;
|
||||
word-break: break-word;
|
||||
}
|
||||
.stream-log .ev-mark {
|
||||
color: #93c5fd;
|
||||
.stream-answer {
|
||||
font: 0.95rem/1.55 var(--sans);
|
||||
color: var(--ink);
|
||||
white-space: pre-wrap;
|
||||
word-break: break-word;
|
||||
}
|
||||
.stream-log .ev-reason {
|
||||
color: #c4b5fd;
|
||||
.stream-answer:empty { display: none; }
|
||||
.stream-decision {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
align-items: baseline;
|
||||
gap: 0.35rem 0.65rem;
|
||||
font: 0.78rem/1.35 var(--sans);
|
||||
color: var(--muted);
|
||||
padding: 0.15rem 0;
|
||||
border-top: 1px dashed var(--line);
|
||||
}
|
||||
.stream-log .ev-err {
|
||||
color: #fca5a5;
|
||||
.stream-decision strong {
|
||||
font-weight: 600;
|
||||
color: #1d1d1f;
|
||||
letter-spacing: 0.02em;
|
||||
}
|
||||
.stream-child {
|
||||
border: 1px solid var(--line);
|
||||
border-left: 3px solid #2563eb;
|
||||
border-radius: 10px;
|
||||
background: #f8fafc;
|
||||
padding: 0.65rem 0.85rem;
|
||||
}
|
||||
.stream-child-head {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
align-items: center;
|
||||
gap: 0.4rem 0.75rem;
|
||||
font: 0.78rem/1.3 var(--sans);
|
||||
color: var(--muted);
|
||||
margin-bottom: 0.35rem;
|
||||
}
|
||||
.stream-child-head strong {
|
||||
color: #1d1d1f;
|
||||
font-weight: 600;
|
||||
}
|
||||
.stream-child-head a { color: #1d4ed8; }
|
||||
.stream-child-meta {
|
||||
font: 0.72rem/1.3 var(--mono);
|
||||
color: var(--muted);
|
||||
}
|
||||
.stream-child-body {
|
||||
font: 0.88rem/1.5 var(--sans);
|
||||
color: var(--ink);
|
||||
white-space: pre-wrap;
|
||||
word-break: break-word;
|
||||
}
|
||||
.stream-child-body:empty { display: none; }
|
||||
.stream-err {
|
||||
font: 0.85rem/1.45 var(--sans);
|
||||
color: #9b1c1c;
|
||||
background: #fff5f5;
|
||||
border: 1px solid #e4c1c1;
|
||||
border-radius: 8px;
|
||||
padding: 0.55rem 0.75rem;
|
||||
white-space: pre-wrap;
|
||||
}
|
||||
.code-head {
|
||||
display: flex;
|
||||
@@ -856,81 +969,383 @@ pub fn layout(title: &str, nav: &str, main: &str) -> String {
|
||||
}}
|
||||
function initStreamPanel() {{
|
||||
if (!window.MARQDO_VIEW_LIVE) return;
|
||||
var log = document.getElementById("stream-log");
|
||||
var body = document.getElementById("stream-body");
|
||||
var status = document.getElementById("stream-status");
|
||||
var runBtn = document.getElementById("stream-run");
|
||||
if (!log || !window.EventSource) return;
|
||||
var es = new EventSource("/api/events?path=" + encodeURIComponent(window.MARQDO_FILE_PATH || ""));
|
||||
function appendLine(cls, text) {{
|
||||
var span = document.createElement("span");
|
||||
if (cls) span.className = cls;
|
||||
span.textContent = text;
|
||||
log.appendChild(span);
|
||||
log.appendChild(document.createTextNode("\\n"));
|
||||
log.scrollTop = log.scrollHeight;
|
||||
var stopBtn = document.getElementById("stream-stop");
|
||||
var jumpBtn = document.getElementById("stream-jump");
|
||||
if (!body) return;
|
||||
|
||||
var turn = null;
|
||||
var thinkingEl = null;
|
||||
var thinkingBody = null;
|
||||
var answerEl = null;
|
||||
var reasonNode = null;
|
||||
var answerNode = null;
|
||||
var pinned = true;
|
||||
var ignoreScroll = false;
|
||||
var pendingReason = "";
|
||||
var pendingAnswer = "";
|
||||
var raf = 0;
|
||||
// Drip 1 Unicode scalar per frame (一字一吐). Catch up if backlog grows.
|
||||
var CHARS_PER_TICK = 1;
|
||||
var abortCtrl = null;
|
||||
|
||||
function nearBottom() {{
|
||||
return body.scrollHeight - body.scrollTop - body.clientHeight < 72;
|
||||
}}
|
||||
es.onopen = function () {{
|
||||
if (status) status.textContent = "connected";
|
||||
}};
|
||||
es.onerror = function () {{
|
||||
if (status) status.textContent = "reconnecting…";
|
||||
}};
|
||||
es.onmessage = function (msg) {{
|
||||
try {{
|
||||
var ev = JSON.parse(msg.data);
|
||||
var t = ev.type || "?";
|
||||
if (t === "reasoning") {{
|
||||
appendLine("ev-reason", ev.text || "");
|
||||
}} else if (t === "delta") {{
|
||||
appendLine(null, ev.text || "");
|
||||
}} else if (t === "error") {{
|
||||
appendLine("ev-err", "error: " + (ev.message || ""));
|
||||
}} else if (t === "round" || t === "decision" || t === "run_start" || t === "done") {{
|
||||
var mark = t;
|
||||
if (t === "round") mark += " #" + (ev.round != null ? ev.round : "");
|
||||
if (t === "decision") mark += " " + (ev.decision || "");
|
||||
if (t === "done" && ev.path) mark += " " + ev.path;
|
||||
appendLine("ev-mark", "— " + mark + " —");
|
||||
if (t === "done" && typeof ev.result === "string" && ev.result) {{
|
||||
appendLine(null, ev.result.slice(0, 2000));
|
||||
}}
|
||||
if (ev.workbook) {{
|
||||
appendLine("ev-mark", "workbook: " + ev.workbook);
|
||||
}}
|
||||
}} else {{
|
||||
appendLine("ev-mark", JSON.stringify(ev));
|
||||
}}
|
||||
}} catch (e) {{
|
||||
appendLine("ev-err", String(msg.data || e));
|
||||
function updateJump() {{
|
||||
if (!jumpBtn) return;
|
||||
jumpBtn.hidden = pinned;
|
||||
}}
|
||||
function scrollIfPinned() {{
|
||||
if (!pinned) {{
|
||||
updateJump();
|
||||
return;
|
||||
}}
|
||||
}};
|
||||
if (runBtn) {{
|
||||
runBtn.addEventListener("click", function () {{
|
||||
runBtn.disabled = true;
|
||||
if (status) status.textContent = "running…";
|
||||
log.textContent = "";
|
||||
var stdinEl = document.getElementById("stdin");
|
||||
fetch("/api/run", {{
|
||||
method: "POST",
|
||||
headers: {{ "Content-Type": "application/json" }},
|
||||
body: JSON.stringify({{
|
||||
path: window.MARQDO_FILE_PATH || "",
|
||||
stdin: stdinEl ? stdinEl.value : ""
|
||||
}})
|
||||
}})
|
||||
.then(function (r) {{ return r.json(); }})
|
||||
.then(function (data) {{
|
||||
if (!data.ok) {{
|
||||
appendLine("ev-err", data.error || "run failed");
|
||||
if (status) status.textContent = "error";
|
||||
}}
|
||||
}})
|
||||
.catch(function (e) {{
|
||||
appendLine("ev-err", String(e));
|
||||
if (status) status.textContent = "error";
|
||||
}})
|
||||
.finally(function () {{ runBtn.disabled = false; }});
|
||||
ignoreScroll = true;
|
||||
body.scrollTop = body.scrollHeight;
|
||||
// Ignore the scroll event our own assignment fires.
|
||||
requestAnimationFrame(function () {{
|
||||
ignoreScroll = false;
|
||||
}});
|
||||
updateJump();
|
||||
}}
|
||||
function onUserScrollIntent() {{
|
||||
// Re-evaluate after the browser applies the user scroll.
|
||||
requestAnimationFrame(function () {{
|
||||
if (ignoreScroll) return;
|
||||
pinned = nearBottom();
|
||||
updateJump();
|
||||
}});
|
||||
}}
|
||||
body.addEventListener("scroll", function () {{
|
||||
if (ignoreScroll) return;
|
||||
pinned = nearBottom();
|
||||
updateJump();
|
||||
}}, {{ passive: true }});
|
||||
body.addEventListener("wheel", onUserScrollIntent, {{ passive: true }});
|
||||
body.addEventListener("touchmove", onUserScrollIntent, {{ passive: true }});
|
||||
if (jumpBtn) {{
|
||||
jumpBtn.addEventListener("click", function () {{
|
||||
pinned = true;
|
||||
ignoreScroll = true;
|
||||
body.scrollTop = body.scrollHeight;
|
||||
requestAnimationFrame(function () {{ ignoreScroll = false; }});
|
||||
updateJump();
|
||||
}});
|
||||
}}
|
||||
|
||||
function clearStream() {{
|
||||
body.innerHTML = "";
|
||||
turn = null;
|
||||
thinkingEl = null;
|
||||
thinkingBody = null;
|
||||
answerEl = null;
|
||||
reasonNode = null;
|
||||
answerNode = null;
|
||||
pendingReason = "";
|
||||
pendingAnswer = "";
|
||||
if (raf) {{ cancelAnimationFrame(raf); raf = 0; }}
|
||||
pinned = true;
|
||||
updateJump();
|
||||
}}
|
||||
function ensureTurn() {{
|
||||
if (turn) return;
|
||||
turn = document.createElement("div");
|
||||
turn.className = "stream-turn";
|
||||
body.appendChild(turn);
|
||||
}}
|
||||
function ensureThinking() {{
|
||||
ensureTurn();
|
||||
if (thinkingEl) return;
|
||||
thinkingEl = document.createElement("details");
|
||||
thinkingEl.className = "stream-thinking";
|
||||
thinkingEl.open = true;
|
||||
var sum = document.createElement("summary");
|
||||
sum.textContent = "Thinking";
|
||||
thinkingBody = document.createElement("div");
|
||||
thinkingBody.className = "stream-thinking-body";
|
||||
reasonNode = document.createTextNode("");
|
||||
thinkingBody.appendChild(reasonNode);
|
||||
thinkingEl.appendChild(sum);
|
||||
thinkingEl.appendChild(thinkingBody);
|
||||
turn.appendChild(thinkingEl);
|
||||
}}
|
||||
function ensureAnswer() {{
|
||||
ensureTurn();
|
||||
if (answerEl) return;
|
||||
answerEl = document.createElement("div");
|
||||
answerEl.className = "stream-answer";
|
||||
answerNode = document.createTextNode("");
|
||||
answerEl.appendChild(answerNode);
|
||||
turn.appendChild(answerEl);
|
||||
}}
|
||||
function flushPendingAll() {{
|
||||
if (raf) {{ cancelAnimationFrame(raf); raf = 0; }}
|
||||
if (pendingReason) {{
|
||||
ensureThinking();
|
||||
reasonNode.data += pendingReason;
|
||||
pendingReason = "";
|
||||
}}
|
||||
if (pendingAnswer) {{
|
||||
ensureAnswer();
|
||||
answerNode.data += pendingAnswer;
|
||||
pendingAnswer = "";
|
||||
}}
|
||||
scrollIfPinned();
|
||||
}}
|
||||
function dripPending() {{
|
||||
raf = 0;
|
||||
function take(src, n) {{
|
||||
var chars = Array.from(src);
|
||||
var takeN = Math.min(n, chars.length);
|
||||
return [chars.slice(0, takeN).join(""), chars.slice(takeN).join("")];
|
||||
}}
|
||||
if (pendingReason) {{
|
||||
ensureThinking();
|
||||
var n = pendingReason.length > 240 ? 12 : (pendingReason.length > 80 ? 4 : CHARS_PER_TICK);
|
||||
var r = take(pendingReason, n);
|
||||
reasonNode.data += r[0];
|
||||
pendingReason = r[1];
|
||||
}} else if (pendingAnswer) {{
|
||||
ensureAnswer();
|
||||
var n2 = pendingAnswer.length > 240 ? 12 : (pendingAnswer.length > 80 ? 4 : CHARS_PER_TICK);
|
||||
var a = take(pendingAnswer, n2);
|
||||
answerNode.data += a[0];
|
||||
pendingAnswer = a[1];
|
||||
}}
|
||||
scrollIfPinned();
|
||||
if (pendingReason || pendingAnswer) {{
|
||||
raf = requestAnimationFrame(dripPending);
|
||||
}}
|
||||
}}
|
||||
function queueText(kind, text) {{
|
||||
if (!text) return;
|
||||
if (kind === "reasoning") pendingReason += text;
|
||||
else pendingAnswer += text;
|
||||
if (!raf) raf = requestAnimationFrame(dripPending);
|
||||
}}
|
||||
function formatResult(v) {{
|
||||
if (v == null) return "";
|
||||
if (typeof v === "string") return v;
|
||||
try {{ return JSON.stringify(v, null, 2); }} catch (e) {{ return String(v); }}
|
||||
}}
|
||||
function resetTurnRefs() {{
|
||||
turn = null;
|
||||
thinkingEl = null;
|
||||
thinkingBody = null;
|
||||
answerEl = null;
|
||||
reasonNode = null;
|
||||
answerNode = null;
|
||||
}}
|
||||
function addDecision(ev) {{
|
||||
flushPendingAll();
|
||||
ensureTurn();
|
||||
var row = document.createElement("div");
|
||||
row.className = "stream-decision";
|
||||
var label = document.createElement("strong");
|
||||
label.textContent = "DECISION: " + (ev.decision || "?");
|
||||
row.appendChild(label);
|
||||
if (ev.summary) {{
|
||||
var s = document.createElement("span");
|
||||
s.textContent = ev.summary;
|
||||
row.appendChild(s);
|
||||
}}
|
||||
turn.appendChild(row);
|
||||
resetTurnRefs();
|
||||
scrollIfPinned();
|
||||
}}
|
||||
function addChildCard(ev) {{
|
||||
flushPendingAll();
|
||||
var card = document.createElement("div");
|
||||
card.className = "stream-child";
|
||||
var head = document.createElement("div");
|
||||
head.className = "stream-child-head";
|
||||
var title = document.createElement("strong");
|
||||
title.textContent = "Child workbook";
|
||||
head.appendChild(title);
|
||||
var meta = document.createElement("span");
|
||||
meta.className = "stream-child-meta";
|
||||
var bits = [];
|
||||
if (ev.round != null) bits.push("round " + ev.round);
|
||||
if (ev.exit_code != null) bits.push("exit " + ev.exit_code);
|
||||
meta.textContent = bits.join(" · ");
|
||||
head.appendChild(meta);
|
||||
if (ev.workbook) {{
|
||||
var a = document.createElement("a");
|
||||
a.href = "/file?path=" + encodeURIComponent(ev.workbook);
|
||||
a.textContent = ev.workbook;
|
||||
head.appendChild(a);
|
||||
}}
|
||||
card.appendChild(head);
|
||||
var res = formatResult(ev.result);
|
||||
if (res) {{
|
||||
var out = document.createElement("div");
|
||||
out.className = "stream-child-body";
|
||||
out.textContent = res.length > 8000 ? res.slice(0, 8000) + "…" : res;
|
||||
card.appendChild(out);
|
||||
}}
|
||||
body.appendChild(card);
|
||||
resetTurnRefs();
|
||||
scrollIfPinned();
|
||||
}}
|
||||
function addError(msg) {{
|
||||
flushPendingAll();
|
||||
var err = document.createElement("div");
|
||||
err.className = "stream-err";
|
||||
err.textContent = msg || "error";
|
||||
body.appendChild(err);
|
||||
scrollIfPinned();
|
||||
}}
|
||||
function handleEvent(ev) {{
|
||||
var t = ev.type || "?";
|
||||
if (t === "reasoning") {{
|
||||
queueText("reasoning", ev.text || "");
|
||||
}} else if (t === "delta") {{
|
||||
queueText("answer", ev.text || "");
|
||||
}} else if (t === "error") {{
|
||||
addError(ev.message || "error");
|
||||
if (status) status.textContent = "error";
|
||||
}} else if (t === "decision") {{
|
||||
addDecision(ev);
|
||||
}} else if (t === "round") {{
|
||||
addChildCard(ev);
|
||||
}} else if (t === "run_start") {{
|
||||
if (status) status.textContent = "running…";
|
||||
ensureTurn();
|
||||
var mark = document.createElement("div");
|
||||
mark.className = "stream-decision";
|
||||
var strong = document.createElement("strong");
|
||||
strong.textContent = "RUN";
|
||||
mark.appendChild(strong);
|
||||
if (ev.path) {{
|
||||
var sp = document.createElement("span");
|
||||
sp.textContent = ev.path;
|
||||
mark.appendChild(sp);
|
||||
}}
|
||||
turn.appendChild(mark);
|
||||
scrollIfPinned();
|
||||
}} else if (t === "done") {{
|
||||
flushPendingAll();
|
||||
if (ev.run_id != null && status) status.textContent = "done";
|
||||
var hasAnswer = answerNode && answerNode.data;
|
||||
if (!hasAnswer && typeof ev.result === "string" && ev.result) {{
|
||||
ensureAnswer();
|
||||
answerNode.data += ev.result;
|
||||
scrollIfPinned();
|
||||
}}
|
||||
if (thinkingEl && (answerNode && answerNode.data)) thinkingEl.open = false;
|
||||
}}
|
||||
}}
|
||||
function setRunning(on) {{
|
||||
if (runBtn) runBtn.disabled = on;
|
||||
var formBtn = document.querySelector(".stdin-form button[type='submit']");
|
||||
if (formBtn) formBtn.disabled = on;
|
||||
if (stopBtn) {{
|
||||
stopBtn.disabled = !on;
|
||||
stopBtn.hidden = !on;
|
||||
}}
|
||||
}}
|
||||
async function readSseStream(response) {{
|
||||
var ctype = (response.headers.get("content-type") || "").toLowerCase();
|
||||
if (ctype.indexOf("application/json") !== -1) {{
|
||||
var data = await response.json();
|
||||
throw new Error(data.error || data.message || JSON.stringify(data));
|
||||
}}
|
||||
if (!response.ok) {{
|
||||
var errText = await response.text();
|
||||
throw new Error(errText || ("HTTP " + response.status));
|
||||
}}
|
||||
if (!response.body || !response.body.getReader) {{
|
||||
throw new Error("ReadableStream not supported");
|
||||
}}
|
||||
var reader = response.body.getReader();
|
||||
var decoder = new TextDecoder();
|
||||
var buf = "";
|
||||
while (true) {{
|
||||
var chunk = await reader.read();
|
||||
if (chunk.done) break;
|
||||
buf += decoder.decode(chunk.value, {{ stream: true }});
|
||||
buf = buf.replace(/\r\n/g, "\n").replace(/\r/g, "\n");
|
||||
var parts = buf.split("\n\n");
|
||||
buf = parts.pop() || "";
|
||||
for (var i = 0; i < parts.length; i++) {{
|
||||
var block = parts[i];
|
||||
if (!block || block.charAt(0) === ":") continue;
|
||||
var lines = block.split("\n");
|
||||
var dataLines = [];
|
||||
for (var j = 0; j < lines.length; j++) {{
|
||||
var line = lines[j];
|
||||
if (line.indexOf("data:") === 0) {{
|
||||
dataLines.push(line.slice(5).replace(/^ /, ""));
|
||||
}}
|
||||
}}
|
||||
if (!dataLines.length) continue;
|
||||
try {{
|
||||
handleEvent(JSON.parse(dataLines.join("\n")));
|
||||
}} catch (e) {{
|
||||
addError(String(e));
|
||||
}}
|
||||
}}
|
||||
}}
|
||||
flushPendingAll();
|
||||
}}
|
||||
function startRun() {{
|
||||
if (abortCtrl) abortCtrl.abort();
|
||||
abortCtrl = new AbortController();
|
||||
setRunning(true);
|
||||
if (status) status.textContent = "running…";
|
||||
clearStream();
|
||||
pinned = true;
|
||||
var stdinEl = document.getElementById("stdin");
|
||||
fetch("/api/run", {{
|
||||
method: "POST",
|
||||
headers: {{
|
||||
"Content-Type": "application/json",
|
||||
"Accept": "text/event-stream"
|
||||
}},
|
||||
body: JSON.stringify({{
|
||||
path: window.MARQDO_FILE_PATH || "",
|
||||
stdin: stdinEl ? stdinEl.value : ""
|
||||
}}),
|
||||
signal: abortCtrl.signal
|
||||
}})
|
||||
.then(readSseStream)
|
||||
.then(function () {{
|
||||
if (status && status.textContent === "running…") status.textContent = "done";
|
||||
}})
|
||||
.catch(function (e) {{
|
||||
if (e && e.name === "AbortError") {{
|
||||
if (status) status.textContent = "stopped";
|
||||
return;
|
||||
}}
|
||||
addError(String(e && e.message ? e.message : e));
|
||||
if (status) status.textContent = "error";
|
||||
}})
|
||||
.finally(function () {{
|
||||
setRunning(false);
|
||||
abortCtrl = null;
|
||||
}});
|
||||
}}
|
||||
window.marqdoStreamRun = startRun;
|
||||
if (stopBtn) {{
|
||||
stopBtn.addEventListener("click", function () {{
|
||||
if (abortCtrl) abortCtrl.abort();
|
||||
}});
|
||||
}}
|
||||
if (runBtn) {{
|
||||
runBtn.addEventListener("click", function () {{ startRun(); }});
|
||||
}}
|
||||
var form = document.querySelector(".stdin-form");
|
||||
if (form) {{
|
||||
form.addEventListener("submit", function (ev) {{
|
||||
ev.preventDefault();
|
||||
startRun();
|
||||
}});
|
||||
}}
|
||||
if (window.MARQDO_AUTO_STREAM) {{
|
||||
startRun();
|
||||
}}
|
||||
}}
|
||||
|
||||
@@ -1157,7 +1572,14 @@ pub fn page_file(files: &[PathBuf], rel: &str, vm: &FileViewModel, links: &LinkM
|
||||
(
|
||||
"pending",
|
||||
"input",
|
||||
"Fill preset input below, then click <strong>Run with input</strong>.".to_string(),
|
||||
"Fill preset input, then <strong>Run with input</strong> — output streams in the panel (no page reload)."
|
||||
.to_string(),
|
||||
)
|
||||
} else if vm.auto_stream {
|
||||
(
|
||||
"pending",
|
||||
"stream",
|
||||
"Streaming in the panel below…".to_string(),
|
||||
)
|
||||
} else if vm.ok {
|
||||
(
|
||||
@@ -1264,9 +1686,13 @@ pub fn page_file(files: &[PathBuf], rel: &str, vm: &FileViewModel, links: &LinkM
|
||||
<div class="stream-head">
|
||||
<strong>Stream</strong>
|
||||
<button type="button" id="stream-run">Run</button>
|
||||
<button type="button" class="stream-stop" id="stream-stop" hidden disabled>Stop</button>
|
||||
<span class="stream-status" id="stream-status">idle</span>
|
||||
</div>
|
||||
<pre class="stream-log" id="stream-log"></pre>
|
||||
<div class="stream-body" id="stream-body">
|
||||
<p class="stream-empty">Use <strong>Run with input</strong> or <strong>Run</strong> — thinking and answer stream here.</p>
|
||||
</div>
|
||||
<button type="button" class="stream-jump" id="stream-jump" hidden>↓ Latest</button>
|
||||
</div>"#
|
||||
.to_string(),
|
||||
LinkMode::Static { .. } => String::new(),
|
||||
@@ -1276,6 +1702,7 @@ pub fn page_file(files: &[PathBuf], rel: &str, vm: &FileViewModel, links: &LinkM
|
||||
<script>
|
||||
window.MARQDO_VIEW_LIVE = {live};
|
||||
window.MARQDO_FILE_PATH = {path_js};
|
||||
window.MARQDO_AUTO_STREAM = {auto_stream};
|
||||
</script>
|
||||
<h1 class="page">{title}<span class="status-pill {status}">{status_label}</span></h1>
|
||||
<p class="meta">{rel}</p>
|
||||
@@ -1298,6 +1725,7 @@ window.MARQDO_FILE_PATH = {path_js};
|
||||
"#,
|
||||
live = live_flag,
|
||||
path_js = escape_js_string(rel),
|
||||
auto_stream = if vm.auto_stream { "true" } else { "false" },
|
||||
title = escape(title),
|
||||
status = status,
|
||||
status_label = status_label,
|
||||
|
||||
+99
-28
@@ -323,13 +323,16 @@ pub(crate) fn build_file_view(
|
||||
};
|
||||
let effective = effective_stdin(&source, stdin_lines);
|
||||
let awaiting_input = awaiting_preset_input(&input_prompts, stdin_lines, &source);
|
||||
// Live + has `input` + stdin ready → stream via client `/api/run` (no page-blocking capture).
|
||||
let auto_stream =
|
||||
live && !input_prompts.is_empty() && !awaiting_input && !stdin_lines.is_empty();
|
||||
// Same host caps as `marqdo run`. Soft exit + sleep clamp only.
|
||||
let mut opts = RunOptions::default();
|
||||
opts.stdin_lines = effective.clone();
|
||||
opts.sleep_limit_ms = if live { Some(30_000) } else { Some(0) };
|
||||
// Optional: sandbox to the whole view tree so sibling folders are reachable.
|
||||
opts.fs_root = Some(root.root.clone());
|
||||
let (stdout, stderr, ok, plots) = if awaiting_input {
|
||||
let (stdout, stderr, ok, plots) = if awaiting_input || auto_stream {
|
||||
(String::new(), String::new(), true, Vec::new())
|
||||
} else {
|
||||
match run_file_capture(abs, &opts) {
|
||||
@@ -348,7 +351,7 @@ pub(crate) fn build_file_view(
|
||||
}
|
||||
};
|
||||
// Re-read after run so writeback slots / workbook edits show in Structure.
|
||||
if !awaiting_input {
|
||||
if !awaiting_input && !auto_stream {
|
||||
if let Ok(next) = fs::read_to_string(abs) {
|
||||
source = next;
|
||||
if let Ok(module) = crate::parse::parse_source(&source) {
|
||||
@@ -369,6 +372,7 @@ pub(crate) fn build_file_view(
|
||||
preset_stdin: effective.join("\n"),
|
||||
input_prompts,
|
||||
awaiting_input,
|
||||
auto_stream,
|
||||
plots,
|
||||
})
|
||||
}
|
||||
@@ -549,26 +553,13 @@ fn api_foreign_run(root: &ViewRoot, mut request: Request) -> Result<()> {
|
||||
}
|
||||
|
||||
/// SSE: long-lived `text/event-stream` of EventBus JSON maps (`data: …\n\n`).
|
||||
/// Kept for debugging; the Stream panel uses `POST /api/run` instead (no page-load spinner).
|
||||
fn api_events(request: Request) -> Result<()> {
|
||||
let rx = crate::host::event_bus::EventBus::global().subscribe();
|
||||
let body = SseBody::new(rx);
|
||||
let ct = Header::from_bytes(&b"Content-Type"[..], &b"text/event-stream; charset=utf-8"[..])
|
||||
.map_err(|_| anyhow::anyhow!("header"))?;
|
||||
let cc = Header::from_bytes(&b"Cache-Control"[..], &b"no-cache"[..])
|
||||
.map_err(|_| anyhow::anyhow!("header"))?;
|
||||
let conn = Header::from_bytes(&b"Connection"[..], &b"keep-alive"[..])
|
||||
.map_err(|_| anyhow::anyhow!("header"))?;
|
||||
let resp = Response::empty(StatusCode(200))
|
||||
.with_header(ct)
|
||||
.with_header(cc)
|
||||
.with_header(conn)
|
||||
.with_data(body, None)
|
||||
.with_chunked_threshold(0);
|
||||
request.respond(resp).context("respond sse")?;
|
||||
Ok(())
|
||||
respond_sse(request, SseBody::new(rx, false))
|
||||
}
|
||||
|
||||
/// Background `run_file` for the live Stream panel; events go to the EventBus.
|
||||
/// Run a file and stream EventBus events as SSE on this same response (ChatGPT-style fetch stream).
|
||||
fn api_run(root: &ViewRoot, mut request: Request) -> Result<()> {
|
||||
let mut body = Vec::new();
|
||||
std::io::Read::read_to_end(&mut request.as_reader(), &mut body)?;
|
||||
@@ -578,25 +569,31 @@ fn api_run(root: &ViewRoot, mut request: Request) -> Result<()> {
|
||||
let stdin_raw = v.get("stdin").and_then(|x| x.as_str()).unwrap_or("");
|
||||
let stdin_lines = split_stdin_text(stdin_raw);
|
||||
if rel.is_empty() {
|
||||
return respond_json(
|
||||
return respond_json_status(
|
||||
request,
|
||||
StatusCode(400),
|
||||
serde_json::json!({ "ok": false, "error": "missing path" }).to_string(),
|
||||
);
|
||||
}
|
||||
let abs = match resolve_rel(root, &rel) {
|
||||
Ok(p) => p,
|
||||
Err(e) => {
|
||||
return respond_json(
|
||||
return respond_json_status(
|
||||
request,
|
||||
StatusCode(400),
|
||||
serde_json::json!({ "ok": false, "error": format!("{e:#}") }).to_string(),
|
||||
);
|
||||
}
|
||||
};
|
||||
// Subscribe before spawn so early tokens are not lost.
|
||||
let rx = crate::host::event_bus::EventBus::global().subscribe();
|
||||
let run_id = next_run_id();
|
||||
let root_path = root.root.clone();
|
||||
let rel_clone = rel.clone();
|
||||
std::thread::spawn(move || {
|
||||
let start = serde_json::json!({
|
||||
"type": "run_start",
|
||||
"run_id": run_id,
|
||||
"path": rel_clone,
|
||||
});
|
||||
crate::host::event_bus::EventBus::global().publish_json(&start.to_string());
|
||||
@@ -608,17 +605,21 @@ fn api_run(root: &ViewRoot, mut request: Request) -> Result<()> {
|
||||
opts.fs_root = Some(root_path);
|
||||
match run_file_capture(&abs, &opts) {
|
||||
Ok(cap) => {
|
||||
// Keep the terminal frame small — tokens already streamed via bus.
|
||||
let preview: String = cap.stdout.chars().take(500).collect();
|
||||
let done = serde_json::json!({
|
||||
"type": "done",
|
||||
"run_id": run_id,
|
||||
"path": rel_clone,
|
||||
"ok": true,
|
||||
"result": cap.stdout,
|
||||
"result": preview,
|
||||
});
|
||||
crate::host::event_bus::EventBus::global().publish_json(&done.to_string());
|
||||
}
|
||||
Err(e) => {
|
||||
let err = serde_json::json!({
|
||||
"type": "error",
|
||||
"run_id": run_id,
|
||||
"path": rel_clone,
|
||||
"message": format!("{e:#}"),
|
||||
});
|
||||
@@ -626,30 +627,80 @@ fn api_run(root: &ViewRoot, mut request: Request) -> Result<()> {
|
||||
}
|
||||
}
|
||||
});
|
||||
respond_json(
|
||||
request,
|
||||
serde_json::json!({ "ok": true, "started": true, "path": rel }).to_string(),
|
||||
)
|
||||
// Only close on this run's terminal frame — ignore nested LLM `done` without run_id.
|
||||
respond_sse(request, SseBody::for_run(rx, run_id))
|
||||
}
|
||||
|
||||
fn next_run_id() -> u64 {
|
||||
use std::sync::atomic::{AtomicU64, Ordering};
|
||||
static SEQ: AtomicU64 = AtomicU64::new(1);
|
||||
SEQ.fetch_add(1, Ordering::Relaxed)
|
||||
}
|
||||
|
||||
fn respond_sse(request: Request, body: SseBody) -> Result<()> {
|
||||
let ct = Header::from_bytes(&b"Content-Type"[..], &b"text/event-stream; charset=utf-8"[..])
|
||||
.map_err(|_| anyhow::anyhow!("header"))?;
|
||||
let cc = Header::from_bytes(&b"Cache-Control"[..], &b"no-cache, no-transform"[..])
|
||||
.map_err(|_| anyhow::anyhow!("header"))?;
|
||||
let conn = Header::from_bytes(&b"Connection"[..], &b"keep-alive"[..])
|
||||
.map_err(|_| anyhow::anyhow!("header"))?;
|
||||
let xb = Header::from_bytes(&b"X-Accel-Buffering"[..], &b"no"[..])
|
||||
.map_err(|_| anyhow::anyhow!("header"))?;
|
||||
let resp = Response::empty(StatusCode(200))
|
||||
.with_header(ct)
|
||||
.with_header(cc)
|
||||
.with_header(conn)
|
||||
.with_header(xb)
|
||||
.with_data(body, None)
|
||||
.with_chunked_threshold(0);
|
||||
request.respond(resp).context("respond sse")?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
struct SseBody {
|
||||
rx: Receiver<String>,
|
||||
buf: Vec<u8>,
|
||||
pos: usize,
|
||||
/// When set, close after a `done`/`error` frame that carries this `run_id`.
|
||||
stop_run_id: Option<u64>,
|
||||
closed: bool,
|
||||
}
|
||||
|
||||
impl SseBody {
|
||||
fn new(rx: Receiver<String>) -> Self {
|
||||
fn new(rx: Receiver<String>, _legacy: bool) -> Self {
|
||||
Self {
|
||||
rx,
|
||||
buf: Vec::new(),
|
||||
pos: 0,
|
||||
stop_run_id: None,
|
||||
closed: false,
|
||||
}
|
||||
}
|
||||
|
||||
fn for_run(rx: Receiver<String>, run_id: u64) -> Self {
|
||||
Self {
|
||||
rx,
|
||||
buf: Vec::new(),
|
||||
pos: 0,
|
||||
stop_run_id: Some(run_id),
|
||||
closed: false,
|
||||
}
|
||||
}
|
||||
|
||||
fn frame_ends_run(json: &str, run_id: u64) -> bool {
|
||||
let id_marker = format!("\"run_id\":{run_id}");
|
||||
if !json.contains(&id_marker) {
|
||||
return false;
|
||||
}
|
||||
json.contains("\"type\":\"done\"") || json.contains("\"type\":\"error\"")
|
||||
}
|
||||
}
|
||||
|
||||
impl Read for SseBody {
|
||||
fn read(&mut self, out: &mut [u8]) -> io::Result<usize> {
|
||||
if self.closed && self.pos >= self.buf.len() {
|
||||
return Ok(0);
|
||||
}
|
||||
loop {
|
||||
if self.pos < self.buf.len() {
|
||||
let n = (self.buf.len() - self.pos).min(out.len());
|
||||
@@ -657,16 +708,30 @@ impl Read for SseBody {
|
||||
self.pos += n;
|
||||
return Ok(n);
|
||||
}
|
||||
if self.closed {
|
||||
return Ok(0);
|
||||
}
|
||||
match self.rx.recv_timeout(Duration::from_secs(15)) {
|
||||
Ok(json) => {
|
||||
let terminal = self
|
||||
.stop_run_id
|
||||
.map(|id| Self::frame_ends_run(&json, id))
|
||||
.unwrap_or(false);
|
||||
self.buf = format!("data: {json}\n\n").into_bytes();
|
||||
self.pos = 0;
|
||||
if terminal {
|
||||
self.closed = true;
|
||||
}
|
||||
}
|
||||
Err(RecvTimeoutError::Timeout) => {
|
||||
// Comment heartbeat keeps intermediaries from buffering forever.
|
||||
self.buf = b": ping\n\n".to_vec();
|
||||
self.pos = 0;
|
||||
}
|
||||
Err(RecvTimeoutError::Disconnected) => return Ok(0),
|
||||
Err(RecvTimeoutError::Disconnected) => {
|
||||
self.closed = true;
|
||||
return Ok(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -681,9 +746,15 @@ fn respond_html(request: Request, body: String) -> Result<()> {
|
||||
}
|
||||
|
||||
fn respond_json(request: Request, body: String) -> Result<()> {
|
||||
respond_json_status(request, StatusCode(200), body)
|
||||
}
|
||||
|
||||
fn respond_json_status(request: Request, code: StatusCode, body: String) -> Result<()> {
|
||||
let header = Header::from_bytes(&b"Content-Type"[..], &b"application/json; charset=utf-8"[..])
|
||||
.map_err(|_| anyhow::anyhow!("header"))?;
|
||||
let resp = Response::from_string(body).with_header(header);
|
||||
let resp = Response::from_string(body)
|
||||
.with_status_code(code)
|
||||
.with_header(header);
|
||||
request.respond(resp).context("respond")?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -37,6 +37,8 @@ pub struct FileViewModel {
|
||||
pub input_prompts: Vec<String>,
|
||||
/// Live view: program uses `input` but no stdin yet — execution deferred until form submit.
|
||||
pub awaiting_input: bool,
|
||||
/// Live view: stdin present — skip blocking page-load run; client auto-starts Stream SSE.
|
||||
pub auto_stream: bool,
|
||||
/// SVG plots from math lib (embedded in Execution).
|
||||
pub plots: Vec<String>,
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user