Allow delete all types (#8018)

Signed-off-by: Douwe Osinga <douwe@squareup.com>
Co-authored-by: Douwe Osinga <douwe@squareup.com>
This commit is contained in:
Douwe Osinga
2026-03-19 15:06:50 -04:00
committed by GitHub
parent a68c6788eb
commit 53236ff327
2 changed files with 33 additions and 27 deletions
+22 -11
View File
@@ -311,7 +311,11 @@ impl SessionManager {
}
pub async fn list_sessions_by_types(&self, types: &[SessionType]) -> Result<Vec<Session>> {
self.storage.list_sessions_by_types(types).await
self.storage.list_sessions_by_types(Some(types)).await
}
pub async fn list_all_sessions(&self) -> Result<Vec<Session>> {
self.storage.list_sessions_by_types(None).await
}
pub async fn delete_session(&self, id: &str) -> Result<()> {
@@ -1239,12 +1243,19 @@ impl SessionStorage {
Self::replace_conversation_inner(pool, session_id, conversation).await
}
async fn list_sessions_by_types(&self, types: &[SessionType]) -> Result<Vec<Session>> {
if types.is_empty() {
return Ok(Vec::new());
}
async fn list_sessions_by_types(&self, types: Option<&[SessionType]>) -> Result<Vec<Session>> {
let (where_clause, binds): (String, Vec<String>) = match types {
Some(t) if !t.is_empty() => {
let placeholders: String = t.iter().map(|_| "?").collect::<Vec<_>>().join(", ");
(
format!("WHERE s.session_type IN ({})", placeholders),
t.iter().map(|t| t.to_string()).collect(),
)
}
Some(_) => return Ok(Vec::new()),
None => (String::new(), Vec::new()),
};
let placeholders: String = types.iter().map(|_| "?").collect::<Vec<_>>().join(", ");
let query = format!(
r#"
SELECT s.id, s.working_dir, s.name, s.description, s.user_set_name, s.session_type, s.created_at, s.updated_at, s.extension_data,
@@ -1255,16 +1266,16 @@ impl SessionStorage {
COUNT(m.id) as message_count
FROM sessions s
INNER JOIN messages m ON s.id = m.session_id
WHERE s.session_type IN ({})
{}
GROUP BY s.id
ORDER BY s.updated_at DESC
"#,
placeholders
where_clause
);
let mut q = sqlx::query_as::<_, Session>(&query);
for t in types {
q = q.bind(t.to_string());
for b in &binds {
q = q.bind(b);
}
let pool = self.pool().await?;
@@ -1272,7 +1283,7 @@ impl SessionStorage {
}
async fn list_sessions(&self) -> Result<Vec<Session>> {
self.list_sessions_by_types(&[SessionType::User, SessionType::Scheduled])
self.list_sessions_by_types(Some(&[SessionType::User, SessionType::Scheduled]))
.await
}