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
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
//! Agent module for interactive AI-powered CLI assistance
//!
//! This module provides an agent layer using the Rig library that allows users
//! to interact with the CLI through natural language conversations.
//!
//! # Features
//!
//! - **Conversation History**: Maintains context across multiple turns
//! - **Automatic Compaction**: Compresses old history when token count exceeds threshold
//! - **Tool Tracking**: Records tool calls for better context preservation
//!
//! # Usage
//!
//! ```bash
//! # Interactive mode
//! sync-ctl chat
//!
//! # With specific provider
//! sync-ctl chat --provider openai --model gpt-5.2
//!
//! # Single query
//! sync-ctl chat --query "What security issues does this project have?"
//! ```
//!
//! # Interactive Commands
//!
//! - `/model` - Switch to a different AI model
//! - `/provider` - Switch provider (prompts for API key if needed)
//! - `/help` - Show available commands
//! - `/clear` - Clear conversation history
//! - `/exit` - Exit the chat
pub mod commands;
pub mod compact;
pub mod history;
pub mod ide;
pub mod persistence;
pub mod prompts;
pub mod session;
pub mod tools;
pub mod ui;
use colored::Colorize;
use commands::TokenUsage;
use history::{ConversationHistory, ToolCallRecord};
use ide::IdeClient;
use rig::{
client::{CompletionClient, ProviderClient},
completion::Prompt,
providers::{anthropic, openai},
};
use session::{ChatSession, PlanMode};
use std::path::Path;
use std::sync::Arc;
use tokio::sync::Mutex as TokioMutex;
use ui::{ResponseFormatter, ToolDisplayHook};
/// Provider type for the agent
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ProviderType {
#[default]
OpenAI,
Anthropic,
Bedrock,
}
impl std::fmt::Display for ProviderType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ProviderType::OpenAI => write!(f, "openai"),
ProviderType::Anthropic => write!(f, "anthropic"),
ProviderType::Bedrock => write!(f, "bedrock"),
}
}
}
impl std::str::FromStr for ProviderType {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_str() {
"openai" => Ok(ProviderType::OpenAI),
"anthropic" => Ok(ProviderType::Anthropic),
"bedrock" | "aws" | "aws-bedrock" => Ok(ProviderType::Bedrock),
_ => Err(format!(
"Unknown provider: {}. Use: openai, anthropic, or bedrock",
s
)),
}
}
}
/// Error types for the agent
#[derive(Debug, thiserror::Error)]
pub enum AgentError {
#[error("Missing API key. Set {0} environment variable.")]
MissingApiKey(String),
#[error("Provider error: {0}")]
ProviderError(String),
#[error("Tool error: {0}")]
ToolError(String),
}
pub type AgentResult<T> = Result<T, AgentError>;
// =============================================================================
// AG-UI State Types
// =============================================================================
/// Agent state for AG-UI state synchronization
#[derive(Debug, Clone, serde::Serialize)]
pub struct AgentState {
/// Project being analyzed
pub project_path: String,
/// LLM provider name
pub provider: String,
/// Model being used
pub model: String,
/// Whether plan mode is active
pub plan_mode: bool,
/// Token usage statistics
pub token_usage: TokenUsageState,
/// Conversation state
pub conversation: ConversationState,
}
/// Token usage state for AG-UI
#[derive(Debug, Clone, serde::Serialize)]
pub struct TokenUsageState {
/// Estimated input tokens
pub input_tokens: usize,
/// Estimated output tokens
pub output_tokens: usize,
/// Total tokens
pub total_tokens: usize,
}
/// Conversation state for AG-UI
#[derive(Debug, Clone, serde::Serialize)]
pub struct ConversationState {
/// Number of conversation turns
pub turn_count: usize,
/// Whether history has been compacted
pub has_compacted: bool,
}
/// Build AgentState from session and conversation history
fn build_agent_state(session: &ChatSession, history: &ConversationHistory) -> AgentState {
// Check if history has been compacted (status contains "compacted")
let has_compacted = history.status().contains("compacted");
let input = session.token_usage.prompt_tokens as usize;
let output = session.token_usage.completion_tokens as usize;
AgentState {
project_path: session.project_path.display().to_string(),
provider: session.provider.to_string(),
model: session.model.clone(),
plan_mode: session.plan_mode.is_planning(),
token_usage: TokenUsageState {
input_tokens: input,
output_tokens: output,
total_tokens: input + output,
},
conversation: ConversationState {
turn_count: history.turn_count(),
has_compacted,
},
}
}
/// Get the system prompt for the agent based on query type and plan mode
fn get_system_prompt(project_path: &Path, query: Option<&str>, plan_mode: PlanMode) -> String {
// In planning mode, use the read-only exploration prompt
if plan_mode.is_planning() {
return prompts::get_planning_prompt(project_path);
}
if let Some(q) = query {
// First check if it's a code development task (highest priority)
if prompts::is_code_development_query(q) {
return prompts::get_code_development_prompt(project_path);
}
// Then check if it's DevOps generation (Docker, Terraform, Helm)
if prompts::is_generation_query(q) {
return prompts::get_devops_prompt(project_path, Some(q));
}
}
// Default to analysis prompt
prompts::get_analysis_prompt(project_path)
}
/// Run the agent as a dedicated AG-UI server (headless mode for containers/deployments).
///
/// This starts the AG-UI server without interactive stdin, accepting connections
/// from frontends via SSE or WebSocket. The agent processes messages received
/// through the AG-UI protocol.
///
/// # Arguments
///
/// * `project_path` - Path to the project directory
/// * `provider` - LLM provider to use
/// * `model` - Optional model override
/// * `host` - Host address to bind to
/// * `port` - Port number to listen on
pub async fn run_agent_server(
project_path: &Path,
provider: ProviderType,
model: Option<String>,
host: &str,
port: u16,
) -> AgentResult<()> {
use crate::server::{AgUiConfig, AgUiServer, ProcessorConfig};
// Configure the agent processor with provider, model, and project path
// Use regional model IDs (no global. prefix) for wider availability
let default_model = match provider {
// Claude 3.5 Sonnet v2 is widely available across regions
ProviderType::Bedrock => "anthropic.claude-3-5-sonnet-20241022-v2:0".to_string(),
ProviderType::Anthropic => "claude-3-5-sonnet-20241022".to_string(),
ProviderType::OpenAI => "gpt-4o".to_string(),
};
let processor_config = ProcessorConfig::new()
.with_provider(&provider.to_string())
.with_model(&model.unwrap_or(default_model))
.with_project_path(project_path);
let config = AgUiConfig::new()
.port(port)
.host(host)
.with_processor_config(processor_config);
let server = AgUiServer::new(config);
println!("AG-UI agent server listening on http://{}:{}", host, port);
println!("Project path: {}", project_path.display());
println!("Connect frontends via SSE (/sse) or WebSocket (/ws)");
println!("Press Ctrl+C to stop the server");
// Run server (blocks until shutdown signal)
server
.run()
.await
.map_err(|e| AgentError::ProviderError(e.to_string()))
}
/// Run the agent in interactive mode with custom REPL supporting /model and /provider commands
pub async fn run_interactive(
project_path: &Path,
provider: ProviderType,
model: Option<String>,
event_bridge: Option<crate::server::EventBridge>,
) -> AgentResult<()> {
use tools::*;
let mut session = ChatSession::new(project_path, provider, model);
// Store event bridge for use in tool hooks
let event_bridge = event_bridge;
// Shared background process manager for Prometheus port-forwards
let bg_manager = Arc::new(BackgroundProcessManager::new());
// Terminal layout for split screen is disabled for now - see notes below
// let terminal_layout = ui::TerminalLayout::new();
// let layout_state = terminal_layout.state();
// Initialize conversation history with compaction support
let mut conversation_history = ConversationHistory::new();
// Initialize IDE client for native diff viewing
let ide_client: Option<Arc<TokioMutex<IdeClient>>> = {
let mut client = IdeClient::new().await;
if client.is_ide_available() {
match client.connect().await {
Ok(()) => {
println!(
"{} Connected to {} IDE companion",
"✓".green(),
client.ide_name().unwrap_or("VS Code")
);
Some(Arc::new(TokioMutex::new(client)))
}
Err(e) => {
// IDE detected but companion not running or connection failed
println!("{} IDE companion not connected: {}", "!".yellow(), e);
None
}
}
} else {
println!(
"{} No IDE detected (TERM_PROGRAM={})",
"·".dimmed(),
std::env::var("TERM_PROGRAM").unwrap_or_default()
);
None
}
};
// Load API key from config file to env if not already set
ChatSession::load_api_key_to_env(session.provider);
// Check if API key is configured, prompt if not
if !ChatSession::has_api_key(session.provider) {
ChatSession::prompt_api_key(session.provider)?;
}
session.print_banner();
// Display platform context if a project is selected
if session.platform_session.is_project_selected() {
println!(
"{}",
format!(
"Platform context: {}",
session.platform_session.display_context()
)
.dimmed()
);
}
// NOTE: Terminal layout with ANSI scroll regions is disabled for now.
// The scroll region approach conflicts with the existing input/output flow.
// TODO: Implement proper scroll region support that integrates with the input handler.
// For now, we rely on the pause/resume mechanism in progress indicator.
//
// if let Err(e) = terminal_layout.init() {
// eprintln!(
// "{}",
// format!("Note: Terminal layout initialization failed: {}. Using fallback mode.", e)
// .dimmed()
// );
// }
// Raw Rig messages for multi-turn - preserves Reasoning blocks for thinking
// Our ConversationHistory only stores text summaries, but rig needs full Message structure
let mut raw_chat_history: Vec<rig::completion::Message> = Vec::new();
// Pending input for auto-continue after plan creation
let mut pending_input: Option<String> = None;
// Auto-accept mode for plan execution (skips write confirmations)
let mut auto_accept_writes = false;
// Initialize session recorder for conversation persistence
let mut session_recorder = persistence::SessionRecorder::new(project_path);
// Track if we exit due to an error (for AG-UI error events)
let mut exit_error: Option<String> = None;
// Emit AG-UI RunStarted event and initial state for connected frontends
if let Some(ref bridge) = event_bridge {
bridge.start_run().await;
// Emit initial agent state snapshot
let state = build_agent_state(&session, &conversation_history);
if let Ok(state_json) = serde_json::to_value(&state) {
bridge.emit_state_snapshot(state_json).await;
}
}
loop {
// Show conversation status if we have history
if !conversation_history.is_empty() {
println!(
"{}",
format!(" 💬 Context: {}", conversation_history.status()).dimmed()
);
}
// Check for pending input (from plan menu selection)
let input = if let Some(pending) = pending_input.take() {
// Show what we're executing
println!("{} {}", "→".cyan(), pending.dimmed());
pending
} else {
// New user turn - reset auto-accept mode from previous plan execution
auto_accept_writes = false;
// Read user input (returns InputResult)
let input_result = match session.read_input() {
Ok(result) => result,
Err(_) => break,
};
// Handle the input result
match input_result {
ui::InputResult::Submit(text) => ChatSession::process_submitted_text(&text),
ui::InputResult::Cancel | ui::InputResult::Exit => break,
ui::InputResult::TogglePlanMode => {
// Toggle planning mode - minimal feedback, no extra newlines
let new_mode = session.toggle_plan_mode();
if new_mode.is_planning() {
println!("{}", "★ plan mode".yellow());
} else {
println!("{}", "▶ standard mode".green());
}
// Emit AG-UI state delta for plan mode change
if let Some(ref bridge) = event_bridge {
bridge
.emit_state_delta(vec![serde_json::json!({
"op": "replace",
"path": "/plan_mode",
"value": new_mode.is_planning()
})])
.await;
}
continue;
}
}
};
if input.is_empty() {
continue;
}
// Check for commands
if ChatSession::is_command(&input) {
// Special handling for /clear to also clear conversation history
if input.trim().to_lowercase() == "/clear" || input.trim().to_lowercase() == "/c" {
conversation_history.clear();
raw_chat_history.clear();
}
match session.process_command(&input) {
Ok(true) => {
// Check if /resume loaded a session
if let Some(record) = session.pending_resume.take() {
// Display previous messages
println!();
println!("{}", "─── Previous Conversation ───".dimmed());
for msg in &record.messages {
match msg.role {
persistence::MessageRole::User => {
println!();
println!(
"{} {}",
"You:".cyan().bold(),
truncate_string(&msg.content, 500)
);
}
persistence::MessageRole::Assistant => {
println!();
// Show tool calls if any (same format as live display)
if let Some(ref tools) = msg.tool_calls {
for tc in tools {
// Match live tool display: green dot for completed, cyan bold name
if tc.args_summary.is_empty() {
println!(
"{} {}",
"●".green(),
tc.name.cyan().bold()
);
} else {
println!(
"{} {}({})",
"●".green(),
tc.name.cyan().bold(),
truncate_string(&tc.args_summary, 50).dimmed()
);
}
}
}
// Show response (same ResponseFormatter as live)
if !msg.content.is_empty() {
ResponseFormatter::print_response(&truncate_string(
&msg.content,
1000,
));
}
}
persistence::MessageRole::System => {
// Skip system messages in display
}
}
}
println!("{}", "─── End of History ───".dimmed());
println!();
// Try to restore from history_snapshot (new format with full context)
let restored_from_snapshot = if let Some(history_json) =
&record.history_snapshot
{
match ConversationHistory::from_json(history_json) {
Ok(restored) => {
conversation_history = restored;
// Rebuild raw_chat_history from restored conversation_history
raw_chat_history = conversation_history.to_messages();
println!(
"{}",
" ✓ Restored full conversation context (including compacted history)".green()
);
true
}
Err(e) => {
eprintln!(
"{}",
format!(
" Warning: Failed to restore history snapshot: {}",
e
)
.yellow()
);
false
}
}
} else {
false
};
// Fallback: Load from messages (old format or if snapshot failed)
if !restored_from_snapshot {
// Load messages into raw_chat_history for AI context
for msg in &record.messages {
match msg.role {
persistence::MessageRole::User => {
raw_chat_history.push(rig::completion::Message::User {
content: rig::one_or_many::OneOrMany::one(
rig::completion::message::UserContent::text(
&msg.content,
),
),
});
}
persistence::MessageRole::Assistant => {
raw_chat_history
.push(rig::completion::Message::Assistant {
id: Some(msg.id.clone()),
content: rig::one_or_many::OneOrMany::one(
rig::completion::message::AssistantContent::text(
&msg.content,
),
),
});
}
persistence::MessageRole::System => {}
}
}
// Load into conversation_history with tool calls from message records
for msg in &record.messages {
if msg.role == persistence::MessageRole::User {
// Find the next assistant message
let (response, tool_calls) = record
.messages
.iter()
.skip_while(|m| m.id != msg.id)
.skip(1)
.find(|m| m.role == persistence::MessageRole::Assistant)
.map(|m| {
let tcs = m.tool_calls.as_ref().map(|calls| {
calls
.iter()
.map(|tc| history::ToolCallRecord {
tool_name: tc.name.clone(),
args_summary: tc.args_summary.clone(),
result_summary: tc.result_summary.clone(),
tool_id: None,
droppable: false,
})
.collect::<Vec<_>>()
});
(m.content.clone(), tcs.unwrap_or_default())
})
.unwrap_or_default();
conversation_history.add_turn(
msg.content.clone(),
response,
tool_calls,
);
}
}
println!(
"{}",
format!(
" ✓ Loaded {} messages (legacy format).",
record.messages.len()
)
.green()
);
}
println!();
}
continue;
}
Ok(false) => break, // /exit
Err(e) => {
eprintln!("{}", format!("Error: {}", e).red());
continue;
}
}
}
// Check API key before making request (in case provider changed)
if !ChatSession::has_api_key(session.provider) {
eprintln!(
"{}",
"No API key configured. Use /provider to set one.".yellow()
);
continue;
}
// Check if compaction is needed before making the request
if conversation_history.needs_compaction() {
println!("{}", " 📦 Compacting conversation history...".dimmed());
if let Some(summary) = conversation_history.compact() {
println!(
"{}",
format!(" ✓ Compressed {} turns", summary.matches("Turn").count()).dimmed()
);
}
}
// Pre-request check: estimate if we're approaching context limit
// Check raw_chat_history (actual messages) not conversation_history
// because conversation_history may be out of sync
let estimated_input_tokens = estimate_raw_history_tokens(&raw_chat_history)
+ input.len() / 4 // New input
+ 5000; // System prompt overhead estimate
if estimated_input_tokens > 150_000 {
println!(
"{}",
" ⚠ Large context detected. Pre-truncating...".yellow()
);
let old_count = raw_chat_history.len();
// Keep last 20 messages when approaching limit
if raw_chat_history.len() > 20 {
let drain_count = raw_chat_history.len() - 20;
raw_chat_history.drain(0..drain_count);
// Ensure history starts with User message for OpenAI Responses API compatibility
ensure_history_starts_with_user(&mut raw_chat_history);
// Preserve compacted summary while clearing turns to stay in sync
conversation_history.clear_turns_preserve_context();
println!(
"{}",
format!(
" ✓ Truncated {} → {} messages",
old_count,
raw_chat_history.len()
)
.dimmed()
);
}
}
// Retry loop for automatic error recovery
// MAX_RETRIES is for failures without progress
// MAX_CONTINUATIONS is for truncations WITH progress (more generous)
// TOOL_CALL_CHECKPOINT is the interval at which we ask user to confirm
// MAX_TOOL_CALLS is the absolute maximum (300 = 6 checkpoints x 50)
const MAX_RETRIES: u32 = 3;
const MAX_CONTINUATIONS: u32 = 10;
const _TOOL_CALL_CHECKPOINT: usize = 50;
const MAX_TOOL_CALLS: usize = 300;
let mut retry_attempt = 0;
let mut continuation_count = 0;
let mut total_tool_calls: usize = 0;
let mut auto_continue_tools = false; // User can select "always" to skip future prompts
let mut current_input = input.clone();
let mut succeeded = false;
// Emit AG-UI step event for processing
if let Some(ref bridge) = event_bridge {
bridge.start_step("processing").await;
}
while retry_attempt < MAX_RETRIES && continuation_count < MAX_CONTINUATIONS && !succeeded {
// Log if this is a continuation attempt
if continuation_count > 0 {
eprintln!("{}", " 📡 Sending continuation request...".dimmed());
}
// Create hook for Claude Code style tool display
let hook = ToolDisplayHook::new();
// Create progress indicator for visual feedback during generation
let progress = ui::GenerationIndicator::new();
// Layout connection disabled - using inline progress mode
// progress.state().set_layout(layout_state.clone());
hook.set_progress_state(progress.state()).await;
// Connect AG-UI EventBridge if provided (for streaming tool events to frontends)
if let Some(ref bridge) = event_bridge {
hook.set_event_bridge(bridge.clone()).await;
}
let project_path_buf = session.project_path.clone();
// Select prompt based on query type (analysis vs generation) and plan mode
let preamble = get_system_prompt(
&session.project_path,
Some(¤t_input),
session.plan_mode,
);
let is_planning = session.plan_mode.is_planning();
// Inherit generation mode for short follow-up messages ("sure", "yes", "go ahead",
// etc.) so the write/shell tool set is not lost between turns.
let is_generation = prompts::is_generation_query(¤t_input)
|| (!is_planning && session.last_was_generation && current_input.trim().len() < 60);
// Note: using raw_chat_history directly which preserves Reasoning blocks
// This is needed for extended thinking to work with multi-turn conversations
// Get progress state for interrupt detection
let progress_state = progress.state();
// Use tokio::select! to race the API call against Ctrl+C
// This allows immediate cancellation, not just between tool calls
let mut user_interrupted = false;
// Emit AG-UI thinking event before LLM call
if let Some(ref bridge) = event_bridge {
bridge.start_thinking(Some("Generating response")).await;
}
// API call with Ctrl+C interrupt support
let response = tokio::select! {
biased; // Check ctrl_c first for faster response
_ = tokio::signal::ctrl_c() => {
user_interrupted = true;
Err::<String, String>("User cancelled".to_string())
}
result = async {
match session.provider {
ProviderType::OpenAI => {
// Use Responses API (default) for reasoning model support.
// rig-core 0.28+ handles Reasoning items properly in multi-turn.
let client = openai::Client::from_env();
let mut builder = client
.agent(&session.model)
.preamble(&preamble)
.max_tokens(4096)
.tool(AnalyzeTool::new(project_path_buf.clone()))
.tool(SecurityScanTool::new(project_path_buf.clone()))
.tool(VulnerabilitiesTool::new(project_path_buf.clone()))
.tool(HadolintTool::new(project_path_buf.clone()))
.tool(DclintTool::new(project_path_buf.clone()))
.tool(KubelintTool::new(project_path_buf.clone()))
.tool(K8sOptimizeTool::new(project_path_buf.clone()))
.tool(K8sCostsTool::new(project_path_buf.clone()))
.tool(K8sDriftTool::new(project_path_buf.clone()))
.tool(HelmlintTool::new(project_path_buf.clone()))
.tool(TerraformFmtTool::new(project_path_buf.clone()))
.tool(TerraformValidateTool::new(project_path_buf.clone()))
.tool(TerraformInstallTool::new())
.tool(ReadFileTool::new(project_path_buf.clone()))
.tool(ListDirectoryTool::new(project_path_buf.clone()))
.tool(WebFetchTool::new())
// Prometheus discovery and connection tools for live K8s analysis
.tool(PrometheusDiscoverTool::new())
.tool(PrometheusConnectTool::new(bg_manager.clone()))
// RAG retrieval tools for compressed tool outputs
.tool(RetrieveOutputTool::new())
.tool(ListOutputsTool::new())
// Platform tools for project management
.tool(ListOrganizationsTool::new())
.tool(ListProjectsTool::new())
.tool(SelectProjectTool::new())
.tool(CurrentContextTool::new())
.tool(OpenProviderSettingsTool::new())
.tool(CheckProviderConnectionTool::new())
.tool(ListDeploymentCapabilitiesTool::new())
.tool(ListHetznerAvailabilityTool::new())
// Deployment tools for service management
.tool(CreateDeploymentConfigTool::new())
.tool(DeployServiceTool::with_context(project_path_buf.clone(), ExecutionContext::InteractiveCli))
.tool(ListDeploymentConfigsTool::new())
.tool(TriggerDeploymentTool::new())
.tool(GetDeploymentStatusTool::new())
.tool(ListDeploymentsTool::new())
.tool(GetServiceLogsTool::new())
.tool(SetDeploymentSecretsTool::with_context(ExecutionContext::InteractiveCli));
// Add tools based on mode
if is_planning {
// Plan mode: read-only shell + plan creation tools
builder = builder
.tool(ShellTool::new(project_path_buf.clone()).with_read_only(true))
.tool(PlanCreateTool::new(project_path_buf.clone()))
.tool(PlanListTool::new(project_path_buf.clone()));
} else if is_generation {
// Standard mode + generation query: all tools including file writes and plan execution
let (mut write_file_tool, mut write_files_tool) =
if let Some(ref client) = ide_client {
(
WriteFileTool::new(project_path_buf.clone())
.with_ide_client(client.clone()),
WriteFilesTool::new(project_path_buf.clone())
.with_ide_client(client.clone()),
)
} else {
(
WriteFileTool::new(project_path_buf.clone()),
WriteFilesTool::new(project_path_buf.clone()),
)
};
// Disable confirmations if auto-accept mode is enabled (from plan menu)
if auto_accept_writes {
write_file_tool = write_file_tool.without_confirmation();
write_files_tool = write_files_tool.without_confirmation();
}
builder = builder
.tool(write_file_tool)
.tool(write_files_tool)
.tool(ShellTool::new(project_path_buf.clone()))
.tool(PlanListTool::new(project_path_buf.clone()))
.tool(PlanNextTool::new(project_path_buf.clone()))
.tool(PlanUpdateTool::new(project_path_buf.clone()));
}
// Enable reasoning for OpenAI reasoning models (GPT-5.x, O1, O3, O4)
let model_lower = session.model.to_lowercase();
let is_reasoning_model = model_lower.starts_with("gpt-5")
|| model_lower.starts_with("gpt5")
|| model_lower.starts_with("o1")
|| model_lower.starts_with("o3")
|| model_lower.starts_with("o4");
let agent = if is_reasoning_model {
let reasoning_params = serde_json::json!({
"reasoning": {
"effort": "medium",
"summary": "detailed"
}
});
builder.additional_params(reasoning_params).build()
} else {
builder.build()
};
// Use multi_turn with Responses API
agent
.prompt(¤t_input)
.with_history(&mut raw_chat_history)
.with_hook(hook.clone())
.multi_turn(50)
.await
}
ProviderType::Anthropic => {
let client = anthropic::Client::from_env();
// TODO: Extended thinking for Claude is disabled because rig-bedrock/rig-anthropic
// don't properly handle thinking blocks in multi-turn conversations with tool use.
// When thinking is enabled, ALL assistant messages must start with thinking blocks
// BEFORE tool_use blocks, but rig doesn't preserve/replay these.
// See: forge/crates/forge_services/src/provider/bedrock/provider.rs for reference impl.
let mut builder = client
.agent(&session.model)
.preamble(&preamble)
.max_tokens(4096)
.tool(AnalyzeTool::new(project_path_buf.clone()))
.tool(SecurityScanTool::new(project_path_buf.clone()))
.tool(VulnerabilitiesTool::new(project_path_buf.clone()))
.tool(HadolintTool::new(project_path_buf.clone()))
.tool(DclintTool::new(project_path_buf.clone()))
.tool(KubelintTool::new(project_path_buf.clone()))
.tool(K8sOptimizeTool::new(project_path_buf.clone()))
.tool(K8sCostsTool::new(project_path_buf.clone()))
.tool(K8sDriftTool::new(project_path_buf.clone()))
.tool(HelmlintTool::new(project_path_buf.clone()))
.tool(TerraformFmtTool::new(project_path_buf.clone()))
.tool(TerraformValidateTool::new(project_path_buf.clone()))
.tool(TerraformInstallTool::new())
.tool(ReadFileTool::new(project_path_buf.clone()))
.tool(ListDirectoryTool::new(project_path_buf.clone()))
.tool(WebFetchTool::new())
// Prometheus discovery and connection tools for live K8s analysis
.tool(PrometheusDiscoverTool::new())
.tool(PrometheusConnectTool::new(bg_manager.clone()))
// RAG retrieval tools for compressed tool outputs
.tool(RetrieveOutputTool::new())
.tool(ListOutputsTool::new())
// Platform tools for project management
.tool(ListOrganizationsTool::new())
.tool(ListProjectsTool::new())
.tool(SelectProjectTool::new())
.tool(CurrentContextTool::new())
.tool(OpenProviderSettingsTool::new())
.tool(CheckProviderConnectionTool::new())
.tool(ListDeploymentCapabilitiesTool::new())
.tool(ListHetznerAvailabilityTool::new())
// Deployment tools for service management
.tool(CreateDeploymentConfigTool::new())
.tool(DeployServiceTool::with_context(project_path_buf.clone(), ExecutionContext::InteractiveCli))
.tool(ListDeploymentConfigsTool::new())
.tool(TriggerDeploymentTool::new())
.tool(GetDeploymentStatusTool::new())
.tool(ListDeploymentsTool::new())
.tool(GetServiceLogsTool::new())
.tool(SetDeploymentSecretsTool::with_context(ExecutionContext::InteractiveCli));
// Add tools based on mode
if is_planning {
// Plan mode: read-only shell + plan creation tools
builder = builder
.tool(ShellTool::new(project_path_buf.clone()).with_read_only(true))
.tool(PlanCreateTool::new(project_path_buf.clone()))
.tool(PlanListTool::new(project_path_buf.clone()));
} else if is_generation {
// Standard mode + generation query: all tools including file writes and plan execution
let (mut write_file_tool, mut write_files_tool) =
if let Some(ref client) = ide_client {
(
WriteFileTool::new(project_path_buf.clone())
.with_ide_client(client.clone()),
WriteFilesTool::new(project_path_buf.clone())
.with_ide_client(client.clone()),
)
} else {
(
WriteFileTool::new(project_path_buf.clone()),
WriteFilesTool::new(project_path_buf.clone()),
)
};
// Disable confirmations if auto-accept mode is enabled (from plan menu)
if auto_accept_writes {
write_file_tool = write_file_tool.without_confirmation();
write_files_tool = write_files_tool.without_confirmation();
}
builder = builder
.tool(write_file_tool)
.tool(write_files_tool)
.tool(ShellTool::new(project_path_buf.clone()))
.tool(PlanListTool::new(project_path_buf.clone()))
.tool(PlanNextTool::new(project_path_buf.clone()))
.tool(PlanUpdateTool::new(project_path_buf.clone()));
}
let agent = builder.build();
// Allow up to 50 tool call turns for complex generation tasks
// Use hook to display tool calls as they happen
// Pass conversation history for context continuity
agent
.prompt(¤t_input)
.with_history(&mut raw_chat_history)
.with_hook(hook.clone())
.multi_turn(50)
.await
}
ProviderType::Bedrock => {
// Bedrock provider via rig-bedrock - same pattern as OpenAI/Anthropic
let client = crate::bedrock::client::Client::from_env();
// Extended thinking for Claude models via Bedrock
// This enables Claude to show its reasoning process before responding.
// Requires vendored rig-bedrock that preserves Reasoning blocks with tool calls.
// Extended thinking budget - reduced to help with rate limits
// 8000 is enough for most tasks, increase to 16000 for complex analysis
let thinking_params = serde_json::json!({
"thinking": {
"type": "enabled",
"budget_tokens": 8000
}
});
let mut builder = client
.agent(&session.model)
.preamble(&preamble)
.max_tokens(64000) // Max output tokens for Claude Sonnet on Bedrock
.tool(AnalyzeTool::new(project_path_buf.clone()))
.tool(SecurityScanTool::new(project_path_buf.clone()))
.tool(VulnerabilitiesTool::new(project_path_buf.clone()))
.tool(HadolintTool::new(project_path_buf.clone()))
.tool(DclintTool::new(project_path_buf.clone()))
.tool(KubelintTool::new(project_path_buf.clone()))
.tool(K8sOptimizeTool::new(project_path_buf.clone()))
.tool(K8sCostsTool::new(project_path_buf.clone()))
.tool(K8sDriftTool::new(project_path_buf.clone()))
.tool(HelmlintTool::new(project_path_buf.clone()))
.tool(TerraformFmtTool::new(project_path_buf.clone()))
.tool(TerraformValidateTool::new(project_path_buf.clone()))
.tool(TerraformInstallTool::new())
.tool(ReadFileTool::new(project_path_buf.clone()))
.tool(ListDirectoryTool::new(project_path_buf.clone()))
.tool(WebFetchTool::new())
// Prometheus discovery and connection tools for live K8s analysis
.tool(PrometheusDiscoverTool::new())
.tool(PrometheusConnectTool::new(bg_manager.clone()))
// RAG retrieval tools for compressed tool outputs
.tool(RetrieveOutputTool::new())
.tool(ListOutputsTool::new())
// Platform tools for project management
.tool(ListOrganizationsTool::new())
.tool(ListProjectsTool::new())
.tool(SelectProjectTool::new())
.tool(CurrentContextTool::new())
.tool(OpenProviderSettingsTool::new())
.tool(CheckProviderConnectionTool::new())
.tool(ListDeploymentCapabilitiesTool::new())
.tool(ListHetznerAvailabilityTool::new())
// Deployment tools for service management
.tool(CreateDeploymentConfigTool::new())
.tool(DeployServiceTool::with_context(project_path_buf.clone(), ExecutionContext::InteractiveCli))
.tool(ListDeploymentConfigsTool::new())
.tool(TriggerDeploymentTool::new())
.tool(GetDeploymentStatusTool::new())
.tool(ListDeploymentsTool::new())
.tool(GetServiceLogsTool::new())
.tool(SetDeploymentSecretsTool::with_context(ExecutionContext::InteractiveCli));
// Add tools based on mode
if is_planning {
// Plan mode: read-only shell + plan creation tools
builder = builder
.tool(ShellTool::new(project_path_buf.clone()).with_read_only(true))
.tool(PlanCreateTool::new(project_path_buf.clone()))
.tool(PlanListTool::new(project_path_buf.clone()));
} else if is_generation {
// Standard mode + generation query: all tools including file writes and plan execution
let (mut write_file_tool, mut write_files_tool) =
if let Some(ref client) = ide_client {
(
WriteFileTool::new(project_path_buf.clone())
.with_ide_client(client.clone()),
WriteFilesTool::new(project_path_buf.clone())
.with_ide_client(client.clone()),
)
} else {
(
WriteFileTool::new(project_path_buf.clone()),
WriteFilesTool::new(project_path_buf.clone()),
)
};
// Disable confirmations if auto-accept mode is enabled (from plan menu)
if auto_accept_writes {
write_file_tool = write_file_tool.without_confirmation();
write_files_tool = write_files_tool.without_confirmation();
}
builder = builder
.tool(write_file_tool)
.tool(write_files_tool)
.tool(ShellTool::new(project_path_buf.clone()))
.tool(PlanListTool::new(project_path_buf.clone()))
.tool(PlanNextTool::new(project_path_buf.clone()))
.tool(PlanUpdateTool::new(project_path_buf.clone()));
}
// Add thinking params for extended reasoning
builder = builder.additional_params(thinking_params);
let agent = builder.build();
// Use same multi-turn pattern as OpenAI/Anthropic
agent
.prompt(¤t_input)
.with_history(&mut raw_chat_history)
.with_hook(hook.clone())
.multi_turn(50)
.await
}
}.map_err(|e| e.to_string())
} => result
};
// Stop the progress indicator before handling the response
progress.stop().await;
// End AG-UI thinking event
if let Some(ref bridge) = event_bridge {
bridge.end_thinking().await;
}
// Suppress unused variable warnings
let _ = (&progress_state, user_interrupted);
match response {
Ok(text) => {
// Emit AG-UI text message event (for connected frontends)
if let Some(ref bridge) = event_bridge {
bridge.emit_message(&text).await;
}
// Show final response
println!();
ResponseFormatter::print_response(&text);
// Track token usage - use actual from hook if available, else estimate
let hook_usage = hook.get_usage().await;
if hook_usage.has_data() {
// Use actual token counts from API response
session
.token_usage
.add_actual(hook_usage.input_tokens, hook_usage.output_tokens);
} else {
// Fall back to estimation when API doesn't provide usage
let prompt_tokens = TokenUsage::estimate_tokens(&input);
let completion_tokens = TokenUsage::estimate_tokens(&text);
session
.token_usage
.add_estimated(prompt_tokens, completion_tokens);
}
// Reset hook usage for next request batch
hook.reset_usage().await;
// Show context indicator like Forge: [model/~tokens]
let model_short = session
.model
.split('/')
.next_back()
.unwrap_or(&session.model)
.split(':')
.next()
.unwrap_or(&session.model);
println!();
println!(
" {}[{}/{}]{}",
ui::colors::ansi::DIM,
model_short,
session.token_usage.format_compact(),
ui::colors::ansi::RESET
);
// Emit AG-UI state update with new token counts
if let Some(ref bridge) = event_bridge {
let state = build_agent_state(&session, &conversation_history);
if let Ok(state_json) = serde_json::to_value(&state) {
bridge.emit_state_snapshot(state_json).await;
}
}
// Extract tool calls from the hook state for history tracking
let tool_calls = extract_tool_calls_from_hook(&hook).await;
let batch_tool_count = tool_calls.len();
total_tool_calls += batch_tool_count;
// Show tool call summary if significant
if batch_tool_count > 10 {
println!(
"{}",
format!(
" ✓ Completed with {} tool calls ({} total this session)",
batch_tool_count, total_tool_calls
)
.dimmed()
);
}
// Add to conversation history with tool call records
conversation_history.add_turn(input.clone(), text.clone(), tool_calls.clone());
// Remember whether this turn had generation tools active so short follow-up
// messages ("sure", "go ahead", etc.) don't lose write/shell access.
session.last_was_generation = is_generation;
// Check if this heavy turn requires immediate compaction
// This helps prevent context overflow in subsequent requests
if conversation_history.needs_compaction() {
println!("{}", " 📦 Compacting conversation history...".dimmed());
if let Some(summary) = conversation_history.compact() {
println!(
"{}",
format!(" ✓ Compressed {} turns", summary.matches("Turn").count())
.dimmed()
);
}
}
// Simplify history for OpenAI Responses API reasoning models
// Keep only User text and Assistant text - strip reasoning, tool calls, tool results
// This prevents pairing errors like "rs_... without its required following item"
// and "fc_... without its required reasoning item"
if session.provider == ProviderType::OpenAI {
simplify_history_for_openai_reasoning(&mut raw_chat_history);
}
// Also update legacy session history for compatibility
session.history.push(("user".to_string(), input.clone()));
session
.history
.push(("assistant".to_string(), text.clone()));
// Record to persistent session storage (includes full history snapshot)
session_recorder.record_user_message(&input);
session_recorder.record_assistant_message(&text, Some(&tool_calls));
if let Err(e) = session_recorder.save_with_history(&conversation_history) {
eprintln!(
"{}",
format!(" Warning: Failed to save session: {}", e).dimmed()
);
}
// Check if plan_create was called - show interactive menu
if let Some(plan_info) = find_plan_create_call(&tool_calls) {
println!(); // Space before menu
// Show the plan action menu (don't switch modes yet - let user choose)
match ui::show_plan_action_menu(&plan_info.0, plan_info.1) {
ui::PlanActionResult::ExecuteAutoAccept => {
// Now switch to standard mode for execution
if session.plan_mode.is_planning() {
session.plan_mode = session.plan_mode.toggle();
}
auto_accept_writes = true;
pending_input = Some(format!(
"Execute the plan at '{}'. Use plan_next to get tasks and execute them in order. Auto-accept all file writes.",
plan_info.0
));
succeeded = true;
}
ui::PlanActionResult::ExecuteWithReview => {
// Now switch to standard mode for execution
if session.plan_mode.is_planning() {
session.plan_mode = session.plan_mode.toggle();
}
pending_input = Some(format!(
"Execute the plan at '{}'. Use plan_next to get tasks and execute them in order.",
plan_info.0
));
succeeded = true;
}
ui::PlanActionResult::ChangePlan(feedback) => {
// Stay in plan mode for modifications
pending_input = Some(format!(
"Please modify the plan at '{}'. User feedback: {}",
plan_info.0, feedback
));
succeeded = true;
}
ui::PlanActionResult::Cancel => {
// Just complete normally, don't execute
succeeded = true;
}
}
} else {
succeeded = true;
}
}
Err(e) => {
let err_str = e.to_string();
println!();
// Check if this was a user-initiated cancellation (Ctrl+C)
if err_str.contains("cancelled") || err_str.contains("Cancelled") {
// Extract any completed work before cancellation
let completed_tools = extract_tool_calls_from_hook(&hook).await;
let tool_count = completed_tools.len();
eprintln!("{}", "⚠ Generation interrupted.".yellow());
if tool_count > 0 {
eprintln!(
"{}",
format!(" {} tool calls completed before interrupt.", tool_count)
.dimmed()
);
// Add partial progress to history
conversation_history.add_turn(
current_input.clone(),
format!("[Interrupted after {} tool calls]", tool_count),
completed_tools,
);
}
eprintln!("{}", " Type your next message to continue.".dimmed());
// Don't retry, don't mark as succeeded - just break to return to prompt
break;
}
// Check if this is a max depth error - handle as checkpoint
if err_str.contains("MaxDepth")
|| err_str.contains("max_depth")
|| err_str.contains("reached limit")
{
// Extract what was done before hitting the limit
let completed_tools = extract_tool_calls_from_hook(&hook).await;
let agent_thinking = extract_agent_messages_from_hook(&hook).await;
let batch_tool_count = completed_tools.len();
total_tool_calls += batch_tool_count;
eprintln!("{}", format!(
"⚠ Reached {} tool calls this batch ({} total). Maximum allowed: {}",
batch_tool_count, total_tool_calls, MAX_TOOL_CALLS
).yellow());
// Check if we've hit the absolute maximum
if total_tool_calls >= MAX_TOOL_CALLS {
eprintln!(
"{}",
format!("Maximum tool call limit ({}) reached.", MAX_TOOL_CALLS)
.red()
);
eprintln!(
"{}",
"The task is too complex. Try breaking it into smaller parts."
.dimmed()
);
break;
}
// Ask user if they want to continue (unless auto-continue is enabled)
let should_continue = if auto_continue_tools {
eprintln!(
"{}",
" Auto-continuing (you selected 'always')...".dimmed()
);
true
} else {
eprintln!(
"{}",
"Excessive tool calls used. Want to continue?".yellow()
);
eprintln!(
"{}",
" [y] Yes, continue [n] No, stop [a] Always continue".dimmed()
);
print!(" > ");
let _ = std::io::Write::flush(&mut std::io::stdout());
// Read user input
let mut response = String::new();
match std::io::stdin().read_line(&mut response) {
Ok(_) => {
let resp = response.trim().to_lowercase();
if resp == "a" || resp == "always" {
auto_continue_tools = true;
true
} else {
resp == "y" || resp == "yes" || resp.is_empty()
}
}
Err(_) => false,
}
};
if !should_continue {
eprintln!(
"{}",
"Stopped by user. Type 'continue' to resume later.".dimmed()
);
// Add partial progress to history
if !completed_tools.is_empty() {
conversation_history.add_turn(
current_input.clone(),
format!(
"[Stopped at checkpoint - {} tools completed]",
batch_tool_count
),
vec![],
);
}
break;
}
// Continue from checkpoint
eprintln!(
"{}",
format!(
" → Continuing... {} remaining tool calls available",
MAX_TOOL_CALLS - total_tool_calls
)
.dimmed()
);
// Add partial progress to history (without duplicating tool calls)
conversation_history.add_turn(
current_input.clone(),
format!(
"[Checkpoint - {} tools completed, continuing...]",
batch_tool_count
),
vec![],
);
// Build continuation prompt
current_input =
build_continuation_prompt(&input, &completed_tools, &agent_thinking);
// Brief delay before continuation
tokio::time::sleep(tokio::time::Duration::from_millis(500)).await;
continue; // Continue the loop without incrementing retry_attempt
} else if err_str.contains("rate")
|| err_str.contains("Rate")
|| err_str.contains("429")
|| err_str.contains("Too many tokens")
|| err_str.contains("please wait")
|| err_str.contains("throttl")
|| err_str.contains("Throttl")
{
eprintln!("{}", "⚠ Rate limited by API provider.".yellow());
// Wait before retry for rate limits (longer wait for "too many tokens")
retry_attempt += 1;
let wait_secs = if err_str.contains("Too many tokens") {
30
} else {
5
};
eprintln!(
"{}",
format!(
" Waiting {} seconds before retry ({}/{})...",
wait_secs, retry_attempt, MAX_RETRIES
)
.dimmed()
);
tokio::time::sleep(tokio::time::Duration::from_secs(wait_secs)).await;
} else if is_input_too_long_error(&err_str) {
// Context too large - truncate raw_chat_history directly
// NOTE: We truncate raw_chat_history (actual messages) not conversation_history
// because conversation_history may be empty/stale during errors
eprintln!(
"{}",
"⚠ Context too large for model. Truncating history...".yellow()
);
let old_token_count = estimate_raw_history_tokens(&raw_chat_history);
let old_msg_count = raw_chat_history.len();
// Strategy 1: Keep only the last N messages (user/assistant pairs)
// More aggressive truncation on each retry: 10 → 6 → 4 messages
let keep_count = match retry_attempt {
0 => 10,
1 => 6,
_ => 4,
};
if raw_chat_history.len() > keep_count {
// Drain older messages, keep the most recent ones
let drain_count = raw_chat_history.len() - keep_count;
raw_chat_history.drain(0..drain_count);
// Ensure history starts with User message for OpenAI Responses API compatibility
ensure_history_starts_with_user(&mut raw_chat_history);
}
// Strategy 2: Compact large tool outputs to temp files + summaries
// This preserves data (agent can read file if needed) while reducing context
let max_output_chars = match retry_attempt {
0 => 50_000, // 50KB on first try
1 => 20_000, // 20KB on second
_ => 5_000, // 5KB on third (aggressive)
};
compact_large_tool_outputs(&mut raw_chat_history, max_output_chars);
let new_token_count = estimate_raw_history_tokens(&raw_chat_history);
eprintln!("{}", format!(
" ✓ Truncated: {} messages (~{} tokens) → {} messages (~{} tokens)",
old_msg_count, old_token_count, raw_chat_history.len(), new_token_count
).green());
// Preserve compacted summary while clearing turns to stay in sync
conversation_history.clear_turns_preserve_context();
// Retry with truncated context
retry_attempt += 1;
if retry_attempt < MAX_RETRIES {
eprintln!(
"{}",
format!(
" → Retrying with truncated context ({}/{})...",
retry_attempt, MAX_RETRIES
)
.dimmed()
);
tokio::time::sleep(tokio::time::Duration::from_millis(500)).await;
} else {
eprintln!(
"{}",
"Context still too large after truncation. Try /clear to reset."
.red()
);
break;
}
} else if is_truncation_error(&err_str) {
// Truncation error - try intelligent continuation
let completed_tools = extract_tool_calls_from_hook(&hook).await;
let agent_thinking = extract_agent_messages_from_hook(&hook).await;
// Count actually completed tools (not in-progress)
let completed_count = completed_tools
.iter()
.filter(|t| !t.result_summary.contains("IN PROGRESS"))
.count();
let in_progress_count = completed_tools.len() - completed_count;
if !completed_tools.is_empty() && continuation_count < MAX_CONTINUATIONS {
// We have partial progress - continue from where we left off
continuation_count += 1;
let status_msg = if in_progress_count > 0 {
format!(
"⚠ Response truncated. {} completed, {} in-progress. Auto-continuing ({}/{})...",
completed_count,
in_progress_count,
continuation_count,
MAX_CONTINUATIONS
)
} else {
format!(
"⚠ Response truncated. {} tool calls completed. Auto-continuing ({}/{})...",
completed_count, continuation_count, MAX_CONTINUATIONS
)
};
eprintln!("{}", status_msg.yellow());
// Add partial progress to conversation history
// NOTE: We intentionally pass empty tool_calls here because the
// continuation prompt already contains the detailed file list.
// Including them in history would duplicate the context and waste tokens.
conversation_history.add_turn(
current_input.clone(),
format!("[Partial response - {} tools completed, {} in-progress before truncation. See continuation prompt for details.]",
completed_count, in_progress_count),
vec![] // Don't duplicate - continuation prompt has the details
);
// Check if we need compaction after adding this heavy turn
// This is important for long multi-turn sessions with many tool calls
if conversation_history.needs_compaction() {
eprintln!(
"{}",
" 📦 Compacting history before continuation...".dimmed()
);
if let Some(summary) = conversation_history.compact() {
eprintln!(
"{}",
format!(
" ✓ Compressed {} turns",
summary.matches("Turn").count()
)
.dimmed()
);
}
}
// Build continuation prompt with context
current_input = build_continuation_prompt(
&input,
&completed_tools,
&agent_thinking,
);
// Log continuation details for debugging
eprintln!("{}", format!(
" → Continuing with {} files read, {} written, {} other actions tracked",
completed_tools.iter().filter(|t| t.tool_name == "read_file").count(),
completed_tools.iter().filter(|t| t.tool_name == "write_file" || t.tool_name == "write_files").count(),
completed_tools.iter().filter(|t| t.tool_name != "read_file" && t.tool_name != "write_file" && t.tool_name != "write_files" && t.tool_name != "list_directory").count()
).dimmed());
// Brief delay before continuation
tokio::time::sleep(tokio::time::Duration::from_millis(500)).await;
// Don't increment retry_attempt - this is progress via continuation
} else if retry_attempt < MAX_RETRIES {
// No tool calls completed - simple retry
retry_attempt += 1;
eprintln!(
"{}",
format!(
"⚠ Response error (attempt {}/{}). Retrying...",
retry_attempt, MAX_RETRIES
)
.yellow()
);
tokio::time::sleep(tokio::time::Duration::from_millis(500)).await;
} else {
// Max retries/continuations reached
eprintln!("{}", format!("Error: {}", e).red());
if continuation_count >= MAX_CONTINUATIONS {
eprintln!("{}", format!("Max continuations ({}) reached. The task is too complex for one request.", MAX_CONTINUATIONS).dimmed());
} else {
eprintln!(
"{}",
"Max retries reached. The response may be too complex."
.dimmed()
);
}
eprintln!(
"{}",
"Try breaking your request into smaller parts.".dimmed()
);
exit_error = Some(e.to_string());
break;
}
} else if err_str.contains("timeout") || err_str.contains("Timeout") {
// Timeout - simple retry
retry_attempt += 1;
if retry_attempt < MAX_RETRIES {
eprintln!(
"{}",
format!(
"⚠ Request timed out (attempt {}/{}). Retrying...",
retry_attempt, MAX_RETRIES
)
.yellow()
);
tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;
} else {
eprintln!("{}", "Request timed out. Please try again.".red());
exit_error = Some("Request timed out".to_string());
break;
}
} else {
// Unknown error - show details and break
eprintln!("{}", format!("Error: {}", e).red());
if continuation_count > 0 {
eprintln!(
"{}",
format!(
" (occurred during continuation attempt {})",
continuation_count
)
.dimmed()
);
}
eprintln!("{}", "Error details for debugging:".dimmed());
eprintln!(
"{}",
format!(" - retry_attempt: {}/{}", retry_attempt, MAX_RETRIES)
.dimmed()
);
eprintln!(
"{}",
format!(
" - continuation_count: {}/{}",
continuation_count, MAX_CONTINUATIONS
)
.dimmed()
);
exit_error = Some(e.to_string());
break;
}
}
}
}
// End AG-UI step event for this turn
if let Some(ref bridge) = event_bridge {
bridge.end_step().await;
}
println!();
}
// Emit AG-UI run completion event for connected frontends
if let Some(ref bridge) = event_bridge {
if let Some(error_msg) = exit_error {
bridge.finish_run_with_error(&error_msg).await;
} else {
bridge.finish_run().await;
}
}
// Clean up terminal layout before exiting (disabled - layout not initialized)
// if let Err(e) = terminal_layout.cleanup() {
// eprintln!(
// "{}",
// format!("Warning: Terminal cleanup failed: {}", e).dimmed()
// );
// }
Ok(())
}
// NOTE: wait_for_interrupt function removed - ESC interrupt feature disabled
// due to terminal corruption issues with spawn_blocking raw mode handling.
// TODO: Re-implement using tool hook callbacks for cleaner interruption.
/// Extract tool call records from the hook state for history tracking
async fn extract_tool_calls_from_hook(hook: &ToolDisplayHook) -> Vec<ToolCallRecord> {
let state = hook.state();
let guard = state.lock().await;
guard
.tool_calls
.iter()
.enumerate()
.map(|(i, tc)| {
let result = if tc.is_running {
// Tool was in progress when error occurred
"[IN PROGRESS - may need to be re-run]".to_string()
} else if let Some(output) = &tc.output {
truncate_string(output, 200)
} else {
"completed".to_string()
};
ToolCallRecord {
tool_name: tc.name.clone(),
args_summary: truncate_string(&tc.args, 100),
result_summary: result,
// Generate a unique tool ID for proper message pairing
tool_id: Some(format!("tool_{}_{}", tc.name, i)),
// Mark read-only tools as droppable (their results can be re-fetched)
droppable: matches!(
tc.name.as_str(),
"read_file" | "list_directory" | "analyze_project"
),
}
})
.collect()
}
/// Extract any agent thinking/messages from the hook for context
async fn extract_agent_messages_from_hook(hook: &ToolDisplayHook) -> Vec<String> {
let state = hook.state();
let guard = state.lock().await;
guard.agent_messages.clone()
}
/// Helper to truncate strings for summaries
fn truncate_string(s: &str, max_len: usize) -> String {
if s.len() <= max_len {
s.to_string()
} else {
format!("{}...", &s[..max_len.saturating_sub(3)])
}
}
/// Compact large tool outputs by saving them to temp files and replacing with summaries.
/// This preserves all data (agent can read the file) while reducing context size.
fn compact_large_tool_outputs(messages: &mut [rig::completion::Message], max_chars: usize) {
use rig::completion::message::{Text, ToolResultContent, UserContent};
use std::fs;
// Create temp directory for compacted outputs
let temp_dir = std::env::temp_dir().join("syncable-agent-outputs");
let _ = fs::create_dir_all(&temp_dir);
for msg in messages.iter_mut() {
if let rig::completion::Message::User { content } = msg {
for item in content.iter_mut() {
if let UserContent::ToolResult(tr) = item {
for trc in tr.content.iter_mut() {
if let ToolResultContent::Text(text) = trc
&& text.text.len() > max_chars
{
// Save full output to temp file
let file_id = format!(
"{}_{}.txt",
tr.id,
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_millis()
);
let file_path = temp_dir.join(&file_id);
if let Ok(()) = fs::write(&file_path, &text.text) {
// Create a smart summary
let summary = create_output_summary(
&text.text,
&file_path.display().to_string(),
max_chars / 2, // Use half max for summary
);
// Replace with summary
*trc = ToolResultContent::Text(Text { text: summary });
}
}
}
}
}
}
}
}
/// Create a smart summary of a large output using incremental chunk processing.
/// Processes output in logical sections, summarizes each, then combines into actionable summary.
fn create_output_summary(full_output: &str, file_path: &str, max_summary_len: usize) -> String {
let total_lines = full_output.lines().count();
let total_chars = full_output.len();
let summary_content =
if full_output.trim_start().starts_with('{') || full_output.trim_start().starts_with('[') {
// JSON output - extract structured summary
summarize_json_incrementally(full_output, max_summary_len)
} else {
// Text output - chunk and summarize
summarize_text_incrementally(full_output, max_summary_len)
};
format!(
"[COMPACTED OUTPUT]\n\
Full data: {}\n\
Size: {} chars, {} lines\n\
\n\
{}\n\
\n\
[Read file with offset/limit for specific sections if needed]",
file_path, total_chars, total_lines, summary_content
)
}
/// Incrementally summarize JSON output, extracting key fields and prioritizing important items.
fn summarize_json_incrementally(json_str: &str, max_len: usize) -> String {
let Ok(json) = serde_json::from_str::<serde_json::Value>(json_str) else {
return "Failed to parse JSON".to_string();
};
let mut parts: Vec<String> = Vec::new();
let mut current_len = 0;
match &json {
serde_json::Value::Object(obj) => {
// Priority 1: Summary/stats fields
for key in ["summary", "stats", "metadata", "status"] {
if let Some(v) = obj.get(key) {
let s = format!("{}:\n{}", key, indent_json(v, 2, 500));
if current_len + s.len() < max_len {
parts.push(s.clone());
current_len += s.len();
}
}
}
// Priority 2: Error/critical items (summarize each)
for key in [
"errors",
"critical",
"failures",
"issues",
"findings",
"recommendations",
] {
if let Some(serde_json::Value::Array(arr)) = obj.get(key) {
if arr.is_empty() {
continue;
}
parts.push(format!("\n{} ({} items):", key, arr.len()));
// Group by severity/type if present
let mut by_severity: std::collections::HashMap<
String,
Vec<&serde_json::Value>,
> = std::collections::HashMap::new();
for item in arr {
let severity = item
.get("severity")
.or_else(|| item.get("level"))
.or_else(|| item.get("type"))
.and_then(|v| v.as_str())
.unwrap_or("other")
.to_string();
by_severity.entry(severity).or_default().push(item);
}
// Show critical/high first, summarize others
for sev in [
"critical", "high", "error", "warning", "medium", "low", "info", "other",
] {
if let Some(items) = by_severity.get(sev) {
let show_count = match sev {
"critical" | "high" | "error" => 5.min(items.len()),
"warning" | "medium" => 3.min(items.len()),
_ => 2.min(items.len()),
};
if !items.is_empty() {
let s =
format!(" [{}] {} items:", sev.to_uppercase(), items.len());
if current_len + s.len() < max_len {
parts.push(s.clone());
current_len += s.len();
for item in items.iter().take(show_count) {
let item_summary = summarize_single_item(item);
if current_len + item_summary.len() < max_len {
parts.push(format!(" • {}", item_summary));
current_len += item_summary.len();
}
}
if items.len() > show_count {
parts.push(format!(
" ... and {} more",
items.len() - show_count
));
}
}
}
}
}
}
}
// Priority 3: Show remaining top-level keys
let shown_keys: std::collections::HashSet<&str> = [
"summary",
"stats",
"metadata",
"status",
"errors",
"critical",
"failures",
"issues",
"findings",
"recommendations",
]
.iter()
.cloned()
.collect();
let other_keys: Vec<_> = obj
.keys()
.filter(|k| !shown_keys.contains(k.as_str()))
.collect();
if !other_keys.is_empty() && current_len < max_len - 200 {
parts.push(format!("\nOther fields: {:?}", other_keys));
}
}
serde_json::Value::Array(arr) => {
parts.push(format!("Array with {} items", arr.len()));
// Try to group by type/severity
for (i, item) in arr.iter().take(10).enumerate() {
let s = format!("[{}] {}", i, summarize_single_item(item));
if current_len + s.len() < max_len {
parts.push(s.clone());
current_len += s.len();
}
}
if arr.len() > 10 {
parts.push(format!("... and {} more items", arr.len() - 10));
}
}
_ => {
parts.push(truncate_json_value(&json, max_len));
}
}
parts.join("\n")
}
/// Summarize a single JSON item (issue, error, etc.) into a one-liner.
fn summarize_single_item(item: &serde_json::Value) -> String {
let mut parts: Vec<String> = Vec::new();
// Extract common fields
for key in [
"message",
"description",
"title",
"name",
"file",
"path",
"code",
"rule",
] {
if let Some(v) = item.get(key)
&& let Some(s) = v.as_str()
{
parts.push(truncate_string(s, 80));
break; // Only take first descriptive field
}
}
// Add location if present
if let Some(file) = item
.get("file")
.or_else(|| item.get("path"))
.and_then(|v| v.as_str())
{
if let Some(line) = item.get("line").and_then(|v| v.as_u64()) {
parts.push(format!("at {}:{}", file, line));
} else {
parts.push(format!("in {}", truncate_string(file, 40)));
}
}
if parts.is_empty() {
truncate_json_value(item, 100)
} else {
parts.join(" ")
}
}
/// Indent JSON for display.
fn indent_json(v: &serde_json::Value, indent: usize, max_len: usize) -> String {
let s = serde_json::to_string_pretty(v).unwrap_or_else(|_| v.to_string());
let prefix = " ".repeat(indent);
let indented: String = s
.lines()
.map(|l| format!("{}{}", prefix, l))
.collect::<Vec<_>>()
.join("\n");
if indented.len() > max_len {
format!("{}...", &indented[..max_len.saturating_sub(3)])
} else {
indented
}
}
/// Incrementally summarize text output by processing in chunks.
fn summarize_text_incrementally(text: &str, max_len: usize) -> String {
let lines: Vec<&str> = text.lines().collect();
let mut parts: Vec<String> = Vec::new();
let mut current_len = 0;
// Look for section headers or key patterns
let mut sections: Vec<(usize, &str)> = Vec::new();
for (i, line) in lines.iter().enumerate() {
// Detect headers (lines that look like titles)
if line.starts_with('#')
|| line.starts_with("==")
|| line.starts_with("--")
|| (line.ends_with(':') && line.len() < 50)
|| line.chars().all(|c| c.is_uppercase() || c.is_whitespace())
{
sections.push((i, line));
}
}
if !sections.is_empty() {
// Summarize by sections
parts.push(format!("Found {} sections:", sections.len()));
for (i, (line_num, header)) in sections.iter().enumerate() {
let next_section = sections.get(i + 1).map(|(n, _)| *n).unwrap_or(lines.len());
let section_lines = next_section - line_num;
let s = format!(
" [L{}] {} ({} lines)",
line_num + 1,
header.trim(),
section_lines
);
if current_len + s.len() < max_len / 2 {
parts.push(s.clone());
current_len += s.len();
}
}
parts.push("".to_string());
}
// Show first chunk
let preview_lines = 15.min(lines.len());
parts.push("Content preview:".to_string());
for line in lines.iter().take(preview_lines) {
let s = format!(" {}", truncate_string(line, 120));
if current_len + s.len() < max_len * 3 / 4 {
parts.push(s.clone());
current_len += s.len();
}
}
if lines.len() > preview_lines {
parts.push(format!(
" ... ({} more lines)",
lines.len() - preview_lines
));
}
// Show last few lines if space permits
if lines.len() > preview_lines * 2 && current_len < max_len - 500 {
parts.push("\nEnd of output:".to_string());
for line in lines.iter().skip(lines.len() - 5) {
let s = format!(" {}", truncate_string(line, 120));
if current_len + s.len() < max_len {
parts.push(s.clone());
current_len += s.len();
}
}
}
parts.join("\n")
}
/// Truncate a JSON value for display
fn truncate_json_value(v: &serde_json::Value, max_len: usize) -> String {
let s = v.to_string();
if s.len() <= max_len {
s
} else {
format!("{}...", &s[..max_len.saturating_sub(3)])
}
}
/// Simplify history for OpenAI Responses API compatibility with reasoning models.
///
/// OpenAI's Responses API has strict pairing requirements:
/// - Reasoning items must be followed by their output (text or function_call)
/// - Function_call items must be preceded by their reasoning item
///
/// When passing history across user turns, these pairings get broken, causing errors like:
/// - "Item 'rs_...' of type 'reasoning' was provided without its required following item"
/// - "Item 'fc_...' of type 'function_call' was provided without its required 'reasoning' item"
///
/// Solution: Keep only User messages and final Assistant Text responses.
/// This preserves conversation context without the complex internal tool/reasoning structure.
fn simplify_history_for_openai_reasoning(history: &mut Vec<rig::completion::Message>) {
use rig::completion::message::{AssistantContent, UserContent};
use rig::one_or_many::OneOrMany;
// Filter to keep only User text messages and Assistant text messages
let simplified: Vec<rig::completion::Message> = history
.iter()
.filter_map(|msg| match msg {
// Keep User messages, but only text content (not tool results)
rig::completion::Message::User { content } => {
let text_only: Vec<UserContent> = content
.iter()
.filter(|c| matches!(c, UserContent::Text(_)))
.cloned()
.collect();
if text_only.is_empty() {
None
} else {
let mut iter = text_only.into_iter();
let first = iter.next().unwrap();
let rest: Vec<_> = iter.collect();
let new_content = if rest.is_empty() {
OneOrMany::one(first)
} else {
OneOrMany::many(std::iter::once(first).chain(rest)).unwrap()
};
Some(rig::completion::Message::User {
content: new_content,
})
}
}
// Keep Assistant messages, but only text content (not reasoning, tool calls)
rig::completion::Message::Assistant { content, id } => {
let text_only: Vec<AssistantContent> = content
.iter()
.filter(|c| matches!(c, AssistantContent::Text(_)))
.cloned()
.collect();
if text_only.is_empty() {
None
} else {
let mut iter = text_only.into_iter();
let first = iter.next().unwrap();
let rest: Vec<_> = iter.collect();
let new_content = if rest.is_empty() {
OneOrMany::one(first)
} else {
OneOrMany::many(std::iter::once(first).chain(rest)).unwrap()
};
Some(rig::completion::Message::Assistant {
content: new_content,
id: id.clone(),
})
}
}
})
.collect();
*history = simplified;
}
/// Ensure history starts with a User message for OpenAI Responses API compatibility.
///
/// OpenAI's Responses API requires that reasoning items are properly structured within
/// a conversation. When history truncation leaves an Assistant message (containing
/// Reasoning blocks) at the start, OpenAI rejects it with:
/// "Item 'rs_...' of type 'reasoning' was provided without its required following item."
///
/// This function inserts a synthetic User message at the beginning if history starts
/// with an Assistant message, preserving the context while maintaining valid structure.
fn ensure_history_starts_with_user(history: &mut Vec<rig::completion::Message>) {
if !history.is_empty()
&& matches!(
history.first(),
Some(rig::completion::Message::Assistant { .. })
)
{
// Insert synthetic User message at the beginning to maintain valid conversation structure
history.insert(
0,
rig::completion::Message::User {
content: rig::one_or_many::OneOrMany::one(
rig::completion::message::UserContent::text("(Conversation continued)"),
),
},
);
}
}
/// Estimate token count from raw rig Messages
/// This is used for context length management to prevent "input too long" errors.
/// Estimates ~4 characters per token.
fn estimate_raw_history_tokens(messages: &[rig::completion::Message]) -> usize {
use rig::completion::message::{AssistantContent, ToolResultContent, UserContent};
messages
.iter()
.map(|msg| -> usize {
match msg {
rig::completion::Message::User { content } => {
content
.iter()
.map(|c| -> usize {
match c {
UserContent::Text(t) => t.text.len() / 4,
UserContent::ToolResult(tr) => {
// Tool results can be HUGE - properly estimate them
tr.content
.iter()
.map(|trc| match trc {
ToolResultContent::Text(t) => t.text.len() / 4,
_ => 100,
})
.sum::<usize>()
}
_ => 100, // Estimate for images/documents
}
})
.sum::<usize>()
}
rig::completion::Message::Assistant { content, .. } => {
content
.iter()
.map(|c| -> usize {
match c {
AssistantContent::Text(t) => t.text.len() / 4,
AssistantContent::ToolCall(tc) => {
// arguments is serde_json::Value, convert to string for length estimate
let args_len = tc.function.arguments.to_string().len();
(tc.function.name.len() + args_len) / 4
}
_ => 100,
}
})
.sum::<usize>()
}
}
})
.sum()
}
/// Find a plan_create tool call in the list and extract plan info
/// Returns (plan_path, task_count) if found
fn find_plan_create_call(tool_calls: &[ToolCallRecord]) -> Option<(String, usize)> {
for tc in tool_calls {
if tc.tool_name == "plan_create" {
// Try to parse the result_summary as JSON to extract plan_path
// Note: result_summary may be truncated, so we have multiple fallbacks
let plan_path =
if let Ok(result) = serde_json::from_str::<serde_json::Value>(&tc.result_summary) {
result
.get("plan_path")
.and_then(|v| v.as_str())
.map(|s| s.to_string())
} else {
None
};
// If JSON parsing failed, find the most recently created plan file
// This is more reliable than trying to reconstruct the path from truncated args
let plan_path = plan_path.unwrap_or_else(|| {
find_most_recent_plan_file().unwrap_or_else(|| "plans/plan.md".to_string())
});
// Count tasks by reading the plan file directly
let task_count = count_tasks_in_plan_file(&plan_path).unwrap_or(0);
return Some((plan_path, task_count));
}
}
None
}
/// Find the most recently created plan file in the plans directory
fn find_most_recent_plan_file() -> Option<String> {
let plans_dir = std::env::current_dir().ok()?.join("plans");
if !plans_dir.exists() {
return None;
}
let mut newest: Option<(std::path::PathBuf, std::time::SystemTime)> = None;
for entry in std::fs::read_dir(&plans_dir).ok()?.flatten() {
let path = entry.path();
if path.extension().is_some_and(|e| e == "md")
&& let Ok(metadata) = entry.metadata()
&& let Ok(modified) = metadata.modified()
&& newest.as_ref().map(|(_, t)| modified > *t).unwrap_or(true)
{
newest = Some((path, modified));
}
}
newest.map(|(path, _)| {
// Return relative path
path.strip_prefix(std::env::current_dir().unwrap_or_default())
.map(|p| p.display().to_string())
.unwrap_or_else(|_| path.display().to_string())
})
}
/// Count tasks (checkbox items) in a plan file
fn count_tasks_in_plan_file(plan_path: &str) -> Option<usize> {
use regex::Regex;
// Try both relative and absolute paths
let path = std::path::Path::new(plan_path);
let content = if path.exists() {
std::fs::read_to_string(path).ok()?
} else {
// Try with current directory
std::fs::read_to_string(std::env::current_dir().ok()?.join(plan_path)).ok()?
};
// Count task checkboxes: - [ ], - [x], - [~], - [!]
let task_regex = Regex::new(r"^\s*-\s*\[[ x~!]\]").ok()?;
let count = content
.lines()
.filter(|line| task_regex.is_match(line))
.count();
Some(count)
}
/// Check if an error is a truncation/JSON parsing error that can be recovered via continuation
fn is_truncation_error(err_str: &str) -> bool {
err_str.contains("JsonError")
|| err_str.contains("EOF while parsing")
|| err_str.contains("JSON")
|| err_str.contains("unexpected end")
}
/// Check if error is "input too long" - context exceeds model limit
/// This happens when conversation history grows beyond what the model can handle.
/// Recovery: compact history and retry with reduced context.
fn is_input_too_long_error(err_str: &str) -> bool {
err_str.contains("too long")
|| err_str.contains("Too long")
|| err_str.contains("context length")
|| err_str.contains("maximum context")
|| err_str.contains("exceeds the model")
|| err_str.contains("Input is too long")
}
/// Build a continuation prompt that tells the AI what work was completed
/// and asks it to continue from where it left off
fn build_continuation_prompt(
original_task: &str,
completed_tools: &[ToolCallRecord],
agent_thinking: &[String],
) -> String {
use std::collections::HashSet;
// Group tools by type and extract unique files read
let mut files_read: HashSet<String> = HashSet::new();
let mut files_written: HashSet<String> = HashSet::new();
let mut dirs_listed: HashSet<String> = HashSet::new();
let mut other_tools: Vec<String> = Vec::new();
let mut in_progress: Vec<String> = Vec::new();
for tool in completed_tools {
let is_in_progress = tool.result_summary.contains("IN PROGRESS");
if is_in_progress {
in_progress.push(format!("{}({})", tool.tool_name, tool.args_summary));
continue;
}
match tool.tool_name.as_str() {
"read_file" => {
// Extract path from args
files_read.insert(tool.args_summary.clone());
}
"write_file" | "write_files" => {
files_written.insert(tool.args_summary.clone());
}
"list_directory" => {
dirs_listed.insert(tool.args_summary.clone());
}
_ => {
other_tools.push(format!(
"{}({})",
tool.tool_name,
truncate_string(&tool.args_summary, 40)
));
}
}
}
let mut prompt = format!(
"[CONTINUE] Your previous response was interrupted. DO NOT repeat completed work.\n\n\
Original task: {}\n",
truncate_string(original_task, 500)
);
// Show files already read - CRITICAL for preventing re-reads
if !files_read.is_empty() {
prompt.push_str("\n== FILES ALREADY READ (do NOT read again) ==\n");
for file in &files_read {
prompt.push_str(&format!(" - {}\n", file));
}
}
if !dirs_listed.is_empty() {
prompt.push_str("\n== DIRECTORIES ALREADY LISTED ==\n");
for dir in &dirs_listed {
prompt.push_str(&format!(" - {}\n", dir));
}
}
if !files_written.is_empty() {
prompt.push_str("\n== FILES ALREADY WRITTEN ==\n");
for file in &files_written {
prompt.push_str(&format!(" - {}\n", file));
}
}
if !other_tools.is_empty() {
prompt.push_str("\n== OTHER COMPLETED ACTIONS ==\n");
for tool in other_tools.iter().take(20) {
prompt.push_str(&format!(" - {}\n", tool));
}
if other_tools.len() > 20 {
prompt.push_str(&format!(" ... and {} more\n", other_tools.len() - 20));
}
}
if !in_progress.is_empty() {
prompt.push_str("\n== INTERRUPTED (may need re-run) ==\n");
for tool in &in_progress {
prompt.push_str(&format!(" ⚠ {}\n", tool));
}
}
// Include last thinking context if available
if let Some(last_thought) = agent_thinking.last() {
prompt.push_str(&format!(
"\n== YOUR LAST THOUGHTS ==\n\"{}\"\n",
truncate_string(last_thought, 300)
));
}
prompt.push_str("\n== INSTRUCTIONS ==\n");
prompt.push_str("IMPORTANT: Your previous response was too long and got cut off.\n");
prompt.push_str("1. Do NOT re-read files listed above - they are already in context.\n");
prompt.push_str("2. If writing a document, write it in SECTIONS - complete one section now, then continue.\n");
prompt.push_str("3. Keep your response SHORT and focused. Better to complete small chunks than fail on large ones.\n");
prompt.push_str("4. If the task involves writing a file, START WRITING NOW - don't explain what you'll do.\n");
prompt
}
/// Run a single query and return the response
/// Note: event_bridge is accepted for API consistency but not used in single-query mode
pub async fn run_query(
project_path: &Path,
query: &str,
provider: ProviderType,
model: Option<String>,
_event_bridge: Option<crate::server::EventBridge>,
) -> AgentResult<String> {
use tools::*;
let project_path_buf = project_path.to_path_buf();
// Background process manager for Prometheus port-forwards (single query context)
let bg_manager = Arc::new(BackgroundProcessManager::new());
// Select prompt based on query type (analysis vs generation)
// For single queries (non-interactive), always use standard mode
let preamble = get_system_prompt(project_path, Some(query), PlanMode::default());
let is_generation = prompts::is_generation_query(query);
match provider {
ProviderType::OpenAI => {
// Use Responses API (default) for reasoning model support
let client = openai::Client::from_env();
let model_name = model.as_deref().unwrap_or("gpt-5.2");
let mut builder = client
.agent(model_name)
.preamble(&preamble)
.max_tokens(4096)
.tool(AnalyzeTool::new(project_path_buf.clone()))
.tool(SecurityScanTool::new(project_path_buf.clone()))
.tool(VulnerabilitiesTool::new(project_path_buf.clone()))
.tool(HadolintTool::new(project_path_buf.clone()))
.tool(DclintTool::new(project_path_buf.clone()))
.tool(KubelintTool::new(project_path_buf.clone()))
.tool(K8sOptimizeTool::new(project_path_buf.clone()))
.tool(K8sCostsTool::new(project_path_buf.clone()))
.tool(K8sDriftTool::new(project_path_buf.clone()))
.tool(HelmlintTool::new(project_path_buf.clone()))
.tool(TerraformFmtTool::new(project_path_buf.clone()))
.tool(TerraformValidateTool::new(project_path_buf.clone()))
.tool(TerraformInstallTool::new())
.tool(ReadFileTool::new(project_path_buf.clone()))
.tool(ListDirectoryTool::new(project_path_buf.clone()))
.tool(WebFetchTool::new())
// Prometheus discovery and connection tools for live K8s analysis
.tool(PrometheusDiscoverTool::new())
.tool(PrometheusConnectTool::new(bg_manager.clone()))
// RAG retrieval tools for compressed tool outputs
.tool(RetrieveOutputTool::new())
.tool(ListOutputsTool::new())
// Platform tools for project management
.tool(ListOrganizationsTool::new())
.tool(ListProjectsTool::new())
.tool(SelectProjectTool::new())
.tool(CurrentContextTool::new())
.tool(OpenProviderSettingsTool::new())
.tool(CheckProviderConnectionTool::new())
.tool(ListDeploymentCapabilitiesTool::new())
.tool(ListHetznerAvailabilityTool::new())
// Deployment tools for service management
.tool(CreateDeploymentConfigTool::new())
.tool(DeployServiceTool::with_context(project_path_buf.clone(), ExecutionContext::InteractiveCli))
.tool(ListDeploymentConfigsTool::new())
.tool(TriggerDeploymentTool::new())
.tool(GetDeploymentStatusTool::new())
.tool(ListDeploymentsTool::new())
.tool(GetServiceLogsTool::new())
.tool(SetDeploymentSecretsTool::with_context(ExecutionContext::InteractiveCli));
// Add generation tools if this is a generation query
if is_generation {
builder = builder
.tool(WriteFileTool::new(project_path_buf.clone()))
.tool(WriteFilesTool::new(project_path_buf.clone()))
.tool(ShellTool::new(project_path_buf.clone()));
}
// Enable reasoning for OpenAI reasoning models
let model_lower = model_name.to_lowercase();
let is_reasoning_model = model_lower.starts_with("gpt-5")
|| model_lower.starts_with("gpt5")
|| model_lower.starts_with("o1")
|| model_lower.starts_with("o3")
|| model_lower.starts_with("o4");
let agent = if is_reasoning_model {
let reasoning_params = serde_json::json!({
"reasoning": {
"effort": "medium",
"summary": "detailed"
}
});
builder.additional_params(reasoning_params).build()
} else {
builder.build()
};
agent
.prompt(query)
.multi_turn(50)
.await
.map_err(|e| AgentError::ProviderError(e.to_string()))
}
ProviderType::Anthropic => {
let client = anthropic::Client::from_env();
let model_name = model.as_deref().unwrap_or("claude-sonnet-4-5-20250929");
// TODO: Extended thinking for Claude is disabled because rig doesn't properly
// handle thinking blocks in multi-turn conversations with tool use.
// See: forge/crates/forge_services/src/provider/bedrock/provider.rs for reference.
let mut builder = client
.agent(model_name)
.preamble(&preamble)
.max_tokens(4096)
.tool(AnalyzeTool::new(project_path_buf.clone()))
.tool(SecurityScanTool::new(project_path_buf.clone()))
.tool(VulnerabilitiesTool::new(project_path_buf.clone()))
.tool(HadolintTool::new(project_path_buf.clone()))
.tool(DclintTool::new(project_path_buf.clone()))
.tool(KubelintTool::new(project_path_buf.clone()))
.tool(K8sOptimizeTool::new(project_path_buf.clone()))
.tool(K8sCostsTool::new(project_path_buf.clone()))
.tool(K8sDriftTool::new(project_path_buf.clone()))
.tool(HelmlintTool::new(project_path_buf.clone()))
.tool(TerraformFmtTool::new(project_path_buf.clone()))
.tool(TerraformValidateTool::new(project_path_buf.clone()))
.tool(TerraformInstallTool::new())
.tool(ReadFileTool::new(project_path_buf.clone()))
.tool(ListDirectoryTool::new(project_path_buf.clone()))
.tool(WebFetchTool::new())
// Prometheus discovery and connection tools for live K8s analysis
.tool(PrometheusDiscoverTool::new())
.tool(PrometheusConnectTool::new(bg_manager.clone()))
// RAG retrieval tools for compressed tool outputs
.tool(RetrieveOutputTool::new())
.tool(ListOutputsTool::new())
// Platform tools for project management
.tool(ListOrganizationsTool::new())
.tool(ListProjectsTool::new())
.tool(SelectProjectTool::new())
.tool(CurrentContextTool::new())
.tool(OpenProviderSettingsTool::new())
.tool(CheckProviderConnectionTool::new())
.tool(ListDeploymentCapabilitiesTool::new())
.tool(ListHetznerAvailabilityTool::new())
// Deployment tools for service management
.tool(CreateDeploymentConfigTool::new())
.tool(DeployServiceTool::with_context(project_path_buf.clone(), ExecutionContext::InteractiveCli))
.tool(ListDeploymentConfigsTool::new())
.tool(TriggerDeploymentTool::new())
.tool(GetDeploymentStatusTool::new())
.tool(ListDeploymentsTool::new())
.tool(GetServiceLogsTool::new())
.tool(SetDeploymentSecretsTool::with_context(ExecutionContext::InteractiveCli));
// Add generation tools if this is a generation query
if is_generation {
builder = builder
.tool(WriteFileTool::new(project_path_buf.clone()))
.tool(WriteFilesTool::new(project_path_buf.clone()))
.tool(ShellTool::new(project_path_buf.clone()));
}
let agent = builder.build();
agent
.prompt(query)
.multi_turn(50)
.await
.map_err(|e| AgentError::ProviderError(e.to_string()))
}
ProviderType::Bedrock => {
// Bedrock provider via rig-bedrock - same pattern as Anthropic
let client = crate::bedrock::client::Client::from_env();
let model_name = model
.as_deref()
.unwrap_or("global.anthropic.claude-sonnet-4-5-20250929-v1:0");
// Extended thinking for Claude via Bedrock
let thinking_params = serde_json::json!({
"thinking": {
"type": "enabled",
"budget_tokens": 16000
}
});
let mut builder = client
.agent(model_name)
.preamble(&preamble)
.max_tokens(64000) // Max output tokens for Claude Sonnet on Bedrock
.tool(AnalyzeTool::new(project_path_buf.clone()))
.tool(SecurityScanTool::new(project_path_buf.clone()))
.tool(VulnerabilitiesTool::new(project_path_buf.clone()))
.tool(HadolintTool::new(project_path_buf.clone()))
.tool(DclintTool::new(project_path_buf.clone()))
.tool(KubelintTool::new(project_path_buf.clone()))
.tool(K8sOptimizeTool::new(project_path_buf.clone()))
.tool(K8sCostsTool::new(project_path_buf.clone()))
.tool(K8sDriftTool::new(project_path_buf.clone()))
.tool(HelmlintTool::new(project_path_buf.clone()))
.tool(TerraformFmtTool::new(project_path_buf.clone()))
.tool(TerraformValidateTool::new(project_path_buf.clone()))
.tool(TerraformInstallTool::new())
.tool(ReadFileTool::new(project_path_buf.clone()))
.tool(ListDirectoryTool::new(project_path_buf.clone()))
.tool(WebFetchTool::new())
// Prometheus discovery and connection tools for live K8s analysis
.tool(PrometheusDiscoverTool::new())
.tool(PrometheusConnectTool::new(bg_manager.clone()))
// RAG retrieval tools for compressed tool outputs
.tool(RetrieveOutputTool::new())
.tool(ListOutputsTool::new())
// Platform tools for project management
.tool(ListOrganizationsTool::new())
.tool(ListProjectsTool::new())
.tool(SelectProjectTool::new())
.tool(CurrentContextTool::new())
.tool(OpenProviderSettingsTool::new())
.tool(CheckProviderConnectionTool::new())
.tool(ListDeploymentCapabilitiesTool::new())
.tool(ListHetznerAvailabilityTool::new())
// Deployment tools for service management
.tool(CreateDeploymentConfigTool::new())
.tool(DeployServiceTool::with_context(project_path_buf.clone(), ExecutionContext::InteractiveCli))
.tool(ListDeploymentConfigsTool::new())
.tool(TriggerDeploymentTool::new())
.tool(GetDeploymentStatusTool::new())
.tool(ListDeploymentsTool::new())
.tool(GetServiceLogsTool::new())
.tool(SetDeploymentSecretsTool::with_context(ExecutionContext::InteractiveCli));
// Add generation tools if this is a generation query
if is_generation {
builder = builder
.tool(WriteFileTool::new(project_path_buf.clone()))
.tool(WriteFilesTool::new(project_path_buf.clone()))
.tool(ShellTool::new(project_path_buf.clone()));
}
let agent = builder.additional_params(thinking_params).build();
agent
.prompt(query)
.multi_turn(50)
.await
.map_err(|e| AgentError::ProviderError(e.to_string()))
}
}
}