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.

refactor(host): split tui app state into modules

leepokai407712c66441
diff --git a/host/src/tui/app.rs b/host/src/tui/app.rs
deleted file mode 100644
index c3c62ba5..00000000
--- a/host/src/tui/app.rs
+++ /dev/null
@@ -1,2670 +0,0 @@
-//! TUI state and everything that changes it: host events, keys, mouse, replies.
-
-use crossterm::event::{KeyCode, KeyEvent, KeyModifiers, MouseButton, MouseEvent, MouseEventKind};
-use ratatui::layout::{Position, Rect};
-use serde_json::{Value, json};
-use std::collections::{BTreeMap, HashMap, HashSet};
-use std::io::Write as _;
-use std::time::{Duration, Instant};
-use tokio::sync::mpsc::UnboundedSender;
-
-use super::manage::{AgentSetup, Fields, Market, MemorySheet, Reply, RoutineForm, RoutineList, Shell};
-use super::net::Client;
-
-pub enum Msg {
- Online(bool),
- Event(Value),
- Reply(After, Result<Value, String>),
- /// The stream is starting over from rev 0: drop what we have.
- Rewind,
- /// A setup terminal's output or exit.
- Term(String, Value),
-}
-
-/// What to do with a command's reply.
-#[derive(Clone)]
-pub enum After {
- Nothing,
- /// A permission card's answer arrived (or couldn't).
- Answered(String),
- Sent(String),
- Connection(String, super::connections::Step),
- Hello,
- History(String),
- Thread,
- Dirs,
- Pairing,
- Backends,
- Update,
- Saved,
- Connectors,
- Skills,
- Installed,
- /// Memory, routines, marketplace, sign-in (`manage`).
- Sheet(Reply),
-}
-
-#[derive(Clone, Copy, PartialEq, Eq, Debug)]
-pub enum Status {
- Idle,
- Working,
- NeedsInput,
- Error,
-}
-
-/// What a bot's row shows, highest priority first.
-#[derive(Clone, Copy, PartialEq, Eq, Debug)]
-pub enum Mark {
- Need,
- Error,
- Unread,
- Work,
- Idle,
-}
-
-impl Mark {
- pub fn priority(self) -> u8 {
- match self {
- Self::Need => 4,
- Self::Error => 3,
- Self::Unread => 2,
- Self::Work => 1,
- Self::Idle => 0,
- }
- }
-}
-
-#[derive(Clone, Debug)]
-#[allow(clippy::struct_excessive_bools, reason = "mirrors the host's bot JSON")]
-pub struct Bot {
- pub id: String,
- pub name: String,
- pub description: String,
- pub color: String,
- pub shape: String,
- pub backend: String,
- pub cwd: String,
- pub managed_workspace: bool,
- pub auto: bool,
- pub model: Option<String>,
- pub pinned: bool,
- pub hidden: bool,
- pub notify: bool,
- pub computer: bool,
- pub connectors: Vec<String>,
- pub skills: Vec<String>,
- pub status: Status,
- pub activity: String,
- pub started_at: Option<i64>,
- pub unread: i64,
- pub last_message: String,
- pub last_at: i64,
- /// A group chat: several bots and the user, no agent of its own.
- pub group: bool,
- pub members: Vec<String>,
- /// Where the running turn talks: a chat (`None` = its own) and a thread in it.
- pub working_chat: Option<String>,
- pub working_thread: Option<String>,
-}
-
-fn strs(v: &Value) -> Vec<String> {
- v.as_array().into_iter().flatten().filter_map(Value::as_str).map(str::to_owned).collect()
-}
-
-impl Bot {
- /// Where it works, as the header shows it.
- pub fn folder(&self, home: &str) -> String {
- if self.managed_workspace { "personal workspace".to_owned() } else { tilde(&self.cwd, home) }
- }
-
- fn parse(v: &Value) -> Option<Self> {
- let s = |k: &str| v[k].as_str().unwrap_or_default().to_owned();
- Some(Self {
- id: v["id"].as_str()?.to_owned(),
- name: s("name"),
- description: s("description"),
- color: v["avatarColor"].as_str().unwrap_or("blue").to_owned(),
- shape: v["avatarShape"].as_str().unwrap_or("blob").to_owned(),
- backend: s("backend"),
- cwd: s("cwd"),
- managed_workspace: v["managedWorkspace"] == true,
- auto: v["permission"] == "auto",
- model: v["model"].as_str().filter(|m| !m.is_empty()).map(str::to_owned),
- pinned: v["pinned"].as_bool().unwrap_or(false),
- hidden: v["hidden"].as_bool().unwrap_or(false),
- notify: v["notify"].as_bool().unwrap_or(true),
- computer: v["computer"].as_bool().unwrap_or(false),
- connectors: strs(&v["connectors"]),
- skills: strs(&v["skills"]),
- status: match v["status"].as_str() {
- Some("working") => Status::Working,
- Some("needsInput") => Status::NeedsInput,
- Some("error") => Status::Error,
- _ => Status::Idle,
- },
- activity: s("activity"),
- started_at: v["startedAt"].as_i64(),
- unread: v["unread"].as_i64().unwrap_or(0),
- last_message: s("lastMessage"),
- last_at: v["lastAt"].as_i64().unwrap_or(0),
- group: v["kind"] == "group",
- members: strs(&v["members"]),
- working_chat: v["workingChat"].as_str().map(str::to_owned),
- working_thread: v["workingThread"].as_str().map(str::to_owned),
- })
- }
-
- /// Whether the running turn talks in this chat: its main lane (`None`) or that thread.
- pub fn works_in(&self, thread: Option<&str>) -> bool {
- self.away().is_none() && self.working_thread.as_deref() == thread
- }
-
- /// The other chat (a group) the running turn talks in, if it isn't this bot's own.
- pub fn away(&self) -> Option<&str> {
- self.working_chat.as_deref().filter(|c| *c != self.id)
- }
-
- pub fn mark(&self) -> Mark {
- match self.status {
- Status::NeedsInput => Mark::Need,
- Status::Error => Mark::Error,
- Status::Working => Mark::Work,
- Status::Idle if self.unread > 0 => Mark::Unread,
- Status::Idle => Mark::Idle,
- }
- }
-}
-
-#[derive(Clone, Copy, PartialEq, Eq, Debug)]
-pub enum Kind {
- User,
- Agent,
- Thought,
- Tool,
- Plan,
- Permission,
- Notice,
- Other,
-}
-
-#[derive(Clone, Debug)]
-pub struct Entry {
- pub id: String,
- pub seq: i64,
- pub turn: i64,
- pub kind: Kind,
- pub data: Value,
- pub created_at: i64,
- /// The main-chat message whose thread this is in; `None` is the main chat.
- pub thread_id: Option<String>,
-}
-
-impl Entry {
- fn parse(v: &Value) -> Option<(String, Self)> {
- let kind = match v["kind"].as_str()? {
- "user" => Kind::User,
- "agent" => Kind::Agent,
- "thought" => Kind::Thought,
- "tool" => Kind::Tool,
- "plan" => Kind::Plan,
- "permission" => Kind::Permission,
- "notice" => Kind::Notice,
- _ => Kind::Other,
- };
- Some((
- v["botId"].as_str()?.to_owned(),
- Self {
- id: v["id"].as_str()?.to_owned(),
- seq: v["seq"].as_i64()?,
- turn: v["turn"].as_i64().unwrap_or(0),
- kind,
- data: v["data"].clone(),
- created_at: v["createdAt"].as_i64().unwrap_or(0),
- thread_id: v["threadId"].as_str().map(str::to_owned),
- },
- ))
- }
-
- pub fn text(&self) -> &str {
- self.data["text"].as_str().unwrap_or_default()
- }
-
- /// Files sent with a user message: `(name, size)`.
- pub fn attachments(&self) -> Vec<(&str, u64)> {
- self.data["attachments"]
- .as_array()
- .into_iter()
- .flatten()
- .filter_map(|a| Some((a["name"].as_str()?, a["size"].as_u64().unwrap_or(0))))
- .collect()
- }
-
- /// A notice's line; a voice call gets its length ("Voice chat · 00:16").
- pub fn notice_text(&self) -> String {
- match self.data["callSeconds"].as_u64() {
- Some(s) => format!("{} · {:02}:{:02}", self.text(), s / 60, s % 60),
- None => self.text().to_owned(),
- }
- }
-
- pub fn is_final(&self) -> bool {
- self.kind == Kind::Agent && self.data["final"].as_bool().unwrap_or(false)
- }
-
- /// What the chat shows as a message: the user's, and each turn's final reply.
- pub fn is_message(&self) -> bool {
- self.kind == Kind::User || self.is_final() || self.data["connectionRequest"].is_object()
- }
-
- pub fn pending(&self) -> bool {
- self.kind == Kind::Permission && self.data["status"] == "pending"
- }
-
- /// Approval options in the apps' order: allow once, allow always, reject once, reject always.
- pub fn options(&self) -> Vec<(String, String, String)> {
- let rank = |k: &str| match k {
- "allow_once" => 0,
- "allow_always" => 1,
- "reject_once" => 2,
- "reject_always" => 3,
- _ => 9,
- };
- let mut o: Vec<(String, String, String)> = self.data["options"]
- .as_array()
- .into_iter()
- .flatten()
- .map(|o| {
- let s = |k: &str| o[k].as_str().unwrap_or_default().to_owned();
- (s("optionId"), s("name"), s("kind"))
- })
- .collect();
- o.sort_by_key(|(_, _, k)| rank(k));
- o
- }
-}
-
-/// A one-line-or-more text field with readline-style editing.
-#[derive(Clone, Default, Debug)]
-pub struct Editor {
- pub text: String,
- /// Byte offset, always on a char boundary.
- pub cursor: usize,
-}
-
-impl Editor {
- pub fn with(text: &str) -> Self {
- Self { text: text.to_owned(), cursor: text.len() }
- }
-
- pub fn clear(&mut self) {
- self.text.clear();
- self.cursor = 0;
- }
-
- pub fn insert(&mut self, s: &str) {
- self.text.insert_str(self.cursor, s);
- self.cursor += s.len();
- }
-
- fn prev(&self, i: usize) -> usize {
- self.text[..i].char_indices().next_back().map_or(0, |(j, _)| j)
- }
-
- fn next(&self, i: usize) -> usize {
- self.text[i..].chars().next().map_or(i, |c| i + c.len_utf8())
- }
-
- fn word_left(&self) -> usize {
- let mut i = self.cursor;
- while i > 0 && self.text[..i].ends_with(char::is_whitespace) {
- i = self.prev(i);
- }
- while i > 0 && !self.text[..i].ends_with(char::is_whitespace) {
- i = self.prev(i);
- }
- i
- }
-
- fn word_right(&self) -> usize {
- let mut i = self.cursor;
- while i < self.text.len() && self.text[i..].starts_with(char::is_whitespace) {
- i = self.next(i);
- }
- while i < self.text.len() && !self.text[i..].starts_with(char::is_whitespace) {
- i = self.next(i);
- }
- i
- }
-
- fn line_start(&self) -> usize {
- self.text[..self.cursor].rfind('\n').map_or(0, |i| i + 1)
- }
-
- fn line_end(&self) -> usize {
- self.text[self.cursor..].find('\n').map_or(self.text.len(), |i| self.cursor + i)
- }
-
- /// Editing keys shared by every text field. Returns whether the key was used.
- pub fn key(&mut self, k: KeyEvent) -> bool {
- let ctrl = k.modifiers.contains(KeyModifiers::CONTROL);
- let alt = k.modifiers.contains(KeyModifiers::ALT);
- match k.code {
- KeyCode::Char('a') if ctrl => self.cursor = self.line_start(),
- KeyCode::Char('e') if ctrl => self.cursor = self.line_end(),
- KeyCode::Char('b') if ctrl => self.cursor = self.prev(self.cursor),
- KeyCode::Char('f') if ctrl => self.cursor = self.next(self.cursor),
- KeyCode::Char('b') if alt => self.cursor = self.word_left(),
- KeyCode::Char('f') if alt => self.cursor = self.word_right(),
- KeyCode::Char('w') if ctrl => self.cut(self.word_left(), self.cursor),
- KeyCode::Backspace if alt || ctrl => self.cut(self.word_left(), self.cursor),
- KeyCode::Char('d') if alt => self.cut(self.cursor, self.word_right()),
- KeyCode::Char('u') if ctrl => self.cut(self.line_start(), self.cursor),
- KeyCode::Char('h') if ctrl => self.cut(self.prev(self.cursor), self.cursor),
- KeyCode::Char('d') if ctrl => self.cut(self.cursor, self.next(self.cursor)),
- KeyCode::Left if alt || ctrl => self.cursor = self.word_left(),
- KeyCode::Right if alt || ctrl => self.cursor = self.word_right(),
- KeyCode::Left => self.cursor = self.prev(self.cursor),
- KeyCode::Right => self.cursor = self.next(self.cursor),
- KeyCode::Home => self.cursor = self.line_start(),
- KeyCode::End => self.cursor = self.line_end(),
- KeyCode::Backspace => self.cut(self.prev(self.cursor), self.cursor),
- KeyCode::Delete => self.cut(self.cursor, self.next(self.cursor)),
- KeyCode::Char(c) if !ctrl && !alt => self.insert(c.encode_utf8(&mut [0; 4])),
- KeyCode::Char(c) if alt && !ctrl && !c.is_ascii_alphabetic() => self.insert(c.encode_utf8(&mut [0; 4])),
- _ => return false,
- }
- true
- }
-
- fn cut(&mut self, from: usize, to: usize) {
- if from < to {
- self.text.replace_range(from..to, "");
- self.cursor = from;
- }
- }
-}
-
-#[derive(Clone, Copy, PartialEq, Eq, Debug)]
-pub enum Focus {
- Roster,
- Chat,
- Trace,
-}
-
-#[derive(Clone, Copy, PartialEq, Eq, Debug)]
-pub enum TraceMode {
- Off,
- Pane,
- Full,
-}
-
-/// Width class, set by the last draw.
-#[derive(Clone, Copy, PartialEq, Eq, Debug)]
-pub enum Width {
- /// < 80 columns: roster and chat are separate pages.
- Narrow,
- /// 80–99: compact roster.
- Mid,
- /// 100–149.
- Wide,
- /// ≥ 150.
- Huge,
-}
-
-pub const FILTERS: [(&str, Option<Mark>); 5] = [
- ("all", None),
- ("needs you", Some(Mark::Need)),
- ("working", Some(Mark::Work)),
- ("unread", Some(Mark::Unread)),
- ("error", Some(Mark::Error)),
-];
-
-#[derive(Clone, Copy, PartialEq, Eq, Debug)]
-pub enum Action {
- NewBot,
- NewGroup,
- Market,
- Memory,
- Routines,
- RefreshAgents,
- Pair,
- Usage,
- CheckUpdate,
- Keys,
-}
-
-pub const NEW_GROUP: &str = "New group chat";
-
-pub const ACTIONS: [(&str, &str, Action); 10] = [
- ("New bot…", "n", Action::NewBot),
- ("New group chat…", "m", Action::NewGroup),
- ("Marketplace: agents, connectors, skills…", "A", Action::Market),
- ("Memory of this bot…", "M", Action::Memory),
- ("Routines of this bot…", "R", Action::Routines),
- ("Refresh agents", "", Action::RefreshAgents),
- ("Pair a phone…", "P", Action::Pair),
- ("Usage", "U", Action::Usage),
- ("Check for updates", "", Action::CheckUpdate),
- ("Keys", "?", Action::Keys),
-];
-
-pub enum GotoItem {
- Bot(String),
- Action(Action),
-}
-
-pub struct Goto {
- pub query: Editor,
- pub filter: usize,
- pub cursor: usize,
-}
-
-pub enum ConfirmAct {
- Delete(String),
- NewSession(String),
- ClearMemory(String),
- DeleteRoutine(String, String),
- RotateRoutineKey(String, String),
- RemoveConnector(String),
- RemoveSkill(String),
-}
-
-pub struct Confirm {
- pub title: String,
- pub detail: String,
- pub note: String,
- pub button: &'static str,
- pub act: ConfirmAct,
-}
-
-pub struct AgentPicker {
- pub query: Editor,
- pub cursor: usize,
-}
-
-#[derive(Clone)]
-pub struct Dir {
- pub name: String,
- pub path: String,
- pub git: bool,
-}
-
-pub struct FolderPicker {
- pub path: String,
- pub parent: Option<String>,
- pub dirs: Vec<Dir>,
- pub query: Editor,
- pub cursor: usize,
- pub error: Option<String>,
-}
-
-pub const COLORS: [&str; 11] =
- ["black", "brown", "red", "orange", "yellow", "green", "cyan", "blue", "violet", "magenta", "gray"];
-pub const SHAPES: [&str; 8] = ["blob", "pebble", "squircle", "tablet", "wedge", "hex", "cloud", "teardrop"];
-
-#[derive(Clone, Copy, PartialEq, Eq, Debug)]
-pub enum Field {
- Name,
- Instructions,
- Agent,
- Model,
- Folder,
- Approvals,
- Notify,
- Computer,
- Color,
- Shape,
- Connectors,
- Skills,
-}
-
-pub const FIELDS: [Field; 12] = [
- Field::Name,
- Field::Instructions,
- Field::Agent,
- Field::Model,
- Field::Folder,
- Field::Approvals,
- Field::Notify,
- Field::Computer,
- Field::Color,
- Field::Shape,
- Field::Connectors,
- Field::Skills,
-];
-
-pub struct Toggle {
- pub id: String,
- pub name: String,
- pub on: bool,
-}
-
-#[allow(clippy::struct_excessive_bools, reason = "independent bot settings and save state")]
-pub struct Form {
- pub bot_id: Option<String>,
- pub name: Editor,
- pub instructions: Editor,
- pub backend: String,
- pub model: Editor,
- pub cwd: String,
- pub auto: bool,
- pub notify: bool,
- pub computer: bool,
- pub color: usize,
- pub shape: usize,
- pub connectors: Vec<Toggle>,
- pub skills: Vec<Toggle>,
- pub field: usize,
- /// Cursor inside the connector / skill toggles.
- pub sub: usize,
- pub error: Option<String>,
- pub saving: bool,
-}
-
-impl Form {
- pub fn current(&self) -> Field {
- FIELDS[self.field]
- }
-}
-
-#[derive(Clone, Copy, PartialEq, Eq, Debug)]
-pub enum GroupField {
- Name,
- About,
- Bots,
-}
-
-impl GroupField {
- fn next(self) -> Self {
- match self {
- Self::Name => Self::About,
- Self::About => Self::Bots,
- Self::Bots => Self::Name,
- }
- }
-
- fn prev(self) -> Self {
- self.next().next()
- }
-}
-
-/// Create a group chat, or rename one and change who's in it.
-pub struct GroupForm {
- /// The group being edited; `None` creates one.
- pub group_id: Option<String>,
- pub name: Editor,
- /// What the group is for; sent as `description`, shown to its members.
- pub about: Editor,
- /// Picked bot ids, in the order they were picked.
- pub members: Vec<String>,
- pub field: GroupField,
- pub cursor: usize,
- pub error: Option<String>,
- pub saving: bool,
-}
-
-pub enum Overlay {
- Goto(Goto),
- Help(Editor),
- Confirm(Confirm),
- Agents(AgentPicker),
- Folder(FolderPicker),
- Form(Box<Form>),
- Group(GroupForm),
- Usage,
- Pair(Option<String>),
- Memory(MemorySheet),
- Routines(RoutineList),
- Routine(Box<RoutineForm>),
- Market(Box<Market>),
- Agent(AgentSetup),
- Fields(Box<Fields>),
-}
-
-pub struct Toast {
- pub text: String,
- pub need: bool,
- pub until: Instant,
-}
-
-#[derive(Clone)]
-pub enum Click {
- Bot(String),
- Option {
- entry: String,
- option: String,
- },
- Filter(usize),
- Composer,
- /// The replies line under a message: opens its thread.
- Thread(String),
-}
-
-#[derive(Default)]
-pub struct Hits {
- pub roster: Rect,
- pub chat: Rect,
- pub trace: Rect,
- pub clicks: Vec<(Rect, Click)>,
-}
-
-#[derive(Clone)]
-struct PendingSend {
- text: String,
- files: Vec<std::path::PathBuf>,
- nonce: String,
- busy: bool,
-}
-
-#[allow(clippy::struct_excessive_bools, reason = "independent UI flags, not a state machine")]
-pub struct App {
- pub client: Client,
- pub(super) tx: UnboundedSender<Msg>,
- pub url: String,
- pub host: String,
- pub home: String,
- pub online: bool,
- pub error: Option<String>,
- /// This client and the host can't work together until one of them updates.
- pub mismatch: Option<crate::compat::Mismatch>,
- pub bots: HashMap<String, Bot>,
- pub entries: HashMap<String, BTreeMap<i64, Entry>>,
- pub usage: Value,
- pub backends: Vec<Value>,
- pub selected: Option<String>,
- pub focus: Focus,
- pub typing: bool,
- /// Narrow layout: showing the chat page (else the roster page).
- pub chat_page: bool,
- pub drafts: HashMap<String, Editor>,
- /// Files dropped on the composer (pasted paths), per draft like the text.
- pub files: HashMap<String, Vec<std::path::PathBuf>>,
- sends: HashMap<String, PendingSend>,
- /// Lines scrolled up from the bottom of the chat (0 = follow new messages).
- pub chat_scroll: usize,
- pub trace_scroll: usize,
- pub trace: TraceMode,
- /// Turn shown in the trace; `None` follows the latest.
- pub trace_turn: Option<i64>,
- pub compact: bool,
- pub overlays: Vec<Overlay>,
- pub toasts: Vec<Toast>,
- pub frame: u64,
- pub term_focused: bool,
- pub quit: bool,
- pub hits: Hits,
- pub width: Width,
- /// Chat/trace viewport heights from the last draw, for page scrolling.
- pub chat_height: usize,
- pub chat_top: bool,
- /// The open thread's root entry in the selected chat; `None` shows the main chat.
- pub thread: Option<String>,
- /// A main-chat message picked to reply to in its thread (`r`, then j/k and ↵).
- pub pick: Option<String>,
- pub market: (Vec<Toggle>, Vec<Toggle>),
- /// Installed connectors and skills as the host lists them.
- pub plugins: (Vec<Value>, Vec<Value>),
- /// Models each agent offers (`None` while loading).
- pub models: HashMap<String, Option<Vec<(String, String)>>>,
- /// A setup terminal holding the screen.
- pub shell: Option<Shell>,
- reading: HashSet<String>,
- history_busy: HashSet<String>,
- /// Permission cards whose answer is on its way, with the chosen option.
- pub answering: HashMap<String, String>,
- history_done: HashSet<String>,
- pub hint: Option<(String, Instant)>,
-}
-
-impl App {
- pub fn new(client: Client, tx: UnboundedSender<Msg>, url: String) -> Self {
- Self {
- client,
- tx,
- url,
- host: String::new(),
- home: String::new(),
- online: false,
- error: None,
- mismatch: None,
- bots: HashMap::new(),
- entries: HashMap::new(),
- usage: Value::Null,
- backends: vec![],
- selected: None,
- focus: Focus::Roster,
- typing: false,
- chat_page: false,
- drafts: HashMap::new(),
- files: HashMap::new(),
- sends: HashMap::new(),
- chat_scroll: 0,
- trace_scroll: 0,
- trace: TraceMode::Off,
- trace_turn: None,
- compact: false,
- overlays: vec![],
- toasts: vec![],
- frame: 0,
- term_focused: true,
- quit: false,
- hits: Hits::default(),
- width: Width::Wide,
- chat_height: 20,
- chat_top: false,
- thread: None,
- pick: None,
- market: (vec![], vec![]),
- plugins: (vec![], vec![]),
- models: HashMap::new(),
- shell: None,
- reading: HashSet::new(),
- history_busy: HashSet::new(),
- answering: HashMap::new(),
- history_done: HashSet::new(),
- hint: None,
- }
- }
-
- pub fn call(&self, method: &'static str, body: Value, after: After) {
- self.client.spawn_call(method, body, after, self.tx.clone());
- }
-
- // ---------- derived ----------
-
- /// Roster order: pinned first, then most recent (same as the phone).
- pub fn roster(&self) -> Vec<&Bot> {
- let mut v: Vec<&Bot> = self.bots.values().filter(|b| !b.hidden).collect();
- v.sort_by(|a, b| b.pinned.cmp(&a.pinned).then(b.last_at.cmp(&a.last_at)).then(a.name.cmp(&b.name)));
- v
- }
-
- pub fn bot(&self) -> Option<&Bot> {
- self.selected.as_ref().and_then(|id| self.bots.get(id))
- }
-
- pub fn counts(&self) -> [usize; 4] {
- let mut c = [0; 4];
- for b in self.bots.values().filter(|b| !b.hidden) {
- match b.mark() {
- Mark::Need => c[0] += 1,
- Mark::Work => c[1] += 1,
- Mark::Unread => c[2] += 1,
- Mark::Error => c[3] += 1,
- Mark::Idle => {}
- }
- }
- c
- }
-
- /// A chat's entries in the lane on screen (main chat, or the open thread's replies), in display order.
- pub fn lane(&self, bot: &str) -> Vec<&Entry> {
- let thread = self.thread.as_deref();
- let mut v: Vec<&Entry> = self
- .entries
- .get(bot)
- .map(|m| m.values().filter(|e| e.thread_id.as_deref() == thread).collect())
- .unwrap_or_default();
- // A message sent mid-turn belongs after that turn's reply.
- v.sort_by_key(|e| (e.turn, e.seq));
- v
- }
-
- pub fn pending(&self) -> Option<&Entry> {
- let id = self.selected.as_ref()?;
- self.lane(id).into_iter().rev().find(|e| e.pending())
- }
-
- /// Where the composer's draft lives: per chat, and per thread in it.
- fn draft_key(&self) -> Option<String> {
- let id = self.selected.as_ref()?;
- Some(self.thread.as_ref().map_or_else(|| id.clone(), |root| format!("{id}#{root}")))
- }
-
- pub fn draft(&self) -> Editor {
- self.draft_key().and_then(|k| self.drafts.get(&k).cloned()).unwrap_or_default()
- }
-
- pub fn draft_files(&self) -> &[std::path::PathBuf] {
- self.draft_key().and_then(|k| self.files.get(&k)).map_or(&[], Vec::as_slice)
- }
-
- /// Name of an entry's author (a group reply's bot), as the apps show it.
- pub fn author_name(&self, id: Option<&str>) -> String {
- id.and_then(|id| self.bots.get(id)).map_or_else(|| "A deleted bot".into(), |b| b.name.clone())
- }
-
- /// The turns of the lane on screen, oldest first: the trace steps through these.
- /// Turn numbers count per bot, so a lane's own are not consecutive.
- pub fn lane_turns(&self, bot: &str) -> Vec<i64> {
- let mut v: Vec<i64> = self.lane(bot).iter().map(|e| e.turn).collect();
- v.sort_unstable();
- v.dedup();
- v
- }
-
- /// Whether every older entry of this bot is loaded.
- pub fn history_complete(&self, id: &str) -> bool {
- self.history_done.contains(id) || self.oldest_main(id).is_none_or(|s| s <= 1)
- }
-
- /// Paging (`history`) covers the main chat only; thread replies don't count.
- fn oldest_main(&self, id: &str) -> Option<i64> {
- self.entries.get(id)?.values().find(|e| e.thread_id.is_none()).map(|e| e.seq)
- }
-
- pub fn chat_visible(&self) -> bool {
- self.selected.is_some() && (self.width != Width::Narrow || self.chat_page) && self.trace != TraceMode::Full
- }
-
- pub fn animating(&self) -> bool {
- !self.toasts.is_empty()
- || self.hint.is_some()
- || !self.online
- || !self.answering.is_empty()
- || self.bots.values().any(|b| b.status == Status::Working)
- }
-
- fn select(&mut self, id: &str) {
- if self.selected.as_deref() != Some(id) {
- self.selected = Some(id.to_owned());
- self.thread = None;
- self.pick = None;
- self.chat_scroll = 0;
- self.trace_scroll = 0;
- self.trace_turn = None;
- }
- }
-
- fn move_bot(&mut self, delta: isize) {
- let ids: Vec<String> = self.roster().iter().map(|b| b.id.clone()).collect();
- if ids.is_empty() {
- return;
- }
- let i = self.selected.as_ref().and_then(|s| ids.iter().position(|x| x == s));
- let n = isize::try_from(ids.len()).unwrap_or(isize::MAX);
- let next = match i {
- Some(i) => (isize::try_from(i).unwrap_or(0) + delta).clamp(0, n - 1),
- None => 0,
- };
- let id = ids[usize::try_from(next).unwrap_or(0)].clone();
- self.select(&id);
- }
-
- /// Next bot (after the selected one, wrapping) whose mark matches.
- fn jump(&mut self, want: Mark) {
- // A bot busy in a group needs you there, and the group is marked too: skip the bot.
- let ids: Vec<(String, bool)> = self
- .roster()
- .iter()
- .map(|b| (b.id.clone(), b.mark() == want && (want != Mark::Need || b.away().is_none())))
- .collect();
- let start = self.selected.as_ref().and_then(|s| ids.iter().position(|(x, _)| x == s)).map_or(0, |i| i + 1);
- let found = (0..ids.len()).map(|k| &ids[(start + k) % ids.len()]).find(|(_, hit)| *hit).cloned();
- match found {
- Some((id, _)) => {
- self.select(&id);
- self.chat_page = true;
- self.focus = Focus::Chat;
- // A card waiting in a thread shows there.
- let thread = self.bots.get(&id).and_then(|b| b.working_thread.clone()).filter(|_| want == Mark::Need);
- if let Some(root) = thread {
- self.open_thread(root);
- }
- }
- None => self.flash(match want {
- Mark::Need => "No bot needs you",
- Mark::Unread => "Nothing unread",
- _ => "No match",
- }),
- }
- }
-
- pub fn flash(&mut self, s: &str) {
- self.hint = Some((s.to_owned(), Instant::now() + Duration::from_secs(3)));
- }
-
- fn toast(&mut self, text: String, need: bool) {
- if !self.term_focused {
- // OSC 9 desktop notification (iTerm2, Ghostty, kitty, WezTerm…) plus a bell.
- let mut out = std::io::stdout();
- let _ = write!(out, "\x1b]9;{}\x07\x07", text.replace(['\x07', '\x1b'], ""));
- let _ = out.flush();
- }
- self.toasts.retain(|t| t.text != text);
- self.toasts.push(Toast { text, need, until: Instant::now() + Duration::from_secs(4) });
- if self.toasts.len() > 2 {
- self.toasts.remove(0);
- }
- }
-
- /// Drops expired toasts and keeps the viewed bot marked read.
- pub fn tick(&mut self) {
- if self.selected.is_none()
- && let Some(id) = self.roster().first().map(|b| b.id.clone())
- {
- self.selected = Some(id);
- }
- let now = Instant::now();
- self.toasts.retain(|t| t.until > now);
- if self.hint.as_ref().is_some_and(|(_, until)| *until <= now) {
- self.hint = None;
- }
- if self.chat_visible()
- && self.term_focused
- && let Some(b) = self.bot()
- && b.unread > 0
- && !self.reading.contains(&b.id)
- {
- let id = b.id.clone();
- self.reading.insert(id.clone());
- self.call("markRead", json!({"botId": id, "threadId": self.thread}), After::Nothing);
- }
- if self.chat_top && self.chat_visible() && self.thread.is_none() {
- self.load_history();
- }
- }
-
- fn load_history(&mut self) {
- let Some(id) = self.selected.clone() else { return };
- if self.history_busy.contains(&id) || self.history_done.contains(&id) {
- return;
- }
- let Some(oldest) = self.oldest_main(&id) else { return };
- if oldest <= 1 {
- self.history_done.insert(id);
- return;
- }
- self.history_busy.insert(id.clone());
- self.call("history", json!({"botId": id, "beforeSeq": oldest, "limit": 200}), After::History(id));
- }
-
- // ---------- host messages ----------
-
- pub fn on_msg(&mut self, m: Msg) {
- match m {
- Msg::Online(on) => {
- self.online = on;
- if !on && self.error.is_none() && self.bots.is_empty() {
- // Say why instead of spinning on "Connecting…".
- self.call("hello", json!({}), After::Hello);
- }
- if on {
- self.error = None;
- // Every reconnect: the host may have been updated (or replaced) meanwhile.
- self.call("hello", json!({}), After::Hello);
- }
- }
- Msg::Rewind => {
- self.bots.clear();
- self.entries.clear();
- self.history_done.clear();
- // Catch-up carries only recent thread replies: fetch the open thread whole again.
- if let (Some(id), Some(root)) = (&self.selected, &self.thread) {
- self.call("thread", json!({"botId": id, "rootId": root}), After::Thread);
- }
- }
- Msg::Event(v) => self.on_event(&v),
- Msg::Reply(after, r) => self.on_reply(after, r),
- Msg::Term(id, v) => self.on_term(&id, &v),
- }
- }
-
- fn on_event(&mut self, v: &Value) {
- match v["type"].as_str() {
- Some("hello") if !v["usage"].is_null() => self.usage = v["usage"].clone(),
- Some("usage") => self.usage = v["usage"].clone(),
- Some("bot") => self.upsert_bot(&v["bot"]),
- Some("entry") => {
- if let Some((bot, e)) = Entry::parse(&v["entry"]) {
- self.entries.entry(bot).or_default().insert(e.seq, e);
- }
- }
- _ => {}
- }
- }
-
- fn upsert_bot(&mut self, v: &Value) {
- let Some(id) = v["id"].as_str() else { return };
- if v["deleted"].as_bool().unwrap_or(false) {
- self.bots.remove(id);
- self.entries.remove(id);
- let prefix = format!("{id}#");
- self.drafts.retain(|key, _| key != id && !key.starts_with(&prefix));
- self.files.retain(|key, _| key != id && !key.starts_with(&prefix));
- if self.selected.as_deref() == Some(id) {
- self.selected = None;
- self.thread = None;
- self.pick = None;
- self.chat_page = false;
- self.typing = false;
- }
- return;
- }
- let Some(new) = Bot::parse(v) else { return };
- // Marked read once per change: an open thread leaves the main chat's replies unread.
- if self.bots.get(&new.id).is_none_or(|old| old.unread != new.unread) {
- self.reading.remove(&new.id);
- }
- let old = self.bots.insert(new.id.clone(), new.clone());
- let Some(old) = old else { return };
- let watching = self.selected.as_deref() == Some(new.id.as_str()) && self.chat_visible() && self.term_focused;
- // A member's turn in a group is the group's: it alone says done / needs you.
- if !new.notify || watching || old.away().is_some() || new.away().is_some() {
- return;
- }
- if new.status == Status::NeedsInput && old.status != Status::NeedsInput {
- self.toast(format!("{} needs you", new.name), true);
- } else if old.status == Status::Working && new.status == Status::Idle {
- self.toast(format!("{} is done", new.name), false);
- } else if old.status != Status::Error && new.status == Status::Error {
- self.toast(format!("{} hit an error", new.name), true);
- }
- }
-
- fn on_reply(&mut self, after: After, r: Result<Value, String>) {
- if let After::Connection(id, step) = after {
- self.connection_reply(&id, step, r);
- return;
- }
- if let After::Sent(key) = after {
- self.sent(&key, r);
- return;
- }
- if let After::Answered(entry) = after {
- self.answering.remove(&entry);
- if let Err(e) = r {
- self.flash(&e);
- }
- return;
- }
- if let After::Sheet(s) = after {
- self.on_sheet_reply(s, r);
- return;
- }
- let v = match r {
- Ok(v) => v,
- Err(e) => {
- match after {
- After::Hello => self.error = Some(e),
- After::History(id) => {
- self.history_busy.remove(&id);
- }
- After::Saved => match self.overlays.last_mut() {
- Some(Overlay::Form(f)) => {
- f.saving = false;
- f.error = Some(e);
- }
- Some(Overlay::Group(g)) => {
- g.saving = false;
- g.error = Some(e);
- }
- _ => {}
- },
- After::Dirs => {
- if let Some(Overlay::Folder(p)) = self.overlays.last_mut() {
- p.error = Some(e);
- }
- }
- _ => self.flash(&e),
- }
- return;
- }
- };
- match after {
- After::Nothing | After::Answered(_) | After::Sheet(_) | After::Sent(_) | After::Connection(_, _) => {}
- After::Installed => {
- self.error = None;
- self.flash("Host installed; connecting…");
- self.call("hello", json!({}), After::Hello);
- }
- After::Hello => {
- self.mismatch = crate::compat::check(env!("CARGO_PKG_VERSION"), crate::compat::MIN_HOST, &v);
- v["name"].as_str().unwrap_or("computer").clone_into(&mut self.host);
- v["home"].as_str().unwrap_or_default().clone_into(&mut self.home);
- self.backends = v["backends"].as_array().cloned().unwrap_or_default();
- }
- After::Backends => {
- self.backends = v["backends"].as_array().cloned().unwrap_or_default();
- self.flash("Agents refreshed");
- }
- After::Update => match (v["state"]["availableVersion"].as_str(), v["state"]["requiredApp"].as_str()) {
- (Some(version), Some(app)) => self.flash(&match v["state"]["appStoreVersion"].as_str() {
- Some(store) => format!(
- "Version {version} waits for the iPhone app {app} (App Store has {store}); it installs after review"
- ),
- None => format!(
- "Version {version} waits: couldn't check the App Store for the iPhone app {app}. \
- codync-host update --skip-app-check installs anyway"
- ),
- }),
- (Some(version), None) => self.flash(&format!("Version {version} is available: run codync-host update")),
- (None, _) => self.flash("The host is up to date."),
- },
- After::Thread => {
- for e in v["entries"].as_array().into_iter().flatten() {
- if let Some((bot, e)) = Entry::parse(e) {
- self.entries.entry(bot).or_default().entry(e.seq).or_insert(e);
- }
- }
- }
- After::History(id) => {
- self.history_busy.remove(&id);
- let list = v["entries"].as_array().cloned().unwrap_or_default();
- if list.is_empty() {
- self.history_done.insert(id);
- }
- for e in &list {
- if let Some((bot, e)) = Entry::parse(e) {
- self.entries.entry(bot).or_default().entry(e.seq).or_insert(e);
- }
- }
- }
- After::Dirs => {
- if let Some(Overlay::Folder(p)) = self.overlays.last_mut() {
- v["path"].as_str().unwrap_or_default().clone_into(&mut p.path);
- p.parent = v["parent"].as_str().map(str::to_owned);
- p.dirs = v["dirs"]
- .as_array()
- .into_iter()
- .flatten()
- .map(|d| Dir {
- name: d["name"].as_str().unwrap_or_default().to_owned(),
- path: d["path"].as_str().unwrap_or_default().to_owned(),
- git: d["isGit"].as_bool().unwrap_or(false),
- })
- .collect();
- p.query.clear();
- p.cursor = 0;
- p.error = None;
- }
- }
- After::Pairing => {
- if let Some(Overlay::Pair(url)) = self.overlays.last_mut() {
- *url = v["pairingUrl"].as_str().map(str::to_owned);
- }
- }
- After::Saved => {
- let saved = self.overlays.iter().rposition(|o| matches!(o, Overlay::Form(_) | Overlay::Group(_)));
- // A new bot or group opens ready to type; an edit goes back to where it was.
- let created = saved.is_some_and(|i| match &self.overlays[i] {
- Overlay::Form(f) => f.bot_id.is_none(),
- Overlay::Group(g) => g.group_id.is_none(),
- _ => false,
- });
- if let Some(i) = saved {
- self.overlays.truncate(i);
- }
- if let Some(id) = v["bot"]["id"].as_str() {
- let id = id.to_owned();
- self.upsert_bot(&v["bot"]);
- if created {
- self.select(&id);
- self.chat_page = true;
- self.focus = Focus::Chat;
- self.typing = true;
- }
- }
- }
- After::Connectors | After::Skills => {
- let connectors = matches!(after, After::Connectors);
- let list = self.set_plugins(connectors, v["items"].as_array().cloned().unwrap_or_default());
- if connectors {
- self.market.0 = list;
- } else {
- self.market.1 = list;
- }
- for o in &mut self.overlays {
- if let Overlay::Form(f) = o {
- if connectors {
- // Connectors start on for a new bot.
- merge_toggles(&mut f.connectors, &self.market.0, f.bot_id.is_none());
- } else {
- merge_toggles(&mut f.skills, &self.market.1, false);
- }
- }
- }
- }
- }
- }
-
- // ---------- input ----------
-
- pub fn on_paste(&mut self, s: &str) {
- if let Some(sh) = self.shell.as_ref().filter(|s| s.exited.is_none()) {
- let _ = sh.input.send(s.as_bytes().to_vec());
- return;
- }
- let s = s.replace("\r\n", "\n").replace('\r', "\n");
- if let Some(ed) = self.top_editor() {
- ed.insert(&s);
- } else if let Some(key) = self.draft_key() {
- self.typing = true;
- self.focus = Focus::Chat;
- // A file dragged onto the terminal pastes its path: attach it (groups have no folder for files).
- match dropped_files(&s).filter(|_| self.bot().is_some_and(|b| !b.group)) {
- Some(paths) => self.files.entry(key).or_default().extend(paths),
- None => self.drafts.entry(key).or_default().insert(&s),
- }
- }
- }
-
- fn top_editor(&mut self) -> Option<&mut Editor> {
- match self.overlays.last_mut()? {
- Overlay::Goto(g) => Some(&mut g.query),
- Overlay::Help(e) => Some(e),
- Overlay::Agents(a) => Some(&mut a.query),
- Overlay::Folder(p) => Some(&mut p.query),
- Overlay::Group(g) => match g.field {
- GroupField::Name => Some(&mut g.name),
- GroupField::About => Some(&mut g.about),
- GroupField::Bots => None,
- },
- Overlay::Form(f) => match f.current() {
- Field::Name => Some(&mut f.name),
- Field::Instructions => Some(&mut f.instructions),
- Field::Model => Some(&mut f.model),
- _ => None,
- },
- Overlay::Market(m) => Some(&mut m.query),
- Overlay::Fields(f) => f.current().map(|i| &mut i.ed),
- Overlay::Routine(f) => Some(match f.field {
- super::manage::RoutineField::Name => &mut f.name,
- super::manage::RoutineField::Instruction => &mut f.instruction,
- super::manage::RoutineField::When => &mut f.when,
- }),
- _ => None,
- }
- }
-
- pub fn on_key(&mut self, k: KeyEvent) {
- if self.shell.is_some() {
- self.shell_key(k);
- return;
- }
- let ctrl = k.modifiers.contains(KeyModifiers::CONTROL);
- if ctrl && k.code == KeyCode::Char('k') {
- if matches!(self.overlays.last(), Some(Overlay::Goto(_))) {
- self.overlays.pop();
- } else {
- self.overlays.push(Overlay::Goto(Goto { query: Editor::default(), filter: 0, cursor: 0 }));
- }
- return;
- }
- if !self.overlays.is_empty() {
- self.overlay_key(k);
- return;
- }
- if self.typing {
- self.composer_key(k);
- return;
- }
- self.nav_key(k);
- }
-
- fn composer_key(&mut self, k: KeyEvent) {
- let (Some(id), Some(key)) = (self.selected.clone(), self.draft_key()) else {
- self.typing = false;
- return;
- };
- let ctrl = k.modifiers.contains(KeyModifiers::CONTROL);
- let newline = k.modifiers.intersects(KeyModifiers::SHIFT | KeyModifiers::ALT);
- let thread = self.thread.clone();
- let draft = self.drafts.entry(key.clone()).or_default();
- match k.code {
- KeyCode::Esc => self.typing = false,
- KeyCode::Enter if newline => draft.insert("\n"),
- KeyCode::Char('j') if ctrl => draft.insert("\n"),
- KeyCode::Enter => {
- let text = draft.text.trim().to_owned();
- let files = self.files.get(&key).cloned().unwrap_or_default();
- if text.is_empty() && files.is_empty() {
- return;
- }
- if let Some(m) = &self.mismatch {
- // The draft stays; it sends once the update is done.
- let why = mismatch_text(m, &self.host);
- self.flash(&why);
- return;
- }
- if self.sends.get(&key).is_some_and(|s| s.busy) {
- self.flash("Sending…");
- return;
- }
- let nonce = self
- .sends
- .get(&key)
- .filter(|s| s.text == text && s.files == files)
- .map_or_else(|| uuid::Uuid::new_v4().to_string(), |s| s.nonce.clone());
- self.sends.insert(
- key.clone(),
- PendingSend { text: text.clone(), files: files.clone(), nonce: nonce.clone(), busy: true },
- );
- self.chat_scroll = 0;
- let mut body = json!({"botId": id, "text": text, "clientNonce": nonce});
- if let Some(root) = thread {
- body["threadId"] = root.into();
- }
- let after = After::Sent(key);
- if files.is_empty() {
- self.call("send", body, after);
- } else {
- self.client.spawn_send_files(body, files, after, self.tx.clone());
- }
- self.flash("Sending… Your draft stays here until delivered.");
- }
- KeyCode::Backspace if draft.text.is_empty() => {
- if let Some(files) = self.files.get_mut(&key) {
- files.pop();
- }
- }
- KeyCode::Char('c') if ctrl => {
- if draft.text.is_empty() {
- self.flash("esc then s stops the bot");
- } else {
- draft.clear();
- }
- }
- KeyCode::Up if draft.text.is_empty() => self.scroll_chat(1),
- KeyCode::Down if draft.text.is_empty() => self.scroll_chat(-1),
- KeyCode::PageUp => self.scroll_chat(page(self.chat_height)),
- KeyCode::PageDown => self.scroll_chat(-page(self.chat_height)),
- KeyCode::Up => move_line(draft, -1),
- KeyCode::Down => move_line(draft, 1),
- _ => {
- draft.key(k);
- }
- }
- }
-
- fn sent(&mut self, key: &str, result: Result<Value, String>) {
- let Some(sent) = self.sends.get_mut(key) else { return };
- sent.busy = false;
- match result {
- Ok(value) => {
- if self.drafts.get(key).is_some_and(|d| d.text.trim() == sent.text)
- && self.files.get(key).map_or(&[][..], Vec::as_slice) == sent.files
- {
- self.drafts.remove(key);
- self.files.remove(key);
- }
- self.sends.remove(key);
- self.on_event(&json!({"type": "entry", "entry": value["entry"]}));
- self.flash("Sent");
- }
- Err(error) => self.flash(&format!("{error} Draft kept; Enter retries, Ctrl+C clears text.")),
- }
- }
-
- fn scroll_chat(&mut self, delta: isize) {
- self.chat_scroll = self.chat_scroll.saturating_add_signed(delta);
- }
-
- fn scroll_trace(&mut self, delta: isize) {
- self.trace_scroll = self.trace_scroll.saturating_add_signed(delta);
- }
-
- fn nav_key(&mut self, k: KeyEvent) {
- let ctrl = k.modifiers.contains(KeyModifiers::CONTROL);
- let half = page(self.chat_height) / 2;
- let narrow = self.width == Width::Narrow;
- let roster_page = narrow && !self.chat_page;
- if self.pick.is_some() && self.pick_key(k) {
- return;
- }
- match k.code {
- KeyCode::Char('c') if ctrl => self.flash("q quits"),
- KeyCode::Char('d') if ctrl => self.scroll_focused(-half),
- KeyCode::Char('u') if ctrl => self.scroll_focused(half),
- KeyCode::Char('q') => self.quit = true,
- KeyCode::Char('C') => self.open_connection(),
- KeyCode::Char('I') if self.error.is_some() && self.bots.is_empty() => self.install_host(),
- KeyCode::Char('?') => self.overlays.push(Overlay::Help(Editor::default())),
- KeyCode::Char('!') => self.jump(Mark::Need),
- KeyCode::Char('u') => self.jump(Mark::Unread),
- KeyCode::Char('[') => self.move_bot(-1),
- KeyCode::Char(']') => self.move_bot(1),
- KeyCode::Char(c @ '1'..='9') => {
- let i = (c as usize) - ('1' as usize);
- let pending = if roster_page { None } else { self.pending().map(|e| (e.id.clone(), e.options())) };
- match pending {
- Some((entry, opts)) if !opts.is_empty() => {
- if let Some((option, _, _)) = opts.get(i) {
- self.answer(&entry, option);
- }
- }
- _ => {
- if let Some(id) = self.roster().get(i).map(|b| b.id.clone()) {
- self.select(&id);
- }
- }
- }
- }
- KeyCode::Char(c @ ('y' | 'a' | 'n' | 'N')) if !roster_page && self.pending().is_some() => {
- self.answer_kind(c);
- }
- KeyCode::Char('n') => self.new_bot(),
- KeyCode::Char('m') => self.new_group(),
- KeyCode::Char('i') if !roster_page => self.start_typing(),
- KeyCode::Char('r') if !roster_page && self.selected.is_some() => {
- if self.thread.is_some() {
- self.start_typing();
- } else {
- self.chat_page = true;
- self.focus = Focus::Chat;
- self.step_pick(0);
- }
- }
- KeyCode::Enter => {
- if roster_page || self.focus == Focus::Roster {
- if self.selected.is_some() {
- self.chat_page = true;
- self.focus = Focus::Chat;
- self.start_typing();
- }
- } else {
- self.start_typing();
- }
- }
- KeyCode::Esc | KeyCode::Backspace => {
- if self.trace == TraceMode::Full {
- self.trace = TraceMode::Off;
- self.focus = Focus::Chat;
- } else if self.thread.is_some() {
- self.thread = None;
- self.chat_scroll = 0;
- self.trace_turn = None;
- self.trace_scroll = 0;
- } else if narrow && self.chat_page {
- self.chat_page = false;
- self.focus = Focus::Roster;
- } else if self.focus == Focus::Trace {
- self.focus = Focus::Chat;
- }
- }
- KeyCode::Tab | KeyCode::BackTab => self.cycle_focus(k.code == KeyCode::BackTab),
- KeyCode::Char('h') | KeyCode::Left => self.focus_side(-1),
- KeyCode::Char('l') | KeyCode::Right => self.focus_side(1),
- KeyCode::Char('j') | KeyCode::Down => self.line(1),
- KeyCode::Char('k') | KeyCode::Up => self.line(-1),
- KeyCode::PageDown => self.scroll_focused(-page(self.chat_height)),
- KeyCode::PageUp => self.scroll_focused(page(self.chat_height)),
- KeyCode::Char('g') | KeyCode::Home => {
- if self.focus == Focus::Trace {
- self.trace_scroll = 0;
- } else {
- self.chat_scroll = usize::MAX / 2;
- }
- }
- KeyCode::Char('G') | KeyCode::End => {
- if self.focus == Focus::Trace {
- self.trace_scroll = usize::MAX / 2;
- } else {
- self.chat_scroll = 0;
- }
- }
- KeyCode::Char('{') => self.step_turn(-1),
- KeyCode::Char('}') => self.step_turn(1),
- KeyCode::Char('t') => {
- self.trace = if self.trace == TraceMode::Pane { TraceMode::Off } else { TraceMode::Pane };
- if self.trace == TraceMode::Off && self.focus == Focus::Trace {
- self.focus = Focus::Chat;
- }
- if self.width == Width::Narrow || self.width == Width::Mid {
- self.trace = if self.trace == TraceMode::Off { TraceMode::Off } else { TraceMode::Full };
- }
- }
- KeyCode::Char('T') => {
- self.trace = if self.trace == TraceMode::Full { TraceMode::Off } else { TraceMode::Full };
- self.focus = if self.trace == TraceMode::Full { Focus::Trace } else { Focus::Chat };
- }
- KeyCode::Char('o') => {
- let Some(id) = self.selected.clone() else { return };
- // The newest turn in this lane that did something.
- self.trace_turn = self
- .lane(&id)
- .into_iter()
- .filter(|e| matches!(e.kind, Kind::Tool | Kind::Plan))
- .map(|e| e.turn)
- .max();
- self.trace_scroll = 0;
- if self.trace == TraceMode::Off {
- self.trace = if matches!(self.width, Width::Narrow | Width::Mid) {
- TraceMode::Full
- } else {
- TraceMode::Pane
- };
- }
- self.focus = Focus::Trace;
- }
- KeyCode::Char('b') => self.compact = !self.compact,
- KeyCode::Char('U') => self.overlays.push(Overlay::Usage),
- KeyCode::Char('M') => self.open_memory(),
- KeyCode::Char('R') => self.open_routines(),
- KeyCode::Char('A') => self.open_market(),
- KeyCode::Char('v') if !roster_page && self.selected.is_some() => {
- self.chat_page = true;
- self.focus = Focus::Chat;
- self.step_pick(0);
- }
- KeyCode::Char('P') => self.open_pair(),
- KeyCode::Char('s') => {
- if let Some(b) = self.bot() {
- if matches!(b.status, Status::Working | Status::NeedsInput) {
- let id = b.id.clone();
- self.call("stop", json!({"botId": id}), After::Nothing);
- self.flash("Stopping…");
- } else {
- self.flash("Not working right now");
- }
- }
- }
- KeyCode::Char('S') => {
- if self.bot().is_some_and(|b| b.group) {
- self.flash("A group has no session of its own");
- } else if let Some(b) = self.bot() {
- let c = Confirm {
- title: format!("Start a new session for {}?", b.name),
- detail: "The agent forgets this conversation's context.".into(),
- note: "The transcript stays here.".into(),
- button: "New session",
- act: ConfirmAct::NewSession(b.id.clone()),
- };
- self.overlays.push(Overlay::Confirm(c));
- }
- }
- KeyCode::Char('x') => {
- if let Some(b) = self.bot() {
- let (detail, note) = if b.group {
- ("Its conversation goes away.".into(), "Its bots stay.".into())
- } else {
- (
- "Its conversation and settings go away.".into(),
- format!("Files in {} stay where they are.", tilde(&b.cwd, &self.home)),
- )
- };
- let c = Confirm {
- title: format!("Delete {}?", b.name),
- detail,
- note,
- button: "Delete",
- act: ConfirmAct::Delete(b.id.clone()),
- };
- self.overlays.push(Overlay::Confirm(c));
- }
- }
- KeyCode::Char('e') => self.edit_bot(),
- KeyCode::Char('p') => {
- if let Some(b) = self.bot() {
- let (id, pinned) = (b.id.clone(), !b.pinned);
- self.call("updateBot", json!({"id": id, "pinned": pinned}), After::Nothing);
- }
- }
- KeyCode::Char('c') => self.copy_last(),
- _ => {}
- }
- }
-
- /// `codync-host install` with this same binary, for when nothing is listening yet.
- 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));
- });
- }
-
- 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;
- }
- }
- }
-
- 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),
- }
- }
-
- 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
- }
-
- 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 }];
- }
-
- 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];
- }
-
- 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 };
- }
- }
-
- 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()),
- );
- }
-
- 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 main chat's messages (none picked: the newest).
- fn step_pick(&mut self, d: isize) {
- let Some(id) = self.selected.clone() else { return };
- let ids: Vec<String> = self.lane(&id).into_iter().filter(|e| e.is_message()).map(|e| e.id.clone()).collect();
- if ids.is_empty() {
- self.pick = None;
- self.flash("No message to reply to yet");
- return;
- }
- let at = self.pick.as_ref().and_then(|p| ids.iter().position(|x| x == p));
- let i = at.map_or(ids.len() - 1, |i| i.saturating_add_signed(d).min(ids.len() - 1));
- self.pick = Some(ids[i].clone());
- }
-
- /// Keys while a message is picked. Returns whether the key was used.
- 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::Enter if picked.as_ref().is_some_and(|e| e.data["connectionRequest"]["status"] == "pending") => {
- self.open_connection();
- }
- KeyCode::Enter | KeyCode::Char('r') if self.thread.is_some() => {
- self.pick = None;
- self.start_typing();
- }
- KeyCode::Enter | KeyCode::Char('r') => {
- if let Some(root) = self.pick.take() {
- self.open_thread(root);
- self.start_typing();
- }
- }
- 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.data);
- }
- }
- KeyCode::Char('c') => {
- if let Some(e) = picked {
- copy(e.text());
- self.flash("Copied");
- }
- }
- KeyCode::Esc => self.pick = None,
- _ => return false,
- }
- true
- }
-
- fn open_thread(&mut self, root: String) {
- let Some(id) = self.selected.clone() else { return };
- self.pick = None;
- self.chat_scroll = 0;
- self.trace_turn = None;
- self.trace_scroll = 0;
- self.chat_page = true;
- self.focus = Focus::Chat;
- if self.trace == TraceMode::Full {
- self.trace = TraceMode::Off;
- }
- self.call("thread", json!({"botId": id, "rootId": root}), After::Thread);
- self.thread = Some(root);
- }
-
- 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");
- }
-
- fn open_pair(&mut self) {
- self.overlays.push(Overlay::Pair(None));
- self.call("pairing", json!({}), After::Pairing);
- }
-
- fn new_bot(&mut self) {
- if self.backends.is_empty() {
- self.call("hello", json!({}), After::Hello);
- }
- self.overlays.push(Overlay::Agents(AgentPicker { query: Editor::default(), cursor: 0 }));
- }
-
- fn new_group(&mut self) {
- self.overlays.push(Overlay::Group(GroupForm {
- group_id: None,
- name: Editor::default(),
- about: Editor::default(),
- members: vec![],
- field: GroupField::Bots,
- cursor: 0,
- error: None,
- saving: false,
- }));
- }
-
- /// Bots a group can hold: the roster's agents, plus hidden ones already in it.
- pub fn group_candidates(&self, members: &[String]) -> Vec<&Bot> {
- let mut v: Vec<&Bot> = self.roster().into_iter().filter(|b| !b.group).collect();
- v.extend(self.bots.values().filter(|b| b.hidden && !b.group && members.contains(&b.id)));
- v
- }
-
- /// "Alice, Bob" when no name is typed.
- pub fn group_default_name(&self, members: &[String]) -> String {
- let names: Vec<&str> = members.iter().filter_map(|m| self.bots.get(m)).map(|b| b.name.as_str()).collect();
- names.join(", ")
- }
-
- fn group_key(&mut self, mut g: GroupForm, k: KeyEvent) -> Option<Overlay> {
- let ctrl = k.modifiers.contains(KeyModifiers::CONTROL);
- let n = self.group_candidates(&g.members).len();
- match k.code {
- KeyCode::Esc => return None,
- KeyCode::Enter => self.save_group(&mut g),
- KeyCode::Char('s') if ctrl => self.save_group(&mut g),
- KeyCode::Tab => g.field = g.field.next(),
- KeyCode::BackTab => g.field = g.field.prev(),
- KeyCode::Down if g.field != GroupField::Bots => g.field = g.field.next(),
- KeyCode::Down => g.cursor = (g.cursor + 1).min(n.saturating_sub(1)),
- KeyCode::Up if g.field == GroupField::About => g.field = GroupField::Name,
- KeyCode::Up if g.field == GroupField::Bots && g.cursor == 0 => g.field = GroupField::About,
- KeyCode::Up if g.field == GroupField::Bots => g.cursor -= 1,
- KeyCode::Char(' ') if g.field == GroupField::Bots => {
- let id = self.group_candidates(&g.members).get(g.cursor).map(|b| b.id.clone());
- if let Some(id) = id {
- g.error = None;
- if let Some(i) = g.members.iter().position(|m| *m == id) {
- g.members.remove(i);
- } else {
- g.members.push(id);
- }
- }
- }
- _ if g.field == GroupField::Name => {
- g.name.key(k);
- }
- _ if g.field == GroupField::About => {
- g.about.key(k);
- }
- _ => {}
- }
- Some(Overlay::Group(g))
- }
-
- fn save_group(&mut self, g: &mut GroupForm) {
- if g.saving {
- return;
- }
- if g.members.is_empty() {
- g.error = Some("Pick at least one bot.".into());
- return;
- }
- let typed = g.name.text.trim();
- let name = if typed.is_empty() { self.group_default_name(&g.members) } else { typed.to_owned() };
- let mut body = json!({"name": name, "members": g.members, "description": g.about.text.trim()});
- g.saving = true;
- g.error = None;
- if let Some(id) = &g.group_id {
- body["id"] = id.clone().into();
- self.call("updateBot", body, After::Saved);
- } else {
- body["id"] = "".into();
- body["kind"] = "group".into();
- self.call("createBot", body, After::Saved);
- }
- }
-
- fn edit_bot(&mut self) {
- let Some(b) = self.bot().cloned() else { return };
- if b.group {
- self.overlays.push(Overlay::Group(GroupForm {
- group_id: Some(b.id.clone()),
- name: Editor::with(&b.name),
- about: Editor::with(&b.description),
- members: b.members.clone(),
- field: GroupField::Name,
- cursor: 0,
- error: None,
- saving: false,
- }));
- return;
- }
- let mut f = Form {
- bot_id: Some(b.id.clone()),
- name: Editor::with(&b.name),
- instructions: Editor::with(&b.description),
- backend: b.backend.clone(),
- model: Editor::with(b.model.as_deref().unwrap_or_default()),
- cwd: if b.managed_workspace { String::new() } else { b.cwd.clone() },
- auto: b.auto,
- notify: b.notify,
- computer: b.computer,
- color: COLORS.iter().position(|c| *c == b.color).unwrap_or(7),
- shape: SHAPES.iter().position(|s| *s == b.shape).unwrap_or(0),
- connectors: b.connectors.iter().map(|id| Toggle { id: id.clone(), name: id.clone(), on: true }).collect(),
- skills: b.skills.iter().map(|id| Toggle { id: id.clone(), name: id.clone(), on: true }).collect(),
- field: 0,
- sub: 0,
- error: None,
- saving: false,
- };
- merge_toggles(&mut f.connectors, &self.market.0, false);
- merge_toggles(&mut f.skills, &self.market.1, false);
- self.want_models(&b.backend);
- self.overlays.push(Overlay::Form(Box::new(f)));
- self.call("connectors", json!({}), After::Connectors);
- self.call("skills", json!({}), After::Skills);
- }
-
- /// Agents offered for a new bot: runnable ones on this computer first.
- pub fn agent_choices(&self, query: &str) -> Vec<&Value> {
- let mut v: Vec<&Value> = self
- .backends
- .iter()
- .filter(|b| b["available"].as_bool().unwrap_or(false) || b["registry"].is_string())
- .filter(|b| fuzzy(query, &[b["name"].as_str().unwrap_or_default(), b["id"].as_str().unwrap_or_default()]))
- .collect();
- v.sort_by_key(|b| !b["installed"].as_bool().unwrap_or(false));
- v
- }
-
- pub fn goto_items(&self, g: &Goto) -> Vec<GotoItem> {
- let q = g.query.text.trim();
- let want = FILTERS[g.filter].1;
- let mut bots: Vec<&Bot> = self
- .bots
- .values()
- .filter(|b| want.is_none_or(|m| b.mark() == m))
- .filter(|b| fuzzy(q, &[&b.name, &b.backend]) || contains(q, &[&b.cwd, &b.last_message]))
- .collect();
- bots.sort_by(|a, b| {
- b.mark().priority().cmp(&a.mark().priority()).then(b.last_at.cmp(&a.last_at)).then(a.name.cmp(&b.name))
- });
- let mut items: Vec<GotoItem> = bots.into_iter().map(|b| GotoItem::Bot(b.id.clone())).collect();
- if want.is_none() {
- items.extend(
- ACTIONS.iter().filter(|(label, _, _)| fuzzy(q, &[label])).map(|(_, _, a)| GotoItem::Action(*a)),
- );
- }
- items
- }
-
- fn run_action(&mut self, a: Action) {
- match a {
- Action::NewBot => self.new_bot(),
- Action::NewGroup => self.new_group(),
- Action::Market => self.open_market(),
- Action::Memory => self.open_memory(),
- Action::Routines => self.open_routines(),
- Action::RefreshAgents => self.call("refreshBackends", json!({}), After::Backends),
- Action::Pair => self.open_pair(),
- Action::Usage => self.overlays.push(Overlay::Usage),
- Action::CheckUpdate => self.call("checkHostUpdate", json!({}), After::Update),
- Action::Keys => self.overlays.push(Overlay::Help(Editor::default())),
- }
- }
-
- fn overlay_key(&mut self, k: KeyEvent) {
- let Some(top) = self.overlays.pop() else { return };
- let depth = self.overlays.len();
- let keep = match top {
- Overlay::Goto(g) => self.goto_key(g, k),
- Overlay::Help(mut e) => {
- if k.code == KeyCode::Esc || k.code == KeyCode::Char('?') && e.text.is_empty() {
- None
- } else {
- if k.code != KeyCode::Char('/') || !e.text.is_empty() {
- e.key(k);
- }
- Some(Overlay::Help(e))
- }
- }
- Overlay::Confirm(c) => match k.code {
- KeyCode::Enter => {
- let changed = After::Sheet(Reply::Changed);
- match &c.act {
- ConfirmAct::Delete(id) => self.call("deleteBot", json!({"botId": id}), After::Nothing),
- ConfirmAct::NewSession(id) => self.call("newSession", json!({"botId": id}), After::Nothing),
- ConfirmAct::ClearMemory(id) => self.call("clearMemory", json!({"botId": id}), changed),
- ConfirmAct::DeleteRoutine(bot, id) => {
- self.call("deleteRoutine", json!({"botId": bot, "id": id}), changed);
- }
- ConfirmAct::RotateRoutineKey(bot, id) => self.call(
- "routineWebhook",
- json!({"botId": bot, "id": id, "rotate": true}),
- After::Sheet(Reply::Webhook),
- ),
- ConfirmAct::RemoveConnector(id) => self.call("removeConnector", json!({"id": id}), changed),
- ConfirmAct::RemoveSkill(id) => self.call("removeSkill", json!({"id": id}), changed),
- }
- None
- }
- KeyCode::Esc | KeyCode::Char('q') => None,
- _ => Some(Overlay::Confirm(c)),
- },
- Overlay::Agents(a) => self.agents_key(a, k),
- Overlay::Folder(p) => self.folder_key(p, k),
- Overlay::Form(f) => self.form_key(f, k),
- Overlay::Group(g) => self.group_key(g, k),
- Overlay::Memory(m) => self.memory_key(m, k),
- Overlay::Routines(l) => self.routines_key(l, k),
- Overlay::Routine(f) => self.routine_key(f, k),
- Overlay::Market(m) => self.market_key(m, k),
- Overlay::Agent(a) => self.agent_key(a, k),
- Overlay::Fields(f) => self.fields_key(f, k),
- o @ (Overlay::Usage | Overlay::Pair(_)) => {
- if matches!(k.code, KeyCode::Esc | KeyCode::Enter | KeyCode::Char('q' | 'U' | 'P')) {
- None
- } else {
- Some(o)
- }
- }
- };
- if let Some(o) = keep {
- // Anything the handler opened stays on top of the overlay it came from.
- self.overlays.insert(depth.min(self.overlays.len()), o);
- }
- }
-
- fn goto_key(&mut self, mut g: Goto, k: KeyEvent) -> Option<Overlay> {
- let items_len = self.goto_items(&g).len();
- match k.code {
- KeyCode::Esc => return None,
- KeyCode::Tab => {
- g.filter = (g.filter + 1) % FILTERS.len();
- g.cursor = 0;
- }
- KeyCode::BackTab => {
- g.filter = (g.filter + FILTERS.len() - 1) % FILTERS.len();
- g.cursor = 0;
- }
- KeyCode::Down => g.cursor = (g.cursor + 1).min(items_len.saturating_sub(1)),
- KeyCode::Up => g.cursor = g.cursor.saturating_sub(1),
- KeyCode::Char('n' | 'j') if k.modifiers.contains(KeyModifiers::CONTROL) => {
- g.cursor = (g.cursor + 1).min(items_len.saturating_sub(1));
- }
- KeyCode::Char('p') if k.modifiers.contains(KeyModifiers::CONTROL) => g.cursor = g.cursor.saturating_sub(1),
- KeyCode::Enter => {
- let items = self.goto_items(&g);
- match items.get(g.cursor) {
- Some(GotoItem::Bot(id)) => {
- let id = id.clone();
- self.select(&id);
- self.start_typing();
- }
- Some(GotoItem::Action(a)) => self.run_action(*a),
- None => {}
- }
- return None;
- }
- _ => {
- if g.query.key(k) {
- g.cursor = 0;
- }
- }
- }
- Some(Overlay::Goto(g))
- }
-
- /// The agent picker's last row, "New group chat", when the filter matches it.
- pub fn agent_group_row(query: &str) -> bool {
- fuzzy(query, &[NEW_GROUP])
- }
-
- fn agents_key(&mut self, mut a: AgentPicker, k: KeyEvent) -> Option<Overlay> {
- let agents = self.agent_choices(&a.query.text).len();
- let n = agents + usize::from(Self::agent_group_row(&a.query.text));
- match k.code {
- KeyCode::Esc => return None,
- KeyCode::Down => a.cursor = (a.cursor + 1).min(n.saturating_sub(1)),
- KeyCode::Up => a.cursor = a.cursor.saturating_sub(1),
- KeyCode::Enter if a.cursor == agents && Self::agent_group_row(&a.query.text) => {
- self.new_group();
- return None;
- }
- KeyCode::Enter => {
- let backend =
- self.agent_choices(&a.query.text).get(a.cursor).and_then(|b| b["id"].as_str()).map(str::to_owned);
- if let Some(backend) = backend {
- self.overlays.push(Overlay::Agents(a));
- self.new_bot_form(backend);
- return None;
- }
- }
- _ => {
- if a.query.key(k) {
- a.cursor = 0;
- }
- }
- }
- Some(Overlay::Agents(a))
- }
-
- fn open_folder(&mut self, start: &str) {
- self.overlays.push(Overlay::Folder(FolderPicker {
- path: start.to_owned(),
- parent: None,
- dirs: vec![],
- query: Editor::default(),
- cursor: 0,
- error: None,
- }));
- let body = if start.is_empty() { json!({}) } else { json!({"path": start}) };
- self.call("listDirs", body, After::Dirs);
- }
-
- pub fn folder_matches(p: &FolderPicker) -> Vec<&Dir> {
- p.dirs.iter().filter(|d| fuzzy(&p.query.text, &[&d.name])).collect()
- }
-
- fn folder_key(&mut self, mut p: FolderPicker, k: KeyEvent) -> Option<Overlay> {
- let matches: Vec<Dir> = Self::folder_matches(&p).into_iter().cloned().collect();
- match k.code {
- KeyCode::Esc => return None,
- KeyCode::Down => p.cursor = (p.cursor + 1).min(matches.len().saturating_sub(1)),
- KeyCode::Up => p.cursor = p.cursor.saturating_sub(1),
- KeyCode::Right | KeyCode::Tab => {
- if let Some(d) = matches.get(p.cursor) {
- let path = d.path.clone();
- self.call("listDirs", json!({"path": path}), After::Dirs);
- }
- }
- KeyCode::Left | KeyCode::Backspace if p.query.text.is_empty() => {
- if let Some(parent) = p.parent.clone() {
- self.call("listDirs", json!({"path": parent}), After::Dirs);
- }
- }
- KeyCode::Enter => {
- // Enter on a highlighted child uses it; with nothing typed and no match, the current folder.
- let chosen = if p.query.text.is_empty() && k.modifiers.contains(KeyModifiers::ALT) {
- p.path.clone()
- } else {
- matches.get(p.cursor).map_or_else(|| p.path.clone(), |d| d.path.clone())
- };
- self.folder_chosen(chosen);
- return None;
- }
- KeyCode::Char('.') if p.query.text.is_empty() => {
- let chosen = p.path.clone();
- self.folder_chosen(chosen);
- return None;
- }
- _ => {
- if p.query.key(k) {
- p.cursor = 0;
- }
- }
- }
- Some(Overlay::Folder(p))
- }
-
- fn folder_chosen(&mut self, path: String) {
- if let Some(Overlay::Form(f)) = self.overlays.last_mut() {
- f.cwd = path;
- }
- }
-
- /// Step 2 of a new bot: name and looks, prefilled, in a personal workspace.
- fn new_bot_form(&mut self, backend: String) {
- if let Some(i) = self.overlays.iter().rposition(|o| matches!(o, Overlay::Agents(_))) {
- self.overlays.truncate(i);
- }
- let n = self.bots.len() + usize::try_from(crate::store::now_ms() % 97).unwrap_or(0);
- let name = NAMES[n % NAMES.len()].to_owned();
- let f = Form {
- bot_id: None,
- name: Editor::with(&name),
- instructions: Editor::default(),
- backend,
- model: Editor::default(),
- cwd: String::new(),
- auto: true,
- notify: true,
- computer: false,
- color: (n * 7) % COLORS.len(),
- shape: n % SHAPES.len(),
- connectors: self
- .market
- .0
- .iter()
- .map(|t| Toggle { id: t.id.clone(), name: t.name.clone(), on: true })
- .collect(),
- skills: self
- .market
- .1
- .iter()
- .map(|t| Toggle { id: t.id.clone(), name: t.name.clone(), on: false })
- .collect(),
- field: 0,
- sub: 0,
- error: None,
- saving: false,
- };
- self.want_models(&f.backend);
- self.overlays.push(Overlay::Form(Box::new(f)));
- self.call("connectors", json!({}), After::Connectors);
- self.call("skills", json!({}), After::Skills);
- }
-
- fn form_key(&mut self, mut f: Box<Form>, k: KeyEvent) -> Option<Overlay> {
- let ctrl = k.modifiers.contains(KeyModifiers::CONTROL);
- let field = f.current();
- match k.code {
- KeyCode::Esc => return None,
- KeyCode::Char('s') if ctrl => {
- self.save_form(&mut f);
- }
- KeyCode::Tab | KeyCode::Down => {
- f.field = (f.field + 1) % FIELDS.len();
- f.sub = 0;
- }
- KeyCode::BackTab | KeyCode::Up => {
- f.field = (f.field + FIELDS.len() - 1) % FIELDS.len();
- f.sub = 0;
- }
- KeyCode::Enter
- if field == Field::Instructions && k.modifiers.intersects(KeyModifiers::SHIFT | KeyModifiers::ALT) =>
- {
- f.instructions.insert("\n");
- }
- KeyCode::Char('j') if ctrl && field == Field::Instructions => f.instructions.insert("\n"),
- KeyCode::Backspace | KeyCode::Delete if field == Field::Folder => f.cwd.clear(),
- KeyCode::Enter if field == Field::Folder => {
- let start = f.cwd.clone();
- self.overlays.push(Overlay::Form(f));
- self.open_folder(&start);
- return None;
- }
- KeyCode::Enter if field == Field::Agent => {
- let backend = f.backend.clone();
- self.overlays.push(Overlay::Form(f));
- self.open_agent(&backend);
- return None;
- }
- KeyCode::Left | KeyCode::Right if field == Field::Model => {
- let d: isize = if k.code == KeyCode::Left { -1 } else { 1 };
- self.want_models(&f.backend);
- if let Some(m) = self.cycle_model(&f.backend, f.model.text.trim(), d) {
- f.model = Editor::with(&m);
- }
- }
- KeyCode::Enter => self.save_form(&mut f),
- KeyCode::Left | KeyCode::Right | KeyCode::Char(' ')
- if matches!(
- field,
- Field::Agent
- | Field::Approvals
- | Field::Notify
- | Field::Computer
- | Field::Color
- | Field::Shape
- | Field::Connectors
- | Field::Skills
- ) =>
- {
- let d: isize = if k.code == KeyCode::Left { -1 } else { 1 };
- let space = k.code == KeyCode::Char(' ');
- match field {
- Field::Agent if !space => {
- let ids: Vec<String> =
- self.agent_choices("").iter().filter_map(|b| b["id"].as_str().map(str::to_owned)).collect();
- if !ids.is_empty() {
- let i = ids.iter().position(|x| *x == f.backend).unwrap_or(0);
- f.backend.clone_from(&ids[cycle(i, d, ids.len())]);
- // Another agent's model ids mean nothing here.
- f.model.clear();
- self.want_models(&f.backend);
- }
- }
- Field::Approvals => f.auto = !f.auto,
- Field::Notify => f.notify = !f.notify,
- Field::Computer => f.computer = !f.computer,
- Field::Color if !space => f.color = cycle(f.color, d, COLORS.len()),
- Field::Shape if !space => f.shape = cycle(f.shape, d, SHAPES.len()),
- Field::Connectors | Field::Skills => {
- let list = if field == Field::Connectors { &mut f.connectors } else { &mut f.skills };
- if space {
- if let Some(t) = list.get_mut(f.sub) {
- t.on = !t.on;
- }
- } else if !list.is_empty() {
- f.sub = cycle(f.sub, d, list.len());
- }
- }
- _ => {}
- }
- }
- _ => {
- let ed = match field {
- Field::Name => Some(&mut f.name),
- Field::Instructions => Some(&mut f.instructions),
- Field::Model => Some(&mut f.model),
- _ => None,
- };
- if let Some(ed) = ed {
- ed.key(k);
- }
- }
- }
- Some(Overlay::Form(f))
- }
-
- fn save_form(&mut self, f: &mut Form) {
- if f.saving {
- return;
- }
- if f.bot_id.is_some() && f.name.text.trim().is_empty() {
- f.error = Some("Give the bot a name.".into());
- return;
- }
- let model = f.model.text.trim();
- let mut body = json!({
- "name": f.name.text.trim(),
- "description": f.instructions.text.trim(),
- "backend": f.backend,
- "cwd": f.cwd,
- "permission": if f.auto { "auto" } else { "ask" },
- "model": if model.is_empty() { Value::Null } else { model.into() },
- "notify": f.notify,
- "computer": f.computer,
- "avatarColor": COLORS[f.color],
- "avatarShape": SHAPES[f.shape],
- "connectors": f.connectors.iter().filter(|t| t.on).map(|t| t.id.clone()).collect::<Vec<_>>(),
- "skills": f.skills.iter().filter(|t| t.on).map(|t| t.id.clone()).collect::<Vec<_>>(),
- });
- f.saving = true;
- f.error = None;
- match &f.bot_id {
- Some(id) => {
- body["id"] = id.clone().into();
- self.call("updateBot", body, After::Saved);
- }
- None => self.call("createBot", body, After::Saved),
- }
- }
-
- // ---------- mouse ----------
-
- pub fn on_mouse(&mut self, m: MouseEvent) {
- let at = Position::new(m.column, m.row);
- match m.kind {
- MouseEventKind::ScrollUp | MouseEventKind::ScrollDown => {
- let d: isize = if m.kind == MouseEventKind::ScrollUp { -3 } else { 3 };
- if self.hits.trace.contains(at) {
- self.scroll_trace(d);
- } else if self.hits.chat.contains(at) {
- self.scroll_chat(-d);
- } else if self.hits.roster.contains(at) {
- self.move_bot(d.signum());
- }
- }
- MouseEventKind::Down(MouseButton::Left) => {
- let hit = self.hits.clicks.iter().rev().find(|(r, _)| r.contains(at)).map(|(_, c)| c.clone());
- match hit {
- Some(Click::Bot(id)) => {
- self.overlays.clear();
- self.select(&id);
- self.focus = Focus::Roster;
- if self.width == Width::Narrow {
- self.chat_page = true;
- self.focus = Focus::Chat;
- }
- }
- Some(Click::Option { entry, option }) => self.answer(&entry, &option),
- Some(Click::Filter(i)) => {
- if let Some(Overlay::Goto(g)) = self.overlays.last_mut() {
- g.filter = i;
- g.cursor = 0;
- } else {
- self.overlays.push(Overlay::Goto(Goto { query: Editor::default(), filter: i, cursor: 0 }));
- }
- }
- Some(Click::Composer) => self.start_typing(),
- Some(Click::Thread(root)) => self.open_thread(root),
- None => {
- if self.overlays.is_empty() {
- if self.hits.trace.contains(at) {
- self.focus = Focus::Trace;
- } else if self.hits.chat.contains(at) {
- self.focus = Focus::Chat;
- } else if self.hits.roster.contains(at) {
- self.focus = Focus::Roster;
- }
- }
- }
- }
- }
- _ => {}
- }
- }
-}
-
-const NAMES: [&str; 24] = [
- "Ada", "Rex", "Mia", "Sol", "Kit", "Oli", "Ivy", "Max", "Zoe", "Leo", "Ari", "Bea", "Cal", "Dot", "Eli", "Fay",
- "Gus", "Hal", "Ida", "Jin", "Kai", "Lux", "Nia", "Otto",
-];
-
-/// What to update, in the words every client uses (docs/reference/compatibility.md).
-pub fn mismatch_text(m: &crate::compat::Mismatch, host: &str) -> String {
- let host = if host.is_empty() { "the computer" } else { host };
- match m {
- crate::compat::Mismatch::UpdateApp { minimum } => {
- format!("Update this app: {host} needs Codync {minimum} or newer here.")
- }
- crate::compat::Mismatch::UpdateHost { version, minimum } => format!(
- "Update Codync on {host}: it runs {version}, this app needs {minimum} or newer. ^k → Check for updates."
- ),
- }
-}
-
-/// OSC 52: the terminal puts it on the clipboard, over SSH too.
-pub fn copy(text: &str) {
- let mut out = std::io::stdout();
- let _ = write!(out, "\x1b]52;c;{}\x07", base64::Engine::encode(&base64::engine::general_purpose::STANDARD, text));
- let _ = out.flush();
-}
-
-fn page(h: usize) -> isize {
- isize::try_from(h.max(4)).unwrap_or(20) - 2
-}
-
-pub(super) fn cycle(i: usize, d: isize, n: usize) -> usize {
- let n = isize::try_from(n).unwrap_or(1).max(1);
- usize::try_from((isize::try_from(i).unwrap_or(0) + d).rem_euclid(n)).unwrap_or(0)
-}
-
-fn merge_toggles(have: &mut Vec<Toggle>, all: &[Toggle], on: bool) {
- for t in all {
- match have.iter_mut().find(|h| h.id == t.id) {
- Some(h) => h.name.clone_from(&t.name),
- None => have.push(Toggle { id: t.id.clone(), name: t.name.clone(), on }),
- }
- }
-}
-
-fn move_line(e: &mut Editor, d: isize) {
- let start = e.text[..e.cursor].rfind('\n').map_or(0, |i| i + 1);
- let col = e.text[start..e.cursor].chars().count();
- let target_start = if d < 0 {
- if start == 0 {
- return;
- }
- e.text[..start - 1].rfind('\n').map_or(0, |i| i + 1)
- } else {
- match e.text[e.cursor..].find('\n') {
- Some(i) => e.cursor + i + 1,
- None => return,
- }
- };
- let line_end = e.text[target_start..].find('\n').map_or(e.text.len(), |i| target_start + i);
- e.cursor = e.text[target_start..line_end].char_indices().nth(col).map_or(line_end, |(i, _)| target_start + i);
-}
-
-/// Case-insensitive substring match: for long fields where a subsequence matches almost anything.
-fn contains(query: &str, fields: &[&str]) -> bool {
- let q = query.to_lowercase();
- fields.iter().any(|f| f.to_lowercase().contains(&q))
-}
-
-/// Subsequence match, case-insensitive, over any of the fields.
-pub fn fuzzy(query: &str, fields: &[&str]) -> bool {
- let q: Vec<char> = query.to_lowercase().chars().filter(|c| !c.is_whitespace()).collect();
- if q.is_empty() {
- return true;
- }
- fields.iter().any(|f| {
- let mut it = q.iter().peekable();
- for c in f.to_lowercase().chars() {
- if it.peek() == Some(&&c) {
- it.next();
- }
- }
- it.peek().is_none()
- })
-}
-
-/// Paths a terminal pastes for dropped files (shell-escaped, quoted or `file://`), when every one is a file.
-fn dropped_files(s: &str) -> Option<Vec<std::path::PathBuf>> {
- let paths: Vec<std::path::PathBuf> = shlex::split(s.trim())?
- .into_iter()
- .map(|p| match p.strip_prefix("file://") {
- Some(url) => url.replace("%20", " "),
- None => p,
- })
- .map(std::path::PathBuf::from)
- .collect();
- (!paths.is_empty() && paths.iter().all(|p| p.is_absolute() && p.is_file())).then_some(paths)
-}
-
-pub fn tilde(path: &str, home: &str) -> String {
- match path.strip_prefix(home) {
- Some(rest) if !home.is_empty() => format!("~{rest}"),
- _ => path.to_owned(),
- }
-}
-
-#[cfg(test)]
-mod tests {
- use super::*;
-
- fn test_app() -> App {
- let (tx, _) = tokio::sync::mpsc::unbounded_channel();
- App::new(Client::new("http://127.0.0.1:1", None), tx, String::new())
- }
-
- #[test]
- fn send_failure_keeps_text_files_and_nonce_for_retry() {
- let mut app = test_app();
- let key = "bot/thread";
- let files = vec![std::path::PathBuf::from("/tmp/report.txt")];
- app.drafts.insert(key.into(), Editor::with("draft"));
- app.files.insert(key.into(), files.clone());
- app.sends.insert(
- key.into(),
- PendingSend { text: "draft".into(), files: files.clone(), nonce: "same-nonce".into(), busy: true },
- );
- app.on_reply(After::Sent(key.into()), Err("Disconnected".into()));
- assert_eq!(app.drafts[key].text, "draft");
- assert_eq!(app.files[key], files);
- assert_eq!(app.sends[key].nonce, "same-nonce");
- assert!(!app.sends[key].busy);
- app.on_reply(After::Sent(key.into()), Ok(json!({})));
- assert!(!app.drafts.contains_key(key));
- assert!(!app.files.contains_key(key));
- }
-
- #[test]
- fn late_send_success_does_not_erase_a_new_draft() {
- let mut app = test_app();
- app.drafts.insert("bot".into(), Editor::with("new draft"));
- app.sends.insert(
- "bot".into(),
- PendingSend { text: "old draft".into(), files: vec![], nonce: "n".into(), busy: true },
- );
- app.on_reply(After::Sent("bot".into()), Ok(json!({})));
- assert_eq!(app.drafts["bot"].text, "new draft");
- }
-
- #[test]
- fn computer_permission_survives_loading_the_editor() {
- let mut app = test_app();
- app.upsert_bot(&json!({"id":"b", "computer":true}));
- assert!(app.bots["b"].computer);
- assert!(FIELDS.contains(&Field::Computer));
- }
-
- #[test]
- fn dropped_paths_are_files_only() {
- let dir = std::env::temp_dir().join(format!("codync-drop-{}", std::process::id()));
- std::fs::create_dir_all(&dir).expect("temp dir");
- let a = dir.join("a b.txt");
- std::fs::write(&a, "x").expect("write");
- let esc = a.to_string_lossy().replace(' ', "\\ ");
- assert_eq!(dropped_files(&esc), Some(vec![a.clone()]));
- assert_eq!(dropped_files(&format!("'{}'", a.display())), Some(vec![a.clone()]));
- assert_eq!(dropped_files(&format!("file://{}", a.to_string_lossy().replace(' ', "%20"))), Some(vec![a]));
- assert_eq!(dropped_files(&dir.to_string_lossy()), None);
- assert_eq!(dropped_files("hello world"), None);
- std::fs::remove_dir_all(&dir).expect("cleanup");
- }
-
- #[test]
- fn fuzzy_is_a_subsequence_match() {
- assert!(fuzzy("rx", &["Rex"]));
- assert!(fuzzy("", &["anything"]));
- assert!(!fuzzy("xr", &["Rex"]));
- assert!(fuzzy("web", &["Rex", "~/mycode/web"]));
- }
-
- #[test]
- fn editor_words_and_cuts() {
- let mut e = Editor::with("hello brave world");
- e.key(KeyEvent::new(KeyCode::Char('w'), KeyModifiers::CONTROL));
- assert_eq!(e.text, "hello brave ");
- e.key(KeyEvent::new(KeyCode::Char('a'), KeyModifiers::CONTROL));
- assert_eq!(e.cursor, 0);
- e.key(KeyEvent::new(KeyCode::Char('你'), KeyModifiers::NONE));
- assert_eq!(e.text, "你hello brave ");
- e.key(KeyEvent::new(KeyCode::Backspace, KeyModifiers::NONE));
- assert_eq!(e.text, "hello brave ");
- }
-
- #[test]
- fn permission_options_follow_app_order() {
- let e = Entry {
- id: "p".into(),
- seq: 1,
- turn: 1,
- kind: Kind::Permission,
- data: json!({"status": "pending", "options": [
- {"optionId": "r", "name": "Reject", "kind": "reject_once"},
- {"optionId": "a", "name": "Allow", "kind": "allow_once"},
- ]}),
- created_at: 0,
- thread_id: None,
- };
- assert!(e.pending());
- let kinds: Vec<String> = e.options().into_iter().map(|o| o.2).collect();
- assert_eq!(kinds, ["allow_once", "reject_once"]);
- }
-
- #[test]
- fn lanes_split_the_chat_from_its_threads() {
- let g = Bot::parse(&json!({"id": "g", "kind": "group", "members": ["a", "b"], "workingThread": "r"}))
- .expect("a bot with an id parses");
- assert!(g.group && g.members == ["a", "b"]);
- assert!(g.works_in(Some("r")) && !g.works_in(None));
- let (tx, _rx) = tokio::sync::mpsc::unbounded_channel();
- let mut app = App::new(Client::new("http://127.0.0.1:1", None), tx, String::new());
- for (seq, thread) in [(1, Value::Null), (2, json!("r")), (3, Value::Null)] {
- let v = json!({"botId": "g", "id": format!("e{seq}"), "seq": seq, "turn": seq, "kind": "user", "threadId": thread});
- let (bot, e) = Entry::parse(&v).expect("a full entry parses");
- app.entries.entry(bot).or_default().insert(e.seq, e);
- }
- let ids = |app: &App| app.lane("g").iter().map(|e| e.id.clone()).collect::<Vec<_>>();
- assert_eq!(ids(&app), ["e1", "e3"]);
- assert_eq!(app.lane_turns("g"), [1, 3]);
- app.thread = Some("r".into());
- assert_eq!(ids(&app), ["e2"]);
- assert_eq!(app.lane_turns("g"), [2]);
- }
-
- #[test]
- fn line_moves_keep_the_column() {
- let mut e = Editor::with("abc\nde");
- move_line(&mut e, -1);
- assert_eq!(e.cursor, 2);
- move_line(&mut e, 1);
- assert_eq!(e.cursor, 6);
- }
-}
diff --git a/host/src/tui/app/editor.rs b/host/src/tui/app/editor.rs
new file mode 100644
index 00000000..902cbaed
--- /dev/null
+++ b/host/src/tui/app/editor.rs
@@ -0,0 +1,149 @@
+//! The text field every composer and sheet types into.
+
+use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
+
+/// A one-line-or-more text field with readline-style editing.
+#[derive(Clone, Default, Debug)]
+pub struct Editor {
+ pub text: String,
+ /// Byte offset, always on a char boundary.
+ pub cursor: usize,
+}
+
+impl Editor {
+ pub fn with(text: &str) -> Self {
+ Self { text: text.to_owned(), cursor: text.len() }
+ }
+
+ pub fn clear(&mut self) {
+ self.text.clear();
+ self.cursor = 0;
+ }
+
+ pub fn insert(&mut self, s: &str) {
+ self.text.insert_str(self.cursor, s);
+ self.cursor += s.len();
+ }
+
+ fn prev(&self, i: usize) -> usize {
+ self.text[..i].char_indices().next_back().map_or(0, |(j, _)| j)
+ }
+
+ fn next(&self, i: usize) -> usize {
+ self.text[i..].chars().next().map_or(i, |c| i + c.len_utf8())
+ }
+
+ fn word_left(&self) -> usize {
+ let mut i = self.cursor;
+ while i > 0 && self.text[..i].ends_with(char::is_whitespace) {
+ i = self.prev(i);
+ }
+ while i > 0 && !self.text[..i].ends_with(char::is_whitespace) {
+ i = self.prev(i);
+ }
+ i
+ }
+
+ fn word_right(&self) -> usize {
+ let mut i = self.cursor;
+ while i < self.text.len() && self.text[i..].starts_with(char::is_whitespace) {
+ i = self.next(i);
+ }
+ while i < self.text.len() && !self.text[i..].starts_with(char::is_whitespace) {
+ i = self.next(i);
+ }
+ i
+ }
+
+ fn line_start(&self) -> usize {
+ self.text[..self.cursor].rfind('\n').map_or(0, |i| i + 1)
+ }
+
+ fn line_end(&self) -> usize {
+ self.text[self.cursor..].find('\n').map_or(self.text.len(), |i| self.cursor + i)
+ }
+
+ /// Editing keys shared by every text field. Returns whether the key was used.
+ pub fn key(&mut self, k: KeyEvent) -> bool {
+ let ctrl = k.modifiers.contains(KeyModifiers::CONTROL);
+ let alt = k.modifiers.contains(KeyModifiers::ALT);
+ match k.code {
+ KeyCode::Char('a') if ctrl => self.cursor = self.line_start(),
+ KeyCode::Char('e') if ctrl => self.cursor = self.line_end(),
+ KeyCode::Char('b') if ctrl => self.cursor = self.prev(self.cursor),
+ KeyCode::Char('f') if ctrl => self.cursor = self.next(self.cursor),
+ KeyCode::Char('b') if alt => self.cursor = self.word_left(),
+ KeyCode::Char('f') if alt => self.cursor = self.word_right(),
+ KeyCode::Char('w') if ctrl => self.cut(self.word_left(), self.cursor),
+ KeyCode::Backspace if alt || ctrl => self.cut(self.word_left(), self.cursor),
+ KeyCode::Char('d') if alt => self.cut(self.cursor, self.word_right()),
+ KeyCode::Char('u') if ctrl => self.cut(self.line_start(), self.cursor),
+ KeyCode::Char('h') if ctrl => self.cut(self.prev(self.cursor), self.cursor),
+ KeyCode::Char('d') if ctrl => self.cut(self.cursor, self.next(self.cursor)),
+ KeyCode::Left if alt || ctrl => self.cursor = self.word_left(),
+ KeyCode::Right if alt || ctrl => self.cursor = self.word_right(),
+ KeyCode::Left => self.cursor = self.prev(self.cursor),
+ KeyCode::Right => self.cursor = self.next(self.cursor),
+ KeyCode::Home => self.cursor = self.line_start(),
+ KeyCode::End => self.cursor = self.line_end(),
+ KeyCode::Backspace => self.cut(self.prev(self.cursor), self.cursor),
+ KeyCode::Delete => self.cut(self.cursor, self.next(self.cursor)),
+ KeyCode::Char(c) if !ctrl && !alt => self.insert(c.encode_utf8(&mut [0; 4])),
+ KeyCode::Char(c) if alt && !ctrl && !c.is_ascii_alphabetic() => self.insert(c.encode_utf8(&mut [0; 4])),
+ _ => return false,
+ }
+ true
+ }
+
+ fn cut(&mut self, from: usize, to: usize) {
+ if from < to {
+ self.text.replace_range(from..to, "");
+ self.cursor = from;
+ }
+ }
+}
+
+pub(super) fn move_line(e: &mut Editor, d: isize) {
+ let start = e.text[..e.cursor].rfind('\n').map_or(0, |i| i + 1);
+ let col = e.text[start..e.cursor].chars().count();
+ let target_start = if d < 0 {
+ if start == 0 {
+ return;
+ }
+ e.text[..start - 1].rfind('\n').map_or(0, |i| i + 1)
+ } else {
+ match e.text[e.cursor..].find('\n') {
+ Some(i) => e.cursor + i + 1,
+ None => return,
+ }
+ };
+ let line_end = e.text[target_start..].find('\n').map_or(e.text.len(), |i| target_start + i);
+ e.cursor = e.text[target_start..line_end].char_indices().nth(col).map_or(line_end, |(i, _)| target_start + i);
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn editor_words_and_cuts() {
+ let mut e = Editor::with("hello brave world");
+ e.key(KeyEvent::new(KeyCode::Char('w'), KeyModifiers::CONTROL));
+ assert_eq!(e.text, "hello brave ");
+ e.key(KeyEvent::new(KeyCode::Char('a'), KeyModifiers::CONTROL));
+ assert_eq!(e.cursor, 0);
+ e.key(KeyEvent::new(KeyCode::Char('你'), KeyModifiers::NONE));
+ assert_eq!(e.text, "你hello brave ");
+ e.key(KeyEvent::new(KeyCode::Backspace, KeyModifiers::NONE));
+ assert_eq!(e.text, "hello brave ");
+ }
+
+ #[test]
+ fn line_moves_keep_the_column() {
+ let mut e = Editor::with("abc\nde");
+ move_line(&mut e, -1);
+ assert_eq!(e.cursor, 2);
+ move_line(&mut e, 1);
+ assert_eq!(e.cursor, 6);
+ }
+}
diff --git a/host/src/tui/app/events.rs b/host/src/tui/app/events.rs
new file mode 100644
index 00000000..74aecbf6
--- /dev/null
+++ b/host/src/tui/app/events.rs
@@ -0,0 +1,320 @@
+//! Host messages: the event stream and command replies.
+
+use serde_json::{Value, json};
+
+use super::{After, App, Bot, Dir, Entry, Focus, Msg, Overlay, Status, merge_toggles};
+
+impl App {
+ pub fn on_msg(&mut self, m: Msg) {
+ match m {
+ Msg::Online(on) => {
+ self.online = on;
+ if !on && self.error.is_none() && self.bots.is_empty() {
+ // Say why instead of spinning on "Connecting…".
+ self.call("hello", json!({}), After::Hello);
+ }
+ if on {
+ self.error = None;
+ // Every reconnect: the host may have been updated (or replaced) meanwhile.
+ self.call("hello", json!({}), After::Hello);
+ }
+ }
+ Msg::Rewind => {
+ self.bots.clear();
+ self.entries.clear();
+ self.history_done.clear();
+ // Catch-up carries only recent thread replies: fetch the open thread whole again.
+ if let (Some(id), Some(root)) = (&self.selected, &self.thread) {
+ self.call("thread", json!({"botId": id, "rootId": root}), After::Thread);
+ }
+ }
+ Msg::Event(v) => self.on_event(&v),
+ Msg::Reply(after, r) => self.on_reply(after, r),
+ Msg::Term(id, v) => self.on_term(&id, &v),
+ }
+ }
+
+ fn on_event(&mut self, v: &Value) {
+ match v["type"].as_str() {
+ Some("hello") if !v["usage"].is_null() => self.usage = v["usage"].clone(),
+ Some("usage") => self.usage = v["usage"].clone(),
+ Some("bot") => self.upsert_bot(&v["bot"]),
+ Some("entry") => {
+ if let Some((bot, e)) = Entry::parse(&v["entry"]) {
+ self.entries.entry(bot).or_default().insert(e.seq, e);
+ }
+ }
+ _ => {}
+ }
+ }
+
+ fn upsert_bot(&mut self, v: &Value) {
+ let Some(id) = v["id"].as_str() else { return };
+ if v["deleted"].as_bool().unwrap_or(false) {
+ self.bots.remove(id);
+ self.entries.remove(id);
+ let prefix = format!("{id}#");
+ self.drafts.retain(|key, _| key != id && !key.starts_with(&prefix));
+ self.files.retain(|key, _| key != id && !key.starts_with(&prefix));
+ if self.selected.as_deref() == Some(id) {
+ self.selected = None;
+ self.thread = None;
+ self.pick = None;
+ self.chat_page = false;
+ self.typing = false;
+ }
+ return;
+ }
+ let Some(new) = Bot::parse(v) else { return };
+ // Marked read once per change: an open thread leaves the main chat's replies unread.
+ if self.bots.get(&new.id).is_none_or(|old| old.unread != new.unread) {
+ self.reading.remove(&new.id);
+ }
+ let old = self.bots.insert(new.id.clone(), new.clone());
+ let Some(old) = old else { return };
+ let watching = self.selected.as_deref() == Some(new.id.as_str()) && self.chat_visible() && self.term_focused;
+ // A member's turn in a group is the group's: it alone says done / needs you.
+ if !new.notify || watching || old.away().is_some() || new.away().is_some() {
+ return;
+ }
+ if new.status == Status::NeedsInput && old.status != Status::NeedsInput {
+ self.toast(format!("{} needs you", new.name), true);
+ } else if old.status == Status::Working && new.status == Status::Idle {
+ self.toast(format!("{} is done", new.name), false);
+ } else if old.status != Status::Error && new.status == Status::Error {
+ self.toast(format!("{} hit an error", new.name), true);
+ }
+ }
+
+ fn on_reply(&mut self, after: After, r: Result<Value, String>) {
+ if let After::Connection(id, step) = after {
+ self.connection_reply(&id, step, r);
+ return;
+ }
+ if let After::Sent(key) = after {
+ self.sent(&key, r);
+ return;
+ }
+ if let After::Answered(entry) = after {
+ self.answering.remove(&entry);
+ if let Err(e) = r {
+ self.flash(&e);
+ }
+ return;
+ }
+ if let After::Sheet(s) = after {
+ self.on_sheet_reply(s, r);
+ return;
+ }
+ let v = match r {
+ Ok(v) => v,
+ Err(e) => {
+ match after {
+ After::Hello => self.error = Some(e),
+ After::History(id) => {
+ self.history_busy.remove(&id);
+ }
+ After::Saved => match self.overlays.last_mut() {
+ Some(Overlay::Form(f)) => {
+ f.saving = false;
+ f.error = Some(e);
+ }
+ Some(Overlay::Group(g)) => {
+ g.saving = false;
+ g.error = Some(e);
+ }
+ _ => {}
+ },
+ After::Dirs => {
+ if let Some(Overlay::Folder(p)) = self.overlays.last_mut() {
+ p.error = Some(e);
+ }
+ }
+ _ => self.flash(&e),
+ }
+ return;
+ }
+ };
+ match after {
+ After::Nothing | After::Answered(_) | After::Sheet(_) | After::Sent(_) | After::Connection(_, _) => {}
+ After::Installed => {
+ self.error = None;
+ self.flash("Host installed; connecting…");
+ self.call("hello", json!({}), After::Hello);
+ }
+ After::Hello => {
+ self.mismatch = crate::compat::check(env!("CARGO_PKG_VERSION"), crate::compat::MIN_HOST, &v);
+ v["name"].as_str().unwrap_or("computer").clone_into(&mut self.host);
+ v["home"].as_str().unwrap_or_default().clone_into(&mut self.home);
+ self.backends = v["backends"].as_array().cloned().unwrap_or_default();
+ }
+ After::Backends => {
+ self.backends = v["backends"].as_array().cloned().unwrap_or_default();
+ self.flash("Agents refreshed");
+ }
+ After::Update => match (v["state"]["availableVersion"].as_str(), v["state"]["requiredApp"].as_str()) {
+ (Some(version), Some(app)) => self.flash(&match v["state"]["appStoreVersion"].as_str() {
+ Some(store) => format!(
+ "Version {version} waits for the iPhone app {app} (App Store has {store}); it installs after review"
+ ),
+ None => format!(
+ "Version {version} waits: couldn't check the App Store for the iPhone app {app}. \
+ codync-host update --skip-app-check installs anyway"
+ ),
+ }),
+ (Some(version), None) => self.flash(&format!("Version {version} is available: run codync-host update")),
+ (None, _) => self.flash("The host is up to date."),
+ },
+ After::Thread => {
+ for e in v["entries"].as_array().into_iter().flatten() {
+ if let Some((bot, e)) = Entry::parse(e) {
+ self.entries.entry(bot).or_default().entry(e.seq).or_insert(e);
+ }
+ }
+ }
+ After::History(id) => {
+ self.history_busy.remove(&id);
+ let list = v["entries"].as_array().cloned().unwrap_or_default();
+ if list.is_empty() {
+ self.history_done.insert(id);
+ }
+ for e in &list {
+ if let Some((bot, e)) = Entry::parse(e) {
+ self.entries.entry(bot).or_default().entry(e.seq).or_insert(e);
+ }
+ }
+ }
+ After::Dirs => {
+ if let Some(Overlay::Folder(p)) = self.overlays.last_mut() {
+ v["path"].as_str().unwrap_or_default().clone_into(&mut p.path);
+ p.parent = v["parent"].as_str().map(str::to_owned);
+ p.dirs = v["dirs"]
+ .as_array()
+ .into_iter()
+ .flatten()
+ .map(|d| Dir {
+ name: d["name"].as_str().unwrap_or_default().to_owned(),
+ path: d["path"].as_str().unwrap_or_default().to_owned(),
+ git: d["isGit"].as_bool().unwrap_or(false),
+ })
+ .collect();
+ p.query.clear();
+ p.cursor = 0;
+ p.error = None;
+ }
+ }
+ After::Pairing => {
+ if let Some(Overlay::Pair(url)) = self.overlays.last_mut() {
+ *url = v["pairingUrl"].as_str().map(str::to_owned);
+ }
+ }
+ After::Saved => {
+ let saved = self.overlays.iter().rposition(|o| matches!(o, Overlay::Form(_) | Overlay::Group(_)));
+ // A new bot or group opens ready to type; an edit goes back to where it was.
+ let created = saved.is_some_and(|i| match &self.overlays[i] {
+ Overlay::Form(f) => f.bot_id.is_none(),
+ Overlay::Group(g) => g.group_id.is_none(),
+ _ => false,
+ });
+ if let Some(i) = saved {
+ self.overlays.truncate(i);
+ }
+ if let Some(id) = v["bot"]["id"].as_str() {
+ let id = id.to_owned();
+ self.upsert_bot(&v["bot"]);
+ if created {
+ self.select(&id);
+ self.chat_page = true;
+ self.focus = Focus::Chat;
+ self.typing = true;
+ }
+ }
+ }
+ After::Connectors | After::Skills => {
+ let connectors = matches!(after, After::Connectors);
+ let list = self.set_plugins(connectors, v["items"].as_array().cloned().unwrap_or_default());
+ if connectors {
+ self.market.0 = list;
+ } else {
+ self.market.1 = list;
+ }
+ for o in &mut self.overlays {
+ if let Overlay::Form(f) = o {
+ if connectors {
+ // Connectors start on for a new bot.
+ merge_toggles(&mut f.connectors, &self.market.0, f.bot_id.is_none());
+ } else {
+ merge_toggles(&mut f.skills, &self.market.1, false);
+ }
+ }
+ }
+ }
+ }
+ }
+
+ fn sent(&mut self, key: &str, result: Result<Value, String>) {
+ let Some(sent) = self.sends.get_mut(key) else { return };
+ sent.busy = false;
+ match result {
+ Ok(value) => {
+ if self.drafts.get(key).is_some_and(|d| d.text.trim() == sent.text)
+ && self.files.get(key).map_or(&[][..], Vec::as_slice) == sent.files
+ {
+ self.drafts.remove(key);
+ self.files.remove(key);
+ }
+ self.sends.remove(key);
+ self.on_event(&json!({"type": "entry", "entry": value["entry"]}));
+ self.flash("Sent");
+ }
+ Err(error) => self.flash(&format!("{error} Draft kept; Enter retries, Ctrl+C clears text.")),
+ }
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::super::{Editor, FIELDS, Field, PendingSend, test_app};
+ use super::*;
+
+ #[test]
+ fn send_failure_keeps_text_files_and_nonce_for_retry() {
+ let mut app = test_app();
+ let key = "bot/thread";
+ let files = vec![std::path::PathBuf::from("/tmp/report.txt")];
+ app.drafts.insert(key.into(), Editor::with("draft"));
+ app.files.insert(key.into(), files.clone());
+ app.sends.insert(
+ key.into(),
+ PendingSend { text: "draft".into(), files: files.clone(), nonce: "same-nonce".into(), busy: true },
+ );
+ app.on_reply(After::Sent(key.into()), Err("Disconnected".into()));
+ assert_eq!(app.drafts[key].text, "draft");
+ assert_eq!(app.files[key], files);
+ assert_eq!(app.sends[key].nonce, "same-nonce");
+ assert!(!app.sends[key].busy);
+ app.on_reply(After::Sent(key.into()), Ok(json!({})));
+ assert!(!app.drafts.contains_key(key));
+ assert!(!app.files.contains_key(key));
+ }
+
+ #[test]
+ fn late_send_success_does_not_erase_a_new_draft() {
+ let mut app = test_app();
+ app.drafts.insert("bot".into(), Editor::with("new draft"));
+ app.sends.insert(
+ "bot".into(),
+ PendingSend { text: "old draft".into(), files: vec![], nonce: "n".into(), busy: true },
+ );
+ app.on_reply(After::Sent("bot".into()), Ok(json!({})));
+ assert_eq!(app.drafts["bot"].text, "new draft");
+ }
+
+ #[test]
+ fn computer_permission_survives_loading_the_editor() {
+ let mut app = test_app();
+ app.upsert_bot(&json!({"id":"b", "computer":true}));
+ assert!(app.bots["b"].computer);
+ assert!(FIELDS.contains(&Field::Computer));
+ }
+}
diff --git a/host/src/tui/app/forms.rs b/host/src/tui/app/forms.rs
new file mode 100644
index 00000000..f78ec8a5
--- /dev/null
+++ b/host/src/tui/app/forms.rs
@@ -0,0 +1,334 @@
+//! The new/edit bot and group chat forms.
+
+use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
+use serde_json::{Value, json};
+
+use super::{
+ After, AgentPicker, App, Bot, COLORS, Editor, FIELDS, Field, Form, GroupField, GroupForm, Overlay, SHAPES, Toggle,
+ cycle, merge_toggles,
+};
+
+impl App {
+ pub(super) fn open_pair(&mut self) {
+ self.overlays.push(Overlay::Pair(None));
+ self.call("pairing", json!({}), After::Pairing);
+ }
+
+ pub(super) fn new_bot(&mut self) {
+ if self.backends.is_empty() {
+ self.call("hello", json!({}), After::Hello);
+ }
+ self.overlays.push(Overlay::Agents(AgentPicker { query: Editor::default(), cursor: 0 }));
+ }
+
+ pub(super) fn new_group(&mut self) {
+ self.overlays.push(Overlay::Group(GroupForm {
+ group_id: None,
+ name: Editor::default(),
+ about: Editor::default(),
+ members: vec![],
+ field: GroupField::Bots,
+ cursor: 0,
+ error: None,
+ saving: false,
+ }));
+ }
+
+ /// Bots a group can hold: the roster's agents, plus hidden ones already in it.
+ pub fn group_candidates(&self, members: &[String]) -> Vec<&Bot> {
+ let mut v: Vec<&Bot> = self.roster().into_iter().filter(|b| !b.group).collect();
+ v.extend(self.bots.values().filter(|b| b.hidden && !b.group && members.contains(&b.id)));
+ v
+ }
+
+ /// "Alice, Bob" when no name is typed.
+ pub fn group_default_name(&self, members: &[String]) -> String {
+ let names: Vec<&str> = members.iter().filter_map(|m| self.bots.get(m)).map(|b| b.name.as_str()).collect();
+ names.join(", ")
+ }
+
+ pub(super) fn group_key(&mut self, mut g: GroupForm, k: KeyEvent) -> Option<Overlay> {
+ let ctrl = k.modifiers.contains(KeyModifiers::CONTROL);
+ let n = self.group_candidates(&g.members).len();
+ match k.code {
+ KeyCode::Esc => return None,
+ KeyCode::Enter => self.save_group(&mut g),
+ KeyCode::Char('s') if ctrl => self.save_group(&mut g),
+ KeyCode::Tab => g.field = g.field.next(),
+ KeyCode::BackTab => g.field = g.field.prev(),
+ KeyCode::Down if g.field != GroupField::Bots => g.field = g.field.next(),
+ KeyCode::Down => g.cursor = (g.cursor + 1).min(n.saturating_sub(1)),
+ KeyCode::Up if g.field == GroupField::About => g.field = GroupField::Name,
+ KeyCode::Up if g.field == GroupField::Bots && g.cursor == 0 => g.field = GroupField::About,
+ KeyCode::Up if g.field == GroupField::Bots => g.cursor -= 1,
+ KeyCode::Char(' ') if g.field == GroupField::Bots => {
+ let id = self.group_candidates(&g.members).get(g.cursor).map(|b| b.id.clone());
+ if let Some(id) = id {
+ g.error = None;
+ if let Some(i) = g.members.iter().position(|m| *m == id) {
+ g.members.remove(i);
+ } else {
+ g.members.push(id);
+ }
+ }
+ }
+ _ if g.field == GroupField::Name => {
+ g.name.key(k);
+ }
+ _ if g.field == GroupField::About => {
+ g.about.key(k);
+ }
+ _ => {}
+ }
+ Some(Overlay::Group(g))
+ }
+
+ fn save_group(&mut self, g: &mut GroupForm) {
+ if g.saving {
+ return;
+ }
+ if g.members.is_empty() {
+ g.error = Some("Pick at least one bot.".into());
+ return;
+ }
+ let typed = g.name.text.trim();
+ let name = if typed.is_empty() { self.group_default_name(&g.members) } else { typed.to_owned() };
+ let mut body = json!({"name": name, "members": g.members, "description": g.about.text.trim()});
+ g.saving = true;
+ g.error = None;
+ if let Some(id) = &g.group_id {
+ body["id"] = id.clone().into();
+ self.call("updateBot", body, After::Saved);
+ } else {
+ body["id"] = "".into();
+ body["kind"] = "group".into();
+ self.call("createBot", body, After::Saved);
+ }
+ }
+
+ pub(super) fn edit_bot(&mut self) {
+ let Some(b) = self.bot().cloned() else { return };
+ if b.group {
+ self.overlays.push(Overlay::Group(GroupForm {
+ group_id: Some(b.id.clone()),
+ name: Editor::with(&b.name),
+ about: Editor::with(&b.description),
+ members: b.members.clone(),
+ field: GroupField::Name,
+ cursor: 0,
+ error: None,
+ saving: false,
+ }));
+ return;
+ }
+ let mut f = Form {
+ bot_id: Some(b.id.clone()),
+ name: Editor::with(&b.name),
+ instructions: Editor::with(&b.description),
+ backend: b.backend.clone(),
+ model: Editor::with(b.model.as_deref().unwrap_or_default()),
+ cwd: if b.managed_workspace { String::new() } else { b.cwd.clone() },
+ auto: b.auto,
+ notify: b.notify,
+ computer: b.computer,
+ color: COLORS.iter().position(|c| *c == b.color).unwrap_or(7),
+ shape: SHAPES.iter().position(|s| *s == b.shape).unwrap_or(0),
+ connectors: b.connectors.iter().map(|id| Toggle { id: id.clone(), name: id.clone(), on: true }).collect(),
+ skills: b.skills.iter().map(|id| Toggle { id: id.clone(), name: id.clone(), on: true }).collect(),
+ field: 0,
+ sub: 0,
+ error: None,
+ saving: false,
+ };
+ merge_toggles(&mut f.connectors, &self.market.0, false);
+ merge_toggles(&mut f.skills, &self.market.1, false);
+ self.want_models(&b.backend);
+ self.overlays.push(Overlay::Form(Box::new(f)));
+ self.call("connectors", json!({}), After::Connectors);
+ self.call("skills", json!({}), After::Skills);
+ }
+
+ /// Step 2 of a new bot: name and looks, prefilled, in a personal workspace.
+ pub(super) fn new_bot_form(&mut self, backend: String) {
+ if let Some(i) = self.overlays.iter().rposition(|o| matches!(o, Overlay::Agents(_))) {
+ self.overlays.truncate(i);
+ }
+ let n = self.bots.len() + usize::try_from(crate::store::now_ms() % 97).unwrap_or(0);
+ let name = NAMES[n % NAMES.len()].to_owned();
+ let f = Form {
+ bot_id: None,
+ name: Editor::with(&name),
+ instructions: Editor::default(),
+ backend,
+ model: Editor::default(),
+ cwd: String::new(),
+ auto: true,
+ notify: true,
+ computer: false,
+ color: (n * 7) % COLORS.len(),
+ shape: n % SHAPES.len(),
+ connectors: self
+ .market
+ .0
+ .iter()
+ .map(|t| Toggle { id: t.id.clone(), name: t.name.clone(), on: true })
+ .collect(),
+ skills: self
+ .market
+ .1
+ .iter()
+ .map(|t| Toggle { id: t.id.clone(), name: t.name.clone(), on: false })
+ .collect(),
+ field: 0,
+ sub: 0,
+ error: None,
+ saving: false,
+ };
+ self.want_models(&f.backend);
+ self.overlays.push(Overlay::Form(Box::new(f)));
+ self.call("connectors", json!({}), After::Connectors);
+ self.call("skills", json!({}), After::Skills);
+ }
+
+ pub(super) fn form_key(&mut self, mut f: Box<Form>, k: KeyEvent) -> Option<Overlay> {
+ let ctrl = k.modifiers.contains(KeyModifiers::CONTROL);
+ let field = f.current();
+ match k.code {
+ KeyCode::Esc => return None,
+ KeyCode::Char('s') if ctrl => {
+ self.save_form(&mut f);
+ }
+ KeyCode::Tab | KeyCode::Down => {
+ f.field = (f.field + 1) % FIELDS.len();
+ f.sub = 0;
+ }
+ KeyCode::BackTab | KeyCode::Up => {
+ f.field = (f.field + FIELDS.len() - 1) % FIELDS.len();
+ f.sub = 0;
+ }
+ KeyCode::Enter
+ if field == Field::Instructions && k.modifiers.intersects(KeyModifiers::SHIFT | KeyModifiers::ALT) =>
+ {
+ f.instructions.insert("\n");
+ }
+ KeyCode::Char('j') if ctrl && field == Field::Instructions => f.instructions.insert("\n"),
+ KeyCode::Backspace | KeyCode::Delete if field == Field::Folder => f.cwd.clear(),
+ KeyCode::Enter if field == Field::Folder => {
+ let start = f.cwd.clone();
+ self.overlays.push(Overlay::Form(f));
+ self.open_folder(&start);
+ return None;
+ }
+ KeyCode::Enter if field == Field::Agent => {
+ let backend = f.backend.clone();
+ self.overlays.push(Overlay::Form(f));
+ self.open_agent(&backend);
+ return None;
+ }
+ KeyCode::Left | KeyCode::Right if field == Field::Model => {
+ let d: isize = if k.code == KeyCode::Left { -1 } else { 1 };
+ self.want_models(&f.backend);
+ if let Some(m) = self.cycle_model(&f.backend, f.model.text.trim(), d) {
+ f.model = Editor::with(&m);
+ }
+ }
+ KeyCode::Enter => self.save_form(&mut f),
+ KeyCode::Left | KeyCode::Right | KeyCode::Char(' ')
+ if matches!(
+ field,
+ Field::Agent
+ | Field::Approvals
+ | Field::Notify
+ | Field::Computer
+ | Field::Color
+ | Field::Shape
+ | Field::Connectors
+ | Field::Skills
+ ) =>
+ {
+ let d: isize = if k.code == KeyCode::Left { -1 } else { 1 };
+ let space = k.code == KeyCode::Char(' ');
+ match field {
+ Field::Agent if !space => {
+ let ids: Vec<String> =
+ self.agent_choices("").iter().filter_map(|b| b["id"].as_str().map(str::to_owned)).collect();
+ if !ids.is_empty() {
+ let i = ids.iter().position(|x| *x == f.backend).unwrap_or(0);
+ f.backend.clone_from(&ids[cycle(i, d, ids.len())]);
+ // Another agent's model ids mean nothing here.
+ f.model.clear();
+ self.want_models(&f.backend);
+ }
+ }
+ Field::Approvals => f.auto = !f.auto,
+ Field::Notify => f.notify = !f.notify,
+ Field::Computer => f.computer = !f.computer,
+ Field::Color if !space => f.color = cycle(f.color, d, COLORS.len()),
+ Field::Shape if !space => f.shape = cycle(f.shape, d, SHAPES.len()),
+ Field::Connectors | Field::Skills => {
+ let list = if field == Field::Connectors { &mut f.connectors } else { &mut f.skills };
+ if space {
+ if let Some(t) = list.get_mut(f.sub) {
+ t.on = !t.on;
+ }
+ } else if !list.is_empty() {
+ f.sub = cycle(f.sub, d, list.len());
+ }
+ }
+ _ => {}
+ }
+ }
+ _ => {
+ let ed = match field {
+ Field::Name => Some(&mut f.name),
+ Field::Instructions => Some(&mut f.instructions),
+ Field::Model => Some(&mut f.model),
+ _ => None,
+ };
+ if let Some(ed) = ed {
+ ed.key(k);
+ }
+ }
+ }
+ Some(Overlay::Form(f))
+ }
+
+ fn save_form(&mut self, f: &mut Form) {
+ if f.saving {
+ return;
+ }
+ if f.bot_id.is_some() && f.name.text.trim().is_empty() {
+ f.error = Some("Give the bot a name.".into());
+ return;
+ }
+ let model = f.model.text.trim();
+ let mut body = json!({
+ "name": f.name.text.trim(),
+ "description": f.instructions.text.trim(),
+ "backend": f.backend,
+ "cwd": f.cwd,
+ "permission": if f.auto { "auto" } else { "ask" },
+ "model": if model.is_empty() { Value::Null } else { model.into() },
+ "notify": f.notify,
+ "computer": f.computer,
+ "avatarColor": COLORS[f.color],
+ "avatarShape": SHAPES[f.shape],
+ "connectors": f.connectors.iter().filter(|t| t.on).map(|t| t.id.clone()).collect::<Vec<_>>(),
+ "skills": f.skills.iter().filter(|t| t.on).map(|t| t.id.clone()).collect::<Vec<_>>(),
+ });
+ f.saving = true;
+ f.error = None;
+ match &f.bot_id {
+ Some(id) => {
+ body["id"] = id.clone().into();
+ self.call("updateBot", body, After::Saved);
+ }
+ None => self.call("createBot", body, After::Saved),
+ }
+ }
+}
+
+const NAMES: [&str; 24] = [
+ "Ada", "Rex", "Mia", "Sol", "Kit", "Oli", "Ivy", "Max", "Zoe", "Leo", "Ari", "Bea", "Cal", "Dot", "Eli", "Fay",
+ "Gus", "Hal", "Ida", "Jin", "Kai", "Lux", "Nia", "Otto",
+];
diff --git a/host/src/tui/app/input.rs b/host/src/tui/app/input.rs
new file mode 100644
index 00000000..a96e0102
--- /dev/null
+++ b/host/src/tui/app/input.rs
@@ -0,0 +1,457 @@
+//! Keys, pastes and the mouse on the main screen and the composer.
+
+use crossterm::event::{KeyCode, KeyEvent, KeyModifiers, MouseButton, MouseEvent, MouseEventKind};
+use ratatui::layout::Position;
+use serde_json::json;
+
+use super::editor::move_line;
+use super::{
+ After, App, Click, Confirm, ConfirmAct, Editor, Field, Focus, Goto, GroupField, Kind, Mark, Overlay, PendingSend,
+ Status, TraceMode, Width, mismatch_text, page, tilde,
+};
+
+impl App {
+ pub fn on_paste(&mut self, s: &str) {
+ if let Some(sh) = self.shell.as_ref().filter(|s| s.exited.is_none()) {
+ let _ = sh.input.send(s.as_bytes().to_vec());
+ return;
+ }
+ let s = s.replace("\r\n", "\n").replace('\r', "\n");
+ if let Some(ed) = self.top_editor() {
+ ed.insert(&s);
+ } else if let Some(key) = self.draft_key() {
+ self.typing = true;
+ self.focus = Focus::Chat;
+ // A file dragged onto the terminal pastes its path: attach it (groups have no folder for files).
+ match dropped_files(&s).filter(|_| self.bot().is_some_and(|b| !b.group)) {
+ Some(paths) => self.files.entry(key).or_default().extend(paths),
+ None => self.drafts.entry(key).or_default().insert(&s),
+ }
+ }
+ }
+
+ fn top_editor(&mut self) -> Option<&mut Editor> {
+ match self.overlays.last_mut()? {
+ Overlay::Goto(g) => Some(&mut g.query),
+ Overlay::Help(e) => Some(e),
+ Overlay::Agents(a) => Some(&mut a.query),
+ Overlay::Folder(p) => Some(&mut p.query),
+ Overlay::Group(g) => match g.field {
+ GroupField::Name => Some(&mut g.name),
+ GroupField::About => Some(&mut g.about),
+ GroupField::Bots => None,
+ },
+ Overlay::Form(f) => match f.current() {
+ Field::Name => Some(&mut f.name),
+ Field::Instructions => Some(&mut f.instructions),
+ Field::Model => Some(&mut f.model),
+ _ => None,
+ },
+ Overlay::Market(m) => Some(&mut m.query),
+ Overlay::Fields(f) => f.current().map(|i| &mut i.ed),
+ Overlay::Routine(f) => Some(match f.field {
+ super::super::manage::RoutineField::Name => &mut f.name,
+ super::super::manage::RoutineField::Instruction => &mut f.instruction,
+ super::super::manage::RoutineField::When => &mut f.when,
+ }),
+ _ => None,
+ }
+ }
+
+ pub fn on_key(&mut self, k: KeyEvent) {
+ if self.shell.is_some() {
+ self.shell_key(k);
+ return;
+ }
+ let ctrl = k.modifiers.contains(KeyModifiers::CONTROL);
+ if ctrl && k.code == KeyCode::Char('k') {
+ if matches!(self.overlays.last(), Some(Overlay::Goto(_))) {
+ self.overlays.pop();
+ } else {
+ self.overlays.push(Overlay::Goto(Goto { query: Editor::default(), filter: 0, cursor: 0 }));
+ }
+ return;
+ }
+ if !self.overlays.is_empty() {
+ self.overlay_key(k);
+ return;
+ }
+ if self.typing {
+ self.composer_key(k);
+ return;
+ }
+ self.nav_key(k);
+ }
+
+ fn composer_key(&mut self, k: KeyEvent) {
+ let (Some(id), Some(key)) = (self.selected.clone(), self.draft_key()) else {
+ self.typing = false;
+ return;
+ };
+ let ctrl = k.modifiers.contains(KeyModifiers::CONTROL);
+ let newline = k.modifiers.intersects(KeyModifiers::SHIFT | KeyModifiers::ALT);
+ let thread = self.thread.clone();
+ let draft = self.drafts.entry(key.clone()).or_default();
+ match k.code {
+ KeyCode::Esc => self.typing = false,
+ KeyCode::Enter if newline => draft.insert("\n"),
+ KeyCode::Char('j') if ctrl => draft.insert("\n"),
+ KeyCode::Enter => {
+ let text = draft.text.trim().to_owned();
+ let files = self.files.get(&key).cloned().unwrap_or_default();
+ if text.is_empty() && files.is_empty() {
+ return;
+ }
+ if let Some(m) = &self.mismatch {
+ // The draft stays; it sends once the update is done.
+ let why = mismatch_text(m, &self.host);
+ self.flash(&why);
+ return;
+ }
+ if self.sends.get(&key).is_some_and(|s| s.busy) {
+ self.flash("Sending…");
+ return;
+ }
+ let nonce = self
+ .sends
+ .get(&key)
+ .filter(|s| s.text == text && s.files == files)
+ .map_or_else(|| uuid::Uuid::new_v4().to_string(), |s| s.nonce.clone());
+ self.sends.insert(
+ key.clone(),
+ PendingSend { text: text.clone(), files: files.clone(), nonce: nonce.clone(), busy: true },
+ );
+ self.chat_scroll = 0;
+ let mut body = json!({"botId": id, "text": text, "clientNonce": nonce});
+ if let Some(root) = thread {
+ body["threadId"] = root.into();
+ }
+ let after = After::Sent(key);
+ if files.is_empty() {
+ self.call("send", body, after);
+ } else {
+ self.client.spawn_send_files(body, files, after, self.tx.clone());
+ }
+ self.flash("Sending… Your draft stays here until delivered.");
+ }
+ KeyCode::Backspace if draft.text.is_empty() => {
+ if let Some(files) = self.files.get_mut(&key) {
+ files.pop();
+ }
+ }
+ KeyCode::Char('c') if ctrl => {
+ if draft.text.is_empty() {
+ self.flash("esc then s stops the bot");
+ } else {
+ draft.clear();
+ }
+ }
+ KeyCode::Up if draft.text.is_empty() => self.scroll_chat(1),
+ KeyCode::Down if draft.text.is_empty() => self.scroll_chat(-1),
+ KeyCode::PageUp => self.scroll_chat(page(self.chat_height)),
+ KeyCode::PageDown => self.scroll_chat(-page(self.chat_height)),
+ KeyCode::Up => move_line(draft, -1),
+ KeyCode::Down => move_line(draft, 1),
+ _ => {
+ draft.key(k);
+ }
+ }
+ }
+
+ pub(super) fn scroll_chat(&mut self, delta: isize) {
+ self.chat_scroll = self.chat_scroll.saturating_add_signed(delta);
+ }
+
+ pub(super) fn scroll_trace(&mut self, delta: isize) {
+ self.trace_scroll = self.trace_scroll.saturating_add_signed(delta);
+ }
+
+ fn nav_key(&mut self, k: KeyEvent) {
+ let ctrl = k.modifiers.contains(KeyModifiers::CONTROL);
+ let half = page(self.chat_height) / 2;
+ let narrow = self.width == Width::Narrow;
+ let roster_page = narrow && !self.chat_page;
+ if self.pick.is_some() && self.pick_key(k) {
+ return;
+ }
+ match k.code {
+ KeyCode::Char('c') if ctrl => self.flash("q quits"),
+ KeyCode::Char('d') if ctrl => self.scroll_focused(-half),
+ KeyCode::Char('u') if ctrl => self.scroll_focused(half),
+ KeyCode::Char('q') => self.quit = true,
+ KeyCode::Char('C') => self.open_connection(),
+ KeyCode::Char('I') if self.error.is_some() && self.bots.is_empty() => self.install_host(),
+ KeyCode::Char('?') => self.overlays.push(Overlay::Help(Editor::default())),
+ KeyCode::Char('!') => self.jump(Mark::Need),
+ KeyCode::Char('u') => self.jump(Mark::Unread),
+ KeyCode::Char('[') => self.move_bot(-1),
+ KeyCode::Char(']') => self.move_bot(1),
+ KeyCode::Char(c @ '1'..='9') => {
+ let i = (c as usize) - ('1' as usize);
+ let pending = if roster_page { None } else { self.pending().map(|e| (e.id.clone(), e.options())) };
+ match pending {
+ Some((entry, opts)) if !opts.is_empty() => {
+ if let Some((option, _, _)) = opts.get(i) {
+ self.answer(&entry, option);
+ }
+ }
+ _ => {
+ if let Some(id) = self.roster().get(i).map(|b| b.id.clone()) {
+ self.select(&id);
+ }
+ }
+ }
+ }
+ KeyCode::Char(c @ ('y' | 'a' | 'n' | 'N')) if !roster_page && self.pending().is_some() => {
+ self.answer_kind(c);
+ }
+ KeyCode::Char('n') => self.new_bot(),
+ KeyCode::Char('m') => self.new_group(),
+ KeyCode::Char('i') if !roster_page => self.start_typing(),
+ KeyCode::Char('r') if !roster_page && self.selected.is_some() => {
+ if self.thread.is_some() {
+ self.start_typing();
+ } else {
+ self.chat_page = true;
+ self.focus = Focus::Chat;
+ self.step_pick(0);
+ }
+ }
+ KeyCode::Enter => {
+ if roster_page || self.focus == Focus::Roster {
+ if self.selected.is_some() {
+ self.chat_page = true;
+ self.focus = Focus::Chat;
+ self.start_typing();
+ }
+ } else {
+ self.start_typing();
+ }
+ }
+ KeyCode::Esc | KeyCode::Backspace => {
+ if self.trace == TraceMode::Full {
+ self.trace = TraceMode::Off;
+ self.focus = Focus::Chat;
+ } else if self.thread.is_some() {
+ self.thread = None;
+ self.chat_scroll = 0;
+ self.trace_turn = None;
+ self.trace_scroll = 0;
+ } else if narrow && self.chat_page {
+ self.chat_page = false;
+ self.focus = Focus::Roster;
+ } else if self.focus == Focus::Trace {
+ self.focus = Focus::Chat;
+ }
+ }
+ KeyCode::Tab | KeyCode::BackTab => self.cycle_focus(k.code == KeyCode::BackTab),
+ KeyCode::Char('h') | KeyCode::Left => self.focus_side(-1),
+ KeyCode::Char('l') | KeyCode::Right => self.focus_side(1),
+ KeyCode::Char('j') | KeyCode::Down => self.line(1),
+ KeyCode::Char('k') | KeyCode::Up => self.line(-1),
+ KeyCode::PageDown => self.scroll_focused(-page(self.chat_height)),
+ KeyCode::PageUp => self.scroll_focused(page(self.chat_height)),
+ KeyCode::Char('g') | KeyCode::Home => {
+ if self.focus == Focus::Trace {
+ self.trace_scroll = 0;
+ } else {
+ self.chat_scroll = usize::MAX / 2;
+ }
+ }
+ KeyCode::Char('G') | KeyCode::End => {
+ if self.focus == Focus::Trace {
+ self.trace_scroll = usize::MAX / 2;
+ } else {
+ self.chat_scroll = 0;
+ }
+ }
+ KeyCode::Char('{') => self.step_turn(-1),
+ KeyCode::Char('}') => self.step_turn(1),
+ KeyCode::Char('t') => {
+ self.trace = if self.trace == TraceMode::Pane { TraceMode::Off } else { TraceMode::Pane };
+ if self.trace == TraceMode::Off && self.focus == Focus::Trace {
+ self.focus = Focus::Chat;
+ }
+ if self.width == Width::Narrow || self.width == Width::Mid {
+ self.trace = if self.trace == TraceMode::Off { TraceMode::Off } else { TraceMode::Full };
+ }
+ }
+ KeyCode::Char('T') => {
+ self.trace = if self.trace == TraceMode::Full { TraceMode::Off } else { TraceMode::Full };
+ self.focus = if self.trace == TraceMode::Full { Focus::Trace } else { Focus::Chat };
+ }
+ KeyCode::Char('o') => {
+ let Some(id) = self.selected.clone() else { return };
+ // The newest turn in this lane that did something.
+ self.trace_turn = self
+ .lane(&id)
+ .into_iter()
+ .filter(|e| matches!(e.kind, Kind::Tool | Kind::Plan))
+ .map(|e| e.turn)
+ .max();
+ self.trace_scroll = 0;
+ if self.trace == TraceMode::Off {
+ self.trace = if matches!(self.width, Width::Narrow | Width::Mid) {
+ TraceMode::Full
+ } else {
+ TraceMode::Pane
+ };
+ }
+ self.focus = Focus::Trace;
+ }
+ KeyCode::Char('b') => self.compact = !self.compact,
+ KeyCode::Char('U') => self.overlays.push(Overlay::Usage),
+ KeyCode::Char('M') => self.open_memory(),
+ KeyCode::Char('R') => self.open_routines(),
+ KeyCode::Char('A') => self.open_market(),
+ KeyCode::Char('v') if !roster_page && self.selected.is_some() => {
+ self.chat_page = true;
+ self.focus = Focus::Chat;
+ self.step_pick(0);
+ }
+ KeyCode::Char('P') => self.open_pair(),
+ KeyCode::Char('s') => {
+ if let Some(b) = self.bot() {
+ if matches!(b.status, Status::Working | Status::NeedsInput) {
+ let id = b.id.clone();
+ self.call("stop", json!({"botId": id}), After::Nothing);
+ self.flash("Stopping…");
+ } else {
+ self.flash("Not working right now");
+ }
+ }
+ }
+ KeyCode::Char('S') => {
+ if self.bot().is_some_and(|b| b.group) {
+ self.flash("A group has no session of its own");
+ } else if let Some(b) = self.bot() {
+ let c = Confirm {
+ title: format!("Start a new session for {}?", b.name),
+ detail: "The agent forgets this conversation's context.".into(),
+ note: "The transcript stays here.".into(),
+ button: "New session",
+ act: ConfirmAct::NewSession(b.id.clone()),
+ };
+ self.overlays.push(Overlay::Confirm(c));
+ }
+ }
+ KeyCode::Char('x') => {
+ if let Some(b) = self.bot() {
+ let (detail, note) = if b.group {
+ ("Its conversation goes away.".into(), "Its bots stay.".into())
+ } else {
+ (
+ "Its conversation and settings go away.".into(),
+ format!("Files in {} stay where they are.", tilde(&b.cwd, &self.home)),
+ )
+ };
+ let c = Confirm {
+ title: format!("Delete {}?", b.name),
+ detail,
+ note,
+ button: "Delete",
+ act: ConfirmAct::Delete(b.id.clone()),
+ };
+ self.overlays.push(Overlay::Confirm(c));
+ }
+ }
+ KeyCode::Char('e') => self.edit_bot(),
+ KeyCode::Char('p') => {
+ if let Some(b) = self.bot() {
+ let (id, pinned) = (b.id.clone(), !b.pinned);
+ self.call("updateBot", json!({"id": id, "pinned": pinned}), After::Nothing);
+ }
+ }
+ KeyCode::Char('c') => self.copy_last(),
+ _ => {}
+ }
+ }
+
+ // ---------- mouse ----------
+
+ pub fn on_mouse(&mut self, m: MouseEvent) {
+ let at = Position::new(m.column, m.row);
+ match m.kind {
+ MouseEventKind::ScrollUp | MouseEventKind::ScrollDown => {
+ let d: isize = if m.kind == MouseEventKind::ScrollUp { -3 } else { 3 };
+ if self.hits.trace.contains(at) {
+ self.scroll_trace(d);
+ } else if self.hits.chat.contains(at) {
+ self.scroll_chat(-d);
+ } else if self.hits.roster.contains(at) {
+ self.move_bot(d.signum());
+ }
+ }
+ MouseEventKind::Down(MouseButton::Left) => {
+ let hit = self.hits.clicks.iter().rev().find(|(r, _)| r.contains(at)).map(|(_, c)| c.clone());
+ match hit {
+ Some(Click::Bot(id)) => {
+ self.overlays.clear();
+ self.select(&id);
+ self.focus = Focus::Roster;
+ if self.width == Width::Narrow {
+ self.chat_page = true;
+ self.focus = Focus::Chat;
+ }
+ }
+ Some(Click::Option { entry, option }) => self.answer(&entry, &option),
+ Some(Click::Filter(i)) => {
+ if let Some(Overlay::Goto(g)) = self.overlays.last_mut() {
+ g.filter = i;
+ g.cursor = 0;
+ } else {
+ self.overlays.push(Overlay::Goto(Goto { query: Editor::default(), filter: i, cursor: 0 }));
+ }
+ }
+ Some(Click::Composer) => self.start_typing(),
+ Some(Click::Thread(root)) => self.open_thread(root),
+ None => {
+ if self.overlays.is_empty() {
+ if self.hits.trace.contains(at) {
+ self.focus = Focus::Trace;
+ } else if self.hits.chat.contains(at) {
+ self.focus = Focus::Chat;
+ } else if self.hits.roster.contains(at) {
+ self.focus = Focus::Roster;
+ }
+ }
+ }
+ }
+ }
+ _ => {}
+ }
+ }
+}
+
+/// Paths a terminal pastes for dropped files (shell-escaped, quoted or `file://`), when every one is a file.
+fn dropped_files(s: &str) -> Option<Vec<std::path::PathBuf>> {
+ let paths: Vec<std::path::PathBuf> = shlex::split(s.trim())?
+ .into_iter()
+ .map(|p| match p.strip_prefix("file://") {
+ Some(url) => url.replace("%20", " "),
+ None => p,
+ })
+ .map(std::path::PathBuf::from)
+ .collect();
+ (!paths.is_empty() && paths.iter().all(|p| p.is_absolute() && p.is_file())).then_some(paths)
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn dropped_paths_are_files_only() {
+ let dir = std::env::temp_dir().join(format!("codync-drop-{}", std::process::id()));
+ std::fs::create_dir_all(&dir).expect("temp dir");
+ let a = dir.join("a b.txt");
+ std::fs::write(&a, "x").expect("write");
+ let esc = a.to_string_lossy().replace(' ', "\\ ");
+ assert_eq!(dropped_files(&esc), Some(vec![a.clone()]));
+ assert_eq!(dropped_files(&format!("'{}'", a.display())), Some(vec![a.clone()]));
+ assert_eq!(dropped_files(&format!("file://{}", a.to_string_lossy().replace(' ', "%20"))), Some(vec![a]));
+ assert_eq!(dropped_files(&dir.to_string_lossy()), None);
+ assert_eq!(dropped_files("hello world"), None);
+ std::fs::remove_dir_all(&dir).expect("cleanup");
+ }
+}
diff --git a/host/src/tui/app/mod.rs b/host/src/tui/app/mod.rs
new file mode 100644
index 00000000..74c59c39
--- /dev/null
+++ b/host/src/tui/app/mod.rs
@@ -0,0 +1,466 @@
+//! TUI state and everything that changes it: host events, keys, mouse, replies.
+
+mod editor;
+mod events;
+mod forms;
+mod input;
+mod model;
+mod navigate;
+mod overlay_keys;
+mod ui;
+
+pub use editor::Editor;
+pub use model::{After, Bot, Entry, Kind, Mark, Msg, Status};
+pub use ui::{
+ ACTIONS, Action, AgentPicker, COLORS, Click, Confirm, ConfirmAct, Dir, FIELDS, FILTERS, Field, Focus, FolderPicker,
+ Form, Goto, GotoItem, GroupField, GroupForm, Hits, NEW_GROUP, Overlay, SHAPES, Toast, Toggle, TraceMode, Width,
+};
+
+use serde_json::{Value, json};
+use std::collections::{BTreeMap, HashMap, HashSet};
+use std::io::Write as _;
+use std::time::{Duration, Instant};
+use tokio::sync::mpsc::UnboundedSender;
+
+use super::manage::Shell;
+use super::net::Client;
+
+#[derive(Clone)]
+struct PendingSend {
+ text: String,
+ files: Vec<std::path::PathBuf>,
+ nonce: String,
+ busy: bool,
+}
+
+#[allow(clippy::struct_excessive_bools, reason = "independent UI flags, not a state machine")]
+pub struct App {
+ pub client: Client,
+ pub(super) tx: UnboundedSender<Msg>,
+ pub url: String,
+ pub host: String,
+ pub home: String,
+ pub online: bool,
+ pub error: Option<String>,
+ /// This client and the host can't work together until one of them updates.
+ pub mismatch: Option<crate::compat::Mismatch>,
+ pub bots: HashMap<String, Bot>,
+ pub entries: HashMap<String, BTreeMap<i64, Entry>>,
+ pub usage: Value,
+ pub backends: Vec<Value>,
+ pub selected: Option<String>,
+ pub focus: Focus,
+ pub typing: bool,
+ /// Narrow layout: showing the chat page (else the roster page).
+ pub chat_page: bool,
+ pub drafts: HashMap<String, Editor>,
+ /// Files dropped on the composer (pasted paths), per draft like the text.
+ pub files: HashMap<String, Vec<std::path::PathBuf>>,
+ sends: HashMap<String, PendingSend>,
+ /// Lines scrolled up from the bottom of the chat (0 = follow new messages).
+ pub chat_scroll: usize,
+ pub trace_scroll: usize,
+ pub trace: TraceMode,
+ /// Turn shown in the trace; `None` follows the latest.
+ pub trace_turn: Option<i64>,
+ pub compact: bool,
+ pub overlays: Vec<Overlay>,
+ pub toasts: Vec<Toast>,
+ pub frame: u64,
+ pub term_focused: bool,
+ pub quit: bool,
+ pub hits: Hits,
+ pub width: Width,
+ /// Chat/trace viewport heights from the last draw, for page scrolling.
+ pub chat_height: usize,
+ pub chat_top: bool,
+ /// The open thread's root entry in the selected chat; `None` shows the main chat.
+ pub thread: Option<String>,
+ /// A main-chat message picked to reply to in its thread (`r`, then j/k and ↵).
+ pub pick: Option<String>,
+ pub market: (Vec<Toggle>, Vec<Toggle>),
+ /// Installed connectors and skills as the host lists them.
+ pub plugins: (Vec<Value>, Vec<Value>),
+ /// Models each agent offers (`None` while loading).
+ pub models: HashMap<String, Option<Vec<(String, String)>>>,
+ /// A setup terminal holding the screen.
+ pub shell: Option<Shell>,
+ reading: HashSet<String>,
+ history_busy: HashSet<String>,
+ /// Permission cards whose answer is on its way, with the chosen option.
+ pub answering: HashMap<String, String>,
+ history_done: HashSet<String>,
+ pub hint: Option<(String, Instant)>,
+}
+
+impl App {
+ pub fn new(client: Client, tx: UnboundedSender<Msg>, url: String) -> Self {
+ Self {
+ client,
+ tx,
+ url,
+ host: String::new(),
+ home: String::new(),
+ online: false,
+ error: None,
+ mismatch: None,
+ bots: HashMap::new(),
+ entries: HashMap::new(),
+ usage: Value::Null,
+ backends: vec![],
+ selected: None,
+ focus: Focus::Roster,
+ typing: false,
+ chat_page: false,
+ drafts: HashMap::new(),
+ files: HashMap::new(),
+ sends: HashMap::new(),
+ chat_scroll: 0,
+ trace_scroll: 0,
+ trace: TraceMode::Off,
+ trace_turn: None,
+ compact: false,
+ overlays: vec![],
+ toasts: vec![],
+ frame: 0,
+ term_focused: true,
+ quit: false,
+ hits: Hits::default(),
+ width: Width::Wide,
+ chat_height: 20,
+ chat_top: false,
+ thread: None,
+ pick: None,
+ market: (vec![], vec![]),
+ plugins: (vec![], vec![]),
+ models: HashMap::new(),
+ shell: None,
+ reading: HashSet::new(),
+ history_busy: HashSet::new(),
+ answering: HashMap::new(),
+ history_done: HashSet::new(),
+ hint: None,
+ }
+ }
+
+ pub fn call(&self, method: &'static str, body: Value, after: After) {
+ self.client.spawn_call(method, body, after, self.tx.clone());
+ }
+
+ // ---------- derived ----------
+
+ /// Roster order: pinned first, then most recent (same as the phone).
+ pub fn roster(&self) -> Vec<&Bot> {
+ let mut v: Vec<&Bot> = self.bots.values().filter(|b| !b.hidden).collect();
+ v.sort_by(|a, b| b.pinned.cmp(&a.pinned).then(b.last_at.cmp(&a.last_at)).then(a.name.cmp(&b.name)));
+ v
+ }
+
+ pub fn bot(&self) -> Option<&Bot> {
+ self.selected.as_ref().and_then(|id| self.bots.get(id))
+ }
+
+ pub fn counts(&self) -> [usize; 4] {
+ let mut c = [0; 4];
+ for b in self.bots.values().filter(|b| !b.hidden) {
+ match b.mark() {
+ Mark::Need => c[0] += 1,
+ Mark::Work => c[1] += 1,
+ Mark::Unread => c[2] += 1,
+ Mark::Error => c[3] += 1,
+ Mark::Idle => {}
+ }
+ }
+ c
+ }
+
+ /// A chat's entries in the lane on screen (main chat, or the open thread's replies), in display order.
+ pub fn lane(&self, bot: &str) -> Vec<&Entry> {
+ let thread = self.thread.as_deref();
+ let mut v: Vec<&Entry> = self
+ .entries
+ .get(bot)
+ .map(|m| m.values().filter(|e| e.thread_id.as_deref() == thread).collect())
+ .unwrap_or_default();
+ // A message sent mid-turn belongs after that turn's reply.
+ v.sort_by_key(|e| (e.turn, e.seq));
+ v
+ }
+
+ pub fn pending(&self) -> Option<&Entry> {
+ let id = self.selected.as_ref()?;
+ self.lane(id).into_iter().rev().find(|e| e.pending())
+ }
+
+ /// Where the composer's draft lives: per chat, and per thread in it.
+ fn draft_key(&self) -> Option<String> {
+ let id = self.selected.as_ref()?;
+ Some(self.thread.as_ref().map_or_else(|| id.clone(), |root| format!("{id}#{root}")))
+ }
+
+ pub fn draft(&self) -> Editor {
+ self.draft_key().and_then(|k| self.drafts.get(&k).cloned()).unwrap_or_default()
+ }
+
+ pub fn draft_files(&self) -> &[std::path::PathBuf] {
+ self.draft_key().and_then(|k| self.files.get(&k)).map_or(&[], Vec::as_slice)
+ }
+
+ /// Name of an entry's author (a group reply's bot), as the apps show it.
+ pub fn author_name(&self, id: Option<&str>) -> String {
+ id.and_then(|id| self.bots.get(id)).map_or_else(|| "A deleted bot".into(), |b| b.name.clone())
+ }
+
+ /// The turns of the lane on screen, oldest first: the trace steps through these.
+ /// Turn numbers count per bot, so a lane's own are not consecutive.
+ pub fn lane_turns(&self, bot: &str) -> Vec<i64> {
+ let mut v: Vec<i64> = self.lane(bot).iter().map(|e| e.turn).collect();
+ v.sort_unstable();
+ v.dedup();
+ v
+ }
+
+ /// Whether every older entry of this bot is loaded.
+ pub fn history_complete(&self, id: &str) -> bool {
+ self.history_done.contains(id) || self.oldest_main(id).is_none_or(|s| s <= 1)
+ }
+
+ /// Paging (`history`) covers the main chat only; thread replies don't count.
+ fn oldest_main(&self, id: &str) -> Option<i64> {
+ self.entries.get(id)?.values().find(|e| e.thread_id.is_none()).map(|e| e.seq)
+ }
+
+ pub fn chat_visible(&self) -> bool {
+ self.selected.is_some() && (self.width != Width::Narrow || self.chat_page) && self.trace != TraceMode::Full
+ }
+
+ pub fn animating(&self) -> bool {
+ !self.toasts.is_empty()
+ || self.hint.is_some()
+ || !self.online
+ || !self.answering.is_empty()
+ || self.bots.values().any(|b| b.status == Status::Working)
+ }
+
+ fn select(&mut self, id: &str) {
+ if self.selected.as_deref() != Some(id) {
+ self.selected = Some(id.to_owned());
+ self.thread = None;
+ self.pick = None;
+ self.chat_scroll = 0;
+ self.trace_scroll = 0;
+ self.trace_turn = None;
+ }
+ }
+
+ fn move_bot(&mut self, delta: isize) {
+ let ids: Vec<String> = self.roster().iter().map(|b| b.id.clone()).collect();
+ if ids.is_empty() {
+ return;
+ }
+ let i = self.selected.as_ref().and_then(|s| ids.iter().position(|x| x == s));
+ let n = isize::try_from(ids.len()).unwrap_or(isize::MAX);
+ let next = match i {
+ Some(i) => (isize::try_from(i).unwrap_or(0) + delta).clamp(0, n - 1),
+ None => 0,
+ };
+ let id = ids[usize::try_from(next).unwrap_or(0)].clone();
+ self.select(&id);
+ }
+
+ /// Next bot (after the selected one, wrapping) whose mark matches.
+ fn jump(&mut self, want: Mark) {
+ // A bot busy in a group needs you there, and the group is marked too: skip the bot.
+ let ids: Vec<(String, bool)> = self
+ .roster()
+ .iter()
+ .map(|b| (b.id.clone(), b.mark() == want && (want != Mark::Need || b.away().is_none())))
+ .collect();
+ let start = self.selected.as_ref().and_then(|s| ids.iter().position(|(x, _)| x == s)).map_or(0, |i| i + 1);
+ let found = (0..ids.len()).map(|k| &ids[(start + k) % ids.len()]).find(|(_, hit)| *hit).cloned();
+ match found {
+ Some((id, _)) => {
+ self.select(&id);
+ self.chat_page = true;
+ self.focus = Focus::Chat;
+ // A card waiting in a thread shows there.
+ let thread = self.bots.get(&id).and_then(|b| b.working_thread.clone()).filter(|_| want == Mark::Need);
+ if let Some(root) = thread {
+ self.open_thread(root);
+ }
+ }
+ None => self.flash(match want {
+ Mark::Need => "No bot needs you",
+ Mark::Unread => "Nothing unread",
+ _ => "No match",
+ }),
+ }
+ }
+
+ pub fn flash(&mut self, s: &str) {
+ self.hint = Some((s.to_owned(), Instant::now() + Duration::from_secs(3)));
+ }
+
+ fn toast(&mut self, text: String, need: bool) {
+ if !self.term_focused {
+ // OSC 9 desktop notification (iTerm2, Ghostty, kitty, WezTerm…) plus a bell.
+ let mut out = std::io::stdout();
+ let _ = write!(out, "\x1b]9;{}\x07\x07", text.replace(['\x07', '\x1b'], ""));
+ let _ = out.flush();
+ }
+ self.toasts.retain(|t| t.text != text);
+ self.toasts.push(Toast { text, need, until: Instant::now() + Duration::from_secs(4) });
+ if self.toasts.len() > 2 {
+ self.toasts.remove(0);
+ }
+ }
+
+ /// Drops expired toasts and keeps the viewed bot marked read.
+ pub fn tick(&mut self) {
+ if self.selected.is_none()
+ && let Some(id) = self.roster().first().map(|b| b.id.clone())
+ {
+ self.selected = Some(id);
+ }
+ let now = Instant::now();
+ self.toasts.retain(|t| t.until > now);
+ if self.hint.as_ref().is_some_and(|(_, until)| *until <= now) {
+ self.hint = None;
+ }
+ if self.chat_visible()
+ && self.term_focused
+ && let Some(b) = self.bot()
+ && b.unread > 0
+ && !self.reading.contains(&b.id)
+ {
+ let id = b.id.clone();
+ self.reading.insert(id.clone());
+ self.call("markRead", json!({"botId": id, "threadId": self.thread}), After::Nothing);
+ }
+ if self.chat_top && self.chat_visible() && self.thread.is_none() {
+ self.load_history();
+ }
+ }
+
+ fn load_history(&mut self) {
+ let Some(id) = self.selected.clone() else { return };
+ if self.history_busy.contains(&id) || self.history_done.contains(&id) {
+ return;
+ }
+ let Some(oldest) = self.oldest_main(&id) else { return };
+ if oldest <= 1 {
+ self.history_done.insert(id);
+ return;
+ }
+ self.history_busy.insert(id.clone());
+ self.call("history", json!({"botId": id, "beforeSeq": oldest, "limit": 200}), After::History(id));
+ }
+}
+
+/// What to update, in the words every client uses (docs/reference/compatibility.md).
+pub fn mismatch_text(m: &crate::compat::Mismatch, host: &str) -> String {
+ let host = if host.is_empty() { "the computer" } else { host };
+ match m {
+ crate::compat::Mismatch::UpdateApp { minimum } => {
+ format!("Update this app: {host} needs Codync {minimum} or newer here.")
+ }
+ crate::compat::Mismatch::UpdateHost { version, minimum } => format!(
+ "Update Codync on {host}: it runs {version}, this app needs {minimum} or newer. ^k → Check for updates."
+ ),
+ }
+}
+
+/// OSC 52: the terminal puts it on the clipboard, over SSH too.
+pub fn copy(text: &str) {
+ let mut out = std::io::stdout();
+ let _ = write!(out, "\x1b]52;c;{}\x07", base64::Engine::encode(&base64::engine::general_purpose::STANDARD, text));
+ let _ = out.flush();
+}
+
+fn page(h: usize) -> isize {
+ isize::try_from(h.max(4)).unwrap_or(20) - 2
+}
+
+pub(super) fn cycle(i: usize, d: isize, n: usize) -> usize {
+ let n = isize::try_from(n).unwrap_or(1).max(1);
+ usize::try_from((isize::try_from(i).unwrap_or(0) + d).rem_euclid(n)).unwrap_or(0)
+}
+
+fn merge_toggles(have: &mut Vec<Toggle>, all: &[Toggle], on: bool) {
+ for t in all {
+ match have.iter_mut().find(|h| h.id == t.id) {
+ Some(h) => h.name.clone_from(&t.name),
+ None => have.push(Toggle { id: t.id.clone(), name: t.name.clone(), on }),
+ }
+ }
+}
+
+/// Case-insensitive substring match: for long fields where a subsequence matches almost anything.
+fn contains(query: &str, fields: &[&str]) -> bool {
+ let q = query.to_lowercase();
+ fields.iter().any(|f| f.to_lowercase().contains(&q))
+}
+
+/// Subsequence match, case-insensitive, over any of the fields.
+pub fn fuzzy(query: &str, fields: &[&str]) -> bool {
+ let q: Vec<char> = query.to_lowercase().chars().filter(|c| !c.is_whitespace()).collect();
+ if q.is_empty() {
+ return true;
+ }
+ fields.iter().any(|f| {
+ let mut it = q.iter().peekable();
+ for c in f.to_lowercase().chars() {
+ if it.peek() == Some(&&c) {
+ it.next();
+ }
+ }
+ it.peek().is_none()
+ })
+}
+
+pub fn tilde(path: &str, home: &str) -> String {
+ match path.strip_prefix(home) {
+ Some(rest) if !home.is_empty() => format!("~{rest}"),
+ _ => path.to_owned(),
+ }
+}
+
+#[cfg(test)]
+fn test_app() -> App {
+ let (tx, _) = tokio::sync::mpsc::unbounded_channel();
+ App::new(Client::new("http://127.0.0.1:1", None), tx, String::new())
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn fuzzy_is_a_subsequence_match() {
+ assert!(fuzzy("rx", &["Rex"]));
+ assert!(fuzzy("", &["anything"]));
+ assert!(!fuzzy("xr", &["Rex"]));
+ assert!(fuzzy("web", &["Rex", "~/mycode/web"]));
+ }
+
+ #[test]
+ fn lanes_split_the_chat_from_its_threads() {
+ let g = Bot::parse(&json!({"id": "g", "kind": "group", "members": ["a", "b"], "workingThread": "r"}))
+ .expect("a bot with an id parses");
+ assert!(g.group && g.members == ["a", "b"]);
+ assert!(g.works_in(Some("r")) && !g.works_in(None));
+ let (tx, _rx) = tokio::sync::mpsc::unbounded_channel();
+ let mut app = App::new(Client::new("http://127.0.0.1:1", None), tx, String::new());
+ for (seq, thread) in [(1, Value::Null), (2, json!("r")), (3, Value::Null)] {
+ let v = json!({"botId": "g", "id": format!("e{seq}"), "seq": seq, "turn": seq, "kind": "user", "threadId": thread});
+ let (bot, e) = Entry::parse(&v).expect("a full entry parses");
+ app.entries.entry(bot).or_default().insert(e.seq, e);
+ }
+ let ids = |app: &App| app.lane("g").iter().map(|e| e.id.clone()).collect::<Vec<_>>();
+ assert_eq!(ids(&app), ["e1", "e3"]);
+ assert_eq!(app.lane_turns("g"), [1, 3]);
+ app.thread = Some("r".into());
+ assert_eq!(ids(&app), ["e2"]);
+ assert_eq!(app.lane_turns("g"), [2]);
+ }
+}
diff --git a/host/src/tui/app/model.rs b/host/src/tui/app/model.rs
new file mode 100644
index 00000000..8f82e831
--- /dev/null
+++ b/host/src/tui/app/model.rs
@@ -0,0 +1,303 @@
+//! What the host sends: bots, transcript entries, and the messages that carry them.
+
+use serde_json::Value;
+
+use super::super::manage::Reply;
+use super::tilde;
+
+pub enum Msg {
+ Online(bool),
+ Event(Value),
+ Reply(After, Result<Value, String>),
+ /// The stream is starting over from rev 0: drop what we have.
+ Rewind,
+ /// A setup terminal's output or exit.
+ Term(String, Value),
+}
+
+/// What to do with a command's reply.
+#[derive(Clone)]
+pub enum After {
+ Nothing,
+ /// A permission card's answer arrived (or couldn't).
+ Answered(String),
+ Sent(String),
+ Connection(String, super::super::connections::Step),
+ Hello,
+ History(String),
+ Thread,
+ Dirs,
+ Pairing,
+ Backends,
+ Update,
+ Saved,
+ Connectors,
+ Skills,
+ Installed,
+ /// Memory, routines, marketplace, sign-in (`manage`).
+ Sheet(Reply),
+}
+
+#[derive(Clone, Copy, PartialEq, Eq, Debug)]
+pub enum Status {
+ Idle,
+ Working,
+ NeedsInput,
+ Error,
+}
+
+/// What a bot's row shows, highest priority first.
+#[derive(Clone, Copy, PartialEq, Eq, Debug)]
+pub enum Mark {
+ Need,
+ Error,
+ Unread,
+ Work,
+ Idle,
+}
+
+impl Mark {
+ pub fn priority(self) -> u8 {
+ match self {
+ Self::Need => 4,
+ Self::Error => 3,
+ Self::Unread => 2,
+ Self::Work => 1,
+ Self::Idle => 0,
+ }
+ }
+}
+
+#[derive(Clone, Debug)]
+#[allow(clippy::struct_excessive_bools, reason = "mirrors the host's bot JSON")]
+pub struct Bot {
+ pub id: String,
+ pub name: String,
+ pub description: String,
+ pub color: String,
+ pub shape: String,
+ pub backend: String,
+ pub cwd: String,
+ pub managed_workspace: bool,
+ pub auto: bool,
+ pub model: Option<String>,
+ pub pinned: bool,
+ pub hidden: bool,
+ pub notify: bool,
+ pub computer: bool,
+ pub connectors: Vec<String>,
+ pub skills: Vec<String>,
+ pub status: Status,
+ pub activity: String,
+ pub started_at: Option<i64>,
+ pub unread: i64,
+ pub last_message: String,
+ pub last_at: i64,
+ /// A group chat: several bots and the user, no agent of its own.
+ pub group: bool,
+ pub members: Vec<String>,
+ /// Where the running turn talks: a chat (`None` = its own) and a thread in it.
+ pub working_chat: Option<String>,
+ pub working_thread: Option<String>,
+}
+
+fn strs(v: &Value) -> Vec<String> {
+ v.as_array().into_iter().flatten().filter_map(Value::as_str).map(str::to_owned).collect()
+}
+
+impl Bot {
+ /// Where it works, as the header shows it.
+ pub fn folder(&self, home: &str) -> String {
+ if self.managed_workspace { "personal workspace".to_owned() } else { tilde(&self.cwd, home) }
+ }
+
+ pub(super) fn parse(v: &Value) -> Option<Self> {
+ let s = |k: &str| v[k].as_str().unwrap_or_default().to_owned();
+ Some(Self {
+ id: v["id"].as_str()?.to_owned(),
+ name: s("name"),
+ description: s("description"),
+ color: v["avatarColor"].as_str().unwrap_or("blue").to_owned(),
+ shape: v["avatarShape"].as_str().unwrap_or("blob").to_owned(),
+ backend: s("backend"),
+ cwd: s("cwd"),
+ managed_workspace: v["managedWorkspace"] == true,
+ auto: v["permission"] == "auto",
+ model: v["model"].as_str().filter(|m| !m.is_empty()).map(str::to_owned),
+ pinned: v["pinned"].as_bool().unwrap_or(false),
+ hidden: v["hidden"].as_bool().unwrap_or(false),
+ notify: v["notify"].as_bool().unwrap_or(true),
+ computer: v["computer"].as_bool().unwrap_or(false),
+ connectors: strs(&v["connectors"]),
+ skills: strs(&v["skills"]),
+ status: match v["status"].as_str() {
+ Some("working") => Status::Working,
+ Some("needsInput") => Status::NeedsInput,
+ Some("error") => Status::Error,
+ _ => Status::Idle,
+ },
+ activity: s("activity"),
+ started_at: v["startedAt"].as_i64(),
+ unread: v["unread"].as_i64().unwrap_or(0),
+ last_message: s("lastMessage"),
+ last_at: v["lastAt"].as_i64().unwrap_or(0),
+ group: v["kind"] == "group",
+ members: strs(&v["members"]),
+ working_chat: v["workingChat"].as_str().map(str::to_owned),
+ working_thread: v["workingThread"].as_str().map(str::to_owned),
+ })
+ }
+
+ /// Whether the running turn talks in this chat: its main lane (`None`) or that thread.
+ pub fn works_in(&self, thread: Option<&str>) -> bool {
+ self.away().is_none() && self.working_thread.as_deref() == thread
+ }
+
+ /// The other chat (a group) the running turn talks in, if it isn't this bot's own.
+ pub fn away(&self) -> Option<&str> {
+ self.working_chat.as_deref().filter(|c| *c != self.id)
+ }
+
+ pub fn mark(&self) -> Mark {
+ match self.status {
+ Status::NeedsInput => Mark::Need,
+ Status::Error => Mark::Error,
+ Status::Working => Mark::Work,
+ Status::Idle if self.unread > 0 => Mark::Unread,
+ Status::Idle => Mark::Idle,
+ }
+ }
+}
+
+#[derive(Clone, Copy, PartialEq, Eq, Debug)]
+pub enum Kind {
+ User,
+ Agent,
+ Thought,
+ Tool,
+ Plan,
+ Permission,
+ Notice,
+ Other,
+}
+
+#[derive(Clone, Debug)]
+pub struct Entry {
+ pub id: String,
+ pub seq: i64,
+ pub turn: i64,
+ pub kind: Kind,
+ pub data: Value,
+ pub created_at: i64,
+ /// The main-chat message whose thread this is in; `None` is the main chat.
+ pub thread_id: Option<String>,
+}
+
+impl Entry {
+ pub(super) fn parse(v: &Value) -> Option<(String, Self)> {
+ let kind = match v["kind"].as_str()? {
+ "user" => Kind::User,
+ "agent" => Kind::Agent,
+ "thought" => Kind::Thought,
+ "tool" => Kind::Tool,
+ "plan" => Kind::Plan,
+ "permission" => Kind::Permission,
+ "notice" => Kind::Notice,
+ _ => Kind::Other,
+ };
+ Some((
+ v["botId"].as_str()?.to_owned(),
+ Self {
+ id: v["id"].as_str()?.to_owned(),
+ seq: v["seq"].as_i64()?,
+ turn: v["turn"].as_i64().unwrap_or(0),
+ kind,
+ data: v["data"].clone(),
+ created_at: v["createdAt"].as_i64().unwrap_or(0),
+ thread_id: v["threadId"].as_str().map(str::to_owned),
+ },
+ ))
+ }
+
+ pub fn text(&self) -> &str {
+ self.data["text"].as_str().unwrap_or_default()
+ }
+
+ /// Files sent with a user message: `(name, size)`.
+ pub fn attachments(&self) -> Vec<(&str, u64)> {
+ self.data["attachments"]
+ .as_array()
+ .into_iter()
+ .flatten()
+ .filter_map(|a| Some((a["name"].as_str()?, a["size"].as_u64().unwrap_or(0))))
+ .collect()
+ }
+
+ /// A notice's line; a voice call gets its length ("Voice chat · 00:16").
+ pub fn notice_text(&self) -> String {
+ match self.data["callSeconds"].as_u64() {
+ Some(s) => format!("{} · {:02}:{:02}", self.text(), s / 60, s % 60),
+ None => self.text().to_owned(),
+ }
+ }
+
+ pub fn is_final(&self) -> bool {
+ self.kind == Kind::Agent && self.data["final"].as_bool().unwrap_or(false)
+ }
+
+ /// What the chat shows as a message: the user's, and each turn's final reply.
+ pub fn is_message(&self) -> bool {
+ self.kind == Kind::User || self.is_final() || self.data["connectionRequest"].is_object()
+ }
+
+ pub fn pending(&self) -> bool {
+ self.kind == Kind::Permission && self.data["status"] == "pending"
+ }
+
+ /// Approval options in the apps' order: allow once, allow always, reject once, reject always.
+ pub fn options(&self) -> Vec<(String, String, String)> {
+ let rank = |k: &str| match k {
+ "allow_once" => 0,
+ "allow_always" => 1,
+ "reject_once" => 2,
+ "reject_always" => 3,
+ _ => 9,
+ };
+ let mut o: Vec<(String, String, String)> = self.data["options"]
+ .as_array()
+ .into_iter()
+ .flatten()
+ .map(|o| {
+ let s = |k: &str| o[k].as_str().unwrap_or_default().to_owned();
+ (s("optionId"), s("name"), s("kind"))
+ })
+ .collect();
+ o.sort_by_key(|(_, _, k)| rank(k));
+ o
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use serde_json::json;
+
+ #[test]
+ fn permission_options_follow_app_order() {
+ let e = Entry {
+ id: "p".into(),
+ seq: 1,
+ turn: 1,
+ kind: Kind::Permission,
+ data: json!({"status": "pending", "options": [
+ {"optionId": "r", "name": "Reject", "kind": "reject_once"},
+ {"optionId": "a", "name": "Allow", "kind": "allow_once"},
+ ]}),
+ created_at: 0,
+ thread_id: None,
+ };
+ assert!(e.pending());
+ let kinds: Vec<String> = e.options().into_iter().map(|o| o.2).collect();
+ assert_eq!(kinds, ["allow_once", "reject_once"]);
+ }
+}
diff --git a/host/src/tui/app/navigate.rs b/host/src/tui/app/navigate.rs
new file mode 100644
index 00000000..312cfbbf
--- /dev/null
+++ b/host/src/tui/app/navigate.rs
@@ -0,0 +1,210 @@
+//! 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, Msg, TraceMode, Width, copy};
+
+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 main chat's messages (none picked: the newest).
+ pub(super) fn step_pick(&mut self, d: isize) {
+ let Some(id) = self.selected.clone() else { return };
+ let ids: Vec<String> = self.lane(&id).into_iter().filter(|e| e.is_message()).map(|e| e.id.clone()).collect();
+ if ids.is_empty() {
+ self.pick = None;
+ self.flash("No message to reply to yet");
+ return;
+ }
+ let at = self.pick.as_ref().and_then(|p| ids.iter().position(|x| x == p));
+ let i = at.map_or(ids.len() - 1, |i| i.saturating_add_signed(d).min(ids.len() - 1));
+ self.pick = Some(ids[i].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::Enter if picked.as_ref().is_some_and(|e| e.data["connectionRequest"]["status"] == "pending") => {
+ self.open_connection();
+ }
+ KeyCode::Enter | KeyCode::Char('r') if self.thread.is_some() => {
+ self.pick = None;
+ self.start_typing();
+ }
+ KeyCode::Enter | KeyCode::Char('r') => {
+ if let Some(root) = self.pick.take() {
+ self.open_thread(root);
+ self.start_typing();
+ }
+ }
+ 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.data);
+ }
+ }
+ KeyCode::Char('c') => {
+ if let Some(e) = picked {
+ copy(e.text());
+ self.flash("Copied");
+ }
+ }
+ KeyCode::Esc => self.pick = None,
+ _ => return false,
+ }
+ true
+ }
+
+ pub(super) fn open_thread(&mut self, root: String) {
+ let Some(id) = self.selected.clone() else { return };
+ self.pick = None;
+ self.chat_scroll = 0;
+ self.trace_turn = None;
+ self.trace_scroll = 0;
+ self.chat_page = true;
+ self.focus = Focus::Chat;
+ if self.trace == TraceMode::Full {
+ self.trace = TraceMode::Off;
+ }
+ self.call("thread", json!({"botId": id, "rootId": root}), After::Thread);
+ self.thread = Some(root);
+ }
+
+ 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");
+ }
+}
diff --git a/host/src/tui/app/overlay_keys.rs b/host/src/tui/app/overlay_keys.rs
new file mode 100644
index 00000000..e68e5339
--- /dev/null
+++ b/host/src/tui/app/overlay_keys.rs
@@ -0,0 +1,260 @@
+//! Keys in the pickers: go-to, actions, agents and folders.
+
+use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
+use serde_json::{Value, json};
+
+use super::super::manage::Reply;
+use super::{
+ ACTIONS, Action, After, AgentPicker, App, Bot, ConfirmAct, Dir, Editor, FILTERS, FolderPicker, Goto, GotoItem,
+ NEW_GROUP, Overlay, contains, fuzzy,
+};
+
+impl App {
+ /// Agents offered for a new bot: runnable ones on this computer first.
+ pub fn agent_choices(&self, query: &str) -> Vec<&Value> {
+ let mut v: Vec<&Value> = self
+ .backends
+ .iter()
+ .filter(|b| b["available"].as_bool().unwrap_or(false) || b["registry"].is_string())
+ .filter(|b| fuzzy(query, &[b["name"].as_str().unwrap_or_default(), b["id"].as_str().unwrap_or_default()]))
+ .collect();
+ v.sort_by_key(|b| !b["installed"].as_bool().unwrap_or(false));
+ v
+ }
+
+ pub fn goto_items(&self, g: &Goto) -> Vec<GotoItem> {
+ let q = g.query.text.trim();
+ let want = FILTERS[g.filter].1;
+ let mut bots: Vec<&Bot> = self
+ .bots
+ .values()
+ .filter(|b| want.is_none_or(|m| b.mark() == m))
+ .filter(|b| fuzzy(q, &[&b.name, &b.backend]) || contains(q, &[&b.cwd, &b.last_message]))
+ .collect();
+ bots.sort_by(|a, b| {
+ b.mark().priority().cmp(&a.mark().priority()).then(b.last_at.cmp(&a.last_at)).then(a.name.cmp(&b.name))
+ });
+ let mut items: Vec<GotoItem> = bots.into_iter().map(|b| GotoItem::Bot(b.id.clone())).collect();
+ if want.is_none() {
+ items.extend(
+ ACTIONS.iter().filter(|(label, _, _)| fuzzy(q, &[label])).map(|(_, _, a)| GotoItem::Action(*a)),
+ );
+ }
+ items
+ }
+
+ fn run_action(&mut self, a: Action) {
+ match a {
+ Action::NewBot => self.new_bot(),
+ Action::NewGroup => self.new_group(),
+ Action::Market => self.open_market(),
+ Action::Memory => self.open_memory(),
+ Action::Routines => self.open_routines(),
+ Action::RefreshAgents => self.call("refreshBackends", json!({}), After::Backends),
+ Action::Pair => self.open_pair(),
+ Action::Usage => self.overlays.push(Overlay::Usage),
+ Action::CheckUpdate => self.call("checkHostUpdate", json!({}), After::Update),
+ Action::Keys => self.overlays.push(Overlay::Help(Editor::default())),
+ }
+ }
+
+ pub(super) fn overlay_key(&mut self, k: KeyEvent) {
+ let Some(top) = self.overlays.pop() else { return };
+ let depth = self.overlays.len();
+ let keep = match top {
+ Overlay::Goto(g) => self.goto_key(g, k),
+ Overlay::Help(mut e) => {
+ if k.code == KeyCode::Esc || k.code == KeyCode::Char('?') && e.text.is_empty() {
+ None
+ } else {
+ if k.code != KeyCode::Char('/') || !e.text.is_empty() {
+ e.key(k);
+ }
+ Some(Overlay::Help(e))
+ }
+ }
+ Overlay::Confirm(c) => match k.code {
+ KeyCode::Enter => {
+ let changed = After::Sheet(Reply::Changed);
+ match &c.act {
+ ConfirmAct::Delete(id) => self.call("deleteBot", json!({"botId": id}), After::Nothing),
+ ConfirmAct::NewSession(id) => self.call("newSession", json!({"botId": id}), After::Nothing),
+ ConfirmAct::ClearMemory(id) => self.call("clearMemory", json!({"botId": id}), changed),
+ ConfirmAct::DeleteRoutine(bot, id) => {
+ self.call("deleteRoutine", json!({"botId": bot, "id": id}), changed);
+ }
+ ConfirmAct::RotateRoutineKey(bot, id) => self.call(
+ "routineWebhook",
+ json!({"botId": bot, "id": id, "rotate": true}),
+ After::Sheet(Reply::Webhook),
+ ),
+ ConfirmAct::RemoveConnector(id) => self.call("removeConnector", json!({"id": id}), changed),
+ ConfirmAct::RemoveSkill(id) => self.call("removeSkill", json!({"id": id}), changed),
+ }
+ None
+ }
+ KeyCode::Esc | KeyCode::Char('q') => None,
+ _ => Some(Overlay::Confirm(c)),
+ },
+ Overlay::Agents(a) => self.agents_key(a, k),
+ Overlay::Folder(p) => self.folder_key(p, k),
+ Overlay::Form(f) => self.form_key(f, k),
+ Overlay::Group(g) => self.group_key(g, k),
+ Overlay::Memory(m) => self.memory_key(m, k),
+ Overlay::Routines(l) => self.routines_key(l, k),
+ Overlay::Routine(f) => self.routine_key(f, k),
+ Overlay::Market(m) => self.market_key(m, k),
+ Overlay::Agent(a) => self.agent_key(a, k),
+ Overlay::Fields(f) => self.fields_key(f, k),
+ o @ (Overlay::Usage | Overlay::Pair(_)) => {
+ if matches!(k.code, KeyCode::Esc | KeyCode::Enter | KeyCode::Char('q' | 'U' | 'P')) {
+ None
+ } else {
+ Some(o)
+ }
+ }
+ };
+ if let Some(o) = keep {
+ // Anything the handler opened stays on top of the overlay it came from.
+ self.overlays.insert(depth.min(self.overlays.len()), o);
+ }
+ }
+
+ fn goto_key(&mut self, mut g: Goto, k: KeyEvent) -> Option<Overlay> {
+ let items_len = self.goto_items(&g).len();
+ match k.code {
+ KeyCode::Esc => return None,
+ KeyCode::Tab => {
+ g.filter = (g.filter + 1) % FILTERS.len();
+ g.cursor = 0;
+ }
+ KeyCode::BackTab => {
+ g.filter = (g.filter + FILTERS.len() - 1) % FILTERS.len();
+ g.cursor = 0;
+ }
+ KeyCode::Down => g.cursor = (g.cursor + 1).min(items_len.saturating_sub(1)),
+ KeyCode::Up => g.cursor = g.cursor.saturating_sub(1),
+ KeyCode::Char('n' | 'j') if k.modifiers.contains(KeyModifiers::CONTROL) => {
+ g.cursor = (g.cursor + 1).min(items_len.saturating_sub(1));
+ }
+ KeyCode::Char('p') if k.modifiers.contains(KeyModifiers::CONTROL) => g.cursor = g.cursor.saturating_sub(1),
+ KeyCode::Enter => {
+ let items = self.goto_items(&g);
+ match items.get(g.cursor) {
+ Some(GotoItem::Bot(id)) => {
+ let id = id.clone();
+ self.select(&id);
+ self.start_typing();
+ }
+ Some(GotoItem::Action(a)) => self.run_action(*a),
+ None => {}
+ }
+ return None;
+ }
+ _ => {
+ if g.query.key(k) {
+ g.cursor = 0;
+ }
+ }
+ }
+ Some(Overlay::Goto(g))
+ }
+
+ /// The agent picker's last row, "New group chat", when the filter matches it.
+ pub fn agent_group_row(query: &str) -> bool {
+ fuzzy(query, &[NEW_GROUP])
+ }
+
+ fn agents_key(&mut self, mut a: AgentPicker, k: KeyEvent) -> Option<Overlay> {
+ let agents = self.agent_choices(&a.query.text).len();
+ let n = agents + usize::from(Self::agent_group_row(&a.query.text));
+ match k.code {
+ KeyCode::Esc => return None,
+ KeyCode::Down => a.cursor = (a.cursor + 1).min(n.saturating_sub(1)),
+ KeyCode::Up => a.cursor = a.cursor.saturating_sub(1),
+ KeyCode::Enter if a.cursor == agents && Self::agent_group_row(&a.query.text) => {
+ self.new_group();
+ return None;
+ }
+ KeyCode::Enter => {
+ let backend =
+ self.agent_choices(&a.query.text).get(a.cursor).and_then(|b| b["id"].as_str()).map(str::to_owned);
+ if let Some(backend) = backend {
+ self.overlays.push(Overlay::Agents(a));
+ self.new_bot_form(backend);
+ return None;
+ }
+ }
+ _ => {
+ if a.query.key(k) {
+ a.cursor = 0;
+ }
+ }
+ }
+ Some(Overlay::Agents(a))
+ }
+
+ pub(super) fn open_folder(&mut self, start: &str) {
+ self.overlays.push(Overlay::Folder(FolderPicker {
+ path: start.to_owned(),
+ parent: None,
+ dirs: vec![],
+ query: Editor::default(),
+ cursor: 0,
+ error: None,
+ }));
+ let body = if start.is_empty() { json!({}) } else { json!({"path": start}) };
+ self.call("listDirs", body, After::Dirs);
+ }
+
+ pub fn folder_matches(p: &FolderPicker) -> Vec<&Dir> {
+ p.dirs.iter().filter(|d| fuzzy(&p.query.text, &[&d.name])).collect()
+ }
+
+ fn folder_key(&mut self, mut p: FolderPicker, k: KeyEvent) -> Option<Overlay> {
+ let matches: Vec<Dir> = Self::folder_matches(&p).into_iter().cloned().collect();
+ match k.code {
+ KeyCode::Esc => return None,
+ KeyCode::Down => p.cursor = (p.cursor + 1).min(matches.len().saturating_sub(1)),
+ KeyCode::Up => p.cursor = p.cursor.saturating_sub(1),
+ KeyCode::Right | KeyCode::Tab => {
+ if let Some(d) = matches.get(p.cursor) {
+ let path = d.path.clone();
+ self.call("listDirs", json!({"path": path}), After::Dirs);
+ }
+ }
+ KeyCode::Left | KeyCode::Backspace if p.query.text.is_empty() => {
+ if let Some(parent) = p.parent.clone() {
+ self.call("listDirs", json!({"path": parent}), After::Dirs);
+ }
+ }
+ KeyCode::Enter => {
+ // Enter on a highlighted child uses it; with nothing typed and no match, the current folder.
+ let chosen = if p.query.text.is_empty() && k.modifiers.contains(KeyModifiers::ALT) {
+ p.path.clone()
+ } else {
+ matches.get(p.cursor).map_or_else(|| p.path.clone(), |d| d.path.clone())
+ };
+ self.folder_chosen(chosen);
+ return None;
+ }
+ KeyCode::Char('.') if p.query.text.is_empty() => {
+ let chosen = p.path.clone();
+ self.folder_chosen(chosen);
+ return None;
+ }
+ _ => {
+ if p.query.key(k) {
+ p.cursor = 0;
+ }
+ }
+ }
+ Some(Overlay::Folder(p))
+ }
+
+ fn folder_chosen(&mut self, path: String) {
+ if let Some(Overlay::Form(f)) = self.overlays.last_mut() {
+ f.cwd = path;
+ }
+ }
+}
diff --git a/host/src/tui/app/ui.rs b/host/src/tui/app/ui.rs
new file mode 100644
index 00000000..9fa03afc
--- /dev/null
+++ b/host/src/tui/app/ui.rs
@@ -0,0 +1,271 @@
+//! The screen's state: panes, overlays and their forms, and where clicks land.
+
+use ratatui::layout::Rect;
+use std::time::Instant;
+
+use super::super::manage::{AgentSetup, Fields, Market, MemorySheet, RoutineForm, RoutineList};
+use super::{Editor, Mark};
+
+#[derive(Clone, Copy, PartialEq, Eq, Debug)]
+pub enum Focus {
+ Roster,
+ Chat,
+ Trace,
+}
+
+#[derive(Clone, Copy, PartialEq, Eq, Debug)]
+pub enum TraceMode {
+ Off,
+ Pane,
+ Full,
+}
+
+/// Width class, set by the last draw.
+#[derive(Clone, Copy, PartialEq, Eq, Debug)]
+pub enum Width {
+ /// < 80 columns: roster and chat are separate pages.
+ Narrow,
+ /// 80–99: compact roster.
+ Mid,
+ /// 100–149.
+ Wide,
+ /// ≥ 150.
+ Huge,
+}
+
+pub const FILTERS: [(&str, Option<Mark>); 5] = [
+ ("all", None),
+ ("needs you", Some(Mark::Need)),
+ ("working", Some(Mark::Work)),
+ ("unread", Some(Mark::Unread)),
+ ("error", Some(Mark::Error)),
+];
+
+#[derive(Clone, Copy, PartialEq, Eq, Debug)]
+pub enum Action {
+ NewBot,
+ NewGroup,
+ Market,
+ Memory,
+ Routines,
+ RefreshAgents,
+ Pair,
+ Usage,
+ CheckUpdate,
+ Keys,
+}
+
+pub const NEW_GROUP: &str = "New group chat";
+
+pub const ACTIONS: [(&str, &str, Action); 10] = [
+ ("New bot…", "n", Action::NewBot),
+ ("New group chat…", "m", Action::NewGroup),
+ ("Marketplace: agents, connectors, skills…", "A", Action::Market),
+ ("Memory of this bot…", "M", Action::Memory),
+ ("Routines of this bot…", "R", Action::Routines),
+ ("Refresh agents", "", Action::RefreshAgents),
+ ("Pair a phone…", "P", Action::Pair),
+ ("Usage", "U", Action::Usage),
+ ("Check for updates", "", Action::CheckUpdate),
+ ("Keys", "?", Action::Keys),
+];
+
+pub enum GotoItem {
+ Bot(String),
+ Action(Action),
+}
+
+pub struct Goto {
+ pub query: Editor,
+ pub filter: usize,
+ pub cursor: usize,
+}
+
+pub enum ConfirmAct {
+ Delete(String),
+ NewSession(String),
+ ClearMemory(String),
+ DeleteRoutine(String, String),
+ RotateRoutineKey(String, String),
+ RemoveConnector(String),
+ RemoveSkill(String),
+}
+
+pub struct Confirm {
+ pub title: String,
+ pub detail: String,
+ pub note: String,
+ pub button: &'static str,
+ pub act: ConfirmAct,
+}
+
+pub struct AgentPicker {
+ pub query: Editor,
+ pub cursor: usize,
+}
+
+#[derive(Clone)]
+pub struct Dir {
+ pub name: String,
+ pub path: String,
+ pub git: bool,
+}
+
+pub struct FolderPicker {
+ pub path: String,
+ pub parent: Option<String>,
+ pub dirs: Vec<Dir>,
+ pub query: Editor,
+ pub cursor: usize,
+ pub error: Option<String>,
+}
+
+pub const COLORS: [&str; 11] =
+ ["black", "brown", "red", "orange", "yellow", "green", "cyan", "blue", "violet", "magenta", "gray"];
+pub const SHAPES: [&str; 8] = ["blob", "pebble", "squircle", "tablet", "wedge", "hex", "cloud", "teardrop"];
+
+#[derive(Clone, Copy, PartialEq, Eq, Debug)]
+pub enum Field {
+ Name,
+ Instructions,
+ Agent,
+ Model,
+ Folder,
+ Approvals,
+ Notify,
+ Computer,
+ Color,
+ Shape,
+ Connectors,
+ Skills,
+}
+
+pub const FIELDS: [Field; 12] = [
+ Field::Name,
+ Field::Instructions,
+ Field::Agent,
+ Field::Model,
+ Field::Folder,
+ Field::Approvals,
+ Field::Notify,
+ Field::Computer,
+ Field::Color,
+ Field::Shape,
+ Field::Connectors,
+ Field::Skills,
+];
+
+pub struct Toggle {
+ pub id: String,
+ pub name: String,
+ pub on: bool,
+}
+
+#[allow(clippy::struct_excessive_bools, reason = "independent bot settings and save state")]
+pub struct Form {
+ pub bot_id: Option<String>,
+ pub name: Editor,
+ pub instructions: Editor,
+ pub backend: String,
+ pub model: Editor,
+ pub cwd: String,
+ pub auto: bool,
+ pub notify: bool,
+ pub computer: bool,
+ pub color: usize,
+ pub shape: usize,
+ pub connectors: Vec<Toggle>,
+ pub skills: Vec<Toggle>,
+ pub field: usize,
+ /// Cursor inside the connector / skill toggles.
+ pub sub: usize,
+ pub error: Option<String>,
+ pub saving: bool,
+}
+
+impl Form {
+ pub fn current(&self) -> Field {
+ FIELDS[self.field]
+ }
+}
+
+#[derive(Clone, Copy, PartialEq, Eq, Debug)]
+pub enum GroupField {
+ Name,
+ About,
+ Bots,
+}
+
+impl GroupField {
+ pub(super) fn next(self) -> Self {
+ match self {
+ Self::Name => Self::About,
+ Self::About => Self::Bots,
+ Self::Bots => Self::Name,
+ }
+ }
+
+ pub(super) fn prev(self) -> Self {
+ self.next().next()
+ }
+}
+
+/// Create a group chat, or rename one and change who's in it.
+pub struct GroupForm {
+ /// The group being edited; `None` creates one.
+ pub group_id: Option<String>,
+ pub name: Editor,
+ /// What the group is for; sent as `description`, shown to its members.
+ pub about: Editor,
+ /// Picked bot ids, in the order they were picked.
+ pub members: Vec<String>,
+ pub field: GroupField,
+ pub cursor: usize,
+ pub error: Option<String>,
+ pub saving: bool,
+}
+
+pub enum Overlay {
+ Goto(Goto),
+ Help(Editor),
+ Confirm(Confirm),
+ Agents(AgentPicker),
+ Folder(FolderPicker),
+ Form(Box<Form>),
+ Group(GroupForm),
+ Usage,
+ Pair(Option<String>),
+ Memory(MemorySheet),
+ Routines(RoutineList),
+ Routine(Box<RoutineForm>),
+ Market(Box<Market>),
+ Agent(AgentSetup),
+ Fields(Box<Fields>),
+}
+
+pub struct Toast {
+ pub text: String,
+ pub need: bool,
+ pub until: Instant,
+}
+
+#[derive(Clone)]
+pub enum Click {
+ Bot(String),
+ Option {
+ entry: String,
+ option: String,
+ },
+ Filter(usize),
+ Composer,
+ /// The replies line under a message: opens its thread.
+ Thread(String),
+}
+
+#[derive(Default)]
+pub struct Hits {
+ pub roster: Rect,
+ pub chat: Rect,
+ pub trace: Rect,
+ pub clicks: Vec<(Rect, Click)>,
+}