(
&self,
tool_id: &str,
args: serde_json::Value,
ctx: ToolContext,
)
| 84 | } |
| 85 | |
| 86 | pub async fn execute( |
| 87 | &self, |
| 88 | tool_id: &str, |
| 89 | args: serde_json::Value, |
| 90 | ctx: ToolContext, |
| 91 | ) -> Result<ToolResult, ToolError> { |
| 92 | let tool = match self.get(tool_id).await { |
| 93 | Some(t) => t, |
| 94 | None => { |
| 95 | let suggestions = self.suggest_tools(tool_id).await; |
| 96 | return Err(ToolError::InvalidArguments(format!( |
| 97 | "Tool '{}' not found in registry. Available tools: {}", |
| 98 | tool_id, |
| 99 | suggestions.join(", ") |
| 100 | ))); |
| 101 | } |
| 102 | }; |
| 103 | |
| 104 | let mut args = args; |
| 105 | // Plugin hook: tool.execute.before |
| 106 | let mut before_hook_ctx = HookContext::new(HookEvent::ToolExecuteBefore) |
| 107 | .with_session(&ctx.session_id) |
| 108 | .with_data("tool", serde_json::json!(tool_id)) |
| 109 | .with_data("args", args.clone()); |
| 110 | if let Some(call_id) = &ctx.call_id { |
| 111 | before_hook_ctx = before_hook_ctx.with_data("call_id", serde_json::json!(call_id)); |
| 112 | } |
| 113 | let before_outputs = opencode_plugin::trigger_collect(before_hook_ctx).await; |
| 114 | for output in before_outputs { |
| 115 | if let Some(payload) = output.payload.as_ref() { |
| 116 | apply_tool_before_payload(&mut args, payload); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | tool.validate(&args)?; |
| 121 | let mut result = tool.execute(args.clone(), ctx.clone()).await; |
| 122 | |
| 123 | // Plugin hook: tool.execute.after |
| 124 | let mut hook_ctx = HookContext::new(HookEvent::ToolExecuteAfter) |
| 125 | .with_session(&ctx.session_id) |
| 126 | .with_data("tool", serde_json::json!(tool_id)) |
| 127 | .with_data("args", args); |
| 128 | if let Some(call_id) = &ctx.call_id { |
| 129 | hook_ctx = hook_ctx.with_data("call_id", serde_json::json!(call_id)); |
| 130 | } |
| 131 | |
| 132 | hook_ctx = match &result { |
| 133 | Ok(r) => hook_ctx |
| 134 | .with_data("title", serde_json::json!(&r.title)) |
| 135 | .with_data("output", serde_json::json!(&r.output)) |
| 136 | .with_data("metadata", serde_json::json!(&r.metadata)) |
| 137 | .with_data("error", serde_json::json!(false)), |
| 138 | Err(e) => hook_ctx |
| 139 | .with_data("output", serde_json::json!(e.to_string())) |
| 140 | .with_data("error", serde_json::json!(true)), |
| 141 | }; |
| 142 | |
| 143 | let after_outputs = opencode_plugin::trigger_collect(hook_ctx).await; |
nothing calls this directly
no test coverage detected