File size: 13,183 Bytes
69f686e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
//! ็ฎก็† API ่ทฏ็”ฑๅค„็†ๅ™จ โ€”โ€” ็™ปๅฝ•/่ฎพ็ฝฎๅฏ†็ ใ€่ดฆๅทๆฑ ็Šถๆ€ใ€่ฏทๆฑ‚็ปŸ่ฎกใ€ๆจกๅž‹ๅˆ—่กจใ€้…็ฝฎๆŸฅ็œ‹

use axum::{
    body::Body,
    extract::{Query, State},
    http::{StatusCode, header},
    response::Response,
};
use serde::{Deserialize, Serialize};

use super::handlers::AppState;
use crate::config::Config;

// โ”€โ”€ ่ฏทๆฑ‚/ๅ“ๅบ”็ฑปๅž‹ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

#[derive(Deserialize)]
pub struct SetupRequest {
    pub password: String,
}

#[derive(Deserialize)]
pub struct LoginRequest {
    pub password: String,
}

#[derive(Serialize)]
pub struct LoginResponse {
    pub token: String,
}

#[derive(Serialize)]
pub struct AdminStatusResponse {
    pub accounts: Vec<ds_core::AccountStatus>,
    pub total: usize,
    pub idle: usize,
    pub busy: usize,
    pub error: usize,
    pub invalid: usize,
}

#[derive(Serialize)]
pub struct AdminStatsResponse {
    #[serde(flatten)]
    pub stats: super::stats::StatsSnapshot,
}

#[derive(Serialize)]
pub struct AdminConfigResponse {
    pub server: ServerConfigView,
    pub ds_core: DsCoreView,
    pub proxy: ProxyConfigView,
    pub admin: AdminConfigView,
    pub api_keys: Vec<ApiKeyEntryView>,
}

#[derive(Serialize)]
pub struct DsCoreView {
    pub accounts: Vec<AccountView>,
    pub api_base: String,
    pub wasm_url: String,
    pub user_agent: String,
    pub client_version: String,
    pub client_platform: String,
    pub client_locale: String,
    pub model_types: Vec<String>,
    pub max_input_tokens: Vec<u32>,
    pub max_output_tokens: Vec<u32>,
    pub input_character_limits: Vec<u32>,
    pub model_aliases: Vec<String>,
    pub tool_call: ToolCallTagConfigView,
}

#[derive(Serialize)]
pub struct ServerConfigView {
    pub host: String,
    pub port: u16,
    pub cors_origins: Vec<String>,
}

#[derive(Serialize)]
pub struct ToolCallTagConfigView {
    pub extra_starts: Vec<String>,
    pub extra_ends: Vec<String>,
}

#[derive(Serialize)]
pub struct ProxyConfigView {
    pub url: Option<String>,
}

#[derive(Serialize)]
pub struct AdminConfigView {
    pub password_set: bool,
    pub jwt_issued_at: u64,
}

#[derive(Serialize)]
pub struct ApiKeyEntryView {
    pub key: String,
    pub description: String,
}
#[derive(Serialize)]
pub struct AccountView {
    pub email: String,
    pub mobile: String,
    pub area_code: String,
    pub password: String,
}

// โ”€โ”€ ่„ฑๆ• โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

fn mask_config(config: &Config) -> AdminConfigResponse {
    AdminConfigResponse {
        server: ServerConfigView {
            host: config.server.host.clone(),
            port: config.server.port,
            cors_origins: config.server.cors_origins.clone(),
        },
        ds_core: DsCoreView {
            accounts: config
                .ds_core
                .accounts
                .iter()
                .map(|a| AccountView {
                    email: a.email.clone(),
                    mobile: a.mobile.clone(),
                    area_code: a.area_code.clone(),
                    password: a.password.clone(),
                })
                .collect(),
            api_base: config.ds_core.api_base.clone(),
            wasm_url: config.ds_core.wasm_url.clone(),
            user_agent: config.ds_core.user_agent.clone(),
            client_version: config.ds_core.client_version.clone(),
            client_platform: config.ds_core.client_platform.clone(),
            client_locale: config.ds_core.client_locale.clone(),
            model_types: config.ds_core.model_types.clone(),
            max_input_tokens: config.ds_core.max_input_tokens.clone(),
            max_output_tokens: config.ds_core.max_output_tokens.clone(),
            input_character_limits: config.ds_core.input_character_limits.clone(),
            model_aliases: config.ds_core.model_aliases.clone(),
            tool_call: ToolCallTagConfigView {
                extra_starts: config.ds_core.tool_call.extra_starts.clone(),
                extra_ends: config.ds_core.tool_call.extra_ends.clone(),
            },
        },
        proxy: ProxyConfigView {
            url: config.proxy.url.clone(),
        },
        admin: AdminConfigView {
            password_set: !config.admin.password_hash.is_empty(),
            jwt_issued_at: config.admin.jwt_issued_at,
        },
        api_keys: config
            .api_keys
            .iter()
            .map(|k| ApiKeyEntryView {
                key: k.key.clone(),
                description: k.description.clone(),
            })
            .collect(),
    }
}

