agtrace-providers 0.8.0

Internal provider adapters for the agtrace CLI. Not intended for direct use.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/// Claude Code provider-specific tool argument types
///
/// These structs represent the exact schema that Claude Code uses, before normalization
/// to the domain model in agtrace-types.
///
/// Note: Claude Code schemas happen to align closely with the domain model, but we document
/// them here for:
/// 1. Provider-specific validation
/// 2. Future schema divergence
/// 3. Claude-specific fields (e.g., timeout, dangerouslyDisableSandbox)
use agtrace_types::{ExecuteArgs, FileEditArgs, FileReadArgs, FileWriteArgs, SearchArgs};
use serde::{Deserialize, Serialize};
use serde_json::json;

/// Claude Code Bash tool arguments
///
/// Claude Code Bash supports additional fields beyond the domain model.
///
/// # Format
/// ```json
/// {
///   "command": "cargo build --release",
///   "description": "Build agtrace in release mode",
///   "timeout": 120000,
///   "dangerouslyDisableSandbox": false,
///   "run_in_background": false
/// }
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
#[allow(non_snake_case)]
pub struct ClaudeBashArgs {
    pub command: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub timeout: Option<u64>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub dangerouslyDisableSandbox: Option<bool>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub run_in_background: Option<bool>,
}

impl ClaudeBashArgs {
    /// Convert Claude Bash args to standard ExecuteArgs
    pub fn to_execute_args(&self) -> ExecuteArgs {
        let mut extra = json!({});
        if let Some(sandbox) = self.dangerouslyDisableSandbox {
            extra["dangerouslyDisableSandbox"] = json!(sandbox);
        }
        if let Some(bg) = self.run_in_background {
            extra["run_in_background"] = json!(bg);
        }

        ExecuteArgs {
            command: Some(self.command.clone()),
            description: self.description.clone(),
            timeout: self.timeout,
            extra,
        }
    }
}

/// Claude Code Read tool arguments
///
/// # Format
/// ```json
/// {
///   "file_path": "/path/to/file.rs",
///   "offset": 0,
///   "limit": 2000
/// }
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ClaudeReadArgs {
    pub file_path: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub offset: Option<u64>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub limit: Option<u64>,
}

impl ClaudeReadArgs {
    /// Convert Claude Read args to standard FileReadArgs
    pub fn to_file_read_args(&self) -> FileReadArgs {
        let mut extra = json!({});
        if let Some(offset) = self.offset {
            extra["offset"] = json!(offset);
        }
        if let Some(limit) = self.limit {
            extra["limit"] = json!(limit);
        }

        FileReadArgs {
            file_path: Some(self.file_path.clone()),
            path: None,
            pattern: None,
            extra,
        }
    }
}

/// Claude Code Glob tool arguments
///
/// # Format
/// ```json
/// {
///   "pattern": "**/*.rs",
///   "path": "/path/to/search"
/// }
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ClaudeGlobArgs {
    pub pattern: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub path: Option<String>,
}

impl ClaudeGlobArgs {
    /// Convert Claude Glob args to standard FileReadArgs
    pub fn to_file_read_args(&self) -> FileReadArgs {
        FileReadArgs {
            file_path: None,
            path: self.path.clone(),
            pattern: Some(self.pattern.clone()),
            extra: json!({}),
        }
    }
}

/// Claude Code Edit tool arguments
///
/// # Format
/// ```json
/// {
///   "file_path": "src/lib.rs",
///   "old_string": "old code",
///   "new_string": "new code",
///   "replace_all": false
/// }
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ClaudeEditArgs {
    pub file_path: String,
    pub old_string: String,
    pub new_string: String,
    #[serde(default)]
    pub replace_all: bool,
}

impl ClaudeEditArgs {
    /// Convert Claude Edit args to standard FileEditArgs
    pub fn to_file_edit_args(&self) -> FileEditArgs {
        FileEditArgs {
            file_path: self.file_path.clone(),
            old_string: self.old_string.clone(),
            new_string: self.new_string.clone(),
            replace_all: self.replace_all,
        }
    }
}

