Skip to content

Open source Grok Bot alternative, Bring your own agent and subscription: message Claude Code, Codex, Gemini CLI, Cursor, Copilot and 40+ AI agents as bots on your own computer, from iPhone, Mac, Windows, Linux or terminal. Agent-agnostic and free. Also a Muse and Dots alternative. Built in Rust.

14.6 KB
Raw Blame
//! Moving through panes, turns and messages, and answering permission cards.

use crossterm::event::{KeyCode, KeyEvent};
use serde_json::{Value, json};

use super::{After, App, Focus, Item, Msg, TraceMode, Width, copy, group};

impl App {
    /// `codync-host install` with this same binary, for when nothing is listening yet.
    pub(super) fn install_host(&mut self) {
        let Ok(exe) = std::env::current_exe() else { return };
        self.flash("Installing the host…");
        let tx = self.tx.clone();
        tokio::spawn(async move {
            let out = tokio::process::Command::new(exe).arg("install").output().await;
            let r = match out {
                Ok(o) if o.status.success() => Ok(Value::Null),
                Ok(o) => Err(String::from_utf8_lossy(&o.stderr).trim().to_owned()),
                Err(e) => Err(e.to_string()),
            };
            let _ = tx.send(Msg::Reply(After::Installed, r));
        });
    }

    pub(super) fn start_typing(&mut self) {
        if self.selected.is_some() {
            self.typing = true;
            self.chat_page = true;
            self.focus = Focus::Chat;
            if self.trace == TraceMode::Full {
                self.trace = TraceMode::Off;
            }
        }
    }

    pub(super) fn line(&mut self, d: isize) {
        match self.focus {
            Focus::Roster => self.move_bot(d),
            Focus::Chat if self.width == Width::Narrow && !self.chat_page => self.move_bot(d),
            Focus::Chat => self.scroll_chat(-d),
            Focus::Trace => self.scroll_trace(d),
        }
    }

    pub(super) fn scroll_focused(&mut self, up: isize) {
        if self.focus == Focus::Trace {
            self.scroll_trace(-up);
        } else {
            self.scroll_chat(up);
        }
    }

    fn panes(&self) -> Vec<Focus> {
        let mut p = vec![];
        if self.width != Width::Narrow {
            p.push(Focus::Roster);
        }
        if self.trace != TraceMode::Full {
            p.push(Focus::Chat);
        }
        if self.trace != TraceMode::Off {
            p.push(Focus::Trace);
        }
        p
    }

    pub(super) fn cycle_focus(&mut self, back: bool) {
        let p = self.panes();
        let i = p.iter().position(|f| *f == self.focus).unwrap_or(0);
        let n = p.len();
        self.focus = p[if back { (i + n - 1) % n } else { (i + 1) % n }];
    }

    pub(super) fn focus_side(&mut self, d: isize) {
        let p = self.panes();
        let i = p.iter().position(|f| *f == self.focus).unwrap_or(0);
        let j = i.saturating_add_signed(d).min(p.len() - 1);
        self.focus = p[j];
    }

    pub(super) fn step_turn(&mut self, d: isize) {
        let Some(id) = self.selected.clone() else { return };
        let turns = self.lane_turns(&id);
        let Some(&latest) = turns.last() else { return };
        let at = self.trace_turn.and_then(|t| turns.iter().position(|x| *x == t)).unwrap_or(turns.len() - 1);
        let next = turns[at.saturating_add_signed(d).min(turns.len() - 1)];
        self.trace_turn = if next == latest { None } else { Some(next) };
        self.trace_scroll = 0;
        if self.trace == TraceMode::Off {
            self.trace =
                if matches!(self.width, Width::Narrow | Width::Mid) { TraceMode::Full } else { TraceMode::Pane };
        }
    }

    pub(super) fn answer(&mut self, entry: &str, option: &str) {
        // One answer per card: while it is on its way, the card spins and takes no other.
        if self.answering.contains_key(entry) {
            return;
        }
        self.answering.insert(entry.to_owned(), option.to_owned());
        self.call(
            "respondPermission",
            json!({"entryId": entry, "optionId": option}),
            After::Answered(entry.to_owned()),
        );
    }

    pub(super) fn answer_kind(&mut self, key: char) {
        let Some(e) = self.pending() else { return };
        let (entry, opts) = (e.id.clone(), e.options());
        let want: &[&str] = match key {
            'y' => &["allow_once", "allow_always"],
            'a' => &["allow_always", "allow_once"],
            _ => &["reject_once", "reject_always"],
        };
        let pick = want.iter().find_map(|k| opts.iter().find(|(_, _, kind)| kind == k));
        match pick {
            Some((option, _, _)) => {
                let option = option.clone();
                self.answer(&entry, &option);
                if key == 'N' {
                    self.start_typing();
                }
            }
            None => self.flash("That choice isn't offered; use 1–9"),
        }
    }

