rusty-commit 1.0.25

Rust-powered AI commit message generator - Write impressive commits in seconds
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
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
// AI Provider modules - conditionally compiled based on features
#[cfg(feature = "anthropic")]
pub mod anthropic;
#[cfg(feature = "azure")]
pub mod azure;
#[cfg(feature = "bedrock")]
pub mod bedrock;
#[cfg(feature = "flowise")]
pub mod flowise;
#[cfg(feature = "gemini")]
pub mod gemini;
#[cfg(feature = "huggingface")]
pub mod huggingface;
#[cfg(feature = "mlx")]
pub mod mlx;
#[cfg(feature = "nvidia")]
pub mod nvidia;
#[cfg(feature = "ollama")]
pub mod ollama;
#[cfg(feature = "openai")]
pub mod openai;
#[cfg(feature = "perplexity")]
pub mod perplexity;
#[cfg(feature = "vertex")]
pub mod vertex;
#[cfg(feature = "xai")]
pub mod xai;

// Provider registry for extensible provider management
pub mod registry;

use crate::config::accounts::AccountConfig;
use crate::config::Config;
use anyhow::{Context, Result};
use async_trait::async_trait;
use once_cell::sync::Lazy;

#[async_trait]
pub trait AIProvider: Send + Sync {
    async fn generate_commit_message(
        &self,
        diff: &str,
        context: Option<&str>,
        full_gitmoji: bool,
        config: &Config,
    ) -> Result<String>;

    /// Generate multiple commit message variations
    async fn generate_commit_messages(
        &self,
        diff: &str,
        context: Option<&str>,
        full_gitmoji: bool,
        config: &Config,
        count: u8,
    ) -> Result<Vec<String>> {
        use futures::stream::StreamExt;

        if count <= 1 {
            // For single message, no parallelism needed
            match self
                .generate_commit_message(diff, context, full_gitmoji, config)
                .await
            {
                Ok(msg) => Ok(vec![msg]),
                Err(e) => {
                    tracing::warn!("Failed to generate message: {}", e);
                    Ok(vec![])
                }
            }
        } else {
            // Generate messages in parallel using FuturesUnordered
            let futures = (0..count)
                .map(|_| self.generate_commit_message(diff, context, full_gitmoji, config));
            let mut stream = futures::stream::FuturesUnordered::from_iter(futures);

            let mut messages = Vec::with_capacity(count as usize);
            while let Some(result) = stream.next().await {
                match result {
                    Ok(msg) => messages.push(msg),
                    Err(e) => tracing::warn!("Failed to generate message: {}", e),
                }
            }
            Ok(messages)
        }
    }

    /// Generate a PR description from commits
    #[cfg(any(feature = "openai", feature = "xai"))]
    async fn generate_pr_description(
        &self,
        commits: &[String],
        diff: &str,
        config: &Config,
    ) -> Result<String> {
        let commits_text = commits.join("\n");
        let prompt = format!(
            "Generate a professional pull request description based on the following commits:\n\n{}\n\nDiff:\n{}\n\nFormat the output as:\n## Summary\n## Changes\n## Testing\n## Breaking Changes\n\nKeep it concise and informative.",
            commits_text, diff
        );

        let messages = vec![
            async_openai::types::chat::ChatCompletionRequestSystemMessage::from(
                "You are an expert at writing pull request descriptions.",
            )
            .into(),
            async_openai::types::chat::ChatCompletionRequestUserMessage::from(prompt).into(),
        ];

        let request = async_openai::types::chat::CreateChatCompletionRequestArgs::default()
            .model(
                config
                    .model
                    .clone()
                    .unwrap_or_else(|| "gpt-3.5-turbo".to_string()),
            )
            .messages(messages)
            .temperature(0.7)
            .max_tokens(config.tokens_max_output.unwrap_or(1000) as u16)
            .build()?;

        // Create a new client for this request
        let api_key = config
            .api_key
            .as_ref()
            .context("API key not configured. Run: rco config set RCO_API_KEY=<your_key>")?;
        let api_url = config
            .api_url
            .as_deref()
            .unwrap_or("https://api.openai.com/v1");

        let openai_config = async_openai::config::OpenAIConfig::new()
            .with_api_key(api_key)
            .with_api_base(api_url);

        let client = async_openai::Client::with_config(openai_config);

        let response = client.chat().create(request).await?;

        let message = response
            .choices
            .first()
            .and_then(|choice| choice.message.content.as_ref())
            .context("AI returned an empty response")?
            .trim()
            .to_string();

        Ok(message)
    }

