srz 0.6.1

Fast, modular CLI to summarize webpages, documents, books, and live news using local LLMs
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
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
use clap::{Parser, Subcommand, ValueEnum};
use serde::{Deserialize, Serialize};
use std::path::PathBuf;

#[derive(ValueEnum, Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum SummaryStyle {
    /// Concise summary with key takeaways
    Concise,
    /// Detailed comprehensive summary
    Detailed,
    /// Bulleted key points
    Bullets,
}

impl SummaryStyle {
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Concise => "concise",
            Self::Detailed => "detailed",
            Self::Bullets => "bullets",
        }
    }

    pub fn prompt_guidance(&self) -> &'static str {
        match self {
            Self::Concise => {
                "Provide a concise, high-level summary capturing the core message in 2-3 brief paragraphs."
            }
            Self::Detailed => {
                "Provide a detailed, structured summary covering all major sections, themes, arguments, and conclusions."
            }
            Self::Bullets => {
                "Provide a structured summary formatted as clear, concise bullet points highlighting key insights."
            }
        }
    }
}

#[derive(ValueEnum, Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
#[serde(rename_all = "lowercase")]
pub enum NewsTimeframe {
    /// Past 24 hours / current day (default)
    #[default]
    Day,
    /// Past 7 days / week
    Week,
    /// Past 30 days / month
    Month,
    /// Any time / all results
    All,
}

impl NewsTimeframe {
    pub fn as_google_query_suffix(&self) -> &'static str {
        match self {
            Self::Day => "when:1d",
            Self::Week => "when:7d",
            Self::Month => "when:30d",
            Self::All => "",
        }
    }

    pub fn display_label(&self) -> &'static str {
        match self {
            Self::Day => "Today / Past 24 Hours",
            Self::Week => "Past Week",
            Self::Month => "Past Month",
            Self::All => "All Time",
        }
    }
}

#[derive(ValueEnum, Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
#[serde(rename_all = "lowercase")]
pub enum PagingMode {
    /// Automatically page output when it exceeds terminal height
    #[default]
    Auto,
    /// Always page output through a terminal pager
    Always,
    /// Never page output
    Never,
}

#[derive(Parser, Debug)]
#[command(
    name = "srz",
    version,
    about = "Modular CLI to summarize webpages and books using local OpenAI-compliant LLMs",
    long_about = "A fast, modular CLI that extracts and summarizes webpages and books. \
                  Supports local OpenAI-compatible backends like Ollama, LM Studio, vLLM, and llama.cpp. \
                  For books, searches the web by default for grounded synopses, with automatic fallback to LLM memory."
)]
pub struct Cli {
    #[command(subcommand)]
    pub command: Option<Commands>,

    /// Auto-detected target (URL or book title) when no subcommand is specified
    #[arg(value_name = "TARGET")]
    pub target: Option<String>,

    /// Local OpenAI-compatible base URL
    #[arg(
        long,
        global = true,
        env = "OPENAI_BASE_URL",
        default_value = "http://localhost:9931/v1"
    )]
    pub base_url: String,

    /// Model name to use
    #[arg(
        long,
        global = true,
        env = "OPENAI_MODEL",
        default_value = "ggml-org/gemma-4-E4B-it-GGUF:Q8_0"
    )]
    pub model: String,

    /// OpenAI API key (optional for local LLMs)
    #[arg(long, global = true, env = "OPENAI_API_KEY", default_value = "")]
    pub api_key: String,

    /// Output summary style
    #[arg(long, global = true, value_enum, default_value = "concise")]
    pub style: SummaryStyle,

    /// Maximum tokens to generate (configured via config.toml; default: 8192)
    #[arg(skip = 8192_u32)]
    pub max_tokens: u32,

    /// Bypass local cache and force fresh web scraping and LLM summarization
    #[arg(long, global = true)]
    pub no_cache: bool,

    /// Path to optional config file (defaults to ~/.config/srz/config.toml)
    #[arg(long, global = true)]
    pub config: Option<PathBuf>,

    /// Named configuration profile from config.toml to use
    #[arg(long, global = true)]
    pub profile: Option<String>,

    /// Save the summary to an output file
    #[arg(short, long, global = true)]
    pub output: Option<PathBuf>,

    /// Suppress status messages, spinners, and diagnostics
    #[arg(short, long, global = true)]
    pub quiet: bool,

    /// Display verbose diagnostic and timing logs
    #[arg(short, long, global = true)]
    pub verbose: bool,

    /// Format output as machine-readable JSON
    #[arg(long, global = true)]
    pub json: bool,

    /// Automatically open the styled HTML summary in the default web browser
    #[arg(short = 'b', long, global = true)]
    pub open: bool,

    /// Concurrency limit for parallel map-reduce document chunking (configured via config.toml)
    #[arg(skip = 1_usize)]
    pub concurrency: usize,

    /// Stream LLM output tokens live to stdout as they are generated (default: false)
    #[arg(long, global = true)]
    pub stream: bool,

    /// Control terminal paging of summary output (auto, always, never)
    #[arg(long, global = true, value_enum, default_value = "auto")]
    pub paging: PagingMode,

    /// Output raw, unstyled Markdown without terminal formatting or ANSI colors
    #[arg(long, global = true)]
    pub plain: bool,
}