    /// `r`: moves the pick through the chat's messages (none picked: the newest).
    pub(super) fn step_pick(&mut self, d: isize) {
        let Some(id) = self.selected.clone() else { return };
        // A run of exchanges with one peer is one pick: its first entry.
        let lane = self.chat(&id);
        let picks: Vec<(String, bool)> = group(&id, &lane)
            .into_iter()
            .filter_map(|item| match item {
                Item::Entry(e)
                    if e.is_message() || e.is_bot_message() || e.unfolds() || e.retries() || e.adds_fallback() =>
                {
                    Some((e.id.clone(), e.is_message()))
                }
                Item::Group(g) => Some((g.first.id.clone(), false)),
                Item::Entry(_) => None,
            })
            .collect();
        if picks.is_empty() {
            self.pick = None;
            self.flash("No message to reply to yet");
            return;
        }
        let at = self.pick.as_ref().and_then(|p| picks.iter().position(|(x, _)| x == p));
        // A fresh pick lands on the newest message, so `r` still replies to it; j/k reach bot rows.
        let newest = picks.iter().rposition(|&(_, message)| message).unwrap_or(picks.len() - 1);
        let i = at.map_or(newest, |i| i.saturating_add_signed(d).min(picks.len() - 1));
        self.pick = Some(picks[i].0.clone());
    }

    /// Keys while a message is picked. Returns whether the key was used.
    pub(super) fn pick_key(&mut self, k: KeyEvent) -> bool {
        let Some(id) = self.selected.clone() else { return false };
        let picked = self.pick.as_ref().and_then(|p| self.lane(&id).into_iter().find(|e| &e.id == p).cloned());
        match k.code {
            KeyCode::Char('j') | KeyCode::Down => self.step_pick(1),
            KeyCode::Char('k') | KeyCode::Up => self.step_pick(-1),
            KeyCode::Char('C') => self.open_connection(),
            KeyCode::Char('F') => self.retry_failed_run(),
            KeyCode::Char('A') if picked.as_ref().is_some_and(|e| self.fallback_owner(&id, e).is_some()) => {
                if let Some(e) = picked {
                    self.add_fallback(&id, &e);
                }
            }
            KeyCode::Enter if picked.as_ref().is_some_and(super::Entry::is_bot_message) => {
                if let Some(e) = picked {
                    self.open_bot_chat(&e.id);
                }
            }
            KeyCode::Char('c') if picked.as_ref().is_some_and(super::Entry::is_bot_message) => {
                if let Some(e) = picked {
                    let lane = self.lane(&id);
                    let grouped = group(&id, &lane).iter().any(|i| matches!(i, Item::Group(g) if g.first.id == e.id));
                    if grouped {
                        self.flash("Bot conversations are read-only");
                    } else {
                        copy(&super::Exchange::of(&id, &e).map(|x| x.text).unwrap_or_default());
                        self.flash("Copied");
                    }
                }
            }
            KeyCode::Char('r' | 'f' | '1'..='6') if picked.as_ref().is_some_and(super::Entry::is_bot_message) => {
                self.flash("Bot conversations are read-only");
            }
            KeyCode::Enter if picked.as_ref().is_some_and(|e| e.data["connectionRequest"]["status"] == "pending") => {
                self.open_connection();
            }
            KeyCode::Char('o') if picked.as_ref().is_some_and(|e| e.artifact().is_some()) => {
                if let Some(artifact) = picked.as_ref().and_then(super::Entry::artifact) {
                    self.fetch_artifact(&id, artifact, crate::tui::net::artifacts::ArtifactTo::Browser);
                }
            }
            KeyCode::Char('o') if picked.as_ref().is_some_and(super::Entry::unfolds) => {
                if let Some(e) = picked
                    && !self.unfolded.remove(&e.id)
                {
                    // A call's own messages, which the loaded chat may not reach back to.
                    if let Some(call) =
                        e.call().filter(|c| e.kind == super::Kind::Notice && !self.call_entries.contains_key(*c))
                    {
                        self.fetch_call(&id, call);
                    }
                    self.unfolded.insert(e.id);
                }
            }
            KeyCode::Char('e') if picked.as_ref().is_some_and(|e| e.data["handoff"]["status"] == "draft") => {
                if let Some(e) = picked {
                    self.overlays.push(super::Overlay::Fields(Box::new(crate::tui::manage::handoff_fields(&e))));
                }
            }
            KeyCode::Char('K') if picked.as_ref().is_some_and(|e| e.data["handoff"]["status"] == "draft") => {
                if let Some(e) = picked {
                    self.call("keepConversation", json!({"entryId": e.id}), After::Answered(e.id.clone()));
                }
            }
            KeyCode::Enter | KeyCode::Char('r') => match picked {
                Some(e) if !e.takes_reply() => self.flash("Only messages take replies"),
                Some(e) => {
                    self.reply = Some(e.id);
                    self.pick = None;
                    self.start_typing();
                }
                None => self.pick = None,
            },
            KeyCode::Char(c @ '1'..='6') => {
                if let Some(e) = picked {
                    self.react(&e.id, (c as usize) - ('1' as usize));
                }
            }
            KeyCode::Char('f') => {
                if let Some(e) = picked {
                    self.save_files(&id, &e.id, &e.data);
                }
            }
            KeyCode::Char('c') => {
                if let Some(e) = picked {
                    copy(e.text());
                    self.flash("Copied");
                }
            }
            KeyCode::Esc => self.pick = None,
            _ => return false,
        }
        true
    }

