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_message.rs98 lines, 3.2 KiBHistoryRawDownload
1//! RAG message workflows: the turns of a thread. Sending is asynchronous by
2//! design — the send path writes the user's question plus a `pending`
3//! assistant row and answers at once; the answering task settles that row
4//! later with [`complete`] or [`fail`], and the read paths present a
5//! long-stale `pending` as failed via the domain's read-time projection
6//! (see [`RagMessage::projected`]). The queries live in
7//! [`crate::db::rag_message`].
8
9use crate::database::Database;
10use crate::db::rag_message;
11use crate::domain::rag_message::{RagContent, RagMessage, RagMessageId, RagReply};
12use crate::domain::rag_thread::RagThreadId;
13use crate::domain::user::UserId;
14use crate::error::AppError;
15use sqlx::types::Json;
16
17/// Append the user's question. Nothing is awaited for it, so it is born
18/// complete.
19pub async fn append_user(
20 db: &Database,
21 thread: &RagThreadId,
22 user: &UserId,
23 content: RagContent,
24) -> Result<RagMessage, AppError> {
25 rag_message::append_user(db, thread, user, content).await
26}
27
28/// Reserve the assistant's answer *before* the AI call: the row exists,
29/// empty and `pending`, so a reload finds the turn and can wait on it.
30pub async fn append_pending_assistant(
31 db: &Database,
32 thread: &RagThreadId,
33 user: &UserId,
34) -> Result<RagMessage, AppError> {
35 rag_message::append_pending_assistant(db, thread, user).await
36}
37
38/// The whole thread, oldest first — the order both the UI and the AI
39/// history payload read in.
40pub async fn list_for_thread(
41 db: &Database,
42 thread: &RagThreadId,
43 limit: Option<i64>,
44 offset: i64,
45) -> Result<(Vec<RagMessage>, i64), AppError> {
46 rag_message::list_for_thread(db, thread, limit, offset).await
47}
48
49/// The last `limit` *settled* turns that carry text, still oldest-first —
50/// the tail replayed to the AI service as context.
51pub async fn list_settled_tail(
52 db: &Database,
53 thread: &RagThreadId,
54 limit: usize,
55) -> Result<Vec<RagMessage>, AppError> {
56 rag_message::list_settled_tail(db, thread, limit).await
57}
58
59/// The user turn a reserved answer belongs to, by *identity*: the question
60/// row of the very POST that reserved it, whose id the answering task
61/// carries.
62pub async fn prompt_of(
63 db: &Database,
64 id: &RagMessageId,
65) -> Result<Option<RagMessage>, AppError> {
66 rag_message::prompt_of(db, id).await
67}
68
69/// Read one turn only if `user` owns it — the poll loop's read.
70pub async fn read_for(
71 db: &Database,
72 id: &RagMessageId,
73 user: &UserId,
74) -> Result<Option<RagMessage>, AppError> {
75 rag_message::read_for(db, id, user).await
76}
77
78/// Land the answer: its text and the service's reply (abstention, reason,
79/// citations). Gated on `status = 'pending'`, so a late answer can't
80/// overwrite a row that was already failed.
81pub async fn complete(
82 db: &Database,
83 id: &RagMessageId,
84 text: RagContent,
85 reply: Json<RagReply>,
86) -> Result<RagMessage, AppError> {
87 rag_message::complete(db, id, text, reply).await
88}
89
90/// Mark the answer failed with a short code (`unavailable`, the service's
91/// own error code, …). Same pending gate as [`complete`].
92pub async fn fail(
93 db: &Database,
94 id: &RagMessageId,
95 error_code: &str,
96) -> Result<RagMessage, AppError> {
97 rag_message::fail(db, id, error_code).await
98}