asterai 1.0.0-alpha.16

CLI for asterai - the portable AI compute platform
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
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
use crate::tui::app::{
    App, CORE_COMPONENTS, ChatMessage, ChatState, DynamicItem, MessageRole, PROVIDERS, PickerState,
    SLASH_COMMANDS, SPINNER_FRAMES, Screen, resolve_state_dir, tool_env_vars,
};
use crate::tui::ops;
use crossterm::event::{Event, KeyCode};
use ratatui::prelude::*;
use ratatui::widgets::{Block, Borders, Clear, Paragraph};
use std::time::{Duration, Instant};

const ROBOT: &[&str] = &[
    "     o     ",
    "",
    " ╔═══════╗ ",
    "═╣ ●   ● ╠═",
    " ║  ───  ║ ",
    " ╚═══════╝ ",
];

pub fn render(f: &mut Frame, state: &ChatState, app: &App) {
    let area = f.area();
    let agent = app.agent.as_ref();
    let bot_name = agent.map(|b| b.bot_name.as_str()).unwrap_or("Asterbot");
    // Compute input box height based on visual wrap lines.
    // Inner width = total width - 2 (left/right borders).
    let inner_w = area.width.saturating_sub(2) as usize;
    let input_text = match state.has_env_prompt() {
        true => &state.env_prompt_input,
        false => &state.input,
    };
    let prefix_len = match state.has_env_prompt() {
        true => state
            .env_prompt_vars
            .get(state.env_prompt_idx)
            .map(|v| v.len() + 1) // "VAR="
            .unwrap_or(0),
        false => 2, // "> "
    };
    let content_len = prefix_len + input_text.len() + 1; // +1 for cursor
    let visual_lines = match inner_w > 0 {
        true => content_len.div_ceil(inner_w).max(1) as u16,
        false => 1,
    };
    let input_h = (visual_lines + 2).max(3); // +2 for top/bottom borders
    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Length(12),
            Constraint::Min(4),
            Constraint::Length(input_h),
        ])
        .split(area);
    render_banner(f, bot_name, state, app, chunks[0]);
    let env_name = agent.map(|b| b.env_name.as_str()).unwrap_or("asterbot");
    render_messages(f, state, env_name, chunks[1]);
    // Input box with border (top border acts as separator).
    let input_area = chunks[2];
    let input_block = Block::default()
        .borders(Borders::ALL)
        .border_style(Style::default().fg(Color::DarkGray));
    let input_inner = input_block.inner(input_area);
    f.render_widget(input_block, input_area);
    render_input(f, state, input_inner);
    // Toast notification rendered on the banner bottom-right.
    if let Some(toast) = &state.toast {
        let banner_area = chunks[0];
        let msg = format!(" {} ", toast);
        let msg_w = msg.len() as u16;
        if msg_w + 2 < banner_area.width {
            let toast_area = Rect::new(
                banner_area.x + banner_area.width.saturating_sub(msg_w + 1),
                banner_area.y + banner_area.height.saturating_sub(1),
                msg_w,
                1,
            );
            f.render_widget(
                Paragraph::new(Span::styled(msg, Style::default().fg(state.toast_color))),
                toast_area,
            );
        }
    }
    // Slash menu renders above the input box, overlaying messages.
    let has_dynamic = state.dynamic_command.is_some();
    let has_sub_menu = state.active_command.is_some() && !state.sub_matches.is_empty();
    let has_slash = (state.input.starts_with('/')
        && !state.input.contains(' ')
        && !state.slash_matches.is_empty())
        || has_sub_menu
        || has_dynamic;
    let menu_count = match (has_dynamic, has_sub_menu) {
        (true, _) => match state.dynamic_loading {
            true => 1,
            false => state.dynamic_matches.len(),
        },
        (false, true) => state.sub_matches.len(),
        (false, false) => state.slash_matches.len(),
    };
    let content_h = match has_slash {
        true => menu_count as u16,
        false => 0,
    };
    if content_h > 0 {
        // +2 for top/bottom borders, capped to available message area.
        let max_menu = chunks[1].height;
        let menu_h = (content_h + 2).min(max_menu);
        let menu_area = Rect::new(
            input_area.x + 1,
            input_area.y.saturating_sub(menu_h),
            input_area.width.saturating_sub(2),
            menu_h,
        );
        f.render_widget(Clear, menu_area);
        let block = Block::default()
            .borders(Borders::ALL)
            .border_style(Style::default().fg(Color::DarkGray));
        let inner = block.inner(menu_area);
        f.render_widget(block, menu_area);
        render_slash_menu(f, state, inner);
    }
    // Info overlay renders over the message area.
    if state.info_overlay.is_some() {
        render_info_overlay(f, state, chunks[1]);
    }
}

