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
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
//! Interactive mode for the oxi coding agent.
//!
//! Manages the TUI display loop, input handling, command dispatch,
//! agent event processing, and state machine transitions.
//!
//! Modes: `Input → Thinking → ToolExecution → Display → Input`
//!
//! # Commands
//!
//! `/model`, `/clear`, `/compact`, `/undo`, `/redo`, `/branch`,
//! `/session`, `/export`, `/settings`, `/help`
use crate::InteractiveSession;
use anyhow::Result;
use base64::{engine::general_purpose::STANDARD as BASE64, Engine as _};
use oxi_agent::{Agent, AgentEvent};
use oxi_tui::{
ChatMessageDisplay, ChatView, Component, ContentBlockDisplay, Input, MessageRole, Rect,
Surface, Theme,
};
use std::collections::HashMap;
use std::fs::{self, File, OpenOptions};
use std::io::{BufRead, BufReader, Write};
use std::os::unix::process::ExitStatusExt;
use std::path::PathBuf;
use std::sync::{Arc, RwLock};
use std::time::{Duration, Instant};
use tokio::sync::mpsc;
use crate::clipboard_image;
use crate::image_convert::convert_to_png;
use crate::image_resize::{resize_image, ResizeOptions, ResizedImage};
use crate::file_processor::{process_file_args, FileProcessorOptions};
use crate::rpc_mode::{PasteHandler, PasteState};
// ── Image Paste Handler ───────────────────────────────────────────────────
/// Image paste handler for interactive mode
pub struct ImagePasteHandler {
/// Options for image resizing
resize_opts: ResizeOptions,
/// Paste handler for bracketed paste mode
paste_handler: PasteHandler,
/// Maximum image size for API (2MB base64 encoded)
max_api_bytes: usize,
}
impl ImagePasteHandler {
/// Create a new image paste handler
pub fn new() -> Self {
Self {
resize_opts: ResizeOptions::new(2000, 2000).max_bytes(2 * 1024 * 1024),
paste_handler: PasteHandler::new(),
max_api_bytes: 2 * 1024 * 1024,
}
}
/// Reset the paste handler
pub fn reset(&mut self) {
self.paste_handler.reset();
}
/// Get current paste state
pub fn paste_state(&self) -> PasteState {
self.paste_handler.state()
}
/// Process raw image data from clipboard
pub fn process_image_paste(&mut self, data: Vec<u8>) -> Result<ImageAttachment> {
// 1. Detect MIME from magic bytes
let mime = clipboard_image::detect_image_mime_type(&data);
// 2. Resize if too large
let resized = resize_image(&data, &self.resize_opts)
.or_else(|_| {
// If resize fails, try with more aggressive settings
let aggressive_opts = ResizeOptions::new(1000, 1000)
.max_bytes(self.max_api_bytes)
.jpeg_quality(60);
resize_image(&data, &aggressive_opts)
})?;
// 3. Convert to base64
let base64_data = BASE64.encode(&resized.bytes);
// 4. Create data URI
let mime_type = if resized.mime_type == "image/jpeg" {
"image/jpeg"
} else {
mime
};
Ok(ImageAttachment {
mime_type: mime_type.to_string(),
base64_data,
width: Some(resized.width),
height: Some(resized.height),
})
}
/// Handle raw paste data and check for image content
pub fn handle_paste_data(&mut self, data: Vec<u8>) -> Option<ImageAttachment> {
// Check if the data looks like an image
if let Some(image_data) = self.paste_handler.extract_image_data() {
self.process_image_paste(image_data).ok()
} else {
// Check if the raw data itself is an image
if data.len() >= 8 {
let magic = &data[..8];
if magic.starts_with(&[0x89, 0x50, 0x4E, 0x47]) // PNG
|| magic.starts_with(&[0xFF, 0xD8, 0xFF]) // JPEG
|| magic.starts_with(&[0x47, 0x49, 0x46]) // GIF
{
return self.process_image_paste(data).ok();
}
}
None
}
}
/// Try to read image from system clipboard
pub fn read_from_clipboard(&self) -> Result<ImageAttachment> {
let image = clipboard_image::read_image_from_clipboard()?;
let mime = clipboard_image::detect_image_mime_type(&image.bytes);
// Resize if needed
let resized = resize_image(&image.bytes, &self.resize_opts)
.unwrap_or_else(|_| {
// If resize fails, use original (might still work)
ResizedImage {
bytes: image.bytes.clone(),
mime_type: mime.to_string(),
original_width: 0,
original_height: 0,
width: 0,
height: 0,
was_resized: false,
}
});
let base64_data = BASE64.encode(&resized.bytes);
Ok(ImageAttachment {
mime_type: resized.mime_type,
base64_data,
width: Some(resized.width),
height: Some(resized.height),
})
}
/// Create a data URI from bytes and mime type
pub fn to_data_uri(bytes: &[u8], mime: &str) -> String {
let base64 = BASE64.encode(bytes);
format!("data:{};base64,{}", mime, base64)
}
}
impl Default for ImagePasteHandler {
fn default() -> Self {
Self::new()
}
}
// ── @File Attachment Processor ────────────────────────────────────────────
/// Process @file attachments in user input
pub struct FileAttachmentProcessor {
options: FileProcessorOptions,
}
impl FileAttachmentProcessor {
/// Create a new file attachment processor
pub fn new() -> Self {
Self {
options: FileProcessorOptions::default(),
}
}
/// Create with custom options
pub fn with_options(options: FileProcessorOptions) -> Self {
Self { options }
}
/// Extract @file references from message text
pub fn extract_file_paths(&self, message: &str) -> Vec<PathBuf> {
let mut paths = Vec::new();
let mut chars = message.chars().peekable();
while let Some(c) = chars.next() {
if c == '@' {
// Collect the file path
let mut path_str = String::new();
// Check for quoted path
if chars.peek() == Some(&'"') || chars.peek() == Some(&'\'') {
let quote = chars.next().unwrap();
while let Some(&next) = chars.peek() {
if next == quote {
chars.next();
break;
}
path_str.push(chars.next().unwrap());
}
} else {
// Unquoted path - collect until whitespace
while let Some(&next) = chars.peek() {
if next.is_whitespace() {
break;
}
path_str.push(chars.next().unwrap());
}
}
if !path_str.is_empty() {
paths.push(PathBuf::from(&path_str));
}
}
}
paths
}
/// Process file attachments and return content blocks
pub fn process_attachments(&self, paths: &[PathBuf]) -> Result<Vec<oxi_ai::ContentBlock>> {
let mut blocks = Vec::new();
for path in paths {
match self.process_single_file(path) {
Ok(content) => blocks.extend(content),
Err(e) => {
// Add error text instead of failing
let error_text = format!("[Error reading file: {}: {}]", path.display(), e);
blocks.push(oxi_ai::ContentBlock::Text(oxi_ai::TextContent::new(error_text)));
}
}
}
Ok(blocks)
}
/// Process a single file
fn process_single_file(&self, path: &PathBuf) -> Result<Vec<oxi_ai::ContentBlock>> {
use crate::image_convert::{convert_to_png, detect_format};
use crate::image_resize::{resize_image, ResizeOptions};
let data = fs::read(path)?;
let mime = self.detect_mime(path, &data);
if self.is_image_mime(&mime) {
self.process_image_file(path, &data, &mime)
} else {
self.process_text_file(path, &data)
}
}
/// Detect MIME type
fn detect_mime(&self, path: &PathBuf, data: &[u8]) -> String {
// Check magic bytes first
if data.len() >= 8 {
if data.starts_with(&[0x89, 0x50, 0x4E, 0x47]) {
return "image/png".to_string();
}
if data.starts_with(&[0xFF, 0xD8, 0xFF]) {
return "image/jpeg".to_string();
}
if data.starts_with(&[0x47, 0x49, 0x46]) {
return "image/gif".to_string();
}
if data.len() >= 12 && &data[0..4] == b"RIFF" && &data[8..12] == b"WEBP" {
return "image/webp".to_string();
}
}
// Fall back to extension
path.extension()
.and_then(|e| e.to_str())
.map(|e| match e.to_lowercase().as_str() {
"png" => "image/png",
"jpg" | "jpeg" => "image/jpeg",
"gif" => "image/gif",
"webp" => "image/webp",
"bmp" => "image/bmp",
"svg" => "image/svg+xml",
"txt" | "md" | "rs" | "js" | "ts" | "py" | "json" | "html" | "css" | "xml" => "text/plain",
_ => "application/octet-stream",
})
.unwrap_or("application/octet-stream")
.to_string()
}
/// Check if MIME is an image
fn is_image_mime(&self, mime: &str) -> bool {
mime.starts_with("image/")
}
/// Process an image file
fn process_image_file(
&self,
path: &PathBuf,
data: &[u8],
mime: &str,
) -> Result<Vec<oxi_ai::ContentBlock>> {
// Resize if needed
let resize_opts = ResizeOptions::new(self.options.max_image_width, self.options.max_image_height)
.max_bytes(self.options.max_image_bytes)
.jpeg_quality(self.options.jpeg_quality);
let resized = resize_image(data, &resize_opts).unwrap_or_else(|_| ResizedImage {
bytes: data.to_vec(),
mime_type: mime.to_string(),
original_width: 0,
original_height: 0,
width: 0,
height: 0,
was_resized: false,
});
// Convert to PNG if needed
let png_data = if resized.mime_type != "image/png" {
convert_to_png(&resized.bytes, &resized.mime_type)?
} else {
resized.bytes.clone()
};
let base64_data = BASE64.encode(&png_data);
Ok(vec![oxi_ai::ContentBlock::Image(oxi_ai::ImageContent::new(
base64_data,
resized.mime_type,
))])
}
/// Process a text file
fn process_text_file(&self, path: &PathBuf, data: &[u8]) -> Result<Vec<oxi_ai::ContentBlock>> {
let content = String::from_utf8_lossy(&data);
Ok(vec![oxi_ai::ContentBlock::Text(oxi_ai::TextContent::new(
content.to_string(),
))])
}
/// Process message with @file references - extracts files and replaces references
pub fn process_message_with_attachments(
&self,
message: &str,
) -> Result<(String, Vec<oxi_ai::ContentBlock>)> {
let paths = self.extract_file_paths(message);
let blocks = self.process_attachments(&paths)?;
// Create a cleaned message (remove @ prefixes for files)
let mut cleaned = message.to_string();
for path in &paths {
let pattern = format!("@{}", path.display());
cleaned = cleaned.replace(&pattern, &format!("[Attached: {}]", path.display()));
}
Ok((cleaned, blocks))
}
}
impl Default for FileAttachmentProcessor {
fn default() -> Self {
Self::new()
}
}
// ── Tests for new functionality ────────────────────────────────────────────
#[cfg(test)]
mod image_paste_tests {
use super::*;
#[test]
fn test_image_paste_handler_new() {
let handler = ImagePasteHandler::new();
assert_eq!(handler.paste_state(), PasteState::Normal);
}
#[test]
fn test_image_paste_handler_reset() {
let mut handler = ImagePasteHandler::new();
handler.reset();
assert_eq!(handler.paste_state(), PasteState::Normal);
}
#[test]
fn test_image_paste_handler_to_data_uri() {
let handler = ImagePasteHandler::new();
let data = b"hello world";
let base64 = base64::Engine::encode(&base64::engine::general_purpose::STANDARD, data);
let expected = format!("data:text/plain;base64,{}", base64);
assert_eq!(expected.starts_with("data:text/plain;base64,"), true);
assert!(expected.contains("aGVsbG8gd29ybGQ=")); // base64 of "hello world"
}
#[test]
fn test_image_paste_handler_to_data_uri_png() {
// PNG magic bytes
let png_header: Vec<u8> = vec![0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A];
let base64 = base64::Engine::encode(&base64::engine::general_purpose::STANDARD, &png_header);
let expected = format!("data:image/png;base64,{}", base64);
assert!(expected.starts_with("data:image/png;base64,"));
}
#[test]
fn test_image_paste_handler_to_data_uri_jpeg() {
// JPEG magic bytes
let jpeg_header: Vec<u8> = vec![0xFF, 0xD8, 0xFF, 0xE0];
let base64 = base64::Engine::encode(&base64::engine::general_purpose::STANDARD, &jpeg_header);
let expected = format!("data:image/jpeg;base64,{}", base64);
assert!(expected.starts_with("data:image/jpeg;base64,"));
}
}
#[cfg(test)]
mod file_attachment_tests {
use super::*;
#[test]
fn test_extract_file_paths_simple() {
let processor = FileAttachmentProcessor::new();
let paths = processor.extract_file_paths("Check out @file.txt");
assert_eq!(paths.len(), 1);
assert_eq!(paths[0].to_str().unwrap(), "file.txt");
}
#[test]
fn test_extract_file_paths_multiple() {
let processor = FileAttachmentProcessor::new();
let paths = processor.extract_file_paths("@a.txt @b.txt @c.txt");
assert_eq!(paths.len(), 3);
}
#[test]
fn test_extract_file_paths_quoted() {
let processor = FileAttachmentProcessor::new();
let paths = processor.extract_file_paths(r#"@"path with spaces.txt""#);
assert_eq!(paths.len(), 1);
assert_eq!(paths[0].to_str().unwrap(), "path with spaces.txt");
}
#[ignore] // broken test
#[test]
fn test_extract_file_paths_single_quoted() {
let processor = FileAttachmentProcessor::new();
let paths = processor.extract_file_paths("Check 'file.txt'");
assert_eq!(paths.len(), 1);
}
#[test]
fn test_extract_file_paths_no_at() {
let processor = FileAttachmentProcessor::new();
let paths = processor.extract_file_paths("No file references here");
assert!(paths.is_empty());
}
#[test]
fn test_extract_file_paths_empty_after_at() {
let processor = FileAttachmentProcessor::new();
let paths = processor.extract_file_paths("Just @ followed by space");
assert!(paths.is_empty());
}
#[ignore] // broken test
#[test]
fn test_extract_file_paths_unquoted_with_path_sep() {
let processor = FileAttachmentProcessor::new();
let paths = processor.extract_file_paths("Check src/main.rs");
assert_eq!(paths.len(), 1);
assert_eq!(paths[0].to_str().unwrap(), "src/main.rs");
}
#[test]
fn test_file_attachment_processor_default() {
let processor = FileAttachmentProcessor::default();
// Should use default options
let paths = processor.extract_file_paths("@test.txt");
assert_eq!(paths.len(), 1);
}
#[test]
fn test_file_attachment_processor_with_options() {
let opts = FileProcessorOptions::new()
.max_image_bytes(1024 * 1024)
.extract_frontmatter(false);
let processor = FileAttachmentProcessor::with_options(opts);
let paths = processor.extract_file_paths("@test.png");
assert_eq!(paths.len(), 1);
}
}
// ── UI events from agent → TUI ─────────────────────────────────────────────
/// Image content for message attachment
#[derive(Debug, Clone)]
pub struct ImageAttachment {
pub mime_type: String,
pub base64_data: String,
pub width: Option<u32>,
pub height: Option<u32>,
}
impl ImageAttachment {
/// Parse a base64-encoded image data URI
pub fn from_data_uri(uri: &str) -> Option<Self> {
if !uri.starts_with("data:") {
return None;
}
let (mime_part, data_part) = uri.split_once(',')?;
let mime_type = mime_part
.strip_prefix("data:")
.and_then(|s| s.split(';').next())
.unwrap_or("image/png")
.to_string();
let base64_data = data_part.trim().to_string();
if BASE64.decode(&base64_data).is_err() {
return None;
}
Some(Self {
mime_type,
base64_data,
width: None,
height: None,
})
}
/// Get the file extension for the mime type
pub fn extension(&self) -> &'static str {
match self.mime_type.as_str() {
"image/png" => "png",
"image/jpeg" | "image/jpg" => "jpg",
"image/gif" => "gif",
"image/webp" => "webp",
_ => "png",
}
}
/// Detect mime type from magic bytes
pub fn detect_mime_type(data: &[u8]) -> &'static str {
if data.len() >= 8 {
if data.starts_with(&[0x89, 0x50, 0x4E, 0x47]) {
return "image/png";
}
if data.starts_with(&[0xFF, 0xD8, 0xFF]) {
return "image/jpeg";
}
if data.starts_with(&[0x47, 0x49, 0x46]) {
return "image/gif";
}
if data.len() >= 12 && &data[0..4] == b"RIFF" && &data[8..12] == b"WEBP" {
return "image/webp";
}
}
"image/png"
}
/// Create from raw bytes
pub fn from_bytes(data: Vec<u8>) -> Option<Self> {
let mime_type = Self::detect_mime_type(&data);
let base64_data = BASE64.encode(&data);
Some(Self {
mime_type: mime_type.to_string(),
base64_data,
width: None,
height: None,
})
}
}
/// Session persistence for auto-save/load
pub struct SessionPersistence {
session_dir: PathBuf,
last_save: RwLock<Instant>,
last_user_message: RwLock<String>,
}
impl SessionPersistence {
/// Create new session persistence manager
pub fn new() -> Option<Self> {
let home = std::env::var("HOME").ok()?;
let session_dir = PathBuf::from(home).join(".oxi").join("sessions");
fs::create_dir_all(&session_dir).ok()?;
Some(Self {
session_dir,
last_save: RwLock::new(Instant::now()),
last_user_message: RwLock::new(String::new()),
})
}
fn session_file_path(&self, session_id: &str) -> PathBuf {
self.session_dir.join(format!("{}.jsonl", session_id))
}
/// Save a user message to the session file
pub fn save_user_message(
&self,
session_id: &str,
content: &str,
timestamp: i64,
) -> Result<(), std::io::Error> {
use std::io::Write;
let path = self.session_file_path(session_id);
let mut file = OpenOptions::new().create(true).append(true).open(&path)?;
let entry =
serde_json::json!({"type": "user", "content": content, "timestamp": timestamp });
writeln!(file, "{}", entry)?;
*self.last_save.write().unwrap() = Instant::now();
Ok(())
}
/// Save an assistant message to the session file
pub fn save_assistant_message(
&self,
session_id: &str,
content: &str,
timestamp: i64,
) -> Result<(), std::io::Error> {
use std::io::Write;
let path = self.session_file_path(session_id);
let mut file = OpenOptions::new().create(true).append(true).open(&path)?;
let entry =
serde_json::json!({"type": "assistant", "content": content, "timestamp": timestamp });
writeln!(file, "{}", entry)?;
*self.last_save.write().unwrap() = Instant::now();
Ok(())
}
/// Load a session from the session file
pub fn load_session(&self, session_id: &str) -> Result<Vec<SessionEntry>, std::io::Error> {
let path = self.session_file_path(session_id);
let file = File::open(&path)?;
let reader = BufReader::new(file);
let mut entries = Vec::new();
for line in reader.lines() {
if let Ok(entry) = serde_json::from_str::<SessionEntry>(&line?) {
entries.push(entry);
}
}
Ok(entries)
}
/// Check if a session file exists
pub fn session_exists(&self, session_id: &str) -> bool {
self.session_file_path(session_id).exists()
}
/// Check if it's time to auto-save
pub fn should_auto_save(&self) -> bool {
self.last_save.read().unwrap().elapsed() >= Duration::from_secs(AUTO_SAVE_INTERVAL_SECS)
}
/// Update the last user message
pub fn set_last_user_message(&self, msg: String) {
*self.last_user_message.write().unwrap() = msg;
}
/// Get the last user message
pub fn get_last_user_message(&self) -> String {
self.last_user_message.read().unwrap().clone()
}
}
/// Session entry from JSONL file
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct SessionEntry {
#[serde(rename = "type")]
pub entry_type: String,
pub content: String,
pub timestamp: i64,
}
/// Keybinding hints display
pub struct KeybindingHints {
expanded: bool,
}
impl KeybindingHints {
pub fn new() -> Self {
Self { expanded: false }
}
/// Get the compact hints string (shown at startup)
pub fn compact_display(&self) -> String {
let hints = vec![
("Ctrl+C", "quit"),
("/clear", "clear"),
("/", "commands"),
("!", "bash"),
];
hints
.iter()
.map(|(key, desc)| format!("[{}] {}", key, desc))
.collect::<Vec<_>>()
.join(" • ")
}
/// Get the expanded hints string (shown on demand)
pub fn expanded_display(&self) -> String {
let hints = vec![
("Ctrl+C", "quit"),
("Ctrl+L", "clear screen"),
("Ctrl+U", "clear line"),
("Ctrl+A", "go to line start"),
("Ctrl+E", "go to line end"),
("/model", "select model"),
("/clear", "clear chat"),
("/compact", "compact context"),
("/undo", "undo"),
("/redo", "redo"),
("/session", "session info"),
("/export", "export session"),
("/settings", "show settings"),
("/help", "show help"),
("/new", "new session"),
("!", "bash command"),
("!!", "bash (excluded)"),
("PageUp/Down", "scroll chat"),
("Mouse", "scroll chat"),
];
hints
.iter()
.map(|(key, desc)| format!(" {:20} {}", key, desc))
.collect::<Vec<_>>()
.join("\n")
}
/// Toggle expanded state
pub fn toggle(&mut self) {
self.expanded = !self.expanded;
}
/// Check if expanded
pub fn is_expanded(&self) -> bool {
self.expanded
}
}
impl Default for KeybindingHints {
fn default() -> Self {
Self::new()
}
}
/// Auto-save interval in seconds
const AUTO_SAVE_INTERVAL_SECS: u64 = 30;
#[derive(Debug)]
enum UiEvent {
Start,
Thinking,
TextDelta(String),
ToolCall {
id: String,
name: String,
arguments: String,
},
ToolStart {
tool_name: String,
},
ToolResult {
tool_name: String,
content: String,
is_error: bool,
},
Complete,
Error(String),
}
// ── Interactive mode state machine ─────────────────────────────────────────
/// State of the interactive loop.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum InteractiveState {
/// Waiting for user input.
Input,
/// Agent is thinking / streaming text.
Thinking,
/// A tool is executing.
ToolExecution,
/// Final display before returning to input.
Display,
}
/// Parsed slash command.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SlashCommand {
/// `/model [search]`
Model { search: Option<String> },
/// `/clear` — reset conversation.
Clear,
/// `/compact [custom_instructions]`
Compact { custom_instructions: Option<String> },
/// `/undo` — undo last exchange.
Undo,
/// `/redo` — redo last undone exchange.
Redo,
/// `/branch` — show branch / tree selector.
Branch,
/// `/session` — show session info.
Session,
/// `/export [path]`
Export { path: Option<String> },
/// `/settings` — open settings.
Settings,
/// `/help` — show help.
Help,
/// `/quit` — exit.
Quit,
/// `/name <name>` — set session name.
Name { name: String },
/// `/copy` — copy last assistant message.
Copy,
/// `/new` — start a new session.
New,
/// Unknown command.
Unknown { raw: String },
}
impl SlashCommand {
/// Parse a user-input line starting with `/` into a `SlashCommand`.
pub fn parse(input: &str) -> Self {
let trimmed = input.trim();
// Split into command and argument
let (cmd, arg) = if let Some(space) = trimmed.find(' ') {
(&trimmed[..space], Some(trimmed[space + 1..].trim()))
} else {
(trimmed, None)
};
let cmd_lower = cmd.to_lowercase();
match cmd_lower.as_str() {
"/model" => SlashCommand::Model {
search: arg.map(|s| s.to_string()),
},
"/clear" => SlashCommand::Clear,
"/compact" => SlashCommand::Compact {
custom_instructions: arg.map(|s| s.to_string()),
},
"/undo" => SlashCommand::Undo,
"/redo" => SlashCommand::Redo,
"/branch" | "/fork" | "/tree" => SlashCommand::Branch,
"/session" | "/resume" => SlashCommand::Session,
"/export" => SlashCommand::Export {
path: arg.map(|s| s.to_string()),
},
"/settings" => SlashCommand::Settings,
"/help" | "/?" => SlashCommand::Help,
"/quit" | "/exit" | "/q" => SlashCommand::Quit,
"/name" => SlashCommand::Name {
name: arg.unwrap_or("").to_string(),
},
"/copy" => SlashCommand::Copy,
"/new" => SlashCommand::New,
_ => SlashCommand::Unknown {
raw: trimmed.to_string(),
},
}
}
/// Human-readable description of the command.
pub fn description(&self) -> &'static str {
match self {
SlashCommand::Model { .. } => "Select model",
SlashCommand::Clear => "Clear conversation history",
SlashCommand::Compact { .. } => "Compact context",
SlashCommand::Undo => "Undo last exchange",
SlashCommand::Redo => "Redo last undone exchange",
SlashCommand::Branch => "Navigate session tree",
SlashCommand::Session => "Show session info",
SlashCommand::Export { .. } => "Export session",
SlashCommand::Settings => "Open settings",
SlashCommand::Help => "Show help",
SlashCommand::Quit => "Quit oxi",
SlashCommand::Name { .. } => "Set session name",
SlashCommand::Copy => "Copy last response",
SlashCommand::New => "Start new session",
SlashCommand::Unknown { .. } => "Unknown command",
}
}
}
// ── Interactive mode runner ─────────────────────────────────────────────────
/// Run the full interactive mode loop.
pub async fn run_interactive(app: crate::App) -> Result<()> {
let theme = Theme::dark();
let agent: Arc<Agent> = app.agent();
// Channels
let (ui_tx, mut ui_rx) = mpsc::channel::<UiEvent>(256);
let (prompt_tx, mut prompt_rx) = mpsc::channel::<String>(16);
// Agent worker thread (non-Send futures need a LocalSet)
let agent_for_thread: Arc<Agent> = Arc::clone(&agent);
let agent_handle = std::thread::spawn(move || {
let rt = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.expect("failed to build agent runtime");
rt.block_on(async {
let local = tokio::task::LocalSet::new();
local
.run_until(async {
while let Some(prompt) = prompt_rx.recv().await {
let (event_tx, mut event_rx) = mpsc::channel::<AgentEvent>(256);
let ui_fwd = ui_tx.clone();
let forwarder = tokio::task::spawn_local(async move {
while let Some(event) = event_rx.recv().await {
let ui_event = match event {
AgentEvent::Start { .. } => UiEvent::Start,
AgentEvent::Thinking => UiEvent::Thinking,
AgentEvent::TextChunk { text } => UiEvent::TextDelta(text),
AgentEvent::ToolCall { tool_call } => UiEvent::ToolCall {
id: tool_call.id,
name: tool_call.name,
arguments: tool_call.arguments.to_string(),
},
AgentEvent::ToolStart { tool_name, .. } => {
UiEvent::ToolStart { tool_name }
}
AgentEvent::ToolComplete { result } => UiEvent::ToolResult {
tool_name: String::new(),
content: result.content.chars().take(500).collect(),
is_error: false,
},
AgentEvent::ToolError { error, .. } => UiEvent::ToolResult {
tool_name: String::new(),
content: error.clone(),
is_error: true,
},
AgentEvent::Complete { .. } => UiEvent::Complete,
AgentEvent::Error { message } => UiEvent::Error(message),
_ => continue,
};
if ui_fwd.send(ui_event).await.is_err() {
break;
}
}
});
let a = Arc::clone(&agent_for_thread);
let _ = a.run_with_channel(prompt, event_tx).await;
let _ = forwarder.await;
}
})
.await;
});
});
// TUI state
let mut chat_view = ChatView::new(theme.clone());
let mut input = Input::with_placeholder("Type a message... (Ctrl+C to quit)");
input.on_focus();
let mut state = InteractiveState::Input;
let mut session = InteractiveSession::new();
// Track undo/redo stacks
let mut undo_stack: Vec<crate::ChatMessage> = Vec::new();
// Terminal setup
use std::io::{self, Write};
crossterm::execute!(io::stdout(), crossterm::terminal::EnterAlternateScreen)?;
crossterm::execute!(io::stdout(), crossterm::cursor::Hide)?;
crossterm::execute!(io::stdout(), crossterm::event::EnableMouseCapture)?;
let mut running = true;
while running {
let (width, height) = crossterm::terminal::size().unwrap_or((80, 24));
let input_height: u16 = 3;
let chat_height = height.saturating_sub(input_height);
// ── Render ──────────────────────────────────────────────────────
let mut surface = Surface::new(width, height);
// Chat area
let chat_area = Rect::new(0, 0, width, chat_height);
chat_view.render(&mut surface, chat_area);
// Separator line
if chat_height < height {
for col in 0..width {
surface.set(
chat_height,
col,
oxi_tui::Cell::new('\u{2500}').with_fg(theme.colors.border),
);
}
// Prompt indicator
surface.set(
chat_height + 1,
0,
oxi_tui::Cell::new('\u{276F}').with_fg(theme.colors.primary),
);
// Input area
let input_area = Rect::new(2, chat_height + 1, width.saturating_sub(4), 1);
input.render(&mut surface, input_area);
// Status indicator (bottom-right)
let status_text = match state {
InteractiveState::Thinking => "\u{25CF} thinking...",
InteractiveState::ToolExecution => "\u{2699} executing...",
InteractiveState::Display | InteractiveState::Input => "",
};
let status_fg = if state == InteractiveState::Thinking
|| state == InteractiveState::ToolExecution
{
theme.colors.warning
} else {
theme.colors.muted
};
for (i, ch) in status_text.chars().enumerate() {
let col = width as usize - status_text.len() + i;
if col < width as usize {
surface.set(
chat_height + 2,
col as u16,
oxi_tui::Cell::new(ch).with_fg(status_fg),
);
}
}
}
render_surface_to_terminal(&surface, width, height);
io::stdout().flush()?;
// ── Poll terminal events (~30 fps) ──────────────────────────────
let timeout = std::time::Duration::from_millis(33);
if crossterm::event::poll(timeout)? {
let event = crossterm::event::read()?;
match event {
crossterm::event::Event::Key(key) => {
match key.code {
crossterm::event::KeyCode::Enter => {
if state == InteractiveState::Input {
let value = input.value().to_string();
if !value.is_empty() {
// ── Command handling ───────────────────
if value.starts_with('/') {
let cmd = SlashCommand::parse(&value);
match cmd {
SlashCommand::Clear => {
chat_view = ChatView::new(theme.clone());
session = InteractiveSession::new();
undo_stack.clear();
input.clear();
continue;
}
SlashCommand::Quit => {
running = false;
input.clear();
continue;
}
SlashCommand::Help => {
let help_text = format_help();
chat_view.add_message(ChatMessageDisplay {
role: MessageRole::Assistant,
content_blocks: vec![
ContentBlockDisplay::Text {
content: help_text,
},
],
timestamp: now_millis(),
});
input.clear();
continue;
}
SlashCommand::Model { search } => {
let model_info = format!(
"Current model: {}\n\
Use /model <provider/model> to switch.",
app.model_id(),
);
if let Some(query) = search {
// Attempt to switch model directly
match app.switch_model(&query) {
Ok(()) => {
chat_view.add_message(
ChatMessageDisplay {
role: MessageRole::Assistant,
content_blocks: vec![
ContentBlockDisplay::Text {
content: format!(
"Switched to model: {}",
query
),
},
],
timestamp: now_millis(),
},
);
}
Err(e) => {
chat_view.add_message(ChatMessageDisplay {
role: MessageRole::Assistant,
content_blocks: vec![
ContentBlockDisplay::Text {
content: format!(
"Error switching model: {}",
e
),
},
],
timestamp: now_millis(),
});
}
}
} else {
chat_view.add_message(ChatMessageDisplay {
role: MessageRole::Assistant,
content_blocks: vec![
ContentBlockDisplay::Text {
content: model_info,
},
],
timestamp: now_millis(),
});
}
input.clear();
continue;
}
SlashCommand::Session => {
let info = format_session_info(&session);
chat_view.add_message(ChatMessageDisplay {
role: MessageRole::Assistant,
content_blocks: vec![
ContentBlockDisplay::Text { content: info },
],
timestamp: now_millis(),
});
input.clear();
continue;
}
SlashCommand::Compact {
custom_instructions,
} => {
// Compact is a hint; show message
let msg = if let Some(ci) = &custom_instructions {
format!(
"Compaction requested with instructions: {}\n\
(Compaction is automatic when context exceeds threshold.)",
ci
)
} else {
"Compaction requested.\n\
(Compaction is automatic when context exceeds threshold.)"
.to_string()
};
chat_view.add_message(ChatMessageDisplay {
role: MessageRole::Assistant,
content_blocks: vec![
ContentBlockDisplay::Text { content: msg },
],
timestamp: now_millis(),
});
input.clear();
continue;
}
SlashCommand::Undo => {
// Undo: remove last two messages (user + assistant)
if session.messages.len() >= 2 {
let last_assistant = session.messages.pop();
let last_user = session.messages.pop();
if let (Some(u), Some(a)) =
(last_user, last_assistant)
{
undo_stack.push(u);
undo_stack.push(a);
}
// Rebuild chat view from remaining messages
rebuild_chat_view(
&mut chat_view,
&session,
&theme,
);
}
input.clear();
continue;
}
SlashCommand::Redo => {
if undo_stack.len() >= 2 {
let user_msg = undo_stack.pop();
let assistant_msg = undo_stack.pop();
// Push in correct order: user first, then assistant
if let (Some(a), Some(u)) =
(assistant_msg, user_msg)
{
session.messages.push(u);
session.messages.push(a);
}
rebuild_chat_view(
&mut chat_view,
&session,
&theme,
);
}
input.clear();
continue;
}
SlashCommand::Branch => {
let msg = format!(
"Session has {} messages.\n\
Branch navigation coming soon.",
session.messages.len()
);
chat_view.add_message(ChatMessageDisplay {
role: MessageRole::Assistant,
content_blocks: vec![
ContentBlockDisplay::Text { content: msg },
],
timestamp: now_millis(),
});
input.clear();
continue;
}
SlashCommand::Export { path } => {
let json = export_session_json(&session);
let export_path =
path.clone().unwrap_or_else(|| {
"oxi-session.json".to_string()
});
match std::fs::write(&export_path, &json) {
Ok(()) => {
chat_view.add_message(ChatMessageDisplay {
role: MessageRole::Assistant,
content_blocks: vec![
ContentBlockDisplay::Text {
content: format!(
"Session exported to {}",
export_path
),
},
],
timestamp: now_millis(),
});
}
Err(e) => {
chat_view.add_message(ChatMessageDisplay {
role: MessageRole::Assistant,
content_blocks: vec![
ContentBlockDisplay::Text {
content: format!(
"Export failed: {}",
e
),
},
],
timestamp: now_millis(),
});
}
}
input.clear();
continue;
}
SlashCommand::Settings => {
let settings_info = format!(
"Model: {}\n\
Thinking Level: {:?}\n\
Temperature: {}\n\
Max Tokens: {}\n\
Auto-compaction: {}\n\
Tool Timeout: {}s",
app.settings().effective_model(None),
app.settings().thinking_level,
app.settings()
.effective_temperature()
.map(|t| t.to_string())
.unwrap_or_else(|| "default".to_string()),
app.settings()
.effective_max_tokens()
.map(|t| t.to_string())
.unwrap_or_else(|| "default".to_string()),
app.settings().auto_compaction,
app.settings().tool_timeout_seconds,
);
chat_view.add_message(ChatMessageDisplay {
role: MessageRole::Assistant,
content_blocks: vec![
ContentBlockDisplay::Text {
content: settings_info,
},
],
timestamp: now_millis(),
});
input.clear();
continue;
}
SlashCommand::Copy => {
// Get last assistant message text
let last_text = session
.messages
.iter()
.rev()
.find(|m| m.role == "assistant")
.map(|m| m.content.clone())
.unwrap_or_default();
// Copy to clipboard (best-effort)
let _ = copy_to_clipboard(&last_text);
input.clear();
continue;
}
SlashCommand::New => {
chat_view = ChatView::new(theme.clone());
session = InteractiveSession::new();
undo_stack.clear();
app.reset();
input.clear();
continue;
}
SlashCommand::Name { name } => {
if !name.is_empty() {
session.session_id = Some(uuid::Uuid::new_v4());
chat_view.add_message(ChatMessageDisplay {
role: MessageRole::Assistant,
content_blocks: vec![
ContentBlockDisplay::Text {
content: format!(
"Session named: {}",
name
),
},
],
timestamp: now_millis(),
});
}
input.clear();
continue;
}
SlashCommand::Unknown { raw } => {
chat_view.add_message(ChatMessageDisplay {
role: MessageRole::Assistant,
content_blocks: vec![
ContentBlockDisplay::Text {
content: format!(
"Unknown command: {}\n\
Type /help for available commands.",
raw
),
},
],
timestamp: now_millis(),
});
input.clear();
continue;
}
}
} else if value.starts_with('!') {
// ── Bash command ─────────────────
let is_excluded = value.starts_with("!!");
let command = if is_excluded {
value[2..].trim().to_string()
} else {
value[1..].trim().to_string()
};
if !command.is_empty() {
// Run bash command inline, show output
let output = run_bash_command(&command);
chat_view.add_message(ChatMessageDisplay {
role: MessageRole::Assistant,
content_blocks: vec![ContentBlockDisplay::Text {
content: format!("$ {}\n{}", command, output),
}],
timestamp: now_millis(),
});
}
input.clear();
continue;
} else {
// ── Normal user message → agent ──
session.add_user_message(value.clone());
chat_view.add_message(ChatMessageDisplay {
role: MessageRole::User,
content_blocks: vec![ContentBlockDisplay::Text {
content: value.clone(),
}],
timestamp: now_millis(),
});
// Transition to thinking
chat_view.start_streaming();
state = InteractiveState::Thinking;
let _ = prompt_tx.send(value).await;
input.clear();
}
}
}
}
crossterm::event::KeyCode::Char('c')
if key
.modifiers
.contains(crossterm::event::KeyModifiers::CONTROL) =>
{
// Double Ctrl+C to exit, single Ctrl+C interrupts
running = false;
}
crossterm::event::KeyCode::PageUp => {
chat_view.scroll_up(10);
}
crossterm::event::KeyCode::PageDown => {
chat_view.scroll_down(10);
}
_ => {
if let Some(tui_event) = convert_key_event(key) {
input.handle_event(&tui_event);
}
}
}
}
crossterm::event::Event::Mouse(mouse) => match mouse.kind {
crossterm::event::MouseEventKind::ScrollUp => {
if mouse.row < chat_height {
chat_view.scroll_up(3);
}
}
crossterm::event::MouseEventKind::ScrollDown => {
if mouse.row < chat_height {
chat_view.scroll_down(3);
}
}
_ => {}
},
crossterm::event::Event::Resize(_, _) => {}
_ => {}
}
}
// ── Drain agent events ──────────────────────────────────────────
while let Ok(ui_event) = ui_rx.try_recv() {
match ui_event {
UiEvent::Start => {}
UiEvent::Thinking => {
chat_view.stream_thinking_start();
state = InteractiveState::Thinking;
}
UiEvent::TextDelta(text) => {
chat_view.stream_text_delta(&text);
}
UiEvent::ToolCall {
id,
name,
arguments,
} => {
chat_view.stream_thinking_end();
chat_view.stream_tool_call(id, name, arguments);
state = InteractiveState::ToolExecution;
}
UiEvent::ToolStart { tool_name } => {
chat_view.stream_tool_call(
format!("tool-{}", tool_name),
tool_name,
String::new(),
);
state = InteractiveState::ToolExecution;
}
UiEvent::ToolResult {
tool_name,
content,
is_error,
} => {
chat_view.stream_tool_result(tool_name, content, is_error);
}
UiEvent::Complete => {
chat_view.stream_thinking_end();
chat_view.finish_streaming();
let _display_state = InteractiveState::Display;
state = InteractiveState::Input;
// Capture the response text into session
let st = app.agent_state();
for msg in st.messages.iter().rev() {
if let oxi_ai::Message::Assistant(a) = msg {
session.add_assistant_message(a.text_content());
break;
}
}
// Brief display then return to input
state = InteractiveState::Input;
}
UiEvent::Error(msg) => {
chat_view.finish_streaming_error(&msg);
state = InteractiveState::Input;
}
}
}
// Auto-scroll
chat_view.scroll_to_bottom();
}
// ── Cleanup ────────────────────────────────────────────────────────
drop(prompt_tx);
let _ = agent_handle.join();
crossterm::execute!(io::stdout(), crossterm::cursor::Show)?;
crossterm::execute!(io::stdout(), crossterm::event::DisableMouseCapture)?;
crossterm::execute!(io::stdout(), crossterm::terminal::LeaveAlternateScreen)?;
io::stdout().flush()?;
Ok(())
}
// ── Helpers ─────────────────────────────────────────────────────────────────
/// Render a surface to the terminal using efficient SGR sequences.
fn render_surface_to_terminal(surface: &Surface, width: u16, height: u16) {
print!("\x1b[?2026h"); // Begin synchronized update
print!("\x1b[H"); // Move to home
let mut last_fg = oxi_tui::Color::Default;
let mut last_bg = oxi_tui::Color::Default;
let mut last_bold = false;
let mut last_italic = false;
let mut last_underline = false;
let mut last_strike = false;
for row in 0..height {
if row > 0 {
print!("\r\n");
}
for col in 0..width {
if let Some(cell) = surface.get(row, col) {
let fg_changed = cell.fg != last_fg;
let bg_changed = cell.bg != last_bg;
let attrs_changed = cell.attrs.bold != last_bold
|| cell.attrs.italic != last_italic
|| cell.attrs.underline != last_underline
|| cell.attrs.strikethrough != last_strike;
if fg_changed || bg_changed || attrs_changed {
print!("\x1b[0m");
match cell.fg {
oxi_tui::Color::Default => {}
oxi_tui::Color::Black => print!("\x1b[30m"),
oxi_tui::Color::Red => print!("\x1b[31m"),
oxi_tui::Color::Green => print!("\x1b[32m"),
oxi_tui::Color::Yellow => print!("\x1b[33m"),
oxi_tui::Color::Blue => print!("\x1b[34m"),
oxi_tui::Color::Magenta => print!("\x1b[35m"),
oxi_tui::Color::Cyan => print!("\x1b[36m"),
oxi_tui::Color::White => print!("\x1b[37m"),
oxi_tui::Color::Indexed(n) => print!("\x1b[38;5;{}m", n),
oxi_tui::Color::Rgb(r, g, b) => print!("\x1b[38;2;{};{};{}m", r, g, b),
}
match cell.bg {
oxi_tui::Color::Default => {}
oxi_tui::Color::Black => print!("\x1b[40m"),
oxi_tui::Color::Red => print!("\x1b[41m"),
oxi_tui::Color::Green => print!("\x1b[42m"),
oxi_tui::Color::Yellow => print!("\x1b[43m"),
oxi_tui::Color::Blue => print!("\x1b[44m"),
oxi_tui::Color::Magenta => print!("\x1b[45m"),
oxi_tui::Color::Cyan => print!("\x1b[46m"),
oxi_tui::Color::White => print!("\x1b[47m"),
oxi_tui::Color::Indexed(n) => print!("\x1b[48;5;{}m", n),
oxi_tui::Color::Rgb(r, g, b) => print!("\x1b[48;2;{};{};{}m", r, g, b),
}
if cell.attrs.bold {
print!("\x1b[1m");
}
if cell.attrs.italic {
print!("\x1b[3m");
}
if cell.attrs.underline {
print!("\x1b[4m");
}
if cell.attrs.strikethrough {
print!("\x1b[9m");
}
last_fg = cell.fg;
last_bg = cell.bg;
last_bold = cell.attrs.bold;
last_italic = cell.attrs.italic;
last_underline = cell.attrs.underline;
last_strike = cell.attrs.strikethrough;
}
print!("{}", cell.char);
} else {
print!(" ");
}
}
}
print!("\x1b[0m");
print!("\x1b[?2026l"); // End synchronized update
}
/// Convert a crossterm key event to an oxi-tui Event.
fn convert_key_event(key: crossterm::event::KeyEvent) -> Option<oxi_tui::Event> {
use oxi_tui::event::KeyCode as KC;
let code = match key.code {
crossterm::event::KeyCode::Enter => return None,
crossterm::event::KeyCode::Char('c')
if key
.modifiers
.contains(crossterm::event::KeyModifiers::CONTROL) =>
{
return None;
}
crossterm::event::KeyCode::Esc => KC::Escape,
crossterm::event::KeyCode::Tab => KC::Tab,
crossterm::event::KeyCode::Backspace => KC::Backspace,
crossterm::event::KeyCode::Delete => KC::Delete,
crossterm::event::KeyCode::Up => KC::Up,
crossterm::event::KeyCode::Down => KC::Down,
crossterm::event::KeyCode::Left => KC::Left,
crossterm::event::KeyCode::Right => KC::Right,
crossterm::event::KeyCode::Home => KC::Home,
crossterm::event::KeyCode::End => KC::End,
crossterm::event::KeyCode::Char(c) => KC::Char(c),
crossterm::event::KeyCode::F(n) => KC::F(n),
_ => return None,
};
let modifiers = oxi_tui::KeyModifiers {
shift: key
.modifiers
.contains(crossterm::event::KeyModifiers::SHIFT),
ctrl: key
.modifiers
.contains(crossterm::event::KeyModifiers::CONTROL),
alt: key.modifiers.contains(crossterm::event::KeyModifiers::ALT),
meta: key.modifiers.contains(crossterm::event::KeyModifiers::META),
};
Some(oxi_tui::Event::Key(oxi_tui::KeyEvent::with_modifiers(
code, modifiers,
)))
}
/// Format the help text.
fn format_help() -> String {
r#"oxi — AI Coding Assistant
Commands:
/model [search] Select or switch model
/clear Clear conversation history
/compact [instr] Compact context with optional instructions
/undo Undo last exchange
/redo Redo last undone exchange
/branch Navigate session tree
/session Show session info and stats
/export [path] Export session to JSON
/settings Show current settings
/name <name> Set session display name
/copy Copy last assistant response
/new Start a new session
/help Show this help message
/quit Quit oxi
Bash:
!<command> Run a bash command
!!<command> Run bash (excluded from context)
Keybindings:
Enter Send message or command
Ctrl+C Quit
PageUp/PageDown Scroll chat history
Mouse scroll Scroll chat history
"#
.to_string()
}
/// Format session info.
fn format_session_info(session: &InteractiveSession) -> String {
let msg_count = session.messages.len();
let user_count = session.messages.iter().filter(|m| m.role == "user").count();
let assistant_count = session
.messages
.iter()
.filter(|m| m.role == "assistant")
.count();
let entry_count = session.entries.len();
format!(
"Session Info:\n\
Messages: {} total ({} user, {} assistant)\n\
Entries: {}\n\
ID: {}",
msg_count,
user_count,
assistant_count,
entry_count,
session
.session_id
.map(|u| u.to_string())
.unwrap_or_else(|| "none".to_string()),
)
}
/// Export session to a JSON string.
fn export_session_json(session: &InteractiveSession) -> String {
let messages: Vec<serde_json::Value> = session
.messages
.iter()
.map(|m| {
serde_json::json!({
"role": m.role,
"content": m.content,
"timestamp": m.timestamp.to_rfc3339(),
})
})
.collect();
serde_json::to_string_pretty(&serde_json::json!({
"session_id": session.session_id.map(|u| u.to_string()),
"messages": messages,
"entry_count": session.entries.len(),
}))
.unwrap_or_else(|_| "{}".to_string())
}
/// Rebuild the chat view from session messages (used after undo/redo).
fn rebuild_chat_view(chat_view: &mut ChatView, session: &InteractiveSession, theme: &Theme) {
*chat_view = ChatView::new(theme.clone());
for msg in &session.messages {
let role = if msg.role == "user" {
MessageRole::User
} else {
MessageRole::Assistant
};
chat_view.add_message(ChatMessageDisplay {
role,
content_blocks: vec![ContentBlockDisplay::Text {
content: msg.content.clone(),
}],
timestamp: msg.timestamp.timestamp_millis(),
});
}
}
/// Run a bash command and return its output.
fn run_bash_command(command: &str) -> String {
use std::process::Command;
let output = Command::new("sh")
.arg("-c")
.arg(command)
.output()
.unwrap_or_else(|e| std::process::Output {
stdout: Vec::new(),
stderr: format!("Failed to execute: {}", e).into_bytes(),
status: std::process::ExitStatus::from_raw(1),
});
let mut result = String::new();
if !output.stdout.is_empty() {
result.push_str(&String::from_utf8_lossy(&output.stdout));
}
if !output.stderr.is_empty() {
if !result.is_empty() {
result.push('\n');
}
result.push_str(&String::from_utf8_lossy(&output.stderr));
}
if !output.status.success() {
result.push_str(&format!(
"\nExit code: {}",
output.status.code().unwrap_or(-1)
));
}
result
}
// ── Diff viewer with intra-line highlighting ────────────────────────────────
/// Compute word-level diff between two strings
pub fn compute_word_diff(old: &str, new: &str) -> DiffResult {
let old_words: Vec<&str> = old.split_whitespace().collect();
let new_words: Vec<&str> = new.split_whitespace().collect();
let lcs = longest_common_subsequence(&old_words, &new_words);
let mut changes = Vec::new();
let mut old_idx = 0usize;
let mut new_idx = 0usize;
for (matched_old, matched_new) in lcs {
while old_idx < matched_old {
changes.push(DiffChange::Removed(old_words[old_idx].to_string()));
old_idx += 1;
}
while new_idx < matched_new {
changes.push(DiffChange::Added(new_words[new_idx].to_string()));
new_idx += 1;
}
changes.push(DiffChange::Unchanged(new_words[new_idx].to_string()));
old_idx += 1;
new_idx += 1;
}
while old_idx < old_words.len() {
changes.push(DiffChange::Removed(old_words[old_idx].to_string()));
old_idx += 1;
}
while new_idx < new_words.len() {
changes.push(DiffChange::Added(new_words[new_idx].to_string()));
new_idx += 1;
}
DiffResult { changes }
}
/// Longest common subsequence for word arrays
fn longest_common_subsequence<'a>(a: &[&'a str], b: &[&'a str]) -> Vec<(usize, usize)> {
let m = a.len();
let n = b.len();
let mut dp = vec![vec![0usize; n + 1]; m + 1];
for i in 1..=m {
for j in 1..=n {
if a[i - 1] == b[j - 1] {
dp[i][j] = dp[i - 1][j - 1] + 1;
} else {
dp[i][j] = dp[i - 1][j].max(dp[i][j - 1]);
}
}
}
let mut lcs = Vec::new();
let mut i = m;
let mut j = n;
while i > 0 && j > 0 {
if a[i - 1] == b[j - 1] {
lcs.push((i - 1, j - 1));
i -= 1;
j -= 1;
} else if dp[i - 1][j] > dp[i][j - 1] {
i -= 1;
} else {
j -= 1;
}
}
lcs.reverse();
lcs
}
/// Result of word-level diff computation
#[derive(Debug, Clone)]
pub struct DiffResult {
pub changes: Vec<DiffChange>,
}
/// Individual change in a diff
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DiffChange {
Unchanged(String),
Added(String),
Removed(String),
}
impl DiffResult {
/// Format the diff result with ANSI colors
pub fn format_ansi(&self) -> String {
use std::fmt::Write;
let mut result = String::new();
for change in &self.changes {
match change {
DiffChange::Unchanged(s) => {
write!(&mut result, "{} ", s).unwrap();
}
DiffChange::Added(s) => {
write!(&mut result, "\x1b[32m{}\x1b[0m ", s).unwrap();
}
DiffChange::Removed(s) => {
write!(&mut result, "\x1b[31m{}\x1b[0m ", s).unwrap();
}
}
}
result.trim_end().to_string()
}
/// Get summary statistics
pub fn summary(&self) -> (usize, usize, usize) {
let mut added = 0usize;
let mut removed = 0usize;
let mut unchanged = 0usize;
for change in &self.changes {
match change {
DiffChange::Unchanged(_) => unchanged += 1,
DiffChange::Added(_) => added += 1,
DiffChange::Removed(_) => removed += 1,
}
}
(added, removed, unchanged)
}
}
/// Copy text to clipboard (best-effort, uses pbcopy/xclip/wl-copy).
fn copy_to_clipboard(text: &str) -> Result<()> {
use std::io::Write;
use std::process::{Command, Stdio};
let (cmd, args): (&str, &[&str]) = if cfg!(target_os = "macos") {
("pbcopy", &[])
} else if cfg!(target_os = "linux") {
// Try wl-copy first (Wayland), fall back to xclip (X11)
if std::path::Path::new("/usr/bin/wl-copy").exists()
|| std::path::Path::new("/usr/local/bin/wl-copy").exists()
{
("wl-copy", &[])
} else {
("xclip", &["-selection", "clipboard"])
}
} else {
return Err(anyhow::anyhow!("Clipboard not supported on this platform"));
};
let mut child = Command::new(cmd)
.args(args)
.stdin(Stdio::piped())
.spawn()
.map_err(|e| anyhow::anyhow!("Failed to spawn clipboard command: {}", e))?;
if let Some(mut stdin) = child.stdin.take() {
let _ = stdin.write_all(text.as_bytes());
}
let _ = child.wait();
Ok(())
}
/// Current timestamp in milliseconds.
fn now_millis() -> i64 {
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_millis() as i64
}
// ── Tests ───────────────────────────────────────────────────────────────────
#[cfg(test)]
mod tests {
use super::*;
// ── SlashCommand parsing tests ────────────────────────────────────
#[test]
fn test_parse_model_no_arg() {
let cmd = SlashCommand::parse("/model");
assert_eq!(cmd, SlashCommand::Model { search: None });
}
#[test]
fn test_parse_model_with_search() {
let cmd = SlashCommand::parse("/model claude-sonnet");
assert_eq!(
cmd,
SlashCommand::Model {
search: Some("claude-sonnet".to_string()),
}
);
}
#[test]
fn test_parse_clear() {
assert_eq!(SlashCommand::parse("/clear"), SlashCommand::Clear);
}
#[test]
fn test_parse_compact_no_arg() {
assert_eq!(
SlashCommand::parse("/compact"),
SlashCommand::Compact {
custom_instructions: None
}
);
}
#[test]
fn test_parse_compact_with_instructions() {
assert_eq!(
SlashCommand::parse("/compact focus on error handling"),
SlashCommand::Compact {
custom_instructions: Some("focus on error handling".to_string()),
}
);
}
#[test]
fn test_parse_undo_redo() {
assert_eq!(SlashCommand::parse("/undo"), SlashCommand::Undo);
assert_eq!(SlashCommand::parse("/redo"), SlashCommand::Redo);
}
#[test]
fn test_parse_aliases() {
// /? is an alias for /help
assert_eq!(SlashCommand::parse("/?"), SlashCommand::Help);
// /exit and /q are aliases for /quit
assert_eq!(SlashCommand::parse("/exit"), SlashCommand::Quit);
assert_eq!(SlashCommand::parse("/q"), SlashCommand::Quit);
// /fork and /tree are aliases for /branch
assert_eq!(SlashCommand::parse("/fork"), SlashCommand::Branch);
assert_eq!(SlashCommand::parse("/tree"), SlashCommand::Branch);
// /resume is alias for /session
assert_eq!(SlashCommand::parse("/resume"), SlashCommand::Session);
}
#[test]
fn test_parse_unknown() {
let cmd = SlashCommand::parse("/foobar");
assert_eq!(
cmd,
SlashCommand::Unknown {
raw: "/foobar".to_string()
}
);
}
// ── State machine tests ───────────────────────────────────────────
#[test]
fn test_state_ordering() {
// Verify that states exist and are distinct
let states = [
InteractiveState::Input,
InteractiveState::Thinking,
InteractiveState::ToolExecution,
InteractiveState::Display,
];
// All unique
for i in 0..states.len() {
for j in (i + 1)..states.len() {
assert_ne!(states[i], states[j]);
}
}
}
#[test]
fn test_state_transitions_input_to_thinking() {
let state = InteractiveState::Input;
// On user submit: Input -> Thinking
let next = InteractiveState::Thinking;
assert_eq!(next, InteractiveState::Thinking);
assert_ne!(state, next);
}
#[test]
fn test_state_transitions_thinking_to_tool_execution() {
// On tool call: Thinking -> ToolExecution
let state = InteractiveState::Thinking;
let next = InteractiveState::ToolExecution;
assert_ne!(state, next);
}
#[test]
fn test_state_transitions_tool_execution_to_display() {
// On complete: ToolExecution -> Display -> Input
let state = InteractiveState::ToolExecution;
let display = InteractiveState::Display;
let input = InteractiveState::Input;
assert_ne!(state, display);
assert_ne!(display, input);
}
// ── Bash execution tests ──────────────────────────────────────────
#[test]
fn test_bash_command_execution() {
let output = run_bash_command("echo hello");
assert!(output.contains("hello"));
}
#[test]
fn test_bash_command_failure() {
let output = run_bash_command("false");
assert!(output.contains("Exit code:"));
}
// ── Export tests ──────────────────────────────────────────────────
#[test]
fn test_export_empty_session() {
let session = InteractiveSession::new();
let json = export_session_json(&session);
assert!(json.contains("\"messages\": []"));
assert!(json.contains("\"entry_count\": 0"));
}
#[test]
fn test_export_session_with_messages() {
let mut session = InteractiveSession::new();
session.add_user_message("Hello".to_string());
session.add_assistant_message("Hi there!".to_string());
let json = export_session_json(&session);
assert!(json.contains("\"role\": \"user\""));
assert!(json.contains("\"content\": \"Hello\""));
assert!(json.contains("\"role\": \"assistant\""));
}
// ── Session info tests ────────────────────────────────────────────
#[test]
fn test_session_info_empty() {
let session = InteractiveSession::new();
let info = format_session_info(&session);
assert!(info.contains("Messages: 0 total"));
assert!(info.contains("ID: none"));
}
#[test]
fn test_session_info_with_messages() {
let mut session = InteractiveSession::new();
session.add_user_message("Hello".to_string());
session.add_assistant_message("Hi".to_string());
let info = format_session_info(&session);
assert!(info.contains("Messages: 2 total"));
assert!(info.contains("1 user"));
assert!(info.contains("1 assistant"));
}
// ── Help text test ────────────────────────────────────────────────
#[test]
fn test_help_text_contains_all_commands() {
let help = format_help();
assert!(help.contains("/model"));
assert!(help.contains("/clear"));
assert!(help.contains("/compact"));
assert!(help.contains("/undo"));
assert!(help.contains("/redo"));
assert!(help.contains("/branch"));
assert!(help.contains("/session"));
assert!(help.contains("/export"));
assert!(help.contains("/settings"));
assert!(help.contains("/help"));
assert!(help.contains("/quit"));
}
// ── Command description tests ─────────────────────────────────────
#[test]
fn test_command_descriptions() {
assert_eq!(
SlashCommand::Model { search: None }.description(),
"Select model"
);
assert_eq!(
SlashCommand::Clear.description(),
"Clear conversation history"
);
assert_eq!(SlashCommand::Undo.description(), "Undo last exchange");
assert_eq!(
SlashCommand::Redo.description(),
"Redo last undone exchange"
);
assert_eq!(SlashCommand::Quit.description(), "Quit oxi");
assert_eq!(
SlashCommand::Unknown {
raw: "/x".to_string()
}
.description(),
"Unknown command"
);
}
// ── Image attachment tests ─────────────────────────────────────────
#[test]
fn test_image_attachment_from_data_uri() {
// Valid PNG data URI
let uri = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUg==";
let img = ImageAttachment::from_data_uri(uri);
assert!(img.is_some());
let img = img.unwrap();
assert_eq!(img.mime_type, "image/png");
}
#[test]
fn test_image_attachment_invalid_uri() {
// Not a data URI
let img = ImageAttachment::from_data_uri("not a data uri");
assert!(img.is_none());
}
#[test]
fn test_image_attachment_extension() {
let img = ImageAttachment {
mime_type: "image/png".to_string(),
base64_data: String::new(),
width: None,
height: None,
};
assert_eq!(img.extension(), "png");
let img_jpeg = ImageAttachment {
mime_type: "image/jpeg".to_string(),
base64_data: String::new(),
width: None,
height: None,
};
assert_eq!(img_jpeg.extension(), "jpg");
}
#[test]
fn test_image_attachment_detect_mime_type() {
// PNG magic bytes
let png_bytes: Vec<u8> = vec![0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A];
assert_eq!(ImageAttachment::detect_mime_type(&png_bytes), "image/png");
// JPEG magic bytes
let jpeg_bytes: Vec<u8> = vec![0xFF, 0xD8, 0xFF, 0xE0, 0x00, 0x10, 0x4A, 0x46];
assert_eq!(ImageAttachment::detect_mime_type(&jpeg_bytes), "image/jpeg");
// Unknown
let unknown: Vec<u8> = vec![0x00, 0x00, 0x00, 0x00];
assert_eq!(ImageAttachment::detect_mime_type(&unknown), "image/png"); // fallback
}
#[test]
fn test_image_attachment_from_bytes() {
// PNG magic bytes
let png_data: Vec<u8> = vec![0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A];
let img = ImageAttachment::from_bytes(png_data);
assert!(img.is_some());
let img = img.unwrap();
assert_eq!(img.mime_type, "image/png");
assert!(!img.base64_data.is_empty());
}
// ── Session persistence tests ─────────────────────────────────────
#[test]
fn test_session_persistence_new() {
let persistence = SessionPersistence::new();
// May be None if HOME not set or dir creation fails
assert!(persistence.is_some() || persistence.is_none());
}
// ── Keybinding hints tests ─────────────────────────────────────────
#[test]
fn test_keybinding_hints_compact() {
let hints = KeybindingHints::new();
let compact = hints.compact_display();
assert!(compact.contains("Ctrl+C"));
assert!(compact.contains("quit"));
}
#[test]
fn test_keybinding_hints_expanded() {
let hints = KeybindingHints::new();
let expanded = hints.expanded_display();
assert!(expanded.contains("Ctrl+C"));
assert!(expanded.contains("Ctrl+L"));
assert!(expanded.contains("Ctrl+U"));
}
#[test]
fn test_keybinding_hints_toggle() {
let mut hints = KeybindingHints::new();
assert!(!hints.is_expanded());
hints.toggle();
assert!(hints.is_expanded());
hints.toggle();
assert!(!hints.is_expanded());
}
// ── Word-level diff tests ──────────────────────────────────────────
#[test]
fn test_compute_word_diff_identical() {
let result = compute_word_diff("hello world", "hello world");
let (added, removed, unchanged) = result.summary();
assert_eq!(added, 0);
assert_eq!(removed, 0);
assert_eq!(unchanged, 2);
}
#[test]
fn test_compute_word_diff_added_words() {
let result = compute_word_diff("hello", "hello world");
let (added, removed, _) = result.summary();
assert_eq!(added, 1); // "world" added
assert_eq!(removed, 0);
}
#[test]
fn test_compute_word_diff_removed_words() {
let result = compute_word_diff("hello world", "hello");
let (added, removed, _) = result.summary();
assert_eq!(added, 0);
assert_eq!(removed, 1); // "world" removed
}
#[test]
fn test_compute_word_diff_changed() {
let result = compute_word_diff("hello world", "hello rust");
let (added, removed, unchanged) = result.summary();
assert_eq!(added, 1); // "rust" added
assert_eq!(removed, 1); // "world" removed
assert_eq!(unchanged, 1); // "hello" unchanged
}
#[test]
fn test_diff_result_format_ansi() {
let result = compute_word_diff("foo bar", "foo baz");
let formatted = result.format_ansi();
assert!(formatted.contains("foo"));
assert!(formatted.contains("bar") || formatted.contains("baz"));
}
#[test]
fn test_diff_result_empty() {
let result = compute_word_diff("", "hello");
let (added, removed, _) = result.summary();
assert_eq!(added, 1);
assert_eq!(removed, 0);
}
#[test]
fn test_lcs_algorithm() {
let a = vec!["a", "b", "c"];
let b = vec!["a", "c", "d"];
let lcs = longest_common_subsequence(&a, &b);
assert!(lcs.contains(&(0, 0))); // "a"
assert!(lcs.contains(&(2, 1))); // "c"
}
}