oxios-web 1.2.0

Web dashboard channel for Oxios
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
//! API routes for email management.
//!
//! Provides endpoints for SMTP setup, viewing sent history,
//! and browsing templates. Actual email sending is done by
//! agents via the `send_email` tool.

use std::sync::Arc;

use axum::extract::{Path, Query, State};
use axum::Json;
use serde::{Deserialize, Serialize};
#[cfg(test)]
use serde_json::Value;

use crate::error::AppError;
use crate::server::AppState;

// ---------------------------------------------------------------------------
// Request / response types
// ---------------------------------------------------------------------------

/// Query parameters for email history listing.
#[derive(Debug, Deserialize, Default)]
pub struct EmailHistoryParams {
    /// Maximum number of records to return.
    #[serde(default = "default_history_limit")]
    pub limit: usize,
}

fn default_history_limit() -> usize {
    100
}

/// A single sent email record returned by the API.
#[derive(Debug, Serialize, Deserialize)]
pub struct SentEmailResponse {
    /// Unique ID.
    pub id: String,
    /// Timestamp (RFC 3339).
    pub sent_at: String,
    /// Email subject.
    pub subject: String,
    /// Recipient.
    pub to: String,
    /// Template used (if any).
    pub template_used: Option<String>,
    /// SMTP message ID.
    pub message_id: String,
    /// First 500 chars of HTML for preview.
    pub html_preview: String,
    /// Full HTML body (원문). May be large.
    pub html_full: String,
    /// Plain text fallback if provided.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub body_text: Option<String>,
    /// Associated cron job name.
    pub cron_job: Option<String>,
}

/// Email setup status response.
#[derive(Debug, Serialize)]
pub struct EmailStatusResponse {
    /// Whether email is configured and ready.
    pub configured: bool,
    /// Configured email address.
    pub email: Option<String>,
    /// SMTP provider.
    pub provider: Option<String>,
    /// Number of templates available.
    pub template_count: usize,
    /// Number of emails sent (total).
    pub total_sent: usize,
}

/// Email test request. In v1, the `to` field is accepted but ignored
/// (test emails always go to `my_email`).
#[derive(Debug, Deserialize)]
pub struct EmailTestRequest {
    /// Optional custom recipient (ignored in v1).
    #[serde(default)]
    pub to: Option<String>,
}

/// Template info returned by the API.
#[derive(Debug, Serialize)]
pub struct TemplateResponse {
    /// Template name.
    pub name: String,
    /// First 200 chars of template content.
    pub preview: String,
    /// Template file size in bytes.
    pub size: usize,
}

/// Email config update request (for setup).
#[derive(Debug, Deserialize)]
pub struct EmailConfigRequest {
    /// User's email address.
    pub my_email: String,
    /// SMTP provider.
    #[serde(default = "default_provider")]
    pub provider: String,
    /// SMTP host (for custom).
    #[serde(default)]
    pub host: Option<String>,
    /// SMTP port (for custom).
    #[serde(default)]
    pub port: Option<u16>,
    /// SMTP auth username (defaults to my_email).
    #[serde(default)]
    pub user: Option<String>,
    /// SMTP password / app password.
    pub password: String,
}

fn default_provider() -> String {
    "resend".to_string()
}

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

/// Extract the email API, returning 503 if unavailable.
macro_rules! email_api {
    ($state:expr) => {
        $state.kernel.email.as_ref().ok_or_else(|| {
            AppError::ServiceUnavailable(
                "Email subsystem not available. Add [email] enabled = true to config.toml".into(),
            )
        })
    };
}

