ai-agent 0.88.0

Idiomatic agent sdk inspired by the claude code source leak
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
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
// Source: /data/home/swei/claudecode/openclaudecode/src/types/generated/events_mono/common/v1/auth.ts
#![allow(dead_code)]

use crate::constants::env::{anthropic, ai, ai_code, system};
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use std::time::{Duration, SystemTime, UNIX_EPOCH};

use once_cell::sync::Lazy;

// Default TTL for API key helper cache in milliseconds (5 minutes)
const DEFAULT_API_KEY_HELPER_TTL: u64 = 5 * 60 * 1000;

// Default STS credentials are one hour
const DEFAULT_AWS_STS_TTL: u64 = 60 * 60 * 1000;

// Timeout for AWS auth refresh command (3 minutes)
const AWS_AUTH_REFRESH_TIMEOUT_MS: u64 = 3 * 60 * 1000;

// Short timeout for the GCP credentials probe
const GCP_CREDENTIALS_CHECK_TIMEOUT_MS: u64 = 5_000;

// Default GCP credential TTL - 1 hour
const DEFAULT_GCP_CREDENTIAL_TTL: u64 = 60 * 60 * 1000;

#[derive(Debug, Clone, PartialEq)]
pub enum ApiKeySource {
    AnthropicApiKey,
    ApiKeyHelper,
    LoginManagedKey,
    None,
}

#[derive(Debug, Clone)]
pub struct ApiKeyResult {
    pub key: Option<String>,
    pub source: ApiKeySource,
}

/// CCR and Claude Desktop spawn the CLI with OAuth and should never fall back
/// to the user's ~/.ai/settings.json API-key config (apiKeyHelper,
/// env.AI_API_KEY, env.AI_AUTH_TOKEN). Those settings exist for
/// the user's terminal CLI, not managed sessions.
fn is_managed_oauth_context() -> bool {
    std::env::var(ai::REMOTE).is_ok()
        || std::env::var(ai::CODE_ENTRYPOINT).map(|v| v == "claude-desktop").unwrap_or(false)
}

/// Whether we are supporting direct 1P auth.
pub fn is_anthropic_auth_enabled() -> bool {
    // --bare: API-key-only, never OAuth.
    if is_bare_mode() {
        return false;
    }

    // `claude ssh` remote: ANTHROPIC_UNIX_SOCKET tunnels API calls through a
    // local auth-injecting proxy.
    if std::env::var(anthropic::UNIX_SOCKET).is_ok() {
        return std::env::var(ai_code::OAUTH_TOKEN).is_ok();
    }

    let is_3p = is_env_truthy("AI_CODE_USE_BEDROCK")
        || is_env_truthy("AI_CODE_USE_VERTEX")
        || is_env_truthy("AI_CODE_USE_FOUNDRY");

    // Check if user has configured an external API key source
    let settings = get_settings_deprecated().unwrap_or_default();
    let api_key_helper = settings.get("apiKeyHelper").cloned();
    let has_external_auth_token = std::env::var(anthropic::AUTH_TOKEN).is_ok()
        || api_key_helper.is_some()
        || std::env::var(ai_code::API_KEY_FILE_DESCRIPTOR).is_ok();

    // Check if API key is from an external source
    let api_key_source = get_anthropic_api_key_with_source_internal(true).source;
    let has_external_api_key = api_key_source == ApiKeySource::AnthropicApiKey
        || api_key_source == ApiKeySource::ApiKeyHelper;

    // Disable Anthropic auth if:
    // 1. Using 3rd party services (Bedrock/Vertex/Foundry)
    // 2. User has an external API key (regardless of proxy configuration)
    // 3. User has an external auth token (regardless of proxy configuration)
    let should_disable_auth = is_3p
        || (has_external_auth_token && !is_managed_oauth_context())
        || (has_external_api_key && !is_managed_oauth_context());

    !should_disable_auth
}

