Skip to content

Commit e08f5b2

Browse files
committed
core, protocol: rename legacy_events to as_legacy_events on TurnItem, ReasoningItem, AgentMessageItem
Refactor event conversion methods from legacy_events to as_legacy_events for clarity and consistency. Update all call sites and tests accordingly. Adapt AgentMessageItem to return Vec<EventMsg> from as_legacy_events. Update event_mapping tests to directly test parse_turn_item and TurnItem variants.
1 parent a8fa24a commit e08f5b2

File tree

3 files changed

+61
-52
lines changed

3 files changed

+61
-52
lines changed

codex-rs/core/src/codex.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -752,7 +752,7 @@ impl Session {
752752
item: &TurnItem,
753753
emit_raw_agent_reasoning: bool,
754754
) {
755-
for event in item.legacy_events(emit_raw_agent_reasoning) {
755+
for event in item.as_legacy_events(emit_raw_agent_reasoning) {
756756
self.send_event(turn_context, event).await;
757757
}
758758
}

codex-rs/core/src/event_mapping.rs

Lines changed: 50 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ pub(crate) fn map_response_item_to_event_messages(
121121
show_raw_agent_reasoning: bool,
122122
) -> Vec<EventMsg> {
123123
if let Some(turn_item) = parse_turn_item(item) {
124-
return turn_item.legacy_events(show_raw_agent_reasoning);
124+
return turn_item.as_legacy_events(show_raw_agent_reasoning);
125125
}
126126

127127
// Variants that require side effects are handled by higher layers and do not emit events here.
@@ -130,19 +130,18 @@ pub(crate) fn map_response_item_to_event_messages(
130130

131131
#[cfg(test)]
132132
mod tests {
133-
use super::map_response_item_to_event_messages;
134-
use crate::protocol::EventMsg;
135-
use crate::protocol::WebSearchEndEvent;
136-
133+
use super::parse_turn_item;
134+
use codex_protocol::items::TurnItem;
137135
use codex_protocol::models::ContentItem;
138136
use codex_protocol::models::ReasoningItemContent;
139137
use codex_protocol::models::ReasoningItemReasoningSummary;
140138
use codex_protocol::models::ResponseItem;
141139
use codex_protocol::models::WebSearchAction;
140+
use codex_protocol::user_input::UserInput;
142141
use pretty_assertions::assert_eq;
143142

144143
#[test]
145-
fn maps_user_message_with_text_and_two_images() {
144+
fn parses_user_message_with_text_and_two_images() {
146145
let img1 = "https://example.com/one.png".to_string();
147146
let img2 = "https://example.com/two.jpg".to_string();
148147

@@ -162,20 +161,25 @@ mod tests {
162161
],
163162
};
164163

165-
let events = map_response_item_to_event_messages(&item, false);
166-
assert_eq!(events.len(), 1, "expected a single user message event");
164+
let turn_item = parse_turn_item(&item).expect("expected user message turn item");
167165

168-
match &events[0] {
169-
EventMsg::UserMessage(user) => {
170-
assert_eq!(user.message, "Hello world");
171-
assert_eq!(user.images, Some(vec![img1, img2]));
166+
match turn_item {
167+
TurnItem::UserMessage(user) => {
168+
let expected_content = vec![
169+
UserInput::Text {
170+
text: "Hello world".to_string(),
171+
},
172+
UserInput::Image { image_url: img1 },
173+
UserInput::Image { image_url: img2 },
174+
];
175+
assert_eq!(user.content, expected_content);
172176
}
173-
other => panic!("expected UserMessage, got {other:?}"),
177+
other => panic!("expected TurnItem::UserMessage, got {other:?}"),
174178
}
175179
}
176180

177181
#[test]
178-
fn maps_reasoning_summary_without_raw_content() {
182+
fn parses_reasoning_summary_and_raw_content() {
179183
let item = ResponseItem::Reasoning {
180184
id: "reasoning_1".to_string(),
181185
summary: vec![
@@ -192,18 +196,23 @@ mod tests {
192196
encrypted_content: None,
193197
};
194198

195-
let events = map_response_item_to_event_messages(&item, false);
199+
let turn_item = parse_turn_item(&item).expect("expected reasoning turn item");
196200

197-
assert_eq!(events.len(), 2, "expected only reasoning summaries");
198-
assert!(
199-
events
200-
.iter()
201-
.all(|event| matches!(event, EventMsg::AgentReasoning(_)))
202-
);
201+
match turn_item {
202+
TurnItem::Reasoning(reasoning) => {
203+
assert_eq!(
204+
reasoning.summary_text,
205+
vec!["Step 1".to_string(), "Step 2".to_string()]
206+
);
207+
assert_eq!(reasoning.raw_content, vec!["raw details".to_string()]);
208+
assert_eq!(reasoning.encrypted_content, None);
209+
}
210+
other => panic!("expected TurnItem::Reasoning, got {other:?}"),
211+
}
203212
}
204213

205214
#[test]
206-
fn maps_reasoning_including_raw_content_when_enabled() {
215+
fn parses_reasoning_including_raw_content() {
207216
let item = ResponseItem::Reasoning {
208217
id: "reasoning_2".to_string(),
209218
summary: vec![ReasoningItemReasoningSummary::SummaryText {
@@ -220,20 +229,23 @@ mod tests {
220229
encrypted_content: None,
221230
};
222231

223-
let events = map_response_item_to_event_messages(&item, true);
232+
let turn_item = parse_turn_item(&item).expect("expected reasoning turn item");
224233

225-
assert_eq!(
226-
events.len(),
227-
3,
228-
"expected summary and raw reasoning content events"
229-
);
230-
assert!(matches!(events[0], EventMsg::AgentReasoning(_)));
231-
assert!(matches!(events[1], EventMsg::AgentReasoningRawContent(_)));
232-
assert!(matches!(events[2], EventMsg::AgentReasoningRawContent(_)));
234+
match turn_item {
235+
TurnItem::Reasoning(reasoning) => {
236+
assert_eq!(reasoning.summary_text, vec!["Summarized step".to_string()]);
237+
assert_eq!(
238+
reasoning.raw_content,
239+
vec!["raw step".to_string(), "final thought".to_string()]
240+
);
241+
assert_eq!(reasoning.encrypted_content, None);
242+
}
243+
other => panic!("expected TurnItem::Reasoning, got {other:?}"),
244+
}
233245
}
234246

235247
#[test]
236-
fn maps_web_search_call() {
248+
fn parses_web_search_call() {
237249
let item = ResponseItem::WebSearchCall {
238250
id: Some("ws_1".to_string()),
239251
status: Some("completed".to_string()),
@@ -242,15 +254,14 @@ mod tests {
242254
},
243255
};
244256

245-
let events = map_response_item_to_event_messages(&item, false);
246-
assert_eq!(events.len(), 1, "expected a single web search event");
257+
let turn_item = parse_turn_item(&item).expect("expected web search turn item");
247258

248-
match &events[0] {
249-
EventMsg::WebSearchEnd(WebSearchEndEvent { call_id, query }) => {
250-
assert_eq!(call_id, "ws_1");
251-
assert_eq!(query, "weather");
259+
match turn_item {
260+
TurnItem::WebSearch(search) => {
261+
assert_eq!(search.id, "ws_1");
262+
assert_eq!(search.query, "weather");
252263
}
253-
other => panic!("expected WebSearchEnd, got {other:?}"),
264+
other => panic!("expected TurnItem::WebSearch, got {other:?}"),
254265
}
255266
}
256267
}

codex-rs/protocol/src/items.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -96,22 +96,20 @@ impl AgentMessageItem {
9696
}
9797
}
9898

99-
pub fn as_legacy_event(&self) -> EventMsg {
100-
let message = self
101-
.content
99+
pub fn as_legacy_events(&self) -> Vec<EventMsg> {
100+
self.content
102101
.iter()
103102
.map(|c| match c {
104-
AgentMessageContent::Text { text } => text.clone(),
103+
AgentMessageContent::Text { text } => EventMsg::AgentMessage(AgentMessageEvent {
104+
message: text.clone(),
105+
}),
105106
})
106-
.collect::<Vec<String>>()
107-
.join("");
108-
109-
EventMsg::AgentMessage(AgentMessageEvent { message })
107+
.collect()
110108
}
111109
}
112110

113111
impl ReasoningItem {
114-
pub fn legacy_events(&self, show_raw_agent_reasoning: bool) -> Vec<EventMsg> {
112+
pub fn as_legacy_events(&self, show_raw_agent_reasoning: bool) -> Vec<EventMsg> {
115113
let mut events = Vec::new();
116114
for summary in &self.summary_text {
117115
events.push(EventMsg::AgentReasoning(AgentReasoningEvent {
@@ -152,12 +150,12 @@ impl TurnItem {
152150
}
153151
}
154152

155-
pub fn legacy_events(&self, show_raw_agent_reasoning: bool) -> Vec<EventMsg> {
153+
pub fn as_legacy_events(&self, show_raw_agent_reasoning: bool) -> Vec<EventMsg> {
156154
match self {
157155
TurnItem::UserMessage(item) => vec![item.as_legacy_event()],
158-
TurnItem::AgentMessage(item) => vec![item.as_legacy_event()],
156+
TurnItem::AgentMessage(item) => item.as_legacy_events(),
159157
TurnItem::WebSearch(item) => vec![item.as_legacy_event()],
160-
TurnItem::Reasoning(item) => item.legacy_events(show_raw_agent_reasoning),
158+
TurnItem::Reasoning(item) => item.as_legacy_events(show_raw_agent_reasoning),
161159
}
162160
}
163161
}

0 commit comments

Comments
 (0)