    /// Generate a PR description - stub when OpenAI/xAI features are disabled
    #[cfg(not(any(feature = "openai", feature = "xai")))]
    async fn generate_pr_description(
        &self,
        _commits: &[String],
        _diff: &str,
        _config: &Config,
    ) -> Result<String> {
        anyhow::bail!(
            "PR description generation requires the 'openai' or 'xai' feature to be enabled"
        );
    }
}

/// Global provider registry - automatically populated based on enabled features
pub static PROVIDER_REGISTRY: Lazy<registry::ProviderRegistry> = Lazy::new(|| {
    let reg = registry::ProviderRegistry::new();

    // Register OpenAI-compatible providers (require openai feature)
    #[cfg(feature = "openai")]
    {
        let _ = reg.register(Box::new(openai::OpenAICompatibleProvider::new()));
    }

    // Register dedicated providers
    #[cfg(feature = "anthropic")]
    {
        let _ = reg.register(Box::new(anthropic::AnthropicProviderBuilder));
    }

    #[cfg(feature = "ollama")]
    {
        let _ = reg.register(Box::new(ollama::OllamaProviderBuilder));
    }

    #[cfg(feature = "gemini")]
    {
        let _ = reg.register(Box::new(gemini::GeminiProviderBuilder));
    }

    #[cfg(feature = "azure")]
    {
        let _ = reg.register(Box::new(azure::AzureProviderBuilder));
    }

    #[cfg(feature = "perplexity")]
    {
        let _ = reg.register(Box::new(perplexity::PerplexityProviderBuilder));
    }

    #[cfg(feature = "xai")]
    {
        let _ = reg.register(Box::new(xai::XAIProviderBuilder));
    }

    #[cfg(feature = "huggingface")]
    {
        let _ = reg.register(Box::new(huggingface::HuggingFaceProviderBuilder));
    }

    #[cfg(feature = "bedrock")]
    {
        let _ = reg.register(Box::new(bedrock::BedrockProviderBuilder));
    }

    #[cfg(feature = "vertex")]
    {
        let _ = reg.register(Box::new(vertex::VertexProviderBuilder));
    }

    #[cfg(feature = "mlx")]
    {
        let _ = reg.register(Box::new(mlx::MlxProviderBuilder));
    }

    #[cfg(feature = "nvidia")]
    {
        let _ = reg.register(Box::new(nvidia::NvidiaProviderBuilder));
    }

    #[cfg(feature = "flowise")]
    {
        let _ = reg.register(Box::new(flowise::FlowiseProviderBuilder));
    }

    reg
});

/// Create an AI provider instance from configuration
pub fn create_provider(config: &Config) -> Result<Box<dyn AIProvider>> {
    let provider_name = config.ai_provider.as_deref().unwrap_or("openai");

    // Try to create from registry
    if let Some(provider) = PROVIDER_REGISTRY.create(provider_name, config)? {
        return Ok(provider);
    }

    // Provider not found - build error message with available providers
    let available: Vec<String> = PROVIDER_REGISTRY
        .all()
        .unwrap_or_default()
        .iter()
        .map(|e| {
            let aliases = if e.aliases.is_empty() {
                String::new()
            } else {
                format!(" ({})", e.aliases.join(", "))
            };
            format!("- {}{}", e.name, aliases)
        })
        .chain(std::iter::once(format!(
            "- {} OpenAI-compatible providers (deepseek, groq, openrouter, etc.)",
            PROVIDER_REGISTRY
                .by_category(registry::ProviderCategory::OpenAICompatible)
                .map_or(0, |v| v.len())
        )))
        .filter(|s| !s.contains("0 OpenAI-compatible"))
        .collect();

    if available.is_empty() {
        anyhow::bail!(
            "No AI provider features enabled. Please enable at least one provider feature:\n\
             --features openai,anthropic,ollama,gemini,azure,perplexity,xai,huggingface,bedrock,vertex"
        );
    }

    anyhow::bail!(
        "Unsupported or disabled AI provider: {}\n\n\
         Available providers (based on enabled features):\n{}\n\n\
         Set with: rco config set RCO_AI_PROVIDER=<provider_name>",
        provider_name,
        available.join("\n")
    )
}

