|
| 1 | +use std::path::Path; |
| 2 | + |
| 3 | +use pu_core::paths; |
| 4 | +use pu_core::protocol::{AgentDefInfo, Response}; |
| 5 | + |
| 6 | +use super::super::Engine; |
| 7 | + |
| 8 | +impl Engine { |
| 9 | + pub(in crate::engine) async fn handle_list_agent_defs(&self, project_root: &str) -> Response { |
| 10 | + let pr = project_root.to_string(); |
| 11 | + match tokio::task::spawn_blocking(move || { |
| 12 | + let root = Path::new(&pr); |
| 13 | + let defs = pu_core::agent_def::list_agent_defs(root); |
| 14 | + let infos: Vec<AgentDefInfo> = defs |
| 15 | + .into_iter() |
| 16 | + .map(|d| AgentDefInfo { |
| 17 | + name: d.name, |
| 18 | + agent_type: d.agent_type, |
| 19 | + template: d.template, |
| 20 | + inline_prompt: d.inline_prompt, |
| 21 | + tags: d.tags, |
| 22 | + scope: d.scope, |
| 23 | + available_in_command_dialog: d.available_in_command_dialog, |
| 24 | + icon: d.icon, |
| 25 | + command: d.command, |
| 26 | + }) |
| 27 | + .collect(); |
| 28 | + infos |
| 29 | + }) |
| 30 | + .await |
| 31 | + { |
| 32 | + Ok(agent_defs) => Response::AgentDefList { agent_defs }, |
| 33 | + Err(e) => Response::Error { |
| 34 | + code: "INTERNAL_ERROR".into(), |
| 35 | + message: format!("task join error: {e}"), |
| 36 | + }, |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + pub(in crate::engine) async fn handle_get_agent_def( |
| 41 | + &self, |
| 42 | + project_root: &str, |
| 43 | + name: &str, |
| 44 | + ) -> Response { |
| 45 | + let pr = project_root.to_string(); |
| 46 | + let n = name.to_string(); |
| 47 | + match tokio::task::spawn_blocking(move || { |
| 48 | + pu_core::agent_def::find_agent_def(Path::new(&pr), &n) |
| 49 | + }) |
| 50 | + .await |
| 51 | + { |
| 52 | + Ok(Some(d)) => Response::AgentDefDetail { |
| 53 | + name: d.name, |
| 54 | + agent_type: d.agent_type, |
| 55 | + template: d.template, |
| 56 | + inline_prompt: d.inline_prompt, |
| 57 | + tags: d.tags, |
| 58 | + scope: d.scope, |
| 59 | + available_in_command_dialog: d.available_in_command_dialog, |
| 60 | + icon: d.icon, |
| 61 | + command: d.command, |
| 62 | + }, |
| 63 | + Ok(None) => Response::Error { |
| 64 | + code: "NOT_FOUND".into(), |
| 65 | + message: format!("agent def '{name}' not found"), |
| 66 | + }, |
| 67 | + Err(e) => Response::Error { |
| 68 | + code: "INTERNAL_ERROR".into(), |
| 69 | + message: format!("task join error: {e}"), |
| 70 | + }, |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + #[allow(clippy::too_many_arguments)] |
| 75 | + pub(in crate::engine) async fn handle_save_agent_def( |
| 76 | + &self, |
| 77 | + project_root: &str, |
| 78 | + name: &str, |
| 79 | + agent_type: &str, |
| 80 | + template: Option<String>, |
| 81 | + inline_prompt: Option<String>, |
| 82 | + tags: Vec<String>, |
| 83 | + scope: &str, |
| 84 | + available_in_command_dialog: bool, |
| 85 | + icon: Option<String>, |
| 86 | + command: Option<String>, |
| 87 | + ) -> Response { |
| 88 | + let dir = match Self::resolve_scope_dir( |
| 89 | + project_root, |
| 90 | + scope, |
| 91 | + paths::agents_dir, |
| 92 | + paths::global_agents_dir, |
| 93 | + ) { |
| 94 | + Ok(d) => d, |
| 95 | + Err(msg) => { |
| 96 | + return Response::Error { |
| 97 | + code: "IO_ERROR".into(), |
| 98 | + message: msg, |
| 99 | + }; |
| 100 | + } |
| 101 | + }; |
| 102 | + let def = pu_core::agent_def::AgentDef { |
| 103 | + name: name.to_string(), |
| 104 | + agent_type: agent_type.to_string(), |
| 105 | + template, |
| 106 | + inline_prompt, |
| 107 | + tags, |
| 108 | + scope: scope.to_string(), |
| 109 | + available_in_command_dialog, |
| 110 | + icon, |
| 111 | + command, |
| 112 | + }; |
| 113 | + match tokio::task::spawn_blocking(move || pu_core::agent_def::save_agent_def(&dir, &def)) |
| 114 | + .await |
| 115 | + { |
| 116 | + Ok(Ok(())) => Response::Ok, |
| 117 | + Ok(Err(e)) => Response::Error { |
| 118 | + code: "IO_ERROR".into(), |
| 119 | + message: format!("failed to save agent def: {e}"), |
| 120 | + }, |
| 121 | + Err(e) => Response::Error { |
| 122 | + code: "INTERNAL_ERROR".into(), |
| 123 | + message: format!("task join error: {e}"), |
| 124 | + }, |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + pub(in crate::engine) async fn handle_delete_agent_def( |
| 129 | + &self, |
| 130 | + project_root: &str, |
| 131 | + name: &str, |
| 132 | + scope: &str, |
| 133 | + ) -> Response { |
| 134 | + let dir = match Self::resolve_scope_dir( |
| 135 | + project_root, |
| 136 | + scope, |
| 137 | + paths::agents_dir, |
| 138 | + paths::global_agents_dir, |
| 139 | + ) { |
| 140 | + Ok(d) => d, |
| 141 | + Err(msg) => { |
| 142 | + return Response::Error { |
| 143 | + code: "IO_ERROR".into(), |
| 144 | + message: msg, |
| 145 | + }; |
| 146 | + } |
| 147 | + }; |
| 148 | + let n = name.to_string(); |
| 149 | + match tokio::task::spawn_blocking(move || pu_core::agent_def::delete_agent_def(&dir, &n)) |
| 150 | + .await |
| 151 | + { |
| 152 | + Ok(Ok(_)) => Response::Ok, |
| 153 | + Ok(Err(e)) => Response::Error { |
| 154 | + code: "IO_ERROR".into(), |
| 155 | + message: format!("failed to delete agent def: {e}"), |
| 156 | + }, |
| 157 | + Err(e) => Response::Error { |
| 158 | + code: "INTERNAL_ERROR".into(), |
| 159 | + message: format!("task join error: {e}"), |
| 160 | + }, |
| 161 | + } |
| 162 | + } |
| 163 | +} |
0 commit comments