spider_agent_types 2.51.125

Pure data types and constants for spider_agent automation. Zero heavy dependencies.
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
//! Page observation types for understanding page state.

use crate::AutomationUsage;

/// Observation of a page's current state.
///
/// Used by the agent to understand what's on the page and what actions
/// are available.
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
pub struct PageObservation {
    /// Current page URL.
    pub url: String,
    /// Page title.
    pub title: String,
    /// Brief description of the page.
    pub description: String,
    /// Type of page (login, search, product, etc.).
    pub page_type: String,
    /// Interactive elements on the page.
    #[serde(default)]
    pub interactive_elements: Vec<InteractiveElement>,
    /// Forms on the page.
    #[serde(default)]
    pub forms: Vec<FormInfo>,
    /// Navigation options.
    #[serde(default)]
    pub navigation: Vec<NavigationOption>,
    /// Suggested next actions.
    #[serde(default)]
    pub suggested_actions: Vec<String>,
    /// Screenshot (base64).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub screenshot: Option<String>,
    /// Token usage for this observation.
    #[serde(default)]
    pub usage: AutomationUsage,
    /// Raw HTML (truncated).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub html: Option<String>,
    /// Page text content.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub text_content: Option<String>,
}

impl PageObservation {
    /// Create a new observation.
    pub fn new(url: impl Into<String>) -> Self {
        Self {
            url: url.into(),
            ..Default::default()
        }
    }

    /// Set title.
    pub fn with_title(mut self, title: impl Into<String>) -> Self {
        self.title = title.into();
        self
    }

    /// Set description.
    pub fn with_description(mut self, desc: impl Into<String>) -> Self {
        self.description = desc.into();
        self
    }

    /// Set page type.
    pub fn with_page_type(mut self, page_type: impl Into<String>) -> Self {
        self.page_type = page_type.into();
        self
    }

    /// Add an interactive element.
    pub fn add_element(mut self, element: InteractiveElement) -> Self {
        self.interactive_elements.push(element);
        self
    }

    /// Add a form.
    pub fn add_form(mut self, form: FormInfo) -> Self {
        self.forms.push(form);
        self
    }

    /// Add a navigation option.
    pub fn add_navigation(mut self, nav: NavigationOption) -> Self {
        self.navigation.push(nav);
        self
    }

    /// Add a suggested action.
    pub fn suggest_action(mut self, action: impl Into<String>) -> Self {
        self.suggested_actions.push(action.into());
        self
    }

    /// Set screenshot.
    pub fn with_screenshot(mut self, screenshot: impl Into<String>) -> Self {
        self.screenshot = Some(screenshot.into());
        self
    }

    /// Set usage.
    pub fn with_usage(mut self, usage: AutomationUsage) -> Self {
        self.usage = usage;
        self
    }

    /// Find an element by selector.
    pub fn find_element(&self, selector: &str) -> Option<&InteractiveElement> {
        self.interactive_elements
            .iter()
            .find(|e| e.selector == selector)
    }

    /// Find elements by type.
    pub fn elements_by_type(&self, element_type: &str) -> Vec<&InteractiveElement> {
        self.interactive_elements
            .iter()
            .filter(|e| e.element_type == element_type)
            .collect()
    }

    /// Get clickable elements.
    pub fn clickable_elements(&self) -> Vec<&InteractiveElement> {
        self.interactive_elements
            .iter()
            .filter(|e| {
                e.visible && e.enabled && (e.element_type == "button" || e.element_type == "link")
            })
            .collect()
    }

    /// Get form inputs.
    pub fn input_elements(&self) -> Vec<&InteractiveElement> {
        self.interactive_elements
            .iter()
            .filter(|e| e.element_type.starts_with("input") || e.element_type == "textarea")
            .collect()
    }
}

/// An interactive element on the page.
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
pub struct InteractiveElement {
    /// CSS selector for this element.
    pub selector: String,
    /// Type of element (button, input, link, etc.).
    pub element_type: String,
    /// Visible text content.
    pub text: String,
    /// Description of the element's purpose.
    pub description: String,
    /// Whether the element is visible.
    pub visible: bool,
    /// Whether the element is enabled.
    pub enabled: bool,
    /// Element tag name.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tag: Option<String>,
    /// Element ID.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub id: Option<String>,
    /// Element classes.
    #[serde(default)]
    pub classes: Vec<String>,
    /// ARIA label.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub aria_label: Option<String>,
    /// Placeholder text (for inputs).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub placeholder: Option<String>,
    /// Current value (for inputs).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value: Option<String>,
    /// Bounding box [x, y, width, height].
    #[serde(skip_serializing_if = "Option::is_none")]
    pub bounds: Option<[f64; 4]>,
}

