everruns-sdk 0.1.6

Rust SDK for Everruns API
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
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
//! Main client for Everruns API

use crate::auth::ApiKey;
use crate::error::{Error, Result};
use crate::models::*;
use reqwest::header::{AUTHORIZATION, CONTENT_TYPE, HeaderMap, HeaderValue};
use url::Url;

const DEFAULT_BASE_URL: &str = "https://custom.example.com/api";

/// Main client for interacting with the Everruns API
#[derive(Clone)]
pub struct Everruns {
    http: reqwest::Client,
    base_url: Url,
    api_key: ApiKey,
}

impl Everruns {
    /// Create a new client with explicit API key
    pub fn new(api_key: impl Into<String>) -> Result<Self> {
        Self::with_base_url(api_key, DEFAULT_BASE_URL)
    }

    /// Create a new client using environment variables.
    ///
    /// Reads `EVERRUNS_API_KEY` (required) and `EVERRUNS_API_URL` (optional).
    pub fn from_env() -> Result<Self> {
        let api_key = ApiKey::from_env()?;
        let base_url =
            std::env::var("EVERRUNS_API_URL").unwrap_or_else(|_| DEFAULT_BASE_URL.to_string());
        Self::with_api_key_and_url(api_key, &base_url)
    }

    /// Create a new client with a custom base URL
    pub fn with_base_url(api_key: impl Into<String>, base_url: &str) -> Result<Self> {
        let api_key = ApiKey::new(api_key);
        Self::with_api_key_and_url(api_key, base_url)
    }

    /// Create a new client with an ApiKey instance
    pub fn with_api_key(api_key: ApiKey) -> Result<Self> {
        Self::with_api_key_and_url(api_key, DEFAULT_BASE_URL)
    }

    fn with_api_key_and_url(api_key: ApiKey, base_url: &str) -> Result<Self> {
        let http = reqwest::Client::builder()
            .timeout(std::time::Duration::from_secs(30))
            .build()?;

        // Ensure base URL has trailing slash for correct URL joining.
        // Url::join follows RFC 3986: without trailing slash, relative paths
        // replace the last path segment instead of appending.
        // Example: "http://host/api" + "v1/x" = "http://host/v1/x" (wrong)
        //          "http://host/api/" + "v1/x" = "http://host/api/v1/x" (correct)
        let normalized = if base_url.ends_with('/') {
            base_url.to_string()
        } else {
            format!("{}/", base_url)
        };
        let base_url = Url::parse(&normalized)?;

        Ok(Self {
            http,
            base_url,
            api_key,
        })
    }

