eoka-cli 0.1.1

Browser automation CLI for AI agents and pentesting
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
//! Command handler — maps CLI commands to eoka browser operations.

pub mod intercept;
mod persist;
pub mod profile;
pub mod state;
mod target;

use std::collections::HashMap;
use std::fmt::Write as FmtWrite;

use base64::Engine;
use eoka_agent::{annotate, observe, snapshot};
use serde_json::{json, Value};

use crate::launch_spec::LaunchSpec;
use crate::protocol::Response;
use intercept::{InterceptLogEntry, InterceptState};
use persist::{capture_state, ensure_console_capture, restore_state, SavedState};
use state::{BrowserState, TabState};
use target::{
    auto_observe_if_needed, click_with_retry, fill_with_retry, json_str, resolve_target,
    title_nonblocking, wait_for_stable,
};

// ---------------------------------------------------------------------------
// Handler
// ---------------------------------------------------------------------------

pub struct Handler {
    state: Option<BrowserState>,
    spec: LaunchSpec,
    intercept: InterceptState,
}

impl Handler {
    pub fn new(spec: LaunchSpec) -> Self {
        Self {
            state: None,
            spec,
            intercept: InterceptState::new(),
        }
    }

    async fn ensure_browser(&mut self) -> Result<(), String> {
        if self.state.is_some() {
            return Ok(());
        }
        let mut state = match &self.spec {
            LaunchSpec::Connect { ws_url } => BrowserState::connected(ws_url)
                .await
                .map_err(|e| e.to_string())?,
            LaunchSpec::Launch {
                headless,
                from_profile,
                clone_state_from,
                no_stealth,
            } => {
                let mut s = BrowserState::launched(*headless, from_profile.as_deref(), *no_stealth)
                    .await
                    .map_err(|e| e.to_string())?;

                // If --clone-state-from was set, capture from the live Chrome,
                // open a blank tab, restore. The state is loaded into the launched
                // browser before any user command runs.
                if let Some(source) = clone_state_from {
                    let saved = persist::clone_state_from_source(source).await?;
                    let url = saved.url.clone();
                    let tab = if !url.is_empty() {
                        s.ensure_tab(&url).await.map_err(|e| e.to_string())?
                    } else {
                        s.ensure_blank_tab().await.map_err(|e| e.to_string())?
                    };
                    restore_state(&tab.page, &saved).await?;
                    tab.invalidate();
                }
                s
            }
        };

        // In live mode, auto-attach to the most recent user tab so the first
        // command (snapshot, click, etc.) operates on what the user is looking
        // at rather than refusing with "no tab open".
        if state.is_live && state.current_tab_id.is_none() {
            if let Ok(tabs) = state.browser.tabs().await {
                if let Some(t) = tabs.into_iter().find(|t| !t.url.starts_with("devtools://")) {
                    let _ = state.attach_existing_tab(&t.id).await;
                }
            }
        }

        self.state = Some(state);
        Ok(())
    }

    fn require_tab(&self) -> Result<&TabState, String> {
        self.state
            .as_ref()
            .ok_or("No browser open. Use 'open' first.")?
            .current_tab()
            .ok_or_else(|| "No tab open. Use 'open' first.".to_string())
    }

    fn require_tab_mut(&mut self) -> Result<&mut TabState, String> {
        self.state
            .as_mut()
            .ok_or("No browser open. Use 'open' first.")?
            .current_tab_mut()
            .ok_or_else(|| "No tab open. Use 'open' first.".to_string())
    }

    fn require_state_mut(&mut self) -> Result<&mut BrowserState, String> {
        self.state
            .as_mut()
            .ok_or_else(|| "No browser open. Use 'open' first.".to_string())
    }

    pub async fn handle(&mut self, cmd: &str, args: &Value) -> Response {
        match self.dispatch(cmd, args).await {
            Ok(resp) => resp,
            Err(e) => Response::err(e),
        }
    }

    async fn dispatch(&mut self, cmd: &str, args: &Value) -> Result<Response, String> {
        match cmd {
            "open" => self.cmd_open(args).await,
            "back" => {
                self.cmd_nav(|p| Box::pin(async { p.back().await }), "Back")
                    .await
            }
            "forward" => {
                self.cmd_nav(|p| Box::pin(async { p.forward().await }), "Forward")
                    .await
            }
            "reload" => self.cmd_reload().await,
            "snapshot" => self.cmd_snapshot(args).await,
            "observe" => self.cmd_observe(args).await,
            "screenshot" => self.cmd_screenshot(args).await,
            "emulate" => self.cmd_emulate(args).await,
            "info" => self.cmd_info().await,
            "text" => self.cmd_text().await,
            "find" => self.cmd_find(args).await,
            "click" => self.cmd_click(args).await,
            "dblclick" => self.cmd_dblclick(args).await,
            "fill" => self.cmd_fill(args).await,
            "select" => self.cmd_select(args).await,
            "hover" => self.cmd_hover(args).await,
            "key" => self.cmd_key(args).await,
            "scroll" => self.cmd_scroll(args).await,
            "eval" => self.cmd_eval(args).await,
            "exec" => self.cmd_exec(args).await,
            "fetch" => self.cmd_fetch(args).await,
            "cookies" => self.cmd_cookies().await,
            "set_cookie" => self.cmd_set_cookie(args).await,
            "delete_cookie" => self.cmd_delete_cookie(args).await,
            "clear_cookies" => self.cmd_clear_cookies().await,
            "storage" => self.cmd_storage(args).await,
            "set_storage" => self.cmd_set_storage(args).await,
            "dump_storage" => self.cmd_dump_storage().await,
            "save_state" => self.cmd_save_state(args).await,
            "load_state" => self.cmd_load_state(args).await,
            "headers" => self.cmd_headers(args).await,
            "console" => self.cmd_console(args).await,
            "errors" => self.cmd_errors(args).await,
            "tab_list" => self.cmd_tab_list().await,
            "tab_new" => self.cmd_tab_new(args).await,
            "tab_switch" => self.cmd_tab_switch(args).await,
            "tab_close" => self.cmd_tab_close(args).await,
            "tab_attach" => self.cmd_tab_attach(args).await,
            "clone_from" => self.cmd_clone_from(args).await,
            "wait" => self.cmd_wait(args).await,
            "spa_info" => self.cmd_spa_info().await,
            "spa_navigate" => self.cmd_spa_navigate(args).await,
            "fake_camera" => self.cmd_fake_camera(args).await,
            "wasm_info" => self.cmd_wasm_info().await,
            "wasm_read" => self.cmd_wasm_read(args).await,
            "wasm_write" => self.cmd_wasm_write(args).await,
            "wasm_find" => self.cmd_wasm_find(args).await,
            "intercept_add" => self.cmd_intercept_add(args).await,
            "intercept_list" => self.cmd_intercept_list().await,
            "intercept_remove" => self.cmd_intercept_remove(args).await,
            "intercept_log" => self.cmd_intercept_log(args).await,
            "close" => self.cmd_close().await,
            other => Err(format!("Unknown command: {}", other)),
        }
    }

    // ── Helpers ─────────────────────────────────────────────────────────

