llmman 0.1.689

Run any agent on any model, models stored as OCI images
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
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
//! `llmman run` for media generation models, after ollama's (since removed)
//! `x/imagegen/cli.go`: one prompt in, one file out (png, or mp4 / wav
//! with `--video` / `--audio`), saved next to the shell and shown inline
//! where the terminal can. Progress comes as server-sent events.

use std::io::{self, BufRead, IsTerminal, Write};
use std::path::{Path, PathBuf};
use std::time::Duration;

use anyhow::{Context, Result};
use base64::Engine as _;
use indicatif::{ProgressBar, ProgressStyle};

/// What to generate for each prompt.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Media {
    #[default]
    Image,
    Video,
    Audio,
}

/// Per-request knobs, `ollama run`'s hidden `--width/--height/--steps/
/// --seed/--negative` flags. Zero / empty means "model default".
#[derive(Debug, Clone, Default)]
pub struct ImageOptions {
    pub media: Media,
    pub width: u32,
    pub height: u32,
    pub steps: u32,
    pub seed: Option<u32>,
    /// Guidance scale; the negative prompt only counts above 1.
    pub cfg_scale: f32,
    pub negative: String,
    /// `--video` / `--audio` clip length; 0 = server default.
    pub seconds: f32,
    /// `--fps`; 0 = server default.
    pub fps: f32,
    pub flow_shift: Option<f32>,
    /// `--image`: conditioning image for image-to-video / an action run.
    pub image: Option<PathBuf>,
    /// `--input-video`: a video-to-video or action run's conditioning clip.
    pub input_video: Option<PathBuf>,
    /// `--condition-frames`: comma-separated latent frame indexes.
    pub condition_frames: String,
    pub condition_keep_last: bool,
    pub action: Option<ActionOptions>,
}

/// `--action-*`: a Cosmos3 action run.
#[derive(Debug, Clone, Default)]
pub struct ActionOptions {
    pub mode: String,
    pub domain: String,
    pub actions: Option<PathBuf>,
    pub chunk: u32,
    pub tier: u32,
    pub view: String,
}

impl ImageOptions {
    fn request(&self, model: &str, prompt: &str) -> serde_json::Value {
        let mut req = serde_json::json!({
            "model": model,
            "prompt": prompt,
            "stream": true,
            "response_format": "b64_json",
        });
        if self.width > 0 {
            req["width"] = self.width.into();
        }
        if self.height > 0 {
            req["height"] = self.height.into();
        }
        if self.steps > 0 {
            req["steps"] = self.steps.into();
        }
        if let Some(seed) = self.seed {
            req["seed"] = seed.into();
        }
        self.guidance(&mut req);
        req
    }

    fn guidance(&self, req: &mut serde_json::Value) {
        if self.cfg_scale > 0.0 {
            req["cfg_scale"] = self.cfg_scale.into();
        }
        if !self.negative.is_empty() {
            req["negative_prompt"] = serde_json::Value::String(self.negative.clone());
        }
    }

    /// The `/v1/videos` request: no streaming, the mp4 is fetched from
    /// the job's `content_url` afterwards.
    fn video_request(&self, model: &str, prompt: &str) -> Result<serde_json::Value> {
        let mut req = self.request(model, prompt);
        req["stream"] = false.into();
        req.as_object_mut().map(|o| o.remove("response_format"));
        if self.seconds > 0.0 {
            req["seconds"] = self.seconds.into();
        }
        if self.fps > 0.0 {
            req["fps"] = self.fps.into();
        }
        if let Some(f) = self.flow_shift {
            req["flow_shift"] = f.into();
        }
        let b64 = |path: &PathBuf| -> Result<String> {
            let bytes = std::fs::read(path).with_context(|| format!("read {}", path.display()))?;
            Ok(base64::engine::general_purpose::STANDARD.encode(bytes))
        };
        if let Some(p) = &self.image {
            req["image"] = b64(p)?.into();
        }
        if let Some(p) = &self.input_video {
            req["video"] = b64(p)?.into();
            if !self.condition_frames.is_empty() {
                let idx: Vec<i64> = self
                    .condition_frames
                    .split(',')
                    .map(|v| v.trim().parse::<i64>())
                    .collect::<Result<_, _>>()
                    .context("--condition-frames takes comma-separated integers")?;
                req["condition_frames"] = idx.into();
            }
            if self.condition_keep_last {
                req["condition_video_keep"] = "last".into();
            }
        }
        if let Some(a) = &self.action {
            let mut act = serde_json::json!({
                "mode": a.mode, "domain": a.domain, "resolution_tier": a.tier, "view_point": a.view,
            });
            if a.chunk > 0 {
                act["chunk_size"] = a.chunk.into();
            }
            if let Some(p) = &a.actions {
                let text =
                    std::fs::read_to_string(p).with_context(|| format!("read {}", p.display()))?;
                act["actions"] = serde_json::from_str(&text)
                    .with_context(|| format!("parse {}", p.display()))?;
            }
            req["action"] = act;
        }
        Ok(req)
    }

