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:
Richard ZinkandClaude Sonnet 5.5 committed 2026-11-20 12:14:04 +01:00
1 parent 58475ce65e
commit 171b87b766
48 files changed
+754 -10

No files matched your search

+67 -1
View File
@@ -125,6 +125,13 @@ impl Store {
last_status TEXT,
created_at INTEGER NOT NULL
);
CREATE TABLE IF NOT EXISTS chat_drafts (
session_id TEXT NOT NULL,
user_id TEXT NOT NULL,
text TEXT NOT NULL,
updated_at INTEGER NOT NULL,
PRIMARY KEY (session_id, user_id)
);
CREATE TABLE IF NOT EXISTS user_settings (
user_id TEXT NOT NULL,
key TEXT NOT NULL,
@@ -1569,13 +1576,42 @@ impl Store {
Ok(rows.filter_map(|r| r.ok()).collect())
}
/// The unsent message of a chat, kept on the server so that it can be continued on another device.
pub fn chat_draft(&self, session: &str, user: &str) -> Result<Option<(String, i64)>> {
Ok(self.conn.lock().unwrap().query_row("SELECT text, updated_at FROM chat_drafts WHERE session_id = ?1 AND user_id = ?2", params![session, user], |r| Ok((r.get(0)?, r.get(1)?))).optional()?)
}
/// Store the unsent message (an empty text deletes it).
pub fn chat_draft_set(&self, session: &str, user: &str, text: &str) -> Result<i64> {
let conn = self.conn.lock().unwrap();
let now = crate::types::now_ms();
if text.trim().is_empty() {
conn.execute("DELETE FROM chat_drafts WHERE session_id = ?1 AND user_id = ?2", params![session, user])?;
} else {
conn.execute("INSERT INTO chat_drafts (session_id, user_id, text, updated_at) VALUES (?1,?2,?3,?4) ON CONFLICT(session_id, user_id) DO UPDATE SET text = ?3, updated_at = ?4", params![session, user, text, now])?;
}
Ok(now)
}
pub fn factory_issues(&self, owner: &str) -> Result<Vec<Value>> {
let nums: Vec<(String, i64)> = {
let conn = self.conn.lock().unwrap();
let mut st = conn.prepare("SELECT repo_url, number FROM factory_issues WHERE owner_id = ?1 ORDER BY updated_at DESC")?;
st.query_map([owner], |r| Ok((r.get(0)?, r.get(1)?)))?.filter_map(|r| r.ok()).collect()
};
Ok(nums.into_iter().filter_map(|(r, n)| self.factory_issue(&r, n).ok().flatten()).collect())
let mut rows: Vec<Value> = nums.into_iter().filter_map(|(r, n)| self.factory_issue(&r, n).ok().flatten()).collect();
// The newest phase chat that still exists, for the "open chat" button of the card.
let conn = self.conn.lock().unwrap();
if let Ok(mut chat) = conn.prepare("SELECT r.session_id, r.phase FROM factory_runs r WHERE r.repo_url = ?1 AND r.issue = ?2 AND EXISTS (SELECT 1 FROM sessions s WHERE s.id = r.session_id) ORDER BY r.started_at DESC LIMIT 1") {
for row in rows.iter_mut() {
let (repo, n) = (row["repo"].as_str().unwrap_or("").to_string(), row["number"].as_i64().unwrap_or(0));
if let Ok((sid, phase)) = chat.query_row(params![repo, n], |x| Ok((x.get::<_, String>(0)?, x.get::<_, String>(1)?))) {
row["chat_session"] = json!(sid);
row["chat_phase"] = json!(phase);
}
}
}
Ok(rows)
}
pub fn factory_run_add(&self, id: &str, owner: &str, repo: &str, issue: i64, phase: &str, round: i64, session: &str, model: &str) -> Result<()> {
@@ -1597,6 +1633,11 @@ impl Store {
Ok(self.conn.lock().unwrap().query_row("SELECT COUNT(*) FROM factory_issues WHERE repo_url = ?1 AND state IN ('review', 'building', 'testing')", [repo], |r| r.get(0))?)
}
/// Phases of this user that are running right now (any repository).
pub fn factory_active(&self, owner: &str) -> Result<i64> {
Ok(self.conn.lock().unwrap().query_row("SELECT COUNT(*) FROM factory_issues WHERE owner_id = ?1 AND state IN ('review', 'building', 'testing')", [owner], |r| r.get(0))?)
}
/// Runs with token and cost totals (joined from the usage table).
pub fn factory_runs(&self, owner: &str, repo: Option<&str>, issue: Option<i64>, limit: i64) -> Result<Vec<Value>> {
let conn = self.conn.lock().unwrap();
@@ -2022,3 +2063,28 @@ mod link_tests {
std::fs::remove_dir_all(&dir).unwrap();
}
}
#[cfg(test)]
mod draft_tests {
use super::*;
#[test]
fn drafts_are_kept_per_chat_and_user_and_cleared_when_empty() {
let dir = std::env::temp_dir().join(format!("cc-drafts-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let store = Store::open(&dir.join("t.db")).unwrap();
assert!(store.chat_draft("s1", "u1").unwrap().is_none());
store.chat_draft_set("s1", "u1", "half a message").unwrap();
store.chat_draft_set("s1", "u2", "someone else").unwrap();
store.chat_draft_set("s2", "u1", "other chat").unwrap();
assert_eq!(store.chat_draft("s1", "u1").unwrap().unwrap().0, "half a message");
store.chat_draft_set("s1", "u1", "continued on another device").unwrap();
assert_eq!(store.chat_draft("s1", "u1").unwrap().unwrap().0, "continued on another device");
assert_eq!(store.chat_draft("s1", "u2").unwrap().unwrap().0, "someone else");
// Sending clears it.
store.chat_draft_set("s1", "u1", " \n").unwrap();
assert!(store.chat_draft("s1", "u1").unwrap().is_none());
assert_eq!(store.chat_draft("s2", "u1").unwrap().unwrap().0, "other chat");
let _ = std::fs::remove_dir_all(&dir);
}
}