syncable-cli 0.37.1

A Rust-based CLI that analyzes code repositories and generates Infrastructure as Code configurations
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
//! Provider-related logic for API key management, model selection, and credential handling.
//!
//! This module contains:
//! - `get_available_models` - Returns available models per provider
//! - `has_api_key` - Checks if API key is configured for a provider
//! - `load_api_key_to_env` - Loads API key from config and sets in environment
//! - `get_configured_providers` - Returns list of providers with valid credentials
//! - `prompt_api_key` - Prompts user for API key interactively

use crate::agent::{AgentError, AgentResult, ProviderType};
use crate::config::{load_agent_config, save_agent_config};
use colored::Colorize;
use std::io::{self, Write};

/// Available models per provider
pub fn get_available_models(provider: ProviderType) -> Vec<(&'static str, &'static str)> {
    match provider {
        ProviderType::OpenAI => vec![
            ("gpt-5.2", "GPT-5.2 - Latest reasoning model (Dec 2025)"),
            ("gpt-5.2-mini", "GPT-5.2 Mini - Fast and affordable"),
            ("gpt-4o", "GPT-4o - Multimodal workhorse"),
            ("o1-preview", "o1-preview - Advanced reasoning"),
        ],
        ProviderType::Anthropic => vec![
            (
                "claude-opus-4-5-20251101",
                "Claude Opus 4.5 - Most capable (Nov 2025)",
            ),
            (
                "claude-sonnet-4-5-20250929",
                "Claude Sonnet 4.5 - Balanced (Sep 2025)",
            ),
            (
                "claude-haiku-4-5-20251001",
                "Claude Haiku 4.5 - Fast (Oct 2025)",
            ),
            ("claude-sonnet-4-20250514", "Claude Sonnet 4 - Previous gen"),
        ],
        // Bedrock models - use cross-region inference profile format (global. prefix)
        ProviderType::Bedrock => vec![
            (
                "global.anthropic.claude-opus-4-5-20251101-v1:0",
                "Claude Opus 4.5 - Most capable (Nov 2025)",
            ),
            (
                "global.anthropic.claude-sonnet-4-5-20250929-v1:0",
                "Claude Sonnet 4.5 - Balanced (Sep 2025)",
            ),
            (
                "global.anthropic.claude-haiku-4-5-20251001-v1:0",
                "Claude Haiku 4.5 - Fast (Oct 2025)",
            ),
            (
                "global.anthropic.claude-sonnet-4-20250514-v1:0",
                "Claude Sonnet 4 - Previous gen",
            ),
        ],
    }
}

/// Check if API key is configured for a provider (env var OR config file)
pub fn has_api_key(provider: ProviderType) -> bool {
    // Check environment variable first
    let env_key = match provider {
        ProviderType::OpenAI => std::env::var("OPENAI_API_KEY").ok(),
        ProviderType::Anthropic => std::env::var("ANTHROPIC_API_KEY").ok(),
        ProviderType::Bedrock => {
            // Check for AWS credentials from env vars
            if std::env::var("AWS_ACCESS_KEY_ID").is_ok()
                && std::env::var("AWS_SECRET_ACCESS_KEY").is_ok()
            {
                return true;
            }
            if std::env::var("AWS_PROFILE").is_ok() {
                return true;
            }
            None
        }
    };

    if env_key.is_some() {
        return true;
    }

    // Check config file - first try active global profile
    let agent_config = load_agent_config();

    // Check active global profile first
    if let Some(profile_name) = &agent_config.active_profile
        && let Some(profile) = agent_config.profiles.get(profile_name)
    {
        match provider {
            ProviderType::OpenAI => {
                if profile
                    .openai
                    .as_ref()
                    .map(|o| !o.api_key.is_empty())
                    .unwrap_or(false)
                {
                    return true;
                }
            }
            ProviderType::Anthropic => {
                if profile
                    .anthropic
                    .as_ref()
                    .map(|a| !a.api_key.is_empty())
                    .unwrap_or(false)
                {
                    return true;
                }
            }
            ProviderType::Bedrock => {
                if let Some(bedrock) = &profile.bedrock
                    && (bedrock.profile.is_some()
                        || (bedrock.access_key_id.is_some() && bedrock.secret_access_key.is_some()))
                {
                    return true;
                }
            }
        }
    }

    // Check any profile that has this provider configured
    for profile in agent_config.profiles.values() {
        match provider {
            ProviderType::OpenAI => {
                if profile
                    .openai
                    .as_ref()
                    .map(|o| !o.api_key.is_empty())
                    .unwrap_or(false)
                {
                    return true;
                }
            }
            ProviderType::Anthropic => {
                if profile
                    .anthropic
                    .as_ref()
                    .map(|a| !a.api_key.is_empty())
                    .unwrap_or(false)
                {
                    return true;
                }
            }
            ProviderType::Bedrock => {
                if let Some(bedrock) = &profile.bedrock
                    && (bedrock.profile.is_some()
                        || (bedrock.access_key_id.is_some() && bedrock.secret_access_key.is_some()))
                {
                    return true;
                }
            }
        }
    }

    // Fall back to legacy config
    match provider {
        ProviderType::OpenAI => agent_config.openai_api_key.is_some(),
        ProviderType::Anthropic => agent_config.anthropic_api_key.is_some(),
        ProviderType::Bedrock => {
            if let Some(bedrock) = &agent_config.bedrock {
                bedrock.profile.is_some()
                    || (bedrock.access_key_id.is_some() && bedrock.secret_access_key.is_some())
            } else {
                agent_config.bedrock_configured.unwrap_or(false)
            }
        }
    }
}

