llm-toolkit 0.63.1

A low-level, unopinionated Rust toolkit for the LLM last mile problem.
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
//! ClaudeCodeAgent - A universal agent implementation that wraps the Claude CLI.
//!
//! This agent can handle a wide variety of tasks by spawning the `claude` command
//! with the `-p` flag to pass prompts directly.

use crate::agent::{Agent, AgentError, Payload};
use crate::models::ClaudeModel;
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use tokio::process::Command;
use tracing::{debug, error, info, instrument};

use super::cli_agent::{CliAgent, CliAgentConfig};

/// A general-purpose agent that executes tasks using the Claude CLI.
///
/// This agent wraps the `claude` command-line tool and can handle
/// coding, research, analysis, and other general tasks.
///
/// # Output
///
/// Returns the raw string output from Claude. For structured output,
/// you can parse this string using `serde_json` or other parsers.
///
/// # Example
///
/// ```rust,ignore
/// use llm_toolkit::agent::{Agent, ClaudeCodeAgent};
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
///     let agent = ClaudeCodeAgent::new();
///
///     let result = agent.execute(
///         "Analyze the Rust ownership model and explain it in 3 bullet points".to_string()
///     ).await?;
///
///     println!("{}", result);
///     Ok(())
/// }
/// ```
pub struct ClaudeCodeAgent {
    /// Path to the `claude` executable. If None, searches in PATH.
    claude_path: Option<PathBuf>,
    /// Model to use for generation
    model: Option<ClaudeModel>,
    /// Common CLI agent configuration
    config: CliAgentConfig,
}

impl ClaudeCodeAgent {
    /// Creates a new ClaudeCodeAgent with default settings.
    ///
    /// By default:
    /// - Searches for `claude` in the system PATH
    /// - Uses default model (Sonnet 4.5)
    /// - No working directory specified (uses current directory)
    /// - No additional environment variables
    /// - No extra CLI arguments
    pub fn new() -> Self {
        Self {
            claude_path: None,
            model: None,
            config: CliAgentConfig::new(),
        }
    }

    /// Creates a new ClaudeCodeAgent with a custom path to the claude executable.
    pub fn with_path(path: PathBuf) -> Self {
        Self {
            claude_path: Some(path),
            model: None,
            config: CliAgentConfig::new(),
        }
    }

    /// Sets the model to use.
    pub fn with_model(mut self, model: ClaudeModel) -> Self {
        self.model = Some(model);
        self
    }

    /// Sets the model using a string identifier.
    ///
    /// Accepts: "sonnet", "sonnet-4.6", "sonnet-4.5", "opus", "opus-4.6", etc.
    /// See [`ClaudeModel`] for all supported variants.
    pub fn with_model_str(mut self, model: &str) -> Self {
        self.model = Some(model.parse().unwrap_or_default());
        self
    }

    /// Sets the execution profile.
    ///
    /// # Example
    /// ```rust,ignore
    /// use llm_toolkit::agent::ExecutionProfile;
    ///
    /// let agent = ClaudeCodeAgent::new()
    ///     .with_execution_profile(ExecutionProfile::Creative);
    /// ```
    pub fn with_execution_profile(mut self, profile: crate::agent::ExecutionProfile) -> Self {
        self.config = self.config.with_execution_profile(profile);
        self
    }

    /// Sets the working directory where the claude command will be executed.
    ///
    /// # Example
    /// ```rust,ignore
    /// let agent = ClaudeCodeAgent::new()
    ///     .with_cwd("/path/to/project");
    /// ```
    pub fn with_cwd(mut self, path: impl Into<PathBuf>) -> Self {
        self.config = self.config.with_cwd(path);
        self
    }

    /// Alias for `with_cwd` using more explicit name.
    ///
    /// # Example
    /// ```rust,ignore
    /// let agent = ClaudeCodeAgent::new()
    ///     .with_directory("/path/to/project");
    /// ```
    pub fn with_directory(mut self, path: impl Into<PathBuf>) -> Self {
        self.config = self.config.with_directory(path);
        self
    }

    /// Sets a single environment variable for the claude command.
    ///
    /// # Example
    /// ```rust,ignore
    /// let agent = ClaudeCodeAgent::new()
    ///     .with_env("CLAUDE_API_KEY", "my-key")
    ///     .with_env("PATH", "/usr/local/bin:/usr/bin");
    /// ```
    pub fn with_env(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.config = self.config.with_env(key, value);
        self
    }