/// Load sent records from the state store directory.
///
/// Reads raw JSON files (which include `html_full` and `body_text`),
/// then strips large fields when `full=false` (for list view) to reduce payload.
fn load_sent_records(
    state_store: &oxios_kernel::state_store::StateStore,
    limit: usize,
    full: bool,
) -> Vec<serde_json::Value> {
    let sent_dir = state_store.base_path.join("email_sent");
    if !sent_dir.exists() {
        return Vec::new();
    }

    let mut records = Vec::new();
    let Ok(entries) = std::fs::read_dir(&sent_dir) else {
        return records;
    };

    for entry in entries.flatten() {
        if entry.path().extension().is_some_and(|ext| ext == "json") {
            match std::fs::read_to_string(entry.path()) {
                Ok(content) => match serde_json::from_str::<serde_json::Value>(&content) {
                    Ok(mut val) => {
                        if !full {
                            // Strip heavy fields for list view
                            if let Some(obj) = val.as_object_mut() {
                                obj.remove("html_full");
                                obj.remove("body_text");
                            }
                        }
                        records.push(val);
                    }
                    Err(e) => {
                        tracing::warn!(
                            file = ?entry.path(),
                            error = %e,
                            "Skipping corrupted email record"
                        );
                    }
                },
                Err(e) => {
                    tracing::warn!(
                        file = ?entry.path(),
                        error = %e,
                        "Failed to read email record"
                    );
                }
            }
        }
    }

    // Sort by sent_at descending (newest first)
    records.sort_by(|a, b| {
        let sa = a.get("sent_at").and_then(|v| v.as_str()).unwrap_or("");
        let sb = b.get("sent_at").and_then(|v| v.as_str()).unwrap_or("");
        sb.cmp(sa)
    });
    records.truncate(limit);
    records
}

/// Load a single sent record by exact ID match.
fn load_sent_record(
    state_store: &oxios_kernel::state_store::StateStore,
    id: &str,
) -> Option<serde_json::Value> {
    let sent_dir = state_store.base_path.join("email_sent");
    if !sent_dir.exists() {
        return None;
    }

    let Ok(entries) = std::fs::read_dir(&sent_dir) else {
        return None;
    };

    for entry in entries.flatten() {
        if entry.path().extension().is_some_and(|ext| ext == "json") {
            if let Ok(content) = std::fs::read_to_string(entry.path()) {
                if let Ok(val) = serde_json::from_str::<serde_json::Value>(&content) {
                    if val.get("id").and_then(|v| v.as_str()) == Some(id) {
                        return Some(val);
                    }
                }
            }
        }
    }
    None
}

// ---------------------------------------------------------------------------
// Handlers
// ---------------------------------------------------------------------------

/// GET /api/email/status — Email subsystem status.
pub(crate) async fn handle_email_status(
    state: State<Arc<AppState>>,
) -> Result<Json<EmailStatusResponse>, AppError> {
    let provider_name = match state.config.read().email.provider {
        oxios_kernel::email::SmtpProvider::Resend => "resend",
        oxios_kernel::email::SmtpProvider::Gmail => "gmail",
        oxios_kernel::email::SmtpProvider::Icloud => "icloud",
        oxios_kernel::email::SmtpProvider::Fastmail => "fastmail",
        oxios_kernel::email::SmtpProvider::Custom => "custom",
    }
    .to_string();

    let configured = state.kernel.email.is_some();
    let (email, provider, template_count) = if let Some(api) = &state.kernel.email {
        let templates = api.list_templates().unwrap_or_default();
        (
            Some(api.default_to().to_string()),
            Some(provider_name),
            templates.len(),
        )
    } else {
        (None, None, 0)
    };

    let total_sent = load_sent_records(state.kernel.state.store(), usize::MAX, false).len();

    Ok(Json(EmailStatusResponse {
        configured,
        email,
        provider,
        template_count,
        total_sent,
    }))
}

/// GET /api/email/history — List sent emails.
pub(crate) async fn handle_email_history(
    state: State<Arc<AppState>>,
    Query(params): Query<EmailHistoryParams>,
) -> Result<Json<serde_json::Value>, AppError> {
    // Email subsystem doesn't need to be configured to view history
    let limit = params.limit.min(500);
    let records = load_sent_records(state.kernel.state.store(), limit, false);

    Ok(Json(serde_json::json!({
        "emails": records,
        "total": records.len(),
        "limit": limit,
    })))
}

/// GET /api/email/history/{id} — Get a specific sent email.
pub(crate) async fn handle_email_history_detail(
    state: State<Arc<AppState>>,
    Path(id): Path<String>,
) -> Result<Json<serde_json::Value>, AppError> {
    let record = load_sent_record(state.kernel.state.store(), &id)
        .ok_or_else(|| AppError::NotFound(format!("Email record '{id}' not found")))?;

    Ok(Json(record))
}

