Execute a tool by name with an external context
(
&self,
name: &str,
args: &serde_json::Value,
ctx: &ToolContext,
)
| 198 | |
| 199 | /// Execute a tool by name with an external context |
| 200 | pub async fn execute_with_context( |
| 201 | &self, |
| 202 | name: &str, |
| 203 | args: &serde_json::Value, |
| 204 | ctx: &ToolContext, |
| 205 | ) -> Result<ToolResult> { |
| 206 | let start = std::time::Instant::now(); |
| 207 | |
| 208 | let tool = self.get(name); |
| 209 | |
| 210 | let result = match tool { |
| 211 | Some(tool) => { |
| 212 | let mut output = tool.execute(args, ctx).await?; |
| 213 | let original_content = output.content.clone(); |
| 214 | let truncated = truncate_tool_output_with_artifact(name, &output.content); |
| 215 | output.content = truncated.content; |
| 216 | if let Some(artifact) = truncated.artifact { |
| 217 | self.store_tool_artifact(name, &original_content, &artifact); |
| 218 | output.metadata = Some(merge_tool_output_artifact_metadata( |
| 219 | output.metadata, |
| 220 | &artifact, |
| 221 | )); |
| 222 | } |
| 223 | Ok(ToolResult { |
| 224 | name: name.to_string(), |
| 225 | output: output.content, |
| 226 | exit_code: if output.success { 0 } else { 1 }, |
| 227 | metadata: output.metadata, |
| 228 | images: output.images, |
| 229 | error_kind: output.error_kind, |
| 230 | }) |
| 231 | } |
| 232 | None => Ok(ToolResult::error(name, format!("Unknown tool: {}", name))), |
| 233 | }; |
| 234 | |
| 235 | if let Ok(ref r) = result { |
| 236 | crate::telemetry::record_tool_result(r.exit_code, start.elapsed()); |
| 237 | self.record_trace_event(name, r, start.elapsed()); |
| 238 | } |
| 239 | |
| 240 | result |
| 241 | } |
| 242 | |
| 243 | /// Execute a tool and return raw output using the registry's default context |
| 244 | pub async fn execute_raw( |