/// Where the auth token is being sourced from, if any.
pub fn get_auth_token_source() -> (String, bool) {
    // --bare: API-key-only
    if is_bare_mode() {
        if get_configured_api_key_helper().is_some() {
            return ("apiKeyHelper".to_string(), true);
        }
        return ("none".to_string(), false);
    }

    if std::env::var(anthropic::AUTH_TOKEN).is_ok() && !is_managed_oauth_context() {
        return (anthropic::AUTH_TOKEN.to_string(), true);
    }

    if std::env::var(ai_code::OAUTH_TOKEN).is_ok() {
        return ("AI_CODE_OAUTH_TOKEN".to_string(), true);
    }

    // Check for OAuth token from file descriptor
    if let Some(token) = get_oauth_token_from_file_descriptor() {
        if std::env::var(ai_code::OAUTH_TOKEN_FILE_DESCRIPTOR).is_ok() {
            return ("AI_CODE_OAUTH_TOKEN_FILE_DESCRIPTOR".to_string(), true);
        }
        return ("CCR_OAUTH_TOKEN_FILE".to_string(), true);
    }

    // Check if apiKeyHelper is configured
    let api_key_helper = get_configured_api_key_helper();
    if api_key_helper.is_some() && !is_managed_oauth_context() {
        return ("apiKeyHelper".to_string(), true);
    }

    // Check for Claude.ai OAuth tokens
    let oauth_tokens = get_claude_ai_oauth_tokens();
    if should_use_claude_ai_auth(&oauth_tokens) && oauth_tokens.as_ref().map(|t| t.access_token.is_some()).unwrap_or(false) {
        return ("claude.ai".to_string(), true);
    }

    ("none".to_string(), false)
}

pub fn get_anthropic_api_key() -> Option<String> {
    let result = get_anthropic_api_key_with_source_internal(false);
    result.key
}

pub fn has_anthropic_api_key_auth() -> bool {
    let result = get_anthropic_api_key_with_source_internal(true);
    result.key.is_some() && result.source != ApiKeySource::None
}

pub fn get_anthropic_api_key_with_source(
    skip_retrieving_key_from_api_key_helper: bool,
) -> ApiKeyResult {
    get_anthropic_api_key_with_source_internal(skip_retrieving_key_from_api_key_helper)
}

fn get_anthropic_api_key_with_source_internal(
    skip_retrieving_key_from_api_key_helper: bool,
) -> ApiKeyResult {
    // --bare: hermetic auth
    if is_bare_mode() {
        if let Ok(key) = std::env::var(anthropic::API_KEY) {
            return ApiKeyResult {
                key: Some(key),
                source: ApiKeySource::AnthropicApiKey,
            };
        }
        if let Some(helper) = get_configured_api_key_helper() {
            return ApiKeyResult {
                key: if skip_retrieving_key_from_api_key_helper {
                    None
                } else {
                    get_api_key_from_api_key_helper_cached()
                },
                source: ApiKeySource::ApiKeyHelper,
            };
        }
        return ApiKeyResult {
            key: None,
            source: ApiKeySource::None,
        };
    }

    // On homespace, don't use ANTHROPIC_API_KEY (use Console key instead)
    let api_key_env = if is_running_on_homespace() {
        None
    } else {
        std::env::var(anthropic::API_KEY).ok()
    };

    // Always check for direct environment variable when the user ran claude --print
    if prefer_third_party_authentication() {
        if let Some(ref key) = api_key_env {
            return ApiKeyResult {
                key: Some(key.clone()),
                source: ApiKeySource::AnthropicApiKey,
            };
        }
    }

    // CI or test mode
    if std::env::var(system::CI).is_ok() || std::env::var(system::NODE_ENV).map(|v| v == "test").unwrap_or(false) {
        // Check for API key from file descriptor first
        if let Some(key) = get_api_key_from_file_descriptor() {
            return ApiKeyResult {
                key: Some(key),
                source: ApiKeySource::AnthropicApiKey,
            };
        }

        if api_key_env.is_none()
            && std::env::var(ai_code::OAUTH_TOKEN).is_err()
            && std::env::var(ai_code::OAUTH_TOKEN_FILE_DESCRIPTOR).is_err()
        {
            // In Rust we don't throw, return None
            return ApiKeyResult {
                key: None,
                source: ApiKeySource::None,
            };
        }

        if let Some(key) = api_key_env {
            return ApiKeyResult {
                key: Some(key),
                source: ApiKeySource::AnthropicApiKey,
            };
        }

        return ApiKeyResult {
            key: None,
            source: ApiKeySource::None,
        };
    }

    // Check for ANTHROPIC_API_KEY before checking the apiKeyHelper or /login-managed key
    if let Some(ref key) = api_key_env {
        let config = get_global_config();
        if let Some(approved) = config.custom_api_key_responses.as_ref().and_then(|r| r.approved.as_ref()) {
            let normalized = normalize_api_key_for_config(key);
            if approved.contains(&normalized) {
                return ApiKeyResult {
                    key: Some(key.clone()),
                    source: ApiKeySource::AnthropicApiKey,
                };
            }
        }
    }

    // Check for API key from file descriptor
    if let Some(key) = get_api_key_from_file_descriptor() {
        return ApiKeyResult {
            key: Some(key),
            source: ApiKeySource::AnthropicApiKey,
        };
    }

    // Check for apiKeyHelper
    if let Some(helper) = get_configured_api_key_helper() {
        if skip_retrieving_key_from_api_key_helper {
            return ApiKeyResult {
                key: None,
                source: ApiKeySource::ApiKeyHelper,
            };
        }
        return ApiKeyResult {
            key: get_api_key_from_api_key_helper_cached(),
            source: ApiKeySource::ApiKeyHelper,
        };
    }

    // Check config or macOS keychain
    if let Some(result) = get_api_key_from_config_or_macos_keychain() {
        return result;
    }

    ApiKeyResult {
        key: None,
        source: ApiKeySource::None,
    }
}