#[allow(dead_code)]
/// Get list of all available provider names
pub fn available_providers() -> Vec<&'static str> {
    let mut providers = PROVIDER_REGISTRY
        .all()
        .unwrap_or_default()
        .iter()
        .flat_map(|e| std::iter::once(e.name).chain(e.aliases.iter().copied()))
        .collect::<Vec<_>>();

    #[cfg(feature = "openai")]
    {
        providers.extend_from_slice(&[
            // ═════════════════════════════════════════════════════════════════
            // Major Cloud Providers
            // ═════════════════════════════════════════════════════════════════
            "deepseek",
            "groq",
            "openrouter",
            "together",
            "deepinfra",
            "mistral",
            "github-models",
            "fireworks",
            "moonshot",
            "dashscope",
            "perplexity",
            // ═════════════════════════════════════════════════════════════════
            // Enterprise & Specialized
            // ═════════════════════════════════════════════════════════════════
            "cohere",
            "cohere-ai",
            "ai21",
            "ai21-labs",
            "upstage",
            "upstage-ai",
            "solar",
            "solar-pro",
            // ═════════════════════════════════════════════════════════════════
            // GPU Cloud & Inference Providers
            // ═════════════════════════════════════════════════════════════════
            "nebius",
            "nebius-ai",
            "nebius-studio",
            "ovh",
            "ovhcloud",
            "ovh-ai",
            "scaleway",
            "scaleway-ai",
            "friendli",
            "friendli-ai",
            "baseten",
            "baseten-ai",
            "chutes",
            "chutes-ai",
            "ionet",
            "io-net",
            "modelscope",
            "requesty",
            "morph",
            "morph-labs",
            "synthetic",
            "nano-gpt",
            "nanogpt",
            "zenmux",
            "v0",
            "v0-vercel",
            "iflowcn",
            "venice",
            "venice-ai",
            "cortecs",
            "cortecs-ai",
            "kimi-coding",
            "abacus",
            "abacus-ai",
            "bailing",
            "fastrouter",
            "inference",
            "inference-net",
            "submodel",
            "zai",
            "zai-coding",
            "zhipu-coding",
            "poe",
            "poe-ai",
            "cerebras",
            "cerebras-ai",
            "sambanova",
            "sambanova-ai",
            "novita",
            "novita-ai",
            "predibase",
            "tensorops",
            "hyperbolic",
            "hyperbolic-ai",
            "kluster",
            "kluster-ai",
            "lambda",
            "lambda-labs",
            "replicate",
            "targon",
            "corcel",
            "cybernative",
            "cybernative-ai",
            "edgen",
            "gigachat",
            "gigachat-ai",
            "hydra",
            "hydra-ai",
            "jina",
            "jina-ai",
            "lingyi",
            "lingyiwanwu",
            "monica",
            "monica-ai",
            "pollinations",
            "pollinations-ai",
            "rawechat",
            "shuttleai",
            "shuttle-ai",
            "teknium",
            "theb",
            "theb-ai",
            "tryleap",
            "leap-ai",
            // ═════════════════════════════════════════════════════════════════
            // Local/Self-hosted Providers
            // ═════════════════════════════════════════════════════════════════
            "lmstudio",
            "lm-studio",
            "llamacpp",
            "llama-cpp",
            "kobold",
            "koboldcpp",
            "textgen",
            "text-generation",
            "tabby",
            // ═════════════════════════════════════════════════════════════════
            // China-based Providers
            // ═════════════════════════════════════════════════════════════════
            "siliconflow",
            "silicon-flow",
            "zhipu",
            "zhipu-ai",
            "bigmodel",
            "minimax",
            "minimax-ai",
            "glm",
            "chatglm",
            "baichuan",
            "01-ai",
            "yi",
            // ═════════════════════════════════════════════════════════════════
            // AI Gateway & Proxy Services
            // ═════════════════════════════════════════════════════════════════
            "helicone",
            "helicone-ai",
            "workers-ai",
            "cloudflare-ai",
            "cloudflare-gateway",
            "vercel-ai",
            "vercel-gateway",
            // ═════════════════════════════════════════════════════════════════
            // Specialized Providers
            // ═════════════════════════════════════════════════════════════════
            "302ai",
            "302-ai",
            "sap-ai",
            "sap-ai-core",
            // ═════════════════════════════════════════════════════════════════
            // Additional Providers from OpenCommit
            // ═════════════════════════════════════════════════════════════════
            "aimlapi",
            "ai-ml-api",
        ]);
    }

    providers
}

