agent-first-http 0.4.2

Persistent HTTP client for AI agents — one request, one JSON line
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
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::HashMap;

// ---------------------------------------------------------------------------
// Input types (stdin)
// ---------------------------------------------------------------------------

#[derive(Deserialize)]
#[serde(tag = "code")]
pub enum Input {
    #[serde(rename = "request")]
    Request {
        id: String,
        #[serde(default)]
        tag: Option<String>,
        method: String,
        url: String,
        #[serde(default)]
        headers: HashMap<String, Value>,
        body: Option<Value>,
        body_base64: Option<String>,
        body_file: Option<String>,
        body_multipart: Option<Vec<MultipartPart>>,
        body_urlencoded: Option<Vec<UrlencodedPart>>,
        #[serde(default)]
        options: RequestOptions,
    },
    #[serde(rename = "config")]
    Config(ConfigPatch),
    #[serde(rename = "ping")]
    Ping,
    #[serde(rename = "send")]
    Send {
        id: String,
        data: Option<Value>,
        data_base64: Option<String>,
    },
    #[serde(rename = "cancel")]
    Cancel { id: String },
    #[serde(rename = "close")]
    Close,
}

/// All fields from a `{"code":"config",...}` command.
/// Passed directly to RuntimeConfig::apply_update.
#[derive(Deserialize, Default)]
pub struct ConfigPatch {
    pub response_save_dir: Option<String>,
    pub response_save_above_bytes: Option<u64>,
    pub request_concurrency_limit: Option<u64>,
    pub timeout_connect_s: Option<u64>,
    pub pool_idle_timeout_s: Option<u64>,
    pub retry_base_delay_ms: Option<u64>,
    pub proxy: Option<String>,
    pub tls: Option<TlsConfigPartial>,
    pub log: Option<Vec<String>>,
    pub defaults: Option<RequestDefaultsPartial>,
    pub host_defaults: Option<HashMap<String, HostDefaultsPartial>>,
}

pub enum WsCommand {
    Send {
        data: Option<Value>,
        data_base64: Option<String>,
    },
    Close,
}

#[derive(Deserialize, Default)]
pub struct RequestOptions {
    pub timeout_idle_s: Option<u64>,
    pub retry: Option<u32>,
    pub response_redirect: Option<u32>,
    pub response_parse_json: Option<bool>,
    pub response_decompress: Option<bool>,
    pub response_save_resume: Option<bool>,
    #[serde(default)]
    pub chunked: bool,
    #[serde(default = "default_chunked_delimiter")]
    pub chunked_delimiter: Value, // String = delimiter, Null = raw, absent = "\n"
    pub response_save_file: Option<String>,
    pub progress_bytes: Option<u64>,
    pub progress_ms: Option<u64>,
    pub retry_on_status: Option<Vec<u16>>,
    pub response_max_bytes: Option<u64>,
    pub upgrade: Option<String>,
    /// Per-request TLS overrides — merged on top of global TLS config.
    /// Builds a one-off HTTP client for this request (no shared connection pool).
    pub tls: Option<TlsConfigPartial>,
}

#[derive(Deserialize)]
pub struct MultipartPart {
    pub name: String,
    pub value: Option<String>,
    pub value_base64: Option<String>,
    pub file: Option<String>,
    pub filename: Option<String>,
    pub content_type: Option<String>,
}

#[derive(Deserialize)]
pub struct UrlencodedPart {
    pub name: String,
    pub value: String,
}

#[derive(Deserialize, Default)]
pub struct RequestDefaultsPartial {
    pub headers_for_any_hosts: Option<HashMap<String, Value>>,
    pub timeout_idle_s: Option<u64>,
    pub retry: Option<u32>,
    pub response_redirect: Option<u32>,
    pub response_parse_json: Option<bool>,
    pub response_decompress: Option<bool>,
    pub response_save_resume: Option<bool>,
    pub retry_on_status: Option<Vec<u16>>,
}

