|
| 1 | +use tauri::{ipc::Channel, AppHandle, Manager, State}; |
| 2 | +use uuid::Uuid; |
| 3 | + |
| 4 | +use crate::commands::events::ChannelEventBus; |
| 5 | +use crate::runtime::blocks::handler::BlockOutput; |
| 6 | +use crate::runtime::blocks::registry::BlockRegistry; |
| 7 | +use crate::runtime::blocks::Block; |
| 8 | +use crate::runtime::workflow::context_builder::ContextBuilder; |
| 9 | +use crate::state::AtuinState; |
| 10 | + |
| 11 | +/// Convert editor document block to runtime Block enum |
| 12 | +fn document_to_block(block_data: &serde_json::Value) -> Result<Block, String> { |
| 13 | + Block::from_document(block_data) |
| 14 | +} |
| 15 | + |
| 16 | +#[tauri::command] |
| 17 | +pub async fn execute_block( |
| 18 | + state: State<'_, AtuinState>, |
| 19 | + app_handle: AppHandle, |
| 20 | + block_id: String, |
| 21 | + runbook_id: String, |
| 22 | + editor_document: Vec<serde_json::Value>, |
| 23 | + output_channel: Channel<BlockOutput>, |
| 24 | +) -> Result<String, String> { |
| 25 | + // Build execution context |
| 26 | + let mut context = ContextBuilder::build_context(&block_id, &editor_document, &runbook_id) |
| 27 | + .await |
| 28 | + .map_err(|e| e.to_string())?; |
| 29 | + |
| 30 | + // Add SSH pool to context |
| 31 | + context.ssh_pool = Some(state.ssh_pool()); |
| 32 | + |
| 33 | + // Add output storage to context |
| 34 | + context.output_storage = Some(state.runbook_output_variables.clone()); |
| 35 | + |
| 36 | + // Add PTY store to context |
| 37 | + context.pty_store = Some(state.pty_store()); |
| 38 | + |
| 39 | + // Add event bus to context |
| 40 | + let gc_sender = state.gc_event_sender(); |
| 41 | + let event_bus = std::sync::Arc::new(ChannelEventBus::new(gc_sender)); |
| 42 | + context.event_bus = Some(event_bus); |
| 43 | + |
| 44 | + // Find the block in the document |
| 45 | + let block_data = editor_document |
| 46 | + .iter() |
| 47 | + .find(|b| b.get("id").and_then(|v| v.as_str()) == Some(&block_id)) |
| 48 | + .ok_or("Block not found")?; |
| 49 | + |
| 50 | + // Convert document block to runtime block |
| 51 | + let block = document_to_block(block_data)?; |
| 52 | + |
| 53 | + // Get event sender from state |
| 54 | + let event_sender = state.event_sender(); |
| 55 | + |
| 56 | + // Create registry and execute |
| 57 | + let registry = BlockRegistry::new(); |
| 58 | + |
| 59 | + match registry |
| 60 | + .execute_block(&block, context, event_sender, Some(output_channel)) |
| 61 | + .await |
| 62 | + { |
| 63 | + Ok(handle) => { |
| 64 | + let execution_id = handle.id; |
| 65 | + // Store the execution handle for cancellation |
| 66 | + if let Some(state) = app_handle.try_state::<AtuinState>() { |
| 67 | + state |
| 68 | + .block_executions |
| 69 | + .write() |
| 70 | + .await |
| 71 | + .insert(execution_id, handle.clone()); |
| 72 | + } |
| 73 | + Ok(execution_id.to_string()) |
| 74 | + } |
| 75 | + Err(e) => Err(format!("Execution failed: {}", e)), |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +#[tauri::command] |
| 80 | +pub async fn cancel_block_execution( |
| 81 | + app_handle: AppHandle, |
| 82 | + execution_id: String, |
| 83 | +) -> Result<(), String> { |
| 84 | + let execution_uuid = Uuid::parse_str(&execution_id).map_err(|e| e.to_string())?; |
| 85 | + |
| 86 | + if let Some(state) = app_handle.try_state::<AtuinState>() { |
| 87 | + let mut executions = state.block_executions.write().await; |
| 88 | + if let Some(handle) = executions.remove(&execution_uuid) { |
| 89 | + // Cancel the execution |
| 90 | + handle.cancellation_token.cancel(); |
| 91 | + Ok(()) |
| 92 | + } else { |
| 93 | + Err("Execution not found".to_string()) |
| 94 | + } |
| 95 | + } else { |
| 96 | + Err("State not available".to_string()) |
| 97 | + } |
| 98 | +} |
0 commit comments