/// Get provider info for display
#[allow(dead_code)]
pub fn provider_info(provider: &str) -> Option<String> {
    PROVIDER_REGISTRY.get(provider).map(|e| {
        let aliases = if e.aliases.is_empty() {
            String::new()
        } else {
            format!(" (aliases: {})", e.aliases.join(", "))
        };
        let model = e
            .default_model
            .map(|m| format!(", default model: {}", m))
            .unwrap_or_default();
        format!("{}{}{}", e.name, aliases, model)
    })
}

/// Split the prompt into system and user parts for providers that support it
pub fn split_prompt(
    diff: &str,
    context: Option<&str>,
    config: &Config,
    full_gitmoji: bool,
) -> (String, String) {
    let system_prompt = build_system_prompt(config, full_gitmoji);
    let user_prompt = build_user_prompt(diff, context, full_gitmoji, config);
    (system_prompt, user_prompt)
}

/// Build the system prompt part (role definition, rules)
fn build_system_prompt(config: &Config, full_gitmoji: bool) -> String {
    let mut prompt = String::new();

    prompt.push_str("You are an expert at writing clear, concise git commit messages.\n\n");

    // Core constraints
    prompt.push_str("OUTPUT RULES:\n");
    prompt.push_str("- Return ONLY the commit message, with no additional explanation, markdown formatting, or code blocks\n");
    prompt.push_str("- Do not include any reasoning, thinking, analysis, <thinking> tags, or XML-like tags in your response\n");
    prompt.push_str("- Never explain your choices or provide commentary\n");
    prompt.push_str(
        "- If you cannot generate a meaningful commit message, return \"chore: update\"\n\n",
    );

    // Add style guidance from history if enabled
    if config.learn_from_history.unwrap_or(false) {
        if let Some(style_guidance) = get_style_guidance(config) {
            prompt.push_str("REPO STYLE (learned from commit history):\n");
            prompt.push_str(&style_guidance);
            prompt.push('\n');
        }
    }

    // Add locale if specified
    if let Some(locale) = &config.language {
        prompt.push_str(&format!(
            "- Generate the commit message in {} language\n",
            locale
        ));
    }

    // Add commit type preference
    let commit_type = config.commit_type.as_deref().unwrap_or("conventional");
    match commit_type {
        "conventional" => {
            prompt.push_str("- Use conventional commit format: <type>(<scope>): <description>\n");
            prompt.push_str(
                "- Types: feat, fix, docs, style, refactor, perf, test, build, ci, chore\n",
            );
            if config.omit_scope.unwrap_or(false) {
                prompt.push_str("- Omit the scope, use format: <type>: <description>\n");
            }
        }
        "gitmoji" => {
            if full_gitmoji {
                prompt.push_str("- Use GitMoji format with full emoji specification from https://gitmoji.dev/\n");
                prompt.push_str("- Common emojis: ✨(feat), 🐛(fix), 📝(docs), 🚀(deploy), ♻️(refactor), ✅(test), 🔧(chore), ⚡(perf), 🎨(style), 📦(build), 👷(ci)\n");
                prompt.push_str("- For breaking changes, add 💥 after the type\n");
            } else {
                prompt.push_str("- Use GitMoji format: <emoji> <type>: <description>\n");
                prompt.push_str("- Common emojis: 🐛(fix), ✨(feat), 📝(docs), 🚀(deploy), ✅(test), ♻️(refactor), 🔧(chore), ⚡(perf), 🎨(style), 📦(build), 👷(ci)\n");
            }
        }
        _ => {}
    }

    // Description requirements
    let max_length = config.description_max_length.unwrap_or(100);
    prompt.push_str(&format!(
        "- Keep the description under {} characters\n",
        max_length
    ));

    if config.description_capitalize.unwrap_or(true) {
        prompt.push_str("- Capitalize the first letter of the description\n");
    }

    if !config.description_add_period.unwrap_or(false) {
        prompt.push_str("- Do not end the description with a period\n");
    }

    // Add commit body guidance if enabled
    if config.enable_commit_body.unwrap_or(false) {
        prompt.push_str("\nCOMMIT BODY (optional):\n");
        prompt.push_str(
            "- Add a blank line after the description, then explain WHY the change was made\n",
        );
        prompt.push_str("- Use bullet points for multiple changes\n");
        prompt.push_str("- Wrap body text at 72 characters\n");
        prompt
            .push_str("- Focus on motivation and context, not what changed (that's in the diff)\n");
    }

    prompt
}

