|
| 1 | +// Extension data management for sessions |
| 2 | +// Provides a simple way to store extension-specific data with versioned keys |
| 3 | + |
| 4 | +use anyhow::Result; |
| 5 | +use serde::{Deserialize, Serialize}; |
| 6 | +use serde_json::Value; |
| 7 | +use std::collections::HashMap; |
| 8 | +use utoipa::ToSchema; |
| 9 | + |
| 10 | +/// Extension data containing all extension states |
| 11 | +/// Keys are in format "extension_name.version" (e.g., "todo.v0") |
| 12 | +#[derive(Debug, Clone, Serialize, Deserialize, Default, ToSchema)] |
| 13 | +pub struct ExtensionData { |
| 14 | + #[serde(flatten)] |
| 15 | + pub extension_states: HashMap<String, Value>, |
| 16 | +} |
| 17 | + |
| 18 | +impl ExtensionData { |
| 19 | + /// Create a new empty ExtensionData |
| 20 | + pub fn new() -> Self { |
| 21 | + Self { |
| 22 | + extension_states: HashMap::new(), |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + /// Get extension state for a specific extension and version |
| 27 | + pub fn get_extension_state(&self, extension_name: &str, version: &str) -> Option<&Value> { |
| 28 | + let key = format!("{}.{}", extension_name, version); |
| 29 | + self.extension_states.get(&key) |
| 30 | + } |
| 31 | + |
| 32 | + /// Set extension state for a specific extension and version |
| 33 | + pub fn set_extension_state(&mut self, extension_name: &str, version: &str, state: Value) { |
| 34 | + let key = format!("{}.{}", extension_name, version); |
| 35 | + self.extension_states.insert(key, state); |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +/// Helper trait for extension-specific state management |
| 40 | +pub trait ExtensionState: Sized + Serialize + for<'de> Deserialize<'de> { |
| 41 | + /// The name of the extension |
| 42 | + const EXTENSION_NAME: &'static str; |
| 43 | + |
| 44 | + /// The version of the extension state format |
| 45 | + const VERSION: &'static str; |
| 46 | + |
| 47 | + /// Convert from JSON value |
| 48 | + fn from_value(value: &Value) -> Result<Self> { |
| 49 | + serde_json::from_value(value.clone()).map_err(|e| { |
| 50 | + anyhow::anyhow!( |
| 51 | + "Failed to deserialize {} state: {}", |
| 52 | + Self::EXTENSION_NAME, |
| 53 | + e |
| 54 | + ) |
| 55 | + }) |
| 56 | + } |
| 57 | + |
| 58 | + /// Convert to JSON value |
| 59 | + fn to_value(&self) -> Result<Value> { |
| 60 | + serde_json::to_value(self).map_err(|e| { |
| 61 | + anyhow::anyhow!("Failed to serialize {} state: {}", Self::EXTENSION_NAME, e) |
| 62 | + }) |
| 63 | + } |
| 64 | + |
| 65 | + /// Get state from extension data |
| 66 | + fn from_extension_data(extension_data: &ExtensionData) -> Option<Self> { |
| 67 | + extension_data |
| 68 | + .get_extension_state(Self::EXTENSION_NAME, Self::VERSION) |
| 69 | + .and_then(|v| Self::from_value(v).ok()) |
| 70 | + } |
| 71 | + |
| 72 | + /// Save state to extension data |
| 73 | + fn to_extension_data(&self, extension_data: &mut ExtensionData) -> Result<()> { |
| 74 | + let value = self.to_value()?; |
| 75 | + extension_data.set_extension_state(Self::EXTENSION_NAME, Self::VERSION, value); |
| 76 | + Ok(()) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +/// TODO extension state implementation |
| 81 | +#[derive(Debug, Clone, Serialize, Deserialize)] |
| 82 | +pub struct TodoState { |
| 83 | + pub content: String, |
| 84 | +} |
| 85 | + |
| 86 | +impl ExtensionState for TodoState { |
| 87 | + const EXTENSION_NAME: &'static str = "todo"; |
| 88 | + const VERSION: &'static str = "v0"; |
| 89 | +} |
| 90 | + |
| 91 | +impl TodoState { |
| 92 | + /// Create a new TODO state |
| 93 | + pub fn new(content: String) -> Self { |
| 94 | + Self { content } |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +#[cfg(test)] |
| 99 | +mod tests { |
| 100 | + use super::*; |
| 101 | + use serde_json::json; |
| 102 | + |
| 103 | + #[test] |
| 104 | + fn test_extension_data_basic_operations() { |
| 105 | + let mut extension_data = ExtensionData::new(); |
| 106 | + |
| 107 | + // Test setting and getting extension state |
| 108 | + let todo_state = json!({"content": "- Task 1\n- Task 2"}); |
| 109 | + extension_data.set_extension_state("todo", "v0", todo_state.clone()); |
| 110 | + |
| 111 | + assert_eq!( |
| 112 | + extension_data.get_extension_state("todo", "v0"), |
| 113 | + Some(&todo_state) |
| 114 | + ); |
| 115 | + assert_eq!(extension_data.get_extension_state("todo", "v1"), None); |
| 116 | + } |
| 117 | + |
| 118 | + #[test] |
| 119 | + fn test_multiple_extension_states() { |
| 120 | + let mut extension_data = ExtensionData::new(); |
| 121 | + |
| 122 | + // Add multiple extension states |
| 123 | + extension_data.set_extension_state("todo", "v0", json!("TODO content")); |
| 124 | + extension_data.set_extension_state("memory", "v1", json!({"items": ["item1", "item2"]})); |
| 125 | + extension_data.set_extension_state("config", "v2", json!({"setting": true})); |
| 126 | + |
| 127 | + // Check all states exist |
| 128 | + assert_eq!(extension_data.extension_states.len(), 3); |
| 129 | + assert!(extension_data.get_extension_state("todo", "v0").is_some()); |
| 130 | + assert!(extension_data.get_extension_state("memory", "v1").is_some()); |
| 131 | + assert!(extension_data.get_extension_state("config", "v2").is_some()); |
| 132 | + } |
| 133 | + |
| 134 | + #[test] |
| 135 | + fn test_todo_state_trait() { |
| 136 | + let mut extension_data = ExtensionData::new(); |
| 137 | + |
| 138 | + // Create and save TODO state |
| 139 | + let todo = TodoState::new("- Task 1\n- Task 2".to_string()); |
| 140 | + todo.to_extension_data(&mut extension_data).unwrap(); |
| 141 | + |
| 142 | + // Retrieve TODO state |
| 143 | + let retrieved = TodoState::from_extension_data(&extension_data); |
| 144 | + assert!(retrieved.is_some()); |
| 145 | + assert_eq!(retrieved.unwrap().content, "- Task 1\n- Task 2"); |
| 146 | + } |
| 147 | + |
| 148 | + #[test] |
| 149 | + fn test_extension_data_serialization() { |
| 150 | + let mut extension_data = ExtensionData::new(); |
| 151 | + extension_data.set_extension_state("todo", "v0", json!("TODO content")); |
| 152 | + extension_data.set_extension_state("memory", "v1", json!({"key": "value"})); |
| 153 | + |
| 154 | + // Serialize to JSON |
| 155 | + let json = serde_json::to_value(&extension_data).unwrap(); |
| 156 | + |
| 157 | + // Check the structure |
| 158 | + assert!(json.is_object()); |
| 159 | + assert_eq!(json.get("todo.v0"), Some(&json!("TODO content"))); |
| 160 | + assert_eq!(json.get("memory.v1"), Some(&json!({"key": "value"}))); |
| 161 | + |
| 162 | + // Deserialize back |
| 163 | + let deserialized: ExtensionData = serde_json::from_value(json).unwrap(); |
| 164 | + assert_eq!( |
| 165 | + deserialized.get_extension_state("todo", "v0"), |
| 166 | + Some(&json!("TODO content")) |
| 167 | + ); |
| 168 | + assert_eq!( |
| 169 | + deserialized.get_extension_state("memory", "v1"), |
| 170 | + Some(&json!({"key": "value"})) |
| 171 | + ); |
| 172 | + } |
| 173 | +} |
0 commit comments