Skip to main content

limit_cli/tools/browser/handlers/
tabs.rs

1use super::super::args::ArgsExt;
2use super::super::client::BrowserClient;
3use super::super::client_ext::TabsExt;
4use super::super::response::ok_msg;
5use limit_agent::error::AgentError;
6use serde_json::Value;
7
8pub async fn tab_list(client: &BrowserClient, _args: &Value) -> Result<Value, AgentError> {
9    let tabs = client.tab_list().await?;
10
11    let tabs_json: Vec<Value> = tabs
12        .iter()
13        .map(|t| {
14            serde_json::json!({
15                "index": t.index,
16                "title": t.title
17            })
18        })
19        .collect();
20
21    Ok(serde_json::json!({
22        "success": true,
23        "tabs": tabs_json,
24        "count": tabs.len()
25    }))
26}
27
28pub async fn tab_new(client: &BrowserClient, args: &Value) -> Result<Value, AgentError> {
29    let url = args.get_opt_str("url");
30    client.tab_new(url).await?;
31    Ok(ok_msg("Opened new tab"))
32}
33
34pub async fn tab_close(client: &BrowserClient, args: &Value) -> Result<Value, AgentError> {
35    let index = args.get_opt_u64("index").map(|i| i as usize);
36    client.tab_close(index).await?;
37    Ok(ok_msg("Tab closed"))
38}
39
40pub async fn tab_select(client: &BrowserClient, args: &Value) -> Result<Value, AgentError> {
41    let index = args.get_u64("index", "tab_select")? as usize;
42    client.tab_select(index).await?;
43    Ok(ok_msg(format!("Switched to tab {}", index)))
44}