|
| 1 | +use anyhow::Result; |
| 2 | +use serde_json::Value; |
| 3 | +use crate::tools::Tool; |
| 4 | +use async_trait::async_trait; |
| 5 | +use crate::sandbox::cape::CapeClient; |
| 6 | + |
| 7 | +pub struct CapeSubmitter; |
| 8 | + |
| 9 | +#[async_trait] |
| 10 | +impl Tool for CapeSubmitter { |
| 11 | + fn name(&self) -> &str { "cape_submit" } |
| 12 | + fn description(&self) -> &str { "Submits a file to CAPEv2 sandbox for analysis. Args: file_path, base_url, machine (optional), timeout (optional)" } |
| 13 | + |
| 14 | + async fn execute(&self, args: Value) -> Result<Value> { |
| 15 | + let file_path = args["file_path"].as_str().ok_or(anyhow::anyhow!("Missing file_path"))?; |
| 16 | + let base_url = args["base_url"].as_str().ok_or(anyhow::anyhow!("Missing base_url"))?; |
| 17 | + let machine = args["machine"].as_str(); |
| 18 | + let timeout = args["timeout"].as_u64().unwrap_or(300); // 5 mins default |
| 19 | + |
| 20 | + let client = CapeClient::new(base_url, ""); // Token support can be added if args provide it |
| 21 | + |
| 22 | + tracing::info!("Submitting {} to CAPE at {}", file_path, base_url); |
| 23 | + let task_id = client.submit_file(file_path, machine).await?; |
| 24 | + |
| 25 | + tracing::info!("File submitted. Task ID: {}. Waiting for analysis...", task_id); |
| 26 | + |
| 27 | + // Wait logic |
| 28 | + match client.wait_for_analysis(task_id, timeout).await { |
| 29 | + Ok(status) => { |
| 30 | + tracing::info!("Analysis completed with status: {}", status); |
| 31 | + |
| 32 | + // Fetch report |
| 33 | + let report = client.get_report(task_id).await?; |
| 34 | + |
| 35 | + // Return a simplified summary or full report? |
| 36 | + // Full report is huge. Let's return the full JSON and let the Agent parse/filter. |
| 37 | + // But add our own metadata wrapper. |
| 38 | + Ok(serde_json::json!({ |
| 39 | + "status": "analysis_finished", |
| 40 | + "task_id": task_id, |
| 41 | + "cape_status": status, |
| 42 | + "report": report |
| 43 | + })) |
| 44 | + }, |
| 45 | + Err(e) => { |
| 46 | + Err(anyhow::anyhow!("Analysis timeout or failed: {}", e)) |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | +} |
0 commit comments