mps-rs 1.8.2

MPS — plain-text personal productivity CLI (Rust)
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
use crate::config::{ChatConfig, Config};
use crate::llm::context::build_system_prompt;
use crate::llm::session::{validate_name, ChatSession, SessionContextConfig};
use crate::llm::{LlmClient, Message};
use crate::store::Store;
use anyhow::{bail, Context as _};
use chrono::Local;
use futures_util::StreamExt;
use rustyline::error::ReadlineError;
use rustyline::DefaultEditor;
use std::io::Write as _;
use std::path::{Path, PathBuf};
use std::time::Duration;

pub struct ChatCliArgs {
    pub url: Option<String>,
    pub model: Option<String>,
    pub api_key: Option<String>,
    pub context_days: Option<u64>,
    pub since: Option<String>,
    pub all: bool,
    pub stream: Option<bool>,
    pub session_name: Option<String>,
    pub new: bool,
    pub list_sessions: bool,
}

pub async fn run(config: Config, args: ChatCliArgs) -> anyhow::Result<()> {
    // 1. Build effective ChatConfig from config + cli overrides
    let mut chat_cfg = config.chat.clone();
    if let Some(ref url) = args.url {
        chat_cfg.url = Some(url.clone());
    }
    if let Some(ref model) = args.model {
        chat_cfg.model = model.clone();
    }
    if let Some(ref key) = args.api_key {
        chat_cfg.api_key = key.clone();
    }
    if let Some(days) = args.context_days {
        chat_cfg.context_days = days;
    }
    if let Some(stream) = args.stream {
        chat_cfg.stream = stream;
    }

    // 2. Resolve sessions directory
    let sessions_dir = resolve_sessions_dir(&config, &chat_cfg);

    // 3. --list-sessions: print table and return
    if args.list_sessions {
        return list_sessions(&sessions_dir);
    }

    // 4. Auto-detect URL if not set
    let base_url = match chat_cfg.url.clone() {
        Some(u) => u,
        None => match LlmClient::auto_detect().await {
            Some(u) => u,
            None => bail!(
                "Cannot reach LLM — no server found on :11434 or :8080.\n\
                     Start ollama/llama-server or set 'chat.url' in config."
            ),
        },
    };

    let client = LlmClient::new(
        &base_url,
        &chat_cfg.model,
        &chat_cfg.api_key,
        chat_cfg.connect_timeout_secs,
    );

    // 5. Load or create session
    let (mut session, resumed) = match &args.session_name {
        None => (None, false),
        Some(name) => {
            if !args.new {
                let path = sessions_dir.join(format!("{}.json", name));
                if path.exists() {
                    match ChatSession::load(&sessions_dir, name) {
                        Ok(s) => {
                            let n = s.messages.len();
                            (Some(s), n > 0)
                        }
                        Err(e) => {
                            eprintln!(
                                "warning: could not load session '{}': {} — starting fresh.",
                                name, e
                            );
                            (
                                Some(ChatSession::new(
                                    name,
                                    make_context_config(&chat_cfg, &args),
                                )),
                                false,
                            )
                        }
                    }
                } else {
                    (
                        Some(ChatSession::new(
                            name,
                            make_context_config(&chat_cfg, &args),
                        )),
                        false,
                    )
                }
            } else {
                (
                    Some(ChatSession::new(
                        name,
                        make_context_config(&chat_cfg, &args),
                    )),
                    false,
                )
            }
        }
    };

    // 6. Build system prompt fresh from current journal
    let store = Store::new(&config.storage_dir);
    let today = Local::now().date_naive();
    let since = if args.all {
        chrono::NaiveDate::from_ymd_opt(2000, 1, 1).unwrap()
    } else {
        match &args.since {
            Some(s) => crate::date_parse::parse_date(s)
                .with_context(|| format!("cannot parse --since date '{}'", s))?,
            None => {
                let days_back = chat_cfg.context_days.max(1) as i64 - 1;
                today - chrono::Duration::days(days_back)
            }
        }
    };

    let (system_text, element_count) =
        build_system_prompt(&store, since, today).context("failed to build journal context")?;

    // 7. Print header
    let display_url = base_url
        .trim_start_matches("http://")
        .trim_start_matches("https://");
    println!("\n  chat  —  {} @ {}", chat_cfg.model, display_url);
    println!(
        "  context: {}, last {} days ({} elements)",
        today.format("%Y-%m-%d"),
        chat_cfg.context_days,
        element_count
    );
    if let Some(ref s) = session {
        let status = if resumed {
            format!("(resumed, {} messages)", s.messages.len())
        } else {
            "(new)".to_string()
        };
        println!("  session: {}  {}", s.name, status);
    }
    println!("  :help  :clear  :context  :session  :save  :sessions  :quit\n");

    // 8. Assemble initial message list
    let system_msg = Message::system(&system_text);
    let mut messages: Vec<Message> = std::iter::once(system_msg.clone())
        .chain(
            session
                .as_ref()
                .map(|s| s.to_messages())
                .unwrap_or_default(),
        )
        .collect();

    // 9. rustyline REPL
    let mut rl = DefaultEditor::new().context("readline init")?;

    loop {
        let readline = rl.readline("> ");
        match readline {
            Ok(line) => {
                let input = line.trim().to_string();
                if input.is_empty() {
                    continue;
                }
                let _ = rl.add_history_entry(&input);

                // Session commands
                if input == ":quit" || input == ":exit" {
                    auto_save(&mut session, &sessions_dir);
                    break;
                }
                if input == ":help" {
                    print_help();
                    continue;
                }
                if input == ":context" {
                    println!("{}\n", system_text);
                    continue;
                }
                if input == ":clear" {
                    messages = vec![system_msg.clone()];
                    if let Some(ref mut s) = session {
                        s.messages.clear();
                        let _ = s.save(&sessions_dir);
                    }
                    println!("  context cleared.\n");
                    continue;
                }
                if input == ":reload" {
                    match build_system_prompt(&store, since, today) {
                        Ok((new_text, new_count)) => {
                            let new_sys = Message::system(&new_text);
                            messages[0] = new_sys.clone();
                            println!("  context reloaded ({} elements).\n", new_count);
                        }
                        Err(e) => eprintln!("  reload failed: {}\n", e),
                    }
                    continue;
                }
                if input == ":session" {
                    match &session {
                        Some(s) => println!(
                            "  session: {}  |  {} messages  |  updated {}\n",
                            s.name,
                            s.messages.len(),
                            s.updated_at.format("%Y-%m-%d %H:%M")
                        ),
                        None => println!("  no active session (ephemeral).\n"),
                    }
                    continue;
                }
                if input == ":sessions" {
                    let _ = list_sessions(&sessions_dir);
                    continue;
                }
                if input == ":save" {
                    match &mut session {
                        Some(s) => match s.save(&sessions_dir) {
                            Ok(()) => println!("  saved session '{}'.\n", s.name),
                            Err(e) => eprintln!("  save failed: {}\n", e),
                        },
                        None => {
                            eprintln!("  no session name — use :save NAME to name this session.\n")
                        }
                    }
                    continue;
                }
                if let Some(name) = input
                    .strip_prefix(":save ")
                    .map(|s| s.trim())
                    .filter(|s| !s.is_empty())
                {
                    if let Err(e) = validate_name(name) {
                        eprintln!("  invalid session name: {}\n", e);
                        continue;
                    }
                    match &mut session {
                        Some(s) => {
                            s.name = name.to_string();
                            match s.save(&sessions_dir) {
                                Ok(()) => println!("  saved as '{}'.\n", name),
                                Err(e) => eprintln!("  save failed: {}\n", e),
                            }
                        }
                        None => {
                            let mut new_session =
                                ChatSession::new(name, make_context_config(&chat_cfg, &args));
                            new_session.messages = messages[1..]
                                .iter()
                                .enumerate()
                                .map(|(i, m)| crate::llm::session::SessionMessage {
                                    role: m.role.clone(),
                                    content: m.content.clone(),
                                    ts: chrono::Utc::now() + chrono::Duration::seconds(i as i64),
                                })
                                .collect();
                            new_session.llm_hint = format!("{} @ {}", chat_cfg.model, base_url);
                            match new_session.save(&sessions_dir) {
                                Ok(()) => println!("  saved as '{}'.\n", name),
                                Err(e) => eprintln!("  save failed: {}\n", e),
                            }
                            session = Some(new_session);
                        }
                    }
                    continue;
                }
                if let Some(name) = input
                    .strip_prefix(":save-as ")
                    .map(|s| s.trim())
                    .filter(|s| !s.is_empty())
                {
                    if let Err(e) = validate_name(name) {
                        eprintln!("  invalid session name: {}\n", e);
                        continue;
                    }
                    let old_messages = messages[1..].to_vec();
                    let mut new_session =
                        ChatSession::new(name, make_context_config(&chat_cfg, &args));
                    new_session.messages = old_messages
                        .iter()
                        .enumerate()
                        .map(|(i, m)| crate::llm::session::SessionMessage {
                            role: m.role.clone(),
                            content: m.content.clone(),
                            ts: chrono::Utc::now() + chrono::Duration::seconds(i as i64),
                        })
                        .collect();
                    new_session.llm_hint = format!("{} @ {}", chat_cfg.model, base_url);
                    match new_session.save(&sessions_dir) {
                        Ok(()) => println!("  saved copy as '{}'.\n", name),
                        Err(e) => eprintln!("  save failed: {}\n", e),
                    }
                    session = Some(new_session);
                    continue;
                }
                if let Some(name) = input
                    .strip_prefix(":load ")
                    .map(|s| s.trim())
                    .filter(|s| !s.is_empty())
                {
                    match ChatSession::load(&sessions_dir, name) {
                        Ok(loaded) => {
                            let loaded_msgs = loaded.to_messages();
                            messages = std::iter::once(messages[0].clone())
                                .chain(loaded_msgs)
                                .collect();
                            println!(
                                "  loaded session '{}' ({} messages).\n",
                                name,
                                loaded.messages.len()
                            );
                            session = Some(loaded);
                        }
                        Err(e) => eprintln!("  load failed: {}\n", e),
                    }
                    continue;
                }

                // Regular input → chat
                messages.push(Message::user(&input));
                if let Some(ref mut s) = session {
                    s.push_user(input.clone());
                }

                let assistant_text = if chat_cfg.stream {
                    stream_response(&client, &messages).await
                } else {
                    non_stream_response(&client, &messages).await
                };

                match assistant_text {
                    Ok(text) => {
                        messages.push(Message::assistant(&text));
                        if let Some(ref mut s) = session {
                            s.push_assistant(text.clone());
                            s.llm_hint = format!("{} @ {}", chat_cfg.model, base_url);
                            if let Err(e) = s.save(&sessions_dir) {
                                eprintln!("  auto-save failed: {}\n", e);
                            }
                        }
                        println!();
                    }
                    Err(e) => {
                        // Pop the user message we added since no response was received
                        messages.pop();
                        if let Some(ref mut s) = session {
                            s.messages.pop();
                        }
                        eprintln!("\n  error: {}\n", e);
                    }
                }
            }
            Err(ReadlineError::Interrupted) => {
                // Ctrl-C cancels current input, continue loop
                println!();
            }
            Err(ReadlineError::Eof) => {
                // Ctrl-D exits
                auto_save(&mut session, &sessions_dir);
                break;
            }
            Err(e) => {
                eprintln!("readline error: {}", e);
                break;
            }
        }
    }

    println!("\n  goodbye.\n");
    Ok(())
}

