limit-cli 0.0.46

AI-powered terminal coding assistant with TUI. Multi-provider LLM support, session persistence, and built-in tools.
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
//! Query and snapshot operations

use crate::tools::browser::executor::BrowserError;
use crate::tools::browser::types::{BoundingBox, SnapshotResult};
use serde_json::Value as JsonValue;
use std::collections::HashMap;

/// Query and snapshot operations for browser client
pub trait QueryExt {
    /// Take an accessibility snapshot of the current page
    fn snapshot(
        &self,
    ) -> impl std::future::Future<Output = Result<SnapshotResult, BrowserError>> + Send;

    /// Take a screenshot and save to path
    fn screenshot(
        &self,
        path: &str,
    ) -> impl std::future::Future<Output = Result<(), BrowserError>> + Send;

    /// Save page as PDF
    fn pdf(&self, path: &str)
        -> impl std::future::Future<Output = Result<(), BrowserError>> + Send;

    /// Evaluate JavaScript in the browser
    fn eval(
        &self,
        script: &str,
    ) -> impl std::future::Future<Output = Result<JsonValue, BrowserError>> + Send;

    /// Get page content (text, html, value, url, or title)
    fn get(
        &self,
        what: &str,
    ) -> impl std::future::Future<Output = Result<String, BrowserError>> + Send;

    /// Get element attribute value
    fn get_attr(
        &self,
        selector: &str,
        attr: &str,
    ) -> impl std::future::Future<Output = Result<String, BrowserError>> + Send;

    /// Get count of elements matching selector
    fn get_count(
        &self,
        selector: &str,
    ) -> impl std::future::Future<Output = Result<usize, BrowserError>> + Send;

    /// Get element bounding box
    fn get_box(
        &self,
        selector: &str,
    ) -> impl std::future::Future<Output = Result<BoundingBox, BrowserError>> + Send;

    /// Get element computed styles
    fn get_styles(
        &self,
        selector: &str,
    ) -> impl std::future::Future<Output = Result<HashMap<String, String>, BrowserError>> + Send;

    /// Find elements using various locator strategies
    fn find(
        &self,
        locator_type: &str,
        value: &str,
        action: &str,
        action_value: Option<&str>,
    ) -> impl std::future::Future<Output = Result<String, BrowserError>> + Send;

    /// Check element state (visible, hidden, enabled, disabled, editable)
    fn is_(
        &self,
        what: &str,
        selector: &str,
    ) -> impl std::future::Future<Output = Result<bool, BrowserError>> + Send;

    /// Download file from link/button to path
    fn download(
        &self,
        selector: &str,
        path: &str,
    ) -> impl std::future::Future<Output = Result<String, BrowserError>> + Send;
}

impl QueryExt for super::super::BrowserClient {
    async fn snapshot(&self) -> Result<SnapshotResult, BrowserError> {
        let output = self.executor().execute(&["snapshot"]).await?;

        if output.success {
            let content = output.stdout.trim().to_string();
            let title = Self::extract_field(&content, "Title:");
            let url = Self::extract_field(&content, "URL:");

            Ok(SnapshotResult {
                content,
                title,
                url,
            })
        } else {
            Err(BrowserError::Other(format!(
                "Failed to take snapshot: {}",
                output.stderr
            )))
        }
    }

    async fn screenshot(&self, path: &str) -> Result<(), BrowserError> {
        if path.is_empty() {
            return Err(BrowserError::InvalidArguments(
                "Path cannot be empty".to_string(),
            ));
        }

        let output = self.executor().execute(&["screenshot", path]).await?;

        if output.success {
            Ok(())
        } else {
            Err(BrowserError::Other(format!(
                "Failed to take screenshot: {}",
                output.stderr
            )))
        }
    }