    /// Moves the selected bot one place up (-1) or down (+1) and saves the order, staying in its band
    /// (`Bot::band`: pinned bots, the rest). Pinned bots move among all pinned bots (they
    /// show no sections), the rest only inside their section. Shown at once; the host's bot events
    /// confirm it.
    pub(super) fn reorder_bot(&mut self, d: isize) {
        let roster = self.roster();
        let Some(i) = self.selected.as_ref().and_then(|s| roster.iter().position(|b| &b.id == s)) else { return };
        let group = roster[i].roster_group();
        // A folded section shows no order to change.
        if group.1.is_some_and(|s| self.folded.contains(s)) {
            return;
        }
        let Some(j) = i.checked_add_signed(d).filter(|&j| roster.get(j).is_some_and(|b| b.roster_group() == group))
        else {
            return;
        };
        let mut ids: Vec<String> = roster.iter().map(|b| b.id.clone()).collect();
        ids.swap(i, j);
        for (position, id) in (0_i64..).zip(&ids) {
            if let Some(b) = self.bots.get_mut(id) {
                b.position = position;
            }
        }
        self.call("reorderBots", json!({"ids": ids}), After::Nothing);
    }

    pub(super) fn copy_last(&mut self) {
        let Some(id) = self.selected.clone() else { return };
        let Some(text) = self.lane(&id).into_iter().rev().find(|e| e.is_final()).map(|e| e.text().to_owned()) else {
            self.flash("No reply to copy");
            return;
        };
        copy(&text);
        self.flash("Copied the last reply");
    }
}

#[cfg(test)]
mod tests {
    use super::super::{Bot, test_app};
    use serde_json::json;

    #[tokio::test]
    async fn reordering_stays_inside_a_band() {
        let mut app = test_app();
        let bots = [("p", true, false, 0), ("a", false, false, 3), ("b", false, false, 2), ("l", false, true, 0)];
        for (id, pinned, lead, last_at) in bots.into_iter().chain([("c", false, false, 1)]) {
            let bot = Bot::parse(&json!({"id": id, "pinned": pinned, "lead": lead, "lastAt": last_at}))
                .expect("a bot with an id parses");
            app.bots.insert(id.into(), bot);
        }
        let order = |app: &super::App| app.roster().iter().map(|b| b.id.clone()).collect::<Vec<_>>();
        assert_eq!(order(&app), ["p", "a", "b", "c", "l"], "unarranged: pinned, then recent activity");
        app.selected = Some("c".into());
        app.reorder_bot(-1);
        assert_eq!(order(&app), ["p", "a", "c", "b", "l"]);
        app.selected = Some("a".into());
        app.reorder_bot(-1);
        assert_eq!(order(&app), ["p", "a", "c", "b", "l"], "an unpinned bot can't move above a pinned one");
        app.selected = Some("l".into());
        app.reorder_bot(-1);
        assert_eq!(order(&app), ["p", "a", "c", "l", "b"], "the lead moves like any bot");
    }

    #[tokio::test]
    async fn unpinned_bots_reorder_inside_their_section_pinned_ones_among_all_pinned() {
        let mut app = test_app();
        let bots = [
            ("p1", true, Some("Home"), 5),
            ("p2", true, None, 4),
            ("a", false, Some("Work"), 3),
            ("b", false, Some("Work"), 2),
            ("c", false, None, 1),
        ];
        for (id, pinned, section, last_at) in bots {
            let bot = Bot::parse(&json!({"id": id, "pinned": pinned, "section": section, "lastAt": last_at}))
                .expect("a bot with an id parses");
            app.bots.insert(id.into(), bot);
        }
        let order = |app: &super::App| app.roster().iter().map(|b| b.id.clone()).collect::<Vec<_>>();
        app.selected = Some("p2".into());
        app.reorder_bot(-1);
        assert_eq!(order(&app), ["p2", "p1", "a", "b", "c"], "pinned rows show no sections: any two swap");
        app.selected = Some("c".into());
        app.reorder_bot(-1);
        assert_eq!(order(&app), ["p2", "p1", "a", "b", "c"], "a bot in no section can't move into one");
        app.selected = Some("b".into());
        app.reorder_bot(-1);
        assert_eq!(order(&app), ["p2", "p1", "b", "a", "c"]);
        app.folded.insert("Work".into());
        app.reorder_bot(1);
        assert_eq!(order(&app), ["p2", "p1", "b", "a", "c"], "a folded section shows no order to change");
    }
}