ares-tools 0.10.0

Built-in tools for ARES agents
Documentation
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
//! Runtime SQL tool executor for A.R.E.S.
//!
//! This tool executes SQL queries using templates configured via `execution_config`.
//! Queries use `$1, $2, ...` style positional parameters.  Arguments are bound in
//! **alphabetical key order** — e.g. `{"b": 1, "a": 2}` maps `$1 → 2`, `$2 → 1`.
//! Results are returned as a JSON array of objects.
//!
//! # Security
//!
//! The tool relies on PostgreSQL parameterised queries.  Never build SQL by
//! interpolating argument strings into the template; always use `$N` placeholders.

use crate::registry::Tool;
use ares_types::Result;
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use sqlx::{Column, PgPool, Row};
use std::time::Duration;
use tokio::time::timeout;

// =============================================================================
// Configuration
// =============================================================================

/// SQL-specific configuration parsed from `execution_config` JSONB.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct SqlToolConfig {
    /// SQL query template with `$1, $2, ...` positional placeholders.
    pub query_template: String,
    /// Optional PostgreSQL connection string.  If omitted the tool uses the
    /// default `PgPool` supplied at construction time.
    #[serde(default)]
    pub connection_string: Option<String>,
    /// Query timeout in seconds (default: 30).
    #[serde(default)]
    pub timeout_secs: Option<u64>,
    /// Maximum number of result rows allowed (default: 1000).
    #[serde(default)]
    pub max_rows: Option<u64>,
}

impl Default for SqlToolConfig {
    fn default() -> Self {
        Self {
            query_template: String::new(),
            connection_string: None,
            timeout_secs: Some(30),
            max_rows: Some(1000),
        }
    }
}

// =============================================================================
// Tool implementation
// =============================================================================

/// Runtime SQL tool that executes parameterised PostgreSQL queries.
#[derive(Debug)]
pub struct SqlTool {
    name: String,
    description: String,
    parameters_schema: Value,
    config: SqlToolConfig,
    default_pool: Option<PgPool>,
    custom_pool: Option<PgPool>,
}

impl SqlTool {
    /// Create an SQL tool from its runtime configuration.
    ///
    /// `default_pool` is used when `config.connection_string` is `None`.
    pub fn new(
        name: impl Into<String>,
        description: impl Into<String>,
        parameters_schema: Value,
        config: SqlToolConfig,
        default_pool: Option<PgPool>,
    ) -> Result<Self> {
        let custom_pool = config
            .connection_string
            .as_ref()
            .map(|url| {
                sqlx::postgres::PgPoolOptions::new()
                    .max_connections(1)
                    .connect_lazy(url)
                    .map_err(|e| {
                        ares_types::AppError::Configuration(format!(
                            "Invalid SQL tool connection string: {e}"
                        ))
                    })
            })
            .transpose()?;

        Ok(Self {
            name: name.into(),
            description: description.into(),
            parameters_schema,
            config,
            default_pool,
            custom_pool,
        })
    }

    /// Parse `execution_config` JSONB into [`SqlToolConfig`].
    pub fn parse_config(execution_config: &Value) -> Result<SqlToolConfig> {
        serde_json::from_value(execution_config.clone()).map_err(|e| {
            ares_types::AppError::Configuration(format!("Invalid SQL tool config: {e}"))
        })
    }

    fn timeout(&self) -> Duration {
        Duration::from_secs(self.config.timeout_secs.unwrap_or(30))
    }

    fn max_rows(&self) -> u64 {
        self.config.max_rows.unwrap_or(1000)
    }
}

#[async_trait]
impl Tool for SqlTool {
    fn name(&self) -> &str {
        &self.name
    }

    fn description(&self) -> &str {
        &self.description
    }

    fn parameters_schema(&self) -> Value {
        self.parameters_schema.clone()
    }

    async fn execute(&self, args: Value) -> Result<Value> {
        let args_map = args.as_object().ok_or_else(|| {
            ares_types::AppError::InvalidInput("args must be a JSON object".to_string())
        })?;

        let pool = self
            .custom_pool
            .as_ref()
            .or(self.default_pool.as_ref())
            .ok_or_else(|| {
                ares_types::AppError::Configuration(
                    "No database pool available for SQL tool".to_string(),
                )
            })?;

        // Bind arguments in alphabetical key order so the mapping to `$1,$2,…`
        // is deterministic and self-documenting.
        let mut keys: Vec<&str> = args_map.keys().map(|s| s.as_str()).collect();
        keys.sort_unstable();

        let mut query = sqlx::query(&self.config.query_template);
        for key in &keys {
            let val = &args_map[*key];
            query = bind_json_value(query, val);
        }

        let fut = query.fetch_all(pool);
        let rows = timeout(self.timeout(), fut)
            .await
            .map_err(|_| ares_types::AppError::External("SQL query timed out".to_string()))?
            .map_err(|e| ares_types::AppError::Database(format!("SQL execution failed: {e}")))?;

        let max_rows = self.max_rows() as usize;
        if rows.len() > max_rows {
            return Err(ares_types::AppError::InvalidInput(format!(
                "Query returned {} rows, exceeding max_rows of {}",
                rows.len(),
                max_rows
            )));
        }

        let results: Result<Vec<Value>> = rows.iter().map(row_to_json).collect();

        Ok(json!(results?))
    }
}