/// GET /api/email/templates — List email templates.
pub(crate) async fn handle_email_templates(
    state: State<Arc<AppState>>,
) -> Result<Json<serde_json::Value>, AppError> {
    let api = email_api!(state)?;
    let names = api
        .list_templates()
        .map_err(|e| AppError::Internal(e.to_string()))?;

    let mut templates = Vec::new();
    for name in &names {
        match api.load_template(name) {
            Ok(content) => templates.push(TemplateResponse {
                name: name.clone(),
                preview: content.chars().take(200).collect(),
                size: content.len(),
            }),
            Err(e) => {
                tracing::warn!(template = %name, error = %e, "Failed to load template");
            }
        }
    }

    Ok(Json(serde_json::json!({
        "templates": templates,
    })))
}

/// GET /api/email/templates/{name} — Get a specific template.
pub(crate) async fn handle_email_template_get(
    state: State<Arc<AppState>>,
    Path(name): Path<String>,
) -> Result<Json<serde_json::Value>, AppError> {
    let api = email_api!(state)?;
    let content = api
        .load_template(&name)
        .map_err(|e| AppError::NotFound(e.to_string()))?;

    Ok(Json(serde_json::json!({
        "name": name,
        "content": content,
        "size": content.len(),
    })))
}

/// POST /api/email/test — Send a test email.
///
/// Body (optional): `{"to": "custom@example.com"}`
/// Note: In v1, the `to` field is ignored — the test email always goes to
/// `my_email` (the user's own address). External recipients are a v2 feature.
pub(crate) async fn handle_email_test(
    state: State<Arc<AppState>>,
    body: Option<Json<EmailTestRequest>>,
) -> Result<Json<serde_json::Value>, AppError> {
    let api = email_api!(state)?;
    let default_to = api.default_to().to_string();
    api.test_connection()
        .await
        .map_err(|e| AppError::Internal(format!("SMTP test failed: {e}")))?;

    let requested_to = body.and_then(|Json(b)| b.to);
    let recipient = requested_to.unwrap_or_else(|| default_to.clone());
    let override_note = if recipient != default_to {
        format!(" (requested '{recipient}' ignored in v1, sent to '{default_to}')")
    } else {
        String::new()
    };

    Ok(Json(serde_json::json!({
        "status": "ok",
        "message": format!("Test email sent successfully{override_note}"),
        "to": default_to,
    })))
}

/// POST /api/email/setup — Configure email from web UI.
pub(crate) async fn handle_email_setup(
    _state: State<Arc<AppState>>,
    Json(body): Json<EmailConfigRequest>,
) -> Result<Json<serde_json::Value>, AppError> {
    // Validate we can connect with these credentials
    let provider = match body.provider.as_str() {
        "resend" => oxios_kernel::email::SmtpProvider::Resend,
        "gmail" => oxios_kernel::email::SmtpProvider::Gmail,
        "icloud" => oxios_kernel::email::SmtpProvider::Icloud,
        "fastmail" => oxios_kernel::email::SmtpProvider::Fastmail,
        "custom" => oxios_kernel::email::SmtpProvider::Custom,
        _ => {
            return Err(AppError::BadRequest(format!(
                "Unknown provider: {}",
                body.provider
            )))
        }
    };

    let config = oxios_kernel::config::EmailConfig {
        enabled: true,
        my_email: body.my_email.clone(),
        provider,
        host: body.host.unwrap_or_default(),
        port: body.port.unwrap_or(0),
        tls: None,
        user: body.user.unwrap_or_default(),
        secret_ref: "email_smtp".to_string(),
        rate_limit_per_hour: 10,
    };

    let smtp = oxios_kernel::SmtpClient::from_config(&config, &body.password)
        .map_err(|e| AppError::BadRequest(format!("SMTP config error: {e}")))?;

    // Test the connection before saving
    smtp.test_connection()
        .await
        .map_err(|e| AppError::BadRequest(format!("SMTP test failed: {e}")))?;

    // Save password to env-like store
    // Store as a token in oxi auth store
    let token = oxi_sdk::TokenBundle {
        access_token: body.password,
        refresh_token: None,
        token_type: "Bearer".to_string(),
        obtained_at: chrono::Utc::now(),
        expires_in: 0,
        scope: None,
    };
    oxi_sdk::save_token("email_smtp", &token)
        .map_err(|e| AppError::Internal(format!("Failed to save credentials: {e}")))?;

    // Also append to config.toml if it exists
    let config_path = oxios_kernel::config::expand_home("~/.oxios/config.toml");
    if config_path.exists() {
        let _ = append_email_section_to_config(&config_path, &config);
    }

    Ok(Json(serde_json::json!({
        "status": "ok",
        "message": "Email configured successfully. Restart oxios to activate.",
        "email": body.my_email,
    })))
}

