bridge_common/repo/
agents.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// Copyright 2024 StarfleetAI
// SPDX-License-Identifier: Apache-2.0

use chrono::Utc;
use sqlx::{query, query_as, Executor, Postgres};

use crate::types::{agents::Agent, Result};

pub struct CreateParams {
    pub name: String,
    pub description: String,
    pub system_message: String,
    pub is_code_interpreter_enabled: bool,
    pub is_web_browser_enabled: bool,
}

pub struct UpdateParams {
    pub id: i32,
    pub name: String,
    pub description: String,
    pub system_message: String,
    pub is_code_interpreter_enabled: bool,
    pub is_web_browser_enabled: bool,
}

/// List all agents.
///
/// # Errors
///
/// Returns error if there was a problem while accessing database.
pub async fn list<'a, E>(executor: E, company_id: i32) -> Result<Vec<Agent>>
where
    E: Executor<'a, Database = Postgres>,
{
    Ok(query_as!(
        Agent,
        "SELECT * FROM agents WHERE company_id = $1 ORDER BY id ASC",
        company_id
    )
    .fetch_all(executor)
    .await?)
}

/// List enabled agents.
///
/// # Errors
///
/// Returns error if there was a problem while accessing database.
pub async fn list_enabled<'a, E>(executor: E, company_id: i32) -> Result<Vec<Agent>>
where
    E: Executor<'a, Database = Postgres>,
{
    Ok(query_as!(
        Agent,
        "SELECT * FROM agents WHERE company_id = $1 AND is_enabled = TRUE ORDER BY id ASC",
        company_id
    )
    .fetch_all(executor)
    .await?)
}

/// Get agent by id.
///
/// # Errors
///
/// Returns error if there was a problem while accessing database.
pub async fn get<'a, E>(executor: E, company_id: i32, id: i32) -> Result<Agent>
where
    E: Executor<'a, Database = Postgres>,
{
    Ok(query_as!(
        Agent,
        "SELECT * FROM agents WHERE company_id = $1 AND id = $2 LIMIT 1",
        company_id,
        id
    )
    .fetch_one(executor)
    .await?)
}

/// Get agent by chat id.
///
/// # Errors
///
/// Returns error if there was a problem while accessing database.
pub async fn get_for_chat<'a, E>(executor: E, company_id: i32, chat_id: i32) -> Result<Agent>
where
    E: Executor<'a, Database = Postgres>,
{
    Ok(query_as!(
        Agent,
        r#"
        SELECT agents.*
        FROM agents
        INNER JOIN agents_chats ON agents.id = agents_chats.agent_id
        WHERE agents.company_id = $1 AND agents_chats.chat_id = $2
        LIMIT 1
        "#,
        company_id,
        chat_id
    )
    .fetch_one(executor)
    .await?)
}

/// Get agent.
///
/// # Errors
///
/// Returns error if there was a problem while accessing database.
pub async fn create<'a, E>(executor: E, company_id: i32, params: CreateParams) -> Result<Agent>
where
    E: Executor<'a, Database = Postgres>,
{
    let now = Utc::now();

    Ok(query_as!(
        Agent,
        r#"
        INSERT INTO agents (
            company_id, name, description, system_message,
            created_at, updated_at, is_code_interpreter_enabled, is_web_browser_enabled
        )
        VALUES ($1, $2, $3, $4, $5, $5, $6, $7)
        RETURNING *
        "#,
        company_id,
        params.name,
        params.description,
        params.system_message,
        now,
        params.is_code_interpreter_enabled,
        params.is_web_browser_enabled,
    )
    .fetch_one(executor)
    .await?)
}

/// Update agent.
///
/// # Errors
///
/// Returns error if there was a problem while accessing database.
pub async fn update<'a, E>(executor: E, company_id: i32, params: UpdateParams) -> Result<Agent>
where
    E: Executor<'a, Database = Postgres>,
{
    let now = Utc::now();

    Ok(query_as!(
        Agent,
        r#"
        UPDATE agents
        SET
            name = $3, description = $4, system_message = $5, updated_at = $6,
            is_code_interpreter_enabled = $7, is_web_browser_enabled = $8
        WHERE company_id = $1 AND id = $2
        RETURNING *
        "#,
        company_id,
        params.id,
        params.name,
        params.description,
        params.system_message,
        now,
        params.is_code_interpreter_enabled,
        params.is_web_browser_enabled,
    )
    .fetch_one(executor)
    .await?)
}

/// Update `is_enabled` field for agent by id.
///
/// # Errors
///
/// Returns error if agent with given id does not exist.
/// Returns error if any error occurs while accessing database.
pub async fn update_is_enabled<'a, E>(
    executor: E,
    company_id: i32,
    id: i32,
    is_enabled: bool,
) -> Result<()>
where
    E: Executor<'a, Database = Postgres>,
{
    query!(
        "UPDATE agents SET is_enabled = $3 WHERE company_id = $1 AND id = $2",
        company_id,
        id,
        is_enabled
    )
    .execute(executor)
    .await?;

    Ok(())
}

/// Delete agent.
///
/// # Errors
///
/// Returns error if there was a problem while accessing database.
pub async fn delete<'a, E>(executor: E, company_id: i32, id: i32) -> Result<()>
where
    E: Executor<'a, Database = Postgres>,
{
    query!(
        "DELETE FROM agents WHERE company_id = $1 AND id = $2",
        company_id,
        id
    )
    .execute(executor)
    .await?;

    Ok(())
}