feat(view): add cell stdin for live and static runs
View gets a Cell stdin/input field wired to cell Run, Step and Run All. Stateless cells receive raw stdin; persistent kernels receive marqdo_input. Static export uses the document preset stdin and the same run_all path.
This commit is contained in:
1 parent
e0d34669e0
commit
9da12c9093
7 files changed
+118
-13
No files matched your search
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
title: lib/外联 — 外联内核 v2
|
||||
description: 结构化执行、JSON 调用、并行扇出、持久内核与显式状态桥
|
||||
导入 外联:lib/外联.mq.md
|
||||
stdin: 21
|
||||
---
|
||||
|
||||
# main
|
||||
|
||||
+61
-4
@@ -561,6 +561,15 @@ impl DocumentSession {
|
||||
}
|
||||
|
||||
pub fn run_cell(&mut self, cell_name: &str, kernel_override: Option<&str>) -> Result<Value, String> {
|
||||
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<Value, String> {
|
||||
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<Value, String> {
|
||||
self.step_with_stdin(None)
|
||||
}
|
||||
|
||||
pub fn step_with_stdin(&mut self, stdin: Option<&str>) -> Result<Value, String> {
|
||||
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<Value, String> {
|
||||
self.run_all_with_stdin(restart, None)
|
||||
}
|
||||
|
||||
pub fn run_all_with_stdin(
|
||||
&mut self,
|
||||
restart: bool,
|
||||
stdin: Option<&str>,
|
||||
) -> Result<Value, String> {
|
||||
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() {
|
||||
|
||||
+40
-3
@@ -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 {
|
||||
<button type="button" id="nb-theme" title="切换浅色/深色主题">浅色</button>
|
||||
<span id="nb-status">opening…</span>
|
||||
</div>
|
||||
<div class="nb-stdin-bar" id="nb-stdin-bar">
|
||||
<label for="nb-stdin">Cell stdin / input</label>
|
||||
<textarea id="nb-stdin" rows="1" placeholder="raw stdin for stateless cells; JSON/string for persistent kernels" spellcheck="false"></textarea>
|
||||
</div>
|
||||
<div class="notebook-bar" id="nb-state-actions" hidden>
|
||||
<span>Bridge</span>
|
||||
<input id="nb-kernel-input" placeholder="kernel" autocomplete="off"/>
|
||||
@@ -3521,6 +3552,10 @@ body.notebook-page .stdin-form button {
|
||||
}).then(function (r) { return r.json(); });
|
||||
}
|
||||
function status(t) { var e = document.getElementById("nb-status"); if (e) e.textContent = t; }
|
||||
function cellStdin() {
|
||||
var el = document.getElementById("nb-stdin");
|
||||
return el ? el.value : "";
|
||||
}
|
||||
function card(name) {
|
||||
var els = document.querySelectorAll(".code-card");
|
||||
for (var i = 0; i < els.length; i++) {
|
||||
@@ -3610,7 +3645,7 @@ body.notebook-page .stdin-form button {
|
||||
});
|
||||
}
|
||||
function runOne(name) {
|
||||
return post("api/session/cell/run", { cell: name }).then(function (r) {
|
||||
return post("api/session/cell/run", { cell: name, stdin: cellStdin() }).then(function (r) {
|
||||
if (r.ok) { renderRecord(name, r.record); if (r.session) renderSummary(r.session); }
|
||||
else status(r.error || "run failed");
|
||||
});
|
||||
@@ -3634,7 +3669,7 @@ body.notebook-page .stdin-form button {
|
||||
}).catch(function (e) { status(String(e)); });
|
||||
return;
|
||||
}
|
||||
post("api/session/run_all", { restart: !!restart }).then(function (r) {
|
||||
post("api/session/run_all", { restart: !!restart, stdin: cellStdin() }).then(function (r) {
|
||||
if (!r.ok) { status(r.error || "run all failed"); return; }
|
||||
if (r.session) renderSummary(r.session);
|
||||
var recs = (r.summary && r.summary.records) || [];
|
||||
@@ -3696,6 +3731,8 @@ body.notebook-page .stdin-form button {
|
||||
if (!live) {
|
||||
var staticBar = document.getElementById("notebook-bar");
|
||||
if (staticBar) staticBar.hidden = false;
|
||||
var staticStdin = document.getElementById("nb-stdin-bar");
|
||||
if (staticStdin) staticStdin.hidden = false;
|
||||
function staticNotice() {
|
||||
status("静态页面无法真实执行 / static export: no live execution");
|
||||
}
|
||||
@@ -3719,7 +3756,7 @@ body.notebook-page .stdin-form button {
|
||||
status("no cells; Run All runs the document");
|
||||
return;
|
||||
}
|
||||
post("api/session/cell/step", {}).then(function (r) {
|
||||
post("api/session/cell/step", { stdin: cellStdin() }).then(function (r) {
|
||||
if (!r.ok) { status(r.error || "step failed"); return; }
|
||||
if (r.record) {
|
||||
if (r.record.cell) renderRecord(r.record.cell, r.record);
|
||||
|
||||
@@ -178,20 +178,24 @@ pub fn api_cell_run(root: &Path, body: &str) -> serde_json::Value {
|
||||
let v = body_value(body);
|
||||
let cell = v.get("cell").and_then(|x| x.as_str()).unwrap_or("");
|
||||
let kernel = v.get("kernel").and_then(|x| x.as_str()).filter(|s| !s.is_empty());
|
||||
let stdin = v.get("stdin").and_then(|x| x.as_str());
|
||||
if cell.is_empty() {
|
||||
return err("missing cell".into());
|
||||
}
|
||||
with_session(root, body, "record", |s| s.run_cell(cell, kernel))
|
||||
with_session(root, body, "record", |s| s.run_cell_with_stdin(cell, kernel, stdin))
|
||||
}
|
||||
|
||||
pub fn api_cell_step(root: &Path, body: &str) -> serde_json::Value {
|
||||
with_session(root, body, "record", |s| s.step())
|
||||
let v = body_value(body);
|
||||
let stdin = v.get("stdin").and_then(|x| x.as_str());
|
||||
with_session(root, body, "record", |s| s.step_with_stdin(stdin))
|
||||
}
|
||||
|
||||
pub fn api_run_all(root: &Path, body: &str) -> serde_json::Value {
|
||||
let v = body_value(body);
|
||||
let restart = v.get("restart").and_then(|x| x.as_bool()).unwrap_or(false);
|
||||
with_session(root, body, "summary", |s| s.run_all(restart))
|
||||
let stdin = v.get("stdin").and_then(|x| x.as_str());
|
||||
with_session(root, body, "summary", |s| s.run_all_with_stdin(restart, stdin))
|
||||
}
|
||||
|
||||
pub fn api_kernel_restart(root: &Path, body: &str) -> serde_json::Value {
|
||||
|
||||
+7
-3
@@ -50,12 +50,16 @@ fn record_field(record: &Value, key: &str) -> Option<Value> {
|
||||
|
||||
/// Run the document's cells at export time and keep their real outputs. This
|
||||
/// is what makes a static page show foreign results instead of just source.
|
||||
fn static_cell_outputs(root: &ViewRoot, abs: &std::path::Path) -> HashMap<String, StaticCellOutput> {
|
||||
fn static_cell_outputs(
|
||||
root: &ViewRoot,
|
||||
abs: &std::path::Path,
|
||||
stdin: &str,
|
||||
) -> HashMap<String, StaticCellOutput> {
|
||||
let mut out = HashMap::new();
|
||||
let Ok(mut session) = DocumentSession::open(abs, &root.root) else {
|
||||
return out;
|
||||
};
|
||||
let Ok(summary) = session.run_all(false) else {
|
||||
let Ok(summary) = session.run_all_with_stdin(false, Some(stdin)) else {
|
||||
session.close();
|
||||
return out;
|
||||
};
|
||||
@@ -185,7 +189,7 @@ pub fn write_static(opts: OutputOptions) -> Result<()> {
|
||||
let cell_outputs = if opts.no_exec {
|
||||
HashMap::new()
|
||||
} else {
|
||||
static_cell_outputs(&root, &abs)
|
||||
static_cell_outputs(&root, &abs, &vm.preset_stdin)
|
||||
};
|
||||
let html = inject_cell_outputs(
|
||||
&page_file(&root.files, &rel, &vm, &links),
|
||||
|
||||
Reference in new issue
Block a user