    /// Sets multiple environment variables at once.
    ///
    /// # Example
    /// ```rust,ignore
    /// use std::collections::HashMap;
    ///
    /// let mut env_map = HashMap::new();
    /// env_map.insert("PATH".to_string(), "/custom/path".to_string());
    /// env_map.insert("CLAUDE_API_KEY".to_string(), "key".to_string());
    ///
    /// let agent = ClaudeCodeAgent::new()
    ///     .with_envs(env_map);
    /// ```
    pub fn with_envs(mut self, envs: std::collections::HashMap<String, String>) -> Self {
        self.config = self.config.with_envs(envs);
        self
    }

    /// Clears all environment variables.
    pub fn clear_env(mut self) -> Self {
        self.config = self.config.clear_env();
        self
    }

    /// Adds a single CLI argument to pass to the claude command.
    ///
    /// # Example
    /// ```rust,ignore
    /// let agent = ClaudeCodeAgent::new()
    ///     .with_arg("--experimental")
    ///     .with_arg("--timeout")
    ///     .with_arg("60");
    /// ```
    pub fn with_arg(mut self, arg: impl Into<String>) -> Self {
        self.config = self.config.with_arg(arg);
        self
    }

    /// Adds multiple CLI arguments at once.
    ///
    /// # Example
    /// ```rust,ignore
    /// let agent = ClaudeCodeAgent::new()
    ///     .with_args(vec!["--experimental".to_string(), "--verbose".to_string()]);
    /// ```
    pub fn with_args(mut self, args: Vec<String>) -> Self {
        self.config = self.config.with_args(args);
        self
    }

    /// Sets the directory where attachment files will be written.
    ///
    /// If not specified, falls back to `working_dir` or system temp directory.
    ///
    /// # Example
    /// ```rust,ignore
    /// let agent = ClaudeCodeAgent::new()
    ///     .with_attachment_dir("/tmp/my-attachments");
    /// ```
    pub fn with_attachment_dir(mut self, path: impl Into<PathBuf>) -> Self {
        self.config = self.config.with_attachment_dir(path);
        self
    }

    /// Sets whether to keep temporary attachment files after execution.
    ///
    /// By default, temp files are deleted after each execution.
    /// Set to `true` to keep files for debugging purposes.
    ///
    /// # Example
    /// ```rust,ignore
    /// let agent = ClaudeCodeAgent::new()
    ///     .with_keep_attachments(true); // Don't delete temp files
    /// ```
    pub fn with_keep_attachments(mut self, keep: bool) -> Self {
        self.config = self.config.with_keep_attachments(keep);
        self
    }

    /// Checks if the `claude` CLI is available in the system (static version).
    ///
    /// Returns `true` if the command exists in PATH, `false` otherwise.
    /// Uses `which` on Unix/macOS or `where` on Windows for a quick check.
    pub fn is_available() -> bool {
        #[cfg(unix)]
        let check_cmd = "which";
        #[cfg(windows)]
        let check_cmd = "where";

        std::process::Command::new(check_cmd)
            .arg("claude")
            .output()
            .map(|output| output.status.success())
            .unwrap_or(false)
    }

    /// Checks availability using tokio (async version for trait implementation).
    async fn check_available() -> Result<(), AgentError> {
        #[cfg(unix)]
        let check_cmd = "which";
        #[cfg(windows)]
        let check_cmd = "where";

        let output = Command::new(check_cmd)
            .arg("claude")
            .output()
            .await
            .map_err(|e| AgentError::ProcessError {
                status_code: None,
                message: format!("Failed to check claude availability: {}", e),
                is_retryable: true,
                retry_after: None,
            })?;

        if output.status.success() {
            Ok(())
        } else {
            Err(AgentError::ExecutionFailed(
                "claude CLI not found in PATH. Please install Claude CLI.".to_string(),
            ))
        }
    }
}

impl Default for ClaudeCodeAgent {
    fn default() -> Self {
        Self::new()
    }
}

impl CliAgent for ClaudeCodeAgent {
    fn config(&self) -> &CliAgentConfig {
        &self.config
    }

    fn config_mut(&mut self) -> &mut CliAgentConfig {
        &mut self.config
    }

    fn cli_path(&self) -> Option<&Path> {
        self.claude_path.as_deref()
    }

    fn cli_command_name(&self) -> &str {
        "claude"
    }

