Drafts in every chat, factory chat overlay, factory main switch
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
58475ce65e
commit
171b87b766
48 files changed
+754
-10
No files matched your search
+114
@@ -143,6 +143,33 @@ pub struct App {
|
||||
pending_note: Option<oneshot::Receiver<Result<String>>>,
|
||||
/// The factory issue ("repo#number") whose actions are open.
|
||||
factory_key: String,
|
||||
/// Unsent input is kept per chat: on the server for server chats (so another device can continue it),
|
||||
/// in a local file for local chats.
|
||||
draft: DraftSync,
|
||||
}
|
||||
|
||||
struct DraftSync {
|
||||
/// "remote:<id>" or "local:<id>" of the chat the draft belongs to.
|
||||
key: String,
|
||||
/// What the editor held at the last look, and since when.
|
||||
seen: String,
|
||||
changed: Instant,
|
||||
/// What is stored (or was last stored) for this chat.
|
||||
synced: String,
|
||||
loaded: bool,
|
||||
pull: Option<oneshot::Receiver<Result<String>>>,
|
||||
last_pull: Instant,
|
||||
}
|
||||
|
||||
impl DraftSync {
|
||||
fn new() -> Self {
|
||||
Self { key: String::new(), seen: String::new(), changed: Instant::now(), synced: String::new(), loaded: false, pull: None, last_pull: Instant::now() }
|
||||
}
|
||||
}
|
||||
|
||||
fn local_draft_path(id: &str) -> std::path::PathBuf {
|
||||
let safe: String = id.chars().filter(|c| c.is_ascii_alphanumeric() || *c == '-' || *c == '_').collect();
|
||||
crate::conf::data_dir().join("drafts").join(format!("{safe}.txt"))
|
||||
}
|
||||
|
||||
const SPINNER: &[&str] = &["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
|
||||
@@ -316,6 +343,7 @@ impl App {
|
||||
dialog: Dialog::None,
|
||||
todos: session.todos.clone(),
|
||||
context: session.context_tokens,
|
||||
draft: DraftSync::new(),
|
||||
session,
|
||||
effort: None,
|
||||
images: vec![],
|
||||
@@ -1441,7 +1469,93 @@ impl App {
|
||||
self.status = "loading models…".into();
|
||||
}
|
||||
|
||||
/// Keep the unsent input of the current chat: load it when the chat changes, save it shortly after typing.
|
||||
fn poll_draft(&mut self) {
|
||||
let remote_id = self.remote.as_ref().map(|r| r.id.clone());
|
||||
let key = match &remote_id {
|
||||
Some(id) => format!("remote:{id}"),
|
||||
None => format!("local:{}", self.session.id),
|
||||
};
|
||||
if self.draft.key != key {
|
||||
self.draft = DraftSync::new();
|
||||
self.draft.key = key.clone();
|
||||
self.draft.seen = self.editor.text();
|
||||
match &remote_id {
|
||||
Some(id) => self.draft_pull(id.clone()),
|
||||
None => {
|
||||
let text = std::fs::read_to_string(local_draft_path(&self.session.id)).unwrap_or_default();
|
||||
if self.editor.is_empty() && !text.is_empty() {
|
||||
self.editor.set(&text);
|
||||
self.draft.seen = text.clone();
|
||||
}
|
||||
self.draft.synced = if self.editor.is_empty() { String::new() } else { self.draft.seen.clone() };
|
||||
self.draft.loaded = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if let Some(rx) = &mut self.draft.pull {
|
||||
if let Ok(res) = rx.try_recv() {
|
||||
self.draft.pull = None;
|
||||
if let Ok(text) = res {
|
||||
// First load: only into an empty field. Later: only when nothing was typed here since the last sync.
|
||||
let untouched = if self.draft.loaded { self.editor.text() == self.draft.synced } else { self.editor.is_empty() };
|
||||
if untouched && text != self.editor.text() {
|
||||
self.editor.set(&text);
|
||||
self.draft.seen = text.clone();
|
||||
self.draft.synced = text;
|
||||
} else if !self.draft.loaded {
|
||||
self.draft.synced = if untouched { text } else { String::new() };
|
||||
}
|
||||
self.draft.loaded = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
let cur = self.editor.text();
|
||||
if cur != self.draft.seen {
|
||||
self.draft.seen = cur;
|
||||
self.draft.changed = Instant::now();
|
||||
}
|
||||
if self.draft.loaded && self.draft.seen != self.draft.synced && self.draft.changed.elapsed() >= Duration::from_millis(700) {
|
||||
let text = self.draft.seen.clone();
|
||||
self.draft.synced = text.clone();
|
||||
match (&remote_id, self.env.server.clone()) {
|
||||
(Some(id), Some(server)) => {
|
||||
let id = id.clone();
|
||||
tokio::spawn(async move {
|
||||
let _ = remote::draft_put(&server, &id, &text).await;
|
||||
});
|
||||
}
|
||||
(None, _) => {
|
||||
let path = local_draft_path(&self.session.id);
|
||||
if text.trim().is_empty() {
|
||||
let _ = std::fs::remove_file(&path);
|
||||
} else if let Some(dir) = path.parent() {
|
||||
let _ = std::fs::create_dir_all(dir);
|
||||
let _ = std::fs::write(&path, &text);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
// Pick up what was typed on another device while this field has no unsent edits.
|
||||
if let (Some(id), true) = (&remote_id, self.draft.loaded && self.draft.pull.is_none() && self.draft.last_pull.elapsed() >= Duration::from_secs(10) && self.editor.text() == self.draft.synced) {
|
||||
self.draft.last_pull = Instant::now();
|
||||
self.draft_pull(id.clone());
|
||||
}
|
||||
}
|
||||
|
||||
fn draft_pull(&mut self, id: String) {
|
||||
let Some(server) = self.env.server.clone() else { return };
|
||||
let (tx, rx) = oneshot::channel();
|
||||
tokio::spawn(async move {
|
||||
let _ = tx.send(remote::draft_get(&server, &id).await);
|
||||
});
|
||||
self.draft.pull = Some(rx);
|
||||
self.draft.last_pull = Instant::now();
|
||||
}
|
||||
|
||||
fn poll_pending(&mut self) {
|
||||
self.poll_draft();
|
||||
self.poll_remote();
|
||||
if let Some(rx) = &mut self.pending_detect {
|
||||
if let Ok(found) = rx.try_recv() {
|
||||
|
||||
Reference in new issue
Block a user