honcho-ai 0.2.1

Rust SDK for Honcho — AI agent memory and social cognition infrastructure
Documentation
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
//! Session wrapper — construction, metadata, peer management, per-peer config.

use std::collections::HashMap;
use std::sync::{Arc, RwLock};

use chrono::{DateTime, Utc};
use reqwest::Method;
use reqwest::multipart::Form;
use serde_json::Value;

use crate::error::{HonchoError, Result};
use crate::http::client::HttpClient;
use crate::http::routes;
use crate::message::Message;
use crate::types::message::MessageResponse;
use crate::types::session::SessionResponse;
use crate::types::session::{
    SessionConfiguration, SessionConfigurationSet, SessionPeerConfig, SessionUpdate,
};
use crate::upload::FileSource;

/// Single-lock cache of the server-owned session state.
///
/// Wrapping all mutable fields in one [`RwLock`] keeps them consistent: a
/// refresh/PUT response updates every field under one write, so readers never
/// observe a torn mix of fresh and stale fields.
#[derive(Default)]
struct SessionCacheState {
    metadata: Option<HashMap<String, Value>>,
    configuration: Option<SessionConfiguration>,
    is_active: bool,
}

pub(crate) struct SessionInner {
    http: HttpClient,
    // Stored as `Arc<str>` so per-Message/Peer/Session clones are refcount bumps
    // rather than allocations. Public method signatures keep taking `String`, so
    // boundary conversions still allocate; this only removes the internal churn.
    workspace_id: Arc<str>,
    id: String,
    cache: RwLock<SessionCacheState>,
    created_at: DateTime<Utc>,
}

impl SessionInner {
    /// Acquire the cache read lock, recovering from poisoning instead of panicking.
    fn read_lock(&self) -> std::sync::RwLockReadGuard<'_, SessionCacheState> {
        self.cache
            .read()
            .unwrap_or_else(std::sync::PoisonError::into_inner)
    }

    /// Acquire the cache write lock, recovering from poisoning instead of panicking.
    fn write_lock(&self) -> std::sync::RwLockWriteGuard<'_, SessionCacheState> {
        self.cache
            .write()
            .unwrap_or_else(std::sync::PoisonError::into_inner)
    }

    /// Atomically replace every cached field from a fresh server response.
    fn update_cache(&self, resp: &SessionResponse) {
        let mut cache = self.write_lock();
        cache.metadata = Some(resp.metadata.clone());
        cache.configuration = Some(resp.configuration.clone());
        cache.is_active = resp.is_active;
    }
}

/// A session in a Honcho workspace.
///
/// Wraps the API response and provides methods for metadata, configuration,
/// peer management, messages, and more.
#[derive(Clone)]
pub struct Session {
    inner: Arc<SessionInner>,
}

/// Specification for adding/setting peers on a session.
///
/// Use [`PeerSpec::Id`] for a bare peer ID or [`PeerSpec::WithConfig`] to
/// include per-peer observation settings.
#[non_exhaustive]
#[derive(Debug, Clone)]
pub enum PeerSpec {
    /// A peer identified by ID with no explicit config.
    Id(String),
    /// A peer identified by ID with per-session configuration.
    WithConfig(String, SessionPeerConfig),
}

impl From<&str> for PeerSpec {
    fn from(s: &str) -> Self {
        Self::Id(s.to_owned())
    }
}

impl From<String> for PeerSpec {
    fn from(s: String) -> Self {
        Self::Id(s)
    }
}

impl From<&crate::Peer> for PeerSpec {
    fn from(p: &crate::Peer) -> Self {
        Self::Id(p.id().to_owned())
    }
}

impl From<crate::Peer> for PeerSpec {
    fn from(p: crate::Peer) -> Self {
        Self::Id(p.id().to_owned())
    }
}

impl From<(String, SessionPeerConfig)> for PeerSpec {
    fn from((id, cfg): (String, SessionPeerConfig)) -> Self {
        Self::WithConfig(id, cfg)
    }
}

impl From<(&str, SessionPeerConfig)> for PeerSpec {
    fn from((id, cfg): (&str, SessionPeerConfig)) -> Self {
        Self::WithConfig(id.to_owned(), cfg)
    }
}

impl From<(&crate::Peer, SessionPeerConfig)> for PeerSpec {
    fn from((p, cfg): (&crate::Peer, SessionPeerConfig)) -> Self {
        Self::WithConfig(p.id().to_owned(), cfg)
    }
}

impl PeerSpec {
    /// Decompose into `(peer_id, config)`.
    ///
    /// The bare-ID variant ([`PeerSpec::Id`]) yields a default
    /// [`SessionPeerConfig`] (all observation settings unset), so callers can
    /// treat both variants uniformly without re-matching.
    #[must_use]
    pub fn into_parts(self) -> (String, SessionPeerConfig) {
        match self {
            Self::Id(id) => (id, SessionPeerConfig::default()),
            Self::WithConfig(id, cfg) => (id, cfg),
        }
    }
}

/// Builder for the file-upload operation returned by [`Session::upload_file`].
///
/// Call `.peer(id)` (required) and optionally chain `.metadata()`,
/// `.configuration()`, `.created_at()` before calling `.send()`.
#[must_use]
pub struct UploadFileBuilder<'a> {
    session: &'a Session,
    source: Option<FileSource>,
    peer_id: Option<String>,
    metadata: Option<Value>,
    configuration: Option<Value>,
    created_at: Option<DateTime<Utc>>,
}

fn serialize_upload_fields(
    builder: &UploadFileBuilder<'_>,
) -> Result<impl Fn(Form) -> Form + Clone + Send + 'static> {
    let metadata_text = builder
        .metadata
        .as_ref()
        .map(|md| {
            serde_json::to_string(md).map_err(|e| HonchoError::Serialization {
                path: "MessageUploadFormMetadata".into(),
                source: e,
            })
        })
        .transpose()?;

    let configuration_text = builder
        .configuration
        .as_ref()
        .map(|cfg| {
            serde_json::to_string(cfg).map_err(|e| HonchoError::Serialization {
                path: "MessageUploadFormConfiguration".into(),
                source: e,
            })
        })
        .transpose()?;

    let created_at_text = builder.created_at.map(|dt| dt.to_rfc3339());

    Ok(move |mut form: Form| -> Form {
        if let Some(ref md) = metadata_text {
            form = form.text("metadata", md.clone());
        }
        if let Some(ref cfg) = configuration_text {
            form = form.text("configuration", cfg.clone());
        }
        if let Some(ref dt) = created_at_text {
            form = form.text("created_at", dt.clone());
        }
        form
    })
}

/// Derive the multipart filename for a `Path` upload, rejecting paths with no
/// final file-name component.
///
/// A bare root (`/`) or a trailing `..` has no file name; uploading it would
/// send an empty/absent filename. Surfacing it as [`HonchoError::Validation`]
/// here lets the production upload path fail fast before any request is made.
fn derive_path_filename(path: &std::path::Path) -> Result<String> {
    path.file_name()
        .map(|n| n.to_string_lossy().into_owned())
        .filter(|n| !n.is_empty())
        .ok_or_else(|| {
            HonchoError::Validation(format!(
                "file source path has no file name component: {}",
                path.display()
            ))
        })
}

/// Build a multipart form for an in-memory (`Bytes`) upload payload.
///
/// The payload is a [`bytes::Bytes`], so each retry clones it as a cheap
/// refcount bump rather than copying the buffer. An invalid `content_type`
/// is surfaced as [`HonchoError::Validation`] instead of being silently dropped.
fn build_form(
    filename: String,
    bytes: bytes::Bytes,
    content_type: &str,
    peer_id: String,
    add_text_fields: impl Fn(Form) -> Form,
) -> Result<Form> {
    let mut headers = reqwest::header::HeaderMap::new();
    let value = reqwest::header::HeaderValue::from_str(content_type)
        .map_err(|_| HonchoError::Validation("invalid content_type".into()))?;
    headers.insert(reqwest::header::CONTENT_TYPE, value);

    let file_part = reqwest::multipart::Part::stream(reqwest::Body::from(bytes))
        .file_name(filename)
        .headers(headers);
    let form = Form::new().part("file", file_part).text("peer_id", peer_id);
    Ok(add_text_fields(form))
}

