firecrawl 2.17.0

Official Rust SDK for Firecrawl API v2.
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
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
//! Agent endpoint for Firecrawl API v2.
//!
//! The Agent endpoint provides autonomous web browsing capabilities using AI
//! to accomplish complex tasks that may require multiple page interactions.

use crate::client::Client;
use crate::types::{AgentEffort, AgentModel, AgentWebhookConfig};
use crate::{AuditMetadata, FirecrawlError};
use serde::{Deserialize, Serialize};
use serde_json::Value;

/// Options for running an agent task.
#[serde_with::skip_serializing_none]
#[derive(Deserialize, Serialize, Debug, Default, Clone)]
#[serde(rename_all = "camelCase")]
pub struct AgentOptions {
    /// Starting URLs for the agent to explore.
    pub urls: Option<Vec<String>>,

    /// The prompt describing what the agent should accomplish.
    pub prompt: String,

    /// JSON schema for the expected output structure.
    pub schema: Option<Value>,

    /// Integration identifier for tracking.
    pub integration: Option<String>,

    /// Origin label for request attribution (e.g., "rust-sdk@2.16.1").
    /// Defaults to `rust-sdk@<version>` when unset.
    pub origin: Option<String>,

    /// Maximum credits the agent can use.
    pub max_credits: Option<u32>,

    /// Strictly constrain the agent to the provided URLs.
    pub strict_constrain_to_urls: Option<bool>,

    /// Agent model to use. Defaults to `spark-1-pro` server-side when unset.
    pub model: Option<AgentModel>,

    /// Reasoning effort for the agent task. Every level runs spark-2.
    pub effort: Option<AgentEffort>,

    /// Webhook configuration for agent notifications.
    pub webhook: Option<AgentWebhookConfig>,

    /// User attribution to include with SIEM logging events.
    pub audit_metadata: Option<AuditMetadata>,

    /// Poll interval for synchronous agent execution (milliseconds).
    #[serde(skip)]
    pub poll_interval: Option<u64>,

    /// Timeout for synchronous agent execution (seconds).
    #[serde(skip)]
    pub timeout: Option<u64>,
}

/// Response from starting an agent task.
#[derive(Deserialize, Serialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct AgentResponse {
    /// Whether the request was successful.
    pub success: bool,
    /// The agent task ID.
    pub id: String,
    /// Error message if the request failed.
    pub error: Option<String>,
}

/// Agent task status.
#[derive(Deserialize, Serialize, Clone, Copy, Debug, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub enum AgentStatus {
    /// The agent is still processing.
    Processing,
    /// The agent has completed its task.
    Completed,
    /// The agent task failed.
    Failed,
    /// The agent task was cancelled.
    Cancelled,
}

/// Status response from an agent task.
#[serde_with::skip_serializing_none]
#[derive(Deserialize, Serialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct AgentStatusResponse {
    /// Whether the status check was successful.
    pub success: bool,
    /// Current status of the agent task.
    pub status: AgentStatus,
    /// Error message if the task failed.
    pub error: Option<String>,
    /// Extracted data (if schema was provided) or task results.
    pub data: Option<Value>,
    /// Model used for the agent task.
    pub model: Option<AgentModel>,
    /// Reasoning effort the task ran with, if one was requested.
    pub effort: Option<AgentEffort>,
    /// Expiry time of the task data.
    pub expires_at: Option<String>,
    /// Credits used by the agent task.
    pub credits_used: Option<u32>,
}

// Agent trace types (GET /v2/agent/:id/trace). These mirror the agent
// service's canonical event schema (schemaVersion 1): usage.recorded events
// are withheld server-side and agent.started carries no model name.

/// Which agent produced a trace event.
#[derive(Deserialize, Serialize, Clone, Debug, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct AgentTraceAgentIdentity {
    pub id: String,
    pub role: AgentTraceAgentRole,
    pub name: String,
    #[serde(default)]
    pub parent_id: Option<String>,
}

/// Role of the agent that produced a trace event.
#[derive(Deserialize, Serialize, Clone, Copy, Debug, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum AgentTraceAgentRole {
    Orchestrator,
    Subagent,
    Browser,
    System,
}

/// Structured error attached to terminal and error trace events.
#[derive(Deserialize, Serialize, Clone, Debug, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct AgentTraceError {
    pub code: AgentTraceErrorCode,
    pub source: AgentTraceErrorSource,
    pub retryable: bool,
    pub message: String,
}

/// Machine-readable trace error code.
#[derive(Deserialize, Serialize, Clone, Copy, Debug, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum AgentTraceErrorCode {
    Cancelled,
    CreditLimitReached,
    ParentFinished,
    Refused,
    Internal,
}