// โ”€โ”€ Handlers โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

/// POST /admin/api/setup โ€” ้ฆ–ๆฌก่ฎพ็ฝฎๅฏ†็ 
pub(crate) async fn admin_setup(
    State(state): State<AppState>,
    body: axum::body::Bytes,
) -> Response {
    let req: SetupRequest = match serde_json::from_slice(&body) {
        Ok(r) => r,
        Err(e) => return error_response(StatusCode::BAD_REQUEST, &format!("่ฏทๆฑ‚ๆ ผๅผ้”™่ฏฏ: {}", e)),
    };

    match super::auth::setup_admin(&state.store, &state.login_limiter, &req.password).await {
        Ok(token) => json_response(&LoginResponse { token }),
        Err(msg) => {
            let status = if msg.contains("ๅทฒ่ฎพ็ฝฎ") {
                StatusCode::FORBIDDEN
            } else if msg.contains("ๆฌกๆ•ฐ่ฟ‡ๅคš") {
                StatusCode::TOO_MANY_REQUESTS
            } else if msg.contains("่‡ณๅฐ‘ 6 ไฝ") {
                StatusCode::BAD_REQUEST
            } else {
                StatusCode::INTERNAL_SERVER_ERROR
            };
            error_response(status, &msg)
        }
    }
}

pub(crate) async fn admin_login(
    State(state): State<AppState>,
    body: axum::body::Bytes,
) -> Response {
    let req: LoginRequest = match serde_json::from_slice(&body) {
        Ok(r) => r,
        Err(e) => return error_response(StatusCode::BAD_REQUEST, &format!("่ฏทๆฑ‚ๆ ผๅผ้”™่ฏฏ: {}", e)),
    };

    match super::auth::login_admin(&state.store, &state.login_limiter, &req.password).await {
        Ok(token) => json_response(&LoginResponse { token }),
        Err(msg) => {
            let status = if msg.contains("ๆฌกๆ•ฐ่ฟ‡ๅคš") {
                StatusCode::TOO_MANY_REQUESTS
            } else if msg.contains("ๆœช่ฎพ็ฝฎๅฏ†็ ") {
                StatusCode::FORBIDDEN
            } else {
                StatusCode::UNAUTHORIZED
            };
            error_response(status, &msg)
        }
    }
}

/// GET /admin/api/status
pub(crate) async fn admin_status(State(state): State<AppState>) -> Response {
    let statuses = state.adapter.account_statuses();
    let total = statuses.len();
    let busy = statuses.iter().filter(|a| a.state == "busy").count();
    let idle = statuses.iter().filter(|a| a.state == "idle").count();
    let error = statuses.iter().filter(|a| a.state == "error").count();
    let invalid = statuses.iter().filter(|a| a.state == "invalid").count();

    let resp = AdminStatusResponse {
        accounts: statuses,
        total,
        idle,
        busy,
        error,
        invalid,
    };
    json_response(&resp)
}

/// GET /admin/api/stats
pub(crate) async fn admin_stats(State(state): State<AppState>) -> Response {
    let snapshot = state.stats.snapshot();
    let resp = AdminStatsResponse { stats: snapshot };
    json_response(&resp)
}

/// GET /admin/api/models
pub(crate) async fn admin_models(State(state): State<AppState>) -> Response {
    let models = state.adapter.list_models().await;
    json_response(&models)
}

/// GET /admin/api/config
pub(crate) async fn admin_config(State(state): State<AppState>) -> Response {
    let config = state.config.read().await;
    let config_view = mask_config(&config);
    json_response(&config_view)
}

