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