Skip to main content

eoka_protocol/
args.rs

1use serde::{Deserialize, Serialize};
2
3#[derive(Debug, Clone, Serialize, Deserialize, Default, schemars::JsonSchema)]
4pub struct OpenArgs {
5    #[serde(flatten)]
6    pub operation: crate::ActionSettings,
7    pub url: String,
8    pub headers: Option<serde_json::Value>,
9    pub user_agent: Option<String>,
10    #[serde(default)]
11    pub bypass_csp: bool,
12    pub inject_js: Option<String>,
13    pub load_state: Option<String>,
14}
15
16#[derive(Debug, Clone, Serialize, Deserialize, Default, schemars::JsonSchema)]
17pub struct SnapshotArgs {
18    #[serde(default)]
19    pub interactive: bool,
20    #[serde(default)]
21    pub all: bool,
22}
23
24#[derive(Debug, Clone, Serialize, Deserialize, Default, schemars::JsonSchema)]
25pub struct ObserveArgs {
26    #[schemars(
27        description = "Filter: 'inputs' for form elements, 'buttons' for buttons and links, or 'all' for all observed elements."
28    )]
29    pub filter: Option<String>,
30    #[schemars(description = "Maximum elements to return.")]
31    pub max: Option<usize>,
32    #[serde(default)]
33    pub structured: bool,
34}
35
36#[derive(Debug, Clone, Serialize, Deserialize, Default, schemars::JsonSchema)]
37pub struct ScreenshotArgs {
38    pub output: Option<String>,
39    #[serde(default)]
40    pub annotate: bool,
41    #[serde(default)]
42    #[schemars(description = "Image format; PNG when omitted.")]
43    pub format: Option<crate::sidecar::ScreenshotFormat>,
44    #[serde(default)]
45    #[schemars(range(max = 100), description = "JPEG or WebP quality, 0 to 100.")]
46    pub quality: Option<u8>,
47    #[serde(default)]
48    #[schemars(description = "Document-relative CSS pixel region; conflicts with full_page.")]
49    pub clip: Option<crate::sidecar::ScreenshotClip>,
50    #[serde(default)]
51    #[schemars(description = "Capture the whole scrollable page instead of the viewport.")]
52    pub full_page: bool,
53    #[serde(default)]
54    #[schemars(
55        description = "Return the image as base64 dataBase64 instead of writing a file; conflicts with output."
56    )]
57    pub inline: bool,
58}
59
60impl ScreenshotArgs {
61    pub fn validate(&self) -> Result<(), crate::sidecar::ServerError> {
62        crate::sidecar::validate_screenshot(
63            self.format,
64            self.quality,
65            self.clip.as_ref(),
66            Some(self.full_page),
67        )?;
68        if self.inline && self.output.is_some() {
69            return Err(crate::sidecar::ServerError::invalid_params(
70                "inline cannot be combined with output",
71            ));
72        }
73        if self.annotate
74            && (self.format.is_some()
75                || self.quality.is_some()
76                || self.clip.is_some()
77                || self.full_page)
78        {
79            return Err(crate::sidecar::ServerError::invalid_params(
80                "annotate cannot be combined with format, quality, clip or full page",
81            ));
82        }
83        Ok(())
84    }
85
86    pub fn options(&self) -> eoka::ScreenshotOptions {
87        crate::sidecar::screenshot_options(
88            self.format,
89            self.quality,
90            self.clip.as_ref(),
91            Some(self.full_page),
92        )
93    }
94
95    pub fn mime_type(&self) -> &'static str {
96        match self.format.unwrap_or(crate::sidecar::ScreenshotFormat::Png) {
97            crate::sidecar::ScreenshotFormat::Png => "image/png",
98            crate::sidecar::ScreenshotFormat::Jpeg => "image/jpeg",
99            crate::sidecar::ScreenshotFormat::Webp => "image/webp",
100        }
101    }
102
103    pub fn extension(&self) -> &'static str {
104        match self.format.unwrap_or(crate::sidecar::ScreenshotFormat::Png) {
105            crate::sidecar::ScreenshotFormat::Png => "png",
106            crate::sidecar::ScreenshotFormat::Jpeg => "jpg",
107            crate::sidecar::ScreenshotFormat::Webp => "webp",
108        }
109    }
110}
111
112#[derive(Debug, Clone, Serialize, Deserialize, Default, schemars::JsonSchema)]
113pub struct RecordingStartArgs {
114    #[schemars(description = "Absolute path of the MP4 file to write.")]
115    pub path: String,
116    #[serde(default)]
117    #[schemars(description = "UUID naming this recording; generated when omitted.")]
118    pub recording_id: Option<String>,
119    #[serde(default)]
120    pub max_width: Option<u32>,
121    #[serde(default)]
122    pub max_height: Option<u32>,
123    #[serde(default)]
124    #[schemars(range(min = 1, max = 120))]
125    pub fps: Option<u32>,
126}
127
128#[derive(Debug, Clone, Serialize, Deserialize, Default, schemars::JsonSchema)]
129pub struct RecordingIdArgs {
130    #[schemars(description = "Recording UUID returned by recording_start.")]
131    pub recording_id: String,
132}
133
134#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
135pub struct EmulateArgs {
136    #[serde(default = "default_viewport_width")]
137    pub width: u32,
138    #[serde(default = "default_viewport_height")]
139    pub height: u32,
140    #[serde(default = "default_device_pixel_ratio")]
141    pub dpr: f64,
142    #[serde(default)]
143    pub desktop: bool,
144    #[serde(default)]
145    pub reset: bool,
146}
147
148impl Default for EmulateArgs {
149    fn default() -> Self {
150        Self {
151            width: default_viewport_width(),
152            height: default_viewport_height(),
153            dpr: default_device_pixel_ratio(),
154            desktop: false,
155            reset: false,
156        }
157    }
158}
159
160#[derive(Debug, Clone, Serialize, Deserialize, Default, schemars::JsonSchema)]
161pub struct TextArgs {
162    #[schemars(description = "Text substring to search for, matched case-insensitively.")]
163    pub text: String,
164}
165
166#[derive(Debug, Clone, Serialize, Deserialize, Default, schemars::JsonSchema)]
167pub struct TargetArgs {
168    #[serde(flatten)]
169    pub operation: crate::ActionSettings,
170    #[schemars(
171        description = "Target element. Supports scoped refs from view, text:Submit, placeholder:Email, role:button, css:form button, id:my-btn, or plain text search."
172    )]
173    pub target: String,
174}
175
176#[derive(Debug, Clone, Serialize, Deserialize, Default, schemars::JsonSchema)]
177pub struct ClickArgs {
178    #[serde(flatten)]
179    pub element: TargetArgs,
180    #[serde(default, skip_serializing_if = "Option::is_none")]
181    pub intercept_nav: Option<NavIntercept>,
182}
183
184#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
185#[serde(rename_all = "lowercase")]
186#[schemars(
187    description = "Install a history.pushState/replaceState diagnostic before the click and record the attempted navigation in window.__eoka_nav. block keeps the page where it is; allow lets the history change through. Requires expect; location navigation cannot be intercepted."
188)]
189pub enum NavIntercept {
190    Block,
191    Allow,
192}
193
194#[derive(Debug, Clone, Serialize, Deserialize, Default, schemars::JsonSchema)]
195pub struct HistoryGoArgs {
196    #[serde(flatten)]
197    pub operation: crate::ActionSettings,
198    #[schemars(description = "History delta: -1 for back, 1 for forward, -2 for back twice, etc.")]
199    pub delta: i32,
200}
201
202#[derive(Debug, Clone, Serialize, Deserialize, Default, schemars::JsonSchema)]
203pub struct FillArgs {
204    #[serde(flatten)]
205    pub operation: crate::ActionSettings,
206    #[schemars(
207        description = "Target input. Supports scoped refs from view, text:Email, placeholder:Enter code, css:input.search, id:email-field, or plain text search."
208    )]
209    pub target: String,
210    #[schemars(description = "Text to type into the element.")]
211    pub text: String,
212    #[serde(default)]
213    pub input_mode: crate::InputMode,
214}
215
216#[derive(Debug, Clone, Serialize, Deserialize, Default, schemars::JsonSchema)]
217pub struct SelectArgs {
218    #[serde(flatten)]
219    pub operation: crate::ActionSettings,
220    #[schemars(
221        description = "Target select element by scoped reference, text, CSS selector, id, or role."
222    )]
223    pub target: String,
224    #[schemars(description = "Option value or visible text to select.")]
225    pub value: String,
226}
227
228#[derive(Debug, Clone, Serialize, Deserialize, Default, schemars::JsonSchema)]
229pub struct KeyArgs {
230    #[serde(flatten)]
231    pub operation: crate::ActionSettings,
232    #[schemars(
233        description = "Key to press, for example Enter, Tab, Escape, ArrowDown, or Backspace."
234    )]
235    pub key: String,
236}
237
238#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
239pub struct HeldKeyArgs {
240    #[schemars(description = "Key to hold or release, for example Shift, Control, or a.")]
241    pub key: String,
242}
243
244#[derive(Debug, Clone, Copy, Serialize, Deserialize, schemars::JsonSchema)]
245#[serde(rename_all = "lowercase")]
246pub enum MouseButton {
247    Left,
248    Middle,
249    Right,
250    Back,
251    Forward,
252}
253
254#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
255pub struct MouseButtonArgs {
256    #[schemars(description = "Viewport horizontal coordinate in CSS pixels.")]
257    pub x: f64,
258    #[schemars(description = "Viewport vertical coordinate in CSS pixels.")]
259    pub y: f64,
260    #[schemars(description = "Mouse button to hold or release.")]
261    pub button: MouseButton,
262}
263
264#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
265pub struct MouseMoveArgs {
266    #[schemars(description = "Viewport horizontal coordinate in CSS pixels.")]
267    pub x: f64,
268    #[schemars(description = "Viewport vertical coordinate in CSS pixels.")]
269    pub y: f64,
270}
271
272#[derive(Debug, Clone, Serialize, Deserialize, Default, schemars::JsonSchema)]
273pub struct ScriptArgs {
274    #[serde(default)]
275    pub arguments: Vec<serde_json::Value>,
276    #[serde(default)]
277    pub raw: bool,
278    pub code: Option<String>,
279    pub file: Option<String>,
280    pub max_size: Option<usize>,
281    #[serde(default)]
282    pub no_await: bool,
283    #[serde(default)]
284    #[schemars(
285        description = "Treat code as function source and call it with arguments as written, without expression or statement wrapping; conflicts with raw."
286    )]
287    pub function: bool,
288    #[serde(default)]
289    #[schemars(
290        description = "With raw, return the expression's value as strict JSON, failing on values JSON cannot represent."
291    )]
292    pub json: bool,
293}
294
295#[derive(Debug, Clone, Serialize, Deserialize, Default, schemars::JsonSchema)]
296pub struct FrameEvalArgs {
297    #[schemars(
298        description = "CSS selector matching exactly one iframe/frame in the top document, or id:<frame-id> from frames for explicit/nested selection. Bare URLs and indices are not supported. Evaluation uses an isolated world, not page-owned JavaScript globals."
299    )]
300    pub frame: String,
301    pub code: Option<String>,
302    pub file: Option<String>,
303}
304
305#[derive(Debug, Clone, Serialize, Deserialize, Default, schemars::JsonSchema)]
306pub struct FetchArgs {
307    pub url: String,
308    pub method: Option<String>,
309    pub headers: Option<serde_json::Value>,
310    pub body: Option<String>,
311    pub redirect: Option<String>,
312    #[serde(default)]
313    pub body_only: bool,
314    #[schemars(
315        description = "Raw text byte limit: default 8192, maximum 16777216; zero inspects headers only.",
316        range(max = 16777216)
317    )]
318    pub max_body: Option<usize>,
319}
320
321#[derive(Debug, Clone, Serialize, Deserialize, Default, schemars::JsonSchema)]
322pub struct SetCookieArgs {
323    pub name: String,
324    pub value: String,
325    pub domain: Option<String>,
326    pub path: Option<String>,
327}
328
329#[derive(Debug, Clone, Serialize, Deserialize, Default, schemars::JsonSchema)]
330pub struct DeleteCookieArgs {
331    pub name: String,
332    pub domain: Option<String>,
333}
334
335#[derive(Debug, Clone, Serialize, Deserialize, Default, schemars::JsonSchema)]
336pub struct StorageArgs {
337    pub key: Option<String>,
338    #[serde(default)]
339    pub session_storage: bool,
340}
341
342#[derive(Debug, Clone, Serialize, Deserialize, Default, schemars::JsonSchema)]
343pub struct SetStorageArgs {
344    pub key: String,
345    pub value: String,
346    #[serde(default)]
347    pub session_storage: bool,
348}
349
350#[derive(Debug, Clone, Serialize, Deserialize, Default, schemars::JsonSchema)]
351pub struct PathArgs {
352    #[schemars(description = "File path.")]
353    pub path: String,
354}
355
356#[derive(Debug, Clone, Serialize, Deserialize, Default, schemars::JsonSchema)]
357pub struct LoadStateArgs {
358    pub path: String,
359    #[serde(default)]
360    pub no_navigate: bool,
361}
362
363#[derive(Debug, Clone, Serialize, Deserialize, Default, schemars::JsonSchema)]
364pub struct HeadersArgs {
365    pub headers_json: String,
366}
367
368#[derive(Debug, Clone, Serialize, Deserialize, Default, schemars::JsonSchema)]
369pub struct ConsoleArgs {
370    #[serde(default)]
371    pub clear: bool,
372    pub level: Option<String>,
373}
374
375#[derive(Debug, Clone, Serialize, Deserialize, Default, schemars::JsonSchema)]
376pub struct ClearFlagArgs {
377    #[serde(default)]
378    pub clear: bool,
379}
380
381#[derive(Debug, Clone, Serialize, Deserialize, Default, schemars::JsonSchema)]
382pub struct TabNewArgs {
383    #[schemars(description = "Optional URL to navigate to. If omitted, opens a blank tab.")]
384    pub url: Option<String>,
385}
386
387#[derive(Debug, Clone, Serialize, Deserialize, Default, schemars::JsonSchema)]
388pub struct TabIdArgs {
389    #[schemars(description = "Tab ID from list_tabs.")]
390    pub tab_id: String,
391}
392
393#[derive(Debug, Clone, Serialize, Deserialize, Default, schemars::JsonSchema)]
394pub struct CloneFromArgs {
395    pub source: String,
396    pub to: Option<String>,
397}
398
399#[derive(Debug, Clone, Serialize, Deserialize, Default, schemars::JsonSchema)]
400pub struct WaitArgs {
401    #[serde(default)]
402    pub expect: Option<crate::Expectation>,
403    pub ms: Option<u64>,
404    pub text: Option<String>,
405    pub url: Option<String>,
406    pub load: Option<String>,
407    #[serde(default)]
408    #[schemars(description = "CSS selector to wait for; see `state`.")]
409    pub selector: Option<String>,
410    #[serde(default)]
411    #[schemars(description = "What the selector must become. Defaults to visible.")]
412    pub state: Option<WaitSelectorState>,
413    #[serde(default)]
414    #[schemars(
415        description = "Wait until no XHR or fetch request has run for this many milliseconds."
416    )]
417    pub network_idle: Option<u64>,
418    pub timeout: Option<u64>,
419}
420
421#[derive(
422    Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default, schemars::JsonSchema,
423)]
424#[serde(rename_all = "lowercase")]
425#[schemars(description = "The condition a `wait` selector must reach.")]
426pub enum WaitSelectorState {
427    #[schemars(description = "Present in the DOM, rendered or not.")]
428    Attached,
429    #[schemars(description = "Rendered with visible CSS and a nonzero box.")]
430    #[default]
431    Visible,
432    #[schemars(description = "Absent or no longer rendered.")]
433    Hidden,
434}
435
436#[derive(Debug, Clone, Serialize, Deserialize, Default, schemars::JsonSchema)]
437pub struct DialogAcceptArgs {
438    #[serde(default)]
439    #[schemars(
440        description = "Text to enter for a prompt() dialog; ignored for alert and confirm."
441    )]
442    pub prompt_text: Option<String>,
443}
444
445#[derive(Debug, Clone, Serialize, Deserialize, Default, schemars::JsonSchema)]
446pub struct SelectorArgs {
447    #[schemars(description = "CSS selector of the element.")]
448    pub selector: String,
449}
450
451#[derive(Debug, Clone, Serialize, Deserialize, Default, schemars::JsonSchema)]
452pub struct AttributeArgs {
453    #[schemars(description = "CSS selector of the element.")]
454    pub selector: String,
455    #[schemars(description = "Attribute name, for example href.")]
456    pub name: String,
457}
458
459#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
460pub struct ClickShadowArgs {
461    #[serde(deserialize_with = "shadow_selector")]
462    #[schemars(description = "CSS selector, matched through open and closed shadow roots.")]
463    pub selector: String,
464    #[serde(default)]
465    #[schemars(description = "Frame ID from `frames`; the top frame when omitted.")]
466    pub frame_id: Option<String>,
467    #[serde(
468        default = "default_click_shadow_timeout_ms",
469        deserialize_with = "shadow_timeout"
470    )]
471    pub timeout_ms: u64,
472}
473
474fn default_click_shadow_timeout_ms() -> u64 {
475    10_000
476}
477
478fn shadow_selector<'de, D: serde::Deserializer<'de>>(d: D) -> Result<String, D::Error> {
479    let selector = String::deserialize(d)?;
480    if selector.trim().is_empty() || selector.len() > 4096 {
481        return Err(serde::de::Error::custom(
482            "selector must be 1..=4096 characters",
483        ));
484    }
485    Ok(selector)
486}
487
488fn shadow_timeout<'de, D: serde::Deserializer<'de>>(d: D) -> Result<u64, D::Error> {
489    let timeout = u64::deserialize(d)?;
490    if !(1..=60_000).contains(&timeout) {
491        return Err(serde::de::Error::custom(
492            "timeout_ms must be between 1 and 60000",
493        ));
494    }
495    Ok(timeout)
496}
497
498#[derive(Debug, Clone, Serialize, Deserialize, Default, schemars::JsonSchema)]
499pub struct PathStringArgs {
500    #[schemars(description = "Target path, for example /docs or /about.")]
501    pub path: String,
502}
503
504#[derive(Debug, Clone, Serialize, Deserialize, Default, schemars::JsonSchema)]
505pub struct FakeCameraArgs {
506    pub file: String,
507    #[serde(default)]
508    pub loop_video: bool,
509}
510
511#[derive(Debug, Clone, Serialize, Deserialize, Default, schemars::JsonSchema)]
512pub struct WasmReadArgs {
513    pub addr: String,
514    pub len: usize,
515    pub memory: Option<String>,
516}
517
518#[derive(Debug, Clone, Serialize, Deserialize, Default, schemars::JsonSchema)]
519pub struct WasmWriteArgs {
520    pub addr: String,
521    pub hex: String,
522    pub memory: Option<String>,
523}
524
525#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
526pub struct WasmFindArgs {
527    pub pattern: String,
528    pub start: Option<String>,
529    pub end: Option<String>,
530    #[serde(default = "default_wasm_find_max")]
531    pub max: usize,
532    pub memory: Option<String>,
533}
534
535impl Default for WasmFindArgs {
536    fn default() -> Self {
537        Self {
538            pattern: String::new(),
539            start: None,
540            end: None,
541            max: default_wasm_find_max(),
542            memory: None,
543        }
544    }
545}
546
547#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
548pub struct InterceptAddArgs {
549    pub url_pattern: String,
550    pub capture: Option<String>,
551    pub respond: Option<String>,
552    #[serde(default = "default_intercept_status")]
553    pub status: u16,
554}
555
556impl Default for InterceptAddArgs {
557    fn default() -> Self {
558        Self {
559            url_pattern: String::new(),
560            capture: None,
561            respond: None,
562            status: default_intercept_status(),
563        }
564    }
565}
566
567#[derive(Debug, Clone, Serialize, Deserialize, Default, schemars::JsonSchema)]
568pub struct IdArgs {
569    pub id: String,
570}
571
572#[derive(Debug, Clone, Serialize, Deserialize, Default, schemars::JsonSchema)]
573pub struct ModeArgs {
574    pub mode: String,
575}
576
577#[derive(Debug, Clone, Serialize, Deserialize, Default, schemars::JsonSchema)]
578pub struct DomainArgs {
579    pub domain: String,
580}
581
582#[derive(Debug, Clone, Serialize, Deserialize, Default, schemars::JsonSchema)]
583pub struct CaptchaInjectArgs {
584    pub token: String,
585    #[serde(default = "default_captcha_type")]
586    pub captcha_type: String,
587    pub callback: Option<String>,
588    pub click_after: Option<String>,
589}
590
591#[derive(Debug, Clone, Serialize, Deserialize, Default, schemars::JsonSchema)]
592pub struct NetworkRecordStartArgs {
593    #[serde(default)]
594    pub patterns: Vec<String>,
595    #[serde(default)]
596    pub no_bodies: bool,
597    #[serde(default = "default_max_body_bytes")]
598    pub max_body_bytes: usize,
599    #[serde(default)]
600    pub clear: bool,
601}
602
603#[derive(Debug, Clone, Serialize, Deserialize, Default, schemars::JsonSchema)]
604pub struct NetworkLogArgs {
605    pub limit: Option<usize>,
606    pub pattern: Option<String>,
607    pub method: Option<String>,
608    pub status: Option<u16>,
609    pub since: Option<u64>,
610    #[serde(default)]
611    pub compact: bool,
612}
613
614#[derive(Debug, Clone, Serialize, Deserialize, Default, schemars::JsonSchema)]
615pub struct NetworkShowArgs {
616    pub id: u64,
617    #[serde(default)]
618    pub body: bool,
619    pub max_body: Option<usize>,
620}
621
622#[derive(Debug, Clone, Serialize, Deserialize, Default, schemars::JsonSchema)]
623pub struct NetworkWaitArgs {
624    pub pattern: Option<String>,
625    pub method: Option<String>,
626    pub status: Option<u16>,
627    pub timeout: Option<u64>,
628    pub since: Option<u64>,
629    #[serde(default)]
630    pub include_existing: bool,
631}
632
633#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
634pub struct NetworkExportArgs {
635    pub path: String,
636    #[serde(default = "default_network_export_format")]
637    pub format: String,
638    pub settle_ms: Option<u64>,
639}
640
641impl Default for NetworkExportArgs {
642    fn default() -> Self {
643        Self {
644            path: String::new(),
645            format: default_network_export_format(),
646            settle_ms: None,
647        }
648    }
649}
650
651fn default_max_body_bytes() -> usize {
652    10 * 1024 * 1024
653}
654
655fn default_network_export_format() -> String {
656    "har".to_string()
657}
658
659fn default_viewport_width() -> u32 {
660    390
661}
662
663fn default_viewport_height() -> u32 {
664    844
665}
666
667fn default_device_pixel_ratio() -> f64 {
668    2.0
669}
670
671fn default_wasm_find_max() -> usize {
672    20
673}
674
675fn default_intercept_status() -> u16 {
676    200
677}
678
679fn default_captcha_type() -> String {
680    "auto".to_string()
681}