impl Cli {
    /// Returns the effective paging mode.
    pub fn effective_paging(&self) -> PagingMode {
        self.paging
    }
}

#[derive(Subcommand, Debug)]
pub enum Commands {
    /// Scrape and summarize a webpage from a URL (hidden; prefer `srz <URL>`)
    #[command(hide = true)]
    Web {
        /// Webpage URL (http:// or https://)
        url: String,
    },
    /// Summarize a book by title (hidden; prefer `srz <TITLE>`)
    #[command(hide = true)]
    Book {
        /// Title of the book
        title: String,
    },
    /// Summarize a local file or piped input from standard input (-) (hidden; prefer `srz <FILE>`)
    #[command(hide = true)]
    File {
        /// Path to the local file, or '-' for stdin
        path: PathBuf,
    },
    /// Manage the local disk cache (dir, size, clean)
    Cache {
        #[command(subcommand)]
        action: CacheCommand,
    },
    /// Manage srz configuration (path, show, init)
    Config {
        #[command(subcommand)]
        action: ConfigSubcommand,
    },
    /// View recent summarization history and metrics
    History {
        /// Maximum entries to display (default: 10)
        #[arg(short, long, default_value_t = 10)]
        limit: usize,
    },
    /// Display aggregate library and LLM performance statistics
    Stats,
    /// Search your offline summarized library for keywords or phrases
    Find {
        /// Keyword or query to search for across titles and summaries
        query: String,
    },
    /// List available models from the local LLM backend
    Models,
    /// Diagnose system health, LLM connectivity, database, and cache
    Doctor,
    /// Generate and view the standalone HTML library dashboard
    Index,
    /// Launch the interactive terminal UI library explorer
    #[command(alias = "tui", alias = "library")]
    Browse,
    /// Ask questions grounded on a document, URL, file, or the most recent read
    #[command(alias = "chat")]
    Ask {
        /// Target document (URL/file/title) or question
        first: Option<String>,

        /// Question to ask (when target is provided as the first argument)
        second: Option<String>,

        /// Run in interactive multi-turn chat mode
        #[arg(short, long)]
        interactive: bool,

        /// List saved Q&A chat sessions
        #[arg(long)]
        chats: bool,

        /// Resume an existing chat session by its session ID
        #[arg(long)]
        resume: Option<i64>,

        /// Print the complete transcript of a past chat session
        #[arg(long)]
        show_chat: Option<i64>,

        /// Delete a saved chat session by its session ID
        #[arg(long)]
        delete_chat: Option<i64>,
    },
    /// Search and summarize latest news on a topic using Google News RSS
    News {
        /// Topic or query to search for (e.g. "javascript", "rust", "tech")
        topic: String,
        /// Timeframe for news recency (day, week, month, all; default: day)
        #[arg(short = 't', long, value_enum, default_value = "day")]
        timeframe: NewsTimeframe,
        /// Maximum number of top articles to aggregate and summarize (default: 5)
        #[arg(short, long, default_value_t = 5)]
        limit: usize,
    },
    /// Generate shell auto-completion script
    Completions {
        /// Target shell to generate completions for
        #[arg(value_enum)]
        shell: clap_complete::Shell,
    },
    /// Check for and install the latest srz release
    Upgrade {
        /// Only check if an update is available without downloading or installing
        #[arg(short, long)]
        check: bool,

        /// Force reinstall even if already on the latest version
        #[arg(short, long)]
        force: bool,

        /// Install a specific version (e.g. "0.2.0" or "v0.2.0")
        #[arg(long)]
        version: Option<String>,
    },
}

#[derive(Subcommand, Debug, Clone, PartialEq, Eq)]
pub enum CacheCommand {
    /// Print the cache directory path
    Dir,
    /// Show total disk usage and entry counts in the cache
    Size,
    /// Remove cached files (pages, search, summaries, views)
    Clean {
        /// Also reset and wipe the DuckDB analytics/history database
        #[arg(long)]
        reset_db: bool,
    },
    /// Prune expired or old cached entries based on retention policy
    Prune {
        /// Remove entries older than duration (e.g. 7d, 24h, 30m, 60s, 2w)
        #[arg(long)]
        older_than: Option<String>,

        /// Evict oldest entries until cache size is within limit (e.g. 500MB, 1GB, 50MB)
        #[arg(long)]
        max_size: Option<String>,
    },
}