impl UploadFileBuilder<'_> {
    /// Set the peer that owns the uploaded file (required).
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # fn example(session: &honcho_ai::Session) {
    /// let _builder = session.upload_file(honcho_ai::FileSource::bytes("f.txt", b"data", "text/plain")).peer("alice");
    /// # }
    /// ```
    pub fn peer(mut self, id: impl Into<String>) -> Self {
        self.peer_id = Some(id.into());
        self
    }

    /// Attach arbitrary JSON metadata to the created message(s).
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # fn example(session: &honcho_ai::Session) {
    /// let _builder = session.upload_file(honcho_ai::FileSource::bytes("f.txt", b"data", "text/plain"))
    ///     .peer("alice")
    ///     .metadata(serde_json::json!({"source": "upload"}));
    /// # }
    /// ```
    pub fn metadata(mut self, value: Value) -> Self {
        self.metadata = Some(value);
        self
    }

    /// Attach configuration to the created message(s).
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # fn example(session: &honcho_ai::Session) {
    /// let _builder = session.upload_file(honcho_ai::FileSource::bytes("f.txt", b"data", "text/plain"))
    ///     .peer("alice")
    ///     .configuration(serde_json::json!({"reasoning": true}));
    /// # }
    /// ```
    pub fn configuration(mut self, value: Value) -> Self {
        self.configuration = Some(value);
        self
    }

    /// Override the creation timestamp (RFC 3339).
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # fn example(session: &honcho_ai::Session) {
    /// let _builder = session.upload_file(honcho_ai::FileSource::bytes("f.txt", b"data", "text/plain"))
    ///     .peer("alice")
    ///     .created_at(chrono::Utc::now());
    /// # }
    /// ```
    pub fn created_at(mut self, dt: DateTime<Utc>) -> Self {
        self.created_at = Some(dt);
        self
    }

    /// Resolve the file source, build the multipart form, POST, and return
    /// the created messages.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # async fn example(session: &honcho_ai::Session) -> honcho_ai::error::Result<()> {
    /// let msgs = session
    ///     .upload_file(honcho_ai::FileSource::bytes("doc.pdf", b"data", "application/pdf"))
    ///     .peer("alice")
    ///     .send()
    ///     .await?;
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # Errors
    ///
    /// Returns [`HonchoError::Validation`] if no peer was set via `.peer()`.
    #[cfg_attr(
        feature = "tracing",
        tracing::instrument(skip(self), name = "upload_file_send")
    )]
    pub async fn send(self) -> Result<Vec<crate::Message>> {
        // Local items are hoisted to the top of the scope (items-after-statements).
        // `Resolved` normalizes the source to a single in-memory representation so
        // the form-building match has only two arms; `FormFactory` is the per-retry
        // form builder (see the usage sites below for the rationale).
        enum Resolved {
            Path {
                path: std::path::PathBuf,
                filename: String,
            },
            Bytes {
                filename: String,
                bytes: bytes::Bytes,
                content_type: String,
            },
        }
        type FormFactory = Box<
            dyn Fn() -> std::pin::Pin<
                    Box<dyn std::future::Future<Output = Result<Form>> + Send + 'static>,
                > + Send
                + 'static,
        >;

        let add_text_fields = serialize_upload_fields(&self)?;

        let Some(peer_id) = self.peer_id else {
            return Err(HonchoError::Validation("peer_id is required".into()));
        };
        let Some(source) = self.source else {
            return Err(HonchoError::Validation("file source is required".into()));
        };

        // Normalize the source to a single in-memory representation: `Stream` is
        // buffered into `Bytes` here so the form-building match has only two arms
        // (disk-streamed `Path` vs. in-memory `Bytes`). The `Bytes` payload makes
        // per-retry clones cheap refcount bumps instead of full buffer copies.
        let resolved = match source {
            FileSource::Path(path) => {
                // Derive (and validate) the multipart filename up front so an
                // unnamed path fails fast before any request is attempted.
                let filename = derive_path_filename(&path)?;
                Resolved::Path { path, filename }
            }
            FileSource::Bytes {
                filename,
                bytes,
                content_type,
            } => Resolved::Bytes {
                filename,
                // `FileSource::Bytes` carries a `Vec<u8>` (kept non-breaking in the
                // public API). Convert it to `bytes::Bytes` exactly once here, before
                // the retry closure is built, so each retry clones a cheap refcount
                // handle (`Bytes::clone`) instead of copying the whole buffer.
                bytes: bytes::Bytes::from(bytes),
                content_type,
            },
            FileSource::Stream {
                filename,
                mut reader,
                content_type,
            } => {
                let mut buf = Vec::new();
                tokio::io::AsyncReadExt::read_to_end(&mut reader, &mut buf)
                    .await
                    .map_err(HonchoError::from)?;
                Resolved::Bytes {
                    filename,
                    bytes: bytes::Bytes::from(buf),
                    content_type,
                }
            }
        };

        // FileSource::Path — Part::file() streams from disk, re-opens on retry.
        // Resolved::Bytes — in-memory payload, cheap to clone per retry.
        let form_factory: FormFactory = match resolved {
            Resolved::Path { path, filename } => Box::new(move || {
                let path = path.clone();
                let filename = filename.clone();
                let peer_id = peer_id.clone();
                let add_text_fields = add_text_fields.clone();
                Box::pin(async move {
                    // `Part::file` streams the body straight from disk (re-opened
                    // on each retry) — the file is never buffered into memory.
                    let file_part = reqwest::multipart::Part::file(&path)
                        .await
                        .map_err(HonchoError::from)?
                        .file_name(filename);
                    let form = Form::new().part("file", file_part).text("peer_id", peer_id);
                    Ok(add_text_fields(form))
                })
            }),
            Resolved::Bytes {
                filename,
                bytes,
                content_type,
            } => Box::new(move || {
                let filename = filename.clone();
                let bytes = bytes.clone();
                let content_type = content_type.clone();
                let peer_id = peer_id.clone();
                let add_text_fields = add_text_fields.clone();
                Box::pin(async move {
                    build_form(filename, bytes, &content_type, peer_id, add_text_fields)
                })
            }),
        };

        let route =
            routes::messages_upload(&self.session.inner.workspace_id, &self.session.inner.id)?;

        let responses: Vec<MessageResponse> = self
            .session
            .inner
            .http
            .post_multipart(&route, form_factory, &[])
            .await?;

        Ok(responses
            .into_iter()
            .map(crate::Message::from_raw)
            .collect())
    }
}

impl Session {
    pub(crate) fn from_parts(
        http: HttpClient,
        workspace_id: String,
        resp: SessionResponse,
    ) -> Self {
        Self {
            inner: Arc::new(SessionInner {
                http,
                workspace_id: Arc::from(workspace_id),
                id: resp.id,
                cache: RwLock::new(SessionCacheState {
                    metadata: Some(resp.metadata),
                    configuration: Some(resp.configuration),
                    is_active: resp.is_active,
                }),
                created_at: resp.created_at,
            }),
        }
    }

    pub(crate) fn from_response(honcho: &crate::Honcho, resp: SessionResponse) -> Self {
        Self::from_parts(
            honcho.http().clone(),
            honcho.workspace_id().to_owned(),
            resp,
        )
    }

    /// The session's unique identifier.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # fn example(session: &honcho_ai::Session) {
    /// println!("{}", session.id());
    /// # }
    /// ```
    #[must_use]
    pub fn id(&self) -> &str {
        &self.inner.id
    }

    /// Whether the session is currently active.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # fn example(session: &honcho_ai::Session) {
    /// if session.is_active() {
    ///     println!("session is active");
    /// }
    /// # }
    /// ```
    #[must_use]
    pub fn is_active(&self) -> bool {
        self.inner.read_lock().is_active
    }

    /// Cached metadata from the last API response.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # fn example(session: &honcho_ai::Session) {
    /// if let Some(meta) = session.metadata() {
    ///     println!("{meta:?}");
    /// }
    /// # }
    /// ```
    #[must_use]
    pub fn metadata(&self) -> Option<HashMap<String, Value>> {
        self.inner.read_lock().metadata.clone()
    }

    /// Cached configuration from the last API response.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # fn example(session: &honcho_ai::Session) {
    /// if let Some(config) = session.configuration() {
    ///     println!("{config:?}");
    /// }
    /// # }
    /// ```
    #[must_use]
    pub fn configuration(&self) -> Option<SessionConfiguration> {
        self.inner.read_lock().configuration.clone()
    }

