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    id,
51    created_at,
52    updated_at,
53    deleted_at,
54    chatbot_conversation_message_id,
55    output,
56    tool_call_id,
57    tool_kind as "tool_kind: ToolKind",
58    response_id
59        "#,
60        msg_id,
61        input.output,
62        input.tool_call_id,
63        input.tool_kind as ToolKind,
64        input.response_id
65    )
66    .fetch_one(conn)
67    .await?;
68    Ok(res)
69}
70
71pub async fn get_by_id(
72    conn: &mut PgConnection,
73    id: Uuid,
74) -> ModelResult<ChatbotConversationMessageToolOutput> {
75    let res = sqlx::query_as!(
76        ChatbotConversationMessageToolOutput,
77        r#"
78SELECT
79    id,
80    created_at,
81    updated_at,
82    deleted_at,
83    chatbot_conversation_message_id,
84    output,
85    tool_call_id,
86    tool_kind as "tool_kind: ToolKind",
87    response_id
88FROM chatbot_conversation_message_tool_outputs
89WHERE id = $1
90  AND deleted_at IS NULL
91        "#,
92        id
93    )
94    .fetch_one(conn)
95    .await?;
96    Ok(res)
97}
98
99pub async fn get_by_message_id(
100    conn: &mut PgConnection,
101    message_id: Uuid,
102) -> ModelResult<Option<ChatbotConversationMessageToolOutput>> {
103    let res = sqlx::query_as!(
104        ChatbotConversationMessageToolOutput,
105        r#"
106SELECT
107    id,
108    created_at,
109    updated_at,
110    deleted_at,
111    chatbot_conversation_message_id,
112    output,
113    tool_call_id,
114    tool_kind as "tool_kind: ToolKind",
115    response_id
116FROM chatbot_conversation_message_tool_outputs
117WHERE chatbot_conversation_message_id = $1
118  AND deleted_at IS NULL
119        "#,
120        message_id
121    )
122    .fetch_optional(conn)
123    .await?;
124    Ok(res)
125}
126
127pub async fn delete(
128    conn: &mut PgConnection,
129    id: Uuid,
130) -> ModelResult<ChatbotConversationMessageToolOutput> {
131    let res = sqlx::query_as!(
132        ChatbotConversationMessageToolOutput,
133        r#"
134UPDATE chatbot_conversation_message_tool_outputs
135SET deleted_at = NOW()
136WHERE id = $1
137  AND deleted_at IS NULL
138RETURNING
139    id,
140    created_at,
141    updated_at,
142    deleted_at,
143    chatbot_conversation_message_id,
144    output,
145    tool_call_id,
146    tool_kind as "tool_kind: ToolKind",
147    response_id
148        "#,
149        id
150    )
151    .fetch_one(conn)
152    .await?;
153    Ok(res)
154}