forge
Explore
Log in

Don't you dare go Hollow

hezarfen_backend

Clone
★ 0

0 watching0 forksRust8.3 MiB6d68754Ahmet Kaan Gümüş2 months ago566 commits

rag_thread.rs60 lines, 2.2 KiBHistoryRawDownload
1//! RAG thread workflows: the create, rename and delete paths the RAG
2//! surface drives, all scoped to the owning user — a foreign id reads as
3//! absent. The queries live in [`crate::db::rag_thread`].
4
5use crate::database::Database;
6use crate::db::rag_thread;
7use crate::domain::rag_thread::{RagThread, RagThreadId, RagThreadTitle};
8use crate::domain::user::UserId;
9use crate::error::AppError;
10
11/// Start a thread unless `user` is already at the school's
12/// `max_chatbot_threads` (409 at the cap — the cap is storage protection,
13/// not a usage quota). The cap check and the row write are one atomic
14/// conditional write, so two requests racing the same user's last slot
15/// cannot both win. The knob is the chatbot nest's — one AI package, one
16/// ceiling — while the seats are this nest's own.
17pub async fn create_capped(
18 db: &Database,
19 user: &UserId,
20 title: Option<RagThreadTitle>,
21) -> Result<RagThread, AppError> {
22 rag_thread::create_capped(db, user, title).await
23}
24
25/// The caller's own threads, most recently active first.
26pub async fn list_for_user(
27 db: &Database,
28 user: &UserId,
29 limit: Option<i64>,
30 offset: i64,
31) -> Result<(Vec<RagThread>, i64), AppError> {
32 rag_thread::list_for_user(db, user, limit, offset).await
33}
34
35/// Read a thread only if `user` owns it — a foreign id reads as absent, so
36/// the web layer answers 404 rather than leaking that it exists.
37pub async fn read_for(
38 db: &Database,
39 id: &RagThreadId,
40 user: &UserId,
41) -> Result<Option<RagThread>, AppError> {
42 rag_thread::read_for(db, id, user).await
43}
44
45/// Rename the thread (`None` clears the name back to untitled), stamping
46/// the edit as activity so the thread moves to the top of the list.
47pub async fn rename(
48 db: &Database,
49 thread: &RagThread,
50 title: Option<RagThreadTitle>,
51) -> Result<RagThread, AppError> {
52 rag_thread::rename(db, thread, title).await
53}
54
55/// Delete the thread and every turn in it — one transaction, so a crash
56/// can't orphan messages under a vanished thread — and give the owner's
57/// cap slot back in the same transaction.
58pub async fn delete(db: &Database, thread: RagThread) -> Result<RagThread, AppError> {
59 rag_thread::delete(db, thread).await
60}