AnyBot/src/app/attach_ui.rs

211 lines
9.5 KiB
Rust
Raw Normal View History

// Attachments in the window: the chips above the message box, the + button, dropping files on
// the window, pasting a picture with ⌘V, and the thumbnails in sent messages.
use super::*;
use crate::attach::{self, Attachment};
#[derive(Default)]
pub struct AttachUi {
pub pending: HashMap<u64, Vec<Attachment>>,
picked: Option<Receiver<(u64, Vec<PathBuf>)>>,
pub(super) thumbs: HashMap<String, Option<egui::TextureHandle>>,
pub error: Option<String>,
Voice: its own 設定 › 語音, run by the brain for every screen and the phone Voice was three implementations that broke on their own: speech to text borrowed the OpenAI key (so with only Grok set up the mic recorded, then failed), OpenAI's models it hard-coded stop on 2027-02-26, read aloud was macOS `say` or espeak-ng (silently nothing on Windows, where espeak isn't installed), and the phone used the browser's own recognition. Now: - 設定 › 語音 has 聽 and 說, each a service: off, OpenAI-compatible (OpenAI, Groq, a Whisper server of your own: the address decides), xAI or ElevenLabs. An empty key borrows 設定 › 模型's for the same service. Each side says 可以用 or what it lacks; 聽 has 試試看 (record a sentence, show what was heard), 說 has 試聽 and the voice list. speech.rs does the calls; xAI's both ways were checked for real (Mandarin read, then heard back, in Traditional characters). - The brain does it all: the phone POSTs its recording (MediaRecorder) to /api/voice/transcribe, screens to /api/sync/voice/transcribe; answers come back as MP3 from …/voice/speech and are played on the device (rodio; an <audio> on the phone, 「唸出來」 in a message's menu). Keys stay on the brain (the voice keys are redacted for screens like the others). - No service, no voice anywhere: the mic, 語音聊天, 會議錄音 and 朗讀 only appear once 聽 (and 說) can be called; the phone's state says which. Without voice the send arrow just rests where 語音聊天 was. - 會議錄音 is uploaded to the brain, which turns it into text in the background and hands it to the bot itself: a meeting is longer than the 35 s the account lets a request wait, and a brain on a server has no window (the jobs are polled in background()). - Recognisers answer Mandarin in Simplified: in a Traditional Chinese window or phone the text becomes Traditional with Taiwan wording. - Older saves: whoever had an OpenAI key keeps voice input (聽 = OpenAI); everyone else starts with it off. Bots' macOS voice names are cleared. 對話詳細資訊's 加入會議 started plain voice input; it starts a meeting now.
2026-11-10 14:58:18 +00:00
pub(super) voice: Option<(u64, crate::voice::Recording)>,
pub(super) voice_rx: Option<Receiver<(u64, Result<String, String>)>>,
/// The recording is a meeting (longer; becomes a transcript to summarize).
pub meeting: bool,
/// 通話模式: the bot being talked to, and where the call is.
pub call: Option<(u64, CallPhase)>,
Voice: its own 設定 › 語音, run by the brain for every screen and the phone Voice was three implementations that broke on their own: speech to text borrowed the OpenAI key (so with only Grok set up the mic recorded, then failed), OpenAI's models it hard-coded stop on 2027-02-26, read aloud was macOS `say` or espeak-ng (silently nothing on Windows, where espeak isn't installed), and the phone used the browser's own recognition. Now: - 設定 › 語音 has 聽 and 說, each a service: off, OpenAI-compatible (OpenAI, Groq, a Whisper server of your own: the address decides), xAI or ElevenLabs. An empty key borrows 設定 › 模型's for the same service. Each side says 可以用 or what it lacks; 聽 has 試試看 (record a sentence, show what was heard), 說 has 試聽 and the voice list. speech.rs does the calls; xAI's both ways were checked for real (Mandarin read, then heard back, in Traditional characters). - The brain does it all: the phone POSTs its recording (MediaRecorder) to /api/voice/transcribe, screens to /api/sync/voice/transcribe; answers come back as MP3 from …/voice/speech and are played on the device (rodio; an <audio> on the phone, 「唸出來」 in a message's menu). Keys stay on the brain (the voice keys are redacted for screens like the others). - No service, no voice anywhere: the mic, 語音聊天, 會議錄音 and 朗讀 only appear once 聽 (and 說) can be called; the phone's state says which. Without voice the send arrow just rests where 語音聊天 was. - 會議錄音 is uploaded to the brain, which turns it into text in the background and hands it to the bot itself: a meeting is longer than the 35 s the account lets a request wait, and a brain on a server has no window (the jobs are polled in background()). - Recognisers answer Mandarin in Simplified: in a Traditional Chinese window or phone the text becomes Traditional with Taiwan wording. - Older saves: whoever had an OpenAI key keeps voice input (聽 = OpenAI); everyone else starts with it off. Bots' macOS voice names are cleared. 對話詳細資訊's 加入會議 started plain voice input; it starts a meeting now.
2026-11-10 14:58:18 +00:00
pub(super) call_rec: Option<crate::voice::Recording>,
pub(super) call_rx: Option<Receiver<Result<String, String>>>,
}
#[derive(Clone, Copy, PartialEq, Debug)]
pub enum CallPhase {
Listening,
Transcribing,
Thinking,
Speaking,
}
impl CallPhase {
pub fn label(self) -> &'static str {
match self {
CallPhase::Listening => "正在聽…",
CallPhase::Transcribing => "聽懂中…",
CallPhase::Thinking => "想一下…",
CallPhase::Speaking => "正在說…",
}
}
}
impl App {
fn add_attachment_paths(&mut self, bot_id: u64, paths: Vec<PathBuf>) {
for path in paths {
match attach::store(bot_id, &path) {
Ok(a) => self.att.pending.entry(bot_id).or_default().push(a),
Err(e) => self.att.error = Some(e),
}
}
}
/// Files dropped on the window, the picker's answer, and ⌘V with a picture on the clipboard.
pub(super) fn attachments_poll(&mut self, ctx: &egui::Context, bot_id: u64, composer_focused: bool) {
// (While 傳送到其他裝置 is open, dropped files are for sending, not for this chat.)
let dropped: Vec<PathBuf> = if self.drop_page.is_some() { vec![] } else { ctx.input(|i| i.raw.dropped_files.iter().map(|f| f.path().to_path_buf()).filter(|p| !p.as_os_str().is_empty()).collect()) };
if !dropped.is_empty() {
self.add_attachment_paths(bot_id, dropped);
}
if let Some(rx) = &self.att.picked {
if let Ok((id, paths)) = rx.try_recv() {
self.att.picked = None;
self.add_attachment_paths(id, paths);
}
}
Bug sweep: gates, relay 2FA, Windows input, snapshots, encodings Permissions and reach - a file leaving a computer passes the same questions as reading it; "looking" shell commands that do more than look ask first - a symlink out of the work/sealed folder counts as outside it (real paths, not just lexical ones); the search walk skips links - web_fetch and skill_install read the public web only: localhost, the LAN and cloud metadata are refused, at every redirect - preview shows a file without running it; browser_upload may ask - vault: logins no longer match across shared suffixes (pages.dev, com.tw, ...); registrable_domain knows public suffixes - axwrite: found elements are retained (use-after-free) Relay - replacing or turning off 2FA needs the current code (rate-limited); new recovery codes only count once enrollment is verified - a removed computer loses its socket and its `call` access - a site name taken by another account answers 409 - RELAY_TRUST_PROXY: the login lock counts the caller's address, not the proxy's (compose files and README) - httpd: half a body is not a request Desktop - sign-in, join and restore work with 2FA on (code field; --serve reads ANYBOT_ACCOUNT_CODE; the setup wizard points to a pairing code) - Windows: text was typed twice; modifiers are held through click, drag and scroll; horizontal scroll; screen control is no longer refused - a recording that hits the limit is transcribed, not thrown away - bot's computer: a new home volume restores the snapshot; a snapshot is read through before the home is wiped; a failed volume removal stops reset/restore - events of a stopped turn no longer land in the next one; each channel message gets a turn of its own - saving a bot's settings keeps what it got meanwhile (three-way merge); a save file that cannot be read is kept - pages in Big5/GBK/Shift_JIS are read in their own encoding (encoding_rs); osascript output past 64 KB no longer stalls - pick_tab keeps a job's own non-http tab; pictures larger than the texture limit are fitted; the auto-update time field takes typing - skills live in <data>/skills; memories are unnumbered in the prompt; the phone may skillify and skill_install CI - the test step fails when the tests fail (it piped into tail) Tests: anybot 268 passed, 27 ignored; relay 16 passed; clippy unchanged. Not run: Windows on a real machine (win.rs type-checked alone), the Docker paths of the bot's computer, the live accessibility writes. Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-10 03:46:17 +00:00
// ⌘V with no text on the clipboard: maybe a picture. The window answers the paste keys
// itself and hands over the picture (no V key arrives for ⌘V); a V key with ⌘ that does
// arrive (⌘⇧V) looks at the clipboard as before.
let pasted_text = ctx.input(|i| i.events.iter().any(|e| matches!(e, egui::Event::Paste(t) if !t.is_empty())));
Bug sweep: gates, relay 2FA, Windows input, snapshots, encodings Permissions and reach - a file leaving a computer passes the same questions as reading it; "looking" shell commands that do more than look ask first - a symlink out of the work/sealed folder counts as outside it (real paths, not just lexical ones); the search walk skips links - web_fetch and skill_install read the public web only: localhost, the LAN and cloud metadata are refused, at every redirect - preview shows a file without running it; browser_upload may ask - vault: logins no longer match across shared suffixes (pages.dev, com.tw, ...); registrable_domain knows public suffixes - axwrite: found elements are retained (use-after-free) Relay - replacing or turning off 2FA needs the current code (rate-limited); new recovery codes only count once enrollment is verified - a removed computer loses its socket and its `call` access - a site name taken by another account answers 409 - RELAY_TRUST_PROXY: the login lock counts the caller's address, not the proxy's (compose files and README) - httpd: half a body is not a request Desktop - sign-in, join and restore work with 2FA on (code field; --serve reads ANYBOT_ACCOUNT_CODE; the setup wizard points to a pairing code) - Windows: text was typed twice; modifiers are held through click, drag and scroll; horizontal scroll; screen control is no longer refused - a recording that hits the limit is transcribed, not thrown away - bot's computer: a new home volume restores the snapshot; a snapshot is read through before the home is wiped; a failed volume removal stops reset/restore - events of a stopped turn no longer land in the next one; each channel message gets a turn of its own - saving a bot's settings keeps what it got meanwhile (three-way merge); a save file that cannot be read is kept - pages in Big5/GBK/Shift_JIS are read in their own encoding (encoding_rs); osascript output past 64 KB no longer stalls - pick_tab keeps a job's own non-http tab; pictures larger than the texture limit are fitted; the auto-update time field takes typing - skills live in <data>/skills; memories are unnumbered in the prompt; the phone may skillify and skill_install CI - the test step fails when the tests fail (it piped into tail) Tests: anybot 268 passed, 27 ignored; relay 16 passed; clippy unchanged. Not run: Windows on a real machine (win.rs type-checked alone), the Docker paths of the bot's computer, the live accessibility writes. Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-10 03:46:17 +00:00
let pasted_image = ctx.input(|i| i.events.iter().find_map(|e| if let egui::Event::PasteImage(img) = e { Some(img.clone()) } else { None }));
let cmd_v = ctx.input(|i| i.events.iter().any(|e| matches!(e, egui::Event::Key { key: Key::V, pressed: true, modifiers, .. } if modifiers.command)));
Bug sweep: gates, relay 2FA, Windows input, snapshots, encodings Permissions and reach - a file leaving a computer passes the same questions as reading it; "looking" shell commands that do more than look ask first - a symlink out of the work/sealed folder counts as outside it (real paths, not just lexical ones); the search walk skips links - web_fetch and skill_install read the public web only: localhost, the LAN and cloud metadata are refused, at every redirect - preview shows a file without running it; browser_upload may ask - vault: logins no longer match across shared suffixes (pages.dev, com.tw, ...); registrable_domain knows public suffixes - axwrite: found elements are retained (use-after-free) Relay - replacing or turning off 2FA needs the current code (rate-limited); new recovery codes only count once enrollment is verified - a removed computer loses its socket and its `call` access - a site name taken by another account answers 409 - RELAY_TRUST_PROXY: the login lock counts the caller's address, not the proxy's (compose files and README) - httpd: half a body is not a request Desktop - sign-in, join and restore work with 2FA on (code field; --serve reads ANYBOT_ACCOUNT_CODE; the setup wizard points to a pairing code) - Windows: text was typed twice; modifiers are held through click, drag and scroll; horizontal scroll; screen control is no longer refused - a recording that hits the limit is transcribed, not thrown away - bot's computer: a new home volume restores the snapshot; a snapshot is read through before the home is wiped; a failed volume removal stops reset/restore - events of a stopped turn no longer land in the next one; each channel message gets a turn of its own - saving a bot's settings keeps what it got meanwhile (three-way merge); a save file that cannot be read is kept - pages in Big5/GBK/Shift_JIS are read in their own encoding (encoding_rs); osascript output past 64 KB no longer stalls - pick_tab keeps a job's own non-http tab; pictures larger than the texture limit are fitted; the auto-update time field takes typing - skills live in <data>/skills; memories are unnumbered in the prompt; the phone may skillify and skill_install CI - the test step fails when the tests fail (it piped into tail) Tests: anybot 268 passed, 27 ignored; relay 16 passed; clippy unchanged. Not run: Windows on a real machine (win.rs type-checked alone), the Docker paths of the bot's computer, the live accessibility writes. Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-10 03:46:17 +00:00
if !composer_focused || self.demo {
return;
}
if let Some(img) = pasted_image {
let rgba: Vec<u8> = img.pixels.iter().flat_map(|c| c.to_srgba_unmultiplied()).collect();
match attach::store_pasted(bot_id, img.size, rgba) {
Ok(a) => self.att.pending.entry(bot_id).or_default().push(a),
Err(e) => self.att.error = Some(e),
}
} else if cmd_v && !pasted_text {
if let Some(a) = attach::paste_image(bot_id) {
self.att.pending.entry(bot_id).or_default().push(a);
}
}
}
pub(super) fn pick_attachments(&mut self, ctx: &egui::Context, bot_id: u64) {
let (tx, rx) = channel();
self.att.picked = Some(rx);
let c = ctx.clone();
std::thread::spawn(move || {
let paths = attach::pick_files();
let _ = tx.send((bot_id, paths));
c.request_repaint();
});
}
/// "Drop to attach" while files hover over the window.
pub(super) fn drop_overlay(&self, ctx: &egui::Context) {
if self.drop_page.is_some() || !ctx.input(|i| !i.raw.hovered_files.is_empty()) {
return;
}
let p = self.pal;
let screen = ctx.content_rect();
let painter = ctx.layer_painter(egui::LayerId::new(egui::Order::Foreground, egui::Id::unique("drop-overlay")));
painter.rect_filled(screen, 0, p.pane.gamma_multiply(0.85));
let inner = screen.shrink(24.0);
painter.rect_stroke(inner, 18, Stroke::new(2.0, p.accent), egui::StrokeKind::Inside);
painter.text(inner.center(), egui::Align2::CENTER_CENTER, "放開以附加檔案", egui::FontId::proportional(FONT_XL), p.accent);
}
fn thumb(&mut self, ctx: &egui::Context, path: &str) -> Option<egui::TextureHandle> {
if let Some(t) = self.att.thumbs.get(path) {
return t.clone();
}
let t = image::open(path).ok().map(|img| {
let img = img.thumbnail(240, 240).to_rgba8();
let size = [img.width() as usize, img.height() as usize];
ctx.load_texture(format!("thumb-{path}"), egui::ColorImage::from_rgba_unmultiplied(size, img.as_raw()), egui::TextureOptions::LINEAR)
});
self.att.thumbs.insert(path.to_string(), t.clone());
t
}
/// One attachment: a picture thumbnail, or a file chip. Returns true when its × was clicked.
pub(super) fn attachment_chip(&mut self, ui: &mut egui::Ui, a: &Attachment, removable: bool) -> bool {
let p = self.pal;
let ctx = ui.ctx().clone();
let mut remove = false;
let thumb = if a.image { self.thumb(&ctx, &a.path) } else { None };
let frame = Frame::new().fill(p.hover).corner_radius(10).inner_margin(if thumb.is_some() { Margin::same(3) } else { Margin::symmetric(10, 6) });
let r = frame
.show(ui, |ui| {
ui.horizontal(|ui| {
if let Some(t) = &thumb {
let s = t.size_vec2();
let k = (64.0 / s.y).min(120.0 / s.x);
ui.add(egui::Image::new((t.id(), s * k)).corner_radius(8));
} else {
let (rect, _) = ui.allocate_exact_size(vec2(14.0, 16.0), egui::Sense::hover());
let r = rect.shrink2(vec2(1.5, 1.0));
ui.painter().rect_stroke(r, 2.0, Stroke::new(1.3, p.text2), egui::StrokeKind::Middle);
ui.painter().line_segment([r.left_top() + vec2(3.0, 5.0), r.right_top() + vec2(-3.0, 5.0)], Stroke::new(1.0, p.text3));
ui.painter().line_segment([r.left_top() + vec2(3.0, 8.5), r.right_top() + vec2(-3.0, 8.5)], Stroke::new(1.0, p.text3));
ui.vertical(|ui| {
ui.spacing_mut().item_spacing.y = 0.0;
let name: String = a.name.chars().take(28).collect();
ui.label(RichText::new(name).size(FONT_XS).color(p.text));
ui.label(RichText::new(attach::human(a.bytes)).size(10.5).color(p.text3));
});
}
if removable && ui.add(egui::Button::new(RichText::new("×").size(13.0).color(p.text3)).frame(false)).on_hover_text(t("移除")).clicked() {
remove = true;
}
});
})
.response;
if !removable && r.interact(egui::Sense::click()).on_hover_text(t("打開")).clicked() {
crate::preview::open_with_system(&a.path);
}
remove
}
/// The chips above the message box.
pub(super) fn pending_chips(&mut self, ui: &mut egui::Ui, bot_id: u64) {
let list = self.att.pending.get(&bot_id).cloned().unwrap_or_default();
if list.is_empty() && self.att.error.is_none() {
return;
}
let mut remove = None;
ui.horizontal_wrapped(|ui| {
for (i, a) in list.iter().enumerate() {
if self.attachment_chip(ui, a, true) {
remove = Some(i);
}
}
});
if let Some(i) = remove {
if let Some(v) = self.att.pending.get_mut(&bot_id) {
let a = v.remove(i);
let _ = std::fs::remove_file(&a.path);
}
}
if let Some(e) = self.att.error.clone() {
ui.horizontal(|ui| {
ui.label(RichText::new(e).size(FONT_XS).color(self.pal.danger));
if ui.add(egui::Button::new(RichText::new(t("知道了")).size(FONT_XS)).frame(false)).clicked() {
self.att.error = None;
}
});
}
ui.add_space(4.0);
}
/// Take this bot's pending attachments for a message (copying them onto its own computer when
/// it's running).
pub(super) fn take_attachments(&mut self, bot_id: u64) -> Vec<Attachment> {
let mut list = self.att.pending.remove(&bot_id).unwrap_or_default();
for a in list.iter_mut() {
if let Ok(dst) = crate::cloudbox::copy_in(&a.path, bot_id, &a.name) {
a.on_box = dst;
}
}
list
}
pub(super) fn has_pending_attachments(&self, bot_id: u64) -> bool {
self.att.pending.get(&bot_id).map(|v| !v.is_empty()).unwrap_or(false)
}
}