    fn build_command(&self, prompt: &str) -> Result<Command, AgentError> {
        let cmd_name = self
            .claude_path
            .as_ref()
            .map(|p| p.to_string_lossy().to_string())
            .unwrap_or_else(|| "claude".to_string());

        let mut cmd = Command::new(cmd_name);

        // Apply common configuration (working dir, env vars)
        self.config.apply_to_command(&mut cmd);

        // Add prompt argument
        cmd.arg("-p").arg(prompt);

        // Add model if specified
        if let Some(model) = &self.model {
            cmd.arg("--model").arg(model.as_cli_name());
            debug!(
                target: "llm_toolkit::agent::claude_code",
                "Using model: {}", model.as_cli_name()
            );
        }

        // Add extra CLI arguments from config
        for arg in &self.config.extra_args {
            debug!(
                target: "llm_toolkit::agent::claude_code",
                "Adding extra argument: {}", arg
            );
            cmd.arg(arg);
        }

        Ok(cmd)
    }
}

#[async_trait]
impl Agent for ClaudeCodeAgent {
    type Output = String;
    type Expertise = &'static str;

    fn expertise(&self) -> &Self::Expertise {
        &"A general-purpose AI agent capable of coding, research, analysis, \
         writing, and problem-solving across various domains. Can handle \
         complex multi-step tasks autonomously."
    }

    #[instrument(skip(self, intent), fields(
        model = ?self.model,
        working_dir = ?self.config.working_dir,
        has_attachments = intent.has_attachments(),
        prompt_length = intent.to_text().len()
    ))]
    async fn execute(&self, intent: Payload) -> Result<Self::Output, AgentError> {
        let payload = intent;

        // Process attachments using shared config method
        let (final_prompt, _temp_dir) = self.config.process_payload_attachments(&payload).await?;

        debug!(
            target: "llm_toolkit::agent::claude_code",
            "Building claude command with prompt length: {}", final_prompt.len()
        );

        crate::tracing::trace!(
            target: "llm_toolkit::agent::claude_code",
            "\n========== CLAUDE CODE PROMPT ==========\n{}\n====================================",
            final_prompt
        );

        let mut cmd = self.build_command(&final_prompt)?;

        debug!(
            target: "llm_toolkit::agent::claude_code",
            "Executing claude command: {:?}", cmd
        );

        let output = cmd.output().await.map_err(|e| {
            error!(
                target: "llm_toolkit::agent::claude_code",
                "Failed to execute claude command: {}", e
            );
            AgentError::ProcessError {
                status_code: None,
                message: format!(
                    "Failed to spawn claude process: {}. \
                     Make sure 'claude' is installed and in PATH.",
                    e
                ),
                is_retryable: true,
                retry_after: None,
            }
        })?;

        if output.status.success() {
            let stdout = String::from_utf8(output.stdout).map_err(|e| {
                error!(
                    target: "llm_toolkit::agent::claude_code",
                    "Failed to parse stdout as UTF-8: {}", e
                );
                AgentError::Other(format!("Failed to parse claude output as UTF-8: {}", e))
            })?;

            info!(
                target: "llm_toolkit::agent::claude_code",
                "Claude command completed successfully, response length: {}", stdout.len()
            );
            Ok(stdout)
        } else {
            let stderr = String::from_utf8_lossy(&output.stderr);
            error!(
                target: "llm_toolkit::agent::claude_code",
                "Claude command failed with stderr: {}", stderr
            );
            Err(AgentError::ExecutionFailed(format!(
                "Claude command failed with status {}: {}",
                output.status, stderr
            )))
        }
    }

    fn name(&self) -> String {
        "ClaudeCodeAgent".to_string()
    }

    async fn is_available(&self) -> Result<(), AgentError> {
        Self::check_available().await
    }
}

/// A typed variant of ClaudeCodeAgent that attempts to parse JSON output.
///
/// This agent is useful when you expect structured output from Claude.
///
/// # Example
///
/// ```rust,ignore
/// use llm_toolkit::agent::{Agent, ClaudeCodeJsonAgent};
/// use serde::{Deserialize, Serialize};
///
/// #[derive(Deserialize, Serialize)]
/// struct Analysis {
///     summary: String,
///     key_points: Vec<String>,
/// }
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
///     let agent = ClaudeCodeJsonAgent::<Analysis>::new();
///
///     let result = agent.execute(
///         "Analyze Rust's ownership model and return JSON with 'summary' \
///          and 'key_points' (array of strings)".to_string()
///     ).await?;
///
///     println!("Summary: {}", result.summary);
///     Ok(())
/// }
/// ```
pub struct ClaudeCodeJsonAgent<T> {
    inner: ClaudeCodeAgent,
    _phantom: std::marker::PhantomData<T>,
}