/// Claude Code Write tool arguments
///
/// # Format
/// ```json
/// {
///   "file_path": "test.txt",
///   "content": "hello world"
/// }
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ClaudeWriteArgs {
    pub file_path: String,
    pub content: String,
}

impl ClaudeWriteArgs {
    /// Convert Claude Write args to standard FileWriteArgs
    pub fn to_file_write_args(&self) -> FileWriteArgs {
        FileWriteArgs {
            file_path: self.file_path.clone(),
            content: self.content.clone(),
        }
    }
}

/// Claude Code Grep tool arguments
///
/// # Format
/// ```json
/// {
///   "pattern": "fn main",
///   "path": "src/",
///   "glob": "*.rs",
///   "type": "rust",
///   "output_mode": "content",
///   "-i": true,
///   "-n": true,
///   "-A": 5,
///   "-B": 5,
///   "-C": 5,
///   "head_limit": 100,
///   "multiline": false
/// }
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ClaudeGrepArgs {
    pub pattern: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub path: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub glob: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub r#type: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub output_mode: Option<String>,
    #[serde(rename = "-i", default, skip_serializing_if = "Option::is_none")]
    pub case_insensitive: Option<bool>,
    #[serde(rename = "-n", default, skip_serializing_if = "Option::is_none")]
    pub line_numbers: Option<bool>,
    #[serde(rename = "-A", default, skip_serializing_if = "Option::is_none")]
    pub after_context: Option<u64>,
    #[serde(rename = "-B", default, skip_serializing_if = "Option::is_none")]
    pub before_context: Option<u64>,
    #[serde(rename = "-C", default, skip_serializing_if = "Option::is_none")]
    pub context: Option<u64>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub head_limit: Option<u64>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub multiline: Option<bool>,
}

impl ClaudeGrepArgs {
    /// Convert Claude Grep args to standard SearchArgs
    pub fn to_search_args(&self) -> SearchArgs {
        let mut extra = json!({});
        if let Some(glob) = &self.glob {
            extra["glob"] = json!(glob);
        }
        if let Some(t) = &self.r#type {
            extra["type"] = json!(t);
        }
        if let Some(mode) = &self.output_mode {
            extra["output_mode"] = json!(mode);
        }
        if let Some(ci) = self.case_insensitive {
            extra["-i"] = json!(ci);
        }
        if let Some(ln) = self.line_numbers {
            extra["-n"] = json!(ln);
        }
        if let Some(a) = self.after_context {
            extra["-A"] = json!(a);
        }
        if let Some(b) = self.before_context {
            extra["-B"] = json!(b);
        }
        if let Some(c) = self.context {
            extra["-C"] = json!(c);
        }
        if let Some(hl) = self.head_limit {
            extra["head_limit"] = json!(hl);
        }
        if let Some(ml) = self.multiline {
            extra["multiline"] = json!(ml);
        }

        SearchArgs {
            pattern: Some(self.pattern.clone()),
            query: None,
            input: None,
            path: self.path.clone(),
            extra,
        }
    }
}

/// Claude Code TodoWrite tool arguments
///
/// Claude uses `content` and `activeForm` for todo items.
///
/// # Format
/// ```json
/// {
///   "todos": [
///     {
///       "content": "Task description",
///       "activeForm": "Task in progress form",
///       "status": "pending"
///     }
///   ]
/// }
/// ```
///
/// # Normalization Note
/// This tool maps to ToolKind::Plan but currently falls back to Generic variant
/// since there's no unified Plan variant in ToolCallPayload yet.
/// The raw JSON is preserved in Generic.arguments.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ClaudeTodoWriteArgs {
    pub todos: Vec<ClaudeTodoItem>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[allow(non_snake_case)]