    fn arg_str<'a>(&self, args: &'a Value, key: &str) -> Result<&'a str, String> {
        args[key]
            .as_str()
            .ok_or_else(|| format!("Missing '{}'", key))
    }

    /// Get tab + viewport_only config together (avoids repeated boilerplate).
    fn tab_with_config(&mut self) -> Result<(&mut TabState, bool), String> {
        let state = self.require_state_mut()?;
        let vp = state.config.viewport_only;
        let tab = state
            .current_tab_mut()
            .ok_or("No tab open. Use 'open' first.")?;
        Ok((tab, vp))
    }

    // ── Navigation ──────────────────────────────────────────────────────

    async fn cmd_open(&mut self, args: &Value) -> Result<Response, String> {
        let url = self.arg_str(args, "url")?;
        self.ensure_browser().await?;

        let headers: Option<HashMap<String, String>> = args
            .get("headers")
            .filter(|v| !v.is_null())
            .and_then(|v| serde_json::from_value(v.clone()).ok());
        let user_agent = args["user_agent"].as_str();
        let bypass_csp = args["bypass_csp"].as_bool().unwrap_or(false);
        // --inject-js: inject a script via addScriptToEvaluateOnNewDocument before navigation
        let inject_js = args["inject_js"].as_str();
        let has_extras =
            headers.is_some() || user_agent.is_some() || bypass_csp || inject_js.is_some();

        let state = self.require_state_mut()?;

        // In live mode, never navigate the user's currently-attached tab.
        // Always pop a fresh tab in their browser.
        if state.is_live {
            state.new_tab(None).await.map_err(|e| e.to_string())?;
        }

        if has_extras {
            let tab = if state.current_tab_id.is_some() {
                state.current_tab_mut().ok_or("No tab")?
            } else {
                state.ensure_blank_tab().await.map_err(|e| e.to_string())?
            };
            if let Some(ua) = user_agent {
                tab.page
                    .set_user_agent(ua)
                    .await
                    .map_err(|e| e.to_string())?;
            }
            if bypass_csp {
                tab.page
                    .set_bypass_csp(true)
                    .await
                    .map_err(|e| e.to_string())?;
            }
            if let Some(ref h) = headers {
                tab.page
                    .set_extra_headers(h.clone())
                    .await
                    .map_err(|e| e.to_string())?;
            }
            if let Some(js) = inject_js {
                let source = if js.ends_with(".js") {
                    std::fs::read_to_string(js)
                        .map_err(|e| format!("inject_js read error: {}", e))?
                } else {
                    js.to_string()
                };
                tab.page
                    .session()
                    .send::<_, serde_json::Value>(
                        "Page.addScriptToEvaluateOnNewDocument",
                        &serde_json::json!({ "source": source }),
                    )
                    .await
                    .map_err(|e| e.to_string())?;
            }
            tab.invalidate();
            let nav_result = tab.page.goto(url).await;
            if headers.is_some() {
                // Best-effort clear; if it fails the browser is likely broken
                let _ = tab.page.clear_extra_headers().await;
            }
            nav_result.map_err(|e| e.to_string())?;
        } else {
            state.ensure_tab(url).await.map_err(|e| e.to_string())?;
        }

        let tab = state.current_tab_mut().ok_or("No tab after navigate")?;
        let _ = wait_for_stable(&tab.page).await;
        let url = tab.page.url().await.map_err(|e| e.to_string())?;
        let title = title_nonblocking(&tab.page).await;
        Ok(Response::ok_text(format!(
            "Navigated to: {}\nTitle: {}",
            url, title
        )))
    }

    async fn cmd_nav<F>(&mut self, f: F, label: &str) -> Result<Response, String>
    where
        F: FnOnce(
            &eoka::Page,
        )
            -> std::pin::Pin<Box<dyn std::future::Future<Output = eoka::Result<()>> + '_>>,
    {
        let tab = self.require_tab()?;
        f(&tab.page).await.map_err(|e| e.to_string())?;
        let _ = wait_for_stable(&tab.page).await;
        let url = tab.page.url().await.map_err(|e| e.to_string())?;
        Ok(Response::ok_text(format!("{} to: {}", label, url)))
    }

    async fn cmd_reload(&mut self) -> Result<Response, String> {
        let tab = self.require_tab_mut()?;
        tab.page.reload().await.map_err(|e| e.to_string())?;
        tab.invalidate();
        let _ = wait_for_stable(&tab.page).await;
        let url = tab.page.url().await.map_err(|e| e.to_string())?;
        Ok(Response::ok_text(format!("Reloaded: {}", url)))
    }

    // ── Observation ─────────────────────────────────────────────────────

    async fn cmd_snapshot(&mut self, args: &Value) -> Result<Response, String> {
        self.ensure_browser().await?;
        let include_all = args["all"].as_bool().unwrap_or(false);
        let tab = self.require_tab_mut()?;

        let result = snapshot::snapshot(&tab.page, include_all)
            .await
            .map_err(|e| e.to_string())?;

        tab.snapshot_refs.clear();
        for r in &result.refs {
            tab.snapshot_refs
                .insert(r.ref_label.clone(), r.backend_node_id);
        }

        Ok(Response::ok_text(result.tree_text))
    }

    async fn cmd_observe(&mut self, args: &Value) -> Result<Response, String> {
        let filter = args["filter"].as_str();
        let max = args["max"]
            .as_u64()
            .map(|v| v as usize)
            .unwrap_or(usize::MAX);

        if let Some(f) = filter {
            if !matches!(f, "inputs" | "buttons" | "all") {
                return Err(format!(
                    "Unknown filter '{}'. Valid: inputs, buttons, all",
                    f
                ));
            }
        }

        let (tab, viewport_only) = self.tab_with_config()?;
        tab.elements = observe::observe(&tab.page, viewport_only)
            .await
            .map_err(|e| e.to_string())?;

        let filter_fn: fn(&&eoka_agent::InteractiveElement) -> bool = match filter {
            Some("inputs") => |e| {
                matches!(
                    e.tag.as_str(),
                    "input" | "select" | "textarea" | "contenteditable"
                )
            },
            Some("buttons") => {
                |e| matches!(e.tag.as_str(), "button" | "a") || e.role.as_deref() == Some("button")
            }
            _ => |_| true,
        };
        let mut list = String::new();
        for el in tab.elements.iter().filter(filter_fn).take(max) {
            let _ = writeln!(list, "{}", el);
        }
        Ok(Response::ok_text(if list.is_empty() {
            "No interactive elements found.".into()
        } else {
            list
        }))
    }

    async fn cmd_screenshot(&mut self, args: &Value) -> Result<Response, String> {
        let annotate_flag = args["annotate"].as_bool().unwrap_or(false);
        let output = args["output"].as_str();

        let (tab, viewport_only) = self.tab_with_config()?;

        let png = if annotate_flag {
            if tab.elements.is_empty() {
                tab.elements = observe::observe(&tab.page, viewport_only)
                    .await
                    .map_err(|e| e.to_string())?;
            }
            annotate::annotated_screenshot(&tab.page, &tab.elements)
                .await
                .map_err(|e| e.to_string())?
        } else {
            tab.page.screenshot().await.map_err(|e| e.to_string())?
        };

        let path = match output {
            Some(p) => std::path::PathBuf::from(p),
            None => {
                let ts = std::time::SystemTime::now()
                    .duration_since(std::time::UNIX_EPOCH)
                    .unwrap_or_default()
                    .as_millis();
                std::env::temp_dir().join(format!("eoka-screenshot-{}.png", ts))
            }
        };

        std::fs::write(&path, &png).map_err(|e| e.to_string())?;

        let mut result = json!({ "path": path.to_string_lossy(), "size": png.len() });
        if annotate_flag {
            let mut list = String::new();
            for el in &tab.elements {
                let _ = writeln!(list, "{}", el);
            }
            result["elements"] = Value::String(list);
        }
        Ok(Response::ok(result))
    }

    async fn cmd_emulate(&mut self, args: &Value) -> Result<Response, String> {
        self.ensure_browser().await?;
        let reset = args["reset"].as_bool().unwrap_or(false);
        let tab = self.require_tab_mut()?;
        let session = tab.page.session();

        if reset {
            session
                .send::<_, Value>("Emulation.clearDeviceMetricsOverride", &json!({}))
                .await
                .map_err(|e| e.to_string())?;
            let _ = session
                .send::<_, Value>(
                    "Emulation.setTouchEmulationEnabled",
                    &json!({ "enabled": false }),
                )
                .await;
            return Ok(Response::ok_text("Device emulation cleared"));
        }

        let width = args["width"].as_u64().unwrap_or(390);
        let height = args["height"].as_u64().unwrap_or(844);
        let dpr = args["dpr"].as_f64().unwrap_or(2.0);
        let mobile = !args["desktop"].as_bool().unwrap_or(false);

        session
            .send::<_, Value>(
                "Emulation.setDeviceMetricsOverride",
                &json!({
                    "width": width,
                    "height": height,
                    "deviceScaleFactor": dpr,
                    "mobile": mobile,
                }),
            )
            .await
            .map_err(|e| e.to_string())?;

        // Mobile viewports get touch input so gesture-driven UIs (RN-web
        // PanResponder, carousels) behave like a real device. Best-effort.
        let _ = session
            .send::<_, Value>(
                "Emulation.setTouchEmulationEnabled",
                &json!({ "enabled": mobile, "maxTouchPoints": 5 }),
            )
            .await;

        Ok(Response::ok(json!({
            "width": width,
            "height": height,
            "dpr": dpr,
            "mobile": mobile,
        })))
    }

    async fn cmd_info(&mut self) -> Result<Response, String> {
        let tab = self.require_tab()?;
        let url = tab.page.url().await.map_err(|e| e.to_string())?;
        let title = title_nonblocking(&tab.page).await;
        Ok(Response::ok(json!({ "url": url, "title": title })))
    }

    async fn cmd_text(&mut self) -> Result<Response, String> {
        let tab = self.require_tab()?;
        let text = tab.page.text().await.map_err(|e| e.to_string())?;
        Ok(Response::ok_text(text))
    }

    async fn cmd_find(&mut self, args: &Value) -> Result<Response, String> {
        let text = self.arg_str(args, "text")?;
        let (tab, viewport_only) = self.tab_with_config()?;

        if tab.elements.is_empty() {
            tab.elements = observe::observe(&tab.page, viewport_only)
                .await
                .map_err(|e| e.to_string())?;
        }

        let needle = text.to_lowercase();
        let matches: Vec<_> = tab
            .elements
            .iter()
            .filter(|e| {
                e.text.to_lowercase().contains(&needle)
                    || e.placeholder
                        .as_ref()
                        .is_some_and(|p| p.to_lowercase().contains(&needle))
            })
            .collect();

        if matches.is_empty() {
            Ok(Response::ok_text(format!(
                "No elements found matching \"{}\"",
                text
            )))
        } else {
            let out: String = matches.iter().map(|e| format!("{}\n", e)).collect();
            Ok(Response::ok_text(out))
        }
    }

    // ── Actions ─────────────────────────────────────────────────────────

    async fn cmd_click(&mut self, args: &Value) -> Result<Response, String> {
        self.ensure_browser().await?;
        let target_str = self.arg_str(args, "target")?;
        let (tab, vp) = self.tab_with_config()?;

        auto_observe_if_needed(tab, target_str, vp).await?;
        let desc = click_with_retry(tab, target_str, vp).await?;
        let _ = wait_for_stable(&tab.page).await;
        tab.elements.clear();
        Ok(Response::ok_text(format!("Clicked {}", desc)))
    }

    async fn cmd_dblclick(&mut self, args: &Value) -> Result<Response, String> {
        let target_str = self.arg_str(args, "target")?;
        let (tab, vp) = self.tab_with_config()?;

        auto_observe_if_needed(tab, target_str, vp).await?;
        let resolved = resolve_target(tab, target_str).await?;
        let js = format!(
            "document.querySelector({})?.dispatchEvent(new MouseEvent('dblclick', {{bubbles:true}}))",
            json_str(&resolved.selector)
        );
        tab.page
            .execute_sync(&js)
            .await
            .map_err(|e| e.to_string())?;
        tab.elements.clear();
        Ok(Response::ok_text(format!(
            "Double-clicked {}",
            resolved.desc
        )))
    }

    async fn cmd_fill(&mut self, args: &Value) -> Result<Response, String> {
        self.ensure_browser().await?;
        let target_str = self.arg_str(args, "target")?;
        let text = self.arg_str(args, "text")?;
        let (tab, vp) = self.tab_with_config()?;

        auto_observe_if_needed(tab, target_str, vp).await?;
        let desc = fill_with_retry(tab, target_str, text, vp).await?;
        tab.elements.clear();
        Ok(Response::ok_text(format!(
            "Filled {} with \"{}\"",
            desc, text
        )))
    }

    async fn cmd_select(&mut self, args: &Value) -> Result<Response, String> {
        let target_str = self.arg_str(args, "target")?;
        let value = self.arg_str(args, "value")?;
        let (tab, vp) = self.tab_with_config()?;

        auto_observe_if_needed(tab, target_str, vp).await?;
        let resolved = resolve_target(tab, target_str).await?;
        let arg = json!({ "sel": resolved.selector, "val": value });
        let js = format!(
            r#"(() => {{
                const arg = {arg};
                const sel = document.querySelector(arg.sel);
                if (!sel) return false;
                const opt = Array.from(sel.options).find(o => o.value === arg.val || o.text === arg.val);
                if (!opt) return false;
                sel.value = opt.value;
                sel.dispatchEvent(new Event('change', {{ bubbles: true }}));
                return true;
            }})()"#,
            arg = serde_json::to_string(&arg).map_err(|e| e.to_string())?
        );
        let selected: bool = tab.page.evaluate(&js).await.map_err(|e| e.to_string())?;
        if !selected {
            return Err(format!(
                "Option \"{}\" not found in {}",
                value, resolved.desc
            ));
        }
        let _ = wait_for_stable(&tab.page).await;
        tab.elements.clear();
        Ok(Response::ok_text(format!(
            "Selected \"{}\" in {}",
            value, resolved.desc
        )))
    }

    async fn cmd_hover(&mut self, args: &Value) -> Result<Response, String> {
        let target_str = self.arg_str(args, "target")?;
        let (tab, vp) = self.tab_with_config()?;

        auto_observe_if_needed(tab, target_str, vp).await?;
        let resolved = resolve_target(tab, target_str).await?;
        let cx = resolved.bbox.x + resolved.bbox.width / 2.0;
        let cy = resolved.bbox.y + resolved.bbox.height / 2.0;
        tab.page
            .session()
            .dispatch_mouse_event(eoka::cdp::MouseEventType::MouseMoved, cx, cy, None, None)
            .await
            .map_err(|e| e.to_string())?;
        Ok(Response::ok_text(format!("Hovered {}", resolved.desc)))
    }

    async fn cmd_key(&mut self, args: &Value) -> Result<Response, String> {
        let key = self.arg_str(args, "key")?;
        let tab = self.require_tab()?;
        tab.page
            .human()
            .press_key(key)
            .await
            .map_err(|e| e.to_string())?;
        Ok(Response::ok_text(format!("Pressed {}", key)))
    }

    async fn cmd_scroll(&mut self, args: &Value) -> Result<Response, String> {
        let target_str = self.arg_str(args, "target")?;
        let (tab, vp) = self.tab_with_config()?;

        match target_str {
            "up" => tab.page.execute_sync("window.scrollBy(0, -window.innerHeight * 0.8)").await,
            "down" => tab.page.execute_sync("window.scrollBy(0, window.innerHeight * 0.8)").await,
            "top" => tab.page.execute_sync("window.scrollTo(0, 0)").await,
            "bottom" => tab.page.execute_sync("window.scrollTo(0, document.body.scrollHeight)").await,
            _ => {
                auto_observe_if_needed(tab, target_str, vp).await?;
                let resolved = resolve_target(tab, target_str).await?;
                let js = format!(
                    "document.querySelector({})?.scrollIntoView({{behavior:'smooth',block:'center'}})",
                    json_str(&resolved.selector)
                );
                tab.page.execute_sync(&js).await
            }
        }
        .map_err(|e| e.to_string())?;
        Ok(Response::ok_text(format!("Scrolled {}", target_str)))
    }

    // ── JavaScript ──────────────────────────────────────────────────────

    async fn cmd_eval(&mut self, args: &Value) -> Result<Response, String> {
        let code = resolve_js(args)?;
        let max_size = args["max_size"].as_u64().map(|v| v as usize);
        let tab = self.require_tab()?;

        // Truncate server-side in JS to avoid CDP message size crashes
        let js = match max_size {
            Some(max) => format!(
                "(() => {{ const r = JSON.stringify(eval({})); return r.length > {} ? r.slice(0, {}) + '...(truncated ' + r.length + ' chars)' : r; }})()",
                json_str(&code), max, max
            ),
            None => format!("JSON.stringify(eval({}))", json_str(&code)),
        };
        let result: String = tab
            .page
            .evaluate_sync(&js)
            .await
            .map_err(|e| e.to_string())?;
        Ok(Response::ok_text(result))
    }

    async fn cmd_exec(&mut self, args: &Value) -> Result<Response, String> {
        let code = resolve_js(args)?;
        let tab = self.require_tab()?;
        let _: String = tab.page.evaluate_sync(&code).await.unwrap_or_default();
        Ok(Response::ok_text("Executed successfully"))
    }

    // ── Network ─────────────────────────────────────────────────────────

    async fn cmd_fetch(&mut self, args: &Value) -> Result<Response, String> {
        let url = self.arg_str(args, "url")?;
        let method = args["method"].as_str().unwrap_or("GET");
        let redirect = args["redirect"].as_str().unwrap_or("follow");
        let max_body = args["max_body"].as_u64().unwrap_or(8192) as usize;

        let headers_json = args
            .get("headers")
            .filter(|v| !v.is_null())
            .map(|h| serde_json::to_string(h).unwrap_or_else(|_| "null".into()))
            .unwrap_or_else(|| "null".into());
        let body_json = args
            .get("body")
            .filter(|v| !v.is_null())
            .map(|b| serde_json::to_string(b).unwrap_or_else(|_| "null".into()))
            .unwrap_or_else(|| "null".into());

        let js = format!(
            r#"(async () => {{
                try {{
                    const opts = {{ method: {method}, credentials: 'include', redirect: {redirect} }};
                    const h = {headers};
                    if (h) opts.headers = h;
                    const b = {body};
                    if (b) opts.body = b;
                    const r = await fetch({url}, opts);
                    const hdrs = {{}};
                    r.headers.forEach((v,k) => hdrs[k] = v);
                    let text = '';
                    try {{ text = await r.text(); }} catch(e) {{}}
                    if ({max_body} > 0 && text.length > {max_body}) text = text.slice(0, {max_body}) + '...(truncated)';
                    return JSON.stringify({{ status: r.status, statusText: r.statusText, type: r.type, url: r.url, headers: hdrs, body: text }});
                }} catch(e) {{
                    return JSON.stringify({{ error: e.message }});
                }}
            }})()"#,
            method = json_str(method),
            redirect = json_str(redirect),
            headers = headers_json,
            body = body_json,
            url = json_str(url),
            max_body = max_body,
        );

        let tab = self.require_tab()?;
        let result: String = tab.page.evaluate(&js).await.map_err(|e| e.to_string())?;
        let parsed: Value = serde_json::from_str(&result).unwrap_or(Value::String(result));
        Ok(Response::ok(parsed))
    }

    // ── Cookies ─────────────────────────────────────────────────────────

    async fn cmd_cookies(&mut self) -> Result<Response, String> {
        let tab = self.require_tab()?;
        let cookies = tab.page.cookies().await.map_err(|e| e.to_string())?;
        let json: Vec<Value> = cookies
            .into_iter()
            .map(|c| {
                json!({
                    "name": c.name, "value": c.value, "domain": c.domain,
                    "path": c.path, "httpOnly": c.http_only, "secure": c.secure,
                    "sameSite": c.same_site, "expires": c.expires,
                })
            })
            .collect();
        Ok(Response::ok(Value::Array(json)))
    }

    async fn cmd_set_cookie(&mut self, args: &Value) -> Result<Response, String> {
        let name = self.arg_str(args, "name")?;
        let value = self.arg_str(args, "value")?;
        let path = args["path"].as_str().unwrap_or("/");

        let tab = self.require_tab()?;
        let domain = match args["domain"].as_str() {
            Some(d) => d.to_string(),
            None => tab
                .page
                .evaluate_sync("location.hostname")
                .await
                .unwrap_or_default(),
        };

        let cookie = eoka::cdp::types::NetworkSetCookie {
            name: name.to_string(),
            value: value.to_string(),
            url: None,
            domain: Some(domain),
            path: Some(path.to_string()),
            secure: None,
            http_only: None,
            same_site: None,
            expires: None,
        };
        tab.page
            .set_cookies_bulk(vec![cookie])
            .await
            .map_err(|e| e.to_string())?;
        Ok(Response::ok_text(format!("Set cookie: {}={}", name, value)))
    }

    async fn cmd_delete_cookie(&mut self, args: &Value) -> Result<Response, String> {
        let name = self.arg_str(args, "name")?;
        let tab = self.require_tab()?;
        let domain = match args["domain"].as_str() {
            Some(d) => d.to_string(),
            None => tab
                .page
                .evaluate_sync::<String>("location.hostname")
                .await
                .unwrap_or_default(),
        };
        tab.page
            .session()
            .send::<_, Value>(
                "Network.deleteCookies",
                &json!({ "name": name, "domain": domain }),
            )
            .await
            .map_err(|e| e.to_string())?;
        Ok(Response::ok_text(format!("Deleted cookie: {}", name)))
    }

    async fn cmd_clear_cookies(&mut self) -> Result<Response, String> {
        let tab = self.require_tab()?;
        tab.page
            .clear_all_cookies()
            .await
            .map_err(|e| e.to_string())?;
        Ok(Response::ok_text("Cleared all cookies"))
    }

    // ── Storage ─────────────────────────────────────────────────────────

    async fn cmd_storage(&mut self, args: &Value) -> Result<Response, String> {
        let key = args["key"].as_str();
        let session = args["session_storage"].as_bool().unwrap_or(false);
        let s = if session {
            "sessionStorage"
        } else {
            "localStorage"
        };
        let tab = self.require_tab()?;

        if let Some(k) = key {
            let js = format!("{}.getItem({})", s, json_str(k));
            let val: Value = tab.page.evaluate(&js).await.map_err(|e| e.to_string())?;
            Ok(Response::ok(val))
        } else {
            let js = format!("JSON.stringify(Object.fromEntries(Object.entries({})))", s);
            let json_str: String = tab.page.evaluate_sync(&js).await.unwrap_or_default();
            let parsed: Value = serde_json::from_str(&json_str).unwrap_or(Value::String(json_str));
            Ok(Response::ok(parsed))
        }
    }

    async fn cmd_set_storage(&mut self, args: &Value) -> Result<Response, String> {
        let key = self.arg_str(args, "key")?;
        let value = self.arg_str(args, "value")?;
        let session = args["session_storage"].as_bool().unwrap_or(false);
        let s = if session {
            "sessionStorage"
        } else {
            "localStorage"
        };
        let tab = self.require_tab()?;
        let js = format!("{}.setItem({}, {})", s, json_str(key), json_str(value));
        tab.page
            .execute_sync(&js)
            .await
            .map_err(|e| e.to_string())?;
        Ok(Response::ok_text(format!(
            "Set {}.{} = \"{}\"",
            s, key, value
        )))
    }

    async fn cmd_dump_storage(&mut self) -> Result<Response, String> {
        let tab = self.require_tab()?;
        let combined: String = tab
            .page
            .evaluate_sync(
                "JSON.stringify({\
                    localStorage: Object.fromEntries(Object.entries(localStorage)),\
                    sessionStorage: Object.fromEntries(Object.entries(sessionStorage))\
                })",
            )
            .await
            .unwrap_or_else(|_| "{}".to_string());
        let parsed: Value = serde_json::from_str(&combined).unwrap_or_else(|_| json!({}));
        Ok(Response::ok(parsed))
    }

    // ── State save/load ─────────────────────────────────────────────────

    async fn cmd_save_state(&mut self, args: &Value) -> Result<Response, String> {
        let path = self.arg_str(args, "path")?;
        let tab = self.require_tab()?;
        let state = capture_state(&tab.page).await?;
        let json = serde_json::to_string_pretty(&state).map_err(|e| e.to_string())?;
        std::fs::write(path, &json).map_err(|e| e.to_string())?;
        Ok(Response::ok_text(format!(
            "Saved state to {} ({} cookies, {} localStorage, {} sessionStorage)",
            path,
            state.cookies.len(),
            state.local_storage.len(),
            state.session_storage.len()
        )))
    }

    async fn cmd_load_state(&mut self, args: &Value) -> Result<Response, String> {
        let path = self.arg_str(args, "path")?;
        let no_navigate = args["no_navigate"].as_bool().unwrap_or(false);

        let json = std::fs::read_to_string(path).map_err(|e| e.to_string())?;
        let saved: SavedState = serde_json::from_str(&json).map_err(|e| e.to_string())?;

        self.ensure_browser().await?;
        let state = self.require_state_mut()?;

        if !no_navigate && !saved.url.is_empty() {
            state
                .ensure_tab(&saved.url)
                .await
                .map_err(|e| e.to_string())?;
        }

        let tab = state.current_tab_mut().ok_or("No tab open.")?;
        restore_state(&tab.page, &saved).await?;
        tab.invalidate();

        Ok(Response::ok_text(format!(
            "Loaded state from {} ({} cookies, {} localStorage, {} sessionStorage)",
            path,
            saved.cookies.len(),
            saved.local_storage.len(),
            saved.session_storage.len()
        )))
    }

    // ── Headers ─────────────────────────────────────────────────────────

    async fn cmd_headers(&mut self, args: &Value) -> Result<Response, String> {
        let raw = &args["headers_json"];
        let headers: HashMap<String, String> = if let Some(s) = raw.as_str() {
            serde_json::from_str(s).map_err(|_| "Invalid headers JSON")?
        } else {
            serde_json::from_value(raw.clone()).map_err(|_| "Invalid headers JSON")?
        };

        let tab = self.require_tab()?;
        tab.page
            .set_extra_headers(headers.clone())
            .await
            .map_err(|e| e.to_string())?;
        Ok(Response::ok_text(format!(
            "Set {} extra headers",
            headers.len()
        )))
    }

    // ── Console / Errors ────────────────────────────────────────────────

    async fn cmd_console(&mut self, args: &Value) -> Result<Response, String> {
        let clear = args["clear"].as_bool().unwrap_or(false);
        let level = args["level"].as_str();

        let tab = self.require_tab_mut()?;
        ensure_console_capture(tab).await?;

        let js = match level {
            Some(l) => format!(
                "JSON.stringify((__eoka_console || []).filter(e => e.level === {}))",
                json_str(l)
            ),
            None => "JSON.stringify(__eoka_console || [])".into(),
        };
        let result: String = tab
            .page
            .evaluate_sync(&js)
            .await
            .unwrap_or_else(|_| "[]".into());
        if clear {
            let _: String = tab
                .page
                .evaluate_sync("__eoka_console = []")
                .await
                .unwrap_or_default();
        }
        let parsed: Value = serde_json::from_str(&result).unwrap_or(Value::Array(vec![]));
        Ok(Response::ok(parsed))
    }

    async fn cmd_errors(&mut self, args: &Value) -> Result<Response, String> {
        let clear = args["clear"].as_bool().unwrap_or(false);
        let tab = self.require_tab_mut()?;
        ensure_console_capture(tab).await?;

        let result: String = tab
            .page
            .evaluate_sync("JSON.stringify(__eoka_errors || [])")
            .await
            .unwrap_or_else(|_| "[]".into());
        if clear {
            let _: String = tab
                .page
                .evaluate_sync("__eoka_errors = []")
                .await
                .unwrap_or_default();
        }
        let parsed: Value = serde_json::from_str(&result).unwrap_or(Value::Array(vec![]));
        Ok(Response::ok(parsed))
    }

    // ── Tabs ────────────────────────────────────────────────────────────

    async fn cmd_tab_list(&mut self) -> Result<Response, String> {
        let state = self.state.as_ref().ok_or("No browser open.")?;
        let tabs = state.browser.tabs().await.map_err(|e| e.to_string())?;
        let current_id = state.current_tab_id.as_deref();

        let mut out = String::new();
        for tab in tabs {
            let marker = if Some(tab.id.as_str()) == current_id {
                " *"
            } else {
                ""
            };
            let _ = writeln!(out, "[{}]{} {}\n  {}", tab.id, marker, tab.title, tab.url);
        }
        Ok(Response::ok_text(if out.is_empty() {
            "No tabs open.".into()
        } else {
            out
        }))
    }

    async fn cmd_tab_new(&mut self, args: &Value) -> Result<Response, String> {
        self.ensure_browser().await?;
        let url = args["url"].as_str();
        let state = self.require_state_mut()?;
        let (tab_id, tab) = state.new_tab(url).await.map_err(|e| e.to_string())?;
        let url = tab.page.url().await.map_err(|e| e.to_string())?;
        let title = title_nonblocking(&tab.page).await;
        Ok(Response::ok_text(format!(
            "Opened new tab [{}]\nURL: {}\nTitle: {}",
            tab_id, url, title
        )))
    }

    async fn cmd_tab_switch(&mut self, args: &Value) -> Result<Response, String> {
        let tab_id = self.arg_str(args, "tab_id")?;
        let state = self.require_state_mut()?;
        state.switch_tab(tab_id).await.map_err(|e| e.to_string())?;
        let tab = state.current_tab().ok_or("Tab switch failed")?;
        let url = tab.page.url().await.map_err(|e| e.to_string())?;
        let title = title_nonblocking(&tab.page).await;
        Ok(Response::ok_text(format!(
            "Switched to tab [{}]\nURL: {}\nTitle: {}",
            tab_id, url, title
        )))
    }

    async fn cmd_tab_attach(&mut self, args: &Value) -> Result<Response, String> {
        let tab_id = self.arg_str(args, "tab_id")?.to_string();
        self.ensure_browser().await?;
        let state = self.require_state_mut()?;
        state
            .attach_existing_tab(&tab_id)
            .await
            .map_err(|e| e.to_string())?;
        let tab = state.current_tab().ok_or("Attach failed")?;
        let url = tab.page.url().await.map_err(|e| e.to_string())?;
        let title = title_nonblocking(&tab.page).await;
        Ok(Response::ok_text(format!(
            "Attached to tab [{}]\nURL: {}\nTitle: {}",
            tab_id, url, title
        )))
    }

    async fn cmd_clone_from(&mut self, args: &Value) -> Result<Response, String> {
        let source = self.arg_str(args, "source")?.to_string();
        let to = args["to"].as_str().map(std::path::PathBuf::from);

        let saved = persist::clone_state_from_source(&source).await?;

        if let Some(path) = to {
            let json = serde_json::to_string_pretty(&saved).map_err(|e| e.to_string())?;
            std::fs::write(&path, &json).map_err(|e| e.to_string())?;
            return Ok(Response::ok_text(format!(
                "Saved state from {} to {} ({} cookies, {} localStorage, {} sessionStorage)",
                source,
                path.display(),
                saved.cookies.len(),
                saved.local_storage.len(),
                saved.session_storage.len()
            )));
        }

        // No --to: load directly into the active session.
        self.ensure_browser().await?;
        let state = self.require_state_mut()?;
        let url = saved.url.clone();
        let tab = if !url.is_empty() {
            state.ensure_tab(&url).await.map_err(|e| e.to_string())?
        } else {
            state.ensure_blank_tab().await.map_err(|e| e.to_string())?
        };
        restore_state(&tab.page, &saved).await?;
        tab.invalidate();
        Ok(Response::ok_text(format!(
            "Hydrated session from {} ({} cookies, {} localStorage, {} sessionStorage)",
            source,
            saved.cookies.len(),
            saved.local_storage.len(),
            saved.session_storage.len()
        )))
    }

    async fn cmd_tab_close(&mut self, args: &Value) -> Result<Response, String> {
        let tab_id = self.arg_str(args, "tab_id")?;
        let state = self.require_state_mut()?;
        state.close_tab(tab_id).await.map_err(|e| e.to_string())?;
        Ok(Response::ok_text(format!("Closed tab [{}]", tab_id)))
    }

    // ── Wait ────────────────────────────────────────────────────────────

    async fn cmd_wait(&mut self, args: &Value) -> Result<Response, String> {
        if let Some(ms) = args["ms"].as_u64() {
            let ms = ms.min(30_000);
            tokio::time::sleep(std::time::Duration::from_millis(ms)).await;
            return Ok(Response::ok_text(format!("Waited {}ms", ms)));
        }
        let timeout = args["timeout"].as_u64().unwrap_or(10_000);
        let tab = self.require_tab()?;

        if let Some(text) = args["text"].as_str() {
            tab.page
                .wait_for_text(text, timeout)
                .await
                .map_err(|e| e.to_string())?;
            return Ok(Response::ok_text(format!("Text \"{}\" appeared", text)));
        }
        if let Some(url_pat) = args["url"].as_str() {
            tab.page
                .wait_for_url_contains(url_pat, timeout)
                .await
                .map_err(|e| e.to_string())?;
            return Ok(Response::ok_text(format!("URL matched \"{}\"", url_pat)));
        }
        if args["load"].as_str().is_some() {
            tab.page
                .wait_for_network_idle(500, timeout)
                .await
                .map_err(|e| e.to_string())?;
            return Ok(Response::ok_text("Network idle"));
        }
        Err("Provide ms, --text, --url, or --load".into())
    }

    // ── SPA ─────────────────────────────────────────────────────────────

    async fn cmd_spa_info(&mut self) -> Result<Response, String> {
        let tab = self.require_tab()?;
        let info = eoka_agent::spa::detect_router(&tab.page)
            .await
            .map_err(|e: eoka::Error| e.to_string())?;
        Ok(Response::ok(json!({
            "router_type": format!("{:?}", info.router_type),
            "current_path": info.current_path,
            "can_navigate": info.can_navigate,
        })))
    }

    async fn cmd_spa_navigate(&mut self, args: &Value) -> Result<Response, String> {
        let path = self.arg_str(args, "path")?;
        let tab = self.require_tab_mut()?;
        let info = eoka_agent::spa::detect_router(&tab.page)
            .await
            .map_err(|e: eoka::Error| e.to_string())?;
        eoka_agent::spa::spa_navigate(&tab.page, &info.router_type, path)
            .await
            .map_err(|e: eoka::Error| e.to_string())?;
        let _ = wait_for_stable(&tab.page).await;
        tab.invalidate();
        let url = tab.page.url().await.map_err(|e| e.to_string())?;
        Ok(Response::ok_text(format!("SPA navigated to: {}", url)))
    }

    // ── Fake Camera ──────────────────────────────────────────────────────

    async fn cmd_fake_camera(&mut self, args: &Value) -> Result<Response, String> {
        let file_path = self.arg_str(args, "file")?;
        let loop_video = args["loop_video"].as_bool().unwrap_or(false);

        let data = std::fs::read(file_path)
            .map_err(|e| format!("Failed to read video file '{}': {}", file_path, e))?;

        let mime = if file_path.ends_with(".webm") {
            "video/webm"
        } else {
            "video/mp4"
        };

        let b64 = base64::engine::general_purpose::STANDARD.encode(&data);

        let template = include_str!("../js/fake_camera.js");
        let js = template
            .replace("/*VIDEO_DATA*/", &b64)
            .replace("/*MIME_TYPE*/", mime)
            .replace("/*LOOP*/", if loop_video { "true" } else { "false" });

        self.ensure_browser().await?;
        let tab = self.require_tab_mut()?;

        // Register for all future navigations
        tab.page
            .session()
            .send::<_, Value>(
                "Page.addScriptToEvaluateOnNewDocument",
                &json!({ "source": js }),
            )
            .await
            .map_err(|e| e.to_string())?;

        // Inject into current page too
        let _: String = tab.page.evaluate_sync(&js).await.unwrap_or_default();

        // Grant camera permission
        let _ = tab
            .page
            .session()
            .send::<_, Value>(
                "Browser.grantPermissions",
                &json!({ "permissions": ["videoCapture", "audioCapture"] }),
            )
            .await;

        Ok(Response::ok_text(format!(
            "Fake camera injected ({}KB {}, loop={})",
            data.len() / 1024,
            mime,
            loop_video
        )))
    }

    // ── WASM ────────────────────────────────────────────────────────────

    async fn cmd_wasm_info(&mut self) -> Result<Response, String> {
        let tab = self.require_tab()?;
        let js = include_str!("../js/wasm_info.js");
        let result: String = tab
            .page
            .evaluate_sync(js)
            .await
            .map_err(|e| e.to_string())?;
        let parsed: Value = serde_json::from_str(&result).unwrap_or(Value::Array(vec![]));
        Ok(Response::ok(parsed))
    }

    async fn cmd_wasm_read(&mut self, args: &Value) -> Result<Response, String> {
        let addr = parse_addr(self.arg_str(args, "addr")?)?;
        let len = args["len"].as_u64().ok_or("Missing 'len'")? as usize;
        let memory = args["memory"].as_str();

        let tab = self.require_tab()?;
        let mem_expr = wasm_memory_expr(memory);

        // Read in 64KB chunks to avoid CDP message size issues
        let chunk_size = 65536;
        let mut hex_output = String::new();
        let mut offset = 0;

        while offset < len {
            let chunk_len = (len - offset).min(chunk_size);
            let js = format!(
                r#"(() => {{
                    const mem = {mem};
                    if (!mem || !mem.buffer) return null;
                    const buf = new Uint8Array(mem.buffer, {addr} + {offset}, {chunk_len});
                    return Array.from(buf).map(b => b.toString(16).padStart(2, '0')).join('');
                }})()"#,
                mem = mem_expr,
                addr = addr,
                offset = offset,
                chunk_len = chunk_len,
            );
            let chunk: String = tab
                .page
                .evaluate_sync(&js)
                .await
                .map_err(|e| e.to_string())?;
            if chunk == "null" || chunk.is_empty() {
                return Err(format!(
                    "WASM memory not found or address out of bounds (tried {})",
                    mem_expr
                ));
            }
            hex_output.push_str(&chunk);
            offset += chunk_len;
        }

        // Format as hex dump
        let mut formatted = String::new();
        let bytes_per_line = 16;
        for (i, chunk) in hex_output.as_bytes().chunks(bytes_per_line * 2).enumerate() {
            let line_addr = addr + i * bytes_per_line;
            use std::fmt::Write;
            let _ = write!(formatted, "{:08x}  ", line_addr);
            for (j, pair) in chunk.chunks(2).enumerate() {
                if j == 8 {
                    formatted.push(' ');
                }
                formatted.push(pair[0] as char);
                formatted.push(pair[1] as char);
                formatted.push(' ');
            }
            formatted.push('\n');
        }

        Ok(Response::ok_text(formatted))
    }

    async fn cmd_wasm_write(&mut self, args: &Value) -> Result<Response, String> {
        let addr = parse_addr(self.arg_str(args, "addr")?)?;
        let hex = self.arg_str(args, "hex")?;
        let memory = args["memory"].as_str();

        // Normalize hex: remove spaces, ensure even length
        let hex_clean: String = hex.chars().filter(|c| c.is_ascii_hexdigit()).collect();
        if !hex_clean.len().is_multiple_of(2) {
            return Err("Hex string must have even length".into());
        }

        let mem_expr = wasm_memory_expr(memory);
        let js = format!(
            r#"(() => {{
                const mem = {mem};
                if (!mem || !mem.buffer) return 'error: WASM memory not found';
                const bytes = new Uint8Array([{byte_array}]);
                new Uint8Array(mem.buffer).set(bytes, {addr});
                return 'ok';
            }})()"#,
            mem = mem_expr,
            addr = addr,
            byte_array = hex_to_byte_array(&hex_clean),
        );

        let tab = self.require_tab()?;
        let result: String = tab
            .page
            .evaluate_sync(&js)
            .await
            .map_err(|e| e.to_string())?;
        if result.starts_with("error:") {
            return Err(result);
        }

        Ok(Response::ok_text(format!(
            "Wrote {} bytes at 0x{:x}",
            hex_clean.len() / 2,
            addr
        )))
    }

    async fn cmd_wasm_find(&mut self, args: &Value) -> Result<Response, String> {
        let pattern = self.arg_str(args, "pattern")?;
        let memory = args["memory"].as_str();
        let max = args["max"].as_u64().unwrap_or(20) as usize;
        let start = args["start"]
            .as_str()
            .map(parse_addr)
            .transpose()?
            .unwrap_or(0);
        let end = args["end"].as_str().map(parse_addr).transpose()?;

        let pattern_clean: String = pattern.chars().filter(|c| c.is_ascii_hexdigit()).collect();
        if !pattern_clean.len().is_multiple_of(2) || pattern_clean.is_empty() {
            return Err("Pattern must be non-empty hex with even length".into());
        }

        let mem_expr = wasm_memory_expr(memory);
        let end_expr = match end {
            Some(e) => format!("{}", e),
            None => "buf.length".to_string(),
        };

        let js = format!(
            r#"(() => {{
                const mem = {mem};
                if (!mem || !mem.buffer) return JSON.stringify({{error: 'WASM memory not found'}});
                const buf = new Uint8Array(mem.buffer);
                const pattern = [{byte_array}];
                const results = [];
                const end = Math.min({end_expr}, buf.length);
                for (let i = {start}; i <= end - pattern.length; i++) {{
                    let match = true;
                    for (let j = 0; j < pattern.length; j++) {{
                        if (buf[i + j] !== pattern[j]) {{ match = false; break; }}
                    }}
                    if (match) {{
                        results.push(i);
                        if (results.length >= {max}) break;
                    }}
                }}
                return JSON.stringify({{matches: results, searched: end - {start}}});
            }})()"#,
            mem = mem_expr,
            byte_array = hex_to_byte_array(&pattern_clean),
            start = start,
            end_expr = end_expr,
            max = max,
        );

        let tab = self.require_tab()?;
        let result: String = tab
            .page
            .evaluate_sync(&js)
            .await
            .map_err(|e| e.to_string())?;
        let parsed: Value = serde_json::from_str(&result).unwrap_or(Value::Null);

        if let Some(err) = parsed.get("error").and_then(|v| v.as_str()) {
            return Err(err.to_string());
        }

        let matches = parsed.get("matches").and_then(|v| v.as_array());
        let searched = parsed.get("searched").and_then(|v| v.as_u64()).unwrap_or(0);

        match matches {
            Some(addrs) if !addrs.is_empty() => {
                let mut out = format!(
                    "Found {} matches (searched {} bytes):\n",
                    addrs.len(),
                    searched
                );
                for a in addrs {
                    if let Some(addr) = a.as_u64() {
                        use std::fmt::Write;
                        let _ = writeln!(out, "  0x{:08x}", addr);
                    }
                }
                Ok(Response::ok_text(out))
            }
            _ => Ok(Response::ok_text(format!(
                "No matches found (searched {} bytes)",
                searched
            ))),
        }
    }

    // ── Intercept ───────────────────────────────────────────────────────

    async fn cmd_intercept_add(&mut self, args: &Value) -> Result<Response, String> {
        let url_pattern = self.arg_str(args, "url_pattern")?.to_string();
        let capture = args["capture"].as_str().map(std::path::PathBuf::from);
        let respond = args["respond"].as_str().map(std::path::PathBuf::from);
        let status = args["status"].as_u64().unwrap_or(200) as u16;

        let id = self
            .intercept
            .add_rule(url_pattern.clone(), capture, respond, status);

        // Enable Fetch interception if not already enabled
        if !self.intercept.enabled {
            self.ensure_browser().await?;
            let tab = self.require_tab()?;
            tab.page
                .session()
                .fetch_enable(false)
                .await
                .map_err(|e| e.to_string())?;
            self.intercept.enabled = true;
        }

        Ok(Response::ok_text(format!(
            "Added intercept rule #{} for \"{}\"",
            id, url_pattern
        )))
    }

    async fn cmd_intercept_list(&mut self) -> Result<Response, String> {
        // Drain any pending Fetch events before listing
        self.drain_fetch_events().await;
        Ok(Response::ok(self.intercept.list_json()))
    }

    async fn cmd_intercept_remove(&mut self, args: &Value) -> Result<Response, String> {
        let id = self.arg_str(args, "id")?;
        if id == "all" {
            self.intercept.remove_all();
            if self.intercept.enabled {
                if let Some(ref state) = self.state {
                    if let Some(tab) = state.current_tab() {
                        let _ = tab
                            .page
                            .session()
                            .send::<_, Value>("Fetch.disable", &json!({}))
                            .await;
                    }
                }
                self.intercept.enabled = false;
            }
            Ok(Response::ok_text("Removed all intercept rules"))
        } else {
            let id_num: usize = id.parse().map_err(|_| "Invalid rule ID")?;
            if self.intercept.remove_rule(id_num) {
                Ok(Response::ok_text(format!("Removed rule #{}", id_num)))
            } else {
                Err(format!("Rule #{} not found", id_num))
            }
        }
    }

    async fn cmd_intercept_log(&mut self, args: &Value) -> Result<Response, String> {
        // Drain pending events first
        self.drain_fetch_events().await;
        let clear = args["clear"].as_bool().unwrap_or(false);
        let result = self.intercept.log_json();
        if clear {
            self.intercept.clear_log();
        }
        Ok(Response::ok(result))
    }

    /// Drain pending Fetch.requestPaused CDP events and process them.
    async fn drain_fetch_events(&mut self) {
        if !self.intercept.enabled {
            return;
        }

        // Collect events first to avoid borrow conflict
        let events = {
            let state = match self.state.as_ref() {
                Some(s) => s,
                None => return,
            };
            let tab = match state.current_tab() {
                Some(t) => t,
                None => return,
            };
            let transport = tab.page.session().transport();
            let mut events = Vec::new();
            loop {
                match transport.try_recv_event().await {
                    Some(eoka::cdp::transport::CdpMessage::Event { method, params, .. })
                        if method == "Fetch.requestPaused" =>
                    {
                        events.push(params);
                    }
                    Some(_) => {} // discard non-Fetch events
                    None => break,
                }
            }
            events
        };

        // Process collected events
        for params in events {
            let request_id = match params.get("requestId").and_then(|v| v.as_str()) {
                Some(id) => id.to_string(),
                None => continue,
            };
            let req_field = |name: &str| -> Option<String> {
                params.get("request")?.get(name)?.as_str().map(String::from)
            };
            let url = req_field("url").unwrap_or_default();
            let method = req_field("method").unwrap_or_else(|| "GET".to_string());
            let post_data = req_field("postData");

            // Match rule and extract what we need before borrowing session
            let matched = self.intercept.match_url(&url).map(|r| {
                (
                    r.id,
                    r.capture_path.clone(),
                    r.respond_path.clone(),
                    r.respond_status,
                )
            });

            let session = match self
                .state
                .as_ref()
                .and_then(|s| s.current_tab())
                .map(|t| t.page.session())
            {
                Some(s) => s,
                None => continue,
            };

            if let Some((rule_id, capture_path, respond_path, respond_status)) = matched {
                if let Some(ref path) = capture_path {
                    let body = json!({
                        "url": &url,
                        "method": &method,
                        "postData": &post_data,
                        "headers": params.get("request").and_then(|r| r.get("headers")),
                    });
                    let _ = std::fs::write(
                        path,
                        serde_json::to_string_pretty(&body).unwrap_or_default(),
                    );
                }

                let action = if let Some(ref path) = respond_path {
                    if let Ok(body) = std::fs::read(path) {
                        let body_str = String::from_utf8_lossy(&body);
                        let _ = session
                            .fetch_fulfill(&request_id, respond_status, None, Some(&body_str))
                            .await;
                        "responded"
                    } else {
                        let _ = session
                            .fetch_continue(&request_id, None, None, None, None)
                            .await;
                        "continue (respond file not found)"
                    }
                } else {
                    let _ = session
                        .fetch_continue(&request_id, None, None, None, None)
                        .await;
                    "continue (captured)"
                };

                self.intercept.add_log(InterceptLogEntry {
                    rule_id,
                    url: url.clone(),
                    method: method.clone(),
                    has_body: post_data.is_some(),
                    action: action.to_string(),
                });
            } else {
                let _ = session
                    .fetch_continue(&request_id, None, None, None, None)
                    .await;
            }
        }
    }

    // ── Close ───────────────────────────────────────────────────────────

    async fn cmd_close(&mut self) -> Result<Response, String> {
        if let Some(state) = self.state.take() {
            state.close().await.map_err(|e| e.to_string())?;
        }
        Ok(Response::ok_text("Browser closed"))
    }
}

