Skip to main content

zagens_core/engine/turn_loop/
tool_exec.rs

1//! Tool execution port for parallel/sequential paths in the turn loop (P2 PR4).
2
3use std::sync::Arc;
4
5use async_trait::async_trait;
6use serde_json::Value;
7use tokio::sync::{RwLock, mpsc};
8use zagens_tools::{ToolError, ToolResult};
9
10use crate::events::Event;
11
12use super::host::TurnLoopToolRegistry;
13
14/// Cloned handles passed into `async move` tool tasks.
15#[derive(Clone)]
16pub struct TurnLoopToolExec {
17    pub lock: Arc<RwLock<()>>,
18    pub tx_event: mpsc::Sender<Event>,
19}
20
21/// Executes registry/MCP tools for the turn loop (implemented by TUI `Engine` L2).
22#[async_trait]
23pub trait TurnLoopToolExecutor: Send + Sync {
24    type ToolRegistry: TurnLoopToolRegistry;
25
26    #[allow(clippy::too_many_arguments)]
27    async fn execute_with_lock(
28        &self,
29        exec: TurnLoopToolExec,
30        supports_parallel: bool,
31        interactive: bool,
32        tool_name: String,
33        tool_input: Value,
34        registry: Option<&Self::ToolRegistry>,
35        mcp_pool: Option<Arc<tokio::sync::Mutex<dyn McpPoolPort + Send + Sync>>>,
36        tool_progress_id: Option<String>,
37    ) -> Result<ToolResult, ToolError>;
38}
39
40/// Opaque MCP pool surface for the core turn loop.
41#[async_trait]
42pub trait McpPoolPort: Send + Sync {
43    async fn execute_tool(&self, tool_name: &str, input: Value) -> Result<ToolResult, ToolError>;
44}