Skip to main content

headless_lms_models/
chatbot_conversation_message_tool_outputs.rs

1use crate::{chatbot_conversation_message_tool_calls::ToolKind, prelude::*};
2use utoipa::ToSchema;
3
4#[derive(Clone, PartialEq, Deserialize, Serialize, Debug, ToSchema)]
5pub struct ChatbotConversationMessageToolOutput {
6    pub id: Uuid,
7    pub created_at: DateTime<Utc>,
8    pub updated_at: DateTime<Utc>,
9    pub deleted_at: Option<DateTime<Utc>>,
10    pub chatbot_conversation_message_id: Uuid,
11    pub output: String,
12    pub tool_call_id: String,
13    pub tool_kind: ToolKind,
14    pub response_id: String,
15    /// The answer payload the client sent, in the shape the answered tool defines. None for
16    /// outputs no client answered.
17    #[schema(value_type = Option<Object>)]
18    pub client_answer: Option<serde_json::Value>,
19}
20
21impl Default for ChatbotConversationMessageToolOutput {
22    fn default() -> Self {
23        Self {
24            id: Uuid::nil(),
25            created_at: Default::default(),
26            updated_at: Default::default(),
27            deleted_at: None,
28            chatbot_conversation_message_id: Uuid::nil(),
29            output: Default::default(),
30            tool_call_id: Default::default(),
31            tool_kind: ToolKind::Function,
32            response_id: Default::default(),
33            client_answer: None,
34        }
35    }
36}
37
38pub async fn insert(
39    conn: &mut PgConnection,
40    input: ChatbotConversationMessageToolOutput,
41    msg_id: Uuid,
42) -> ModelResult<ChatbotConversationMessageToolOutput> {
43    let res = sqlx::query_as!(
44        ChatbotConversationMessageToolOutput,
45        r#"
46INSERT INTO chatbot_conversation_message_tool_outputs (
47    chatbot_conversation_message_id,
48    output,
49    tool_call_id,
50    tool_kind,
51    response_id,
52    client_answer
53  )
54VALUES ($1, $2, $3, $4, $5, $6)
55RETURNING *
56        "#,
57        msg_id,
58        input.output,
59        input.tool_call_id,
60        input.tool_kind as ToolKind,
61        input.response_id,
62        input.client_answer
63    )
64    .fetch_one(conn)
65    .await?;
66    Ok(res)
67}
68
69pub async fn get_by_id(
70    conn: &mut PgConnection,
71    id: Uuid,
72) -> ModelResult<ChatbotConversationMessageToolOutput> {
73    let res = sqlx::query_as!(
74        ChatbotConversationMessageToolOutput,
75        r#"
76SELECT *
77FROM chatbot_conversation_message_tool_outputs
78WHERE id = $1
79  AND deleted_at IS NULL
80        "#,
81        id
82    )
83    .fetch_one(conn)
84    .await?;
85    Ok(res)
86}
87
88pub async fn get_by_message_id(
89    conn: &mut PgConnection,
90    message_id: Uuid,
91) -> ModelResult<Option<ChatbotConversationMessageToolOutput>> {
92    let res = sqlx::query_as!(
93        ChatbotConversationMessageToolOutput,
94        r#"
95SELECT *
96FROM chatbot_conversation_message_tool_outputs
97WHERE chatbot_conversation_message_id = $1
98  AND deleted_at IS NULL
99        "#,
100        message_id
101    )
102    .fetch_optional(conn)
103    .await?;
104    Ok(res)
105}
106
107pub async fn delete(
108    conn: &mut PgConnection,
109    id: Uuid,
110) -> ModelResult<ChatbotConversationMessageToolOutput> {
111    let res = sqlx::query_as!(
112        ChatbotConversationMessageToolOutput,
113        r#"
114UPDATE chatbot_conversation_message_tool_outputs
115SET deleted_at = NOW()
116WHERE id = $1
117  AND deleted_at IS NULL
118RETURNING *
119        "#,
120        id
121    )
122    .fetch_one(conn)
123    .await?;
124    Ok(res)
125}