impl<T> ClaudeCodeJsonAgent<T>
where
    T: Serialize + for<'de> Deserialize<'de>,
{
    /// Creates a new ClaudeCodeJsonAgent.
    pub fn new() -> Self {
        Self {
            inner: ClaudeCodeAgent::new(),
            _phantom: std::marker::PhantomData,
        }
    }

    /// Creates a new ClaudeCodeJsonAgent with a custom path.
    pub fn with_path(path: PathBuf) -> Self {
        Self {
            inner: ClaudeCodeAgent::with_path(path),
            _phantom: std::marker::PhantomData,
        }
    }

    /// Sets the model to use.
    pub fn with_model(mut self, model: ClaudeModel) -> Self {
        self.inner = self.inner.with_model(model);
        self
    }

    /// Sets the model using a string identifier.
    pub fn with_model_str(mut self, model: &str) -> Self {
        self.inner = self.inner.with_model_str(model);
        self
    }

    /// Sets the working directory where the claude command will be executed.
    pub fn with_cwd(mut self, path: impl Into<PathBuf>) -> Self {
        self.inner = self.inner.with_cwd(path);
        self
    }

    /// Alias for `with_cwd` using more explicit name.
    pub fn with_directory(mut self, path: impl Into<PathBuf>) -> Self {
        self.inner = self.inner.with_directory(path);
        self
    }

    /// Sets a single environment variable for the claude command.
    pub fn with_env(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.inner = self.inner.with_env(key, value);
        self
    }

    /// Sets multiple environment variables at once.
    pub fn with_envs(mut self, envs: HashMap<String, String>) -> Self {
        self.inner = self.inner.with_envs(envs);
        self
    }

    /// Clears all environment variables.
    pub fn clear_env(mut self) -> Self {
        self.inner = self.inner.clear_env();
        self
    }

    /// Adds a single CLI argument to pass to the claude command.
    pub fn with_arg(mut self, arg: impl Into<String>) -> Self {
        self.inner = self.inner.with_arg(arg);
        self
    }

    /// Adds multiple CLI arguments at once.
    pub fn with_args(mut self, args: Vec<String>) -> Self {
        self.inner = self.inner.with_args(args);
        self
    }

    /// Sets the directory where attachment files will be written.
    pub fn with_attachment_dir(mut self, path: impl Into<PathBuf>) -> Self {
        self.inner = self.inner.with_attachment_dir(path);
        self
    }

    /// Sets whether to keep temporary attachment files after execution.
    pub fn with_keep_attachments(mut self, keep: bool) -> Self {
        self.inner = self.inner.with_keep_attachments(keep);
        self
    }
}

impl<T> Default for ClaudeCodeJsonAgent<T>
where
    T: Serialize + for<'de> Deserialize<'de>,
{
    fn default() -> Self {
        Self::new()
    }
}