    /// Get the agents client
    pub fn agents(&self) -> AgentsClient<'_> {
        AgentsClient { client: self }
    }

    /// Get the sessions client
    pub fn sessions(&self) -> SessionsClient<'_> {
        SessionsClient { client: self }
    }

    /// Get the messages client
    pub fn messages(&self) -> MessagesClient<'_> {
        MessagesClient { client: self }
    }

    /// Get the events client
    pub fn events(&self) -> EventsClient<'_> {
        EventsClient { client: self }
    }

    /// Get the capabilities client
    pub fn capabilities(&self) -> CapabilitiesClient<'_> {
        CapabilitiesClient { client: self }
    }

    /// Get the session files client
    pub fn session_files(&self) -> SessionFilesClient<'_> {
        SessionFilesClient { client: self }
    }

    /// Get the connections client
    pub fn connections(&self) -> ConnectionsClient<'_> {
        ConnectionsClient { client: self }
    }

    pub(crate) fn url(&self, path: &str) -> Url {
        // Use relative path (no leading slash) for correct joining with base URL.
        // The path parameter starts with "/" (e.g., "/agents"), so we strip it.
        let path_without_slash = path.strip_prefix('/').unwrap_or(path);
        let full_path = format!("v1/{}", path_without_slash);
        self.base_url.join(&full_path).expect("valid URL")
    }

    fn headers(&self) -> HeaderMap {
        let mut headers = HeaderMap::new();
        headers.insert(
            AUTHORIZATION,
            HeaderValue::from_str(self.api_key.expose()).expect("valid header"),
        );
        headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));
        headers
    }

    pub(crate) async fn get<T: serde::de::DeserializeOwned>(&self, path: &str) -> Result<T> {
        let resp = self
            .http
            .get(self.url(path))
            .headers(self.headers())
            .send()
            .await?;

        self.handle_response(resp).await
    }

    pub(crate) async fn get_url<T: serde::de::DeserializeOwned>(&self, url: Url) -> Result<T> {
        let resp = self.http.get(url).headers(self.headers()).send().await?;

        self.handle_response(resp).await
    }

    pub(crate) async fn post<T: serde::de::DeserializeOwned, B: serde::Serialize>(
        &self,
        path: &str,
        body: &B,
    ) -> Result<T> {
        let resp = self
            .http
            .post(self.url(path))
            .headers(self.headers())
            .json(body)
            .send()
            .await?;

        self.handle_response(resp).await
    }

    #[allow(dead_code)]
    pub(crate) async fn patch<T: serde::de::DeserializeOwned, B: serde::Serialize>(
        &self,
        path: &str,
        body: &B,
    ) -> Result<T> {
        let resp = self
            .http
            .patch(self.url(path))
            .headers(self.headers())
            .json(body)
            .send()
            .await?;

        self.handle_response(resp).await
    }

    pub(crate) async fn post_text<T: serde::de::DeserializeOwned>(
        &self,
        path: &str,
        body: &str,
    ) -> Result<T> {
        let mut headers = self.headers();
        headers.insert(CONTENT_TYPE, HeaderValue::from_static("text/plain"));
        let resp = self
            .http
            .post(self.url(path))
            .headers(headers)
            .body(body.to_string())
            .send()
            .await?;

        self.handle_response(resp).await
    }

    pub(crate) async fn get_text(&self, path: &str) -> Result<String> {
        let resp = self
            .http
            .get(self.url(path))
            .headers(self.headers())
            .send()
            .await?;

        if resp.status().is_success() {
            Ok(resp.text().await?)
        } else {
            let status = resp.status().as_u16();
            let body = resp.text().await.unwrap_or_default();
            Err(Error::from_api_response(status, &body))
        }
    }

    pub(crate) async fn put<T: serde::de::DeserializeOwned, B: serde::Serialize>(
        &self,
        path: &str,
        body: &B,
    ) -> Result<T> {
        let resp = self
            .http
            .put(self.url(path))
            .headers(self.headers())
            .json(body)
            .send()
            .await?;

        self.handle_response(resp).await
    }

    pub(crate) async fn put_empty(&self, path: &str) -> Result<()> {
        let resp = self
            .http
            .put(self.url(path))
            .headers(self.headers())
            .send()
            .await?;

        if resp.status().is_success() {
            Ok(())
        } else {
            let status = resp.status().as_u16();
            let body = resp.text().await.unwrap_or_default();
            Err(Error::from_api_response(status, &body))
        }
    }

    pub(crate) async fn delete(&self, path: &str) -> Result<()> {
        let resp = self
            .http
            .delete(self.url(path))
            .headers(self.headers())
            .send()
            .await?;

        if resp.status().is_success() {
            Ok(())
        } else {
            let status = resp.status().as_u16();
            let body = resp.text().await.unwrap_or_default();
            Err(Error::from_api_response(status, &body))
        }
    }

    pub(crate) async fn delete_url<T: serde::de::DeserializeOwned>(&self, url: Url) -> Result<T> {
        let resp = self.http.delete(url).headers(self.headers()).send().await?;

        self.handle_response(resp).await
    }

    async fn handle_response<T: serde::de::DeserializeOwned>(
        &self,
        resp: reqwest::Response,
    ) -> Result<T> {
        if resp.status().is_success() {
            Ok(resp.json().await?)
        } else {
            let status = resp.status().as_u16();
            let body = resp.text().await.unwrap_or_default();
            Err(Error::from_api_response(status, &body))
        }
    }

    /// Get the SSE URL for a session
    pub(crate) fn sse_url(
        &self,
        session_id: &str,
        since_id: Option<&str>,
        types: &[&str],
        exclude: &[&str],
    ) -> Url {
        let mut url = self.url(&format!("/sessions/{}/sse", session_id));
        if let Some(id) = since_id {
            url.query_pairs_mut().append_pair("since_id", id);
        }
        for t in types {
            url.query_pairs_mut().append_pair("types", t);
        }
        for e in exclude {
            url.query_pairs_mut().append_pair("exclude", e);
        }
        url
    }

    pub(crate) fn auth_header(&self) -> String {
        self.api_key.expose().to_string()
    }
}