async fn stream_response(client: &LlmClient, messages: &[Message]) -> anyhow::Result<String> {
    // Print the first frame synchronously — guaranteed visible even for sub-80ms responses.
    print!("  ⠋ thinking…");
    std::io::stdout().flush().ok();

    let (stop_tx, stop_rx) = tokio::sync::oneshot::channel::<()>();
    let spinner = tokio::spawn(run_spinner(stop_rx));

    let stream_result = client.chat_stream(messages).await;
    let _ = stop_tx.send(());
    spinner.await.ok();

    let mut stream = stream_result?;
    let mut full_text = String::new();
    println!();
    while let Some(chunk) = stream.next().await {
        let token = chunk?;
        if !token.is_empty() {
            print!("{}", token);
            std::io::stdout().flush().ok();
            full_text.push_str(&token);
        }
    }
    println!();
    Ok(full_text)
}

async fn non_stream_response(client: &LlmClient, messages: &[Message]) -> anyhow::Result<String> {
    print!("  ⠋ thinking…");
    std::io::stdout().flush().ok();

    let (stop_tx, stop_rx) = tokio::sync::oneshot::channel::<()>();
    let spinner = tokio::spawn(run_spinner(stop_rx));

    let stream_result = client.chat_stream(messages).await;
    let _ = stop_tx.send(());
    spinner.await.ok();

    let mut stream = stream_result?;
    let mut full_text = String::new();
    while let Some(chunk) = stream.next().await {
        full_text.push_str(&chunk?);
    }
    println!("\n{}\n", full_text);
    Ok(full_text)
}