    /// The `/v1/audio/speech` request: OpenAI's field is `input`.
    fn audio_request(&self, model: &str, prompt: &str) -> serde_json::Value {
        let mut req = serde_json::json!({
            "model": model,
            "input": prompt,
            "response_format": "wav",
        });
        if self.steps > 0 {
            req["steps"] = self.steps.into();
        }
        if let Some(seed) = self.seed {
            req["seed"] = seed.into();
        }
        self.guidance(&mut req);
        if self.seconds > 0.0 {
            req["seconds"] = self.seconds.into();
        }
        req
    }
}

/// `llmman run <image model> ["prompt"]`: one-shot when a prompt was
/// given (or piped), otherwise ollama's `>>> ` loop where every line is
/// a prompt and `/set` adjusts the options.
pub fn run(model: &str, prompt: &str, interactive: bool, mut opts: ImageOptions) -> Result<()> {
    if !interactive {
        let p = if prompt.is_empty() {
            let mut s = String::new();
            io::stdin().read_line(&mut s)?;
            s.trim().to_string()
        } else {
            prompt.to_string()
        };
        if p.is_empty() {
            return Ok(());
        }
        return generate(model, &p, &opts).map(|_| ());
    }

    eprintln!(
        "Send a prompt to generate {} (/? for help)",
        match opts.media {
            Media::Image => "an image",
            Media::Video => "a video",
            Media::Audio => "audio",
        }
    );
    let stdin = io::stdin();
    loop {
        eprint!(">>> ");
        let mut line = String::new();
        if stdin.lock().read_line(&mut line)? == 0 {
            eprintln!();
            return Ok(());
        }
        let line = line.trim();
        if line.is_empty() {
            continue;
        }
        if let Some(cmd) = line.strip_prefix('/') {
            match handle_command(cmd, &mut opts) {
                Command::Continue => continue,
                Command::Quit => return Ok(()),
            }
        }
        if let Err(e) = generate(model, line, &opts) {
            eprintln!("Error: {e:#}");
        }
    }
}

enum Command {
    Continue,
    Quit,
}

/// ollama's image REPL slash commands.
fn handle_command(cmd: &str, opts: &mut ImageOptions) -> Command {
    let mut words = cmd.split_whitespace();
    match words.next().unwrap_or("") {
        "bye" | "exit" | "quit" => return Command::Quit,
        "?" | "help" => {
            eprintln!("Available Commands:");
            eprintln!("  /set width <n>     Image width in pixels");
            eprintln!("  /set height <n>    Image height in pixels");
            eprintln!("  /set steps <n>     Denoising steps");
            eprintln!("  /set seed <n>      Random seed");
            eprintln!("  /set cfg <n>       Guidance scale (>1 enables the negative prompt)");
            eprintln!("  /set negative <t>  Negative prompt");
            eprintln!("  /set seconds <n>   Video / audio length");
            eprintln!("  /set media <m>     image, video or audio");
            eprintln!("  /show              Show current settings");
            eprintln!("  /bye               Exit");
        }
        "show" => {
            eprintln!(
                "width={} height={} steps={} seed={} cfg={} negative={:?}",
                opts.width,
                opts.height,
                opts.steps,
                opts.seed.map_or("random".to_string(), |s| s.to_string()),
                opts.cfg_scale,
                opts.negative
            );
        }
        "set" => {
            let key = words.next().unwrap_or("");
            let rest: Vec<&str> = words.collect();
            let value = rest.join(" ");
            let num = value.parse::<u32>();
            match (key, num) {
                ("width", Ok(v)) => opts.width = v,
                ("height", Ok(v)) => opts.height = v,
                ("steps", Ok(v)) => opts.steps = v,
                ("seed", Ok(v)) => opts.seed = Some(v),
                ("negative", _) => opts.negative = value,
                ("seconds", _) | ("cfg", _) => match value.parse::<f32>() {
                    Ok(v) if key == "cfg" => opts.cfg_scale = v,
                    Ok(v) => opts.seconds = v,
                    Err(_) => eprintln!("Invalid value for /set {key}: {value}"),
                },
                ("media", _) => {
                    opts.media = match value.as_str() {
                        "image" => Media::Image,
                        "video" => Media::Video,
                        "audio" => Media::Audio,
                        other => {
                            eprintln!("Unknown media type: {other} (image, video or audio)");
                            opts.media
                        }
                    }
                }
                (k, _) => eprintln!("Unknown or invalid setting: /set {k} {value}"),
            }
        }
        other => eprintln!("Unknown command: /{other}. Type /? for help"),
    }
    Command::Continue
}

