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
use std::{collections::HashMap, pin::Pin, sync::Arc, time::Duration};
use crate::ctx;
use futures::StreamExt;
use indexmap::IndexMap;
/// A function that transforms messages before they are sent to an upstream.
/// Keyed by agent ID so each agent in an swarm can receive different messages.
pub type TransformMessages = HashMap<
String,
Box<dyn Fn(Vec<objectiveai_sdk::agent::completions::message::Message>) -> Vec<objectiveai_sdk::agent::completions::message::Message> + Send + Sync>,
>;
pub fn response_id(created: u64) -> String {
crate::util::response_id(None, created)
}
// ---------------------------------------------------------------------------
/// Filters agents by upstream type (if required by the continuation) and
/// drops agents whose declared MCP servers can't be authorized — i.e. any
/// server with `requires_auth = true` for which we lack a value in
/// `request_mcp_auth` / `self.mcp_authorization`. The proxy connection
/// is per-agent now, so there's no "URL superset" filter anymore.
fn filter_agents(
agents: Vec<objectiveai_sdk::agent::InlineAgent>,
required_upstream: Option<objectiveai_sdk::agent::Upstream>,
request_mcp_auth: Option<&std::collections::HashMap<String, String>>,
default_mcp_auth: Option<&std::collections::HashMap<String, String>>,
) -> Vec<objectiveai_sdk::agent::InlineAgent> {
agents
.into_iter()
.filter(|agent| {
if let Some(upstream) = required_upstream {
if agent.base().upstream() != upstream {
return false;
}
}
if let Some(servers) = agent.base().mcp_servers() {
for s in servers {
if s.authorization
&& request_mcp_auth.and_then(|m| m.get(&s.url)).is_none()
&& default_mcp_auth.and_then(|m| m.get(&s.url)).is_none()
{
return false;
}
}
}
true
})
.collect()
}
// ---------------------------------------------------------------------------
pub struct Client<CTXEXT, OPENROUTER, CLAUDEAGENTSDK, CODEXSDK, MOCK, RETRG, RETRF, RETRM, CUSG> {
/// MCP Client
pub mcp_client: Arc<objectiveai_sdk::mcp::Client>,
/// Lazy in-process mcp-proxy used for every per-agent MCP connection.
pub proxy_spawner: Arc<super::ProxySpawner>,
/// Default MCP authorization headers (used when ctx doesn't provide them).
pub mcp_authorization: Option<Arc<std::collections::HashMap<String, String>>>,
/// Retrieve router for resolving remote agent references.
pub retrieve_router: Arc<crate::retrieval::retrieve::Router<RETRG, RETRF, RETRM, CTXEXT>>,
/// Handler for tracking usage after completion.
pub usage_handler: Arc<CUSG>,
/// Upstream client for Openrouter agents.
pub openrouter: Arc<OPENROUTER>,
/// Upstream client for Claude Agent SDK agents.
pub claude_agent_sdk: Arc<CLAUDEAGENTSDK>,
/// Upstream client for Codex SDK agents.
pub codex_sdk: Arc<CODEXSDK>,
/// Upstream client for Mock agents.
pub mock: Arc<MOCK>,
/// Viewer client for streaming telemetry.
pub viewer_client: Arc<crate::viewer::Client<CTXEXT>>,
/// Current backoff interval for retry logic.
pub backoff_current_interval: Duration,
/// Initial backoff interval for retry logic.
pub backoff_initial_interval: Duration,
/// Randomization factor for backoff jitter.
pub backoff_randomization_factor: f64,
/// Multiplier for exponential backoff growth.
pub backoff_multiplier: f64,
/// Maximum backoff interval.
pub backoff_max_interval: Duration,
/// Maximum total time to spend on retries.
pub backoff_max_elapsed_time: Duration,
/// Maximum wait time for the first chunk in a streaming response.
pub first_chunk_timeout: Duration,
/// Maximum wait time between subsequent chunks in a streaming response.
pub other_chunk_timeout: Duration,
_marker: std::marker::PhantomData<CTXEXT>,
}
impl<CTXEXT, OPENROUTER, CLAUDEAGENTSDK, CODEXSDK, MOCK, RETRG, RETRF, RETRM, CUSG> Client<CTXEXT, OPENROUTER, CLAUDEAGENTSDK, CODEXSDK, MOCK, RETRG, RETRF, RETRM, CUSG> {
pub fn new(
mcp_client: Arc<objectiveai_sdk::mcp::Client>,
proxy_spawner: Arc<super::ProxySpawner>,
mcp_authorization: Option<Arc<std::collections::HashMap<String, String>>>,
retrieve_router: Arc<crate::retrieval::retrieve::Router<RETRG, RETRF, RETRM, CTXEXT>>,
usage_handler: Arc<CUSG>,
openrouter: Arc<OPENROUTER>,
claude_agent_sdk: Arc<CLAUDEAGENTSDK>,
codex_sdk: Arc<CODEXSDK>,
mock: Arc<MOCK>,
viewer_client: Arc<crate::viewer::Client<CTXEXT>>,
backoff_current_interval: Duration,
backoff_initial_interval: Duration,
backoff_randomization_factor: f64,
backoff_multiplier: f64,
backoff_max_interval: Duration,
backoff_max_elapsed_time: Duration,
first_chunk_timeout: Duration,
other_chunk_timeout: Duration,
) -> Self {
Self {
mcp_client,
proxy_spawner,
mcp_authorization,
retrieve_router,
usage_handler,
openrouter,
claude_agent_sdk,
codex_sdk,
mock,
viewer_client,
backoff_current_interval,
backoff_initial_interval,
backoff_randomization_factor,
backoff_multiplier,
backoff_max_interval,
backoff_max_elapsed_time,
first_chunk_timeout,
other_chunk_timeout,
_marker: std::marker::PhantomData,
}
}
}
impl<CTXEXT, OPENROUTER, CLAUDEAGENTSDK, CODEXSDK, MOCK, RETRG, RETRF, RETRM, CUSG> Clone
for Client<CTXEXT, OPENROUTER, CLAUDEAGENTSDK, CODEXSDK, MOCK, RETRG, RETRF, RETRM, CUSG>
{
fn clone(&self) -> Self {
Self {
mcp_client: self.mcp_client.clone(),
proxy_spawner: self.proxy_spawner.clone(),
mcp_authorization: self.mcp_authorization.clone(),
retrieve_router: self.retrieve_router.clone(),
usage_handler: self.usage_handler.clone(),
openrouter: self.openrouter.clone(),
claude_agent_sdk: self.claude_agent_sdk.clone(),
codex_sdk: self.codex_sdk.clone(),
mock: self.mock.clone(),
viewer_client: self.viewer_client.clone(),
backoff_current_interval: self.backoff_current_interval,
backoff_initial_interval: self.backoff_initial_interval,
backoff_randomization_factor: self.backoff_randomization_factor,
backoff_multiplier: self.backoff_multiplier,
backoff_max_interval: self.backoff_max_interval,
backoff_max_elapsed_time: self.backoff_max_elapsed_time,
first_chunk_timeout: self.first_chunk_timeout,
other_chunk_timeout: self.other_chunk_timeout,
_marker: std::marker::PhantomData,
}
}
}
impl<CTXEXT, OPENROUTER, CLAUDEAGENTSDK, CODEXSDK, MOCK, RETRG, RETRF, RETRM, CUSG> Client<CTXEXT, OPENROUTER, CLAUDEAGENTSDK, CODEXSDK, MOCK, RETRG, RETRF, RETRM, CUSG>
where
CTXEXT: ctx::ContextExt + Send + Sync + 'static,
OPENROUTER: super::UpstreamClient<objectiveai_sdk::agent::openrouter::Agent, objectiveai_sdk::agent::openrouter::Continuation> + Send + Sync + 'static,
CLAUDEAGENTSDK: super::UpstreamClient<objectiveai_sdk::agent::claude_agent_sdk::Agent, objectiveai_sdk::agent::claude_agent_sdk::Continuation> + Send + Sync + 'static,
CODEXSDK: super::UpstreamClient<objectiveai_sdk::agent::codex_sdk::Agent, objectiveai_sdk::agent::codex_sdk::Continuation> + Send + Sync + 'static,
MOCK: super::UpstreamClient<objectiveai_sdk::agent::mock::Agent, objectiveai_sdk::agent::mock::Continuation> + Send + Sync + 'static,
RETRG: crate::retrieval::retrieve::Client<CTXEXT>,
RETRF: crate::retrieval::retrieve::Client<CTXEXT>,
RETRM: crate::retrieval::retrieve::Client<CTXEXT>,
CUSG: super::usage_handler::UsageHandler<CTXEXT> + Send + Sync + 'static,
{
/// Creates a unary agent completion, tracking usage after completion.
///
/// Internally streams the response and aggregates chunks into a single response.
pub async fn create_unary_handle_usage(
self: Arc<Self>,
ctx: ctx::Context<CTXEXT, impl crate::ctx::persistent_cache::PersistentCacheClient>,
params: Arc<objectiveai_sdk::agent::completions::request::AgentCompletionCreateParams>,
continuation: Option<
super::Continuation<
OPENROUTER::State,
CLAUDEAGENTSDK::State,
CODEXSDK::State,
MOCK::State,
>,
>,
disable_tools: Option<Arc<dyn Fn() -> bool + Send + Sync>>,
extra_mcp_servers: Vec<super::ExtraMcpServer>,
extra_mcp_headers: indexmap::IndexMap<String, String>,
transform_messages: Option<Arc<TransformMessages>>,
viewer: bool,
invention_type: Option<objectiveai_sdk::functions::inventions::prompts::StepPromptType>,
invention_step: Option<usize>,
invention_tasks_min: Option<u64>,
invention_input_schema: Option<String>,
) -> Result<
objectiveai_sdk::agent::completions::response::unary::AgentCompletion,
super::Error,
> {
let mut aggregate: Option<
objectiveai_sdk::agent::completions::response::streaming::AgentCompletionChunk,
> = None;
let mut stream = self
.create_streaming_handle_usage(ctx, params, continuation, disable_tools, extra_mcp_servers, extra_mcp_headers, transform_messages, viewer, invention_type, invention_step, invention_tasks_min, invention_input_schema)
.await?;
while let Some(item) = stream.next().await {
match item {
super::StreamItem::Chunk(chunk) => match &mut aggregate {
Some(agg) => agg.push(&chunk),
None => aggregate = Some(chunk),
},
super::StreamItem::State(_) => {}
}
}
Ok(aggregate.unwrap().into())
}
/// Creates a streaming agent completion, tracking usage after the stream ends.
pub async fn create_streaming_handle_usage(
self: Arc<Self>,
ctx: ctx::Context<CTXEXT, impl crate::ctx::persistent_cache::PersistentCacheClient>,
params: Arc<objectiveai_sdk::agent::completions::request::AgentCompletionCreateParams>,
continuation: Option<
super::Continuation<
OPENROUTER::State,
CLAUDEAGENTSDK::State,
CODEXSDK::State,
MOCK::State,
>,
>,
disable_tools: Option<Arc<dyn Fn() -> bool + Send + Sync>>,
extra_mcp_servers: Vec<super::ExtraMcpServer>,
extra_mcp_headers: indexmap::IndexMap<String, String>,
transform_messages: Option<Arc<TransformMessages>>,
viewer: bool,
invention_type: Option<objectiveai_sdk::functions::inventions::prompts::StepPromptType>,
invention_step: Option<usize>,
invention_tasks_min: Option<u64>,
invention_input_schema: Option<String>,
) -> Result<
impl futures::Stream<
Item = super::StreamItem<
super::Continuation<
OPENROUTER::State,
CLAUDEAGENTSDK::State,
CODEXSDK::State,
MOCK::State,
>,
>,
> + Send
+ Unpin
+ 'static,
super::Error,
> {
let (tx, rx) = tokio::sync::mpsc::unbounded_channel();
let _ = tokio::spawn(async move {
let stream = match self
.create_streaming(ctx.clone(), params.clone(), continuation, disable_tools, extra_mcp_servers, extra_mcp_headers, transform_messages, viewer, invention_type, invention_step, invention_tasks_min, invention_input_schema)
.await
{
Ok(stream) => stream,
Err(e) => {
let _ = tx.send(Err(e));
return;
}
};
let mut aggregate: Option<
objectiveai_sdk::agent::completions::response::streaming::AgentCompletionChunk,
> = None;
futures::pin_mut!(stream);
while let Some(item) = stream.next().await {
match &item {
super::StreamItem::Chunk(chunk) => {
match &mut aggregate {
Some(agg) => agg.push(chunk),
None => aggregate = Some(chunk.clone()),
}
}
super::StreamItem::State(_) => {}
}
if tx.send(Ok(item)).is_err() {
ctx.cancel();
}
}
drop(stream);
drop(tx);
let response: objectiveai_sdk::agent::completions::response::unary::AgentCompletion =
aggregate.unwrap().into();
if response.usage.any_usage() {
self.usage_handler
.handle_usage(ctx, params, response)
.await;
}
});
let mut stream =
tokio_stream::wrappers::UnboundedReceiverStream::new(rx);
match stream.next().await {
Some(Ok(first)) => Ok(
futures::stream::iter(std::iter::once(first))
.chain(stream.map(Result::unwrap)),
),
Some(Err(e)) => Err(e),
None => unreachable!(),
}
}
pub async fn create_streaming(
&self,
ctx: ctx::Context<CTXEXT, impl crate::ctx::persistent_cache::PersistentCacheClient>,
params: Arc<objectiveai_sdk::agent::completions::request::AgentCompletionCreateParams>,
continuation: Option<
super::Continuation<
OPENROUTER::State,
CLAUDEAGENTSDK::State,
CODEXSDK::State,
MOCK::State,
>,
>,
disable_tools: Option<Arc<dyn Fn() -> bool + Send + Sync>>,
// URLs to fold into every per-agent `X-MCP-Servers` header
// *without* mutating the agent's own `mcp_servers` config — used
// by the function-inventions orchestrator to plumb the shared
// InventionServer URL through the proxy without affecting the
// agent's content-derived ID.
extra_mcp_servers: Vec<super::ExtraMcpServer>,
// Headers to merge into the per-agent `X-MCP-Headers` map. The
// proxy forwards these verbatim to every upstream it fans out
// to. Used by the function-inventions orchestrator to send its
// tenant id (`X-Invention-Session-Id`) to the shared
// InventionServer.
extra_mcp_headers: indexmap::IndexMap<String, String>,
transform_messages: Option<Arc<TransformMessages>>,
viewer: bool,
invention_type: Option<objectiveai_sdk::functions::inventions::prompts::StepPromptType>,
invention_step: Option<usize>,
invention_tasks_min: Option<u64>,
invention_input_schema: Option<String>,
) -> Result<
impl futures::Stream<
Item = super::StreamItem<
super::Continuation<
OPENROUTER::State,
CLAUDEAGENTSDK::State,
CODEXSDK::State,
MOCK::State,
>,
>,
> + Send,
super::Error,
> {
// Cancellation check closure factory — creates a new closure
// that shares the same underlying AtomicBool via ctx's Arc.
let make_is_cancelled = {
let ctx = ctx.clone();
move || {
let ctx = ctx.clone();
move || ctx.is_cancelled()
}
};
// Parse request continuation from base64 string if provided.
let request_continuation = match ¶ms.continuation {
Some(s) => Some(
objectiveai_sdk::agent::Continuation::try_from_string(s)
.ok_or(super::Error::InvalidContinuation)?,
),
None => None,
};
let created = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_secs();
// Capture the internal continuation's instance hierarchy before
// `continuation` is consumed during item extraction below. The wire
// composite (`request_continuation`) stays live until the per-slot
// builder, where `continuation_agent_instance_hierarchy` is resolved.
let continuation_internal_aih: Option<String> = continuation
.as_ref()
.map(|c| c.agent_instance_hierarchy().to_string());
// Pre-yield errors correlate against an agent's response id, passed
// explicitly at each call site (the primary's response id).
let send_viewer_err = {
let viewer_client = self.viewer_client.clone();
let ctx_for_err = ctx.clone();
move |response_id: &str, e: super::Error| -> super::Error {
if viewer {
viewer_client.send_agent_completion_error(
ctx_for_err.clone(),
response_id.to_string(),
&e,
);
}
e
}
};
// 1. Panic if internal and request continuation upstream types conflict.
if let (Some(ic), Some(rc)) = (&continuation, &request_continuation) {
assert_eq!(
ic.upstream(), rc.upstream(),
"internal and request continuation upstream types must match"
);
}
// 2. Extract continuation items, MCP connection, and upstream type.
let cont_upstream = continuation.as_ref().map(|c| c.upstream());
let (
mut cont_items_or,
mut cont_items_cas,
mut cont_items_cdx,
mut cont_items_mock,
internal_conn,
) = match continuation {
Some(super::Continuation::Openrouter { items, mcp_connection, .. }) => {
(items, vec![], vec![], vec![], mcp_connection)
}
Some(super::Continuation::ClaudeAgentSdk { items, mcp_connection, .. }) => {
(vec![], items, vec![], vec![], mcp_connection)
}
Some(super::Continuation::CodexSdk { items, mcp_connection, .. }) => {
(vec![], vec![], items, vec![], mcp_connection)
}
Some(super::Continuation::Mock { items, mcp_connection, .. }) => {
(vec![], vec![], vec![], items, mcp_connection)
}
None => (vec![], vec![], vec![], vec![], None),
};
// 3. Always resolve agents from params.agent.
// `agent_remote` is `Some(path)` when the WF was fetched from a
// remote (stamped on every chunk + outbound MCP-proxy header),
// `None` when the request supplied the WF inline.
let (agent_wf, agent_remote) = self
.retrieve_router
.get_agent(&ctx, params.agent.clone())
.await
.map_err(|e| super::Error::InvalidAgent(e.message.to_string()))?;
let inline = agent_wf.inline();
// WF-level identity: concatenation of the primary id with all
// fallback ids. See `InlineAgentWithFallbacks::full_id`. Same
// value for every slot in this completion.
let agent_full_id = inline.full_id();
let mut all_agents: Vec<objectiveai_sdk::agent::InlineAgent> = vec![inline.inner.clone()];
if let Some(fallbacks) = &inline.fallbacks {
all_agents.extend(fallbacks.iter().cloned());
}
// 4. Filter agents: drop those whose required upstream doesn't
// match the continuation, or whose required MCP authorization
// is missing.
let required_upstream = cont_upstream
.or_else(|| request_continuation.as_ref().map(|c| c.upstream()));
let request_mcp_auth = ctx.mcp_authorization().await;
let filtered_agents = filter_agents(
all_agents,
required_upstream,
request_mcp_auth.as_deref(),
self.mcp_authorization.as_deref(),
);
// Per-agent identities. `response_ids` are freshly minted, one per
// slot, and stay pure (no dash) so the `-`-joined RESPONSE-IDS group
// and X-OBJECTIVEAI-RESPONSE-ID are unaffected. An agent *instance* is
// `{agent.id()}-{response_id}`; the hierarchy is the spawner lineage
// (`ctx.agent_instance_hierarchy()`) joined by `/` with this instance.
let response_ids: Vec<String> =
filtered_agents.iter().map(|_| response_id(created)).collect();
// Primary id for pre-yield viewer-error correlation (empty when no
// agents survived filtering).
let primary_response_id: String =
response_ids.first().cloned().unwrap_or_default();
// Reuse on resume: internal continuation first, else the wire
// continuation; `None` on a fresh call.
let continuation_agent_instance_hierarchy: Option<String> =
continuation_internal_aih.or_else(|| {
request_continuation
.as_ref()
.map(|c| c.agent_instance_hierarchy().to_string())
.filter(|s| !s.is_empty())
});
// When resuming, the hierarchy is fixed: every slot gets that exact
// value. Otherwise build a fresh per-agent instance. The first
// segment is the WF-level `agent_full_id` (constant across all
// slots in this completion) followed by `-{response_id}` to
// disambiguate slots.
let agent_instance_hierarchies: Vec<String> =
match &continuation_agent_instance_hierarchy {
Some(h) => vec![h.clone(); filtered_agents.len()],
None => filtered_agents
.iter()
.enumerate()
.map(|(i, _agent)| {
let agent_instance = format!("{}-{}", agent_full_id, response_ids[i]);
match ctx.agent_instance_hierarchy() {
Some(prefix) => format!("{prefix}/{agent_instance}"),
None => agent_instance,
}
})
.collect(),
};
// 5. Boot the in-process proxy (idempotent — first call wins,
// subsequent calls reuse the same handle) and kick off one
// connect per agent in parallel. Awaiting each `JoinHandle`
// inside the per-agent branch later means the round-trips
// overlap rather than serializing.
let proxy_handle = self
.proxy_spawner
.get()
.await
.map_err(|e| send_viewer_err(&primary_response_id, super::Error::McpProxyBootstrap(e.to_string())))?;
let proxy_url = proxy_handle.url.clone();
let request_mcp_auth_owned = request_mcp_auth.clone();
let default_mcp_auth_owned = self.mcp_authorization.clone();
let internal_conn_for_resume = internal_conn.clone();
// Client-side resume path: when the wire continuation carries
// a `mcp_sessions[proxy_url]`, use it to resume the proxy
// session so upstream MCP sessions (and therefore tool
// subprocess `MCP_SESSION_ID`s) stay stable across separate
// `agents message` / `agent completions create` invocations.
// Without this fallback, every continuation turn creates a
// fresh proxy session, which dials upstream MCPs fresh, and
// any per-session tool state (counters, caches keyed on
// `MCP_SESSION_ID`) resets per turn.
let wire_proxy_session_id: Option<String> = request_continuation
.as_ref()
.and_then(|rc| rc.mcp_sessions().get(&proxy_url).cloned());
// Per-agent reverse-attach registration. For every surviving
// agent that declares `client_objectiveai_mcp`, register that
// agent's `response_id` against the inbound WS's
// `ReverseAttachHandle` — same value the proxy stamps on every
// outbound reverse-channel request as
// `X-OBJECTIVEAI-RESPONSE-ID`, so `route()` in
// `objectiveai_mcp/routes.rs` finds the matching channel via a
// header lookup against this exact id.
//
// Multiplicities preserved:
// - **One response_id → many per-MCP routes from the proxy.**
// The proxy fans out one outbound HTTP MCP connection per
// upstream URL (`/objectiveai`, `/{owner}/{name}/{ver}/{mcp}`,
// ...). Every one carries the same `X-OBJECTIVEAI-RESPONSE-ID`;
// route() finds the channel once, the per-MCP discrimination
// happens downstream via the path-extracted `McpKind`.
// - **One WS may serve many response_ids (swarm).** Each
// surviving agent registers its own response_id; all entries
// point to the same underlying `ReverseChannel`.
// `ReverseAttachGuard::drop` removes every registered id
// when the WS closes.
// - **Continuations.** Each turn mints a fresh response_id and
// re-registers. Cross-turn upstream-session stability is
// handled by the proxy's own `Mcp-Session-Id` + the
// continuation's `mcp_sessions[proxy_url]` map (the proxy
// reuses its in-memory `Session` and overwrites cached
// transient headers with the new turn's response_id via
// `apply_transient_headers` before any reused-connection
// request goes out). The api-side registry key is
// deliberately a per-turn value because that's what
// `apply_transient_headers` keeps fresh.
let agent_needs_reverse_attach: Vec<bool> = filtered_agents
.iter()
.enumerate()
.map(|(i, agent)| {
let needs = agent.base().client_objectiveai_mcp().is_some()
&& ctx.mcp_port().is_some()
&& ctx.reverse_attach().is_some();
if needs {
// Both `mcp_port` and `reverse_attach` were just
// checked above; unwrap is safe.
ctx.reverse_attach()
.unwrap()
.register(response_ids[i].clone());
}
needs
})
.collect();
let mcp_port_for_synth = ctx.mcp_port();
// Dash-joined list of every per-agent response_id leaf in
// this completion. Stamped identically on every per-agent
// proxy connect (`X-OBJECTIVEAI-RESPONSE-IDS`) so cli-stream
// learns the sibling set from whichever connect lands first
// — driving the group-local loser sweep in
// `ConduitMcpHandler::select_response_ids`.
let response_ids_group: String = response_ids.join("-");
let connect_handles: Vec<
Option<
tokio::task::JoinHandle<
Result<
(
objectiveai_sdk::mcp::Connection,
Option<
std::sync::Arc<Vec<objectiveai_sdk::mcp::tool::Tool>>,
>,
),
std::sync::Arc<objectiveai_sdk::mcp::Error>,
>,
>,
>,
> = filtered_agents
.iter()
.zip(agent_instance_hierarchies.iter())
.zip(response_ids.iter())
.zip(agent_needs_reverse_attach.iter().copied())
.map(|(((agent, agent_instance_hierarchy), id), needs_reverse_attach)| {
// Build the per-agent X-MCP-* header set: the agent's
// declared `mcp_servers` plus any caller-supplied
// `extra_mcp_servers` (e.g. the function-inventions
// orchestrator's per-step InventionServer URL — kept
// out of the agent's own config so its content-hashed
// ID stays stable across runs).
let mut urls: Vec<String> = agent
.base()
.mcp_servers()
.map(|s| s.iter().map(|s| s.url.clone()).collect())
.unwrap_or_default();
urls.extend(extra_mcp_servers.iter().map(|s| s.url.clone()));
// If the agent declares `client_objectiveai_mcp` AND
// a WS-attached CLI is on the other end, emit one
// synthetic URL per CLI-hosted MCP server. The proxy
// dials each as an independent upstream; the API's
// loopback MCP router parses the path back into a
// [`McpKind`] and forwards over the WS conduit. The
// CLI conduit treats each URL as a separate MCP
// session with its own `Mcp-Session-Id`, no
// aggregation, no tool renaming.
//
// - `/objectiveai` is emitted only when the agent
// actually needs the primary upstream (declared
// `tools`, set `objectiveai = true`, or declared an
// `executable` plugin — non-executable plugins are
// present purely for their `mcp_servers`).
// - One `/{owner}/{name}/{version}/{mcp}` per declared
// plugin MCP server. Plugin args ride alongside as
// `X-OBJECTIVEAI-ARGUMENTS` (per-URL header), JSON-
// serialized in declaration order.
let client_mcp_synthetic_urls: Vec<(
String,
Option<indexmap::IndexMap<String, Option<String>>>,
)> = match (
needs_reverse_attach,
mcp_port_for_synth,
agent.base().client_objectiveai_mcp(),
) {
(true, Some(mcp_port), Some(client_mcp)) => {
let mut out: Vec<(
String,
Option<indexmap::IndexMap<String, Option<String>>>,
)> = Vec::new();
let needs_objectiveai = !client_mcp.tools.is_empty()
|| client_mcp.objectiveai.unwrap_or(false)
|| client_mcp.plugins.iter().any(|p| p.executable);
if needs_objectiveai {
out.push((
format!("http://127.0.0.1:{mcp_port}/objectiveai"),
None,
));
}
for plugin in &client_mcp.plugins {
for entry in plugin.mcp_servers.as_deref().unwrap_or(&[]) {
let path = format!(
"{owner}/{name}/{version}/{mcp}",
owner = percent_encode_segment(&plugin.owner),
name = percent_encode_segment(&plugin.name),
version = percent_encode_segment(&plugin.version),
mcp = percent_encode_segment(&entry.name),
);
out.push((
format!("http://127.0.0.1:{mcp_port}/{path}"),
entry.arguments.clone(),
));
}
}
out
}
_ => Vec::new(),
};
urls.extend(client_mcp_synthetic_urls.iter().map(|(u, _)| u.clone()));
// No MCP servers → no proxy
// connection needed for this agent. Skipping the spawn
// also keeps the per-agent proxy session out of the
// response continuation's `mcp_sessions` map for
// requests that don't use MCP at all.
if urls.is_empty() {
return None;
}
// Build the per-URL header map sent as `X-MCP-Headers`
// to the proxy. For each agent-declared server URL,
// start from the orchestrator-supplied `extra_mcp_headers`
// (e.g. the function-inventions tenant id) and layer on
// any configured `Authorization` for that URL. For each
// entry in `extra_mcp_servers`, start from
// `extra_mcp_headers` and layer on its own per-server
// headers (which win on conflict). The proxy stamps
// each per-URL header set on every outbound request
// to that upstream.
let mut per_url_headers: indexmap::IndexMap<
String,
indexmap::IndexMap<String, String>,
> = indexmap::IndexMap::new();
if let Some(servers) = agent.base().mcp_servers() {
for s in servers {
let mut h = extra_mcp_headers.clone();
if let Some(v) = request_mcp_auth_owned
.as_deref()
.and_then(|m| m.get(&s.url))
.or_else(|| {
default_mcp_auth_owned
.as_deref()
.and_then(|m| m.get(&s.url))
})
{
h.insert("Authorization".to_string(), v.clone());
}
per_url_headers.insert(s.url.clone(), h);
}
}
for s in &extra_mcp_servers {
let entry = per_url_headers
.entry(s.url.clone())
.or_insert_with(|| extra_mcp_headers.clone());
if let Some(server_headers) = &s.headers {
for (k, v) in server_headers {
entry.insert(k.clone(), v.clone());
}
}
}
// Plugin URLs carry `X-OBJECTIVEAI-ARGUMENTS` —
// JSON-serialized declaration-order IndexMap the CLI
// uses to spawn `<plugin> mcp <mcp> begin --<k> [v]`.
// The URL path carries the McpKind discriminator.
// `X-OBJECTIVEAI-RESPONSE-ID` is NOT stamped per-URL —
// it's session-global, transmitted at the top level
// (`proxy_request_headers` below) and stored on the
// proxy's `Session::transient_headers` for re-stamping
// on every outbound request.
for (url, args) in &client_mcp_synthetic_urls {
let entry = per_url_headers
.entry(url.clone())
.or_insert_with(|| extra_mcp_headers.clone());
if let Some(args) = args {
if let Ok(json) = serde_json::to_string(args) {
entry.insert(
"X-OBJECTIVEAI-ARGUMENTS".to_string(),
json,
);
}
}
}
// Stamp the three `X-OBJECTIVEAI-MCP-*` headers on
// the `/objectiveai` per-URL entry. Stamped
// unconditionally — the proxy ignores inbound
// `X-MCP-Headers` entirely on its resume path (it
// rebuilds the per-URL header bag from the AEAD-encoded
// payload), so emitting them on a resume is inert.
// `per_url_headers` only contains the `/objectiveai`
// key when `client_mcp_synthetic_urls` synthesized it
// above (driven by `needs_objectiveai`), so a missing
// entry is the correct no-op signal.
if let (Some(mcp_port), Some(client_mcp)) = (
mcp_port_for_synth,
agent.base().client_objectiveai_mcp(),
) {
let objectiveai_url =
format!("http://127.0.0.1:{mcp_port}/objectiveai");
if let Some(entry) =
per_url_headers.get_mut(&objectiveai_url)
{
for (k, v) in client_mcp.mcp_headers().to_headers() {
entry.insert(k, v);
}
}
}
// Both `agent_instance_hierarchy` and `id` here are the closure's
// per-slot bindings (zipped in from `agent_instance_hierarchies` and
// `response_ids` above). `agent_instance_hierarchy` is the full
// hierarchy (caller-lineage joined with this slot's instance,
// `{agent_full_id}-{response_id}`); `id` is this slot's pure
// response id — same value used downstream as `attempt.id`
// (chunk id, notify key) and `X-OBJECTIVEAI-RESPONSE-ID`.
// `RESPONSE-IDS` is the dash-joined group of every
// sibling response_id in this completion, stamped
// identically on every per-agent connect.
// `AGENT-ID` is the per-slot leaf id; `AGENT-FULL-ID`
// is the WF-level id (same across slots);
// `AGENT-REMOTE` is the JSON-encoded `RemotePath` when
// the WF was fetched remotely and is **omitted entirely**
// when the agent is inline — empty-string headers are
// forbidden end-to-end (the proxy filters absent keys
// out of `Session::transient_headers` naturally, and
// the cli conduit's `require_transient` treats
// AGENT-REMOTE as optional).
let mut proxy_request_headers: indexmap::IndexMap<String, String> =
indexmap::indexmap! {
"X-MCP-Servers".to_string() => serde_json::to_string(&urls).unwrap(),
"X-MCP-Headers".to_string() => serde_json::to_string(&per_url_headers).unwrap(),
"X-OBJECTIVEAI-AGENT-INSTANCE-HIERARCHY".to_string() => agent_instance_hierarchy.clone(),
"X-OBJECTIVEAI-AGENT-ID".to_string() => agent.id().to_string(),
"X-OBJECTIVEAI-AGENT-FULL-ID".to_string() => agent_full_id.clone(),
"X-OBJECTIVEAI-RESPONSE-ID".to_string() => id.clone(),
"X-OBJECTIVEAI-RESPONSE-IDS".to_string() => response_ids_group.clone(),
};
if let Some(remote) = agent_remote.as_ref() {
if let Ok(serialized) = serde_json::to_string(remote) {
proxy_request_headers.insert(
"X-OBJECTIVEAI-AGENT-REMOTE".to_string(),
serialized,
);
}
}
let mcp_client = self.mcp_client.clone();
let proxy_url = proxy_url.clone();
// Resume the proxy session if we're continuing — the
// upstream sessions already live behind it. Prefer the
// internal continuation (server-side retry; in-memory
// Connection still warm) over the wire continuation
// (client-side resume; only the encoded session id is
// available — proxy reconstructs upstreams via its
// AEAD payload).
let session_id = internal_conn_for_resume
.as_ref()
.map(|c| c.session_id.clone())
.or_else(|| wire_proxy_session_id.clone());
// Only agents that declared `client_objectiveai_mcp`
// need a `list_tools` round-trip — they're the ones
// we have a declaration to validate against. Agents
// with only `mcp_servers` / `extra_mcp_servers` skip
// it and pay one fewer round-trip.
let needs_list_tools = agent.base().client_objectiveai_mcp().is_some();
// Per-agent spawn: connect → optionally list_tools.
// Every agent's task runs concurrently with every
// other agent's, so the proxy `initialize` round-trips
// fan out in parallel. The list_tools result is the
// union across every declared upstream; the CLI's
// conduit applies its `X-OBJECTIVEAI-MCP-CONFIG`
// filter to the synthetic-upstream slice.
//
// Plugin MCP upstreams are NOT dialed here — the CLI
// dials them inside its `initialize` handler so each
// upstream gets the proxy-supplied aggregate session
// id for resume on continuation.
//
// Error type is `Arc<mcp::Error>` because the SDK's
// `list_tools` returns shared-ref errors (the cached
// refresh-tools task fills the same slot), so wrapping
// the `connect` error in `Arc` matches the downstream
// error handling shape uniformly.
Some(tokio::spawn(async move {
let conn = mcp_client
.connect(proxy_url, session_id, Some(proxy_request_headers))
.await
.map_err(std::sync::Arc::new)?;
let tools = if needs_list_tools {
Some(conn.list_tools().await?)
} else {
None
};
Ok::<_, std::sync::Arc<objectiveai_sdk::mcp::Error>>((conn, tools))
}))
})
.collect();
// 7. Build agent attempts. Each holds its own connect-handle
// JoinHandle (or None when the agent has no MCP work);
// the actual `await` happens inside the per-agent branch
// in step 8 below.
struct AgentAttempt {
agent: objectiveai_sdk::agent::InlineAgent,
connect_handle: Option<
tokio::task::JoinHandle<
Result<
(
objectiveai_sdk::mcp::Connection,
Option<
std::sync::Arc<
Vec<objectiveai_sdk::mcp::tool::Tool>,
>,
>,
),
std::sync::Arc<objectiveai_sdk::mcp::Error>,
>,
>,
>,
/// Composite per-slot agent id forwarded as
/// `X-OBJECTIVEAI-AGENT-INSTANCE-HIERARCHY` to the MCP proxy and (for
/// runner-backed upstreams) as `OBJECTIVEAI_AGENT_INSTANCE_HIERARCHY` in
/// the env dict the runner hands to its child SDK
/// subprocess. Derived from the response id (see step
/// 6.5 above).
agent_instance_hierarchy: String,
/// Per-slot response-id leaf — trailing slash-segment of
/// `agent_instance_hierarchy`. The value passed into `run_agent_loop`
/// as the `id` argument, which becomes
/// `AgentCompletionChunk.id` and the value cli-stream's
/// conduit cache + sibling-group sweep match on.
id: String,
}
let mut attempts: Vec<AgentAttempt> = filtered_agents
.into_iter()
.zip(connect_handles)
.zip(agent_instance_hierarchies)
.zip(response_ids)
.map(|(((agent, connect_handle), agent_instance_hierarchy), id)| AgentAttempt {
agent,
connect_handle,
agent_instance_hierarchy,
id,
})
.collect();
// Slot of resolved-or-None per attempt — populated lazily on
// first awaited iteration of the retry loop, reused across
// backoff retries so we don't re-issue the connect.
let mut attempt_connections: Vec<Option<objectiveai_sdk::mcp::Connection>> =
(0..attempts.len()).map(|_| None).collect();
let mut attempt_connect_done: Vec<bool> = (0..attempts.len()).map(|_| false).collect();
// 8. Backoff retry loop — try each agent in order.
let mut backoff = backoff::ExponentialBackoff {
current_interval: self.backoff_current_interval,
initial_interval: self.backoff_initial_interval,
randomization_factor: self.backoff_randomization_factor,
multiplier: self.backoff_multiplier,
max_interval: self.backoff_max_interval,
start_time: std::time::Instant::now(),
max_elapsed_time: Some(self.backoff_max_elapsed_time),
clock: backoff::SystemClock::default(),
};
loop {
let mut errors: Vec<super::Error> = Vec::new();
for (idx, attempt) in attempts.iter_mut().enumerate() {
// Resolve the per-agent proxy connect handle on first
// visit. All N connects were spawned up-front so the
// initialize round-trips overlap; awaiting individual
// handles here is cheap on later retry iterations. The
// spawned task also calls `list_tools()` on the new
// connection — the union list returned across every
// upstream — so we can immediately validate the
// agent's declared
// tools without an extra round-trip.
if !attempt_connect_done[idx] {
attempt_connect_done[idx] = true;
if let Some(handle) = attempt.connect_handle.take() {
match handle.await.unwrap() {
Ok((conn, tools_opt)) => {
// Validate the agent's
// `client_objectiveai_mcp.tools`
// declaration against the actual
// upstream tool list. Missing any
// declared tool fails this attempt.
// `tools_opt` is `Some` iff the agent
// declared `client_objectiveai_mcp`
// (we only paid for the round-trip
// when there was a declaration to
// validate).
let declaration =
attempt.agent.base().client_objectiveai_mcp();
let mut missing: Option<
objectiveai_sdk::agent::ClientObjectiveaiMcpEntry,
> = None;
if let (Some(client_mcp), Some(tools)) =
(declaration, tools_opt.as_ref())
{
let returned: Vec<&str> = tools
.iter()
.map(|t| t.name.as_ref())
.collect();
for declared in &client_mcp.tools {
let suffix = format!("_{}", declared.name);
let present = returned.iter().any(|n| {
*n == declared.name.as_str()
|| n.ends_with(&suffix)
});
if !present {
missing = Some(declared.clone());
break;
}
}
}
match missing {
None => attempt_connections[idx] = Some(conn),
Some(declared) => {
errors.push(
super::Error::ClientObjectiveaiMcpToolMissing {
owner: declared.owner,
name: declared.name,
version: declared.version,
},
);
}
}
}
Err(e) => {
errors.push(super::Error::McpConnectionArc(e));
}
}
}
}
// An agent whose declared MCP servers are empty has no
// connection but is still allowed to run (no proxy
// session needed); only skip when the agent declared
// servers and the connect failed.
//
// `client_objectiveai_mcp` declarations REQUIRE an
// mcp_connection: on the SSE/unary path
// (`ctx.reverse_attach()` is `None`) no synthetic URL
// is added → `urls.is_empty()` → `connect_handle` is
// `None` → `attempt_connections[idx]` is `None` →
// we surface `ClientObjectiveaiMcpUnavailable` so the
// caller knows reverse-attach was required but not
// available.
let agent_needs_mcp = attempt.agent.base().mcp_servers().is_some()
|| !extra_mcp_servers.is_empty()
|| attempt.agent.base().client_objectiveai_mcp().is_some();
let mcp_connection: Option<objectiveai_sdk::mcp::Connection> =
attempt_connections[idx].clone();
if agent_needs_mcp && mcp_connection.is_none() {
if attempt.agent.base().client_objectiveai_mcp().is_some()
&& (ctx.mcp_port().is_none() || ctx.reverse_attach().is_none())
{
errors.push(super::Error::ClientObjectiveaiMcpUnavailable);
}
continue;
}
// d. Get BYOK for this agent's upstream.
let byok = ctx.upstream_authorization(attempt.agent.base().upstream()).await;
// e. BYOK strategy: try with key first, then without.
let byok_attempts: Vec<Option<&str>> = match &byok {
Some(key) => vec![Some(key.as_str()), None],
None => vec![None],
};
let agent_transform = transform_messages.as_ref().and_then(|tm| {
tm.get(attempt.agent.id()).map(|f| f.as_ref())
});
for byok_attempt in &byok_attempts {
let err = match &attempt.agent {
objectiveai_sdk::agent::InlineAgent::Openrouter(or_agent) => {
let c = mcp_connection.clone();
let rc = match &request_continuation {
Some(objectiveai_sdk::agent::Continuation::Openrouter(c)) => Some(c),
_ => None,
};
match self.run_agent_loop(
self.openrouter.clone(), or_agent, rc, ¶ms, mcp_connection.clone(),
ctx.reverse_attach().cloned(),
&mut cont_items_or, &attempt.id, created,
*byok_attempt, ctx.cost_multiplier,
{
let agent_instance_hierarchy = attempt.agent_instance_hierarchy.clone();
move |items| super::Continuation::Openrouter {
items, mcp_connection: c, agent_instance_hierarchy,
}
},
|e| super::Error::UpstreamOpenrouter(Box::new(e)),
objectiveai_sdk::agent::InlineAgentRef::Openrouter(&or_agent.base),
disable_tools.clone(),
agent_transform,
make_is_cancelled(),
invention_type,
invention_step,
invention_tasks_min,
invention_input_schema.clone(),
attempt.agent_instance_hierarchy.as_str(),
attempt.agent.id(),
agent_full_id.as_str(),
agent_remote.as_ref(),
).await {
Ok(stream) => {
if !viewer { return Ok(stream); }
let vc = self.viewer_client.clone();
let vctx = ctx.clone();
let params_for_viewer = params.clone();
let mut sent_begin = false;
return Ok(Box::pin(futures::StreamExt::inspect(stream, move |item| {
if let super::StreamItem::Chunk(chunk) = item {
if !sent_begin {
sent_begin = true;
vc.send_agent_completion_begin(
vctx.clone(), chunk.id.clone(), params_for_viewer.clone(),
);
}
vc.send_agent_completion_continue(vctx.clone(), chunk.clone());
}
})));
}
Err(e) => e,
}
}
objectiveai_sdk::agent::InlineAgent::ClaudeAgentSdk(cas_agent) => {
let c = mcp_connection.clone();
let rc = match &request_continuation {
Some(objectiveai_sdk::agent::Continuation::ClaudeAgentSdk(c)) => Some(c),
_ => None,
};
match self.run_agent_loop(
self.claude_agent_sdk.clone(), cas_agent, rc, ¶ms, mcp_connection.clone(),
ctx.reverse_attach().cloned(),
&mut cont_items_cas, &attempt.id, created,
*byok_attempt, ctx.cost_multiplier,
{
let agent_instance_hierarchy = attempt.agent_instance_hierarchy.clone();
move |items| super::Continuation::ClaudeAgentSdk {
items, mcp_connection: c, agent_instance_hierarchy,
}
},
|e| super::Error::UpstreamClaudeAgentSdk(Box::new(e)),
objectiveai_sdk::agent::InlineAgentRef::ClaudeAgentSdk(&cas_agent.base),
disable_tools.clone(),
agent_transform,
make_is_cancelled(),
invention_type,
invention_step,
invention_tasks_min,
invention_input_schema.clone(),
attempt.agent_instance_hierarchy.as_str(),
attempt.agent.id(),
agent_full_id.as_str(),
agent_remote.as_ref(),
).await {
Ok(stream) => {
if !viewer { return Ok(stream); }
let vc = self.viewer_client.clone();
let vctx = ctx.clone();
let params_for_viewer = params.clone();
let mut sent_begin = false;
return Ok(Box::pin(futures::StreamExt::inspect(stream, move |item| {
if let super::StreamItem::Chunk(chunk) = item {
if !sent_begin {
sent_begin = true;
vc.send_agent_completion_begin(
vctx.clone(), chunk.id.clone(), params_for_viewer.clone(),
);
}
vc.send_agent_completion_continue(vctx.clone(), chunk.clone());
}
})));
}
Err(e) => e,
}
}
objectiveai_sdk::agent::InlineAgent::CodexSdk(cdx_agent) => {
let c = mcp_connection.clone();
let rc = match &request_continuation {
Some(objectiveai_sdk::agent::Continuation::CodexSdk(c)) => Some(c),
_ => None,
};
match self.run_agent_loop(
self.codex_sdk.clone(), cdx_agent, rc, ¶ms, mcp_connection.clone(),
ctx.reverse_attach().cloned(),
&mut cont_items_cdx, &attempt.id, created,
*byok_attempt, ctx.cost_multiplier,
{
let agent_instance_hierarchy = attempt.agent_instance_hierarchy.clone();
move |items| super::Continuation::CodexSdk {
items, mcp_connection: c, agent_instance_hierarchy,
}
},
|e| super::Error::UpstreamCodexSdk(Box::new(e)),
objectiveai_sdk::agent::InlineAgentRef::CodexSdk(&cdx_agent.base),
disable_tools.clone(),
agent_transform,
make_is_cancelled(),
invention_type,
invention_step,
invention_tasks_min,
invention_input_schema.clone(),
attempt.agent_instance_hierarchy.as_str(),
attempt.agent.id(),
agent_full_id.as_str(),
agent_remote.as_ref(),
).await {
Ok(stream) => {
if !viewer { return Ok(stream); }
let vc = self.viewer_client.clone();
let vctx = ctx.clone();
let params_for_viewer = params.clone();
let mut sent_begin = false;
return Ok(Box::pin(futures::StreamExt::inspect(stream, move |item| {
if let super::StreamItem::Chunk(chunk) = item {
if !sent_begin {
sent_begin = true;
vc.send_agent_completion_begin(
vctx.clone(), chunk.id.clone(), params_for_viewer.clone(),
);
}
vc.send_agent_completion_continue(vctx.clone(), chunk.clone());
}
})));
}
Err(e) => e,
}
}
objectiveai_sdk::agent::InlineAgent::Mock(mock_agent) => {
let c = mcp_connection.clone();
let rc = match &request_continuation {
Some(objectiveai_sdk::agent::Continuation::Mock(c)) => Some(c),
_ => None,
};
match self.run_agent_loop(
self.mock.clone(), mock_agent, rc, ¶ms, mcp_connection.clone(),
ctx.reverse_attach().cloned(),
&mut cont_items_mock, &attempt.id, created,
*byok_attempt, ctx.cost_multiplier,
{
let agent_instance_hierarchy = attempt.agent_instance_hierarchy.clone();
move |items| super::Continuation::Mock {
items, mcp_connection: c, agent_instance_hierarchy,
}
},
|e| super::Error::UpstreamMock(Box::new(e)),
objectiveai_sdk::agent::InlineAgentRef::Mock(&mock_agent.base),
disable_tools.clone(),
agent_transform,
make_is_cancelled(),
invention_type,
invention_step,
invention_tasks_min,
invention_input_schema.clone(),
attempt.agent_instance_hierarchy.as_str(),
attempt.agent.id(),
agent_full_id.as_str(),
agent_remote.as_ref(),
).await {
Ok(stream) => {
if !viewer { return Ok(stream); }
let vc = self.viewer_client.clone();
let vctx = ctx.clone();
let params_for_viewer = params.clone();
let mut sent_begin = false;
return Ok(Box::pin(futures::StreamExt::inspect(stream, move |item| {
if let super::StreamItem::Chunk(chunk) = item {
if !sent_begin {
sent_begin = true;
vc.send_agent_completion_begin(
vctx.clone(), chunk.id.clone(), params_for_viewer.clone(),
);
}
vc.send_agent_completion_continue(vctx.clone(), chunk.clone());
}
})));
}
Err(e) => e,
}
}
};
errors.push(err);
}
}
// All agents failed this round — apply backoff or give up.
if errors.is_empty() {
return Err(send_viewer_err(&primary_response_id, super::Error::NoAgentsResolved));
}
use backoff::backoff::Backoff;
match backoff.next_backoff() {
Some(d) => tokio::time::sleep(d).await,
None => {
return Err(send_viewer_err(&primary_response_id, if errors.len() == 1 {
errors.into_iter().next().unwrap()
} else {
super::Error::MultipleErrors(errors)
}));
}
}
}
}
/// Creates an upstream stream and runs the tool-calling loop.
///
/// 1. Calls `upstream.create()` with `first_chunk_timeout`.
/// 2. Returns a stream that yields chunks as they arrive, executes
/// callable tools (MCP and invention), and re-invokes the upstream
/// for each continuation until no more callable tool calls remain.
/// 3. The final stream item is always `StreamItem::State(CONT)`.
///
/// On success, takes ownership of `cont_items` (via `std::mem::take`).
/// On failure, `cont_items` remains intact for BYOK retry.
async fn run_agent_loop<A, U, RC, CONT>(
&self,
upstream: Arc<U>,
agent: &A,
request_continuation: Option<&RC>,
params: &objectiveai_sdk::agent::completions::request::AgentCompletionCreateParams,
mcp_connection: Option<objectiveai_sdk::mcp::Connection>,
reverse_attach: Option<Arc<crate::objectiveai_mcp::ReverseAttachHandle>>,
cont_items: &mut Vec<super::ContinuationItem<U::State>>,
id: &str,
created: u64,
byok: Option<&str>,
cost_multiplier: rust_decimal::Decimal,
wrap_continuation: impl FnOnce(Vec<super::ContinuationItem<U::State>>) -> CONT + Send + 'static,
map_upstream_err: impl Fn(U::Error) -> super::Error + Send + 'static,
agent_base: objectiveai_sdk::agent::InlineAgentRef<'_>,
disable_tools: Option<Arc<dyn Fn() -> bool + Send + Sync>>,
transform_messages: Option<&(dyn Fn(Vec<objectiveai_sdk::agent::completions::message::Message>) -> Vec<objectiveai_sdk::agent::completions::message::Message> + Send + Sync)>,
is_cancelled: impl Fn() -> bool + Send + Sync + 'static,
invention_type: Option<objectiveai_sdk::functions::inventions::prompts::StepPromptType>,
invention_step: Option<usize>,
invention_tasks_min: Option<u64>,
invention_input_schema: Option<String>,
agent_instance_hierarchy_header: &str,
agent_id: &str,
agent_full_id: &str,
agent_remote: Option<&objectiveai_sdk::RemotePath>,
) -> Result<
Pin<Box<dyn futures::Stream<Item = super::StreamItem<CONT>> + Send>>,
super::Error,
>
where
U: super::UpstreamClient<A, RC> + Send + Sync + 'static,
A: Send + Sync + Clone + 'static,
RC: Send + Sync + Clone + Into<objectiveai_sdk::agent::Continuation> + 'static,
CONT: Send + 'static,
{
// --- Merge messages, drain proxy-queued notifications, prepare,
// and apply transform. ---
//
// `merged_messages` injects the agent's personality (system_prompt,
// prefix/suffix content, etc.) and bakes in `params.messages`. It
// must run **only on a fresh conversation**. On resumption — either
// a wire-level resume (`request_continuation.is_some()`) or an
// in-process resume (`!cont_items.is_empty()`) — the merged prefix
// is already part of the prior turn's accumulated state (e.g.
// OpenRouter's `Continuation.messages`, Claude SDK's session). Re-
// merging here would prepend a duplicate personality block on every
// turn, snowballing linearly with the conversation length.
//
// The drained-notifications user message is inserted at the FRONT
// of `messages` (index 0) so the notifications lead the prompt —
// ahead of any system / developer / user content from the caller.
// The prepare pass that follows still folds redundant consecutive
// same-role messages, and `transform_messages` sees the drained
// content like any other message. The proxy's tool-response path
// still drains in-flight notifications during a turn; this
// init-time drain covers the gap *between* turns — i.e. when the
// previous turn ended without a tool call, or when the user is
// starting a fresh continuation. On resumption with an empty merged
// prefix the drain message simply leads the new turn's content,
// landing before the continuation items in the upstream request.
let resuming = request_continuation.is_some() || !cont_items.is_empty();
let mut messages = if resuming {
Vec::new()
} else {
agent_base.merged_messages(params.messages.clone())
};
// Drain the CLI's local prompt queue via the WS reverse-
// attach. The queue lives in the CLI's postgres-backed
// `message_queue`; the API asks for its current contents
// via `read_message_queue`, joins every entry into a single
// new user turn, and saves the entry ids. The ids are then
// stamped onto the first assistant chunk yielded downstream
// via `AssistantResponseChunk.request_message_ids` — a
// signal to the consumer that those rows have been
// consumed for this turn. The consumer owns row deletion
// (no longer fires a separate `clear_message_queue` WS
// call). If the upstream stream errors before any assistant
// chunk lands, the ids drop on the floor and the queue
// stays populated — the next turn re-reads it.
//
// The CLI now returns per-row data (no cross-row separator
// splicing); we join here so the agent sees one User
// message with `\n\n` separators between consumed rows.
// The flat content-id list rides through to the first
// assistant chunk via the stamp pass below.
let mut queue_ids_to_clear: Vec<i64> = Vec::new();
if let Some(handle) = &reverse_attach {
let response = read_message_queue_via_ws(
handle,
agent_instance_hierarchy_header,
)
.await?;
if !response.rows.is_empty() {
use objectiveai_sdk::agent::completions::message::{
Message, RichContent, RichContentPart, UserMessage,
};
// Splice rows together with `"\n\n"` separators and
// flatten the content_ids. Mirrors the joining the
// CLI used to do server-side.
let mut all_parts: Vec<RichContentPart> = Vec::new();
for (i, row) in response.rows.into_iter().enumerate() {
if i > 0 {
all_parts.push(RichContentPart::Text {
text: "\n\n".to_string(),
});
}
queue_ids_to_clear.extend(row.content_ids);
match row.rich_content {
RichContent::Text(text) => {
all_parts.push(RichContentPart::Text { text });
}
RichContent::Parts(parts) => {
all_parts.extend(parts);
}
}
}
// Collapse single-text-part to RichContent::Text
// (lossless) for the same wire shape as before.
let rich_content = if all_parts.len() == 1
&& matches!(all_parts.first(), Some(RichContentPart::Text { .. }))
{
let Some(RichContentPart::Text { text }) =
all_parts.into_iter().next()
else {
unreachable!("matched single Text part above")
};
RichContent::Text(text)
} else {
RichContent::Parts(all_parts)
};
// Insert AFTER the leading system/developer chain so
// the agent sees its personality prefix first, then
// the queued content arrives as one user turn, then
// any caller-supplied content follows. On resumption
// `messages` is empty so `insert_idx == 0` and the
// queued message simply leads the new turn.
let insert_idx = messages
.iter()
.position(|m| {
!matches!(m, Message::System(_) | Message::Developer(_))
})
.unwrap_or(messages.len());
messages.insert(
insert_idx,
Message::User(UserMessage {
content: rich_content,
name: None,
}),
);
}
}
// Register with the in-process MCP-proxy queue delegate
// for the lifetime of this loop. The proxy calls
// `read_pending_blocks` on every tool response; the
// delegate routes by AIH to the per-loop state we just
// seeded with `queue_ids_to_clear` (the startup snapshot
// that we already stamped on the first assistant chunk).
// Subsequent reads filter against this confirmed set so
// the proxy never re-surfaces a row we already injected
// upfront. Registration is conditional on a
// `reverse_attach` handle being available — without one
// the delegate can't reach back to the CLI to read the
// queue, so the proxy gets a no-op when it calls in.
let delegate_guard = if let Some(handle) = &reverse_attach {
let delegate = self.proxy_spawner.queue_delegate();
delegate
.register(
agent_instance_hierarchy_header.to_string(),
handle.clone(),
queue_ids_to_clear.clone(),
)
.await;
Some(DelegateUnregisterGuard {
delegate,
aih: agent_instance_hierarchy_header.to_string(),
})
} else {
None
};
objectiveai_sdk::agent::completions::message::prompt::prepare(&mut messages);
let messages = match transform_messages {
Some(f) => f(messages),
None => messages,
};
// --- Create the initial upstream stream with timeout. ---
let cont_ref = if cont_items.is_empty() {
None
} else {
Some(cont_items.as_slice())
};
let create_fut = upstream.create(
id,
created,
agent,
request_continuation.clone(),
params,
&messages,
mcp_connection.clone(),
cont_ref,
byok,
cost_multiplier,
true,
invention_type,
invention_step,
invention_tasks_min,
invention_input_schema.clone(),
agent_instance_hierarchy_header,
agent_id,
agent_full_id,
agent_remote,
);
let initial_stream =
tokio::time::timeout(self.first_chunk_timeout, create_fut)
.await
.map_err(|_| super::Error::Timeout)?
.map_err(&map_upstream_err)?;
// Resolve the proxy's tool name set once upfront. Used to
// distinguish tool calls the orchestrator should dispatch
// (proxy-routed MCP calls, including invention tools served
// through the proxy) from tool calls the upstream encodes for
// its own reasons (response_format, etc.).
let mcp_tool_names: Option<std::collections::HashSet<String>> =
if let Some(conn) = &mcp_connection {
let tools = conn.list_tools().await.map_err(|e| super::Error::McpListTools {
url: conn.url.clone(),
error: e,
})?;
Some(tools.iter().map(|t| t.name.clone()).collect())
} else {
None
};
// Success — take ownership of continuation items and build the stream.
let mut continuation_items = std::mem::take(cont_items);
let other_chunk_timeout = self.other_chunk_timeout;
let agent = agent.clone();
let params = params.clone();
let id = id.to_string();
let byok = byok.map(|s| s.to_string());
let agent_instance_hierarchy_header = agent_instance_hierarchy_header.to_string();
let agent_id = agent_id.to_string();
let agent_full_id = agent_full_id.to_string();
let agent_remote = agent_remote.cloned();
let request_continuation = request_continuation.cloned();
// Capture into the stream. The Drop on `_delegate_guard`
// fires when the stream future is dropped (natural
// end-of-stream OR early cancel), spawning the async
// unregister — pending tokens that never got confirmed
// (and therefore weren't really delivered) drop with the
// state, so the queue rows re-issue on the next loop.
let _delegate_guard = delegate_guard;
let queue_delegate_for_stream =
self.proxy_spawner.queue_delegate();
Ok(Box::pin(async_stream::stream! {
use objectiveai_sdk::agent::completions::message::{RichContent, ToolMessage};
let mut aggregate: Option<
objectiveai_sdk::agent::completions::response::streaming::AgentCompletionChunk,
> = None;
let mut usage =
objectiveai_sdk::agent::completions::response::Usage::default();
let mut upstream_kind = objectiveai_sdk::agent::Upstream::Unknown;
let mut final_error: Option<objectiveai_sdk::error::ResponseError> = None;
let mut stream: Pin<Box<dyn futures::Stream<Item = super::StreamItem<U::State>> + Send>> =
Box::pin(initial_stream);
// In-band signal of queue consumption: stamp
// `queue_ids_to_clear` onto the first
// `MessageChunk::Assistant` we can find in an OK
// outbound chunk. The downstream consumer owns row
// deletion (no more `clear_message_queue` WS RPC).
// Until the stamp lands, the ids ride in the local
// `pending_request_message_ids` slot; if the upstream
// errors first, they fall on the floor and the queue
// stays populated for the next turn to re-read.
let mut pending_request_message_ids: Option<Vec<i64>> =
if queue_ids_to_clear.is_empty() {
None
} else {
Some(std::mem::take(&mut queue_ids_to_clear))
};
loop {
let mut current_state: Option<U::State> = None;
let mut had_error = false;
let mut pending_chunk: Option<
objectiveai_sdk::agent::completions::response::streaming::AgentCompletionChunk,
> = None;
loop {
match tokio::time::timeout(other_chunk_timeout, stream.next()).await {
Ok(Some(super::StreamItem::Chunk(chunk))) => {
// Identity (`agent_instance_hierarchy`,
// `agent_id`, `agent_full_id`, `agent_remote`)
// is stamped at the upstream-client level
// when each chunk is constructed — no need
// to re-stamp here.
// Import usage from assistant response chunks.
for msg in &chunk.messages {
if let objectiveai_sdk::agent::completions::response::streaming::MessageChunk::Assistant(asst) = msg {
if let Some(upstream_usage) = &asst.usage {
usage.push_upstream_usage(upstream_usage);
}
}
}
// Track upstream from the first chunk that sets it.
if upstream_kind == objectiveai_sdk::agent::Upstream::Unknown
&& chunk.upstream != objectiveai_sdk::agent::Upstream::Unknown
{
upstream_kind = chunk.upstream;
}
// An error chunk means the upstream failed mid-stream.
// Keep draining but prevent further continuation.
if chunk.error.is_some() {
had_error = true;
}
match &mut aggregate {
Some(agg) => agg.push(&chunk),
None => aggregate = Some(chunk.clone()),
}
// Yield the previous pending chunk (without usage),
// buffer the current one. Before yielding, walk
// its messages and stamp `request_message_ids`
// onto the first assistant chunk we can find —
// single-shot per stream lifetime.
if let Some(mut prev) = pending_chunk.replace(chunk) {
if pending_request_message_ids.is_some() && prev.error.is_none() {
for m in prev.messages.iter_mut() {
if let objectiveai_sdk::agent::completions::response::streaming::MessageChunk::Assistant(asst) = m {
asst.request_message_ids = pending_request_message_ids.take();
break;
}
}
}
yield super::StreamItem::Chunk(prev);
}
}
Ok(Some(super::StreamItem::State(state))) => {
current_state = Some(state);
break;
}
Ok(None) => break,
Err(_) => {
had_error = true;
break;
}
}
}
// Yield the last buffered chunk. Same stamp-on-
// first-assistant pass as above so the pending ids
// get a final attempt before the stream ends.
if let Some(mut last) = pending_chunk.take() {
if pending_request_message_ids.is_some() && last.error.is_none() {
for m in last.messages.iter_mut() {
if let objectiveai_sdk::agent::completions::response::streaming::MessageChunk::Assistant(asst) = m {
asst.request_message_ids = pending_request_message_ids.take();
break;
}
}
}
yield super::StreamItem::Chunk(last);
}
// Push the upstream state (carries SDK session_id) onto the
// continuation BEFORE the early-exit branches. Without this,
// a turn that ends without calling any tools — common when the
// agent emits free-form text — drops the session_id, so the
// next call (e.g. invention's retry-with-error prompt) opens
// a fresh SDK session and the agent loses memory of the prior
// turn.
if let Some(state) = current_state.take() {
continuation_items.push(super::ContinuationItem::State(state));
}
if had_error || is_cancelled() {
break;
}
let Some(ref agg) = aggregate else { break };
let callable =
extract_callable_tool_calls(agg, mcp_tool_names.as_ref());
if callable.is_empty() {
break;
}
// TODO: return to concurrent dispatch (`join_all`) once
// we have a way to keep the per-call response order
// deterministic. The blocker is invention tools that
// mutate shared state and return order-sensitive values
// (`AppendTask` returns the new tasks length, etc.):
// when those run in parallel, the tokio scheduler
// decides which acquires the state mutex first,
// shuffling each call's return value and propagating
// through to the next step's prompt-id-derived mock
// seed. Possible avenues — server-side ordering by
// call_id, idempotent-only tools, agent-side
// parallel-then-canonicalize — all need design.
//
// Dispatch tool calls in this turn SEQUENTIALLY in the
// meantime. We used to `join_all` for latency, but
// serialising fixes the race by construction. The
// proxy's per-call latency dominates anyway (the
// InventionServer's session worker is a single-event
// loop, so it would have serialised them server-side
// regardless).
let conn = mcp_connection
.as_ref()
.expect("callable extraction returns empty without a connection")
.clone();
let mut results = Vec::with_capacity(callable.len());
for (call_id, name, args) in &callable {
let arguments: Option<
indexmap::IndexMap<String, serde_json::Value>,
> = serde_json::from_str(args).ok();
let res = conn
.call_tool_as_message(
&objectiveai_sdk::mcp::tool::CallToolRequestParams {
name: name.clone(),
arguments,
_meta: None,
task: None,
},
call_id.clone(),
)
.await;
results.push(res);
}
let mut any_invention_tool_called = false;
for result in results {
match result {
Ok(tool_msg) => {
let idx = continuation_items.len() as u64;
// Regex-scan the tool message for the
// proxy's confirmation prefix. Each
// captured token resolves to the IDs the
// delegate speculatively issued in
// that batch; `confirm()` promotes them
// from pending→confirmed and we stamp
// them onto the ToolResponse so the
// downstream LogWriter logs the
// delivery via `MessageQueueContent` row
// writes. Tokens we never see (proxy
// didn't append a prefix this turn,
// tool message truncated, etc.) stay
// pending until `unregister` drops
// them — those rows re-deliver next
// loop.
let request_message_ids = scan_and_confirm_tokens(
&queue_delegate_for_stream,
&agent_instance_hierarchy_header,
&tool_msg.content,
)
.await;
let chunk = make_tool_chunk(
&id,
&agent_instance_hierarchy_header,
&agent_id,
&agent_full_id,
agent_remote.as_ref(),
created,
upstream_kind,
idx,
&tool_msg,
request_message_ids,
);
if let Some(ref mut agg) = aggregate {
agg.push(&chunk);
}
yield super::StreamItem::Chunk(chunk);
continuation_items
.push(super::ContinuationItem::ToolMessage(tool_msg));
}
Err(_) => {
had_error = true;
break;
}
}
}
// `disable_tools` is the sentinel the orchestrator hands
// us to signal "the model has produced what we needed;
// run the next continuation with tools off so it closes
// out with a free-form response". We can't tell from
// here whether any of the tools we just dispatched were
// invention tools (the proxy hides that), so we let the
// sentinel decide on its own.
let _ = &any_invention_tool_called;
let tools_enabled = !disable_tools.as_ref().is_some_and(|f| f());
if had_error {
break;
}
// Reset aggregate so the next iteration doesn't carry
// old tool calls forward from the previous response.
aggregate = None;
let _ = (&invention_type, &invention_step, &invention_tasks_min);
match upstream
.create(
&id,
created,
&agent,
request_continuation.as_ref(),
¶ms,
&messages,
mcp_connection.clone(),
Some(&continuation_items),
byok.as_deref(),
cost_multiplier,
tools_enabled,
invention_type,
invention_step,
invention_tasks_min,
invention_input_schema.clone(),
agent_instance_hierarchy_header.as_str(),
agent_id.as_str(),
agent_full_id.as_str(),
agent_remote.as_ref(),
)
.await
{
Ok(new_stream) => {
stream = Box::pin(new_stream);
}
Err(e) => {
use objectiveai_sdk::error::StatusError;
let e = map_upstream_err(e);
final_error = Some(objectiveai_sdk::error::ResponseError {
code: e.status(),
message: e.message().unwrap_or(serde_json::Value::Null),
});
break;
}
}
}
// Build MCP sessions map. With the per-agent proxy connection,
// this is at most one entry: `proxy_url → agent_session_id`.
let mcp_sessions: IndexMap<String, String> = mcp_connection
.as_ref()
.map(|c| {
let mut m = IndexMap::new();
m.insert(c.url.clone(), c.session_id.clone());
m
})
.unwrap_or_default();
// Build response continuation token. The upstream stamps
// `agent_instance_hierarchy` on the returned continuation
// itself — no post-stamp from the orchestrator.
let response_cont = upstream.response_continuation(
mcp_sessions,
request_continuation.as_ref(),
&messages,
Some(&continuation_items),
&agent_instance_hierarchy_header,
);
let continuation_token: objectiveai_sdk::agent::Continuation = response_cont.into();
let continuation_token = continuation_token.to_string();
// Set cancellation error if the stream was cancelled.
if is_cancelled() && final_error.is_none() {
final_error = Some(objectiveai_sdk::error::ResponseError::from(
&super::Error::StreamCancelled,
));
}
// Peek the proxy's pending-notifications queue so the
// caller can tell whether a follow-up continuation would
// surface queued blocks. Only meaningful when a
// continuation is also being returned — this site always
// sets `continuation: Some(_)`, so the peek is
// unconditionally relevant here. A peek failure is
// surfaced via the chunk's `error` field only if no prior
// error already occupies it (the earlier failure is the
// more important signal).
//
// With the CLI→API notify path removed there's nothing to
// serialize against here — the read of the proxy queue is
// straight, no lock needed. `messages_queued` simply
// initializes to `None` and is populated from the peek
// below.
let mut messages_queued: Option<bool> = None;
if let Some(conn) = &mcp_connection {
match conn.has_pending_notifications().await {
Ok(true) => messages_queued = Some(true),
Ok(false) => {}
Err(error) => {
if final_error.is_none() {
final_error = Some(
objectiveai_sdk::error::ResponseError::from(
&super::Error::McpQueuedNotifications {
url: conn.url.clone(),
error,
},
),
);
}
}
}
}
// Single site for usage, continuation, and error (if a continuation call failed).
yield super::StreamItem::Chunk(
objectiveai_sdk::agent::completions::response::streaming::AgentCompletionChunk {
id: id.clone(),
agent_instance_hierarchy: agent_instance_hierarchy_header.clone(),
agent_id: agent_id.clone(),
agent_full_id: agent_full_id.clone(),
agent_remote: agent_remote.clone(),
created,
upstream: upstream_kind,
usage: Some(usage),
error: final_error,
continuation: Some(continuation_token),
messages_queued,
..Default::default()
},
);
let cont = wrap_continuation(continuation_items);
yield super::StreamItem::State(cont);
}))
}
}
/// Resolves the response format for a given agent from the request params.
fn resolve_response_format(
agent_instance_hierarchy: &str,
params: &objectiveai_sdk::agent::completions::request::AgentCompletionCreateParams,
) -> Option<objectiveai_sdk::agent::completions::request::ResponseFormat> {
use objectiveai_sdk::agent::completions::request::ResponseFormatParam;
match params.response_format.as_ref()? {
ResponseFormatParam::Single(rf) => Some(rf.clone()),
ResponseFormatParam::PerAgent(map) => map.get(agent_instance_hierarchy).cloned(),
}
}
/// Extracts callable tool calls from the last assistant message.
///
/// `callable_names` is the set of tool names the proxy connection
/// advertises (or `None` when the agent has no MCP work to do — in
/// which case nothing is callable). Tool calls whose names aren't in
/// the set are response-format / hallucinated-tool calls that the
/// orchestrator should leave alone, so the loop terminates.
///
/// Returns `(call_id, tool_name, arguments_json)` for each callable
/// tool, in the order the assistant emitted them.
fn extract_callable_tool_calls(
aggregate: &objectiveai_sdk::agent::completions::response::streaming::AgentCompletionChunk,
callable_names: Option<&std::collections::HashSet<String>>,
) -> Vec<(String, String, String)> {
use objectiveai_sdk::agent::completions::response::streaming::MessageChunk;
let mut callable = Vec::new();
let Some(names) = callable_names else { return callable };
for msg in aggregate.messages.iter().rev() {
if let MessageChunk::Assistant(chunk) = msg {
if let Some(tool_calls) = &chunk.tool_calls {
for tc in tool_calls {
let id = tc.id.clone().unwrap_or_default();
let name = tc
.function
.as_ref()
.and_then(|f| f.name.clone())
.unwrap_or_default();
let args = tc
.function
.as_ref()
.and_then(|f| f.arguments.clone())
.unwrap_or_default();
if name.is_empty() {
continue;
}
if names.contains(&name) {
callable.push((id, name, args));
}
}
}
break;
}
}
callable
}
/// Wrap MCP `ContentBlock`s drained from the proxy at agent init time
/// into a `UserMessage`.
///
/// The blocks are presented to the model as a plain user turn — no
/// `<system-reminder>` wrapper. (The wrapper is reserved for the
/// proxy's tool-response drain path, where notifications surface
/// mid-turn and need the "while you were working" framing.) Init-time
/// notifications are semantically a user message that arrived between
/// turns, so they take the user-message shape directly.
///
/// Delegates to the SDK's [`From<Vec<ContentBlock>> for RichContent`]
/// impl — same mapping (text / image-data-URL / audio direct;
/// `ResourceLink` / `EmbeddedResource` → JSON text), same collapse
/// of all-text inputs into a single `RichContent::Text`. Empty input
/// is the caller's responsibility.
fn build_drain_user_message(
blocks: Vec<objectiveai_sdk::mcp::tool::ContentBlock>,
) -> objectiveai_sdk::agent::completions::message::UserMessage {
objectiveai_sdk::agent::completions::message::UserMessage {
content: blocks.into(),
name: None,
}
}
/// Time budget for one `read_message_queue` / `clear_message_queue`
/// round-trip over the WS reverse-attach. Matches
/// [`crate::objectiveai_mcp::handlers::FORWARD_TIMEOUT`]'s shape: long
/// enough that a healthy CLI always answers in time, short enough
/// that a wedged WS doesn't stall the agent loop indefinitely.
const MESSAGE_QUEUE_WS_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(30);
/// Issue a `ReadMessageQueue` server-request over the WS reverse-
/// attach and return the joined `(rich_content, ids)` payload.
///
/// The CLI side joins every queued entry into one RichContent
/// (with `"\n\n"` separators between entries) and returns the
/// content-id refs (id + kind) for every consumed
/// `message_queue_contents` row. The envelope carries no
/// `mcp_kind` and the headers map is empty. Failures (channel
/// closed, dropped, timed out, or CLI-side JSON-RPC error) collapse
/// to [`super::Error::MessageQueueRead`].
async fn read_message_queue_via_ws(
handle: &std::sync::Arc<crate::objectiveai_mcp::ReverseAttachHandle>,
agent_instance_hierarchy: &str,
) -> Result<
objectiveai_sdk::client_objectiveai_mcp::server_response::ReadMessageQueueResult,
super::Error,
> {
use objectiveai_sdk::client_objectiveai_mcp::{server_request, server_response};
let rc = handle.channel();
let request = server_request::Request {
id: uuid::Uuid::new_v4().to_string(),
headers: indexmap::IndexMap::new(),
payload: server_request::Payload::ReadMessageQueue(
server_request::ReadMessageQueueRequest {
agent_instance_hierarchy: agent_instance_hierarchy.to_string(),
},
),
};
let rx = crate::objectiveai_mcp::send_server_request(&rc.sink, &rc.pending, request)
.await
.map_err(|()| super::Error::MessageQueueRead("reverse channel closed".to_string()))?;
let response = match tokio::time::timeout(MESSAGE_QUEUE_WS_TIMEOUT, rx).await {
Ok(Ok(response)) => response,
Ok(Err(_)) => {
return Err(super::Error::MessageQueueRead(
"reverse channel dropped before reply".to_string(),
));
}
Err(_) => {
return Err(super::Error::MessageQueueRead(
"reverse channel timed out".to_string(),
));
}
};
match response.payload {
server_response::Payload::ReadMessageQueue(server_response::JsonRpcResult::Ok {
result,
}) => Ok(result),
server_response::Payload::ReadMessageQueue(server_response::JsonRpcResult::Err {
code,
message,
..
}) => Err(super::Error::MessageQueueRead(format!(
"CLI returned JSON-RPC error {code}: {message}"
))),
other => Err(super::Error::MessageQueueRead(format!(
"CLI returned wrong variant: {other:?}"
))),
}
}
/// Builds an `AgentCompletionChunk` containing a single
/// tool-response message. `request_message_ids` is `Some` when
/// the run-loop's prefix-token scan over `tool_msg.content`
/// confirmed delivery for one or more `message_queue_contents.id`s
/// (the `ApiQueueDelegate::confirm` return value); `None` otherwise.
#[allow(clippy::too_many_arguments)]
fn make_tool_chunk(
id: &str,
agent_instance_hierarchy: &str,
agent_id: &str,
agent_full_id: &str,
agent_remote: Option<&objectiveai_sdk::RemotePath>,
created: u64,
upstream: objectiveai_sdk::agent::Upstream,
index: u64,
tool_msg: &objectiveai_sdk::agent::completions::message::ToolMessage,
request_message_ids: Option<Vec<i64>>,
) -> objectiveai_sdk::agent::completions::response::streaming::AgentCompletionChunk {
use objectiveai_sdk::agent::completions::response::streaming::{
AgentCompletionChunk, MessageChunk,
};
use objectiveai_sdk::agent::completions::response::ToolResponse;
AgentCompletionChunk {
id: id.to_string(),
agent_instance_hierarchy: agent_instance_hierarchy.to_string(),
agent_id: agent_id.to_string(),
agent_full_id: agent_full_id.to_string(),
agent_remote: agent_remote.cloned(),
created,
upstream,
messages: vec![MessageChunk::Tool(ToolResponse {
role: Default::default(),
index,
inner: tool_msg.clone(),
request_message_ids,
})],
..Default::default()
}
}
/// Drop-fires an async `unregister` on the embedded queue
/// delegate when the agent loop's stream future drops. Pending
/// tokens that never got confirmed go with it; their ids
/// re-deliver on the next loop.
struct DelegateUnregisterGuard {
delegate: std::sync::Arc<super::ApiQueueDelegate>,
aih: String,
}
impl Drop for DelegateUnregisterGuard {
fn drop(&mut self) {
let delegate = self.delegate.clone();
let aih = std::mem::take(&mut self.aih);
tokio::spawn(async move {
delegate.unregister(&aih).await;
});
}
}
/// Scan every text part of a `ToolMessage`'s content for the
/// `<system-reminder>` confirmation prefix the MCP proxy
/// embedded (one token per delegate batch), call
/// `ApiQueueDelegate::confirm` on each captured token, and
/// concatenate the returned ids in scan order. Returns `None`
/// when nothing matched or no delegate is wired in.
async fn scan_and_confirm_tokens(
delegate: &super::ApiQueueDelegate,
aih: &str,
content: &objectiveai_sdk::agent::completions::message::RichContent,
) -> Option<Vec<i64>> {
use objectiveai_sdk::agent::completions::message::{RichContent, RichContentPart};
let texts: Vec<&str> = match content {
RichContent::Text(t) => vec![t.as_str()],
RichContent::Parts(parts) => parts
.iter()
.filter_map(|p| match p {
RichContentPart::Text { text } => Some(text.as_str()),
_ => None,
})
.collect(),
};
let mut all_ids: Vec<i64> = Vec::new();
for text in texts {
for token in
objectiveai_sdk::mcp::queue_notification::extract_tokens(text)
{
let ids = delegate.confirm(aih, &token).await;
all_ids.extend(ids);
}
}
if all_ids.is_empty() { None } else { Some(all_ids) }
}
/// Percent-encode a single path segment for the synthetic per-MCP
/// URLs in `X-MCP-Servers`. Mirrors RFC 3986's `pchar` minus the
/// sub-delims we want literal; deliberately strict so that
/// `owner`/`name`/`version`/`mcp` values containing `/` `?` `#` `&`
/// `=` get encoded and the API's `Path` extractor sees exactly four
/// segments.
fn percent_encode_segment(s: &str) -> String {
/// `unreserved` + `:@` (the path-segment-internal `pchar` set,
/// minus sub-delims to keep `&` `=` etc out of segments).
const SEGMENT: &percent_encoding::AsciiSet =
&percent_encoding::NON_ALPHANUMERIC
.remove(b'-')
.remove(b'.')
.remove(b'_')
.remove(b'~')
.remove(b':')
.remove(b'@');
percent_encoding::utf8_percent_encode(s, SEGMENT).to_string()
}
#[cfg(test)]
mod build_drain_user_message_tests {
use super::build_drain_user_message;
use objectiveai_sdk::agent::completions::message::{
RichContent, RichContentPart,
};
use objectiveai_sdk::mcp::tool::{
AudioContent, ContentBlock, ImageContent, TextContent,
};
/// All-text input collapses to a single `RichContent::Text` joined
/// with `\n\n` between blocks. No `<system-reminder>` wrapper —
/// that's reserved for the proxy's tool-response drain path.
#[test]
fn text_only_blocks_collapse_to_joined_text() {
let msg = build_drain_user_message(vec![
ContentBlock::Text(TextContent {
text: "hello".into(),
annotations: None,
_meta: None,
}),
ContentBlock::Text(TextContent {
text: "world".into(),
annotations: None,
_meta: None,
}),
]);
assert_eq!(msg.name, None);
match msg.content {
RichContent::Text(s) => assert_eq!(s, "hello\n\nworld"),
other => panic!("expected RichContent::Text, got {other:?}"),
}
}
/// A single text block becomes a plain `Text` (no `Parts` wrapper).
#[test]
fn single_text_block_is_plain_text() {
let msg = build_drain_user_message(vec![ContentBlock::Text(TextContent {
text: "just one".into(),
annotations: None,
_meta: None,
})]);
match msg.content {
RichContent::Text(s) => assert_eq!(s, "just one"),
other => panic!("expected RichContent::Text, got {other:?}"),
}
}
/// Mixed content (text + image) stays as `Parts` since the image
/// has to ride alongside the text in a multimodal-aware shape.
#[test]
fn mixed_content_becomes_parts() {
let msg = build_drain_user_message(vec![
ContentBlock::Text(TextContent {
text: "look at this".into(),
annotations: None,
_meta: None,
}),
ContentBlock::Image(ImageContent {
data: "BASE64DATA".into(),
mime_type: "image/png".into(),
annotations: None,
_meta: None,
}),
]);
match msg.content {
RichContent::Parts(parts) => {
assert_eq!(parts.len(), 2);
match &parts[0] {
RichContentPart::Text { text } => assert_eq!(text, "look at this"),
other => panic!("expected text part, got {other:?}"),
}
match &parts[1] {
RichContentPart::ImageUrl { image_url } => {
assert_eq!(image_url.url, "data:image/png;base64,BASE64DATA");
}
other => panic!("expected image part, got {other:?}"),
}
}
other => panic!("expected RichContent::Parts, got {other:?}"),
}
}
/// Audio block round-trips through `InputAudio` part.
#[test]
fn audio_block_becomes_input_audio_part() {
let msg = build_drain_user_message(vec![ContentBlock::Audio(AudioContent {
data: "AUDIO".into(),
mime_type: "audio/wav".into(),
annotations: None,
_meta: None,
})]);
match msg.content {
RichContent::Parts(parts) => {
assert_eq!(parts.len(), 1);
match &parts[0] {
RichContentPart::InputAudio { input_audio } => {
assert_eq!(input_audio.data, "AUDIO");
assert_eq!(input_audio.format, "audio/wav");
}
other => panic!("expected input_audio part, got {other:?}"),
}
}
other => panic!("expected RichContent::Parts, got {other:?}"),
}
}
}