/// Get style guidance from commit history analysis
fn get_style_guidance(config: &Config) -> Option<String> {
    use crate::git;
    use crate::utils::commit_style::CommitStyleProfile;

    // Get cached style profile or analyze fresh
    if let Some(cached) = &config.style_profile {
        // Use cached profile if available
        return Some(cached.clone());
    }

    // Analyze recent commits - default now 50 for better learning
    let count = config.history_commits_count.unwrap_or(50);

    match git::get_recent_commit_messages(count) {
        Ok(commits) => {
            if commits.is_empty() {
                return None;
            }

            let profile = CommitStyleProfile::analyze_from_commits(&commits);

            // Only use profile if we have enough confident data (at least 10 commits with patterns)
            // Increased from 5 to 10 for better confidence
            if profile.is_empty() || commits.len() < 10 {
                return None;
            }

            Some(profile.to_prompt_guidance())
        }
        Err(e) => {
            tracing::warn!("Failed to get commit history for style analysis: {}", e);
            None
        }
    }
}

/// Build the user prompt part (actual task + diff)
fn build_user_prompt(
    diff: &str,
    context: Option<&str>,
    _full_gitmoji: bool,
    _config: &Config,
) -> String {
    let mut prompt = String::new();

    // Add project context if available
    if let Some(project_context) = get_project_context() {
        prompt.push_str(&format!("Project Context: {}\n\n", project_context));
    }

    // Add file type summary with detailed extension info
    let file_summary = extract_file_summary(diff);
    if !file_summary.is_empty() {
        prompt.push_str(&format!("Files Changed: {}\n\n", file_summary));
    }

    // Add chunk indicator with more detail if diff was chunked
    if diff.contains("---CHUNK") {
        let chunk_count = diff.matches("---CHUNK").count();
        if chunk_count > 1 {
            prompt.push_str(&format!(
                "Note: This diff was split into {} chunks due to size. Focus on the overall purpose of the changes across all chunks.\n\n",
                chunk_count
            ));
        } else {
            prompt.push_str("Note: The diff was split into chunks due to size. Focus on the overall purpose of the changes.\n\n");
        }
    }

    // Add context if provided
    if let Some(ctx) = context {
        prompt.push_str(&format!("Additional context: {}\n\n", ctx));
    }

    prompt.push_str("Generate a commit message for the following git diff:\n");
    prompt.push_str("```diff\n");
    prompt.push_str(diff);
    prompt.push_str("\n```\n");

    // Add reminder about output format
    prompt.push_str("\nRemember: Return ONLY the commit message, no explanations or markdown.");

    prompt
}

