Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

fix(threads): upsert message rows so metadata.error survives restart

Akarshan93915df1e8a2
diff --git a/src-tauri/src/core/threads/db.rs b/src-tauri/src/core/threads/db.rs
index c996fb576..aeda468bd 100644
--- a/src-tauri/src/core/threads/db.rs
+++ b/src-tauri/src/core/threads/db.rs
@@ -244,7 +244,11 @@ pub async fn db_create_message<R: Runtime>(
let data = serde_json::to_string(&message).map_err(|e| e.to_string())?;
- sqlx::query("INSERT INTO messages (id, thread_id, data) VALUES (?1, ?2, ?3)")
+ // INSERT OR IGNORE: a concurrent modify_message may have already upserted
+ // this row (race when the UI stamps metadata onto a freshly-sent message
+ // before the original create lands). Skipping rather than replacing
+ // preserves the modify's data.
+ sqlx::query("INSERT OR IGNORE INTO messages (id, thread_id, data) VALUES (?1, ?2, ?3)")
.bind(message_id)
.bind(thread_id)
.bind(&data)
@@ -267,14 +271,26 @@ pub async fn db_modify_message<R: Runtime>(
.and_then(|v| v.as_str())
.ok_or("Missing message id")?;
+ let thread_id = message
+ .get("thread_id")
+ .and_then(|v| v.as_str())
+ .ok_or("Missing thread_id")?;
+
let data = serde_json::to_string(&message).map_err(|e| e.to_string())?;
- sqlx::query("UPDATE messages SET data = ?1 WHERE id = ?2")
- .bind(&data)
- .bind(message_id)
- .execute(&pool)
- .await
- .map_err(|e| format!("Failed to modify message: {}", e))?;
+ // UPSERT: a plain UPDATE silently affects 0 rows when modify_message races
+ // ahead of the original create_message — dropping metadata edits (e.g.
+ // metadata.error stamped onto the user message right after an OOM).
+ sqlx::query(
+ "INSERT INTO messages (id, thread_id, data) VALUES (?1, ?2, ?3) \
+ ON CONFLICT(id) DO UPDATE SET data = excluded.data",
+ )
+ .bind(message_id)
+ .bind(thread_id)
+ .bind(&data)
+ .execute(&pool)
+ .await
+ .map_err(|e| format!("Failed to modify message: {}", e))?;
Ok(message)
}