orchflow_core/manager/
execution.rs

1use super::handlers;
2use super::{Action, Manager};
3use crate::error::Result;
4use serde_json::Value;
5
6/// Execute an action through the action channel
7pub async fn execute_action(manager: &Manager, action: Action) -> Result<Value> {
8    let (tx, mut rx) = tokio::sync::mpsc::channel(1);
9
10    manager
11        .action_tx
12        .send((action, tx))
13        .await
14        .map_err(|_| crate::error::OrchflowError::General("Failed to send action".to_string()))?;
15
16    rx.recv()
17        .await
18        .ok_or_else(|| crate::error::OrchflowError::General("No response received".to_string()))?
19}
20
21/// Process an action directly
22pub async fn process_action(manager: &Manager, action: Action) -> Result<Value> {
23    match action {
24        // Session management
25        Action::CreateSession { name } => handlers::session::create_session(manager, name).await,
26
27        Action::DeleteSession { session_id } => {
28            handlers::session::delete_session(manager, session_id).await
29        }
30
31        Action::SaveSession { session_id, name } => {
32            // For now, just return success since session persistence is handled by state manager
33            Ok(serde_json::json!({
34                "status": "ok",
35                "session_id": session_id,
36                "name": name
37            }))
38        }
39
40        // Pane management
41        Action::CreatePane {
42            session_id,
43            pane_type,
44            command,
45            shell_type,
46            name,
47        } => {
48            handlers::pane::create_pane(manager, session_id, pane_type, command, shell_type, name)
49                .await
50        }
51
52        Action::ClosePane { pane_id } => handlers::pane::close_pane(manager, pane_id).await,
53
54        Action::ResizePane {
55            pane_id,
56            width,
57            height,
58        } => handlers::pane::resize_pane(manager, &pane_id, width, height).await,
59
60        Action::RenamePane { pane_id, name } => {
61            handlers::pane::rename_pane(manager, &pane_id, &name).await
62        }
63
64        // File management
65        Action::CreateFile { path, content } => {
66            handlers::file::create_file(manager, &path, content.as_deref()).await
67        }
68
69        Action::OpenFile { path } => handlers::file::open_file(manager, &path).await,
70
71        Action::CreateDirectory { path } => handlers::file::create_directory(manager, &path).await,
72
73        Action::DeletePath { path, permanent } => {
74            handlers::file::delete_path(manager, &path, permanent).await
75        }
76
77        Action::RenamePath { old_path, new_name } => {
78            handlers::file::rename_path(manager, &old_path, &new_name).await
79        }
80
81        Action::CopyPath {
82            source,
83            destination,
84        } => handlers::file::copy_path(manager, &source, &destination).await,
85
86        Action::MovePath {
87            source,
88            destination,
89        } => handlers::file::move_path(manager, &source, &destination).await,
90
91        Action::MoveFiles { files, destination } => {
92            handlers::file::move_files(manager, &files, &destination).await
93        }
94
95        Action::CopyFiles { files, destination } => {
96            handlers::file::copy_files(manager, &files, &destination).await
97        }
98
99        Action::GetFileTree { path, max_depth } => {
100            handlers::file::get_file_tree(manager, path.as_deref(), max_depth).await
101        }
102
103        Action::SearchFiles { pattern, path } => {
104            handlers::file::search_files(manager, &pattern, path.as_deref()).await
105        }
106
107        // Search operations
108        Action::SearchProject { pattern, options } => {
109            handlers::search::search_project(manager, &pattern, options).await
110        }
111
112        Action::SearchInFile { file_path, pattern } => {
113            handlers::search::search_in_file(manager, &file_path, &pattern).await
114        }
115
116        // Terminal operations
117        Action::SendKeys { pane_id, keys } => {
118            handlers::terminal::send_keys(manager, &pane_id, &keys).await
119        }
120
121        Action::RunCommand { pane_id, command } => {
122            handlers::terminal::run_command(manager, &pane_id, &command).await
123        }
124
125        Action::GetPaneOutput { pane_id, lines } => {
126            handlers::terminal::get_pane_output(manager, &pane_id, lines).await
127        }
128
129        // Plugin management
130        Action::LoadPlugin { id: _, config: _ } => Err(crate::error::OrchflowError::General(
131            "Plugin loading must be done through Manager::load_plugin".to_string(),
132        )),
133
134        Action::UnloadPlugin { id } => {
135            manager.unload_plugin(&id).await?;
136            Ok(serde_json::json!({
137                "status": "ok",
138                "plugin_id": id,
139                "unloaded": true
140            }))
141        }
142    }
143}