agent-first-psql 0.4.0

Persistent PostgreSQL client for AI agents — SQL-native JSONL in, JSONL out
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
use crate::types::{QueryOptions, SessionConfig};
use agent_first_data::{cli_parse_log_filters, cli_parse_output, OutputFormat};
use clap::{Parser, ValueEnum};
use serde_json::{json, Value};
use std::collections::BTreeMap;

pub enum Mode {
    Cli(CliRequest),
    Pipe(PipeInit),
}

pub struct PipeInit {
    pub output: OutputFormat,
    pub session: SessionConfig,
    pub log: Vec<String>,
    pub startup_argv: Vec<String>,
    pub startup_args: Value,
    pub startup_env: Value,
    pub startup_requested: bool,
}

pub struct CliRequest {
    pub sql: String,
    pub params: Vec<Value>,
    pub options: QueryOptions,
    pub session: SessionConfig,
    pub output: OutputFormat,
    pub log: Vec<String>,
    pub startup_argv: Vec<String>,
    pub startup_args: Value,
    pub startup_env: Value,
    pub startup_requested: bool,
    pub dry_run: bool,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, ValueEnum)]
enum RuntimeMode {
    Cli,
    Pipe,
    #[value(name = "psql")]
    Psql,
}

#[derive(Parser)]
#[command(name = "afpsql", version, about = "Agent-First PostgreSQL client")]
struct AfdCli {
    #[arg(long)]
    sql: Option<String>,
    #[arg(long = "sql-file")]
    sql_file: Option<String>,
    #[arg(long = "param")]
    param: Vec<String>,
    #[arg(long = "stream-rows")]
    stream_rows: bool,
    #[arg(long = "batch-rows")]
    batch_rows: Option<usize>,
    #[arg(long = "batch-bytes")]
    batch_bytes: Option<usize>,
    #[arg(long = "statement-timeout-ms")]
    statement_timeout_ms: Option<u64>,
    #[arg(long = "lock-timeout-ms")]
    lock_timeout_ms: Option<u64>,
    #[arg(long = "inline-max-rows")]
    inline_max_rows: Option<usize>,
    #[arg(long = "inline-max-bytes")]
    inline_max_bytes: Option<usize>,
    #[arg(long = "read-only")]
    read_only: bool,
    /// Preview the query without executing it
    #[arg(long)]
    dry_run: bool,

    #[arg(long = "dsn-secret")]
    dsn_secret: Option<String>,
    #[arg(long = "conninfo-secret")]
    conninfo_secret: Option<String>,
    #[arg(long)]
    host: Option<String>,
    #[arg(long)]
    port: Option<u16>,
    #[arg(long)]
    user: Option<String>,
    #[arg(long)]
    dbname: Option<String>,
    #[arg(long = "password-secret")]
    password_secret: Option<String>,

    #[arg(long, default_value = "json")]
    output: String,
    #[arg(long = "log", value_delimiter = ',')]
    log: Vec<String>,
    #[arg(long, value_enum, default_value_t = RuntimeMode::Cli)]
    mode: RuntimeMode,
}

