Skip to content

armgit / cua

Public Imported from GitHub
0

Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.

perf(cua-driver): build the tool contract manifest once per process (#5977)

Georgios Konstantopoulos6a48bfc0ade6
diff --git a/libs/cua-driver/rust/crates/cua-driver-contract/src/lib.rs b/libs/cua-driver/rust/crates/cua-driver-contract/src/lib.rs
index d412e1a4ca..c24a241d20 100644
--- a/libs/cua-driver/rust/crates/cua-driver-contract/src/lib.rs
+++ b/libs/cua-driver/rust/crates/cua-driver-contract/src/lib.rs
@@ -224,7 +224,18 @@ pub struct ContractManifest {
pub tools: Vec<ToolContract>,
}
+/// The published contract. Built once per process: every schema in it comes
+/// from compiled Rust types, so rebuilding it can only repeat the same work.
pub fn manifest() -> ContractManifest {
+ cached_manifest().clone()
+}
+
+fn cached_manifest() -> &'static ContractManifest {
+ static MANIFEST: OnceLock<ContractManifest> = OnceLock::new();
+ MANIFEST.get_or_init(build_manifest)
+}
+
+fn build_manifest() -> ContractManifest {
let mut tools = session::contracts();
tools.extend(desktop::contracts());
tools.extend(cursor_tools::contracts());
@@ -244,7 +255,14 @@ pub fn manifest() -> ContractManifest {
}
pub fn tool_contract(name: &str) -> Option<ToolContract> {
- manifest().tools.into_iter().find(|tool| tool.name == name)
+ cached_tool_contract(name).cloned()
+}
+
+fn cached_tool_contract(name: &str) -> Option<&'static ToolContract> {
+ cached_manifest()
+ .tools
+ .iter()
+ .find(|tool| tool.name == name)
}
#[derive(Debug)]
@@ -258,9 +276,10 @@ struct ToolIndexEntry {
fn tool_index() -> &'static BTreeMap<String, ToolIndexEntry> {
static INDEX: OnceLock<BTreeMap<String, ToolIndexEntry>> = OnceLock::new();
INDEX.get_or_init(|| {
- manifest()
+ cached_manifest()
.tools
- .into_iter()
+ .iter()
+ .cloned()
.map(|tool| {
let input_fields = tool
.input_schema
@@ -302,7 +321,7 @@ pub fn tool_input_fields(name: &str) -> Option<&'static BTreeSet<String>> {
/// MCP tool. Runtime-only tools can define a narrow shared schema here without
/// committing every generated SDK to their broader platform-specific shape.
pub fn tool_success_output_schema(name: &str) -> Option<Value> {
- tool_contract(name).and_then(|contract| contract.success_output_schema)
+ cached_tool_contract(name).and_then(|contract| contract.success_output_schema.clone())
}
/// The `outputSchema` one tool advertises on `tools/list`, or `None` when it
@@ -315,7 +334,12 @@ pub fn tool_success_output_schema(name: &str) -> Option<Value> {
/// schema.
pub fn advertised_tool_output_schema(name: &str) -> Option<Value> {
let success = if is_action_result_tool(name) {
- Some(<ActionResult as ToolOutput>::output_schema())
+ static ACTION_RESULT_SCHEMA: OnceLock<Value> = OnceLock::new();
+ Some(
+ ACTION_RESULT_SCHEMA
+ .get_or_init(<ActionResult as ToolOutput>::output_schema)
+ .clone(),
+ )
} else {
tool_success_output_schema(name)
};