    /// When the session was created.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # fn example(session: &honcho_ai::Session) {
    /// println!("{}", session.created_at());
    /// # }
    /// ```
    #[must_use]
    pub fn created_at(&self) -> DateTime<Utc> {
        self.inner.created_at
    }

    // ── F6.1: Refresh / Metadata / Configuration CRUD ──────────────────

    /// Refresh the session's cached metadata and configuration from the server.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # async fn example(session: &honcho_ai::Session) -> honcho_ai::error::Result<()> {
    /// session.refresh().await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn refresh(&self) -> Result<()> {
        self.refresh_into().await?;
        Ok(())
    }

    /// Re-fetch the current session state, refresh the cache, and return the
    /// response.
    ///
    /// The server exposes no `GET /sessions/{id}` (it answers `405`), so this
    /// reads through the get-or-create `POST /sessions` collection endpoint.
    /// One consequence: a session deleted server-side is silently re-created
    /// rather than surfacing as `NotFound`. Callers that need the fresh metadata
    /// or configuration should read it from the returned response to avoid a
    /// refresh-then-read race against concurrent writers.
    async fn refresh_into(&self) -> Result<SessionResponse> {
        // The individual-resource path (`GET /v3/workspaces/{ws}/sessions/{id}`)
        // is not exposed by the server — only `PUT`/`DELETE` are — so reads go
        // through the collection get-or-create, which returns the full session
        // without mutating it: `SessionCreate` skips its `None` fields, so the
        // request body is just `{"id": …}`.
        let body = crate::types::session::SessionCreate {
            id: self.inner.id.clone(),
            metadata: None,
            peers: None,
            configuration: None,
        };
        let resp: SessionResponse = self
            .inner
            .http
            .post(
                &routes::sessions(&self.inner.workspace_id)?,
                Some(&body),
                &[],
            )
            .await?;
        self.inner.update_cache(&resp);
        Ok(resp)
    }

    /// Fetch and return the session's metadata, updating the cache.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # async fn example(session: &honcho_ai::Session) -> honcho_ai::error::Result<()> {
    /// let meta = session.get_metadata().await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn get_metadata(&self) -> Result<HashMap<String, Value>> {
        let resp = self.refresh_into().await?;
        Ok(resp.metadata)
    }

    /// Set session metadata on the server and update the cache.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # async fn example(session: &honcho_ai::Session) -> honcho_ai::error::Result<()> {
    /// let mut meta = std::collections::HashMap::new();
    /// meta.insert("topic".into(), "rust".into());
    /// session.set_metadata(meta).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn set_metadata(&self, metadata: HashMap<String, Value>) -> Result<()> {
        let body = crate::types::session::SessionMetadataSet { metadata };
        let resp: SessionResponse = self
            .inner
            .http
            .put(
                &routes::session(&self.inner.workspace_id, &self.inner.id)?,
                Some(&body),
                &[],
            )
            .await?;
        self.inner.update_cache(&resp);
        Ok(())
    }

    /// Fetch and return session configuration, updating the cache.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # async fn example(session: &honcho_ai::Session) -> honcho_ai::error::Result<()> {
    /// let config = session.get_configuration().await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn get_configuration(&self) -> Result<SessionConfiguration> {
        let resp = self.refresh_into().await?;
        Ok(resp.configuration)
    }

    /// Set session configuration on the server and update the cache.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # async fn example(session: &honcho_ai::Session) -> honcho_ai::error::Result<()> {
    /// use honcho_ai::types::session::SessionConfiguration;
    /// let config = SessionConfiguration::default();
    /// session.set_configuration(&config).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn set_configuration(&self, configuration: &SessionConfiguration) -> Result<()> {
        let body = SessionUpdate {
            metadata: None,
            configuration: Some(configuration.clone()),
        };
        let resp: SessionResponse = self
            .inner
            .http
            .put(
                &routes::session(&self.inner.workspace_id, &self.inner.id)?,
                Some(&body),
                &[],
            )
            .await?;
        self.inner.update_cache(&resp);
        Ok(())
    }

    /// Fetch session configuration as a raw JSON map.
    ///
    /// Prefer [`get_configuration`](Self::get_configuration) for typed access.
    /// Use this when the server returns fields not yet represented in
    /// [`SessionConfiguration`].
    pub async fn get_configuration_raw(&self) -> Result<HashMap<String, Value>> {
        let body = crate::types::session::SessionCreate {
            id: self.inner.id.clone(),
            metadata: None,
            peers: None,
            configuration: None,
        };
        let raw: serde_json::Value = self
            .inner
            .http
            .post(
                &routes::sessions(&self.inner.workspace_id)?,
                Some(&body),
                &[],
            )
            .await?;
        match raw.get("configuration") {
            Some(serde_json::Value::Object(map)) => {
                Ok(map.iter().map(|(k, v)| (k.clone(), v.clone())).collect())
            }
            _ => Ok(HashMap::new()),
        }
    }

    /// Set session configuration from a raw JSON map.
    ///
    /// Prefer [`set_configuration`](Self::set_configuration) for typed access.
    /// Use this when you need to send fields not yet represented in
    /// [`SessionConfiguration`].
    pub async fn set_configuration_raw(&self, configuration: HashMap<String, Value>) -> Result<()> {
        let body = SessionConfigurationSet { configuration };
        let resp: SessionResponse = self
            .inner
            .http
            .put(
                &routes::session(&self.inner.workspace_id, &self.inner.id)?,
                Some(&body),
                &[],
            )
            .await?;
        self.inner.update_cache(&resp);
        Ok(())
    }

    // ── F6.2: Peer Management ──────────────────────────────────────────

    /// Add a single peer to this session.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # async fn example(session: &honcho_ai::Session) -> honcho_ai::error::Result<()> {
    /// session.add_peer("alice").await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn add_peer(&self, id: impl Into<String>) -> Result<()> {
        self.add_peers(std::iter::once(PeerSpec::Id(id.into())))
            .await
    }

    /// Add multiple peers to this session.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # async fn example(session: &honcho_ai::Session) -> honcho_ai::error::Result<()> {
    /// session.add_peers(["alice", "bob"]).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn add_peers(
        &self,
        specs: impl IntoIterator<Item = impl Into<PeerSpec>>,
    ) -> Result<()> {
        let peers_map = normalize_peers(specs)?;
        let route = routes::session_peers(&self.inner.workspace_id, &self.inner.id)?;
        self.inner.http.post(&route, Some(&peers_map), &[]).await
    }

    /// Set the complete peer list for this session (replaces existing).
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # async fn example(session: &honcho_ai::Session) -> honcho_ai::error::Result<()> {
    /// session.set_peers(["alice", "bob"]).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn set_peers(
        &self,
        specs: impl IntoIterator<Item = impl Into<PeerSpec>>,
    ) -> Result<()> {
        let peers_map = normalize_peers(specs)?;
        let route = routes::session_peers(&self.inner.workspace_id, &self.inner.id)?;
        self.inner.http.put(&route, Some(&peers_map), &[]).await
    }

    /// Remove peers from this session.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # async fn example(session: &honcho_ai::Session) -> honcho_ai::error::Result<()> {
    /// session.remove_peers(["bob"]).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn remove_peers(
        &self,
        ids: impl IntoIterator<Item = impl Into<String>>,
    ) -> Result<()> {
        let id_list: Vec<String> = ids.into_iter().map(Into::into).collect();
        let route = routes::session_peers(&self.inner.workspace_id, &self.inner.id)?;
        self.inner
            .http
            .request::<_, ()>(Method::DELETE, &route, Some(&id_list), &[])
            .await
    }

    /// List peers in this session.
    ///
    /// This call is **all-or-nothing**: it walks every page and deserializes each
    /// peer. If any single peer fails to deserialize (`Peer::from_parts` errors),
    /// the whole call returns that error and the peers already accumulated from
    /// earlier pages are discarded — partial results are never returned.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # async fn example(session: &honcho_ai::Session) -> honcho_ai::error::Result<()> {
    /// let peers = session.peers().await?;
    /// for p in &peers {
    ///     println!("{}", p.id());
    /// }
    /// # Ok(())
    /// # }
    /// ```
    pub async fn peers(&self) -> Result<Vec<crate::Peer>> {
        use crate::types::pagination::PageResponse;

        let route = routes::session_peers(&self.inner.workspace_id, &self.inner.id)?;
        let mut all = Vec::new();
        let mut page: u64 = 1;
        loop {
            let page_str = page.to_string();
            let resp: PageResponse<crate::types::peer::Peer> = self
                .inner
                .http
                .get(&route, &[("page", page_str.as_str())])
                .await?;
            let total_pages = resp.pages;
            let was_empty = resp.items.is_empty();
            for item in resp.items {
                all.push(crate::Peer::from_parts(
                    self.inner.http.clone(),
                    self.inner.workspace_id.to_string(),
                    item,
                )?);
            }
            // Stop once we have walked every page. `was_empty` guards against a
            // server reporting a page count larger than the items it returns,
            // which would otherwise loop forever.
            if was_empty || page >= total_pages {
                break;
            }
            page += 1;
        }
        Ok(all)
    }

    // ── F6.3: Per-peer configuration ───────────────────────────────────

    /// Get per-peer configuration for a specific peer in this session.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # async fn example(session: &honcho_ai::Session) -> honcho_ai::error::Result<()> {
    /// let config = session.get_peer_configuration("alice").await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn get_peer_configuration(&self, peer_id: &str) -> Result<SessionPeerConfig> {
        let route = routes::session_peer_config(&self.inner.workspace_id, &self.inner.id, peer_id)?;
        self.inner.http.get(&route, &[]).await
    }

    /// Set per-peer configuration for a specific peer in this session.
    ///
    /// The peer must already be present in the session. This method does not
    /// create or add peers; use [`Session::add_peer`] or [`Session::add_peers`]
    /// first. If the peer is absent, the server may return 404/`NotFound`.
    ///
    /// # Examples
    ///
    /// ```ignore
    /// # async fn example(session: &honcho_ai::Session) -> honcho_ai::error::Result<()> {
    /// use honcho_ai::types::session::SessionPeerConfig;
    /// let config = SessionPeerConfig { observe_me: Some(true), observe_others: Some(false) };
    /// session.set_peer_configuration("alice", &config).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn set_peer_configuration(
        &self,
        peer_id: &str,
        config: &SessionPeerConfig,
    ) -> Result<()> {
        let route = routes::session_peer_config(&self.inner.workspace_id, &self.inner.id, peer_id)?;
        self.inner.http.put(&route, Some(config), &[]).await
    }

    // ── F6.4: Messages ─────────────────────────────────────────────────

    /// Add messages to this session.
    ///
    /// If more than 100 messages are provided, they are automatically chunked
    /// into batches of 100 and sent as separate requests. On chunk failure the
    /// already-sent messages are **not** rolled back (non-atomic). When a chunk
    /// fails after earlier chunks succeeded, the error is a
    /// [`HonchoError::PartialFailure`] containing the successfully created
    /// messages from the earlier chunks.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # async fn example(client: &honcho_ai::Honcho, session: &honcho_ai::Session) -> honcho_ai::error::Result<()> {
    /// let peer = client.peer("alice").build().await?;
    /// let msg = peer.message("Hello!").build()?;
    /// let messages = session.add_messages(vec![msg]).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn add_messages(
        &self,
        messages: Vec<crate::types::message::MessageCreate>,
    ) -> Result<Vec<Message>> {
        if messages.is_empty() {
            return Ok(Vec::new());
        }

        let route = routes::messages(&self.inner.workspace_id, &self.inner.id)?;

        let responses: Vec<MessageResponse> = if messages.len() <= 100 {
            let body = crate::types::message::MessageBatchCreate { messages };
            self.inner.http.post(&route, Some(&body), &[]).await?
        } else {
            let mut all = Vec::with_capacity(messages.len());
            // Drain owned messages 100 at a time instead of cloning each chunk:
            // `by_ref().take(100)` moves each batch out of the iterator.
            let mut iter = messages.into_iter();
            loop {
                let batch: Vec<crate::types::message::MessageCreate> =
                    iter.by_ref().take(100).collect();
                if batch.is_empty() {
                    break;
                }
                let body = crate::types::message::MessageBatchCreate { messages: batch };
                match self
                    .inner
                    .http
                    .post::<_, Vec<MessageResponse>>(&route, Some(&body), &[])
                    .await
                {
                    Ok(batch_responses) => all.extend(batch_responses),
                    Err(e) if all.is_empty() => return Err(e),
                    Err(e) => {
                        let sent = all.len();
                        let partial: Vec<Message> =
                            all.into_iter().map(Message::from_raw).collect();
                        return Err(HonchoError::PartialFailure {
                            messages: partial,
                            sent,
                            error: Box::new(e),
                        });
                    }
                }
            }
            all
        };

        Ok(responses.into_iter().map(Message::from_raw).collect())
    }

    /// List messages in this session with default pagination (no filters, page 1, size 50).
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # async fn example(session: &honcho_ai::Session) -> honcho_ai::error::Result<()> {
    /// let page = session.messages().await?;
    /// for msg in page.items() {
    ///     println!("{}", msg.content());
    /// }
    /// # Ok(())
    /// # }
    /// ```
    pub async fn messages(
        &self,
    ) -> Result<crate::types::pagination::Page<MessageResponse, Message>> {
        self.messages_with_options(None, 1, 50, false).await
    }

    /// List messages in this session with optional filters, page, size, and reverse.
    ///
    /// `page` is 1-based. `size` must be in `1..=100`.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # async fn example(session: &honcho_ai::Session) -> honcho_ai::error::Result<()> {
    /// let page = session.messages_with_options(None, 1, 25, false).await?;
    /// for msg in page.items() {
    ///     println!("{}", msg.content());
    /// }
    /// # Ok(())
    /// # }
    /// ```
    pub async fn messages_with_options(
        &self,
        filters: Option<HashMap<String, Value>>,
        page: u64,
        size: u64,
        reverse: bool,
    ) -> Result<crate::types::pagination::Page<MessageResponse, Message>> {
        let route = routes::messages_list(&self.inner.workspace_id, &self.inner.id)?;
        let body = filters
            .map(|f| {
                serde_json::to_value(f).map_err(|e| HonchoError::Serialization {
                    path: "MessageGet".into(),
                    source: e,
                })
            })
            .transpose()?;
        let result: crate::types::pagination::Page<MessageResponse> =
            crate::types::pagination::paginate_post(
                &self.inner.http,
                &route,
                body.as_ref(),
                page,
                size,
                reverse,
            )
            .await?;
        Ok(result.map(Message::from_raw))
    }

    // ── F7.3: File upload ───────────────────────────────────────────────

    /// Begin a file upload to this session.
    ///
    /// The API currently accepts `text/plain`, `application/pdf`, and
    /// `application/json`; other MIME types may be rejected by the server.
    ///
    /// Returns an [`UploadFileBuilder`]. You **must** call `.peer(id)` and
    /// then `.send()` to complete the upload.
    ///
    /// # Example
    ///
    /// ```ignore
    /// let messages = session
    ///     .upload_file(FileSource::bytes("doc.pdf", data, "application/pdf"))
    ///     .peer("alice")
    ///     .send()
    ///     .await?;
    /// ```
    pub fn upload_file(&self, source: impl Into<FileSource>) -> UploadFileBuilder<'_> {
        UploadFileBuilder {
            session: self,
            source: Some(source.into()),
            peer_id: None,
            metadata: None,
            configuration: None,
            created_at: None,
        }
    }

    /// Begin a file upload to this session from a streaming reader.
    ///
    /// The API currently accepts `text/plain`, `application/pdf`, and
    /// `application/json`; other MIME types may be rejected by the server.
    ///
    /// The reader is fully buffered into memory before uploading. This is
    /// **not** true streaming — use [`Session::upload_file`] with a
    /// [`FileSource::path`] for filesystem streaming that avoids buffering.
    ///
    /// Returns an [`UploadFileBuilder`]. You **must** call `.peer(id)` and
    /// then `.send()` to complete the upload.
    pub fn upload_file_streamed(
        &self,
        filename: impl Into<String>,
        reader: impl tokio::io::AsyncRead + Send + 'static,
        content_type: impl Into<String>,
    ) -> UploadFileBuilder<'_> {
        UploadFileBuilder {
            session: self,
            source: Some(FileSource::stream(filename, reader, content_type)),
            peer_id: None,
            metadata: None,
            configuration: None,
            created_at: None,
        }
    }

    // ── F6.5: Delete, clone, get/update message ────────────────────────

    /// Delete this session.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # async fn example(session: &honcho_ai::Session) -> honcho_ai::error::Result<()> {
    /// session.delete().await?;
    /// # Ok(())
    /// # }
    /// ```
    #[cfg_attr(feature = "tracing", tracing::instrument(skip(self), fields(session_id = self.inner.id.as_str())))]
    pub async fn delete(&self) -> Result<()> {
        self.inner
            .http
            .delete(
                &routes::session(&self.inner.workspace_id, &self.inner.id)?,
                &[],
            )
            .await
    }

    /// Clone this session, returning a new `Session`.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # async fn example(session: &honcho_ai::Session) -> honcho_ai::error::Result<()> {
    /// let cloned = session.clone_session().await?;
    /// # Ok(())
    /// # }
    /// ```
    #[cfg_attr(feature = "tracing", tracing::instrument(skip(self), fields(session_id = self.inner.id.as_str())))]
    pub async fn clone_session(&self) -> Result<Session> {
        let route = routes::session_clone(&self.inner.workspace_id, &self.inner.id)?;
        let resp: SessionResponse = self.inner.http.post(&route, None::<&Value>, &[]).await?;
        Ok(Self::from_parts(
            self.inner.http.clone(),
            self.inner.workspace_id.to_string(),
            resp,
        ))
    }

    /// Clone this session up to (and including) the given message.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # async fn example(session: &honcho_ai::Session) -> honcho_ai::error::Result<()> {
    /// let cloned = session.clone_session_with_message("msg-42").await?;
    /// # Ok(())
    /// # }
    /// ```
    #[cfg_attr(feature = "tracing", tracing::instrument(skip(self), fields(session_id = self.inner.id.as_str())))]
    pub async fn clone_session_with_message(&self, message_id: &str) -> Result<Session> {
        let route = routes::session_clone(&self.inner.workspace_id, &self.inner.id)?;
        let resp: SessionResponse = self
            .inner
            .http
            .post(&route, None::<&Value>, &[("message_id", message_id)])
            .await?;
        Ok(Self::from_parts(
            self.inner.http.clone(),
            self.inner.workspace_id.to_string(),
            resp,
        ))
    }

    /// Get a single message by ID.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # async fn example(session: &honcho_ai::Session) -> honcho_ai::error::Result<()> {
    /// let msg = session.get_message("msg-1").await?;
    /// println!("{}", msg.content());
    /// # Ok(())
    /// # }
    /// ```
    #[cfg_attr(feature = "tracing", tracing::instrument(skip(self), fields(session_id = self.inner.id.as_str())))]
    pub async fn get_message(&self, id: &str) -> Result<Message> {
        let route = routes::message(&self.inner.workspace_id, &self.inner.id, id)?;
        let resp: MessageResponse = self.inner.http.get(&route, &[]).await?;
        Ok(Message::from_raw(resp))
    }

    /// Update a message's metadata.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # async fn example(session: &honcho_ai::Session) -> honcho_ai::error::Result<()> {
    /// let mut meta = std::collections::HashMap::new();
    /// meta.insert("edited".into(), true.into());
    /// let msg = session.update_message("msg-1", meta).await?;
    /// # Ok(())
    /// # }
    /// ```
    #[cfg_attr(feature = "tracing", tracing::instrument(skip(self, metadata), fields(session_id = self.inner.id.as_str())))]
    pub async fn update_message(
        &self,
        id: &str,
        metadata: HashMap<String, Value>,
    ) -> Result<Message> {
        let route = routes::message(&self.inner.workspace_id, &self.inner.id, id)?;
        let body = crate::types::message::MessageMetadataSet { metadata };
        let resp: MessageResponse = self.inner.http.put(&route, Some(&body), &[]).await?;
        Ok(Message::from_raw(resp))
    }

    // ── F6.6: Context ───────────────────────────────────────────────────

    /// Get the session context with default parameters.
    ///
    /// Fetches messages, summary, peer representation, and peer card for this session.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # async fn example(session: &honcho_ai::Session) -> honcho_ai::error::Result<()> {
    /// let ctx = session.context().await?;
    /// # Ok(())
    /// # }
    /// ```
    #[cfg_attr(feature = "tracing", tracing::instrument(skip(self), fields(session_id = self.inner.id.as_str())))]
    pub async fn context(&self) -> Result<crate::types::session::SessionContext> {
        self.context_builder().send().await
    }

    /// Get the session context with custom parameters.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # async fn example(session: &honcho_ai::Session) -> honcho_ai::error::Result<()> {
    /// use honcho_ai::types::session::SessionContextOptions;
    /// let opts = SessionContextOptions::builder().summary(true).build();
    /// let ctx = session.context_with_options(&opts).await?;
    /// # Ok(())
    /// # }
    /// ```
    #[cfg_attr(feature = "tracing", tracing::instrument(skip(self), fields(session_id = self.inner.id.as_str())))]
    pub async fn context_with_options(
        &self,
        options: &crate::types::session::SessionContextOptions,
    ) -> Result<crate::types::session::SessionContext> {
        fetch_session_context(
            &self.inner.http,
            &self.inner.workspace_id,
            &self.inner.id,
            options,
        )
        .await
    }

    /// Get a context builder for fine-grained control over session context parameters.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # async fn example(session: &honcho_ai::Session) -> honcho_ai::error::Result<()> {
    /// let ctx = session.context_builder()
    ///     .summary(true)
    ///     .peer_target("alice")
    ///     .search_query("preferences")
    ///     .search_top_k(10)
    ///     .send()
    ///     .await?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn context_builder(&self) -> SessionContextBuilder {
        SessionContextBuilder {
            http: self.inner.http.clone(),
            workspace_id: self.inner.workspace_id.to_string(),
            session_id: self.inner.id.clone(),
            summary: true,
            limit_to_session: false,
            tokens: None,
            peer_target: None,
            peer_perspective: None,
            search_query: None,
            search_top_k: None,
            search_max_distance: None,
            include_most_frequent: None,
            max_conclusions: None,
        }
    }

    // ── F6.8: Summaries ─────────────────────────────────────────────────

    /// Get available summaries for this session.
    ///
    /// Returns both short and long summaries if they are available.
    /// Summaries are created asynchronously as messages are added.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # async fn example(session: &honcho_ai::Session) -> honcho_ai::error::Result<()> {
    /// let summaries = session.summaries().await?;
    /// # Ok(())
    /// # }
    /// ```
    #[cfg_attr(feature = "tracing", tracing::instrument(skip(self), fields(session_id = self.inner.id.as_str())))]
    pub async fn summaries(&self) -> Result<crate::types::session::SessionSummaries> {
        let route = routes::session_summaries(&self.inner.workspace_id, &self.inner.id)?;
        self.inner.http.get(&route, &[]).await
    }

    // ── F6.9: Search, representation, queue_status ──────────────────────

    /// Search messages within this session (default limit of 10).
    ///
    /// Returns `Err(HonchoError::Validation)` when `query` is empty.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # async fn example(session: &honcho_ai::Session) -> honcho_ai::error::Result<()> {
    /// let results = session.search("important topic").await?;
    /// for msg in results {
    ///     println!("{}", msg.content());
    /// }
    /// # Ok(())
    /// # }
    /// ```
    #[cfg_attr(feature = "tracing", tracing::instrument(skip(self), fields(session_id = self.inner.id.as_str())))]
    pub async fn search(&self, query: &str) -> Result<Vec<Message>> {
        self.search_with_options(&crate::types::message::MessageSearchOptions {
            query: query.to_string(),
            filters: None,
            limit: 10,
        })
        .await
    }

    /// Search messages within this session with custom options (limit, filters).
    ///
    /// Returns `Err(HonchoError::Validation)` when `query` is empty.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # async fn example(session: &honcho_ai::Session) -> honcho_ai::error::Result<()> {
    /// use honcho_ai::types::message::MessageSearchOptions;
    /// let opts = MessageSearchOptions::builder().query("topic").limit(20).build();
    /// let results = session.search_with_options(&opts).await?;
    /// # Ok(())
    /// # }
    /// ```
    #[cfg_attr(feature = "tracing", tracing::instrument(skip(self, options), fields(session_id = self.inner.id.as_str())))]
    pub async fn search_with_options(
        &self,
        options: &crate::types::message::MessageSearchOptions,
    ) -> Result<Vec<Message>> {
        if options.query.is_empty() {
            return Err(crate::error::HonchoError::Validation(
                "query must not be empty".to_string(),
            ));
        }
        let route = routes::session_search(&self.inner.workspace_id, &self.inner.id)?;
        let responses: Vec<MessageResponse> =
            self.inner.http.post(&route, Some(&options), &[]).await?;
        Ok(responses.into_iter().map(Message::from_raw).collect())
    }

    /// Get a peer's representation scoped to this session.
    ///
    /// Uses the peer representation endpoint with `session_id` filter.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # async fn example(session: &honcho_ai::Session) -> honcho_ai::error::Result<()> {
    /// let rep = session.representation("alice").await?;
    /// println!("{rep}");
    /// # Ok(())
    /// # }
    /// ```
    #[cfg_attr(feature = "tracing", tracing::instrument(skip(self), fields(session_id = self.inner.id.as_str())))]
    pub async fn representation(&self, peer_id: &str) -> Result<String> {
        self.representation_builder(peer_id).send().await
    }

    /// Create a builder for fine-grained representation requests scoped to this session.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # async fn example(session: &honcho_ai::Session) -> honcho_ai::error::Result<()> {
    /// let rep = session.representation_builder("alice")
    ///     .search_query("hobbies")
    ///     .search_top_k(10)
    ///     .send()
    ///     .await?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn representation_builder(
        &self,
        peer_id: impl Into<String>,
    ) -> SessionRepresentationBuilder {
        SessionRepresentationBuilder {
            http: self.inner.http.clone(),
            workspace_id: self.inner.workspace_id.to_string(),
            session_id: self.inner.id.clone(),
            peer_id: peer_id.into(),
            target: None,
            search_query: None,
            search_top_k: None,
            search_max_distance: None,
            include_most_frequent: None,
            max_conclusions: None,
        }
    }

    /// Get the processing queue status for this session.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # async fn example(session: &honcho_ai::Session) -> honcho_ai::error::Result<()> {
    /// let status = session.queue_status(None, None).await?;
    /// # Ok(())
    /// # }
    /// ```
    #[cfg_attr(feature = "tracing", tracing::instrument(skip(self), fields(session_id = self.inner.id.as_str())))]
    pub async fn queue_status(
        &self,
        observer_id: Option<&str>,
        sender_id: Option<&str>,
    ) -> Result<crate::types::dream::QueueStatus> {
        let route = routes::workspace_queue_status(&self.inner.workspace_id)?;
        let mut query: Vec<(&str, &str)> = vec![("session_id", self.inner.id.as_str())];
        if let Some(v) = observer_id {
            query.push(("observer_id", v));
        }
        if let Some(v) = sender_id {
            query.push(("sender_id", v));
        }
        self.inner.http.get(&route, &query).await
    }
}