impl InteractiveElement {
    /// Create a new interactive element.
    pub fn new(selector: impl Into<String>, element_type: impl Into<String>) -> Self {
        Self {
            selector: selector.into(),
            element_type: element_type.into(),
            visible: true,
            enabled: true,
            ..Default::default()
        }
    }

    /// Create a button element.
    pub fn button(selector: impl Into<String>, text: impl Into<String>) -> Self {
        Self {
            selector: selector.into(),
            element_type: "button".to_string(),
            text: text.into(),
            visible: true,
            enabled: true,
            ..Default::default()
        }
    }

    /// Create an input element.
    pub fn input(selector: impl Into<String>, input_type: &str) -> Self {
        Self {
            selector: selector.into(),
            element_type: format!("input:{}", input_type),
            visible: true,
            enabled: true,
            ..Default::default()
        }
    }

    /// Create a link element.
    pub fn link(selector: impl Into<String>, text: impl Into<String>) -> Self {
        Self {
            selector: selector.into(),
            element_type: "link".to_string(),
            text: text.into(),
            visible: true,
            enabled: true,
            ..Default::default()
        }
    }

    /// Set text.
    pub fn with_text(mut self, text: impl Into<String>) -> Self {
        self.text = text.into();
        self
    }

    /// Set description.
    pub fn with_description(mut self, desc: impl Into<String>) -> Self {
        self.description = desc.into();
        self
    }

    /// Set visibility.
    pub fn visible(mut self, visible: bool) -> Self {
        self.visible = visible;
        self
    }

    /// Set enabled state.
    pub fn enabled(mut self, enabled: bool) -> Self {
        self.enabled = enabled;
        self
    }

    /// Set placeholder.
    pub fn with_placeholder(mut self, placeholder: impl Into<String>) -> Self {
        self.placeholder = Some(placeholder.into());
        self
    }

    /// Set aria label.
    pub fn with_aria_label(mut self, label: impl Into<String>) -> Self {
        self.aria_label = Some(label.into());
        self
    }

    /// Check if this element can be interacted with.
    pub fn is_actionable(&self) -> bool {
        self.visible && self.enabled
    }

    /// Get a human-readable description.
    pub fn describe(&self) -> String {
        if !self.description.is_empty() {
            return self.description.clone();
        }
        if !self.text.is_empty() {
            return format!("{}: {}", self.element_type, self.text);
        }
        if let Some(ref label) = self.aria_label {
            return format!("{}: {}", self.element_type, label);
        }
        if let Some(ref placeholder) = self.placeholder {
            return format!("{}: {}", self.element_type, placeholder);
        }
        format!("{} at {}", self.element_type, self.selector)
    }
}

/// Information about a form on the page.
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
pub struct FormInfo {
    /// CSS selector for the form.
    pub selector: String,
    /// Form name attribute.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    /// Form action URL.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub action: Option<String>,
    /// Form method (GET, POST).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub method: Option<String>,
    /// Form fields.
    #[serde(default)]
    pub fields: Vec<FormField>,
    /// Description of what this form does.
    pub description: String,
}

impl FormInfo {
    /// Create a new form info.
    pub fn new(selector: impl Into<String>) -> Self {
        Self {
            selector: selector.into(),
            ..Default::default()
        }
    }

    /// Set name.
    pub fn with_name(mut self, name: impl Into<String>) -> Self {
        self.name = Some(name.into());
        self
    }

    /// Set action.
    pub fn with_action(mut self, action: impl Into<String>) -> Self {
        self.action = Some(action.into());
        self
    }

    /// Add a field.
    pub fn add_field(mut self, field: FormField) -> Self {
        self.fields.push(field);
        self
    }

    /// Get required fields.
    pub fn required_fields(&self) -> Vec<&FormField> {
        self.fields.iter().filter(|f| f.required).collect()
    }

    /// Get empty required fields.
    pub fn empty_required_fields(&self) -> Vec<&FormField> {
        self.fields
            .iter()
            .filter(|f| f.required && f.value.as_ref().map(|v| v.is_empty()).unwrap_or(true))
            .collect()
    }
}

/// A field in a form.
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
pub struct FormField {
    /// Field name attribute.
    pub name: String,
    /// Field type (text, email, password, etc.).
    pub field_type: String,
    /// Field label text.
    pub label: String,
    /// Whether the field is required.
    pub required: bool,
    /// Current value.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value: Option<String>,
    /// Placeholder text.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub placeholder: Option<String>,
    /// CSS selector.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub selector: Option<String>,
    /// Options for select fields.
    #[serde(default)]
    pub options: Vec<String>,
}