#[derive(Deserialize, Default)]
pub struct HostDefaultsPartial {
    pub headers: Option<HashMap<String, Value>>,
}

/// Partial TLS config used for both global config updates and per-request overrides.
/// Inline fields (`cacert`, `cert`, `key`) take precedence over file-path fields
/// (`cacert_file`, `cert_file`, `key_file`). Setting one clears the other.
#[derive(Deserialize, Default, Clone)]
pub struct TlsConfigPartial {
    pub insecure: Option<bool>,
    /// Inline CA certificate as PEM text. Takes precedence over `cacert_file`.
    pub cacert_pem: Option<String>,
    /// Path to CA certificate file (PEM) — like curl --cacert.
    pub cacert_file: Option<String>,
    /// Inline client certificate as PEM text. Takes precedence over `cert_file`.
    pub cert_pem: Option<String>,
    /// Path to client certificate file (PEM) — like curl --cert.
    pub cert_file: Option<String>,
    /// Inline client private key as PEM text (unencrypted). Takes precedence over `key_file`.
    /// Named `_secret` — redacted in all config echo output.
    pub key_pem_secret: Option<String>,
    /// Path to client private key file (PEM, unencrypted) — like curl --key.
    pub key_file: Option<String>,
}

// ---------------------------------------------------------------------------
// Output types (stdout)
// ---------------------------------------------------------------------------

#[derive(Serialize)]
#[serde(tag = "code")]
pub enum Output {
    #[serde(rename = "response")]
    Response {
        id: String,
        #[serde(skip_serializing_if = "Option::is_none")]
        tag: Option<String>,
        status: u16,
        headers: HashMap<String, Value>,
        #[serde(skip_serializing_if = "Option::is_none")]
        body: Option<Value>,
        #[serde(skip_serializing_if = "Option::is_none")]
        body_base64: Option<String>,
        #[serde(skip_serializing_if = "Option::is_none")]
        body_file: Option<String>,
        /// true when Content-Type was application/json but the body failed JSON
        /// parsing — body contains the raw text string instead.
        #[serde(skip_serializing_if = "std::ops::Not::not")]
        body_parse_failed: bool,
        trace: Trace,
    },

    #[serde(rename = "error")]
    Error {
        #[serde(skip_serializing_if = "Option::is_none")]
        id: Option<String>,
        #[serde(skip_serializing_if = "Option::is_none")]
        tag: Option<String>,
        error: String,
        error_code: String,
        #[serde(skip_serializing_if = "Option::is_none")]
        hint: Option<String>,
        retryable: bool,
        trace: Trace,
    },

    #[serde(rename = "dry_run")]
    DryRun {
        method: String,
        url: String,
        headers: HashMap<String, Value>,
        #[serde(skip_serializing_if = "Option::is_none")]
        body: Option<Value>,
        trace: Trace,
    },

    #[serde(rename = "chunk_start")]
    ChunkStart {
        id: String,
        #[serde(skip_serializing_if = "Option::is_none")]
        tag: Option<String>,
        status: u16,
        headers: HashMap<String, Value>,
        #[serde(skip_serializing_if = "Option::is_none")]
        content_length_bytes: Option<u64>,
    },

    #[serde(rename = "chunk_data")]
    ChunkData {
        id: String,
        #[serde(skip_serializing_if = "Option::is_none")]
        data: Option<String>,
        #[serde(skip_serializing_if = "Option::is_none")]
        data_base64: Option<String>,
    },

    #[serde(rename = "chunk_end")]
    ChunkEnd {
        id: String,
        #[serde(skip_serializing_if = "Option::is_none")]
        tag: Option<String>,
        #[serde(skip_serializing_if = "Option::is_none")]
        body_file: Option<String>,
        trace: Trace,
    },

    #[serde(rename = "config")]
    Config(RuntimeConfig),

    #[serde(rename = "pong")]
    Pong { trace: PongTrace },

    #[serde(rename = "close")]
    Close { message: String, trace: CloseTrace },