pub async fn handle_event(app: &mut App, event: Event) -> eyre::Result<()> {
    // Handle paste events.
    if let Event::Paste(text) = &event {
        if let Screen::Chat(state) = &mut app.screen
            && !state.waiting
        {
            state.input.push_str(text);
            update_menus(state);
        }
        return Ok(());
    }
    let Event::Key(key_event) = event else {
        return Ok(());
    };
    // Only handle key press events (not release/repeat) to avoid duplication on Windows.
    if key_event.kind != crossterm::event::KeyEventKind::Press {
        return Ok(());
    }
    // Ignore Ctrl+key combos (e.g. Ctrl+V) to avoid stray characters.
    if key_event
        .modifiers
        .contains(crossterm::event::KeyModifiers::CONTROL)
    {
        return Ok(());
    }
    let code = key_event.code;
    // Pre-capture agent tools for remove picker (avoids borrow conflict with screen).
    let agent_tools: Vec<String> = app
        .agent
        .as_ref()
        .map(|a| a.tools.clone())
        .unwrap_or_default();
    let Screen::Chat(state) = &mut app.screen else {
        return Ok(());
    };
    if state.waiting {
        return Ok(());
    }
    // --- Info overlay handling (scroll or dismiss) ---
    if state.info_overlay.is_some() {
        match code {
            KeyCode::Up => {
                state.info_overlay_scroll = state.info_overlay_scroll.saturating_sub(1);
            }
            KeyCode::Down => {
                if let Some(lines) = &state.info_overlay {
                    let max = (lines.len() as u16).saturating_sub(1);
                    state.info_overlay_scroll = (state.info_overlay_scroll + 1).min(max);
                }
            }
            KeyCode::PageUp => {
                state.info_overlay_scroll = state.info_overlay_scroll.saturating_sub(10);
            }
            KeyCode::PageDown => {
                if let Some(lines) = &state.info_overlay {
                    let max = (lines.len() as u16).saturating_sub(1);
                    state.info_overlay_scroll = (state.info_overlay_scroll + 10).min(max);
                }
            }
            _ => {
                state.info_overlay = None;
                state.info_overlay_scroll = 0;
            }
        }
        return Ok(());
    }
    let has_env_prompt = state.has_env_prompt();
    let has_dynamic = state.dynamic_command.is_some();
    let has_sub_menu = state.active_command.is_some() && !state.sub_matches.is_empty();
    let has_slash_menu = !state.slash_matches.is_empty() || has_sub_menu;
    match code {
        // --- Env var prompt handling ---
        KeyCode::Enter if has_env_prompt => {
            let value = state.env_prompt_input.trim().to_string();
            let var_name = state.env_prompt_vars[state.env_prompt_idx].clone();
            if !value.is_empty() {
                let mut ok = false;
                if let Some(agent) = &app.agent {
                    let env_name = agent.env_name.clone();
                    match ops::set_var(&env_name, &var_name, &value) {
                        Ok(_) => {
                            set_toast_color(app, &format!("{var_name} set."), Color::Green);
                            ok = true;
                        }
                        Err(e) => {
                            push_system(app, &format!("Failed to set {var_name}: {e:#}"));
                            set_toast_color(app, &format!("{var_name} FAILED"), Color::Red);
                        }
                    }
                }
                if !ok && app.agent.is_none() {
                    set_toast(app, &format!("{var_name} skipped (no agent)."));
                }
            } else {
                set_toast(app, &format!("{var_name} skipped."));
            }
            if let Screen::Chat(state) = &mut app.screen {
                state.env_prompt_input.clear();
                state.env_prompt_idx += 1;
                if !state.has_env_prompt() {
                    state.env_prompt_vars.clear();
                    state.env_prompt_idx = 0;
                    // Refresh banner env status after all vars prompted.
                    start_env_check(app);
                }
            }
        }
        KeyCode::Esc if has_env_prompt => {
            let remaining = state.env_prompt_vars.len() - state.env_prompt_idx;
            state.env_prompt_vars.clear();
            state.env_prompt_idx = 0;
            state.env_prompt_input.clear();
            set_toast(
                app,
                &format!("Skipped {remaining} env var(s). /config set KEY=VALUE later."),
            );
        }
        KeyCode::Char(c) if has_env_prompt => {
            state.env_prompt_input.push(c);
        }
        KeyCode::Backspace if has_env_prompt => {
            state.env_prompt_input.pop();
        }
        // --- Dynamic picker navigation ---
        KeyCode::Up if has_dynamic && !state.dynamic_loading => {
            if state.dynamic_selected > 0 {
                state.dynamic_selected -= 1;
            } else if !state.dynamic_matches.is_empty() {
                // At top — confirm selection (same as Enter).
                let item_idx = state.dynamic_matches[state.dynamic_selected];
                let item = &state.dynamic_items[item_idx];
                if item.disabled {
                    return Ok(());
                }
                let cmd = state.dynamic_command.clone().unwrap();
                let full = format!("/{} {}", cmd, item.value);
                clear_dynamic_state(state);
                dispatch_slash(app, &full).await?;
            }
        }
        KeyCode::Down if has_dynamic && !state.dynamic_loading => {
            if state.dynamic_selected + 1 < state.dynamic_matches.len() {
                state.dynamic_selected += 1;
            }
        }
        KeyCode::Enter
            if has_dynamic && !state.dynamic_loading && !state.dynamic_matches.is_empty() =>
        {
            let item_idx = state.dynamic_matches[state.dynamic_selected];
            let item = &state.dynamic_items[item_idx];
            if item.disabled {
                return Ok(());
            }
            let cmd = state.dynamic_command.clone().unwrap();
            let full = format!("/{} {}", cmd, item.value);
            clear_dynamic_state(state);
            dispatch_slash(app, &full).await?;
        }
        KeyCode::Char(c) if has_dynamic && !state.dynamic_loading => {
            state.input.push(c);
            update_dynamic_filter(state);
        }
        KeyCode::Backspace if has_dynamic && !state.dynamic_loading => {
            state.input.pop();
            update_dynamic_filter(state);
        }
        KeyCode::Esc if has_dynamic => {
            clear_dynamic_state(state);
            app.pending_components = None;
        }
        // --- Sub-menu navigation ---
        KeyCode::Up if has_sub_menu => {
            if state.sub_selected > 0 {
                state.sub_selected -= 1;
            } else {
                // At top — confirm selection (same as Enter/Tab).
                let cmd_idx = state.active_command.unwrap();
                let sub_idx = state.sub_matches[state.sub_selected];
                let sub = &SLASH_COMMANDS[cmd_idx].subs[sub_idx];
                let cmd_name = SLASH_COMMANDS[cmd_idx].name;
                if sub.needs_arg && cmd_name == "tools" {
                    enter_tools_picker(app, sub.name, &agent_tools);
                } else if sub.needs_arg {
                    enter_sub_arg_input(state, cmd_name, sub.name);
                } else {
                    dispatch_sub_command(app, cmd_name, sub.name).await?;
                }
            }
        }
        KeyCode::Down if has_sub_menu => {
            if state.sub_selected + 1 < state.sub_matches.len() {
                state.sub_selected += 1;
            }
        }
        KeyCode::Enter | KeyCode::Tab if has_sub_menu => {
            let cmd_idx = state.active_command.unwrap();
            let sub_idx = state.sub_matches[state.sub_selected];
            let sub = &SLASH_COMMANDS[cmd_idx].subs[sub_idx];
            let cmd_name = SLASH_COMMANDS[cmd_idx].name;
            if sub.needs_arg && cmd_name == "tools" {
                enter_tools_picker(app, sub.name, &agent_tools);
            } else if sub.needs_arg {
                enter_sub_arg_input(state, cmd_name, sub.name);
            } else {
                dispatch_sub_command(app, cmd_name, sub.name).await?;
            }
        }
        KeyCode::Esc if has_sub_menu => {
            state.input.clear();
            clear_menu_state(state);
        }
        // --- Top-level slash menu navigation ---
        KeyCode::Up if has_slash_menu => {
            if state.slash_selected > 0 {
                state.slash_selected -= 1;
            } else if state.slash_selected < state.slash_matches.len() {
                // At top — confirm selection (same as Enter/Tab).
                let cmd_idx = state.slash_matches[state.slash_selected];
                let cmd = &SLASH_COMMANDS[cmd_idx];
                if !cmd.subs.is_empty() {
                    state.input = format!("/{} ", cmd.name);
                    state.active_command = Some(cmd_idx);
                    state.sub_matches = (0..cmd.subs.len()).collect();
                    state.sub_selected = 0;
                    state.slash_matches.clear();
                    state.slash_selected = 0;
                } else {
                    state.input = format!("/{}", cmd.name);
                    state.slash_matches.clear();
                    state.slash_selected = 0;
                    let input = state.input.clone();
                    state.input.clear();
                    dispatch_slash(app, &input).await?;
                }
            }
        }
        KeyCode::Down if has_slash_menu => {
            if state.slash_selected + 1 < state.slash_matches.len() {
                state.slash_selected += 1;
            }
        }
        KeyCode::Enter | KeyCode::Tab
            if has_slash_menu && state.slash_selected < state.slash_matches.len() =>
        {
            let cmd_idx = state.slash_matches[state.slash_selected];
            let cmd = &SLASH_COMMANDS[cmd_idx];
            if !cmd.subs.is_empty() {
                // Has subcommands → enter sub-menu.
                state.input = format!("/{} ", cmd.name);
                state.active_command = Some(cmd_idx);
                state.sub_matches = (0..cmd.subs.len()).collect();
                state.sub_selected = 0;
                state.slash_matches.clear();
                state.slash_selected = 0;
            } else {
                // No subcommands → dispatch immediately.
                state.input = format!("/{}", cmd.name);
                state.slash_matches.clear();
                state.slash_selected = 0;
                let input = state.input.clone();
                state.input.clear();
                dispatch_slash(app, &input).await?;
            }
        }
        KeyCode::Esc if has_slash_menu => {
            state.slash_matches.clear();
            state.slash_selected = 0;
            state.input.clear();
        }
        KeyCode::Up if !has_slash_menu => {
            if !state.input_history.is_empty() {
                let idx = match state.history_idx {
                    Some(i) if i > 0 => i - 1,
                    Some(i) => i,
                    None => state.input_history.len() - 1,
                };
                state.history_idx = Some(idx);
                state.input = state.input_history[idx].clone();
            }
        }
        KeyCode::Down if !has_slash_menu => {
            if let Some(idx) = state.history_idx {
                if idx + 1 < state.input_history.len() {
                    let new_idx = idx + 1;
                    state.history_idx = Some(new_idx);
                    state.input = state.input_history[new_idx].clone();
                } else {
                    state.history_idx = None;
                    state.input.clear();
                }
            }
        }
        KeyCode::Enter => {
            let input = state.input.trim().to_string();
            if input.is_empty() {
                return Ok(());
            }
            state.input_history.push(input.clone());
            state.history_idx = None;
            state.input.clear();
            state.slash_matches.clear();
            if input.starts_with('/') {
                dispatch_slash(app, &input).await?;
            } else {
                send_message(app, &input);
            }
        }
        KeyCode::Char(c) => {
            let Screen::Chat(state) = &mut app.screen else {
                return Ok(());
            };
            state.input.push(c);
            update_menus(state);
        }
        KeyCode::Backspace => {
            state.input.pop();
            update_menus(state);
        }
        KeyCode::Esc => {
            if !state.input.is_empty() {
                // Clear input first.
                state.input.clear();
                state.slash_matches.clear();
                state.slash_selected = 0;
            } else {
                // Empty input — return to agent picker.
                app.agent = None;
                app.runtime = None;
                app.pending_response = None;
                app.pending_banner = None;
                app.pending_components = None;
                // Restore saved picker state instantly (no loading screen).
                if let Some(agents) = app.saved_picker.take() {
                    app.screen = Screen::Picker(PickerState {
                        agents,
                        selected: 0,
                        loading: false,
                        error: None,
                        spinner_tick: 0,
                    });
                } else {
                    app.screen = Screen::Picker(PickerState::loading(0));
                }
            }
        }
        _ => {}
    }
    Ok(())
}

