opencrabs 0.3.82

The autonomous, self-improving AI agent. Single Rust binary. Every channel. Recommended: the 40MB prebuilt binary for macOS, Linux and Windows: https://github.com/adolfousier/opencrabs/releases
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
//! Channel-command ladder for the Telegram handler (#1086 seam 2).
//!
//! Extracted verbatim from `handle_message`; owns /help, /usage, /models and
//! the slash-command resolution that rewrites `text` before the agent turn.

use std::sync::Arc;

use teloxide::Bot;
use teloxide::prelude::*;
use teloxide::types::{InlineKeyboardButton, InlineKeyboardMarkup, Message, ParseMode, ThreadId};
use uuid::Uuid;

use super::intermediates::{send_html_or_plain, send_retrying_rate_limit};
use super::keyboards::{build_cd_keyboard, build_profiles_keyboard};
use super::markdown::{command_md_to_html, split_message};
use super::send::message_in_thread;
use super::session_resolve;
use super::state::TelegramState;
use crate::brain::AgentService;
use crate::services::SessionService;

/// Result of the command ladder: fully handled (caller returns) or fall
/// through to the agent with the (possibly rewritten) bindings carried out.
#[derive(Debug)]
pub(crate) enum CommandOutcome {
    Handled,
    Continue {
        text: String,
        command_invocation: Option<String>,
        pre_rewrite_user_text: String,
    },
}