/// Load API key from config if not in env, and set it in env for use
pub fn load_api_key_to_env(provider: ProviderType) {
    let agent_config = load_agent_config();

    // Try to get credentials from active global profile first
    let active_profile = agent_config
        .active_profile
        .as_ref()
        .and_then(|name| agent_config.profiles.get(name));

    match provider {
        ProviderType::OpenAI => {
            if std::env::var("OPENAI_API_KEY").is_ok() {
                return;
            }
            // Check active global profile
            if let Some(key) = active_profile
                .and_then(|p| p.openai.as_ref())
                .map(|o| o.api_key.clone())
                .filter(|k| !k.is_empty())
            {
                unsafe {
                    std::env::set_var("OPENAI_API_KEY", &key);
                }
                return;
            }
            // Fall back to legacy key
            if let Some(key) = &agent_config.openai_api_key {
                unsafe {
                    std::env::set_var("OPENAI_API_KEY", key);
                }
            }
        }
        ProviderType::Anthropic => {
            if std::env::var("ANTHROPIC_API_KEY").is_ok() {
                return;
            }
            // Check active global profile
            if let Some(key) = active_profile
                .and_then(|p| p.anthropic.as_ref())
                .map(|a| a.api_key.clone())
                .filter(|k| !k.is_empty())
            {
                unsafe {
                    std::env::set_var("ANTHROPIC_API_KEY", &key);
                }
                return;
            }
            // Fall back to legacy key
            if let Some(key) = &agent_config.anthropic_api_key {
                unsafe {
                    std::env::set_var("ANTHROPIC_API_KEY", key);
                }
            }
        }
        ProviderType::Bedrock => {
            // Check active global profile first
            let bedrock_config = active_profile
                .and_then(|p| p.bedrock.as_ref())
                .or(agent_config.bedrock.as_ref());

            if let Some(bedrock) = bedrock_config {
                // Load region
                if std::env::var("AWS_REGION").is_err()
                    && let Some(region) = &bedrock.region
                {
                    unsafe {
                        std::env::set_var("AWS_REGION", region);
                    }
                }
                // Load profile OR access keys (profile takes precedence)
                if let Some(profile) = &bedrock.profile
                    && std::env::var("AWS_PROFILE").is_err()
                {
                    unsafe {
                        std::env::set_var("AWS_PROFILE", profile);
                    }
                } else if let (Some(key_id), Some(secret)) =
                    (&bedrock.access_key_id, &bedrock.secret_access_key)
                {
                    if std::env::var("AWS_ACCESS_KEY_ID").is_err() {
                        unsafe {
                            std::env::set_var("AWS_ACCESS_KEY_ID", key_id);
                        }
                    }
                    if std::env::var("AWS_SECRET_ACCESS_KEY").is_err() {
                        unsafe {
                            std::env::set_var("AWS_SECRET_ACCESS_KEY", secret);
                        }
                    }
                }
            }
        }
    }
}

/// Get configured providers (those with API keys)
pub fn get_configured_providers() -> Vec<ProviderType> {
    let mut providers = Vec::new();
    if has_api_key(ProviderType::OpenAI) {
        providers.push(ProviderType::OpenAI);
    }
    if has_api_key(ProviderType::Anthropic) {
        providers.push(ProviderType::Anthropic);
    }
    providers
}