fn render_banner(f: &mut Frame, name: &str, chat: &ChatState, app: &App, area: Rect) {
    let agent = app.agent.as_ref();
    let banner_text = &chat.banner_text;
    let banner_loading = chat.banner_loading;
    let model = agent.and_then(|b| b.model.as_deref()).unwrap_or("not set");
    let tools = agent.map(|b| &b.tools).cloned().unwrap_or_default();
    let dirs_count = agent.map(|b| b.allowed_dirs.len()).unwrap_or(0);
    let title = format!(" {} ", name.to_uppercase());
    let block = Block::default()
        .title(title)
        .borders(Borders::ALL)
        .border_style(Style::default().fg(Color::Cyan));
    let inner = block.inner(area);
    f.render_widget(block, area);
    let cols = Layout::default()
        .direction(Direction::Horizontal)
        .constraints([Constraint::Length(26), Constraint::Min(20)])
        .split(inner);
    // Vertical divider.
    let divider_x = cols[0].x + cols[0].width;
    for y in inner.y..inner.y + inner.height {
        if divider_x < area.x + area.width - 1 {
            let cell = ratatui::buffer::Cell::default()
                .set_char('')
                .set_style(Style::default().fg(Color::Cyan))
                .clone();
            if let Some(c) = f
                .buffer_mut()
                .cell_mut(ratatui::layout::Position::new(divider_x, y))
            {
                *c = cell;
            }
        }
    }
    // Left column: greeting + robot.
    let greeting_name = agent
        .map(|a| a.user_name.as_str())
        .filter(|s| !s.is_empty())
        .unwrap_or("USER");
    let mut left_lines: Vec<Line> = Vec::new();
    left_lines.push(
        Line::from(Span::styled(
            format!("GREETINGS, {}.", greeting_name.to_uppercase()),
            Style::default().fg(Color::Cyan).bold(),
        ))
        .alignment(Alignment::Center),
    );
    left_lines.push(
        Line::from(Span::styled(
            "SHALL WE PLAY A GAME?",
            Style::default().fg(Color::Cyan).bold(),
        ))
        .alignment(Alignment::Center),
    );
    left_lines.push(Line::from(""));
    for line in ROBOT {
        left_lines.push(
            Line::from(Span::styled(*line, Style::default().fg(Color::Cyan)))
                .alignment(Alignment::Center),
        );
    }
    f.render_widget(Paragraph::new(left_lines), cols[0]);
    // Right column: info.
    let right_area = Rect::new(
        cols[1].x + 1,
        cols[1].y,
        cols[1].width.saturating_sub(1),
        cols[1].height,
    );
    let label_style = Style::default().fg(Color::Rgb(120, 120, 140));
    let value_style = Style::default().fg(Color::Rgb(180, 180, 200));
    let tool_green = Style::default().fg(Color::Rgb(80, 220, 120));
    let tool_orange = Style::default().fg(Color::Rgb(255, 165, 0));
    let tool_plain = Style::default().fg(Color::Rgb(140, 140, 160));
    let sep_style = Style::default().fg(Color::Rgb(80, 80, 100));
    let env_status = &chat.tool_env_status;
    let env_loaded = !env_status.is_empty() || app.pending_env_check.is_none();

    let mut right_lines: Vec<Line> = Vec::new();
    right_lines.push(Line::from(vec![
        Span::styled("MODEL   ", label_style),
        Span::styled(model, value_style),
    ]));
    // Tools: inline across the page, color-coded by env var status.
    if tools.is_empty() {
        right_lines.push(Line::from(vec![
            Span::styled("TOOLS   ", label_style),
            Span::styled("none", Style::default().fg(Color::DarkGray)),
        ]));
    } else {
        // Build styled spans for each tool name.
        let mut tool_spans: Vec<(String, Style)> = Vec::new();
        for full_name in &tools {
            let short = full_name
                .split(':')
                .next_back()
                .unwrap_or(full_name)
                .to_string();
            let env_vars = tool_env_vars(full_name);
            let required_vars: Vec<_> = env_vars.iter().filter(|v| v.required).collect();
            let style = if required_vars.is_empty() || !env_loaded {
                tool_plain
            } else {
                let all_set = required_vars
                    .iter()
                    .all(|v| env_status.get(v.name).copied().unwrap_or(false));
                match all_set {
                    true => tool_green,
                    false => tool_orange,
                }
            };
            tool_spans.push((short, style));
        }
        // Wrap tool names across lines to fit available width.
        let label_w = 8; // "TOOLS   ".len()
        let avail = right_area.width.saturating_sub(2) as usize;
        let tool_max = avail.saturating_sub(label_w);
        let sep = " · ";
        let mut first_line = true;
        let mut line_spans: Vec<Span> = Vec::new();
        let mut line_len: usize = 0;
        for (i, (name, style)) in tool_spans.iter().enumerate() {
            let needed = match i == 0 || line_len == 0 {
                true => name.len(),
                false => sep.len() + name.len(),
            };
            if line_len > 0 && line_len + needed > tool_max {
                // Flush current line.
                let prefix = match first_line {
                    true => "TOOLS   ",
                    false => "        ",
                };
                let mut spans = vec![Span::styled(prefix, label_style)];
                spans.append(&mut line_spans);
                right_lines.push(Line::from(spans));
                line_spans = Vec::new();
                line_len = 0;
                first_line = false;
            }
            if line_len > 0 {
                line_spans.push(Span::styled(sep, sep_style));
                line_len += sep.len();
            }
            line_spans.push(Span::styled(name.clone(), *style));
            line_len += name.len();
        }
        if !line_spans.is_empty() {
            let prefix = match first_line {
                true => "TOOLS   ",
                false => "        ",
            };
            let mut spans = vec![Span::styled(prefix, label_style)];
            spans.append(&mut line_spans);
            right_lines.push(Line::from(spans));
        }
    }
    if dirs_count > 0 {
        let dirs_label = match dirs_count {
            1 => "1 folder".to_string(),
            n => format!("{n} folders"),
        };
        right_lines.push(Line::from(vec![
            Span::styled("DIRS    ", label_style),
            Span::styled(dirs_label, value_style),
        ]));
    }
    right_lines.push(Line::from(""));
    right_lines.push(Line::from(vec![
        Span::styled("Type ", Style::default().fg(Color::Rgb(90, 90, 110))),
        Span::styled("/", Style::default().fg(Color::Cyan)),
        Span::styled(
            " for commands · ",
            Style::default().fg(Color::Rgb(90, 90, 110)),
        ),
        Span::styled("Esc", Style::default().fg(Color::Cyan)),
        Span::styled(" to go back", Style::default().fg(Color::Rgb(90, 90, 110))),
    ]));
    // Banner content (quote or tool data).
    if !banner_text.is_empty() {
        right_lines.push(Line::from(""));
        let display = match banner_loading {
            true => format!("{banner_text} ..."),
            false => banner_text.to_string(),
        };
        let display = match display.len() > 120 {
            true => format!("{}...", &display[..117]),
            false => display,
        };
        let max_w = right_area.width.saturating_sub(2) as usize;
        for wrapped in textwrap(&display, max_w) {
            right_lines.push(Line::from(Span::styled(
                wrapped,
                Style::default().fg(Color::Rgb(100, 100, 120)).italic(),
            )));
        }
    }
    f.render_widget(Paragraph::new(right_lines), right_area);
}

fn render_messages(f: &mut Frame, state: &ChatState, env_name: &str, area: Rect) {
    if state.messages.is_empty() && !state.waiting {
        let text = Paragraph::new(Span::styled(
            "Send a message to start chatting.",
            Style::default().fg(Color::DarkGray),
        ))
        .alignment(Alignment::Center);
        let centered = Rect::new(area.x, area.y + area.height / 2, area.width, 1);
        f.render_widget(text, centered);
        return;
    }
    let assistant_prefix = format!("{env_name}: ");
    let mut lines: Vec<Line> = Vec::new();
    for msg in &state.messages {
        let (prefix, style) = match msg.role {
            MessageRole::User => ("You: ", Style::default().fg(Color::Cyan)),
            MessageRole::Assistant => {
                (assistant_prefix.as_str(), Style::default().fg(Color::White))
            }
            MessageRole::System => ("", Style::default().fg(Color::Yellow)),
        };
        lines.push(Line::from(""));
        if let Some(styled) = &msg.styled_lines {
            lines.extend(styled.iter().cloned());
        } else {
            for text_line in msg.content.lines() {
                let is_first_line = text_line == msg.content.lines().next().unwrap_or("");
                let line_prefix = match is_first_line {
                    true => prefix,
                    false => "",
                };
                lines.push(Line::from(vec![
                    Span::styled(line_prefix, style.bold()),
                    Span::styled(text_line, style),
                ]));
            }
        }
    }
    if state.waiting {
        let frame = SPINNER_FRAMES[state.spinner_tick % SPINNER_FRAMES.len()];
        lines.push(Line::from(""));
        lines.push(Line::from(vec![
            Span::styled(format!("{frame} "), Style::default().fg(Color::Cyan)),
            Span::styled("Thinking...", Style::default().fg(Color::DarkGray)),
        ]));
    }
    let total_lines = lines.len() as u16;
    let visible = area.height;
    let scroll = total_lines.saturating_sub(visible);
    let text = Paragraph::new(lines).scroll((scroll, 0));
    f.render_widget(text, area);
}