/// Where a trace error originated.
#[derive(Deserialize, Serialize, Clone, Copy, Debug, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum AgentTraceErrorSource {
    Agent,
    Tool,
    Billing,
    System,
}

/// Reference to an artifact snapshot; fetch the content with
/// [`Client::get_agent_snapshot`].
#[derive(Deserialize, Serialize, Clone, Debug, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct AgentTraceArtifactChange {
    pub kind: AgentTraceArtifactKind,
    pub artifact_id: String,
    #[serde(default)]
    pub path: Option<String>,
    pub snapshot_id: String,
    pub change: AgentTraceArtifactChangeKind,
    #[serde(default)]
    pub changed_fields: Option<Vec<String>>,
    #[serde(default)]
    pub item_count: Option<u64>,
    #[serde(default)]
    pub source_tool_call_id: Option<String>,
}

/// Kind of artifact content behind a snapshot.
#[derive(Deserialize, Serialize, Clone, Copy, Debug, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum AgentTraceArtifactKind {
    Json,
    Markdown,
    Html,
    Screenshot,
    Text,
}

/// How an artifact changed in an artifact.updated event.
#[derive(Deserialize, Serialize, Clone, Copy, Debug, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum AgentTraceArtifactChangeKind {
    Init,
    Partial,
    Append,
    Modify,
    Update,
}

/// Fields every trace event carries.
#[derive(Deserialize, Serialize, Clone, Debug, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct AgentTraceEventBase {
    /// Canonical event schema version (1 at the time of writing).
    pub schema_version: u32,
    pub event_id: String,
    pub run_id: String,
    /// ISO 8601 timestamp with offset.
    pub occurred_at: String,
    pub producer_sequence: u64,
    pub agent: AgentTraceAgentIdentity,
}

macro_rules! trace_event_struct {
    ($name:ident { $( $(#[$meta:meta])* $field:ident : $ty:ty ),* $(,)? }) => {
        #[derive(Deserialize, Serialize, Clone, Debug, PartialEq)]
        #[serde(rename_all = "camelCase")]
        pub struct $name {
            #[serde(flatten)]
            pub base: AgentTraceEventBase,
            $( $(#[$meta])* pub $field : $ty ),*
        }
    };
    ($name:ident) => {
        #[derive(Deserialize, Serialize, Clone, Debug, PartialEq)]
        #[serde(rename_all = "camelCase")]
        pub struct $name {
            #[serde(flatten)]
            pub base: AgentTraceEventBase,
        }
    };
}

trace_event_struct!(AgentTraceRunStartedEvent);
trace_event_struct!(AgentTraceRunCancelRequestedEvent {
    /// Always "user" at present.
    reason: String
});
trace_event_struct!(AgentTraceRunFinishedEvent {
    outcome: AgentTraceRunOutcome,
    error: Option<AgentTraceError>
});
trace_event_struct!(AgentTraceAgentStartedEvent);
trace_event_struct!(AgentTraceAgentFinishedEvent {
    outcome: AgentTraceAgentOutcome,
    duration_ms: u64,
    error: Option<AgentTraceError>
});
trace_event_struct!(AgentTraceBrowserSessionStartedEvent { session_id: String });
trace_event_struct!(AgentTraceBrowserSessionFinishedEvent {
    session_id: String,
    duration_ms: u64
});
trace_event_struct!(AgentTraceProgressReportedEvent {
    phase: AgentTraceProgressPhase,
    message: String
});
trace_event_struct!(AgentTraceReasoningSummaryEvent { text: String });
trace_event_struct!(AgentTraceToolCallStartedEvent {
    tool_call_id: String,
    tool_name: String,
    parameters: Value
});
trace_event_struct!(AgentTraceToolCallFinishedEvent {
    tool_call_id: String,
    tool_name: String,
    result: Value
});
trace_event_struct!(AgentTraceArtifactUpdatedEvent {
    artifact: AgentTraceArtifactChange
});
trace_event_struct!(AgentTraceErrorOccurredEvent {
    error: AgentTraceError
});

/// Terminal outcome of a run.
#[derive(Deserialize, Serialize, Clone, Copy, Debug, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum AgentTraceRunOutcome {
    Succeeded,
    Failed,
    Cancelled,
    Refused,
    CreditLimitReached,
}

/// Terminal outcome of a single agent.
#[derive(Deserialize, Serialize, Clone, Copy, Debug, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum AgentTraceAgentOutcome {
    Succeeded,
    Failed,
    Cancelled,
    Refused,
}

/// Phase a progress.reported event belongs to.
#[derive(Deserialize, Serialize, Clone, Copy, Debug, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum AgentTraceProgressPhase {
    Planning,
    Working,
    Finalizing,
}

/// One event in an agent job's execution trace.
#[derive(Deserialize, Serialize, Clone, Debug, PartialEq)]
#[serde(tag = "type")]
pub enum AgentTraceEvent {
    #[serde(rename = "run.started")]
    RunStarted(AgentTraceRunStartedEvent),
    #[serde(rename = "run.cancel_requested")]
    RunCancelRequested(AgentTraceRunCancelRequestedEvent),
    #[serde(rename = "run.finished")]
    RunFinished(AgentTraceRunFinishedEvent),
    #[serde(rename = "agent.started")]
    AgentStarted(AgentTraceAgentStartedEvent),
    #[serde(rename = "agent.finished")]
    AgentFinished(AgentTraceAgentFinishedEvent),
    #[serde(rename = "browser.session.started")]
    BrowserSessionStarted(AgentTraceBrowserSessionStartedEvent),
    #[serde(rename = "browser.session.finished")]
    BrowserSessionFinished(AgentTraceBrowserSessionFinishedEvent),
    #[serde(rename = "progress.reported")]
    ProgressReported(AgentTraceProgressReportedEvent),
    #[serde(rename = "reasoning.summary")]
    ReasoningSummary(AgentTraceReasoningSummaryEvent),
    #[serde(rename = "tool_call.started")]
    ToolCallStarted(AgentTraceToolCallStartedEvent),
    #[serde(rename = "tool_call.finished")]
    ToolCallFinished(AgentTraceToolCallFinishedEvent),
    #[serde(rename = "artifact.updated")]
    ArtifactUpdated(AgentTraceArtifactUpdatedEvent),
    #[serde(rename = "error.occurred")]
    ErrorOccurred(AgentTraceErrorOccurredEvent),
}

/// Live browser session, present only when the trace is requested with
/// live view enabled.
#[derive(Deserialize, Serialize, Clone, Debug, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct AgentTraceActiveBrowserSession {
    pub id: String,
    pub live_view_url: String,
    pub viewport: AgentTraceViewport,
}

/// Browser viewport dimensions.
#[derive(Deserialize, Serialize, Clone, Copy, Debug, PartialEq, Eq)]
pub struct AgentTraceViewport {
    pub width: u32,
    pub height: u32,
}

/// Response from getting an agent job's execution trace.
#[serde_with::skip_serializing_none]
#[derive(Deserialize, Serialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct AgentTraceResponse {
    pub success: bool,
    pub id: Option<String>,
    pub events: Option<Vec<AgentTraceEvent>>,
    pub credits_used: Option<u32>,
    pub active_browser_sessions: Option<Vec<AgentTraceActiveBrowserSession>>,
    pub error: Option<String>,
}

