mold-ai-tui 0.13.1

Terminal UI for mold — interactive AI image generation
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
//! Prompt history, backed by the `prompt_history` table in the metadata DB.
//!
//! The in-memory shape (cursor + draft for prev/next navigation) is the
//! same as the previous JSONL-backed implementation. Only the storage
//! moved: [`Self::load`] reads from the DB, [`Self::save`] writes there,
//! and the first launch on an upgraded install imports
//! `~/.mold/prompt-history.jsonl` via [`import_legacy_jsonl`].

use mold_db::{HistoryEntry as DbEntry, MetadataDb, PromptHistory as DbHistory};
use serde::{Deserialize, Serialize};

/// Matches the legacy limit from the JSONL writer.
const MAX_ENTRIES: usize = 500;

/// A single prompt history entry. Kept `Serialize`/`Deserialize` so the
/// legacy JSONL importer can parse old files byte-for-byte.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HistoryEntry {
    pub prompt: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub negative: Option<String>,
    #[serde(default)]
    pub model: String,
    #[serde(default)]
    pub timestamp: u64,
}

/// Prompt history with navigation and fuzzy search.
pub struct PromptHistory {
    /// In-memory cache; source of truth for prev/next navigation.
    /// Ordered oldest-first to match legacy semantics.
    entries: Vec<HistoryEntry>,
    /// Current position in history (None = not navigating, 0 = oldest).
    cursor: Option<usize>,
    /// The prompt text before the user started navigating (to restore on cancel).
    draft: Option<String>,
}

fn open_db() -> Option<MetadataDb> {
    match mold_db::open_default() {
        Ok(Some(db)) => Some(db),
        _ => None,
    }
}

impl PromptHistory {
    /// Build an empty history without touching the DB. Used by tests
    /// that construct ad-hoc `App` values and don't care about the
    /// persistent history — prevents parallel test runs from racing on
    /// `MOLD_DB_PATH` mid-flight.
    pub fn empty() -> Self {
        Self {
            entries: Vec::new(),
            cursor: None,
            draft: None,
        }
    }

    /// Load history from the DB (newest first in storage, flipped to
    /// oldest-first for the in-memory cache so `prev()`/`next()` keep
    /// their existing semantics). Returns empty when the DB is
    /// unavailable.
    pub fn load() -> Self {
        let mut entries = Vec::new();
        if let Some(db) = open_db() {
            let h = DbHistory::new(&db);
            if let Ok(rows) = h.recent(MAX_ENTRIES) {
                // DB returns newest-first; flip to oldest-first.
                entries = rows
                    .into_iter()
                    .rev()
                    .map(|e: DbEntry| HistoryEntry {
                        prompt: e.prompt,
                        negative: e.negative,
                        model: e.model,
                        timestamp: (e.created_at_ms / 1000).max(0) as u64,
                    })
                    .collect();
            }
        }
        Self {
            entries,
            cursor: None,
            draft: None,
        }
    }

    /// Append an entry and persist to the DB (best-effort).
    pub fn push(&mut self, entry: HistoryEntry) {
        if self.push_entry(entry.clone()) {
            self.persist(&entry);
        }
    }

    /// Append an entry without persisting. Returns true if entry was added.
    pub(crate) fn push_entry(&mut self, entry: HistoryEntry) -> bool {
        if entry.prompt.trim().is_empty() {
            return false;
        }
        if let Some(last) = self.entries.last() {
            if last.prompt == entry.prompt {
                return false;
            }
        }
        self.entries.push(entry);
        if self.entries.len() > MAX_ENTRIES {
            let excess = self.entries.len() - MAX_ENTRIES;
            self.entries.drain(..excess);
        }
        true
    }