/// Client for agent operations
pub struct AgentsClient<'a> {
    client: &'a Everruns,
}

impl<'a> AgentsClient<'a> {
    /// List all agents
    pub async fn list(&self) -> Result<ListResponse<Agent>> {
        self.client.get("/agents").await
    }

    /// List agents matching a search query (case-insensitive name/description match)
    pub async fn search(&self, query: &str) -> Result<ListResponse<Agent>> {
        let mut url = self.client.url("/agents");
        url.query_pairs_mut().append_pair("search", query);
        self.client.get_url(url).await
    }

    /// Get an agent by ID
    pub async fn get(&self, id: &str) -> Result<Agent> {
        self.client.get(&format!("/agents/{}", id)).await
    }

    /// Create a new agent with a server-assigned ID
    pub async fn create(&self, name: &str, system_prompt: &str) -> Result<Agent> {
        let req = CreateAgentRequest::new(name, system_prompt);
        self.client.post("/agents", &req).await
    }

    /// Create an agent with full options
    pub async fn create_with_options(&self, req: CreateAgentRequest) -> Result<Agent> {
        self.client.post("/agents", &req).await
    }

    /// Create or update an agent with a client-supplied ID (upsert).
    ///
    /// If an agent with the given ID exists, it is updated.
    /// If not, a new agent is created with that ID.
    ///
    /// Use [`generate_agent_id`] to create a properly formatted ID.
    pub async fn apply(&self, id: &str, name: &str, system_prompt: &str) -> Result<Agent> {
        let req = CreateAgentRequest::new(name, system_prompt).id(id);
        self.client.post("/agents", &req).await
    }

    /// Create or update an agent with full options (upsert).
    ///
    /// The `id` parameter is set on the request, overriding any existing value.
    pub async fn apply_with_options(&self, id: &str, req: CreateAgentRequest) -> Result<Agent> {
        let req = req.id(id);
        self.client.post("/agents", &req).await
    }

    /// Copy an agent, creating a new agent with the same configuration
    pub async fn copy(&self, id: &str) -> Result<Agent> {
        self.client
            .post::<Agent, _>(&format!("/agents/{}/copy", id), &())
            .await
    }

    /// Delete (archive) an agent
    pub async fn delete(&self, id: &str) -> Result<()> {
        self.client.delete(&format!("/agents/{}", id)).await
    }

    /// Import an agent from Markdown, YAML, JSON, or plain text
    pub async fn import(&self, content: &str) -> Result<Agent> {
        self.client.post_text("/agents/import", content).await
    }

    /// Export an agent as Markdown with YAML front matter
    pub async fn export(&self, id: &str) -> Result<String> {
        self.client
            .get_text(&format!("/agents/{}/export", id))
            .await
    }
}

/// Client for session operations
pub struct SessionsClient<'a> {
    client: &'a Everruns,
}

impl<'a> SessionsClient<'a> {
    /// List all sessions
    pub async fn list(&self) -> Result<ListResponse<Session>> {
        self.client.get("/sessions").await
    }