// Animates a Braille spinner on the current line until the stop signal fires,
// then erases the line so the response prints cleanly from column 0.
async fn run_spinner(mut stop: tokio::sync::oneshot::Receiver<()>) {
    // First frame already printed by the caller; start from the second frame.
    const FRAMES: &[&str] = &["", "", "", "", "", "", "", "", "", ""];
    let mut i = 0usize;
    loop {
        tokio::select! {
            _ = &mut stop => break,
            _ = tokio::time::sleep(Duration::from_millis(80)) => {
                print!("\r  {} thinking…", FRAMES[i % FRAMES.len()]);
                std::io::stdout().flush().ok();
                i += 1;
            }
        }
    }
    print!("\r                    \r");
    std::io::stdout().flush().ok();
}

fn auto_save(session: &mut Option<ChatSession>, sessions_dir: &Path) {
    if let Some(ref mut s) = session {
        if !s.messages.is_empty() {
            let _ = s.save(sessions_dir);
        }
    }
}

fn list_sessions(sessions_dir: &Path) -> anyhow::Result<()> {
    let summaries = ChatSession::list(sessions_dir)?;
    if summaries.is_empty() {
        println!("  no saved sessions.\n");
        return Ok(());
    }
    println!();
    println!("  {:<24} {:<20} messages", "session", "updated");
    println!("  {}", "".repeat(56));
    for s in &summaries {
        println!(
            "  {:<24} {:<20} {}",
            s.name,
            s.updated_at.format("%Y-%m-%d %H:%M"),
            s.message_count
        );
    }
    println!();
    Ok(())
}

fn print_help() {
    println!(
        "\n  session commands:\n\
         \n\
         :help               print this help\n\
         :context            print the journal context (system prompt)\n\
         :clear              clear chat history (keep session name)\n\
         :reload             rebuild journal context from disk\n\
         :session            show current session info\n\
         :sessions           list all saved sessions\n\
         :save               save to current session name\n\
         :save NAME          save (and name) session as NAME\n\
         :save-as NAME       copy session as NAME, switch to it\n\
         :load NAME          load session NAME into current chat\n\
         :quit / :exit       save and quit\n\
         Ctrl-D              save and quit\n\
         Ctrl-C              cancel current input\n"
    );
}

fn resolve_sessions_dir(config: &Config, chat_cfg: &ChatConfig) -> PathBuf {
    match &chat_cfg.sessions_dir {
        Some(dir) => PathBuf::from(dir),
        None => config.mps_dir.join("sessions"),
    }
}

fn make_context_config(chat_cfg: &ChatConfig, args: &ChatCliArgs) -> SessionContextConfig {
    SessionContextConfig {
        context_days: chat_cfg.context_days,
        since: args.since.clone(),
    }
}