/// Get the configured apiKeyHelper from settings.
pub fn get_configured_api_key_helper() -> Option<String> {
    if is_bare_mode() {
        return get_settings_for_source("flagSettings")
            .and_then(|s| s.get("apiKeyHelper").cloned());
    }
    let settings = get_settings_deprecated().unwrap_or_default();
    settings.get("apiKeyHelper").cloned()
}

/// Calculate TTL in milliseconds for the API key helper cache
pub fn calculate_api_key_helper_ttl() -> u64 {
    if let Ok(env_ttl) = std::env::var(ai_code::API_KEY_HELPER_TTL_MS) {
        if let Ok(parsed) = env_ttl.parse::<u64>() {
            if parsed >= 0 {
                return parsed;
            }
            log_for_debugging(&format!(
                "Found AI_CODE_API_KEY_HELPER_TTL_MS env var, but it was not a valid number. Got {}",
                env_ttl
            ));
        }
    }
    DEFAULT_API_KEY_HELPER_TTL
}

// Async API key helper with sync cache for non-blocking reads.
struct ApiKeyHelperCache {
    value: Option<String>,
    timestamp: u64,
}

static API_KEY_HELPER_CACHE: Lazy<Mutex<Option<ApiKeyHelperCache>>> = Lazy::new(|| Mutex::new(None));
static API_KEY_HELPER_EPOCH: Lazy<Arc<Mutex<u32>>> = Lazy::new(|| Arc::new(Mutex::new(0)));

pub fn get_api_key_helper_elapsed_ms() -> u64 {
    // For now, simplified implementation
    0
}

/// Get API key from apiKeyHelper (async)
pub async fn get_api_key_from_api_key_helper(
    _is_non_interactive_session: bool,
) -> Option<String> {
    let helper = get_configured_api_key_helper()?;
    let ttl = calculate_api_key_helper_ttl();

    let mut cache = API_KEY_HELPER_CACHE.lock().ok()?;
    if let Some(ref cached) = *cache {
        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .map(|d| d.as_millis() as u64)
            .unwrap_or(0);
        if now - cached.timestamp < ttl {
            return cached.value.clone();
        }
    }
    drop(cache);

    // Execute the helper
    let result = execute_api_key_helper(&helper).await;
    
    // Update cache
    if let Ok(mut cache) = API_KEY_HELPER_CACHE.lock() {
        *cache = Some(ApiKeyHelperCache {
            value: result.clone(),
            timestamp: SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .map(|d| d.as_millis() as u64)
                .unwrap_or(0),
        });
    }

    result
}

async fn execute_api_key_helper(_helper: &str) -> Option<String> {
    // This would execute the apiKeyHelper command
    // Simplified for now - would use tokio::process::Command in real implementation
    None
}

/// Sync cache reader — returns the last fetched apiKeyHelper value without executing.
pub fn get_api_key_from_api_key_helper_cached() -> Option<String> {
    API_KEY_HELPER_CACHE
        .lock()
        .ok()
        .and_then(|cache| cache.as_ref().and_then(|c| c.value.clone()))
}

pub fn clear_api_key_helper_cache() {
    if let Ok(mut epoch) = API_KEY_HELPER_EPOCH.lock() {
        *epoch += 1;
    }
    if let Ok(mut cache) = API_KEY_HELPER_CACHE.lock() {
        *cache = None;
    }
}

pub fn prefetch_api_key_from_api_key_helper_if_safe(_is_non_interactive_session: bool) {
    // Skip if trust not yet accepted
    if is_api_key_helper_from_project_or_local_settings() && !check_has_trust_dialog_accepted() {
        return;
    }
    // Would call get_api_key_from_api_key_helper in background
}

