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
use anyhow::{anyhow, Context};
use sal_e_api::{GenParams, ImageParams, Response};
use teloxide::{
    dispatching::UpdateHandler,
    dptree::case,
    macros::BotCommands,
    payloads::setters::*,
    prelude::*,
    types::{
        ChatAction, InlineKeyboardButton, InlineKeyboardMarkup, InputFile, InputMedia,
        InputMediaPhoto, Me, MessageId, PhotoSize,
    },
    utils::command::BotCommands as _,
};
use tracing::{info, instrument, warn};

use crate::{
    bot::{helpers, State},
    BotState,
};

use super::{
    filter_command, filter_map_bot_state, filter_map_settings, ConfigParameters, DiffusionDialogue,
};

/// BotCommands for generating images.
#[derive(BotCommands, Debug, Clone)]
#[command(rename_rule = "lowercase", description = "Image generation commands")]
pub(crate) enum GenCommands {
    /// Command to generate an image
    #[command(description = "generate an image")]
    Gen(String),
    /// Alias for `gen`. Hidden from help to avoid confusion.
    #[command(description = "off")]
    G(String),
    /// Alias for `gen`. Hidden from help to avoid confusion.
    #[command(description = "off")]
    Generate(String),
}

enum Photo {
    Single(Vec<u8>),
    Album(Vec<Vec<u8>>),
}

impl Photo {
    #[allow(dead_code)]
    pub fn single(photo: Vec<u8>) -> anyhow::Result<Self> {
        Ok(Self::Single(photo))
    }

    pub fn album(photos: Vec<Vec<u8>>) -> anyhow::Result<Self> {
        if photos.len() == 1 {
            let images = photos
                .into_iter()
                .next()
                .ok_or_else(|| anyhow!("Failed to get image"))?;
            Ok(Photo::Single(images))
        } else {
            Ok(Photo::Album(photos))
        }
    }
}

struct Reply {
    caption: String,
    images: Photo,
    source: MessageId,
    seed: i64,
}

impl Reply {
    pub fn new(
        caption: String,
        images: Vec<Vec<u8>>,
        seed: i64,
        source: MessageId,
    ) -> anyhow::Result<Self> {
        let images = Photo::album(images)?;
        Ok(Self {
            caption,
            images,
            source,
            seed,
        })
    }

    pub async fn send(self, bot: &Bot, chat_id: ChatId) -> anyhow::Result<()> {
        match self.images {
            Photo::Single(image) => {
                bot.send_photo(chat_id, InputFile::memory(image))
                    .parse_mode(teloxide::types::ParseMode::MarkdownV2)
                    .caption(self.caption)
                    .reply_markup(keyboard(self.seed))
                    .reply_to_message_id(self.source)
                    .await?;
            }
            Photo::Album(images) => {
                let mut caption = Some(self.caption);
                let input_media = images.into_iter().map(|i| {
                    let mut media = InputMediaPhoto::new(InputFile::memory(i));
                    media.caption = caption.take();
                    media.parse_mode = Some(teloxide::types::ParseMode::MarkdownV2);
                    InputMedia::Photo(media)
                });

                bot.send_media_group(chat_id, input_media)
                    .reply_to_message_id(self.source)
                    .await?;
                bot.send_message(
                    chat_id,
                    "What would you like to do? Select below, or enter a new prompt.",
                )
                .reply_markup(keyboard(self.seed))
                .reply_to_message_id(self.source)
                .await?;
            }
        }

        Ok(())
    }
}

struct MessageText(String);

impl MessageText {
    pub fn new_with_image_params(prompt: &str, infotxt: &dyn ImageParams) -> Self {
        use teloxide::utils::markdown::escape;

        Self(format!(
            "`{}`\n\n{}",
            escape(prompt),
            [
                infotxt
                    .negative_prompt()
                    .as_ref()
                    .and_then(|s| (!s.trim().is_empty()).then(|| escape(s)))
                    .map(|s| format!("Negative prompt: `{s}`")),
                infotxt.steps().map(|s| format!("Steps: `{s}`")),
                infotxt
                    .sampler()
                    .as_ref()
                    .map(|s| format!("Sampler: `{s}`")),
                infotxt.cfg().map(|s| format!("CFG scale: `{s}`")),
                infotxt.seed().map(|s| format!("Seed: `{s}`")),
                infotxt
                    .width()
                    .and_then(|w| infotxt.height().map(|h| format!("Size: `{w}×{h}`"))),
                infotxt.model().as_ref().map(|s| format!("Model: `{s}`")),
                infotxt
                    .denoising()
                    .map(|s| format!("Denoising strength: `{s}`")),
            ]
            .into_iter()
            .flatten()
            .collect::<Vec<_>>()
            .join("\n")
        ))
    }
}

