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

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