// =============================================================================
// Helpers
// =============================================================================

/// Bind a `serde_json::Value` into a sqlx Postgres query.
fn bind_json_value<'q>(
    mut query: sqlx::query::Query<'q, sqlx::Postgres, sqlx::postgres::PgArguments>,
    value: &'q Value,
) -> sqlx::query::Query<'q, sqlx::Postgres, sqlx::postgres::PgArguments> {
    match value {
        Value::Null => {
            query = query.bind(None::<String>);
        }
        Value::Bool(b) => {
            query = query.bind(*b);
        }
        Value::Number(n) => {
            if let Some(i) = n.as_i64() {
                query = query.bind(i);
            } else if let Some(f) = n.as_f64() {
                query = query.bind(f);
            } else {
                query = query.bind(n.to_string());
            }
        }
        Value::String(s) => {
            query = query.bind(s.as_str());
        }
        Value::Array(_) | Value::Object(_) => {
            query = query.bind(sqlx::types::Json(value));
        }
    }
    query
}

/// Convert a `PgRow` into a JSON object keyed by column names.
fn row_to_json(row: &sqlx::postgres::PgRow) -> Result<Value> {
    let mut obj = serde_json::Map::new();
    for (i, col) in row.columns().iter().enumerate() {
        let name = col.name();
        let value = pg_value_to_json(row, i)?;
        obj.insert(name.to_string(), value);
    }
    Ok(Value::Object(obj))
}

/// Extract a single PostgreSQL cell value into `serde_json::Value`.
fn pg_value_to_json(row: &sqlx::postgres::PgRow, i: usize) -> Result<Value> {
    // JSON / JSONB columns
    if let Ok(sqlx::types::Json(v)) = row.try_get::<sqlx::types::Json<Value>, _>(i) {
        return Ok(v);
    }

    // Nullable string (covers TEXT, VARCHAR, CHAR, UUID, enums, … and NULL)
    if let Ok(opt) = row.try_get::<Option<String>, _>(i) {
        return Ok(opt.map_or(Value::Null, Value::String));
    }

    // Nullable integers (INT2/INT4 decode separately from INT8)
    if let Ok(opt) = row.try_get::<Option<i64>, _>(i) {
        return Ok(opt.map_or(Value::Null, |v| Value::Number(v.into())));
    }
    if let Ok(opt) = row.try_get::<Option<i32>, _>(i) {
        return Ok(opt.map_or(Value::Null, |v| Value::Number(v.into())));
    }
    if let Ok(opt) = row.try_get::<Option<i16>, _>(i) {
        return Ok(opt.map_or(Value::Null, |v| Value::Number(v.into())));
    }

    // Nullable float
    if let Ok(opt) = row.try_get::<Option<f64>, _>(i) {
        return Ok(opt.map_or(Value::Null, |v| {
            serde_json::Number::from_f64(v)
                .map_or_else(|| Value::String(v.to_string()), Value::Number)
        }));
    }

    // Nullable boolean
    if let Ok(opt) = row.try_get::<Option<bool>, _>(i) {
        return Ok(opt.map_or(Value::Null, Value::Bool));
    }

    // Fallback: try non-null string coercion for anything that can render as text.
    if let Ok(s) = row.try_get::<String, _>(i) {
        return Ok(Value::String(s));
    }

    Ok(Value::Null)
}

