adk_browser/tools/
cookies.rs1use crate::session::BrowserSession;
4use adk_core::{AdkError, Result, Tool, ToolContext};
5use async_trait::async_trait;
6use serde_json::{json, Value};
7use std::sync::Arc;
8
9pub struct GetCookiesTool {
11 browser: Arc<BrowserSession>,
12}
13
14impl GetCookiesTool {
15 pub fn new(browser: Arc<BrowserSession>) -> Self {
16 Self { browser }
17 }
18}
19
20#[async_trait]
21impl Tool for GetCookiesTool {
22 fn name(&self) -> &str {
23 "browser_get_cookies"
24 }
25
26 fn description(&self) -> &str {
27 "Get all cookies for the current page domain."
28 }
29
30 fn parameters_schema(&self) -> Option<Value> {
31 Some(json!({
32 "type": "object",
33 "properties": {}
34 }))
35 }
36
37 async fn execute(&self, _ctx: Arc<dyn ToolContext>, _args: Value) -> Result<Value> {
38 let cookies = self.browser.get_all_cookies().await?;
39 Ok(json!({
40 "success": true,
41 "cookies": cookies,
42 "count": cookies.len()
43 }))
44 }
45}
46
47pub struct GetCookieTool {
49 browser: Arc<BrowserSession>,
50}
51
52impl GetCookieTool {
53 pub fn new(browser: Arc<BrowserSession>) -> Self {
54 Self { browser }
55 }
56}
57
58#[async_trait]
59impl Tool for GetCookieTool {
60 fn name(&self) -> &str {
61 "browser_get_cookie"
62 }
63
64 fn description(&self) -> &str {
65 "Get a specific cookie by name."
66 }
67
68 fn parameters_schema(&self) -> Option<Value> {
69 Some(json!({
70 "type": "object",
71 "properties": {
72 "name": {
73 "type": "string",
74 "description": "The name of the cookie to retrieve"
75 }
76 },
77 "required": ["name"]
78 }))
79 }
80
81 async fn execute(&self, _ctx: Arc<dyn ToolContext>, args: Value) -> Result<Value> {
82 let name = args
83 .get("name")
84 .and_then(|v| v.as_str())
85 .ok_or_else(|| AdkError::Tool("Missing 'name' parameter".to_string()))?;
86
87 let cookie = self.browser.get_cookie(name).await?;
88 Ok(json!({
89 "success": true,
90 "cookie": cookie
91 }))
92 }
93}
94
95pub struct AddCookieTool {
97 browser: Arc<BrowserSession>,
98}
99
100impl AddCookieTool {
101 pub fn new(browser: Arc<BrowserSession>) -> Self {
102 Self { browser }
103 }
104}
105
106#[async_trait]
107impl Tool for AddCookieTool {
108 fn name(&self) -> &str {
109 "browser_add_cookie"
110 }
111
112 fn description(&self) -> &str {
113 "Add a cookie to the browser."
114 }
115
116 fn parameters_schema(&self) -> Option<Value> {
117 Some(json!({
118 "type": "object",
119 "properties": {
120 "name": {
121 "type": "string",
122 "description": "Cookie name"
123 },
124 "value": {
125 "type": "string",
126 "description": "Cookie value"
127 },
128 "domain": {
129 "type": "string",
130 "description": "Cookie domain (optional)"
131 },
132 "path": {
133 "type": "string",
134 "description": "Cookie path (default: '/')"
135 },
136 "secure": {
137 "type": "boolean",
138 "description": "Secure flag (default: false)"
139 },
140 "expiry": {
141 "type": "integer",
142 "description": "Expiry timestamp in seconds since epoch"
143 }
144 },
145 "required": ["name", "value"]
146 }))
147 }
148
149 async fn execute(&self, _ctx: Arc<dyn ToolContext>, args: Value) -> Result<Value> {
150 let name = args
151 .get("name")
152 .and_then(|v| v.as_str())
153 .ok_or_else(|| AdkError::Tool("Missing 'name' parameter".to_string()))?;
154
155 let value = args
156 .get("value")
157 .and_then(|v| v.as_str())
158 .ok_or_else(|| AdkError::Tool("Missing 'value' parameter".to_string()))?;
159
160 let domain = args.get("domain").and_then(|v| v.as_str());
161 let path = args.get("path").and_then(|v| v.as_str());
162 let secure = args.get("secure").and_then(|v| v.as_bool());
163 let expiry = args.get("expiry").and_then(|v| v.as_i64());
164
165 self.browser.add_cookie(name, value, domain, path, secure, expiry).await?;
166
167 Ok(json!({
168 "success": true,
169 "added_cookie": name
170 }))
171 }
172}
173
174pub struct DeleteCookieTool {
176 browser: Arc<BrowserSession>,
177}
178
179impl DeleteCookieTool {
180 pub fn new(browser: Arc<BrowserSession>) -> Self {
181 Self { browser }
182 }
183}
184
185#[async_trait]
186impl Tool for DeleteCookieTool {
187 fn name(&self) -> &str {
188 "browser_delete_cookie"
189 }
190
191 fn description(&self) -> &str {
192 "Delete a specific cookie by name."
193 }
194
195 fn parameters_schema(&self) -> Option<Value> {
196 Some(json!({
197 "type": "object",
198 "properties": {
199 "name": {
200 "type": "string",
201 "description": "The name of the cookie to delete"
202 }
203 },
204 "required": ["name"]
205 }))
206 }
207
208 async fn execute(&self, _ctx: Arc<dyn ToolContext>, args: Value) -> Result<Value> {
209 let name = args
210 .get("name")
211 .and_then(|v| v.as_str())
212 .ok_or_else(|| AdkError::Tool("Missing 'name' parameter".to_string()))?;
213
214 self.browser.delete_cookie(name).await?;
215
216 Ok(json!({
217 "success": true,
218 "deleted_cookie": name
219 }))
220 }
221}
222
223pub struct DeleteAllCookiesTool {
225 browser: Arc<BrowserSession>,
226}
227
228impl DeleteAllCookiesTool {
229 pub fn new(browser: Arc<BrowserSession>) -> Self {
230 Self { browser }
231 }
232}
233
234#[async_trait]
235impl Tool for DeleteAllCookiesTool {
236 fn name(&self) -> &str {
237 "browser_delete_all_cookies"
238 }
239
240 fn description(&self) -> &str {
241 "Delete all cookies for the current domain."
242 }
243
244 fn parameters_schema(&self) -> Option<Value> {
245 Some(json!({
246 "type": "object",
247 "properties": {}
248 }))
249 }
250
251 async fn execute(&self, _ctx: Arc<dyn ToolContext>, _args: Value) -> Result<Value> {
252 self.browser.delete_all_cookies().await?;
253
254 Ok(json!({
255 "success": true,
256 "message": "All cookies deleted"
257 }))
258 }
259}