/// Extract file type summary from diff
fn extract_file_summary(diff: &str) -> String {
    let mut files: Vec<String> = Vec::new();
    let mut extensions: std::collections::HashSet<String> = std::collections::HashSet::new();
    let mut file_types: std::collections::HashMap<String, usize> = std::collections::HashMap::new();

    for line in diff.lines() {
        if line.starts_with("+++ b/") {
            let path = line.strip_prefix("+++ b/").unwrap_or(line);
            if path != "/dev/null" {
                files.push(path.to_string());
                // Extract extension and categorize
                if let Some(ext) = std::path::Path::new(path).extension() {
                    if let Some(ext_str) = ext.to_str() {
                        let ext_lower = ext_str.to_lowercase();
                        extensions.insert(ext_lower.clone());

                        // Categorize file type
                        let category = categorize_file_type(&ext_lower);
                        *file_types.entry(category).or_insert(0) += 1;
                    }
                } else {
                    // No extension - might be a config file or script
                    if path.contains("Makefile")
                        || path.contains("Dockerfile")
                        || path.contains("LICENSE")
                    {
                        *file_types.entry("config".to_string()).or_insert(0) += 1;
                    }
                }
            }
        }
    }

    if files.is_empty() {
        return String::new();
    }

    // Build summary
    let mut summary = format!("{} file(s)", files.len());

    // Add file type categories
    if !file_types.is_empty() {
        let mut type_list: Vec<_> = file_types.into_iter().collect();
        type_list.sort_by(|a, b| b.1.cmp(&a.1)); // Sort by count descending

        let type_str: Vec<_> = type_list
            .iter()
            .map(|(t, c)| format!("{} {}", c, t))
            .collect();
        summary.push_str(&format!(" ({})", type_str.join(", ")));
    }

    // Add extension info if not too many
    if !extensions.is_empty() && extensions.len() <= 5 {
        let ext_list: Vec<_> = extensions.into_iter().collect();
        summary.push_str(&format!(" [.{}]", ext_list.join(", .")));
    }

    // Add first few file names if small number
    if files.len() <= 3 {
        summary.push_str(&format!(": {}", files.join(", ")));
    }

    summary
}

/// Categorize file extension into a type
fn categorize_file_type(ext: &str) -> String {
    match ext {
        // Programming languages
        "rs" => "Rust",
        "py" => "Python",
        "js" => "JavaScript",
        "ts" => "TypeScript",
        "jsx" | "tsx" => "React",
        "go" => "Go",
        "java" => "Java",
        "kt" => "Kotlin",
        "swift" => "Swift",
        "c" | "cpp" | "cc" | "h" | "hpp" => "C/C++",
        "rb" => "Ruby",
        "php" => "PHP",
        "cs" => "C#",
        "scala" => "Scala",
        "r" => "R",
        "m" => "Objective-C",
        "lua" => "Lua",
        "pl" => "Perl",

        // Web
        "html" | "htm" => "HTML",
        "css" | "scss" | "sass" | "less" => "CSS",
        "vue" => "Vue",
        "svelte" => "Svelte",

        // Data/Config
        "json" => "JSON",
        "yaml" | "yml" => "YAML",
        "toml" => "TOML",
        "xml" => "XML",
        "csv" => "CSV",
        "sql" => "SQL",

        // Documentation
        "md" | "markdown" => "Markdown",
        "rst" => "reStructuredText",
        "txt" => "Text",

        // Build/Config
        "sh" | "bash" | "zsh" | "fish" => "Shell",
        "ps1" => "PowerShell",
        "bat" | "cmd" => "Batch",
        "dockerfile" => "Docker",
        "makefile" | "mk" => "Make",
        "cmake" => "CMake",

        // Other
        _ => "Other",
    }
    .to_string()
}