    #[serde(rename = "log")]
    Log {
        event: String,
        #[serde(flatten)]
        fields: HashMap<String, Value>,
    },
}

// ---------------------------------------------------------------------------
// Shared structs
// ---------------------------------------------------------------------------

#[derive(Serialize, Deserialize, Clone)]
pub struct RuntimeConfig {
    pub response_save_dir: String,
    pub response_save_above_bytes: u64,
    pub request_concurrency_limit: u64,
    pub timeout_connect_s: u64,
    pub pool_idle_timeout_s: u64,
    pub retry_base_delay_ms: u64,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub proxy: Option<String>,
    pub tls: TlsConfig,
    pub log: Vec<String>,
    pub defaults: RequestDefaults,
    #[serde(default, skip_serializing_if = "HashMap::is_empty")]
    pub host_defaults: HashMap<String, HostDefaults>,
}

#[derive(Serialize, Deserialize, Clone)]
pub struct RequestDefaults {
    #[serde(default)]
    pub headers_for_any_hosts: HashMap<String, Value>,
    pub timeout_idle_s: u64,
    pub retry: u32,
    pub response_redirect: u32,
    pub response_parse_json: bool,
    pub response_decompress: bool,
    pub response_save_resume: bool,
    #[serde(default)]
    pub retry_on_status: Vec<u16>,
}

#[derive(Serialize, Deserialize, Clone, Default)]
pub struct HostDefaults {
    #[serde(default)]
    pub headers: HashMap<String, Value>,
}

/// Stored TLS configuration (full, non-partial). Inline PEM fields (`*_pem`) take
/// precedence over file-path fields (`*_file`).
#[derive(Serialize, Deserialize, Clone)]
pub struct TlsConfig {
    #[serde(default)]
    pub insecure: bool,
    /// Inline CA certificate as PEM text
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cacert_pem: Option<String>,
    /// Path to CA certificate file (PEM)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cacert_file: Option<String>,
    /// Inline client certificate as PEM text
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cert_pem: Option<String>,
    /// Path to client certificate file (PEM)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cert_file: Option<String>,
    /// Inline client private key as PEM text (unencrypted). Redacted in config echo.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub key_pem_secret: Option<String>,
    /// Path to client private key file (PEM, unencrypted)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub key_file: Option<String>,
}

#[derive(Serialize, Clone)]
pub struct Trace {
    pub duration_ms: u64,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub http_version: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub remote_addr: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sent_bytes: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub received_bytes: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub redirects: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub chunks: Option<u32>,
}

#[derive(Serialize)]
pub struct PongTrace {
    pub uptime_s: u64,
    pub requests_total: u64,
    pub connections_active: u64,
}

#[derive(Serialize)]
pub struct CloseTrace {
    pub uptime_s: u64,
    pub requests_total: u64,
}

// ---------------------------------------------------------------------------
// Resolved options (config defaults merged with per-request options)
// ---------------------------------------------------------------------------

pub struct ResolvedOptions {
    pub timeout_idle_s: u64,
    pub retry: u32,
    pub response_redirect: u32,
    pub response_parse_json: bool,
    pub response_decompress: bool,
    pub response_save_resume: bool,
    pub chunked: bool,
    pub chunked_delimiter: Option<String>, // None = raw
    pub response_save_file: Option<String>,
    pub progress_bytes: u64,
    pub progress_ms: u64,
    pub response_save_above_bytes: u64,
    pub retry_base_delay_ms: u64,
    pub retry_on_status: Vec<u16>,
    pub response_max_bytes: Option<u64>,
}

fn default_chunked_delimiter() -> Value {
    Value::String("\n".to_string())
}

impl Trace {
    pub fn error_only(duration_ms: u64) -> Self {
        Trace {
            duration_ms,
            http_version: None,
            remote_addr: None,
            sent_bytes: None,
            received_bytes: None,
            redirects: None,
            chunks: None,
        }
    }
}