/// One generation: an image (streamed, with a step bar), or a video / an
/// audio clip (one blocking request behind a spinner). Returns the saved
/// file's path.
pub fn generate(model: &str, prompt: &str, opts: &ImageOptions) -> Result<PathBuf> {
    match opts.media {
        Media::Image => generate_image(model, prompt, opts),
        Media::Video => generate_video(model, prompt, opts),
        Media::Audio => generate_audio(model, prompt, opts),
    }
}

fn client() -> Result<reqwest::blocking::Client> {
    crate::daemon::client_builder()?
        .timeout(None)
        .build()
        .context("build http client")
}

fn spinner(msg: &str) -> Option<ProgressBar> {
    io::stderr().is_terminal().then(|| {
        let pb = ProgressBar::new_spinner();
        pb.set_style(
            ProgressStyle::with_template("{spinner} {msg}")
                .unwrap_or_else(|_| ProgressStyle::default_spinner()),
        );
        pb.set_message(msg.to_string());
        pb.enable_steady_tick(Duration::from_millis(100));
        pb
    })
}

/// Clears a spinner on every exit path, like ollama's `defer p.StopAndClear()`.
struct Spinner(Option<ProgressBar>);

impl Drop for Spinner {
    fn drop(&mut self) {
        if let Some(pb) = self.0.take() {
            pb.finish_and_clear();
        }
    }
}

fn fail(resp: reqwest::blocking::Response) -> anyhow::Error {
    let body = resp.text().unwrap_or_default();
    anyhow::anyhow!("{}", crate::daemon::api_error(&body).unwrap_or(body))
}

/// `/v1/videos` then `GET <content_url>` for the mp4 (llama-server muxes
/// it with ffmpeg; without ffmpeg the job carries frames only, which is
/// reported as an error here rather than written as hundreds of PNGs).
fn generate_video(model: &str, prompt: &str, opts: &ImageOptions) -> Result<PathBuf> {
    let client = client()?;
    let _pb = Spinner(spinner("Generating video..."));
    let resp = client
        .post(format!("{}/v1/videos", crate::daemon::server()))
        .json(&opts.video_request(model, prompt)?)
        .send()
        .context("request /v1/videos")?;
    if !resp.status().is_success() {
        return Err(fail(resp));
    }
    let job: serde_json::Value = resp.json().context("decode /v1/videos response")?;
    if let Some(actions) = job.get("actions").filter(|a| a.is_array()) {
        // an action run's predictions, next to the video
        let path = output_path(prompt, "json");
        std::fs::write(&path, serde_json::to_string_pretty(actions)?)
            .with_context(|| format!("write {}", path.display()))?;
        println!("Actions saved to: {}", path.display());
    }
    let Some(content_url) = job["content_url"].as_str() else {
        anyhow::bail!(
            "the server generated {} frames but has no ffmpeg to mux an mp4; install ffmpeg next to llama-server",
            job["n_frames"].as_u64().unwrap_or(0)
        );
    };
    let mp4 = client
        .get(format!("{}{}", crate::daemon::server(), content_url))
        .send()
        .context("fetch video content")?;
    if !mp4.status().is_success() {
        return Err(fail(mp4));
    }
    let bytes = mp4.bytes().context("read video content")?;
    drop(_pb);
    let path = output_path(prompt, "mp4");
    std::fs::write(&path, &bytes).with_context(|| format!("write {}", path.display()))?;
    println!("Video saved to: {}", path.display());
    Ok(path)
}