impl FormField {
    /// Create a new form field.
    pub fn new(name: impl Into<String>, field_type: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            field_type: field_type.into(),
            ..Default::default()
        }
    }

    /// Set label.
    pub fn with_label(mut self, label: impl Into<String>) -> Self {
        self.label = label.into();
        self
    }

    /// Set required.
    pub fn required(mut self) -> Self {
        self.required = true;
        self
    }

    /// Set selector.
    pub fn with_selector(mut self, selector: impl Into<String>) -> Self {
        self.selector = Some(selector.into());
        self
    }

    /// Set current value.
    pub fn with_value(mut self, value: impl Into<String>) -> Self {
        self.value = Some(value.into());
        self
    }
}

/// Result of a single action execution via `act()`.
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
pub struct ActResult {
    /// Whether the action was executed successfully.
    pub success: bool,
    /// Description of the action that was taken.
    pub action_taken: String,
    /// The specific action executed (if any).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub action_type: Option<String>,
    /// Base64-encoded screenshot after the action.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub screenshot: Option<String>,
    /// Error message if the action failed.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub error: Option<String>,
    /// Token usage for this action.
    #[serde(default)]
    pub usage: AutomationUsage,
}

impl ActResult {
    /// Create a successful action result.
    pub fn success(action_taken: impl Into<String>) -> Self {
        Self {
            success: true,
            action_taken: action_taken.into(),
            ..Default::default()
        }
    }

    /// Create a failed action result.
    pub fn failure(error: impl Into<String>) -> Self {
        Self {
            success: false,
            error: Some(error.into()),
            ..Default::default()
        }
    }

    /// Set the action type.
    pub fn with_action_type(mut self, action_type: impl Into<String>) -> Self {
        self.action_type = Some(action_type.into());
        self
    }

    /// Set the screenshot.
    pub fn with_screenshot(mut self, screenshot: impl Into<String>) -> Self {
        self.screenshot = Some(screenshot.into());
        self
    }

    /// Set usage stats.
    pub fn with_usage(mut self, usage: AutomationUsage) -> Self {
        self.usage = usage;
        self
    }
}

/// A navigation option on the page.
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
pub struct NavigationOption {
    /// Link text.
    pub text: String,
    /// Target URL.
    pub url: String,
    /// CSS selector.
    pub selector: String,
    /// Whether this is the current page.
    pub is_current: bool,
    /// Category (main nav, footer, sidebar, etc.).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub category: Option<String>,
}

impl NavigationOption {
    /// Create a new navigation option.
    pub fn new(
        text: impl Into<String>,
        url: impl Into<String>,
        selector: impl Into<String>,
    ) -> Self {
        Self {
            text: text.into(),
            url: url.into(),
            selector: selector.into(),
            is_current: false,
            category: None,
        }
    }

    /// Mark as current page.
    pub fn current(mut self) -> Self {
        self.is_current = true;
        self
    }

    /// Set category.
    pub fn with_category(mut self, category: impl Into<String>) -> Self {
        self.category = Some(category.into());
        self
    }
}

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

    #[test]
    fn test_page_observation() {
        let obs = PageObservation::new("https://example.com")
            .with_title("Example")
            .with_page_type("homepage")
            .add_element(InteractiveElement::button("btn.login", "Login"))
            .add_element(InteractiveElement::input("input.email", "email"))
            .suggest_action("Click login button");

        assert_eq!(obs.clickable_elements().len(), 1);
        assert_eq!(obs.input_elements().len(), 1);
        assert_eq!(obs.suggested_actions.len(), 1);
    }

    #[test]
    fn test_interactive_element() {
        let elem = InteractiveElement::button("button.submit", "Submit")
            .with_description("Submit the form")
            .with_aria_label("Submit form");

        assert!(elem.is_actionable());
        assert_eq!(elem.describe(), "Submit the form");

        let disabled = elem.clone().enabled(false);
        assert!(!disabled.is_actionable());
    }

    #[test]
    fn test_form_info() {
        let form = FormInfo::new("form#login")
            .with_action("/login")
            .add_field(
                FormField::new("email", "email")
                    .required()
                    .with_label("Email"),
            )
            .add_field(FormField::new("password", "password").required());

        assert_eq!(form.required_fields().len(), 2);
        assert_eq!(form.empty_required_fields().len(), 2);
    }
}