    async fn pdf(&self, path: &str) -> Result<(), BrowserError> {
        if path.is_empty() {
            return Err(BrowserError::InvalidArguments(
                "Path cannot be empty".to_string(),
            ));
        }

        let output = self.executor().execute(&["pdf", path]).await?;

        if output.success {
            Ok(())
        } else {
            Err(BrowserError::Other(format!(
                "Failed to save PDF: {}",
                output.stderr
            )))
        }
    }

    async fn eval(&self, script: &str) -> Result<JsonValue, BrowserError> {
        if script.is_empty() {
            return Err(BrowserError::InvalidArguments(
                "Script cannot be empty".to_string(),
            ));
        }

        let output = self.executor().execute(&["eval", script]).await?;

        if output.success {
            let trimmed = output.stdout.trim();
            if trimmed.is_empty() {
                Ok(JsonValue::Null)
            } else {
                serde_json::from_str(trimmed)
                    .map_err(|e| BrowserError::ParseError(format!("Invalid JSON: {}", e)))
            }
        } else {
            Err(BrowserError::Other(format!(
                "Failed to evaluate script: {}",
                output.stderr
            )))
        }
    }

    async fn get(&self, what: &str) -> Result<String, BrowserError> {
        let valid_types = ["text", "html", "value", "url", "title"];
        if !valid_types.contains(&what) {
            return Err(BrowserError::InvalidArguments(format!(
                "Invalid get type '{}'. Valid types: {}",
                what,
                valid_types.join(", ")
            )));
        }

        let output = self.executor().execute(&["get", what]).await?;

        if output.success {
            Ok(output.stdout.trim().to_string())
        } else {
            Err(BrowserError::Other(format!(
                "Failed to get {}: {}",
                what, output.stderr
            )))
        }
    }

    async fn get_attr(&self, selector: &str, attr: &str) -> Result<String, BrowserError> {
        if selector.is_empty() {
            return Err(BrowserError::InvalidArguments(
                "Selector cannot be empty".to_string(),
            ));
        }

        if attr.is_empty() {
            return Err(BrowserError::InvalidArguments(
                "Attribute name cannot be empty".to_string(),
            ));
        }

        let output = self
            .executor()
            .execute(&["get", "attr", selector, attr])
            .await?;

        if output.success {
            Ok(output.stdout.trim().to_string())
        } else {
            Err(BrowserError::Other(format!(
                "Failed to get attribute: {}",
                output.stderr
            )))
        }
    }

    async fn get_count(&self, selector: &str) -> Result<usize, BrowserError> {
        if selector.is_empty() {
            return Err(BrowserError::InvalidArguments(
                "Selector cannot be empty".to_string(),
            ));
        }

        let output = self.executor().execute(&["get", "count", selector]).await?;

        if output.success {
            let count = output
                .stdout
                .trim()
                .parse::<usize>()
                .map_err(|_| BrowserError::ParseError("Invalid count value".to_string()))?;
            Ok(count)
        } else {
            Err(BrowserError::Other(format!(
                "Failed to get count: {}",
                output.stderr
            )))
        }
    }

    async fn get_box(&self, selector: &str) -> Result<BoundingBox, BrowserError> {
        if selector.is_empty() {
            return Err(BrowserError::InvalidArguments(
                "Selector cannot be empty".to_string(),
            ));
        }

        let output = self.executor().execute(&["get", "box", selector]).await?;

        if output.success {
            let parts: Vec<&str> = output.stdout.trim().split(',').collect();
            if parts.len() == 4 {
                Ok(BoundingBox {
                    x: parts[0]
                        .parse()
                        .map_err(|_| BrowserError::ParseError("Invalid x value".to_string()))?,
                    y: parts[1]
                        .parse()
                        .map_err(|_| BrowserError::ParseError("Invalid y value".to_string()))?,
                    width: parts[2]
                        .parse()
                        .map_err(|_| BrowserError::ParseError("Invalid width value".to_string()))?,
                    height: parts[3].parse().map_err(|_| {
                        BrowserError::ParseError("Invalid height value".to_string())
                    })?,
                })
            } else {
                Err(BrowserError::ParseError(
                    "Invalid bounding box format".to_string(),
                ))
            }
        } else {
            Err(BrowserError::Other(format!(
                "Failed to get bounding box: {}",
                output.stderr
            )))
        }
    }