/// Interactive wizard to set up AWS Bedrock credentials
pub(crate) fn run_bedrock_setup_wizard() -> AgentResult<String> {
    use crate::config::types::BedrockConfig as BedrockConfigType;

    println!();
    println!(
        "{}",
        "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━".cyan()
    );
    println!("{}", "  AWS Bedrock Setup Wizard".cyan().bold());
    println!(
        "{}",
        "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━".cyan()
    );
    println!();
    println!("AWS Bedrock provides access to Claude models via AWS.");
    println!("You'll need an AWS account with Bedrock access enabled.");
    println!();

    // Step 1: Choose authentication method
    println!("{}", "Step 1: Choose authentication method".white().bold());
    println!();
    println!(
        "  {} Use AWS Profile (from ~/.aws/credentials)",
        "[1]".cyan()
    );
    println!(
        "      {}",
        "Best for: AWS CLI users, SSO, multiple accounts".dimmed()
    );
    println!();
    println!("  {} Enter Access Keys directly", "[2]".cyan());
    println!(
        "      {}",
        "Best for: Quick setup, CI/CD environments".dimmed()
    );
    println!();
    println!("  {} Use existing environment variables", "[3]".cyan());
    println!(
        "      {}",
        "Best for: Already configured AWS_* env vars".dimmed()
    );
    println!();
    print!("Enter choice [1-3]: ");
    io::stdout().flush().unwrap();

    let mut choice = String::new();
    io::stdin()
        .read_line(&mut choice)
        .map_err(|e| AgentError::ToolError(e.to_string()))?;
    let choice = choice.trim();

    let mut bedrock_config = BedrockConfigType::default();

    match choice {
        "1" => {
            // AWS Profile
            println!();
            println!("{}", "Step 2: Enter AWS Profile".white().bold());
            println!("{}", "Press Enter for 'default' profile".dimmed());
            print!("Profile name: ");
            io::stdout().flush().unwrap();

            let mut profile = String::new();
            io::stdin()
                .read_line(&mut profile)
                .map_err(|e| AgentError::ToolError(e.to_string()))?;
            let profile = profile.trim();
            let profile = if profile.is_empty() {
                "default"
            } else {
                profile
            };

            bedrock_config.profile = Some(profile.to_string());

            // Set in env for current session
            unsafe {
                std::env::set_var("AWS_PROFILE", profile);
            }
            println!("{}", format!("Using profile: {}", profile).green());
        }
        "2" => {
            // Access Keys
            println!();
            println!("{}", "Step 2: Enter AWS Access Keys".white().bold());
            println!(
                "{}",
                "Get these from AWS Console -> IAM -> Security credentials".dimmed()
            );
            println!();

            print!("AWS Access Key ID: ");
            io::stdout().flush().unwrap();
            let mut access_key = String::new();
            io::stdin()
                .read_line(&mut access_key)
                .map_err(|e| AgentError::ToolError(e.to_string()))?;
            let access_key = access_key.trim().to_string();

            if access_key.is_empty() {
                return Err(AgentError::MissingApiKey("AWS_ACCESS_KEY_ID".to_string()));
            }

            print!("AWS Secret Access Key: ");
            io::stdout().flush().unwrap();
            let mut secret_key = String::new();
            io::stdin()
                .read_line(&mut secret_key)
                .map_err(|e| AgentError::ToolError(e.to_string()))?;
            let secret_key = secret_key.trim().to_string();

            if secret_key.is_empty() {
                return Err(AgentError::MissingApiKey(
                    "AWS_SECRET_ACCESS_KEY".to_string(),
                ));
            }

            bedrock_config.access_key_id = Some(access_key.clone());
            bedrock_config.secret_access_key = Some(secret_key.clone());

            // Set in env for current session
            unsafe {
                std::env::set_var("AWS_ACCESS_KEY_ID", &access_key);
                std::env::set_var("AWS_SECRET_ACCESS_KEY", &secret_key);
            }
            println!("{}", "Access keys configured".green());
        }
        "3" => {
            // Use existing env vars
            if std::env::var("AWS_ACCESS_KEY_ID").is_err() && std::env::var("AWS_PROFILE").is_err()
            {
                println!("{}", "No AWS credentials found in environment!".yellow());
                println!("Set AWS_ACCESS_KEY_ID + AWS_SECRET_ACCESS_KEY or AWS_PROFILE");
                return Err(AgentError::MissingApiKey("AWS credentials".to_string()));
            }
            println!("{}", "Using existing environment variables".green());
        }
        _ => {
            println!("{}", "Invalid choice, using environment variables".yellow());
        }
    }

    // Step 2: Region selection
    if bedrock_config.region.is_none() {
        println!();
        println!("{}", "Step 2: Select AWS Region".white().bold());
        println!(
            "{}",
            "Bedrock is available in select regions. Common choices:".dimmed()
        );
        println!();
        println!(
            "  {} us-east-1     (N. Virginia) - Most models",
            "[1]".cyan()
        );
        println!("  {} us-west-2     (Oregon)", "[2]".cyan());
        println!("  {} eu-west-1     (Ireland)", "[3]".cyan());
        println!("  {} ap-northeast-1 (Tokyo)", "[4]".cyan());
        println!();
        print!("Enter choice [1-4] or region name: ");
        io::stdout().flush().unwrap();

        let mut region_choice = String::new();
        io::stdin()
            .read_line(&mut region_choice)
            .map_err(|e| AgentError::ToolError(e.to_string()))?;
        let region = match region_choice.trim() {
            "1" | "" => "us-east-1",
            "2" => "us-west-2",
            "3" => "eu-west-1",
            "4" => "ap-northeast-1",
            other => other,
        };

        bedrock_config.region = Some(region.to_string());
        unsafe {
            std::env::set_var("AWS_REGION", region);
        }
        println!("{}", format!("Region: {}", region).green());
    }

    // Step 3: Model selection
    println!();
    println!("{}", "Step 3: Select Default Model".white().bold());
    println!();
    let models = get_available_models(ProviderType::Bedrock);
    for (i, (id, desc)) in models.iter().enumerate() {
        let marker = if i == 0 { "-> " } else { "  " };
        println!("  {} {} {}", marker, format!("[{}]", i + 1).cyan(), desc);
        println!("      {}", id.dimmed());
    }
    println!();
    print!("Enter choice [1-{}] (default: 1): ", models.len());
    io::stdout().flush().unwrap();

    let mut model_choice = String::new();
    io::stdin()
        .read_line(&mut model_choice)
        .map_err(|e| AgentError::ToolError(e.to_string()))?;
    let model_idx: usize = model_choice.trim().parse().unwrap_or(1);
    let model_idx = model_idx.saturating_sub(1).min(models.len() - 1);
    let selected_model = models[model_idx].0.to_string();

    bedrock_config.default_model = Some(selected_model.clone());
    println!(
        "{}",
        format!(
            "Default model: {}",
            models[model_idx]
                .1
                .split(" - ")
                .next()
                .unwrap_or(&selected_model)
        )
        .green()
    );

    // Save configuration
    let mut agent_config = load_agent_config();
    agent_config.bedrock = Some(bedrock_config);
    agent_config.bedrock_configured = Some(true);

    if let Err(e) = save_agent_config(&agent_config) {
        eprintln!(
            "{}",
            format!("Warning: Could not save config: {}", e).yellow()
        );
    } else {
        println!();
        println!("{}", "Configuration saved to ~/.syncable.toml".green());
    }

    println!();
    println!(
        "{}",
        "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━".cyan()
    );
    println!("{}", "  AWS Bedrock setup complete!".green().bold());
    println!(
        "{}",
        "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━".cyan()
    );
    println!();

    Ok(selected_model)
}