    /// List sessions matching a search query (case-insensitive title match)
    pub async fn search(&self, query: &str) -> Result<ListResponse<Session>> {
        let mut url = self.client.url("/sessions");
        url.query_pairs_mut().append_pair("search", query);
        self.client.get_url(url).await
    }

    /// Get a session by ID
    pub async fn get(&self, id: &str) -> Result<Session> {
        self.client.get(&format!("/sessions/{}", id)).await
    }

    /// Create a new session (server defaults to Generic harness)
    pub async fn create(&self) -> Result<Session> {
        let req = CreateSessionRequest::new();
        self.client.post("/sessions", &req).await
    }

    /// Create a session with full options
    pub async fn create_with_options(&self, req: CreateSessionRequest) -> Result<Session> {
        self.client.post("/sessions", &req).await
    }

    /// Delete a session
    pub async fn delete(&self, id: &str) -> Result<()> {
        self.client.delete(&format!("/sessions/{}", id)).await
    }

    /// Cancel the current turn in a session
    pub async fn cancel(&self, id: &str) -> Result<()> {
        self.client
            .post::<serde_json::Value, _>(&format!("/sessions/{}/cancel", id), &())
            .await?;
        Ok(())
    }

    /// Pin a session for the current user
    pub async fn pin(&self, id: &str) -> Result<()> {
        self.client
            .put_empty(&format!("/sessions/{}/pin", id))
            .await
    }

    /// Unpin a session for the current user
    pub async fn unpin(&self, id: &str) -> Result<()> {
        self.client.delete(&format!("/sessions/{}/pin", id)).await
    }

    /// Batch-set encrypted secrets for a session
    pub async fn set_secrets(
        &self,
        id: &str,
        secrets: &std::collections::HashMap<String, String>,
    ) -> Result<()> {
        let req = SetSecretsRequest::new(secrets.clone());
        self.client
            .put::<serde_json::Value, _>(&format!("/sessions/{}/storage/secrets", id), &req)
            .await?;
        Ok(())
    }
}

/// Client for message operations
pub struct MessagesClient<'a> {
    client: &'a Everruns,
}

impl<'a> MessagesClient<'a> {
    /// List messages in a session
    pub async fn list(&self, session_id: &str) -> Result<ListResponse<Message>> {
        self.client
            .get(&format!("/sessions/{}/messages", session_id))
            .await
    }

    /// Create a new message (send text)
    pub async fn create(&self, session_id: &str, text: &str) -> Result<Message> {
        let req = CreateMessageRequest::user_text(text);
        self.client
            .post(&format!("/sessions/{}/messages", session_id), &req)
            .await
    }

    /// Send tool results back to the session.
    ///
    /// Use this after receiving tool calls from an `output.message.completed`
    /// event to provide results from locally-executed tools.
    pub async fn create_tool_results(
        &self,
        session_id: &str,
        results: Vec<ContentPart>,
    ) -> Result<Message> {
        let req = CreateMessageRequest::tool_results(results);
        self.client
            .post(&format!("/sessions/{}/messages", session_id), &req)
            .await
    }

    /// Create a message with full options
    pub async fn create_with_options(
        &self,
        session_id: &str,
        req: CreateMessageRequest,
    ) -> Result<Message> {
        self.client
            .post(&format!("/sessions/{}/messages", session_id), &req)
            .await
    }
}

/// Client for event operations
pub struct EventsClient<'a> {
    client: &'a Everruns,
}

/// Options for listing events with backward pagination
#[derive(Debug, Clone, Default)]
pub struct ListEventsOptions {
    /// Positive type filter
    pub types: Vec<String>,
    /// Event types to exclude
    pub exclude: Vec<String>,
    /// Max events to return (backward pagination)
    pub limit: Option<u32>,
    /// Cursor for backward pagination: only return events with sequence < this value
    pub before_sequence: Option<i32>,
}