/// Builder for fine-grained representation requests scoped to a session.
#[must_use]
pub struct SessionRepresentationBuilder {
    http: HttpClient,
    workspace_id: String,
    session_id: String,
    peer_id: String,
    target: Option<String>,
    search_query: Option<String>,
    search_top_k: Option<u32>,
    search_max_distance: Option<f64>,
    include_most_frequent: Option<bool>,
    max_conclusions: Option<u32>,
}

impl SessionRepresentationBuilder {
    /// Get the representation for a specific target peer.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # fn example(session: &honcho_ai::Session) {
    /// let _builder = session.representation_builder("alice").target("bob");
    /// # }
    /// ```
    pub fn target(mut self, val: impl Into<String>) -> Self {
        self.target = Some(val.into());
        self
    }

    /// Semantic search query to curate the representation.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # fn example(session: &honcho_ai::Session) {
    /// let _builder = session.representation_builder("alice").search_query("hobbies");
    /// # }
    /// ```
    pub fn search_query(mut self, val: impl Into<String>) -> Self {
        self.search_query = Some(val.into());
        self
    }

    /// Number of semantic-search-retrieved conclusions (1–100).
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # fn example(session: &honcho_ai::Session) {
    /// let _builder = session.representation_builder("alice").search_top_k(20);
    /// # }
    /// ```
    pub fn search_top_k(mut self, val: u32) -> Self {
        self.search_top_k = Some(val);
        self
    }

