= None;
let (stdout, stderr, ok, plots, mut bindings_html) = if !execute {
(
String::new(),
"execution skipped (--no-exec)".into(),
true,
Vec::new(),
String::from(r#"skipped
"#),
)
} else if library_document {
(
String::new(),
String::new(),
true,
Vec::new(),
String::from(r#"Choose a named function with marqdo call.
"#),
)
} else if awaiting_input || auto_stream {
(
String::new(),
String::new(),
true,
Vec::new(),
String::new(),
)
} else {
match run_file_capture(abs, &opts) {
Ok(cap) => {
let stdout = cap.stdout.clone();
let plots = cap.plots.iter().map(|p| p.svg.clone()).collect();
let bindings_html = render_bindings_html(&cap.bindings);
captured = Some(cap);
(stdout, String::new(), true, plots, bindings_html)
}
Err(e) => (
String::new(),
tidy_user_error(&format!("{e:#}"), abs, rel),
false,
Vec::new(),
String::new(),
),
}
};
// Re-read after run so writeback slots / workbook edits show in Structure.
if execute && !library_document && !awaiting_input && !auto_stream {
if let Ok(next) = fs::read_to_string(abs) {
source = next;
}
if let Ok(module) = crate::load::parse_source_auto(&source) {
let annots = match &captured {
Some(cap) => build_view_annots(&module, &source, cap),
None => Vec::new(),
};
structure = render_module_structure_with_annots(&module, &source, &annots);
module_opt = Some(module);
}
}
let wb = crate::view::render::render_writeback_outline(&source);
let outline = match &module_opt {
Some(module) => render_right_rail(module, &bindings_html, &wb),
None => {
if bindings_html.is_empty() && wb.is_empty() {
String::new()
} else {
render_right_rail(&crate::ast::Module::default(), &bindings_html, &wb)
}
}
};
let trace_json = match &captured {
Some(cap) => view_execution_report_json(rel, ok, cap),
None => String::new(),
};
// Analysis is intentionally independent from capture/run. If a document
// has side effects, its facts remain visible while its values stay absent.
let title_guess = crate::view::title_from_source(&source, rel);
let analysis = if root.only_file.is_some() {
crate::sema::compile_file_closure(abs)
} else {
crate::sema::compile_project(&crate::sema::CompileOptions {
project_root: root.root.clone(),
entry: Some(abs.to_path_buf()),
lock_mode: crate::sema::LockMode::Off,
})
};
let (knowledge_html, knowledge_error, page_token) = match analysis {
Ok(compilation) => {
// Dependency closures may have a wider root than the reader's file directory.
let semantic_rel = abs
.strip_prefix(&compilation.semantics.root)
.unwrap_or(abs)
.to_string_lossy()
.replace('\\', "/");
let html = crate::view::knowledge_reading::render(
&compilation.semantics,
&semantic_rel,
module_opt.as_ref(),
);
// V1: also materialize structured presentation (JSON available to page scripts).
let mut presentation = crate::view::KnowledgePresentation::from_project(
&compilation.semantics,
&semantic_rel,
&title_guess,
);
presentation.document_id = rel.to_string();
presentation.page_token = crate::view::page_token_from_source(rel, &source);
let json = presentation.to_json().to_string();
let with_model = if html.is_empty() {
format!(
r#""#,
json = json.replace("", "<\\/")
)
} else {
format!(
r#"{html}"#,
html = html,
json = json.replace("", "<\\/")
)
};
(with_model, String::new(), presentation.page_token.clone())
}
Err(e) => (
String::new(),
format!(
"知识关系暂不可用:{}",
tidy_user_error(&format!("{e:#}"), abs, rel)
),
crate::view::page_token_from_source(rel, &source),
),
};
let page_token = if page_token.is_empty() {
crate::view::page_token_from_source(rel, &source)
} else {
page_token
};
Ok(FileViewModel {
rel_path: rel.to_string(),
source,
structure_html: html::document_links(&structure, &root.files, rel, &LinkMode::Live),
outline_html: outline,
stdout,
stderr,
ok,
library_document,
preset_stdin: effective.join("\n"),
input_prompts,
awaiting_input,
auto_stream,
show_stream,
plots,
bindings_html: std::mem::take(&mut bindings_html),
trace_json,
knowledge_html,
knowledge_error,
page_token,
})
}
/// Defer execution until the view preset-input form supplies lines (no query stdin, no FM stdin).
pub(crate) fn awaiting_preset_input(
input_prompts: &[String],
stdin_lines: &[String],
source: &str,
) -> bool {
!input_prompts.is_empty()
&& stdin_lines.is_empty()
&& extract_frontmatter_stdin(source).is_empty()
}
/// Prefer view-relative paths and strip Windows `\\?\` noise in error text.
fn tidy_user_error(msg: &str, abs: &Path, rel: &str) -> String {
use crate::diagnostics::display_path;
let mut s = msg.to_string();
let abs_disp = abs.display().to_string();
let abs_clean = display_path(abs);
for candidate in [
abs_disp.as_str(),
abs_clean.as_str(),
&abs_disp.replace('/', "\\"),
&abs_clean.replace('/', "\\"),
&abs_disp.replace('\\', "/"),
&abs_clean.replace('\\', "/"),
] {
if !candidate.is_empty() {
s = s.replace(candidate, rel);
}
}
// Leftover extended prefixes anywhere in the message
s = s.replace(r"\\?\", "");
s = s.replace("//?/", "");
s
}
fn resolve_rel(root: &ViewRoot, rel: &str) -> Result {
if rel.is_empty() {
bail!("missing path");
}
let rel_path = PathBuf::from(rel.replace('\\', "/"));
if rel_path
.components()
.any(|c| matches!(c, std::path::Component::ParentDir))
{
bail!("path escapes root");
}
if let Some(only) = &root.only_file {
if &rel_path != only {
bail!("file not in view scope");
}
} else if !root.files.iter().any(|f| f == &rel_path) {
// also allow forward-slash normalized compare
let norm = rel_path.to_string_lossy().replace('\\', "/");
if !root
.files
.iter()
.any(|f| f.to_string_lossy().replace('\\', "/") == norm)
{
bail!("unknown file: {rel}");
}
}
let abs = root.root.join(&rel_path);
let canon = abs.canonicalize().unwrap_or(abs.clone());
let root_canon = root.root.canonicalize().unwrap_or_else(|_| root.root.clone());
if !canon.starts_with(&root_canon) {
bail!("path escapes root");
}
if !is_mq_md(&canon) {
bail!("not a .mq.md file");
}
Ok(canon)
}
fn split_url(url: &str) -> (&str, &str) {
match url.split_once('?') {
Some((p, q)) => (p, q),
None => (url, ""),
}
}
fn query_param<'a>(query: &'a str, key: &str) -> Option {
for pair in query.split('&') {
let mut kv = pair.splitn(2, '=');
let k = kv.next()?;
let v = kv.next().unwrap_or("");
if k == key {
return Some(urlencoding_decode(v));
}
}
None
}
fn urlencoding_decode(s: &str) -> String {
// Minimal decode for %XX and + (UTF-8 byte sequences, not Latin-1 chars).
let mut out = Vec::new();
let bytes = s.as_bytes();
let mut i = 0;
while i < bytes.len() {
match bytes[i] {
b'+' => {
out.push(b' ');
i += 1;
}
b'%' if i + 2 < bytes.len() => {
let hex = &s[i + 1..i + 3];
if let Ok(v) = u8::from_str_radix(hex, 16) {
out.push(v);
i += 3;
} else {
out.push(b'%');
i += 1;
}
}
c => {
out.push(c);
i += 1;
}
}
}
String::from_utf8_lossy(&out).into_owned()
}
fn files_json(files: &[PathBuf]) -> String {
let list: Vec = files
.iter()
.map(|f| format!("\"{}\"", escape_json(&f.to_string_lossy().replace('\\', "/"))))
.collect();
format!("{{\"files\":[{}]}}", list.join(","))
}
fn escape_json(s: &str) -> String {
s.replace('\\', "\\\\").replace('"', "\\\"")
}
fn api_debug_post(
mut request: Request,
f: impl FnOnce(&str) -> serde_json::Value,
) -> Result<()> {
let mut body = Vec::new();
std::io::Read::read_to_end(&mut request.as_reader(), &mut body)?;
let body = String::from_utf8_lossy(&body);
respond_json(request, f(&body).to_string())
}
fn api_foreign_run(root: &ViewRoot, mut request: Request) -> Result<()> {
let mut body = Vec::new();
std::io::Read::read_to_end(&mut request.as_reader(), &mut body)?;
let body = String::from_utf8_lossy(&body);
let v: serde_json::Value = serde_json::from_str(&body).unwrap_or(serde_json::Value::Null);
let lang = v
.get("lang")
.and_then(|x| x.as_str())
.unwrap_or("python");
let source = v.get("source").and_then(|x| x.as_str()).unwrap_or("");
let stdin = v.get("stdin").and_then(|x| x.as_str());
let timeout = v.get("timeout").and_then(|x| x.as_f64());
let cmd = v
.get("cmd")
.and_then(|x| x.as_str())
.map(str::trim)
.filter(|s| !s.is_empty());
let json = if source.is_empty() {
serde_json::json!({ "ok": false, "error": "missing source" }).to_string()
} else {
match crate::host::foreign::invoke_for_view(
&root.root,
lang,
source,
cmd,
stdin,
timeout,
) {
Ok(value) => match crate::host::json::value_to_json(&value) {
Ok(j) => j.to_string(),
Err(e) => serde_json::json!({ "ok": false, "error": e }).to_string(),
},
Err(error) => serde_json::json!({ "ok": false, "error": error }).to_string(),
}
};
respond_json(request, json)
}
/// 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();
respond_sse(request, SseBody::new(rx, false))
}
/// 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)?;
let body = String::from_utf8_lossy(&body);
let v: serde_json::Value = serde_json::from_str(&body).unwrap_or(serde_json::Value::Null);
let rel = v.get("path").and_then(|x| x.as_str()).unwrap_or("").to_string();
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_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_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());
let source = fs::read_to_string(&abs).unwrap_or_default();
let effective = effective_stdin(&source, &stdin_lines);
let mut opts = RunOptions::default();
opts.stdin_lines = effective;
opts.sleep_limit_ms = Some(30_000);
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 bindings_html = render_bindings_html(&cap.bindings);
let source_now = fs::read_to_string(&abs).unwrap_or(source);
let (structure_html, trace_json) = match crate::load::parse_source_auto(&source_now) {
Ok(module) => {
let annots = build_view_annots(&module, &source_now, &cap);
(
render_module_structure_with_annots(&module, &source_now, &annots),
view_execution_report_json(&rel_clone, true, &cap),
)
}
Err(_) => (String::new(), String::new()),
};
let page_token = crate::view::page_token_from_source(&rel_clone, &source_now);
let done = serde_json::json!({
"type": "done",
"run_id": run_id,
"path": rel_clone,
"ok": true,
"result": preview,
"bindings_html": bindings_html,
"structure_html": structure_html,
"trace_json": trace_json,
"page_token": page_token,
});
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:#}"),
});
crate::host::event_bus::EventBus::global().publish_json(&err.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,
buf: Vec,
pos: usize,
/// When set, close after a `done`/`error` frame that carries this `run_id`.
stop_run_id: Option,
closed: bool,
}
impl SseBody {
fn new(rx: Receiver, _legacy: bool) -> Self {
Self {
rx,
buf: Vec::new(),
pos: 0,
stop_run_id: None,
closed: false,
}
}
fn for_run(rx: Receiver, run_id: u64) -> Self {
// Prime with a comment so proxies/browsers flush response headers immediately
// (industry SSE practice: first bytes ASAP after Content-Type).
Self {
rx,
buf: b": connected\n\n".to_vec(),
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 {
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());
out[..n].copy_from_slice(&self.buf[self.pos..self.pos + n]);
self.pos += n;
return Ok(n);
}
if self.closed {
return Ok(0);
}
match self.rx.recv_timeout(Duration::from_secs(15)) {
Ok(json) => {
// One event per TCP write — smooth token UX. Do not coalesce large
// batches (that caused jumpy "dump then pause" in the Stream panel).
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) => {
self.closed = true;
return Ok(0);
}
}
}
}
}
fn respond_html(request: Request, body: String) -> Result<()> {
let header = Header::from_bytes(&b"Content-Type"[..], &b"text/html; charset=utf-8"[..])
.map_err(|_| anyhow::anyhow!("header"))?;
let resp = Response::from_string(body).with_header(header);
request.respond(resp).context("respond")?;
Ok(())
}
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_status_code(code)
.with_header(header);
request.respond(resp).context("respond")?;
Ok(())
}
fn open_url(url: &str) -> Result<()> {
#[cfg(target_os = "windows")]
{
Command::new("cmd")
.args(["/C", "start", "", url])
.spawn()
.context("open browser")?;
}
#[cfg(target_os = "macos")]
{
Command::new("open").arg(url).spawn().context("open browser")?;
}
#[cfg(all(unix, not(target_os = "macos")))]
{
Command::new("xdg-open")
.arg(url)
.spawn()
.context("open browser")?;
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::awaiting_preset_input;
#[test]
fn awaiting_input_when_no_stdin_source() {
let src = include_str!("../../tests/keywords/input.mq.md");
assert!(awaiting_preset_input(&["Name: ".into()], &[], src));
assert!(!awaiting_preset_input(&["Name: ".into()], &["Ada".into()], src));
}
#[test]
fn not_awaiting_when_frontmatter_stdin() {
let src = "---\nstdin: Ada\n---\n\n# main\n\n**n = > input**\n";
assert!(!awaiting_preset_input(&["p".into()], &[], src));
}
}