impl<'a> EventsClient<'a> {
    /// List events in a session
    pub async fn list(&self, session_id: &str) -> Result<ListResponse<Event>> {
        self.client
            .get(&format!("/sessions/{}/events", session_id))
            .await
    }

    /// List events with options (filtering, backward pagination)
    pub async fn list_with_options(
        &self,
        session_id: &str,
        options: &ListEventsOptions,
    ) -> Result<ListResponse<Event>> {
        let mut url = self.client.url(&format!("/sessions/{}/events", session_id));
        for t in &options.types {
            url.query_pairs_mut().append_pair("types", t);
        }
        for e in &options.exclude {
            url.query_pairs_mut().append_pair("exclude", e);
        }
        if let Some(limit) = options.limit {
            url.query_pairs_mut()
                .append_pair("limit", &limit.to_string());
        }
        if let Some(seq) = options.before_sequence {
            url.query_pairs_mut()
                .append_pair("before_sequence", &seq.to_string());
        }
        self.client.get_url(url).await
    }

    /// Stream events from a session via SSE
    pub fn stream(&self, session_id: &str) -> crate::sse::EventStream {
        crate::sse::EventStream::new(
            self.client.clone(),
            session_id.to_string(),
            crate::sse::StreamOptions::default(),
        )
    }

    /// Stream events with options
    pub fn stream_with_options(
        &self,
        session_id: &str,
        options: crate::sse::StreamOptions,
    ) -> crate::sse::EventStream {
        crate::sse::EventStream::new(self.client.clone(), session_id.to_string(), options)
    }
}

/// Client for capability operations
pub struct CapabilitiesClient<'a> {
    client: &'a Everruns,
}

impl<'a> CapabilitiesClient<'a> {
    /// List all available capabilities
    pub async fn list(&self) -> Result<ListResponse<CapabilityInfo>> {
        self.client.get("/capabilities").await
    }

    /// Get a specific capability by ID
    pub async fn get(&self, id: &str) -> Result<CapabilityInfo> {
        self.client.get(&format!("/capabilities/{}", id)).await
    }
}

/// Client for session filesystem operations
pub struct SessionFilesClient<'a> {
    client: &'a Everruns,
}

impl<'a> SessionFilesClient<'a> {
    /// List files in a directory
    pub async fn list(
        &self,
        session_id: &str,
        path: Option<&str>,
        recursive: Option<bool>,
    ) -> Result<ListResponse<FileInfo>> {
        let api_path = match path {
            Some(p) => format!("/sessions/{}/fs/{}", session_id, p.trim_start_matches('/')),
            None => format!("/sessions/{}/fs", session_id),
        };
        let mut url = self.client.url(&api_path);
        if let Some(true) = recursive {
            url.query_pairs_mut().append_pair("recursive", "true");
        }
        self.client.get_url(url).await
    }

    /// Read a file's content
    pub async fn read(&self, session_id: &str, path: &str) -> Result<SessionFile> {
        self.client
            .get(&format!(
                "/sessions/{}/fs/{}",
                session_id,
                path.trim_start_matches('/')
            ))
            .await
    }

    /// Create a file
    pub async fn create(
        &self,
        session_id: &str,
        path: &str,
        content: &str,
        encoding: Option<&str>,
    ) -> Result<SessionFile> {
        let mut req = CreateFileRequest::file(content);
        if let Some(enc) = encoding {
            req = req.encoding(enc);
        }
        self.client
            .post(
                &format!(
                    "/sessions/{}/fs/{}",
                    session_id,
                    path.trim_start_matches('/')
                ),
                &req,
            )
            .await
    }

    /// Create a file with full options
    pub async fn create_with_options(
        &self,
        session_id: &str,
        path: &str,
        req: CreateFileRequest,
    ) -> Result<SessionFile> {
        self.client
            .post(
                &format!(
                    "/sessions/{}/fs/{}",
                    session_id,
                    path.trim_start_matches('/')
                ),
                &req,
            )
            .await
    }

