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