File size: 64,166 Bytes
e5034c3 | 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 | use std::fmt::Display;
use std::ops::Deref;
use derive_more::derive::{Display, From};
use derive_setters::Setters;
use forge_template::Element;
use serde::{Deserialize, Serialize};
use tracing::debug;
use super::{ToolCallFull, ToolResult};
/// Helper function for serde to skip serializing false boolean values
fn is_false(value: &bool) -> bool {
!value
}
use crate::temperature::Temperature;
use crate::top_k::TopK;
use crate::top_p::TopP;
use crate::{
Attachment, AttachmentContent, ConversationId, EventValue, Image, MessagePhase, ModelId,
ReasoningFull, ToolChoice, ToolDefinition, ToolOutput, ToolValue, Usage,
};
/// Response format for structured output
#[derive(Clone, Debug, Default, Deserialize, Serialize, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum ResponseFormat {
/// Plain text response
#[default]
Text,
/// JSON response with schema
JsonSchema(Box<schemars::Schema>),
}
/// Represents a message being sent to the LLM provider
/// NOTE: ToolResults message are part of the larger Request object and not part
/// of the message.
#[derive(Clone, Debug, Deserialize, From, Serialize, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum ContextMessage {
Text(TextMessage),
Tool(ToolResult),
Image(Image),
}
/// Creates a filtered version of ToolOutput that excludes base64 images to
/// avoid serializing large image data in the context output
fn filter_base64_images_from_tool_output(output: &ToolOutput) -> ToolOutput {
let filtered_values: Vec<ToolValue> = output
.values
.iter()
.map(|value| match value {
ToolValue::Image(image) => {
// Skip base64 images (URLs that start with "data:")
if image.url().starts_with("data:") {
ToolValue::Text(format!("[base64 image: {}]", image.mime_type()))
} else {
value.clone()
}
}
_ => value.clone(),
})
.collect();
ToolOutput { is_error: output.is_error, values: filtered_values }
}
impl ContextMessage {
pub fn content(&self) -> Option<&str> {
match self {
ContextMessage::Text(text_message) => Some(&text_message.content),
ContextMessage::Tool(_) => None,
ContextMessage::Image(_) => None,
}
}
/// Returns the raw content before template rendering (only for User
/// messages)
pub fn as_value(&self) -> Option<&EventValue> {
match self {
ContextMessage::Text(text_message) => text_message.raw_content.as_ref(),
ContextMessage::Tool(_) => None,
ContextMessage::Image(_) => None,
}
}
/// Estimates the number of tokens in a message using character-based
/// approximation.
/// ref: https://github.com/openai/codex/blob/main/codex-cli/src/utils/approximate-tokens-used.ts
pub fn token_count_approx(&self) -> usize {
let char_count = match self {
ContextMessage::Text(text_message) => {
text_message.content.chars().count()
+ tool_call_content_char_count(text_message)
+ reasoning_content_char_count(text_message)
}
ContextMessage::Tool(tool_result) => tool_result
.output
.values
.iter()
.map(|result| match result {
ToolValue::Text(text) => text.chars().count(),
_ => 0,
})
.sum(),
_ => 0,
};
char_count.div_ceil(4)
}
pub fn to_text(&self) -> String {
match self {
ContextMessage::Text(message) => {
let mut message_element = Element::new("message").attr("role", message.role);
message_element =
message_element.append(Element::new("content").text(&message.content));
if let Some(tool_calls) = &message.tool_calls {
for call in tool_calls {
message_element = message_element.append(
Element::new("forge_tool_call")
.attr("name", &call.name)
.cdata(call.arguments.clone().into_string()),
);
}
}
if let Some(thought_signature) = &message.thought_signature {
message_element = message_element
.append(Element::new("thought_signature").text(thought_signature));
}
if let Some(reasoning_details) = &message.reasoning_details {
for reasoning_detail in reasoning_details {
if let Some(text) = &reasoning_detail.text {
message_element =
message_element.append(Element::new("reasoning_detail").text(text));
}
}
}
message_element.render()
}
ContextMessage::Tool(result) => {
let filtered_output = filter_base64_images_from_tool_output(&result.output);
Element::new("message")
.attr("role", "tool")
.append(
Element::new("forge_tool_result")
.attr("name", &result.name)
.cdata(serde_json::to_string(&filtered_output).unwrap()),
)
.render()
}
ContextMessage::Image(_) => Element::new("image").attr("path", "[base64 URL]").render(),
}
}
pub fn user(content: impl ToString, model: Option<ModelId>) -> Self {
TextMessage {
role: Role::User,
content: content.to_string(),
raw_content: None,
tool_calls: None,
thought_signature: None,
reasoning_details: None,
model,
droppable: false,
phase: None,
}
.into()
}
pub fn system(content: impl ToString) -> Self {
TextMessage {
role: Role::System,
content: content.to_string(),
raw_content: None,
tool_calls: None,
thought_signature: None,
model: None,
reasoning_details: None,
droppable: false,
phase: None,
}
.into()
}
pub fn assistant(
content: impl ToString,
thought_signature: Option<String>,
reasoning_details: Option<Vec<ReasoningFull>>,
tool_calls: Option<Vec<ToolCallFull>>,
) -> Self {
let tool_calls = tool_calls.filter(|calls| !calls.is_empty());
TextMessage {
role: Role::Assistant,
content: content.to_string(),
raw_content: None,
tool_calls,
thought_signature,
reasoning_details,
model: None,
droppable: false,
phase: None,
}
.into()
}
pub fn tool_result(result: ToolResult) -> Self {
Self::Tool(result)
}
pub fn has_role(&self, role: Role) -> bool {
match self {
ContextMessage::Text(message) => message.role == role,
ContextMessage::Tool(_) => false,
ContextMessage::Image(_) => Role::User == role,
}
}
pub fn is_droppable(&self) -> bool {
match self {
ContextMessage::Text(message) => message.droppable,
ContextMessage::Tool(_) => false,
ContextMessage::Image(_) => false,
}
}
pub fn has_tool_result(&self) -> bool {
match self {
ContextMessage::Text(_) => false,
ContextMessage::Tool(_) => true,
ContextMessage::Image(_) => false,
}
}
pub fn has_tool_call(&self) -> bool {
match self {
ContextMessage::Text(message) => message.tool_calls.is_some(),
ContextMessage::Tool(_) => false,
ContextMessage::Image(_) => false,
}
}
pub fn has_reasoning_details(&self) -> bool {
match self {
ContextMessage::Text(message) => message.reasoning_details.is_some(),
ContextMessage::Tool(_) => false,
ContextMessage::Image(_) => false,
}
}
/// Returns the tool result if this message is a Tool variant
pub fn as_tool_result(&self) -> Option<&ToolResult> {
match self {
ContextMessage::Tool(result) => Some(result),
_ => None,
}
}
}
fn tool_call_content_char_count(text_message: &TextMessage) -> usize {
text_message
.tool_calls
.as_ref()
.map(|tool_calls| {
tool_calls
.iter()
.map(|tc| {
tc.arguments.to_owned().into_string().chars().count()
+ tc.name.as_str().chars().count()
})
.sum()
})
.unwrap_or(0)
}
fn reasoning_content_char_count(text_message: &TextMessage) -> usize {
text_message
.reasoning_details
.as_ref()
.map_or(0, |details| {
details
.iter()
.map(|rd| rd.text.as_ref().map_or(0, |text| text.chars().count()))
.sum::<usize>()
})
}
//TODO: Rename to TextMessage
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize, Setters)]
#[setters(strip_option, into)]
#[serde(rename_all = "snake_case")]
pub struct TextMessage {
pub role: Role,
pub content: String,
/// The raw content before any template rendering (only for User messages)
#[serde(default, skip_serializing_if = "Option::is_none")]
pub raw_content: Option<EventValue>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub tool_calls: Option<Vec<ToolCallFull>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub thought_signature: Option<String>,
// note: this used to track model used for this message.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub model: Option<ModelId>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub reasoning_details: Option<Vec<ReasoningFull>>,
/// Indicates whether this message can be dropped during context compaction
#[serde(default, skip_serializing_if = "is_false")]
pub droppable: bool,
/// Phase label for assistant messages (`Commentary` or `FinalAnswer`).
/// Preserved from OpenAI Responses API and replayed back on subsequent
/// requests.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub phase: Option<MessagePhase>,
}
impl TextMessage {
/// Creates a new TextMessage with the given role and content
pub fn new(role: Role, content: impl Into<String>) -> Self {
Self {
role,
content: content.into(),
raw_content: None,
tool_calls: None,
thought_signature: None,
model: None,
reasoning_details: None,
droppable: false,
phase: None,
}
}
pub fn has_role(&self, role: Role) -> bool {
self.role == role
}
pub fn assistant(
content: impl ToString,
reasoning_details: Option<Vec<ReasoningFull>>,
model: Option<ModelId>,
) -> Self {
Self {
role: Role::Assistant,
content: content.to_string(),
raw_content: None,
tool_calls: None,
thought_signature: None,
reasoning_details,
model,
droppable: false,
phase: None,
}
}
}
#[derive(Clone, Copy, Debug, Deserialize, PartialEq, Serialize, Display)]
pub enum Role {
System,
User,
Assistant,
}
#[derive(Clone, Debug, Serialize, Deserialize, Setters, PartialEq)]
#[setters(into, strip_option)]
pub struct MessageEntry {
#[serde(flatten)]
pub message: ContextMessage,
#[serde(skip_serializing_if = "Option::is_none")]
pub usage: Option<Usage>,
}
impl From<ContextMessage> for MessageEntry {
fn from(value: ContextMessage) -> Self {
MessageEntry { message: value, usage: Default::default() }
}
}
impl Deref for MessageEntry {
type Target = ContextMessage;
fn deref(&self) -> &Self::Target {
&self.message
}
}
impl std::ops::DerefMut for MessageEntry {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.message
}
}
/// Represents a request being made to the LLM provider. By default the request
/// is created with assuming the model supports use of external tools.
#[derive(Clone, Debug, Deserialize, Serialize, Setters, Default, PartialEq)]
#[setters(into, strip_option)]
pub struct Context {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub conversation_id: Option<ConversationId>,
/// Indicates who initiated the conversation: "user" or "agent".
/// Used for GitHub Copilot billing optimization.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub initiator: Option<String>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub messages: Vec<MessageEntry>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub tools: Vec<ToolDefinition>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub tool_choice: Option<ToolChoice>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub max_tokens: Option<usize>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub temperature: Option<Temperature>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub top_p: Option<TopP>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub top_k: Option<TopK>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub reasoning: Option<crate::ReasoningConfig>,
/// Controls whether responses should be streamed. When `true`, responses
/// are delivered incrementally as they're generated. When `false`, the
/// complete response is returned at once. Defaults to `true` if not
/// specified.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub stream: Option<bool>,
/// Response format for structured output (JSON schema)
#[serde(default, skip_serializing_if = "Option::is_none")]
pub response_format: Option<ResponseFormat>,
}
impl Context {
pub fn accumulate_usage(&self) -> Option<Usage> {
self.messages
.iter()
.filter_map(|msg| msg.usage.as_ref())
.cloned()
.reduce(|a, b| a.accumulate(&b))
}
pub fn system_prompt(&self) -> Option<&str> {
self.messages
.iter()
.find(|message| message.has_role(Role::System))
.and_then(|msg| msg.content())
}
pub fn add_base64_url(mut self, image: Image) -> Self {
self.messages.push(ContextMessage::Image(image).into());
self
}
pub fn add_tool(mut self, tool: impl Into<ToolDefinition>) -> Self {
let tool: ToolDefinition = tool.into();
self.tools.push(tool);
self
}
pub fn add_message(self, content: impl Into<ContextMessage>) -> Self {
self.add_entry(content.into())
}
pub fn add_entry(mut self, content: impl Into<MessageEntry>) -> Self {
let content = content.into();
self.messages.push(content);
self
}
pub fn add_attachments(self, attachments: Vec<Attachment>, model_id: Option<ModelId>) -> Self {
attachments.into_iter().fold(self, |ctx, attachment| {
ctx.add_message(match attachment.content {
AttachmentContent::Image(image) => ContextMessage::Image(image),
AttachmentContent::FileContent { content, info } => {
let elm = Element::new("file_content")
.attr("path", attachment.path)
.attr("start_line", info.start_line)
.attr("end_line", info.end_line)
.attr("total_lines", info.total_lines)
.cdata(content);
let mut message = TextMessage::new(Role::User, elm.to_string()).droppable(true);
if let Some(model) = model_id.clone() {
message = message.model(model);
}
message.into()
}
AttachmentContent::DirectoryListing { entries } => {
let elm = Element::new("directory_listing")
.attr("path", attachment.path)
.append(entries.into_iter().map(|entry| {
let tag_name = if entry.is_dir { "dir" } else { "file" };
Element::new(tag_name).text(entry.path)
}));
let mut message = TextMessage::new(Role::User, elm.to_string()).droppable(true);
if let Some(model) = model_id.clone() {
message = message.model(model);
}
message.into()
}
})
})
}
pub fn add_tool_results(mut self, results: Vec<ToolResult>) -> Self {
if !results.is_empty() {
debug!(results = ?results, "Adding tool results to context");
self.messages.extend(
results
.into_iter()
.map(ContextMessage::tool_result)
.map(MessageEntry::from),
);
}
self
}
/// Updates the set system message
pub fn set_system_messages<S: Into<String>>(mut self, content: Vec<S>) -> Self {
if self.messages.is_empty() {
for message in content {
self.messages
.push(ContextMessage::system(message.into()).into());
}
self
} else {
// drop all the system messages;
self.messages.retain(|m| !m.has_role(Role::System));
// add the system message at the beginning.
for message in content.into_iter().rev() {
self.messages
.insert(0, ContextMessage::system(message.into()).into());
}
self
}
}
/// Converts the context to textual format
pub fn to_text(&self) -> String {
let mut lines = String::new();
for message in self.messages.iter() {
lines.push_str(&message.to_text());
}
format!("<chat_history>{lines}</chat_history>")
}
/// Will append a message to the context. This method always assumes tools
/// are supported and uses the appropriate format. For models that don't
/// support tools, use the TransformToolCalls transformer to convert the
/// context afterward.
#[allow(clippy::too_many_arguments)]
pub fn append_message(
self,
content: impl ToString,
thought_signature: Option<String>,
reasoning: Option<String>,
reasoning_details: Option<Vec<ReasoningFull>>,
usage: Usage,
tool_records: Vec<(ToolCallFull, ToolResult)>,
phase: Option<MessagePhase>,
) -> Self {
// Convert flat reasoning string to reasoning_details only when no structured
// reasoning_details are present. When reasoning_details already exists it
// already contains the text (with its cryptographic signature), so adding
// another entry from the raw `reasoning` string would produce a duplicate
// thinking block with a null signature, which Anthropic rejects.
let merged_reasoning_details = match (reasoning, reasoning_details) {
(_, Some(details)) => Some(details),
(Some(reasoning_text), None) => Some(vec![ReasoningFull {
text: Some(reasoning_text),
type_of: Some("reasoning.text".to_string()),
..Default::default()
}]),
(None, None) => None,
};
// Adding tool calls
let mut message: MessageEntry = ContextMessage::assistant(
content,
thought_signature,
merged_reasoning_details,
Some(
tool_records
.iter()
.map(|record| record.0.clone())
.collect::<Vec<_>>(),
),
)
.into();
// Set phase on the assistant TextMessage if provided
if let ContextMessage::Text(ref mut text_msg) = message.message {
text_msg.phase = phase;
}
let tool_results = tool_records
.iter()
.map(|record| record.1.clone())
.collect::<Vec<_>>();
self.add_entry(message.usage(usage))
.add_tool_results(tool_results)
}
/// Returns the token count for context
pub fn token_count(&self) -> TokenCount {
let actual = self
.messages
.last()
.as_ref()
.and_then(|u| u.usage)
.map(|u| u.total_tokens)
.unwrap_or_default();
match actual {
TokenCount::Actual(actual) if actual > 0 => TokenCount::Actual(actual),
_ => TokenCount::Approx(self.token_count_approx()),
}
}
pub fn token_count_approx(&self) -> usize {
self.messages
.iter()
.map(|m| m.token_count_approx())
.sum::<usize>()
}
/// Checks if reasoning is enabled by user or not.
pub fn is_reasoning_supported(&self) -> bool {
self.reasoning.as_ref().is_some_and(|reasoning| {
// `Effort::None` is a strong opt-out that wins over `enabled` and
// `max_tokens`.
if matches!(reasoning.effort, Some(crate::Effort::None)) {
return false;
}
// When enabled parameter is defined then return it's value directly.
if reasoning.enabled.is_some() {
return reasoning.enabled.unwrap_or_default();
}
// If not defined (None), check other parameters
reasoning.effort.is_some() || reasoning.max_tokens.is_some_and(|token| token > 0)
})
}
/// Returns a vector of user messages, selecting the first message from
/// each consecutive sequence of user messages.
pub fn first_user_messages(&self) -> Vec<&ContextMessage> {
if self.messages.is_empty() {
return Vec::new();
}
let mut result = Vec::new();
let mut is_user = false;
for msg in &self.messages {
if msg.has_role(Role::User) {
// Only add the first message of each consecutive user sequence
if !is_user {
result.push(&**msg);
is_user = true;
}
} else {
is_user = false;
}
}
result
}
/// Returns the total number of messages in the context
pub fn total_messages(&self) -> usize {
self.messages.len()
}
/// Returns the count of user messages in the context
pub fn user_message_count(&self) -> usize {
self.messages
.iter()
.filter(|msg| msg.has_role(Role::User))
.count()
}
/// Returns the count of assistant messages in the context
pub fn assistant_message_count(&self) -> usize {
self.messages
.iter()
.filter(|msg| msg.has_role(Role::Assistant))
.count()
}
/// Returns the total count of tool calls across all messages
pub fn tool_call_count(&self) -> usize {
self.messages
.iter()
.filter(|msg| msg.has_tool_call())
.map(|msg| {
if let ContextMessage::Text(text_msg) = &**msg {
text_msg.tool_calls.as_ref().map_or(0, |calls| calls.len())
} else {
0
}
})
.sum()
}
/// Checks if the model has changed from the previous assistant message.
/// Returns true if the previous assistant message has a different model
/// than the provided current_model, or if there is no previous
/// assistant message with a model.
///
/// This is used to determine whether to apply reasoning normalization - we
/// only want to strip reasoning when switching models, not when
/// continuing with the same model.
pub fn has_model_changed(&self, current_model: &ModelId) -> bool {
// Find the last assistant message with a model field
let last_assistant_model = self.messages.iter().rev().find_map(|msg| {
if let ContextMessage::Text(text_msg) = &**msg
&& text_msg.has_role(Role::Assistant)
{
return text_msg.model.as_ref();
}
None
});
// If there's no previous assistant model, consider it as changed
// If there is a previous model, check if it differs from current
match last_assistant_model {
None => true,
Some(prev_model) => prev_model != current_model,
}
}
}
#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub enum TokenCount {
Actual(usize),
Approx(usize),
}
impl Display for TokenCount {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
TokenCount::Actual(count) => write!(f, "{count}"),
TokenCount::Approx(count) => write!(f, "~{count}"),
}
}
}
impl std::ops::Add for TokenCount {
type Output = Self;
fn add(self, other: Self) -> Self::Output {
match (self, other) {
(TokenCount::Actual(a), TokenCount::Actual(b)) => TokenCount::Actual(a + b),
(TokenCount::Approx(a), TokenCount::Approx(b)) => TokenCount::Approx(a + b),
(TokenCount::Actual(a), TokenCount::Approx(b)) => TokenCount::Approx(a + b),
(TokenCount::Approx(a), TokenCount::Actual(b)) => TokenCount::Approx(a + b),
}
}
}
impl Default for TokenCount {
fn default() -> Self {
TokenCount::Actual(0)
}
}
impl TokenCount {
/// Returns the larger of two TokenCount values by their inner count.
/// If both are `Actual`, the result is `Actual`. If either is `Approx`,
/// the result is `Approx`.
pub fn max(self, other: TokenCount) -> TokenCount {
use TokenCount::*;
match (self, other) {
(Actual(a), Actual(b)) => Actual(a.max(b)),
(Actual(a), Approx(b)) => Approx(a.max(b)),
(Approx(a), Actual(b)) => Approx(a.max(b)),
(Approx(a), Approx(b)) => Approx(a.max(b)),
}
}
}
impl Deref for TokenCount {
type Target = usize;
fn deref(&self) -> &Self::Target {
match self {
TokenCount::Actual(i) => i,
TokenCount::Approx(i) => i,
}
}
}
#[cfg(test)]
mod tests {
use insta::assert_yaml_snapshot;
use pretty_assertions::assert_eq;
use super::*;
use crate::transformer::Transformer;
use crate::{DirectoryEntry, FileInfo, estimate_token_count};
#[test]
fn test_override_system_message() {
let request = Context::default()
.add_message(ContextMessage::system("Initial system message"))
.set_system_messages(vec!["Updated system message"]);
assert_eq!(
request.messages[0],
ContextMessage::system("Updated system message").into(),
);
}
#[test]
fn test_set_system_message() {
let request = Context::default().set_system_messages(vec!["A system message"]);
assert_eq!(
request.messages[0],
ContextMessage::system("A system message").into(),
);
}
#[test]
fn test_insert_system_message() {
let model = ModelId::new("test-model");
let request = Context::default()
.add_message(ContextMessage::user("Do something", Some(model)))
.set_system_messages(vec!["A system message"]);
assert_eq!(
request.messages[0],
ContextMessage::system("A system message").into(),
);
}
#[test]
fn test_estimate_token_count() {
// Create a context with some messages
let model = ModelId::new("test-model");
let context = Context::default()
.add_message(ContextMessage::system("System message"))
.add_message(ContextMessage::user("User message", model.into()))
.add_message(ContextMessage::assistant(
"Assistant message",
None,
None,
None,
));
// Get the token count
let token_count = estimate_token_count(context.to_text().len());
// Validate the token count is reasonable
// The exact value will depend on the implementation of estimate_token_count
assert!(token_count > 0, "Token count should be greater than 0");
}
#[test]
fn test_update_image_tool_calls_empty_context() {
let fixture = Context::default();
let mut transformer = crate::transformer::ImageHandling::new();
let actual = transformer.transform(fixture);
assert_yaml_snapshot!(actual);
}
#[test]
fn test_update_image_tool_calls_no_tool_results() {
let fixture = Context::default()
.add_message(ContextMessage::system("System message"))
.add_message(ContextMessage::user("User message", None))
.add_message(ContextMessage::assistant(
"Assistant message",
None,
None,
None,
));
let mut transformer = crate::transformer::ImageHandling::new();
let actual = transformer.transform(fixture);
assert_yaml_snapshot!(actual);
}
#[test]
fn test_update_image_tool_calls_tool_results_no_images() {
let fixture = Context::default()
.add_message(ContextMessage::system("System message"))
.add_tool_results(vec![
ToolResult {
name: crate::ToolName::new("text_tool"),
call_id: Some(crate::ToolCallId::new("call1")),
output: crate::ToolOutput::text("Text output".to_string()),
},
ToolResult {
name: crate::ToolName::new("empty_tool"),
call_id: Some(crate::ToolCallId::new("call2")),
output: crate::ToolOutput {
values: vec![crate::ToolValue::Empty],
is_error: false,
},
},
]);
let mut transformer = crate::transformer::ImageHandling::new();
let actual = transformer.transform(fixture);
assert_yaml_snapshot!(actual);
}
#[test]
fn test_update_image_tool_calls_single_image() {
let image = Image::new_base64("test123".to_string(), "image/png");
let fixture = Context::default()
.add_message(ContextMessage::system("System message"))
.add_tool_results(vec![ToolResult {
name: crate::ToolName::new("image_tool"),
call_id: Some(crate::ToolCallId::new("call1")),
output: crate::ToolOutput::image(image),
}]);
let mut transformer = crate::transformer::ImageHandling::new();
let actual = transformer.transform(fixture);
assert_yaml_snapshot!(actual);
}
#[test]
fn test_update_image_tool_calls_multiple_images_single_tool_result() {
let image1 = Image::new_base64("test123".to_string(), "image/png");
let image2 = Image::new_base64("test456".to_string(), "image/jpeg");
let fixture = Context::default().add_tool_results(vec![ToolResult {
name: crate::ToolName::new("multi_image_tool"),
call_id: Some(crate::ToolCallId::new("call1")),
output: crate::ToolOutput {
values: vec![
crate::ToolValue::Text("First text".to_string()),
crate::ToolValue::Image(image1),
crate::ToolValue::Text("Second text".to_string()),
crate::ToolValue::Image(image2),
],
is_error: false,
},
}]);
let mut transformer = crate::transformer::ImageHandling::new();
let actual = transformer.transform(fixture);
assert_yaml_snapshot!(actual);
}
#[test]
fn test_update_image_tool_calls_multiple_tool_results_with_images() {
let image1 = Image::new_base64("test123".to_string(), "image/png");
let image2 = Image::new_base64("test456".to_string(), "image/jpeg");
let fixture = Context::default()
.add_message(ContextMessage::system("System message"))
.add_tool_results(vec![
ToolResult {
name: crate::ToolName::new("text_tool"),
call_id: Some(crate::ToolCallId::new("call1")),
output: crate::ToolOutput::text("Text output".to_string()),
},
ToolResult {
name: crate::ToolName::new("image_tool1"),
call_id: Some(crate::ToolCallId::new("call2")),
output: crate::ToolOutput::image(image1),
},
ToolResult {
name: crate::ToolName::new("image_tool2"),
call_id: Some(crate::ToolCallId::new("call3")),
output: crate::ToolOutput::image(image2),
},
]);
let mut transformer = crate::transformer::ImageHandling::new();
let actual = transformer.transform(fixture);
assert_yaml_snapshot!(actual);
}
#[test]
fn test_update_image_tool_calls_mixed_content_with_images() {
let image = Image::new_base64("test123".to_string(), "image/png");
let fixture = Context::default()
.add_message(ContextMessage::system("System message"))
.add_message(ContextMessage::user("User question", None))
.add_message(ContextMessage::assistant(
"Assistant response",
None,
None,
None,
))
.add_tool_results(vec![ToolResult {
name: crate::ToolName::new("mixed_tool"),
call_id: Some(crate::ToolCallId::new("call1")),
output: crate::ToolOutput {
values: vec![
crate::ToolValue::Text("Before image".to_string()),
crate::ToolValue::Image(image),
crate::ToolValue::Text("After image".to_string()),
crate::ToolValue::Empty,
],
is_error: false,
},
}]);
let mut transformer = crate::transformer::ImageHandling::new();
let actual = transformer.transform(fixture);
assert_yaml_snapshot!(actual);
}
#[test]
fn test_update_image_tool_calls_preserves_error_flag() {
let image = Image::new_base64("test123".to_string(), "image/png");
let fixture = Context::default().add_tool_results(vec![ToolResult {
name: crate::ToolName::new("error_tool"),
call_id: Some(crate::ToolCallId::new("call1")),
output: crate::ToolOutput {
values: vec![crate::ToolValue::Image(image)],
is_error: true,
},
}]);
let mut transformer = crate::transformer::ImageHandling::new();
let actual = transformer.transform(fixture);
assert_yaml_snapshot!(actual);
}
#[test]
fn test_context_should_return_max_token_count() {
let fixture = Context::default();
let actual = fixture.token_count();
let expected = TokenCount::Approx(0); // Empty context has no tokens
assert_eq!(actual, expected);
// case 2: context with usage - since total_tokens present return that.
let usage = Usage { total_tokens: TokenCount::Actual(100), ..Default::default() };
let mut wrapper = MessageEntry::from(ContextMessage::user("Hello", None));
wrapper.usage = Some(usage);
let fixture = Context::default().messages(vec![wrapper]);
assert_eq!(fixture.token_count(), TokenCount::Actual(100));
// case 3: context with usage - since total_tokens present return that.
let usage = Usage { total_tokens: TokenCount::Actual(80), ..Default::default() };
let mut wrapper = MessageEntry::from(ContextMessage::user("Hello", None));
wrapper.usage = Some(usage);
let fixture = Context::default().messages(vec![wrapper]);
assert_eq!(fixture.token_count(), TokenCount::Actual(80));
// case 4: context with messages - since total_tokens are not present return
// estimate
let fixture = Context::default()
.add_message(ContextMessage::user("Hello", None))
.add_message(ContextMessage::assistant("Hi there!", None, None, None))
.add_message(ContextMessage::assistant(
"How can I help you?",
None,
None,
None,
))
.add_message(ContextMessage::user("I'm looking for a restaurant.", None));
assert_eq!(fixture.token_count(), TokenCount::Approx(18));
}
#[test]
fn test_context_token_count_uses_last_message_usage() {
// Setup: Create multiple messages with different usage values
let first_usage = Usage { total_tokens: TokenCount::Actual(100), ..Default::default() };
let mut first_message = MessageEntry::from(ContextMessage::user("First message", None));
first_message.usage = Some(first_usage);
let second_usage = Usage { total_tokens: TokenCount::Actual(200), ..Default::default() };
let mut second_message = MessageEntry::from(ContextMessage::assistant(
"Second message",
None,
None,
None,
));
second_message.usage = Some(second_usage);
let third_usage = Usage { total_tokens: TokenCount::Actual(300), ..Default::default() };
let mut third_message = MessageEntry::from(ContextMessage::user("Third message", None));
third_message.usage = Some(third_usage);
// Execute: Create context with all three messages
let fixture =
Context::default().messages(vec![first_message, second_message, third_message]);
let actual = fixture.token_count();
// Expected: Should use the LAST message's usage (300), not the first (100) or
// second (200)
let expected = TokenCount::Actual(300);
assert_eq!(actual, expected);
}
#[test]
fn test_context_is_reasoning_supported_when_enabled() {
let fixture = Context::default()
.reasoning(crate::ReasoningConfig { enabled: Some(true), ..Default::default() });
let actual = fixture.is_reasoning_supported();
let expected = true;
assert_eq!(actual, expected);
}
#[test]
fn test_context_is_reasoning_supported_when_effort_set() {
let fixture = Context::default().reasoning(crate::ReasoningConfig {
effort: Some(crate::Effort::High),
..Default::default()
});
let actual = fixture.is_reasoning_supported();
let expected = true;
assert_eq!(actual, expected);
}
#[test]
fn test_context_is_reasoning_supported_when_max_tokens_positive() {
let fixture = Context::default()
.reasoning(crate::ReasoningConfig { max_tokens: Some(1024), ..Default::default() });
let actual = fixture.is_reasoning_supported();
let expected = true;
assert_eq!(actual, expected);
}
#[test]
fn test_context_is_reasoning_not_supported_when_max_tokens_zero() {
let fixture = Context::default()
.reasoning(crate::ReasoningConfig { max_tokens: Some(0), ..Default::default() });
let actual = fixture.is_reasoning_supported();
let expected = false;
assert_eq!(actual, expected);
}
#[test]
fn test_context_is_reasoning_not_supported_when_disabled() {
let fixture = Context::default()
.reasoning(crate::ReasoningConfig { enabled: Some(false), ..Default::default() });
let actual = fixture.is_reasoning_supported();
let expected = false;
assert_eq!(actual, expected);
}
#[test]
fn test_context_is_reasoning_not_supported_when_no_config() {
let fixture = Context::default();
let actual = fixture.is_reasoning_supported();
let expected = false;
assert_eq!(actual, expected);
}
#[test]
fn test_context_is_reasoning_not_supported_when_explicitly_disabled() {
let fixture = Context::default().reasoning(crate::ReasoningConfig {
enabled: Some(false),
effort: Some(crate::Effort::High), /* Should be ignored when
* explicitly disabled */
..Default::default()
});
let actual = fixture.is_reasoning_supported();
let expected = false;
assert_eq!(
actual, expected,
"Should not be supported when explicitly disabled, even with effort set"
);
}
#[test]
fn test_context_is_reasoning_not_supported_when_effort_is_none() {
// `Effort::None` is documented as "skips the thinking step entirely" and
// must act as an explicit opt-out regardless of other fields.
let fixture = Context::default().reasoning(crate::ReasoningConfig {
effort: Some(crate::Effort::None),
..Default::default()
});
let actual = fixture.is_reasoning_supported();
assert!(!actual);
}
#[test]
fn test_context_is_reasoning_not_supported_when_effort_none_overrides_enabled_true() {
let fixture = Context::default().reasoning(crate::ReasoningConfig {
enabled: Some(true),
effort: Some(crate::Effort::None),
max_tokens: Some(8000),
..Default::default()
});
let actual = fixture.is_reasoning_supported();
assert!(
!actual,
"Effort::None must win over enabled: true and max_tokens"
);
}
#[test]
fn test_add_attachments_file_content_is_droppable() {
let fixture_attachments = vec![Attachment {
path: "/path/to/file.rs".to_string(),
content: AttachmentContent::FileContent {
content: "fn main() {}\n".to_string(),
info: FileInfo::new(1, 1, 1, "hash".to_string()),
},
}];
let fixture_model = ModelId::new("test-model");
let actual = Context::default().add_attachments(fixture_attachments, Some(fixture_model));
// Verify the message was added
assert_eq!(actual.messages.len(), 1);
// Verify the message is droppable
let message = &actual.messages[0];
assert!(
message.is_droppable(),
"File content attachments should be marked as droppable"
);
// Verify the message is a User message
assert!(message.has_role(Role::User));
}
#[test]
fn test_add_attachments_image_is_not_droppable() {
let fixture_image = Image::new_base64("base64data".to_string(), "image/png");
let fixture_attachments = vec![Attachment {
path: "image.png".to_string(),
content: AttachmentContent::Image(fixture_image),
}];
let actual = Context::default().add_attachments(fixture_attachments, None);
// Verify the message was added
assert_eq!(actual.messages.len(), 1);
// Verify the image message is NOT droppable (images use different
// ContextMessage variant)
let message = &actual.messages[0];
assert!(
!message.is_droppable(),
"Image attachments should not be marked as droppable"
);
}
#[test]
fn test_add_attachments_multiple_file_contents_all_droppable() {
let fixture_attachments = vec![
Attachment {
path: "/path/to/file1.rs".to_string(),
content: AttachmentContent::FileContent {
content: "fn foo() {}\n".to_string(),
info: FileInfo::new(1, 1, 1, "hash1".to_string()),
},
},
Attachment {
path: "/path/to/file2.rs".to_string(),
content: AttachmentContent::FileContent {
content: "fn bar() {}\n".to_string(),
info: FileInfo::new(1, 1, 1, "hash2".to_string()),
},
},
];
let actual = Context::default().add_attachments(fixture_attachments, None);
// Verify both messages were added
assert_eq!(actual.messages.len(), 2);
// Verify all file content messages are droppable
for message in &actual.messages {
assert!(
message.is_droppable(),
"All file content attachments should be marked as droppable"
);
}
}
#[test]
fn test_add_attachments_directory_listing() {
let fixture_attachments = vec![Attachment {
path: "/test/mydir".to_string(),
content: AttachmentContent::DirectoryListing {
entries: vec![
DirectoryEntry { path: "/test/mydir/file1.txt".to_string(), is_dir: false },
DirectoryEntry { path: "/test/mydir/file2.rs".to_string(), is_dir: false },
DirectoryEntry { path: "/test/mydir/subdir".to_string(), is_dir: true },
],
},
}];
let actual = Context::default().add_attachments(fixture_attachments, None);
// Verify message was added
assert_eq!(actual.messages.len(), 1);
// Verify directory listing is formatted correctly as XML
let message = actual.messages.first().unwrap();
assert!(
message.is_droppable(),
"Directory listing should be marked as droppable"
);
let text = message.to_text();
// The XML is encoded within the message content
assert!(text.contains("<directory_listing"));
// Check that files use <file> tag
assert!(text.contains("<file>"));
// Check that directories use <dir> tag
assert!(text.contains("<dir>"));
}
#[test]
fn test_context_message_statistics() {
let fixture = Context::default()
.add_message(ContextMessage::system("System message"))
.add_message(ContextMessage::user("User message 1", None))
.add_message(ContextMessage::assistant(
"Assistant response",
None,
None,
None,
))
.add_message(ContextMessage::user("User message 2", None))
.add_message(ContextMessage::assistant(
"Assistant with tool",
None,
None,
Some(vec![
ToolCallFull {
call_id: Some(crate::ToolCallId::new("call1")),
name: crate::ToolName::new("tool1"),
arguments: serde_json::json!({"arg": "value"}).into(),
thought_signature: None,
},
ToolCallFull {
call_id: Some(crate::ToolCallId::new("call2")),
name: crate::ToolName::new("tool2"),
arguments: serde_json::json!({"arg": "value"}).into(),
thought_signature: None,
},
]),
))
.add_tool_results(vec![
ToolResult {
name: crate::ToolName::new("tool1"),
call_id: Some(crate::ToolCallId::new("call1")),
output: crate::ToolOutput::text("Result 1".to_string()),
},
ToolResult {
name: crate::ToolName::new("tool2"),
call_id: Some(crate::ToolCallId::new("call2")),
output: crate::ToolOutput::text("Result 2".to_string()),
},
]);
// Test total messages (6 messages: 1 system + 2 user + 2 assistant + 2 tool
// results)
assert_eq!(fixture.total_messages(), 7);
// Test user message count
assert_eq!(fixture.user_message_count(), 2);
// Test assistant message count
assert_eq!(fixture.assistant_message_count(), 2);
// Test tool call count (2 tool calls in the second assistant message)
assert_eq!(fixture.tool_call_count(), 2);
}
#[test]
fn test_directory_listing_sorted_dirs_first() {
// Create entries already sorted (as they would come from attachment service)
// Directories first, then files, all sorted alphabetically
let fixture_attachments = vec![Attachment {
path: "/test/root".to_string(),
content: AttachmentContent::DirectoryListing {
entries: vec![
DirectoryEntry { path: "apple_dir".to_string(), is_dir: true },
DirectoryEntry { path: "berry_dir".to_string(), is_dir: true },
DirectoryEntry { path: "zoo_dir".to_string(), is_dir: true },
DirectoryEntry { path: "banana.txt".to_string(), is_dir: false },
DirectoryEntry { path: "cherry.txt".to_string(), is_dir: false },
DirectoryEntry { path: "zebra.txt".to_string(), is_dir: false },
],
},
}];
let actual = Context::default().add_attachments(fixture_attachments, None);
let text = actual.messages.first().unwrap().to_text();
// Extract the order of entries from the XML
let dir_entries: Vec<&str> = text
.split("<")
.filter(|s| s.starts_with("dir>") || s.starts_with("file>"))
.collect();
// Verify directories come first, then files, all sorted alphabetically
let expected_order = [
"dir>apple_dir",
"dir>berry_dir",
"dir>zoo_dir",
"file>banana.txt",
"file>cherry.txt",
"file>zebra.txt",
];
for (i, expected) in expected_order.iter().enumerate() {
assert!(
dir_entries[i].starts_with(expected),
"Expected entry {} to start with '{}', but got '{}'",
i,
expected,
dir_entries[i]
);
}
}
#[test]
fn test_context_message_token_count_approx_user_text() {
// Fixture: User text message with 40 characters (10 tokens)
let fixture = ContextMessage::user("This is a test message with content", None);
let actual = fixture.token_count_approx();
let expected = 9; // 36 chars / 4 = 9 tokens
assert_eq!(actual, expected);
}
#[test]
fn test_context_message_token_count_approx_assistant_text() {
// Fixture: Assistant text message
let fixture =
ContextMessage::assistant("Hello! How can I help you today?", None, None, None);
let actual = fixture.token_count_approx();
let expected = 8; // 32 chars / 4 = 8 tokens
assert_eq!(actual, expected);
}
#[test]
fn test_context_message_token_count_approx_system() {
// Fixture: System message should now be counted in token approximation
let fixture = ContextMessage::system("System instructions here");
let actual = fixture.token_count_approx();
let expected = 6; // System messages are now counted in the approximation
assert_eq!(actual, expected);
}
#[test]
fn test_context_message_token_count_approx_with_tool_calls() {
// Fixture: Assistant message with tool calls
let fixture_tool_calls = vec![
ToolCallFull {
call_id: Some(crate::ToolCallId::new("call1")),
name: crate::ToolName::new("fs_search"),
arguments: serde_json::json!({"query": "test"}).into(),
thought_signature: None,
},
ToolCallFull {
call_id: Some(crate::ToolCallId::new("call2")),
name: crate::ToolName::new("calculate"),
arguments: serde_json::json!({"expression": "2+2"}).into(),
thought_signature: None,
},
];
let fixture =
ContextMessage::assistant("Let me help", None, None, Some(fixture_tool_calls));
let actual = fixture.token_count_approx();
// Content: "Let me help" = 11 chars
// Tool call 1: "fs_search" (9 chars) + {"query":"test"} (16 chars) = 25 chars
// Tool call 2: "calculate" (9 chars) + {"expression":"2+2"} (20 chars) = 29
// chars Total: 11 + 25 + 29 = 65 chars / 4 = 17 tokens
let expected = 17;
assert_eq!(actual, expected);
}
#[test]
fn test_context_message_token_count_approx_with_reasoning() {
// Fixture: Assistant message with reasoning details
let fixture_reasoning = vec![
ReasoningFull {
text: Some("First reasoning step".to_string()),
..Default::default()
},
ReasoningFull {
text: Some("Second reasoning step".to_string()),
..Default::default()
},
];
let fixture =
ContextMessage::assistant("Final answer", None, Some(fixture_reasoning), None);
let actual = fixture.token_count_approx();
// Content: "Final answer" = 12 chars = 3 tokens
// Reasoning 1: "First reasoning step" = 20 chars = 5 tokens
// Reasoning 2: "Second reasoning step" = 21 chars = 6 tokens
// Total: 3 + 5 + 6 = 14 tokens
let expected = 14;
assert_eq!(actual, expected);
}
#[test]
fn test_context_message_token_count_approx_tool_result_text() {
// Fixture: Tool result with text output
let fixture = ContextMessage::tool_result(ToolResult {
name: crate::ToolName::new("fs_search"),
call_id: Some(crate::ToolCallId::new("call1")),
output: crate::ToolOutput::text("Search results: Found 3 items".to_string()),
});
let actual = fixture.token_count_approx();
let expected = 8; // 30 chars / 4 = 8 tokens (rounded up)
assert_eq!(actual, expected);
}
#[test]
fn test_context_message_token_count_approx_tool_result_image() {
// Fixture: Tool result with image (images are not counted)
let fixture_image = Image::new_base64("base64data".to_string(), "image/png");
let fixture = ContextMessage::tool_result(ToolResult {
name: crate::ToolName::new("screenshot"),
call_id: Some(crate::ToolCallId::new("call1")),
output: crate::ToolOutput::image(fixture_image),
});
let actual = fixture.token_count_approx();
let expected = 0; // Images are not counted in token approximation
assert_eq!(actual, expected);
}
#[test]
fn test_context_message_token_count_approx_image() {
// Fixture: Image message
let fixture_image = Image::new_base64("imagedata".to_string(), "image/jpeg");
let fixture = ContextMessage::Image(fixture_image);
let actual = fixture.token_count_approx();
let expected = 0; // Image messages return 0 tokens
assert_eq!(actual, expected);
}
#[test]
fn test_context_message_token_count_approx_empty_content() {
// Fixture: Empty message
let fixture = ContextMessage::user("", None);
let actual = fixture.token_count_approx();
let expected = 0; // 0 chars / 4 = 0 tokens
assert_eq!(actual, expected);
}
#[test]
fn test_context_message_token_count_approx_unicode() {
// Fixture: Message with Unicode characters
let fixture = ContextMessage::user("Hello 世界 🌍 émojis", None);
let actual = fixture.token_count_approx();
// "Hello 世界 🌍 émojis" has 18 Unicode characters
let expected = 5; // 18 chars / 4 = 5 tokens (rounded up)
assert_eq!(actual, expected);
}
#[test]
fn test_has_model_changed_returns_true_when_no_previous_messages() {
let fixture = Context::default();
let current_model = ModelId::new("gpt-4");
let actual = fixture.has_model_changed(¤t_model);
let expected = true;
assert_eq!(actual, expected);
}
#[test]
fn test_has_model_changed_returns_true_when_model_differs() {
let fixture = Context::default()
.add_message(TextMessage::new(Role::Assistant, "Hello").model(ModelId::new("gpt-3.5")));
let current_model = ModelId::new("gpt-4");
let actual = fixture.has_model_changed(¤t_model);
let expected = true;
assert_eq!(actual, expected);
}
#[test]
fn test_has_model_changed_returns_false_when_model_same() {
let fixture = Context::default()
.add_message(TextMessage::new(Role::Assistant, "Hello").model(ModelId::new("gpt-4")));
let current_model = ModelId::new("gpt-4");
let actual = fixture.has_model_changed(¤t_model);
let expected = false;
assert_eq!(actual, expected);
}
#[test]
fn test_has_model_changed_returns_true_when_previous_has_no_model() {
let fixture = Context::default().add_message(TextMessage::new(Role::Assistant, "Hello")); // No model set
let current_model = ModelId::new("gpt-4");
let actual = fixture.has_model_changed(¤t_model);
let expected = true;
assert_eq!(actual, expected);
}
#[test]
fn test_has_model_changed_checks_last_assistant_message_with_model() {
let fixture = Context::default()
.add_message(TextMessage::new(Role::Assistant, "First").model(ModelId::new("gpt-3.5")))
.add_message(TextMessage::new(Role::User, "Question"))
.add_message(TextMessage::new(Role::Assistant, "Second").model(ModelId::new("gpt-4")));
let current_model = ModelId::new("gpt-4");
let actual = fixture.has_model_changed(¤t_model);
let expected = false; // Last assistant message with model is "gpt-4", same as current
assert_eq!(actual, expected);
}
#[test]
fn test_has_model_changed_with_multiple_messages_model_changed() {
let fixture = Context::default()
.add_message(TextMessage::new(Role::Assistant, "First").model(ModelId::new("gpt-3.5")))
.add_message(TextMessage::new(Role::User, "Question"))
.add_message(
TextMessage::new(Role::Assistant, "Second").model(ModelId::new("claude-3")),
);
let current_model = ModelId::new("gpt-4");
let actual = fixture.has_model_changed(¤t_model);
let expected = true; // Last assistant message with model is "claude-3", different from "gpt-4"
assert_eq!(actual, expected);
}
#[test]
fn test_has_model_changed_ignores_user_messages() {
// User messages have model tracking too, but we should only check assistant
// messages
let fixture = Context::default()
.add_message(TextMessage::new(Role::Assistant, "Response").model(ModelId::new("gpt-4")))
.add_message(TextMessage::new(Role::User, "Question").model(ModelId::new("claude-3")));
let current_model = ModelId::new("gpt-4");
let actual = fixture.has_model_changed(¤t_model);
let expected = false; // Last ASSISTANT message is "gpt-4", user message should be ignored
assert_eq!(actual, expected);
}
#[test]
fn test_has_model_changed_continuing_same_model() {
// Scenario: model1 -> model2 -> model2 (the second model2 should not drop
// reasoning)
let fixture = Context::default()
.add_message(TextMessage::new(Role::Assistant, "First").model(ModelId::new("model1")))
.add_message(TextMessage::new(Role::User, "Question"))
.add_message(TextMessage::new(Role::Assistant, "Second").model(ModelId::new("model2")))
.add_message(TextMessage::new(Role::User, "Another question"));
let current_model = ModelId::new("model2");
let actual = fixture.has_model_changed(¤t_model);
let expected = false; // Last assistant used "model2", same as current
assert_eq!(actual, expected);
}
/// Regression test: when both `reasoning` (raw text) and
/// `reasoning_details` (structured, with a cryptographic signature) are
/// present, `append_message` must NOT create a duplicate thinking block
/// with a null signature.
///
/// The Anthropic API rejects messages where any thinking block carries a
/// null or missing signature, so the stored `reasoning_details` must
/// contain exactly the structured entries that were passed in — no
/// extras.
#[test]
fn test_append_message_does_not_duplicate_reasoning_when_details_present() {
// Fixture: a structured reasoning detail with a valid signature, as would
// arrive after aggregating an Anthropic streaming response.
let fixture_details = vec![ReasoningFull {
text: Some("Let me think about this.".to_string()),
signature: Some("EpwFvalidSignatureABC123".to_string()),
type_of: Some("reasoning.text".to_string()),
format: Some("anthropic-claude-v1".to_string()),
index: Some(0),
..Default::default()
}];
// Both reasoning (raw string) and reasoning_details (structured) are provided,
// mirroring what orch.rs passes after collecting a streamed Anthropic response.
let fixture = Context::default().add_message(ContextMessage::user("Hello", None));
let actual = fixture.append_message(
"Answer",
None,
Some("Let me think about this.".to_string()), // raw reasoning string
Some(fixture_details.clone()), // structured reasoning_details
Usage::default(),
vec![],
None,
);
// Extract the stored reasoning_details from the assistant message.
let stored = actual
.messages
.iter()
.find_map(|entry| {
if let ContextMessage::Text(msg) = &**entry
&& msg.role == Role::Assistant
{
return msg.reasoning_details.as_ref();
}
None
})
.expect("Assistant message should have reasoning_details");
// Expected: exactly the one structured entry that was passed in.
// No duplicate null-signature entry should have been appended.
let expected = fixture_details;
assert_eq!(stored, &expected);
}
}
|