pub fn parse_args() -> Result<Mode, String> {
    let raw: Vec<String> = std::env::args().collect();
    if is_psql_mode_requested(&raw) {
        return parse_psql_mode(&raw);
    }
    let startup_requested = startup_requested_from_raw(&raw);

    let cli = match AfdCli::try_parse_from(&raw) {
        Ok(c) => c,
        Err(e) => {
            use clap::error::ErrorKind;
            if matches!(e.kind(), ErrorKind::DisplayHelp | ErrorKind::DisplayVersion) {
                println!("{e}");
                std::process::exit(0);
            }
            return Err(e.to_string());
        }
    };
    let output = parse_output(&cli.output)?;
    let log = parse_log_categories(&cli.log);
    let session = SessionConfig {
        dsn_secret: cli.dsn_secret,
        conninfo_secret: cli.conninfo_secret,
        host: cli.host,
        port: cli.port,
        user: cli.user,
        dbname: cli.dbname,
        password_secret: cli.password_secret,
    };
    let mode_name = match cli.mode {
        RuntimeMode::Cli => "cli",
        RuntimeMode::Pipe => "pipe",
        RuntimeMode::Psql => "psql",
    };
    let startup_args = json!({
        "mode": mode_name,
        "sql": &cli.sql,
        "sql_file": &cli.sql_file,
        "param": &cli.param,
        "stream_rows": cli.stream_rows,
        "batch_rows": cli.batch_rows,
        "batch_bytes": cli.batch_bytes,
        "statement_timeout_ms": cli.statement_timeout_ms,
        "lock_timeout_ms": cli.lock_timeout_ms,
        "inline_max_rows": cli.inline_max_rows,
        "inline_max_bytes": cli.inline_max_bytes,
        "read_only": cli.read_only,
        "dsn_secret": &session.dsn_secret,
        "conninfo_secret": &session.conninfo_secret,
        "host": &session.host,
        "port": session.port,
        "user": &session.user,
        "dbname": &session.dbname,
        "password_secret": &session.password_secret,
        "output": output_name(output),
        "log": &log,
    });
    let startup_env = startup_env_snapshot();

    match cli.mode {
        RuntimeMode::Pipe => {
            return Ok(Mode::Pipe(PipeInit {
                output,
                session,
                log: log.clone(),
                startup_argv: raw,
                startup_args,
                startup_env,
                startup_requested,
            }));
        }
        RuntimeMode::Cli | RuntimeMode::Psql => {}
    }

    let sql = load_sql(cli.sql, cli.sql_file)?;
    let params = parse_params(&cli.param)?;

    let options = QueryOptions {
        stream_rows: cli.stream_rows,
        batch_rows: cli.batch_rows,
        batch_bytes: cli.batch_bytes,
        statement_timeout_ms: cli.statement_timeout_ms,
        lock_timeout_ms: cli.lock_timeout_ms,
        read_only: if cli.read_only { Some(true) } else { None },
        inline_max_rows: cli.inline_max_rows,
        inline_max_bytes: cli.inline_max_bytes,
    };

    Ok(Mode::Cli(CliRequest {
        sql,
        params,
        options,
        session,
        output,
        log,
        startup_argv: raw,
        startup_args,
        startup_env,
        startup_requested,
        dry_run: cli.dry_run,
    }))
}