    /// Persist a single entry to the DB and trim the table to MAX_ENTRIES.
    fn persist(&self, entry: &HistoryEntry) {
        let Some(db) = open_db() else {
            return;
        };
        let h = DbHistory::new(&db);
        let db_entry = DbEntry {
            prompt: entry.prompt.clone(),
            negative: entry.negative.clone(),
            model: entry.model.clone(),
            // `timestamp` is seconds; the DB wants ms. 0 means "stamp now".
            created_at_ms: if entry.timestamp == 0 {
                0
            } else {
                (entry.timestamp as i64) * 1000
            },
        };
        if let Err(e) = h.push(&db_entry) {
            tracing::warn!(error = %e, "prompt history: push failed");
        }
        if let Err(e) = h.trim_to(MAX_ENTRIES) {
            tracing::warn!(error = %e, "prompt history: trim failed");
        }
    }

    /// Start or continue navigating backward through history.
    pub fn prev(&mut self, current_prompt: &str) -> Option<&str> {
        if self.entries.is_empty() {
            return None;
        }
        let new_cursor = match self.cursor {
            None => {
                self.draft = Some(current_prompt.to_string());
                self.entries.len().saturating_sub(1)
            }
            Some(pos) => {
                if pos == 0 {
                    return None;
                }
                pos - 1
            }
        };
        self.cursor = Some(new_cursor);
        Some(&self.entries[new_cursor].prompt)
    }

    /// Navigate forward through history toward the draft.
    pub fn next(&mut self, _current_prompt: &str) -> Option<&str> {
        match self.cursor {
            None => None,
            Some(pos) => {
                if pos + 1 >= self.entries.len() {
                    self.cursor = None;
                    self.draft.as_deref()
                } else {
                    self.cursor = Some(pos + 1);
                    Some(&self.entries[pos + 1].prompt)
                }
            }
        }
    }

    pub fn reset_cursor(&mut self) {
        self.cursor = None;
        self.draft = None;
    }

    /// Search history entries by substring (case-insensitive).
    pub fn search(&self, query: &str) -> Vec<&HistoryEntry> {
        let query_lower = query.to_lowercase();
        self.entries
            .iter()
            .rev()
            .filter(|e| e.prompt.to_lowercase().contains(&query_lower))
            .collect()
    }

    pub fn len(&self) -> usize {
        self.entries.len()
    }

    pub fn recent(&self, max: usize) -> impl Iterator<Item = &HistoryEntry> {
        self.entries.iter().rev().take(max)
    }

    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }

    pub fn is_navigating(&self) -> bool {
        self.cursor.is_some()
    }
}

/// Import `~/.mold/prompt-history.jsonl` into the DB exactly once.
/// Called from `session::import_legacy_json_once`; no-op if the file is
/// missing or the DB already holds rows.
pub(crate) fn import_legacy_jsonl(db: &MetadataDb) {
    let path = match mold_core::Config::mold_dir().map(|d| d.join("prompt-history.jsonl")) {
        Some(p) if p.exists() => p,
        _ => return,
    };
    let contents = match std::fs::read_to_string(&path) {
        Ok(c) => c,
        Err(e) => {
            tracing::warn!(path = %path.display(), error = %e,
                "prompt history: legacy JSONL read failed");
            return;
        }
    };
    let entries: Vec<HistoryEntry> = contents
        .lines()
        .filter_map(|line| serde_json::from_str::<HistoryEntry>(line).ok())
        .collect();
    if entries.is_empty() {
        // Still rename the file so we don't reparse on every launch.
        rename_to_migrated(&path);
        return;
    }

    let h = DbHistory::new(db);
    let mut imported = 0;
    for e in &entries {
        let db_entry = DbEntry {
            prompt: e.prompt.clone(),
            negative: e.negative.clone(),
            model: e.model.clone(),
            created_at_ms: if e.timestamp == 0 {
                0
            } else {
                (e.timestamp as i64) * 1000
            },
        };
        if h.push(&db_entry).is_ok() {
            imported += 1;
        }
    }
    let _ = h.trim_to(MAX_ENTRIES);
    rename_to_migrated(&path);
    tracing::info!(
        path = %path.display(),
        imported,
        "imported legacy prompt-history.jsonl into metadata DB"
    );
}

