feat: multiple persona conversations with branching and generated titles
Persona chat stored a flat Vec<ChatMessage> keyed on (user_id, persona_id), which meant one rolling transcript per persona and no way to revisit a turn. This moves it onto the same ChatHistoryStore tree the file chat uses and gives conversations their own identity. Storage - messages_json now holds a serialized ChatHistoryStore. Reads accept the old flat array and upgrade it in place, so existing transcripts survive without a data migration. The upgrade drops the v1 seed greeting, which the flat renderer hid but the tree renderer would surface as a bubble the user has never seen. - New migration re-keys persona_chat_conversations on an opaque conversation_id and adds title + created_at, so one persona can hold any number of separate threads. Every DAO read and write is scoped by user_id as well: a conversation id is a bearer token for someone's transcript and must never grant access on its own. - The per-conversation lock and in-flight turn slot key on conversation_id, so two threads with the same persona can run turns concurrently. Endpoints - POST/DELETE /persona_chat/conversations — start and remove a thread, replacing /persona_chat/reset. - GET /persona_chat/conversations — the chat list, with snippet and counts derived from each tree's active branch. - POST /persona_chat/rewind, POST /persona_chat/switch-branch, GET /persona_chat/branches — rewind and fork, mirroring the file chat. Index 0 is rewindable here (it is the user's own first question, not a synthetic prompt) and re-anchors on the seed node. - history/turn/rewind/switch-branch/branches all key on conversation_id; history gained branch_id and now returns real fork_info, active_leaf_id and viewing_branch_id instead of placeholders. The turn body no longer carries a persona at all — it is read from the stored conversation, so a stale client cannot swap a thread's voice midway. Titles After the first turn persists, the conversation is named from its opening exchange on the same backend the turn ran on. Small models wrap titles in quotes, prefix them with "Title:" and append explanations, so sanitize_title strips all of that and truncates on a character boundary. Any failure falls back to the user's opening question. Generation runs after persistence: a failed title must not cost the turn. Fixes found along the way - insight_chat: both file-chat turn paths captured path.len() before apply_context_budget drained messages out of the middle, then sliced messages[path_len..] for the new tree nodes. Once truncation fired that dropped the user turn from the tree or panicked on an out-of-range start index. Now read after the budget pass as history_len. - Persona chat had no context budget at all and hardcoded truncated: false in the done frame, so a rolling transcript grew unbounded. - A cancelled turn persisted a half-finished transcript and pushed a second terminal frame; it now returns early like the file chat. - The seeded system prompt was frozen at conversation creation, so editing a persona never reached a thread already in flight. Re-resolved per turn. - turn_count was overwritten each write with the per-turn message delta; it is now the cumulative assistant-turn count on the active branch. - is_initial is always false: the file chat reserves it for its synthetic "describe this photo" prompt, and marking a persona chat's first question with it made the opening reply impossible to regenerate. 600 lib tests pass, clippy --all-targets clean. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
883e2a0e1b
commit
65cceaa67c
13 files changed
+1847
-454
No files matched your search
+344
-113
@@ -13,16 +13,48 @@ use crate::otel::trace_db_call;
|
||||
/// One row of the persona-chat transcript. Lives in
|
||||
/// `persona_chat_conversations` (one row per `(user_id, persona_id)`).
|
||||
///
|
||||
/// `turn_count` is the number of new turn-rows since the last persisted
|
||||
/// slice — used by the SSE `done` event for stats dashboards. The actual
|
||||
/// transcript is the `messages_json` blob.
|
||||
/// One persona conversation.
|
||||
///
|
||||
/// `turn_count` is the cumulative number of assistant turns on the active
|
||||
/// branch — what the list screen shows as the length of a conversation. The
|
||||
/// transcript itself is the `messages_json` blob, holding a serialized
|
||||
/// `ChatHistoryStore` tree. `title` is a generated summary of the opening
|
||||
/// exchange, empty until the first turn completes.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct PersonaChatRow {
|
||||
pub conversation_id: String,
|
||||
pub persona_id: String,
|
||||
pub title: String,
|
||||
pub messages_json: String,
|
||||
pub turn_count: i32,
|
||||
pub created_at: i64,
|
||||
pub updated_at: i64,
|
||||
}
|
||||
|
||||
/// Column tuple → `PersonaChatRow`. Shared by the single-row and list
|
||||
/// queries so their `select(...)` orders can never drift apart.
|
||||
fn persona_chat_row(
|
||||
(conversation_id, persona_id, title, messages_json, turn_count, created_at, updated_at): (
|
||||
String,
|
||||
String,
|
||||
String,
|
||||
String,
|
||||
i32,
|
||||
i64,
|
||||
i64,
|
||||
),
|
||||
) -> PersonaChatRow {
|
||||
PersonaChatRow {
|
||||
conversation_id,
|
||||
persona_id,
|
||||
title,
|
||||
messages_json,
|
||||
turn_count,
|
||||
created_at,
|
||||
updated_at,
|
||||
}
|
||||
}
|
||||
|
||||
/// Patch shape for update_persona. None = leave field alone. Built-ins are
|
||||
/// allowed to flip `include_all_memories` but should reject name/prompt
|
||||
/// edits at the handler layer (built-in copy lives in the migration).
|
||||
@@ -96,39 +128,72 @@ pub trait PersonaDao: Sync + Send {
|
||||
|
||||
// ── Persona-chat (open chat with persona) persistence ───────────
|
||||
//
|
||||
// Keyed by `(user_id, persona_id)` with a single rolling transcript
|
||||
// per pair. No tree branching in v1 — matches the locked-in scope.
|
||||
// Keyed on an opaque `conversation_id` so one persona can hold several
|
||||
// separate conversations. `user_id` is checked on every read and write:
|
||||
// the id is a bearer token for a transcript, so ownership can never be
|
||||
// assumed from the id alone.
|
||||
|
||||
/// Fetch the rolling transcript for one `(user, persona)`. None when
|
||||
/// the user has never started a conversation with this persona.
|
||||
/// Fetch one conversation. None when it doesn't exist or belongs to
|
||||
/// another user — the two are deliberately indistinguishable to callers.
|
||||
fn get_persona_chat(
|
||||
&mut self,
|
||||
cx: &opentelemetry::Context,
|
||||
user_id: i32,
|
||||
persona_id: &str,
|
||||
conversation_id: &str,
|
||||
) -> Result<Option<PersonaChatRow>, DbError>;
|
||||
|
||||
/// Upsert (create-or-replace) the rolling transcript. Called once per
|
||||
/// completed turn with the full new `messages_json`. The `turn_count`
|
||||
/// is the number of user/assistant pairs added in this write.
|
||||
fn upsert_persona_chat(
|
||||
/// Every conversation this user has going, newest first. Backs the chat
|
||||
/// list screen, so it returns the transcript blob too — the snippet is
|
||||
/// derived from the tree rather than denormalized into its own column.
|
||||
fn list_persona_chats(
|
||||
&mut self,
|
||||
cx: &opentelemetry::Context,
|
||||
user_id: i32,
|
||||
) -> Result<Vec<PersonaChatRow>, DbError>;
|
||||
|
||||
/// Start a new conversation with a persona, returning its id. Several
|
||||
/// conversations with the same persona are expected, so this never
|
||||
/// reuses an existing row.
|
||||
fn create_persona_chat(
|
||||
&mut self,
|
||||
cx: &opentelemetry::Context,
|
||||
user_id: i32,
|
||||
persona_id: &str,
|
||||
created_at: i64,
|
||||
) -> Result<String, DbError>;
|
||||
|
||||
/// Replace a conversation's transcript. Called once per completed turn
|
||||
/// with the full new `messages_json`; `turn_count` is the cumulative
|
||||
/// assistant-turn count on the resulting active branch. Returns the
|
||||
/// number of rows written — 0 means the conversation is gone or is not
|
||||
/// this user's.
|
||||
fn update_persona_chat(
|
||||
&mut self,
|
||||
cx: &opentelemetry::Context,
|
||||
user_id: i32,
|
||||
conversation_id: &str,
|
||||
messages_json: &str,
|
||||
turn_count: i32,
|
||||
updated_at: i64,
|
||||
) -> Result<(), DbError>;
|
||||
) -> Result<usize, DbError>;
|
||||
|
||||
/// Wipe the rolling transcript. Reserved for a future "New
|
||||
/// conversation" affordance; the row itself stays (so a subsequent
|
||||
/// get returns None, not 404).
|
||||
fn clear_persona_chat(
|
||||
/// Name a conversation. Written once, after the first turn produces
|
||||
/// enough of an exchange to summarize.
|
||||
fn set_persona_chat_title(
|
||||
&mut self,
|
||||
cx: &opentelemetry::Context,
|
||||
user_id: i32,
|
||||
persona_id: &str,
|
||||
conversation_id: &str,
|
||||
title: &str,
|
||||
) -> Result<(), DbError>;
|
||||
|
||||
/// Delete a conversation outright. Backs both the list screen's delete
|
||||
/// affordance and the reset endpoint.
|
||||
fn delete_persona_chat(
|
||||
&mut self,
|
||||
cx: &opentelemetry::Context,
|
||||
user_id: i32,
|
||||
conversation_id: &str,
|
||||
) -> Result<(), DbError>;
|
||||
}
|
||||
|
||||
@@ -351,84 +416,155 @@ impl PersonaDao for SqlitePersonaDao {
|
||||
&mut self,
|
||||
cx: &opentelemetry::Context,
|
||||
uid: i32,
|
||||
pid: &str,
|
||||
cid: &str,
|
||||
) -> Result<Option<PersonaChatRow>, DbError> {
|
||||
trace_db_call(cx, "query", "get_persona_chat", |_span| {
|
||||
use schema::persona_chat_conversations::dsl::*;
|
||||
let mut conn = self.connection.lock().expect("PersonaDao lock");
|
||||
persona_chat_conversations
|
||||
.filter(conversation_id.eq(cid))
|
||||
// Scoped by user as well as id: the id is a bearer token for
|
||||
// someone's transcript, so it never grants access on its own.
|
||||
.filter(user_id.eq(uid))
|
||||
.filter(persona_id.eq(pid))
|
||||
.select((messages_json, turn_count, updated_at))
|
||||
.first::<(String, i32, i64)>(conn.deref_mut())
|
||||
.select((
|
||||
conversation_id,
|
||||
persona_id,
|
||||
title,
|
||||
messages_json,
|
||||
turn_count,
|
||||
created_at,
|
||||
updated_at,
|
||||
))
|
||||
.first::<(String, String, String, String, i32, i64, i64)>(conn.deref_mut())
|
||||
.optional()
|
||||
.map(|opt| {
|
||||
opt.map(|(m, t, u)| PersonaChatRow {
|
||||
messages_json: m,
|
||||
turn_count: t,
|
||||
updated_at: u,
|
||||
})
|
||||
})
|
||||
.map(|opt| opt.map(persona_chat_row))
|
||||
.map_err(|e| anyhow::anyhow!("Query error: {}", e))
|
||||
})
|
||||
.map_err(|e| DbError::log(DbErrorKind::QueryError, e))
|
||||
}
|
||||
|
||||
fn upsert_persona_chat(
|
||||
fn list_persona_chats(
|
||||
&mut self,
|
||||
cx: &opentelemetry::Context,
|
||||
uid: i32,
|
||||
) -> Result<Vec<PersonaChatRow>, DbError> {
|
||||
trace_db_call(cx, "query", "list_persona_chats", |_span| {
|
||||
use schema::persona_chat_conversations::dsl::*;
|
||||
let mut conn = self.connection.lock().expect("PersonaDao lock");
|
||||
persona_chat_conversations
|
||||
.filter(user_id.eq(uid))
|
||||
.order(updated_at.desc())
|
||||
.select((
|
||||
conversation_id,
|
||||
persona_id,
|
||||
title,
|
||||
messages_json,
|
||||
turn_count,
|
||||
created_at,
|
||||
updated_at,
|
||||
))
|
||||
.load::<(String, String, String, String, i32, i64, i64)>(conn.deref_mut())
|
||||
.map(|rows| rows.into_iter().map(persona_chat_row).collect())
|
||||
.map_err(|e| anyhow::anyhow!("Query error: {}", e))
|
||||
})
|
||||
.map_err(|e| DbError::log(DbErrorKind::QueryError, e))
|
||||
}
|
||||
|
||||
fn create_persona_chat(
|
||||
&mut self,
|
||||
cx: &opentelemetry::Context,
|
||||
uid: i32,
|
||||
pid: &str,
|
||||
json: &str,
|
||||
count: i32,
|
||||
updated_at: i64,
|
||||
) -> Result<(), DbError> {
|
||||
trace_db_call(cx, "upsert", "upsert_persona_chat", |_span| {
|
||||
created: i64,
|
||||
) -> Result<String, DbError> {
|
||||
trace_db_call(cx, "insert", "create_persona_chat", |_span| {
|
||||
use schema::persona_chat_conversations::dsl::*;
|
||||
let mut conn = self.connection.lock().expect("PersonaDao lock");
|
||||
// INSERT OR REPLACE on the (user_id, persona_id) PRIMARY KEY —
|
||||
// single rolling transcript, so a new write always supersedes
|
||||
// the prior one in full. The mobile hook serialises turns with
|
||||
// a per-persona mutex, so this never races. Plain
|
||||
// `sql_query` sidesteps a Diesel type-recursion blow-up that
|
||||
// hits `insert_into(...).on_conflict(...).do_update().set(...)`
|
||||
// with this many typed columns.
|
||||
diesel::sql_query(
|
||||
"INSERT INTO persona_chat_conversations \
|
||||
(user_id, persona_id, messages_json, turn_count, updated_at) \
|
||||
VALUES (?, ?, ?, ?, ?) \
|
||||
ON CONFLICT(user_id, persona_id) DO UPDATE SET \
|
||||
messages_json = excluded.messages_json, \
|
||||
turn_count = excluded.turn_count, \
|
||||
updated_at = excluded.updated_at",
|
||||
)
|
||||
.bind::<diesel::sql_types::Integer, _>(uid)
|
||||
.bind::<diesel::sql_types::Text, _>(pid)
|
||||
.bind::<diesel::sql_types::Text, _>(json)
|
||||
.bind::<diesel::sql_types::Integer, _>(count)
|
||||
.bind::<diesel::sql_types::BigInt, _>(updated_at)
|
||||
.execute(conn.deref_mut())
|
||||
.map_err(|e| anyhow::anyhow!("Upsert error: {}", e))?;
|
||||
Ok(())
|
||||
let new_id = uuid::Uuid::new_v4().to_string();
|
||||
diesel::insert_into(persona_chat_conversations)
|
||||
.values((
|
||||
conversation_id.eq(&new_id),
|
||||
user_id.eq(uid),
|
||||
persona_id.eq(pid),
|
||||
title.eq(""),
|
||||
// The empty tree; the first turn seeds it with the
|
||||
// persona's system prompt.
|
||||
messages_json.eq("[]"),
|
||||
turn_count.eq(0),
|
||||
created_at.eq(created),
|
||||
updated_at.eq(created),
|
||||
))
|
||||
.execute(conn.deref_mut())
|
||||
.map_err(|e| anyhow::anyhow!("Insert error: {}", e))?;
|
||||
Ok(new_id)
|
||||
})
|
||||
.map_err(|e| DbError::log(DbErrorKind::InsertError, e))
|
||||
}
|
||||
|
||||
fn clear_persona_chat(
|
||||
fn update_persona_chat(
|
||||
&mut self,
|
||||
cx: &opentelemetry::Context,
|
||||
uid: i32,
|
||||
pid: &str,
|
||||
cid: &str,
|
||||
json: &str,
|
||||
count: i32,
|
||||
updated: i64,
|
||||
) -> Result<usize, DbError> {
|
||||
trace_db_call(cx, "update", "update_persona_chat", |_span| {
|
||||
use schema::persona_chat_conversations::dsl::*;
|
||||
let mut conn = self.connection.lock().expect("PersonaDao lock");
|
||||
diesel::update(
|
||||
persona_chat_conversations
|
||||
.filter(conversation_id.eq(cid))
|
||||
.filter(user_id.eq(uid)),
|
||||
)
|
||||
.set((
|
||||
messages_json.eq(json),
|
||||
turn_count.eq(count),
|
||||
updated_at.eq(updated),
|
||||
))
|
||||
.execute(conn.deref_mut())
|
||||
.map_err(|e| anyhow::anyhow!("Update error: {}", e))
|
||||
})
|
||||
.map_err(|e| DbError::log(DbErrorKind::QueryError, e))
|
||||
}
|
||||
|
||||
fn set_persona_chat_title(
|
||||
&mut self,
|
||||
cx: &opentelemetry::Context,
|
||||
uid: i32,
|
||||
cid: &str,
|
||||
new_title: &str,
|
||||
) -> Result<(), DbError> {
|
||||
trace_db_call(cx, "delete", "clear_persona_chat", |_span| {
|
||||
trace_db_call(cx, "update", "set_persona_chat_title", |_span| {
|
||||
use schema::persona_chat_conversations::dsl::*;
|
||||
let mut conn = self.connection.lock().expect("PersonaDao lock");
|
||||
diesel::update(
|
||||
persona_chat_conversations
|
||||
.filter(conversation_id.eq(cid))
|
||||
.filter(user_id.eq(uid)),
|
||||
)
|
||||
.set(title.eq(new_title))
|
||||
.execute(conn.deref_mut())
|
||||
.map_err(|e| anyhow::anyhow!("Update error: {}", e))?;
|
||||
Ok(())
|
||||
})
|
||||
.map_err(|e| DbError::log(DbErrorKind::QueryError, e))
|
||||
}
|
||||
|
||||
fn delete_persona_chat(
|
||||
&mut self,
|
||||
cx: &opentelemetry::Context,
|
||||
uid: i32,
|
||||
cid: &str,
|
||||
) -> Result<(), DbError> {
|
||||
trace_db_call(cx, "delete", "delete_persona_chat", |_span| {
|
||||
use schema::persona_chat_conversations::dsl::*;
|
||||
let mut conn = self.connection.lock().expect("PersonaDao lock");
|
||||
// Delete the row entirely so the next get returns None.
|
||||
// The next send will INSERT a fresh seed (system + greeting)
|
||||
// via the same code path a brand-new persona uses.
|
||||
diesel::delete(
|
||||
persona_chat_conversations
|
||||
.filter(user_id.eq(uid))
|
||||
.filter(persona_id.eq(pid)),
|
||||
.filter(conversation_id.eq(cid))
|
||||
.filter(user_id.eq(uid)),
|
||||
)
|
||||
.execute(conn.deref_mut())
|
||||
.map_err(|e| anyhow::anyhow!("Delete error: {}", e))?;
|
||||
@@ -437,7 +573,6 @@ impl PersonaDao for SqlitePersonaDao {
|
||||
.map_err(|e| DbError::log(DbErrorKind::QueryError, e))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -587,74 +722,170 @@ mod tests {
|
||||
|
||||
// ── Persona-chat DAO tests ─────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn persona_chat_get_returns_none_for_never_started() {
|
||||
let cx = opentelemetry::Context::new();
|
||||
let (mut dao, uid) = dao_with_user("p1");
|
||||
let row = dao.get_persona_chat(&cx, uid, "default").unwrap();
|
||||
assert!(row.is_none());
|
||||
/// Second user, for the isolation tests.
|
||||
fn second_user(dao: &SqlitePersonaDao, username: &str) -> i32 {
|
||||
use crate::database::schema::users::dsl as u;
|
||||
let conn = dao.connection.clone();
|
||||
diesel::insert_into(u::users)
|
||||
.values((u::username.eq(username), u::password.eq("x")))
|
||||
.execute(conn.lock().unwrap().deref_mut())
|
||||
.unwrap();
|
||||
u::users
|
||||
.filter(u::username.eq(username))
|
||||
.select(u::id)
|
||||
.first(conn.lock().unwrap().deref_mut())
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn persona_chat_upsert_then_get_round_trip() {
|
||||
fn persona_chat_get_returns_none_for_an_unknown_conversation() {
|
||||
let cx = opentelemetry::Context::new();
|
||||
let (mut dao, uid) = dao_with_user("p1");
|
||||
assert!(
|
||||
dao.get_persona_chat(&cx, uid, "no-such-id")
|
||||
.unwrap()
|
||||
.is_none()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn persona_chat_create_then_get_round_trip() {
|
||||
let cx = opentelemetry::Context::new();
|
||||
let (mut dao, uid) = dao_with_user("p2");
|
||||
dao.upsert_persona_chat(&cx, uid, "journal", "[]", 1, 100)
|
||||
.unwrap();
|
||||
let row = dao.get_persona_chat(&cx, uid, "journal").unwrap().unwrap();
|
||||
assert_eq!(row.messages_json, "[]");
|
||||
assert_eq!(row.turn_count, 1);
|
||||
let cid = dao.create_persona_chat(&cx, uid, "journal", 100).unwrap();
|
||||
|
||||
let row = dao.get_persona_chat(&cx, uid, &cid).unwrap().unwrap();
|
||||
assert_eq!(row.conversation_id, cid);
|
||||
assert_eq!(row.persona_id, "journal");
|
||||
assert_eq!(row.title, "", "unnamed until the first turn completes");
|
||||
assert_eq!(row.turn_count, 0);
|
||||
assert_eq!(row.created_at, 100);
|
||||
assert_eq!(row.updated_at, 100);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn persona_chat_upsert_replaces_existing_row() {
|
||||
fn persona_chat_create_makes_a_separate_row_each_time() {
|
||||
// The whole point of the conversation_id key: one persona, several
|
||||
// independent threads.
|
||||
let cx = opentelemetry::Context::new();
|
||||
let (mut dao, uid) = dao_with_user("p3");
|
||||
dao.upsert_persona_chat(&cx, uid, "journal", "first", 1, 100).unwrap();
|
||||
dao.upsert_persona_chat(&cx, uid, "journal", "second", 2, 200).unwrap();
|
||||
let row = dao.get_persona_chat(&cx, uid, "journal").unwrap().unwrap();
|
||||
let first = dao.create_persona_chat(&cx, uid, "journal", 100).unwrap();
|
||||
let second = dao.create_persona_chat(&cx, uid, "journal", 200).unwrap();
|
||||
|
||||
assert_ne!(first, second);
|
||||
assert_eq!(dao.list_persona_chats(&cx, uid).unwrap().len(), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn persona_chat_update_replaces_the_transcript() {
|
||||
let cx = opentelemetry::Context::new();
|
||||
let (mut dao, uid) = dao_with_user("p4");
|
||||
let cid = dao.create_persona_chat(&cx, uid, "journal", 100).unwrap();
|
||||
|
||||
let rows = dao
|
||||
.update_persona_chat(&cx, uid, &cid, "first", 1, 150)
|
||||
.unwrap();
|
||||
assert_eq!(rows, 1);
|
||||
let rows = dao
|
||||
.update_persona_chat(&cx, uid, &cid, "second", 2, 200)
|
||||
.unwrap();
|
||||
assert_eq!(rows, 1);
|
||||
|
||||
let row = dao.get_persona_chat(&cx, uid, &cid).unwrap().unwrap();
|
||||
assert_eq!(row.messages_json, "second");
|
||||
assert_eq!(row.turn_count, 2);
|
||||
assert_eq!(row.updated_at, 200);
|
||||
// Single rolling transcript → exactly one row per (user, persona).
|
||||
assert_eq!(row.created_at, 100, "creation time is not disturbed");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn persona_chat_isolation_between_users() {
|
||||
fn persona_chat_update_reports_zero_rows_for_an_unknown_conversation() {
|
||||
// The turn loop treats 0 as "the conversation went away mid-turn"
|
||||
// rather than silently succeeding.
|
||||
let cx = opentelemetry::Context::new();
|
||||
let (mut dao, uid1) = dao_with_user("u1");
|
||||
let uid2: i32 = {
|
||||
let conn = dao.connection.clone();
|
||||
use crate::database::schema::users::dsl as u;
|
||||
diesel::insert_into(u::users)
|
||||
.values((u::username.eq("u2"), u::password.eq("x")))
|
||||
.execute(conn.lock().unwrap().deref_mut())
|
||||
.unwrap();
|
||||
u::users
|
||||
.filter(u::username.eq("u2"))
|
||||
.select(u::id)
|
||||
.first(conn.lock().unwrap().deref_mut())
|
||||
.unwrap()
|
||||
};
|
||||
dao.upsert_persona_chat(&cx, uid1, "default", "u1-row", 1, 1).unwrap();
|
||||
dao.upsert_persona_chat(&cx, uid2, "default", "u2-row", 1, 2).unwrap();
|
||||
let (mut dao, uid) = dao_with_user("p5");
|
||||
let rows = dao
|
||||
.update_persona_chat(&cx, uid, "no-such-id", "x", 1, 1)
|
||||
.unwrap();
|
||||
assert_eq!(rows, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn persona_chat_set_title_names_the_conversation() {
|
||||
let cx = opentelemetry::Context::new();
|
||||
let (mut dao, uid) = dao_with_user("p6");
|
||||
let cid = dao.create_persona_chat(&cx, uid, "journal", 100).unwrap();
|
||||
|
||||
dao.set_persona_chat_title(&cx, uid, &cid, "June recap")
|
||||
.unwrap();
|
||||
let row = dao.get_persona_chat(&cx, uid, &cid).unwrap().unwrap();
|
||||
assert_eq!(row.title, "June recap");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn persona_chat_list_orders_newest_first() {
|
||||
let cx = opentelemetry::Context::new();
|
||||
let (mut dao, uid) = dao_with_user("p7");
|
||||
let older = dao.create_persona_chat(&cx, uid, "journal", 100).unwrap();
|
||||
let newer = dao.create_persona_chat(&cx, uid, "coach", 300).unwrap();
|
||||
let middle = dao.create_persona_chat(&cx, uid, "default", 200).unwrap();
|
||||
|
||||
let ids: Vec<String> = dao
|
||||
.list_persona_chats(&cx, uid)
|
||||
.unwrap()
|
||||
.into_iter()
|
||||
.map(|r| r.conversation_id)
|
||||
.collect();
|
||||
assert_eq!(
|
||||
dao.get_persona_chat(&cx, uid1, "default").unwrap().unwrap().messages_json,
|
||||
"u1-row"
|
||||
);
|
||||
assert_eq!(
|
||||
dao.get_persona_chat(&cx, uid2, "default").unwrap().unwrap().messages_json,
|
||||
"u2-row"
|
||||
ids,
|
||||
vec![newer, middle, older],
|
||||
"ordered by updated_at descending so the list screen needs no re-sort"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn persona_chat_clear_wipes_the_row_so_get_returns_none() {
|
||||
fn persona_chat_list_is_empty_for_a_user_who_has_never_chatted() {
|
||||
let cx = opentelemetry::Context::new();
|
||||
let (mut dao, uid) = dao_with_user("p4");
|
||||
dao.upsert_persona_chat(&cx, uid, "default", "[]", 1, 1).unwrap();
|
||||
dao.clear_persona_chat(&cx, uid, "default").unwrap();
|
||||
assert!(dao.get_persona_chat(&cx, uid, "default").unwrap().is_none());
|
||||
let (mut dao, uid) = dao_with_user("p8");
|
||||
assert!(dao.list_persona_chats(&cx, uid).unwrap().is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn persona_chat_delete_removes_the_conversation() {
|
||||
let cx = opentelemetry::Context::new();
|
||||
let (mut dao, uid) = dao_with_user("p9");
|
||||
let cid = dao.create_persona_chat(&cx, uid, "journal", 100).unwrap();
|
||||
|
||||
dao.delete_persona_chat(&cx, uid, &cid).unwrap();
|
||||
assert!(dao.get_persona_chat(&cx, uid, &cid).unwrap().is_none());
|
||||
assert!(dao.list_persona_chats(&cx, uid).unwrap().is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn persona_chat_reads_and_writes_are_scoped_to_the_owner() {
|
||||
// A conversation_id is a bearer token for someone's transcript, so
|
||||
// holding one must not grant another user access to it.
|
||||
let cx = opentelemetry::Context::new();
|
||||
let (mut dao, uid1) = dao_with_user("owner");
|
||||
let uid2 = second_user(&dao, "intruder");
|
||||
let cid = dao.create_persona_chat(&cx, uid1, "journal", 100).unwrap();
|
||||
dao.update_persona_chat(&cx, uid1, &cid, "private", 1, 100)
|
||||
.unwrap();
|
||||
|
||||
assert!(
|
||||
dao.get_persona_chat(&cx, uid2, &cid).unwrap().is_none(),
|
||||
"another user cannot read it"
|
||||
);
|
||||
assert_eq!(
|
||||
dao.update_persona_chat(&cx, uid2, &cid, "tampered", 9, 999)
|
||||
.unwrap(),
|
||||
0,
|
||||
"another user cannot write it"
|
||||
);
|
||||
dao.delete_persona_chat(&cx, uid2, &cid).unwrap();
|
||||
|
||||
let row = dao.get_persona_chat(&cx, uid1, &cid).unwrap().unwrap();
|
||||
assert_eq!(row.messages_json, "private", "and cannot delete it");
|
||||
assert!(dao.list_persona_chats(&cx, uid2).unwrap().is_empty());
|
||||
}
|
||||
}
|
||||
@@ -172,11 +172,14 @@ diesel::table! {
|
||||
}
|
||||
|
||||
diesel::table! {
|
||||
persona_chat_conversations (user_id, persona_id) {
|
||||
persona_chat_conversations (conversation_id) {
|
||||
conversation_id -> Text,
|
||||
user_id -> Integer,
|
||||
persona_id -> Text,
|
||||
title -> Text,
|
||||
messages_json -> Text,
|
||||
turn_count -> Integer,
|
||||
created_at -> BigInt,
|
||||
updated_at -> BigInt,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user