fn parse_psql_mode(raw: &[String]) -> Result<Mode, String> {
    let startup_requested = startup_requested_from_raw(raw);
    let mut sql: Option<String> = None;
    let mut sql_file: Option<String> = None;
    let mut host: Option<String> = None;
    let mut port: Option<u16> = None;
    let mut user: Option<String> = None;
    let mut dbname: Option<String> = None;
    let mut dsn_secret: Option<String> = None;
    let mut conninfo_secret: Option<String> = None;
    let mut params_kv: Vec<String> = vec![];
    let mut output = OutputFormat::Json;
    let mut log_entries: Vec<String> = vec![];

    let mut i = 1usize;
    while i < raw.len() {
        match raw[i].as_str() {
            "--mode" => {
                i += 1;
                let v = raw.get(i).ok_or("--mode requires value")?;
                if v != "psql" {
                    return Err(format!("unsupported psql-mode argument: --mode {v}; only --mode psql is allowed with psql translation"));
                }
                i += 1;
            }
            other if other.starts_with("--mode=") => {
                let v = other.trim_start_matches("--mode=");
                if v != "psql" {
                    return Err(format!("unsupported psql-mode argument: {other}; only --mode=psql is allowed with psql translation"));
                }
                i += 1;
            }
            "-c" => {
                i += 1;
                let v = raw.get(i).ok_or("-c requires SQL")?;
                sql = Some(v.clone());
                i += 1;
            }
            "-f" => {
                i += 1;
                let v = raw.get(i).ok_or("-f requires file path")?;
                sql_file = Some(v.clone());
                i += 1;
            }
            "-h" => {
                i += 1;
                host = Some(raw.get(i).ok_or("-h requires value")?.clone());
                i += 1;
            }
            "-p" => {
                i += 1;
                port = Some(
                    raw.get(i)
                        .ok_or("-p requires value")?
                        .parse()
                        .map_err(|_| "invalid -p port")?,
                );
                i += 1;
            }
            "-U" => {
                i += 1;
                user = Some(raw.get(i).ok_or("-U requires value")?.clone());
                i += 1;
            }
            "-d" => {
                i += 1;
                dbname = Some(raw.get(i).ok_or("-d requires value")?.clone());
                i += 1;
            }
            "--dsn-secret" => {
                i += 1;
                dsn_secret = Some(raw.get(i).ok_or("--dsn-secret requires value")?.clone());
                i += 1;
            }
            "--conninfo-secret" => {
                i += 1;
                conninfo_secret = Some(
                    raw.get(i)
                        .ok_or("--conninfo-secret requires value")?
                        .clone(),
                );
                i += 1;
            }
            "-v" => {
                i += 1;
                params_kv.push(raw.get(i).ok_or("-v requires N=value")?.clone());
                i += 1;
            }
            "--output" => {
                i += 1;
                output = parse_output(raw.get(i).ok_or("--output requires value")?)?;
                i += 1;
            }
            "--log" => {
                i += 1;
                let values = raw.get(i).ok_or("--log requires value")?;
                for part in values.split(',') {
                    let trimmed = part.trim();
                    if !trimmed.is_empty() {
                        log_entries.push(trimmed.to_string());
                    }
                }
                i += 1;
            }
            other if other.starts_with("postgresql://") || other.starts_with("postgres://") => {
                dsn_secret = Some(other.to_string());
                i += 1;
            }
            unsupported => {
                return Err(format!(
                    "unsupported psql-mode argument: {unsupported}; only --mode psql, -c/-f/-h/-p/-U/-d/-v/--dsn-secret/--conninfo-secret/--output/--log are supported"
                ));
            }
        }
    }

    let session = SessionConfig {
        dsn_secret,
        conninfo_secret,
        host,
        port,
        user,
        dbname,
        password_secret: None,
    };

    let startup_sql = sql.clone();
    let startup_sql_file = sql_file.clone();
    let sql = load_sql(sql, sql_file)?;
    let params = parse_params(&params_kv)?;
    let startup_args = psql_startup_args(
        "psql",
        startup_sql.or_else(|| Some(sql.clone())),
        startup_sql_file,
        &params_kv,
        &session,
        output,
        &log_entries,
    );
    Ok(Mode::Cli(CliRequest {
        sql,
        params,
        options: QueryOptions::default(),
        session,
        output,
        log: parse_log_categories(&log_entries),
        startup_argv: raw.to_vec(),
        startup_args,
        startup_env: startup_env_snapshot(),
        startup_requested,
        dry_run: false,
    }))
}

fn is_psql_mode_requested(raw: &[String]) -> bool {
    let mut i = 1usize;
    while i < raw.len() {
        let arg = raw[i].as_str();
        if arg == "--mode" {
            if let Some(v) = raw.get(i + 1) {
                return v == "psql";
            }
            return false;
        }
        if arg == "--mode=psql" {
            return true;
        }
        i += 1;
    }
    false
}

fn load_sql(sql: Option<String>, sql_file: Option<String>) -> Result<String, String> {
    match (sql, sql_file) {
        (Some(s), None) => Ok(s),
        (None, Some(path)) => {
            std::fs::read_to_string(path).map_err(|e| format!("read --sql-file failed: {e}"))
        }
        (Some(_), Some(_)) => Err("--sql and --sql-file are mutually exclusive".to_string()),
        (None, None) => Err("one of --sql or --sql-file is required".to_string()),
    }
}

fn parse_output(v: &str) -> Result<OutputFormat, String> {
    cli_parse_output(v)
}

fn parse_log_categories(entries: &[String]) -> Vec<String> {
    cli_parse_log_filters(entries)
}

fn startup_requested_from_raw(raw: &[String]) -> bool {
    let mut i = 1usize;
    while i < raw.len() {
        if raw[i] == "--log" {
            if let Some(values) = raw.get(i + 1) {
                for part in values.split(',') {
                    let v = part.trim().to_ascii_lowercase();
                    if matches!(v.as_str(), "startup" | "all" | "*") {
                        return true;
                    }
                }
            }
            i += 2;
            continue;
        }
        if let Some(values) = raw[i].strip_prefix("--log=") {
            for part in values.split(',') {
                let v = part.trim().to_ascii_lowercase();
                if matches!(v.as_str(), "startup" | "all" | "*") {
                    return true;
                }
            }
        }
        i += 1;
    }
    false
}

