diff --git a/CHANGELOG.md b/CHANGELOG.md index d4241fb..f7fcf8c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ ### Added +- **view Cell stdin**:view 增加 `Cell stdin / input` 输入框,Run/Step/Run All 都会传给外联 cells;无状态 cell 使用 raw stdin,持久内核使用 `marqdo_input`;静态导出使用 frontmatter `stdin` 作为构建时输入。 - **foreign 拥有视图会话**:新增 `foreign.session kernels= cells=`,返回 foreign 会话句柄;`view` 只消费 `会话` 绑定,不再识别 `内核表` / `会话表` 保留名。没有 `会话` 时,view cells 使用无状态一次性执行,不创建内核。清除输出已接通 `/api/session/clear`,普通 view 与 foreign cells 共用,并由 Execution 面板的 Clear 按钮清除页面输出;无 `会话` 的 view 在 Run All 时执行整个文档并回写 Execution(不再空转);静态导出与动态 view 共用同一套 View 外壳和按钮布局;静态页面执行按钮点击显示“静态页面无法真实执行”,不伪造成功;静态导出与 view 的 Run All 共用同一 foreign_doc `run_all` 路径,成功/失败行为一致;构建时实际执行代码单元并把 stdout/stderr 注入代码卡,失败也显示真实错误;移除 `In []` / `Out []` 与 Notebook 品牌;没有新增 CLI 或第二个交互模式。 ### Fixed diff --git a/public/stdlib/foreign.mq.md b/public/stdlib/foreign.mq.md index f7969e5..d82aef5 100644 --- a/public/stdlib/foreign.mq.md +++ b/public/stdlib/foreign.mq.md @@ -2,6 +2,7 @@ title: lib/foreign — foreign kernels v2 description: Structured execution, JSON calls, parallel fan-out, persistent kernels and an explicit state bridge import foreign:lib/foreign.mq.md +stdin: 21 --- # main diff --git a/public/stdlib/外联.mq.md b/public/stdlib/外联.mq.md index 17ab8c3..e30d33b 100644 --- a/public/stdlib/外联.mq.md +++ b/public/stdlib/外联.mq.md @@ -2,6 +2,7 @@ title: lib/外联 — 外联内核 v2 description: 结构化执行、JSON 调用、并行扇出、持久内核与显式状态桥 导入 外联:lib/外联.mq.md +stdin: 21 --- # main diff --git a/src/host/foreign_doc.rs b/src/host/foreign_doc.rs index f462360..60852ea 100644 --- a/src/host/foreign_doc.rs +++ b/src/host/foreign_doc.rs @@ -561,6 +561,15 @@ impl DocumentSession { } pub fn run_cell(&mut self, cell_name: &str, kernel_override: Option<&str>) -> Result { + self.run_cell_with_stdin(cell_name, kernel_override, None) + } + + pub fn run_cell_with_stdin( + &mut self, + cell_name: &str, + kernel_override: Option<&str>, + stdin: Option<&str>, + ) -> Result { let ci = self .cell_index(cell_name) .ok_or_else(|| format!("unknown cell `{cell_name}`"))?; @@ -577,7 +586,7 @@ impl DocumentSession { &lang, &source, None, - None, + stdin, None, ); return match result { @@ -624,7 +633,7 @@ impl DocumentSession { &Value::Text(id), None, Some(&source), - None, + stdin.map(|s| Value::Text(s.to_string())).as_ref(), None, )?; self.record_result(cell_name, &kernel, &record)?; @@ -637,6 +646,10 @@ impl DocumentSession { } pub fn step(&mut self) -> Result { + self.step_with_stdin(None) + } + + pub fn step_with_stdin(&mut self, stdin: Option<&str>) -> Result { if self.cursor >= self.order.len() { return Ok(Value::Map(vec![ ("done".into(), Value::Bool(true)), @@ -644,7 +657,7 @@ impl DocumentSession { ])); } let name = self.order[self.cursor].clone(); - let record = self.run_cell(&name, None)?; + let record = self.run_cell_with_stdin(&name, None, stdin)?; let ci = self.cell_index(&name).unwrap_or(0); let kernel = self.cells[ci].kernel.clone(); self.cursor += 1; @@ -659,6 +672,14 @@ impl DocumentSession { } pub fn run_all(&mut self, restart: bool) -> Result { + self.run_all_with_stdin(restart, None) + } + + pub fn run_all_with_stdin( + &mut self, + restart: bool, + stdin: Option<&str>, + ) -> Result { if restart { self.restart_all()?; } @@ -666,7 +687,7 @@ impl DocumentSession { let mut records = Vec::new(); let mut failed = 0i64; for name in self.order.clone() { - let record = match self.run_cell(&name, None) { + let record = match self.run_cell_with_stdin(&name, None, stdin) { Ok(record) => record, Err(error) => { if let Some(i) = self.cell_index(&name) { @@ -1264,6 +1285,42 @@ console.log(globalThis.kept); let _ = std::fs::remove_file(&path); } + #[test] + fn stateless_cell_accepts_stdin() { + if !node_available() { + return; + } + let root = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")); + let path = root.join(format!( + "target/view-stdin-{}-{}.mq.md", + std::process::id(), + std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .map(|d| d.as_nanos()) + .unwrap_or(0) + )); + let source = r#"# main + +`a` = +```node +const fs = require("fs"); +const input = fs.readFileSync(0, "utf8").trim(); +console.log("got:" + input); +``` +"#; + std::fs::write(&path, source).unwrap(); + let mut s = DocumentSession::open(&path, &root).unwrap(); + let record = s + .run_cell_with_stdin("a", None, Some("hello stdin")) + .unwrap(); + assert_eq!( + field(&record, "stdout").and_then(|v| v.reveal_text().map(|s| s.to_string())), + Some("got:hello stdin\n".to_string()) + ); + s.close(); + let _ = std::fs::remove_file(&path); + } + #[test] fn run_all_continues_after_a_cell_error() { if !node_available() { diff --git a/src/view/html.rs b/src/view/html.rs index 3238163..529e40e 100644 --- a/src/view/html.rs +++ b/src/view/html.rs @@ -3060,6 +3060,33 @@ body.notebook-page #notebook-bar strong { color: var(--nb-strong); font: 600 0.76rem/1.6 var(--sans); } +body.notebook-page .nb-stdin-bar { + display: flex; + align-items: center; + gap: 0.55rem; + margin: 0 0 0.75rem; + padding: 0.4rem 0.55rem; + border: 1px solid var(--nb-border); + border-radius: var(--nb-radius); + background: var(--nb-surface); +} +body.notebook-page .nb-stdin-bar label { + color: var(--nb-muted); + font: 0.7rem/1.2 var(--mono); + white-space: nowrap; +} +body.notebook-page .nb-stdin-bar textarea { + flex: 1; + min-height: 2.1rem; + max-height: 7rem; + resize: vertical; + border: 1px solid var(--nb-border); + border-radius: 6px; + padding: 0.3rem 0.5rem; + background: var(--nb-bg); + color: var(--nb-text); + font: 0.72rem/1.35 var(--mono); +} body.notebook-page #notebook-bar button { appearance: none; padding: 0.34rem 0.58rem; @@ -3482,6 +3509,10 @@ body.notebook-page .stdin-form button { opening… +
+ + +