Skip to main content

headless_lms_models/
chatbot_conversation_message_tool_calls.rs

1use serde_json::Value;
2use utoipa::ToSchema;
3
4use crate::prelude::*;
5
6#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone, Copy, Type, ToSchema)]
7#[sqlx(type_name = "tool_kind", rename_all = "kebab-case")]
8#[serde(rename_all = "snake_case")]
9pub enum ToolKind {
10    Function,
11    AzureAiSearch,
12}
13
14#[derive(Clone, PartialEq, Deserialize, Serialize, Debug, ToSchema)]
15pub struct ChatbotConversationMessageToolCall {
16    pub id: Uuid,
17    pub created_at: DateTime<Utc>,
18    pub updated_at: DateTime<Utc>,
19    pub deleted_at: Option<DateTime<Utc>>,
20    pub chatbot_conversation_message_id: Uuid,
21    pub tool_name: String,
22    pub tool_arguments: Value,
23    pub tool_call_id: String,
24    pub tool_kind: ToolKind,
25    pub response_id: String,
26}
27
28impl Default for ChatbotConversationMessageToolCall {
29    fn default() -> Self {
30        Self {
31            id: Uuid::nil(),
32            created_at: Default::default(),
33            updated_at: Default::default(),
34            deleted_at: None,
35            chatbot_conversation_message_id: Uuid::nil(),
36            tool_name: Default::default(),
37            tool_arguments: Default::default(),
38            tool_call_id: Default::default(),
39            tool_kind: ToolKind::Function,
40            response_id: Default::default(),
41        }
42    }
43}
44
45pub async fn insert(
46    conn: &mut PgConnection,
47    input: ChatbotConversationMessageToolCall,
48    msg_id: Uuid,
49) -> ModelResult<ChatbotConversationMessageToolCall> {
50    let res = sqlx::query_as!(
51        ChatbotConversationMessageToolCall,
52        r#"
53INSERT INTO chatbot_conversation_message_tool_calls (
54    chatbot_conversation_message_id,
55    tool_name,
56    tool_arguments,
57    tool_call_id,
58    tool_kind,
59    response_id
60  )
61VALUES ($1, $2, $3, $4, $5, $6)
62RETURNING
63    id,
64    created_at,
65    updated_at,
66    deleted_at,
67    chatbot_conversation_message_id,
68    tool_name,
69    tool_arguments,
70    tool_call_id,
71    tool_kind as "tool_kind: ToolKind",
72    response_id
73        "#,
74        msg_id,
75        input.tool_name,
76        input.tool_arguments,
77        input.tool_call_id,
78        input.tool_kind as ToolKind,
79        input.response_id
80    )
81    .fetch_one(conn)
82    .await?;
83    Ok(res)
84}
85
86pub async fn get_by_id(
87    conn: &mut PgConnection,
88    id: Uuid,
89) -> ModelResult<ChatbotConversationMessageToolCall> {
90    let res = sqlx::query_as!(
91        ChatbotConversationMessageToolCall,
92        r#"
93SELECT
94    id,
95    created_at,
96    updated_at,
97    deleted_at,
98    chatbot_conversation_message_id,
99    tool_name,
100    tool_arguments,
101    tool_call_id,
102    tool_kind as "tool_kind: ToolKind",
103    response_id
104FROM chatbot_conversation_message_tool_calls
105WHERE id = $1
106  AND deleted_at IS NULL
107        "#,
108        id
109    )
110    .fetch_one(conn)
111    .await?;
112    Ok(res)
113}
114
115pub async fn get_by_message_id(
116    conn: &mut PgConnection,
117    msg_id: Uuid,
118) -> ModelResult<Option<ChatbotConversationMessageToolCall>> {
119    let res = sqlx::query_as!(
120        ChatbotConversationMessageToolCall,
121        r#"
122SELECT
123    id,
124    created_at,
125    updated_at,
126    deleted_at,
127    chatbot_conversation_message_id,
128    tool_name,
129    tool_arguments,
130    tool_call_id,
131    tool_kind as "tool_kind: ToolKind",
132    response_id
133FROM chatbot_conversation_message_tool_calls
134WHERE chatbot_conversation_message_id = $1
135  AND deleted_at IS NULL
136        "#,
137        msg_id
138    )
139    .fetch_optional(conn)
140    .await?;
141    Ok(res)
142}
143
144pub async fn delete(
145    conn: &mut PgConnection,
146    id: Uuid,
147) -> ModelResult<ChatbotConversationMessageToolCall> {
148    let res = sqlx::query_as!(
149        ChatbotConversationMessageToolCall,
150        r#"
151UPDATE chatbot_conversation_message_tool_calls
152SET deleted_at = NOW()
153WHERE id = $1
154  AND deleted_at IS NULL
155RETURNING
156    id,
157    created_at,
158    updated_at,
159    deleted_at,
160    chatbot_conversation_message_id,
161    tool_name,
162    tool_arguments,
163    tool_call_id,
164    tool_kind as "tool_kind: ToolKind",
165    response_id
166        "#,
167        id
168    )
169    .fetch_one(conn)
170    .await?;
171    Ok(res)
172}