fn output_name(output: OutputFormat) -> &'static str {
    match output {
        OutputFormat::Json => "json",
        OutputFormat::Yaml => "yaml",
        OutputFormat::Plain => "plain",
    }
}

fn startup_env_snapshot() -> Value {
    json!({
        "AFPSQL_DSN_SECRET": std::env::var("AFPSQL_DSN_SECRET").ok(),
        "AFPSQL_CONNINFO_SECRET": std::env::var("AFPSQL_CONNINFO_SECRET").ok(),
        "AFPSQL_HOST": std::env::var("AFPSQL_HOST").ok(),
        "AFPSQL_PORT": std::env::var("AFPSQL_PORT").ok(),
        "AFPSQL_USER": std::env::var("AFPSQL_USER").ok(),
        "AFPSQL_DBNAME": std::env::var("AFPSQL_DBNAME").ok(),
        "AFPSQL_PASSWORD_SECRET": std::env::var("AFPSQL_PASSWORD_SECRET").ok(),
        "PGHOST": std::env::var("PGHOST").ok(),
        "PGPORT": std::env::var("PGPORT").ok(),
        "PGUSER": std::env::var("PGUSER").ok(),
        "PGDATABASE": std::env::var("PGDATABASE").ok(),
    })
}

fn psql_startup_args(
    mode: &str,
    sql: Option<String>,
    sql_file: Option<String>,
    params_kv: &[String],
    session: &SessionConfig,
    output: OutputFormat,
    log_entries: &[String],
) -> Value {
    json!({
        "mode": mode,
        "sql": sql,
        "sql_file": sql_file,
        "param": params_kv,
        "dsn_secret": session.dsn_secret,
        "conninfo_secret": session.conninfo_secret,
        "host": session.host,
        "port": session.port,
        "user": session.user,
        "dbname": session.dbname,
        "password_secret": session.password_secret,
        "output": output_name(output),
        "log": parse_log_categories(log_entries),
    })
}

pub fn parse_params(entries: &[String]) -> Result<Vec<Value>, String> {
    let mut by_index: BTreeMap<usize, Value> = BTreeMap::new();
    for entry in entries {
        let (idx, raw) = split_index_value(entry)?;
        if idx == 0 {
            return Err("param index must start at 1".to_string());
        }
        by_index.insert(idx, parse_param_value(raw));
    }
    if by_index.is_empty() {
        return Ok(vec![]);
    }
    let max = by_index.keys().max().copied().unwrap_or(0);
    let mut out = Vec::with_capacity(max);
    for i in 1..=max {
        let v = by_index
            .remove(&i)
            .ok_or_else(|| format!("missing parameter index {i}"))?;
        out.push(v);
    }
    Ok(out)
}

fn split_index_value(entry: &str) -> Result<(usize, &str), String> {
    let mut parts = entry.splitn(2, '=');
    let left = parts.next().unwrap_or_default();
    let right = parts
        .next()
        .ok_or_else(|| format!("invalid param '{entry}', expected N=value"))?;
    let idx = left
        .parse::<usize>()
        .map_err(|_| format!("invalid param index in '{entry}'"))?;
    Ok((idx, right))
}

fn parse_param_value(v: &str) -> Value {
    if v == "null" {
        return Value::Null;
    }
    if v == "true" {
        return Value::Bool(true);
    }
    if v == "false" {
        return Value::Bool(false);
    }
    if let Ok(i) = v.parse::<i64>() {
        return Value::Number(i.into());
    }
    if let Ok(f) = v.parse::<f64>() {
        if let Some(n) = serde_json::Number::from_f64(f) {
            return Value::Number(n);
        }
    }
    Value::String(v.to_string())
}

#[cfg(test)]
#[path = "../tests/support/unit_cli.rs"]
mod tests;