feat(foreign): runtime plan updates and generation

Add explicit add/remove cell and set-value operations with a plan generation counter. Expose /api/session/plan/{assign,add,remove,value}.
This commit is contained in:
cflmy committed 2026-10-18 14:43:26 +08:00
1 parent e5e532d83f
commit 0e407b2bd3
3 files changed
+149

No files matched your search

+81
View File
@@ -89,6 +89,7 @@ pub struct DocumentSession {
pub journal: Vec<JournalRecord>,
pub value_store: HashMap<String, Value>,
pub cursor: usize,
pub generation: u64,
pub opened_ms: i64,
host: HostContext,
kernel_sessions: HashMap<String, String>,
@@ -440,6 +441,7 @@ impl DocumentSession {
journal: Vec::new(),
value_store: HashMap::new(),
cursor: 0,
generation: 1,
opened_ms: now_ms(),
host,
kernel_sessions: HashMap::new(),
@@ -718,6 +720,7 @@ impl DocumentSession {
return Ok(Value::Map(vec![
("done".into(), Value::Bool(true)),
("cursor".into(), Value::Int(self.cursor as i64)),
("generation".into(), Value::Int(self.generation as i64)),
]));
}
let name = self.order[self.cursor].clone();
@@ -863,9 +866,69 @@ impl DocumentSession {
self.cells[ci].kernel = kernel.to_string();
self.cells[ci].listed = true;
self.cells[ci].status = CellStatus::Dirty;
self.generation += 1;
Ok(Value::None)
}
pub fn add_cell(
&mut self,
cell: &str,
lang: &str,
source: &str,
kernel: &str,
) -> Result<Value, String> {
if self.cell_index(cell).is_some() {
return Err(format!("cell `{cell}` already exists"));
}
let lang = registry::canonical(lang);
let decl = self
.kernel(kernel)
.ok_or_else(|| format!("unknown kernel `{kernel}`"))?
.clone();
if registry::canonical(&decl.lang) != lang {
return Err(format!(
"cell `{cell}` ({lang}) cannot use kernel `{kernel}` ({})",
decl.lang
));
}
self.cells.push(Cell {
name: cell.to_string(),
lang,
source: source.to_string(),
line_start: 0,
line_end: 0,
source_hash: hash_source(source),
kernel: kernel.to_string(),
stdin: String::new(),
listed: true,
status: CellStatus::Never,
runs: 0,
last_ms: 0,
last_ok: false,
last_error: String::new(),
});
self.order.push(cell.to_string());
self.generation += 1;
Ok(Value::Bool(true))
}
pub fn remove_cell(&mut self, cell: &str) -> Result<Value, String> {
let Some(i) = self.cell_index(cell) else {
return Err(format!("unknown cell `{cell}`"));
};
self.cells.remove(i);
self.order.retain(|name| name != cell);
self.journal.retain(|j| j.cell != cell);
self.generation += 1;
Ok(Value::Bool(true))
}
pub fn set_value(&mut self, kernel: &str, name: &str, value: Value) -> Result<Value, String> {
let out = self.push_value(kernel, name, value)?;
self.generation += 1;
Ok(out)
}
pub fn state_list(&mut self) -> Result<Value, String> {
let marqdo = self
.value_store
@@ -967,6 +1030,7 @@ impl DocumentSession {
self.order.iter().cloned().map(Value::Text).collect(),
)),
("cursor".into(), Value::Int(self.cursor as i64)),
("generation".into(), Value::Int(self.generation as i64)),
("journal".into(), Value::List(
self.journal
.iter()
@@ -1393,6 +1457,23 @@ console.log(globalThis.kept);
let _ = std::fs::remove_file(&path);
}
#[test]
fn runtime_plan_add_remove_and_generation() {
let root = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"));
let path = root.join("tests/notebook/shared.mq.md");
let mut s = DocumentSession::open(&path, &root).unwrap();
let before = s.generation;
s.add_cell("extra", "node", "console.log('extra');", "分析")
.unwrap();
assert!(s.generation > before);
assert!(s.cells.iter().any(|c| c.name == "extra"));
s.assign_kernel("extra", "分析").unwrap();
s.remove_cell("extra").unwrap();
assert!(!s.cells.iter().any(|c| c.name == "extra"));
assert!(s.generation > before + 1);
s.close();
}
#[test]
fn plan_inputs_feed_each_cell() {
let root = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"));
+20
View File
@@ -194,6 +194,26 @@ fn handle(root: &ViewRoot, request: Request) -> Result<()> {
notebook_api::api_run_document(&root.root, body)
})
}
"/api/session/plan/assign" => {
return api_debug_post(request, |body| {
notebook_api::api_plan_assign(&root.root, body)
})
}
"/api/session/plan/add" => {
return api_debug_post(request, |body| {
notebook_api::api_plan_add(&root.root, body)
})
}
"/api/session/plan/remove" => {
return api_debug_post(request, |body| {
notebook_api::api_plan_remove(&root.root, body)
})
}
"/api/session/plan/value" => {
return api_debug_post(request, |body| {
notebook_api::api_plan_value(&root.root, body)
})
}
"/api/session/interrupt" => {
return api_debug_post(request, |body| {
notebook_api::api_interrupt(&root.root, body)
+48
View File
@@ -225,6 +225,54 @@ pub fn api_kernel_restart(root: &Path, body: &str) -> serde_json::Value {
with_session(root, body, "value", |s| s.restart_kernel(kernel))
}
pub fn api_plan_assign(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()).unwrap_or("");
if cell.is_empty() || kernel.is_empty() {
return err("missing cell/kernel".into());
}
with_session(root, body, "value", |s| s.assign_kernel(cell, kernel))
}
pub fn api_plan_add(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 lang = v.get("lang").and_then(|x| x.as_str()).unwrap_or("");
let kernel = v.get("kernel").and_then(|x| x.as_str()).unwrap_or("");
let source = v.get("source").and_then(|x| x.as_str()).unwrap_or("");
if cell.is_empty() || lang.is_empty() || kernel.is_empty() {
return err("missing cell/lang/kernel".into());
}
with_session(root, body, "value", |s| s.add_cell(cell, lang, source, kernel))
}
pub fn api_plan_remove(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("");
if cell.is_empty() {
return err("missing cell".into());
}
with_session(root, body, "value", |s| s.remove_cell(cell))
}
pub fn api_plan_value(root: &Path, body: &str) -> serde_json::Value {
let v = body_value(body);
let kernel = v.get("kernel").and_then(|x| x.as_str()).unwrap_or("");
let name = v.get("name").and_then(|x| x.as_str()).unwrap_or("");
if kernel.is_empty() || name.is_empty() {
return err("missing kernel/name".into());
}
let value = match v.get("value") {
Some(v) => match json_to_value(v) {
Ok(v) => v,
Err(e) => return err(e),
},
None => Value::None,
};
with_session(root, body, "value", |s| s.set_value(kernel, name, value))
}
pub fn api_assign(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("");