    /// Maximum distance for semantically relevant conclusions (0.0–1.0).
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # fn example(session: &honcho_ai::Session) {
    /// let _builder = session.representation_builder("alice").search_max_distance(0.5);
    /// # }
    /// ```
    pub fn search_max_distance(mut self, val: f64) -> Self {
        self.search_max_distance = Some(val);
        self
    }

    /// Whether to include the most frequent conclusions.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # fn example(session: &honcho_ai::Session) {
    /// let _builder = session.representation_builder("alice").include_most_frequent(true);
    /// # }
    /// ```
    pub fn include_most_frequent(mut self, val: bool) -> Self {
        self.include_most_frequent = Some(val);
        self
    }

    /// Maximum number of conclusions to include (1–100).
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # fn example(session: &honcho_ai::Session) {
    /// let _builder = session.representation_builder("alice").max_conclusions(25);
    /// # }
    /// ```
    pub fn max_conclusions(mut self, val: u32) -> Self {
        self.max_conclusions = Some(val);
        self
    }

    /// Send the representation request with the configured parameters.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # async fn example(session: &honcho_ai::Session) -> honcho_ai::error::Result<()> {
    /// let rep = session.representation_builder("alice")
    ///     .search_query("hobbies")
    ///     .search_top_k(10)
    ///     .send()
    ///     .await?;
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # Errors
    ///
    /// Returns `HonchoError::Validation` if `search_top_k`, `search_max_distance`,
    /// or `max_conclusions` are out of range.
    #[cfg_attr(feature = "tracing", tracing::instrument(skip(self), fields(session_id = self.session_id.as_str(), peer_id = self.peer_id.as_str())))]
    pub async fn send(self) -> Result<String> {
        crate::types::session::validate_search_params(
            self.search_top_k,
            self.search_max_distance,
            self.max_conclusions,
        )?;

        let params = crate::types::peer::PeerRepresentationGet {
            session_id: Some(self.session_id),
            target: self.target,
            search_query: self.search_query,
            search_top_k: self.search_top_k,
            search_max_distance: self.search_max_distance,
            include_most_frequent: self.include_most_frequent,
            max_conclusions: self.max_conclusions,
        };

        let route = routes::peer_representation(&self.workspace_id, &self.peer_id)?;
        let resp: crate::types::dialectic::RepresentationResponse =
            self.http.post(&route, Some(&params), &[]).await?;
        Ok(resp.representation)
    }
}

