File size: 3,011 Bytes
bbb1195
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
use axum::Json;
use serde::Serialize;
use uuid::Uuid;

use crate::proxy::config::ProxyConfig;
use crate::modules;
use crate::error::AppError;

use super::config::ApiResponse;

/// Proxy status response
#[derive(Serialize)]
pub struct ProxyStatus {
    pub running: bool,
    pub port: u16,
    pub base_url: String,
    #[serde(rename = "active_accounts")]
    pub active_accounts: usize,
}

/// Get proxy status
/// In cloud deployment mode, the proxy is always running
pub async fn get_proxy_status() -> Result<Json<ApiResponse<ProxyStatus>>, AppError> {
    let _config = modules::load_app_config().map_err(AppError::Config)?;
    let account_index = modules::load_account_index().map_err(AppError::Account)?;

    let port = std::env::var("PORT")
        .unwrap_or_else(|_| "7860".to_string())
        .parse::<u16>()
        .unwrap_or(7860);

    let base_url = std::env::var("SPACE_HOST")
        .map(|host| format!("https://{}", host))
        .unwrap_or_else(|_| format!("http://localhost:{}", port));

    let status = ProxyStatus {
        running: true, // Always running in cloud mode
        port,
        base_url,
        active_accounts: account_index.accounts.len(),
    };

    Ok(ApiResponse::success(status))
}

/// Update model mapping
pub async fn update_model_mapping(
    Json(config): Json<ProxyConfig>,
) -> Result<Json<ApiResponse<()>>, AppError> {
    // Load current config and update proxy section
    let mut app_config = modules::load_app_config().map_err(AppError::Config)?;
    app_config.proxy.anthropic_mapping = config.anthropic_mapping;
    app_config.proxy.openai_mapping = config.openai_mapping;
    app_config.proxy.custom_mapping = config.custom_mapping;

    modules::save_app_config(&app_config).map_err(AppError::Config)?;
    modules::logger::log_info("Model mapping updated");

    Ok(ApiResponse::success(()))
}

/// Restart proxy service (reload configuration)
/// In cloud mode, this doesn't actually restart the server,
/// but signals that configuration has been updated
pub async fn restart_proxy(
    Json(config): Json<ProxyConfig>,
) -> Result<Json<ApiResponse<()>>, AppError> {
    // Load and update configuration
    let mut app_config = modules::load_app_config().map_err(AppError::Config)?;
    app_config.proxy = config;
    modules::save_app_config(&app_config).map_err(AppError::Config)?;

    modules::logger::log_info("Proxy configuration updated (restart not needed in cloud mode)");

    Ok(ApiResponse::success(()))
}

/// Generate a new API key
pub async fn generate_api_key() -> Result<Json<ApiResponse<String>>, AppError> {
    let new_key = format!("ag-{}", Uuid::new_v4().to_string().replace("-", ""));

    // Update config with new key
    let mut app_config = modules::load_app_config().map_err(AppError::Config)?;
    app_config.proxy.api_key = new_key.clone();
    modules::save_app_config(&app_config).map_err(AppError::Config)?;

    modules::logger::log_info("New API key generated");

    Ok(ApiResponse::success(new_key))
}