|
| 1 | +use rusqlite::{params, Connection, Result}; |
| 2 | +use serde::{Deserialize, Serialize}; |
| 3 | + |
| 4 | +/// A chat message stored inside a conversation. |
| 5 | +#[derive(Debug, Clone, Serialize, Deserialize)] |
| 6 | +pub struct StoredChatMessage { |
| 7 | + pub role: String, |
| 8 | + pub content: String, |
| 9 | +} |
| 10 | + |
| 11 | +/// Full conversation record. |
| 12 | +#[derive(Debug, Clone, Serialize, Deserialize)] |
| 13 | +pub struct LlmConversation { |
| 14 | + pub id: String, |
| 15 | + pub title: String, |
| 16 | + pub created_at: String, |
| 17 | + pub updated_at: String, |
| 18 | + pub archived: bool, |
| 19 | + pub pinned: bool, |
| 20 | + pub messages: Vec<StoredChatMessage>, |
| 21 | + pub summary: Option<String>, |
| 22 | +} |
| 23 | + |
| 24 | +/// Lightweight conversation row for the sidebar list. |
| 25 | +#[derive(Debug, Clone, Serialize, Deserialize)] |
| 26 | +pub struct LlmConversationSummary { |
| 27 | + pub id: String, |
| 28 | + pub title: String, |
| 29 | + pub created_at: String, |
| 30 | + pub updated_at: String, |
| 31 | + pub archived: bool, |
| 32 | + pub pinned: bool, |
| 33 | + pub message_count: usize, |
| 34 | +} |
| 35 | + |
| 36 | +pub fn list_llm_conversations( |
| 37 | + conn: &Connection, |
| 38 | + include_archived: bool, |
| 39 | +) -> Result<Vec<LlmConversationSummary>> { |
| 40 | + let sql = if include_archived { |
| 41 | + "SELECT id, title, created_at, updated_at, archived, pinned, messages |
| 42 | + FROM llm_conversations |
| 43 | + ORDER BY pinned DESC, updated_at DESC" |
| 44 | + } else { |
| 45 | + "SELECT id, title, created_at, updated_at, archived, pinned, messages |
| 46 | + FROM llm_conversations |
| 47 | + WHERE archived = 0 |
| 48 | + ORDER BY pinned DESC, updated_at DESC" |
| 49 | + }; |
| 50 | + let mut stmt = conn.prepare(sql)?; |
| 51 | + let rows = stmt.query_map([], |row| { |
| 52 | + let messages_json: String = row.get(6)?; |
| 53 | + let message_count = serde_json::from_str::<Vec<StoredChatMessage>>(&messages_json) |
| 54 | + .map(|v| v.len()) |
| 55 | + .unwrap_or(0); |
| 56 | + Ok(LlmConversationSummary { |
| 57 | + id: row.get(0)?, |
| 58 | + title: row.get(1)?, |
| 59 | + created_at: row.get(2)?, |
| 60 | + updated_at: row.get(3)?, |
| 61 | + archived: row.get::<_, i32>(4)? != 0, |
| 62 | + pinned: row.get::<_, i32>(5)? != 0, |
| 63 | + message_count, |
| 64 | + }) |
| 65 | + })?; |
| 66 | + rows.collect() |
| 67 | +} |
| 68 | + |
| 69 | +pub fn get_llm_conversation(conn: &Connection, id: &str) -> Result<Option<LlmConversation>> { |
| 70 | + let mut stmt = conn.prepare( |
| 71 | + "SELECT id, title, created_at, updated_at, archived, pinned, messages, summary |
| 72 | + FROM llm_conversations |
| 73 | + WHERE id = ?1", |
| 74 | + )?; |
| 75 | + let row = stmt.query_row([id], |row| { |
| 76 | + let messages_json: String = row.get(6)?; |
| 77 | + let summary: Option<String> = row.get(7)?; |
| 78 | + Ok(LlmConversation { |
| 79 | + id: row.get(0)?, |
| 80 | + title: row.get(1)?, |
| 81 | + created_at: row.get(2)?, |
| 82 | + updated_at: row.get(3)?, |
| 83 | + archived: row.get::<_, i32>(4)? != 0, |
| 84 | + pinned: row.get::<_, i32>(5)? != 0, |
| 85 | + messages: serde_json::from_str(&messages_json).unwrap_or_default(), |
| 86 | + summary, |
| 87 | + }) |
| 88 | + }); |
| 89 | + match row { |
| 90 | + Ok(conv) => Ok(Some(conv)), |
| 91 | + Err(rusqlite::Error::QueryReturnedNoRows) => Ok(None), |
| 92 | + Err(e) => Err(e), |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +pub fn upsert_llm_conversation(conn: &Connection, conversation: &LlmConversation) -> Result<()> { |
| 97 | + let messages_json = serde_json::to_string(&conversation.messages) |
| 98 | + .map_err(|e| rusqlite::Error::InvalidParameterName(e.to_string()))?; |
| 99 | + conn.execute( |
| 100 | + "INSERT INTO llm_conversations (id, title, created_at, updated_at, archived, pinned, messages, summary) |
| 101 | + VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8) |
| 102 | + ON CONFLICT(id) DO UPDATE SET |
| 103 | + title = excluded.title, |
| 104 | + updated_at = excluded.updated_at, |
| 105 | + archived = excluded.archived, |
| 106 | + pinned = excluded.pinned, |
| 107 | + messages = excluded.messages, |
| 108 | + summary = excluded.summary", |
| 109 | + params![ |
| 110 | + conversation.id, |
| 111 | + conversation.title, |
| 112 | + conversation.created_at, |
| 113 | + conversation.updated_at, |
| 114 | + conversation.archived as i32, |
| 115 | + conversation.pinned as i32, |
| 116 | + messages_json, |
| 117 | + conversation.summary, |
| 118 | + ], |
| 119 | + )?; |
| 120 | + Ok(()) |
| 121 | +} |
| 122 | + |
| 123 | +pub fn delete_llm_conversation(conn: &Connection, id: &str) -> Result<()> { |
| 124 | + conn.execute( |
| 125 | + "DELETE FROM llm_conversations WHERE id = ?1", |
| 126 | + params![id], |
| 127 | + )?; |
| 128 | + Ok(()) |
| 129 | +} |
| 130 | + |
| 131 | +pub fn set_llm_conversation_archived( |
| 132 | + conn: &Connection, |
| 133 | + id: &str, |
| 134 | + archived: bool, |
| 135 | +) -> Result<()> { |
| 136 | + conn.execute( |
| 137 | + "UPDATE llm_conversations SET archived = ?1, updated_at = ?2 WHERE id = ?3", |
| 138 | + params![archived as i32, chrono::Local::now().to_rfc3339(), id], |
| 139 | + )?; |
| 140 | + Ok(()) |
| 141 | +} |
| 142 | + |
| 143 | +pub fn set_llm_conversation_pinned(conn: &Connection, id: &str, pinned: bool) -> Result<()> { |
| 144 | + conn.execute( |
| 145 | + "UPDATE llm_conversations SET pinned = ?1, updated_at = ?2 WHERE id = ?3", |
| 146 | + params![pinned as i32, chrono::Local::now().to_rfc3339(), id], |
| 147 | + )?; |
| 148 | + Ok(()) |
| 149 | +} |
0 commit comments