impl TryFrom<&dyn ImageParams> for MessageText {
    type Error = anyhow::Error;

    fn try_from(params: &dyn ImageParams) -> Result<Self, Self::Error> {
        let prompt = if let Some(prompt) = params.prompt() {
            prompt
        } else {
            return Err(anyhow!("No prompt in image info response"));
        };
        Ok(Self::new_with_image_params(prompt.as_str(), params))
    }
}

impl TryFrom<Response> for MessageText {
    type Error = anyhow::Error;

    fn try_from(response: Response) -> Result<Self, Self::Error> {
        Self::try_from(response.params.as_ref())
    }
}

async fn do_img2img(
    bot: &Bot,
    cfg: &ConfigParameters,
    img2img: &mut Box<dyn GenParams>,
    msg: &Message,
    photo: Vec<PhotoSize>,
    prompt: String,
) -> anyhow::Result<Response> {
    img2img.set_prompt(prompt);

    let photo = if let Some(photo) = photo
        .iter()
        .reduce(|a, p| if a.height > p.height { a } else { p })
    {
        photo
    } else {
        bot.send_message(msg.chat.id, "Something went wrong.")
            .await?;
        return Err(anyhow!("Photo vec was empty!"));
    };
    let file = bot.get_file(&photo.file.id).send().await?;

    let photo = helpers::get_file(bot, &file).await?;

    img2img.set_image(Some(photo.into()));

    let resp = cfg.img2img_api.img2img(img2img.as_ref()).await?;

    img2img.set_image(None);

    Ok(resp)
}

async fn handle_image(
    bot: Bot,
    cfg: ConfigParameters,
    dialogue: DiffusionDialogue,
    (txt2img, mut img2img): (Box<dyn GenParams>, Box<dyn GenParams>),
    msg: Message,
    photo: Vec<PhotoSize>,
    text: String,
) -> anyhow::Result<()> {
    if text.is_empty() {
        bot.send_message(msg.chat.id, "A prompt is required.")
            .reply_to_message_id(msg.id)
            .await?;
        return Ok(());
    }

    bot.send_chat_action(msg.chat.id, ChatAction::UploadPhoto)
        .await?;

    let resp = do_img2img(&bot, &cfg, &mut img2img, &msg, photo, text).await?;

    let seed = if resp.params.seed() == resp.gen_params.seed() {
        -1
    } else {
        resp.params.seed().unwrap_or(-1)
    };

    let caption = MessageText::try_from(resp.params.as_ref())
        .context("Failed to build caption from response")?;

    Reply::new(caption.0, resp.images, seed, msg.id)
        .context("Failed to create response!")?
        .send(&bot, msg.chat.id)
        .await?;

    dialogue
        .update(State::Ready {
            bot_state: BotState::default(),
            txt2img,
            img2img,
        })
        .await
        .map_err(|e| anyhow!(e))?;

    Ok(())
}

async fn do_txt2img(
    prompt: String,
    cfg: &ConfigParameters,
    txt2img: &mut dyn GenParams,
) -> anyhow::Result<Response> {
    txt2img.set_prompt(prompt);

    let resp = cfg.txt2img_api.txt2img(txt2img).await?;

    Ok(resp)
}