/// Get project context from .rco/context.txt or README
fn get_project_context() -> Option<String> {
    use std::path::Path;

    // Try .rco/context.txt first
    if let Ok(repo_root) = crate::git::get_repo_root() {
        let context_path = Path::new(&repo_root).join(".rco").join("context.txt");
        if context_path.exists() {
            if let Ok(content) = std::fs::read_to_string(&context_path) {
                let trimmed = content.trim();
                if !trimmed.is_empty() {
                    return Some(trimmed.to_string());
                }
            }
        }

        // Try README.md - extract first paragraph
        let readme_path = Path::new(&repo_root).join("README.md");
        if readme_path.exists() {
            if let Ok(content) = std::fs::read_to_string(&readme_path) {
                // Find first non-empty line that's not a header
                for line in content.lines() {
                    let trimmed = line.trim();
                    if !trimmed.is_empty() && !trimmed.starts_with('#') {
                        // Return first sentence or up to 100 chars
                        let context = if let Some(idx) = trimmed.find('.') {
                            trimmed[..idx + 1].to_string()
                        } else {
                            trimmed.chars().take(100).collect()
                        };
                        if !context.is_empty() {
                            return Some(context);
                        }
                    }
                }
            }
        }

        // Try Cargo.toml for Rust projects
        let cargo_path = Path::new(&repo_root).join("Cargo.toml");
        if cargo_path.exists() {
            if let Ok(content) = std::fs::read_to_string(&cargo_path) {
                // Extract description from Cargo.toml
                let mut in_package = false;
                for line in content.lines() {
                    let trimmed = line.trim();
                    if trimmed == "[package]" {
                        in_package = true;
                    } else if trimmed.starts_with('[') && trimmed != "[package]" {
                        in_package = false;
                    } else if in_package && trimmed.starts_with("description") {
                        if let Some(idx) = trimmed.find('=') {
                            let desc = trimmed[idx + 1..].trim().trim_matches('"');
                            if !desc.is_empty() {
                                return Some(format!("Rust project: {}", desc));
                            }
                        }
                    }
                }
            }
        }

        // Try package.json for Node projects
        let package_path = Path::new(&repo_root).join("package.json");
        if package_path.exists() {
            if let Ok(content) = std::fs::read_to_string(&package_path) {
                if let Ok(json) = serde_json::from_str::<serde_json::Value>(&content) {
                    if let Some(desc) = json.get("description").and_then(|d| d.as_str()) {
                        if !desc.is_empty() {
                            return Some(format!("Node.js project: {}", desc));
                        }
                    }
                }
            }
        }
    }

    None
}

/// Build the combined prompt for providers without system message support
pub fn build_prompt(
    diff: &str,
    context: Option<&str>,
    config: &Config,
    full_gitmoji: bool,
) -> String {
    let (system, user) = split_prompt(diff, context, config, full_gitmoji);
    format!("{}\n\n---\n\n{}", system, user)
}

