diff --git a/Cargo.lock b/Cargo.lock index 55588c503..3626bf564 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1368,6 +1368,14 @@ dependencies = [ "thiserror 2.0.21", ] +[[package]] +name = "craft-jsonrpc" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=bc186ca719958fcce3b6ee24794277eabed14609#bc186ca719958fcce3b6ee24794277eabed14609" +dependencies = [ + "serde_json", +] + [[package]] name = "craft-lottie" version = "0.1.0" @@ -1990,6 +1998,7 @@ dependencies = [ name = "effectcraft-automation" version = "0.24.1" dependencies = [ + "craft-jsonrpc", "craft-mime", "effectcraft-engine", "effectcraft-host", diff --git a/Cargo.toml b/Cargo.toml index 9afe10920..84926a587 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,6 +15,7 @@ craft-timeline = { git = "https://github.com/storytold/craft-libs", rev = "0d6dd craft-mime = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87" } craft-pdf = { git = "https://github.com/storytold/craft-libs", rev = "808730f49b9637b7909fe2ae56f9d63fdb100cae", version = "=0.1.0", default-features = false, features = ["read"] } craft-docpath = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87" } +craft-jsonrpc = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "bc186ca719958fcce3b6ee24794277eabed14609" } craft-quit = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87" } craft-ecproj-format = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87" } craft-dpx = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87" } diff --git a/crates/automation/Cargo.toml b/crates/automation/Cargo.toml index 23cff63c8..c8fbd2389 100644 --- a/crates/automation/Cargo.toml +++ b/crates/automation/Cargo.toml @@ -10,6 +10,7 @@ rust-version.workspace = true workspace = true [dependencies] +craft-jsonrpc = { workspace = true } craft-mime = { workspace = true } effectcraft-engine = { workspace = true } serde_json = { workspace = true } diff --git a/crates/automation/src/server.rs b/crates/automation/src/server.rs index 99b7ac616..67d523f54 100644 --- a/crates/automation/src/server.rs +++ b/crates/automation/src/server.rs @@ -17,7 +17,6 @@ const INSTRUCTIONS: &str = "EffectCraft is an After Effects-class motion graphic // JSON-RPC error codes. const PARSE_ERROR: i64 = -32700; -const INVALID_REQUEST: i64 = -32600; const METHOD_NOT_FOUND: i64 = -32601; const INVALID_PARAMS: i64 = -32602; const INTERNAL_ERROR: i64 = -32603; @@ -61,31 +60,16 @@ impl McpServer { /// Handle one decoded message (request, notification or batch); `None` when no reply is due. pub fn handle(&mut self, msg: &Value) -> Option { - if let Value::Array(batch) = msg { - if batch.is_empty() { - return Some(error(Value::Null, INVALID_REQUEST, "empty batch")); - } - let out: Vec = batch.iter().filter_map(|m| self.handle(m)).collect(); - return (!out.is_empty()).then_some(Value::Array(out)); - } - let Some(method) = msg.get("method").and_then(Value::as_str) else { - // A response from the client (we send no requests) or garbage. - return msg - .get("id") - .filter(|_| msg.get("result").is_none() && msg.get("error").is_none()) - .map(|id| error(id.clone(), INVALID_REQUEST, "missing `method`")); - }; - let Some(id) = msg.get("id").cloned() else { - return None; // notification (notifications/initialized, notifications/cancelled, ...) - }; - let params = msg.get("params").cloned().unwrap_or(json!({})); - Some(match self.request(method, ¶ms) { - Ok(mut result) => { - modern_result_fields(method, ¶ms, &mut result); - json!({"jsonrpc": "2.0", "id": id, "result": result}) - } - Err((code, m)) => error(id, code, &m), - }) + craft_jsonrpc::process_legacy( + msg, + |req| { + let params = req.params.cloned().unwrap_or_else(|| json!({})); + let mut result = self.request(req.method, ¶ms).map_err(|(code, message)| craft_jsonrpc::RpcError::new(code, message))?; + modern_result_fields(req.method, ¶ms, &mut result); + Ok(result) + }, + |_| {}, + ) } /// Handle one line of input; `None` when no reply is due. @@ -304,5 +288,5 @@ pub(crate) fn modern_result_fields(method: &str, params: &Value, result: &mut Va } fn error(id: Value, code: i64, message: &str) -> Value { - json!({"jsonrpc": "2.0", "id": id, "error": {"code": code, "message": message}}) + craft_jsonrpc::error_response(id, craft_jsonrpc::RpcError::new(code, message)) }