// ---------------------------------------------------------------------------
// ErrorInfo — structured error classification
// ---------------------------------------------------------------------------

pub struct ErrorInfo {
    pub error_code: &'static str,
    pub error: String,
    pub hint: Option<String>,
    pub retryable: bool,
}

impl ErrorInfo {
    pub fn invalid_request(detail: impl std::fmt::Display) -> Self {
        ErrorInfo {
            error_code: "invalid_request",
            error: format!("{detail}"),
            hint: None,
            retryable: false,
        }
    }

    pub fn cancelled() -> Self {
        ErrorInfo {
            error_code: "cancelled",
            error: "cancelled".to_string(),
            hint: None,
            retryable: false,
        }
    }

    pub fn too_many_redirects(max: u32) -> Self {
        ErrorInfo {
            error_code: "too_many_redirects",
            error: format!("exceeded {max}"),
            hint: Some("increase --response-redirect or check for redirect loops".into()),
            retryable: false,
        }
    }

    pub fn request_timeout(detail: impl std::fmt::Display) -> Self {
        ErrorInfo {
            error_code: "request_timeout",
            error: format!("{detail}"),
            hint: Some("increase --timeout-idle-s".into()),
            retryable: false,
        }
    }

    pub fn invalid_response(detail: impl std::fmt::Display) -> Self {
        ErrorInfo {
            error_code: "invalid_response",
            error: format!("{detail}"),
            hint: None,
            retryable: false,
        }
    }

    pub fn chunk_disconnected(detail: impl std::fmt::Display) -> Self {
        ErrorInfo {
            error_code: "chunk_disconnected",
            error: format!("{detail}"),
            hint: None,
            retryable: false,
        }
    }

    pub fn response_too_large(limit_bytes: u64) -> Self {
        ErrorInfo {
            error_code: "response_too_large",
            error: format!("exceeded {limit_bytes} bytes"),
            hint: Some("increase --response-max-bytes".into()),
            retryable: false,
        }
    }

    pub fn overloaded(detail: impl std::fmt::Display) -> Self {
        ErrorInfo {
            error_code: "overloaded",
            error: format!("{detail}"),
            hint: None,
            retryable: true,
        }
    }

    pub fn from_reqwest(e: &reqwest::Error) -> Self {
        if e.is_timeout() {
            if e.is_connect() {
                return ErrorInfo {
                    error_code: "connect_timeout",
                    error: e.to_string(),
                    hint: Some("increase --timeout-connect-s or check host reachability".into()),
                    retryable: true,
                };
            }
            return ErrorInfo {
                error_code: "request_timeout",
                error: e.to_string(),
                hint: Some("increase --timeout-idle-s".into()),
                retryable: false,
            };
        }
        if e.is_connect() {
            let msg = e.to_string().to_lowercase();
            if msg.contains("dns") || msg.contains("resolve") || msg.contains("name") {
                return ErrorInfo {
                    error_code: "dns_failed",
                    error: e.to_string(),
                    hint: Some("check the hostname spelling".into()),
                    retryable: true,
                };
            }
            return ErrorInfo {
                error_code: "connect_refused",
                error: e.to_string(),
                hint: None,
                retryable: true,
            };
        }
        let msg = e.to_string().to_lowercase();
        if msg.contains("tls") || msg.contains("ssl") || msg.contains("certificate") {
            return ErrorInfo {
                error_code: "tls_error",
                error: e.to_string(),
                hint: None,
                retryable: false,
            };
        }
        if msg.contains("dns") || msg.contains("resolve") {
            return ErrorInfo {
                error_code: "dns_failed",
                error: e.to_string(),
                hint: Some("check the hostname spelling".into()),
                retryable: true,
            };
        }
        ErrorInfo {
            error_code: "connect_refused",
            error: e.to_string(),
            hint: None,
            retryable: true,
        }
    }
}