/// Create an AI provider from an account configuration
#[allow(dead_code)]
pub fn create_provider_for_account(
    account: &AccountConfig,
    config: &Config,
) -> Result<Box<dyn AIProvider>> {
    use crate::auth::token_storage;
    use crate::config::secure_storage;

    let provider = account.provider.to_lowercase();

    // Extract credentials from the account's auth method
    let credentials = match &account.auth {
        crate::config::accounts::AuthMethod::ApiKey { key_id } => {
            // Get API key from secure storage using the account's key_id
            token_storage::get_api_key_for_account(key_id)?
                .or_else(|| secure_storage::get_secret(key_id).ok().flatten())
        }
        crate::config::accounts::AuthMethod::OAuth {
            provider: _oauth_provider,
            account_id,
        } => {
            // Get OAuth access token from secure storage
            token_storage::get_tokens_for_account(account_id)?.map(|t| t.access_token)
        }
        crate::config::accounts::AuthMethod::EnvVar { name } => std::env::var(name).ok(),
        crate::config::accounts::AuthMethod::Bearer { token_id } => {
            // Get bearer token from secure storage
            token_storage::get_bearer_token_for_account(token_id)?
                .or_else(|| secure_storage::get_secret(token_id).ok().flatten())
        }
    };

    match provider.as_str() {
        #[cfg(feature = "openai")]
        "openai" | "codex" => {
            if let Some(key) = credentials.as_ref() {
                Ok(Box::new(openai::OpenAIProvider::from_account(
                    account, key, config,
                )?))
            } else {
                Ok(Box::new(openai::OpenAIProvider::new(config)?))
            }
        }
        #[cfg(feature = "anthropic")]
        "anthropic" | "claude" | "claude-code" => {
            if let Some(key) = credentials.as_ref() {
                Ok(Box::new(anthropic::AnthropicProvider::from_account(
                    account, key, config,
                )?))
            } else {
                Ok(Box::new(anthropic::AnthropicProvider::new(config)?))
            }
        }
        #[cfg(feature = "ollama")]
        "ollama" => {
            if let Some(key) = credentials.as_ref() {
                Ok(Box::new(ollama::OllamaProvider::from_account(
                    account, key, config,
                )?))
            } else {
                Ok(Box::new(ollama::OllamaProvider::new(config)?))
            }
        }
        #[cfg(feature = "gemini")]
        "gemini" => {
            if let Some(key) = credentials.as_ref() {
                Ok(Box::new(gemini::GeminiProvider::from_account(
                    account, key, config,
                )?))
            } else {
                Ok(Box::new(gemini::GeminiProvider::new(config)?))
            }
        }
        #[cfg(feature = "azure")]
        "azure" | "azure-openai" => {
            if let Some(key) = credentials.as_ref() {
                Ok(Box::new(azure::AzureProvider::from_account(
                    account, key, config,
                )?))
            } else {
                Ok(Box::new(azure::AzureProvider::new(config)?))
            }
        }
        #[cfg(feature = "perplexity")]
        "perplexity" => {
            if let Some(key) = credentials.as_ref() {
                Ok(Box::new(perplexity::PerplexityProvider::from_account(
                    account, key, config,
                )?))
            } else {
                Ok(Box::new(perplexity::PerplexityProvider::new(config)?))
            }
        }
        #[cfg(feature = "xai")]
        "xai" | "grok" | "x-ai" => {
            if let Some(key) = credentials.as_ref() {
                Ok(Box::new(xai::XAIProvider::from_account(
                    account, key, config,
                )?))
            } else {
                Ok(Box::new(xai::XAIProvider::new(config)?))
            }
        }
        #[cfg(feature = "huggingface")]
        "huggingface" | "hf" => {
            if let Some(key) = credentials.as_ref() {
                Ok(Box::new(huggingface::HuggingFaceProvider::from_account(
                    account, key, config,
                )?))
            } else {
                Ok(Box::new(huggingface::HuggingFaceProvider::new(config)?))
            }
        }
        #[cfg(feature = "bedrock")]
        "bedrock" | "aws-bedrock" | "amazon-bedrock" => Ok(Box::new(
            bedrock::BedrockProvider::from_account(account, "", config)?,
        )),
        #[cfg(feature = "vertex")]
        "vertex" | "vertex-ai" | "google-vertex" | "gcp-vertex" => Ok(Box::new(
            vertex::VertexProvider::from_account(account, "", config)?,
        )),
        #[cfg(feature = "mlx")]
        "mlx" | "mlx-lm" | "apple-mlx" => {
            if let Some(_key) = credentials.as_ref() {
                Ok(Box::new(mlx::MlxProvider::from_account(
                    account, "", config,
                )?))
            } else {
                Ok(Box::new(mlx::MlxProvider::new(config)?))
            }
        }
        #[cfg(feature = "nvidia")]
        "nvidia" | "nvidia-nim" | "nim" | "nvidia-ai" => {
            if let Some(key) = credentials.as_ref() {
                Ok(Box::new(nvidia::NvidiaProvider::from_account(
                    account, key, config,
                )?))
            } else {
                Ok(Box::new(nvidia::NvidiaProvider::new(config)?))
            }
        }
        #[cfg(feature = "flowise")]
        "flowise" | "flowise-ai" => {
            if let Some(_key) = credentials.as_ref() {
                Ok(Box::new(flowise::FlowiseProvider::from_account(
                    account, "", config,
                )?))
            } else {
                Ok(Box::new(flowise::FlowiseProvider::new(config)?))
            }
        }
        _ => {
            anyhow::bail!(
                "Unsupported AI provider for account: {}\n\n\
                 Account provider: {}\n\
                 Supported providers: openai, anthropic, ollama, gemini, azure, perplexity, xai, huggingface, bedrock, vertex",
                account.alias,
                provider
            );
        }
    }
}