clm-kernel 0.0.20

Cubical Logic Model (CLM) sovereign execution engine, CPN-VM, and Cordis spatiotemporal substrate
Documentation
//! Pluggable Presentation Membrane — Sprint 364
//!
//! Provides the presentation membrane abstractions and lifecycle management,
//! decoupling execution kernels from human interactive presentation surfaces (TUI, Web, Chatbot).

use serde::{Deserialize, Serialize};
use serde_json::Value;

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct MembraneEvent {
    pub event_type: String,
    pub timestamp: String,
    pub payload: Value,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct MembraneAction {
    pub action_type: String,
    pub payload: Value,
}

impl MembraneAction {
    pub fn user_prompt(prompt: &str) -> Self {
        Self {
            action_type: "user_prompt".to_string(),
            payload: serde_json::json!({ "prompt": prompt }),
        }
    }

    pub fn select_card(card_hash: &str) -> Self {
        Self {
            action_type: "select_card".to_string(),
            payload: serde_json::json!({ "card_hash": card_hash }),
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ActionResult {
    pub success: bool,
    pub message: Option<String>,
    pub emitted_cards: Vec<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct MembraneSession {
    pub session_id: String,
    pub user_did: String,
    pub active_panel: String,
    pub history: Vec<MembraneEvent>,
}

impl MembraneSession {
    pub fn new(session_id: &str, user_did: &str) -> Self {
        Self {
            session_id: session_id.to_string(),
            user_did: user_did.to_string(),
            active_panel: "main".to_string(),
            history: Vec::new(),
        }
    }

    pub fn push_event(&mut self, event_type: &str, payload: Value, timestamp: &str) {
        self.history.push(MembraneEvent {
            event_type: event_type.to_string(),
            timestamp: timestamp.to_string(),
            payload,
        });
    }
}

pub struct PresentationMembrane {
    pub membrane_id: String,
    pub membrane_type: String,
}

impl PresentationMembrane {
    pub fn new(membrane_id: &str, membrane_type: &str) -> Self {
        Self {
            membrane_id: membrane_id.to_string(),
            membrane_type: membrane_type.to_string(),
        }
    }

    pub fn handle_action(
        &self,
        session: &mut MembraneSession,
        action: MembraneAction,
        timestamp: &str,
    ) -> ActionResult {
        match action.action_type.as_str() {
            "user_prompt" => {
                let prompt = action
                    .payload
                    .get("prompt")
                    .and_then(|v| v.as_str())
                    .unwrap_or("");
                let card_hash = crate::layer0::hash::compute_blake3(prompt.as_bytes());
                session.push_event(
                    "prompt_received",
                    serde_json::json!({ "prompt": prompt, "card_hash": card_hash }),
                    timestamp,
                );
                ActionResult {
                    success: true,
                    message: Some("Prompt dispatched to CPN VM".to_string()),
                    emitted_cards: vec![card_hash],
                }
            }
            "select_card" => {
                let hash = action
                    .payload
                    .get("card_hash")
                    .and_then(|v| v.as_str())
                    .unwrap_or("");
                session.active_panel = hash.to_string();
                session.push_event(
                    "card_selected",
                    serde_json::json!({ "card_hash": hash }),
                    timestamp,
                );
                ActionResult {
                    success: true,
                    message: Some(format!("Card {} selected", hash)),
                    emitted_cards: vec![hash.to_string()],
                }
            }
            _ => ActionResult {
                success: false,
                message: Some(format!("Unknown action: {}", action.action_type)),
                emitted_cards: vec![],
            },
        }
    }
}