fn render_input(f: &mut Frame, state: &ChatState, area: Rect) {
    // Blinking cursor: toggle every ~500ms (5 ticks at 100ms).
    let cursor_visible = (state.spinner_tick / 5).is_multiple_of(2);
    let cursor_ch = match cursor_visible {
        true => "_",
        false => " ",
    };
    let w = area.width as usize;
    if w == 0 {
        return;
    }
    if state.has_env_prompt() {
        let var_name = &state.env_prompt_vars[state.env_prompt_idx];
        let remaining = state.env_prompt_vars.len() - state.env_prompt_idx;
        let ghost = Style::default().fg(Color::Rgb(60, 60, 70));
        let hint_text = match remaining > 1 {
            true => format!("  ({remaining} remaining, Esc to skip)"),
            false => "  (Esc to skip)".to_string(),
        };
        let placeholder = match state.env_prompt_input.is_empty() {
            true => "paste your API key here",
            false => "",
        };
        let mut spans: Vec<Span<'static>> = vec![
            Span::styled(
                format!("{var_name}="),
                Style::default().fg(Color::Yellow).bold(),
            ),
            Span::styled(
                state.env_prompt_input.clone(),
                Style::default().fg(Color::White),
            ),
            Span::styled(cursor_ch.to_string(), Style::default().fg(Color::White)),
        ];
        if !placeholder.is_empty() {
            spans.push(Span::styled(placeholder.to_string(), ghost));
        }
        spans.push(Span::styled(hint_text, Style::default().fg(Color::White)));
        f.render_widget(Paragraph::new(Line::from(spans)), area);
    } else if state.input.is_empty() {
        // Ghost placeholder text with blinking cursor.
        let ghost = Style::default().fg(Color::Rgb(60, 60, 70));
        let line = Line::from(vec![
            Span::styled("> ", Style::default().fg(Color::Cyan)),
            Span::styled(cursor_ch.to_string(), Style::default().fg(Color::White)),
            Span::styled(" start typing here or ", ghost),
            Span::styled(" / ", ghost.bold()),
            Span::styled("for commands", ghost),
        ]);
        f.render_widget(Paragraph::new(line), area);
    } else {
        let full = format!("> {}{cursor_ch}", state.input);
        let lines = wrap_input_chars(&full, w);
        let mut out: Vec<Line<'static>> = Vec::new();
        for chunk in lines {
            if out.is_empty() {
                // First line: color "> " cyan.
                out.push(Line::from(vec![
                    Span::styled("> ".to_string(), Style::default().fg(Color::Cyan)),
                    Span::raw(chunk[2..].to_string()),
                ]));
            } else {
                out.push(Line::from(Span::raw(chunk)));
            }
        }
        f.render_widget(Paragraph::new(out), area);
    }
}

/// Break a string into chunks of at most `w` characters.
fn wrap_input_chars(s: &str, w: usize) -> Vec<String> {
    if w == 0 {
        return vec![s.to_string()];
    }
    let chars: Vec<char> = s.chars().collect();
    chars.chunks(w).map(|c| c.iter().collect()).collect()
}

fn render_slash_menu(f: &mut Frame, state: &ChatState, area: Rect) {
    let max_rows = area.height as usize;

    // Dynamic picker mode: show browsable items.
    if state.dynamic_command.is_some() {
        if state.dynamic_loading {
            let line = Line::from(vec![
                Span::raw("  "),
                Span::styled(
                    "  Loading...",
                    Style::default().fg(Color::DarkGray).italic(),
                ),
            ]);
            f.render_widget(Paragraph::new(vec![line]), area);
            return;
        }
        if state.dynamic_matches.is_empty() {
            let line = Line::from(vec![
                Span::raw("  "),
                Span::styled("  (no matches)", Style::default().fg(Color::DarkGray)),
            ]);
            f.render_widget(Paragraph::new(vec![line]), area);
            return;
        }
        let skip = state
            .dynamic_selected
            .saturating_sub(max_rows.saturating_sub(1));
        let mut lines: Vec<Line> = Vec::new();
        for (i, &item_idx) in state
            .dynamic_matches
            .iter()
            .enumerate()
            .skip(skip)
            .take(max_rows)
        {
            let item = &state.dynamic_items[item_idx];
            let is_selected = i == state.dynamic_selected;
            let pointer = match is_selected {
                true => "",
                false => "  ",
            };
            let (name_style, desc_style) = if item.disabled {
                (
                    Style::default().fg(Color::DarkGray),
                    Style::default().fg(Color::DarkGray),
                )
            } else {
                (
                    match is_selected {
                        true => Style::default().fg(Color::Cyan).bold(),
                        false => Style::default().fg(Color::Cyan),
                    },
                    match is_selected {
                        true => Style::default().fg(Color::White),
                        false => Style::default().fg(Color::DarkGray),
                    },
                )
            };
            lines.push(Line::from(vec![
                Span::raw("  "),
                Span::raw(pointer),
                Span::styled(format!("{:<20}", item.label), name_style),
                Span::styled(&item.description, desc_style),
            ]));
        }
        f.render_widget(Paragraph::new(lines), area);
        return;
    }

    // Sub-menu mode: show sub-options for the active command.
    if let Some(cmd_idx) = state.active_command {
        let cmd = &SLASH_COMMANDS[cmd_idx];
        let skip = state
            .sub_selected
            .saturating_sub(max_rows.saturating_sub(1));
        let mut lines: Vec<Line> = Vec::new();
        for (i, &sub_idx) in state
            .sub_matches
            .iter()
            .enumerate()
            .skip(skip)
            .take(max_rows)
        {
            let sub = &cmd.subs[sub_idx];
            let is_selected = i == state.sub_selected;
            let pointer = match is_selected {
                true => "",
                false => "  ",
            };
            let name_style = match is_selected {
                true => Style::default().fg(Color::Cyan).bold(),
                false => Style::default().fg(Color::Cyan),
            };
            let desc_style = match is_selected {
                true => Style::default().fg(Color::White),
                false => Style::default().fg(Color::DarkGray),
            };
            lines.push(Line::from(vec![
                Span::raw("  "),
                Span::raw(pointer),
                Span::styled(format!("{:<12}", sub.name), name_style),
                Span::styled(sub.description, desc_style),
            ]));
        }
        f.render_widget(Paragraph::new(lines), area);
        return;
    }

    // Top-level slash command menu.
    let skip = state
        .slash_selected
        .saturating_sub(max_rows.saturating_sub(1));
    let mut lines: Vec<Line> = Vec::new();
    for (i, &cmd_idx) in state
        .slash_matches
        .iter()
        .enumerate()
        .skip(skip)
        .take(max_rows)
    {
        let cmd = &SLASH_COMMANDS[cmd_idx];
        let is_selected = i == state.slash_selected;
        let pointer = match is_selected {
            true => "",
            false => "  ",
        };
        let name_style = match is_selected {
            true => Style::default().fg(Color::Cyan).bold(),
            false => Style::default().fg(Color::Cyan),
        };
        let desc_style = match is_selected {
            true => Style::default().fg(Color::White),
            false => Style::default().fg(Color::DarkGray),
        };
        lines.push(Line::from(vec![
            Span::raw("  "),
            Span::raw(pointer),
            Span::styled(format!("/{:<12}", cmd.name), name_style),
            Span::styled(cmd.description, desc_style),
        ]));
    }
    f.render_widget(Paragraph::new(lines), area);
}

fn render_info_overlay(f: &mut Frame, state: &ChatState, area: Rect) {
    let lines = match &state.info_overlay {
        Some(lines) => lines,
        None => return,
    };
    // Size overlay to fit content, capped at the available message area.
    // +2 for top/bottom borders.
    let content_h = (lines.len() as u16) + 2;
    let h = content_h.min(area.height);
    let overlay = Rect::new(
        area.x + 1,
        area.y + area.height.saturating_sub(h),
        area.width.saturating_sub(2),
        h,
    );
    f.render_widget(Clear, overlay);
    let block = Block::default()
        .title(" Info ")
        .title_alignment(Alignment::Left)
        .borders(Borders::ALL)
        .border_style(Style::default().fg(Color::DarkGray));
    let inner = block.inner(overlay);
    f.render_widget(block, overlay);
    let total_lines = lines.len() as u16;
    let scroll = state.info_overlay_scroll;
    let content = Paragraph::new(lines.clone()).scroll((scroll, 0));
    f.render_widget(content, inner);
    // Show dismiss hint on the bottom border.
    let hint = match total_lines > inner.height {
        true => " ↑↓ scroll · any key to close ",
        false => " any key to close ",
    };
    let hint_w = hint.len() as u16;
    if hint_w + 2 < overlay.width {
        let hint_area = Rect::new(
            overlay.x + overlay.width.saturating_sub(hint_w + 1),
            overlay.y + overlay.height.saturating_sub(1),
            hint_w,
            1,
        );
        f.render_widget(
            Paragraph::new(Span::styled(hint, Style::default().fg(Color::DarkGray))),
            hint_area,
        );
    }
}