/// Builder for fine-grained session context requests.
///
/// Created via [`Session::context_builder()`].
///
/// # Examples
///
/// ```no_run
/// # async fn example(session: &honcho_ai::Session) -> honcho_ai::error::Result<()> {
/// let ctx = session.context_builder()
///     .summary(true)
///     .peer_target("alice")
///     .search_query("preferences")
///     .search_top_k(10)
///     .send()
///     .await?;
/// # Ok(())
/// # }
/// ```
#[must_use]
pub struct SessionContextBuilder {
    http: HttpClient,
    workspace_id: String,
    session_id: String,
    summary: bool,
    limit_to_session: bool,
    tokens: Option<u32>,
    peer_target: Option<String>,
    peer_perspective: Option<String>,
    search_query: Option<String>,
    search_top_k: Option<u32>,
    search_max_distance: Option<f64>,
    include_most_frequent: Option<bool>,
    max_conclusions: Option<u32>,
}

impl SessionContextBuilder {
    /// Whether to include summaries (default: `true`).
    pub fn summary(mut self, val: bool) -> Self {
        self.summary = val;
        self
    }

    /// Limit context to this session only (default: `false`).
    pub fn limit_to_session(mut self, val: bool) -> Self {
        self.limit_to_session = val;
        self
    }

    /// Maximum number of tokens for the context.
    pub fn tokens(mut self, val: u32) -> Self {
        self.tokens = Some(val);
        self
    }

    /// Target peer for perspective-based context.
    pub fn peer_target(mut self, val: impl Into<String>) -> Self {
        self.peer_target = Some(val.into());
        self
    }

    /// Perspective peer for viewing context.
    pub fn peer_perspective(mut self, val: impl Into<String>) -> Self {
        self.peer_perspective = Some(val.into());
        self
    }

    /// Semantic search query to filter relevant conclusions.
    pub fn search_query(mut self, val: impl Into<String>) -> Self {
        self.search_query = Some(val.into());
        self
    }

