Execute a command with plugin hooks (async version)
(
&self,
name: &str,
ctx: CommandContext,
)
| 181 | |
| 182 | /// Execute a command with plugin hooks (async version) |
| 183 | pub async fn execute_with_hooks( |
| 184 | &self, |
| 185 | name: &str, |
| 186 | ctx: CommandContext, |
| 187 | ) -> anyhow::Result<String> { |
| 188 | let command = self |
| 189 | .commands |
| 190 | .get(name) |
| 191 | .ok_or_else(|| anyhow::anyhow!("Command not found: {}", name))?; |
| 192 | |
| 193 | let mut rendered = self.render_template(&command.template, ctx.clone()); |
| 194 | |
| 195 | // Plugin hook: command.execute.before |
| 196 | let hook_outputs = opencode_plugin::trigger_collect( |
| 197 | HookContext::new(HookEvent::CommandExecuteBefore) |
| 198 | .with_data("command", serde_json::json!(name)) |
| 199 | .with_data("source", serde_json::json!(format!("{:?}", command.source))) |
| 200 | .with_data("arguments", serde_json::json!(ctx.arguments.join(" "))) |
| 201 | .with_data( |
| 202 | "parts", |
| 203 | serde_json::json!([{ |
| 204 | "type": "text", |
| 205 | "text": rendered |
| 206 | }]), |
| 207 | ), |
| 208 | ) |
| 209 | .await; |
| 210 | for output in hook_outputs { |
| 211 | let Some(payload) = output.payload.as_ref() else { |
| 212 | continue; |
| 213 | }; |
| 214 | apply_command_hook_payload(&mut rendered, payload); |
| 215 | } |
| 216 | |
| 217 | Ok(rendered) |
| 218 | } |
| 219 | |
| 220 | fn render_template(&self, template: &str, ctx: CommandContext) -> String { |
| 221 | let mut result = template.to_string(); |
nothing calls this directly
no test coverage detected