    /// Create a directory
    pub async fn create_dir(&self, session_id: &str, path: &str) -> Result<SessionFile> {
        self.create_with_options(session_id, path, CreateFileRequest::directory())
            .await
    }

    /// Update a file's content
    pub async fn update(
        &self,
        session_id: &str,
        path: &str,
        content: &str,
        encoding: Option<&str>,
    ) -> Result<SessionFile> {
        let mut req = UpdateFileRequest::content(content);
        if let Some(enc) = encoding {
            req = req.encoding(enc);
        }
        self.client
            .put(
                &format!(
                    "/sessions/{}/fs/{}",
                    session_id,
                    path.trim_start_matches('/')
                ),
                &req,
            )
            .await
    }

    /// Update a file with full options
    pub async fn update_with_options(
        &self,
        session_id: &str,
        path: &str,
        req: UpdateFileRequest,
    ) -> Result<SessionFile> {
        self.client
            .put(
                &format!(
                    "/sessions/{}/fs/{}",
                    session_id,
                    path.trim_start_matches('/')
                ),
                &req,
            )
            .await
    }

    /// Delete a file or directory
    pub async fn delete(
        &self,
        session_id: &str,
        path: &str,
        recursive: Option<bool>,
    ) -> Result<DeleteResponse> {
        let mut url = self.client.url(&format!(
            "/sessions/{}/fs/{}",
            session_id,
            path.trim_start_matches('/')
        ));
        if let Some(true) = recursive {
            url.query_pairs_mut().append_pair("recursive", "true");
        }
        self.client.delete_url(url).await
    }

    /// Move/rename a file
    pub async fn move_file(
        &self,
        session_id: &str,
        src_path: &str,
        dst_path: &str,
    ) -> Result<SessionFile> {
        let req = MoveFileRequest::new(src_path, dst_path);
        self.client
            .post(&format!("/sessions/{}/fs/_/move", session_id), &req)
            .await
    }

    /// Copy a file
    pub async fn copy_file(
        &self,
        session_id: &str,
        src_path: &str,
        dst_path: &str,
    ) -> Result<SessionFile> {
        let req = CopyFileRequest::new(src_path, dst_path);
        self.client
            .post(&format!("/sessions/{}/fs/_/copy", session_id), &req)
            .await
    }

    /// Search files with regex
    pub async fn grep(
        &self,
        session_id: &str,
        pattern: &str,
        path_pattern: Option<&str>,
    ) -> Result<ListResponse<GrepResult>> {
        let mut req = GrepRequest::new(pattern);
        if let Some(pp) = path_pattern {
            req = req.path_pattern(pp);
        }
        self.client
            .post(&format!("/sessions/{}/fs/_/grep", session_id), &req)
            .await
    }

    /// Get file or directory stat
    pub async fn stat(&self, session_id: &str, path: &str) -> Result<FileStat> {
        let req = StatRequest::new(path);
        self.client
            .post(&format!("/sessions/{}/fs/_/stat", session_id), &req)
            .await
    }
}

/// Client for user connection operations
pub struct ConnectionsClient<'a> {
    client: &'a Everruns,
}

impl<'a> ConnectionsClient<'a> {
    /// Set an API key connection for a provider
    pub async fn set(&self, provider: &str, api_key: &str) -> Result<Connection> {
        let req = SetConnectionRequest::new(api_key);
        self.client
            .post(&format!("/user/connections/{}", provider), &req)
            .await
    }

    /// List all connections
    pub async fn list(&self) -> Result<ListResponse<Connection>> {
        self.client.get("/user/connections").await
    }

    /// Remove a connection
    pub async fn remove(&self, provider: &str) -> Result<()> {
        self.client
            .delete(&format!("/user/connections/{}", provider))
            .await
    }
}