    /// Number of semantic-search-retrieved conclusions (1–100).
    pub fn search_top_k(mut self, val: u32) -> Self {
        self.search_top_k = Some(val);
        self
    }

    /// Maximum distance for semantically relevant conclusions (0.0–1.0).
    pub fn search_max_distance(mut self, val: f64) -> Self {
        self.search_max_distance = Some(val);
        self
    }

    /// Whether to include the most frequent conclusions.
    pub fn include_most_frequent(mut self, val: bool) -> Self {
        self.include_most_frequent = Some(val);
        self
    }

    /// Maximum number of conclusions to include (1–100).
    pub fn max_conclusions(mut self, val: u32) -> Self {
        self.max_conclusions = Some(val);
        self
    }

    /// Send the context request with the configured parameters.
    ///
    /// # Errors
    ///
    /// Returns `HonchoError::Validation` if `search_top_k`, `search_max_distance`,
    /// or `max_conclusions` are out of range, if `peer_perspective` is set without
    /// `peer_target`, or if `search_query` is set without `peer_target`.
    #[cfg_attr(feature = "tracing", tracing::instrument(skip(self), fields(session_id = self.session_id.as_str())))]
    pub async fn send(self) -> Result<crate::types::session::SessionContext> {
        let options = crate::types::session::SessionContextOptions {
            summary: self.summary,
            limit_to_session: self.limit_to_session,
            tokens: self.tokens,
            peer_target: self.peer_target,
            peer_perspective: self.peer_perspective,
            search_query: self.search_query,
            search_top_k: self.search_top_k,
            search_max_distance: self.search_max_distance,
            include_most_frequent: self.include_most_frequent,
            max_conclusions: self.max_conclusions,
        };
        fetch_session_context(&self.http, &self.workspace_id, &self.session_id, &options).await
    }
}

/// Validate options, then GET the session context endpoint with the query
/// parameters they produce. Shared by [`Session::context_with_options`] and
/// [`SessionContextBuilder::send`] so the route/query/GET logic lives once.
async fn fetch_session_context(
    http: &HttpClient,
    workspace_id: &str,
    session_id: &str,
    options: &crate::types::session::SessionContextOptions,
) -> Result<crate::types::session::SessionContext> {
    options.validate()?;
    let route = routes::session_context(workspace_id, session_id)?;
    let params = options.to_query_params();
    let refs: Vec<(&str, &str)> = params.iter().map(|(k, v)| (*k, &**v)).collect();
    http.get(&route, &refs).await
}

fn normalize_peers(
    specs: impl IntoIterator<Item = impl Into<PeerSpec>>,
) -> Result<serde_json::Value> {
    use serde_json::map::Entry;

    let mut map = serde_json::Map::new();
    for s in specs {
        // Decompose by value: the id is owned (no extra clone) and the config is
        // taken directly, defaulting for the bare-ID variant.
        let (id, cfg) = s.into().into_parts();
        let val = serde_json::to_value(&cfg).map_err(|e| HonchoError::Serialization {
            path: "SessionPeerConfig".into(),
            source: e,
        })?;
        // Reject duplicate IDs instead of letting a later entry silently clobber
        // an earlier one. The Entry API does a single lookup for both the
        // duplicate check and the insert.
        match map.entry(id) {
            Entry::Occupied(e) => {
                return Err(HonchoError::Validation(format!(
                    "duplicate peer id: {}",
                    e.key()
                )));
            }
            Entry::Vacant(e) => {
                e.insert(val);
            }
        }
    }
    Ok(Value::Object(map))
}

#[cfg(test)]
mod tests {
    #![allow(clippy::unwrap_used, clippy::expect_used)]

    use static_assertions::assert_impl_all;

    use super::*;
    use crate::http::client::HttpClient;
    use crate::types::session::SessionResponse;
    use chrono::TimeZone;
    use wiremock::matchers::{body_string_contains, method, path};
    use wiremock::{Mock, MockServer, ResponseTemplate};