/// Response from getting an artifact snapshot of an agent job.
#[serde_with::skip_serializing_none]
#[derive(Deserialize, Serialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct AgentSnapshotResponse {
    pub success: bool,
    pub id: Option<String>,
    pub snapshot_id: Option<String>,
    /// Full artifact content as a JSON/string blob.
    pub snapshot: Option<String>,
    pub error: Option<String>,
}

impl Client {
    /// Starts an agent task asynchronously.
    ///
    /// Returns immediately with a task ID that can be used to check status.
    ///
    /// # Arguments
    ///
    /// * `options` - Agent task configuration including the prompt.
    ///
    /// # Returns
    ///
    /// An `AgentResponse` containing the task ID.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use firecrawl::{Client, AgentOptions};
    ///
    /// #[tokio::main]
    /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
    ///     let client = Client::new("your-api-key")?;
    ///
    ///     let options = AgentOptions {
    ///         urls: Some(vec!["https://example.com".to_string()]),
    ///         prompt: "Find the pricing information on this website".to_string(),
    ///         ..Default::default()
    ///     };
    ///
    ///     let response = client.start_agent(options).await?;
    ///     println!("Agent task started: {}", response.id);
    ///
    ///     Ok(())
    /// }
    /// ```
    pub async fn start_agent(
        &self,
        options: AgentOptions,
    ) -> Result<AgentResponse, FirecrawlError> {
        let mut options = options;
        if options.origin.is_none() {
            options.origin = Some(format!("rust-sdk@{}", env!("CARGO_PKG_VERSION")));
        }

        let headers = self.prepare_headers(None);

        let response = self
            .client
            .post(self.url("/agent"))
            .headers(headers)
            .json(&options)
            .send()
            .await
            .map_err(|e| FirecrawlError::HttpError("Starting agent task".to_string(), e))?;

        self.handle_response(response, "start agent").await
    }