#[derive(Subcommand, Debug, Clone, Copy, PartialEq, Eq)]
pub enum ConfigSubcommand {
    /// Print the config file path and existence status
    Path,
    /// Display active configuration
    Show,
    /// Generate a default ~/.config/srz/config.toml template
    Init,
}

#[derive(Debug, PartialEq, Eq)]
pub enum ResolvedAction<'a> {
    Web {
        url: &'a str,
    },
    Book {
        title: &'a str,
    },
    File {
        path: PathBuf,
    },
    Stdin,
    Cache(CacheCommand),
    Config(ConfigSubcommand),
    History {
        limit: usize,
    },
    Stats,
    Find {
        query: &'a str,
    },
    Models,
    Doctor,
    Index,
    Browse,
    Ask {
        target: Option<String>,
        question: Option<String>,
        interactive: bool,
        list_chats: bool,
        resume: Option<i64>,
        show_chat: Option<i64>,
        delete_chat: Option<i64>,
    },
    News {
        topic: String,
        timeframe: NewsTimeframe,
        limit: usize,
    },
    Completions {
        shell: clap_complete::Shell,
    },
    Upgrade {
        check: bool,
        force: bool,
        version: Option<String>,
    },
}

impl Cli {
    pub fn stream_mode<'a>(&self, header_title: &'a str) -> crate::llm::StreamMode<'a> {
        if self.stream && self.output.is_none() && !self.json {
            crate::llm::StreamMode::Stdout { header_title }
        } else {
            crate::llm::StreamMode::Buffered
        }
    }

    pub fn resolve_action(&self) -> Result<ResolvedAction<'_>, &'static str> {
        if let Some(cmd) = &self.command {
            match cmd {
                Commands::Web { url } => Ok(ResolvedAction::Web { url: url.trim() }),
                Commands::Book { title } => Ok(ResolvedAction::Book {
                    title: title.trim(),
                }),
                Commands::File { path } => {
                    if path.as_os_str() == "-" {
                        Ok(ResolvedAction::Stdin)
                    } else {
                        Ok(ResolvedAction::File { path: path.clone() })
                    }
                }
                Commands::Cache { action } => Ok(ResolvedAction::Cache(action.clone())),
                Commands::Config { action } => Ok(ResolvedAction::Config(*action)),
                Commands::History { limit } => Ok(ResolvedAction::History { limit: *limit }),
                Commands::Stats => Ok(ResolvedAction::Stats),
                Commands::Find { query } => Ok(ResolvedAction::Find {
                    query: query.trim(),
                }),
                Commands::Models => Ok(ResolvedAction::Models),
                Commands::Doctor => Ok(ResolvedAction::Doctor),
                Commands::Index => Ok(ResolvedAction::Index),
                Commands::Browse => Ok(ResolvedAction::Browse),
                Commands::Ask {
                    first,
                    second,
                    interactive,
                    chats,
                    resume,
                    show_chat,
                    delete_chat,
                } => {
                    let (target, question) = match (first.as_deref(), second.as_deref()) {
                        (Some(f), Some(s)) => {
                            (Some(f.trim().to_string()), Some(s.trim().to_string()))
                        }
                        (Some(f), None) => {
                            if is_probable_question(f) {
                                (None, Some(f.trim().to_string()))
                            } else {
                                (Some(f.trim().to_string()), None)
                            }
                        }
                        (None, None) => (None, None),
                        (None, Some(s)) => (None, Some(s.trim().to_string())),
                    };
                    Ok(ResolvedAction::Ask {
                        target,
                        question,
                        interactive: *interactive || resume.is_some(),
                        list_chats: *chats,
                        resume: *resume,
                        show_chat: *show_chat,
                        delete_chat: *delete_chat,
                    })
                }
                Commands::News {
                    topic,
                    timeframe,
                    limit,
                } => Ok(ResolvedAction::News {
                    topic: topic.trim().to_string(),
                    timeframe: *timeframe,
                    limit: *limit,
                }),
                Commands::Completions { shell } => {
                    Ok(ResolvedAction::Completions { shell: *shell })
                }
                Commands::Upgrade {
                    check,
                    force,
                    version,
                } => Ok(ResolvedAction::Upgrade {
                    check: *check,
                    force: *force,
                    version: version.clone(),
                }),
            }
        } else if let Some(target) = &self.target {
            let trimmed = target.trim();
            if trimmed == "-" {
                Ok(ResolvedAction::Stdin)
            } else if trimmed.starts_with("http://") || trimmed.starts_with("https://") {
                Ok(ResolvedAction::Web { url: trimmed })
            } else {
                let candidate_path = std::path::Path::new(trimmed);
                if candidate_path.is_file() || is_file_like(trimmed) {
                    Ok(ResolvedAction::File {
                        path: candidate_path.to_path_buf(),
                    })
                } else if is_news_query(trimmed) {
                    let clean_topic = clean_news_query(trimmed);
                    Ok(ResolvedAction::News {
                        topic: clean_topic,
                        timeframe: NewsTimeframe::Day,
                        limit: 5,
                    })
                } else {
                    Ok(ResolvedAction::Book { title: trimmed })
                }
            }
        } else {
            Err(
                "Please provide a URL, local file, book title, or '-' for stdin. Run `srz --help` for details.",
            )
        }
    }

    pub fn apply_config(
        &mut self,
        config: &crate::config::Config,
        matches: Option<&clap::ArgMatches>,
    ) {
        let profile = self.profile.as_deref().and_then(|p| config.profiles.get(p));

        let was_provided = |id: &str| -> bool {
            if let Some(m) = matches {
                if !m.ids().any(|arg_id| arg_id.as_str() == id) {
                    return false;
                }
                m.value_source(id) == Some(clap::parser::ValueSource::CommandLine)
                    || m.value_source(id) == Some(clap::parser::ValueSource::EnvVariable)
            } else {
                false
            }
        };

        macro_rules! apply_field {
            ($field:ident) => {
                if !was_provided(stringify!($field))
                    && let Some(val) = profile
                        .and_then(|p| p.$field.clone())
                        .or_else(|| config.$field.clone())
                {
                    self.$field = val;
                }
            };
        }

        apply_field!(base_url);
        apply_field!(model);
        apply_field!(api_key);
        apply_field!(style);
        apply_field!(max_tokens);
        apply_field!(no_cache);
        apply_field!(quiet);
        apply_field!(verbose);
        apply_field!(open);
        apply_field!(concurrency);
        apply_field!(stream);
    }
}