    assert_impl_all!(UploadFileBuilder<'_>: Send);

    fn session_json(id: &str) -> serde_json::Value {
        serde_json::json!({
            "id": id,
            "workspace_id": "ws1",
            "is_active": true,
            "metadata": {},
            "configuration": {},
            "created_at": "2025-01-15T10:30:00Z"
        })
    }

    fn message_response_json(content: &str, peer_id: &str) -> serde_json::Value {
        serde_json::json!({
            "id": "msg_1",
            "content": content,
            "peer_id": peer_id,
            "session_id": "sess1",
            "metadata": {},
            "created_at": "2025-01-15T10:30:00Z",
            "workspace_id": "ws1",
            "token_count": 5
        })
    }

    fn make_session(http: HttpClient, id: &str) -> Session {
        let resp: SessionResponse = serde_json::from_value(session_json(id)).unwrap();
        Session::from_parts(http, "ws1".to_owned(), resp)
    }

    fn upload_response_json() -> serde_json::Value {
        serde_json::json!([message_response_json("extracted text", "alice")])
    }

    #[tokio::test]
    async fn upload_file_with_bytes_sends_correct_multipart() {
        let server = MockServer::start().await;
        let http =
            HttpClient::from_params(HttpClient::builder().base_url(server.uri()).build()).unwrap();
        let session = make_session(http, "sess1");

        Mock::given(method("POST"))
            .and(path("/v3/workspaces/ws1/sessions/sess1/messages/upload"))
            .and(body_string_contains("file content here"))
            .and(body_string_contains("peer_id"))
            .and(body_string_contains("alice"))
            .respond_with(ResponseTemplate::new(200).set_body_json(upload_response_json()))
            .mount(&server)
            .await;

        let msgs = session
            .upload_file(FileSource::bytes(
                "test.txt",
                b"file content here".as_slice(),
                "text/plain",
            ))
            .peer("alice")
            .send()
            .await
            .unwrap();

        assert_eq!(msgs.len(), 1);
        assert_eq!(msgs[0].content(), "extracted text");
        assert_eq!(msgs[0].peer_id(), "alice");
    }

    #[tokio::test]
    async fn upload_file_with_metadata_sends_json_stringified_field() {
        let server = MockServer::start().await;
        let http =
            HttpClient::from_params(HttpClient::builder().base_url(server.uri()).build()).unwrap();
        let session = make_session(http, "sess1");

        let metadata = serde_json::json!({"source": "upload", "priority": 1});

        Mock::given(method("POST"))
            .and(path("/v3/workspaces/ws1/sessions/sess1/messages/upload"))
            .and(body_string_contains("\"source\":\"upload\""))
            .and(body_string_contains("\"priority\":1"))
            .respond_with(ResponseTemplate::new(200).set_body_json(upload_response_json()))
            .mount(&server)
            .await;

        let msgs = session
            .upload_file(FileSource::bytes("f.txt", b"data", "text/plain"))
            .peer("alice")
            .metadata(metadata)
            .send()
            .await
            .unwrap();

        assert_eq!(msgs.len(), 1);
    }

    #[tokio::test]
    async fn upload_file_with_configuration_sends_json_stringified() {
        let server = MockServer::start().await;
        let http =
            HttpClient::from_params(HttpClient::builder().base_url(server.uri()).build()).unwrap();
        let session = make_session(http, "sess1");

        let config = serde_json::json!({"reasoning": {"enabled": true}});

        Mock::given(method("POST"))
            .and(path("/v3/workspaces/ws1/sessions/sess1/messages/upload"))
            .and(body_string_contains("\"reasoning\""))
            .and(body_string_contains("\"enabled\":true"))
            .respond_with(ResponseTemplate::new(200).set_body_json(upload_response_json()))
            .mount(&server)
            .await;

        let msgs = session
            .upload_file(FileSource::bytes("f.txt", b"data", "text/plain"))
            .peer("bob")
            .configuration(config)
            .send()
            .await
            .unwrap();

        assert_eq!(msgs.len(), 1);
    }

    #[tokio::test]
    async fn upload_file_with_created_at_datetime_sends_iso_string() {
        let server = MockServer::start().await;
        let http =
            HttpClient::from_params(HttpClient::builder().base_url(server.uri()).build()).unwrap();
        let session = make_session(http, "sess1");

        let dt = Utc.with_ymd_and_hms(2025, 3, 14, 9, 26, 53).unwrap();

        Mock::given(method("POST"))
            .and(path("/v3/workspaces/ws1/sessions/sess1/messages/upload"))
            .and(body_string_contains("2025-03-14T09:26:53+00:00"))
            .respond_with(ResponseTemplate::new(200).set_body_json(upload_response_json()))
            .mount(&server)
            .await;

        let msgs = session
            .upload_file(FileSource::bytes("f.txt", b"data", "text/plain"))
            .peer("alice")
            .created_at(dt)
            .send()
            .await
            .unwrap();

        assert_eq!(msgs.len(), 1);
    }

    #[tokio::test]
    async fn upload_file_with_path_reads_file_and_uploads() {
        let dir = tempfile::tempdir().unwrap();
        let file_path = dir.path().join("notes.txt");
        std::fs::write(&file_path, "file from disk").unwrap();

        let server = MockServer::start().await;
        let http =
            HttpClient::from_params(HttpClient::builder().base_url(server.uri()).build()).unwrap();
        let session = make_session(http, "sess1");

        Mock::given(method("POST"))
            .and(path("/v3/workspaces/ws1/sessions/sess1/messages/upload"))
            .and(body_string_contains("file from disk"))
            .respond_with(ResponseTemplate::new(200).set_body_json(upload_response_json()))
            .mount(&server)
            .await;

        let msgs = session
            .upload_file(FileSource::path(&file_path))
            .peer("alice")
            .send()
            .await
            .unwrap();

        assert_eq!(msgs.len(), 1);
    }

    #[tokio::test]
    async fn upload_file_path_without_filename_returns_validation_error() {
        // Regression: a path with no final file-name component (`/`, trailing
        // `..`) must fail fast on the PRODUCTION upload path before any request
        // is made, rather than silently uploading with an empty filename.
        let server = MockServer::start().await;
        let http =
            HttpClient::from_params(HttpClient::builder().base_url(server.uri()).build()).unwrap();
        let session = make_session(http, "sess1");

        let err = session
            .upload_file(FileSource::path("/"))
            .peer("alice")
            .send()
            .await
            .unwrap_err();

        assert_eq!(err.code(), "validation_error");
    }

    #[tokio::test]
    async fn upload_file_without_peer_returns_validation_error() {
        let server = MockServer::start().await;
        let http =
            HttpClient::from_params(HttpClient::builder().base_url(server.uri()).build()).unwrap();
        let session = make_session(http, "sess1");

        let err = session
            .upload_file(FileSource::bytes("f.txt", b"data", "text/plain"))
            .send()
            .await
            .unwrap_err();

        assert_eq!(err.code(), "validation_error");
    }

    #[tokio::test]
    async fn upload_file_streamed_uses_reader_stream() {
        let server = MockServer::start().await;
        let http =
            HttpClient::from_params(HttpClient::builder().base_url(server.uri()).build()).unwrap();
        let session = make_session(http, "sess1");

        Mock::given(method("POST"))
            .and(path("/v3/workspaces/ws1/sessions/sess1/messages/upload"))
            .and(body_string_contains("streamed payload"))
            .and(body_string_contains("peer_id"))
            .and(body_string_contains("carol"))
            .respond_with(ResponseTemplate::new(200).set_body_json(upload_response_json()))
            .mount(&server)
            .await;

        let cursor = std::io::Cursor::new(b"streamed payload".to_vec());
        let msgs = session
            .upload_file_streamed("doc.txt", cursor, "text/plain")
            .peer("carol")
            .send()
            .await
            .unwrap();

        assert_eq!(msgs.len(), 1);
    }

    fn peer_json(id: &str) -> serde_json::Value {
        serde_json::json!({
            "id": id,
            "workspace_id": "ws1",
            "created_at": "2025-01-15T10:30:00Z",
            "metadata": {},
            "configuration": {}
        })
    }

    #[tokio::test]
    async fn peers_traverses_all_pages() {
        use wiremock::matchers::query_param;

        let server = MockServer::start().await;
        let http =
            HttpClient::from_params(HttpClient::builder().base_url(server.uri()).build()).unwrap();
        let session = make_session(http, "sess1");

        Mock::given(method("GET"))
            .and(path("/v3/workspaces/ws1/sessions/sess1/peers"))
            .and(query_param("page", "1"))
            .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
                "items": [peer_json("alice")],
                "total": 2,
                "page": 1,
                "size": 1,
                "pages": 2
            })))
            .mount(&server)
            .await;

        Mock::given(method("GET"))
            .and(path("/v3/workspaces/ws1/sessions/sess1/peers"))
            .and(query_param("page", "2"))
            .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
                "items": [peer_json("bob")],
                "total": 2,
                "page": 2,
                "size": 1,
                "pages": 2
            })))
            .mount(&server)
            .await;

        let peers = session.peers().await.unwrap();
        assert_eq!(peers.len(), 2);
        assert_eq!(peers[0].id(), "alice");
        assert_eq!(peers[1].id(), "bob");
    }

    #[tokio::test]
    async fn refresh_uses_get_or_create_and_updates_all_cache_fields() {
        let server = MockServer::start().await;
        let http =
            HttpClient::from_params(HttpClient::builder().base_url(server.uri()).build()).unwrap();
        let session = make_session(http, "sess1");
        assert!(session.is_active());

        // No `GET /sessions/{id}` exists server-side; reads go through the
        // get-or-create `POST /sessions` collection endpoint.
        Mock::given(method("POST"))
            .and(path("/v3/workspaces/ws1/sessions"))
            .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
                "id": "sess1",
                "workspace_id": "ws1",
                "is_active": false,
                "metadata": {"topic": "fresh"},
                "configuration": {},
                "created_at": "2025-01-15T10:30:00Z"
            })))
            .mount(&server)
            .await;

        session.refresh().await.unwrap();
        assert!(!session.is_active());
        assert_eq!(session.metadata().unwrap().get("topic").unwrap(), "fresh");
    }

    // A real get-or-create `POST /sessions` re-creates a missing session (200),
    // so "deleted session" never yields 404 in practice; this just verifies the
    // SDK surfaces a server 404 as `NotFound` rather than swallowing it.
    #[tokio::test]
    async fn refresh_surfaces_server_404_as_not_found() {
        let server = MockServer::start().await;
        let http =
            HttpClient::from_params(HttpClient::builder().base_url(server.uri()).build()).unwrap();
        let session = make_session(http, "sess1");

        Mock::given(method("POST"))
            .and(path("/v3/workspaces/ws1/sessions"))
            .respond_with(ResponseTemplate::new(404))
            .mount(&server)
            .await;

        let err = session.refresh().await.unwrap_err();
        assert_eq!(err.code(), "not_found");
    }

    #[tokio::test]
    async fn set_metadata_keeps_is_active_fresh() {
        let server = MockServer::start().await;
        let http =
            HttpClient::from_params(HttpClient::builder().base_url(server.uri()).build()).unwrap();
        let session = make_session(http, "sess1");
        assert!(session.is_active());

        // The server flips is_active in its PUT response: the single-lock cache
        // must refresh is_active too, not just metadata.
        Mock::given(method("PUT"))
            .and(path("/v3/workspaces/ws1/sessions/sess1"))
            .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
                "id": "sess1",
                "workspace_id": "ws1",
                "is_active": false,
                "metadata": {"updated": true},
                "configuration": {},
                "created_at": "2025-01-15T10:30:00Z"
            })))
            .mount(&server)
            .await;

        let mut meta = HashMap::new();
        meta.insert("updated".to_owned(), serde_json::json!(true));
        session.set_metadata(meta).await.unwrap();

        assert!(!session.is_active());
        assert_eq!(session.metadata().unwrap().get("updated").unwrap(), true);
    }

    #[tokio::test]
    async fn upload_invalid_content_type_returns_validation_error() {
        let server = MockServer::start().await;
        let http =
            HttpClient::from_params(HttpClient::builder().base_url(server.uri()).build()).unwrap();
        let session = make_session(http, "sess1");

        let err = session
            .upload_file(FileSource::bytes("f.txt", b"data", "text/plain\n"))
            .peer("alice")
            .send()
            .await
            .unwrap_err();

        assert_eq!(err.code(), "validation_error");
    }

    #[tokio::test]
    async fn add_peers_duplicate_ids_returns_validation_error() {
        let server = MockServer::start().await;
        let http =
            HttpClient::from_params(HttpClient::builder().base_url(server.uri()).build()).unwrap();
        let session = make_session(http, "sess1");

        let err = session.add_peers(["alice", "alice"]).await.unwrap_err();
        assert_eq!(err.code(), "validation_error");
    }
}