返回 CodeWhale
file_tool.rs
根目录 / crates / engine / src / tools / file_tool.rs
1 //! Model-facing file tools: the small-contract `read`, `write`, and `edit`
2 //! primitives.
3
4 use async_trait::async_trait;
5 use serde_json::{Value, json};
6
7 use super::file::{EditFileTool, ReadFileTool, WriteFileTool};
8 use super::spec::{
9 ApprovalRequirement, RichToolResult, ToolCapability, ToolContext, ToolError, ToolResult,
10 ToolSpec,
11 };
12
13 /// Small model-facing reader with the same public contract as the small-contract built-in
14 /// `read` tool. [`ReadFileTool`] stays registered as `read_file` for protocol
15 /// clients that call it by name.
16 pub struct ReadTool;
17
18 #[async_trait]
19 impl ToolSpec for ReadTool {
20 fn name(&self) -> &'static str {
21 "read"
22 }
23
24 fn description(&self) -> &'static str {
25 "Read a file. Returns a header with line range, total lines, byte size, and truncated flag followed by 1-based numbered lines (`N: <line>`), up to 2000 lines per call within a 51200-byte output cap; a line longer than 2000 chars is truncated with a marker. Use offset and limit to read a line range or continue a large file; the footer names the next offset. For PDFs, text is extracted when pdftotext is installed and optional pages (e.g. \"1-5\" or \"10\") selects a page range. PNG/JPEG/GIF/WebP return as visible image content on vision-capable routes (large images downscaled); read an image instead of OCR or screenshots, and use `read_media` for crop regions or explicit detail levels."
26 }
27
28 fn input_schema(&self) -> Value {
29 json!({
30 "type": "object",
31 "properties": {
32 "path": {
33 "type": "string",
34 "description": "Path to the file to read (relative or absolute)."
35 },
36 "offset": {
37 "type": "number",
38 "description": "Line number to start reading from (1-indexed)."
39 },
40 "limit": {
41 "type": "number",
42 "description": "Maximum number of lines to return. Defaults to 2000."
43 },
44 "pages": {
45 "type": "string",
46 "description": "Optional PDF page range (e.g. \"1-5\" or \"10\"); ignored for non-PDF files."
47 }
48 },
49 "required": ["path"],
50 "additionalProperties": false
51 })
52 }
53
54 fn capabilities(&self) -> Vec<ToolCapability> {
55 ReadFileTool.capabilities()
56 }
57
58 fn supports_parallel(&self) -> bool {
59 true
60 }
61
62 async fn execute(&self, input: Value, context: &ToolContext) -> Result<ToolResult, ToolError> {
63 ReadFileTool::execute_contract_read(input, context)
64 .await
65 .map(RichToolResult::into_result)
66 }
67
68 async fn execute_rich(
69 &self,
70 input: Value,
71 context: &ToolContext,
72 ) -> Result<RichToolResult, ToolError> {
73 ReadFileTool::execute_contract_read(input, context).await
74 }
75 }
76
77 /// Small model-facing whole-file writer backed by [`WriteFileTool`].
78 pub struct WriteTool;
79
80 #[async_trait]
81 impl ToolSpec for WriteTool {
82 fn name(&self) -> &'static str {
83 "write"
84 }
85
86 fn description(&self) -> &'static str {
87 "Write content to a file. Creates the file if it does not exist, overwrites it if it does, and creates parent directories automatically."
88 }
89
90 fn input_schema(&self) -> Value {
91 let write = WriteFileTool.input_schema();
92 json!({
93 "type": "object",
94 "properties": {
95 "path": {
96 "type": "string",
97 "description": "Path to the file to write (relative or absolute)."
98 },
99 "content": { "type": "string", "description": "Content to write to the file." },
100 "line_ending": write["properties"]["line_ending"]
101 },
102 "required": ["path", "content"],
103 "additionalProperties": false
104 })
105 }
106
107 fn capabilities(&self) -> Vec<ToolCapability> {
108 WriteFileTool.capabilities()
109 }
110
111 fn approval_requirement(&self) -> ApprovalRequirement {
112 WriteFileTool.approval_requirement()
113 }
114
115 async fn execute(&self, input: Value, context: &ToolContext) -> Result<ToolResult, ToolError> {
116 WriteFileTool::execute_contract_write(input, context).await
117 }
118 }
119
120 /// small-contract-style multi-edit surface. Every `oldText` is resolved against the same
121 /// original file and ranges must be unique and non-overlapping.
122 pub struct EditTool;
123
124 #[async_trait]
125 impl ToolSpec for EditTool {
126 fn name(&self) -> &'static str {
127 "edit"
128 }
129
130 fn description(&self) -> &'static str {
131 "Edit one file with targeted text replacements. Every edits[].oldText must identify a unique, non-overlapping region of the original file. Merge nearby or overlapping changes into one edit."
132 }
133
134 fn input_schema(&self) -> Value {
135 json!({
136 "type": "object",
137 "properties": {
138 "path": {
139 "type": "string",
140 "description": "Path to the file to edit (relative or absolute)."
141 },
142 "edits": {
143 "type": "array",
144 "items": {
145 "type": "object",
146 "properties": {
147 "oldText": { "type": "string", "description": "Text identifying one unique region to replace." },
148 "newText": {
149 "type": "string",
150 "description": "Replacement text; may be empty to delete the matched span."
151 }
152 },
153 "required": ["oldText", "newText"],
154 "additionalProperties": false
155 },
156 "description": "One or more disjoint replacements, all matched against the original file."
157 }
158 },
159 "required": ["path", "edits"],
160 "additionalProperties": false
161 })
162 }
163
164 fn capabilities(&self) -> Vec<ToolCapability> {
165 EditFileTool.capabilities()
166 }
167
168 fn approval_requirement(&self) -> ApprovalRequirement {
169 EditFileTool.approval_requirement()
170 }
171
172 async fn execute(&self, input: Value, context: &ToolContext) -> Result<ToolResult, ToolError> {
173 EditFileTool::execute_contract_edits(input, context).await
174 }
175 }
176
177 #[cfg(test)]
178 mod tests;
179
179 lines RUST