Skip to main content

omega_cognitive_kernel/llm/
mod.rs

1#[allow(unused_imports)]
2use crate::prelude::*;
3
4use alloc::collections::BTreeSet;
5use serde::{Deserialize, Serialize};
6
7pub mod schema;
8pub use schema::{parse_suggestion_json, SchemaResult};
9
10#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
11pub struct LlmProposal {
12    pub action: String,
13    pub decision: String,
14    pub rationale: String,
15    pub write_authorized: bool,
16}
17
18#[derive(Debug, Clone)]
19pub struct LlmBridge {
20    pub allowed_actions: BTreeSet<String>,
21}
22
23impl LlmBridge {
24    pub fn new<I, S>(actions: I) -> Self
25    where
26        I: IntoIterator<Item = S>,
27        S: Into<String>,
28    {
29        Self {
30            allowed_actions: actions.into_iter().map(Into::into).collect(),
31        }
32    }
33
34    pub fn validate_proposal(&self, action: &str, rationale: &str) -> LlmProposal {
35        let permitted = self.allowed_actions.contains(action);
36        LlmProposal {
37            action: if permitted {
38                action.to_string()
39            } else {
40                "observe".into()
41            },
42            decision: "HOLD".into(),
43            rationale: rationale.to_string(),
44            write_authorized: false,
45        }
46    }
47
48    pub fn validate_json_proposal(&self, raw_json: &str) -> SchemaResult {
49        parse_suggestion_json(raw_json, &self.allowed_actions)
50    }
51}
52
53#[cfg(test)]
54mod tests {
55    use super::*;
56
57    #[test]
58    fn llm_bridge_forces_hold_and_filters_unknown_actions() {
59        let bridge = LlmBridge::new(["measure", "observe"]);
60        let blocked = bridge.validate_proposal("rm_rf", "unsafe");
61        assert_eq!(blocked.action, "observe");
62        assert_eq!(blocked.decision, "HOLD");
63        assert!(!blocked.write_authorized);
64    }
65}