// ============ OAuth Tokens ============

#[derive(Debug, Clone)]
pub struct OAuthTokens {
    pub access_token: Option<String>,
    pub refresh_token: Option<String>,
    pub expires_at: Option<u64>,
    pub scopes: Vec<String>,
    pub subscription_type: Option<String>,
    pub rate_limit_tier: Option<String>,
}

/// Save OAuth tokens to secure storage
pub fn save_oauth_tokens_if_needed(tokens: OAuthTokens) -> (bool, Option<String>) {
    if !should_use_claude_ai_auth(&tokens) {
        return (true, None);
    }

    // Skip saving inference-only tokens (they come from env vars)
    if tokens.refresh_token.is_none() || tokens.expires_at.is_none() {
        return (true, None);
    }

    // Would save to secure storage
    // For now, simplified
    (true, None)
}

/// Get Claude.ai OAuth tokens
pub fn get_claude_ai_oauth_tokens() -> Option<OAuthTokens> {
    // --bare: API-key-only
    if is_bare_mode() {
        return None;
    }

    // Check for force-set OAuth token from environment variable
    if let Ok(token) = std::env::var(ai_code::OAUTH_TOKEN) {
        return Some(OAuthTokens {
            access_token: Some(token),
            refresh_token: None,
            expires_at: None,
            scopes: vec!["user:inference".to_string()],
            subscription_type: None,
            rate_limit_tier: None,
        });
    }

    // Check for OAuth token from file descriptor
    if let Some(token) = get_oauth_token_from_file_descriptor() {
        return Some(OAuthTokens {
            access_token: Some(token),
            refresh_token: None,
            expires_at: None,
            scopes: vec!["user:inference".to_string()],
            subscription_type: None,
            rate_limit_tier: None,
        });
    }

    // Would read from secure storage
    None
}

/// Clear OAuth token cache
pub fn clear_oauth_token_cache() {
    // Would clear all OAuth caches
}

// ============ AWS Auth ============

/// Refresh AWS authentication
pub async fn refresh_aws_auth(aws_auth_refresh: &str) -> bool {
    // Simplified implementation
    // Would run the awsAuthRefresh command
    log_for_debugging(&format!("Running AWS auth refresh command: {}", aws_auth_refresh));
    false
}

/// Refresh and get AWS credentials
pub async fn refresh_and_get_aws_credentials() -> Option<AwsCredentials> {
    // First check if caller identity is valid
    match check_sts_caller_identity().await {
        Ok(_) => {
            log_for_debugging("Fetched AWS caller identity, skipping AWS auth refresh command");
            return None;
        }
        Err(_) => {
            // Need to refresh
        }
    }

    // Run auth refresh if needed
    let refreshed = refresh_aws_auth_internal().await;

    // Get credentials from export
    let credentials = get_aws_creds_from_credential_export().await;

    // Clear AWS INI cache if we refreshed or got credentials
    if refreshed || credentials.is_some() {
        clear_aws_ini_cache().await;
    }

    credentials
}

async fn refresh_aws_auth_internal() -> bool {
    let aws_auth_refresh = get_configured_aws_auth_refresh()?;
    
    // Check if from project settings and trust required
    if is_aws_auth_refresh_from_project_settings() {
        if !check_has_trust_dialog_accepted() && !get_is_non_interactive_session() {
            return false;
        }
    }

    refresh_aws_auth(&aws_auth_refresh).await
}

pub fn clear_aws_credentials_cache() {
    // Would clear the memoized cache
}

#[derive(Debug, Clone)]
pub struct AwsCredentials {
    pub access_key_id: String,
    pub secret_access_key: String,
    pub session_token: String,
}

async fn get_aws_creds_from_credential_export() -> Option<AwsCredentials> {
    let aws_credential_export = get_configured_aws_credential_export()?;

    // SECURITY: Check if from project settings
    if is_aws_credential_export_from_project_settings() {
        if !check_has_trust_dialog_accepted() && !get_is_non_interactive_session() {
            return None;
        }
    }

    // Check if credentials are already valid
    match check_sts_caller_identity().await {
        Ok(_) => {
            log_for_debugging("Fetched AWS caller identity, skipping AWS credential export command");
            return None;
        }
        Err(_) => {}
    }

    // Would execute awsCredentialExport and parse JSON
    None
}

async fn check_sts_caller_identity() -> Result<(), String> {
    // Simplified - would use AWS SDK
    Ok(())
}