    /// Gets the status of an agent task.
    ///
    /// # Arguments
    ///
    /// * `id` - The agent task ID.
    ///
    /// # Returns
    ///
    /// An `AgentStatusResponse` containing the current status and any results.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use firecrawl::Client;
    ///
    /// #[tokio::main]
    /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
    ///     let client = Client::new("your-api-key")?;
    ///
    ///     let status = client.get_agent_status("task-id").await?;
    ///     println!("Status: {:?}", status.status);
    ///
    ///     if let Some(data) = status.data {
    ///         println!("Result: {}", data);
    ///     }
    ///
    ///     Ok(())
    /// }
    /// ```
    pub async fn get_agent_status(
        &self,
        id: impl AsRef<str>,
    ) -> Result<AgentStatusResponse, FirecrawlError> {
        let response = self
            .client
            .get(self.url(&format!("/agent/{}", id.as_ref())))
            .headers(self.prepare_headers(None))
            .send()
            .await
            .map_err(|e| {
                FirecrawlError::HttpError(format!("Getting agent status {}", id.as_ref()), e)
            })?;

        self.handle_response(response, format!("agent status {}", id.as_ref()))
            .await
    }

    /// Gets the execution trace of an agent task (spark-2 runs only).
    ///
    /// # Arguments
    ///
    /// * `id` - The agent task ID.
    /// * `live_view` - Also include currently active browser sessions with
    ///   live view URLs.
    ///
    /// # Returns
    ///
    /// An `AgentTraceResponse` containing the ordered trace events.
    pub async fn get_agent_trace(
        &self,
        id: impl AsRef<str>,
        live_view: bool,
    ) -> Result<AgentTraceResponse, FirecrawlError> {
        let path = if live_view {
            format!("/agent/{}/trace?liveView=true", id.as_ref())
        } else {
            format!("/agent/{}/trace", id.as_ref())
        };

        let response = self
            .client
            .get(self.url(&path))
            .headers(self.prepare_headers(None))
            .send()
            .await
            .map_err(|e| {
                FirecrawlError::HttpError(format!("Getting agent trace {}", id.as_ref()), e)
            })?;

        self.handle_response(response, format!("agent trace {}", id.as_ref()))
            .await
    }

    /// Gets the full content of an artifact snapshot referenced by a trace
    /// event.
    ///
    /// # Arguments
    ///
    /// * `id` - The agent task ID.
    /// * `snapshot_id` - Snapshot ID from an `artifact.updated` trace event.
    ///
    /// # Returns
    ///
    /// An `AgentSnapshotResponse` containing the snapshot content.
    pub async fn get_agent_snapshot(
        &self,
        id: impl AsRef<str>,
        snapshot_id: impl AsRef<str>,
    ) -> Result<AgentSnapshotResponse, FirecrawlError> {
        let response = self
            .client
            .get(self.url(&format!(
                "/agent/{}/snapshots/{}",
                id.as_ref(),
                snapshot_id.as_ref()
            )))
            .headers(self.prepare_headers(None))
            .send()
            .await
            .map_err(|e| {
                FirecrawlError::HttpError(
                    format!(
                        "Getting agent snapshot {} of {}",
                        snapshot_id.as_ref(),
                        id.as_ref()
                    ),
                    e,
                )
            })?;

        self.handle_response(response, format!("agent snapshot {}", id.as_ref()))
            .await
    }

    /// Runs an agent task and waits for completion.
    ///
    /// This method starts an agent task and polls until it completes, fails, or times out.
    ///
    /// # Arguments
    ///
    /// * `options` - Agent task configuration including the prompt.
    ///
    /// # Returns
    ///
    /// An `AgentStatusResponse` containing the final status and results.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use firecrawl::{Client, AgentOptions, AgentModel};
    /// use serde_json::json;
    ///
    /// #[tokio::main]
    /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
    ///     let client = Client::new("your-api-key")?;
    ///
    ///     let options = AgentOptions {
    ///         urls: Some(vec!["https://example.com/pricing".to_string()]),
    ///         prompt: "Extract the pricing tiers and their features".to_string(),
    ///         schema: Some(json!({
    ///             "type": "object",
    ///             "properties": {
    ///                 "tiers": {
    ///                     "type": "array",
    ///                     "items": {
    ///                         "type": "object",
    ///                         "properties": {
    ///                             "name": { "type": "string" },
    ///                             "price": { "type": "number" },
    ///                             "features": { "type": "array", "items": { "type": "string" } }
    ///                         }
    ///                     }
    ///                 }
    ///             }
    ///         })),
    ///         model: Some(AgentModel::Spark1Pro),
    ///         poll_interval: Some(3000),
    ///         timeout: Some(300),
    ///         ..Default::default()
    ///     };
    ///
    ///     let result = client.agent(options).await?;
    ///
    ///     if let Some(data) = result.data {
    ///         println!("Extracted pricing: {}", serde_json::to_string_pretty(&data)?);
    ///     }
    ///
    ///     Ok(())
    /// }
    /// ```
    pub async fn agent(
        &self,
        options: AgentOptions,
    ) -> Result<AgentStatusResponse, FirecrawlError> {
        let poll_interval = options.poll_interval.unwrap_or(2000);
        let timeout = options.timeout;

        let response = self.start_agent(options).await?;
        self.wait_for_agent(&response.id, poll_interval, timeout)
            .await
    }