pub struct ClaudeTodoItem {
    /// Task description (Claude-specific field name)
    pub content: String,
    /// Active form of the task (Claude-specific, used for progress display)
    pub activeForm: String,
    /// Task status: "pending", "in_progress", "completed", "cancelled"
    pub status: String,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_bash_args_conversion() {
        let args = ClaudeBashArgs {
            command: "ls -la".to_string(),
            description: Some("List files".to_string()),
            timeout: Some(60000),
            dangerouslyDisableSandbox: Some(false),
            run_in_background: None,
        };

        let domain_args = args.to_execute_args();
        assert_eq!(domain_args.command, Some("ls -la".to_string()));
        assert_eq!(domain_args.description, Some("List files".to_string()));
        assert_eq!(domain_args.timeout, Some(60000));
        assert_eq!(
            domain_args.extra.get("dangerouslyDisableSandbox"),
            Some(&json!(false))
        );
    }

    #[test]
    fn test_read_args_conversion() {
        let args = ClaudeReadArgs {
            file_path: "src/main.rs".to_string(),
            offset: Some(100),
            limit: Some(500),
        };

        let domain_args = args.to_file_read_args();
        assert_eq!(domain_args.file_path, Some("src/main.rs".to_string()));
        assert_eq!(domain_args.extra.get("offset"), Some(&json!(100)));
        assert_eq!(domain_args.extra.get("limit"), Some(&json!(500)));
    }

    #[test]
    fn test_glob_args_conversion() {
        let args = ClaudeGlobArgs {
            pattern: "**/*.rs".to_string(),
            path: Some("src/".to_string()),
        };

        let domain_args = args.to_file_read_args();
        assert_eq!(domain_args.pattern, Some("**/*.rs".to_string()));
        assert_eq!(domain_args.path, Some("src/".to_string()));
    }

    #[test]
    fn test_edit_args_conversion() {
        let args = ClaudeEditArgs {
            file_path: "src/lib.rs".to_string(),
            old_string: "old".to_string(),
            new_string: "new".to_string(),
            replace_all: true,
        };

        let domain_args = args.to_file_edit_args();
        assert_eq!(domain_args.file_path, "src/lib.rs");
        assert_eq!(domain_args.old_string, "old");
        assert_eq!(domain_args.new_string, "new");
        assert!(domain_args.replace_all);
    }

    #[test]
    fn test_write_args_conversion() {
        let args = ClaudeWriteArgs {
            file_path: "test.txt".to_string(),
            content: "hello world".to_string(),
        };

        let domain_args = args.to_file_write_args();
        assert_eq!(domain_args.file_path, "test.txt");
        assert_eq!(domain_args.content, "hello world");
    }

    #[test]
    fn test_grep_args_conversion() {
        let args = ClaudeGrepArgs {
            pattern: "fn main".to_string(),
            path: Some("src/".to_string()),
            glob: Some("*.rs".to_string()),
            r#type: None,
            output_mode: Some("content".to_string()),
            case_insensitive: Some(true),
            line_numbers: Some(true),
            after_context: Some(3),
            before_context: None,
            context: None,
            head_limit: Some(100),
            multiline: None,
        };

        let domain_args = args.to_search_args();
        assert_eq!(domain_args.pattern, Some("fn main".to_string()));
        assert_eq!(domain_args.path, Some("src/".to_string()));
        assert_eq!(domain_args.extra.get("glob"), Some(&json!("*.rs")));
        assert_eq!(
            domain_args.extra.get("output_mode"),
            Some(&json!("content"))
        );
        assert_eq!(domain_args.extra.get("-i"), Some(&json!(true)));
        assert_eq!(domain_args.extra.get("-A"), Some(&json!(3)));
    }

    #[test]
    fn test_todo_write_args_parsing() {
        let json_value = json!({
            "todos": [
                {
                    "content": "Create tools.rs",
                    "activeForm": "Creating tools.rs",
                    "status": "in_progress"
                },
                {
                    "content": "Add tests",
                    "activeForm": "Adding tests",
                    "status": "pending"
                }
            ]
        });

        let args: ClaudeTodoWriteArgs = serde_json::from_value(json_value).unwrap();
        assert_eq!(args.todos.len(), 2);
        assert_eq!(args.todos[0].content, "Create tools.rs");
        assert_eq!(args.todos[0].activeForm, "Creating tools.rs");
        assert_eq!(args.todos[0].status, "in_progress");
    }
}