// ---------------------------------------------------------------------------
// Shared helpers
// ---------------------------------------------------------------------------

fn resolve_js(args: &Value) -> Result<String, String> {
    if let Some(path) = args["file"].as_str() {
        std::fs::read_to_string(path)
            .map_err(|e| format!("Failed to read JS file '{}': {}", path, e))
    } else if let Some(code) = args["code"].as_str() {
        Ok(code.to_string())
    } else {
        Err("Either 'code' or '--file' must be provided".into())
    }
}

/// Parse an address string (supports 0x hex prefix or decimal).
fn parse_addr(s: &str) -> Result<usize, String> {
    let s = s.trim();
    if let Some(hex) = s.strip_prefix("0x").or_else(|| s.strip_prefix("0X")) {
        usize::from_str_radix(hex, 16).map_err(|e| format!("Invalid hex address '{}': {}", s, e))
    } else {
        s.parse()
            .map_err(|e| format!("Invalid address '{}': {}", s, e))
    }
}

/// Build JS expression to access WASM memory. Auto-detects if not specified.
fn wasm_memory_expr(memory: Option<&str>) -> String {
    match memory {
        Some(expr) => expr.to_string(),
        None => {
            // Auto-detect: try common paths
            r#"(window.__ft?.mem || window.Module?.wasmMemory || window.Module?.HEAPU8?.buffer && {buffer: window.Module.HEAPU8.buffer} || (() => { for (const k of Object.keys(window)) { try { if (window[k] instanceof WebAssembly.Memory) return window[k]; } catch(e){} } return null; })())"#.to_string()
        }
    }
}

/// Convert hex string to JS byte array literal: "deadbeef" → "0xde,0xad,0xbe,0xef"
fn hex_to_byte_array(hex: &str) -> String {
    hex.as_bytes()
        .chunks(2)
        .map(|pair| format!("0x{}{}", pair[0] as char, pair[1] as char))
        .collect::<Vec<_>>()
        .join(",")
}