    /// Waits for an agent task to complete.
    async fn wait_for_agent(
        &self,
        id: &str,
        poll_interval: u64,
        timeout: Option<u64>,
    ) -> Result<AgentStatusResponse, FirecrawlError> {
        let start = std::time::Instant::now();

        loop {
            let status = self.get_agent_status(id).await?;

            match status.status {
                AgentStatus::Completed | AgentStatus::Failed | AgentStatus::Cancelled => {
                    return Ok(status);
                }
                AgentStatus::Processing => {
                    // Check timeout
                    if let Some(timeout_secs) = timeout {
                        if start.elapsed().as_secs() > timeout_secs {
                            return Ok(status);
                        }
                    }

                    tokio::time::sleep(tokio::time::Duration::from_millis(poll_interval)).await;
                }
            }
        }
    }

    /// Cancels a running agent task.
    ///
    /// # Arguments
    ///
    /// * `id` - The agent task ID to cancel.
    ///
    /// # Returns
    ///
    /// `true` if the cancellation was successful.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use firecrawl::Client;
    ///
    /// #[tokio::main]
    /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
    ///     let client = Client::new("your-api-key")?;
    ///
    ///     let cancelled = client.cancel_agent("task-id").await?;
    ///     println!("Cancelled: {}", cancelled);
    ///
    ///     Ok(())
    /// }
    /// ```
    pub async fn cancel_agent(&self, id: impl AsRef<str>) -> Result<bool, FirecrawlError> {
        let response = self
            .client
            .delete(self.url(&format!("/agent/{}", id.as_ref())))
            .headers(self.prepare_headers(None))
            .send()
            .await
            .map_err(|e| {
                FirecrawlError::HttpError(format!("Cancelling agent {}", id.as_ref()), e)
            })?;

        #[derive(Deserialize)]
        struct CancelResponse {
            success: bool,
        }

        let result: CancelResponse = self
            .handle_response(response, format!("cancel agent {}", id.as_ref()))
            .await?;

        Ok(result.success)
    }