fn rename_to_migrated(path: &std::path::Path) {
    if let Some(fname) = path.file_name().and_then(|n| n.to_str()) {
        if let Some(parent) = path.parent() {
            let dst = parent.join(format!("{fname}.migrated"));
            if let Err(e) = std::fs::rename(path, &dst) {
                tracing::warn!(src = %path.display(), dst = %dst.display(), error = %e,
                    "rename legacy history file to .migrated failed");
            }
        }
    }
}

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

    fn make_entry(prompt: &str) -> HistoryEntry {
        HistoryEntry {
            prompt: prompt.to_string(),
            negative: None,
            model: "test".to_string(),
            timestamp: 0,
        }
    }

    // ---------- in-memory navigation + trim behaviour ----------
    // These tests exercise the cache logic and don't touch the DB, so they
    // don't need env isolation.

    #[test]
    fn push_and_len() {
        let mut history = PromptHistory {
            entries: Vec::new(),
            cursor: None,
            draft: None,
        };
        history.entries.push(make_entry("first"));
        history.entries.push(make_entry("second"));
        assert_eq!(history.len(), 2);
    }

    #[test]
    fn push_deduplicates_consecutive() {
        let mut history = PromptHistory {
            entries: Vec::new(),
            cursor: None,
            draft: None,
        };
        history.push_entry(make_entry("hello"));
        history.push_entry(make_entry("hello"));
        assert_eq!(history.len(), 1);
    }

    #[test]
    fn push_skips_empty_prompts() {
        let mut history = PromptHistory {
            entries: Vec::new(),
            cursor: None,
            draft: None,
        };
        assert!(!history.push_entry(make_entry("")));
        assert!(!history.push_entry(make_entry("   ")));
        assert!(history.push_entry(make_entry("real prompt")));
        assert_eq!(history.len(), 1);
    }

    #[test]
    fn push_trims_oldest() {
        let mut history = PromptHistory {
            entries: (0..MAX_ENTRIES + 10)
                .map(|i| make_entry(&format!("prompt {i}")))
                .collect(),
            cursor: None,
            draft: None,
        };
        history.push_entry(make_entry("new"));
        assert!(history.len() <= MAX_ENTRIES);
        assert_eq!(history.entries.last().unwrap().prompt, "new");
    }

    #[test]
    fn prev_navigates_backward() {
        let mut history = PromptHistory {
            entries: vec![
                make_entry("first"),
                make_entry("second"),
                make_entry("third"),
            ],
            cursor: None,
            draft: None,
        };
        assert_eq!(history.prev("draft"), Some("third"));
        assert_eq!(history.prev("draft"), Some("second"));
        assert_eq!(history.prev("draft"), Some("first"));
        assert_eq!(history.prev("draft"), None);
    }

    #[test]
    fn next_navigates_forward_to_draft() {
        let mut history = PromptHistory {
            entries: vec![make_entry("old"), make_entry("new")],
            cursor: None,
            draft: None,
        };
        history.prev("my draft");
        history.prev("my draft");
        assert_eq!(history.next(""), Some("new"));
        assert_eq!(history.next(""), Some("my draft"));
        assert!(!history.is_navigating());
    }

    #[test]
    fn search_case_insensitive() {
        let history = PromptHistory {
            entries: vec![
                make_entry("a Cat in a hat"),
                make_entry("sunset mountains"),
                make_entry("CATS everywhere"),
            ],
            cursor: None,
            draft: None,
        };
        let results = history.search("cat");
        assert_eq!(results.len(), 2);
        assert_eq!(results[0].prompt, "CATS everywhere");
        assert_eq!(results[1].prompt, "a Cat in a hat");
    }

    #[test]
    fn reset_cursor_clears_state() {
        let mut history = PromptHistory {
            entries: vec![make_entry("test")],
            cursor: None,
            draft: None,
        };
        history.prev("draft");
        assert!(history.is_navigating());
        history.reset_cursor();
        assert!(!history.is_navigating());
    }

    #[test]
    fn entry_serialization() {
        let entry = HistoryEntry {
            prompt: "a cat".to_string(),
            negative: Some("blurry".to_string()),
            model: "flux:q8".to_string(),
            timestamp: 12345,
        };
        let json = serde_json::to_string(&entry).unwrap();
        let restored: HistoryEntry = serde_json::from_str(&json).unwrap();
        assert_eq!(restored.prompt, "a cat");
        assert_eq!(restored.negative, Some("blurry".to_string()));
    }

    #[test]
    fn prev_does_not_navigate_when_empty() {
        let mut history = PromptHistory {
            entries: Vec::new(),
            cursor: None,
            draft: None,
        };
        assert!(history.prev("current").is_none());
        assert!(!history.is_navigating());
    }

    #[test]
    fn next_without_prev_returns_none() {
        let mut history = PromptHistory {
            entries: vec![make_entry("test")],
            cursor: None,
            draft: None,
        };
        assert!(history.next("current").is_none());
    }

    #[test]
    fn push_entry_returns_false_for_duplicates() {
        let mut history = PromptHistory {
            entries: Vec::new(),
            cursor: None,
            draft: None,
        };
        assert!(history.push_entry(make_entry("hello")));
        assert!(!history.push_entry(make_entry("hello")));
        assert!(history.push_entry(make_entry("world")));
    }

    #[test]
    fn recent_yields_newest_first_up_to_max() {
        let mut history = PromptHistory {
            entries: vec![
                make_entry("oldest"),
                make_entry("middle"),
                make_entry("newest"),
            ],
            cursor: None,
            draft: None,
        };
        let prompts: Vec<&str> = history.recent(5).map(|e| e.prompt.as_str()).collect();
        assert_eq!(prompts, vec!["newest", "middle", "oldest"]);

        let capped: Vec<&str> = history.recent(1).map(|e| e.prompt.as_str()).collect();
        assert_eq!(capped, vec!["newest"]);

        history.entries.clear();
        assert_eq!(history.recent(3).count(), 0);
    }

    // ---------- DB round-trip ----------

    use crate::test_env::with_isolated_env;
    use serial_test::serial;

    #[test]
    #[serial(mold_env)]
    fn push_then_load_roundtrips_through_db() {
        with_isolated_env(|_home| {
            let mut h = PromptHistory::load();
            h.push(HistoryEntry {
                prompt: "first prompt".into(),
                negative: None,
                model: "flux-dev:q4".into(),
                timestamp: 1_000,
            });
            h.push(HistoryEntry {
                prompt: "second prompt".into(),
                negative: Some("ugly".into()),
                model: "sdxl:fp16".into(),
                timestamp: 2_000,
            });

            let reloaded = PromptHistory::load();
            // Cache is oldest-first.
            let prompts: Vec<_> = reloaded.entries.iter().map(|e| &e.prompt).collect();
            assert_eq!(
                prompts,
                vec![&"first prompt".to_string(), &"second prompt".to_string()]
            );
        });
    }

    #[test]
    #[serial(mold_env)]
    fn legacy_jsonl_import_populates_db_and_renames_file() {
        with_isolated_env(|home| {
            let src = home.join("prompt-history.jsonl");
            std::fs::write(
                &src,
                r#"{"prompt":"cat","model":"m","timestamp":1000}
{"prompt":"dog","model":"m","timestamp":2000}
{"prompt":"bird","model":"m","timestamp":3000}"#,
            )
            .unwrap();

            // Trigger import via TuiSession::load (the unified entry point).
            let _ = super::super::session::TuiSession::load();

            assert!(!src.exists());
            assert!(home.join("prompt-history.jsonl.migrated").exists());

            let h = PromptHistory::load();
            let prompts: Vec<_> = h.entries.iter().map(|e| e.prompt.as_str()).collect();
            assert_eq!(prompts, vec!["cat", "dog", "bird"]);
        });
    }

    #[test]
    #[serial(mold_env)]
    fn db_disabled_keeps_history_in_memory_only() {
        with_isolated_env(|_home| {
            std::env::set_var("MOLD_DB_DISABLE", "1");
            let mut h = PromptHistory::load();
            h.push(HistoryEntry {
                prompt: "in memory".into(),
                negative: None,
                model: "m".into(),
                timestamp: 0,
            });
            assert_eq!(h.len(), 1);

            // A fresh load returns empty because nothing was persisted.
            let fresh = PromptHistory::load();
            assert!(fresh.is_empty());
            std::env::remove_var("MOLD_DB_DISABLE");
        });
    }
}