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}
16
17impl Default for ChatbotConversationMessageToolOutput {
18    fn default() -> Self {
19        Self {
20            id: Uuid::nil(),
21            created_at: Default::default(),
22            updated_at: Default::default(),
23            deleted_at: None,
24            chatbot_conversation_message_id: Uuid::nil(),
25            output: Default::default(),
26            tool_call_id: Default::default(),
27            tool_kind: ToolKind::Function,
28            response_id: Default::default(),
29        }
30    }
31}
32
33pub async fn insert(
34    conn: &mut PgConnection,
35    input: ChatbotConversationMessageToolOutput,
36    msg_id: Uuid,
37) -> ModelResult<ChatbotConversationMessageToolOutput> {
38    let res = sqlx::query_as!(
39        ChatbotConversationMessageToolOutput,
40        r#"
41INSERT INTO chatbot_conversation_message_tool_outputs (
42    chatbot_conversation_message_id,
43    output,
44    tool_call_id,
45    tool_kind,
46    response_id
47  )
48VALUES ($1, $2, $3, $4, $5)
49RETURNING *
50        "#,
51        msg_id,
52        input.output,
53        input.tool_call_id,
54        input.tool_kind as ToolKind,
55        input.response_id
56    )
57    .fetch_one(conn)
58    .await?;
59    Ok(res)
60}
61
62pub async fn get_by_id(
63    conn: &mut PgConnection,
64    id: Uuid,
65) -> ModelResult<ChatbotConversationMessageToolOutput> {
66    let res = sqlx::query_as!(
67        ChatbotConversationMessageToolOutput,
68        r#"
69SELECT *
70FROM chatbot_conversation_message_tool_outputs
71WHERE id = $1
72  AND deleted_at IS NULL
73        "#,
74        id
75    )
76    .fetch_one(conn)
77    .await?;
78    Ok(res)
79}
80
81pub async fn get_by_message_id(
82    conn: &mut PgConnection,
83    message_id: Uuid,
84) -> ModelResult<Option<ChatbotConversationMessageToolOutput>> {
85    let res = sqlx::query_as!(
86        ChatbotConversationMessageToolOutput,
87        r#"
88SELECT *
89FROM chatbot_conversation_message_tool_outputs
90WHERE chatbot_conversation_message_id = $1
91  AND deleted_at IS NULL
92        "#,
93        message_id
94    )
95    .fetch_optional(conn)
96    .await?;
97    Ok(res)
98}
99
100pub async fn delete(
101    conn: &mut PgConnection,
102    id: Uuid,
103) -> ModelResult<ChatbotConversationMessageToolOutput> {
104    let res = sqlx::query_as!(
105        ChatbotConversationMessageToolOutput,
106        r#"
107UPDATE chatbot_conversation_message_tool_outputs
108SET deleted_at = NOW()
109WHERE id = $1
110  AND deleted_at IS NULL
111RETURNING *
112        "#,
113        id
114    )
115    .fetch_one(conn)
116    .await?;
117    Ok(res)
118}