/// Helper to build Output::Error from ErrorInfo
pub fn make_error(
    id: Option<String>,
    tag: Option<String>,
    info: ErrorInfo,
    trace: Trace,
) -> Output {
    Output::Error {
        id,
        tag,
        error: info.error,
        error_code: info.error_code.to_string(),
        hint: info.hint,
        retryable: info.retryable,
        trace,
    }
}

/// Helper to build Output::Log
pub fn make_log(event: &str, fields: Vec<(&str, Value)>) -> Output {
    Output::Log {
        event: event.to_string(),
        fields: fields
            .into_iter()
            .map(|(k, v)| (k.to_string(), v))
            .collect(),
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
mod tests {
    use super::*;

    #[test]
    fn request_options_default_delimiter_is_newline() {
        let opts: RequestOptions = serde_json::from_value(serde_json::json!({})).expect("opts");
        assert_eq!(opts.chunked_delimiter, Value::String("\n".to_string()));
        assert!(!opts.chunked);
    }

    #[test]
    fn trace_error_only_sets_optional_fields_none() {
        let t = Trace::error_only(12);
        assert_eq!(t.duration_ms, 12);
        assert!(t.http_version.is_none());
        assert!(t.remote_addr.is_none());
        assert!(t.sent_bytes.is_none());
        assert!(t.received_bytes.is_none());
        assert!(t.redirects.is_none());
        assert!(t.chunks.is_none());
    }

    #[test]
    fn error_info_builders_and_output_helpers() {
        let version = env!("CARGO_PKG_VERSION");
        let e = ErrorInfo::invalid_request("bad");
        assert_eq!(e.error_code, "invalid_request");
        assert!(!e.retryable);
        let e = ErrorInfo::cancelled();
        assert_eq!(e.error_code, "cancelled");
        let e = ErrorInfo::too_many_redirects(5);
        assert_eq!(e.error, "exceeded 5");
        let e = ErrorInfo::request_timeout("timeout");
        assert_eq!(e.error_code, "request_timeout");
        let e = ErrorInfo::invalid_response("x");
        assert_eq!(e.error_code, "invalid_response");
        let e = ErrorInfo::chunk_disconnected("x");
        assert_eq!(e.error_code, "chunk_disconnected");
        let e = ErrorInfo::response_too_large(100);
        assert_eq!(e.error, "exceeded 100 bytes");
        let e = ErrorInfo::overloaded("busy");
        assert_eq!(e.error_code, "overloaded");
        assert!(e.retryable);

        let out = make_error(
            Some("id1".to_string()),
            Some("tag1".to_string()),
            ErrorInfo::invalid_request("bad"),
            Trace::error_only(1),
        );
        match out {
            Output::Error {
                id,
                tag,
                error_code,
                ..
            } => {
                assert_eq!(id.as_deref(), Some("id1"));
                assert_eq!(tag.as_deref(), Some("tag1"));
                assert_eq!(error_code, "invalid_request");
            }
            _ => panic!("expected Output::Error"),
        }

        let log = make_log(
            "startup",
            vec![("version", Value::String(version.to_string()))],
        );
        match log {
            Output::Log { event, fields } => {
                assert_eq!(event, "startup");
                assert_eq!(fields.get("version"), Some(&Value::String(version.into())));
            }
            _ => panic!("expected Output::Log"),
        }
    }

    #[tokio::test]
    async fn from_reqwest_classifies_connect_and_dns_errors() {
        let client = reqwest::Client::new();

        let connect_err = client
            .get("http://127.0.0.1:1")
            .send()
            .await
            .expect_err("connect should fail");
        let info = ErrorInfo::from_reqwest(&connect_err);
        assert_eq!(info.error_code, "connect_refused");
        assert!(info.retryable);

        let dns_err = client
            .get("http://definitely-not-a-real-host.invalid")
            .send()
            .await
            .expect_err("dns should fail");
        let info = ErrorInfo::from_reqwest(&dns_err);
        assert!(matches!(info.error_code, "dns_failed" | "connect_refused"));
        assert!(info.retryable);
    }
}