/// Kick off an async banner content fetch if banner_mode is "auto".
pub fn start_banner_fetch(app: &mut App) {
    let Some(agent) = &app.agent else { return };
    if agent.banner_mode != "auto" {
        return;
    }
    if let Screen::Chat(state) = &mut app.screen {
        state.banner_loading = true;
    }
    let runtime = app.runtime.clone();
    let (tx, rx) = tokio::sync::oneshot::channel();
    app.pending_banner = Some(rx);
    tokio::spawn(async move {
        let prompt = "In one brief line (under 80 chars), infer a relevant status \
            update from your conversation history and available tools. If there is \
            nothing relevant, reply with an empty string.";
        let result = match runtime {
            Some(rt) => {
                match tokio::task::spawn(async move { ops::call_with_runtime(rt, prompt).await })
                    .await
                {
                    Ok(r) => r.ok().flatten(),
                    Err(_) => None,
                }
            }
            None => None,
        };
        let _ = tx.send(result);
    });
}

/// Kick off an async env-var check so the banner can show per-tool status.
pub fn start_env_check(app: &mut App) {
    let Some(agent) = &app.agent else { return };
    let env_name = agent.env_name.clone();
    let tools = agent.tools.clone();
    let (tx, rx) = tokio::sync::oneshot::channel();
    app.pending_env_check = Some(rx);
    tokio::spawn(async move {
        let var_values = ops::inspect_environment(&env_name)
            .await
            .ok()
            .flatten()
            .map(|d| d.var_values)
            .unwrap_or_default();
        let mut status = std::collections::HashMap::new();
        for tool in &tools {
            for v in tool_env_vars(tool) {
                status.insert(v.name.to_string(), var_values.contains_key(v.name));
            }
        }
        let _ = tx.send(status);
    });
}

fn send_message(app: &mut App, input: &str) {
    let Screen::Chat(state) = &mut app.screen else {
        return;
    };
    state.messages.push(ChatMessage {
        role: MessageRole::User,
        content: input.to_string(),
        styled_lines: None,
    });
    state.waiting = true;
    let runtime = app.runtime.clone();
    let message = input.to_string();
    let (tx, rx) = tokio::sync::oneshot::channel();
    app.pending_response = Some(rx);
    tokio::spawn(async move {
        let result = match runtime {
            Some(rt) => {
                match tokio::task::spawn(async move { ops::call_with_runtime(rt, &message).await })
                    .await
                {
                    Ok(r) => r,
                    Err(e) => {
                        let panic_msg = if let Ok(s) = e.try_into_panic() {
                            s.downcast_ref::<String>()
                                .cloned()
                                .or_else(|| s.downcast_ref::<&str>().map(|s| s.to_string()))
                                .unwrap_or_else(|| "internal error".to_string())
                        } else {
                            "request was cancelled".to_string()
                        };
                        Err(eyre::eyre!("{panic_msg}"))
                    }
                }
            }
            None => Err(eyre::eyre!("no active runtime")),
        };
        let _ = tx.send(result);
    });
}

async fn dispatch_slash(app: &mut App, input: &str) -> eyre::Result<()> {
    let parts: Vec<&str> = input[1..].split_whitespace().collect();
    if parts.is_empty() {
        return Ok(());
    }
    let cmd = parts[0].to_lowercase();
    let args = &parts[1..];
    match cmd.as_str() {
        "help" | "h" | "?" => cmd_help(app),
        "tools" | "t" => cmd_tools(app, args).await,
        "clear" | "c" => cmd_clear(app),
        "model" | "m" => cmd_model(app, args),
        "name" | "rename" => cmd_name(app, args),
        "me" | "whoami" => cmd_me(app, args),
        "dir" | "dirs" => cmd_dir(app, args),
        "status" | "info" => cmd_status(app).await,
        "banner" => cmd_banner(app, args),
        "push" => cmd_push(app).await,
        "pull" | "sync" => cmd_pull(app).await,
        "config" | "vars" => cmd_config(app, args).await,
        "quit" | "exit" | "q" => {
            app.should_quit = true;
            Ok(())
        }
        _ => {
            push_system(
                app,
                &format!("Unknown command: /{cmd}. Type /help for commands."),
            );
            Ok(())
        }
    }
}

/// Reset all dynamic picker, sub-menu, slash menu, and input state.
fn clear_dynamic_state(state: &mut ChatState) {
    state.dynamic_items.clear();
    state.dynamic_matches.clear();
    state.dynamic_selected = 0;
    state.dynamic_command = None;
    state.dynamic_loading = false;
    state.active_command = None;
    state.sub_matches.clear();
    state.sub_selected = 0;
    state.slash_matches.clear();
    state.slash_selected = 0;
    state.input.clear();
}

/// Open the dynamic picker for `/tools add` or `/tools remove`.
fn enter_tools_picker(app: &mut App, sub_name: &str, agent_tools: &[String]) {
    if let Screen::Chat(state) = &mut app.screen {
        state.input.clear();
        clear_menu_state(state);
        if sub_name == "add" {
            state.dynamic_command = Some("tools add".to_string());
            state.dynamic_loading = true;
        } else if sub_name == "remove" {
            state.dynamic_command = Some("tools remove".to_string());
            state.dynamic_items = build_remove_items(agent_tools);
            state.dynamic_matches = (0..state.dynamic_items.len()).collect();
            state.dynamic_selected = 0;
        }
    }
    if sub_name == "add" {
        start_component_fetch(app);
    }
}

/// Fill input with `/{cmd} {sub} ` and clear menu state for sub-commands needing a typed argument.
fn enter_sub_arg_input(state: &mut ChatState, cmd_name: &str, sub_name: &str) {
    state.input = format!("/{cmd_name} {sub_name} ");
    clear_menu_state(state);
}

/// Clear menu state and dispatch a no-arg sub-command.
async fn dispatch_sub_command(app: &mut App, cmd_name: &str, sub_name: &str) -> eyre::Result<()> {
    if let Screen::Chat(state) = &mut app.screen {
        clear_menu_state(state);
        state.input.clear();
    }
    let full = format!("/{cmd_name} {sub_name}");
    dispatch_slash(app, &full).await
}

/// Clear active_command, sub_matches, and slash menu state.
fn clear_menu_state(state: &mut ChatState) {
    state.active_command = None;
    state.sub_matches.clear();
    state.sub_selected = 0;
    state.slash_matches.clear();
    state.slash_selected = 0;
}

fn update_menus(state: &mut ChatState) {
    // If we're in a sub-menu, filter sub-options.
    if let Some(cmd_idx) = state.active_command {
        let cmd = &SLASH_COMMANDS[cmd_idx];
        let prefix = format!("/{} ", cmd.name);
        if state.input.starts_with(&prefix) {
            let partial = state.input[prefix.len()..].to_lowercase();
            // If user typed past the sub-command (has a space after sub), close menu.
            if partial.contains(' ') {
                state.active_command = None;
                state.sub_matches.clear();
                state.sub_selected = 0;
            } else {
                state.sub_matches = cmd
                    .subs
                    .iter()
                    .enumerate()
                    .filter(|(_, sub)| sub.name.starts_with(partial.as_str()))
                    .map(|(i, _)| i)
                    .collect();
                state.sub_selected = state
                    .sub_selected
                    .min(state.sub_matches.len().saturating_sub(1));
            }
        } else {
            // Input no longer matches the command prefix — exit sub-menu.
            state.active_command = None;
            state.sub_matches.clear();
            state.sub_selected = 0;
            // Fall through to update top-level menu.
            update_top_level_menu(state);
        }
        return;
    }
    update_top_level_menu(state);
}