// =============================================================================
// Tests
// =============================================================================

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    fn make_test_tool(config: SqlToolConfig, pool: Option<PgPool>) -> SqlTool {
        SqlTool::new(
            "test_sql",
            "A test SQL tool",
            json!({
                "type": "object",
                "properties": {
                    "user_id": { "type": "string" }
                }
            }),
            config,
            pool,
        )
        .unwrap()
    }

    // -------------------------------------------------------------------------
    // Non-DB tests
    // -------------------------------------------------------------------------

    #[test]
    fn test_name_and_description() {
        let tool = make_test_tool(SqlToolConfig::default(), None);
        assert_eq!(tool.name(), "test_sql");
        assert_eq!(tool.description(), "A test SQL tool");
    }

    #[test]
    fn test_parameters_schema() {
        let tool = make_test_tool(SqlToolConfig::default(), None);
        let schema = tool.parameters_schema();
        assert_eq!(schema["type"], "object");
    }

    #[test]
    fn test_parse_config() {
        let raw = json!({
            "query_template": "SELECT * FROM users WHERE id = $1",
            "timeout_secs": 15,
            "max_rows": 50
        });
        let cfg = SqlTool::parse_config(&raw).unwrap();
        assert_eq!(cfg.query_template, "SELECT * FROM users WHERE id = $1");
        assert_eq!(cfg.timeout_secs, Some(15));
        assert_eq!(cfg.max_rows, Some(50));
        assert!(cfg.connection_string.is_none());
    }

    #[test]
    fn test_parse_config_with_connection_string() {
        let raw = json!({
            "query_template": "SELECT 1",
            "connection_string": "postgres://user:pass@localhost/db"
        });
        let cfg = SqlTool::parse_config(&raw).unwrap();
        assert_eq!(
            cfg.connection_string,
            Some("postgres://user:pass@localhost/db".to_string())
        );
    }

    #[test]
    fn test_parse_config_missing_optional_fields() {
        let raw = json!({
            "query_template": "SELECT 1"
        });
        let cfg = SqlTool::parse_config(&raw).unwrap();
        assert!(cfg.connection_string.is_none());
        assert_eq!(cfg.timeout_secs, None);
        assert_eq!(cfg.max_rows, None);
    }

    #[test]
    fn test_default_values() {
        let cfg = SqlToolConfig::default();
        assert!(cfg.query_template.is_empty());
        assert!(cfg.connection_string.is_none());
        assert_eq!(cfg.timeout_secs, Some(30));
        assert_eq!(cfg.max_rows, Some(1000));
    }

    #[test]
    fn test_invalid_connection_string() {
        let config = SqlToolConfig {
            query_template: "SELECT 1".to_string(),
            connection_string: Some("not-a-url".to_string()),
            ..Default::default()
        };
        let err = SqlTool::new("x", "y", json!({}), config, None).unwrap_err();
        assert!(matches!(
            err,
            ares_types::AppError::Configuration(msg)
                if msg.contains("Invalid SQL tool connection string")
        ));
    }

    #[tokio::test]
    async fn test_non_object_args_rejected() {
        let tool = make_test_tool(SqlToolConfig::default(), None);
        let err = tool.execute(json!("not-an-object")).await.unwrap_err();
        assert!(matches!(
            err,
            ares_types::AppError::InvalidInput(msg)
                if msg.contains("must be a JSON object")
        ));
    }

    #[tokio::test]
    async fn test_no_pool_available() {
        let config = SqlToolConfig {
            query_template: "SELECT 1".to_string(),
            ..Default::default()
        };
        let tool = make_test_tool(config, None);
        let err = tool.execute(json!({})).await.unwrap_err();
        assert!(matches!(
            err,
            ares_types::AppError::Configuration(msg)
                if msg.contains("No database pool")
        ));
    }

    // -------------------------------------------------------------------------
    // DB-backed integration tests (skipped when TEST_DATABASE_URL is unset)
    // -------------------------------------------------------------------------

    async fn try_test_pool() -> Option<PgPool> {
        let url = std::env::var("TEST_DATABASE_URL").ok()?;
        sqlx::postgres::PgPoolOptions::new()
            .max_connections(1)
            .connect(&url)
            .await
            .ok()
    }

    #[tokio::test]
    async fn test_simple_select() {
        let Some(pool) = try_test_pool().await else {
            return;
        };
        let config = SqlToolConfig {
            query_template: "SELECT $1::int as a, $2::text as b".to_string(),
            ..Default::default()
        };
        let tool = make_test_tool(config, Some(pool));
        // Alphabetic order => "a" = $1, "b" = $2
        let result = tool
            .execute(json!({ "a": 42, "b": "hello" }))
            .await
            .unwrap();
        let arr = result.as_array().unwrap();
        assert_eq!(arr.len(), 1);
        assert_eq!(arr[0]["a"], 42);
        assert_eq!(arr[0]["b"], "hello");
    }

    #[tokio::test]
    async fn test_mising_args_extra_ignored() {
        let Some(pool) = try_test_pool().await else {
            return;
        };
        let config = SqlToolConfig {
            query_template: "SELECT $1::int as n".to_string(),
            ..Default::default()
        };
        let tool = make_test_tool(config, Some(pool));
        // "x" is ignored because there is no $2 in the query.
        let result = tool
            .execute(json!({ "n": 7, "x": "ignored" }))
            .await
            .unwrap();
        let arr = result.as_array().unwrap();
        assert_eq!(arr.len(), 1);
        assert_eq!(arr[0]["n"], 7);
    }

    #[tokio::test]
    async fn test_empty_result_set() {
        let Some(pool) = try_test_pool().await else {
            return;
        };
        let config = SqlToolConfig {
            query_template: "SELECT * FROM (VALUES (1)) t WHERE false".to_string(),
            ..Default::default()
        };
        let tool = make_test_tool(config, Some(pool));
        let result = tool.execute(json!({})).await.unwrap();
        assert_eq!(result, json!([]));
    }

    #[tokio::test]
    async fn test_max_rows_enforced() {
        let Some(pool) = try_test_pool().await else {
            return;
        };
        let config = SqlToolConfig {
            query_template: "SELECT generate_series(1, $1::int) as n".to_string(),
            max_rows: Some(5),
            ..Default::default()
        };
        let tool = make_test_tool(config, Some(pool));
        let err = tool.execute(json!({ "n": 10 })).await.unwrap_err();
        assert!(matches!(
            err,
            ares_types::AppError::InvalidInput(msg)
                if msg.contains("exceeding max_rows")
        ));
    }

    #[tokio::test]
    async fn test_max_rows_within_limit_ok() {
        let Some(pool) = try_test_pool().await else {
            return;
        };
        let config = SqlToolConfig {
            query_template: "SELECT generate_series(1, $1::int) as n".to_string(),
            max_rows: Some(100),
            ..Default::default()
        };
        let tool = make_test_tool(config, Some(pool));
        let result = tool.execute(json!({ "n": 3 })).await.unwrap();
        assert_eq!(result.as_array().unwrap().len(), 3);
    }

    #[tokio::test]
    async fn test_timeout_enforced() {
        let Some(pool) = try_test_pool().await else {
            return;
        };
        let config = SqlToolConfig {
            query_template: "SELECT pg_sleep($1::int) as result".to_string(),
            timeout_secs: Some(1),
            ..Default::default()
        };
        let tool = make_test_tool(config, Some(pool));
        let err = tool.execute(json!({ "seconds": 3 })).await.unwrap_err();
        assert!(matches!(
            err,
            ares_types::AppError::External(msg)
                if msg.contains("timed out")
        ));
    }

    #[tokio::test]
    async fn test_null_bool_number_params() {
        let Some(pool) = try_test_pool().await else {
            return;
        };
        // Args bind alphabetically: flag→$1, name→$2, num→$3.
        let config = SqlToolConfig {
            query_template: "SELECT $1::boolean as flag, $2::text as name, $3::int as num"
                .to_string(),
            ..Default::default()
        };
        let tool = make_test_tool(config, Some(pool));
        let result = tool
            .execute(json!({ "flag": true, "name": null, "num": 99 }))
            .await
            .unwrap();
        let arr = result.as_array().unwrap();
        assert_eq!(arr[0]["flag"], true);
        assert_eq!(arr[0]["num"], 99);
        assert_eq!(arr[0]["name"], Value::Null);
    }

    #[tokio::test]
    async fn test_json_param_and_column() {
        let Some(pool) = try_test_pool().await else {
            return;
        };
        let config = SqlToolConfig {
            query_template: "SELECT $1::jsonb as payload".to_string(),
            ..Default::default()
        };
        let tool = make_test_tool(config, Some(pool));
        let result = tool
            .execute(json!({ "payload": { "nested": [1, 2, 3] } }))
            .await
            .unwrap();
        let arr = result.as_array().unwrap();
        assert_eq!(arr[0]["payload"]["nested"][1], 2);
    }

    #[tokio::test]
    async fn test_multiple_rows() {
        let Some(pool) = try_test_pool().await else {
            return;
        };
        let config = SqlToolConfig {
            query_template: "SELECT $1::int + generate_series(0, 2) as val".to_string(),
            ..Default::default()
        };
        let tool = make_test_tool(config, Some(pool));
        let result = tool.execute(json!({ "start": 10 })).await.unwrap();
        let arr = result.as_array().unwrap();
        assert_eq!(arr.len(), 3);
        assert_eq!(arr[0]["val"], 10);
        assert_eq!(arr[1]["val"], 11);
        assert_eq!(arr[2]["val"], 12);
    }

    #[tokio::test]
    async fn test_sql_error_propagated() {
        let Some(pool) = try_test_pool().await else {
            return;
        };
        let config = SqlToolConfig {
            query_template: "SELECT no_such_column FROM no_such_table".to_string(),
            ..Default::default()
        };
        let tool = make_test_tool(config, Some(pool));
        let err = tool.execute(json!({})).await.unwrap_err();
        assert!(matches!(
            err,
            ares_types::AppError::Database(msg)
                if msg.contains("SQL execution failed")
        ));
    }
}