async fn clear_aws_ini_cache() {
    // Would clear AWS INI cache
}

// ============ GCP Auth ============

/// Check if GCP credentials are currently valid
pub async fn check_gcp_credentials_valid() -> bool {
    // Would use google-auth-library
    // Simplified for now
    false
}

/// Refresh GCP authentication
pub async fn refresh_gcp_auth(gcp_auth_refresh: &str) -> bool {
    log_for_debugging(&format!("Running GCP auth refresh command: {}", gcp_auth_refresh));
    false
}

/// Refresh GCP credentials if needed
pub async fn refresh_gcp_credentials_if_needed() -> bool {
    let refreshed = run_gcp_auth_refresh().await;
    refreshed
}

async fn run_gcp_auth_refresh() -> bool {
    let gcp_auth_refresh = get_configured_gcp_auth_refresh()?;

    if is_gcp_auth_refresh_from_project_settings() {
        if !check_has_trust_dialog_accepted() && !get_is_non_interactive_session() {
            return false;
        }
    }

    // Check if credentials are valid
    if check_gcp_credentials_valid().await {
        log_for_debugging("GCP credentials are valid, skipping auth refresh command");
        return false;
    }

    refresh_gcp_auth(&gcp_auth_refresh).await
}

pub fn clear_gcp_credentials_cache() {
    // Would clear cache
}

pub fn prefetch_gcp_credentials_if_safe() {
    let gcp_auth_refresh = get_configured_gcp_auth_refresh();
    if gcp_auth_refresh.is_none() {
        return;
    }

    if is_gcp_auth_refresh_from_project_settings() {
        if !check_has_trust_dialog_accepted() && !get_is_non_interactive_session() {
            return;
        }
    }

    // Would prefetch in background
}

pub fn prefetch_aws_credentials_and_bedrock_info_if_safe() {
    let aws_auth_refresh = get_configured_aws_auth_refresh();
    let aws_credential_export = get_configured_aws_credential_export();

    if aws_auth_refresh.is_none() && aws_credential_export.is_none() {
        return;
    }

    if is_aws_auth_refresh_from_project_settings() || is_aws_credential_export_from_project_settings() {
        if !check_has_trust_dialog_accepted() && !get_is_non_interactive_session() {
            return;
        }
    }

    // Would prefetch in background
}

// ============ API Key Management ============

pub async fn save_api_key(api_key: &str) -> Result<(), String> {
    if !is_valid_api_key(api_key) {
        return Err("Invalid API key format".to_string());
    }

    // Would save to keychain or config
    log_for_debugging("API key saved");
    Ok(())
}

pub fn is_custom_api_key_approved(api_key: &str) -> bool {
    let config = get_global_config();
    let normalized = normalize_api_key_for_config(api_key);
    config
        .custom_api_key_responses
        .as_ref()
        .and_then(|r| r.approved.as_ref())
        .map(|approved| approved.contains(&normalized))
        .unwrap_or(false)
}

pub async fn remove_api_key() {
    // Would remove from keychain and config
}

pub fn get_subscription_type() -> Option<String> {
    // Would get from OAuth tokens or config
    None
}

// ============ Helper Functions ============

fn is_bare_mode() -> bool {
    std::env::var(ai_code::BARE).is_ok()
}

fn is_env_truthy(var: &str) -> bool {
    std::env::var(var).map(|v| v == "1" || v == "true").unwrap_or(false)
}

fn is_running_on_homespace() -> bool {
    std::env::var(ai_code::HOMESPACE).is_ok()
}

fn prefer_third_party_authentication() -> bool {
    std::env::var(ai_code::PREFER_THIRD_PARTY).is_ok()
}

fn is_api_key_helper_from_project_or_local_settings() -> bool {
    let helper = match get_configured_api_key_helper() {
        Some(h) => h,
        None => return false,
    };

    let project_settings = get_settings_for_source("projectSettings");
    let local_settings = get_settings_for_source("localSettings");

    project_settings
        .and_then(|s| s.get("apiKeyHelper").cloned())
        .map(|h| h == helper)
        .unwrap_or(false)
        || local_settings
            .and_then(|s| s.get("apiKeyHelper").cloned())
            .map(|h| h == helper)
            .unwrap_or(false)
}

fn get_configured_aws_auth_refresh() -> Option<String> {
    get_settings_deprecated()
        .and_then(|s| s.get("awsAuthRefresh").cloned())
}