pub fn is_file_like(s: &str) -> bool {
    if s.starts_with("http://") || s.starts_with("https://") {
        return false;
    }
    let path = std::path::Path::new(s);
    if path.is_file() {
        return true;
    }
    if s.starts_with("./")
        || s.starts_with("../")
        || s.starts_with('/')
        || s.starts_with('~')
        || s.contains('/')
        || s.contains('\\')
    {
        return true;
    }
    if let Some(ext) = path.extension().and_then(|e| e.to_str()) {
        let lower = ext.to_ascii_lowercase();
        matches!(
            lower.as_str(),
            "pdf" | "md" | "markdown" | "txt" | "text" | "html" | "htm" | "epub" | "json"
        )
    } else {
        false
    }
}

pub fn is_news_query(s: &str) -> bool {
    let mut words = s.split_whitespace();
    let Some(first) = words.next() else {
        return false;
    };
    if first.eq_ignore_ascii_case("news") || first.eq_ignore_ascii_case("headlines") {
        return true;
    }
    let first_is_current_or_latest =
        first.eq_ignore_ascii_case("current") || first.eq_ignore_ascii_case("latest");
    let mut has_update_or_story = false;
    let mut has_more = false;

    for word in words {
        has_more = true;
        if word.eq_ignore_ascii_case("news") || word.eq_ignore_ascii_case("headlines") {
            return true;
        }
        if first_is_current_or_latest
            && (word.eq_ignore_ascii_case("updates") || word.eq_ignore_ascii_case("stories"))
        {
            has_update_or_story = true;
        }
    }

    first_is_current_or_latest && has_more && has_update_or_story
}

pub fn clean_news_query(s: &str) -> String {
    const STOP_WORDS: &[&str] = &[
        "current",
        "latest",
        "today's",
        "todays",
        "recent",
        "news",
        "headline",
        "headlines",
        "about",
        "on",
        "regarding",
        "for",
    ];

    let mut out = String::new();
    for word in s.split_whitespace() {
        let is_stop = STOP_WORDS
            .iter()
            .any(|stop| word.eq_ignore_ascii_case(stop));
        if !is_stop {
            if !out.is_empty() {
                out.push(' ');
            }
            out.push_str(word);
        }
    }

    if out.is_empty() {
        "top stories".to_string()
    } else {
        out
    }
}

pub fn is_probable_question(s: &str) -> bool {
    let trimmed = s.trim();
    if trimmed.ends_with('?') {
        return true;
    }
    let Some(first_word) = trimmed.split_whitespace().next() else {
        return false;
    };
    if trimmed.len() <= first_word.len() {
        return false;
    }
    const QUESTION_PREFIXES: &[&str] = &[
        "what",
        "why",
        "how",
        "who",
        "when",
        "where",
        "which",
        "can",
        "could",
        "should",
        "would",
        "is",
        "are",
        "explain",
        "summarize",
        "tell",
        "describe",
        "clarify",
    ];
    QUESTION_PREFIXES
        .iter()
        .any(|prefix| first_word.eq_ignore_ascii_case(prefix))
}