fn update_top_level_menu(state: &mut ChatState) {
    if state.input.starts_with('/') && !state.input.contains(' ') {
        let partial = &state.input[1..].to_lowercase();
        state.slash_matches = SLASH_COMMANDS
            .iter()
            .enumerate()
            .filter(|(_, cmd)| cmd.name.starts_with(partial.as_str()))
            .map(|(i, _)| i)
            .collect();
        state.slash_selected = state
            .slash_selected
            .min(state.slash_matches.len().saturating_sub(1));
    } else {
        state.slash_matches.clear();
        state.slash_selected = 0;
    }
}

fn push_system(app: &mut App, msg: &str) {
    if let Screen::Chat(state) = &mut app.screen {
        state.messages.push(ChatMessage {
            role: MessageRole::System,
            content: msg.to_string(),
            styled_lines: None,
        });
    }
}

/// Show an ephemeral toast on the separator line (auto-dismisses after ~3s).
fn set_toast(app: &mut App, msg: &str) {
    set_toast_color(app, msg, Color::Yellow);
}

fn set_toast_color(app: &mut App, msg: &str, color: Color) {
    if let Screen::Chat(state) = &mut app.screen {
        state.toast = Some(msg.to_string());
        state.toast_color = color;
        state.toast_until = Some(Instant::now() + Duration::from_secs(3));
    }
}

fn cmd_help(app: &mut App) -> eyre::Result<()> {
    let mut lines: Vec<Line<'static>> = Vec::new();
    lines.push(Line::from(Span::styled(
        "Commands:",
        Style::default().fg(Color::Yellow).bold(),
    )));
    for cmd in SLASH_COMMANDS {
        lines.push(Line::from(vec![
            Span::styled(
                format!("  /{:<12}", cmd.name),
                Style::default().fg(Color::Cyan),
            ),
            Span::styled(
                cmd.description.to_string(),
                Style::default().fg(Color::White),
            ),
        ]));
        for sub in cmd.subs {
            lines.push(Line::from(vec![
                Span::styled(
                    format!("    {:<10}", sub.name),
                    Style::default().fg(Color::DarkGray),
                ),
                Span::styled(
                    sub.description.to_string(),
                    Style::default().fg(Color::DarkGray),
                ),
            ]));
        }
    }
    app.show_info_overlay(lines);
    Ok(())
}

async fn cmd_tools(app: &mut App, args: &[&str]) -> eyre::Result<()> {
    let tools = app
        .agent
        .as_ref()
        .map(|b| &b.tools)
        .cloned()
        .unwrap_or_default();
    if args.is_empty() || args[0] == "list" {
        let mut lines: Vec<Line<'static>> = Vec::new();
        lines.push(Line::from(Span::styled(
            "Enabled tools:",
            Style::default().fg(Color::Yellow).bold(),
        )));
        if tools.is_empty() {
            lines.push(Line::from(Span::styled(
                "  (none)",
                Style::default().fg(Color::DarkGray),
            )));
        } else {
            let env_name = app.agent.as_ref().map(|a| a.env_name.clone());
            let var_values = if let Some(ref name) = env_name {
                ops::inspect_environment(name)
                    .await
                    .ok()
                    .flatten()
                    .map(|d| d.var_values)
                    .unwrap_or_default()
            } else {
                std::collections::HashMap::new()
            };
            let mut warnings: Vec<Line<'static>> = Vec::new();
            for tool in &tools {
                lines.push(Line::from(Span::styled(
                    format!("  {tool}"),
                    Style::default().fg(Color::White),
                )));
                let needed = tool_env_vars(tool);
                let missing: Vec<_> = needed
                    .iter()
                    .filter(|v| v.required && !var_values.contains_key(v.name))
                    .collect();
                if !missing.is_empty() {
                    let vars = missing
                        .iter()
                        .map(|v| v.name)
                        .collect::<Vec<_>>()
                        .join(", ");
                    warnings.push(Line::from(Span::styled(
                        format!("  {tool}: {vars}"),
                        Style::default().fg(Color::Rgb(255, 165, 0)),
                    )));
                }
            }
            if !warnings.is_empty() {
                lines.push(Line::from(""));
                lines.push(Line::from(Span::styled(
                    "\u{26A0} Missing config:",
                    Style::default().fg(Color::Rgb(255, 165, 0)),
                )));
                lines.extend(warnings);
                lines.push(Line::from(Span::styled(
                    "  Use /config set KEY=VALUE to configure",
                    Style::default().fg(Color::DarkGray),
                )));
            }
        }
        lines.push(Line::from(""));
        lines.push(Line::from(Span::styled(
            "/tools add <ns:component>  Add a tool",
            Style::default().fg(Color::DarkGray),
        )));
        lines.push(Line::from(Span::styled(
            "/tools remove <component>  Remove a tool",
            Style::default().fg(Color::DarkGray),
        )));
        app.show_info_overlay(lines);
        return Ok(());
    }
    let Some(agent) = &app.agent else {
        push_system(app, "No active agent.");
        return Ok(());
    };
    let env_name = agent.env_name.clone();
    if args[0] == "add" && args.len() > 1 {
        // Auto-prepend user namespace if missing (e.g. "trello" → "seadog:trello").
        let component = match args[1].contains(':') {
            true => args[1].to_string(),
            false => {
                let ns = crate::auth::Auth::read_user_or_fallback_namespace();
                format!("{ns}:{}", args[1])
            }
        };
        match ops::add_component(&env_name, &component).await {
            Ok(_) => {
                if let Some(agent) = &mut app.agent {
                    let base = component.split('@').next().unwrap_or(&component);
                    if !agent.tools.contains(&base.to_string()) {
                        agent.tools.push(base.to_string());
                    }
                }
                save_tools(app);
                let env_vars = tool_env_vars(&component);
                if env_vars.is_empty() {
                    set_toast(app, &format!("+ {component}"));
                    start_env_check(app);
                } else {
                    // Check which vars are already set.
                    let data = ops::inspect_environment(&env_name).await.ok().flatten();
                    let var_values = data.map(|d| d.var_values).unwrap_or_default();
                    let missing: Vec<String> = env_vars
                        .iter()
                        .filter(|v| !var_values.contains_key(v.name))
                        .map(|v| v.name.to_string())
                        .collect();
                    if missing.is_empty() {
                        set_toast(app, &format!("+ {component}"));
                    } else {
                        let short = component.split(':').next_back().unwrap_or(&component);
                        set_toast(app, &format!("+ {short} (configure env vars below)"));
                        // Start the inline env-var prompt flow.
                        if let Screen::Chat(state) = &mut app.screen {
                            state.env_prompt_vars = missing;
                            state.env_prompt_idx = 0;
                            state.env_prompt_input.clear();
                        }
                    }
                }
            }
            Err(e) => push_system(app, &format!("Failed to add: {e:#}")),
        }
        return Ok(());
    }
    if (args[0] == "remove" || args[0] == "rm") && args.len() > 1 {
        let component = args[1];
        match ops::remove_component(&env_name, component).await {
            Ok(_) => {
                let base = component.split('@').next().unwrap_or(component);
                if let Some(agent) = &mut app.agent {
                    agent.tools.retain(|t| t != base);
                }
                save_tools(app);
                // Clean up env vars associated with the removed tool.
                let env_vars = tool_env_vars(component);
                let mut removed_vars: Vec<&str> = Vec::new();
                for v in env_vars {
                    if ops::set_var(&env_name, v.name, "").is_ok() {
                        removed_vars.push(v.name);
                    }
                }
                if removed_vars.is_empty() {
                    set_toast(app, &format!("- {component}"));
                } else {
                    let short = component.split(':').next_back().unwrap_or(component);
                    set_toast(
                        app,
                        &format!("- {short} (cleared {})", removed_vars.join(", ")),
                    );
                }
                start_env_check(app);
            }
            Err(e) => push_system(app, &format!("Failed to remove: {e:#}")),
        }
        return Ok(());
    }
    push_system(app, "Usage: /tools [add|remove] <namespace:component>");
    Ok(())
}

fn cmd_clear(app: &mut App) -> eyre::Result<()> {
    if let Screen::Chat(state) = &mut app.screen {
        state.messages.clear();
    }
    if let Some(agent) = &app.agent {
        let state_dir = resolve_state_dir(&agent.env_name);
        let conv_path = state_dir.join("conversation.json");
        let _ = std::fs::write(conv_path, "[]");
    }
    set_toast(app, "Conversation cleared.");
    Ok(())
}

