headless_lms_models/
chatbot_conversation_message_tool_calls.rs

1use serde_json::Value;
2
3use crate::prelude::*;
4
5#[derive(Clone, PartialEq, Deserialize, Serialize, Debug)]
6#[cfg_attr(feature = "ts_rs", derive(TS))]
7pub struct ChatbotConversationMessageToolCall {
8    pub id: Uuid,
9    pub created_at: DateTime<Utc>,
10    pub updated_at: DateTime<Utc>,
11    pub deleted_at: Option<DateTime<Utc>>,
12    pub message_id: Uuid,
13    pub tool_name: String,
14    pub tool_arguments: Value,
15    pub tool_call_id: String,
16}
17
18impl Default for ChatbotConversationMessageToolCall {
19    fn default() -> Self {
20        Self {
21            id: Uuid::nil(),
22            created_at: Default::default(),
23            updated_at: Default::default(),
24            deleted_at: None,
25            message_id: Uuid::nil(),
26            tool_name: Default::default(),
27            tool_arguments: Default::default(),
28            tool_call_id: Default::default(),
29        }
30    }
31}
32
33pub async fn insert(
34    conn: &mut PgConnection,
35    input: ChatbotConversationMessageToolCall,
36    msg_id: Uuid,
37) -> ModelResult<ChatbotConversationMessageToolCall> {
38    let res = sqlx::query_as!(
39        ChatbotConversationMessageToolCall,
40        r#"
41INSERT INTO chatbot_conversation_message_tool_calls (
42    message_id,
43    tool_name,
44    tool_arguments,
45    tool_call_id
46  )
47VALUES ($1, $2, $3, $4)
48RETURNING *
49        "#,
50        msg_id,
51        input.tool_name,
52        input.tool_arguments,
53        input.tool_call_id,
54    )
55    .fetch_one(conn)
56    .await?;
57    Ok(res)
58}
59
60pub async fn insert_batch(
61    conn: &mut PgConnection,
62    input: Vec<ChatbotConversationMessageToolCall>,
63    msg_id: Uuid,
64) -> ModelResult<Vec<ChatbotConversationMessageToolCall>> {
65    let tool_names: Vec<String> = input.iter().map(|i| i.tool_name.to_owned()).collect();
66    let tool_args: Vec<Value> = input.iter().map(|i| i.tool_arguments.to_owned()).collect();
67    let tool_ids: Vec<String> = input.iter().map(|i| i.tool_call_id.to_owned()).collect();
68
69    let res = sqlx::query_as!(
70        ChatbotConversationMessageToolCall,
71        r#"
72INSERT INTO chatbot_conversation_message_tool_calls (
73    message_id,
74    tool_name,
75    tool_arguments,
76    tool_call_id
77  )
78SELECT $1,
79  UNNEST($2::VARCHAR(255) []),
80  UNNEST($3::JSONB []),
81  UNNEST($4::VARCHAR(255) [])
82RETURNING *
83        "#,
84        msg_id,
85        &tool_names,
86        &tool_args,
87        &tool_ids,
88    )
89    .fetch_all(conn)
90    .await?;
91    Ok(res)
92}
93
94pub async fn get_by_id(
95    conn: &mut PgConnection,
96    id: Uuid,
97) -> ModelResult<ChatbotConversationMessageToolCall> {
98    let res = sqlx::query_as!(
99        ChatbotConversationMessageToolCall,
100        r#"
101SELECT *
102FROM chatbot_conversation_message_tool_calls
103WHERE id = $1
104  AND deleted_at IS NULL
105        "#,
106        id
107    )
108    .fetch_one(conn)
109    .await?;
110    Ok(res)
111}
112
113pub async fn get_by_message_id(
114    conn: &mut PgConnection,
115    msg_id: Uuid,
116) -> ModelResult<Vec<ChatbotConversationMessageToolCall>> {
117    let res = sqlx::query_as!(
118        ChatbotConversationMessageToolCall,
119        r#"
120SELECT *
121FROM chatbot_conversation_message_tool_calls
122WHERE message_id = $1
123  AND deleted_at IS NULL
124        "#,
125        msg_id
126    )
127    .fetch_all(conn)
128    .await?;
129    Ok(res)
130}
131
132pub async fn delete_all_by_message_id(
133    conn: &mut PgConnection,
134    msg_id: Uuid,
135) -> ModelResult<Vec<ChatbotConversationMessageToolCall>> {
136    let res = sqlx::query_as!(
137        ChatbotConversationMessageToolCall,
138        r#"
139UPDATE chatbot_conversation_message_tool_calls
140SET deleted_at = NOW()
141WHERE message_id = $1
142RETURNING *
143        "#,
144        msg_id
145    )
146    .fetch_all(conn)
147    .await?;
148    Ok(res)
149}