    async fn get_styles(&self, selector: &str) -> Result<HashMap<String, String>, BrowserError> {
        if selector.is_empty() {
            return Err(BrowserError::InvalidArguments(
                "Selector cannot be empty".to_string(),
            ));
        }

        let output = self
            .executor()
            .execute(&["get", "styles", selector])
            .await?;

        if output.success {
            let mut styles = HashMap::new();
            for line in output.stdout.lines() {
                if let Some((key, value)) = line.split_once(':') {
                    styles.insert(key.trim().to_string(), value.trim().to_string());
                }
            }
            Ok(styles)
        } else {
            Err(BrowserError::Other(format!(
                "Failed to get styles: {}",
                output.stderr
            )))
        }
    }

    async fn find(
        &self,
        locator_type: &str,
        value: &str,
        action: &str,
        action_value: Option<&str>,
    ) -> Result<String, BrowserError> {
        let valid_locators = [
            "role",
            "text",
            "label",
            "placeholder",
            "alt",
            "title",
            "testid",
            "css",
            "xpath",
        ];
        if !valid_locators.contains(&locator_type) {
            return Err(BrowserError::InvalidArguments(format!(
                "Invalid locator type '{}'. Valid types: {}",
                locator_type,
                valid_locators.join(", ")
            )));
        }

        if value.is_empty() {
            return Err(BrowserError::InvalidArguments(
                "Locator value cannot be empty".to_string(),
            ));
        }

        let valid_actions = [
            "click", "fill", "text", "count", "first", "last", "nth", "hover", "focus", "check",
            "uncheck",
        ];
        if !valid_actions.contains(&action) {
            return Err(BrowserError::InvalidArguments(format!(
                "Invalid action '{}'. Valid actions: {}",
                action,
                valid_actions.join(", ")
            )));
        }

        let locator_flag = format!("--{}", locator_type);
        let mut args = vec!["find", &locator_flag, value, action];

        let output = if let Some(av) = action_value {
            args.push(av);
            self.executor().execute(&args).await?
        } else {
            self.executor().execute(&args).await?
        };

        if output.success {
            Ok(output.stdout.trim().to_string())
        } else {
            Err(BrowserError::Other(format!(
                "Find action failed: {}",
                output.stderr
            )))
        }
    }

    async fn is_(&self, what: &str, selector: &str) -> Result<bool, BrowserError> {
        let valid_states = ["visible", "hidden", "enabled", "disabled", "editable"];
        if !valid_states.contains(&what) {
            return Err(BrowserError::InvalidArguments(format!(
                "Invalid state check '{}'. Valid states: {}",
                what,
                valid_states.join(", ")
            )));
        }

        if selector.is_empty() {
            return Err(BrowserError::InvalidArguments(
                "Selector cannot be empty".to_string(),
            ));
        }

        let output = self.executor().execute(&["is", what, selector]).await?;

        if output.success {
            let result = output.stdout.trim().to_lowercase();
            Ok(result == "true" || result == "yes" || result == "1")
        } else {
            Err(BrowserError::Other(format!(
                "Failed to check state: {}",
                output.stderr
            )))
        }
    }

    async fn download(&self, selector: &str, path: &str) -> Result<String, BrowserError> {
        if selector.is_empty() {
            return Err(BrowserError::InvalidArguments(
                "Selector cannot be empty".to_string(),
            ));
        }

        let output = self
            .executor()
            .execute(&["download", selector, path])
            .await?;

        if output.success {
            Ok(output.stdout.trim().to_string())
        } else {
            Err(BrowserError::Other(format!(
                "Failed to download: {}",
                output.stderr
            )))
        }
    }
}