Refine chat management: auto-titles, per-chat rename/delete, custom modal

- Auto-title a session from its first user message (chat_title) so chats are distinguishable in the sidebar instead of all 'Nuova chat'.

- Per-chat rename (✏️) and delete (🗑️) buttons with aria-labels; stopPropagation so they don't switch the conversation. deleteSessionById selects a fallback or creates a fresh session.

- Replace native prompt()/confirm() with a styled custom modal (showDialog): mobile-friendly and inspectable by automation (native dialogs hung the browser subagent).

Verified via direct HTTP (auto-title/rename/delete) + 108 tests; browser subagent avoided after repeated hangs.
This commit is contained in:
adm committed 2026-11-25 02:41:51 +01:00
1 parent 8979201139
commit 0123f41d99
5 files changed
+198 -10

No files matched your search

+44
View File
@@ -126,6 +126,19 @@ pub(crate) async fn stats(State(st): State<AppState>) -> Result<Json<Stats>, Err
Ok(Json(with_store(&st, |s| s.stats()).await?))
}
/// A short, single-line title derived from the first user message.
fn chat_title(content: &str) -> String {
let line = content.split_whitespace().collect::<Vec<_>>().join(" ");
let mut t: String = line.chars().take(48).collect();
if line.chars().count() > 48 {
t.push('…');
}
if t.is_empty() {
t = "Nuova chat".to_string();
}
t
}
fn role_of(s: &str) -> Role {
match s {
"system" | "developer" => Role::System,
@@ -294,6 +307,17 @@ pub(crate) async fn session_chat(
return e.into_response();
}
// Auto-title a fresh session from its first user message, so chats are
// distinguishable in the sidebar instead of all being "Nuova chat".
if history.is_empty() {
let title = chat_title(&body.content);
let _ = with_store(&st, {
let id = id.clone();
move |s| s.rename_session(&id, &title)
})
.await;
}
// Agent mode: drive the model↔shell_exec loop, streaming progress events.
if body.agent {
req.tools = Some(agent::agent_tools());
@@ -438,3 +462,23 @@ async fn persist_assistant(store: &Arc<dyn Store>, sid: &str, a: &Accum, prompt_
})
.await;
}
#[cfg(test)]
mod tests {
use super::chat_title;
#[test]
fn title_collapses_whitespace_and_truncates() {
assert_eq!(chat_title(" ciao mondo "), "ciao mondo");
assert_eq!(chat_title(""), "Nuova chat");
assert_eq!(chat_title(" "), "Nuova chat");
let long = "a".repeat(80);
let t = chat_title(&long);
assert_eq!(t.chars().count(), 49); // 48 + ellipsis
assert!(t.ends_with('…'));
// multibyte truncation stays on char boundaries
let emoji = "😀".repeat(60);
let t2 = chat_title(&emoji);
assert!(t2.chars().count() <= 49);
}
}