/// Append [email] section to config.toml if not already present.
fn append_email_section_to_config(
    config_path: &std::path::Path,
    config: &oxios_kernel::config::EmailConfig,
) -> std::io::Result<()> {
    let content = std::fs::read_to_string(config_path)?;
    if content.contains("[email]") {
        return Ok(());
    }
    let provider_str = match config.provider {
        oxios_kernel::email::SmtpProvider::Resend => "resend",
        oxios_kernel::email::SmtpProvider::Gmail => "gmail",
        oxios_kernel::email::SmtpProvider::Icloud => "icloud",
        oxios_kernel::email::SmtpProvider::Fastmail => "fastmail",
        oxios_kernel::email::SmtpProvider::Custom => "custom",
    };
    let section = format!(
        "\n# Email (configured by web UI)\n[email]\nenabled = true\nmy_email = \"{}\"\nprovider = \"{}\"\n",
        config.my_email, provider_str
    );
    std::fs::write(config_path, content + &section)?;
    Ok(())
}

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

    /// Create a temp StateStore with N sent email records.
    fn make_store_with_records(
        records: &[(&str, &str, &str)],
    ) -> (tempfile::TempDir, oxios_kernel::state_store::StateStore) {
        let tmp = tempfile::tempdir().unwrap();
        let sent_dir = tmp.path().join("email_sent");
        fs::create_dir_all(&sent_dir).unwrap();
        for (id, subject, html) in records {
            let record = serde_json::json!({
                "id": id,
                "sent_at": "2026-06-06T08:00:12+00:00",
                "subject": subject,
                "to": "me@gmail.com",
                "template_used": Value::Null,
                "message_id": format!("<{id}@test>"),
                "html_preview": html.chars().take(100).collect::<String>(),
                "html_full": html,
                "body_text": Value::Null,
                "cron_job": Value::Null,
            });
            fs::write(sent_dir.join(format!("{id}.json")), record.to_string()).unwrap();
        }
        let store = oxios_kernel::state_store::StateStore::new(tmp.path().to_path_buf()).unwrap();
        (tmp, store)
    }

    #[test]
    fn test_load_sent_records_empty() {
        let tmp = tempfile::tempdir().unwrap();
        let store = oxios_kernel::state_store::StateStore::new(tmp.path().to_path_buf()).unwrap();
        let records = load_sent_records(&store, 100, false);
        assert!(records.is_empty());
    }

    #[test]
    fn test_load_sent_records_limit_and_sort() {
        let data = vec![
            ("a", "First", "<p>A</p>"),
            ("b", "Second", "<p>B</p>"),
            ("c", "Third", "<p>C</p>"),
        ];
        let (_tmp, store) = make_store_with_records(&data);
        let records = load_sent_records(&store, 100, false);
        assert_eq!(records.len(), 3);
        // All should be there (sorted by sent_at descending — same timestamp, so stable)
    }

    #[test]
    fn test_load_sent_records_strips_full_when_not_requested() {
        let data = vec![(
            "a",
            "Subject",
            "<p>very long html body that should be stripped</p>",
        )];
        let (_tmp, store) = make_store_with_records(&data);
        let records = load_sent_records(&store, 100, false);
        let rec = &records[0];
        assert!(
            rec.get("html_full").is_none(),
            "html_full should be stripped"
        );
        assert!(
            rec.get("body_text").is_none(),
            "body_text should be stripped"
        );
        assert!(rec.get("html_preview").is_some());
    }

    #[test]
    fn test_load_sent_records_keeps_full_when_requested() {
        let data = vec![("a", "Subject", "<p>full body</p>")];
        let (_tmp, store) = make_store_with_records(&data);
        let records = load_sent_records(&store, 100, true);
        let rec = &records[0];
        assert_eq!(
            rec.get("html_full").and_then(|v| v.as_str()),
            Some("<p>full body</p>")
        );
    }

    #[test]
    fn test_load_sent_record_exact_id_match() {
        // Critical: substring must NOT match — "abc" must not match "abcd1234"
        let data = vec![
            ("abc", "Short ID", "<p>abc</p>"),
            ("abcd1234", "Long ID", "<p>abcd</p>"),
        ];
        let (_tmp, store) = make_store_with_records(&data);

        let r1 = load_sent_record(&store, "abc").expect("abc should match");
        assert_eq!(r1.get("subject").and_then(|v| v.as_str()), Some("Short ID"));

        let r2 = load_sent_record(&store, "abcd1234").expect("abcd1234 should match");
        assert_eq!(r2.get("subject").and_then(|v| v.as_str()), Some("Long ID"));

        // Substring must NOT match
        assert!(
            load_sent_record(&store, "ab").is_none(),
            "ab must not match abc"
        );
        assert!(
            load_sent_record(&store, "abc1").is_none(),
            "abc1 must not match abc"
        );
    }

    #[test]
    fn test_load_sent_record_handles_corruption() {
        let tmp = tempfile::tempdir().unwrap();
        let sent_dir = tmp.path().join("email_sent");
        fs::create_dir_all(&sent_dir).unwrap();
        // Write a valid and a corrupted file
        let good = serde_json::json!({
            "id": "good", "sent_at": "2026-06-06T00:00:00+00:00",
            "subject": "Good", "to": "x", "message_id": "m",
            "html_preview": "", "html_full": "", "template_used": null, "cron_job": null
        });
        fs::write(sent_dir.join("good.json"), good.to_string()).unwrap();
        fs::write(sent_dir.join("corrupt.json"), "NOT VALID JSON {{{").unwrap();

        let store = oxios_kernel::state_store::StateStore::new(tmp.path().to_path_buf()).unwrap();
        // Should not panic; good record should be returned
        let r = load_sent_record(&store, "good");
        assert!(r.is_some());
        // Corrupt file should be silently skipped, not crash
        let records = load_sent_records(&store, 100, false);
        assert_eq!(records.len(), 1);
    }

    #[test]
    fn test_append_email_section_creates_block() {
        let tmp = tempfile::tempdir().unwrap();
        let path = tmp.path().join("config.toml");
        fs::write(&path, "[kernel]\nworkspace = \"/tmp\"").unwrap();
        let config = oxios_kernel::config::EmailConfig {
            enabled: true,
            my_email: "me@gmail.com".to_string(),
            provider: oxios_kernel::email::SmtpProvider::Gmail,
            host: String::new(),
            port: 0,
            tls: None,
            user: String::new(),
            secret_ref: "email_smtp".to_string(),
            rate_limit_per_hour: 10,
        };
        append_email_section_to_config(&path, &config).unwrap();
        let content = fs::read_to_string(&path).unwrap();
        assert!(content.contains("[email]"));
        assert!(content.contains("my_email = \"me@gmail.com\""));
        assert!(content.contains("provider = \"gmail\""));
        // Original [kernel] must be preserved
        assert!(content.contains("[kernel]"));
    }

    #[test]
    fn test_append_email_section_idempotent() {
        let tmp = tempfile::tempdir().unwrap();
        let path = tmp.path().join("config.toml");
        let config = oxios_kernel::config::EmailConfig {
            enabled: true,
            my_email: "me@gmail.com".to_string(),
            provider: oxios_kernel::email::SmtpProvider::Gmail,
            host: String::new(),
            port: 0,
            tls: None,
            user: String::new(),
            secret_ref: "email_smtp".to_string(),
            rate_limit_per_hour: 10,
        };
        // Pre-populate with [email] section
        fs::write(&path, "[email]\nenabled = true\n").unwrap();
        // Should be a no-op
        append_email_section_to_config(&path, &config).unwrap();
        let content = fs::read_to_string(&path).unwrap();
        // Should not duplicate
        assert_eq!(content.matches("[email]").count(), 1);
    }
}