impl std::fmt::Debug for Everruns {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Everruns")
            .field("base_url", &self.base_url.as_str())
            .field("api_key", &self.api_key)
            .finish()
    }
}

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

    fn test_client() -> Everruns {
        Everruns::with_base_url("test_key", "https://api.example.com").unwrap()
    }

    #[test]
    fn test_sse_url_no_params() {
        let client = test_client();
        let url = client.sse_url("session_123", None, &[], &[]);
        assert_eq!(
            url.as_str(),
            "https://api.example.com/v1/sessions/session_123/sse"
        );
    }

    #[test]
    fn test_sse_url_with_since_id() {
        let client = test_client();
        let url = client.sse_url("session_123", Some("evt_001"), &[], &[]);
        assert_eq!(
            url.as_str(),
            "https://api.example.com/v1/sessions/session_123/sse?since_id=evt_001"
        );
    }

    #[test]
    fn test_sse_url_exclude_expands_as_repeated_keys() {
        let client = test_client();
        let url = client.sse_url(
            "session_123",
            None,
            &[],
            &["output.message.delta", "reason.thinking.delta"],
        );
        let url_str = url.as_str();
        // Must use repeated keys: ?exclude=a&exclude=b
        // Not comma-separated: ?exclude=a,b
        assert!(
            url_str.contains("exclude=output.message.delta"),
            "URL missing first exclude: {}",
            url_str
        );
        assert!(
            url_str.contains("exclude=reason.thinking.delta"),
            "URL missing second exclude: {}",
            url_str
        );
        assert!(
            !url_str.contains(','),
            "URL must not use comma-separated excludes: {}",
            url_str
        );
        assert_eq!(
            url_str,
            "https://api.example.com/v1/sessions/session_123/sse?exclude=output.message.delta&exclude=reason.thinking.delta"
        );
    }

    #[test]
    fn test_sse_url_single_exclude() {
        let client = test_client();
        let url = client.sse_url("session_123", None, &[], &["output.message.delta"]);
        assert_eq!(
            url.as_str(),
            "https://api.example.com/v1/sessions/session_123/sse?exclude=output.message.delta"
        );
    }

    #[test]
    fn test_sse_url_combined_since_id_and_exclude() {
        let client = test_client();
        let url = client.sse_url(
            "session_123",
            Some("evt_001"),
            &[],
            &["output.message.delta", "reason.thinking.delta"],
        );
        assert_eq!(
            url.as_str(),
            "https://api.example.com/v1/sessions/session_123/sse?since_id=evt_001&exclude=output.message.delta&exclude=reason.thinking.delta"
        );
    }

    #[test]
    fn test_sse_url_three_exclude_values() {
        let client = test_client();
        let url = client.sse_url(
            "session_123",
            None,
            &[],
            &[
                "output.message.delta",
                "reason.thinking.delta",
                "tool.started",
            ],
        );
        let url_str = url.as_str();
        assert_eq!(url_str.matches("exclude=").count(), 3);
    }

    #[test]
    fn test_sse_url_since_id_special_chars_encoded() {
        let client = test_client();
        let url = client.sse_url("session_123", Some("evt&id=1"), &[], &[]);
        let url_str = url.as_str();
        // URL should encode special characters
        assert!(!url_str.contains("evt&id=1"));
        assert!(url_str.contains("since_id=evt%26id%3D1"));
    }

    #[test]
    fn test_sse_url_with_types() {
        let client = test_client();
        let url = client.sse_url(
            "session_123",
            None,
            &["turn.started", "turn.completed"],
            &[],
        );
        assert_eq!(
            url.as_str(),
            "https://api.example.com/v1/sessions/session_123/sse?types=turn.started&types=turn.completed"
        );
    }

    #[test]
    fn test_sse_url_with_types_and_exclude() {
        let client = test_client();
        let url = client.sse_url(
            "session_123",
            Some("evt_001"),
            &["turn.started"],
            &["output.message.delta"],
        );
        assert_eq!(
            url.as_str(),
            "https://api.example.com/v1/sessions/session_123/sse?since_id=evt_001&types=turn.started&exclude=output.message.delta"
        );
    }
}