Skip to main content

swarm_engine_core/orchestrator/
adapter.rs

1//! WorkResult を MutationInput に変換するアダプター
2
3use crate::exploration::mutation::ExplorationResult;
4use crate::exploration::{MapNodeId, MutationInput};
5
6/// WorkResult を MutationInput に変換するアダプター
7///
8/// Orchestrator 内で WorkResult::Acted を処理する際に使用。
9/// ExplorationSpaceV2 への入力として渡せる。
10pub struct WorkResultAdapter {
11    /// 対象ノード ID
12    pub node_id: MapNodeId,
13    /// アクション名
14    pub action_name: String,
15    /// ターゲット(Optional)
16    pub target: Option<String>,
17    /// アクション実行結果
18    pub result: ExplorationResult,
19}
20
21impl WorkResultAdapter {
22    /// 成功/失敗と discovery から ExplorationResult を構築
23    pub fn new(
24        node_id: MapNodeId,
25        action_name: &str,
26        target: Option<&str>,
27        success: bool,
28        discovery: Option<&serde_json::Value>,
29    ) -> Self {
30        let result = if success {
31            // discovery があれば Discover、なければ Success
32            if let Some(disc) = discovery {
33                // discovery が配列なら children として扱う
34                if let Some(arr) = disc.as_array() {
35                    let children: Vec<String> = arr
36                        .iter()
37                        .filter_map(|v| v.as_str().map(|s| s.to_string()))
38                        .collect();
39                    if !children.is_empty() {
40                        ExplorationResult::Discover(children)
41                    } else {
42                        ExplorationResult::Success
43                    }
44                } else {
45                    ExplorationResult::Success
46                }
47            } else {
48                ExplorationResult::Success
49            }
50        } else {
51            ExplorationResult::Fail(None)
52        };
53
54        Self {
55            node_id,
56            action_name: action_name.to_string(),
57            target: target.map(|s| s.to_string()),
58            result,
59        }
60    }
61}
62
63impl MutationInput for WorkResultAdapter {
64    fn node_id(&self) -> MapNodeId {
65        self.node_id
66    }
67
68    fn action_name(&self) -> &str {
69        &self.action_name
70    }
71
72    fn target(&self) -> Option<&str> {
73        self.target.as_deref()
74    }
75
76    fn result(&self) -> &ExplorationResult {
77        &self.result
78    }
79}