Skip to main content

merry_tools/
registry.rs

1use crate::{
2    config::{WorkspaceToolConfigError, WorkspaceToolsConfig},
3    patch::{self, ApplyPatchExecutor},
4    read::{self, ReadFileExecutor},
5    state::WorkspaceToolState,
6};
7use merry_core::ToolSpec;
8use merry_runtime::{RegisteredTool, ToolActionKind};
9use std::sync::Arc;
10
11/// Built-in filesystem tools with validated, session-stable definitions.
12#[derive(Debug, Clone)]
13pub struct WorkspaceTools {
14    pub(crate) state: Arc<WorkspaceToolState>,
15    read_spec: ToolSpec,
16    patch_spec: ToolSpec,
17}
18
19impl WorkspaceTools {
20    /// Validates paths, resource limits, and both built-in tool definitions.
21    pub fn new(config: WorkspaceToolsConfig) -> Result<Self, WorkspaceToolConfigError> {
22        let state = WorkspaceToolState::new(config)?;
23        let read_spec = read::spec(&state.limits)?;
24        let patch_spec = patch::spec(&state.limits)?;
25        Ok(Self {
26            state: Arc::new(state),
27            read_spec,
28            patch_spec,
29        })
30    }
31
32    /// Returns the bounded file-read tool (text and images) with parallel-safe admission.
33    #[must_use]
34    pub fn into_registered_tools(self) -> Vec<RegisteredTool> {
35        vec![register_read(self.read_spec, self.state)]
36    }
37
38    /// Adds opt-in patches with explicit write admission and proposal evidence.
39    #[must_use]
40    pub fn into_registered_tools_with_patch(self) -> Vec<RegisteredTool> {
41        vec![
42            register_read(self.read_spec, Arc::clone(&self.state)),
43            RegisteredTool::new(
44                self.patch_spec,
45                Arc::new(ApplyPatchExecutor { state: self.state }),
46                ToolActionKind::WorkspaceWrite,
47            )
48            .with_low_risk_workspace_patch_lane(),
49        ]
50    }
51}
52
53fn register_read(spec: ToolSpec, state: Arc<WorkspaceToolState>) -> RegisteredTool {
54    RegisteredTool::read_only(spec, Arc::new(ReadFileExecutor { state }))
55        .with_parallel_safe_execution()
56}