#[async_trait]
impl<T> Agent for ClaudeCodeJsonAgent<T>
where
    T: Serialize + for<'de> Deserialize<'de> + Send + Sync,
{
    type Output = T;
    type Expertise = &'static str;

    fn expertise(&self) -> &Self::Expertise {
        self.inner.expertise()
    }

    async fn execute(&self, intent: Payload) -> Result<Self::Output, AgentError> {
        log::info!(
            "📊 ClaudeCodeJsonAgent<{}> executing...",
            std::any::type_name::<T>()
        );

        let raw_output = self.inner.execute(intent).await?;

        log::debug!("Extracting JSON from raw output...");

        // Try to extract JSON from the output (might be wrapped in markdown, etc.)
        let json_str = crate::extract_json(&raw_output).map_err(|e| {
            log::error!("Failed to extract JSON: {}", e);
            AgentError::ParseError {
                message: format!(
                    "Failed to extract JSON from claude output: {}. Raw output: {}",
                    e, raw_output
                ),
                reason: crate::agent::error::ParseErrorReason::MarkdownExtractionFailed,
            }
        })?;

        log::debug!("Parsing JSON into {}...", std::any::type_name::<T>());

        let result = serde_json::from_str(&json_str).map_err(|e| {
            log::error!("Failed to parse JSON: {}", e);

            // Determine the parse error reason based on serde_json error type
            let reason = if e.is_eof() {
                crate::agent::error::ParseErrorReason::UnexpectedEof
            } else if e.is_syntax() {
                crate::agent::error::ParseErrorReason::InvalidJson
            } else {
                crate::agent::error::ParseErrorReason::SchemaMismatch
            };

            AgentError::ParseError {
                message: format!("Failed to parse JSON: {}. Extracted JSON: {}", e, json_str),
                reason,
            }
        })?;

        log::info!(
            "✅ ClaudeCodeJsonAgent<{}> completed",
            std::any::type_name::<T>()
        );

        Ok(result)
    }

    fn name(&self) -> String {
        format!("ClaudeCodeJsonAgent<{}>", std::any::type_name::<T>())
    }

    async fn is_available(&self) -> Result<(), AgentError> {
        self.inner.is_available().await
    }
}

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

    #[test]
    fn test_claude_code_agent_creation() {
        let agent = ClaudeCodeAgent::new();
        assert_eq!(agent.name(), "ClaudeCodeAgent");
        assert!(!agent.expertise().is_empty());
    }

    #[test]
    fn test_claude_code_agent_with_path() {
        let path = PathBuf::from("/usr/local/bin/claude");
        let agent = ClaudeCodeAgent::with_path(path.clone());
        assert_eq!(agent.claude_path, Some(path));
    }

    #[test]
    fn test_claude_code_agent_with_cwd() {
        let agent = ClaudeCodeAgent::new().with_cwd("/path/to/project");

        assert!(agent.config.working_dir.is_some());
        assert_eq!(
            agent.config.working_dir.unwrap(),
            PathBuf::from("/path/to/project")
        );
    }

    #[test]
    fn test_claude_code_agent_with_directory() {
        let agent = ClaudeCodeAgent::new().with_directory("/path/to/project");

        assert!(agent.config.working_dir.is_some());
        assert_eq!(
            agent.config.working_dir.unwrap(),
            PathBuf::from("/path/to/project")
        );
    }

    #[test]
    fn test_claude_code_agent_with_env() {
        let agent = ClaudeCodeAgent::new()
            .with_env("CLAUDE_API_KEY", "my-key")
            .with_env("PATH", "/usr/local/bin");

        assert_eq!(agent.config.env_vars.len(), 2);
        assert_eq!(
            agent.config.env_vars.get("CLAUDE_API_KEY"),
            Some(&"my-key".to_string())
        );
        assert_eq!(
            agent.config.env_vars.get("PATH"),
            Some(&"/usr/local/bin".to_string())
        );
    }

    #[test]
    fn test_claude_code_agent_with_envs() {
        let mut env_map = HashMap::new();
        env_map.insert("KEY1".to_string(), "value1".to_string());
        env_map.insert("KEY2".to_string(), "value2".to_string());

        let agent = ClaudeCodeAgent::new().with_envs(env_map);

        assert_eq!(agent.config.env_vars.len(), 2);
        assert_eq!(
            agent.config.env_vars.get("KEY1"),
            Some(&"value1".to_string())
        );
        assert_eq!(
            agent.config.env_vars.get("KEY2"),
            Some(&"value2".to_string())
        );
    }

    #[test]
    fn test_claude_code_agent_clear_env() {
        let agent = ClaudeCodeAgent::new()
            .with_env("KEY1", "value1")
            .with_env("KEY2", "value2")
            .clear_env();

        assert!(agent.config.env_vars.is_empty());
    }

    #[test]
    fn test_claude_code_agent_with_arg() {
        let agent = ClaudeCodeAgent::new()
            .with_arg("--experimental")
            .with_arg("--timeout")
            .with_arg("60");

        assert_eq!(agent.config.extra_args.len(), 3);
        assert_eq!(agent.config.extra_args[0], "--experimental");
        assert_eq!(agent.config.extra_args[1], "--timeout");
        assert_eq!(agent.config.extra_args[2], "60");
    }

    #[test]
    fn test_claude_code_agent_with_args() {
        let agent = ClaudeCodeAgent::new()
            .with_args(vec!["--experimental".to_string(), "--verbose".to_string()]);

        assert_eq!(agent.config.extra_args.len(), 2);
        assert_eq!(agent.config.extra_args[0], "--experimental");
        assert_eq!(agent.config.extra_args[1], "--verbose");
    }

    #[test]
    fn test_claude_code_agent_builder_pattern() {
        let agent = ClaudeCodeAgent::new()
            .with_model(ClaudeModel::Opus4)
            .with_cwd("/project")
            .with_env("PATH", "/custom/path")
            .with_arg("--experimental");

        assert!(matches!(agent.model, Some(ClaudeModel::Opus4)));
        assert_eq!(agent.config.working_dir, Some(PathBuf::from("/project")));
        assert_eq!(
            agent.config.env_vars.get("PATH"),
            Some(&"/custom/path".to_string())
        );
        assert_eq!(agent.config.extra_args.len(), 1);
        assert_eq!(agent.config.extra_args[0], "--experimental");
    }
}