fn cmd_model(app: &mut App, args: &[&str]) -> eyre::Result<()> {
    if args.is_empty() {
        // Open the model picker.
        let current_model = app.agent.as_ref().and_then(|b| b.model.clone());
        let items = build_model_items(current_model.as_deref());
        // Pre-select the current model.
        let selected = current_model
            .as_deref()
            .and_then(|m| items.iter().position(|item| item.value == m))
            .unwrap_or(0);
        if let Screen::Chat(state) = &mut app.screen {
            state.dynamic_command = Some("model".to_string());
            state.dynamic_items = items;
            state.dynamic_matches = (0..state.dynamic_items.len()).collect();
            state.dynamic_selected = selected;
            state.dynamic_loading = false;
            state.input.clear();
        }
        return Ok(());
    }
    let new_model = args[0].to_string();
    if let Some(agent) = &app.agent {
        let _ = ops::set_var(&agent.env_name, "ASTERBOT_MODEL", &new_model);
    }
    if let Some(agent) = &mut app.agent {
        agent.model = Some(new_model.clone());
    }
    set_toast(app, &format!("Model set to {new_model}"));
    Ok(())
}

fn cmd_name(app: &mut App, args: &[&str]) -> eyre::Result<()> {
    if args.is_empty() {
        let name = app
            .agent
            .as_ref()
            .map(|b| b.bot_name.as_str())
            .unwrap_or("Asterbot");
        app.show_info_overlay(vec![
            Line::from(vec![
                Span::styled("Agent name: ", Style::default().fg(Color::Yellow)),
                Span::styled(name.to_string(), Style::default().fg(Color::White)),
            ]),
            Line::from(""),
            Line::from(Span::styled(
                "Change with: /name <new name>",
                Style::default().fg(Color::DarkGray),
            )),
        ]);
        return Ok(());
    }
    let new_name = args.join(" ");
    if let Some(agent) = &app.agent {
        let _ = ops::set_var(&agent.env_name, "ASTERBOT_BOT_NAME", &new_name);
    }
    if let Some(agent) = &mut app.agent {
        agent.bot_name = new_name.clone();
    }
    set_toast(app, &format!("Agent renamed to {new_name}"));
    Ok(())
}

fn cmd_me(app: &mut App, args: &[&str]) -> eyre::Result<()> {
    if args.is_empty() {
        let name = app
            .agent
            .as_ref()
            .map(|b| b.user_name.as_str())
            .unwrap_or("not set");
        app.show_info_overlay(vec![
            Line::from(vec![
                Span::styled("Display name: ", Style::default().fg(Color::Yellow)),
                Span::styled(name.to_string(), Style::default().fg(Color::White)),
            ]),
            Line::from(""),
            Line::from(Span::styled(
                "Change with: /me <name>",
                Style::default().fg(Color::DarkGray),
            )),
        ]);
        return Ok(());
    }
    let new_name = args.join(" ");
    if let Some(agent) = &app.agent {
        let _ = ops::set_var(&agent.env_name, "ASTERBOT_USER_NAME", &new_name);
    }
    if let Some(agent) = &mut app.agent {
        agent.user_name = new_name.clone();
    }
    set_toast(app, &format!("Display name set to {new_name}"));
    Ok(())
}

fn cmd_banner(app: &mut App, args: &[&str]) -> eyre::Result<()> {
    if args.is_empty() {
        let mode = app
            .agent
            .as_ref()
            .map(|a| a.banner_mode.as_str())
            .unwrap_or("auto");
        app.show_info_overlay(vec![
            Line::from(vec![
                Span::styled("Banner mode: ", Style::default().fg(Color::Yellow)),
                Span::styled(mode.to_string(), Style::default().fg(Color::White)),
            ]),
            Line::from(""),
            Line::from(Span::styled(
                "/banner auto   Agent picks content from tools",
                Style::default().fg(Color::DarkGray),
            )),
            Line::from(Span::styled(
                "/banner quote  Random quotes only",
                Style::default().fg(Color::DarkGray),
            )),
            Line::from(Span::styled(
                "/banner off    No banner content",
                Style::default().fg(Color::DarkGray),
            )),
        ]);
        return Ok(());
    }
    let mode = args[0].to_lowercase();
    if !["auto", "quote", "off"].contains(&mode.as_str()) {
        push_system(app, "Invalid mode. Use: auto, quote, or off");
        return Ok(());
    }
    if let Some(agent) = &app.agent {
        let _ = ops::set_var(&agent.env_name, "ASTERBOT_BANNER", &mode);
    }
    if let Some(agent) = &mut app.agent {
        agent.banner_mode = mode.clone();
    }
    match mode.as_str() {
        "auto" => {
            set_toast(app, "Banner: auto (fetching from tools)");
            start_banner_fetch(app);
        }
        "quote" => {
            if let Screen::Chat(state) = &mut app.screen {
                state.banner_text = crate::tui::app::random_quote().to_string();
                state.banner_loading = false;
            }
            set_toast(app, "Banner: random quotes");
        }
        "off" => {
            if let Screen::Chat(state) = &mut app.screen {
                state.banner_text.clear();
                state.banner_loading = false;
            }
            set_toast(app, "Banner: off");
        }
        _ => {}
    }
    Ok(())
}

fn cmd_dir(app: &mut App, args: &[&str]) -> eyre::Result<()> {
    let dirs = app
        .agent
        .as_ref()
        .map(|b| b.allowed_dirs.clone())
        .unwrap_or_default();
    if args.is_empty() || args[0] == "list" {
        let mut lines: Vec<Line<'static>> = Vec::new();
        lines.push(Line::from(Span::styled(
            "Allowed directories:",
            Style::default().fg(Color::Yellow).bold(),
        )));
        if dirs.is_empty() {
            lines.push(Line::from(Span::styled(
                "  (none)",
                Style::default().fg(Color::DarkGray),
            )));
        } else {
            for dir in &dirs {
                lines.push(Line::from(Span::styled(
                    format!("  {dir}"),
                    Style::default().fg(Color::White),
                )));
            }
        }
        lines.push(Line::from(""));
        lines.push(Line::from(Span::styled(
            "/dir add <path>     Grant access",
            Style::default().fg(Color::DarkGray),
        )));
        lines.push(Line::from(Span::styled(
            "/dir remove <path>  Revoke access",
            Style::default().fg(Color::DarkGray),
        )));
        app.show_info_overlay(lines);
        return Ok(());
    }
    if args[0] == "add" && args.len() > 1 {
        let path = args[1..].join(" ");
        let resolved = std::path::Path::new(&path)
            .canonicalize()
            .map(|p| p.to_string_lossy().to_string())
            .unwrap_or(path);
        if let Some(agent) = &mut app.agent {
            agent.allowed_dirs.push(resolved.clone());
        }
        save_allowed_dirs(app);
        set_toast(app, &format!("+ {resolved}"));
        return Ok(());
    }
    if (args[0] == "remove" || args[0] == "rm") && args.len() > 1 {
        let path = args[1..].join(" ");
        let resolved = std::path::Path::new(&path)
            .canonicalize()
            .map(|p| p.to_string_lossy().to_string())
            .unwrap_or(path);
        if let Some(agent) = &mut app.agent {
            agent.allowed_dirs.retain(|d| d != &resolved);
        }
        save_allowed_dirs(app);
        set_toast(app, &format!("- {resolved}"));
        return Ok(());
    }
    push_system(app, "Usage: /dir [list|add|remove] <path>");
    Ok(())
}

async fn cmd_status(app: &mut App) -> eyre::Result<()> {
    let Some(agent) = &app.agent else {
        push_system(app, "No active agent.");
        return Ok(());
    };

    let heading = Style::default().fg(Color::Yellow).bold();
    let label = Style::default().fg(Color::Yellow);
    let value = Style::default().fg(Color::White);
    let tool_name_style = Style::default().fg(Color::Rgb(200, 180, 80));
    let ok = Style::default().fg(Color::Green);
    let warn = Style::default().fg(Color::Rgb(255, 165, 0)); // orange

    let mut lines: Vec<Line<'static>> = vec![
        Line::from(Span::styled("Agent Status:", heading)),
        Line::from(vec![
            Span::styled("  Name:        ", label),
            Span::styled(agent.bot_name.clone(), value),
        ]),
        Line::from(vec![
            Span::styled("  Environment: ", label),
            Span::styled(agent.env_name.clone(), value),
        ]),
        Line::from(vec![
            Span::styled("  Model:       ", label),
            Span::styled(
                agent
                    .model
                    .clone()
                    .unwrap_or_else(|| "(not set)".to_string()),
                value,
            ),
        ]),
        Line::from(vec![
            Span::styled("  Provider:    ", label),
            Span::styled(agent.provider.clone(), value),
        ]),
    ];

    if !agent.tools.is_empty() {
        // Fetch env var values for status display.
        let data = ops::inspect_environment(&agent.env_name)
            .await
            .ok()
            .flatten();
        let var_values = data.map(|d| d.var_values).unwrap_or_default();
        lines.push(Line::from(Span::styled("  Tools:", label)));
        for tool in &agent.tools {
            let short = tool.split(':').next_back().unwrap_or(tool).to_string();
            let env_vars = tool_env_vars(tool);
            if env_vars.is_empty() {
                lines.push(Line::from(Span::styled(
                    format!("    {short}"),
                    tool_name_style,
                )));
            } else {
                let padded = format!("{:<16}", short);
                let mut spans: Vec<Span<'static>> =
                    vec![Span::styled(format!("    {padded} "), tool_name_style)];
                for (i, v) in env_vars.iter().enumerate() {
                    if i > 0 {
                        spans.push(Span::styled(", ", tool_name_style));
                    }
                    let suffix = match v.required {
                        true => "",
                        false => " (optional)",
                    };
                    let name = v.name;
                    match var_values.contains_key(name) {
                        true => spans.push(Span::styled(format!("{name}{suffix} \u{2713}"), ok)),
                        false => spans.push(Span::styled(format!("{name}{suffix} \u{2717}"), warn)),
                    }
                }
                lines.push(Line::from(spans));
            }
        }
    }
    if !agent.allowed_dirs.is_empty() {
        lines.push(Line::from(vec![
            Span::styled("  Directories: ", label),
            Span::styled(format!("{} folder(s)", agent.allowed_dirs.len()), value),
        ]));
    }
    app.show_info_overlay(lines);
    Ok(())
}

