1use std::{
2 io,
3 path::{Path, PathBuf},
4 sync::Arc,
5};
6
7use merry_core::HostPathPolicy;
8use merry_runtime::DurableMemory;
9use thiserror::Error;
10
11#[derive(Debug, Clone, PartialEq, Eq)]
13pub struct WorkspaceToolLimits {
14 pub max_read_bytes: usize,
16 pub max_read_lines: usize,
19 pub max_write_bytes: usize,
21 pub max_patch_bytes: usize,
23}
24
25impl Default for WorkspaceToolLimits {
26 fn default() -> Self {
27 Self {
28 max_read_bytes: 1024 * 1024,
29 max_read_lines: 2_000,
30 max_write_bytes: 1024 * 1024,
31 max_patch_bytes: 128 * 1024,
32 }
33 }
34}
35
36#[derive(Debug, Clone)]
38pub struct WorkspaceToolsConfig {
39 pub(crate) root: PathBuf,
40 pub(crate) readonly_resource_roots: Vec<PathBuf>,
41 pub(crate) limits: WorkspaceToolLimits,
42 pub(crate) read_scope: Option<Vec<PathBuf>>,
43 pub(crate) patch_write_scope: Option<Vec<PathBuf>>,
44 pub(crate) forbidden_paths: Vec<PathBuf>,
45 pub(crate) host_path_policy: HostPathPolicy,
46 pub(crate) durable_memory: Option<Arc<DurableMemory>>,
47}
48
49impl WorkspaceToolsConfig {
50 #[must_use]
56 pub fn new(root: impl Into<PathBuf>) -> Self {
57 Self {
58 root: root.into(),
59 readonly_resource_roots: Vec::new(),
60 limits: WorkspaceToolLimits::default(),
61 read_scope: None,
62 patch_write_scope: None,
63 forbidden_paths: Vec::new(),
64 host_path_policy: HostPathPolicy::new(),
65 durable_memory: None,
66 }
67 }
68
69 #[must_use]
71 pub fn with_durable_memory(mut self, memory: Arc<DurableMemory>) -> Self {
72 self.durable_memory = Some(memory);
73 self
74 }
75
76 #[must_use]
78 pub fn root(&self) -> &Path {
79 &self.root
80 }
81
82 #[must_use]
86 pub fn readonly_resource_roots(&self) -> &[PathBuf] {
87 &self.readonly_resource_roots
88 }
89
90 #[must_use]
92 pub fn with_readonly_resource_roots(mut self, roots: Vec<PathBuf>) -> Self {
93 self.readonly_resource_roots = roots;
94 self
95 }
96
97 #[must_use]
99 pub fn limits(&self) -> &WorkspaceToolLimits {
100 &self.limits
101 }
102
103 #[must_use]
105 pub fn read_scope(&self) -> Option<&[PathBuf]> {
106 self.read_scope.as_deref()
107 }
108
109 #[must_use]
111 pub fn patch_write_scope(&self) -> Option<&[PathBuf]> {
112 self.patch_write_scope.as_deref()
113 }
114
115 #[must_use]
117 pub fn forbidden_paths(&self) -> &[PathBuf] {
118 &self.forbidden_paths
119 }
120
121 #[must_use]
123 pub fn with_limits(mut self, limits: WorkspaceToolLimits) -> Self {
124 self.limits = limits;
125 self
126 }
127
128 #[must_use]
134 pub fn with_read_scope(mut self, paths: Option<Vec<PathBuf>>) -> Self {
135 self.read_scope = paths;
136 self
137 }
138
139 #[must_use]
144 pub fn with_patch_write_scope(mut self, paths: Option<Vec<PathBuf>>) -> Self {
145 self.patch_write_scope = paths;
146 self
147 }
148
149 #[must_use]
151 pub fn with_forbidden_paths(mut self, paths: Vec<PathBuf>) -> Self {
152 self.forbidden_paths = paths;
153 self
154 }
155
156 #[must_use]
158 pub fn with_host_path_policy(mut self, policy: HostPathPolicy) -> Self {
159 self.host_path_policy = policy;
160 self
161 }
162}
163
164#[derive(Debug, Error)]
166pub enum WorkspaceToolConfigError {
167 #[error("invalid built-in tool definition: {0}")]
169 ToolDefinition(#[from] merry_runtime::ToolBuildError),
170 #[error("workspace root does not exist: {root}")]
172 RootNotFound {
173 root: PathBuf,
175 },
176 #[error("could not canonicalize workspace root {root}: {source}")]
178 RootCanonicalize {
179 root: PathBuf,
181 #[source]
183 source: io::Error,
184 },
185 #[error("workspace root is not a directory: {root}")]
187 RootNotDirectory {
188 root: PathBuf,
190 },
191 #[error("workspace tool limit {name} must be greater than zero")]
193 InvalidLimit {
194 name: &'static str,
196 },
197 #[error("workspace tool scope path must be relative and normalized: {path}")]
199 InvalidScopePath {
200 path: PathBuf,
202 },
203}