claude_code_switcher/templates/
mod.rs

1//! Template module for AI provider configurations
2//!
3//! This module provides a modular approach to managing different AI provider templates.
4//! Each template is implemented as a separate module with the Template trait.
5
6use crate::{settings::ClaudeSettings, snapshots::SnapshotScope};
7use anyhow::{Result, anyhow};
8
9use serde::{Deserialize, Serialize};
10use std::collections::HashMap;
11
12/// Trait that all AI provider templates must implement
13pub trait Template {
14    /// Get the template type identifier
15    fn template_type(&self) -> TemplateType;
16
17    /// Get the environment variable name for the API key
18    fn env_var_name(&self) -> &'static str;
19
20    /// Create Claude settings for this template
21    fn create_settings(&self, api_key: &str, scope: &SnapshotScope) -> ClaudeSettings;
22
23    /// Get display name for the template
24    fn display_name(&self) -> &'static str;
25
26    /// Get description for the template
27    fn description(&self) -> &'static str;
28
29    /// Check if this template requires additional configuration (like endpoint ID)
30    fn requires_additional_config(&self) -> bool {
31        false
32    }
33
34    /// Get additional configuration if needed
35    fn get_additional_config(&self) -> Result<HashMap<String, String>> {
36        Ok(HashMap::new())
37    }
38}
39
40/// Type of AI provider template
41#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
42pub enum TemplateType {
43    DeepSeek,
44    Zai,
45    K2,
46    K2Thinking,
47    KatCoder,
48    KatCoderPro,
49    KatCoderAir,
50    Kimi,
51    Longcat,
52    MiniMax,
53}
54
55impl std::str::FromStr for TemplateType {
56    type Err = anyhow::Error;
57
58    fn from_str(s: &str) -> Result<Self> {
59        match s.to_lowercase().as_str() {
60            "deepseek" | "ds" => Ok(TemplateType::DeepSeek),
61            "glm" | "zhipu" | "zai" => Ok(TemplateType::Zai),
62            "k2" | "moonshot" => Ok(TemplateType::K2),
63            "k2-thinking" | "k2thinking" => Ok(TemplateType::K2Thinking),
64            "kat-coder" | "katcoder" | "kat" => Ok(TemplateType::KatCoderPro), // Legacy alias: points to Pro version
65            "kat-coder-pro" | "katcoder-pro" | "katpro" => Ok(TemplateType::KatCoderPro),
66            "kat-coder-air" | "katcoder-air" | "katair" => Ok(TemplateType::KatCoderAir),
67            "kimi" | "kimi-for-coding" => Ok(TemplateType::Kimi),
68            "longcat" => Ok(TemplateType::Longcat),
69            "minimax" | "minimax-anthropic" => Ok(TemplateType::MiniMax),
70            _ => Err(anyhow!(
71                "Unknown template: {}. Available templates: deepseek, glm, k2, k2-thinking, kat-coder, kat-coder-pro, kat-coder-air, kimi, longcat, minimax",
72                s
73            )),
74        }
75    }
76}
77
78impl std::fmt::Display for TemplateType {
79    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
80        match self {
81            TemplateType::DeepSeek => write!(f, "deepseek"),
82            TemplateType::Zai => write!(f, "zai"),
83            TemplateType::K2 => write!(f, "k2"),
84            TemplateType::K2Thinking => write!(f, "k2-thinking"),
85            TemplateType::KatCoder => write!(f, "kat-coder"),
86            TemplateType::KatCoderPro => write!(f, "kat-coder-pro"),
87            TemplateType::KatCoderAir => write!(f, "kat-coder-air"),
88            TemplateType::Kimi => write!(f, "kimi"),
89            TemplateType::Longcat => write!(f, "longcat"),
90            TemplateType::MiniMax => write!(f, "minimax"),
91        }
92    }
93}
94
95/// Get template type from string
96pub fn get_template_type(template_str: &str) -> Result<TemplateType> {
97    template_str.parse()
98}
99
100/// Get all available template types
101pub fn get_all_templates() -> Vec<TemplateType> {
102    vec![
103        TemplateType::DeepSeek,
104        TemplateType::Zai,
105        TemplateType::K2,
106        TemplateType::K2Thinking,
107        TemplateType::KatCoder,
108        TemplateType::KatCoderPro,
109        TemplateType::KatCoderAir,
110        TemplateType::Kimi,
111        TemplateType::Longcat,
112        TemplateType::MiniMax,
113    ]
114}
115
116/// Get the environment variable name for a template type
117pub fn get_env_var_name(template_type: &TemplateType) -> &'static str {
118    match template_type {
119        TemplateType::DeepSeek => "DEEPSEEK_API_KEY",
120        TemplateType::Zai => "Z_AI_API_KEY",
121        TemplateType::K2 | TemplateType::K2Thinking => "MOONSHOT_API_KEY",
122        TemplateType::KatCoder | TemplateType::KatCoderPro | TemplateType::KatCoderAir => {
123            "KAT_CODER_API_KEY"
124        }
125        TemplateType::Kimi => "KIMI_API_KEY",
126        TemplateType::Longcat => "LONGCAT_API_KEY",
127        TemplateType::MiniMax => "MINIMAX_API_KEY",
128    }
129}
130
131/// Get a template instance by type
132pub fn get_template_instance(template_type: &TemplateType) -> Box<dyn Template> {
133    match template_type {
134        TemplateType::DeepSeek => Box::new(deepseek::DeepSeekTemplate),
135        TemplateType::Zai => Box::new(zai::ZaiTemplate),
136        TemplateType::K2 => Box::new(k2::K2Template),
137        TemplateType::K2Thinking => Box::new(k2::K2ThinkingTemplate),
138        TemplateType::KatCoder => Box::new(kat_coder::KatCoderProTemplate), // Legacy alias
139        TemplateType::KatCoderPro => Box::new(kat_coder::KatCoderProTemplate),
140        TemplateType::KatCoderAir => Box::new(kat_coder::KatCoderAirTemplate),
141        TemplateType::Kimi => Box::new(kimi::KimiTemplate),
142        TemplateType::Longcat => Box::new(longcat::LongcatTemplate),
143        TemplateType::MiniMax => Box::new(minimax::MiniMaxTemplate),
144    }
145}
146
147/// Legacy compatibility function - creates a settings function for backwards compatibility
148pub fn get_template(template_type: &TemplateType) -> fn(&str, &SnapshotScope) -> ClaudeSettings {
149    match template_type {
150        TemplateType::DeepSeek => create_deepseek_template,
151        TemplateType::Zai => create_zai_template,
152        TemplateType::K2 => create_k2_template,
153        TemplateType::K2Thinking => create_k2_thinking_template,
154        TemplateType::KatCoder => create_kat_coder_pro_template, // Legacy alias: points to Pro version
155        TemplateType::KatCoderPro => create_kat_coder_pro_template,
156        TemplateType::KatCoderAir => create_kat_coder_air_template,
157        TemplateType::Kimi => create_kimi_template,
158        TemplateType::Longcat => create_longcat_template,
159        TemplateType::MiniMax => create_minimax_template,
160    }
161}
162
163// Import all template modules
164pub mod deepseek;
165pub mod k2;
166pub mod kat_coder;
167pub mod kimi;
168pub mod longcat;
169pub mod minimax;
170pub mod zai;
171
172// Re-export for backward compatibility
173pub use deepseek::*;
174pub use k2::*;
175pub use kat_coder::*;
176pub use kimi::*;
177pub use longcat::*;
178pub use minimax::*;
179pub use zai::*;