/// `/v1/audio/speech`, saved as a wav.
fn generate_audio(model: &str, prompt: &str, opts: &ImageOptions) -> Result<PathBuf> {
    let client = client()?;
    let _pb = Spinner(spinner("Generating audio..."));
    let resp = client
        .post(format!("{}/v1/audio/speech", crate::daemon::server()))
        .json(&opts.audio_request(model, prompt))
        .send()
        .context("request /v1/audio/speech")?;
    if !resp.status().is_success() {
        return Err(fail(resp));
    }
    let bytes = resp.bytes().context("read audio")?;
    drop(_pb);
    let path = output_path(prompt, "wav");
    std::fs::write(&path, &bytes).with_context(|| format!("write {}", path.display()))?;
    println!("Audio saved to: {}", path.display());
    Ok(path)
}

/// One streamed `/v1/images/generations` request: spinner, then a step
/// bar once the first progress event arrives, then the saved PNG's path.
fn generate_image(model: &str, prompt: &str, opts: &ImageOptions) -> Result<PathBuf> {
    let client = client()?;
    let resp = client
        .post(format!("{}/v1/images/generations", crate::daemon::server()))
        .json(&opts.request(model, prompt))
        .send()
        .context("request /v1/images/generations")?;
    if !resp.status().is_success() {
        let body = resp.text().unwrap_or_default();
        anyhow::bail!("{}", crate::daemon::api_error(&body).unwrap_or(body));
    }

    let mut bar: Option<ProgressBar> = spinner("Loading model...");

    let mut png: Option<Vec<u8>> = None;
    let reader = io::BufReader::new(resp);
    for line in reader.lines() {
        let line = line.context("read image generation stream")?;
        let Some(data) = line.strip_prefix("data: ") else {
            continue;
        };
        let ev: serde_json::Value = match serde_json::from_str(data) {
            Ok(v) => v,
            Err(_) => continue,
        };
        match ev["type"].as_str().unwrap_or("") {
            "image_generation.progress" => {
                let step = ev["step"].as_u64().unwrap_or(0);
                let total = ev["total"].as_u64().unwrap_or(0);
                if let Some(pb) = &mut bar {
                    if pb.length().is_none() && total > 0 {
                        pb.finish_and_clear();
                        let steps = ProgressBar::new(total);
                        steps.set_style(
                            ProgressStyle::with_template("Generating {bar:30} {pos}/{len}")
                                .unwrap_or_else(|_| ProgressStyle::default_bar()),
                        );
                        *pb = steps;
                    }
                    pb.set_position(step);
                }
            }
            "image_generation.completed" => {
                let b64 = ev["b64_json"]
                    .as_str()
                    .filter(|s| !s.is_empty())
                    .context("completed event carried no image")?;
                png = Some(
                    base64::engine::general_purpose::STANDARD
                        .decode(b64)
                        .context("decode image")?,
                );
            }
            "error" => {
                if let Some(pb) = bar.take() {
                    pb.finish_and_clear();
                }
                let msg = ev["error"]["message"]
                    .as_str()
                    .unwrap_or("image generation failed");
                anyhow::bail!("{msg}");
            }
            _ => {}
        }
    }
    if let Some(pb) = bar.take() {
        pb.finish_and_clear();
    }
    let png = png.ok_or_else(|| anyhow::anyhow!("stream ended without an image"))?;

    let path = output_path(prompt, "png");
    std::fs::write(&path, &png).with_context(|| format!("write {}", path.display()))?;
    if io::stdout().is_terminal() {
        display_inline(&png);
    }
    println!("Image saved to: {}", path.display());
    Ok(path)
}

