use crate::layer0::membrane_events::{
SESSION_ACTION_SELECT_CARD, SESSION_ACTION_USER_PROMPT, SESSION_EVENT_CARD_SELECTED,
SESSION_EVENT_PROMPT_RECEIVED,
};
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: SESSION_ACTION_USER_PROMPT.to_string(),
payload: serde_json::json!({ "prompt": prompt }),
}
}
pub fn select_card(card_hash: &str) -> Self {
Self {
action_type: SESSION_ACTION_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() {
SESSION_ACTION_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(
SESSION_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],
}
}
SESSION_ACTION_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(
SESSION_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![],
},
}
}
}