1use serde::{Deserialize, Serialize};
2use serde_json::Value;
3
4#[cfg(feature = "schemars")]
5use schemars::{JsonSchema, schema_for};
6
7#[cfg(feature = "schemars")]
13pub fn tool_parameters<T: JsonSchema>() -> Value {
14 let mut v = serde_json::to_value(schema_for!(T)).expect("can't parse value from schema");
15
16 if let Some(value) = v.as_object_mut() {
18 value.remove("$schema");
19 value.remove("title");
20 }
21
22 let mut v_str = serde_json::to_string(&v).unwrap();
23 v_str = v_str
24 .replace("/definitions/", "/$defs/")
25 .replace("\"definitions\":", "\"$defs\":");
26
27 v_str = v_str.replace("\"oneOf\":", "\"anyOf\":");
29
30 let mut v: Value = serde_json::from_str(&v_str).expect("can't parse value from updated schema");
31 enforce_openai_strict_schema(&mut v);
32 v
33}
34
35#[cfg(feature = "schemars")]
36fn enforce_openai_strict_schema(v: &mut Value) {
37 match v {
38 Value::Object(map) => {
39 for (_k, child) in map.iter_mut() {
41 enforce_openai_strict_schema(child);
42 }
43
44 let is_object = map
46 .get("type")
47 .and_then(|t| t.as_str())
48 .is_some_and(|t| t == "object");
49 let has_props = map.get("properties").is_some();
50 if is_object || has_props {
51 map.entry("additionalProperties".to_string())
52 .or_insert(Value::Bool(false));
53
54 if let Some(Value::Object(props)) = map.get("properties") {
55 let mut keys: Vec<String> = props.keys().cloned().collect();
56 keys.sort();
57 map.insert(
58 "required".to_string(),
59 Value::Array(keys.into_iter().map(Value::String).collect()),
60 );
61 }
62 }
63 }
64 Value::Array(arr) => {
65 for child in arr.iter_mut() {
66 enforce_openai_strict_schema(child);
67 }
68 }
69 _ => {}
70 }
71}
72
73#[derive(Debug, Clone, Serialize, Deserialize)]
74#[cfg_attr(feature = "schemars", derive(JsonSchema))]
75pub struct ReadFileArgs {
76 pub path: String,
78 pub offset: Option<usize>,
80 pub limit: Option<usize>,
82}
83
84#[derive(Debug, Clone, Serialize, Deserialize)]
85#[cfg_attr(feature = "schemars", derive(JsonSchema))]
86pub struct ListDirArgs {
87 pub path: String,
89}
90
91#[derive(Debug, Clone, Serialize, Deserialize)]
92#[cfg_attr(feature = "schemars", derive(JsonSchema))]
93pub struct GrepArgs {
94 pub pattern: String,
96 pub path: Option<String>,
98 pub glob: Option<String>,
100 pub head_limit: Option<usize>,
102}
103
104#[derive(Debug, Clone, Serialize, Deserialize)]
105#[cfg_attr(feature = "schemars", derive(JsonSchema))]
106pub struct RunShellArgs {
107 pub command: String,
109 pub cwd: Option<String>,
111 pub timeout_ms: Option<u64>,
113 pub max_output_bytes: Option<u64>,
119}
120
121#[derive(Debug, Clone, Serialize, Deserialize)]
122#[cfg_attr(feature = "schemars", derive(JsonSchema))]
123pub struct ApplyDiffArgs {
124 pub diff: String,
130}
131
132#[derive(Debug, Clone, Serialize, Deserialize)]
133#[cfg_attr(feature = "schemars", derive(JsonSchema))]
134pub struct DeleteFilesArgs {
135 pub paths: Vec<String>,
137}
138
139#[cfg(feature = "schemars")]
141pub fn openai_tools() -> Vec<Value> {
142 vec![
143 serde_json::json!({
144 "type": "function",
145 "name": "read_file",
146 "description": "Read a local file (by path), optionally with offset/limit. Returns a JSON string with keys: path, offset, limit, total_lines, content, numbered_content, fingerprint{hash64,len_bytes}, truncated.",
147 "strict": true,
148 "parameters": tool_parameters::<ReadFileArgs>(),
149 }),
150 serde_json::json!({
151 "type": "function",
152 "name": "list_dir",
153 "description": "List a local directory (by path). Returns a JSON string: { path, entries: [{ name, is_dir, is_file }, ...] }.",
154 "strict": true,
155 "parameters": tool_parameters::<ListDirArgs>(),
156 }),
157 serde_json::json!({
158 "type": "function",
159 "name": "grep",
160 "description": "Search for a regex pattern in files. Returns a JSON string including matches (file, line_number, line). May be truncated to head_limit.",
161 "strict": true,
162 "parameters": tool_parameters::<GrepArgs>(),
163 }),
164 serde_json::json!({
165 "type": "function",
166 "name": "run_shell",
167 "description": "Run a shell command via `bash -lc` (supports pipes/redirection). Requires user confirmation. Use `max_output_bytes` intentionally: prefer the smallest limit that answers the question, and increase only when needed. Oversize output is cut from the middle, preserving roughly the first 30% and last 70%, so large requests are rarely necessary just to inspect the tail.",
168 "strict": true,
169 "parameters": tool_parameters::<RunShellArgs>(),
170 }),
171 serde_json::json!({
172 "type": "function",
173 "name": "apply_diff",
174 "description": "Apply a git-style unified diff to the local working tree (create/update files). Returns a JSON string describing what changed or an error.",
175 "strict": true,
176 "parameters": tool_parameters::<ApplyDiffArgs>(),
177 }),
178 serde_json::json!({
179 "type": "function",
180 "name": "delete_files",
181 "description": "Delete one or more files by path (relative to project root). Returns a JSON string listing deleted and missing paths.",
182 "strict": true,
183 "parameters": tool_parameters::<DeleteFilesArgs>(),
184 }),
185 ]
186}
187
188#[cfg(test)]
189mod tests {
190 use super::*;
191
192 #[cfg(feature = "schemars")]
193 #[test]
194 fn run_shell_tool_schema_encourages_small_output_limits() {
195 let run_shell = openai_tools()
196 .into_iter()
197 .find(|tool| tool.get("name").and_then(Value::as_str) == Some("run_shell"))
198 .expect("run_shell tool");
199
200 let description = run_shell
201 .get("description")
202 .and_then(Value::as_str)
203 .expect("run_shell description");
204 assert!(description.contains("max_output_bytes"));
205 assert!(description.contains("30%"));
206 assert!(description.contains("70%"));
207 assert!(description.contains("smallest limit"));
208
209 let max_output_description = run_shell
210 .get("parameters")
211 .and_then(|value| value.get("properties"))
212 .and_then(|value| value.get("max_output_bytes"))
213 .and_then(|value| value.get("description"))
214 .and_then(Value::as_str)
215 .expect("max_output_bytes description");
216 assert!(max_output_description.contains("30%"));
217 assert!(max_output_description.contains("70%"));
218 assert!(max_output_description.contains("few KB"));
219 }
220}