Skip to main content

core_api/node/
mod.rs

1use serde::{Deserialize, Serialize};
2
3/// Node operations use the Full App result contract. Presentation stays at the App boundary.
4pub use conversation_api::execution::{
5    FacadeProblem as OperationProblem, FacadeResponse as OperationResult,
6};
7
8pub mod contracts;
9pub mod plugin;
10pub mod settings;
11pub mod status;
12
13pub const V1: &str = "v1";
14pub const STATUS_CONTRACT: &str = "mhome.node.status.v1";
15pub const PROTOCOL_V1_MANIFEST: &str = include_str!("../../contract/node-runtime-protocol-v1.json");
16
17#[derive(Debug, Clone, Serialize, Deserialize)]
18#[serde(rename_all = "camelCase", deny_unknown_fields)]
19pub struct RuntimeRequest<T> {
20    pub version: String,
21    pub payload: T,
22}
23
24#[derive(Debug, Clone, Serialize, Deserialize)]
25#[serde(rename_all = "camelCase", deny_unknown_fields)]
26pub struct RuntimeResponse {
27    pub version: String,
28    pub result: OperationResult,
29}
30
31/// Reject presentation-only error data at the Node boundary. A successful string
32/// is legitimate business data and must never be interpreted as Essential YAML.
33pub fn validate_operation_result(result: &OperationResult) -> Result<(), &'static str> {
34    if matches!(result, OperationResult::Error { data: Some(_), .. }) {
35        return Err("Node operations require Full error details, not Essential data");
36    }
37    Ok(())
38}
39
40pub fn require_version(actual: &str, expected: &str) -> Result<(), UnsupportedVersion> {
41    if actual == expected {
42        Ok(())
43    } else {
44        Err(UnsupportedVersion {
45            actual: actual.to_string(),
46            expected: expected.to_string(),
47        })
48    }
49}
50
51#[derive(Debug, Clone, PartialEq, Eq)]
52pub struct UnsupportedVersion {
53    actual: String,
54    expected: String,
55}
56
57impl std::fmt::Display for UnsupportedVersion {
58    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
59        write!(
60            formatter,
61            "UNSUPPORTED_NODE_PROTOCOL_VERSION: requested version {}, supported version {}",
62            self.actual, self.expected
63        )
64    }
65}
66
67impl std::error::Error for UnsupportedVersion {}
68
69#[cfg(test)]
70mod tests {
71    use super::*;
72
73    #[test]
74    fn runtime_response_requires_explicit_full_results() {
75        use serde_json::json;
76        for result in [
77            json!({"status":"success","data":[]}),
78            json!({"status":"success"}),
79            json!({"status":"partial","data":[],"warnings":[{"code":"INCOMPLETE","message":"retry"}]}),
80            json!({"status":"error","error":{"code":"PLUGIN_BUSY","message":"retry","details":{"after":2}}}),
81            json!({"status":"cancelled"}),
82        ] {
83            let value = json!({"version":"v1","result":result});
84            let response: RuntimeResponse = serde_json::from_value(value.clone()).unwrap();
85            validate_operation_result(&response.result).unwrap();
86            assert_eq!(serde_json::to_value(response).unwrap(), value);
87        }
88        assert!(
89            serde_json::from_value::<RuntimeResponse>(json!({"version":"v1","data":[]})).is_err()
90        );
91        let result: OperationResult = serde_json::from_value(
92            json!({"status":"error","error":{"code":"X","message":"x"},"data":"yaml"}),
93        )
94        .unwrap();
95        assert!(validate_operation_result(&result).is_err());
96    }
97
98    #[test]
99    fn manifest_matches_internal_targets() {
100        let manifest: serde_json::Value = serde_json::from_str(PROTOCOL_V1_MANIFEST).unwrap();
101        assert_eq!(manifest["version"], V1);
102        assert_eq!(manifest["status"]["contract"], STATUS_CONTRACT);
103        assert_eq!(manifest["status"]["queryTarget"], status::STATUS_TARGET);
104        assert_eq!(
105            manifest["status"]["changedTarget"],
106            status::STATUS_CHANGED_TARGET
107        );
108        assert_eq!(
109            manifest["settings"]["changedTarget"],
110            settings::CHANGED_TARGET
111        );
112        assert_eq!(
113            manifest["llm"]["completeTarget"],
114            llm_api::service::COMPLETE_TARGET
115        );
116        assert_eq!(
117            manifest["llm"]["ttsSynthesizeTarget"],
118            contracts::llm::TTS_SYNTHESIZE_TARGET
119        );
120    }
121}