    /// Runs an agent with a typed schema for structured output.
    ///
    /// This is a convenience method that automatically converts the result
    /// to the specified type.
    ///
    /// # Arguments
    ///
    /// * `urls` - Starting URLs for the agent.
    /// * `prompt` - The task description.
    /// * `schema` - JSON schema for the expected output.
    ///
    /// # Returns
    ///
    /// The extracted data as the specified type, or `None` if extraction failed.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use firecrawl::Client;
    /// use serde::Deserialize;
    /// use serde_json::json;
    ///
    /// #[derive(Debug, Deserialize)]
    /// struct ProductInfo {
    ///     name: String,
    ///     price: f64,
    ///     description: Option<String>,
    /// }
    ///
    /// #[tokio::main]
    /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
    ///     let client = Client::new("your-api-key")?;
    ///
    ///     let schema = json!({
    ///         "type": "object",
    ///         "properties": {
    ///             "name": { "type": "string" },
    ///             "price": { "type": "number" },
    ///             "description": { "type": "string" }
    ///         },
    ///         "required": ["name", "price"]
    ///     });
    ///
    ///     let result: Option<ProductInfo> = client.agent_with_schema(
    ///         vec!["https://example.com/product".to_string()],
    ///         "Extract the product information",
    ///         schema,
    ///     ).await?;
    ///
    ///     if let Some(product) = result {
    ///         println!("Product: {} - ${}", product.name, product.price);
    ///     }
    ///
    ///     Ok(())
    /// }
    /// ```
    pub async fn agent_with_schema<T: serde::de::DeserializeOwned>(
        &self,
        urls: Vec<String>,
        prompt: impl AsRef<str>,
        schema: Value,
    ) -> Result<Option<T>, FirecrawlError> {
        let options = AgentOptions {
            urls: Some(urls),
            prompt: prompt.as_ref().to_string(),
            schema: Some(schema),
            ..Default::default()
        };

        let result = self.agent(options).await?;

        if result.status != AgentStatus::Completed {
            return Ok(None);
        }

        match result.data {
            Some(data) => {
                let typed: T =
                    serde_json::from_value(data).map_err(FirecrawlError::ResponseParseError)?;
                Ok(Some(typed))
            }
            None => Ok(None),
        }
    }
}

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

    #[tokio::test]
    async fn test_start_agent_with_mock() {
        let mut server = mockito::Server::new_async().await;

        let mock = server
            .mock("POST", "/v2/agent")
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(
                json!({
                    "success": true,
                    "id": "agent-123"
                })
                .to_string(),
            )
            .create();

        let client = Client::new_selfhosted(server.url(), Some("test_key")).unwrap();
        let options = AgentOptions {
            urls: Some(vec!["https://example.com".to_string()]),
            prompt: "Find the contact information".to_string(),
            ..Default::default()
        };

        let response = client.start_agent(options).await.unwrap();

        assert!(response.success);
        assert_eq!(response.id, "agent-123");
        mock.assert();
    }

    #[tokio::test]
    async fn test_start_agent_injects_sdk_origin() {
        let mut server = mockito::Server::new_async().await;

        // The mock only matches when the request body carries the SDK origin,
        // so a regression in the injection fails the request itself.
        let mock = server
            .mock("POST", "/v2/agent")
            .match_body(Matcher::PartialJson(json!({
                "origin": format!("rust-sdk@{}", env!("CARGO_PKG_VERSION"))
            })))
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(json!({"success": true, "id": "agent-123"}).to_string())
            .create();

        let client = Client::new_selfhosted(server.url(), Some("test_key")).unwrap();
        let options = AgentOptions {
            prompt: "Find the contact information".to_string(),
            ..Default::default()
        };

        let response = client.start_agent(options).await.unwrap();

        assert!(response.success);
        mock.assert();
    }

    #[tokio::test]
    async fn test_start_agent_preserves_custom_origin() {
        let mut server = mockito::Server::new_async().await;

        let mock = server
            .mock("POST", "/v2/agent")
            .match_body(Matcher::PartialJson(json!({"origin": "my-app@1.0"})))
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(json!({"success": true, "id": "agent-123"}).to_string())
            .create();

        let client = Client::new_selfhosted(server.url(), Some("test_key")).unwrap();
        let options = AgentOptions {
            prompt: "Find the contact information".to_string(),
            origin: Some("my-app@1.0".to_string()),
            ..Default::default()
        };

        let response = client.start_agent(options).await.unwrap();

        assert!(response.success);
        mock.assert();
    }

    #[test]
    fn test_trace_events_deserialize_every_type() {
        let base = json!({
            "schemaVersion": 1,
            "eventId": "018f3c5e-0000-7000-8000-000000000000",
            "runId": "018f3c5e-0000-7000-8000-000000000001",
            "occurredAt": "2026-08-26T12:00:00+00:00",
            "producerSequence": 1,
            "agent": {
                "id": "018f3c5e-0000-7000-8000-000000000002",
                "role": "orchestrator",
                "name": "main"
            }
        });
        let cases = [
            ("run.started", json!({})),
            ("run.cancel_requested", json!({"reason": "user"})),
            (
                "run.finished",
                json!({"outcome": "credit_limit_reached", "error": {
                    "code": "credit_limit_reached",
                    "source": "billing",
                    "retryable": false,
                    "message": "out of credits"
                }}),
            ),
            ("agent.started", json!({})),
            (
                "agent.finished",
                json!({"outcome": "succeeded", "durationMs": 1234, "error": null}),
            ),
            ("browser.session.started", json!({"sessionId": "sess-1"})),
            (
                "browser.session.finished",
                json!({"sessionId": "sess-1", "durationMs": 42}),
            ),
            (
                "progress.reported",
                json!({"phase": "working", "message": "reading page"}),
            ),
            ("reasoning.summary", json!({"text": "thinking"})),
            (
                "tool_call.started",
                json!({"toolCallId": "tc-1", "toolName": "scrape", "parameters": {"url": "https://example.com"}}),
            ),
            (
                "tool_call.finished",
                json!({"toolCallId": "tc-1", "toolName": "scrape", "result": {"ok": true}}),
            ),
            (
                "artifact.updated",
                json!({"artifact": {
                    "kind": "json",
                    "artifactId": "result",
                    "path": "/workspace/data.json",
                    "snapshotId": "018f3c5e-0000-7000-8000-000000000003",
                    "change": "partial",
                    "changedFields": ["price"],
                    "itemCount": 3,
                    "sourceToolCallId": "tc-1"
                }}),
            ),
            (
                "error.occurred",
                json!({"error": {
                    "code": "internal",
                    "source": "system",
                    "retryable": true,
                    "message": "boom"
                }}),
            ),
        ];

        for (event_type, extra) in cases {
            let mut value = base.clone();
            value
                .as_object_mut()
                .unwrap()
                .extend(extra.as_object().unwrap().clone());
            value["type"] = json!(event_type);

            let event: AgentTraceEvent = serde_json::from_value(value.clone())
                .unwrap_or_else(|e| panic!("{event_type} should deserialize: {e}"));
            let round_tripped = serde_json::to_value(&event).unwrap();
            assert_eq!(round_tripped["type"], json!(event_type));
        }
    }

    #[tokio::test]
    async fn test_get_agent_trace_with_mock() {
        let mut server = mockito::Server::new_async().await;

        let mock = server
            .mock("GET", "/v2/agent/agent-123/trace")
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(
                json!({
                    "success": true,
                    "id": "agent-123",
                    "events": [{
                        "schemaVersion": 1,
                        "eventId": "018f3c5e-0000-7000-8000-000000000000",
                        "runId": "018f3c5e-0000-7000-8000-000000000001",
                        "occurredAt": "2026-08-26T12:00:00+00:00",
                        "producerSequence": 1,
                        "agent": {
                            "id": "018f3c5e-0000-7000-8000-000000000002",
                            "role": "orchestrator",
                            "name": "main"
                        },
                        "type": "run.started"
                    }],
                    "creditsUsed": 5
                })
                .to_string(),
            )
            .create();

        let client = Client::new_selfhosted(server.url(), Some("test_key")).unwrap();
        let trace = client.get_agent_trace("agent-123", false).await.unwrap();

        assert!(trace.success);
        let events = trace.events.unwrap();
        assert_eq!(events.len(), 1);
        assert!(matches!(events[0], AgentTraceEvent::RunStarted(_)));
        assert_eq!(trace.credits_used, Some(5));
        assert!(trace.active_browser_sessions.is_none());
        mock.assert();
    }

    #[tokio::test]
    async fn test_get_agent_trace_with_live_view() {
        let mut server = mockito::Server::new_async().await;

        let mock = server
            .mock("GET", "/v2/agent/agent-123/trace")
            .match_query(Matcher::UrlEncoded("liveView".into(), "true".into()))
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(
                json!({
                    "success": true,
                    "id": "agent-123",
                    "events": [],
                    "creditsUsed": 0,
                    "activeBrowserSessions": [{
                        "id": "sess-1",
                        "liveViewUrl": "https://browser.example.com/sess-1",
                        "viewport": {"width": 1280, "height": 720}
                    }]
                })
                .to_string(),
            )
            .create();

        let client = Client::new_selfhosted(server.url(), Some("test_key")).unwrap();
        let trace = client.get_agent_trace("agent-123", true).await.unwrap();

        let sessions = trace.active_browser_sessions.unwrap();
        assert_eq!(sessions.len(), 1);
        assert_eq!(sessions[0].viewport.width, 1280);
        mock.assert();
    }

    #[tokio::test]
    async fn test_get_agent_snapshot_with_mock() {
        let mut server = mockito::Server::new_async().await;

        let mock = server
            .mock(
                "GET",
                "/v2/agent/agent-123/snapshots/018f3c5e-0000-7000-8000-000000000003",
            )
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(
                json!({
                    "success": true,
                    "id": "agent-123",
                    "snapshotId": "018f3c5e-0000-7000-8000-000000000003",
                    "snapshot": "{\"price\": 42}"
                })
                .to_string(),
            )
            .create();

        let client = Client::new_selfhosted(server.url(), Some("test_key")).unwrap();
        let snapshot = client
            .get_agent_snapshot("agent-123", "018f3c5e-0000-7000-8000-000000000003")
            .await
            .unwrap();

        assert!(snapshot.success);
        assert_eq!(snapshot.snapshot.unwrap(), "{\"price\": 42}");
        mock.assert();
    }

    #[tokio::test]
    async fn test_start_agent_sends_effort() {
        let mut server = mockito::Server::new_async().await;

        let mock = server
            .mock("POST", "/v2/agent")
            .match_body(Matcher::PartialJson(json!({"effort": "high"})))
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(json!({"success": true, "id": "agent-123"}).to_string())
            .create();

        let client = Client::new_selfhosted(server.url(), Some("test_key")).unwrap();
        let options = AgentOptions {
            prompt: "Find the contact information".to_string(),
            effort: Some(AgentEffort::High),
            ..Default::default()
        };

        let response = client.start_agent(options).await.unwrap();

        assert!(response.success);
        mock.assert();
    }

    #[tokio::test]
    async fn test_get_agent_status_with_mock() {
        let mut server = mockito::Server::new_async().await;

        let mock = server
            .mock("GET", "/v2/agent/agent-123")
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(
                json!({
                    "success": true,
                    "status": "completed",
                    "data": {
                        "email": "contact@example.com",
                        "phone": "555-1234"
                    },
                    "creditsUsed": 5,
                    "expiresAt": "2024-12-31T23:59:59Z"
                })
                .to_string(),
            )
            .create();

        let client = Client::new_selfhosted(server.url(), Some("test_key")).unwrap();
        let status = client.get_agent_status("agent-123").await.unwrap();

        assert!(status.success);
        assert_eq!(status.status, AgentStatus::Completed);
        assert!(status.data.is_some());
        assert_eq!(status.credits_used, Some(5));
        mock.assert();
    }

    #[tokio::test]
    async fn test_agent_sync_with_mock() {
        let mut server = mockito::Server::new_async().await;

        // Mock the start endpoint
        let start_mock = server
            .mock("POST", "/v2/agent")
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(
                json!({
                    "success": true,
                    "id": "agent-456"
                })
                .to_string(),
            )
            .create();

        // Mock the status endpoint (completed immediately)
        let status_mock = server
            .mock("GET", "/v2/agent/agent-456")
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(
                json!({
                    "success": true,
                    "status": "completed",
                    "data": {
                        "result": "Task completed successfully"
                    }
                })
                .to_string(),
            )
            .create();

        let client = Client::new_selfhosted(server.url(), Some("test_key")).unwrap();
        let options = AgentOptions {
            urls: Some(vec!["https://example.com".to_string()]),
            prompt: "Test task".to_string(),
            ..Default::default()
        };

        let result = client.agent(options).await.unwrap();

        assert_eq!(result.status, AgentStatus::Completed);
        assert!(result.data.is_some());
        start_mock.assert();
        status_mock.assert();
    }

    #[tokio::test]
    async fn test_cancel_agent_with_mock() {
        let mut server = mockito::Server::new_async().await;

        let mock = server
            .mock("DELETE", "/v2/agent/agent-789")
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(
                json!({
                    "success": true
                })
                .to_string(),
            )
            .create();

        let client = Client::new_selfhosted(server.url(), Some("test_key")).unwrap();
        let cancelled = client.cancel_agent("agent-789").await.unwrap();

        assert!(cancelled);
        mock.assert();
    }

    #[tokio::test]
    async fn test_agent_with_schema() {
        let mut server = mockito::Server::new_async().await;

        // Mock the start endpoint
        let start_mock = server
            .mock("POST", "/v2/agent")
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(
                json!({
                    "success": true,
                    "id": "agent-schema"
                })
                .to_string(),
            )
            .create();

        // Mock the status endpoint
        let status_mock = server
            .mock("GET", "/v2/agent/agent-schema")
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(
                json!({
                    "success": true,
                    "status": "completed",
                    "data": {
                        "name": "Test Product",
                        "price": 29.99
                    }
                })
                .to_string(),
            )
            .create();

        #[derive(Debug, serde::Deserialize, PartialEq)]
        struct Product {
            name: String,
            price: f64,
        }

        let client = Client::new_selfhosted(server.url(), Some("test_key")).unwrap();

        let schema = json!({
            "type": "object",
            "properties": {
                "name": { "type": "string" },
                "price": { "type": "number" }
            }
        });

        let result: Option<Product> = client
            .agent_with_schema(
                vec!["https://example.com".to_string()],
                "Extract product info",
                schema,
            )
            .await
            .unwrap();

        assert_eq!(
            result,
            Some(Product {
                name: "Test Product".to_string(),
                price: 29.99
            })
        );
        start_mock.assert();
        status_mock.assert();
    }

    #[tokio::test]
    async fn test_agent_with_model_option() {
        let mut server = mockito::Server::new_async().await;

        let mock = server
            .mock("POST", "/v2/agent")
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(
                json!({
                    "success": true,
                    "id": "agent-model"
                })
                .to_string(),
            )
            .create();

        let client = Client::new_selfhosted(server.url(), Some("test_key")).unwrap();
        let options = AgentOptions {
            urls: Some(vec!["https://example.com".to_string()]),
            prompt: "Task with specific model".to_string(),
            model: Some(AgentModel::Spark1Pro),
            max_credits: Some(100),
            ..Default::default()
        };

        let response = client.start_agent(options).await.unwrap();

        assert!(response.success);
        mock.assert();
    }
}