async fn cmd_push(app: &mut App) -> eyre::Result<()> {
    let Some(agent) = &app.agent else {
        push_system(app, "No active agent.");
        return Ok(());
    };
    let env_name = agent.env_name.clone();
    set_toast(app, &format!("Pushing {env_name}..."));
    match ops::push_env(&env_name).await {
        Ok(_) => set_toast(app, &format!("Pushed {env_name} to cloud.")),
        Err(e) => push_system(app, &format!("Push failed: {e:#}")),
    }
    Ok(())
}

async fn cmd_pull(app: &mut App) -> eyre::Result<()> {
    let Some(agent) = &app.agent else {
        push_system(app, "No active agent.");
        return Ok(());
    };
    let env_name = agent.env_name.clone();
    set_toast(app, &format!("Pulling {env_name}..."));
    match ops::pull_env(&env_name).await {
        Ok(_) => set_toast(app, &format!("Pulled {env_name} from cloud.")),
        Err(e) => push_system(app, &format!("Pull failed: {e:#}")),
    }
    Ok(())
}

async fn cmd_config(app: &mut App, args: &[&str]) -> eyre::Result<()> {
    if args.is_empty() || args[0] == "list" {
        let Some(agent) = &app.agent else {
            push_system(app, "No active agent.");
            return Ok(());
        };
        let env_name = agent.env_name.clone();
        let data = ops::inspect_environment(&env_name).await?;
        let vars = data.map(|d| d.vars).unwrap_or_default();
        let mut lines: Vec<Line<'static>> = Vec::new();
        lines.push(Line::from(Span::styled(
            format!("Environment variables ({env_name}):"),
            Style::default().fg(Color::Yellow).bold(),
        )));
        if vars.is_empty() {
            lines.push(Line::from(Span::styled(
                "  (none)",
                Style::default().fg(Color::DarkGray),
            )));
        } else {
            for v in &vars {
                lines.push(Line::from(Span::styled(
                    format!("  {v}"),
                    Style::default().fg(Color::White),
                )));
            }
        }
        lines.push(Line::from(""));
        lines.push(Line::from(Span::styled(
            "/config set KEY=VALUE  Set a variable",
            Style::default().fg(Color::DarkGray),
        )));
        app.show_info_overlay(lines);
        return Ok(());
    }
    if args[0] == "set" && args.len() > 1 {
        let expr = args[1..].join(" ");
        let Some((key, value)) = expr.split_once('=') else {
            push_system(app, "Usage: /config set KEY=VALUE");
            return Ok(());
        };
        let key = key.trim();
        let value = value.trim();
        let Some(agent) = &app.agent else {
            push_system(app, "No active agent.");
            return Ok(());
        };
        let env_name = agent.env_name.clone();
        match ops::set_var(&env_name, key, value) {
            Ok(_) => {
                set_toast_color(app, &format!("{key} set."), Color::Green);
                start_env_check(app);
            }
            Err(e) => push_system(app, &format!("Failed: {e:#}")),
        }
        return Ok(());
    }
    push_system(app, "Usage: /config [list|set KEY=VALUE]");
    Ok(())
}

fn save_tools(app: &App) {
    let Some(agent) = &app.agent else { return };
    let value = agent.tools.join(",");
    let _ = ops::set_var(&agent.env_name, "ASTERBOT_TOOLS", &value);
}

fn save_allowed_dirs(app: &App) {
    let Some(agent) = &app.agent else { return };
    let value = agent.allowed_dirs.join(",");
    let _ = ops::set_var(&agent.env_name, "ASTERBOT_ALLOWED_DIRS", &value);
}

fn start_component_fetch(app: &mut App) {
    let installed: Vec<String> = app
        .agent
        .as_ref()
        .map(|a| {
            a.tools
                .iter()
                .map(|t| t.split('@').next().unwrap_or(t).to_string())
                .collect()
        })
        .unwrap_or_default();
    let (tx, rx) = tokio::sync::oneshot::channel();
    app.pending_components = Some(rx);
    tokio::spawn(async move {
        let result = ops::list_remote_components().await.map(|components| {
            components
                .into_iter()
                .filter(|(ns, name, _)| {
                    let ref_str = format!("{ns}:{name}");
                    !installed.contains(&ref_str)
                })
                .map(|(ns, name, _ver)| DynamicItem {
                    value: format!("{ns}:{name}"),
                    label: name,
                    description: ns,
                    disabled: false,
                })
                .collect()
        });
        let _ = tx.send(result);
    });
}

/// Build the DynamicItem list for /tools remove: core components (disabled) + user tools.
fn build_remove_items(tools: &[String]) -> Vec<DynamicItem> {
    let mut items: Vec<DynamicItem> = CORE_COMPONENTS
        .iter()
        .map(|c| {
            let label = c.split(':').next_back().unwrap_or(c).to_string();
            let ns = c.split(':').next().unwrap_or("").to_string();
            DynamicItem {
                value: c.to_string(),
                label,
                description: format!("{ns} (core)"),
                disabled: true,
            }
        })
        .collect();
    for t in tools {
        let label = t.split(':').next_back().unwrap_or(t).to_string();
        let ns = t.split(':').next().unwrap_or("").to_string();
        items.push(DynamicItem {
            value: t.clone(),
            label,
            description: ns,
            disabled: false,
        });
    }
    items
}

/// Build the DynamicItem list for /model: all models from PROVIDERS grouped by provider.
fn build_model_items(current_model: Option<&str>) -> Vec<DynamicItem> {
    let mut items = vec![DynamicItem {
        value: String::new(),
        label: "asterai managed LLM (coming soon)".to_string(),
        description: "asterai".to_string(),
        disabled: true,
    }];
    for &(provider_name, _key, models) in PROVIDERS {
        for &(model_id, model_label) in models {
            let is_current = current_model == Some(model_id);
            let label = match is_current {
                true => format!("{model_label} *"),
                false => model_label.to_string(),
            };
            items.push(DynamicItem {
                value: model_id.to_string(),
                label,
                description: provider_name.to_string(),
                disabled: false,
            });
        }
    }
    items
}

fn update_dynamic_filter(state: &mut ChatState) {
    let filter = state.input.to_lowercase();
    if filter.is_empty() {
        state.dynamic_matches = (0..state.dynamic_items.len()).collect();
    } else {
        state.dynamic_matches = state
            .dynamic_items
            .iter()
            .enumerate()
            .filter(|(_, item)| {
                item.label.to_lowercase().contains(&filter)
                    || item.value.to_lowercase().contains(&filter)
            })
            .map(|(i, _)| i)
            .collect();
    }
    state.dynamic_selected = state
        .dynamic_selected
        .min(state.dynamic_matches.len().saturating_sub(1));
}

/// Simple word-wrap for banner text.
fn textwrap(text: &str, max_width: usize) -> Vec<String> {
    if max_width == 0 {
        return vec![text.to_string()];
    }
    let mut lines = Vec::new();
    let mut current = String::new();
    for word in text.split_whitespace() {
        if current.is_empty() {
            current = word.to_string();
        } else if current.len() + 1 + word.len() <= max_width {
            current.push(' ');
            current.push_str(word);
        } else {
            lines.push(current);
            current = word.to_string();
        }
    }
    if !current.is_empty() {
        lines.push(current);
    }
    lines
}