/// `<prompt slug>-<YYYYMMDD-HHMMSS>.<ext>` in the working directory, as
/// ollama named its images.
fn output_path(prompt: &str, ext: &str) -> PathBuf {
    let mut slug: String = prompt
        .chars()
        .map(|c| {
            if c.is_ascii_alphanumeric() {
                c.to_ascii_lowercase()
            } else {
                '-'
            }
        })
        .collect();
    while slug.contains("--") {
        slug = slug.replace("--", "-");
    }
    let slug = slug.trim_matches('-');
    let slug = if slug.is_empty() {
        "image"
    } else {
        &slug[..slug.len().min(50)]
    };
    let stamp = chrono::Local::now().format("%Y%m%d-%H%M%S");
    Path::new(&format!("{slug}-{stamp}.{ext}")).to_path_buf()
}

/// Shows the PNG in terminals that can: iTerm2 / WezTerm (OSC 1337
/// inline images) and kitty / Ghostty (kitty graphics protocol). A no-op
/// elsewhere — the file path is printed regardless.
fn display_inline(png: &[u8]) {
    let term_program = std::env::var("TERM_PROGRAM").unwrap_or_default();
    let term = std::env::var("TERM").unwrap_or_default();
    let b64 = base64::engine::general_purpose::STANDARD.encode(png);
    let mut out = io::stdout().lock();
    if term_program == "iTerm.app"
        || term_program == "WezTerm"
        || std::env::var("LC_TERMINAL").as_deref() == Ok("iTerm2")
    {
        let _ = writeln!(
            out,
            "\x1b]1337;File=inline=1;size={}:{}\x07",
            png.len(),
            b64
        );
    } else if term.contains("kitty")
        || term_program == "ghostty"
        || std::env::var("KITTY_WINDOW_ID").is_ok()
    {
        // kitty graphics protocol: PNG payload in 4096-byte base64 chunks
        let bytes = b64.as_bytes();
        let mut first = true;
        let mut i = 0;
        while i < bytes.len() {
            let end = (i + 4096).min(bytes.len());
            let more = if end < bytes.len() { 1 } else { 0 };
            let chunk = std::str::from_utf8(&bytes[i..end]).unwrap_or("");
            if first {
                let _ = write!(out, "\x1b_Ga=T,f=100,m={more};{chunk}\x1b\\");
                first = false;
            } else {
                let _ = write!(out, "\x1b_Gm={more};{chunk}\x1b\\");
            }
            i = end;
        }
        let _ = writeln!(out);
    }
    let _ = out.flush();
}

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

    #[test]
    fn request_includes_only_set_options() {
        let opts = ImageOptions {
            width: 512,
            seed: Some(7),
            ..Default::default()
        };
        let req = opts.request("m", "a cat");
        assert_eq!(req["width"], 512);
        assert!(req.get("height").is_none());
        assert_eq!(req["seed"], 7);
        assert_eq!(req["stream"], true);
        assert!(req.get("steps").is_none());
        assert!(req.get("negative_prompt").is_none());
    }

    #[test]
    fn output_path_slugifies_prompt() {
        let p = output_path("Draw a cat!", "png").display().to_string();
        assert!(p.starts_with("draw-a-cat-"), "{p}");
        assert!(p.ends_with(".png"));
        assert!(output_path("x", "mp4")
            .display()
            .to_string()
            .ends_with(".mp4"));
    }

    #[test]
    fn video_and_audio_requests_use_their_own_fields() {
        let opts = ImageOptions {
            media: Media::Video,
            seconds: 2.0,
            seed: Some(1),
            ..Default::default()
        };
        let v = opts.video_request("m", "waves").unwrap();
        assert_eq!(v["stream"], false);
        assert_eq!(v["seconds"], 2.0);
        assert!(v.get("response_format").is_none());
        let a = opts.audio_request("m", "rain");
        assert_eq!(a["input"], "rain");
        assert_eq!(a["response_format"], "wav");
        assert_eq!(a["seed"], 1);
    }

    #[test]
    fn set_commands_update_options() {
        let mut opts = ImageOptions::default();
        assert!(matches!(
            handle_command("set width 768", &mut opts),
            Command::Continue
        ));
        assert!(matches!(
            handle_command("set negative blurry, dark", &mut opts),
            Command::Continue
        ));
        assert_eq!(opts.width, 768);
        assert_eq!(opts.negative, "blurry, dark");
        assert!(matches!(handle_command("bye", &mut opts), Command::Quit));
    }
}