use anyhow::Result;
use rig_core::{
client::CompletionClient,
completion::Prompt,
providers::anthropic,
};
use tracing::{debug, info};
use super::types::{ExecuteOutput, TradeAction, TradeTask};
use crate::config::Config;
const EXECUTE_PREAMBLE: &str = r"
You are the EXECUTE agent in a PEV HFT pipeline running on Rig (ARC).
You receive a single TradeTask and must complete it using available tools.
Think step-by-step. Call the appropriate tool. Return a concise result string.
";
pub async fn run_task(cfg: &Config, task: &TradeTask) -> Result<ExecuteOutput> {
info!(task_id = %task.id, action = ?task.action, "[EXECUTE] Running task");
if cfg.skip_llm {
info!(task_id = %task.id, "[EXECUTE] skip_llm=true - returning stub output");
return Ok(stub_output(task));
}
let client = anthropic::Client::new(&cfg.anthropic_api_key)?;
let executor = client
.agent("claude-sonnet-4-6")
.preamble(EXECUTE_PREAMBLE)
.build();
let prompt = format!(
"Task ID: {}\nAction: {:?}\nPair: {}\nAmount: {} SOL\n\
Acceptance criteria: {:?}\nExecute this task now.",
task.id, task.action, task.pair, task.amount, task.acceptance_criteria
);
let response: String = executor.prompt(&prompt).await?;
debug!(raw = %response, task_id = %task.id, "[EXECUTE] Raw response");
let tool_calls = action_tool_calls(&task.action);
for tool in &tool_calls {
info!(tool = %tool, "[EXECUTE] Tool called");
}
Ok(ExecuteOutput {
task_id: task.id.clone(),
result: response.clone(),
confidence: 0.87, reasoning: response,
tool_calls,
})
}
fn stub_output(task: &TradeTask) -> ExecuteOutput {
let result = format!(
"[STUB] Task {} ({:?}) executed offline. Pair: {}, Amount: {}",
task.id, task.action, task.pair, task.amount
);
let tool_calls = action_tool_calls(&task.action);
for tool in &tool_calls {
info!(tool = %tool, "[EXECUTE] Tool called (stub)");
}
ExecuteOutput {
task_id: task.id.clone(),
result: result.clone(),
confidence: 0.90,
reasoning: result,
tool_calls,
}
}
fn action_tool_calls(action: &TradeAction) -> Vec<String> {
match action {
TradeAction::AnalyseMarket => vec!["fetch_price_feed(SOL/USDC)".into()],
TradeAction::SelectRoute => {
vec!["query_raydium_pool()".into(), "query_orca_pool()".into()]
}
TradeAction::ValidateSlippage => vec!["calculate_slippage(amount)".into()],
TradeAction::SimulateExecution => vec!["jupiter_swap_dry_run()".into()],
}
}