browsr_types/
browser_step.rs

1//! Browser step types for the /browser_step API.
2//!
3//! These types are shared between browsr-client and browsr server.
4
5use serde::{Deserialize, Serialize};
6
7use crate::Commands;
8
9/// Input for browser_step execution (tool parameters).
10#[derive(Debug, Clone, Serialize, Deserialize)]
11pub struct BrowserStepInput {
12    /// Browser commands to execute
13    pub commands: Vec<Commands>,
14    /// Whether to run browser in headless mode
15    #[serde(default)]
16    pub headless: Option<bool>,
17    /// Thinking/reasoning about current step
18    #[serde(default)]
19    pub thinking: Option<String>,
20    /// Evaluation of how well the previous goal was achieved
21    #[serde(default)]
22    pub evaluation_previous_goal: Option<String>,
23    /// Memory/context to persist across steps
24    #[serde(default)]
25    pub memory: Option<String>,
26    /// Next goal to achieve
27    #[serde(default)]
28    pub next_goal: Option<String>,
29}
30
31impl BrowserStepInput {
32    /// Create a new BrowserStepInput with commands only.
33    pub fn new(commands: Vec<Commands>) -> Self {
34        Self {
35            commands,
36            headless: None,
37            thinking: None,
38            evaluation_previous_goal: None,
39            memory: None,
40            next_goal: None,
41        }
42    }
43
44    /// Set headless mode.
45    pub fn with_headless(mut self, headless: bool) -> Self {
46        self.headless = Some(headless);
47        self
48    }
49}
50
51/// Request payload for the /browser_step API.
52#[derive(Debug, Clone, Serialize, Deserialize)]
53pub struct BrowserStepRequest {
54    /// Browser session ID (managed by server)
55    #[serde(default)]
56    pub session_id: String,
57    /// Distri thread ID (used for sequence persistence)
58    #[serde(default)]
59    pub thread_id: String,
60    /// Distri task ID (optional)
61    #[serde(default)]
62    pub task_id: Option<String>,
63    /// Distri run ID (optional)
64    #[serde(default)]
65    pub run_id: Option<String>,
66    /// Tool call ID (optional)
67    #[serde(default)]
68    pub tool_call_id: Option<String>,
69    /// Browser commands to execute
70    pub commands: Vec<Commands>,
71    /// Whether to run browser in headless mode
72    #[serde(default)]
73    pub headless: Option<bool>,
74    /// Thinking/reasoning about current step
75    #[serde(default)]
76    pub thinking: Option<String>,
77    /// Evaluation of how well the previous goal was achieved
78    #[serde(default)]
79    pub evaluation_previous_goal: Option<String>,
80    /// Memory/context to persist across steps
81    #[serde(default)]
82    pub memory: Option<String>,
83    /// Next goal to achieve
84    #[serde(default)]
85    pub next_goal: Option<String>,
86}
87
88impl BrowserStepRequest {
89    /// Create a new request from input with context.
90    pub fn new(input: BrowserStepInput) -> Self {
91        Self {
92            session_id: String::new(),
93            thread_id: String::new(),
94            task_id: None,
95            run_id: None,
96            tool_call_id: None,
97            commands: input.commands,
98            headless: input.headless,
99            thinking: input.thinking,
100            evaluation_previous_goal: input.evaluation_previous_goal,
101            memory: input.memory,
102            next_goal: input.next_goal,
103        }
104    }
105
106    /// Set session ID.
107    pub fn with_session_id(mut self, session_id: impl Into<String>) -> Self {
108        self.session_id = session_id.into();
109        self
110    }
111
112    /// Set thread ID.
113    pub fn with_thread_id(mut self, thread_id: impl Into<String>) -> Self {
114        self.thread_id = thread_id.into();
115        self
116    }
117
118    /// Set task ID.
119    pub fn with_task_id(mut self, task_id: impl Into<String>) -> Self {
120        self.task_id = Some(task_id.into());
121        self
122    }
123
124    /// Set run ID.
125    pub fn with_run_id(mut self, run_id: impl Into<String>) -> Self {
126        self.run_id = Some(run_id.into());
127        self
128    }
129
130    /// Set tool call ID.
131    pub fn with_tool_call_id(mut self, tool_call_id: impl Into<String>) -> Self {
132        self.tool_call_id = Some(tool_call_id.into());
133        self
134    }
135}
136
137/// Result of browser_step execution.
138#[derive(Debug, Clone, Serialize, Deserialize)]
139pub struct BrowserStepResult {
140    /// Whether all commands succeeded
141    pub success: bool,
142    /// Browser session ID
143    pub session_id: String,
144    /// Summary of execution
145    pub summary: Option<String>,
146    /// Current URL after execution
147    pub url: Option<String>,
148    /// Error message if any command failed
149    pub error: Option<String>,
150    /// Sequence ID for persistence
151    pub sequence_id: Option<String>,
152    /// Extracted data from commands (e.g., ExtractStructuredContent)
153    #[serde(skip_serializing_if = "Option::is_none")]
154    pub data: Option<serde_json::Value>,
155}
156
157impl BrowserStepResult {
158    /// Create a successful result.
159    pub fn success(session_id: impl Into<String>) -> Self {
160        Self {
161            success: true,
162            session_id: session_id.into(),
163            summary: None,
164            url: None,
165            error: None,
166            sequence_id: None,
167            data: None,
168        }
169    }
170
171    /// Create an error result.
172    pub fn error(session_id: impl Into<String>, error: impl Into<String>) -> Self {
173        Self {
174            success: false,
175            session_id: session_id.into(),
176            summary: None,
177            url: None,
178            error: Some(error.into()),
179            sequence_id: None,
180            data: None,
181        }
182    }
183}