async fn handle_prompt(
    bot: Bot,
    cfg: ConfigParameters,
    dialogue: DiffusionDialogue,
    (mut txt2img, img2img): (Box<dyn GenParams>, Box<dyn GenParams>),
    msg: Message,
    text: String,
) -> anyhow::Result<()> {
    if text.is_empty() {
        bot.send_message(msg.chat.id, "A prompt is required.")
            .reply_to_message_id(msg.id)
            .await?;
        return Ok(());
    }

    bot.send_chat_action(msg.chat.id, ChatAction::UploadPhoto)
        .await?;

    let resp = do_txt2img(text, &cfg, txt2img.as_mut()).await?;

    let seed = if resp.params.seed() == resp.gen_params.seed() {
        -1
    } else {
        resp.params.seed().unwrap_or(-1)
    };

    let caption = MessageText::try_from(resp.params.as_ref())
        .context("Failed to build caption from response")?;

    Reply::new(caption.0, resp.images, seed, msg.id)
        .context("Failed to create response!")?
        .send(&bot, msg.chat.id)
        .await?;

    dialogue
        .update(State::Ready {
            bot_state: BotState::default(),
            txt2img,
            img2img,
        })
        .await
        .map_err(|e| anyhow!(e))?;

    Ok(())
}

fn keyboard(seed: i64) -> InlineKeyboardMarkup {
    let seed_button = if seed == -1 {
        InlineKeyboardButton::callback("🎲 Seed", "reuse/-1")
    } else {
        InlineKeyboardButton::callback("♻️ Seed", format!("reuse/{seed}"))
    };
    InlineKeyboardMarkup::new([[
        InlineKeyboardButton::callback("🔄 Rerun", "rerun"),
        seed_button,
        InlineKeyboardButton::callback("⚙️ Settings", "settings"),
    ]])
}

#[instrument(skip_all)]
async fn handle_rerun(
    me: Me,
    bot: Bot,
    cfg: ConfigParameters,
    dialogue: DiffusionDialogue,
    (txt2img, img2img): (Box<dyn GenParams>, Box<dyn GenParams>),
    q: CallbackQuery,
) -> anyhow::Result<()> {
    let message = if let Some(message) = q.message {
        message
    } else {
        bot.answer_callback_query(q.id)
            .cache_time(60)
            .text("Sorry, this message is no longer available.")
            .await?;
        return Ok(());
    };

    let id = message.id;
    let chat_id = message.chat.id;

    let parent = if let Some(parent) = message.reply_to_message().cloned() {
        parent
    } else {
        bot.answer_callback_query(q.id)
            .cache_time(60)
            .text("Oops, something went wrong.")
            .await?;
        return Ok(());
    };

    if let Some(photo) = parent.photo().map(ToOwned::to_owned) {
        if let Some(text) = message.caption().map(ToOwned::to_owned) {
            let bot_name = me.user.username.expect("Bots must have a username");
            let text = if let Ok(command) = GenCommands::parse(&text, &bot_name) {
                match command {
                    GenCommands::Gen(s) | GenCommands::G(s) | GenCommands::Generate(s) => s,
                }
            } else {
                text
            };

            if let Err(e) = bot
                .answer_callback_query(q.id)
                .cache_time(60)
                .text("Rerunning this image...")
                .await
            {
                warn!("Failed to answer image rerun callback query: {}", e)
            }
            handle_image(
                bot.clone(),
                cfg,
                dialogue,
                (txt2img, img2img),
                parent,
                photo,
                text,
            )
            .await?;
        } else {
            bot.send_message(message.chat.id, "A prompt is required to run img2img.")
                .await?;
            return Err(anyhow!("No prompt provided for img2img"));
        }
    } else if let Some(text) = parent.text().map(ToOwned::to_owned) {
        if let Err(e) = bot
            .answer_callback_query(q.id)
            .cache_time(60)
            .text("Rerunning this prompt...")
            .await
        {
            warn!("Failed to answer prompt rerun callback query: {}", e)
        }
        let bot_name = me.user.username.expect("Bots must have a username");
        let text = if let Ok(command) = GenCommands::parse(&text, &bot_name) {
            match command {
                GenCommands::Gen(s) | GenCommands::G(s) | GenCommands::Generate(s) => s,
            }
        } else {
            text
        };
        handle_prompt(bot.clone(), cfg, dialogue, (txt2img, img2img), parent, text).await?;
    } else {
        bot.answer_callback_query(q.id)
            .cache_time(60)
            .text("Oops, something went wrong.")
            .await?;
        return Ok(());
    }

    bot.edit_message_reply_markup(chat_id, id)
        .reply_markup(InlineKeyboardMarkup::new([[]]))
        .send()
        .await?;

    Ok(())
}