fn is_aws_auth_refresh_from_project_settings() -> bool {
    let refresh = match get_configured_aws_auth_refresh() {
        Some(r) => r,
        None => return false,
    };

    let project_settings = get_settings_for_source("projectSettings");
    let local_settings = get_settings_for_source("localSettings");

    project_settings
        .and_then(|s| s.get("awsAuthRefresh").cloned())
        .map(|r| r == refresh)
        .unwrap_or(false)
        || local_settings
            .and_then(|s| s.get("awsAuthRefresh").cloned())
            .map(|r| r == refresh)
            .unwrap_or(false)
}

fn get_configured_aws_credential_export() -> Option<String> {
    get_settings_deprecated()
        .and_then(|s| s.get("awsCredentialExport").cloned())
}

fn is_aws_credential_export_from_project_settings() -> bool {
    let export = match get_configured_aws_credential_export() {
        Some(e) => e,
        None => return false,
    };

    let project_settings = get_settings_for_source("projectSettings");
    let local_settings = get_settings_for_source("localSettings");

    project_settings
        .and_then(|s| s.get("awsCredentialExport").cloned())
        .map(|e| e == export)
        .unwrap_or(false)
        || local_settings
            .and_then(|s| s.get("awsCredentialExport").cloned())
            .map(|e| e == export)
            .unwrap_or(false)
}

fn get_configured_gcp_auth_refresh() -> Option<String> {
    get_settings_deprecated()
        .and_then(|s| s.get("gcpAuthRefresh").cloned())
}

fn is_gcp_auth_refresh_from_project_settings() -> bool {
    let refresh = match get_configured_gcp_auth_refresh() {
        Some(r) => r,
        None => return false,
    };

    let project_settings = get_settings_for_source("projectSettings");
    let local_settings = get_settings_for_source("localSettings");

    project_settings
        .and_then(|s| s.get("gcpAuthRefresh").cloned())
        .map(|r| r == refresh)
        .unwrap_or(false)
        || local_settings
            .and_then(|s| s.get("gcpAuthRefresh").cloned())
            .map(|r| r == refresh)
            .unwrap_or(false)
}

fn get_settings_deprecated() -> Option<HashMap<String, String>> {
    // Would read from settings file
    None
}

fn get_settings_for_source(_source: &str) -> Option<HashMap<String, String>> {
    // Would read from specific source
    None
}

#[derive(Debug, Clone, Default)]
pub struct GlobalConfig {
    pub primary_api_key: Option<String>,
    pub custom_api_key_responses: Option<CustomApiKeyResponses>,
}

#[derive(Debug, Clone, Default)]
pub struct CustomApiKeyResponses {
    pub approved: Option<Vec<String>>,
    pub rejected: Option<Vec<String>>,
}

fn get_global_config() -> GlobalConfig {
    // Would read from config
    GlobalConfig::default()
}

fn get_api_key_from_config_or_macos_keychain() -> Option<ApiKeyResult> {
    // Would check macOS keychain or config file
    None
}

fn get_api_key_from_file_descriptor() -> Option<String> {
    // Would read from file descriptor
    None
}

fn get_oauth_token_from_file_descriptor() -> Option<String> {
    // Would read from file descriptor
    None
}

fn check_has_trust_dialog_accepted() -> bool {
    // Would check if trust dialog was accepted
    true
}

fn get_is_non_interactive_session() -> bool {
    std::env::var(ai_code::NON_INTERACTIVE).is_ok()
}

fn normalize_api_key_for_config(api_key: &str) -> String {
    // Would normalize the API key
    api_key.to_string()
}

fn is_valid_api_key(api_key: &str) -> bool {
    // Only allow alphanumeric characters, dashes, and underscores
    api_key.chars().all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_')
}

fn should_use_claude_ai_auth(tokens: &Option<OAuthTokens>) -> bool {
    tokens
        .as_ref()
        .map(|t| t.scopes.iter().any(|s| s.contains("user")))
        .unwrap_or(false)
}

fn log_for_debugging(message: &str) {
    eprintln!("[DEBUG] {}", message);
}

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

    #[test]
    fn test_is_valid_api_key() {
        assert!(is_valid_api_key("sk-ant-api03-abc123"));
        assert!(is_valid_api_key("abc-123"));
        assert!(!is_valid_api_key("sk-ant-api03@#!"));
    }

    #[test]
    fn test_normalize_api_key() {
        let normalized = normalize_api_key_for_config("  sk-ant-api03-abc  ");
        assert!(normalized.contains("sk-ant-api03"));
    }
}