/// Prompt user to enter API key for a provider
pub fn prompt_api_key(provider: ProviderType) -> AgentResult<String> {
    // Bedrock uses AWS credential chain - run setup wizard
    if matches!(provider, ProviderType::Bedrock) {
        return run_bedrock_setup_wizard();
    }

    let env_var = match provider {
        ProviderType::OpenAI => "OPENAI_API_KEY",
        ProviderType::Anthropic => "ANTHROPIC_API_KEY",
        ProviderType::Bedrock => unreachable!(), // Handled above
    };

    println!(
        "\n{}",
        format!("No API key found for {}", provider).yellow()
    );
    println!("Please enter your {} API key:", provider);
    print!("> ");
    io::stdout().flush().unwrap();

    let mut key = String::new();
    io::stdin()
        .read_line(&mut key)
        .map_err(|e| AgentError::ToolError(e.to_string()))?;
    let key = key.trim().to_string();

    if key.is_empty() {
        return Err(AgentError::MissingApiKey(env_var.to_string()));
    }

    // Set for current session
    // SAFETY: We're in a single-threaded CLI context during initialization
    unsafe {
        std::env::set_var(env_var, &key);
    }

    // Save to config file for persistence
    let mut agent_config = load_agent_config();
    match provider {
        ProviderType::OpenAI => agent_config.openai_api_key = Some(key.clone()),
        ProviderType::Anthropic => agent_config.anthropic_api_key = Some(key.clone()),
        ProviderType::Bedrock => unreachable!(), // Handled above
    }

    if let Err(e) = save_agent_config(&agent_config) {
        eprintln!(
            "{}",
            format!("Warning: Could not save config: {}", e).yellow()
        );
    } else {
        println!("{}", "API key saved to ~/.syncable.toml".green());
    }

    Ok(key)
}