/// PUT /admin/api/config โ€” ๆ›ดๆ–ฐๅนถ็ƒญ้‡่ฝฝ้…็ฝฎ
pub(crate) async fn admin_put_config(
    State(state): State<AppState>,
    body: axum::body::Bytes,
) -> Response {
    let mut new_config: Config = match serde_json::from_slice(&body) {
        Ok(c) => c,
        Err(e) => return error_response(StatusCode::BAD_REQUEST, &format!("JSON ่งฃๆžๅคฑ่ดฅ: {}", e)),
    };

    // Validate
    if let Err(e) = new_config.validate() {
        return error_response(StatusCode::BAD_REQUEST, &e.to_string());
    }
    // Merge: empty/"***" passwords keep existing values from current config;
    // API keys match by `id` (stable identifier), falling back to description for old-format migration.
    {
        let current = state.config.read().await;
        for a in &mut new_config.ds_core.accounts {
            if (a.password.is_empty() || a.password == "***")
                && let Some(existing) = current
                    .ds_core
                    .accounts
                    .iter()
                    .find(|e| e.email == a.email && e.mobile == a.mobile)
            {
                a.password.clone_from(&existing.password);
            }
        }
        // Admin ้…็ฝฎ๏ผš็ฉบ็š„ password_hash/jwt_secret ไฟ็•™็Žฐๆœ‰ๅ€ผ๏ผˆๅ‰็ซฏไธ่ฟ”ๅ›ž่ฟ™ไบ›ๅญ—ๆฎต๏ผ‰
        if new_config.admin.password_hash.is_empty() {
            new_config
                .admin
                .password_hash
                .clone_from(&current.admin.password_hash);
        }
        if new_config.admin.jwt_secret.is_empty() {
            new_config
                .admin
                .jwt_secret
                .clone_from(&current.admin.jwt_secret);
        }
        // ๅฏ†็ ไฟฎๆ”น๏ผšๅ‰็ซฏๅ‘ old_password + new_password
        if !new_config.admin.old_password.is_empty() || !new_config.admin.new_password.is_empty() {
            if new_config.admin.old_password.is_empty() || new_config.admin.new_password.is_empty()
            {
                return error_response(
                    StatusCode::BAD_REQUEST,
                    "ไฟฎๆ”นๅฏ†็ ้œ€่ฆๅŒๆ—ถๆไพ›ๆ—งๅฏ†็ ๅ’Œๆ–ฐๅฏ†็ ",
                );
            }
            if !bcrypt::verify(&new_config.admin.old_password, &current.admin.password_hash)
                .unwrap_or(false)
            {
                return error_response(StatusCode::BAD_REQUEST, "ๆ—งๅฏ†็ ไธๆญฃ็กฎ");
            }
            new_config.admin.password_hash =
                super::store::hash_password(&new_config.admin.new_password);
            new_config.admin.jwt_secret = super::store::generate_hex_secret();
            new_config.admin.jwt_issued_at += 1;
        }
    }

    // Persist
    {
        let mut guard = state.config.write().await;
        *guard = new_config.clone();
        if let Err(e) = guard.save(&state.config_path) {
            return error_response(
                StatusCode::INTERNAL_SERVER_ERROR,
                &format!("ไฟๅญ˜ๅคฑ่ดฅ: {}", e),
            );
        }
    }

    // Hot-reload: sync accounts from the new config
    state
        .adapter
        .sync_accounts(&new_config.ds_core.accounts)
        .await;
    json_response(&serde_json::json!({"ok": true}))
}

#[derive(Deserialize)]
pub struct LogsQuery {
    #[serde(default = "default_limit")]
    pub limit: usize,
}

fn default_limit() -> usize {
    50
}

/// GET /admin/api/logs โ€” ่Žทๅ–ๆœ€่ฟ‘็š„่ฏทๆฑ‚ๆ—ฅๅฟ—
pub(crate) async fn admin_logs(
    Query(query): Query<LogsQuery>,
    State(state): State<AppState>,
) -> Response {
    let logs = state.stats.recent_logs(query.limit);
    json_response(&logs)
}

#[derive(Deserialize)]
pub struct RuntimeLogsQuery {
    #[serde(default)]
    pub offset: usize,
    #[serde(default = "default_runtime_limit")]
    pub limit: usize,
}

fn default_runtime_limit() -> usize {
    100
}

/// GET /admin/api/runtime-logs โ€” ๅˆ†้กตๆŸฅ่ฏข่ฟ่กŒๆ—ฅๅฟ—
pub(crate) async fn admin_runtime_logs(Query(query): Query<RuntimeLogsQuery>) -> Response {
    let (total, logs) = super::runtime_log::query_logs(query.offset, query.limit).await;
    json_response(&serde_json::json!({
        "total": total,
        "offset": query.offset,
        "limit": query.limit,
        "logs": logs,
    }))
}

// โ”€โ”€ Helpers โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

fn json_response<T: Serialize>(data: &T) -> Response {
    let bytes = serde_json::to_vec(data).unwrap();
    Response::builder()
        .status(StatusCode::OK)
        .header(header::CONTENT_TYPE, "application/json")
        .body(Body::from(bytes))
        .unwrap()
}

fn error_response(status: StatusCode, message: &str) -> Response {
    let body = serde_json::json!({"error": message});
    let bytes = serde_json::to_vec(&body).unwrap();
    Response::builder()
        .status(status)
        .header(header::CONTENT_TYPE, "application/json")
        .body(Body::from(bytes))
        .unwrap()
}