#[allow(clippy::too_many_arguments)]
pub(crate) async fn handle_channel_command(
    bot: &Bot,
    msg: &Message,
    agent: &Arc<AgentService>,
    session_svc: &SessionService,
    telegram_state: &Arc<TelegramState>,
    text: String,
    thread_id: Option<ThreadId>,
    is_dm: bool,
    session_id: Uuid,
    chat_id: i64,
    topic_id: Option<i32>,
    user_id: i64,
    is_owner: bool,
    user: &teloxide::types::User,
    chat_title: &str,
    chat_id_str: &str,
    topic_name: &Option<String>,
    is_voice: bool,
    shared_session: &Arc<tokio::sync::Mutex<Option<Uuid>>>,
) -> Result<CommandOutcome, teloxide::RequestError> {
    // ── Channel commands (/help, /usage, /models) ──────────────────────────
    // Soft-nudge analyzes the user's own words, so capture them before a
    // slash command resolves to a skill/user-command body and rewrites `text`.
    let pre_rewrite_user_text = text.clone();
    let mut text = text;
    // When a slash command resolves to a prompt (a skill or user command),
    // remember the raw invocation (e.g. "/drop_release"). A slash command is a
    // deliberate NEW directive, so if it lands mid-turn it must be injected as
    // its own instruction — not with the "factor into the CURRENT task, do not
    // restart" wrapper a plain follow-up gets, which would neutralize it (#503
    // follow-up: /drop_release arrived mid-turn, got queued, but the wrapper
    // told the model to fold it into unrelated work so the release never ran).
    let mut command_invocation: Option<String> = None;
    if !is_voice {
        use crate::channels::commands::{self, ChannelCommand};
        let cmd = commands::handle_command(
            &text,
            session_id,
            agent,
            session_svc,
            is_owner,
            Some(chat_id_str),
        )
        .await;

        tracing::info!(
            "Telegram: handle_command returned {:?} (chat={}, is_dm={})",
            std::mem::discriminant(&cmd),
            msg.chat.id.0,
            is_dm
        );

        // Handle simple text-response commands (Help, Usage, MissionControl,
        // Evolve, Doctor, etc.). Prefer NATIVE rich rendering — the same
        // `sendRichMessage` path regular messages and cron reports use, which
        // turns markdown tables/headings into real Telegram tables (not `<pre>`
        // ASCII grids). Falls back to chunked HTML-or-plain when rich is
        // disabled, the reply has no rich structure, or the native send fails.
        // (The old single `.parse_mode(Html).await?` had no chunking either, so
        // the >4096-char mission-control report silently failed to send at all.)
        // Direct model switch (#467): on success, Telegram offers an inline
        // "Apply to all sessions" button (#468) unless the user already
        // scoped with the textual `all`. Pipe separator in the callback
        // (like model:) so custom: prefixes and :free suffixes survive;
        // skipped when the payload would exceed Telegram's 64-byte limit.
        if let commands::ChannelCommand::ModelSwitched(reply) = &cmd {
            let mut keyboard: Option<InlineKeyboardMarkup> = None;
            if !reply.starts_with('')
                && !text.trim().ends_with(" all")
                && let Some(arg) = text.trim().split_once(' ').map(|x| x.1.trim())
                && let Ok((prov, model)) = crate::utils::provider_pair::parse_pair(arg)
            {
                let data = format!("allm:{prov}|{model}");
                if data.len() <= 64 {
                    keyboard = Some(InlineKeyboardMarkup::new(vec![vec![
                        InlineKeyboardButton::callback("Apply to all sessions", data),
                    ]]));
                }
            }
            let mut req = bot.send_message(msg.chat.id, reply.clone());
            if let Some(kb) = keyboard {
                req = req.reply_markup(kb);
            }
            if let Some(t) = thread_id {
                req = req.message_thread_id(t);
            }
            if let Err(e) = req.await {
                tracing::warn!("Telegram: model-switch reply failed: {e}");
                send_html_or_plain(bot, msg.chat.id, thread_id, reply, "turn").await?;
            }
            return Ok(CommandOutcome::Handled);
        }
        if let Some(reply) = commands::try_execute_text_command(&cmd).await {
            // Every slash command is owner-gated (built-ins individually,
            // skills and commands.toml entries by the catch-all gate, #975),
            // so its output is addressed to one person: in a group it goes
            // ephemeral (#756). Native rich is tried first for output that
            // benefits from it, so a scoped reply only drops to HTML once the
            // server has refused the rich variant of the parameter.
            if let Some(rx) = super::ephemeral::receiver_for(is_dm, user_id) {
                if super::rich::should_send_native_rich(&reply)
                    && super::ephemeral::try_send_rich(
                        bot.token(),
                        msg.chat.id.0,
                        thread_id,
                        rx,
                        &reply,
                    )
                    .await
                {
                    return Ok(CommandOutcome::Handled);
                }
                let html = command_md_to_html(&reply);
                let chunks = split_message(&html, 4096);
                let delivered = super::ephemeral::send_html_chunks(
                    bot.token(),
                    msg.chat.id.0,
                    thread_id,
                    rx,
                    &chunks,
                )
                .await;
                if delivered > 0 {
                    // A short delivery finishes publicly: dropping the tail
                    // would truncate the reply with nothing to show for it.
                    for chunk in chunks.iter().skip(delivered) {
                        send_html_or_plain(bot, msg.chat.id, thread_id, chunk, "turn").await?;
                    }
                    return Ok(CommandOutcome::Handled);
                }
                // Nothing landed: fall through to the public path unchanged.
            }
            // `.is_ok()` used to discard the error here, so a rich failure left
            // no trace at all and a fallback was indistinguishable from a clean
            // rich send (#927). Both outcomes are logged now.
            let sent_rich = super::rich::should_send_native_rich(&reply) && {
                match super::rich::send_rich_with_mermaid(
                    bot.api_url().as_str(),
                    bot.token(),
                    msg.chat.id.0,
                    thread_id,
                    &reply,
                    "turn",
                    "-",
                )
                .await
                {
                    Ok(()) => true,
                    Err(e) => {
                        tracing::warn!("Telegram: rich command reply failed, using HTML: {e}");
                        false
                    }
                }
            };
            if !sent_rich {
                let html = command_md_to_html(&reply);
                for chunk in split_message(&html, 4096) {
                    send_html_or_plain(bot, msg.chat.id, thread_id, chunk, "turn").await?;
                }
            }
            return Ok(CommandOutcome::Handled);
        }

        // Set once for the command acks below. `None` in a DM, where there is
        // nobody to hide the ack from. Commands whose reply carries an inline
        // keyboard (Models, Sessions, ChangeDir, Profiles) stay public: their
        // buttons drive callback edits, which need the ephemeral edit/delete
        // methods 10.2 added and this path does not implement.
        let ephemeral_rx = super::ephemeral::receiver_for(is_dm, user_id);
        match cmd {
            ChannelCommand::Models(resp) => {
                let rows: Vec<Vec<InlineKeyboardButton>> = resp
                    .providers
                    .iter()
                    .map(|(name, label, configured)| {
                        let display = if !*configured {
                            format!("🔒 {} (setup)", label)
                        } else if *name == resp.current_provider {
                            format!("{}", label)
                        } else {
                            label.clone()
                        };
                        // Unconfigured providers route through `setup:<name>`
                        // so the callback handler can show setup instructions
                        // instead of trying to swap to a provider with no key.
                        let cb = if *configured {
                            format!("provider:{}", name)
                        } else {
                            format!("setup:{}", name)
                        };
                        vec![InlineKeyboardButton::callback(display, cb)]
                    })
                    .collect();
                let keyboard = InlineKeyboardMarkup::new(rows);
                send_retrying_rate_limit("command reply", || {
                    message_in_thread(bot, msg.chat.id, thread_id, command_md_to_html(&resp.text))
                        .parse_mode(ParseMode::Html)
                        .reply_markup(keyboard.clone())
                })
                .await?;
                return Ok(CommandOutcome::Handled);
            }
            ChannelCommand::NewSession => {
                // MUST match the title format used by the per-message
                // session resolver above (see `session_title` at the
                // top of `handle_message`). Without the `[chat:<id>]`
                // suffix, the next typed message won't find this row
                // via `find_session_by_title_suffix` and resolution
                // reverts to the previously-bound session — i.e. /new
                // appears to do nothing (issue #89).
                let session_title = session_resolve::build_session_title(
                    is_dm,
                    &user.first_name,
                    user_id,
                    chat_title,
                    chat_id,
                    topic_id,
                    topic_name.as_deref(),
                );
                // The new session inherits its working directory from the
                // session that received this /new (same chat), not the global
                // most-recent session (#263).
                let prior_session = session_svc
                    .find_session_by_title_suffix(&session_resolve::chat_id_suffix(
                        chat_id, topic_id,
                    ))
                    .await
                    .unwrap_or_else(|e| {
                        // /new means a fresh session IS the intent — creation
                        // proceeds, but the lookup failure is never silent (#442).
                        tracing::error!(
                            "Telegram: /new prior-session lookup failed: {e:#} — \
                             proceeding without wd inheritance"
                        );
                        None
                    });
                // Archive the previous session on /new, except for the owner —
                // owner sessions stay non-archived so they remain visible in
                // /sessions for history review. Guest sessions get archived
                // so the next title lookup resolves cleanly to the new row.
                if !is_owner
                    && let Ok(Some(old)) = session_svc.find_session_by_title(&session_title).await
                    && let Err(e) = session_svc.archive_session(old.id).await
                {
                    tracing::error!("Telegram: failed to archive old session {}: {}", old.id, e);
                }
                match crate::channels::session_init::create_channel_session(
                    session_svc,
                    Some(session_title),
                    prior_session.as_ref(),
                )
                .await
                {
                    Ok(new_session) => {
                        if is_owner {
                            *shared_session.lock().await = Some(new_session.id);
                        }
                        telegram_state
                            .register_session_chat(new_session.id, msg.chat.id.0, topic_id)
                            .await;
                        // Sync provider for the new session so baseline is accurate
                        let new_meta = session_svc.get_session(new_session.id).await.ok().flatten();
                        crate::channels::commands::sync_provider_for_session(
                            agent,
                            new_session.id,
                            new_meta.as_ref().and_then(|s| s.provider_name.as_deref()),
                            new_meta.as_ref().and_then(|s| s.model.as_deref()),
                        )
                        .await;
                        let baseline = agent.base_context_tokens();
                        let ctx_max = agent.context_limit_for_session(new_session.id);
                        let footer = crate::utils::format_ctx_footer(baseline, ctx_max, None);
                        let msg_text = format!("✅ New session started.\n\n{footer}");
                        super::ephemeral::send_ack(
                            bot,
                            msg.chat.id,
                            thread_id,
                            ephemeral_rx,
                            &msg_text,
                        )
                        .await?;
                        tracing::info!(
                            "Telegram /new: sent ctx footer='{}' (baseline={}, ctx_max={})",
                            footer,
                            baseline,
                            ctx_max,
                        );
                    }
                    Err(e) => {
                        tracing::error!("Telegram: failed to create session: {}", e);
                        super::ephemeral::send_ack(
                            bot,
                            msg.chat.id,
                            thread_id,
                            ephemeral_rx,
                            "Failed to create session.",
                        )
                        .await?;
                    }
                }
                return Ok(CommandOutcome::Handled);
            }
            ChannelCommand::Sessions(resp) => {
                let rows: Vec<Vec<InlineKeyboardButton>> = resp
                    .sessions
                    .iter()
                    .map(|(id, label)| {
                        let display = if *id == resp.current_session_id {
                            format!("{} ← current", label)
                        } else {
                            label.clone()
                        };
                        vec![InlineKeyboardButton::callback(
                            display,
                            format!("session:{}", id),
                        )]
                    })
                    .collect();
                let keyboard = InlineKeyboardMarkup::new(rows);
                send_retrying_rate_limit("command reply", || {
                    message_in_thread(bot, msg.chat.id, thread_id, command_md_to_html(&resp.text))
                        .parse_mode(ParseMode::Html)
                        .reply_markup(keyboard.clone())
                })
                .await?;
                return Ok(CommandOutcome::Handled);
            }
            ChannelCommand::Stop => {
                let cancelled = telegram_state.cancel_session(session_id).await;
                let reply = if cancelled {
                    "Operation cancelled."
                } else {
                    "No operation in progress."
                };
                super::ephemeral::send_ack(bot, msg.chat.id, thread_id, ephemeral_rx, reply)
                    .await?;
                return Ok(CommandOutcome::Handled);
            }
            ChannelCommand::ChangeDir(resp) => {
                // Store the browsing state for this chat
                telegram_state
                    .set_dir_browser(
                        msg.chat.id.0,
                        thread_id.map(|t| t.0.0),
                        resp.current_path.clone(),
                        resp.filter.clone(),
                    )
                    .await;

                let rows = build_cd_keyboard(&resp);
                let keyboard = InlineKeyboardMarkup::new(rows);
                send_retrying_rate_limit("command reply", || {
                    message_in_thread(bot, msg.chat.id, thread_id, command_md_to_html(&resp.text))
                        .parse_mode(ParseMode::Html)
                        .reply_markup(keyboard.clone())
                })
                .await?;
                return Ok(CommandOutcome::Handled);
            }
            ChannelCommand::Profiles(resp) => {
                let rows = build_profiles_keyboard(&resp);
                let keyboard = InlineKeyboardMarkup::new(rows);
                send_retrying_rate_limit("command reply", || {
                    message_in_thread(bot, msg.chat.id, thread_id, command_md_to_html(&resp.text))
                        .parse_mode(ParseMode::Html)
                        .reply_markup(keyboard.clone())
                })
                .await?;
                return Ok(CommandOutcome::Handled);
            }
            ChannelCommand::Compact => {
                // Only the ack is scoped: the compaction turn that follows is
                // an ordinary agent turn and stays public.
                super::ephemeral::send_ack(
                    bot,
                    msg.chat.id,
                    thread_id,
                    ephemeral_rx,
                    "⏳ Compacting context...",
                )
                .await?;
                text = "[SYSTEM: Compact context now. Summarize this conversation for continuity.]"
                    .to_string();
                // fall through to agent
            }
            ChannelCommand::ExecutePlan => {
                // Approve and /execute are FORBIDDEN while a turn is
                // running: refuse immediately, never queue (locked).
                if telegram_state.is_turn_active(session_id) {
                    super::ephemeral::send_ack(
                        bot,
                        msg.chat.id,
                        thread_id,
                        ephemeral_rx,
                        "⛔ A turn is running. /execute and Approve are refused while \
                         busy; try again when the turn finishes.",
                    )
                    .await?;
                    return Ok(CommandOutcome::Handled);
                }
                match crate::utils::plan_mode::try_approve(session_id).await {
                    crate::utils::plan_mode::ApproveOutcome::Refused(reply) => {
                        super::ephemeral::send_ack(
                            bot,
                            msg.chat.id,
                            thread_id,
                            ephemeral_rx,
                            &reply,
                        )
                        .await?;
                        return Ok(CommandOutcome::Handled);
                    }
                    crate::utils::plan_mode::ApproveOutcome::SeedTurn { prompt } => {
                        // Visible seed turn: fall through to the agent with
                        // the locked implement-turn prompt as the message.
                        text = prompt;
                    }
                }
            }
            ChannelCommand::DiscardPlan => {
                // /discard cancels an in-flight turn first, then cleans up.
                let cancelled = telegram_state.cancel_session(session_id).await;
                let mut reply = crate::utils::plan_mode::discard(session_id, agent.context()).await;
                // Remove the persistent plan card — no turn runs to refresh it
                // away (#580).
                super::plan_card::remove_plan_card(bot, msg.chat.id, telegram_state, session_id)
                    .await;
                if cancelled {
                    reply = format!("⏹️ Cancelled the running turn. {reply}");
                }
                super::ephemeral::send_ack(bot, msg.chat.id, thread_id, ephemeral_rx, &reply)
                    .await?;
                return Ok(CommandOutcome::Handled);
            }
            ChannelCommand::UserPrompt(prompt) => {
                // Capture the raw invocation ("/drop_release") BEFORE `text` is
                // overwritten with the resolved skill/command body, so a
                // mid-turn injection can name the command and frame it as a
                // distinct directive rather than a follow-up to absorb.
                command_invocation = Some(text.clone());
                text = prompt;
                // fall through to agent with the prompt as the message
            }
            ChannelCommand::PlanModeWithQuery(query) => {
                // `/plan <query>`: Plan mode was already armed in handle_command;
                // run `query` as the planning turn so the agent drafts the design
                // from it in one step (#579).
                command_invocation = Some("/plan".to_string());
                text = query;
                // fall through to agent with the query as the message
            }
            ChannelCommand::NotACommand => {} // fall through to agent
            // Help, Usage, Evolve, Doctor, UserSystem handled by try_execute_text_command above
            _ => {}
        }
    }

    Ok(CommandOutcome::Continue {
        text,
        command_invocation,
        pre_rewrite_user_text,
    })
}