Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

refactor(webmcp): share form choice and checked-state handling

ldm00d4d90977e53
diff --git a/moli-renderer-v8/src/context_bootstrap/web_mcp/declarative/invocation.rs b/moli-renderer-v8/src/context_bootstrap/web_mcp/declarative/invocation.rs
index 92394d17d..660e75f21 100644
--- a/moli-renderer-v8/src/context_bootstrap/web_mcp/declarative/invocation.rs
+++ b/moli-renderer-v8/src/context_bootstrap/web_mcp/declarative/invocation.rs
@@ -81,13 +81,7 @@ pub(in crate::context_bootstrap::web_mcp) fn start(
)
}
Fill::Checked(handle, checked) => {
- let before = unsafe { &*host_ptr }
- .dom_host()
- .node(handle)
- .and_then(Node::as_element)
- .is_some_and(|element| element.checked());
- unsafe { &mut *host_ptr }.set_checked_state(scope, host_ptr, handle, checked);
- (handle, before != checked)
+ (handle, fill_checked(scope, host_ptr, handle, checked))
}
Fill::Checkable {
handle,
@@ -98,13 +92,7 @@ pub(in crate::context_bootstrap::web_mcp) fn start(
if radio && !checked {
continue;
}
- let before = unsafe { &*host_ptr }
- .dom_host()
- .node(handle)
- .and_then(Node::as_element)
- .is_some_and(|element| element.checked());
- unsafe { &mut *host_ptr }.set_checked_state(scope, host_ptr, handle, checked);
- (handle, before != checked)
+ (handle, fill_checked(scope, host_ptr, handle, checked))
}
Fill::Select(handle, selected_values) => {
let dom = unsafe { &*host_ptr }.dom_host();
@@ -188,6 +176,23 @@ pub(in crate::context_bootstrap::web_mcp) fn start(
}
}
+fn fill_checked(
+ scope: &mut v8::PinScope<'_, '_>,
+ host_ptr: *mut JsContextHost,
+ handle: DomHandle,
+ checked: bool,
+) -> bool {
+ // Native changes also track dirty checkedness and radio peers; only this
+ // control's checked value determines whether WebMCP dispatches input.
+ let before = unsafe { &*host_ptr }
+ .dom_host()
+ .node(handle)
+ .and_then(Node::as_element)
+ .is_some_and(|element| element.checked());
+ unsafe { &mut *host_ptr }.set_checked_state(scope, host_ptr, handle, checked);
+ before != checked
+}
+
fn form_is_current(host: &JsContextHost, id: u64, form: DomHandle) -> bool {
let store = &host.native_bridge().web_mcp;
let Some(pending) = store.pending.get(&id) else {
diff --git a/moli-webmcp/src/plan.rs b/moli-webmcp/src/plan.rs
index 1fb2f93b4..e3268b6e6 100644
--- a/moli-webmcp/src/plan.rs
+++ b/moli-webmcp/src/plan.rs
@@ -5,7 +5,7 @@ use std::sync::Arc;
use moli_dom::{
forms::{InputType, sanitize_input_value_for_type_with_multiple},
- native::{DomHost, NativeNodeId, Node},
+ native::{DomHost, NativeNodeId},
};
use serde_json::Value;
@@ -74,10 +74,7 @@ pub fn prepare_fill(
vec![scalar(value)?]
};
let options = dom.select_option_elements(first);
- let choices = options
- .iter()
- .map(|option| dom.option_value(*option).expect("select option"))
- .collect::<Vec<_>>();
+ let choices = schema::option_values(dom, &options);
if selected.iter().any(|value| !choices.contains(value)) {
return None;
}
@@ -97,17 +94,7 @@ pub fn prepare_fill(
} else {
vec![scalar(value)?]
};
- let choices = group
- .iter()
- .map(|handle| {
- dom.node(*handle)
- .and_then(Node::as_element)
- .expect("parameter control")
- .attribute("value")
- .unwrap_or("on")
- .to_owned()
- })
- .collect::<Vec<_>>();
+ let choices = schema::checkable_values(dom, group);
if selected.iter().any(|value| !choices.contains(value)) {
return None;
}
diff --git a/moli-webmcp/src/schema.rs b/moli-webmcp/src/schema.rs
index b2e757c6a..2cc32937b 100644
--- a/moli-webmcp/src/schema.rs
+++ b/moli-webmcp/src/schema.rs
@@ -120,6 +120,27 @@ pub(crate) fn parameter_element<'a>(
supported.then_some(element)
}
+pub(crate) fn option_values(dom: &DomHost, options: &[NativeNodeId]) -> Vec<String> {
+ options
+ .iter()
+ .map(|option| dom.option_value(*option).expect("select option"))
+ .collect()
+}
+
+pub(crate) fn checkable_values(dom: &DomHost, controls: &[NativeNodeId]) -> Vec<String> {
+ controls
+ .iter()
+ .map(|control| {
+ dom.node(*control)
+ .and_then(Node::as_element)
+ .expect("input control")
+ .attribute("value")
+ .unwrap_or("on")
+ .to_owned()
+ })
+ .collect()
+}
+
fn parameter_schema(
dom: &DomHost,
controls: &[NativeNodeId],
@@ -130,10 +151,7 @@ fn parameter_schema(
let mut fields = OrderedObject(indexmap::IndexMap::new());
if element.is_html_element("select") {
let options = dom.select_option_elements(controls[0]);
- let values = options
- .iter()
- .map(|option| dom.option_value(*option).expect("select option"))
- .collect::<Vec<_>>();
+ let values = option_values(dom, &options);
let alternatives = options
.iter()
.zip(&values)
@@ -147,17 +165,7 @@ fn parameter_schema(
match kind {
InputType::Checkbox if controls.len() == 1 => fields.put("type", "boolean"),
InputType::Checkbox | InputType::Radio => {
- let values = controls
- .iter()
- .map(|control| {
- dom.node(*control)
- .and_then(Node::as_element)
- .expect("input control")
- .attribute("value")
- .unwrap_or("on")
- .to_owned()
- })
- .collect::<Vec<_>>();
+ let values = checkable_values(dom, controls);
let alternatives = values
.iter()
.zip(controls)