async fn handle_reuse(
    bot: Bot,
    dialogue: DiffusionDialogue,
    (mut txt2img, mut img2img): (Box<dyn GenParams>, Box<dyn GenParams>),
    q: CallbackQuery,
    seed: i64,
) -> anyhow::Result<()> {
    let message = if let Some(message) = q.message {
        message
    } else {
        bot.answer_callback_query(q.id)
            .cache_time(60)
            .text("Sorry, this message is no longer available.")
            .await?;
        return Ok(());
    };

    let id = message.id;
    let chat_id = message.chat.id;

    let parent = if let Some(parent) = message.reply_to_message().cloned() {
        parent
    } else {
        bot.answer_callback_query(q.id)
            .cache_time(60)
            .text("Oops, something went wrong.")
            .await?;
        return Ok(());
    };

    if parent.photo().is_some() {
        img2img.set_seed(seed);
        dialogue
            .update(State::Ready {
                bot_state: BotState::default(),
                txt2img,
                img2img,
            })
            .await
            .map_err(|e| anyhow!(e))?;
    } else if parent.text().is_some() {
        txt2img.set_seed(seed);
        dialogue
            .update(State::Ready {
                bot_state: BotState::default(),
                txt2img,
                img2img,
            })
            .await
            .map_err(|e| anyhow!(e))?;
    } else {
        bot.answer_callback_query(q.id)
            .cache_time(60)
            .text("Oops, something went wrong.")
            .await?;
        return Ok(());
    }
    if seed == -1 {
        if let Err(e) = bot
            .answer_callback_query(q.id)
            .text("Seed randomized.")
            .await
        {
            warn!("Failed to answer randomize seed callback query: {}", e)
        }
    } else {
        if let Err(e) = bot
            .answer_callback_query(q.id)
            .text(format!("Seed set to {seed}."))
            .await
        {
            warn!("Failed to answer set seed callback query: {}", e)
        }
        bot.edit_message_reply_markup(chat_id, id)
            .reply_markup(keyboard(-1))
            .send()
            .await?;
    }

    Ok(())
}

pub(crate) fn image_schema() -> UpdateHandler<anyhow::Error> {
    let gen_command_handler = Update::filter_message()
        .chain(filter_command::<GenCommands>())
        .chain(dptree::filter_map(|g: GenCommands| match g {
            GenCommands::Gen(s) | GenCommands::G(s) | GenCommands::Generate(s) => Some(s),
        }))
        .branch(Message::filter_photo().endpoint(handle_image))
        .branch(dptree::endpoint(handle_prompt));

    let message_handler = Update::filter_message()
        .branch(
            dptree::filter(|msg: Message| {
                msg.text().map(|t| t.starts_with('/')).unwrap_or_default()
            })
            .endpoint(|msg: Message| async move {
                info!(
                    "Ignoring unknown command: {}",
                    msg.text().unwrap_or_default()
                );
                Ok(())
            }),
        )
        .branch(
            Message::filter_photo()
                .map(|msg: Message| msg.caption().map(str::to_string).unwrap_or_default())
                .endpoint(handle_image),
        )
        .branch(Message::filter_text().endpoint(handle_prompt));

    let callback_handler = Update::filter_callback_query()
        .branch(
            dptree::filter_map(|q: CallbackQuery| {
                q.data
                    .filter(|d| d.starts_with("reuse"))
                    .and_then(|d| d.split('/').skip(1).flat_map(str::parse::<i64>).next())
            })
            .endpoint(handle_reuse),
        )
        .branch(
            dptree::filter(|q: CallbackQuery| q.data.filter(|d| d.starts_with("rerun")).is_some())
                .endpoint(handle_rerun),
        );

    dptree::entry()
        .chain(filter_map_bot_state())
        .chain(case![BotState::Generate])
        .chain(filter_map_settings())
        .branch(gen_command_handler)
        .branch(message_handler)
        .branch(callback_handler)
}